Linux-ARM-Kernel Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 0/3] iio: adc: mt6359: fix scale divisor, request bits, and MT6358 VBAT ratio
@ 2026-11-26  3:10 Ilia Snezhkov via B4 Relay
  2026-11-26  3:10 ` [PATCH v2 1/3] iio: adc: mt6359: divide scale by ADC resolution Ilia Snezhkov via B4 Relay
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Ilia Snezhkov via B4 Relay @ 2026-11-26  3:10 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).

Changes in v2:
- Move the AI coding assistant note below the --- separator in all three
  patches (per Jonathan Cameron's feedback on v1). No code changes.

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] 7+ messages in thread

* [PATCH v2 1/3] iio: adc: mt6359: divide scale by ADC resolution
  2026-11-26  3:10 [PATCH v2 0/3] iio: adc: mt6359: fix scale divisor, request bits, and MT6358 VBAT ratio Ilia Snezhkov via B4 Relay
@ 2026-11-26  3:10 ` Ilia Snezhkov via B4 Relay
  2026-11-26  7:18   ` Roman Vivchar
  2026-11-26  3:10 ` [PATCH v2 2/3] iio: adc: mt6359: fix MT6357 ISENSE/VCDT and MT6358 VCDT request bits Ilia Snezhkov via B4 Relay
  2026-11-26  3:10 ` [PATCH v2 3/3] iio: adc: mt6359: fix MT6358 VBAT impedance resistance ratio Ilia Snezhkov via B4 Relay
  2 siblings, 1 reply; 7+ messages in thread
From: Ilia Snezhkov via B4 Relay @ 2026-11-26  3:10 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.

Fixes: 3587914bf61d ("iio: adc: Add support for MediaTek MT6357/8/9 Auxiliary ADC")
Signed-off-by: Ilia Snezhkov <iasnezhkov@gmail.com>
---
Changes in v2:
- Move AI coding assistant note below ---. No code changes.

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)




^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH v2 2/3] iio: adc: mt6359: fix MT6357 ISENSE/VCDT and MT6358 VCDT request bits
  2026-11-26  3:10 [PATCH v2 0/3] iio: adc: mt6359: fix scale divisor, request bits, and MT6358 VBAT ratio Ilia Snezhkov via B4 Relay
  2026-11-26  3:10 ` [PATCH v2 1/3] iio: adc: mt6359: divide scale by ADC resolution Ilia Snezhkov via B4 Relay
@ 2026-11-26  3:10 ` Ilia Snezhkov via B4 Relay
  2026-11-26  7:32   ` Roman Vivchar
  2026-11-26  3:10 ` [PATCH v2 3/3] iio: adc: mt6359: fix MT6358 VBAT impedance resistance ratio Ilia Snezhkov via B4 Relay
  2 siblings, 1 reply; 7+ messages in thread
From: Ilia Snezhkov via B4 Relay @ 2026-11-26  3:10 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).

Fixes: 3587914bf61d ("iio: adc: Add support for MediaTek MT6357/8/9 Auxiliary ADC")
Signed-off-by: Ilia Snezhkov <iasnezhkov@gmail.com>
---
Changes in v2:
- Move AI coding assistant note below ---. No code changes.

Found and drafted with an AI coding assistant.

 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] 7+ messages in thread

* [PATCH v2 3/3] iio: adc: mt6359: fix MT6358 VBAT impedance resistance ratio
  2026-11-26  3:10 [PATCH v2 0/3] iio: adc: mt6359: fix scale divisor, request bits, and MT6358 VBAT ratio Ilia Snezhkov via B4 Relay
  2026-11-26  3:10 ` [PATCH v2 1/3] iio: adc: mt6359: divide scale by ADC resolution Ilia Snezhkov via B4 Relay
  2026-11-26  3:10 ` [PATCH v2 2/3] iio: adc: mt6359: fix MT6357 ISENSE/VCDT and MT6358 VCDT request bits Ilia Snezhkov via B4 Relay
@ 2026-11-26  3:10 ` Ilia Snezhkov via B4 Relay
  2026-11-26  7:35   ` Roman Vivchar
  2 siblings, 1 reply; 7+ messages in thread
From: Ilia Snezhkov via B4 Relay @ 2026-11-26  3:10 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.

Fixes: 3587914bf61d ("iio: adc: Add support for MediaTek MT6357/8/9 Auxiliary ADC")
Signed-off-by: Ilia Snezhkov <iasnezhkov@gmail.com>
---
Changes in v2:
- Move AI coding assistant note below ---. No code changes.

Found and drafted with an AI coding assistant.

 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] 7+ messages in thread

* Re: [PATCH v2 1/3] iio: adc: mt6359: divide scale by ADC resolution
  2026-11-26  3:10 ` [PATCH v2 1/3] iio: adc: mt6359: divide scale by ADC resolution Ilia Snezhkov via B4 Relay
