From: Hans de Goede <hansg@kernel.org>
To: Jonathan Cameron <jic23@kernel.org>,
David Lechner <dlechner@baylibre.com>
Cc: "Hans de Goede" <hansg@kernel.org>,
"Nuno Sá" <nuno.sa@analog.com>,
"Andy Shevchenko" <andy@kernel.org>,
linux-iio@vger.kernel.org
Subject: [PATCH v3 1/2] iio: Improve iio_read_channel_processed_scale() precision
Date: Sun, 27 Jul 2025 23:06:38 +0200 [thread overview]
Message-ID: <20250727210639.196351-2-hansg@kernel.org> (raw)
In-Reply-To: <20250727210639.196351-1-hansg@kernel.org>
Before this change iio_read_channel_processed_scale() always assumes that
channels which advertise IIO_CHAN_INFO_PROCESSED capability return
IIO_VAL_INT on success.
Ignoring any fractional values from drivers which return
IIO_VAL_INT_PLUS_MICRO / IIO_VAL_INT_PLUS_NANO. These fractional values
might become non fractional after scaling so these should be taken into
account.
While at it also error out for IIO_VAL_* values which
iio_read_channel_processed_scale() does not know how to handle.
Signed-off-by: Hans de Goede <hansg@kernel.org>
---
Changes in v3:
- Use div_s64() instead of div_u64() to fix -1.0 - 0.0 range
- Directly return IIO_VAL_INT from valid cases and drop the final
return ret after the switch-case
Changes in v2:
- New patch in v2 of this patch-series
---
drivers/iio/inkern.c | 24 ++++++++++++++++++++----
1 file changed, 20 insertions(+), 4 deletions(-)
diff --git a/drivers/iio/inkern.c b/drivers/iio/inkern.c
index c174ebb7d5e6..46900be16ff8 100644
--- a/drivers/iio/inkern.c
+++ b/drivers/iio/inkern.c
@@ -714,20 +714,36 @@ int iio_read_channel_processed_scale(struct iio_channel *chan, int *val,
unsigned int scale)
{
struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
- int ret;
+ int ret, val2;
guard(mutex)(&iio_dev_opaque->info_exist_lock);
if (!chan->indio_dev->info)
return -ENODEV;
if (iio_channel_has_info(chan->channel, IIO_CHAN_INFO_PROCESSED)) {
- ret = iio_channel_read(chan, val, NULL,
+ ret = iio_channel_read(chan, val, &val2,
IIO_CHAN_INFO_PROCESSED);
if (ret < 0)
return ret;
- *val *= scale;
- return ret;
+ switch (ret) {
+ case IIO_VAL_INT:
+ *val *= scale;
+ return IIO_VAL_INT;
+ case IIO_VAL_INT_PLUS_MICRO:
+ *val *= scale;
+ *val += div_s64((s64)val2 * scale, 1000000LL);
+ return IIO_VAL_INT;
+ case IIO_VAL_INT_PLUS_NANO:
+ *val *= scale;
+ *val += div_s64((s64)val2 * scale, 1000000000LL);
+ return IIO_VAL_INT;
+ default:
+ dev_err_once(&chan->indio_dev->dev,
+ "unsupported processed IIO-val-type: %d\n",
+ ret);
+ return -EINVAL;
+ }
} else {
ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW);
if (ret < 0)
--
2.49.0
next prev parent reply other threads:[~2025-07-27 21:06 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-07-27 21:06 [PATCH v3 0/2] iio: adc: Add Intel Dollar Cove TI PMIC ADC driver Hans de Goede
2025-07-27 21:06 ` Hans de Goede [this message]
2025-07-29 17:26 ` [PATCH v3 1/2] iio: Improve iio_read_channel_processed_scale() precision David Lechner
2025-08-10 19:25 ` Hans de Goede
2025-08-10 21:12 ` Hans de Goede
2025-08-11 12:37 ` Andy Shevchenko
2025-08-11 14:35 ` Hans de Goede
2025-08-11 14:50 ` Andy Shevchenko
2025-07-27 21:06 ` [PATCH v3 2/2] iio: adc: Add Intel Dollar Cove TI PMIC ADC driver Hans de Goede
2025-07-28 18:41 ` Jonathan Cameron
2025-07-29 17:50 ` David Lechner
2025-07-31 11:12 ` Jonathan Cameron
2025-08-02 11:46 ` 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=20250727210639.196351-2-hansg@kernel.org \
--to=hansg@kernel.org \
--cc=andy@kernel.org \
--cc=dlechner@baylibre.com \
--cc=jic23@kernel.org \
--cc=linux-iio@vger.kernel.org \
--cc=nuno.sa@analog.com \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.