diff mbox series

[2/2] iio: afe: rescale: Implement write_raw

Message ID 20200210225438.112660-2-paul@crapouillou.net (mailing list archive)
State New, archived
Headers show
Series [1/2] iio: afe: rescale: Add support for converting scale avail table | expand

Commit Message

Paul Cercueil Feb. 10, 2020, 10:54 p.m. UTC
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(+)

Comments

Jonathan Cameron Feb. 15, 2020, 6:32 p.m. UTC | #1
On Mon, 10 Feb 2020 19:54:38 -0300
Paul Cercueil <paul@crapouillou.net> 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 95802d9ee25e..a48f6af9316d 100644
> --- a/drivers/iio/afe/iio-rescale.c
> +++ b/drivers/iio/afe/iio-rescale.c
> @@ -35,6 +35,27 @@ struct rescale {
>  	int *scale_data;
>  };
>  
> +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);
> +		return iio_write_channel_attribute(rescale->source, tmp, 0,
> +						   IIO_CHAN_INFO_SCALE);

Why is val2 always 0?  Won't that only work if the backend device
has integer scales?

> +	default:
> +		return iio_write_channel_attribute(rescale->source,
> +						   val, val2, mask);
> +	}
> +}
> +
>  static int rescale_convert(struct rescale *rescale, int type,
>  			   const int val, const int val2,
>  			   int *val_out, int *val2_out)
> @@ -110,6 +131,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,
>  };
Paul Cercueil July 20, 2020, 11:16 p.m. UTC | #2
Hi Jonathan,

Le sam. 15 févr. 2020 à 18:32, Jonathan Cameron <jic23@kernel.org> a 
écrit :
> On Mon, 10 Feb 2020 19:54:38 -0300
> Paul Cercueil <paul@crapouillou.net> 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 95802d9ee25e..a48f6af9316d 100644
>>  --- a/drivers/iio/afe/iio-rescale.c
>>  +++ b/drivers/iio/afe/iio-rescale.c
>>  @@ -35,6 +35,27 @@ struct rescale {
>>   	int *scale_data;
>>   };
>> 
>>  +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);
>>  +		return iio_write_channel_attribute(rescale->source, tmp, 0,
>>  +						   IIO_CHAN_INFO_SCALE);
> 
> Why is val2 always 0?  Won't that only work if the backend device
> has integer scales?

Sorry, somehow I didn't see your answer.

Indeed, this will only work if the backend device has integer scales, 
but what should I do? Just pass 'val2' instead of 0? Will the value be 
correct if I only apply the scale ratio to 'val'?

Cheers,
-Paul


>>  +	default:
>>  +		return iio_write_channel_attribute(rescale->source,
>>  +						   val, val2, mask);
>>  +	}
>>  +}
>>  +
>>   static int rescale_convert(struct rescale *rescale, int type,
>>   			   const int val, const int val2,
>>   			   int *val_out, int *val2_out)
>>  @@ -110,6 +131,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,
>>   };
>
Jonathan Cameron July 26, 2020, 12:41 p.m. UTC | #3
On Tue, 21 Jul 2020 01:16:55 +0200
Paul Cercueil <paul@crapouillou.net> wrote:

> Hi Jonathan,
> 
> Le sam. 15 févr. 2020 à 18:32, Jonathan Cameron <jic23@kernel.org> a 
> écrit :
> > On Mon, 10 Feb 2020 19:54:38 -0300
> > Paul Cercueil <paul@crapouillou.net> 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 95802d9ee25e..a48f6af9316d 100644
> >>  --- a/drivers/iio/afe/iio-rescale.c
> >>  +++ b/drivers/iio/afe/iio-rescale.c
> >>  @@ -35,6 +35,27 @@ struct rescale {
> >>   	int *scale_data;
> >>   };
> >> 
> >>  +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);
> >>  +		return iio_write_channel_attribute(rescale->source, tmp, 0,
> >>  +						   IIO_CHAN_INFO_SCALE);  
> > 
> > Why is val2 always 0?  Won't that only work if the backend device
> > has integer scales?  
> 
> Sorry, somehow I didn't see your answer.
> 
> Indeed, this will only work if the backend device has integer scales, 
> but what should I do? Just pass 'val2' instead of 0? Will the value be 
> correct if I only apply the scale ratio to 'val'?

