From: Matteo Martelli <matteomartelli3@gmail.com>
To: Jonathan Cameron <jic23@kernel.org>,
Lars-Peter Clausen <lars@metafoo.de>
Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org,
Matteo Martelli <matteomartelli3@gmail.com>
Subject: [PATCH] iio: fix scale application in iio_convert_raw_to_processed_unlocked
Date: Tue, 30 Jul 2024 10:11:53 +0200 [thread overview]
Message-ID: <20240730-iio-fix-scale-v1-1-6246638c8daa@gmail.com> (raw)
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@gmail.com>
---
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@linux.dev/
---
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@gmail.com>
next reply other threads:[~2024-07-30 8:12 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-07-30 8:11 Matteo Martelli [this message]
2024-08-03 15:30 ` [PATCH] iio: fix scale application in iio_convert_raw_to_processed_unlocked Jonathan Cameron
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240730-iio-fix-scale-v1-1-6246638c8daa@gmail.com \
--to=matteomartelli3@gmail.com \
--cc=jic23@kernel.org \
--cc=lars@metafoo.de \
--cc=linux-iio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox