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

From: Romain Gantois

Date: Tue Nov 18 2025 - 10:16:33 EST


Hello Andy,

On Thursday, 6 November 2025 17:07:18 CET Andy Shevchenko wrote:
> On Thu, Nov 06, 2025 at 03:11:47PM +0100, Romain Gantois wrote:
> > Add a function to allow IIO consumers to write a processed value to a
> > channel.
>
> ...
>
> > +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 *= numerator;
> > + tmp_den = (s64)abs(val) * tmp_den + (s64)abs(val2);
>
> Here is a subtle bug. The problematic piece is abs(). See
> https://lore.kernel.org/r/20251106152051.2361551-1-andriy.shevchenko@linux.i
> ntel.com for the answer.

Oh wow, that's a nasty one indeed.

> > +EXPORT_SYMBOL_GPL(iio_write_channel_processed_scale);
>
> Can we start using namespaced exports?
>

Sounds good, but won't it look strange to have only
iio_write_channel_processed_scale() use a namespaced export?

...
> > +/**
> > + * iio_write_channel_processed_scale() - scale and write processed value
> > to a given channel + * @chan: The channel being queried.
> > + * @val: Value to write.
> > + * @scale: Processed value is divided by this scale factor
during the
> > conversion. + *
> > + * This function writes a processed value to a channel. A processed value
> > means + * that this value will have the correct unit and not some device
> > internal + * representation. If the device does not support writing a
> > processed value, the + * function will query the channel's scale and
> > offset and write an appropriately + * transformed raw value.
> >
> > + * Context: May sleep.
>
> The above kernel-doc doesn't have this!
>
> > + * Return: an error code or 0.
>
> Be consistent with the existing code, and even in your own change.
>
> ("Return" section name, "Context" section presence, etc.)

I'll match the "Return" section with what I used for iio_divide_by_value()
then, since the format used for iio_read_channel_label() is broken.

>
> Use Perl (original) kernel-doc for now, the Python has a significant
> regression (the fix is pending to go to Linus' branch).

Thanks for the heads up.

--
Romain Gantois, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com

Attachment: signature.asc
Description: This is a digitally signed message part.