Linux IIO development
 help / color / mirror / Atom feed
From: Paul Cercueil <paul@crapouillou.net>
To: Peter Rosin <peda@axentia.se>
Cc: Jonathan Cameron <jic23@kernel.org>,
	Lars-Peter Clausen <lars@metafoo.de>,
	list@opendingux.net, linux-iio@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH 4/4] iio: afe/rescale: Implement write_raw
Date: Fri, 22 Jul 2022 10:33:47 +0100	[thread overview]
Message-ID: <B81FFR.XDQDJYR0QG6N2@crapouillou.net> (raw)
In-Reply-To: <b8539e31-b20a-6eb5-4b86-59a17d7c6939@axentia.se>

Hi Peter,

Le ven., juil. 22 2022 at 00:16:36 +0200, Peter Rosin <peda@axentia.se> 
a écrit :
> Hi!
> 
> 2022-07-21 at 21:15, Paul Cercueil wrote:
>>  Implement write_raw by converting the value if writing the scale, or
>>  just calling the managed channel driver's write_raw otherwise.
>> 
>>  Signed-off-by: Paul Cercueil <paul@crapouillou.net>
>>  ---
>>   drivers/iio/afe/iio-rescale.c | 22 ++++++++++++++++++++++
>>   1 file changed, 22 insertions(+)
>> 
>>  diff --git a/drivers/iio/afe/iio-rescale.c 
>> b/drivers/iio/afe/iio-rescale.c
>>  index 5c9970b93384..0edb62ee4508 100644
>>  --- a/drivers/iio/afe/iio-rescale.c
>>  +++ b/drivers/iio/afe/iio-rescale.c
>>  @@ -141,6 +141,27 @@ int rescale_process_offset(struct rescale 
>> *rescale, int scale_type,
>>   	}
>>   }
>> 
>>  +static int rescale_write_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;
>>  +
>>  +	switch (mask) {
>>  +	case IIO_CHAN_INFO_SCALE:
>>  +		tmp = val * 1000000000LL;
>>  +		do_div(tmp, rescale->numerator);
>>  +		tmp *= rescale->denominator;
>>  +		do_div(tmp, 1000000000LL);
> 
> do_div is for unsigned operands. Can val never ever be negative?
> What about the numerator and denominator, can those be negative? I
> think this code should live in a new rescale_process_inverse_scale
> function, or something like that (and a few tests could be added to
> drivers/iio/test/iio-test-rescale.c)

I can do that.

> 
>>  +		return iio_write_channel_attribute(rescale->source, tmp, 0,
>>  +						   IIO_CHAN_INFO_SCALE);
>>  +	default:
> 
> What if the source driver has a .write_raw_get_fmt callback? That bit
> of info is silently dropped (with no comment that a shortcut has been
> taken). How does inverse rescaling mix with a .write_raw_get_fmt that
> returns e.g. IIO_VAL_INT_PLUS_MICRO_DB anyway? I think all cases might
> get a bit hairy to support, so I think you need to do some filtering
> and somehow fail the .write_raw call if the .write_raw_get_fmt of the
> source returns something that gets too difficult to support.

If the inverse rescale uses the same code as rescale_process_scale() 
then it becomes problematic, yes, as it likes to change the type of the 
value.

What I could try - compute the inverse of the value, then find the 
closest scale value+type that the source driver supports, and use this 
as the value+type. Then the only failure point would be if 
.write_raw_get_fmt returns something different than the formats 
returned by .read_avail, but that sounds unlikely to happen.

Cheers,
-Paul

>>  +		return iio_write_channel_attribute(rescale->source,
>>  +						   val, val2, mask);
>>  +	}
>>  +}
>>  +
>>   static int rescale_read_raw(struct iio_dev *indio_dev,
>>   			    struct iio_chan_spec const *chan,
>>   			    int *val, int *val2, long mask)
>>  @@ -250,6 +271,7 @@ static int rescale_read_avail(struct iio_dev 
>> *indio_dev,
>>   }
>> 
>>   static const struct iio_info rescale_info = {
>>  +	.write_raw = rescale_write_raw,
>>   	.read_raw = rescale_read_raw,
>>   	.read_avail = rescale_read_avail,
>>   };



      reply	other threads:[~2022-07-22  9:42 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-21 19:15 [PATCH 0/4] iio: afe/rescale improvements Paul Cercueil
2022-07-21 19:15 ` [PATCH 1/4] iio: inkern: Remove useless argument to iio_channel_read_max() Paul Cercueil
2022-07-31 17:07   ` Jonathan Cameron
2022-07-21 19:15 ` [PATCH 2/4] iio: core: Add support for IIO_AVAIL_LIST_WITH_TYPE Paul Cercueil
2022-07-31 17:22   ` Jonathan Cameron
2022-07-21 19:15 ` [PATCH 3/4] iio: afe/rescale: Add support for converting scale avail table Paul Cercueil
2022-07-21 22:16   ` Peter Rosin
2022-07-22  8:52     ` Paul Cercueil
2022-07-31 16:58       ` Jonathan Cameron
2022-07-21 19:15 ` [PATCH 4/4] iio: afe/rescale: Implement write_raw Paul Cercueil
2022-07-21 22:16   ` Peter Rosin
2022-07-22  9:33     ` Paul Cercueil [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=B81FFR.XDQDJYR0QG6N2@crapouillou.net \
    --to=paul@crapouillou.net \
    --cc=jic23@kernel.org \
    --cc=lars@metafoo.de \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=list@opendingux.net \
    --cc=peda@axentia.se \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox