linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/2] iio: afe: rescale: Add support for converting scale avail table
@ 2020-02-10 22:54 Paul Cercueil
  2020-02-10 22:54 ` [PATCH 2/2] iio: afe: rescale: Implement write_raw Paul Cercueil
  2020-07-27  9:22 ` [PATCH 1/2] iio: afe: rescale: Add support for converting scale avail table Peter Rosin
  0 siblings, 2 replies; 7+ messages in thread
From: Paul Cercueil @ 2020-02-10 22:54 UTC (permalink / raw)
  To: Peter Rosin, Jonathan Cameron; +Cc: od, linux-iio, linux-kernel, Paul Cercueil

When the IIO channel has a scale_available attribute, we want the values
contained to be properly converted the same way the scale value is.

Signed-off-by: Paul Cercueil <paul@crapouillou.net>
---
 drivers/iio/afe/iio-rescale.c | 125 ++++++++++++++++++++++++++++------
 1 file changed, 103 insertions(+), 22 deletions(-)

diff --git a/drivers/iio/afe/iio-rescale.c b/drivers/iio/afe/iio-rescale.c
index e9ceee66d1e7..95802d9ee25e 100644
--- a/drivers/iio/afe/iio-rescale.c
+++ b/drivers/iio/afe/iio-rescale.c
@@ -31,14 +31,45 @@ struct rescale {
 	struct iio_chan_spec_ext_info *ext_info;
 	s32 numerator;
 	s32 denominator;
+	int scale_type, scale_len;
+	int *scale_data;
 };
 
