Re: [PATCH v2 2/5] iio: add processed write API

From: Jonathan Cameron

Date: Sun Sep 28 2025 - 05:08:08 EST


On Thu, 25 Sep 2025 14:37:34 +0200
Romain Gantois <romain.gantois@xxxxxxxxxxx> wrote:

> Add a function to allow IIO consumers to write a processed value to a
> channel.
>
> Signed-off-by: Romain Gantois <romain.gantois@xxxxxxxxxxx>
Just one trivial thing I noticed.
Thanks for adding the unit tests btw. Makes me much more confident we don't have
the corner case sign errors that plagued the similar functions before unit tests and fixes
were applied.

J
> ---
> drivers/iio/inkern.c | 120 +++++++++++++++++++++++++++++++++++++++++++
> include/linux/iio/consumer.h | 36 +++++++++++++
> 2 files changed, 156 insertions(+)
>
> diff --git a/drivers/iio/inkern.c b/drivers/iio/inkern.c
> index 2a1ecef2b82007f5ee8e8d0f8b35846bc4d2f94a..a6ec724b7c1fb197e6261c1162f8315deda20fd7 100644
> --- a/drivers/iio/inkern.c
> +++ b/drivers/iio/inkern.c
> @@ -631,6 +631,57 @@ int iio_multiply_value(int *result, s64 multiplier,
> }
> EXPORT_SYMBOL_GPL(iio_multiply_value);
>
> +int iio_divide_by_value(int *result, s64 numerator,
> + unsigned int type, int val, int val2)
> +{
> + s64 tmp_num, tmp_den;
> +
> + switch (type) {
> + case IIO_VAL_INT:
> + tmp_num = numerator;
> + tmp_den = val;
> + break;
> + case IIO_VAL_INT_PLUS_MICRO:
> + case IIO_VAL_INT_PLUS_NANO:
> + switch (type) {
> + case IIO_VAL_INT_PLUS_MICRO:
> + tmp_num = MICRO;
> + tmp_den = MICRO;
> + break;
> +
> + case IIO_VAL_INT_PLUS_NANO:
> + tmp_num = NANO;
> + tmp_den = NANO;
> + break;
> + }
> +
> + tmp_num *= (s64)numerator;

Seems to be casting an s64 to an s64.

> + tmp_den = (s64)abs(val) * tmp_den + (s64)abs(val2);
> +
> + if (val < 0 || val2 < 0)
> + tmp_num *= -1;
> +
> + break;
> + case IIO_VAL_FRACTIONAL:
> + tmp_num = (s64)numerator * (s64)val2;
> + tmp_den = val;
> + break;
> + case IIO_VAL_FRACTIONAL_LOG2:
> + tmp_num = (s64)numerator << val2;
> + tmp_den = val;
> + break;
> + default:
> + return -EINVAL;
> + }
> +
> + if (!tmp_den)
> + return -ERANGE;
> +
> + *result = div64_s64(tmp_num, tmp_den);
> +
> + return IIO_VAL_INT;
> +}