@ 2026-11-26  7:18   ` Roman Vivchar
  0 siblings, 0 replies; 7+ messages in thread
From: Roman Vivchar @ 2026-11-26  7:18 UTC (permalink / raw)
  To: iasnezhkov
  Cc: jic23, dlechner, nuno.sa, andy, matthias.bgg,
	angelogioacchino.delregno, linux-iio, linux-mediatek,
	linux-arm-kernel, linux-kernel

Hi,

On Thursday, November 26th, 2026 at 5:11 AM, 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.
> 
> Fixes: 3587914bf61d ("iio: adc: Add support for MediaTek MT6357/8/9 Auxiliary ADC")
> Signed-off-by: Ilia Snezhkov <iasnezhkov@gmail.com>

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@protonmail.com/

> ---
> 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)
> 
> 
> 
>


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v2 2/3] iio: adc: mt6359: fix MT6357 ISENSE/VCDT and MT6358 VCDT request bits
  2026-11-26  3:10 ` [PATCH v2 2/3] iio: adc: mt6359: fix MT6357 ISENSE/VCDT and MT6358 VCDT request bits Ilia Snezhkov via B4 Relay
@ 2026-11-26  7:32   ` Roman Vivchar
  0 siblings, 0 replies; 7+ messages in thread
From: Roman Vivchar @ 2026-11-26  7:32 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 Thursday, November 26th, 2026 at 5:11 AM, Ilia Snezhkov via B4 Relay <devnull+iasnezhkov.gmail.com@kernel.org> wrote:

> 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).
> 
> Fixes: 3587914bf61d ("iio: adc: Add support for MediaTek MT6357/8/9 Auxiliary ADC")
> Signed-off-by: Ilia Snezhkov <iasnezhkov@gmail.com>
> ---

VCDT timeout is gone.

Tested-by: Roman Vivchar <rva333@protonmail.com> # mt6358
Reviewed-by: Roman Vivchar <rva333@protonmail.com>

Best regards,
Roman


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v2 3/3] iio: adc: mt6359: fix MT6358 VBAT impedance resistance ratio
  2026-11-26  3:10 ` [PATCH v2 3/3] iio: adc: mt6359: fix MT6358 VBAT impedance resistance ratio Ilia Snezhkov via B4 Relay
@ 2026-11-26  7:35   ` Roman Vivchar
  0 siblings, 0 replies; 7+ messages in thread
From: Roman Vivchar @ 2026-11-26  7:35 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 Thursday, November 26th, 2026 at 5:11 AM, Ilia Snezhkov via B4 Relay <devnull+iasnezhkov.gmail.com@kernel.org> wrote:

> 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.
> 
> Fixes: 3587914bf61d ("iio: adc: Add support for MediaTek MT6357/8/9 Auxiliary ADC")
> Signed-off-by: Ilia Snezhkov <iasnezhkov@gmail.com>
> ---

Although this change looks correct to me, it's a no-op given that it's
not possible to read the channel.

~ # cat /sys/bus/iio/devices/iio:device0/in_voltage13_raw
cat: read error: Operation timed out
~ # [  243.402155] mt6359-auxadc mt6359-auxadc: Resetting stuck ADC!

You might want to fix it in the previous patch.

Best regards,
Roman


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2026-11-26  7:40 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-26  3:10 [PATCH v2 0/3] iio: adc: mt6359: fix scale divisor, request bits, and MT6358 VBAT ratio Ilia Snezhkov via B4 Relay
2026-11-26  3:10 ` [PATCH v2 1/3] iio: adc: mt6359: divide scale by ADC resolution Ilia Snezhkov via B4 Relay
2026-11-26  7:18   ` Roman Vivchar
2026-11-26  3:10 ` [PATCH v2 2/3] iio: adc: mt6359: fix MT6357 ISENSE/VCDT and MT6358 VCDT request bits Ilia Snezhkov via B4 Relay
2026-11-26  7:32   ` Roman Vivchar
2026-11-26  3:10 ` [PATCH v2 3/3] iio: adc: mt6359: fix MT6358 VBAT impedance resistance ratio Ilia Snezhkov via B4 Relay
2026-11-26  7:35   ` Roman Vivchar

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox