* [PATCH v5 0/2] iio: adc: add mt6397 PMIC AUXADC support
@ 2026-10-25 5:18 Ryan Brue
2026-10-25 5:18 ` [PATCH v5 1/2] dt-bindings: iio: adc: mediatek,mt6359-auxadc: add mt6397 PMIC AUXADC Ryan Brue
` (3 more replies)
0 siblings, 4 replies; 8+ messages in thread
From: Ryan Brue @ 2026-10-25 5:18 UTC (permalink / raw)
To: Jonathan Cameron, David Lechner, Nuno Sá,
Andy Shevchenko, Rob Herring, Krzysztof Kozlowski, Conor Dooley,
Matthias Brugger, AngeloGioacchino Del Regno
Cc: Roman Vivchar, Luca Leonardo Scorcia, linux-iio, devicetree,
linux-kernel, linux-arm-kernel, linux-mediatek, Ryan Brue,
Krzysztof Kozlowski, Andy Shevchenko
The MediaTek mt6397 PMIC has a 10-bit AUXADC that nothing in-tree can
reach. On boards built around it that ADC is the only path to the battery:
the SoC's AUXADC is wired to board thermistors, and the charger ICs these
boards use have no ADC at all, so without it there is no pack voltage and
no state of charge.
Patch 1 adds the compatible to the existing MediaTek PMIC AUXADC schema and
the header naming the channels, patch 2 the driver. The MFD cell that
instantiates it was patch 3 of v4, which Lee has applied [1].
The mt6397 is not given mediatek,mt6359-auxadc as a fallback because the
two are not register compatible; patch 1 has the detail.
Only the two channels a board needs for its pack are described, so patch 1
ships a header naming them: a channel ID is an index into the driver's
array and not the PMIC's channel number, as in mt6323-auxadc. The register
sequences are the ones the vendor kernel's pmic_auxadc.c programs.
Tested on an Amazon Fire HD 10 (2017), an mt8173 with an mt6397. Both
channels are exercised: the battery channel by a fuel gauge, which reads
817 counts, 3830 mV with the pack at rest, and the thermistor channel by an
ntc-thermistor consuming it. The error paths are driven by injecting a
regmap failure into each helper in turn. The read returns the injected
error, and every bit is cleared again. The teardown writes were also made
to fail, one at a time. When one write fails, the others still run, and
only the failed write's bits are left set. The next read clears them
anyway. Run the same way, v3 stopped at the first failed write and left the
later bits set. v5 gives the same results as v4 in all of these. Loading
the two modules alternately on the board, their readings differ by 0.3 LSB
on the battery channel and 0.1 LSB on the thermistor, both inside the
comparison's own noise.
A new driver was created here, instead of modifying an existing driver such
as mt6323-auxadc or mt6359-auxadc, for the following reasons:
- Both mt6323-auxadc and mt6359-auxadc select channels through a request
register (1 bit per channel), while mt6397 uses a 4-bit numeric field
CHSEL in CON1 (10:7), and then pulses a START bit (CON1 bit 0).
- For mt6323-auxadc, which is the closest I could find to the mt6397
(CON0..CON27), it has 13 more registers than the mt6397 (CON0..CON14).
It uses CON22 for its request register, and reads the result value
from the same register as the ready bit. We don't do that - the mt6397
has a factory-calibrated value for each channel at 0x16 higher than the
raw value. mt6323 also has a 1800 mV / 15 bit scale / resolution while
we have 1200 mV / 10 bits. We also have some per-channel preparation
that we have to do before the burst, that the mt6323 doesn't have to
do.
- For mt6359-auxadc, it has a more generic framework for describing the
AUXADC, but it assumes requests are channel-per-bit, and so we would
have to basically ignore req_idx, req_mask, rdy_idx, and rdy_mask.
- We also have our own software sampling, which the vendor does too
(Amazon Fire OS based on Linux 3.18). We'd have to have our own
sampling callback to do it.
I have two things in the driver different from the vendor (Amazon
Fire OS based on a 3.18 kernel). Verified with over 150 interleaved reads
per setting:
- Leaving the ISENSE input enabled between reads, as the vendor does,
makes no difference to the value, so the driver switches it off again.
Both ADC input enables are clear when idle, unless a teardown write
itself fails: the rest of the teardown still runs, and the next read of
the channel clears whatever was left set.
- The chip's sample accumulator makes no difference at 4, 8, 16 or 31
samples and costs no measurable time, so it is left at one. Software
averaging helps, but only by about a quarter: in one interleaved run,
0.92 LSB standard deviation for one conversion against 0.67 for sixteen,
where independent samples would have given 0.23. Conversions in a burst
are correlated.
The reading was checked against the charger, which regulates the pack to
a programmed voltage in constant-voltage mode. Over twelve such voltages
from 3904 to 4080 mV, and charge currents from 111 to 778 mA, the driver's
reading is a mean of 0.9 mV from the regulated value, well inside
the charger's regulation tolerance. That also settles reading the chip's
trimmed result register rather than the raw one - the raw register is
20.7 mV low at every one of the twelve points.
This series was developed with the help of an LLM coding assistant.
[1] https://lore.kernel.org/all/179206667779.1910726.5341177428334836457.b4-ty@b4/
Signed-off-by: Ryan Brue <ryanbrue.dev@gmail.com>
---
Changes in v5:
- Split the read into one helper per channel, each doing its own setup,
burst and teardown, and a caller that takes the lock and dispatches on
the channel. mt6397_auxadc_release() is gone. The register sequence is
unchanged on every path, including every failure path, and retested on
hardware. (Jonathan)
- Keep Andy's Reviewed-by on patch 2, since the change is a restructure
that does not change behavior. If that's incorrect let me know.
- Drop the MFD cell patch, applied by Lee from v4.
- Rebase onto iio/testing. The MAINTAINERS entry now follows the MT6323
PMIC NVMEM one.
- Drop the Assisted-by trailers per the new kernel guidance, and put the
disclosure in the cover letter.
- Link to v4: https://patch.msgid.link/20260928-rbrue-suez-upstreaming-mt6397-auxadc-v4-0-23533a0d30e3@gmail.com
Changes in v4:
- Attempt every teardown write even if a previous one fails, so a failed
write doesn't leave the buffer, bias, or input powered. Reverses the v3
change. The teardown neither checks nor logs errors for failed writes,
similar to mt6358_stop_imp_conv(). The next read will rewrite those bits
anyway. (Angelo, sashiko-bot, Andy)
- Move teardown into mt6397_auxadc_release(), keep error paths out of line
with goto, and use mutex_lock() / mutex_unlock() rather than guard().
(Jonathan)
- Reword the comment above the BATSNS disable prior to ISENSE enable.
(Jonathan)
- Collect tags.
- Link to v3: https://patch.msgid.link/20260920-rbrue-suez-upstreaming-mt6397-auxadc-v3-0-c00edacbee64@gmail.com
Changes in v3:
- Move the sample loop into its own function, so read_channel() is setup,
then a conditional burst, then an unconditional teardown, with no goto
mixed with guard(). (Andy, sashiko-bot)
- battemp_bias_off() and isense_disable() now return on the first failed
write, as mt6323_auxadc_release() does, and twelve other teardowns in
drivers/iio that I could find. A failed write can leave the later bits
set; the teardown runs at the end of every read, so the next read of that
channel clears them. (Andy)
- Drop the "if (!ret) ret = err;" accumulators with it. A teardown failure
now goes to dev_err() rather than being returned, since the caller cannot
act on it, and read_channel() takes the iio_dev so no struct device * is
stored. (Andy)
- Split battemp_bias(adc, bool on) into battemp_bias_on() and
battemp_bias_off(), and drop the bool. (Andy)
- Make the SCALE case an if-else. (Andy)
- Move the "why a new driver" rationale below the --- marker in patch 2,
out of the commit message. (Andy)
- Reduce the CHSEL regmap_update_bits() call from four lines to two. (Andy)
- Drop the comment above guard(mutex). (Andy)
- Say in a comment why lowering START needs no settling delay. The ready
bit survives the fall and is cleared by the rise before the next
transaction on the PMIC wrapper can observe it. I measured this, and the
details are in the reply on 2/3. (Andy)
- Cc Luca Leonardo Scorcia with a trailer rather than in prose. (Andy)
- Link to v2: https://patch.msgid.link/20260917-rbrue-suez-upstreaming-mt6397-auxadc-v2-0-db35882a6080@gmail.com
Changes in v2:
- Add justification for this being a new driver rather than folded into
mt6323-auxadc.c or mt6359-auxadc.c, in the cover letter and patch 2.
- Drop the "ret = ret ?: ..." chains, and let teardown fall through on
errors, allowing the chance for all bits to be properly torn down. The
errors are now held rather than discarded. (Andy and sashiko-bot)
- Run teardown even when per-channel setup fails part-way through, so we
don't leave bits enabled that would have been cleared. (sashiko-bot)
- Set ISENSE settle time to (1 * USEC_PER_MSEC). (Andy)
- Write the two channel descriptors out instead of using a macro. Also
gets rid of the padded column. (Andy, Jonathan)
- Move the MAINTAINERS entry for the dt-bindings header into patch 1, as
the mt6323 series does.
- Declare the sample loop counter in the loop, instead of with sum, and
split the declaration of sample and ret. (Andy)
- Keep the two CHR_CON16 ADCIN bits as BIT()s rather than turning them
into values of a GENMASK field. Fix OR operator on the bits, treat them
as mutually exclusive. (Jonathan)
- The rest of Jonathan's v1 review: two-space indent separating a register's
fields from the register, a blank line before the settle times, a lock
comment that says why the lock is held, the two CON1 START writes on one
line each, the single-call init helper inlined into probe, and { } for the
of_device_id sentinel.
- Add the additional three CHR_CON16 ADCIN defines that make up the 5
single-bit enables. These are currently unused.
- Link to v1: https://patch.msgid.link/20260915-rbrue-suez-upstreaming-mt6397-auxadc-v1-0-d35d2ac3d6f0@gmail.com
---
Ryan Brue (2):
dt-bindings: iio: adc: mediatek,mt6359-auxadc: add mt6397 PMIC AUXADC
iio: adc: mt6397-auxadc: add mt6397 PMIC AUXADC driver
.../bindings/iio/adc/mediatek,mt6359-auxadc.yaml | 1 +
MAINTAINERS | 7 +
drivers/iio/adc/Kconfig | 11 +
drivers/iio/adc/Makefile | 1 +
drivers/iio/adc/mt6397-auxadc.c | 394 +++++++++++++++++++++
.../dt-bindings/iio/adc/mediatek,mt6397-auxadc.h | 9 +
6 files changed, 423 insertions(+)
---
base-commit: c9dbe2a47f635465c9985b7af74d2ae310e98125
change-id: 20260914-rbrue-suez-upstreaming-mt6397-auxadc-5e1228025c50
Best regards,
--
Ryan Brue <ryanbrue.dev@gmail.com>
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH v5 1/2] dt-bindings: iio: adc: mediatek,mt6359-auxadc: add mt6397 PMIC AUXADC
2026-10-25 5:18 [PATCH v5 0/2] iio: adc: add mt6397 PMIC AUXADC support Ryan Brue
@ 2026-10-25 5:18 ` Ryan Brue
2026-10-25 5:18 ` [PATCH v5 2/2] iio: adc: mt6397-auxadc: add mt6397 PMIC AUXADC driver Ryan Brue
` (2 subsequent siblings)
3 siblings, 0 replies; 8+ messages in thread
From: Ryan Brue @ 2026-10-25 5:18 UTC (permalink / raw)
To: Jonathan Cameron, David Lechner, Nuno Sá,
Andy Shevchenko, Rob Herring, Krzysztof Kozlowski, Conor Dooley,
Matthias Brugger, AngeloGioacchino Del Regno
Cc: Roman Vivchar, Luca Leonardo Scorcia, linux-iio, devicetree,
linux-kernel, linux-arm-kernel, linux-mediatek, Ryan Brue,
Krzysztof Kozlowski
The MediaTek mt6397 PMIC includes a 10-bit AUXADC used for battery voltage
and battery temperature measurements. It is not register compatible with
mt6359: a conversion is requested by writing a channel number to a select
field and pulsing an edge triggered start bit rather than by setting the
channel's bit in a request register, and the results live elsewhere.
It therefore gets its own compatible rather than a fallback.
Add the binding and the header naming the channels a consumer puts in
io-channels. The battery channel is named for the input it measures,
ISENSE, which is what mt6323-auxadc calls the same input.
Reviewed-by: AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com>
Acked-by: Krzysztof Kozlowski <krzysztof.kozlowski@oss.qualcomm.com>
Signed-off-by: Ryan Brue <ryanbrue.dev@gmail.com>
---
.../devicetree/bindings/iio/adc/mediatek,mt6359-auxadc.yaml | 1 +
MAINTAINERS | 6 ++++++
include/dt-bindings/iio/adc/mediatek,mt6397-auxadc.h | 9 +++++++++
3 files changed, 16 insertions(+)
diff --git a/Documentation/devicetree/bindings/iio/adc/mediatek,mt6359-auxadc.yaml b/Documentation/devicetree/bindings/iio/adc/mediatek,mt6359-auxadc.yaml
index c2f7387e4bfc..2847bd5d5bc4 100644
--- a/Documentation/devicetree/bindings/iio/adc/mediatek,mt6359-auxadc.yaml
+++ b/Documentation/devicetree/bindings/iio/adc/mediatek,mt6359-auxadc.yaml
@@ -26,6 +26,7 @@ properties:
- mediatek,mt6359-auxadc
- mediatek,mt6363-auxadc
- mediatek,mt6373-auxadc
+ - mediatek,mt6397-auxadc
- items:
- enum:
- mediatek,mt6365-auxadc
diff --git a/MAINTAINERS b/MAINTAINERS
index 02426f316faa..b5417c02eb01 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -16780,6 +16780,12 @@ M: Roman Vivchar <rva333@protonmail.com>
S: Maintained
F: drivers/nvmem/mt6323-efuse.c
+MEDIATEK MT6397 PMIC AUXADC DRIVER
+M: Ryan Brue <ryanbrue.dev@gmail.com>
+L: linux-iio@vger.kernel.org
+S: Maintained
+F: include/dt-bindings/iio/adc/mediatek,mt6397-auxadc.h
+
MEDIATEK MT6735 CLOCK & RESET DRIVERS
M: Yassine Oudjana <y.oudjana@protonmail.com>
L: linux-clk@vger.kernel.org
diff --git a/include/dt-bindings/iio/adc/mediatek,mt6397-auxadc.h b/include/dt-bindings/iio/adc/mediatek,mt6397-auxadc.h
new file mode 100644
index 000000000000..b696c1cb5ebe
--- /dev/null
+++ b/include/dt-bindings/iio/adc/mediatek,mt6397-auxadc.h
@@ -0,0 +1,9 @@
+/* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */
+
+#ifndef _DT_BINDINGS_MEDIATEK_MT6397_AUXADC_H
+#define _DT_BINDINGS_MEDIATEK_MT6397_AUXADC_H
+
+#define MT6397_AUXADC_ISENSE 0
+#define MT6397_AUXADC_BAT_TEMP 1
+
+#endif
--
2.55.0
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH v5 2/2] iio: adc: mt6397-auxadc: add mt6397 PMIC AUXADC driver
2026-10-25 5:18 [PATCH v5 0/2] iio: adc: add mt6397 PMIC AUXADC support Ryan Brue
2026-10-25 5:18 ` [PATCH v5 1/2] dt-bindings: iio: adc: mediatek,mt6359-auxadc: add mt6397 PMIC AUXADC Ryan Brue
@ 2026-10-25 5:18 ` Ryan Brue
2026-11-17 10:50 ` AngeloGioacchino Del Regno
2026-10-25 22:44 ` [PATCH v5 0/2] iio: adc: add mt6397 PMIC AUXADC support Jonathan Cameron
2026-11-17 10:56 ` AngeloGioacchino Del Regno
3 siblings, 1 reply; 8+ messages in thread
From: Ryan Brue @ 2026-10-25 5:18 UTC (permalink / raw)
To: Jonathan Cameron, David Lechner, Nuno Sá,
Andy Shevchenko, Rob Herring, Krzysztof Kozlowski, Conor Dooley,
Matthias Brugger, AngeloGioacchino Del Regno
Cc: Roman Vivchar, Luca Leonardo Scorcia, linux-iio, devicetree,
linux-kernel, linux-arm-kernel, linux-mediatek, Ryan Brue,
Andy Shevchenko
The mt6397 AUXADC is a 10-bit ADC behind the SoC's PMIC wrapper. On boards
built around this PMIC it is the only way to read the battery: the SoC's
AUXADC is wired to board thermistors and the charger ICs these boards use
have no ADC of their own.
Add a driver exposing the battery voltage and battery temperature
channels. Only those two are described, so a channel ID in the device tree
indexes the driver's channel array rather than the PMIC's channel number,
as mt6323-auxadc does. The ready bit lives in a channel's raw result
register, but the value comes from the chip's factory-calibrated copy.
The battery voltage is measured through ISENSE, because a board with a
switching charger in the power path leaves BATSNS on the charger's system
rail instead of on the pack. The thermistor only reads correctly with the
PMIC's battery-detect bias and input buffer enabled, which take 20 ms to
settle. Both are switched back off afterwards.
Reads average sixteen conversions in software; the chip's sample
accumulator makes no measurable difference at any setting, so it is left
at one sample per conversion.
Reviewed-by: Andy Shevchenko <andriy.shevchenko@intel.com>
Signed-off-by: Ryan Brue <ryanbrue.dev@gmail.com>
---
A new driver was created here, instead of modifying an existing driver such
as mt6323-auxadc or mt6359-auxadc, for the following reasons:
- Both mt6323-auxadc and mt6359-auxadc select channels through a request
register (1 bit per channel), while mt6397 uses a 4-bit numeric field
CHSEL in CON1 (10:7), and then pulses a START bit (CON1 bit 0).
- For mt6323-auxadc, which is the closest I could find to the mt6397
(CON0..CON27), it has 13 more registers than the mt6397 (CON0..CON14).
It uses CON22 for its request register, and reads the result value
from the same register as the ready bit. We don't do that - the mt6397
has a factory-calibrated value for each channel at 0x16 higher than the
raw value. mt6323 also has a 1800 mV / 15 bit scale / resolution while
we have 1200 mV / 10 bits. We also have some per-channel preparation
that we have to do before the burst, that the mt6323 doesn't have to
do.
- For mt6359-auxadc, it has a more generic framework for describing the
AUXADC, but it assumes requests are channel-per-bit, and so we would
have to basically ignore req_idx, req_mask, rdy_idx, and rdy_mask.
- We also have our own software sampling, which the vendor does too
(Amazon Fire OS based on Linux 3.18). We'd have to have our own
sampling callback to do it.
---
MAINTAINERS | 1 +
drivers/iio/adc/Kconfig | 11 ++
drivers/iio/adc/Makefile | 1 +
drivers/iio/adc/mt6397-auxadc.c | 394 ++++++++++++++++++++++++++++++++++++++++
4 files changed, 407 insertions(+)
diff --git a/MAINTAINERS b/MAINTAINERS
index b5417c02eb01..761db34f84a1 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -16784,6 +16784,7 @@ MEDIATEK MT6397 PMIC AUXADC DRIVER
M: Ryan Brue <ryanbrue.dev@gmail.com>
L: linux-iio@vger.kernel.org
S: Maintained
+F: drivers/iio/adc/mt6397-auxadc.c
F: include/dt-bindings/iio/adc/mediatek,mt6397-auxadc.h
MEDIATEK MT6735 CLOCK & RESET DRIVERS
diff --git a/drivers/iio/adc/Kconfig b/drivers/iio/adc/Kconfig
index f32f678f73fb..7686314bc91b 100644
--- a/drivers/iio/adc/Kconfig
+++ b/drivers/iio/adc/Kconfig
@@ -1273,6 +1273,17 @@ config MEDIATEK_MT6370_ADC
This driver can also be built as a module. If so, the module
will be called "mt6370-adc".
+config MEDIATEK_MT6397_AUXADC
+ tristate "MediaTek MT6397 PMIC AUXADC driver"
+ depends on MFD_MT6397
+ help
+ Say yes here to enable support for MediaTek MT6397 PMIC Auxiliary ADC.
+ This driver provides the battery voltage and battery temperature
+ channels used by the fuel gauge on boards built around this PMIC.
+
+ This driver can also be built as a module. If so, the module will be
+ called mt6397-auxadc.
+
config MEDIATEK_MT6577_AUXADC
tristate "MediaTek AUXADC driver"
depends on ARCH_MEDIATEK || COMPILE_TEST
diff --git a/drivers/iio/adc/Makefile b/drivers/iio/adc/Makefile
index fee580bdd3b2..d7bb4815df61 100644
--- a/drivers/iio/adc/Makefile
+++ b/drivers/iio/adc/Makefile
@@ -108,6 +108,7 @@ obj-$(CONFIG_MEDIATEK_MT6323_AUXADC) += mt6323-auxadc.o
obj-$(CONFIG_MEDIATEK_MT6359_AUXADC) += mt6359-auxadc.o
obj-$(CONFIG_MEDIATEK_MT6360_ADC) += mt6360-adc.o
obj-$(CONFIG_MEDIATEK_MT6370_ADC) += mt6370-adc.o
+obj-$(CONFIG_MEDIATEK_MT6397_AUXADC) += mt6397-auxadc.o
obj-$(CONFIG_MEDIATEK_MT6577_AUXADC) += mt6577_auxadc.o
obj-$(CONFIG_MEN_Z188_ADC) += men_z188_adc.o
obj-$(CONFIG_MESON_SARADC) += meson_saradc.o
diff --git a/drivers/iio/adc/mt6397-auxadc.c b/drivers/iio/adc/mt6397-auxadc.c
new file mode 100644
index 000000000000..234022f15eba
--- /dev/null
+++ b/drivers/iio/adc/mt6397-auxadc.c
@@ -0,0 +1,394 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * MediaTek MT6397 PMIC AUXADC IIO driver
+ *
+ * Copyright (c) 2026 Ryan Brue <ryanbrue.dev@gmail.com>
+ *
+ * Based on drivers/iio/adc/mt6323-auxadc.c
+ */
+
+#include <linux/array_size.h>
+#include <linux/bitfield.h>
+#include <linux/bits.h>
+#include <linux/cleanup.h>
+#include <linux/delay.h>
+#include <linux/iio/iio.h>
+#include <linux/math.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/platform_device.h>
+#include <linux/regmap.h>
+#include <linux/time.h>
+#include <linux/types.h>
+
+#include <linux/mfd/mt6397/core.h>
+
+#include <dt-bindings/iio/adc/mediatek,mt6397-auxadc.h>
+
+/* The ready bit is in the raw result register, the value in the trimmed one. */
+#define MT6397_AUXADC_ADC(n) (0x0514 + 2 * (n))
+#define MT6397_AUXADC_ADC_RDY BIT(15)
+#define MT6397_AUXADC_ADC_VAL GENMASK(9, 0)
+/* The trimmed copy of ADC(n), which is ADC11 + n. */
+#define MT6397_AUXADC_ADC_TRIM(n) (0x052a + 2 * (n))
+
+/* The thermistor needs the battery-detect bias and the input buffer on. */
+#define MT6397_AUXADC_CON0 0x0542
+#define MT6397_AUXADC_CON0_BUF_PWD_B BIT(1)
+#define MT6397_AUXADC_CON0_BUF_PWD_ON BIT(3)
+/* Samples accumulated per conversion; the chip's accumulator is left at one. */
+#define MT6397_AUXADC_CON0_SPL_NUM GENMASK(11, 7)
+#define MT6397_CHR_CON7 0x000e
+#define MT6397_CHR_CON7_BATON_TDET_EN BIT(2)
+
+#define MT6397_AUXADC_BATTEMP_SETTLE_US (20 * USEC_PER_MSEC)
+
+#define MT6397_AUXADC_CON1 0x0544
+#define MT6397_AUXADC_CON1_CHSEL GENMASK(10, 7)
+#define MT6397_AUXADC_CON1_START BIT(0)
+
+/*
+ * CHR_CON16 and the two SOURCE_CH0 selects route either BATSNS or ISENSE onto
+ * the battery channel. Only ISENSE is used: with a switching charger in the
+ * power path BATSNS sits on the system rail rather than on the pack. All five
+ * ADCIN bits are written out because they are one enable per analog input
+ * and not a select field; the driver touches only the two it needs.
+ */
+#define MT6397_CHR_CON16 0x0020
+#define MT6397_CHR_CON16_ADCIN_VCHR_EN BIT(12)
+#define MT6397_CHR_CON16_ADCIN_VSEN_EN BIT(11)
+#define MT6397_CHR_CON16_ADCIN_VBAT_EN BIT(10)
+#define MT6397_CHR_CON16_ADCIN_VSEN_EXT_BATON_EN BIT(9)
+#define MT6397_CHR_CON16_ADCIN_VSEN_MUX_EN BIT(8)
+#define MT6397_AUXADC_CON14 0x055e
+#define MT6397_AUXADC_CON14_CH0_NORM_SEL BIT(2)
+#define MT6397_AUXADC_CON14_CH0_LBAT_SEL BIT(0)
+
+#define MT6397_AUXADC_ISENSE_SETTLE_US (1 * USEC_PER_MSEC)
+
+/* Conversions averaged in software per read. */
+#define MT6397_AUXADC_SAMPLES 16
+
+/* Hardware limitation: the result is not ready for this long after START. */
+#define MT6397_AUXADC_START_US 30
+
+/* The battery channel is divided down by 4; the thermistor is not divided. */
+#define MT6397_AUXADC_ISENSE_DIVIDER 4
+
+/* The PMIC's channel numbers, as the CON1 select field takes them. */
+#define MT6397_AUXADC_HWCHAN_BATSNS 0
+#define MT6397_AUXADC_HWCHAN_BAT_TEMP 3
+
+struct mt6397_auxadc {
+ struct regmap *regmap;
+ /*
+ * A read is a multi-register sequence: analog setup, then a channel
+ * select and an edge-triggered start per conversion. Interleaving two
+ * of them steals the select and tears down the setup, so a read holds
+ * this for its whole burst.
+ */
+ struct mutex lock;
+};
+
+/*
+ * A device tree channel ID indexes this array; .address holds the PMIC channel
+ * number. The thermistor reports as a voltage because that is all the PMIC
+ * measures: the divider across an NTC whose curve belongs to the board.
+ */
+static const struct iio_chan_spec mt6397_auxadc_channels[] = {
+ {
+ .type = IIO_VOLTAGE,
+ .indexed = 1,
+ .channel = MT6397_AUXADC_ISENSE,
+ .address = MT6397_AUXADC_HWCHAN_BATSNS,
+ .datasheet_name = "isense",
+ .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
+ BIT(IIO_CHAN_INFO_SCALE),
+ }, {
+ .type = IIO_VOLTAGE,
+ .indexed = 1,
+ .channel = MT6397_AUXADC_BAT_TEMP,
+ .address = MT6397_AUXADC_HWCHAN_BAT_TEMP,
+ .datasheet_name = "bat_temp",
+ .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
+ BIT(IIO_CHAN_INFO_SCALE),
+ },
+};
+
+static int mt6397_auxadc_read_once(struct mt6397_auxadc *adc,
+ const struct iio_chan_spec *chan, int *val)
+{
+ struct regmap *map = adc->regmap;
+ unsigned int reg;
+ int ret;
+
+ ret = regmap_update_bits(map, MT6397_AUXADC_CON1, MT6397_AUXADC_CON1_CHSEL,
+ FIELD_PREP(MT6397_AUXADC_CON1_CHSEL, chan->address));
+ if (ret)
+ return ret;
+
+ /*
+ * START is edge triggered: lower it before raising it again. No delay
+ * is needed in between -- the ready bit survives the fall and is
+ * cleared by the rise, before the next transaction on this bus can
+ * observe it, so a ready bit seen by the poll can only be this
+ * conversion's.
+ */
+ ret = regmap_clear_bits(map, MT6397_AUXADC_CON1, MT6397_AUXADC_CON1_START);
+ if (ret)
+ return ret;
+
+ ret = regmap_set_bits(map, MT6397_AUXADC_CON1, MT6397_AUXADC_CON1_START);
+ if (ret)
+ return ret;
+
+ fsleep(MT6397_AUXADC_START_US);
+
+ ret = regmap_read_poll_timeout(map, MT6397_AUXADC_ADC(chan->address),
+ reg, reg & MT6397_AUXADC_ADC_RDY,
+ 100, 100 * USEC_PER_MSEC);
+ if (ret)
+ return ret;
+
+ ret = regmap_read(map, MT6397_AUXADC_ADC_TRIM(chan->address), ®);
+ if (ret)
+ return ret;
+
+ *val = FIELD_GET(MT6397_AUXADC_ADC_VAL, reg);
+
+ return 0;
+}
+
+static int mt6397_auxadc_battemp_bias_on(struct mt6397_auxadc *adc)
+{
+ struct regmap *map = adc->regmap;
+ int ret;
+
+ ret = regmap_set_bits(map, MT6397_AUXADC_CON0,
+ MT6397_AUXADC_CON0_BUF_PWD_ON);
+ if (ret)
+ return ret;
+
+ ret = regmap_set_bits(map, MT6397_AUXADC_CON0,
+ MT6397_AUXADC_CON0_BUF_PWD_B);
+ if (ret)
+ return ret;
+
+ return regmap_set_bits(map, MT6397_CHR_CON7,
+ MT6397_CHR_CON7_BATON_TDET_EN);
+}
+
+static void mt6397_auxadc_battemp_bias_off(struct mt6397_auxadc *adc)
+{
+ struct regmap *map = adc->regmap;
+
+ regmap_clear_bits(map, MT6397_CHR_CON7, MT6397_CHR_CON7_BATON_TDET_EN);
+ regmap_clear_bits(map, MT6397_AUXADC_CON0, MT6397_AUXADC_CON0_BUF_PWD_B);
+ regmap_clear_bits(map, MT6397_AUXADC_CON0, MT6397_AUXADC_CON0_BUF_PWD_ON);
+}
+
+static int mt6397_auxadc_isense_enable(struct mt6397_auxadc *adc)
+{
+ struct regmap *map = adc->regmap;
+ int ret;
+
+ /*
+ * Each ADCIN bit connects one input to the battery channel. BATSNS is
+ * disconnected before ISENSE is connected, so that the two are never
+ * connected at the same time.
+ */
+ ret = regmap_clear_bits(map, MT6397_CHR_CON16, MT6397_CHR_CON16_ADCIN_VBAT_EN);
+ if (ret)
+ return ret;
+
+ ret = regmap_set_bits(map, MT6397_CHR_CON16, MT6397_CHR_CON16_ADCIN_VSEN_EN);
+ if (ret)
+ return ret;
+
+ return regmap_set_bits(map, MT6397_AUXADC_CON14,
+ MT6397_AUXADC_CON14_CH0_NORM_SEL |
+ MT6397_AUXADC_CON14_CH0_LBAT_SEL);
+}
+
+static void mt6397_auxadc_isense_disable(struct mt6397_auxadc *adc)
+{
+ struct regmap *map = adc->regmap;
+
+ regmap_clear_bits(map, MT6397_AUXADC_CON14,
+ MT6397_AUXADC_CON14_CH0_NORM_SEL |
+ MT6397_AUXADC_CON14_CH0_LBAT_SEL);
+ regmap_clear_bits(map, MT6397_CHR_CON16,
+ MT6397_CHR_CON16_ADCIN_VSEN_EN |
+ MT6397_CHR_CON16_ADCIN_VBAT_EN);
+}
+
+/*
+ * One burst on a channel that is already prepared: the caller owns the lock,
+ * the analog setup and the teardown.
+ */
+static int mt6397_auxadc_read_burst(struct mt6397_auxadc *adc,
+ const struct iio_chan_spec *chan, int *val)
+{
+ unsigned int sum = 0;
+ int sample;
+ int ret;
+
+ for (unsigned int i = 0; i < MT6397_AUXADC_SAMPLES; i++) {
+ ret = mt6397_auxadc_read_once(adc, chan, &sample);
+ if (ret)
+ return ret;
+
+ sum += sample;
+ }
+
+ *val = DIV_ROUND_CLOSEST(sum, MT6397_AUXADC_SAMPLES);
+
+ return 0;
+}
+
+static void mt6397_auxadc_stop(struct mt6397_auxadc *adc)
+{
+ /* Lowering START leaves the converter unarmed between reads. */
+ regmap_clear_bits(adc->regmap, MT6397_AUXADC_CON1, MT6397_AUXADC_CON1_START);
+}
+
+static int mt6397_auxadc_isense_read(struct mt6397_auxadc *adc,
+ const struct iio_chan_spec *chan, int *val)
+{
+ int ret;
+
+ ret = mt6397_auxadc_isense_enable(adc);
+ if (ret)
+ goto out_disable;
+
+ fsleep(MT6397_AUXADC_ISENSE_SETTLE_US);
+
+ ret = mt6397_auxadc_read_burst(adc, chan, val);
+
+out_disable:
+ mt6397_auxadc_stop(adc);
+ mt6397_auxadc_isense_disable(adc);
+
+ return ret;
+}
+
+static int mt6397_auxadc_battemp_read(struct mt6397_auxadc *adc,
+ const struct iio_chan_spec *chan, int *val)
+{
+ int ret;
+
+ ret = mt6397_auxadc_battemp_bias_on(adc);
+ if (ret)
+ goto out_bias_off;
+
+ fsleep(MT6397_AUXADC_BATTEMP_SETTLE_US);
+
+ ret = mt6397_auxadc_read_burst(adc, chan, val);
+
+out_bias_off:
+ mt6397_auxadc_stop(adc);
+ mt6397_auxadc_battemp_bias_off(adc);
+
+ return ret;
+}
+
+static int mt6397_auxadc_read_channel(struct iio_dev *indio_dev,
+ const struct iio_chan_spec *chan,
+ int *val)
+{
+ struct mt6397_auxadc *adc = iio_priv(indio_dev);
+
+ guard(mutex)(&adc->lock);
+
+ if (chan->channel == MT6397_AUXADC_ISENSE)
+ return mt6397_auxadc_isense_read(adc, chan, val);
+
+ return mt6397_auxadc_battemp_read(adc, chan, val);
+}
+
+static int mt6397_auxadc_read_raw(struct iio_dev *indio_dev,
+ const struct iio_chan_spec *chan,
+ int *val, int *val2, long mask)
+{
+ int ret;
+
+ switch (mask) {
+ case IIO_CHAN_INFO_RAW:
+ ret = mt6397_auxadc_read_channel(indio_dev, chan, val);
+ if (ret)
+ return ret;
+
+ return IIO_VAL_INT;
+
+ case IIO_CHAN_INFO_SCALE:
+ /* 1200 mV full range with 10-bit resolution. */
+ if (chan->channel == MT6397_AUXADC_ISENSE)
+ *val = 1200 * MT6397_AUXADC_ISENSE_DIVIDER;
+ else
+ *val = 1200;
+
+ *val2 = 10;
+
+ return IIO_VAL_FRACTIONAL_LOG2;
+
+ default:
+ return -EINVAL;
+ }
+}
+
+static const struct iio_info mt6397_auxadc_info = {
+ .read_raw = mt6397_auxadc_read_raw,
+};
+
+static int mt6397_auxadc_probe(struct platform_device *pdev)
+{
+ struct device *dev = &pdev->dev;
+ struct mt6397_chip *chip = dev_get_drvdata(dev->parent);
+ struct mt6397_auxadc *adc;
+ struct iio_dev *indio_dev;
+ int ret;
+
+ indio_dev = devm_iio_device_alloc(dev, sizeof(*adc));
+ if (!indio_dev)
+ return -ENOMEM;
+
+ adc = iio_priv(indio_dev);
+ adc->regmap = chip->regmap;
+
+ ret = devm_mutex_init(dev, &adc->lock);
+ if (ret)
+ return ret;
+
+ ret = regmap_update_bits(adc->regmap, MT6397_AUXADC_CON0,
+ MT6397_AUXADC_CON0_SPL_NUM,
+ FIELD_PREP(MT6397_AUXADC_CON0_SPL_NUM, 1));
+ if (ret)
+ return dev_err_probe(dev, ret, "failed to initialize auxadc\n");
+
+ indio_dev->name = "mt6397-auxadc";
+ indio_dev->info = &mt6397_auxadc_info;
+ indio_dev->modes = INDIO_DIRECT_MODE;
+ indio_dev->channels = mt6397_auxadc_channels;
+ indio_dev->num_channels = ARRAY_SIZE(mt6397_auxadc_channels);
+
+ return devm_iio_device_register(dev, indio_dev);
+}
+
+static const struct of_device_id mt6397_auxadc_of_match[] = {
+ { .compatible = "mediatek,mt6397-auxadc" },
+ { }
+};
+MODULE_DEVICE_TABLE(of, mt6397_auxadc_of_match);
+
+static struct platform_driver mt6397_auxadc_driver = {
+ .driver = {
+ .name = "mt6397-auxadc",
+ .of_match_table = mt6397_auxadc_of_match,
+ },
+ .probe = mt6397_auxadc_probe,
+};
+module_platform_driver(mt6397_auxadc_driver);
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Ryan Brue <ryanbrue.dev@gmail.com>");
+MODULE_DESCRIPTION("MediaTek MT6397 PMIC AUXADC Driver");
--
2.55.0
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v5 0/2] iio: adc: add mt6397 PMIC AUXADC support
2026-10-25 5:18 [PATCH v5 0/2] iio: adc: add mt6397 PMIC AUXADC support Ryan Brue
2026-10-25 5:18 ` [PATCH v5 1/2] dt-bindings: iio: adc: mediatek,mt6359-auxadc: add mt6397 PMIC AUXADC Ryan Brue
2026-10-25 5:18 ` [PATCH v5 2/2] iio: adc: mt6397-auxadc: add mt6397 PMIC AUXADC driver Ryan Brue
@ 2026-10-25 22:44 ` Jonathan Cameron
2026-11-17 10:56 ` AngeloGioacchino Del Regno
3 siblings, 0 replies; 8+ messages in thread
From: Jonathan Cameron @ 2026-10-25 22:44 UTC (permalink / raw)
To: Ryan Brue
Cc: David Lechner, Nuno Sá,
Andy Shevchenko, Rob Herring, Krzysztof Kozlowski, Conor Dooley,
Matthias Brugger, AngeloGioacchino Del Regno, Roman Vivchar,
Luca Leonardo Scorcia, linux-iio, devicetree, linux-kernel,
linux-arm-kernel, linux-mediatek, Krzysztof Kozlowski,
Andy Shevchenko
On Sun, 25 Oct 2026 00:18:07 -0500
Ryan Brue <ryanbrue.dev@gmail.com> wrote:
> The MediaTek mt6397 PMIC has a 10-bit AUXADC that nothing in-tree can
> reach. On boards built around it that ADC is the only path to the battery:
> the SoC's AUXADC is wired to board thermistors, and the charger ICs these
> boards use have no ADC at all, so without it there is no pack voltage and
> no state of charge.
Applied to the testing branch of iio.git. I'll be rebasing that on rc1
once available. At that point I'll push out as togreg which gets picked up
by linux-next.
Thanks,
Jonathan
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v5 2/2] iio: adc: mt6397-auxadc: add mt6397 PMIC AUXADC driver
2026-10-25 5:18 ` [PATCH v5 2/2] iio: adc: mt6397-auxadc: add mt6397 PMIC AUXADC driver Ryan Brue
@ 2026-11-17 10:50 ` AngeloGioacchino Del Regno
0 siblings, 0 replies; 8+ messages in thread
From: AngeloGioacchino Del Regno @ 2026-11-17 10:50 UTC (permalink / raw)
To: Ryan Brue, Jonathan Cameron, David Lechner, Nuno Sá,
Andy Shevchenko, Rob Herring, Krzysztof Kozlowski, Conor Dooley,
Matthias Brugger
Cc: Roman Vivchar, Luca Leonardo Scorcia, linux-iio, devicetree,
linux-kernel, linux-arm-kernel, linux-mediatek, Andy Shevchenko
On 10/25/26 06:18, Ryan Brue wrote:
> The mt6397 AUXADC is a 10-bit ADC behind the SoC's PMIC wrapper. On boards
> built around this PMIC it is the only way to read the battery: the SoC's
> AUXADC is wired to board thermistors and the charger ICs these boards use
> have no ADC of their own.
>
> Add a driver exposing the battery voltage and battery temperature
> channels. Only those two are described, so a channel ID in the device tree
> indexes the driver's channel array rather than the PMIC's channel number,
> as mt6323-auxadc does. The ready bit lives in a channel's raw result
> register, but the value comes from the chip's factory-calibrated copy.
>
> The battery voltage is measured through ISENSE, because a board with a
> switching charger in the power path leaves BATSNS on the charger's system
> rail instead of on the pack. The thermistor only reads correctly with the
> PMIC's battery-detect bias and input buffer enabled, which take 20 ms to
> settle. Both are switched back off afterwards.
>
> Reads average sixteen conversions in software; the chip's sample
> accumulator makes no measurable difference at any setting, so it is left
> at one sample per conversion.
>
> Reviewed-by: Andy Shevchenko <andriy.shevchenko@intel.com>
> Signed-off-by: Ryan Brue <ryanbrue.dev@gmail.com>
Reviewed-by: AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com>
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v5 0/2] iio: adc: add mt6397 PMIC AUXADC support
2026-10-25 5:18 [PATCH v5 0/2] iio: adc: add mt6397 PMIC AUXADC support Ryan Brue
` (2 preceding siblings ...)
2026-10-25 22:44 ` [PATCH v5 0/2] iio: adc: add mt6397 PMIC AUXADC support Jonathan Cameron
@ 2026-11-17 10:56 ` AngeloGioacchino Del Regno
2026-11-17 11:02 ` AngeloGioacchino Del Regno
3 siblings, 1 reply; 8+ messages in thread
From: AngeloGioacchino Del Regno @ 2026-11-17 10:56 UTC (permalink / raw)
To: Ryan Brue, Jonathan Cameron, David Lechner, Nuno Sá,
Andy Shevchenko, Rob Herring, Krzysztof Kozlowski, Conor Dooley,
Matthias Brugger
Cc: Roman Vivchar, Luca Leonardo Scorcia, linux-iio, devicetree,
linux-kernel, linux-arm-kernel, linux-mediatek,
Krzysztof Kozlowski, Andy Shevchenko
On 10/25/26 06:18, Ryan Brue wrote:
> The MediaTek mt6397 PMIC has a 10-bit AUXADC that nothing in-tree can
> reach. On boards built around it that ADC is the only path to the battery:
> the SoC's AUXADC is wired to board thermistors, and the charger ICs these
> boards use have no ADC at all, so without it there is no pack voltage and
> no state of charge.
>
I picked the Amazon Fire HD 10 series, confident that this series will also
be picked for this cycle by the IIO maintainers, as this was fully reviewed.
So please-please-please pick this one ASAP so that we don't get dt warnings.
Cheers,
Angelo
> Patch 1 adds the compatible to the existing MediaTek PMIC AUXADC schema and
> the header naming the channels, patch 2 the driver. The MFD cell that
> instantiates it was patch 3 of v4, which Lee has applied [1].
>
> The mt6397 is not given mediatek,mt6359-auxadc as a fallback because the
> two are not register compatible; patch 1 has the detail.
>
> Only the two channels a board needs for its pack are described, so patch 1
> ships a header naming them: a channel ID is an index into the driver's
> array and not the PMIC's channel number, as in mt6323-auxadc. The register
> sequences are the ones the vendor kernel's pmic_auxadc.c programs.
>
> Tested on an Amazon Fire HD 10 (2017), an mt8173 with an mt6397. Both
> channels are exercised: the battery channel by a fuel gauge, which reads
> 817 counts, 3830 mV with the pack at rest, and the thermistor channel by an
> ntc-thermistor consuming it. The error paths are driven by injecting a
> regmap failure into each helper in turn. The read returns the injected
> error, and every bit is cleared again. The teardown writes were also made
> to fail, one at a time. When one write fails, the others still run, and
> only the failed write's bits are left set. The next read clears them
> anyway. Run the same way, v3 stopped at the first failed write and left the
> later bits set. v5 gives the same results as v4 in all of these. Loading
> the two modules alternately on the board, their readings differ by 0.3 LSB
> on the battery channel and 0.1 LSB on the thermistor, both inside the
> comparison's own noise.
>
> A new driver was created here, instead of modifying an existing driver such
> as mt6323-auxadc or mt6359-auxadc, for the following reasons:
>
> - Both mt6323-auxadc and mt6359-auxadc select channels through a request
> register (1 bit per channel), while mt6397 uses a 4-bit numeric field
> CHSEL in CON1 (10:7), and then pulses a START bit (CON1 bit 0).
>
> - For mt6323-auxadc, which is the closest I could find to the mt6397
> (CON0..CON27), it has 13 more registers than the mt6397 (CON0..CON14).
> It uses CON22 for its request register, and reads the result value
> from the same register as the ready bit. We don't do that - the mt6397
> has a factory-calibrated value for each channel at 0x16 higher than the
> raw value. mt6323 also has a 1800 mV / 15 bit scale / resolution while
> we have 1200 mV / 10 bits. We also have some per-channel preparation
> that we have to do before the burst, that the mt6323 doesn't have to
> do.
>
> - For mt6359-auxadc, it has a more generic framework for describing the
> AUXADC, but it assumes requests are channel-per-bit, and so we would
> have to basically ignore req_idx, req_mask, rdy_idx, and rdy_mask.
>
> - We also have our own software sampling, which the vendor does too
> (Amazon Fire OS based on Linux 3.18). We'd have to have our own
> sampling callback to do it.
>
> I have two things in the driver different from the vendor (Amazon
> Fire OS based on a 3.18 kernel). Verified with over 150 interleaved reads
> per setting:
>
> - Leaving the ISENSE input enabled between reads, as the vendor does,
> makes no difference to the value, so the driver switches it off again.
> Both ADC input enables are clear when idle, unless a teardown write
> itself fails: the rest of the teardown still runs, and the next read of
> the channel clears whatever was left set.
>
> - The chip's sample accumulator makes no difference at 4, 8, 16 or 31
> samples and costs no measurable time, so it is left at one. Software
> averaging helps, but only by about a quarter: in one interleaved run,
> 0.92 LSB standard deviation for one conversion against 0.67 for sixteen,
> where independent samples would have given 0.23. Conversions in a burst
> are correlated.
>
> The reading was checked against the charger, which regulates the pack to
> a programmed voltage in constant-voltage mode. Over twelve such voltages
> from 3904 to 4080 mV, and charge currents from 111 to 778 mA, the driver's
> reading is a mean of 0.9 mV from the regulated value, well inside
> the charger's regulation tolerance. That also settles reading the chip's
> trimmed result register rather than the raw one - the raw register is
> 20.7 mV low at every one of the twelve points.
>
> This series was developed with the help of an LLM coding assistant.
>
> [1] https://lore.kernel.org/all/179206667779.1910726.5341177428334836457.b4-ty@b4/
>
> Signed-off-by: Ryan Brue <ryanbrue.dev@gmail.com>
> ---
> Changes in v5:
> - Split the read into one helper per channel, each doing its own setup,
> burst and teardown, and a caller that takes the lock and dispatches on
> the channel. mt6397_auxadc_release() is gone. The register sequence is
> unchanged on every path, including every failure path, and retested on
> hardware. (Jonathan)
> - Keep Andy's Reviewed-by on patch 2, since the change is a restructure
> that does not change behavior. If that's incorrect let me know.
> - Drop the MFD cell patch, applied by Lee from v4.
> - Rebase onto iio/testing. The MAINTAINERS entry now follows the MT6323
> PMIC NVMEM one.
> - Drop the Assisted-by trailers per the new kernel guidance, and put the
> disclosure in the cover letter.
> - Link to v4: https://patch.msgid.link/20260928-rbrue-suez-upstreaming-mt6397-auxadc-v4-0-23533a0d30e3@gmail.com
>
> Changes in v4:
> - Attempt every teardown write even if a previous one fails, so a failed
> write doesn't leave the buffer, bias, or input powered. Reverses the v3
> change. The teardown neither checks nor logs errors for failed writes,
> similar to mt6358_stop_imp_conv(). The next read will rewrite those bits
> anyway. (Angelo, sashiko-bot, Andy)
> - Move teardown into mt6397_auxadc_release(), keep error paths out of line
> with goto, and use mutex_lock() / mutex_unlock() rather than guard().
> (Jonathan)
> - Reword the comment above the BATSNS disable prior to ISENSE enable.
> (Jonathan)
> - Collect tags.
> - Link to v3: https://patch.msgid.link/20260920-rbrue-suez-upstreaming-mt6397-auxadc-v3-0-c00edacbee64@gmail.com
>
> Changes in v3:
> - Move the sample loop into its own function, so read_channel() is setup,
> then a conditional burst, then an unconditional teardown, with no goto
> mixed with guard(). (Andy, sashiko-bot)
> - battemp_bias_off() and isense_disable() now return on the first failed
> write, as mt6323_auxadc_release() does, and twelve other teardowns in
> drivers/iio that I could find. A failed write can leave the later bits
> set; the teardown runs at the end of every read, so the next read of that
> channel clears them. (Andy)
> - Drop the "if (!ret) ret = err;" accumulators with it. A teardown failure
> now goes to dev_err() rather than being returned, since the caller cannot
> act on it, and read_channel() takes the iio_dev so no struct device * is
> stored. (Andy)
> - Split battemp_bias(adc, bool on) into battemp_bias_on() and
> battemp_bias_off(), and drop the bool. (Andy)
> - Make the SCALE case an if-else. (Andy)
> - Move the "why a new driver" rationale below the --- marker in patch 2,
> out of the commit message. (Andy)
> - Reduce the CHSEL regmap_update_bits() call from four lines to two. (Andy)
> - Drop the comment above guard(mutex). (Andy)
> - Say in a comment why lowering START needs no settling delay. The ready
> bit survives the fall and is cleared by the rise before the next
> transaction on the PMIC wrapper can observe it. I measured this, and the
> details are in the reply on 2/3. (Andy)
> - Cc Luca Leonardo Scorcia with a trailer rather than in prose. (Andy)
> - Link to v2: https://patch.msgid.link/20260917-rbrue-suez-upstreaming-mt6397-auxadc-v2-0-db35882a6080@gmail.com
>
> Changes in v2:
> - Add justification for this being a new driver rather than folded into
> mt6323-auxadc.c or mt6359-auxadc.c, in the cover letter and patch 2.
> - Drop the "ret = ret ?: ..." chains, and let teardown fall through on
> errors, allowing the chance for all bits to be properly torn down. The
> errors are now held rather than discarded. (Andy and sashiko-bot)
> - Run teardown even when per-channel setup fails part-way through, so we
> don't leave bits enabled that would have been cleared. (sashiko-bot)
> - Set ISENSE settle time to (1 * USEC_PER_MSEC). (Andy)
> - Write the two channel descriptors out instead of using a macro. Also
> gets rid of the padded column. (Andy, Jonathan)
> - Move the MAINTAINERS entry for the dt-bindings header into patch 1, as
> the mt6323 series does.
> - Declare the sample loop counter in the loop, instead of with sum, and
> split the declaration of sample and ret. (Andy)
> - Keep the two CHR_CON16 ADCIN bits as BIT()s rather than turning them
> into values of a GENMASK field. Fix OR operator on the bits, treat them
> as mutually exclusive. (Jonathan)
> - The rest of Jonathan's v1 review: two-space indent separating a register's
> fields from the register, a blank line before the settle times, a lock
> comment that says why the lock is held, the two CON1 START writes on one
> line each, the single-call init helper inlined into probe, and { } for the
> of_device_id sentinel.
> - Add the additional three CHR_CON16 ADCIN defines that make up the 5
> single-bit enables. These are currently unused.
> - Link to v1: https://patch.msgid.link/20260915-rbrue-suez-upstreaming-mt6397-auxadc-v1-0-d35d2ac3d6f0@gmail.com
>
> ---
> Ryan Brue (2):
> dt-bindings: iio: adc: mediatek,mt6359-auxadc: add mt6397 PMIC AUXADC
> iio: adc: mt6397-auxadc: add mt6397 PMIC AUXADC driver
>
> .../bindings/iio/adc/mediatek,mt6359-auxadc.yaml | 1 +
> MAINTAINERS | 7 +
> drivers/iio/adc/Kconfig | 11 +
> drivers/iio/adc/Makefile | 1 +
> drivers/iio/adc/mt6397-auxadc.c | 394 +++++++++++++++++++++
> .../dt-bindings/iio/adc/mediatek,mt6397-auxadc.h | 9 +
> 6 files changed, 423 insertions(+)
> ---
> base-commit: c9dbe2a47f635465c9985b7af74d2ae310e98125
> change-id: 20260914-rbrue-suez-upstreaming-mt6397-auxadc-5e1228025c50
>
> Best regards,
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v5 0/2] iio: adc: add mt6397 PMIC AUXADC support
2026-11-17 10:56 ` AngeloGioacchino Del Regno
@ 2026-11-17 11:02 ` AngeloGioacchino Del Regno
2026-11-17 17:35 ` Ryan Brue
0 siblings, 1 reply; 8+ messages in thread
From: AngeloGioacchino Del Regno @ 2026-11-17 11:02 UTC (permalink / raw)
To: Ryan Brue, Jonathan Cameron, David Lechner, Nuno Sá,
Andy Shevchenko, Rob Herring, Krzysztof Kozlowski, Conor Dooley,
Matthias Brugger
Cc: Roman Vivchar, Luca Leonardo Scorcia, linux-iio, devicetree,
linux-kernel, linux-arm-kernel, linux-mediatek,
Krzysztof Kozlowski, Andy Shevchenko
On 11/17/26 11:56, AngeloGioacchino Del Regno wrote:
> On 10/25/26 06:18, Ryan Brue wrote:
>> The MediaTek mt6397 PMIC has a 10-bit AUXADC that nothing in-tree can
>> reach. On boards built around it that ADC is the only path to the battery:
>> the SoC's AUXADC is wired to board thermistors, and the charger ICs these
>> boards use have no ADC at all, so without it there is no pack voltage and
>> no state of charge.
>>
>
> I picked the Amazon Fire HD 10 series, confident that this series will also
> be picked for this cycle by the IIO maintainers, as this was fully reviewed.
>
> So please-please-please pick this one ASAP so that we don't get dt warnings.
>
I reiterate - I couldn't pick the series because, of course, that one depends
on the binding being merged, otherwise the header won't be there and the build
fails.
So I can't pick the DTS until the next cycle (granted that this series lands).
Cheers,
Angelo
> Cheers,
> Angelo
>
>> Patch 1 adds the compatible to the existing MediaTek PMIC AUXADC schema and
>> the header naming the channels, patch 2 the driver. The MFD cell that
>> instantiates it was patch 3 of v4, which Lee has applied [1].
>>
>> The mt6397 is not given mediatek,mt6359-auxadc as a fallback because the
>> two are not register compatible; patch 1 has the detail.
>>
>> Only the two channels a board needs for its pack are described, so patch 1
>> ships a header naming them: a channel ID is an index into the driver's
>> array and not the PMIC's channel number, as in mt6323-auxadc. The register
>> sequences are the ones the vendor kernel's pmic_auxadc.c programs.
>>
>> Tested on an Amazon Fire HD 10 (2017), an mt8173 with an mt6397. Both
>> channels are exercised: the battery channel by a fuel gauge, which reads
>> 817 counts, 3830 mV with the pack at rest, and the thermistor channel by an
>> ntc-thermistor consuming it. The error paths are driven by injecting a
>> regmap failure into each helper in turn. The read returns the injected
>> error, and every bit is cleared again. The teardown writes were also made
>> to fail, one at a time. When one write fails, the others still run, and
>> only the failed write's bits are left set. The next read clears them
>> anyway. Run the same way, v3 stopped at the first failed write and left the
>> later bits set. v5 gives the same results as v4 in all of these. Loading
>> the two modules alternately on the board, their readings differ by 0.3 LSB
>> on the battery channel and 0.1 LSB on the thermistor, both inside the
>> comparison's own noise.
>>
>> A new driver was created here, instead of modifying an existing driver such
>> as mt6323-auxadc or mt6359-auxadc, for the following reasons:
>>
>> - Both mt6323-auxadc and mt6359-auxadc select channels through a request
>> register (1 bit per channel), while mt6397 uses a 4-bit numeric field
>> CHSEL in CON1 (10:7), and then pulses a START bit (CON1 bit 0).
>>
>> - For mt6323-auxadc, which is the closest I could find to the mt6397
>> (CON0..CON27), it has 13 more registers than the mt6397 (CON0..CON14).
>> It uses CON22 for its request register, and reads the result value
>> from the same register as the ready bit. We don't do that - the mt6397
>> has a factory-calibrated value for each channel at 0x16 higher than the
>> raw value. mt6323 also has a 1800 mV / 15 bit scale / resolution while
>> we have 1200 mV / 10 bits. We also have some per-channel preparation
>> that we have to do before the burst, that the mt6323 doesn't have to
>> do.
>>
>> - For mt6359-auxadc, it has a more generic framework for describing the
>> AUXADC, but it assumes requests are channel-per-bit, and so we would
>> have to basically ignore req_idx, req_mask, rdy_idx, and rdy_mask.
>>
>> - We also have our own software sampling, which the vendor does too
>> (Amazon Fire OS based on Linux 3.18). We'd have to have our own
>> sampling callback to do it.
>>
>> I have two things in the driver different from the vendor (Amazon
>> Fire OS based on a 3.18 kernel). Verified with over 150 interleaved reads
>> per setting:
>>
>> - Leaving the ISENSE input enabled between reads, as the vendor does,
>> makes no difference to the value, so the driver switches it off again.
>> Both ADC input enables are clear when idle, unless a teardown write
>> itself fails: the rest of the teardown still runs, and the next read of
>> the channel clears whatever was left set.
>>
>> - The chip's sample accumulator makes no difference at 4, 8, 16 or 31
>> samples and costs no measurable time, so it is left at one. Software
>> averaging helps, but only by about a quarter: in one interleaved run,
>> 0.92 LSB standard deviation for one conversion against 0.67 for sixteen,
>> where independent samples would have given 0.23. Conversions in a burst
>> are correlated.
>>
>> The reading was checked against the charger, which regulates the pack to
>> a programmed voltage in constant-voltage mode. Over twelve such voltages
>> from 3904 to 4080 mV, and charge currents from 111 to 778 mA, the driver's
>> reading is a mean of 0.9 mV from the regulated value, well inside
>> the charger's regulation tolerance. That also settles reading the chip's
>> trimmed result register rather than the raw one - the raw register is
>> 20.7 mV low at every one of the twelve points.
>>
>> This series was developed with the help of an LLM coding assistant.
>>
>> [1] https://lore.kernel.org/all/179206667779.1910726.5341177428334836457.b4-ty@b4/
>>
>> Signed-off-by: Ryan Brue <ryanbrue.dev@gmail.com>
>> ---
>> Changes in v5:
>> - Split the read into one helper per channel, each doing its own setup,
>> burst and teardown, and a caller that takes the lock and dispatches on
>> the channel. mt6397_auxadc_release() is gone. The register sequence is
>> unchanged on every path, including every failure path, and retested on
>> hardware. (Jonathan)
>> - Keep Andy's Reviewed-by on patch 2, since the change is a restructure
>> that does not change behavior. If that's incorrect let me know.
>> - Drop the MFD cell patch, applied by Lee from v4.
>> - Rebase onto iio/testing. The MAINTAINERS entry now follows the MT6323
>> PMIC NVMEM one.
>> - Drop the Assisted-by trailers per the new kernel guidance, and put the
>> disclosure in the cover letter.
>> - Link to v4: https://patch.msgid.link/20260928-rbrue-suez-upstreaming-mt6397-
>> auxadc-v4-0-23533a0d30e3@gmail.com
>>
>> Changes in v4:
>> - Attempt every teardown write even if a previous one fails, so a failed
>> write doesn't leave the buffer, bias, or input powered. Reverses the v3
>> change. The teardown neither checks nor logs errors for failed writes,
>> similar to mt6358_stop_imp_conv(). The next read will rewrite those bits
>> anyway. (Angelo, sashiko-bot, Andy)
>> - Move teardown into mt6397_auxadc_release(), keep error paths out of line
>> with goto, and use mutex_lock() / mutex_unlock() rather than guard().
>> (Jonathan)
>> - Reword the comment above the BATSNS disable prior to ISENSE enable.
>> (Jonathan)
>> - Collect tags.
>> - Link to v3: https://patch.msgid.link/20260920-rbrue-suez-upstreaming-mt6397-
>> auxadc-v3-0-c00edacbee64@gmail.com
>>
>> Changes in v3:
>> - Move the sample loop into its own function, so read_channel() is setup,
>> then a conditional burst, then an unconditional teardown, with no goto
>> mixed with guard(). (Andy, sashiko-bot)
>> - battemp_bias_off() and isense_disable() now return on the first failed
>> write, as mt6323_auxadc_release() does, and twelve other teardowns in
>> drivers/iio that I could find. A failed write can leave the later bits
>> set; the teardown runs at the end of every read, so the next read of that
>> channel clears them. (Andy)
>> - Drop the "if (!ret) ret = err;" accumulators with it. A teardown failure
>> now goes to dev_err() rather than being returned, since the caller cannot
>> act on it, and read_channel() takes the iio_dev so no struct device * is
>> stored. (Andy)
>> - Split battemp_bias(adc, bool on) into battemp_bias_on() and
>> battemp_bias_off(), and drop the bool. (Andy)
>> - Make the SCALE case an if-else. (Andy)
>> - Move the "why a new driver" rationale below the --- marker in patch 2,
>> out of the commit message. (Andy)
>> - Reduce the CHSEL regmap_update_bits() call from four lines to two. (Andy)
>> - Drop the comment above guard(mutex). (Andy)
>> - Say in a comment why lowering START needs no settling delay. The ready
>> bit survives the fall and is cleared by the rise before the next
>> transaction on the PMIC wrapper can observe it. I measured this, and the
>> details are in the reply on 2/3. (Andy)
>> - Cc Luca Leonardo Scorcia with a trailer rather than in prose. (Andy)
>> - Link to v2: https://patch.msgid.link/20260917-rbrue-suez-upstreaming-mt6397-
>> auxadc-v2-0-db35882a6080@gmail.com
>>
>> Changes in v2:
>> - Add justification for this being a new driver rather than folded into
>> mt6323-auxadc.c or mt6359-auxadc.c, in the cover letter and patch 2.
>> - Drop the "ret = ret ?: ..." chains, and let teardown fall through on
>> errors, allowing the chance for all bits to be properly torn down. The
>> errors are now held rather than discarded. (Andy and sashiko-bot)
>> - Run teardown even when per-channel setup fails part-way through, so we
>> don't leave bits enabled that would have been cleared. (sashiko-bot)
>> - Set ISENSE settle time to (1 * USEC_PER_MSEC). (Andy)
>> - Write the two channel descriptors out instead of using a macro. Also
>> gets rid of the padded column. (Andy, Jonathan)
>> - Move the MAINTAINERS entry for the dt-bindings header into patch 1, as
>> the mt6323 series does.
>> - Declare the sample loop counter in the loop, instead of with sum, and
>> split the declaration of sample and ret. (Andy)
>> - Keep the two CHR_CON16 ADCIN bits as BIT()s rather than turning them
>> into values of a GENMASK field. Fix OR operator on the bits, treat them
>> as mutually exclusive. (Jonathan)
>> - The rest of Jonathan's v1 review: two-space indent separating a register's
>> fields from the register, a blank line before the settle times, a lock
>> comment that says why the lock is held, the two CON1 START writes on one
>> line each, the single-call init helper inlined into probe, and { } for the
>> of_device_id sentinel.
>> - Add the additional three CHR_CON16 ADCIN defines that make up the 5
>> single-bit enables. These are currently unused.
>> - Link to v1: https://patch.msgid.link/20260915-rbrue-suez-upstreaming-mt6397-
>> auxadc-v1-0-d35d2ac3d6f0@gmail.com
>>
>> ---
>> Ryan Brue (2):
>> dt-bindings: iio: adc: mediatek,mt6359-auxadc: add mt6397 PMIC AUXADC
>> iio: adc: mt6397-auxadc: add mt6397 PMIC AUXADC driver
>>
>> .../bindings/iio/adc/mediatek,mt6359-auxadc.yaml | 1 +
>> MAINTAINERS | 7 +
>> drivers/iio/adc/Kconfig | 11 +
>> drivers/iio/adc/Makefile | 1 +
>> drivers/iio/adc/mt6397-auxadc.c | 394 +++++++++++++++++++++
>> .../dt-bindings/iio/adc/mediatek,mt6397-auxadc.h | 9 +
>> 6 files changed, 423 insertions(+)
>> ---
>> base-commit: c9dbe2a47f635465c9985b7af74d2ae310e98125
>> change-id: 20260914-rbrue-suez-upstreaming-mt6397-auxadc-5e1228025c50
>>
>> Best regards,
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v5 0/2] iio: adc: add mt6397 PMIC AUXADC support
2026-11-17 11:02 ` AngeloGioacchino Del Regno
@ 2026-11-17 17:35 ` Ryan Brue
0 siblings, 0 replies; 8+ messages in thread
From: Ryan Brue @ 2026-11-17 17:35 UTC (permalink / raw)
To: AngeloGioacchino Del Regno, Jonathan Cameron, David Lechner,
Nuno Sá,
Andy Shevchenko, Rob Herring, Krzysztof Kozlowski, Conor Dooley,
Matthias Brugger
Cc: Roman Vivchar, Luca Leonardo Scorcia, linux-iio, devicetree,
linux-kernel, linux-arm-kernel, linux-mediatek,
Krzysztof Kozlowski, Andy Shevchenko
Hi Angelo,
On 11/17/26 5:02 AM, AngeloGioacchino Del Regno wrote:
> I reiterate - I couldn't pick the series because, of course, that one
> depends
> on the binding being merged, otherwise the header won't be there and
> the build
> fails.
>
> So I can't pick the DTS until the next cycle (granted that this series
> lands).
>
> Cheers,
> Angelo
Thanks for the reviews, and for picking the binding and keypad patches.
The AUXADC series has already landed. Jonathan applied it to iio togreg
on 11-02 (the binding with the header is 8f809aa84698), and it's in
next-20261116, so it will be in 7.5-rc1 along with your tree.
The board only uses the header once, for MT6397_AUXADC_BAT_TEMP (1). If
it helps, I can send a v3 of the board patch that uses the plain channel
number and drops the include - it builds on your v7.4-next/dts64, with
only the binding warnings you mentioned until the trees meet. I would
then switch it to the macro after 7.5-rc1.
Just let me know if that sounds reasonable :)
Best Regards,
Ryan Brue
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2026-11-17 17:35 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-10-25 5:18 [PATCH v5 0/2] iio: adc: add mt6397 PMIC AUXADC support Ryan Brue
2026-10-25 5:18 ` [PATCH v5 1/2] dt-bindings: iio: adc: mediatek,mt6359-auxadc: add mt6397 PMIC AUXADC Ryan Brue
2026-10-25 5:18 ` [PATCH v5 2/2] iio: adc: mt6397-auxadc: add mt6397 PMIC AUXADC driver Ryan Brue
2026-11-17 10:50 ` AngeloGioacchino Del Regno
2026-10-25 22:44 ` [PATCH v5 0/2] iio: adc: add mt6397 PMIC AUXADC support Jonathan Cameron
2026-11-17 10:56 ` AngeloGioacchino Del Regno
2026-11-17 11:02 ` AngeloGioacchino Del Regno
2026-11-17 17:35 ` Ryan Brue
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®