+static int rescale_convert(struct rescale *rescale, int type,
+			   const int val, const int val2,
+			   int *val_out, int *val2_out)
+{
+	unsigned long long tmp;
+
+	switch (type) {
+	case IIO_VAL_FRACTIONAL:
+		*val_out = val * rescale->numerator;
+		*val2_out = val2 * rescale->denominator;
+		return type;
+	case IIO_VAL_INT:
+		*val_out = val * rescale->numerator;
+		if (rescale->denominator == 1)
+			return type;
+		*val2_out = 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_out = tmp;
+		*val2_out = val2;
+		return 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) {
@@ -47,27 +78,7 @@ static int rescale_read_raw(struct iio_dev *indio_dev,
 
 	case IIO_CHAN_INFO_SCALE:
 		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_convert(rescale, ret, *val, *val2, val, val2);
 	default:
 		return -EINVAL;
 	}
@@ -85,6 +96,14 @@ static int rescale_read_avail(struct iio_dev *indio_dev,
 		*type = IIO_VAL_INT;
 		return iio_read_avail_channel_raw(rescale->source,
 						  vals, length);
+	case IIO_CHAN_INFO_SCALE:
+		if (rescale->scale_len) {
+			*type = rescale->scale_type;
+			*length = rescale->scale_len;
+			*vals = rescale->scale_data;
+			return IIO_AVAIL_LIST;
+		}
+		/* fall-through */
 	default:
 		return -EINVAL;
 	}
@@ -119,11 +138,65 @@ static ssize_t rescale_write_ext_info(struct iio_dev *indio_dev,
 					  buf, len);
 }
 
+static int rescale_init_scale_avail(struct device *dev, struct rescale *rescale)
+{
+	const int *scale_raw;
+	unsigned int i;
+	int ret;
+
+	ret = iio_read_avail_channel_attribute(rescale->source, &scale_raw,
+					       &rescale->scale_type,
+					       &rescale->scale_len,
+					       IIO_CHAN_INFO_SCALE);
+	if (ret)
+		return ret;
+
+	if (rescale->scale_type == IIO_VAL_INT && rescale->denominator > 1)
+		rescale->scale_len *= 2;
+
+	rescale->scale_data = devm_kzalloc(dev,
+					   sizeof(int) * rescale->scale_len,
+					   GFP_KERNEL);
+	if (!rescale->scale_len)
+		return -ENOMEM;
+
+	if (rescale->scale_type != IIO_VAL_INT) {
+		for (i = 0; i < rescale->scale_len; i += 2) {
+			ret = rescale_convert(rescale, rescale->scale_type,
+					      scale_raw[i], scale_raw[i + 1],
+					      &rescale->scale_data[i],
+					      &rescale->scale_data[i + 1]);
+			if (ret < 0)
+				return ret;
+		}
+	} else if (rescale->denominator == 1) {
+		for (i = 0; i < rescale->scale_len; i++) {
+			ret = rescale_convert(rescale, IIO_VAL_INT,
+					      scale_raw[i], 0,
+					      &rescale->scale_data[i], NULL);
+			if (ret < 0)
+				return ret;
+		}
+	} else {
+		for (i = 0; i < rescale->scale_len / 2; i++) {
+			ret = rescale_convert(rescale, IIO_VAL_INT,
+					      scale_raw[i], 0,
+					      &rescale->scale_data[i * 2],
+					      &rescale->scale_data[i * 2 + 1]);
+			if (ret < 0)
+				return ret;
+		}
+	}
+
+	return 0;
+}
+
 static int rescale_configure_channel(struct device *dev,
 				     struct rescale *rescale)
 {
 	struct iio_chan_spec *chan = &rescale->chan;
 	struct iio_chan_spec const *schan = rescale->source->channel;
+	int ret;
 
 	chan->indexed = 1;
 	chan->output = schan->output;
@@ -142,6 +215,14 @@ static int rescale_configure_channel(struct device *dev,
 	if (iio_channel_has_available(schan, IIO_CHAN_INFO_RAW))
 		chan->info_mask_separate_available |= BIT(IIO_CHAN_INFO_RAW);
 
+	if (iio_channel_has_available(schan, IIO_CHAN_INFO_SCALE)) {
+		chan->info_mask_separate_available |= BIT(IIO_CHAN_INFO_SCALE);
+
+		ret = rescale_init_scale_avail(dev, rescale);
+		if (ret)
+			return ret;
+	}
+
 	return 0;
 }
 
-- 
2.25.0


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

* [PATCH 2/2] iio: afe: rescale: Implement write_raw
  2020-02-10 22:54 [PATCH 1/2] iio: afe: rescale: Add support for converting scale avail table Paul Cercueil
@ 2020-02-10 22:54 ` Paul Cercueil
  2020-02-15 18:32   ` Jonathan Cameron
  2020-07-27  9:22 ` [PATCH 1/2] iio: afe: rescale: Add support for converting scale avail table Peter Rosin
  1 sibling, 1 reply; 7+ messages in thread
From: Paul Cercueil @ 2020-02-10 22:54 UTC (permalink / raw)
  To: Peter Rosin, Jonathan Cameron; +Cc: od, linux-iio, linux-kernel, Paul Cercueil

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);
+	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,
 };
-- 
2.25.0


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

* Re: [PATCH 2/2] iio: afe: rescale: Implement write_raw
  2020-02-10 22:54 ` [PATCH 2/2] iio: afe: rescale: Implement write_raw Paul Cercueil
@ 2020-02-15 18:32   ` Jonathan Cameron
  2020-07-20 23:16     ` Paul Cercueil
  0 siblings, 1 reply; 7+ messages in thread
From: Jonathan Cameron @ 2020-02-15 18:32 UTC (permalink / raw)
  To: Paul Cercueil; +Cc: Peter Rosin, od, linux-iio, linux-kernel

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,
>  };


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

* Re: [PATCH 2/2] iio: afe: rescale: Implement write_raw
  2020-02-15 18:32   ` Jonathan Cameron
@ 2020-07-20 23:16     ` Paul Cercueil
  2020-07-26 12:41       ` Jonathan Cameron
  0 siblings, 1 reply; 7+ messages in thread
From: Paul Cercueil @ 2020-07-20 23:16 UTC (permalink / raw)
  To: Jonathan Cameron; +Cc: Peter Rosin, od, linux-iio, linux-kernel

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,
>>   };
> 



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

* Re: [PATCH 2/2] iio: afe: rescale: Implement write_raw
  2020-07-20 23:16     ` Paul Cercueil
@ 2020-07-26 12:41       ` Jonathan Cameron
  2020-07-27  9:16         ` Peter Rosin
  0 siblings, 1 reply; 7+ messages in thread
From: Jonathan Cameron @ 2020-07-26 12:41 UTC (permalink / raw)
  To: Paul Cercueil; +Cc: Peter Rosin, od, linux-iio, linux-kernel

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,
> >>   };  
> >   
> 
> 


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

