iio: fix scale application in iio_convert_raw_to_processed_unlocked
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.
---
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
@@ -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]>
Jonathan Cameron
maintainerQuoted 7 lines · On Tue, 30 Jul 2024 10:11:53 +0200 Matteo Martelli <[email protected]>…
When the
scale_typeisIIO_VAL_INT_PLUS_MICROorIIO_VAL_INT_PLUS_NANOthe 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 <[email protected]>
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 afteriio_hwmonswitched toiio_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 theiio_convert_raw_to_processed_unlocked()function that only applies the given scale to the fractional part if the channel scale type isIIO_VAL_INT_PLUS_MICROorIIO_VAL_INT_PLUS_NANO.For example with a raw power value of 71, a power scale type of
IIO_VAL_INT_PLUS_NANOand power scale value of 9.775200000 (mW) the processed power value would be ~694 mW but when scaled to uW by theiio_hwmoncallingiio_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@@ -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-287b7630374eBest regards,