* [PATCH 0/3] iio: adc: mt6359: fix scale divisor, request bits, and MT6358 VBAT ratio
@ 2026-11-26 0:53 Ilia Snezhkov via B4 Relay
2026-11-26 0:53 ` [PATCH 1/3] iio: adc: mt6359: divide scale by ADC resolution Ilia Snezhkov via B4 Relay
` (2 more replies)
0 siblings, 3 replies; 6+ messages in thread
From: Ilia Snezhkov via B4 Relay @ 2026-11-26 0:53 UTC (permalink / raw)
To: jic23, dlechner, nuno.sa, andy
Cc: matthias.bgg, angelogioacchino.delregno, linux-iio,
linux-mediatek, linux-arm-kernel, linux-kernel
This short series fixes three bugs in drivers/iio/adc/mt6359-auxadc.c
introduced in commit 3587914bf61d ("iio: adc: Add support for MediaTek
MT6357/8/9 Auxiliary ADC"):
1. IIO_CHAN_INFO_SCALE omits dividing by (1 << chan->scan_type.realbits),
causing processed readings and sysfs scales to be 4096x or 32768x too
large. Use IIO_VAL_FRACTIONAL with shifted denominator when
denominator > 1, and IIO_VAL_FRACTIONAL_LOG2 otherwise (avoiding
division by zero on unpopulated descriptors such as MT6357 VDCXO).
2. MT6357 ISENSE/VCDT and MT6358 VCDT use PMIC_AUXADC_RQST0 bit 0
(BATADC) instead of their hardware request bits (1 and 2).
3. MT6358 VBAT impedance uses r_ratio 7/2 (MT6359) instead of 3/1
(MT6358/MT6366, matching MT6358 BATADC).
Found and drafted with an AI coding assistant.
Ilia Snezhkov (3):
iio: adc: mt6359: divide scale by ADC resolution
iio: adc: mt6359: fix MT6357 ISENSE/VCDT and MT6358 VCDT request bits
iio: adc: mt6359: fix MT6358 VBAT impedance resistance ratio
drivers/iio/adc/mt6359-auxadc.c | 13 +++++++------
1 file changed, 7 insertions(+), 6 deletions(-)
--
2.54.0 (Apple Git-157)
^ permalink raw reply [flat|nested] 6+ messages in thread* [PATCH 1/3] iio: adc: mt6359: divide scale by ADC resolution 2026-11-26 0:53 [PATCH 0/3] iio: adc: mt6359: fix scale divisor, request bits, and MT6358 VBAT ratio Ilia Snezhkov via B4 Relay @ 2026-11-26 0:53 ` Ilia Snezhkov via B4 Relay 2026-11-26 1:35 ` Jonathan Cameron 2026-11-26 14:03 ` Andy Shevchenko 2026-11-26 0:53 ` [PATCH 2/3] iio: adc: mt6359: fix MT6357 ISENSE/VCDT and MT6358 VCDT request bits Ilia Snezhkov via B4 Relay 2026-11-26 0:53 ` [PATCH 3/3] iio: adc: mt6359: fix MT6358 VBAT impedance resistance ratio Ilia Snezhkov via B4 Relay 2 siblings, 2 replies; 6+ messages in thread From: Ilia Snezhkov via B4 Relay @ 2026-11-26 0:53 UTC (permalink / raw) To: jic23, dlechner, nuno.sa, andy Cc: matthias.bgg, angelogioacchino.delregno, linux-iio, linux-mediatek, linux-arm-kernel, linux-kernel From: Ilia Snezhkov <iasnezhkov@gmail.com> 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. Found and drafted with an AI coding assistant. Fixes: 3587914bf61d ("iio: adc: Add support for MediaTek MT6357/8/9 Auxiliary ADC") Signed-off-by: Ilia Snezhkov <iasnezhkov@gmail.com> --- 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) ^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH 1/3] iio: adc: mt6359: divide scale by ADC resolution 2026-11-26 0:53 ` [PATCH 1/3] iio: adc: mt6359: divide scale by ADC resolution Ilia Snezhkov via B4 Relay @ 2026-11-26 1:35 ` Jonathan Cameron 2026-11-26 14:03 ` Andy Shevchenko 1 sibling, 0 replies; 6+ messages in thread From: Jonathan Cameron @ 2026-11-26 1:35 UTC (permalink / raw) To: Ilia Snezhkov via B4 Relay Cc: iasnezhkov, dlechner, nuno.sa, andy, matthias.bgg, angelogioacchino.delregno, linux-iio, linux-mediatek, linux-arm-kernel, linux-kernel On Thu, 26 Nov 2026 00:53:27 +0000 Ilia Snezhkov via B4 Relay <devnull+iasnezhkov.gmail.com@kernel.org> wrote: > From: Ilia Snezhkov <iasnezhkov@gmail.com> > > 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. > > Found and drafted with an AI coding assistant. This details now belongs below the --- The idea is we won't care much about that going forwards, so let us not put it in the commit log. > > Fixes: 3587914bf61d ("iio: adc: Add support for MediaTek MT6357/8/9 Auxiliary ADC") > Signed-off-by: Ilia Snezhkov <iasnezhkov@gmail.com> > --- > 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) { ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH 1/3] iio: adc: mt6359: divide scale by ADC resolution 2026-11-26 0:53 ` [PATCH 1/3] iio: adc: mt6359: divide scale by ADC resolution Ilia Snezhkov via B4 Relay 2026-11-26 1:35 ` Jonathan Cameron @ 2026-11-26 14:03 ` Andy Shevchenko 1 sibling, 0 replies; 6+ messages in thread From: Andy Shevchenko @ 2026-11-26 14:03 UTC (permalink / raw) To: iasnezhkov Cc: jic23, dlechner, nuno.sa, andy, matthias.bgg, angelogioacchino.delregno, linux-iio, linux-mediatek, linux-arm-kernel, linux-kernel On Thu, Nov 26, 2026 at 12:53:27AM +0000, Ilia Snezhkov via B4 Relay wrote: > > 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. > Found and drafted with an AI coding assistant. Move this to be below the '---' line. > Fixes: 3587914bf61d ("iio: adc: Add support for MediaTek MT6357/8/9 Auxiliary ADC") > Signed-off-by: Ilia Snezhkov <iasnezhkov@gmail.com> > --- ... > + *val2 = (int)desc->r_ratio.denominator << chan->scan_type.realbits; Make the struct to be s8_fract instead? -- With Best Regards, Andy Shevchenko ^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH 2/3] iio: adc: mt6359: fix MT6357 ISENSE/VCDT and MT6358 VCDT request bits 2026-11-26 0:53 [PATCH 0/3] iio: adc: mt6359: fix scale divisor, request bits, and MT6358 VBAT ratio Ilia Snezhkov via B4 Relay 2026-11-26 0:53 ` [PATCH 1/3] iio: adc: mt6359: divide scale by ADC resolution Ilia Snezhkov via B4 Relay @ 2026-11-26 0:53 ` Ilia Snezhkov via B4 Relay 2026-11-26 0:53 ` [PATCH 3/3] iio: adc: mt6359: fix MT6358 VBAT impedance resistance ratio Ilia Snezhkov via B4 Relay 2 siblings, 0 replies; 6+ messages in thread From: Ilia Snezhkov via B4 Relay @ 2026-11-26 0:53 UTC (permalink / raw) To: jic23, dlechner, nuno.sa, andy Cc: matthias.bgg, angelogioacchino.delregno, linux-iio, linux-mediatek, linux-arm-kernel, linux-kernel From: Ilia Snezhkov <iasnezhkov@gmail.com> mt6357_auxadc_ch_desc[] sets req_bit to 0 for both ISENSE and VCDT, and mt6358_auxadc_ch_desc[] sets req_bit to 0 for VCDT. Bit 0 of PMIC_AUXADC_RQST0 triggers BATADC (ADC0) rather than ISENSE (ADC1) or VCDT (ADC2), so polling the ready bit of ADC1/ADC2 times out unless the channel was triggered externally. Use the actual hardware request bits in PMIC_AUXADC_RQST0: bit 1 for MT6357 ISENSE and bit 2 for MT6357/MT6358 VCDT (matching MT6363 VCDT and the MediaTek PMIC AUXADC hardware registers). Found and drafted with an AI coding assistant. Fixes: 3587914bf61d ("iio: adc: Add support for MediaTek MT6357/8/9 Auxiliary ADC") Signed-off-by: Ilia Snezhkov <iasnezhkov@gmail.com> --- drivers/iio/adc/mt6359-auxadc.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/drivers/iio/adc/mt6359-auxadc.c b/drivers/iio/adc/mt6359-auxadc.c index 54c9048..4a66253 100644 --- a/drivers/iio/adc/mt6359-auxadc.c +++ b/drivers/iio/adc/mt6359-auxadc.c @@ -224,8 +224,8 @@ static const struct iio_chan_spec mt6357_auxadc_channels[] = { static const struct mtk_pmic_auxadc_chan mt6357_auxadc_ch_desc[] = { MTK_PMIC_ADC_CHAN(BATADC, PMIC_AUXADC_RQST0, 0, PMIC_AUXADC_IMP0, 8, 128, 3, 1), - MTK_PMIC_ADC_CHAN(ISENSE, PMIC_AUXADC_RQST0, 0, PMIC_AUXADC_IMP0, 8, 128, 3, 1), - MTK_PMIC_ADC_CHAN(VCDT, PMIC_AUXADC_RQST0, 0, PMIC_AUXADC_IMP0, 8, 8, 1, 1), + MTK_PMIC_ADC_CHAN(ISENSE, PMIC_AUXADC_RQST0, 1, PMIC_AUXADC_IMP0, 8, 128, 3, 1), + MTK_PMIC_ADC_CHAN(VCDT, PMIC_AUXADC_RQST0, 2, PMIC_AUXADC_IMP0, 8, 8, 1, 1), MTK_PMIC_ADC_CHAN(BAT_TEMP, PMIC_AUXADC_RQST0, 3, PMIC_AUXADC_IMP0, 8, 8, 1, 1), MTK_PMIC_ADC_CHAN(CHIP_TEMP, PMIC_AUXADC_RQST0, 4, PMIC_AUXADC_IMP0, 8, 8, 1, 1), MTK_PMIC_ADC_CHAN(ACCDET, PMIC_AUXADC_RQST0, 5, PMIC_AUXADC_IMP0, 8, 8, 1, 1), @@ -271,7 +271,7 @@ static const struct iio_chan_spec mt6358_auxadc_channels[] = { static const struct mtk_pmic_auxadc_chan mt6358_auxadc_ch_desc[] = { MTK_PMIC_ADC_CHAN(BATADC, PMIC_AUXADC_RQST0, 0, PMIC_AUXADC_IMP0, 8, 128, 3, 1), - MTK_PMIC_ADC_CHAN(VCDT, PMIC_AUXADC_RQST0, 0, PMIC_AUXADC_IMP0, 8, 8, 1, 1), + MTK_PMIC_ADC_CHAN(VCDT, PMIC_AUXADC_RQST0, 2, PMIC_AUXADC_IMP0, 8, 8, 1, 1), MTK_PMIC_ADC_CHAN(BAT_TEMP, PMIC_AUXADC_RQST0, 3, PMIC_AUXADC_IMP0, 8, 8, 2, 1), MTK_PMIC_ADC_CHAN(CHIP_TEMP, PMIC_AUXADC_RQST0, 4, PMIC_AUXADC_IMP0, 8, 8, 1, 1), MTK_PMIC_ADC_CHAN(ACCDET, PMIC_AUXADC_RQST0, 5, PMIC_AUXADC_IMP0, 8, 8, 1, 1), -- 2.54.0 (Apple Git-157) ^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH 3/3] iio: adc: mt6359: fix MT6358 VBAT impedance resistance ratio 2026-11-26 0:53 [PATCH 0/3] iio: adc: mt6359: fix scale divisor, request bits, and MT6358 VBAT ratio Ilia Snezhkov via B4 Relay 2026-11-26 0:53 ` [PATCH 1/3] iio: adc: mt6359: divide scale by ADC resolution Ilia Snezhkov via B4 Relay 2026-11-26 0:53 ` [PATCH 2/3] iio: adc: mt6359: fix MT6357 ISENSE/VCDT and MT6358 VCDT request bits Ilia Snezhkov via B4 Relay @ 2026-11-26 0:53 ` Ilia Snezhkov via B4 Relay 2 siblings, 0 replies; 6+ messages in thread From: Ilia Snezhkov via B4 Relay @ 2026-11-26 0:53 UTC (permalink / raw) To: jic23, dlechner, nuno.sa, andy Cc: matthias.bgg, angelogioacchino.delregno, linux-iio, linux-mediatek, linux-arm-kernel, linux-kernel From: Ilia Snezhkov <iasnezhkov@gmail.com> On MT6358 (and MT6366), both BATADC and the battery impedance VBAT channel use a 3/1 voltage divider (5400 mV full scale with 1800 mV VREF). mt6358_auxadc_ch_desc[PMIC_AUXADC_CHAN_VBAT] erroneously uses 7/2 (copied from MT6359), over-reporting VBAT impedance readings by 16.7%. Change MT6358 VBAT r_ratio from 7/2 to 3/1 to match MT6358 BATADC and hardware. Found and drafted with an AI coding assistant. Fixes: 3587914bf61d ("iio: adc: Add support for MediaTek MT6357/8/9 Auxiliary ADC") Signed-off-by: Ilia Snezhkov <iasnezhkov@gmail.com> --- drivers/iio/adc/mt6359-auxadc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/iio/adc/mt6359-auxadc.c b/drivers/iio/adc/mt6359-auxadc.c index 4a66253..915378c 100644 --- a/drivers/iio/adc/mt6359-auxadc.c +++ b/drivers/iio/adc/mt6359-auxadc.c @@ -285,7 +285,7 @@ static const struct mtk_pmic_auxadc_chan mt6358_auxadc_ch_desc[] = { MTK_PMIC_ADC_CHAN(VGPU_TEMP, PMIC_AUXADC_RQST1, 10, PMIC_AUXADC_IMP0, 8, 8, 1, 1), /* Battery impedance channels */ - MTK_PMIC_ADC_CHAN(VBAT, 0, 0, PMIC_AUXADC_IMP0, 8, 128, 7, 2), + MTK_PMIC_ADC_CHAN(VBAT, 0, 0, PMIC_AUXADC_IMP0, 8, 128, 3, 1), }; static const u16 mt6358_auxadc_regs[] = { -- 2.54.0 (Apple Git-157) ^ permalink raw reply related [flat|nested] 6+ messages in thread
end of thread, other threads:[~2026-11-26 14:04 UTC | newest] Thread overview: 6+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2026-11-26 0:53 [PATCH 0/3] iio: adc: mt6359: fix scale divisor, request bits, and MT6358 VBAT ratio Ilia Snezhkov via B4 Relay 2026-11-26 0:53 ` [PATCH 1/3] iio: adc: mt6359: divide scale by ADC resolution Ilia Snezhkov via B4 Relay 2026-11-26 1:35 ` Jonathan Cameron 2026-11-26 14:03 ` Andy Shevchenko 2026-11-26 0:53 ` [PATCH 2/3] iio: adc: mt6359: fix MT6357 ISENSE/VCDT and MT6358 VCDT request bits Ilia Snezhkov via B4 Relay 2026-11-26 0:53 ` [PATCH 3/3] iio: adc: mt6359: fix MT6358 VBAT impedance resistance ratio Ilia Snezhkov via B4 Relay
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox