* [PATCH v5 0/3] iio: adc: Add Nuvoton MA35D1 EADC support
@ 2026-11-17 8:28 Chi-Wen Weng
2026-11-17 8:28 ` [PATCH v5 1/3] dt-bindings: iio: adc: Fix channel property validation Chi-Wen Weng
` (2 more replies)
0 siblings, 3 replies; 5+ messages in thread
From: Chi-Wen Weng @ 2026-11-17 8:28 UTC (permalink / raw)
To: jic23, robh, krzk+dt, conor+dt
Cc: dlechner, nuno.sa, andy, linux-arm-kernel, linux-iio, devicetree,
linux-kernel, chernyakin.victor.j, cwweng, cwweng.linux
From: Chi-Wen Weng <cwweng@nuvoton.com>
This series adds IIO support for the Enhanced Analog-to-Digital
Converter (EADC) found on Nuvoton MA35D1 SoCs.
The EADC provides eight external analog inputs which may be used as
single-ended inputs or as four fixed differential pairs: 0-4, 1-5,
2-6 and 3-7. The VBAT input is internally connected and needs no
devicetree description. It is not supported by this initial driver.
The initial driver supports direct conversion and triggered buffered
capture using sample module 0. Although the hardware provides multiple
sample modules, this version uses only sample module 0 and therefore
supports one active voltage channel per buffered scan.
The series also fixes the common ADC channel validation to reject
configurations specifying both single-channel and diff-channels. The
corresponding redundant constraints are removed from the MA35D1 binding.
A reset control is required to put the EADC into a known state at
probe and resume, and to recover from power-up failures without
clearing ADCEN while the start-up sequence is still in progress.
The series is based on Linux v7.4-rc1.
Changes in v5:
- Fix the common adc.yaml channel validation by replacing the existing
anyOf constraint with an explicit reg requirement and a mutual-exclusion
constraint for single-channel and diff-channels, as suggested by
Rob Herring.
- Remove redundant channel-level validation constraints from the
MA35D1 binding.
- Require a reset control in the binding and driver to reset the EADC
during probe and resume and to recover from power-up failures.
- Clarify that the VBAT input is internally connected and needs no
devicetree description.
- Do not specify the internal reference voltage in the vref-supply
description.
- Use folded YAML scalars for prose descriptions.
- Reorganize register definitions so register addresses, fields and
values are grouped together.
- Describe the individual INTSRC0 sample-module interrupt-enable bits
explicitly instead of treating them as a single field.
- Add paired ADC interrupt enable and disable helpers and simplify the
conversion error paths.
- Document the ordering used when aborting a timed-out conversion.
- Correct the comment describing PDREF.
- Remove the trivial differential sign-extension helper.
- Validate the effective scan mask in update_scan_mode() so callback
buffers and merged masks cannot request multiple voltage channels.
Commit the selected channel only after validation.
- Wait for PWRM.PWUPRDY after enabling the ADC.
- Use a full module reset to recover from power-up failures instead of
clearing ADCEN before PWUPRDY is set. Preserve the original error and
report any reset failure.
- Read DAT0 during timeout recovery to discard an unread result and
clear stale VALID and OV flags.
- Reset the whole EADC module at the start of hardware initialization
in both probe and resume, replacing the control-circuit reset and the
register-by-register clearing of inherited interrupt, compare and
trigger configuration. This also removes the access to the
non-existent SCTL8 register.
- Split single-ended and differential channel parsing into separate
helpers.
- Initialize local channel-tracking bitmaps directly using = { } instead
of separate bitmap_zero() calls, as suggested by Victor Chernyakin.
- Remove the unnecessary raw variable in the triggered capture path
and read the conversion result directly into scan.channel.
- Rework suspend and resume hardware handling around scoped mutex
locking.
- Remove the buffer predisable callback. Every conversion path already
leaves the ADC interrupt disabled and ADIF0 cleared, and the callback
could access EADC registers while the clock was gated after a failed
resume.
- Make repeated suspend handling safe after an unsuccessful resume and
avoid unbalanced clock disable operations.
- Only call iio_device_suspend_triggering() and
iio_device_resume_triggering() while the buffer is enabled.
pollfunc->irq is not initialized until the pollfunc is first attached
to a trigger, so calling them unconditionally, as v4 did, could pass
an uninitialized IRQ number to disable_irq() and enable_irq().
- Drop the redundant clk_enabled flag pointed out by Andy Shevchenko;
the suspended flag already tracks whether the clock is gated.
- Add a comment explaining why the EADC register map is left uncached.
- Describe the readable, writable and precious EADC registers in the
regmap configuration.
- Apply the requested structure layout, variable scope, formatting and
control-flow cleanups, including the if/else chain in init_vref().
- Drop the unused linux/io.h and linux/iio/trigger.h includes.
- Require at least one external channel child node.
Changes in v4:
- Use the common ADC diff-channels property to describe differential
inputs and keep reg as the logical channel index.
- Restrict differential inputs to the four hardware-supported pairs.
- Restrict channel child nodes to the ADC properties supported by the
MA35D1 EADC.
- Prevent single-channel and diff-channels from being specified
together.
- Document that each single-ended input and differential input pair
may be described only once.
- Remove interrupt-names and simplify the interrupt descriptions.
- Update the binding example to demonstrate separation of the logical
channel index from the physical ADC inputs.
- Update channel parsing to match the revised binding semantics.
- Store physical ADC inputs in the IIO channel/channel2 fields and use
address for the logical DT channel index.
- Reject duplicate physical single-ended inputs and differential input
pairs.
- Use the physical positive input when programming the sample-module
channel selection and the logical index for firmware-node channel
translation.
- Use interrupt index 0 instead of looking up ADINT0 by name.
- Add the standard one-hot scan-mask validation callback.
- Use iio_for_each_active_channel() when selecting the buffered
channel.
- Use a fixed scan structure for the single-channel buffered layout.
- Limit conversion results to the 12-bit ADC data width.
- Always report -ETIMEDOUT when a conversion times out.
- Make timeout recovery best effort so interrupt disable, control reset,
IRQ synchronization and stale status cleanup are all attempted even
if an earlier recovery step fails.
- Use devm_regulator_get_enable_read_voltage() for the optional
external reference supply.
- Rework clock management to explicitly balance prepare/enable state
across probe, suspend, resume, error paths and driver teardown.
- Improve suspend and resume error-path handling.
- Remove the redundant explicit REGCACHE_NONE setting.
- Split interrupt status read failures from interrupt-source checks and
handle failures when clearing the interrupt status.
- Use symbolic definitions for the ADC resolution and differential
scale width.
- Use cleanup guards for mutex handling in buffered capture and keep
trigger notification outside the locked section.
- Use more specific error codes for channel capacity, duplicate channel
definitions and out-of-range physical inputs.
- Apply the requested regmap, control-flow and coding-style cleanups.
Changes in v3:
- Document all four ADINT0 through ADINT3 hardware interrupt outputs
and add interrupt-names.
- Document ADINT3 as the shared conversion and result-compare interrupt
output.
- Remove the redundant minItems/maxItems constraints from the fixed
interrupts items list.
- Rework the channel binding for the fixed differential pairs and use
single-channel for single-ended inputs.
- Add #io-channel-cells so the EADC can be referenced by DT IIO
consumers, and define the cell as the logical channel index.
- Add firmware-node channel translation so sparse or reordered channel
child nodes are mapped by their logical channel index.
- Restrict unsupported generic ADC channel properties including bipolar,
common-mode-channel, settling-time-us and oversampling-ratio.
- Explicitly configure INTPOS for end-of-conversion interrupt
generation.
- Explicitly configure the ADINT0 sample-module source and clear unused
sample-module routing.
- Reset the EADC control circuits during hardware initialization and
clear inherited ADC interrupt enables, result-compare interrupt
enables and sample-module trigger sources.
- Add timeout recovery using the EADC control-circuit reset, followed by
IRQ synchronization and stale status/completion cleanup.
- Use iio_push_to_buffers_with_ts() for triggered buffered capture.
- Avoid mixing cleanup guards with goto paths in the trigger handler.
- Add ARCH_MA35 || COMPILE_TEST and HAS_IOMEM Kconfig dependencies.
- Drop redundant linux/bitops.h and linux/mod_devicetable.h includes.
- Use direct regmap helpers, explicit REGCACHE_NONE and
regmap_assign_bits() where appropriate.
- Use fixed-width register offsets and kernel time/unit helpers.
- Use size_add() for channel array allocation.
- Request ADINT0 by interrupt name.
- Improve system suspend and resume handling.
- Address review comments from Jonathan Cameron, Rob Herring,
Andy Shevchenko, Uwe Kleine-König and automated review/testing.
Changes in v2:
- Reworked the driver to use regmap.
- Added optional external reference supply support and IIO scale
reporting.
- Added optional reset handling.
- Added externally triggered buffered capture.
- Added signed differential conversion handling.
- Added fixed differential-pair validation.
- Added system sleep support.
- Added optional DMA properties to the binding.
Patch 1 fixes the common ADC channel property validation.
Patch 2 adds the MA35D1 EADC devicetree binding.
Patch 3 adds the MA35D1 EADC IIO driver.
Chi-Wen Weng (3):
dt-bindings: iio: adc: Fix channel property validation
dt-bindings: iio: adc: Add Nuvoton MA35D1 EADC
iio: adc: Add Nuvoton MA35D1 EADC driver
.../devicetree/bindings/iio/adc/adc.yaml | 17 +-
.../bindings/iio/adc/nuvoton,ma35d1-eadc.yaml | 174 +++
drivers/iio/adc/Kconfig | 14 +
drivers/iio/adc/Makefile | 1 +
drivers/iio/adc/ma35d1_eadc.c | 1036 +++++++++++++++++
5 files changed, 1232 insertions(+), 10 deletions(-)
create mode 100644 Documentation/devicetree/bindings/iio/adc/nuvoton,ma35d1-eadc.yaml
create mode 100644 drivers/iio/adc/ma35d1_eadc.c
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
--
2.25.1
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH v5 1/3] dt-bindings: iio: adc: Fix channel property validation
2026-11-17 8:28 [PATCH v5 0/3] iio: adc: Add Nuvoton MA35D1 EADC support Chi-Wen Weng
@ 2026-11-17 8:28 ` Chi-Wen Weng
2026-11-17 8:28 ` [PATCH v5 2/3] dt-bindings: iio: adc: Add Nuvoton MA35D1 EADC Chi-Wen Weng
2026-11-17 8:28 ` [PATCH v5 3/3] iio: adc: Add Nuvoton MA35D1 EADC driver Chi-Wen Weng
2 siblings, 0 replies; 5+ messages in thread
From: Chi-Wen Weng @ 2026-11-17 8:28 UTC (permalink / raw)
To: jic23, robh, krzk+dt, conor+dt
Cc: dlechner, nuno.sa, andy, linux-arm-kernel, linux-iio, devicetree,
linux-kernel, chernyakin.victor.j, cwweng, cwweng.linux
From: Chi-Wen Weng <cwweng@nuvoton.com>
The common ADC channel binding currently uses an anyOf constraint
to require the reg property and select between single-channel and
diff-channels.
However, the reg-only branch allows validation to succeed even when
both single-channel and diff-channels are present, bypassing the
intended mutual exclusion.
Replace the anyOf constraint with an explicit reg requirement and
a not constraint that rejects channels specifying both properties.
This preserves support for reg-only, single-ended and differential
channel descriptions while rejecting conflicting configurations.
Fixes: dd471a2b7759 ("dt-bindings: iio: adc: Add single-channel property")
Suggested-by: Rob Herring <robh@kernel.org>
Signed-off-by: Chi-Wen Weng <cwweng@nuvoton.com>
---
.../devicetree/bindings/iio/adc/adc.yaml | 17 +++++++----------
1 file changed, 7 insertions(+), 10 deletions(-)
diff --git a/Documentation/devicetree/bindings/iio/adc/adc.yaml b/Documentation/devicetree/bindings/iio/adc/adc.yaml
index b673eaa5d072..f2a16b896630 100644
--- a/Documentation/devicetree/bindings/iio/adc/adc.yaml
+++ b/Documentation/devicetree/bindings/iio/adc/adc.yaml
@@ -114,15 +114,12 @@ properties:
Burn-out current sources provide current to the channel's input pins for
open-circuit or short-circuit detection.
-anyOf:
- - oneOf:
- - required:
- - reg
- - diff-channels
- - required:
- - reg
- - single-channel
- - required:
- - reg
+required:
+ - reg
+
+not:
+ required:
+ - single-channel
+ - diff-channels
additionalProperties: true
--
2.25.1
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH v5 2/3] dt-bindings: iio: adc: Add Nuvoton MA35D1 EADC
2026-11-17 8:28 [PATCH v5 0/3] iio: adc: Add Nuvoton MA35D1 EADC support Chi-Wen Weng
2026-11-17 8:28 ` [PATCH v5 1/3] dt-bindings: iio: adc: Fix channel property validation Chi-Wen Weng
@ 2026-11-17 8:28 ` Chi-Wen Weng
2026-11-17 8:42 ` sashiko-bot
2026-11-17 8:28 ` [PATCH v5 3/3] iio: adc: Add Nuvoton MA35D1 EADC driver Chi-Wen Weng
2 siblings, 1 reply; 5+ messages in thread
From: Chi-Wen Weng @ 2026-11-17 8:28 UTC (permalink / raw)
To: jic23, robh, krzk+dt, conor+dt
Cc: dlechner, nuno.sa, andy, linux-arm-kernel, linux-iio, devicetree,
linux-kernel, chernyakin.victor.j, cwweng, cwweng.linux
From: Chi-Wen Weng <cwweng@nuvoton.com>
Add a binding for the Enhanced Analog-to-Digital Converter found on
Nuvoton MA35D1 SoCs.
The controller provides eight external analog inputs that can be used
as single-ended inputs or as the four fixed differential pairs 0-4,
1-5, 2-6 and 3-7. The VBAT input is internally connected and needs no
devicetree description.
Signed-off-by: Chi-Wen Weng <cwweng@nuvoton.com>
---
.../bindings/iio/adc/nuvoton,ma35d1-eadc.yaml | 174 ++++++++++++++++++
1 file changed, 174 insertions(+)
create mode 100644 Documentation/devicetree/bindings/iio/adc/nuvoton,ma35d1-eadc.yaml
diff --git a/Documentation/devicetree/bindings/iio/adc/nuvoton,ma35d1-eadc.yaml b/Documentation/devicetree/bindings/iio/adc/nuvoton,ma35d1-eadc.yaml
new file mode 100644
index 000000000000..1bf64913d67d
--- /dev/null
+++ b/Documentation/devicetree/bindings/iio/adc/nuvoton,ma35d1-eadc.yaml
@@ -0,0 +1,174 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/iio/adc/nuvoton,ma35d1-eadc.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Nuvoton MA35D1 Enhanced Analog-to-Digital Converter
+
+maintainers:
+ - Chi-Wen Weng <cwweng@nuvoton.com>
+
+description: >
+ The Nuvoton MA35D1 Enhanced Analog-to-Digital Converter (EADC) is a
+ 12-bit successive approximation converter with eight external analog
+ inputs and one internally connected VBAT input.
+
+ The external inputs can be used as eight single-ended channels or as
+ four fixed differential pairs: 0-4, 1-5, 2-6 and 3-7. The VBAT input
+ needs no devicetree description.
+
+ The controller provides four interrupt outputs, ADINT0 through ADINT3.
+
+properties:
+ compatible:
+ const: nuvoton,ma35d1-eadc
+
+ reg:
+ maxItems: 1
+
+ interrupts:
+ items:
+ - description: ADINT0
+ - description: ADINT1
+ - description: ADINT2
+ - description: ADINT3
+
+ clocks:
+ maxItems: 1
+
+ resets:
+ maxItems: 1
+
+ vref-supply:
+ description:
+ Optional external reference voltage connected to the VREF pin. If this
+ supply is absent, the internal reference is used.
+
+ dmas:
+ maxItems: 1
+
+ dma-names:
+ const: rx
+
+ '#io-channel-cells':
+ const: 1
+ description:
+ The cell selects the logical external EADC channel identified by the reg
+ value of the corresponding channel child node. Only channels represented
+ by child nodes are valid I/O channel specifiers.
+
+ '#address-cells':
+ const: 1
+
+ '#size-cells':
+ const: 0
+
+patternProperties:
+ '^channel@[0-7]$':
+ type: object
+ $ref: /schemas/iio/adc/adc.yaml#
+ description: >
+ External EADC channel description. The reg property identifies the
+ logical IIO channel index.
+
+ A single-ended channel may use single-channel to select an external
+ analog input independently of reg. If single-channel and diff-channels
+ are both absent, the external analog input is selected by reg.
+
+ A differential channel uses diff-channels to select one of the fixed
+ hardware input pairs: 0-4, 1-5, 2-6 or 3-7. Each single-ended input
+ and each differential input pair may be described at most once.
+
+ properties:
+ reg:
+ maximum: 7
+
+ single-channel:
+ maximum: 7
+
+ diff-channels:
+ oneOf:
+ - items:
+ - const: 0
+ - const: 4
+ - items:
+ - const: 1
+ - const: 5
+ - items:
+ - const: 2
+ - const: 6
+ - items:
+ - const: 3
+ - const: 7
+
+ additionalProperties: false
+
+required:
+ - compatible
+ - reg
+ - interrupts
+ - clocks
+ - resets
+ - '#io-channel-cells'
+ - '#address-cells'
+ - '#size-cells'
+
+anyOf:
+ - required:
+ - channel@0
+ - required:
+ - channel@1
+ - required:
+ - channel@2
+ - required:
+ - channel@3
+ - required:
+ - channel@4
+ - required:
+ - channel@5
+ - required:
+ - channel@6
+ - required:
+ - channel@7
+
+additionalProperties: false
+
+examples:
+ - |
+ #include <dt-bindings/clock/nuvoton,ma35d1-clk.h>
+ #include <dt-bindings/interrupt-controller/arm-gic.h>
+ #include <dt-bindings/interrupt-controller/irq.h>
+ #include <dt-bindings/reset/nuvoton,ma35d1-reset.h>
+
+ soc {
+ #address-cells = <2>;
+ #size-cells = <2>;
+
+ adc@40430000 {
+ compatible = "nuvoton,ma35d1-eadc";
+ reg = <0x0 0x40430000 0x0 0x10000>;
+ interrupts = <GIC_SPI 65 IRQ_TYPE_LEVEL_HIGH>,
+ <GIC_SPI 66 IRQ_TYPE_LEVEL_HIGH>,
+ <GIC_SPI 67 IRQ_TYPE_LEVEL_HIGH>,
+ <GIC_SPI 68 IRQ_TYPE_LEVEL_HIGH>;
+ clocks = <&clk EADC_GATE>;
+ resets = <&sys MA35D1_RESET_EADC0>;
+
+ #io-channel-cells = <1>;
+ #address-cells = <1>;
+ #size-cells = <0>;
+
+ channel@0 {
+ reg = <0>;
+ single-channel = <2>;
+ };
+
+ channel@1 {
+ reg = <1>;
+ diff-channels = <0 4>;
+ };
+ };
+ };
+
+...
--
2.25.1
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH v5 3/3] iio: adc: Add Nuvoton MA35D1 EADC driver
2026-11-17 8:28 [PATCH v5 0/3] iio: adc: Add Nuvoton MA35D1 EADC support Chi-Wen Weng
2026-11-17 8:28 ` [PATCH v5 1/3] dt-bindings: iio: adc: Fix channel property validation Chi-Wen Weng
2026-11-17 8:28 ` [PATCH v5 2/3] dt-bindings: iio: adc: Add Nuvoton MA35D1 EADC Chi-Wen Weng
@ 2026-11-17 8:28 ` Chi-Wen Weng
2 siblings, 0 replies; 5+ messages in thread
From: Chi-Wen Weng @ 2026-11-17 8:28 UTC (permalink / raw)
To: jic23, robh, krzk+dt, conor+dt
Cc: dlechner, nuno.sa, andy, linux-arm-kernel, linux-iio, devicetree,
linux-kernel, chernyakin.victor.j, cwweng, cwweng.linux
From: Chi-Wen Weng <cwweng@nuvoton.com>
Add an IIO driver for the Enhanced Analog-to-Digital Converter found on
Nuvoton MA35D1 SoCs.
Support single-ended and fixed differential inputs, direct conversion,
and triggered buffered capture. This initial implementation uses sample
module 0 and therefore supports one active voltage channel per buffered
scan.
Signed-off-by: Chi-Wen Weng <cwweng@nuvoton.com>
---
drivers/iio/adc/Kconfig | 14 +
drivers/iio/adc/Makefile | 1 +
drivers/iio/adc/ma35d1_eadc.c | 1036 +++++++++++++++++++++++++++++++++
3 files changed, 1051 insertions(+)
create mode 100644 drivers/iio/adc/ma35d1_eadc.c
diff --git a/drivers/iio/adc/Kconfig b/drivers/iio/adc/Kconfig
index a802d8ee4dd9..6868b67d852f 100644
--- a/drivers/iio/adc/Kconfig
+++ b/drivers/iio/adc/Kconfig
@@ -1032,6 +1032,20 @@ config LTC2497
To compile this driver as a module, choose M here: the module will be
called ltc2497.
+config MA35D1_EADC
+ tristate "Nuvoton MA35D1 EADC driver"
+ depends on ARCH_MA35 || COMPILE_TEST
+ depends on HAS_IOMEM
+ select IIO_BUFFER
+ select IIO_TRIGGERED_BUFFER
+ select REGMAP_MMIO
+ help
+ Say yes here to build support for the Nuvoton MA35D1
+ Enhanced Analog to Digital Converter.
+
+ To compile this driver as a module, choose M here: the module will be
+ called ma35d1_eadc.
+
config MAX1027
tristate "Maxim max1027 ADC driver"
depends on SPI
diff --git a/drivers/iio/adc/Makefile b/drivers/iio/adc/Makefile
index 9e82c21dbd37..a478fef5d0b7 100644
--- a/drivers/iio/adc/Makefile
+++ b/drivers/iio/adc/Makefile
@@ -88,6 +88,7 @@ obj-$(CONFIG_LTC2471) += ltc2471.o
obj-$(CONFIG_LTC2485) += ltc2485.o
obj-$(CONFIG_LTC2496) += ltc2496.o ltc2497-core.o
obj-$(CONFIG_LTC2497) += ltc2497.o ltc2497-core.o
+obj-$(CONFIG_MA35D1_EADC) += ma35d1_eadc.o
obj-$(CONFIG_MAX1027) += max1027.o
obj-$(CONFIG_MAX11100) += max11100.o
obj-$(CONFIG_MAX1118) += max1118.o
diff --git a/drivers/iio/adc/ma35d1_eadc.c b/drivers/iio/adc/ma35d1_eadc.c
new file mode 100644
index 000000000000..d9a715efcc6b
--- /dev/null
+++ b/drivers/iio/adc/ma35d1_eadc.c
@@ -0,0 +1,1036 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Nuvoton MA35D1 EADC driver
+ *
+ * Copyright (c) 2026 Nuvoton Technology Corp.
+ */
+
+#include <linux/array_size.h>
+#include <linux/bitfield.h>
+#include <linux/bitmap.h>
+#include <linux/cleanup.h>
+#include <linux/clk.h>
+#include <linux/completion.h>
+#include <linux/delay.h>
+#include <linux/err.h>
+#include <linux/interrupt.h>
+#include <linux/jiffies.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/overflow.h>
+#include <linux/platform_device.h>
+#include <linux/pm.h>
+#include <linux/property.h>
+#include <linux/regmap.h>
+#include <linux/regulator/consumer.h>
+#include <linux/reset.h>
+#include <linux/time.h>
+#include <linux/types.h>
+#include <linux/units.h>
+
+#include <linux/iio/buffer.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/trigger_consumer.h>
+#include <linux/iio/triggered_buffer.h>
+
+#define MA35D1_EADC_RESOLUTION 12
+#define MA35D1_EADC_DIFF_SCALE_BITS (MA35D1_EADC_RESOLUTION - 1)
+
+#define MA35D1_EADC_DAT(n) (0x000 + (n) * 0x004)
+#define MA35D1_EADC_DAT_RESULT_12BIT GENMASK(MA35D1_EADC_RESOLUTION - 1, 0)
+#define MA35D1_EADC_DAT_OV BIT(16)
+#define MA35D1_EADC_DAT_VALID BIT(17)
+
+#define MA35D1_EADC_CURDAT 0x04c
+
+#define MA35D1_EADC_CTL 0x050
+#define MA35D1_EADC_CTL_ADCEN BIT(0)
+#define MA35D1_EADC_CTL_EADCRST BIT(1)
+#define MA35D1_EADC_CTL_ADCIEN0 BIT(2)
+#define MA35D1_EADC_CTL_DIFFEN BIT(8)
+#define MA35D1_EADC_CTL_DMOF BIT(9)
+#define MA35D1_EADC_CTL_VREFSEL_MASK GENMASK(11, 10)
+#define MA35D1_EADC_CTL_VREFSEL_1V6 FIELD_PREP(MA35D1_EADC_CTL_VREFSEL_MASK, 0)
+
+#define MA35D1_EADC_SWTRG 0x054
+#define MA35D1_EADC_SWTRG_SWTRG0 BIT(0)
+
+#define MA35D1_EADC_PENDSTS 0x058
+#define MA35D1_EADC_OVSTS 0x05c
+
+#define MA35D1_EADC_SCTL(n) (0x080 + (n) * 0x004)
+#define MA35D1_EADC_SCTL_CHSEL_MASK GENMASK(3, 0)
+#define MA35D1_EADC_SCTL_TRGSEL_MASK GENMASK(21, 16)
+#define MA35D1_EADC_SCTL_INTPOS BIT(22)
+
+#define MA35D1_EADC_INTSRC0 0x0d0
+#define MA35D1_EADC_INTSRC0_SPLIEN0 BIT(0)
+#define MA35D1_EADC_INTSRC0_SPLIEN1 BIT(1)
+#define MA35D1_EADC_INTSRC0_SPLIEN2 BIT(2)
+#define MA35D1_EADC_INTSRC0_SPLIEN3 BIT(3)
+#define MA35D1_EADC_INTSRC0_SPLIEN4 BIT(4)
+#define MA35D1_EADC_INTSRC0_SPLIEN5 BIT(5)
+#define MA35D1_EADC_INTSRC0_SPLIEN6 BIT(6)
+#define MA35D1_EADC_INTSRC0_SPLIEN7 BIT(7)
+#define MA35D1_EADC_INTSRC0_SPLIEN8 BIT(8)
+#define MA35D1_EADC_INTSRC0_SPLIEN_MASK \
+ (MA35D1_EADC_INTSRC0_SPLIEN0 | MA35D1_EADC_INTSRC0_SPLIEN1 | \
+ MA35D1_EADC_INTSRC0_SPLIEN2 | MA35D1_EADC_INTSRC0_SPLIEN3 | \
+ MA35D1_EADC_INTSRC0_SPLIEN4 | MA35D1_EADC_INTSRC0_SPLIEN5 | \
+ MA35D1_EADC_INTSRC0_SPLIEN6 | MA35D1_EADC_INTSRC0_SPLIEN7 | \
+ MA35D1_EADC_INTSRC0_SPLIEN8)
+
+#define MA35D1_EADC_CMP(n) (0x0e0 + (n) * 0x004)
+#define MA35D1_EADC_STATUS0 0x0f0
+
+#define MA35D1_EADC_STATUS2 0x0f8
+#define MA35D1_EADC_STATUS2_ADIF0 BIT(0)
+
+#define MA35D1_EADC_DDAT(n) (0x100 + (n) * 0x004)
+
+#define MA35D1_EADC_PWRM 0x110
+#define MA35D1_EADC_PWRM_PWUPRDY BIT(0)
+
+#define MA35D1_EADC_CALCTL 0x114
+
+#define MA35D1_EADC_PDMACTL 0x130
+
+#define MA35D1_EADC_SELSMP0 0x140
+#define MA35D1_EADC_SELSMP0_SMPT0_MASK GENMASK(1, 0)
+#define MA35D1_EADC_SELSMP_LONG FIELD_PREP(MA35D1_EADC_SELSMP0_SMPT0_MASK, 3)
+
+#define MA35D1_EADC_SELSMP1 0x144
+
+#define MA35D1_EADC_REFADJCTL 0x150
+#define MA35D1_EADC_REFADJCTL_PDREF BIT(0)
+
+#define MA35D1_EADC_MAX_EXT_CHANNELS 8
+#define MA35D1_EADC_NUM_DIFF_PAIRS 4
+#define MA35D1_EADC_NUM_SAMPLE_MODULES 9
+/* Sample module 8 is fixed to VBAT and has no SCTL register. */
+#define MA35D1_EADC_NUM_SCTL 8
+#define MA35D1_EADC_NUM_CMP 4
+#define MA35D1_EADC_NUM_DDAT 4
+#define MA35D1_EADC_INTERNAL_VREF_mV 1600
+#define MA35D1_EADC_TIMEOUT msecs_to_jiffies(1000)
+#define MA35D1_EADC_RESET_DELAY_US 10
+#define MA35D1_EADC_REF_STABLE_US (1 * USEC_PER_MSEC)
+
+struct ma35d1_adc {
+ struct regmap *regmap;
+ struct clk *clk;
+ struct reset_control *rst;
+ struct completion completion;
+ /* Protect conversion state and register accesses from PM transitions. */
+ struct mutex lock;
+ const struct iio_chan_spec *scan_chan;
+ int irq;
+ unsigned int vref_mV;
+ bool external_vref;
+ /* Set while the clock is gated; EADC registers must not be accessed. */
+ bool suspended;
+};
+
+static const struct regmap_range ma35d1_adc_readable_ranges[] = {
+ regmap_reg_range(MA35D1_EADC_DAT(0),
+ MA35D1_EADC_DAT(MA35D1_EADC_NUM_SAMPLE_MODULES - 1)),
+ regmap_reg_range(MA35D1_EADC_CURDAT, MA35D1_EADC_CTL),
+ regmap_reg_range(MA35D1_EADC_PENDSTS, MA35D1_EADC_OVSTS),
+ regmap_reg_range(MA35D1_EADC_SCTL(0),
+ MA35D1_EADC_SCTL(MA35D1_EADC_NUM_SCTL - 1)),
+ regmap_reg_range(MA35D1_EADC_INTSRC0, MA35D1_EADC_STATUS0),
+ regmap_reg_range(MA35D1_EADC_STATUS2, MA35D1_EADC_CALCTL),
+ regmap_reg_range(MA35D1_EADC_PDMACTL, MA35D1_EADC_PDMACTL),
+ regmap_reg_range(MA35D1_EADC_SELSMP0, MA35D1_EADC_SELSMP1),
+ regmap_reg_range(MA35D1_EADC_REFADJCTL, MA35D1_EADC_REFADJCTL),
+};
+
+static const struct regmap_access_table ma35d1_adc_readable_table = {
+ .yes_ranges = ma35d1_adc_readable_ranges,
+ .n_yes_ranges = ARRAY_SIZE(ma35d1_adc_readable_ranges),
+};
+
+static const struct regmap_range ma35d1_adc_writable_ranges[] = {
+ regmap_reg_range(MA35D1_EADC_CTL, MA35D1_EADC_OVSTS),
+ regmap_reg_range(MA35D1_EADC_SCTL(0),
+ MA35D1_EADC_SCTL(MA35D1_EADC_NUM_SCTL - 1)),
+ regmap_reg_range(MA35D1_EADC_INTSRC0,
+ MA35D1_EADC_CMP(MA35D1_EADC_NUM_CMP - 1)),
+ regmap_reg_range(MA35D1_EADC_STATUS2, MA35D1_EADC_STATUS2),
+ regmap_reg_range(MA35D1_EADC_PWRM, MA35D1_EADC_CALCTL),
+ regmap_reg_range(MA35D1_EADC_PDMACTL, MA35D1_EADC_PDMACTL),
+ regmap_reg_range(MA35D1_EADC_SELSMP0, MA35D1_EADC_SELSMP1),
+ regmap_reg_range(MA35D1_EADC_REFADJCTL, MA35D1_EADC_REFADJCTL),
+};
+
+static const struct regmap_access_table ma35d1_adc_writable_table = {
+ .yes_ranges = ma35d1_adc_writable_ranges,
+ .n_yes_ranges = ARRAY_SIZE(ma35d1_adc_writable_ranges),
+};
+
+/*
+ * Reading DATn or DDATn clears the VALID and OV flags of the result. CURDAT
+ * is the PDMA shadow of DATn, so treat it as read-sensitive as well.
+ */
+static const struct regmap_range ma35d1_adc_precious_ranges[] = {
+ regmap_reg_range(MA35D1_EADC_DAT(0),
+ MA35D1_EADC_DAT(MA35D1_EADC_NUM_SAMPLE_MODULES - 1)),
+ regmap_reg_range(MA35D1_EADC_CURDAT, MA35D1_EADC_CURDAT),
+ regmap_reg_range(MA35D1_EADC_DDAT(0),
+ MA35D1_EADC_DDAT(MA35D1_EADC_NUM_DDAT - 1)),
+};
+
+static const struct regmap_access_table ma35d1_adc_precious_table = {
+ .yes_ranges = ma35d1_adc_precious_ranges,
+ .n_yes_ranges = ARRAY_SIZE(ma35d1_adc_precious_ranges),
+};
+
+/*
+ * Keep the register map uncached because status and conversion data are
+ * updated by hardware, and some status bits use write-one-to-clear semantics.
+ */
+static const struct regmap_config ma35d1_adc_regmap_config = {
+ .reg_bits = 32,
+ .val_bits = 32,
+ .reg_stride = 4,
+ .max_register = MA35D1_EADC_REFADJCTL,
+ .rd_table = &ma35d1_adc_readable_table,
+ .wr_table = &ma35d1_adc_writable_table,
+ .precious_table = &ma35d1_adc_precious_table,
+};
+
+static int ma35d1_adc_enable_irq(struct ma35d1_adc *adc)
+{
+ return regmap_set_bits(adc->regmap, MA35D1_EADC_CTL,
+ MA35D1_EADC_CTL_ADCIEN0);
+}
+
+static int ma35d1_adc_disable_irq(struct ma35d1_adc *adc)
+{
+ return regmap_clear_bits(adc->regmap, MA35D1_EADC_CTL,
+ MA35D1_EADC_CTL_ADCIEN0);
+}
+
+static int ma35d1_adc_setup_reference(struct ma35d1_adc *adc)
+{
+ struct regmap *map = adc->regmap;
+ int ret;
+
+ /*
+ * PDREF powers down the internal reference, so an external reference
+ * must then be supplied through the VREF pin. With the internal
+ * reference, VREFSEL = 0 selects 1.6 V.
+ */
+ if (adc->external_vref) {
+ ret = regmap_set_bits(map, MA35D1_EADC_REFADJCTL,
+ MA35D1_EADC_REFADJCTL_PDREF);
+ if (ret)
+ return ret;
+ } else {
+ ret = regmap_clear_bits(map, MA35D1_EADC_REFADJCTL,
+ MA35D1_EADC_REFADJCTL_PDREF);
+ if (ret)
+ return ret;
+
+ ret = regmap_update_bits(map, MA35D1_EADC_CTL,
+ MA35D1_EADC_CTL_VREFSEL_MASK,
+ MA35D1_EADC_CTL_VREFSEL_1V6);
+ if (ret)
+ return ret;
+ }
+
+ fsleep(MA35D1_EADC_REF_STABLE_US);
+
+ return 0;
+}
+
+static int ma35d1_adc_reset_control(struct ma35d1_adc *adc)
+{
+ struct regmap *map = adc->regmap;
+ unsigned int val;
+ int ret;
+
+ ret = regmap_set_bits(map, MA35D1_EADC_CTL, MA35D1_EADC_CTL_EADCRST);
+ if (ret)
+ return ret;
+
+ return regmap_read_poll_timeout(map, MA35D1_EADC_CTL, val,
+ !(val & MA35D1_EADC_CTL_EADCRST),
+ 1, 1 * USEC_PER_MSEC);
+}
+
+static int ma35d1_adc_reset(struct ma35d1_adc *adc)
+{
+ int ret;
+
+ ret = reset_control_assert(adc->rst);
+ if (ret)
+ return ret;
+
+ fsleep(MA35D1_EADC_RESET_DELAY_US);
+
+ return reset_control_deassert(adc->rst);
+}
+
+static int ma35d1_adc_hw_init(struct ma35d1_adc *adc)
+{
+ struct regmap *map = adc->regmap;
+ unsigned int val;
+ int ret;
+
+ /*
+ * Reset the EADC at probe and on resume. The register reset values
+ * disable all interrupt, PDMA and hardware trigger sources and clear
+ * ADIF0 and the VALID and OV flags of the conversion results.
+ */
+ ret = ma35d1_adc_reset(adc);
+ if (ret)
+ return ret;
+
+ ret = ma35d1_adc_setup_reference(adc);
+ if (ret)
+ return ret;
+
+ ret = regmap_update_bits(map, MA35D1_EADC_SELSMP0,
+ MA35D1_EADC_SELSMP0_SMPT0_MASK,
+ MA35D1_EADC_SELSMP_LONG);
+ if (ret)
+ return ret;
+
+ ret = regmap_set_bits(map, MA35D1_EADC_CTL, MA35D1_EADC_CTL_ADCEN);
+ if (ret)
+ return ret;
+
+ ret = regmap_read_poll_timeout(map, MA35D1_EADC_PWRM, val,
+ val & MA35D1_EADC_PWRM_PWUPRDY,
+ 10, 1 * USEC_PER_SEC);
+ if (ret) {
+ int err;
+
+ /* ADCEN must stay set until PWUPRDY, so abort through module reset. */
+ err = ma35d1_adc_reset(adc);
+ if (err)
+ dev_err(regmap_get_device(map),
+ "failed to reset ADC after power-up failure: %d\n",
+ err);
+ }
+
+ return ret;
+}
+
+static int ma35d1_adc_hw_disable(struct ma35d1_adc *adc)
+{
+ struct regmap *map = adc->regmap;
+ int ret;
+
+ ret = ma35d1_adc_disable_irq(adc);
+ if (ret)
+ return ret;
+
+ return regmap_clear_bits(map, MA35D1_EADC_CTL, MA35D1_EADC_CTL_ADCEN);
+}
+
+static void ma35d1_adc_hw_disable_action(void *data)
+{
+ struct ma35d1_adc *adc = data;
+
+ if (!adc->suspended)
+ ma35d1_adc_hw_disable(adc);
+}
+
+static int ma35d1_adc_config_channel(struct ma35d1_adc *adc,
+ const struct iio_chan_spec *chan)
+{
+ struct regmap *map = adc->regmap;
+ unsigned int mask;
+ int ret;
+
+ mask = MA35D1_EADC_CTL_DIFFEN | MA35D1_EADC_CTL_DMOF;
+ ret = regmap_assign_bits(map, MA35D1_EADC_CTL, mask, chan->differential);
+ if (ret)
+ return ret;
+
+ /*
+ * INTPOS = 0 generates ADIF0 at end of conversion. TRGSEL = 0 disables
+ * the hardware trigger; conversions are started through SWTRG0.
+ */
+ mask = MA35D1_EADC_SCTL_CHSEL_MASK | MA35D1_EADC_SCTL_TRGSEL_MASK |
+ MA35D1_EADC_SCTL_INTPOS;
+ ret = regmap_update_bits(map, MA35D1_EADC_SCTL(0), mask,
+ FIELD_PREP(MA35D1_EADC_SCTL_CHSEL_MASK,
+ chan->channel));
+ if (ret)
+ return ret;
+
+ return regmap_update_bits(map, MA35D1_EADC_INTSRC0,
+ MA35D1_EADC_INTSRC0_SPLIEN_MASK,
+ MA35D1_EADC_INTSRC0_SPLIEN0);
+}
+
+static void ma35d1_adc_abort_conversion(struct ma35d1_adc *adc)
+{
+ struct regmap *map = adc->regmap;
+ struct device *dev = regmap_get_device(map);
+ unsigned int val;
+ int ret;
+
+ /*
+ * Stop ADC interrupt generation before resetting the conversion state,
+ * then wait for any running IRQ handler before discarding the result
+ * and clearing ADIF0.
+ */
+ ret = ma35d1_adc_disable_irq(adc);
+ if (ret)
+ dev_err(dev, "failed to disable ADC interrupt: %d\n", ret);
+
+ /*
+ * Reset the EADC control circuits to terminate a conversion which may
+ * still be in progress. EADCRST is cleared automatically by hardware
+ * when the reset completes and the EADC register values are preserved.
+ */
+ ret = ma35d1_adc_reset_control(adc);
+ if (ret)
+ dev_err(dev, "failed to reset timed out conversion: %d\n", ret);
+
+ synchronize_irq(adc->irq);
+
+ ret = regmap_read(map, MA35D1_EADC_DAT(0), &val);
+ if (ret)
+ dev_err(dev, "failed to discard timed out ADC data: %d\n", ret);
+
+ ret = regmap_write(map, MA35D1_EADC_STATUS2, MA35D1_EADC_STATUS2_ADIF0);
+ if (ret)
+ dev_err(dev, "failed to clear ADC interrupt status: %d\n", ret);
+}
+
+static int ma35d1_adc_read_conversion(struct ma35d1_adc *adc,
+ const struct iio_chan_spec *chan, u16 *raw)
+{
+ struct regmap *map = adc->regmap;
+ unsigned int val;
+ long timeout;
+ int ret;
+
+ reinit_completion(&adc->completion);
+
+ ret = regmap_write(map, MA35D1_EADC_STATUS2, MA35D1_EADC_STATUS2_ADIF0);
+ if (ret)
+ return ret;
+
+ ret = ma35d1_adc_config_channel(adc, chan);
+ if (ret)
+ return ret;
+
+ ret = ma35d1_adc_enable_irq(adc);
+ if (ret)
+ return ret;
+
+ ret = regmap_write(map, MA35D1_EADC_SWTRG, MA35D1_EADC_SWTRG_SWTRG0);
+ if (ret) {
+ ma35d1_adc_disable_irq(adc);
+ return ret;
+ }
+
+ timeout = wait_for_completion_timeout(&adc->completion, MA35D1_EADC_TIMEOUT);
+ if (!timeout) {
+ ma35d1_adc_abort_conversion(adc);
+ return -ETIMEDOUT;
+ }
+
+ ret = ma35d1_adc_disable_irq(adc);
+ if (ret)
+ return ret;
+
+ ret = regmap_read(map, MA35D1_EADC_DAT(0), &val);
+ if (ret)
+ return ret;
+
+ if (!(val & MA35D1_EADC_DAT_VALID))
+ return -EIO;
+
+ if (val & MA35D1_EADC_DAT_OV)
+ return -EOVERFLOW;
+
+ *raw = FIELD_GET(MA35D1_EADC_DAT_RESULT_12BIT, val);
+
+ return 0;
+}
+
+static irqreturn_t ma35d1_adc_isr(int irq, void *data)
+{
+ struct iio_dev *indio_dev = data;
+ struct ma35d1_adc *adc = iio_priv(indio_dev);
+ struct regmap *map = adc->regmap;
+ unsigned int status;
+ int ret;
+
+ ret = regmap_read(map, MA35D1_EADC_STATUS2, &status);
+ if (ret)
+ return IRQ_NONE;
+
+ if (!(status & MA35D1_EADC_STATUS2_ADIF0))
+ return IRQ_NONE;
+
+ ret = regmap_write(map, MA35D1_EADC_STATUS2, MA35D1_EADC_STATUS2_ADIF0);
+ if (ret)
+ dev_err_ratelimited(regmap_get_device(map),
+ "failed to clear ADC interrupt status: %d\n", ret);
+
+ complete(&adc->completion);
+
+ return IRQ_HANDLED;
+}
+
+static int ma35d1_adc_read_raw(struct iio_dev *indio_dev,
+ const struct iio_chan_spec *chan,
+ int *val, int *val2, long mask)
+{
+ struct ma35d1_adc *adc = iio_priv(indio_dev);
+ u16 raw;
+ int ret;
+
+ switch (mask) {
+ case IIO_CHAN_INFO_RAW: {
+ IIO_DEV_ACQUIRE_DIRECT_MODE(indio_dev, claim);
+ if (IIO_DEV_ACQUIRE_FAILED(claim))
+ return -EBUSY;
+
+ guard(mutex)(&adc->lock);
+
+ if (adc->suspended)
+ return -EBUSY;
+
+ ret = ma35d1_adc_read_conversion(adc, chan, &raw);
+ if (ret)
+ return ret;
+
+ if (chan->differential)
+ *val = sign_extend32(raw, MA35D1_EADC_RESOLUTION - 1);
+ else
+ *val = raw;
+
+ return IIO_VAL_INT;
+ }
+ case IIO_CHAN_INFO_SCALE:
+ *val = adc->vref_mV;
+ /* Differential mode has a VREF / 2048 LSB; single-ended uses /4096. */
+ *val2 = chan->differential ? MA35D1_EADC_DIFF_SCALE_BITS : MA35D1_EADC_RESOLUTION;
+ return IIO_VAL_FRACTIONAL_LOG2;
+ default:
+ return -EINVAL;
+ }
+}
+
+static int ma35d1_adc_update_scan_mode(struct iio_dev *indio_dev,
+ const unsigned long *scan_mask)
+{
+ struct ma35d1_adc *adc = iio_priv(indio_dev);
+ const struct iio_chan_spec *scan_chan = NULL;
+ int bit;
+
+ if (!iio_validate_scan_mask_onehot(indio_dev, scan_mask))
+ return -EINVAL;
+
+ iio_for_each_active_channel(indio_dev, bit) {
+ const struct iio_chan_spec *chan = &indio_dev->channels[bit];
+
+ if (chan->type != IIO_VOLTAGE)
+ return -EINVAL;
+
+ scan_chan = chan;
+ break;
+ }
+
+ if (!scan_chan)
+ return -EINVAL;
+
+ adc->scan_chan = scan_chan;
+
+ return 0;
+}
+
+static int ma35d1_adc_buffer_postenable(struct iio_dev *indio_dev)
+{
+ struct ma35d1_adc *adc = iio_priv(indio_dev);
+
+ guard(mutex)(&adc->lock);
+
+ if (adc->suspended)
+ return -EBUSY;
+
+ if (!adc->scan_chan)
+ return -EINVAL;
+
+ return 0;
+}
+
+static void ma35d1_adc_trigger_capture(struct iio_poll_func *pf)
+{
+ struct iio_dev *indio_dev = pf->indio_dev;
+ struct ma35d1_adc *adc = iio_priv(indio_dev);
+ struct {
+ u16 channel;
+ aligned_s64 timestamp;
+ } scan = { };
+ int ret;
+
+ guard(mutex)(&adc->lock);
+
+ if (adc->suspended || !adc->scan_chan)
+ return;
+
+ ret = ma35d1_adc_read_conversion(adc, adc->scan_chan,
+ &scan.channel);
+ if (ret)
+ return;
+
+ iio_push_to_buffers_with_ts(indio_dev, &scan, sizeof(scan), pf->timestamp);
+}
+
+static irqreturn_t ma35d1_adc_trigger_handler(int irq, void *p)
+{
+ struct iio_poll_func *pf = p;
+
+ ma35d1_adc_trigger_capture(pf);
+ iio_trigger_notify_done(pf->indio_dev->trig);
+
+ return IRQ_HANDLED;
+}
+
+static const struct iio_buffer_setup_ops ma35d1_adc_buffer_ops = {
+ .postenable = ma35d1_adc_buffer_postenable,
+ .validate_scan_mask = iio_validate_scan_mask_onehot,
+};
+
+static int ma35d1_adc_fwnode_xlate(struct iio_dev *indio_dev,
+ const struct fwnode_reference_args *iiospec)
+{
+ if (iiospec->nargs != 1)
+ return -EINVAL;
+
+ for (unsigned int i = 0; i < indio_dev->num_channels; i++) {
+ const struct iio_chan_spec *chan = &indio_dev->channels[i];
+
+ if (chan->type == IIO_VOLTAGE &&
+ chan->address == iiospec->args[0])
+ return i;
+ }
+
+ return -EINVAL;
+}
+
+static const struct iio_info ma35d1_adc_info = {
+ .read_raw = ma35d1_adc_read_raw,
+ .update_scan_mode = ma35d1_adc_update_scan_mode,
+ .fwnode_xlate = ma35d1_adc_fwnode_xlate,
+};
+
+static int ma35d1_adc_parse_diff_channel(struct device *dev,
+ struct fwnode_handle *child,
+ struct iio_chan_spec *chan,
+ unsigned long *used_diff_pairs,
+ u32 reg, unsigned int scan_index)
+{
+ u32 inputs[2];
+ int ret;
+
+ ret = fwnode_property_read_u32_array(child, "diff-channels", inputs,
+ ARRAY_SIZE(inputs));
+ if (ret)
+ return dev_err_probe(dev, ret,
+ "invalid diff-channels for index %u\n", reg);
+
+ if (inputs[0] >= MA35D1_EADC_NUM_DIFF_PAIRS || inputs[1] != inputs[0] + 4)
+ return dev_err_probe(dev, -ECHRNG,
+ "invalid differential inputs %u-%u\n",
+ inputs[0], inputs[1]);
+
+ if (test_and_set_bit(inputs[0], used_diff_pairs))
+ return dev_err_probe(dev, -EEXIST,
+ "duplicate differential inputs %u-%u\n",
+ inputs[0], inputs[1]);
+
+ *chan = (struct iio_chan_spec) {
+ .type = IIO_VOLTAGE,
+ .indexed = 1,
+ .differential = 1,
+ .channel = inputs[0],
+ .channel2 = inputs[1],
+ .address = reg,
+ .scan_index = scan_index,
+ .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
+ BIT(IIO_CHAN_INFO_SCALE),
+ .scan_type = {
+ .format = IIO_SCAN_FORMAT_SIGNED_INT,
+ .realbits = MA35D1_EADC_RESOLUTION,
+ .storagebits = 16,
+ .endianness = IIO_CPU,
+ },
+ };
+
+ return 0;
+}
+
+static int ma35d1_adc_parse_single_channel(struct device *dev,
+ struct fwnode_handle *child,
+ struct iio_chan_spec *chan,
+ unsigned long *used_single_inputs,
+ bool has_single, u32 reg,
+ unsigned int scan_index)
+{
+ u32 input = reg;
+ int ret;
+
+ if (has_single) {
+ ret = fwnode_property_read_u32(child, "single-channel", &input);
+ if (ret)
+ return dev_err_probe(dev, ret,
+ "invalid single-channel for index %u\n",
+ reg);
+ }
+
+ if (input >= MA35D1_EADC_MAX_EXT_CHANNELS)
+ return dev_err_probe(dev, -ECHRNG, "invalid ADC input %u\n", input);
+
+ if (test_and_set_bit(input, used_single_inputs))
+ return dev_err_probe(dev, -EEXIST, "duplicate ADC input %u\n", input);
+
+ *chan = (struct iio_chan_spec) {
+ .type = IIO_VOLTAGE,
+ .indexed = 1,
+ .channel = input,
+ .address = reg,
+ .scan_index = scan_index,
+ .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
+ BIT(IIO_CHAN_INFO_SCALE),
+ .scan_type = {
+ .format = IIO_SCAN_FORMAT_UNSIGNED_INT,
+ .realbits = MA35D1_EADC_RESOLUTION,
+ .storagebits = 16,
+ .endianness = IIO_CPU,
+ },
+ };
+
+ return 0;
+}
+
+static int ma35d1_adc_parse_channels(struct iio_dev *indio_dev,
+ struct device *dev)
+{
+ DECLARE_BITMAP(used_single_inputs, MA35D1_EADC_MAX_EXT_CHANNELS) = { };
+ DECLARE_BITMAP(used_diff_pairs, MA35D1_EADC_NUM_DIFF_PAIRS) = { };
+ DECLARE_BITMAP(used_regs, MA35D1_EADC_MAX_EXT_CHANNELS) = { };
+ struct iio_chan_spec *channels;
+ unsigned int scan_index = 0;
+ int num_channels;
+ int ret;
+
+ num_channels = device_get_child_node_count(dev);
+ if (!num_channels)
+ return dev_err_probe(dev, -ENOENT, "no ADC channels configured\n");
+
+ if (num_channels > MA35D1_EADC_MAX_EXT_CHANNELS)
+ return dev_err_probe(dev, -ENOSPC, "too many ADC channels\n");
+
+ channels = devm_kcalloc(dev, size_add(num_channels, 1),
+ sizeof(*channels), GFP_KERNEL);
+ if (!channels)
+ return -ENOMEM;
+
+ device_for_each_child_node_scoped(dev, child) {
+ bool has_diff, has_single;
+ u32 reg;
+
+ ret = fwnode_property_read_u32(child, "reg", ®);
+ if (ret)
+ return dev_err_probe(dev, ret,
+ "missing channel reg property\n");
+
+ if (reg >= MA35D1_EADC_MAX_EXT_CHANNELS)
+ return dev_err_probe(dev, -ENOSPC,
+ "invalid ADC channel index %u\n", reg);
+
+ if (test_and_set_bit(reg, used_regs))
+ return dev_err_probe(dev, -EEXIST,
+ "duplicate ADC channel index %u\n", reg);
+
+ has_single = fwnode_property_present(child, "single-channel");
+ has_diff = fwnode_property_present(child, "diff-channels");
+ if (has_single && has_diff)
+ return dev_err_probe(dev, -EINVAL,
+ "channel %u has conflicting input properties\n",
+ reg);
+
+ if (has_diff)
+ ret = ma35d1_adc_parse_diff_channel(dev, child,
+ &channels[scan_index],
+ used_diff_pairs, reg,
+ scan_index);
+ else
+ ret = ma35d1_adc_parse_single_channel(dev, child,
+ &channels[scan_index],
+ used_single_inputs,
+ has_single, reg,
+ scan_index);
+ if (ret)
+ return ret;
+
+ scan_index++;
+ }
+
+ channels[scan_index] = IIO_CHAN_SOFT_TIMESTAMP(scan_index);
+ indio_dev->channels = channels;
+ indio_dev->num_channels = scan_index + 1;
+
+ return 0;
+}
+
+static int ma35d1_adc_init_vref(struct ma35d1_adc *adc, struct device *dev)
+{
+ int ret;
+
+ ret = devm_regulator_get_enable_read_voltage(dev, "vref");
+ if (ret == -ENODEV) {
+ adc->vref_mV = MA35D1_EADC_INTERNAL_VREF_mV;
+ } else if (ret < 0) {
+ return dev_err_probe(dev, ret, "failed to get VREF supply\n");
+ } else if (ret > 0) {
+ adc->external_vref = true;
+ adc->vref_mV = ret / (MICRO / MILLI);
+ } else {
+ return dev_err_probe(dev, -EINVAL, "invalid VREF voltage\n");
+ }
+
+ return 0;
+}
+
+static void ma35d1_adc_clk_disable_action(void *data)
+{
+ struct ma35d1_adc *adc = data;
+
+ /* A failed resume leaves the clock gated. */
+ if (adc->suspended)
+ return;
+
+ clk_disable_unprepare(adc->clk);
+}
+
+static int ma35d1_adc_probe(struct platform_device *pdev)
+{
+ struct device *dev = &pdev->dev;
+ struct iio_dev *indio_dev;
+ struct ma35d1_adc *adc;
+ void __iomem *regs;
+ int ret;
+
+ indio_dev = devm_iio_device_alloc(dev, sizeof(*adc));
+ if (!indio_dev)
+ return -ENOMEM;
+
+ adc = iio_priv(indio_dev);
+ init_completion(&adc->completion);
+
+ ret = devm_mutex_init(dev, &adc->lock);
+ if (ret)
+ return ret;
+
+ regs = devm_platform_ioremap_resource(pdev, 0);
+ if (IS_ERR(regs))
+ return dev_err_probe(dev, PTR_ERR(regs),
+ "failed to map registers\n");
+
+ adc->regmap = devm_regmap_init_mmio(dev, regs, &ma35d1_adc_regmap_config);
+ if (IS_ERR(adc->regmap))
+ return dev_err_probe(dev, PTR_ERR(adc->regmap),
+ "failed to initialize regmap\n");
+
+ adc->clk = devm_clk_get(dev, NULL);
+ if (IS_ERR(adc->clk))
+ return dev_err_probe(dev, PTR_ERR(adc->clk),
+ "failed to get ADC clock\n");
+
+ ret = clk_prepare_enable(adc->clk);
+ if (ret)
+ return dev_err_probe(dev, ret, "failed to enable ADC clock\n");
+
+ ret = devm_add_action_or_reset(dev, ma35d1_adc_clk_disable_action, adc);
+ if (ret)
+ return ret;
+
+ adc->rst = devm_reset_control_get_exclusive(dev, NULL);
+ if (IS_ERR(adc->rst))
+ return dev_err_probe(dev, PTR_ERR(adc->rst),
+ "failed to get reset control\n");
+
+ ret = ma35d1_adc_init_vref(adc, dev);
+ if (ret)
+ return ret;
+
+ indio_dev->name = "ma35d1-eadc";
+ indio_dev->modes = INDIO_DIRECT_MODE;
+ indio_dev->info = &ma35d1_adc_info;
+
+ ret = ma35d1_adc_parse_channels(indio_dev, dev);
+ if (ret)
+ return ret;
+
+ ret = ma35d1_adc_hw_init(adc);
+ if (ret)
+ return dev_err_probe(dev, ret, "failed to initialize ADC\n");
+
+ ret = devm_add_action_or_reset(dev, ma35d1_adc_hw_disable_action, adc);
+ if (ret)
+ return ret;
+
+ adc->irq = platform_get_irq(pdev, 0);
+ if (adc->irq < 0)
+ return adc->irq;
+
+ ret = devm_request_irq(dev, adc->irq, ma35d1_adc_isr, 0,
+ dev_name(dev), indio_dev);
+ if (ret)
+ return ret;
+
+ ret = devm_iio_triggered_buffer_setup(dev, indio_dev,
+ iio_pollfunc_store_time,
+ ma35d1_adc_trigger_handler,
+ &ma35d1_adc_buffer_ops);
+ if (ret)
+ return ret;
+
+ platform_set_drvdata(pdev, indio_dev);
+
+ return devm_iio_device_register(dev, indio_dev);
+}
+
+static int ma35d1_adc_suspend_hw(struct ma35d1_adc *adc)
+{
+ struct regmap *map = adc->regmap;
+ int ret;
+
+ guard(mutex)(&adc->lock);
+
+ if (adc->suspended)
+ return 0;
+
+ ret = ma35d1_adc_disable_irq(adc);
+ if (ret)
+ return ret;
+
+ synchronize_irq(adc->irq);
+
+ ret = regmap_clear_bits(map, MA35D1_EADC_CTL, MA35D1_EADC_CTL_ADCEN);
+ if (ret) {
+ /*
+ * ADCIEN0 is enabled only while a conversion is active. Holding
+ * adc->lock here guarantees that no conversion is in progress, so
+ * leave it disabled. The next conversion enables it again.
+ */
+ return ret;
+ }
+
+ adc->suspended = true;
+ clk_disable_unprepare(adc->clk);
+
+ return 0;
+}
+
+static int ma35d1_adc_resume_hw(struct ma35d1_adc *adc)
+{
+ int ret;
+
+ guard(mutex)(&adc->lock);
+
+ if (!adc->suspended)
+ return 0;
+
+ ret = clk_prepare_enable(adc->clk);
+ if (ret)
+ return ret;
+
+ ret = ma35d1_adc_hw_init(adc);
+ if (ret) {
+ clk_disable_unprepare(adc->clk);
+ return ret;
+ }
+
+ adc->suspended = false;
+
+ return 0;
+}
+
+static int ma35d1_adc_suspend(struct device *dev)
+{
+ struct iio_dev *indio_dev = dev_get_drvdata(dev);
+ struct ma35d1_adc *adc = iio_priv(indio_dev);
+ bool buffer_enabled;
+ int ret;
+
+ /* A failed resume leaves the device and its triggering suspended. */
+ scoped_guard(mutex, &adc->lock) {
+ if (adc->suspended)
+ return 0;
+ }
+
+ /*
+ * pollfunc->irq is not initialized until the pollfunc is first
+ * attached to a trigger, so only suspend and resume triggering
+ * while the buffer is enabled.
+ */
+ buffer_enabled = iio_buffer_enabled(indio_dev);
+ if (buffer_enabled) {
+ ret = iio_device_suspend_triggering(indio_dev);
+ if (ret)
+ return ret;
+ }
+
+ ret = ma35d1_adc_suspend_hw(adc);
+ if (ret && buffer_enabled)
+ iio_device_resume_triggering(indio_dev);
+
+ return ret;
+}
+
+static int ma35d1_adc_resume(struct device *dev)
+{
+ struct iio_dev *indio_dev = dev_get_drvdata(dev);
+ struct ma35d1_adc *adc = iio_priv(indio_dev);
+ int ret;
+
+ scoped_guard(mutex, &adc->lock) {
+ if (!adc->suspended)
+ return 0;
+ }
+
+ ret = ma35d1_adc_resume_hw(adc);
+ if (ret)
+ return ret;
+
+ if (!iio_buffer_enabled(indio_dev))
+ return 0;
+
+ return iio_device_resume_triggering(indio_dev);
+}
+
+static DEFINE_SIMPLE_DEV_PM_OPS(ma35d1_adc_pm_ops,
+ ma35d1_adc_suspend, ma35d1_adc_resume);
+
+static const struct of_device_id ma35d1_adc_of_match[] = {
+ { .compatible = "nuvoton,ma35d1-eadc" },
+ { }
+};
+MODULE_DEVICE_TABLE(of, ma35d1_adc_of_match);
+
+static struct platform_driver ma35d1_adc_driver = {
+ .probe = ma35d1_adc_probe,
+ .driver = {
+ .name = "ma35d1-eadc",
+ .of_match_table = ma35d1_adc_of_match,
+ .pm = pm_sleep_ptr(&ma35d1_adc_pm_ops),
+ },
+};
+module_platform_driver(ma35d1_adc_driver);
+
+MODULE_AUTHOR("Chi-Wen Weng <cwweng@nuvoton.com>");
+MODULE_DESCRIPTION("Nuvoton MA35D1 EADC driver");
+MODULE_LICENSE("GPL");
--
2.25.1
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH v5 2/3] dt-bindings: iio: adc: Add Nuvoton MA35D1 EADC
2026-11-17 8:28 ` [PATCH v5 2/3] dt-bindings: iio: adc: Add Nuvoton MA35D1 EADC Chi-Wen Weng
@ 2026-11-17 8:42 ` sashiko-bot
0 siblings, 0 replies; 5+ messages in thread
From: sashiko-bot @ 2026-11-17 8:42 UTC (permalink / raw)
To: Chi-Wen Weng; +Cc: devicetree, robh, conor+dt
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] dt-bindings: iio: adc: ma35d1-eadc: missing io-channel
specifier for VBAT
--
commit 9ca0b0a7e74294912e1ef88df7980f863b72d4d7
Author: Chi-Wen Weng <cwweng@nuvoton.com>
dt-bindings: iio: adc: Add Nuvoton MA35D1 EADC
This commit adds the Device Tree binding schema for the Nuvoton MA35D1
Enhanced Analog-to-Digital Converter (EADC).
> diff --git a/Documentation/devicetree/bindings/iio/adc/nuvoton,ma35d1-eadc.yaml b/Documentation/devicetree/bindings/iio/adc/nuvoton,ma35d1-eadc.yaml
> new file mode 100644
> index 0000000000000..1bf64913d67db
> --- /dev/null
> +++ b/Documentation/devicetree/bindings/iio/adc/nuvoton,ma35d1-eadc.yaml
[ ... ]
> + The external inputs can be used as eight single-ended channels or as
> + four fixed differential pairs: 0-4, 1-5, 2-6 and 3-7. The VBAT input
> + needs no devicetree description.
[ ... ]
> + '#io-channel-cells':
> + const: 1
> + description:
> + The cell selects the logical external EADC channel identified by the reg
> + value of the corresponding channel child node. Only channels represented
> + by child nodes are valid I/O channel specifiers.
[Severity: Medium]
As reported by Jonathan Cameron on v3 [1], how can a devicetree IIO
consumer reference the internal VBAT channel if VBAT has no child node and
#io-channel-cells only allows specifiers matching channel@[0-7] child
nodes?
The anyOf constraint also requires at least one channel@[0-7] child node,
preventing a board from using the EADC purely for VBAT monitoring without
defining an external channel node, and adding a VBAT specifier later would
require changing the #io-channel-cells binding contract.
[1] https://lore.kernel.org/r/20261101001934.23588aa2@jic23-hlaptop
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261117082832.1551574-1-cwweng.linux@gmail.com?part=2
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-11-17 8:42 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-17 8:28 [PATCH v5 0/3] iio: adc: Add Nuvoton MA35D1 EADC support Chi-Wen Weng
2026-11-17 8:28 ` [PATCH v5 1/3] dt-bindings: iio: adc: Fix channel property validation Chi-Wen Weng
2026-11-17 8:28 ` [PATCH v5 2/3] dt-bindings: iio: adc: Add Nuvoton MA35D1 EADC Chi-Wen Weng
2026-11-17 8:42 ` sashiko-bot
2026-11-17 8:28 ` [PATCH v5 3/3] iio: adc: Add Nuvoton MA35D1 EADC driver Chi-Wen Weng
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox