linux-iio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH]  iio: afe: iio-rescale: Support processed channels
@ 2020-11-01 23:22 Linus Walleij
  2020-11-15 11:21 ` Linus Walleij
                   ` (3 more replies)
  0 siblings, 4 replies; 16+ messages in thread
From: Linus Walleij @ 2020-11-01 23:22 UTC (permalink / raw)
  To: Jonathan Cameron, linux-iio
  Cc: Hartmut Knaack, Lars-Peter Clausen, Peter Meerwald-Stadler,
	Linus Walleij, Peter Rosin

It happens that an ADC will only provide raw or processed
voltage conversion channels. (adc/ab8500-gpadc.c).
On the Samsung GT-I9070 this is used for a light sensor
and current sense amplifier so we need to think of something.

The idea is to allow processed channels and scale them
with 1/1 and then the rescaler can modify the result
on top.

Cc: Peter Rosin <peda@axentia.se>
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
---
 drivers/iio/afe/iio-rescale.c | 31 +++++++++++++++++++++++++++----
 1 file changed, 27 insertions(+), 4 deletions(-)

diff --git a/drivers/iio/afe/iio-rescale.c b/drivers/iio/afe/iio-rescale.c
index e42ea2b1707d..ea90034cb257 100644
--- a/drivers/iio/afe/iio-rescale.c
+++ b/drivers/iio/afe/iio-rescale.c
@@ -29,6 +29,7 @@ struct rescale {
 	struct iio_channel *source;
 	struct iio_chan_spec chan;
 	struct iio_chan_spec_ext_info *ext_info;
+	bool chan_processed;
 	s32 numerator;
 	s32 denominator;
 };
@@ -43,10 +44,27 @@ static int rescale_read_raw(struct iio_dev *indio_dev,
 
 	switch (mask) {
 	case IIO_CHAN_INFO_RAW:
-		return iio_read_channel_raw(rescale->source, val);
+		if (rescale->chan_processed)
+			/*
+			 * When only processed channels are supported, we
+			 * read the processed data and scale it by 1/1
+			 * augmented with whatever the rescaler has calculated.
+			 */
+			return iio_read_channel_processed(rescale->source, val);
+		else
+			return iio_read_channel_raw(rescale->source, val);
 
 	case IIO_CHAN_INFO_SCALE:
-		ret = iio_read_channel_scale(rescale->source, val, val2);
+		if (rescale->chan_processed) {
+			/*
+			 * Processed channels are scaled 1-to-1
+			 */
+			ret = IIO_VAL_FRACTIONAL;
+			*val = 1;
+			*val2 = 1;
+		} else {
+			ret = iio_read_channel_scale(rescale->source, val, val2);
+		}
 		switch (ret) {
 		case IIO_VAL_FRACTIONAL:
 			*val *= rescale->numerator;
@@ -132,8 +150,13 @@ static int rescale_configure_channel(struct device *dev,
 
 	if (!iio_channel_has_info(schan, IIO_CHAN_INFO_RAW) ||
 	    !iio_channel_has_info(schan, IIO_CHAN_INFO_SCALE)) {
-		dev_err(dev, "source channel does not support raw/scale\n");
-		return -EINVAL;
+		if (iio_channel_has_info(schan, IIO_CHAN_INFO_PROCESSED)) {
+			dev_info(dev, "using processed channel\n");
+			rescale->chan_processed = true;
+		} else {
+			dev_err(dev, "source channel does not support raw+scale or processed data\n");
+			return -EINVAL;
+		}
 	}
 
 	chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
-- 
2.26.2


^ permalink raw reply related	[flat|nested] 16+ messages in thread

end of thread, other threads:[~2021-01-04 18:10 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-01 23:22 [PATCH] iio: afe: iio-rescale: Support processed channels Linus Walleij
2020-11-15 11:21 ` Linus Walleij
2020-11-15 17:44 ` Jonathan Cameron
2020-11-16  8:18   ` Peter Rosin
2020-12-13 12:12     ` Jonathan Cameron
2020-12-12 12:26 ` Linus Walleij
2020-12-12 23:22   ` Peter Rosin
2020-12-13 12:16     ` Jonathan Cameron
2020-12-14  8:34       ` Peter Rosin
2020-12-14 15:07         ` Jonathan Cameron
2020-12-14 15:30           ` Peter Rosin
2020-12-14 16:34             ` Jonathan Cameron
2021-01-04 14:45               ` Linus Walleij
2021-01-04 17:11                 ` Jonathan Cameron
2021-01-04 18:09                   ` Peter Rosin
2020-12-13 15:16 ` Andy Shevchenko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).