Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp1576448pxb; Tue, 8 Feb 2022 22:52:24 -0800 (PST) X-Google-Smtp-Source: ABdhPJx16oGl8lg9ioua02/oU80JyzZ+pQjxpZCd6OxU/nPNTt8Xfb8FZ7dbn6Hs1u6tRzLJT9Vy X-Received: by 2002:a63:87c1:: with SMTP id i184mr811760pge.28.1644389544624; Tue, 08 Feb 2022 22:52:24 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1644389544; cv=none; d=google.com; s=arc-20160816; b=tmqiQzVUAeMoqjuKrk1/EBqTVk2r4iUBClhCJez/r6Bce6cNXCQET3bQwZGOM590Qe M5yX5r8BKX6ZTRk4OkV6RZdOthnhDBxmwVfT/nq+eAICSk3DDLxdkcKM7dOYuzl6lxwI 5cyG6ClOKM3mYm7kvap3fy5lyO5NUxGq9vmJg47Ffm+SRxtR+mPW5lkwiviRm/az52Yt U7x0Yc9JCzc74eu+QhM4iHdoR9N8E1cvJJ0NIFWK+SlXrRmMeNFmItJks6TL4x8odRWf Q2Cg1KJLuLKeluVrlA2XBupSlyFs+O4BXz1TjYZuX/y9NRH9jY2F5h76vaJMi2pPOjtH FCxg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=SMTOaLQ7dmVrFU4YApnbu06iYLDeLwergecv++KC6K8=; b=xBudKxgd7fYJQS5IKieEnqvxJgwRfR6/WxLyan+XBZrnXva192XkGcq3sK17r9qaz3 nC+hSkONs0g+cnZqMppfiR9PsGvyR4XZR+jRL6dEmrfXOcN9TT4XCRsbzKOEjkc8NQvu vzVllKvW8ZfwlhmQNj9jJnZi1GCJU+EENIPDzGR/9piYE3UZcjYPzD/ZUeLxYakiU+GE pHBXcxmPM0NNP+Lmops0pN8ZPE7sc7Q8+zKt4tC5VAt0NENiSedseNxK7Al3zVxjinbb y9eHGSRhEaI1T/ouJUP5oOgy9AwIr86PVkuofQJV6WtcEClI2Yipvn7UzZvGd3SoTq8s tm+Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=mkDTU733; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id n3si13905606pff.351.2022.02.08.22.52.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Feb 2022 22:52:24 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=mkDTU733; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 31C89E05B1D9; Tue, 8 Feb 2022 22:30:45 -0800 (PST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345606AbiBHCJt (ORCPT + 99 others); Mon, 7 Feb 2022 21:09:49 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47792 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345414AbiBHCEu (ORCPT ); Mon, 7 Feb 2022 21:04:50 -0500 Received: from mail-qt1-x833.google.com (mail-qt1-x833.google.com [IPv6:2607:f8b0:4864:20::833]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E32D9C061A73; Mon, 7 Feb 2022 18:04:49 -0800 (PST) Received: by mail-qt1-x833.google.com with SMTP id l14so1357579qtp.7; Mon, 07 Feb 2022 18:04:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=SMTOaLQ7dmVrFU4YApnbu06iYLDeLwergecv++KC6K8=; b=mkDTU733eZDe3IQJBWlpy6D5azVFgSki1MdO1iVCNFFMhN8SGvlP5ph63WRire/zic xU2WRYGO8jQaBRGMniWFW1b0gQIM7Pzqws9PWBiiyO3BSjy27WHBA3USSLxteKiwjUi1 DwcIDArU+phX9TjUmrPEvtEuoldrBlO/Xxy+YasqXqJMrmiJjd7KFRVLnwkN4YuLLaEx 28AsKPMv2QECBGXCoYV9cpwumTdFNiBEMliUCiTcNhv2ws2FHG+lIuVGmkvXDsLkDDcQ /dnRZ5ieirGi317Rdd0hw75UsTdEtO96k+ZjEAxSRbihxYqXGNO07rv2A4nQrwV0ioPk eSIw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=SMTOaLQ7dmVrFU4YApnbu06iYLDeLwergecv++KC6K8=; b=hkjcyMaA6mXaQjjVouq2WFutdrr4xngW2NNyyYpwN9KJgM5WCzVjyu3IskvuK2AwG6 y4nxUiOVI8PQ22dsVaKlXYqcpIhqpd5tqN6xbbTLjFK0RVnib47jdA4rg/XpTWgxAEji Gk1pNqrwXqLiXo1AsOVqOJVOob6qoiFFyECB2SnWqKZ/pwk+7pYBfEaZuZ2iLTLQMl9f 83yJklFWrszzVfbspLLJYR4vurrSuNCJkwRBT2gJFoUS8EuYtg7YIafU7+AICTi8xKwC 3yL0WydrAj96aW/HUIYCl2V7j4j8hws0XCxTrq7kXf7WWCMLkHuObmgVnjOin+JKQXv/ U2mg== X-Gm-Message-State: AOAM533waDDVF8kG4G86r2Q4e/h73+zdB89ANjByC4DLZu/8hVv8CYEN 575ob1mpWkK8+v8rtIvEdZ0= X-Received: by 2002:a05:622a:4d:: with SMTP id y13mr1682304qtw.629.1644285889048; Mon, 07 Feb 2022 18:04:49 -0800 (PST) Received: from shaak.. (modemcable055.92-163-184.mc.videotron.ca. [184.163.92.55]) by smtp.gmail.com with ESMTPSA id u9sm1416055qko.130.2022.02.07.18.04.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Feb 2022 18:04:48 -0800 (PST) From: Liam Beguin To: liambeguin@gmail.com, peda@axentia.se, jic23@kernel.org, andy.shevchenko@gmail.com, lars@metafoo.de Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, robh+dt@kernel.org Subject: [PATCH v14 01/11] iio: afe: rescale: expose scale processing function Date: Mon, 7 Feb 2022 21:04:31 -0500 Message-Id: <20220208020441.3081162-2-liambeguin@gmail.com> X-Mailer: git-send-email 2.35.1.4.g5d01301f2b86 In-Reply-To: <20220208020441.3081162-1-liambeguin@gmail.com> References: <20220208020441.3081162-1-liambeguin@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.5 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI, SPF_HELO_PASS,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org In preparation for the addition of kunit tests, expose the logic responsible for combining channel scales. Signed-off-by: Liam Beguin Reviewed-by: Peter Rosin --- drivers/iio/afe/iio-rescale.c | 65 ++++++++++++++------------------- include/linux/iio/afe/rescale.h | 32 ++++++++++++++++ 2 files changed, 60 insertions(+), 37 deletions(-) create mode 100644 include/linux/iio/afe/rescale.h diff --git a/drivers/iio/afe/iio-rescale.c b/drivers/iio/afe/iio-rescale.c index 69710c481376..65832dd09249 100644 --- a/drivers/iio/afe/iio-rescale.c +++ b/drivers/iio/afe/iio-rescale.c @@ -15,32 +15,43 @@ #include #include +#include #include #include -struct rescale; - -struct rescale_cfg { - enum iio_chan_type type; - int (*props)(struct device *dev, struct rescale *rescale); -}; +int rescale_process_scale(struct rescale *rescale, int scale_type, + int *val, int *val2) +{ + s64 tmp; -struct rescale { - const struct rescale_cfg *cfg; - struct iio_channel *source; - struct iio_chan_spec chan; - struct iio_chan_spec_ext_info *ext_info; - bool chan_processed; - s32 numerator; - s32 denominator; -}; + switch (scale_type) { + case IIO_VAL_FRACTIONAL: + *val *= rescale->numerator; + *val2 *= rescale->denominator; + return scale_type; + case IIO_VAL_INT: + *val *= rescale->numerator; + if (rescale->denominator == 1) + return scale_type; + *val2 = rescale->denominator; + return IIO_VAL_FRACTIONAL; + case IIO_VAL_FRACTIONAL_LOG2: + tmp = (s64)*val * 1000000000LL; + tmp = div_s64(tmp, rescale->denominator); + tmp *= rescale->numerator; + tmp = div_s64(tmp, 1000000000LL); + *val = tmp; + return scale_type; + default: + return -EOPNOTSUPP; + } +} static int rescale_read_raw(struct iio_dev *indio_dev, struct iio_chan_spec const *chan, int *val, int *val2, long mask) { struct rescale *rescale = iio_priv(indio_dev); - s64 tmp; int ret; switch (mask) { @@ -66,27 +77,7 @@ static int rescale_read_raw(struct iio_dev *indio_dev, } else { ret = iio_read_channel_scale(rescale->source, val, val2); } - switch (ret) { - case IIO_VAL_FRACTIONAL: - *val *= rescale->numerator; - *val2 *= rescale->denominator; - return ret; - case IIO_VAL_INT: - *val *= rescale->numerator; - if (rescale->denominator == 1) - return ret; - *val2 = rescale->denominator; - return IIO_VAL_FRACTIONAL; - case IIO_VAL_FRACTIONAL_LOG2: - tmp = (s64)*val * 1000000000LL; - tmp = div_s64(tmp, rescale->denominator); - tmp *= rescale->numerator; - tmp = div_s64(tmp, 1000000000LL); - *val = tmp; - return ret; - default: - return -EOPNOTSUPP; - } + return rescale_process_scale(rescale, ret, val, val2); default: return -EINVAL; } diff --git a/include/linux/iio/afe/rescale.h b/include/linux/iio/afe/rescale.h new file mode 100644 index 000000000000..8a2eb34af327 --- /dev/null +++ b/include/linux/iio/afe/rescale.h @@ -0,0 +1,32 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Copyright (C) 2018 Axentia Technologies AB + */ + +#ifndef __IIO_RESCALE_H__ +#define __IIO_RESCALE_H__ + +#include +#include + +struct device; +struct rescale; + +struct rescale_cfg { + enum iio_chan_type type; + int (*props)(struct device *dev, struct rescale *rescale); +}; + +struct rescale { + const struct rescale_cfg *cfg; + struct iio_channel *source; + struct iio_chan_spec chan; + struct iio_chan_spec_ext_info *ext_info; + bool chan_processed; + s32 numerator; + s32 denominator; +}; + +int rescale_process_scale(struct rescale *rescale, int scale_type, + int *val, int *val2); +#endif /* __IIO_RESCALE_H__ */ -- 2.35.1.4.g5d01301f2b86