Re: [PATCH v2 1/3] iio: adc: mt6359: divide scale by ADC resolution
From: Roman Vivchar
Date: Thu Nov 26 2026 - 02:18:27 EST
Hi,
On Thursday, November 26th, 2026 at 5:11 AM, Ilia Snezhkov via B4 Relay <devnull+iasnezhkov.gmail.com@xxxxxxxxxx> wrote:
> From: Ilia Snezhkov <iasnezhkov@xxxxxxxxx>
>
> mt6359_auxadc_read_raw() returns raw ADC counts for IIO_CHAN_INFO_RAW
> (12-bit or 15-bit depending on the channel), and returns only
> (r_ratio.numerator * vref_mV) / r_ratio.denominator for
> IIO_CHAN_INFO_SCALE without dividing by the ADC full-scale range
> (1 << chan->scan_type.realbits).
>
> As a result, iio_read_channel_processed() and userspace consumers
> multiplying raw * scale obtain millivolt values that are 4096x (12-bit)
> or 32768x (15-bit) too large.
>
> Include the 1 << chan->scan_type.realbits divisor in
> IIO_CHAN_INFO_SCALE: shift r_ratio.denominator left by realbits for
> IIO_VAL_FRACTIONAL when denominator > 1 (r_ratio.denominator is u8 and
> realbits is at most 15, so the result fits comfortably in int), and
> return IIO_VAL_FRACTIONAL_LOG2 with realbits otherwise so channels with
> denominator == 1 use an exact shift and unpopulated descriptors (such
> as MT6357 VDCXO where denominator == 0) cannot trigger division by zero.
>
> Fixes: 3587914bf61d ("iio: adc: Add support for MediaTek MT6357/8/9 Auxiliary ADC")
> Signed-off-by: Ilia Snezhkov <iasnezhkov@xxxxxxxxx>
This fix was already submitted [1]. Please check mailing list for
existing patches first.
Also, wait at least 24 hours before sending v2 for non-trivial series.
[1] https://lore.kernel.org/linux-mediatek/20261117-6768-6358-fixes-v2-3-1ffc3c2f3c80@xxxxxxxxxxxxxx/
> ---
> Changes in v2:
> - Move AI coding assistant note below ---. No code changes.
This applies to all your mediatek patches as well.
Best regards,
Roman
>
> Found and drafted with an AI coding assistant.
>
> drivers/iio/adc/mt6359-auxadc.c | 5 +++--
> 1 file changed, 3 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/iio/adc/mt6359-auxadc.c b/drivers/iio/adc/mt6359-auxadc.c
> index 88b0e26..54c9048 100644
> --- a/drivers/iio/adc/mt6359-auxadc.c
> +++ b/drivers/iio/adc/mt6359-auxadc.c
> @@ -772,11 +772,12 @@ static int mt6359_auxadc_read_raw(struct iio_dev *indio_dev,
> *val = desc->r_ratio.numerator * cinfo->vref_mV;
>
> if (desc->r_ratio.denominator > 1) {
> - *val2 = desc->r_ratio.denominator;
> + *val2 = (int)desc->r_ratio.denominator << chan->scan_type.realbits;
> return IIO_VAL_FRACTIONAL;
> }
>
> - return IIO_VAL_INT;
> + *val2 = chan->scan_type.realbits;
> + return IIO_VAL_FRACTIONAL_LOG2;
> }
>
> scoped_guard(mutex, &adc_dev->lock) {
> --
> 2.54.0 (Apple Git-157)
>
>
>
>