Received: by 2002:a05:6a10:c7c6:0:0:0:0 with SMTP id h6csp1101959pxy; Sun, 1 Aug 2021 12:41:36 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy/EGT8bqAkYGdOXrt3xebkpyJPbLfhS8I5JZ6taVKegRXNpcROkn9GDJYI0SjiMZm86/io X-Received: by 2002:a02:a999:: with SMTP id q25mr11634259jam.16.1627846896539; Sun, 01 Aug 2021 12:41:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1627846896; cv=none; d=google.com; s=arc-20160816; b=YYXAohqNJyRF27VQ9lHII4r8Nh4FTWlyck4oP0GcszLt78lhoCxO0EGVUYCXBbp+Td uwW8DcdPOkHKcdOiazLTmplL/oTodPcFs+qaFUUdX2zHji5v+OFvQ9U4cW/A6pi4EhMx f+3ynVEFuKQrv7MwowLtt66twpGaiNYqLdULsPw7PEC+VQLoLRzaUug6SwuTa2kc76ZU 7xhRXHJUIqlSijpUTW6A3yDmB3WqlKIRuv+gVffr0oqNmINPgT21NlZOFdEab+pQbnqk BMF6LWBoECWo/yU+y1sKxosw884p4hj5TZ1xprq4zYHPnbOQIxWqDOKTRCe0xnm5ad41 vekg== 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=cUoCjxVUE34s1+eP3pk70mRzAMWsM0v+FDRsaXLy0ew=; b=FJzJ6bSYt6A3R/MgFOo7jPiavwQyIZeyCIbMY+ePo1uFUl5EgK8PnOMAj6pbKUSYog pidpbZKqeYgAClrEcc+wSBd3W65w8QEZKnPwl6uKgCNkwnb1APc8VI/cBoZRnoMeuy6J 4NVyFlepDot+XLIY1kNnWHY5hYrAP2LVye0oc1dCRSJhtq3yNfOO7wqotD6AL8B9Jxkr 5CpiRnIj8Ra63D0w11Ms+R9RC0eB8Pc4vro1fhXv0d+4C/LU3WHN0En7z5Qj7jmONrp+ ZhUmyEhhds5/A2CuAwivIKdOiHSMfeLjSI4tZVlglzQ6nJBGEEISYM0r1eV+iyqkwvQS t5lw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b="QDDOb/9O"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.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 vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id d11si11420979ilg.24.2021.08.01.12.41.25; Sun, 01 Aug 2021 12:41:36 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b="QDDOb/9O"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.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: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231502AbhHATk3 (ORCPT + 99 others); Sun, 1 Aug 2021 15:40:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34916 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230186AbhHATkX (ORCPT ); Sun, 1 Aug 2021 15:40:23 -0400 Received: from mail-qk1-x72d.google.com (mail-qk1-x72d.google.com [IPv6:2607:f8b0:4864:20::72d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4EB5AC0613D3; Sun, 1 Aug 2021 12:40:15 -0700 (PDT) Received: by mail-qk1-x72d.google.com with SMTP id t66so14809365qkb.0; Sun, 01 Aug 2021 12:40:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=cUoCjxVUE34s1+eP3pk70mRzAMWsM0v+FDRsaXLy0ew=; b=QDDOb/9OIR5QbnjGLnasWpaABwJPXE0AHCUYUfFL+4gH6ZAkzkWqYQeZlasfXUww2T 1V88mA9k579I96q7Py7KxCHOqbkSf11hYsJAic42zRSqFyDVxkeFVkQQbKuP928/JzJE mH9E21tp61MfIrK4Tv+EAo0eB6rcXT+TWEf4cADcqEZTb1C5VqG0gBzMSIiTSc3W3mVq nhD0r31jSeXx6A+tz/aFaRU0g8gZV8Un8dN/rUscuWoK62ntH0VE1dKLP3Hfh7O+9tlt b14hB/aq0tSdszzphrqBExqAtQ3nJRu62nyeI/EPN0+ku/C6W/7WN6x+xzOzXanonRcK pkyA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=cUoCjxVUE34s1+eP3pk70mRzAMWsM0v+FDRsaXLy0ew=; b=eOkWoJ/6xJoLAxn8O4XNfmKNiRywJ6ZQ+xbuxA3jTGlkdmZaHUT4hfAwajv0xhIcr6 QrmsZDw51+eVZUKyeTk1dhBitdFCq3QLxRIozRSLJrJgCXLeR5VzV2nA5nP/gIam/mRB OR8TBFPuwSrugQIvOdV2+IQ+/vMzq5p0jnX1f+inVl11CwtYeIIBRb3/LmqgtpUYvlWB mu/f8ZyzBpspGoSGVR7N+7qdKLhbt0EmJsXhAFPaUCrxKmzHFKL6YTAVtWLY6CkiLO3r MFgvi3EkB0i8lEwNFm6GN90o/1N5q23j5mZdcps+AGJv2Nj5fNCsZhRQWTzH989VJK+Z GnLA== X-Gm-Message-State: AOAM533ZoakV3uu+RrzKXSkcJjl/ro9spJzaU/j5NheyxEQNHlUEJsp7 lKWEP8OmsKnkkM2P5PXLPJk= X-Received: by 2002:a37:90d:: with SMTP id 13mr12315643qkj.386.1627846814549; Sun, 01 Aug 2021 12:40:14 -0700 (PDT) Received: from shaak.. (198-48-202-89.cpe.pppoe.ca. [198.48.202.89]) by smtp.gmail.com with ESMTPSA id f10sm3621606qto.31.2021.08.01.12.40.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 01 Aug 2021 12:40:14 -0700 (PDT) From: Liam Beguin To: liambeguin@gmail.com, peda@axentia.se, jic23@kernel.org, lars@metafoo.de, pmeerw@pmeerw.net Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, robh+dt@kernel.org Subject: [PATCH v7 04/13] iio: afe: rescale: expose scale processing function Date: Sun, 1 Aug 2021 15:39:51 -0400 Message-Id: <20210801194000.3646303-5-liambeguin@gmail.com> X-Mailer: git-send-email 2.30.1.489.g328c10930387 In-Reply-To: <20210801194000.3646303-1-liambeguin@gmail.com> References: <20210801194000.3646303-1-liambeguin@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Liam Beguin In preparation for the addition of kunit tests, expose the logic responsible for combining channel scales. Signed-off-by: Liam Beguin --- drivers/iio/afe/iio-rescale.c | 65 ++++++++++++++------------------- include/linux/iio/afe/rescale.h | 30 +++++++++++++++ 2 files changed, 58 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 774eb3044edd..d0669fd8eac5 100644 --- a/drivers/iio/afe/iio-rescale.c +++ b/drivers/iio/afe/iio-rescale.c @@ -11,35 +11,46 @@ #include #include #include +#include #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) +{ + unsigned long long 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 = *val * 1000000000LL; + do_div(tmp, rescale->denominator); + tmp *= rescale->numerator; + do_div(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); - unsigned long long tmp; int ret; switch (mask) { @@ -65,27 +76,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 = *val * 1000000000LL; - do_div(tmp, rescale->denominator); - tmp *= rescale->numerator; - do_div(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..14d4ee1227c6 --- /dev/null +++ b/include/linux/iio/afe/rescale.h @@ -0,0 +1,30 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Copyright (C) 2021 Liam Beguin + */ + +#ifndef __IIO_RESCALE_H__ +#define __IIO_RESCALE_H__ + +#include + +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.30.1.489.g328c10930387