I think you'll need to include it through the calculation. Given you
premultiply by 1000000000LL it should be easy enough to do.
Then for the final do_div you can easily work out the val2 part.

I'm not sure we currently have an inkern interface to get the type
of the channel attribute value though.  You may need to add one.

Jonathan



> 
> Cheers,
> -Paul
> 
> 
> >>  +	default:
> >>  +		return iio_write_channel_attribute(rescale->source,
> >>  +						   val, val2, mask);
> >>  +	}
> >>  +}
> >>  +
> >>   static int rescale_convert(struct rescale *rescale, int type,
> >>   			   const int val, const int val2,
> >>   			   int *val_out, int *val2_out)
> >>  @@ -110,6 +131,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,
> >>   };  
> >   
> 
>
Peter Rosin July 27, 2020, 9:16 a.m. UTC | #4
Hi!

Sorry for the delay. Vacation...

On 2020-07-26 14:41, Jonathan Cameron wrote:
> On Tue, 21 Jul 2020 01:16:55 +0200
> Paul Cercueil <paul@crapouillou.net> wrote:
> 
>> Hi Jonathan,
>>
>> Le sam. 15 févr. 2020 à 18:32, Jonathan Cameron <jic23@kernel.org> a 
>> écrit :
>>> On Mon, 10 Feb 2020 19:54:38 -0300
>>> Paul Cercueil <paul@crapouillou.net> 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 95802d9ee25e..a48f6af9316d 100644
>>>>  --- a/drivers/iio/afe/iio-rescale.c
>>>>  +++ b/drivers/iio/afe/iio-rescale.c
>>>>  @@ -35,6 +35,27 @@ struct rescale {
>>>>   	int *scale_data;
>>>>   };
>>>>
>>>>  +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);
>>>>  +		return iio_write_channel_attribute(rescale->source, tmp, 0,
>>>>  +						   IIO_CHAN_INFO_SCALE);  
>>>
>>> Why is val2 always 0?  Won't that only work if the backend device
>>> has integer scales?  
>>
>> Sorry, somehow I didn't see your answer.
>>
>> Indeed, this will only work if the backend device has integer scales, 
>> but what should I do? Just pass 'val2' instead of 0? Will the value be 
>> correct if I only apply the scale ratio to 'val'?
> 
> I think you'll need to include it through the calculation. Given you
> premultiply by 1000000000LL it should be easy enough to do.
> Then for the final do_div you can easily work out the val2 part.
> 
> I'm not sure we currently have an inkern interface to get the type
> of the channel attribute value though.  You may need to add one.

Right, I didn't originally add scaled writing as
1. I don't need it.
2. It's a rats nest, IIRC some drivers are picky in what they take and
   you would need some kind of matrix of how to best handle the different
   conversion cases. I just didn't want to go there, and this patch
   feels far too simple to be adequate. But then again, maybe my memory is
   poorer that I thought...

Cheers,
Peter
diff mbox series

Patch

diff --git a/drivers/iio/afe/iio-rescale.c b/drivers/iio/afe/iio-rescale.c
index 95802d9ee25e..a48f6af9316d 100644
--- a/drivers/iio/afe/iio-rescale.c
+++ b/drivers/iio/afe/iio-rescale.c
@@ -35,6 +35,27 @@  struct rescale {
 	int *scale_data;
 };
 
+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);
+		return iio_write_channel_attribute(rescale->source, tmp, 0,
+						   IIO_CHAN_INFO_SCALE);
+	default:
+		return iio_write_channel_attribute(rescale->source,
+						   val, val2, mask);
+	}
+}
+
 static int rescale_convert(struct rescale *rescale, int type,
 			   const int val, const int val2,
 			   int *val_out, int *val2_out)
@@ -110,6 +131,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,
 };