Skip to contentSkip to conversation

iio: fix scale application in iio_convert_raw_to_processed_unlocked

PATCH In review Matteo Martelli 2 participants 2 messages Jul 30, 2024 – Aug 3, 2024 IIO

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iio: fix scale application in iio_convert_raw_to_processed_unlocked

When the scale_type is IIO_VAL_INT_PLUS_MICRO or IIO_VAL_INT_PLUS_NANO the scale passed as argument is only applied to the fractional part of the value. Fix it by also multiplying the integer part by the scale provided.

Fixes 48e44ce0f881 ("iio:inkern: Add function to read the processed value")

---
I found out that after iio_hwmon switched to iio_read_channel_processed_scale() to convert the power channel value from milli-Watts to micro-Watts [1], lm-sensors started to show unexpected values for the power channel of the pac1921 iio driver I was testing. It looks the issue relies in the iio_convert_raw_to_processed_unlocked() function that only applies the given scale to the fractional part if the channel scale type is IIO_VAL_INT_PLUS_MICRO or IIO_VAL_INT_PLUS_NANO.

For example with a raw power value of 71, a power scale type of IIO_VAL_INT_PLUS_NANO and power scale value of 9.775200000 (mW) the processed power value would be ~694 mW but when scaled to uW by the iio_hwmon calling iio_read_channel_processed_scale() with a scale of 1000, the processed power would wrongly result to ~55678 uW (~55mW) = 71*9 + 71*775200000*1000/1000000000. This because the scale of 1000 is only applied to the fractional part of the power value. Instead it should be 71*9*1000 + 71*775200000*1000/1000000000 = 694039 uW.

[1]: https://lore.kernel.org/linux-hwmon/[email protected]/

---
 drivers/iio/inkern.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/iio/inkern.c +4 -4
index 9f484c94bc6e..151099be2863 100644
@@ -647,17 +647,17 @@ static int iio_convert_raw_to_processed_unlocked(struct iio_channel *chan,
 		break;
 	case IIO_VAL_INT_PLUS_MICRO:
 		if (scale_val2 < 0)
-			*processed = -raw64 * scale_val;
+			*processed = -raw64 * scale_val * scale;
 		else
-			*processed = raw64 * scale_val;
+			*processed = raw64 * scale_val * scale;
 		*processed += div_s64(raw64 * (s64)scale_val2 * scale,
 				      1000000LL);
 		break;
 	case IIO_VAL_INT_PLUS_NANO:
 		if (scale_val2 < 0)
-			*processed = -raw64 * scale_val;
+			*processed = -raw64 * scale_val * scale;
 		else
-			*processed = raw64 * scale_val;
+			*processed = raw64 * scale_val * scale;
 		*processed += div_s64(raw64 * (s64)scale_val2 * scale,
 				      1000000000LL);
 		break;

---
base-commit: 1ebab783647a9e3bf357002d5c4ff060c8474a0a
change-id: 20240729-iio-fix-scale-287b7630374e

Best regards,

Signature · 1 line
Matteo Martelli <[email protected]>
maintainer
#1 lore raw
Quoted 7 lines · On Tue, 30 Jul 2024 10:11:53 +0200 Matteo Martelli <[email protected]>…

When the scale_type is IIO_VAL_INT_PLUS_MICRO or IIO_VAL_INT_PLUS_NANO the scale passed as argument is only applied to the fractional part of the value. Fix it by also multiplying the integer part by the scale provided.

Fixes 48e44ce0f881 ("iio:inkern: Add function to read the processed value")

Ouch. I only vaguely recall the history of this, but I suspect that extra 'scale' was an after thought and we messed it up.

Anyhow, applied to the fixes-togreg branch of iio.git and marked for stable.

Jonathan

Quoted 56 lines

---
I found out that after iio_hwmon switched to iio_read_channel_processed_scale() to convert the power channel value from milli-Watts to micro-Watts [1], lm-sensors started to show unexpected values for the power channel of the pac1921 iio driver I was testing. It looks the issue relies in the iio_convert_raw_to_processed_unlocked() function that only applies the given scale to the fractional part if the channel scale type is IIO_VAL_INT_PLUS_MICRO or IIO_VAL_INT_PLUS_NANO.

For example with a raw power value of 71, a power scale type of IIO_VAL_INT_PLUS_NANO and power scale value of 9.775200000 (mW) the processed power value would be ~694 mW but when scaled to uW by the iio_hwmon calling iio_read_channel_processed_scale() with a scale of 1000, the processed power would wrongly result to ~55678 uW (~55mW) = 71*9 + 71*775200000*1000/1000000000. This because the scale of 1000 is only applied to the fractional part of the power value. Instead it should be 71*9*1000 + 71*775200000*1000/1000000000 = 694039 uW.

[1]: https://lore.kernel.org/linux-hwmon/[email protected]/

---
 drivers/iio/inkern.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/iio/inkern.c +4 -5
index 9f484c94bc6e..151099be2863 100644
@@ -647,17 +647,17 @@ static int iio_convert_raw_to_processed_unlocked(struct iio_channel *chan,
 		break;
 	case IIO_VAL_INT_PLUS_MICRO:
 		if (scale_val2 < 0)
-			*processed = -raw64 * scale_val;
+			*processed = -raw64 * scale_val * scale;
 		else
-			*processed = raw64 * scale_val;
+			*processed = raw64 * scale_val * scale;
 		*processed += div_s64(raw64 * (s64)scale_val2 * scale,
 				      1000000LL);
 		break;
 	case IIO_VAL_INT_PLUS_NANO:
 		if (scale_val2 < 0)
-			*processed = -raw64 * scale_val;
+			*processed = -raw64 * scale_val * scale;
 		else
-			*processed = raw64 * scale_val;
+			*processed = raw64 * scale_val * scale;
 		*processed += div_s64(raw64 * (s64)scale_val2 * scale,
 				      1000000000LL);
 		break;
 
---

base-commit: 1ebab783647a9e3bf357002d5c4ff060c8474a0a
change-id: 20240729-iio-fix-scale-287b7630374e

Best regards,