NXP SAR ADC IIO driver for s32g2/3 platforms
Subsystems (2)
v3 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]/
Changelog:
* 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 | 1029 +++++++++++++++++ 4 files changed, 1105 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
PATCH v3 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
@@ -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
PATCH v3 2/2 iio: adc: Add the NXP SAR ADC support for the s32g2/3 platforms
In reply to Daniel LezcanoFrom: 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.
--- drivers/iio/adc/Kconfig | 12 + drivers/iio/adc/Makefile | 1 + drivers/iio/adc/nxp-sar-adc.c | 1029 +++++++++++++++++++++++++++++++++ 3 files changed, 1042 insertions(+) create mode 100644 drivers/iio/adc/nxp-sar-adc.c
drivers/iio/adc/Kconfig +12 -0
@@ -1168,6 +1168,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
@@ -104,6 +104,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 +1029 -0
1,029 lines not on this page · raw message
Signature · 1 line
2.43.0
Re: [PATCH v3 2/2] iio: adc: Add the NXP SAR ADC support for the s32g2/3 platforms
Le 16/09/2025 à 22:26, Daniel Lezcano a écrit :
Quoted 34 lines
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.
Co-developed-by Alexandru-Catalin Ionita <[email protected]>Signed-off-by Alexandru-Catalin Ionita <[email protected]>Co-developed-by Ciprian Costea <[email protected]>Signed-off-by Ciprian Costea <[email protected]>Co-developed-by Radu Pirea (NXP OSS) <[email protected]>Signed-off-by Radu Pirea (NXP OSS) <[email protected]>Signed-off-by Stefan-Gabriel Mirea <[email protected]>Co-developed-by Daniel Lezcano <[email protected]>Signed-off-by Daniel Lezcano <[email protected]>
Quoted 1 line
---
Hi,
Quoted 10 lines
drivers/iio/adc/nxp-sar-adc.c new file +10
-0+static void nxp_sar_adc_dma_remove (void *data) +{ +struct nxp_sar_adc *info = data; + +dma_free_coherent (info->dma_chan->device->dev,NXP_SAR_ADC_DMA_BUFF_SZ , + info->dma_buf.buf, info->rx_dma_buf); +} + +static int nxp_sar_adc_dma_probe (struct device *dev,struct nxp_sar_adc *info) +{
drivers/iio/adc/nxp-sar-adc.c line 855
Quoted 10 lines
drivers/iio/adc/nxp-sar-adc.c new file +10
-0+struct device *dev_dma; +u8 *rx_buf; + + info->dma_chan =devm_dma_request_chan (dev,"rx" ); +if (IS_ERR (info->dma_chan)) +return PTR_ERR (info->dma_chan); + + dev_dma = info->dma_chan->device->dev; + rx_buf =dma_alloc_coherent (dev_dma,NXP_SAR_ADC_DMA_BUFF_SZ , + &info->rx_dma_buf,GFP_KERNEL );
maybe dmam_alloc_coherent() for the managed version? This would save some LoC.
Quoted 7 lines
drivers/iio/adc/nxp-sar-adc.c new file +7
-0+if (!rx_buf) +return -ENOMEM ; + + info->dma_buf.buf = rx_buf; + +return devm_add_action_or_reset (dev, nxp_sar_adc_dma_remove, info); +}
Re: [PATCH v3 2/2] iio: adc: Add the NXP SAR ADC support for the s32g2/3 platforms
Hi Christophe,
On 16/09/2025 22:53, Christophe JAILLET wrote:
[ ... ]
Quoted 8 lines
Quoted 8 lines
+static int
nxp_sar_adc_dma_probe(struct device *dev, struct nxp_sar_adc *info)drivers/iio/adc/nxp-sar-adc.c new file +6
-0+{ + struct device *dev_dma; + u8 *rx_buf; + + info->dma_chan = devm_dma_request_chan(dev, "rx"); + if (IS_ERR(info->dma_chan))
Quoted 8 lines
Quoted 5 lines
+ return
PTR_ERR(info->dma_chan);
+
+dev_dma=info->dma_chan->device->dev;
+rx_buf= dma_alloc_coherent(dev_dma,NXP_SAR_ADC_DMA_BUFF_SZ, +&info->rx_dma_buf,GFP_KERNEL);maybe
dmam_alloc_coherent()for the managed version? This would save some LoC.
Ok, thanks for the suggestion, I'll do the change.
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