Skip to contentSkip to conversation

NXP SAR ADC IIO driver for s32g2/3 platforms

PATCH Superseded Daniel Lezcano 3 participants 6 messages Nov 18, 2025 – Nov 19, 2025

Subsystems (2)

v6 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

v6 cover NXP SAR ADC IIO driver for s32g2/3 platforms

The S32G2 and S32G3 platforms have a couple of successive approximation register (SAR) ADCs with eight channels and 12-bit resolution. These changes provide the driver support for these ADCs and the bindings describing them.

The driver is derived from the BSP driver version. It has been partly rewritten to conform to upstream criteria.

https://github.com/nxp-auto-linux/linux/blob/release/bsp44.0-6.6.85-rt/drivers/iio/adc/s32cc_adc.c

After the V1 posting there were some discussions around the DMA code to be converted to use the IIO DMA API [1]. Unfortunately this one is not yet fully implemented and merged in the framework to support the cyclic DMA. The current DMA code in the driver has been used in production since several years and even if I agree it can be improved with a dedicated IIO DMA API in the future, IMO, it sounds reasonable to keep it as is until the IIO DMA API supporting the cyclic DMA is merged. I'll be glad to convert the driver code if such an API exists and allows to remove code inside the driver.

[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/

Changelog:
* V6:
** Vinod Koul **
- Dynamically allocate/release the channel at enable/disable

** Jonathan Cameron **
- Reached out Vinod to clarify the buffer life cycle
- Inverted more intuitive variable initialization
- Updated comment with "iio_push_to_buffers_with_ts"

** Andy Shevchenko **
- Removed unused NXP_SAR_ADC_IIO_BUFF_SZ macro
- Removed "<litteral>U" annotation
- Checked the buffer is a byte buffer
- Investigated callback routine vs residue and updated the changelog the conclusions [2]

* V5:
- Rebased against v6.18-rc1

** Jonathan Cameron **
- Replace DRIVER_NAME macro with its literal string
- Used FIELD_MODIFY() wherever it is possible
- Complied with the 80 chars convention
- Combined two variables in a single line declaration
- Removed the 'remove' function as it is useless
- Changed s32g2_sar_adc_data structure indentation / format

* V4:
** Christophe Jaillet **
- Used dmam_alloc_coherent() instead of dma_alloc_coherent()

* V3:
** Jonathan Cameron **
- Removed specific IIO_SYSFS_TRIGGER dependency in Kconfig
- Fixed headers
- Avoided macro generic names
- Used IIO_DECLARE_BUFFER_WITH_TS
- Documented buffer and buffer_chan
- Fixed single line comment
- Commented why channel 32 is the timestamp
- Renamed __<prefixed> functions
- Factored out the raw read function to prevent nested goto in the switch
- Returned -EINVAL instead of break
- Removed explict pointer cast
- Used iio_push_to_buffers_with_ts variant
- Fixed ordering operations in postenable / predisable
- Return IRQ_HANDLED even if there is an error in the isr
- Fixed devm_add_action_or_reset() to return directly
- Used sizeof(*var) instead of sizeof(struct myvar)
- Used model name instead of dev_name()
- Used dev_err_probe() in any case in the probe function
- Fixed indentation

** David Lechner **
- Kept alphabetical order in Makefile
- Changed explicit GPL-2.0-only
- Removed clock name in when calling devm_clk_get_enabled()

** Andriy Shevchenko **
- Fixed headers ordering and added the missing ones
- Fixed constant numeric format
- Ran pahole and consolidated the nxp_sar_adc structure
- Fixed semi-column in comments and typos
- Fixed indentation
- Moved data assignment before iio_dev allocation

* V2:
- Massaged the cover letter changelog to explain the DMA ** Andriy Shevchenko **
- Added missing headers and use proper header for of.h
- Changed macro offset zero to be consistent
- Remove macros REG_ADC_MCR_NRSMPL_* as they are unused
- Changed delays macro under the form 100000 => 100 * USEC_PER_MSEC
- Replaced PAGE_SIZE by a NXP_PAGE_SIZE = SZ_4K macro
- Replaced read_poll_timeout() by readl_poll_timeout()
- Changed error pattern "error first"
- Replaced variable type 'int' to 'unsigned int'
- Fixed bug right instead of left shift, use BIT(channel)
- Returned directly from switch-case
- Used guard(spinlock_irqsave)()
- One liner function call
- Remove redundant {}
- Write default values litterals instead of temporary variables
- Changed variable name vref -> vref_mV
- Removed unneeded error message
- Used dev_err_probe() consistently
- Removed successful driver probe message
- Removed redundant blank line

** Nuno Sa **
- Replaced of_device_get_match_data() by device_get_match_data()
- Removed iio_device_unregister() because devm_iio_device_register() is used
- Removed "/* sentinel */" comment
- Removed CONFIG_PM_SLEEP defiries

** Krzysztof Kozlowski / David Lechner **
- Removed clock-names in DT bindings
- Fixed minItems by maxItems

* V1:
- Initial post

Daniel Lezcano (2):
dt-bindings: iio: adc: Add the NXP SAR ADC for s32g2/3 platforms
iio: adc: Add the NXP SAR ADC support for the s32g2/3 platforms

 .../bindings/iio/adc/nxp,s32g2-sar-adc.yaml   |   63 +
 drivers/iio/adc/Kconfig                       |   12 +
 drivers/iio/adc/Makefile                      |    1 +
 drivers/iio/adc/nxp-sar-adc.c                 | 1016 +++++++++++++++++
 4 files changed, 1092 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/adc/nxp,s32g2-sar-adc.yaml
 create mode 100644 drivers/iio/adc/nxp-sar-adc.c
Signature · 1 line
2.43.0
#1 lore raw

PATCH v6 1/2 dt-bindings: iio: adc: Add the NXP SAR ADC for s32g2/3 platforms

The s32g2 and s32g3 NXP platforms have two instances of a Successive Approximation Register ADC. It supports the raw, trigger and scan modes which involves the DMA. Add their descriptions.

---
 .../bindings/iio/adc/nxp,s32g2-sar-adc.yaml   | 63 +++++++++++++++++++
 1 file changed, 63 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/adc/nxp,s32g2-sar-adc.yaml
Documentation/devicetree/bindings/iio/adc/nxp,s32g2-sar-adc.yaml new file +63 -0
new file mode 100644
index 000000000000..ec258f224df8
@@ -0,0 +1,63 @@
+# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/iio/adc/nxp,s32g2-sar-adc.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: NXP Successive Approximation ADC
+
+description:
+  The NXP SAR ADC provides fast and accurate analog-to-digital
+  conversion using the Successive Approximation Register (SAR) method.
+  It has 12-bit resolution with 8 input channels. Conversions can be
+  launched in software or using hardware triggers. It supports
+  continuous and one-shot modes with separate registers.
+
+maintainers:
+  - Daniel Lezcano <[email protected]>
+
+properties:
+  compatible:
+    oneOf:
+      - const: nxp,s32g2-sar-adc
+      - items:
+          - const: nxp,s32g3-sar-adc
+          - const: nxp,s32g2-sar-adc
+
+  reg:
+    maxItems: 1
+
+  interrupts:
+    maxItems: 1
+
+  clocks:
+    maxItems: 1
+
+  dmas:
+    maxItems: 1
+
+  dma-names:
+    const: rx
+
+required:
+  - compatible
+  - reg
+  - interrupts
+  - clocks
+  - dmas
+  - dma-names
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/interrupt-controller/arm-gic.h>
+
+    adc@401f8000 {
+        compatible = "nxp,s32g2-sar-adc";
+        reg = <0x401f8000 0x1000>;
+        interrupts = <GIC_SPI 70 IRQ_TYPE_LEVEL_HIGH>;
+        clocks = <&clks 0x41>;
+        dmas = <&edma0 0 32>;
+        dma-names = "rx";
+    };
Signature · 1 line
2.43.0
#2 lore raw

PATCH v6 2/2 iio: adc: Add the NXP SAR ADC support for the s32g2/3 platforms

In reply to Daniel Lezcano

From: Stefan-Gabriel Mirea <[email protected]>

The NXP S32G2 and S32G3 platforms integrate a successive approximation register (SAR) ADC. Two instances are available, each providing 8 multiplexed input channels with 12-bit resolution. The conversion rate is up to 1 Msps depending on the configuration and sampling window.

The SAR ADC supports raw, buffer, and trigger modes. It can operate in both single-shot and continuous conversion modes, with optional hardware triggering through the cross-trigger unit (CTU) or external events. An internal prescaler allows adjusting the sampling clock, while per-channel programmable sampling times provide fine-grained trade-offs between accuracy and latency. Automatic calibration is performed at probe time to minimize offset and gain errors.

The driver is derived from the BSP implementation and has been partly rewritten to comply with upstream requirements. For this reason, all contributors are listed as co-developers, while the author refers to the initial BSP driver file creator.

All modes have been validated on the S32G274-RDB2 platform using an externally generated square wave captured by the ADC. Tests covered buffered streaming via IIO, trigger synchronization, and accuracy verification against a precision laboratory signal source.

One potential scenario, not detected during testing, is that in some corner cases the DMA may already have been armed for the next transfer, which can lead dmaengine_tx_status() to return an incorrect residue. The callback_result() operation—intended to supply the residue directly and eliminate the need to call dmaengine_tx_status()—also does not work. Attempting to use dmaengine_pause() and dmaengine_resume() to prevent the residue from being updated does not work either.

This potential scenario should apply to any driver using cyclic DMA. However, no current driver actually handles this case, and they all rely on the same acquisition routine (e.g., the STM32 implementation). The NXP SAR acquisition routine has been used in production for several years, which is a good indication of its robustness.

As the IIO is implementing the cyclic DMA support API, it is not worth to do more spins to the current routine as it will go away when the new API will be available.

---
 drivers/iio/adc/Kconfig       |   12 +
 drivers/iio/adc/Makefile      |    1 +
 drivers/iio/adc/nxp-sar-adc.c | 1016 +++++++++++++++++++++++++++++++++
 3 files changed, 1029 insertions(+)
 create mode 100644 drivers/iio/adc/nxp-sar-adc.c
drivers/iio/adc/Kconfig +12 -0
index 58a14e6833f6..363eb8a3a6f2 100644
@@ -1212,6 +1212,18 @@ config NPCM_ADC
 	  This driver can also be built as a module. If so, the module
 	  will be called npcm_adc.
 
+config NXP_SAR_ADC
+	tristate "NXP S32G SAR-ADC driver"
+	depends on ARCH_S32 || COMPILE_TEST
+	select IIO_BUFFER
+	select IIO_TRIGGERED_BUFFER
+	help
+	  Say yes here to build support for S32G platforms
+	  analog-to-digital converter.
+
+	  This driver can also be built as a module. If so, the module will be
+	  called nxp_sar_adc.
+
 config PAC1921
 	tristate "Microchip Technology PAC1921 driver"
 	depends on I2C
drivers/iio/adc/Makefile +1 -0
index d008f78dc010..c9d5bc976fd3 100644
@@ -107,6 +107,7 @@ obj-$(CONFIG_MXS_LRADC_ADC) += mxs-lradc-adc.o
 obj-$(CONFIG_NAU7802) += nau7802.o
 obj-$(CONFIG_NCT7201) += nct7201.o
 obj-$(CONFIG_NPCM_ADC) += npcm_adc.o
+obj-$(CONFIG_NXP_SAR_ADC) += nxp-sar-adc.o
 obj-$(CONFIG_PAC1921) += pac1921.o
 obj-$(CONFIG_PAC1934) += pac1934.o
 obj-$(CONFIG_PALMAS_GPADC) += palmas_gpadc.o
drivers/iio/adc/nxp-sar-adc.c new file +1016 -0

1,016 lines not on this page · raw message

Signature · 1 line
2.43.0
#3 lore raw

Re: [PATCH v6 2/2] iio: adc: Add the NXP SAR ADC support for the s32g2/3 platforms

On Tue, Nov 18, 2025 at 10:34 PM Daniel Lezcano
<[email protected]> wrote:

Thanks for the update, my comments below (but we are almost done).

Quoted 13 lines

The NXP S32G2 and S32G3 platforms integrate a successive approximation register (SAR) ADC. Two instances are available, each providing 8 multiplexed input channels with 12-bit resolution. The conversion rate is up to 1 Msps depending on the configuration and sampling window.

The SAR ADC supports raw, buffer, and trigger modes. It can operate in both single-shot and continuous conversion modes, with optional hardware triggering through the cross-trigger unit (CTU) or external events. An internal prescaler allows adjusting the sampling clock, while per-channel programmable sampling times provide fine-grained trade-offs between accuracy and latency. Automatic calibration is performed at probe time to minimize offset and gain errors.

Quoted 4 lines

The driver is derived from the BSP implementation and has been partly rewritten to comply with upstream requirements. For this reason, all contributors are listed as co-developers, while the author refers to the initial BSP driver file creator.

I would move this paragraph closer to the tag block and FWIW we also have Originally-by: tag in case it suits better (usually when the original code is rewritten more than ~67%).

Quoted 23 lines

All modes have been validated on the S32G274-RDB2 platform using an externally generated square wave captured by the ADC. Tests covered buffered streaming via IIO, trigger synchronization, and accuracy verification against a precision laboratory signal source.

One potential scenario, not detected during testing, is that in some corner cases the DMA may already have been armed for the next transfer, which can lead dmaengine_tx_status() to return an incorrect residue. The callback_result() operation—intended to supply the residue directly and eliminate the need to call dmaengine_tx_status()—also does not work. Attempting to use dmaengine_pause() and dmaengine_resume() to prevent the residue from being updated does not work either.

This potential scenario should apply to any driver using cyclic DMA. However, no current driver actually handles this case, and they all rely on the same acquisition routine (e.g., the STM32 implementation). The NXP SAR acquisition routine has been used in production for several years, which is a good indication of its robustness.

As the IIO is implementing the cyclic DMA support API, it is not worth to do more spins to the current routine as it will go away when the new API will be available.

...

Quoted 1 line

+#define NXP_SAR_ADC_EOC_CH(c) BIT((c) % 32)

Do you expect "c" to be bigger than 31? In which circumstances?

...

drivers/iio/adc/nxp-sar-adc.c line 97

Quoted 2 lines
drivers/iio/adc/nxp-sar-adc.c new file +2 -0
+#define NXP_SAR_ADC_CTR_INPSAMP_MIN    0x08
+#define NXP_SAR_ADC_CTR_INPSAMP_MAX    0xFF

Keep the style consistent (I think you want small hexadecimal letters in the value).

...

drivers/iio/adc/nxp-sar-adc.c line 108

Quoted 3 lines
drivers/iio/adc/nxp-sar-adc.c new file +3 -0
+/* Other field define */
+#define NXP_SAR_ADC_CONV_TIMEOUT_MS    100
+#define NXP_SAR_ADC_CONV_TIMEOUT_JF    (msecs_to_jiffies(NXP_SAR_ADC_CONV_TIMEOUT_MS))

I don't see the use of the first definition. Dropping _JF (we usually assume the timeouts without units in "ticks") and using 100 directly as a parameter to the msecs_to_jiffies() will make it clear enough.

...

drivers/iio/adc/nxp-sar-adc.c line 209

Quoted 9 lines
drivers/iio/adc/nxp-sar-adc.c new file +8 -0
+static bool nxp_sar_adc_set_enabled(struct nxp_sar_adc *info, bool enable)
+{
+       u32 mcr;
+       bool pwdn;
+
+       mcr = readl(NXP_SAR_ADC_MCR(info->regs));
 
+       /* Return the current state. */
+       pwdn = FIELD_GET(NXP_SAR_ADC_MCR_PWDN, mcr);

The comment is a bit odd. I think you want to say "Save the current state and return it later" or something like this.

drivers/iio/adc/nxp-sar-adc.c line 221

Quoted 11 lines
drivers/iio/adc/nxp-sar-adc.c new file +11 -0
+       /* When the enabled flag is not set, we set the power down bit */
+       FIELD_MODIFY(NXP_SAR_ADC_MCR_PWDN, &mcr, !enable);
+
+       writel(mcr, NXP_SAR_ADC_MCR(info->regs));
+
+       /*
+        * Ensure there are at least three cycles between the
+        * configuration of NCMR and the setting of NSTART.
+        */
+       if (enable)
+               ndelay(div64_u64(NSEC_PER_SEC, clk_get_rate(info->clk) * 3));

I'm wondering how low the clock rate can be? With low enough clock rates this becomes a 100% CPU busyloop and in atomic context (is this the case?) without even the possibility to schedule.

Quoted 2 lines
(quoted diff) +2 -0
+       return pwdn;
+}

...

drivers/iio/adc/nxp-sar-adc.c line 274

Quoted 5 lines
drivers/iio/adc/nxp-sar-adc.c new file +5 -0
+static int nxp_sar_adc_calibration(struct nxp_sar_adc *info)
+{
+       int ret;
+
+       /* Calibration works only if the adc is powered up. */

ADC

drivers/iio/adc/nxp-sar-adc.c line 283

Quoted 9 lines
drivers/iio/adc/nxp-sar-adc.c new file +9 -0
+       nxp_sar_adc_enable(info);
+
+       /* The calibration operation starts. */
+       nxp_sar_adc_calibration_start(info->regs);
+
+       ret = nxp_sar_adc_calibration_wait(info->regs);
+
+       /*
+        * Calibration works only if the adc is powered up. However

ADC

Quoted 7 lines
drivers/iio/adc/nxp-sar-adc.c new file +7 -0
+        * the calibration is called from the probe function where the
+        * iio is not enabled, so we disable after the calibration.
+        */
+       nxp_sar_adc_disable(info);
+
+       return ret;
+}

...

drivers/iio/adc/nxp-sar-adc.c line 318

Quoted 9 lines
drivers/iio/adc/nxp-sar-adc.c new file +8 -0
+static int nxp_sar_adc_read_data(struct nxp_sar_adc *info, unsigned int chan)
+{
+       u32 ceocfr, cdr;
+
+       ceocfr = readl(NXP_SAR_ADC_CEOCFR0(info->regs));
 
+       /* FIELD_GET() can not be used here because EOC_CH is not constant */
+       if (!(NXP_SAR_ADC_EOC_CH(chan) & ceocfr))
+               return -EIO;

[nxp_sar_adc_]field_get() may be defined and used. There is a series pending to bring field_get() to bitfield.h next release.

Quoted 6 lines
drivers/iio/adc/nxp-sar-adc.c new file +6 -0
+       cdr = readl(NXP_SAR_ADC_CDR(info->regs, chan));
+       if (!(FIELD_GET(NXP_SAR_ADC_CDR_VALID, cdr)))
+               return -EIO;
+
+       return FIELD_GET(NXP_SAR_ADC_CDR_CDATA_MASK, cdr);
+}

...

drivers/iio/adc/nxp-sar-adc.c line 367

Quoted 3 lines
drivers/iio/adc/nxp-sar-adc.c new file +3 -0
+static irqreturn_t nxp_sar_adc_isr(int irq, void *dev_id)
+{
+       struct iio_dev *indio_dev = (struct iio_dev *)dev_id;

Unneeded explicit casting.

Quoted 31 lines
drivers/iio/adc/nxp-sar-adc.c new file +30 -0
+       struct nxp_sar_adc *info = iio_priv(indio_dev);
+       int isr;
+
+       isr = readl(NXP_SAR_ADC_ISR(info->regs));
+       if (!(FIELD_GET(NXP_SAR_ADC_ISR_ECH, isr)))
+               return IRQ_NONE;
+
+       if (iio_buffer_enabled(indio_dev))
+               nxp_sar_adc_isr_buffer(indio_dev);
+       else
+               nxp_sar_adc_isr_read_raw(indio_dev);
+
+       writel(NXP_SAR_ADC_ISR_ECH, NXP_SAR_ADC_ISR(info->regs));
+
+       return IRQ_HANDLED;
+}
 
+static void nxp_sar_adc_channels_disable(struct nxp_sar_adc *info, u32 mask)
+{
+       u32 ncmr, cimr;
+
+       ncmr = readl(NXP_SAR_ADC_NCMR0(info->regs));
+       cimr = readl(NXP_SAR_ADC_CIMR0(info->regs));
+
+       /* FIELD_MODIFY() can not be used because the mask is not constant */
+       ncmr &= ~mask;
+       cimr &= ~mask;
+
+       writel(ncmr, NXP_SAR_ADC_NCMR0(info->regs));
+       writel(cimr, NXP_SAR_ADC_CIMR0(info->regs));
+}

...

Quoted 7 lines
drivers/iio/adc/nxp-sar-adc.c new file +7 -0
+static void nxp_sar_adc_stop_conversion(struct nxp_sar_adc *info)
+{
+       u32 mcr;
+
+       mcr = readl(NXP_SAR_ADC_MCR(info->regs));
+
+       FIELD_MODIFY(NXP_SAR_ADC_MCR_NSTART, &mcr, 0x0);

drivers/iio/adc/nxp-sar-adc.c line 463

Quoted 10 lines
drivers/iio/adc/nxp-sar-adc.c new file +10 -0
+
+       writel(mcr, NXP_SAR_ADC_MCR(info->regs));
+
+       /*
+        * On disable, we have to wait for the transaction to finish.
+        * ADC does not abort the transaction if a chain conversion
+        * is in progress.
+        * Wait for the worst case scenario - 80 ADC clk cycles.
+        */
+       ndelay(div64_u64(NSEC_PER_SEC, clk_get_rate(info->clk)) * 80);

Same comment / question about the possible too long delays.

drivers/iio/adc/nxp-sar-adc.c line 473

Quoted 10 lines
drivers/iio/adc/nxp-sar-adc.c new file +10 -0
+}
+
+static int nxp_sar_adc_start_conversion(struct nxp_sar_adc *info, bool raw)
+{
+       u32 mcr;
+
+       mcr = readl(NXP_SAR_ADC_MCR(info->regs));
+
+       FIELD_MODIFY(NXP_SAR_ADC_MCR_NSTART, &mcr, 0x1);
+       FIELD_MODIFY(NXP_SAR_ADC_MCR_MODE, &mcr, !raw);

raw ? 0 : 1

is better to understand (it will be optimised by the compiler anyway, no branches will be added).

Quoted 16 lines
drivers/iio/adc/nxp-sar-adc.c new file +16 -0
+
+       writel(mcr, NXP_SAR_ADC_MCR(info->regs));
+
+       return 0;
+}
+
+static int nxp_sar_adc_read_channel(struct nxp_sar_adc *info, int channel)
+{
+       int ret;
+
+       info->current_channel = channel;
+       nxp_sar_adc_channels_enable(info, BIT(channel));
+       nxp_sar_adc_irq_cfg(info, true);
+       nxp_sar_adc_enable(info);
+
+       reinit_completion(&info->completion);

drivers/iio/adc/nxp-sar-adc.c line 499

Quoted 10 lines
drivers/iio/adc/nxp-sar-adc.c new file +9 -0
+       ret = nxp_sar_adc_start_conversion(info, true);
+       if (ret < 0)
+               goto out_disable;
 
+       ret = wait_for_completion_interruptible_timeout(&info->completion,
+                                                       NXP_SAR_ADC_CONV_TIMEOUT_JF);
+       if (ret == 0)
+               ret = -ETIMEDOUT;
+       if (ret > 0)
+               ret = 0;

Since semantically it's not the same ret, I would write above as

  if (!wait_for_completion...(...))
    ret = -ETIMEDOUT;

And note, no "else" branch is needed in this case.

Quoted 7 lines
drivers/iio/adc/nxp-sar-adc.c new file +7 -0
+       nxp_sar_adc_stop_conversion(info);
+
+out_disable:
+       nxp_sar_adc_channels_disable(info, BIT(channel));
+       nxp_sar_adc_irq_cfg(info, false);
+       nxp_sar_adc_disable(info);
+

drivers/iio/adc/nxp-sar-adc.c line 563

Quoted 10 lines
drivers/iio/adc/nxp-sar-adc.c new file +9 -0
+       return ret;
+}
 
+               /*
+                * Configures the sample period duration in terms of the SAR
+                * controller clock. The minimum acceptable value is 8.
+                * Configuring it to a value lower than 8 sets the sample period
+                * to 8 cycles.  We read the clock value and divide by the
+                * sampling timing which gives us the number of cycles expected.
+                * The value is 8 bits wide, consequently the max value is 0xFF.

8-bit wide

Quoted 1 line

+ */

...

drivers/iio/adc/nxp-sar-adc.c line 597

Quoted 7 lines
drivers/iio/adc/nxp-sar-adc.c new file +7 -0
+       /*
+        * DMA in some corner cases might have already be charged for
+        * the next transfer. Potentially there can be a race where
+        * the residue changes while the dma engine updates the
+        * buffer. That could be handled by using the
+        * callback_result() instead of callback() because the residue
+        * will be passed as parameter to the function. However this

as a parameter

Quoted 5 lines
drivers/iio/adc/nxp-sar-adc.c new file +5 -0
+        * new callback is pretty new and the backend does not update
+        * the residue. So let's stick to the version other drivers do
+        * which has proven running well in production since several
+        * years.
+        */

...

Review

Quoted 2 lines
(quoted diff) +2 -0
+               /*
+                * iio_push_to_buffers_with_ts should not be

iio_push_to_buffers_with_ts()

Quoted 3 lines
drivers/iio/adc/nxp-sar-adc.c new file +3 -0
+                * called with dma_samples as parameter. The samples
+                * will be smashed if timestamp is enabled.
+                */

...

Quoted 1 line

+ nxp_sar_adc_channels_disable(info, *indio_dev->active_scan_mask);

Wondering why this can't take a pointer to a mask.

...

drivers/iio/adc/nxp-sar-adc.c line 900

Quoted 3 lines
drivers/iio/adc/nxp-sar-adc.c new file +2 -0
+       info = iio_priv(indio_dev);
 
+

Unneeded blank line.

Quoted 1 line

+ info->vref_mV = data->vref_mV;

...

drivers/iio/adc/nxp-sar-adc.c line 917

Quoted 4 lines
drivers/iio/adc/nxp-sar-adc.c new file +4 -0
+       ret = devm_request_irq(dev, irq, nxp_sar_adc_isr, 0,
+                              dev_name(dev), indio_dev);
+       if (ret < 0)
+               return dev_err_probe(dev, ret, "failed requesting irq, irq = %d\n", irq);

No error code duplication in the message, please.

...

Quoted 1 line

+ spin_lock_init(&info->lock);

Shouldn't this be _before_ IRQ registration? Theoretically the IRQ may fire already just after the registration (yeah, it might be spurious, but handler and code should be ready for this).

...

drivers/iio/adc/nxp-sar-adc.c line 940

Quoted 3 lines
drivers/iio/adc/nxp-sar-adc.c new file +3 -0
+       ret = nxp_sar_adc_calibration(info);
+       if (ret)
+               dev_err_probe(dev, ret, "Calibration failed: %d\n", ret);

No error code duplication in the message, please.

...

drivers/iio/adc/nxp-sar-adc.c line 963

Quoted 4 lines
drivers/iio/adc/nxp-sar-adc.c new file +4 -0
+static int nxp_sar_adc_suspend(struct device *dev)
+{
+       struct iio_dev *indio_dev = dev_get_drvdata(dev);
+       struct nxp_sar_adc *info = iio_priv(indio_dev);

Can be one-lined

       struct nxp_sar_adc *info = iio_priv(dev_get_drvdata(dev));

drivers/iio/adc/nxp-sar-adc.c line 976

Quoted 12 lines
drivers/iio/adc/nxp-sar-adc.c new file +12 -0
+       info->pwdn = nxp_sar_adc_disable(info);
+       info->inpsamp = nxp_sar_adc_conversion_timing_get(info);
+
+       clk_disable_unprepare(info->clk);
+
+       return 0;
+}
+
+static int nxp_sar_adc_resume(struct device *dev)
+{
+       struct iio_dev *indio_dev = dev_get_drvdata(dev);
+       struct nxp_sar_adc *info = iio_priv(indio_dev);

Ditto.

Quoted 13 lines
drivers/iio/adc/nxp-sar-adc.c new file +13 -0
+       int ret;
+
+       ret = clk_prepare_enable(info->clk);
+       if (ret)
+               return ret;
+
+       nxp_sar_adc_conversion_timing_set(info, info->inpsamp);
+
+       if (!info->pwdn)
+               nxp_sar_adc_enable(info);
+
+       return 0;
+}

...

drivers/iio/adc/nxp-sar-adc.c line 995

Quoted 3 lines
drivers/iio/adc/nxp-sar-adc.c new file +3 -0
+static const struct nxp_sar_adc_data s32g2_sar_adc_data = {
+       .vref_mV = 1800,
+       .model = "s32g2-sar-adc"

Leave trailing comma, it's not a terminator.

Quoted 1 line

+};

--
With Best Regards,
Andy Shevchenko

#4 lore raw

Re: [PATCH v6 2/2] iio: adc: Add the NXP SAR ADC support for the s32g2/3 platforms

Hi Andy,

thanks for the review

Quoted 2 lines · Andy Shevchenko

On Tue, Nov 18, 2025 at 10:34 PM Daniel Lezcano
<[email protected]> wrote:

[ ... ]

Quoted 9 lines
Quoted 3 lines

As the IIO is implementing the cyclic DMA support API, it is not worth to do more spins to the current routine as it will go away when the new API will be available.

...

Quoted 1 line

+#define NXP_SAR_ADC_EOC_CH(c) BIT((c) % 32)

Do you expect "c" to be bigger than 31? In which circumstances?

No, it should be always lesser than 32. We can drop the modulo.

[ ... ]

Quoted 10 lines

drivers/iio/adc/nxp-sar-adc.c line 221

Quoted 6 lines
drivers/iio/adc/nxp-sar-adc.c new file +6 -0
+       /*
+        * Ensure there are at least three cycles between the
+        * configuration of NCMR and the setting of NSTART.
+        */
+       if (enable)
+               ndelay(div64_u64(NSEC_PER_SEC, clk_get_rate(info->clk) * 3));

I'm wondering how low the clock rate can be? With low enough clock rates this becomes a 100% CPU busyloop and in atomic context (is this the case?) without even the possibility to schedule.

I believe this question was already addressed in v1:

https://lore.kernel.org/all/[email protected]/

right ?

[ ... ]

Quoted 6 lines
Quoted 5 lines
drivers/iio/adc/nxp-sar-adc.c new file +5 -0
+static int nxp_sar_adc_read_data(struct nxp_sar_adc *info, unsigned int chan)
+{
+       u32 ceocfr, cdr;
+
+       ceocfr = readl(NXP_SAR_ADC_CEOCFR0(info->regs));
Quoted 6 lines

drivers/iio/adc/nxp-sar-adc.c line 318

Quoted 3 lines
drivers/iio/adc/nxp-sar-adc.c new file +3 -0
+       /* FIELD_GET() can not be used here because EOC_CH is not constant */
+       if (!(NXP_SAR_ADC_EOC_CH(chan) & ceocfr))
+               return -EIO;

[nxp_sar_adc_]field_get() may be defined and used. There is a series pending to bring field_get() to bitfield.h next release.

TBH I don't have an infinite bandwidth to write temporary helpers. So if it is ok, I would prefer to keep it as is

Quoted 7 lines

...

drivers/iio/adc/nxp-sar-adc.c line 367

Quoted 3 lines
drivers/iio/adc/nxp-sar-adc.c new file +3 -0
+static irqreturn_t nxp_sar_adc_isr(int irq, void *dev_id)
+{
+       struct iio_dev *indio_dev = (struct iio_dev *)dev_id;

Unneeded explicit casting.

Right I will fix it.
[ ... ]

Quoted 11 lines

drivers/iio/adc/nxp-sar-adc.c line 473

Quoted 8 lines
drivers/iio/adc/nxp-sar-adc.c new file +8 -0
+static int nxp_sar_adc_start_conversion(struct nxp_sar_adc *info, bool raw)
+{
+       u32 mcr;
+
+       mcr = readl(NXP_SAR_ADC_MCR(info->regs));
+
+       FIELD_MODIFY(NXP_SAR_ADC_MCR_NSTART, &mcr, 0x1);
+       FIELD_MODIFY(NXP_SAR_ADC_MCR_MODE, &mcr, !raw);

raw ? 0 : 1

Quoted 2 lines

is better to understand (it will be optimised by the compiler anyway, no branches will be added).

Ok, will do the change

Quoted 27 lines
Quoted 16 lines
drivers/iio/adc/nxp-sar-adc.c new file +16 -0
+
+       writel(mcr, NXP_SAR_ADC_MCR(info->regs));
+
+       return 0;
+}
+
+static int nxp_sar_adc_read_channel(struct nxp_sar_adc *info, int channel)
+{
+       int ret;
+
+       info->current_channel = channel;
+       nxp_sar_adc_channels_enable(info, BIT(channel));
+       nxp_sar_adc_irq_cfg(info, true);
+       nxp_sar_adc_enable(info);
+
+       reinit_completion(&info->completion);

drivers/iio/adc/nxp-sar-adc.c line 499

Quoted 10 lines
drivers/iio/adc/nxp-sar-adc.c new file +9 -0
+       ret = nxp_sar_adc_start_conversion(info, true);
+       if (ret < 0)
+               goto out_disable;
 
+       ret = wait_for_completion_interruptible_timeout(&info->completion,
+                                                       NXP_SAR_ADC_CONV_TIMEOUT_JF);
+       if (ret == 0)
+               ret = -ETIMEDOUT;
+       if (ret > 0)
+               ret = 0;
Quoted 6 lines

Since semantically it's not the same ret, I would write above as

   if (!wait_for_completion...(...))
     ret = -ETIMEDOUT;

And note, no "else" branch is needed in this case.

Sure, I'll change that

Quoted 3 lines
Quoted 1 line

+ nxp_sar_adc_channels_disable(info, *indio_dev->active_scan_mask);

Wondering why this can't take a pointer to a mask.

nxp_sar_adc_channels_disable() is also called with BIT(x) parameter in other places. So in the function is much easier to do val |= mask;

Quoted 6 lines

drivers/iio/adc/nxp-sar-adc.c line 917

Quoted 4 lines
drivers/iio/adc/nxp-sar-adc.c new file +4 -0
+       ret = devm_request_irq(dev, irq, nxp_sar_adc_isr, 0,
+                              dev_name(dev), indio_dev);
+       if (ret < 0)
+               return dev_err_probe(dev, ret, "failed requesting irq, irq = %d\n", irq);

No error code duplication in the message, please.

Given devm_request will print the "request_irq(%u) %ps %ps %s\n" error message. Would you suggest to just return ret here ?

Quoted 5 lines
Quoted 1 line

+ spin_lock_init(&info->lock);

Shouldn't this be _before_ IRQ registration? Theoretically the IRQ may fire already just after the registration (yeah, it might be spurious, but handler and code should be ready for this).

Well it does not hurt moving it before anyway

[ ... ]

Thanks

-- Daniel

Signature · 5 lines
<http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs

Follow Linaro:  <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog
#5 lore raw

Re: [PATCH v6 2/2] iio: adc: Add the NXP SAR ADC support for the s32g2/3 platforms

Quoted 3 lines · Daniel Lezcano
Quoted 2 lines · Andy Shevchenko

On Tue, Nov 18, 2025 at 10:34 PM Daniel Lezcano
<[email protected]> wrote:

[ ... ]

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

+#define NXP_SAR_ADC_EOC_CH(c) BIT((c) % 32)

Do you expect "c" to be bigger than 31? In which circumstances?

No, it should be always lesser than 32. We can drop the modulo.

Yep, please avoid adding a code that is never needed. It complicates reading, reviewing, and understanding the flow.

[ ... ]

Quoted 11 lines
Quoted 10 lines

drivers/iio/adc/nxp-sar-adc.c line 221

Quoted 6 lines
drivers/iio/adc/nxp-sar-adc.c new file +6 -0
+       /*
+        * Ensure there are at least three cycles between the
+        * configuration of NCMR and the setting of NSTART.
+        */
+       if (enable)
+               ndelay(div64_u64(NSEC_PER_SEC, clk_get_rate(info->clk) * 3));

I'm wondering how low the clock rate can be? With low enough clock rates this becomes a 100% CPU busyloop and in atomic context (is this the case?) without even the possibility to schedule.

Quoted 5 lines

I believe this question was already addressed in v1:

https://lore.kernel.org/all/[email protected]/

right ?

Right, so the bottom line is that we miss the one-two phrases in the comments above to summarize that.

[ ... ]

Quoted 9 lines
Quoted 6 lines

drivers/iio/adc/nxp-sar-adc.c line 318

Quoted 3 lines
drivers/iio/adc/nxp-sar-adc.c new file +3 -0
+       /* FIELD_GET() can not be used here because EOC_CH is not constant */
+       if (!(NXP_SAR_ADC_EOC_CH(chan) & ceocfr))
+               return -EIO;

[nxp_sar_adc_]field_get() may be defined and used. There is a series pending to bring field_get() to bitfield.h next release.

TBH I don't have an infinite bandwidth to write temporary helpers. So if it is ok, I would prefer to keep it as is

Sure, perhaps just add a TODO line instead of the comment above:

	/* TODO: Switch to field_get() when it will be available */

[ ... ]

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

+ nxp_sar_adc_channels_disable(info, *indio_dev->active_scan_mask);

Wondering why this can't take a pointer to a mask.

nxp_sar_adc_channels_disable() is also called with BIT(x) parameter in other places. So in the function is much easier to do val |= mask;

OK!

Quoted 9 lines
Quoted 6 lines

drivers/iio/adc/nxp-sar-adc.c line 917

Quoted 4 lines
drivers/iio/adc/nxp-sar-adc.c new file +4 -0
+       ret = devm_request_irq(dev, irq, nxp_sar_adc_isr, 0,
+                              dev_name(dev), indio_dev);
+       if (ret < 0)
+               return dev_err_probe(dev, ret, "failed requesting irq, irq = %d\n", irq);

No error code duplication in the message, please.

Given devm_request will print the "request_irq(%u) %ps %ps %s\n" error message. Would you suggest to just return ret here ?

Yes!

Signature · 2 lines
With Best Regards,
Andy Shevchenko