All of lore.kernel.org
 help / color / mirror / Atom feed
From: Peter Rosin <peda@axentia.se>
To: Linus Walleij <linus.walleij@linaro.org>,
	Jonathan Cameron <jic23@kernel.org>,
	linux-iio@vger.kernel.org
Cc: Hartmut Knaack <knaack.h@gmx.de>,
	Lars-Peter Clausen <lars@metafoo.de>,
	Peter Meerwald-Stadler <pmeerw@pmeerw.net>
Subject: Re: [PATCH v2] iio: afe: iio-rescale: Support processed channels
Date: Mon, 10 May 2021 10:10:13 +0200	[thread overview]
Message-ID: <c8e7964f-6dc8-291e-2d50-0eab4ce3f3c3@axentia.se> (raw)
In-Reply-To: <20210509225907.351562-1-linus.walleij@linaro.org>

Hi!

Thanks for the update!

On 2021-05-10 00:59, Linus Walleij wrote:
> 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.
> 
> Link: https://lore.kernel.org/linux-iio/20201101232211.1194304-1-linus.walleij@linaro.org/
> Cc: Peter Rosin <peda@axentia.se>
> Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
> ---
> ChangeLog v1->v2:
> - Deny calls to .read_avail() for processed channels, and
>   insert a comment.
> - Move an assignment of IIO_VAL_FRACTIONAL down to the end
>   of the block for better readability.
> ---
>  drivers/iio/afe/iio-rescale.c | 39 +++++++++++++++++++++++++++++++----
>  1 file changed, 35 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/iio/afe/iio-rescale.c b/drivers/iio/afe/iio-rescale.c
> index e42ea2b1707d..ddbb760ae8df 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
> +			 */
> +			*val = 1;
> +			*val2 = 1;
> +			ret = IIO_VAL_FRACTIONAL;
> +		} else {
> +			ret = iio_read_channel_scale(rescale->source, val, val2);
> +		}
>  		switch (ret) {
>  		case IIO_VAL_FRACTIONAL:
>  			*val *= rescale->numerator;
> @@ -82,6 +100,14 @@ static int rescale_read_avail(struct iio_dev *indio_dev,
>  
>  	switch (mask) {
>  	case IIO_CHAN_INFO_RAW:
> +		/*
> +		 * Using .read_avail() is fringe to begin with and makes
> +		 * no sense whatsoever for processed channels, so if called
> +		 * on a processed channel, we just error out here.
> +		 */
> +		if (rescale->chan_processed)
> +			return -EINVAL;
> +

I believe this is not the correct place to block requests for .read_avail.
It's too late; the driver should not first promise that it is possible to
call .read_avail just to later return -EINVAL.

It's better to just avoid adding the IIO_CHAN_INFO_RAW bit to
chan->info_mask_separate_available in the rescale_configure_channel
function. See some suggested -++ below at the end...

>  		*type = IIO_VAL_INT;
>  		return iio_read_avail_channel_raw(rescale->source,
>  						  vals, length);
> @@ -132,8 +158,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;
> +		}
>  	}
I'd also rather have this logic reversed/flattened:

	if (iio_channel_has_info(schan, IIO_CHAN_INFO_RAW) &&
	    iio_channel_has_info(schan, IIO_CHAN_INFO_SCALE)) {
		dev_info(dev, "using raw+scale source channel\n");
	} else if (iio_channel_has_info(schan, IIO_CHAN_INFO_PROCESSED)) {
		dev_info(dev, "using processed source channel\n");
		rescale->chan_processed = true;
	} else {
		dev_err(dev, "source channel is not supported\n");
		return -EINVAL;
	}

>  
>  	chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
		BIT(IIO_CHAN_INFO_SCALE);
 
-	if (iio_channel_has_available(schan, IIO_CHAN_INFO_RAW))
+	if (iio_channel_has_available(schan, IIO_CHAN_INFO_RAW) &&
+	    !rescale->chan_processed)
 		chan->info_mask_separate_available |= BIT(IIO_CHAN_INFO_RAW);

(disclaimer, all new code untested and written directly in the mail client)

CHeers,
Peter

      reply	other threads:[~2021-05-10  8:10 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-09 22:59 [PATCH v2] iio: afe: iio-rescale: Support processed channels Linus Walleij
2021-05-10  8:10 ` Peter Rosin [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=c8e7964f-6dc8-291e-2d50-0eab4ce3f3c3@axentia.se \
    --to=peda@axentia.se \
    --cc=jic23@kernel.org \
    --cc=knaack.h@gmx.de \
    --cc=lars@metafoo.de \
    --cc=linus.walleij@linaro.org \
    --cc=linux-iio@vger.kernel.org \
    --cc=pmeerw@pmeerw.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.