[PATCH] iio: fix scale application in iio_convert_raw_to_processed_unlocked

From: Matteo Martelli
Date: Tue Jul 30 2024 - 04:13:35 EST


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")
Signed-off-by: Matteo Martelli <matteomartelli3@xxxxxxxxx>
---
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/20240620212005.821805-1-sean.anderson@xxxxxxxxx/
---
drivers/iio/inkern.c | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/drivers/iio/inkern.c b/drivers/iio/inkern.c
index 9f484c94bc6e..151099be2863 100644
--- a/drivers/iio/inkern.c
+++ b/drivers/iio/inkern.c
@@ -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,
--
Matteo Martelli <matteomartelli3@xxxxxxxxx>