Skip to contentSkip to conversation

iio: adc: mt6359: fix scale divisor, request bits, and MT6358 VBAT ratio

PATCH In review Ilia Snezhkov 2 participants 7 messages Nov 26

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v2 cover iio: adc: mt6359: fix scale divisor, request bits, and MT6358 VBAT ratio

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(-)
Signature · 1 line
2.54.0 (Apple Git-157)
#1 lore raw

PATCH v2 1/3 iio: adc: mt6359: divide scale by ADC resolution

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

---
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(-)
drivers/iio/adc/mt6359-auxadc.c +3 -2
index 88b0e26..54c9048 100644
@@ -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) {
Signature · 1 line
2.54.0 (Apple Git-157)
#2 lore raw

Re: [PATCH v2 1/3] iio: adc: mt6359: divide scale by ADC resolution

Hi,

Quoted 20 lines · On Thursday, November 26th, 2026 at 5:11 AM, Ilia Snezhkov via B4 Relay…

From: Ilia Snezhkov <[email protected]>

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.

Quoted 2 lines
Fixes 3587914bf61d ("iio: adc: Add support for MediaTek MT6357/8/9 Auxiliary ADC")

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/[email protected]/

Quoted 3 lines

---
Changes in v2:
- Move AI coding assistant note below ---. No code changes.

This applies to all your mediatek patches as well.

Best regards,
Roman

Quoted 31 lines

Found and drafted with an AI coding assistant.

 drivers/iio/adc/mt6359-auxadc.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/iio/adc/mt6359-auxadc.c +3 -3
index 88b0e26..54c9048 100644
@@ -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)

#3 lore raw

PATCH v2 2/3 iio: adc: mt6359: fix MT6357 ISENSE/VCDT and MT6358 VCDT request bits

In reply to Ilia Snezhkov

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

---
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(-)
drivers/iio/adc/mt6359-auxadc.c +3 -3
index 54c9048..4a66253 100644
@@ -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),
Signature · 1 line
2.54.0 (Apple Git-157)
Tested-by Reviewed-by on v2 2/3
#4 lore raw

Re: [PATCH v2 2/3] iio: adc: mt6359: fix MT6357 ISENSE/VCDT and MT6358 VCDT request bits

Quoted 8 lines · On Thursday, November 26th, 2026 at 5:11 AM, Ilia Snezhkov via B4 Relay…

From: Ilia Snezhkov <[email protected]>

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.

Quoted 7 lines

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

---

VCDT timeout is gone.

Best regards,
Roman

#5 lore raw

PATCH v2 3/3 iio: adc: mt6359: fix MT6358 VBAT impedance resistance ratio

In reply to Ilia Snezhkov

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

---
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(-)
drivers/iio/adc/mt6359-auxadc.c +1 -1
index 4a66253..915378c 100644
@@ -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[] = {
Signature · 1 line
2.54.0 (Apple Git-157)
#6 lore raw

Re: [PATCH v2 3/3] iio: adc: mt6359: fix MT6358 VBAT impedance resistance ratio

Quoted 8 lines · On Thursday, November 26th, 2026 at 5:11 AM, Ilia Snezhkov via B4 Relay…

From: Ilia Snezhkov <[email protected]>

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%.

Quoted 6 lines

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

---

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