* Re: [PATCH 2/2] iio: afe: rescale: Implement write_raw
  2020-07-26 12:41       ` Jonathan Cameron
@ 2020-07-27  9:16         ` Peter Rosin
  0 siblings, 0 replies; 7+ messages in thread
From: Peter Rosin @ 2020-07-27  9:16 UTC (permalink / raw)
  To: Jonathan Cameron, Paul Cercueil; +Cc: od, linux-iio, linux-kernel

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

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

* Re: [PATCH 1/2] iio: afe: rescale: Add support for converting scale avail table
  2020-02-10 22:54 [PATCH 1/2] iio: afe: rescale: Add support for converting scale avail table Paul Cercueil
  2020-02-10 22:54 ` [PATCH 2/2] iio: afe: rescale: Implement write_raw Paul Cercueil
@ 2020-07-27  9:22 ` Peter Rosin
  1 sibling, 0 replies; 7+ messages in thread
From: Peter Rosin @ 2020-07-27  9:22 UTC (permalink / raw)
  To: Paul Cercueil, Jonathan Cameron; +Cc: od, linux-iio, linux-kernel

Hi!

On 2020-02-10 23:54, Paul Cercueil wrote:
> When the IIO channel has a scale_available attribute, we want the values
> contained to be properly converted the same way the scale value is.
> 
> Signed-off-by: Paul Cercueil <paul@crapouillou.net>
> ---
>  drivers/iio/afe/iio-rescale.c | 125 ++++++++++++++++++++++++++++------
>  1 file changed, 103 insertions(+), 22 deletions(-)
> 
> diff --git a/drivers/iio/afe/iio-rescale.c b/drivers/iio/afe/iio-rescale.c
> index e9ceee66d1e7..95802d9ee25e 100644
> --- a/drivers/iio/afe/iio-rescale.c
> +++ b/drivers/iio/afe/iio-rescale.c
> @@ -31,14 +31,45 @@ struct rescale {
>  	struct iio_chan_spec_ext_info *ext_info;
>  	s32 numerator;
>  	s32 denominator;
> +	int scale_type, scale_len;
> +	int *scale_data;
>  };
>  
> +static int rescale_convert(struct rescale *rescale, int type,
> +			   const int val, const int val2,
> +			   int *val_out, int *val2_out)
> +{
> +	unsigned long long tmp;
> +
> +	switch (type) {
> +	case IIO_VAL_FRACTIONAL:
> +		*val_out = val * rescale->numerator;
> +		*val2_out = val2 * rescale->denominator;
> +		return type;
> +	case IIO_VAL_INT:
> +		*val_out = val * rescale->numerator;
> +		if (rescale->denominator == 1)
> +			return type;
> +		*val2_out = 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_out = tmp;
> +		*val2_out = val2;
> +		return 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) {
> @@ -47,27 +78,7 @@ static int rescale_read_raw(struct iio_dev *indio_dev,
>  
>  	case IIO_CHAN_INFO_SCALE:
>  		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_convert(rescale, ret, *val, *val2, val, val2);
>  	default:
>  		return -EINVAL;
>  	}
> @@ -85,6 +96,14 @@ static int rescale_read_avail(struct iio_dev *indio_dev,
>  		*type = IIO_VAL_INT;
>  		return iio_read_avail_channel_raw(rescale->source,
>  						  vals, length);
> +	case IIO_CHAN_INFO_SCALE:
> +		if (rescale->scale_len) {
> +			*type = rescale->scale_type;
> +			*length = rescale->scale_len;
> +			*vals = rescale->scale_data;
> +			return IIO_AVAIL_LIST;
> +		}
> +		/* fall-through */
>  	default:
>  		return -EINVAL;
>  	}
> @@ -119,11 +138,65 @@ static ssize_t rescale_write_ext_info(struct iio_dev *indio_dev,
>  					  buf, len);
>  }
>  
> +static int rescale_init_scale_avail(struct device *dev, struct rescale *rescale)
> +{
> +	const int *scale_raw;
> +	unsigned int i;
> +	int ret;
> +
> +	ret = iio_read_avail_channel_attribute(rescale->source, &scale_raw,
> +					       &rescale->scale_type,
> +					       &rescale->scale_len,
> +					       IIO_CHAN_INFO_SCALE);
> +	if (ret)
> +		return ret;
> +
> +	if (rescale->scale_type == IIO_VAL_INT && rescale->denominator > 1)
> +		rescale->scale_len *= 2;
> +
> +	rescale->scale_data = devm_kzalloc(dev,
> +					   sizeof(int) * rescale->scale_len,
> +					   GFP_KERNEL);
> +	if (!rescale->scale_len)
> +		return -ENOMEM;
> +
> +	if (rescale->scale_type != IIO_VAL_INT) {
> +		for (i = 0; i < rescale->scale_len; i += 2) {
> +			ret = rescale_convert(rescale, rescale->scale_type,
> +					      scale_raw[i], scale_raw[i + 1],
> +					      &rescale->scale_data[i],
> +					      &rescale->scale_data[i + 1]);
> +			if (ret < 0)
> +				return ret;
> +		}
> +	} else if (rescale->denominator == 1) {
> +		for (i = 0; i < rescale->scale_len; i++) {
> +			ret = rescale_convert(rescale, IIO_VAL_INT,
> +					      scale_raw[i], 0,
> +					      &rescale->scale_data[i], NULL);
> +			if (ret < 0)
> +				return ret;
> +		}
> +	} else {
> +		for (i = 0; i < rescale->scale_len / 2; i++) {
> +			ret = rescale_convert(rescale, IIO_VAL_INT,
> +					      scale_raw[i], 0,
> +					      &rescale->scale_data[i * 2],
> +					      &rescale->scale_data[i * 2 + 1]);
> +			if (ret < 0)
> +				return ret;
> +		}
> +	}
> +
> +	return 0;
> +}
> +
>  static int rescale_configure_channel(struct device *dev,
>  				     struct rescale *rescale)
>  {
>  	struct iio_chan_spec *chan = &rescale->chan;
>  	struct iio_chan_spec const *schan = rescale->source->channel;
> +	int ret;
>  
>  	chan->indexed = 1;
>  	chan->output = schan->output;
> @@ -142,6 +215,14 @@ static int rescale_configure_channel(struct device *dev,
>  	if (iio_channel_has_available(schan, IIO_CHAN_INFO_RAW))
>  		chan->info_mask_separate_available |= BIT(IIO_CHAN_INFO_RAW);
>  
> +	if (iio_channel_has_available(schan, IIO_CHAN_INFO_SCALE)) {
> +		chan->info_mask_separate_available |= BIT(IIO_CHAN_INFO_SCALE);
> +
> +		ret = rescale_init_scale_avail(dev, rescale);
> +		if (ret)
> +			return ret;
> +	}
> +

You unconditionally add a channel that in some cases then always return
"not supported" if you try to use it. Is that "safe" or can it cause
trouble for some unsuspecting application?

Cheers,
Peter

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

end of thread, other threads:[~2020-07-27  9:22 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-02-10 22:54 [PATCH 1/2] iio: afe: rescale: Add support for converting scale avail table Paul Cercueil
2020-02-10 22:54 ` [PATCH 2/2] iio: afe: rescale: Implement write_raw Paul Cercueil
2020-02-15 18:32   ` Jonathan Cameron
2020-07-20 23:16     ` Paul Cercueil
2020-07-26 12:41       ` Jonathan Cameron
2020-07-27  9:16         ` Peter Rosin
2020-07-27  9:22 ` [PATCH 1/2] iio: afe: rescale: Add support for converting scale avail table Peter Rosin

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).