* [PATCH 0/2] iio: adc: Add Nuvoton MA35D1 EADC support @ 2026-06-25 11:06 Chi-Wen Weng 2026-06-25 11:06 ` [PATCH 1/2] dt-bindings: iio: adc: Add Nuvoton MA35D1 EADC Chi-Wen Weng 2026-06-25 11:06 ` [PATCH 2/2] iio: adc: Add Nuvoton MA35D1 EADC driver Chi-Wen Weng 0 siblings, 2 replies; 7+ messages in thread From: Chi-Wen Weng @ 2026-06-25 11:06 UTC (permalink / raw) To: jic23, robh, krzk+dt, conor+dt Cc: dlechner, nuno.sa, andy, linux-arm-kernel, linux-iio, devicetree, linux-kernel, cwweng, cwweng.linux From: Chi-Wen Weng <cwweng@nuvoton.com> This series adds devicetree binding and IIO driver support for the Nuvoton MA35D1 Enhanced ADC controller. The MA35D1 EADC controller supports multiple ADC input channels. This initial upstream driver supports direct raw reads and triggered buffered capture using the controller end-of-conversion interrupt as the IIO device trigger. ADC channels are described using standard firmware child nodes. Both single-ended and differential channels are supported. Since the differential enable bit is global in the controller, mixed single-ended and differential buffered scans are rejected. DMA support is intentionally not included in this initial version. The driver uses the interrupt-driven conversion path to keep the first upstream submission small and easier to review. Patch 1 adds the devicetree binding. Patch 2 adds the MA35D1 EADC IIO driver. Chi-Wen Weng (2): dt-bindings: iio: adc: Add Nuvoton MA35D1 EADC iio: adc: Add Nuvoton MA35D1 EADC driver .../bindings/iio/adc/nuvoton,ma35d1-eadc.yaml | 100 +++ drivers/iio/adc/Kconfig | 10 + drivers/iio/adc/Makefile | 1 + drivers/iio/adc/ma35d1_eadc.c | 636 ++++++++++++++++++ 4 files changed, 747 insertions(+) create mode 100644 Documentation/devicetree/bindings/iio/adc/nuvoton,ma35d1-eadc.yaml create mode 100644 drivers/iio/adc/ma35d1_eadc.c -- 2.25.1 ^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH 1/2] dt-bindings: iio: adc: Add Nuvoton MA35D1 EADC 2026-06-25 11:06 [PATCH 0/2] iio: adc: Add Nuvoton MA35D1 EADC support Chi-Wen Weng @ 2026-06-25 11:06 ` Chi-Wen Weng 2026-06-25 11:18 ` sashiko-bot 2026-06-25 16:24 ` Conor Dooley 2026-06-25 11:06 ` [PATCH 2/2] iio: adc: Add Nuvoton MA35D1 EADC driver Chi-Wen Weng 1 sibling, 2 replies; 7+ messages in thread From: Chi-Wen Weng @ 2026-06-25 11:06 UTC (permalink / raw) To: jic23, robh, krzk+dt, conor+dt Cc: dlechner, nuno.sa, andy, linux-arm-kernel, linux-iio, devicetree, linux-kernel, cwweng, cwweng.linux From: Chi-Wen Weng <cwweng@nuvoton.com> Add devicetree binding for the Enhanced ADC controller found on Nuvoton MA35D1 SoCs. The controller has one register region, one interrupt and one functional clock. ADC inputs are described using standard channel child nodes, including optional differential channel pairs. Signed-off-by: Chi-Wen Weng <cwweng@nuvoton.com> --- .../bindings/iio/adc/nuvoton,ma35d1-eadc.yaml | 100 ++++++++++++++++++ 1 file changed, 100 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..ae7ad0f7689a --- /dev/null +++ b/Documentation/devicetree/bindings/iio/adc/nuvoton,ma35d1-eadc.yaml @@ -0,0 +1,100 @@ +# 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 ADC controller integrated in the MA35D1 SoC. Each enabled ADC + input is described by a child channel node. + +properties: + compatible: + const: nuvoton,ma35d1-eadc + + reg: + maxItems: 1 + + interrupts: + maxItems: 1 + + clocks: + maxItems: 1 + + '#address-cells': + const: 1 + + '#size-cells': + const: 0 + +patternProperties: + '^channel@[0-8]$': + type: object + $ref: adc.yaml + unevaluatedProperties: false + + properties: + reg: + minimum: 0 + maximum: 8 + + diff-channels: + minItems: 2 + maxItems: 2 + items: + minimum: 0 + maximum: 8 + + required: + - reg + +required: + - compatible + - reg + - interrupts + - clocks + - '#address-cells' + - '#size-cells' + +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> + + 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>; + clocks = <&clk EADC_GATE>; + + #address-cells = <1>; + #size-cells = <0>; + + channel@0 { + reg = <0>; + }; + + channel@1 { + reg = <1>; + }; + + channel@2 { + reg = <2>; + diff-channels = <2 3>; + }; + }; + }; +... -- 2.25.1 ^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH 1/2] dt-bindings: iio: adc: Add Nuvoton MA35D1 EADC 2026-06-25 11:06 ` [PATCH 1/2] dt-bindings: iio: adc: Add Nuvoton MA35D1 EADC Chi-Wen Weng @ 2026-06-25 11:18 ` sashiko-bot 2026-06-25 16:24 ` Conor Dooley 1 sibling, 0 replies; 7+ messages in thread From: sashiko-bot @ 2026-06-25 11:18 UTC (permalink / raw) To: Chi-Wen Weng; +Cc: devicetree, conor+dt, robh Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] Missing `resets` property for a hardware block that has a dedicated reset line. -- commit 1f48c0ec2f3eb7b2358c1e0e2db87a26859d5fc5 Author: Chi-Wen Weng <cwweng@nuvoton.com> dt-bindings: iio: adc: Add Nuvoton MA35D1 EADC This commit adds the devicetree binding for the Enhanced ADC controller found on Nuvoton MA35D1 SoCs. It describes the register region, interrupt, functional clock, and standard channel child nodes. > 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..ae7ad0f7689ab > --- /dev/null > +++ b/Documentation/devicetree/bindings/iio/adc/nuvoton,ma35d1-eadc.yaml [ ... ] > +properties: > + compatible: > + const: nuvoton,ma35d1-eadc > + > + reg: > + maxItems: 1 > + > + interrupts: > + maxItems: 1 > + > + clocks: > + maxItems: 1 [Severity: Low] Is the resets property missing from this binding? The Nuvoton MA35D1 SoC explicitly provides a reset line for the Enhanced ADC block (defined as MA35D1_RESET_EADC0 in include/dt-bindings/reset/nuvoton,ma35d1-reset.h). Should the resets property be declared here and added to the required list to accurately reflect the hardware dependencies and avoid dt-schema validation errors if a board uses the reset line? [ ... ] -- Sashiko AI review · https://sashiko.dev/#/patchset/20260625110638.38438-1-cwweng.linux@gmail.com?part=1 ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH 1/2] dt-bindings: iio: adc: Add Nuvoton MA35D1 EADC 2026-06-25 11:06 ` [PATCH 1/2] dt-bindings: iio: adc: Add Nuvoton MA35D1 EADC Chi-Wen Weng 2026-06-25 11:18 ` sashiko-bot @ 2026-06-25 16:24 ` Conor Dooley 1 sibling, 0 replies; 7+ messages in thread From: Conor Dooley @ 2026-06-25 16:24 UTC (permalink / raw) To: Chi-Wen Weng Cc: jic23, robh, krzk+dt, conor+dt, dlechner, nuno.sa, andy, linux-arm-kernel, linux-iio, devicetree, linux-kernel, cwweng [-- Attachment #1: Type: text/plain, Size: 540 bytes --] On Thu, Jun 25, 2026 at 07:06:37PM +0800, Chi-Wen Weng wrote: > From: Chi-Wen Weng <cwweng@nuvoton.com> > > Add devicetree binding for the Enhanced ADC controller found on > Nuvoton MA35D1 SoCs. > > The controller has one register region, one interrupt and one functional > clock. ADC inputs are described using standard channel child nodes, > including optional differential channel pairs. > > Signed-off-by: Chi-Wen Weng <cwweng@nuvoton.com> Reviewed-by: Conor Dooley <conor.dooley@microchip.com> pw-bot: not-applicable [-- Attachment #2: signature.asc --] [-- Type: application/pgp-signature, Size: 228 bytes --] ^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH 2/2] iio: adc: Add Nuvoton MA35D1 EADC driver 2026-06-25 11:06 [PATCH 0/2] iio: adc: Add Nuvoton MA35D1 EADC support Chi-Wen Weng 2026-06-25 11:06 ` [PATCH 1/2] dt-bindings: iio: adc: Add Nuvoton MA35D1 EADC Chi-Wen Weng @ 2026-06-25 11:06 ` Chi-Wen Weng 2026-06-25 11:20 ` sashiko-bot 2026-06-26 12:54 ` Andy Shevchenko 1 sibling, 2 replies; 7+ messages in thread From: Chi-Wen Weng @ 2026-06-25 11:06 UTC (permalink / raw) To: jic23, robh, krzk+dt, conor+dt Cc: dlechner, nuno.sa, andy, linux-arm-kernel, linux-iio, devicetree, linux-kernel, cwweng, cwweng.linux From: Chi-Wen Weng <cwweng@nuvoton.com> Add an IIO driver for the Nuvoton MA35D1 Enhanced ADC controller. The driver supports direct raw reads and triggered buffered capture. The controller end-of-conversion interrupt is exposed as the device trigger and is used to push samples into the IIO buffer. Channels are described by firmware child nodes and can be configured as single-ended or differential inputs. Since the differential enable bit is global, mixed single-ended and differential buffered scans are rejected. DMA support is intentionally not included in this initial upstream driver; conversions are handled through the interrupt-driven path. Signed-off-by: Chi-Wen Weng <cwweng@nuvoton.com> --- drivers/iio/adc/Kconfig | 10 + drivers/iio/adc/Makefile | 1 + drivers/iio/adc/ma35d1_eadc.c | 636 ++++++++++++++++++++++++++++++++++ 3 files changed, 647 insertions(+) create mode 100644 drivers/iio/adc/ma35d1_eadc.c diff --git a/drivers/iio/adc/Kconfig b/drivers/iio/adc/Kconfig index 1c663c98c6c9..43409999a94b 100644 --- a/drivers/iio/adc/Kconfig +++ b/drivers/iio/adc/Kconfig @@ -981,6 +981,16 @@ config LTC2497 To compile this driver as a module, choose M here: the module will be called ltc2497. +config MA35D1_EADC + tristate "MA35D1 EADC driver" + select IIO_BUFFER + select IIO_TRIGGERED_BUFFER + help + Say yes here to build support for MA35D1 EADC. + + To compile this driver as a module, choose M here: the module will be + called ma35d1. + config MAX1027 tristate "Maxim max1027 ADC driver" depends on SPI diff --git a/drivers/iio/adc/Makefile b/drivers/iio/adc/Makefile index 707dd708912f..7b9b38688223 100644 --- a/drivers/iio/adc/Makefile +++ b/drivers/iio/adc/Makefile @@ -85,6 +85,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..0c075126e139 --- /dev/null +++ b/drivers/iio/adc/ma35d1_eadc.c @@ -0,0 +1,636 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Nuvoton MA35D1 EADC driver + * + * Copyright (c) 2026 Nuvoton Technology Corp. + */ + +#include <linux/bitfield.h> +#include <linux/bits.h> +#include <linux/bitmap.h> +#include <linux/clk.h> +#include <linux/completion.h> +#include <linux/device.h> +#include <linux/err.h> +#include <linux/interrupt.h> +#include <linux/io.h> +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/mod_devicetable.h> +#include <linux/mutex.h> +#include <linux/platform_device.h> +#include <linux/pm.h> +#include <linux/property.h> + +#include <linux/iio/buffer.h> +#include <linux/iio/iio.h> +#include <linux/iio/trigger.h> +#include <linux/iio/trigger_consumer.h> +#include <linux/iio/triggered_buffer.h> + +#define MA35D1_EADC_DAT(n) (0x00 + (n) * 0x04) +#define MA35D1_EADC_CTL 0x50 +#define MA35D1_EADC_SWTRG 0x54 +#define MA35D1_EADC_SCTL(n) (0x80 + (n) * 0x04) +#define MA35D1_EADC_INTSRC0 0xd0 +#define MA35D1_EADC_STATUS2 0xf8 +#define MA35D1_EADC_SELSMP0 0x140 +#define MA35D1_EADC_REFADJCTL 0x150 + +#define MA35D1_EADC_CTL_ADCEN BIT(0) +#define MA35D1_EADC_CTL_ADCIEN0 BIT(2) +#define MA35D1_EADC_CTL_DIFFEN BIT(8) + +#define MA35D1_EADC_SCTL_CHSEL_MASK GENMASK(3, 0) +#define MA35D1_EADC_SCTL_TRGDLY_MASK GENMASK(15, 8) +#define MA35D1_EADC_SCTL_TRGSEL_MASK GENMASK(21, 16) +#define MA35D1_EADC_SCTL_TRGSEL_ADINT0 \ + FIELD_PREP(MA35D1_EADC_SCTL_TRGSEL_MASK, 2) + +#define MA35D1_EADC_DAT_MASK GENMASK(11, 0) +#define MA35D1_EADC_STATUS2_ADIF0 BIT(0) +#define MA35D1_EADC_INTSRC0_ADINT0 BIT(0) +#define MA35D1_EADC_REFADJCTL_EXT_VREF BIT(0) + +#define MA35D1_EADC_MAX_CHANNELS 9 +#define MA35D1_EADC_MAX_SAMPLE_MODULES 16 +#define MA35D1_EADC_CHAN_NAME_LEN 16 +#define MA35D1_EADC_TIMEOUT msecs_to_jiffies(1000) + +struct ma35d1_adc { + struct device *dev; + void __iomem *regs; + struct clk *clk; + struct completion completion; + /* Protects direct conversions against concurrent register access. */ + struct mutex lock; + struct iio_trigger *trig; + unsigned int scan_chancnt; + bool scan_differential; + char chan_name[MA35D1_EADC_MAX_CHANNELS][MA35D1_EADC_CHAN_NAME_LEN]; + struct { + u16 channels[MA35D1_EADC_MAX_SAMPLE_MODULES]; + aligned_s64 timestamp; + } scan; +}; + +static inline u32 ma35d1_adc_read(struct ma35d1_adc *adc, u32 reg) +{ + return readl(adc->regs + reg); +} + +static inline void ma35d1_adc_write(struct ma35d1_adc *adc, u32 reg, u32 val) +{ + writel(val, adc->regs + reg); +} + +static void ma35d1_adc_rmw(struct ma35d1_adc *adc, u32 reg, u32 mask, u32 val) +{ + u32 tmp; + + tmp = ma35d1_adc_read(adc, reg); + tmp &= ~mask; + tmp |= val; + ma35d1_adc_write(adc, reg, tmp); +} + +static void ma35d1_adc_set_diff(struct ma35d1_adc *adc, bool differential) +{ + ma35d1_adc_rmw(adc, MA35D1_EADC_CTL, MA35D1_EADC_CTL_DIFFEN, + differential ? MA35D1_EADC_CTL_DIFFEN : 0); +} + +static void ma35d1_adc_config_sample(struct ma35d1_adc *adc, + unsigned int sample, unsigned int channel) +{ + u32 reg = MA35D1_EADC_SCTL(sample); + + ma35d1_adc_rmw(adc, reg, + MA35D1_EADC_SCTL_CHSEL_MASK | + MA35D1_EADC_SCTL_TRGSEL_MASK, + FIELD_PREP(MA35D1_EADC_SCTL_CHSEL_MASK, channel) | + MA35D1_EADC_SCTL_TRGSEL_ADINT0); +} + +static void ma35d1_adc_disable_irq(struct ma35d1_adc *adc) +{ + ma35d1_adc_rmw(adc, MA35D1_EADC_CTL, MA35D1_EADC_CTL_ADCIEN0, 0); +} + +static void ma35d1_adc_hw_init(struct ma35d1_adc *adc) +{ + ma35d1_adc_disable_irq(adc); + ma35d1_adc_rmw(adc, MA35D1_EADC_CTL, + MA35D1_EADC_CTL_ADCEN, MA35D1_EADC_CTL_ADCEN); + ma35d1_adc_write(adc, MA35D1_EADC_STATUS2, MA35D1_EADC_STATUS2_ADIF0); + ma35d1_adc_rmw(adc, MA35D1_EADC_INTSRC0, + MA35D1_EADC_INTSRC0_ADINT0, + MA35D1_EADC_INTSRC0_ADINT0); + ma35d1_adc_rmw(adc, MA35D1_EADC_REFADJCTL, + MA35D1_EADC_REFADJCTL_EXT_VREF, + MA35D1_EADC_REFADJCTL_EXT_VREF); + ma35d1_adc_rmw(adc, MA35D1_EADC_SELSMP0, GENMASK(1, 0), 3); +} + +static void ma35d1_adc_hw_disable(void *data) +{ + struct ma35d1_adc *adc = data; + + ma35d1_adc_disable_irq(adc); + ma35d1_adc_rmw(adc, MA35D1_EADC_CTL, MA35D1_EADC_CTL_ADCEN, 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); + u32 status; + + status = ma35d1_adc_read(adc, MA35D1_EADC_STATUS2); + if (!(status & MA35D1_EADC_STATUS2_ADIF0)) + return IRQ_NONE; + + ma35d1_adc_write(adc, MA35D1_EADC_STATUS2, MA35D1_EADC_STATUS2_ADIF0); + + if (iio_buffer_enabled(indio_dev)) { + ma35d1_adc_disable_irq(adc); + iio_trigger_poll(adc->trig); + } else { + complete(&adc->completion); + } + + return IRQ_HANDLED; +} + +static irqreturn_t ma35d1_adc_trigger_handler(int irq, void *p) +{ + struct iio_poll_func *pf = p; + struct iio_dev *indio_dev = pf->indio_dev; + struct ma35d1_adc *adc = iio_priv(indio_dev); + int i; + + for (i = 0; i < adc->scan_chancnt; i++) + adc->scan.channels[i] = + ma35d1_adc_read(adc, MA35D1_EADC_DAT(i)) & + MA35D1_EADC_DAT_MASK; + + iio_push_to_buffers_with_timestamp(indio_dev, &adc->scan, pf->timestamp); + iio_trigger_notify_done(adc->trig); + + ma35d1_adc_rmw(adc, MA35D1_EADC_CTL, MA35D1_EADC_CTL_ADCIEN0, + MA35D1_EADC_CTL_ADCIEN0); + ma35d1_adc_write(adc, MA35D1_EADC_SWTRG, 1); + + return IRQ_HANDLED; +} + +static int ma35d1_adc_read_conversion(struct iio_dev *indio_dev, + const struct iio_chan_spec *chan, + int *val) +{ + struct ma35d1_adc *adc = iio_priv(indio_dev); + long timeout; + + reinit_completion(&adc->completion); + + ma35d1_adc_write(adc, MA35D1_EADC_STATUS2, MA35D1_EADC_STATUS2_ADIF0); + ma35d1_adc_rmw(adc, MA35D1_EADC_SCTL(0), + MA35D1_EADC_SCTL_CHSEL_MASK | + MA35D1_EADC_SCTL_TRGSEL_MASK, + FIELD_PREP(MA35D1_EADC_SCTL_CHSEL_MASK, + chan->channel)); + ma35d1_adc_set_diff(adc, chan->differential); + ma35d1_adc_rmw(adc, MA35D1_EADC_CTL, MA35D1_EADC_CTL_ADCIEN0, + MA35D1_EADC_CTL_ADCIEN0); + ma35d1_adc_write(adc, MA35D1_EADC_SWTRG, 1); + + timeout = wait_for_completion_interruptible_timeout(&adc->completion, + MA35D1_EADC_TIMEOUT); + ma35d1_adc_disable_irq(adc); + + if (timeout < 0) + return timeout; + if (!timeout) + return -ETIMEDOUT; + + *val = ma35d1_adc_read(adc, MA35D1_EADC_DAT(0)) & MA35D1_EADC_DAT_MASK; + + return 0; +} + +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); + int ret; + + switch (mask) { + case IIO_CHAN_INFO_RAW: + if (!iio_device_claim_direct(indio_dev)) + return -EBUSY; + + mutex_lock(&adc->lock); + ret = ma35d1_adc_read_conversion(indio_dev, chan, val); + mutex_unlock(&adc->lock); + + iio_device_release_direct(indio_dev); + if (ret) + return ret; + + return IIO_VAL_INT; + default: + return -EINVAL; + } +} + +static int ma35d1_adc_validate_scan(struct iio_dev *indio_dev, + const unsigned long *scan_mask) +{ + const struct iio_chan_spec *chan; + bool have_single = false; + bool have_diff = false; + unsigned int count = 0; + unsigned long bit; + + for_each_set_bit(bit, scan_mask, indio_dev->masklength) { + chan = &indio_dev->channels[bit]; + + if (chan->type == IIO_TIMESTAMP) + continue; + count++; + if (chan->differential) + have_diff = true; + else + have_single = true; + } + + if (!count || count > MA35D1_EADC_MAX_SAMPLE_MODULES) + return -EINVAL; + + if (have_single && have_diff) + return -EINVAL; + + return 0; +} + +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 *chan; + unsigned int sample = 0; + unsigned long bit; + bool differential = false; + int ret; + + ret = ma35d1_adc_validate_scan(indio_dev, scan_mask); + if (ret) + return ret; + + for_each_set_bit(bit, scan_mask, indio_dev->masklength) { + chan = &indio_dev->channels[bit]; + if (chan->type == IIO_TIMESTAMP) + continue; + + if (!sample) + differential = chan->differential; + + ma35d1_adc_config_sample(adc, sample, chan->channel); + sample++; + } + + adc->scan_chancnt = sample; + adc->scan_differential = differential; + + return 0; +} + +static int ma35d1_adc_buffer_postenable(struct iio_dev *indio_dev) +{ + struct ma35d1_adc *adc = iio_priv(indio_dev); + int i; + + if (!adc->scan_chancnt) + return -EINVAL; + + ma35d1_adc_write(adc, MA35D1_EADC_STATUS2, MA35D1_EADC_STATUS2_ADIF0); + ma35d1_adc_rmw(adc, MA35D1_EADC_INTSRC0, + MA35D1_EADC_INTSRC0_ADINT0, + MA35D1_EADC_INTSRC0_ADINT0); + ma35d1_adc_rmw(adc, MA35D1_EADC_REFADJCTL, + MA35D1_EADC_REFADJCTL_EXT_VREF, + MA35D1_EADC_REFADJCTL_EXT_VREF); + ma35d1_adc_rmw(adc, MA35D1_EADC_SELSMP0, GENMASK(1, 0), 3); + ma35d1_adc_set_diff(adc, adc->scan_differential); + + for (i = 0; i < adc->scan_chancnt; i++) + ma35d1_adc_rmw(adc, MA35D1_EADC_SCTL(i), + MA35D1_EADC_SCTL_TRGDLY_MASK, + MA35D1_EADC_SCTL_TRGDLY_MASK); + + ma35d1_adc_rmw(adc, MA35D1_EADC_CTL, MA35D1_EADC_CTL_ADCIEN0, + MA35D1_EADC_CTL_ADCIEN0); + ma35d1_adc_write(adc, MA35D1_EADC_SWTRG, 1); + + return 0; +} + +static int ma35d1_adc_buffer_predisable(struct iio_dev *indio_dev) +{ + struct ma35d1_adc *adc = iio_priv(indio_dev); + int i; + + ma35d1_adc_disable_irq(adc); + for (i = 0; i < adc->scan_chancnt; i++) + ma35d1_adc_rmw(adc, MA35D1_EADC_SCTL(i), + MA35D1_EADC_SCTL_TRGSEL_MASK, 0); + + return 0; +} + +static const struct iio_buffer_setup_ops ma35d1_adc_buffer_ops = { + .postenable = ma35d1_adc_buffer_postenable, + .predisable = ma35d1_adc_buffer_predisable, +}; + +static const struct iio_info ma35d1_adc_info = { + .read_raw = ma35d1_adc_read_raw, + .update_scan_mode = ma35d1_adc_update_scan_mode, +}; + +static const struct iio_trigger_ops ma35d1_adc_trigger_ops = { + .validate_device = iio_trigger_validate_own_device, +}; + +static void ma35d1_adc_init_channel(struct ma35d1_adc *adc, + struct iio_chan_spec *chan, u32 vinp, + u32 vinn, int scan_index, bool differential) +{ + char *name = adc->chan_name[vinp]; + + chan->type = IIO_VOLTAGE; + chan->indexed = 1; + chan->channel = vinp; + chan->address = vinp; + chan->scan_index = scan_index; + chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW); + chan->scan_type.sign = 'u'; + chan->scan_type.realbits = 12; + chan->scan_type.storagebits = 16; + chan->scan_type.endianness = IIO_CPU; + + if (differential) { + chan->differential = 1; + chan->channel2 = vinn; + snprintf(name, MA35D1_EADC_CHAN_NAME_LEN, "in%d-in%d", vinp, + vinn); + } else { + snprintf(name, MA35D1_EADC_CHAN_NAME_LEN, "in%d", vinp); + } + + chan->datasheet_name = name; +} + +static int ma35d1_adc_parse_channels(struct iio_dev *indio_dev, + struct device *dev) +{ + struct ma35d1_adc *adc = iio_priv(indio_dev); + DECLARE_BITMAP(used_channels, MA35D1_EADC_MAX_CHANNELS); + struct fwnode_handle *child; + struct iio_chan_spec *channels; + int num_channels; + int scan_index = 0; + int ret; + + bitmap_zero(used_channels, MA35D1_EADC_MAX_CHANNELS); + + num_channels = device_get_child_node_count(dev); + if (!num_channels) + return dev_err_probe(dev, -ENODATA, + "no ADC channels configured\n"); + + if (num_channels > MA35D1_EADC_MAX_CHANNELS) + return dev_err_probe(dev, -EINVAL, "too many ADC channels\n"); + + channels = devm_kcalloc(dev, num_channels + 1, sizeof(*channels), + GFP_KERNEL); + if (!channels) + return -ENOMEM; + + device_for_each_child_node(dev, child) { + u32 diff[2]; + u32 reg; + bool differential = false; + + ret = fwnode_property_read_u32(child, "reg", ®); + if (ret) { + fwnode_handle_put(child); + return dev_err_probe(dev, ret, + "missing channel reg property\n"); + } + + if (reg >= MA35D1_EADC_MAX_CHANNELS) { + fwnode_handle_put(child); + return dev_err_probe(dev, -EINVAL, + "invalid ADC channel %u\n", reg); + } + + if (test_and_set_bit(reg, used_channels)) { + fwnode_handle_put(child); + return dev_err_probe(dev, -EINVAL, + "duplicate ADC channel %u\n", reg); + } + + if (fwnode_property_present(child, "diff-channels")) { + ret = fwnode_property_read_u32_array(child, + "diff-channels", + diff, + ARRAY_SIZE(diff)); + if (ret) { + fwnode_handle_put(child); + return dev_err_probe(dev, ret, + "invalid diff-channels for channel %u\n", + reg); + } + + if (diff[0] != reg || + diff[1] >= MA35D1_EADC_MAX_CHANNELS || + diff[0] == diff[1]) { + fwnode_handle_put(child); + return dev_err_probe(dev, -EINVAL, + "invalid differential ADC channel %u-%u\n", + diff[0], diff[1]); + } + + if (test_and_set_bit(diff[1], used_channels)) { + fwnode_handle_put(child); + return dev_err_probe(dev, -EINVAL, + "ADC channel %u already used\n", + diff[1]); + } + + differential = true; + } + + ma35d1_adc_init_channel(adc, &channels[scan_index], reg, + differential ? diff[1] : 0, + scan_index, differential); + scan_index++; + } + + channels[scan_index] = (struct iio_chan_spec) + IIO_CHAN_SOFT_TIMESTAMP(scan_index); + + indio_dev->channels = channels; + indio_dev->num_channels = scan_index + 1; + indio_dev->masklength = indio_dev->num_channels; + + return 0; +} + +static int ma35d1_adc_setup_trigger(struct iio_dev *indio_dev, + struct device *dev) +{ + struct ma35d1_adc *adc = iio_priv(indio_dev); + int ret; + + adc->trig = devm_iio_trigger_alloc(dev, "%s-trigger", dev_name(dev)); + if (!adc->trig) + return -ENOMEM; + + adc->trig->ops = &ma35d1_adc_trigger_ops; + iio_trigger_set_drvdata(adc->trig, indio_dev); + + ret = devm_iio_trigger_register(dev, adc->trig); + if (ret) + return dev_err_probe(dev, ret, "failed to register trigger\n"); + + ret = iio_trigger_set_immutable(indio_dev, adc->trig); + if (ret) + return dev_err_probe(dev, ret, "failed to set trigger\n"); + + return 0; +} + +static int ma35d1_adc_probe(struct platform_device *pdev) +{ + struct device *dev = &pdev->dev; + struct iio_dev *indio_dev; + struct ma35d1_adc *adc; + int irq; + int ret; + + indio_dev = devm_iio_device_alloc(dev, sizeof(*adc)); + if (!indio_dev) + return -ENOMEM; + adc = iio_priv(indio_dev); + adc->dev = dev; + mutex_init(&adc->lock); + init_completion(&adc->completion); + + adc->regs = devm_platform_ioremap_resource(pdev, 0); + if (IS_ERR(adc->regs)) + return dev_err_probe(dev, PTR_ERR(adc->regs), + "failed to map registers\n"); + + adc->clk = devm_clk_get_enabled(dev, NULL); + if (IS_ERR(adc->clk)) + return dev_err_probe(dev, PTR_ERR(adc->clk), + "failed to get and enable ADC clock\n"); + + indio_dev->name = "ma35d1-eadc"; + indio_dev->modes = INDIO_DIRECT_MODE | INDIO_BUFFER_TRIGGERED; + indio_dev->info = &ma35d1_adc_info; + + ret = ma35d1_adc_parse_channels(indio_dev, dev); + if (ret) + return ret; + + ma35d1_adc_hw_init(adc); + + ret = devm_add_action_or_reset(dev, ma35d1_adc_hw_disable, adc); + if (ret) + return ret; + + ret = ma35d1_adc_setup_trigger(indio_dev, dev); + if (ret) + return ret; + + irq = platform_get_irq(pdev, 0); + if (irq < 0) + return irq; + + ret = devm_request_irq(dev, irq, ma35d1_adc_isr, 0, dev_name(dev), + indio_dev); + if (ret) + return dev_err_probe(dev, ret, "failed to request IRQ %d\n", irq); + + ret = devm_iio_triggered_buffer_setup(dev, indio_dev, + iio_pollfunc_store_time, + ma35d1_adc_trigger_handler, + &ma35d1_adc_buffer_ops); + if (ret) + return dev_err_probe(dev, ret, + "failed to setup triggered buffer\n"); + + platform_set_drvdata(pdev, indio_dev); + + ret = devm_iio_device_register(dev, indio_dev); + if (ret) + return dev_err_probe(dev, ret, "failed to register IIO device\n"); + + 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); + + if (iio_buffer_enabled(indio_dev)) + return -EBUSY; + + ma35d1_adc_hw_disable(adc); + clk_disable_unprepare(adc->clk); + + return 0; +} + +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; + + ret = clk_prepare_enable(adc->clk); + if (ret) + return ret; + + ma35d1_adc_hw_init(adc); + + return 0; +} + +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] 7+ messages in thread
* Re: [PATCH 2/2] iio: adc: Add Nuvoton MA35D1 EADC driver 2026-06-25 11:06 ` [PATCH 2/2] iio: adc: Add Nuvoton MA35D1 EADC driver Chi-Wen Weng @ 2026-06-25 11:20 ` sashiko-bot 2026-06-26 12:54 ` Andy Shevchenko 1 sibling, 0 replies; 7+ messages in thread From: sashiko-bot @ 2026-06-25 11:20 UTC (permalink / raw) To: Chi-Wen Weng; +Cc: devicetree, robh, conor+dt Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider: - [High] Multi-channel buffered scans read stale or actively converting data for subsequent channels due to premature interrupt mapping. - [High] Unpaced software re-triggering loop in the buffer interrupt handler causes an IRQ storm and livelock. - [High] Missing sign-extension and incorrect scan type for negative differential readings. - [High] Device suspend does not synchronize with ongoing direct conversions, leading to potential hardware aborts. - [Medium] Differential negative channel configuration from device tree is accepted but completely ignored by hardware configuration. -- commit 71778e9c3ef41a6d7da62ab5f861733a638e3f61 Author: Chi-Wen Weng <cwweng@nuvoton.com> iio: adc: Add Nuvoton MA35D1 EADC driver This patch adds an IIO driver for the Nuvoton MA35D1 Enhanced ADC controller, supporting both direct reads and triggered buffered capture. > diff --git a/drivers/iio/adc/ma35d1_eadc.c b/drivers/iio/adc/ma35d1_eadc.c > new file mode 100644 > --- /dev/null > +++ b/drivers/iio/adc/ma35d1_eadc.c [ ... ] > +static void ma35d1_adc_config_sample(struct ma35d1_adc *adc, > + unsigned int sample, unsigned int channel) > +{ > + u32 reg = MA35D1_EADC_SCTL(sample); > + > + ma35d1_adc_rmw(adc, reg, > + MA35D1_EADC_SCTL_CHSEL_MASK | > + MA35D1_EADC_SCTL_TRGSEL_MASK, > + FIELD_PREP(MA35D1_EADC_SCTL_CHSEL_MASK, channel) | > + MA35D1_EADC_SCTL_TRGSEL_ADINT0); > +} [Severity: Medium] Will the hardware properly sample differential pairs if only the positive channel is configured here? The driver parses differential configurations using diff-channels and stores the negative channel in chan->channel2, but only the positive channel is ever written to the hardware CHSEL mask here. If the hardware relies on arbitrary negative channel configurations rather than fixed hardware pairings, the negative pin won't be sampled correctly. Additionally, ma35d1_adc_parse_channels() doesn't seem to validate against any fixed hardware pair requirements. [ ... ] > +static irqreturn_t ma35d1_adc_trigger_handler(int irq, void *p) > +{ > + struct iio_poll_func *pf = p; > + struct iio_dev *indio_dev = pf->indio_dev; > + struct ma35d1_adc *adc = iio_priv(indio_dev); > + int i; > + > + for (i = 0; i < adc->scan_chancnt; i++) > + adc->scan.channels[i] = > + ma35d1_adc_read(adc, MA35D1_EADC_DAT(i)) & > + MA35D1_EADC_DAT_MASK; [Severity: High] Will this loop read stale or actively converting data for channels 1 through N during multi-channel scans? In ma35d1_adc_buffer_postenable(), the interrupt source is hardcoded to sample module 0: ma35d1_adc_rmw(adc, MA35D1_EADC_INTSRC0, MA35D1_EADC_INTSRC0_ADINT0, MA35D1_EADC_INTSRC0_ADINT0); This means the interrupt fires as soon as the first channel finishes converting. When this threaded handler wakes up and immediately reads all DAT(i) registers, the subsequent sample modules may not have completed their conversions yet. > + > + iio_push_to_buffers_with_timestamp(indio_dev, &adc->scan, pf->timestamp); > + iio_trigger_notify_done(adc->trig); > + > + ma35d1_adc_rmw(adc, MA35D1_EADC_CTL, MA35D1_EADC_CTL_ADCIEN0, > + MA35D1_EADC_CTL_ADCIEN0); > + ma35d1_adc_write(adc, MA35D1_EADC_SWTRG, 1); [Severity: High] Does unconditionally triggering the next conversion here cause an interrupt storm? Since the driver starts the conversion in postenable by writing SWTRG = 1, and then unconditionally restarts it immediately at the end of the handler, this creates a 0-delay free-running loop without any hardware timer or pacing mechanism. This can livelock the system by flooding the CPU with interrupts as fast as the ADC converts. > + > + return IRQ_HANDLED; > +} > + > +static int ma35d1_adc_read_conversion(struct iio_dev *indio_dev, > + const struct iio_chan_spec *chan, > + int *val) > +{ [ ... ] > + timeout = wait_for_completion_interruptible_timeout(&adc->completion, > + MA35D1_EADC_TIMEOUT); > + ma35d1_adc_disable_irq(adc); > + > + if (timeout < 0) > + return timeout; > + if (!timeout) > + return -ETIMEDOUT; > + > + *val = ma35d1_adc_read(adc, MA35D1_EADC_DAT(0)) & MA35D1_EADC_DAT_MASK; [Severity: High] Will this return massive positive values for negative differential readings? Differential channels return two's complement values. Without using sign_extend32() before assigning to the 32-bit *val, negative voltage differences will be incorrectly returned to userspace as large positive integers instead of signed values. [ ... ] > +static void ma35d1_adc_init_channel(struct ma35d1_adc *adc, > + struct iio_chan_spec *chan, u32 vinp, > + u32 vinn, int scan_index, bool differential) > +{ [ ... ] > + chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW); > + chan->scan_type.sign = 'u'; [Severity: High] Should differential channels be marked as signed instead of unsigned? When differential is true, this forces the scan type sign to 'u', which incorrectly informs the IIO subsystem that the differential data is unsigned instead of two's complement. [ ... ] > +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); > + > + if (iio_buffer_enabled(indio_dev)) > + return -EBUSY; > + > + ma35d1_adc_hw_disable(adc); > + clk_disable_unprepare(adc->clk); [Severity: High] Can this race with active direct conversions and cause a bus fault? ma35d1_adc_suspend() disables the peripheral clock without acquiring adc->lock or checking iio_device_claim_direct_mode(). If a concurrent direct read is waiting in ma35d1_adc_read_conversion(): timeout = wait_for_completion_interruptible_timeout(&adc->completion, MA35D1_EADC_TIMEOUT); ma35d1_adc_disable_irq(adc); and suspend disables the clock, the wait may time out or be interrupted. Accessing the ADC registers immediately afterward in ma35d1_adc_disable_irq() with the clock disabled could result in a synchronous external abort. > + > + return 0; > +} -- Sashiko AI review · https://sashiko.dev/#/patchset/20260625110638.38438-1-cwweng.linux@gmail.com?part=2 ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH 2/2] iio: adc: Add Nuvoton MA35D1 EADC driver 2026-06-25 11:06 ` [PATCH 2/2] iio: adc: Add Nuvoton MA35D1 EADC driver Chi-Wen Weng 2026-06-25 11:20 ` sashiko-bot @ 2026-06-26 12:54 ` Andy Shevchenko 1 sibling, 0 replies; 7+ messages in thread From: Andy Shevchenko @ 2026-06-26 12:54 UTC (permalink / raw) To: Chi-Wen Weng Cc: jic23, robh, krzk+dt, conor+dt, dlechner, nuno.sa, andy, linux-arm-kernel, linux-iio, devicetree, linux-kernel, cwweng On Thu, Jun 25, 2026 at 07:06:38PM +0800, Chi-Wen Weng wrote: > Add an IIO driver for the Nuvoton MA35D1 Enhanced ADC controller. > > The driver supports direct raw reads and triggered buffered capture. The > controller end-of-conversion interrupt is exposed as the device trigger > and is used to push samples into the IIO buffer. > > Channels are described by firmware child nodes and can be configured as > single-ended or differential inputs. Since the differential enable bit is > global, mixed single-ended and differential buffered scans are rejected. > > DMA support is intentionally not included in this initial upstream driver; > conversions are handled through the interrupt-driven path. Nice written driver, some small issues here and there, and I think in a couple of versions it will stabilize and can be accepted. ... > +#include <linux/bitfield.h> > +#include <linux/bits.h> No need, bitmap.h covers this. > +#include <linux/bitmap.h> > +#include <linux/clk.h> > +#include <linux/completion.h> > +#include <linux/device.h> No need, covered by platform_device.h. > +#include <linux/err.h> > +#include <linux/interrupt.h> > +#include <linux/io.h> > +#include <linux/kernel.h> No way this header should be in the mere drivers. > +#include <linux/module.h> > +#include <linux/mod_devicetable.h> > +#include <linux/mutex.h> > +#include <linux/platform_device.h> > +#include <linux/pm.h> > +#include <linux/property.h> Also missing some headers, such as types.h. ... > +#define MA35D1_EADC_TIMEOUT msecs_to_jiffies(1000) + jiffies.h ... > +static inline u32 ma35d1_adc_read(struct ma35d1_adc *adc, u32 reg) > +{ > + return readl(adc->regs + reg); > +} > + > +static inline void ma35d1_adc_write(struct ma35d1_adc *adc, u32 reg, u32 val) > +{ > + writel(val, adc->regs + reg); > +} > + > +static void ma35d1_adc_rmw(struct ma35d1_adc *adc, u32 reg, u32 mask, u32 val) Name it _update() to be aligned with the _read() and _write() above. > +{ > + u32 tmp; > + > + tmp = ma35d1_adc_read(adc, reg); > + tmp &= ~mask; > + tmp |= val; Correct pattern is to use tmp = (tmp & ~mask) | (val & mask); > + ma35d1_adc_write(adc, reg, tmp); > +} ... > +static void ma35d1_adc_config_sample(struct ma35d1_adc *adc, > + unsigned int sample, unsigned int channel) > +{ > + u32 reg = MA35D1_EADC_SCTL(sample); I don't see the need of this variable, use the value directly. > + ma35d1_adc_rmw(adc, reg, > + MA35D1_EADC_SCTL_CHSEL_MASK | > + MA35D1_EADC_SCTL_TRGSEL_MASK, > + FIELD_PREP(MA35D1_EADC_SCTL_CHSEL_MASK, channel) | > + MA35D1_EADC_SCTL_TRGSEL_ADINT0); > +} ... > +static irqreturn_t ma35d1_adc_trigger_handler(int irq, void *p) > +{ > + struct iio_poll_func *pf = p; > + struct iio_dev *indio_dev = pf->indio_dev; > + struct ma35d1_adc *adc = iio_priv(indio_dev); > + int i; > + > + for (i = 0; i < adc->scan_chancnt; i++) for (unsigned int i = 0; i < adc->scan_chancnt; i++) > + adc->scan.channels[i] = > + ma35d1_adc_read(adc, MA35D1_EADC_DAT(i)) & > + MA35D1_EADC_DAT_MASK; > + > + iio_push_to_buffers_with_timestamp(indio_dev, &adc->scan, pf->timestamp); > + iio_trigger_notify_done(adc->trig); > + > + ma35d1_adc_rmw(adc, MA35D1_EADC_CTL, MA35D1_EADC_CTL_ADCIEN0, > + MA35D1_EADC_CTL_ADCIEN0); > + ma35d1_adc_write(adc, MA35D1_EADC_SWTRG, 1); > + > + return IRQ_HANDLED; > +} ... > +static int ma35d1_adc_validate_scan(struct iio_dev *indio_dev, > + const unsigned long *scan_mask) > +{ > + const struct iio_chan_spec *chan; > + bool have_single = false; > + bool have_diff = false; > + unsigned int count = 0; > + unsigned long bit; > + > + for_each_set_bit(bit, scan_mask, indio_dev->masklength) { > + chan = &indio_dev->channels[bit]; > + > + if (chan->type == IIO_TIMESTAMP) > + continue; > + count++; Make it last in the loop, it will be standard pattern. Otherwise it's hard to read and find. Also it's recommended to split assignment and definition for better maintenance. unsigned int count; ... count = 0; for_each_set_bit(bit, scan_mask, indio_dev->masklength) { ... count++; } > + if (chan->differential) > + have_diff = true; > + else > + have_single = true; > + } > + > + if (!count || count > MA35D1_EADC_MAX_SAMPLE_MODULES) > + return -EINVAL; > + if (have_single && have_diff) > + return -EINVAL; Is it possible IRL? > + return 0; > +} ... > +static int ma35d1_adc_buffer_postenable(struct iio_dev *indio_dev) > +{ > + struct ma35d1_adc *adc = iio_priv(indio_dev); > + int i; > + > + if (!adc->scan_chancnt) > + return -EINVAL; > + > + ma35d1_adc_write(adc, MA35D1_EADC_STATUS2, MA35D1_EADC_STATUS2_ADIF0); > + ma35d1_adc_rmw(adc, MA35D1_EADC_INTSRC0, > + MA35D1_EADC_INTSRC0_ADINT0, > + MA35D1_EADC_INTSRC0_ADINT0); > + ma35d1_adc_rmw(adc, MA35D1_EADC_REFADJCTL, > + MA35D1_EADC_REFADJCTL_EXT_VREF, > + MA35D1_EADC_REFADJCTL_EXT_VREF); > + ma35d1_adc_rmw(adc, MA35D1_EADC_SELSMP0, GENMASK(1, 0), 3); > + ma35d1_adc_set_diff(adc, adc->scan_differential); > + for (i = 0; i < adc->scan_chancnt; i++) for (unsigned int i = 0; i < adc->scan_chancnt; i++) > + ma35d1_adc_rmw(adc, MA35D1_EADC_SCTL(i), > + MA35D1_EADC_SCTL_TRGDLY_MASK, > + MA35D1_EADC_SCTL_TRGDLY_MASK); > + > + ma35d1_adc_rmw(adc, MA35D1_EADC_CTL, MA35D1_EADC_CTL_ADCIEN0, > + MA35D1_EADC_CTL_ADCIEN0); > + ma35d1_adc_write(adc, MA35D1_EADC_SWTRG, 1); > + > + return 0; > +} > + > +static int ma35d1_adc_buffer_predisable(struct iio_dev *indio_dev) > +{ > + struct ma35d1_adc *adc = iio_priv(indio_dev); > + int i; > + > + ma35d1_adc_disable_irq(adc); > + for (i = 0; i < adc->scan_chancnt; i++) > + ma35d1_adc_rmw(adc, MA35D1_EADC_SCTL(i), > + MA35D1_EADC_SCTL_TRGSEL_MASK, 0); Ditto. Also looking to the cases of setting 0s, I would rather have a helper _set_bits() / _clear_bits() in conjunction with _update(). > + return 0; > +} ... > +static void ma35d1_adc_init_channel(struct ma35d1_adc *adc, > + struct iio_chan_spec *chan, u32 vinp, > + u32 vinn, int scan_index, bool differential) > +{ > + char *name = adc->chan_name[vinp]; > + > + chan->type = IIO_VOLTAGE; > + chan->indexed = 1; > + chan->channel = vinp; > + chan->address = vinp; > + chan->scan_index = scan_index; > + chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW); > + chan->scan_type.sign = 'u'; > + chan->scan_type.realbits = 12; > + chan->scan_type.storagebits = 16; > + chan->scan_type.endianness = IIO_CPU; > + > + if (differential) { > + chan->differential = 1; > + chan->channel2 = vinn; > + snprintf(name, MA35D1_EADC_CHAN_NAME_LEN, "in%d-in%d", vinp, > + vinn); Can compiler prove the buffer size is enough? > + } else { > + snprintf(name, MA35D1_EADC_CHAN_NAME_LEN, "in%d", vinp); > + } > + > + chan->datasheet_name = name; Why not use devm_kasprintf() instead? > +} ... > +static int ma35d1_adc_parse_channels(struct iio_dev *indio_dev, > + struct device *dev) > +{ > + struct ma35d1_adc *adc = iio_priv(indio_dev); > + DECLARE_BITMAP(used_channels, MA35D1_EADC_MAX_CHANNELS); > + struct fwnode_handle *child; > + struct iio_chan_spec *channels; > + int num_channels; > + int scan_index = 0; > + int ret; > + > + bitmap_zero(used_channels, MA35D1_EADC_MAX_CHANNELS); > + > + num_channels = device_get_child_node_count(dev); > + if (!num_channels) > + return dev_err_probe(dev, -ENODATA, > + "no ADC channels configured\n"); > + > + if (num_channels > MA35D1_EADC_MAX_CHANNELS) Perhaps >= ? > + return dev_err_probe(dev, -EINVAL, "too many ADC channels\n"); > + > + channels = devm_kcalloc(dev, num_channels + 1, sizeof(*channels), > + GFP_KERNEL); > + if (!channels) > + return -ENOMEM; > + > + device_for_each_child_node(dev, child) { Use _scoped() variant. > + u32 diff[2]; > + u32 reg; > + bool differential = false; > + > + ret = fwnode_property_read_u32(child, "reg", ®); > + if (ret) { > + fwnode_handle_put(child); > + return dev_err_probe(dev, ret, > + "missing channel reg property\n"); > + } > + > + if (reg >= MA35D1_EADC_MAX_CHANNELS) { > + fwnode_handle_put(child); > + return dev_err_probe(dev, -EINVAL, > + "invalid ADC channel %u\n", reg); > + } > + > + if (test_and_set_bit(reg, used_channels)) { > + fwnode_handle_put(child); > + return dev_err_probe(dev, -EINVAL, > + "duplicate ADC channel %u\n", reg); > + } > + > + if (fwnode_property_present(child, "diff-channels")) { > + ret = fwnode_property_read_u32_array(child, > + "diff-channels", > + diff, > + ARRAY_SIZE(diff)); > + if (ret) { > + fwnode_handle_put(child); > + return dev_err_probe(dev, ret, > + "invalid diff-channels for channel %u\n", > + reg); > + } > + > + if (diff[0] != reg || > + diff[1] >= MA35D1_EADC_MAX_CHANNELS || > + diff[0] == diff[1]) { > + fwnode_handle_put(child); > + return dev_err_probe(dev, -EINVAL, > + "invalid differential ADC channel %u-%u\n", > + diff[0], diff[1]); > + } > + > + if (test_and_set_bit(diff[1], used_channels)) { > + fwnode_handle_put(child); > + return dev_err_probe(dev, -EINVAL, > + "ADC channel %u already used\n", > + diff[1]); > + } > + > + differential = true; > + } > + > + ma35d1_adc_init_channel(adc, &channels[scan_index], reg, > + differential ? diff[1] : 0, > + scan_index, differential); > + scan_index++; > + } > + > + channels[scan_index] = (struct iio_chan_spec) > + IIO_CHAN_SOFT_TIMESTAMP(scan_index); > + > + indio_dev->channels = channels; > + indio_dev->num_channels = scan_index + 1; > + indio_dev->masklength = indio_dev->num_channels; > + > + return 0; > +} ... > + ret = devm_request_irq(dev, irq, ma35d1_adc_isr, 0, dev_name(dev), > + indio_dev); Make it a single line, here it's fine. > + if (ret) > + return dev_err_probe(dev, ret, "failed to request IRQ %d\n", irq); Remove duplicate error message. ... > + ret = devm_iio_triggered_buffer_setup(dev, indio_dev, > + iio_pollfunc_store_time, > + ma35d1_adc_trigger_handler, > + &ma35d1_adc_buffer_ops); > + if (ret) > + return dev_err_probe(dev, ret, > + "failed to setup triggered buffer\n"); So, it seems this is very rarely can be not -ENOMEM, and hence it's 99.99% dead code, just return ret; -- With Best Regards, Andy Shevchenko ^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2026-06-26 12:54 UTC | newest] Thread overview: 7+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2026-06-25 11:06 [PATCH 0/2] iio: adc: Add Nuvoton MA35D1 EADC support Chi-Wen Weng 2026-06-25 11:06 ` [PATCH 1/2] dt-bindings: iio: adc: Add Nuvoton MA35D1 EADC Chi-Wen Weng 2026-06-25 11:18 ` sashiko-bot 2026-06-25 16:24 ` Conor Dooley 2026-06-25 11:06 ` [PATCH 2/2] iio: adc: Add Nuvoton MA35D1 EADC driver Chi-Wen Weng 2026-06-25 11:20 ` sashiko-bot 2026-06-26 12:54 ` Andy Shevchenko
This is an external index of several public inboxes, see mirroring instructions on how to clone and mirror all data and code used by this external index.