NXP SAR ADC IIO driver for s32g2/3 platforms
Subsystems (3)
v5 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:
* 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 | 1006 +++++++++++++++++ 4 files changed, 1082 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 v5 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 v5 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 | 1006 +++++++++++++++++++++++++++++++++ 3 files changed, 1019 insertions(+) create mode 100644 drivers/iio/adc/nxp-sar-adc.c
drivers/iio/adc/Kconfig +12 -0
@@ -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
@@ -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 +1006 -0
1,006 lines not on this page · raw message
Signature · 1 line
2.43.0
Re: [PATCH v5 2/2] iio: adc: Add the NXP SAR ADC support for the s32g2/3 platforms
Quoted 24 lines · 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.
...
Quoted 1 line
+#include <
linux/circ_buf.h>
Why not kfifo?
...
Quoted 1 line
+#define
NXP_SAR_ADC_IIO_BUFF_SZ(NXP_SAR_ADC_NR_CHANNELS+ (sizeof(u64) / sizeof(u16)))
Hmm... Don't we have some macros so we can avoid this kind of hard coding?
...
drivers/iio/adc/nxp-sar-adc.c line 465
Quoted 1 line
drivers/iio/adc/nxp-sar-adc.c new file +1
-0+ndelay (div64_u64 (NSEC_PER_SEC ,clk_get_rate (info->clk)) *80U );
Do you need those 'U':s? clk_get_rate() already returns unsigned value of the same or higher rank than int. No?
...
drivers/iio/adc/nxp-sar-adc.c line 475
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, which is boolean, as a parameter to FIELD_MODIFY() seems a bit odd to me, perhaps simple
raw ? 0 : 1
would work better?
(Note, optimizer of the complier will avoid any branching)
Quoted 4 lines
drivers/iio/adc/nxp-sar-adc.c new file +4
-0+writel (mcr,NXP_SAR_ADC_MCR (info->regs)); + +return 0 ; +}
...
drivers/iio/adc/nxp-sar-adc.c line 590
Quoted 1 line
drivers/iio/adc/nxp-sar-adc.c new file +1
-0+ dma_samples = (u32 *)dma_buf->buf;
Is it aligned properly for this type of casting?
...
drivers/iio/adc/nxp-sar-adc.c line 593
Quoted 1 line
drivers/iio/adc/nxp-sar-adc.c new file +1
-0+dmaengine_tx_status (info->dma_chan, info->cookie, &state);
No return value check?
...
drivers/iio/adc/nxp-sar-adc.c line 985
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"
Keep a trailing comma as here it's not a termination member.
Quoted 1 line
+};
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v5 2/2] iio: adc: Add the NXP SAR ADC support for the s32g2/3 platforms
Hi Andy,
Quoted 26 lines · Andy Shevchenko
Quoted 20 lines · 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.
Quoted 4 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.
Quoted 5 lines
...
Quoted 1 line
+#include <
linux/circ_buf.h>Why not kfifo?
I'm sorry but I don't get your comment.
Do you mean why not use kfifo.h or use kfifo API and change all the code using the circ_buf by the kfifo ?
Quoted 5 lines
...
Quoted 1 line
+#define
NXP_SAR_ADC_IIO_BUFF_SZ(NXP_SAR_ADC_NR_CHANNELS+ (sizeof(u64) / sizeof(u16)))Hmm... Don't we have some macros so we can avoid this kind of hard coding?
I don't find such a macro, do you have a pointer ?
Quoted 6 lines
...
drivers/iio/adc/nxp-sar-adc.c line 465
Quoted 1 line
drivers/iio/adc/nxp-sar-adc.c new file +1
-0+ndelay (div64_u64 (NSEC_PER_SEC ,clk_get_rate (info->clk)) *80U );Do you need those 'U':s?
clk_get_rate()already returns unsigned value of the same or higher rank than int. No?
May be not needed, but harmless. I can remove them if you want
Quoted 12 lines
drivers/iio/adc/nxp-sar-adc.c line 475
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, which is boolean, as a parameter to
FIELD_MODIFY()seems a bit odd to me, perhaps simple
Quoted 3 lines
raw ? 0 : 1would work better?
Sure
Quoted 9 lines
(Note, optimizer of the complier will avoid any branching)
Quoted 4 lines
drivers/iio/adc/nxp-sar-adc.c new file +4
-0+writel (mcr,NXP_SAR_ADC_MCR (info->regs)); + +return 0 ; +}...
Quoted 3 lines
drivers/iio/adc/nxp-sar-adc.c line 590
Quoted 1 line
drivers/iio/adc/nxp-sar-adc.c new file +1
-0+ dma_samples = (u32 *)dma_buf->buf;Is it aligned properly for this type of casting?
TBH, I don't know the answer :/
How can I check that ?
Quoted 3 lines
drivers/iio/adc/nxp-sar-adc.c line 593
Quoted 1 line
drivers/iio/adc/nxp-sar-adc.c new file +1
-0+dmaengine_tx_status (info->dma_chan, info->cookie, &state);No return value check?
The return value is not necessary here because the caller of the callback will check with dma_submit_error() in case of error which covers the DMA_ERROR case and the other cases are not useful because the residue is taken into account right after.
It could be written differently with the DMA_COMPLETE check but the result will be the same. I prefer to keep the current implementation which has been tested.
Quoted 5 lines
drivers/iio/adc/nxp-sar-adc.c line 985
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" Keep a trailing comma as here it's not a termination member.
Ok, I'll add it
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
Re: [PATCH v5 2/2] iio: adc: Add the NXP SAR ADC support for the s32g2/3 platforms
Quoted 2 lines · Daniel Lezcano
Quoted 1 line · Andy Shevchenko
On Fri, Oct 17, 2025 at 06:42:38PM +0200, Daniel Lezcano wrote:
...
Quoted 8 lines
Quoted 3 lines
Quoted 1 line
+#include <
linux/circ_buf.h>Why not kfifo?
I'm sorry but I don't get your comment.
Do you mean why not use
kfifo.hor use kfifo API and change all the code using thecirc_bufby the kfifo ?
Yes, I mean why use circ_buf API and not kfifo API.
...
Quoted 5 lines
Quoted 3 lines
Quoted 1 line
+#define
NXP_SAR_ADC_IIO_BUFF_SZ(NXP_SAR_ADC_NR_CHANNELS+ (sizeof(u64) / sizeof(u16)))Hmm... Don't we have some macros so we can avoid this kind of hard coding?
I don't find such a macro, do you have a pointer ?
If I got the use case correctly, I was thinking of IIO_DECLARE_BUFFER_WITH_TS().
...
Quoted 6 lines
Quoted 4 lines
drivers/iio/adc/nxp-sar-adc.c line 465
Quoted 1 line
drivers/iio/adc/nxp-sar-adc.c new file +1
-0+ndelay (div64_u64 (NSEC_PER_SEC ,clk_get_rate (info->clk)) *80U );Do you need those 'U':s?
clk_get_rate()already returns unsigned value of the same or higher rank than int. No?May be not needed, but harmless. I can remove them if you want
I think using them in cases that have no side-effects are not needed.
...
Quoted 7 lines
Quoted 3 lines
drivers/iio/adc/nxp-sar-adc.c line 590
Quoted 1 line
drivers/iio/adc/nxp-sar-adc.c new file +1
-0+ dma_samples = (u32 *)dma_buf->buf;Is it aligned properly for this type of casting?
TBH, I don't know the answer :/
How can I check that ?
Is buf defined as a pointer to u32 / int or bigger? or is it just byte buffer? If the latter, how does the address of it being formed? Does it come from a heap (memory allocator)? If yes, we are fine, as this is usually the case for all (k)malloc'ed memory.
...
Quoted 8 lines
Quoted 3 lines
drivers/iio/adc/nxp-sar-adc.c line 593
Quoted 1 line
drivers/iio/adc/nxp-sar-adc.c new file +1
-0+dmaengine_tx_status (info->dma_chan, info->cookie, &state);No return value check?
The return value is not necessary here because the caller of the callback will check with
dma_submit_error()in case of error which covers theDMA_ERRORcase and the other cases are not useful because the residue is taken into account right after.
In some cases it might return DMA_PAUSE (and actually this is the correct way to get residue, one needs to pause the channel to read it, otherwise it will give outdated / incorrect information).
Quoted 3 lines
It could be written differently with the
DMA_COMPLETEcheck but the result will be the same. I prefer to keep the current implementation which has been tested.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v5 2/2] iio: adc: Add the NXP SAR ADC support for the s32g2/3 platforms
Quoted 8 lines · Andy Shevchenko
Quoted 2 lines · Daniel Lezcano
Quoted 1 line · Andy Shevchenko
On Fri, Oct 17, 2025 at 06:42:38PM +0200, Daniel Lezcano wrote:
...
Quoted 2 lines
Quoted 2 lines
Quoted 1 line
+#include <
linux/circ_buf.h>
Quoted 8 lines
Quoted 6 lines
Quoted 1 line
Why not kfifo?
I'm sorry but I don't get your comment.
Do you mean why not use
kfifo.hor use kfifo API and change all the code using thecirc_bufby the kfifo ?Yes, I mean why use
circ_bufAPI and not kfifo API.
Ok, thanks for the clarification.
At the first glance, the resulting code with the kfifo API would look nicer. However, on one side, the current code was largely tested and it is running on a production system, on the other side the IIO framework will evolve with the cyclic DMA support and this driver would be converted to it when ready.
I can convert to kfifo but if it is changed again for the new IIO cyclic DMA, I'm not sure it is worth the effort.
[ ... ]
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
Re: [PATCH v5 2/2] iio: adc: Add the NXP SAR ADC support for the s32g2/3 platforms
In reply to Andy ShevchenkoQuoted 3 lines · Andy Shevchenko
Quoted 2 lines · Daniel Lezcano
Quoted 1 line · Andy Shevchenko
On Fri, Oct 17, 2025 at 06:42:38PM +0200, Daniel Lezcano wrote:
[ ... ]
Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line
+#define
NXP_SAR_ADC_IIO_BUFF_SZ(NXP_SAR_ADC_NR_CHANNELS+ (sizeof(u64) / sizeof(u16)))Hmm... Don't we have some macros so we can avoid this kind of hard coding?
I don't find such a macro, do you have a pointer ?
If I got the use case correctly, I was thinking of
IIO_DECLARE_BUFFER_WITH_TS().
Oh right ! Actually, IIO_DECLARE_BUFFER_WITH_TS() is used but the macro above was not removed :)
[ ... ]
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
drivers/iio/adc/nxp-sar-adc.c line 590
Quoted 1 line
drivers/iio/adc/nxp-sar-adc.c new file +1
-0+ dma_samples = (u32 *)dma_buf->buf;Is it aligned properly for this type of casting?
Quoted 8 lines
Quoted 3 lines
TBH, I don't know the answer :/
How can I check that ?
Is buf defined as a pointer to u32 / int or bigger? or is it just byte buffer? If the latter, how does the address of it being formed? Does it come from a heap (memory allocator)? If yes, we are fine, as this is usually the case for all (k)malloc'ed memory.
buf is a byte buffer allocated with dmam_alloc_coherent(..., GFP_KERNEL)
Quoted 6 lines
...
Quoted 4 lines
Quoted 3 lines
drivers/iio/adc/nxp-sar-adc.c line 593
Quoted 1 line
drivers/iio/adc/nxp-sar-adc.c new file +1
-0+dmaengine_tx_status (info->dma_chan, info->cookie, &state);No return value check?
Quoted 8 lines
Quoted 4 lines
The return value is not necessary here because the caller of the callback will check with
dma_submit_error()in case of error which covers theDMA_ERRORcase and the other cases are not useful because the residue is taken into account right after.In some cases it might return
DMA_PAUSE(and actually this is the correct way to get residue, one needs to pause the channel to read it, otherwise it will give outdated / incorrect information).
But if the residue is checked in the callback routine without checking DMA_PAUSED, the result is the same no ?
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
Re: [PATCH v5 2/2] iio: adc: Add the NXP SAR ADC support for the s32g2/3 platforms
Quoted 4 lines · Daniel Lezcano
Quoted 3 lines · Andy Shevchenko
Quoted 2 lines · Daniel Lezcano
Quoted 1 line · Andy Shevchenko
On Fri, Oct 17, 2025 at 06:42:38PM +0200, Daniel Lezcano wrote:
[ ... ]
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 3 lines
drivers/iio/adc/nxp-sar-adc.c line 590
Quoted 1 line
drivers/iio/adc/nxp-sar-adc.c new file +1
-0+ dma_samples = (u32 *)dma_buf->buf;Is it aligned properly for this type of casting?
TBH, I don't know the answer :/
Quoted 8 lines
Quoted 6 lines
Quoted 1 line
How can I check that ?
Is buf defined as a pointer to u32 / int or bigger? or is it just byte buffer? If the latter, how does the address of it being formed? Does it come from a heap (memory allocator)? If yes, we are fine, as this is usually the case for all (k)malloc'ed memory.
buf is a byte buffer allocated with
dmam_alloc_coherent(..., GFP_KERNEL)
We are fine :-)
...
Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
Quoted 3 lines
drivers/iio/adc/nxp-sar-adc.c line 593
Quoted 1 line
drivers/iio/adc/nxp-sar-adc.c new file +1
-0+dmaengine_tx_status (info->dma_chan, info->cookie, &state);No return value check?
The return value is not necessary here because the caller of the callback will check with
dma_submit_error()in case of error which covers theDMA_ERRORcase and the other cases are not useful because the residue is taken into account right after.
Quoted 6 lines
Quoted 3 lines
In some cases it might return
DMA_PAUSE(and actually this is the correct way to get residue, one needs to pause the channel to read it, otherwise it will give outdated / incorrect information).But if the residue is checked in the callback routine without checking
DMA_PAUSED, the result is the same no ?
DMA in some corner cases might have already be charged for the next transfer. Do you have a synchronisation between DMA start and residue check?
I.o.w. this may work for your case, but in general it's not guaranteed. The proper read of residue is to: pause DMA --> read residue --> resume DMA.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v5 2/2] iio: adc: Add the NXP SAR ADC support for the s32g2/3 platforms
Quoted 40 lines · Andy Shevchenko
Quoted 4 lines · Daniel Lezcano
Quoted 3 lines · Andy Shevchenko
Quoted 2 lines · Daniel Lezcano
Quoted 1 line · Andy Shevchenko
On Fri, Oct 17, 2025 at 06:42:38PM +0200, Daniel Lezcano wrote:
[ ... ]
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 3 lines
drivers/iio/adc/nxp-sar-adc.c line 590
Quoted 1 line
drivers/iio/adc/nxp-sar-adc.c new file +1
-0+ dma_samples = (u32 *)dma_buf->buf;Is it aligned properly for this type of casting?
TBH, I don't know the answer :/
Quoted 8 lines
Quoted 6 lines
Quoted 1 line
How can I check that ?
Is buf defined as a pointer to u32 / int or bigger? or is it just byte buffer? If the latter, how does the address of it being formed? Does it come from a heap (memory allocator)? If yes, we are fine, as this is usually the case for all (k)malloc'ed memory.
buf is a byte buffer allocated with
dmam_alloc_coherent(..., GFP_KERNEL)We are fine :-)
...
Quoted 13 lines
Quoted 12 lines
Quoted 8 lines
Quoted 3 lines
drivers/iio/adc/nxp-sar-adc.c line 593
Quoted 1 line
drivers/iio/adc/nxp-sar-adc.c new file +1
-0+dmaengine_tx_status (info->dma_chan, info->cookie, &state);No return value check?
The return value is not necessary here because the caller of the callback will check with
dma_submit_error()in case of error which covers theDMA_ERRORcase and the other cases are not useful because the residue is taken into account right after.In some cases it might return
DMA_PAUSE(and actually this is the correct way to get residue, one needs to pause the channel to read it, otherwise it will give outdated / incorrect information).
Quoted 9 lines
Quoted 2 lines
But if the residue is checked in the callback routine without checking
DMA_PAUSED, the result is the same no ?DMA in some corner cases might have already be charged for the next transfer. Do you have a synchronisation between DMA start and residue check?
I.o.w. this may work for your case, but in general it's not guaranteed. The proper read of residue is to: pause DMA --> read residue --> resume DMA.
I'll use the new callback function dma_async_tx_callback_result() which should prevent that and allows to remove the spinlock at the same time
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
Re: [PATCH v5 2/2] iio: adc: Add the NXP SAR ADC support for the s32g2/3 platforms
Quoted 6 lines · Daniel Lezcano
Quoted 5 lines · Andy Shevchenko
Quoted 4 lines · Daniel Lezcano
Quoted 3 lines · Andy Shevchenko
Quoted 2 lines · Daniel Lezcano
Quoted 1 line · Andy Shevchenko
On Fri, Oct 17, 2025 at 06:42:38PM +0200, Daniel Lezcano wrote:
[ ... ]
Quoted 16 lines
Quoted 16 lines
Quoted 15 lines
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
Review
Quoted 1 line
(quoted diff) +1
-0+ dmaengine_tx_status(info->dma_chan, info->cookie, &state);No return value check?
Quoted 8 lines
Quoted 4 lines
The return value is not necessary here because the caller of the callback will check with
dma_submit_error()in case of error which covers theDMA_ERRORcase and the other cases are not useful because the residue is taken into account right after.In some cases it might return
DMA_PAUSE(and actually this is the correct way to get residue, one needs to pause the channel to read it, otherwise it will give outdated / incorrect information).But if the residue is checked in the callback routine without checking
DMA_PAUSED, the result is the same no ?
Quoted 8 lines
Quoted 5 lines
DMA in some corner cases might have already be charged for the next transfer. Do you have a synchronisation between DMA start and residue check?
I.o.w. this may work for your case, but in general it's not guaranteed. The proper read of residue is to: pause DMA --> read residue --> resume DMA.
I'll use the new callback function
dma_async_tx_callback_result()which should prevent that and allows to remove the spinlock at the same time
So, AFAIU this doesn't work for you.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v5 2/2] iio: adc: Add the NXP SAR ADC support for the s32g2/3 platforms
In reply to Andy ShevchenkoHi Andy,
Quoted 40 lines · Andy Shevchenko
Quoted 4 lines · Daniel Lezcano
Quoted 3 lines · Andy Shevchenko
Quoted 2 lines · Daniel Lezcano
Quoted 1 line · Andy Shevchenko
On Fri, Oct 17, 2025 at 06:42:38PM +0200, Daniel Lezcano wrote:
[ ... ]
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 3 lines
drivers/iio/adc/nxp-sar-adc.c line 590
Quoted 1 line
drivers/iio/adc/nxp-sar-adc.c new file +1
-0+ dma_samples = (u32 *)dma_buf->buf;Is it aligned properly for this type of casting?
TBH, I don't know the answer :/
Quoted 8 lines
Quoted 6 lines
Quoted 1 line
How can I check that ?
Is buf defined as a pointer to u32 / int or bigger? or is it just byte buffer? If the latter, how does the address of it being formed? Does it come from a heap (memory allocator)? If yes, we are fine, as this is usually the case for all (k)malloc'ed memory.
buf is a byte buffer allocated with
dmam_alloc_coherent(..., GFP_KERNEL)We are fine :-)
...
Quoted 13 lines
Quoted 12 lines
Quoted 8 lines
Quoted 3 lines
drivers/iio/adc/nxp-sar-adc.c line 593
Quoted 1 line
drivers/iio/adc/nxp-sar-adc.c new file +1
-0+dmaengine_tx_status (info->dma_chan, info->cookie, &state);No return value check?
The return value is not necessary here because the caller of the callback will check with
dma_submit_error()in case of error which covers theDMA_ERRORcase and the other cases are not useful because the residue is taken into account right after.In some cases it might return
DMA_PAUSE(and actually this is the correct way to get residue, one needs to pause the channel to read it, otherwise it will give outdated / incorrect information).
Quoted 8 lines
Quoted 2 lines
But if the residue is checked in the callback routine without checking
DMA_PAUSED, the result is the same no ?DMA in some corner cases might have already be charged for the next transfer. Do you have a synchronisation between DMA start and residue check?
I.o.w. this may work for your case, but in general it's not guaranteed. The proper read of residue is to: pause DMA --> read residue --> resume DMA.
I discussed with Vinod about this change and he suggested to use the callback_result() to get the residue as a parameter so the dmaengine_txstatus() call won't be needed anymore.
Unfortunately, it does not work. I had a look in the DMA driver and the internals but my knowledge is limited in this area so I was unable to find out what is going on. Moreover there are no so many driver using this API I can use as an example. The best I was able to do was propagating the residue to the result in the vchan_complete() but it does not work.
Then I stepped back by not using the callback_result() and used dmaengine_pause(), read the residue, dmaengine_resume() but there are no result after these calls. I don't know why.
The issue you are mentioning above should be handled in other drivers doing the same kind of acquisition but the routine is similar to the one proposed here (eg. stm32).
The NXP SAR acquisition routine is running since several years in production AFAICT.
I investigated the different solutions without success, while I can run the acquisition routine without problem here with my hardware. A signal generator captured by the ADC, plotted and compared with the oscilloscope display.
The circ buffer is working well here and no bug was spotted with the current routine. I think I did my best to make the driver better from its initial submission. The best is the enemy of the good, and I would like to make some progress here in the driver acceptance. Changing the entire driver for the sake of replacing the circ_buffer by the kfifo and change the code for a scenario which is not happening is not really worth. Especially that the DMA engine is being modified to take into the cyclic DMA in its API, thus the circ_buffer and the routine will go away once the driver is changed to take into account this new API.
IOW, can we keep this routine as it is for now as it works fine and go forward for a v6 ?
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
Re: [PATCH v5 2/2] iio: adc: Add the NXP SAR ADC support for the s32g2/3 platforms
Quoted 6 lines · Daniel Lezcano
Quoted 5 lines · Andy Shevchenko
Quoted 4 lines · Daniel Lezcano
Quoted 3 lines · Andy Shevchenko
Quoted 2 lines · Daniel Lezcano
Quoted 1 line · Andy Shevchenko
On Fri, Oct 17, 2025 at 06:42:38PM +0200, Daniel Lezcano wrote:
[ ... ]
Quoted 58 lines
Quoted 13 lines
Quoted 13 lines
Quoted 12 lines
Quoted 8 lines
Quoted 3 lines
Review
Quoted 1 line
(quoted diff) +1
-0+ dmaengine_tx_status(info->dma_chan, info->cookie, &state);No return value check?
The return value is not necessary here because the caller of the callback will check with
dma_submit_error()in case of error which covers theDMA_ERRORcase and the other cases are not useful because the residue is taken into account right after.In some cases it might return
DMA_PAUSE(and actually this is the correct way to get residue, one needs to pause the channel to read it, otherwise it will give outdated / incorrect information).Quoted 8 lines
Quoted 2 lines
But if the residue is checked in the callback routine without checking
DMA_PAUSED, the result is the same no ?DMA in some corner cases might have already be charged for the next transfer. Do you have a synchronisation between DMA start and residue check?
I.o.w. this may work for your case, but in general it's not guaranteed. The proper read of residue is to: pause DMA --> read residue --> resume DMA.
I discussed with Vinod about this change and he suggested to use the
callback_result()to get the residue as a parameter so thedmaengine_txstatus()call won't be needed anymore.Unfortunately, it does not work. I had a look in the DMA driver and the internals but my knowledge is limited in this area so I was unable to find out what is going on. Moreover there are no so many driver using this API I can use as an example. The best I was able to do was propagating the residue to the result in the
vchan_complete()but it does not work.Then I stepped back by not using the
callback_result()and useddmaengine_pause(), read the residue,dmaengine_resume()but there are no result after these calls. I don't know why.The issue you are mentioning above should be handled in other drivers doing the same kind of acquisition but the routine is similar to the one proposed here (eg. stm32).
The NXP SAR acquisition routine is running since several years in production AFAICT.
I investigated the different solutions without success, while I can run the acquisition routine without problem here with my hardware. A signal generator captured by the ADC, plotted and compared with the oscilloscope display.
The circ buffer is working well here and no bug was spotted with the current routine. I think I did my best to make the driver better from its initial submission. The best is the enemy of the good, and I would like to make some progress here in the driver acceptance. Changing the entire driver for the sake of replacing the
circ_bufferby the kfifo and change the code for a scenario which is not happening is not really worth. Especially that the DMA engine is being modified to take into the cyclic DMA in its API, thus thecirc_bufferand the routine will go away once the driver is changed to take into account this new API.
Quoted 2 lines
IOW, can we keep this routine as it is for now as it works fine and go forward for a v6 ?
Thanks for trying and getting to this. Please, make a summary of the research and pack it into a paragraph in the commit message, so we will have this in the history. Maybe even a short comment in the code.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Jonathan Cameron
maintainerRe: [PATCH v5 2/2] iio: adc: Add the NXP SAR ADC support for the s32g2/3 platforms
In reply to Daniel LezcanoQuoted 25 lines · On Fri, 17 Oct 2025 18:42:38 +0200 Daniel Lezcano <[email protected]>…
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.
Quoted 9 lines
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]>
Hi Daniel,
Only significant question in here is around lifetimes of the dma buffer.
+CC Vinod and dmaengine list. Hopefully someone will rapidly tell me my concern is garbage ;)
IIO folk who are familiar with dmaengine channels etc please take a look at this as well. I think all the upstream drivers we have doing similar things to this predate devm_ management being a common thing.
Jonathan
drivers/iio/adc/nxp-sar-adc.c line 579
Quoted 12 lines
drivers/iio/adc/nxp-sar-adc.c new file +5
-0@@ -0,0 +1,1006 @@ + +static void nxp_sar_adc_dma_cb (void *data) +{ +struct nxp_sar_adc *info =iio_priv (data); +struct iio_dev *indio_dev = data;
Trivial but it would slightly more intuitive to do.
struct iio_dev *indio_dev = data; struct nxp_sar_adc *info = iio_priv(indio_dev);
Quoted 36 lines
drivers/iio/adc/nxp-sar-adc.c new file +36
-0+struct dma_tx_state state; +struct circ_buf *dma_buf; +struct device *dev_dma; +u32 *dma_samples; +s64 timestamp; +int idx, ret; + +guard (spinlock_irqsave)(&info->lock); + + dma_buf = &info->dma_buf; + dma_samples = (u32 *)dma_buf->buf; + dev_dma = info->dma_chan->device->dev; + +dmaengine_tx_status (info->dma_chan, info->cookie, &state); + +dma_sync_single_for_cpu (dev_dma, info->rx_dma_buf, +NXP_SAR_ADC_DMA_BUFF_SZ ,DMA_FROM_DEVICE ); + +/* Current head position. */ + dma_buf->head = (NXP_SAR_ADC_DMA_BUFF_SZ - state.residue) / +NXP_SAR_ADC_DMA_SAMPLE_SZ ; + +/* If everything was transferred, avoid an off by one error. */ +if (!state.residue) + dma_buf->head--; + +/* Something went wrong and nothing transferred. */ +if (state.residue ==NXP_SAR_ADC_DMA_BUFF_SZ ) + goto out; + +/* Make sure that head is multiple of info->channels_used. */ + dma_buf->head -= dma_buf->head % info->channels_used; + +/* +* dma_buf->tail != dma_buf->head condition will become false +* because dma_buf->tail will be incremented with 1.
drivers/iio/adc/nxp-sar-adc.c line 625
Quoted 10 lines
drivers/iio/adc/nxp-sar-adc.c new file +10
-0+*/ +while (dma_buf->tail != dma_buf->head) { + idx = dma_buf->tail % info->channels_used; + info->buffer[idx] = dma_samples[dma_buf->tail]; + dma_buf->tail = (dma_buf->tail +1 ) %NXP_SAR_ADC_DMA_SAMPLE_CNT ; +if (idx != info->channels_used -1 ) +continue ; + +/* +* iio_push_to_buffers_with_timestamp should not be
Comment needs an update as using with_ts()
Quoted 23 lines
drivers/iio/adc/nxp-sar-adc.c new file +20
-0+* called with dma_samples as parameter. The samples +* will be smashed if timestamp is enabled. +*/ + timestamp =iio_get_time_ns (indio_dev); + ret =iio_push_to_buffers_with_ts (indio_dev, info->buffer, +sizeof (info->buffer), + timestamp); +if (ret <0 && ret != -EBUSY ) +dev_err_ratelimited (&indio_dev->dev, +"failed to push iio buffer: %d" , + ret); + } + + dma_buf->tail = dma_buf->head; +out: +dma_sync_single_for_device (dev_dma, info->rx_dma_buf, +NXP_SAR_ADC_DMA_BUFF_SZ ,DMA_FROM_DEVICE ); +} +static int nxp_sar_adc_dma_probe (struct device *dev,struct nxp_sar_adc *info) +{
drivers/iio/adc/nxp-sar-adc.c
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 =dmam_alloc_coherent (dev_dma,NXP_SAR_ADC_DMA_BUFF_SZ , + &info->rx_dma_buf,GFP_KERNEL );
Is this setting up the right life time? Superficially it looks to be associating the buffer lifetime with a device related to the dma engine rather than the device we are dealing with here.
This particular pattern with devm_dma_request_chan() is vanishingly rare so not much prior art to rely on.
If the info->dma_chan->device->dev is instantiated by devm_dma_request_chan() and hence torn down as that is unwound it will be fine as this is simply nested devm handling, but it seems a struct dma_device has many chans so I think that isn't the case.
Given that device parameter is also needed for the buffer allocation and making sure we have the right properties / iommu magic etc, I'm not sure how to make this work. One option would be to use dma_alloc_coherent() and tear down with a devm_add_action_or_reset() handler on dev rather than dev_dma.
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 0 ; +}
Vinod Koul
maintainerRe: [PATCH v5 2/2] iio: adc: Add the NXP SAR ADC support for the s32g2/3 platforms
Quoted 38 lines · Jonathan Cameron
Quoted 25 lines · On Fri, 17 Oct 2025 18:42:38 +0200 Daniel Lezcano <[email protected]>…
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.
Quoted 9 lines
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 7 lines
Hi Daniel,
Only significant question in here is around lifetimes of the dma buffer.
+CC Vinod and dmaengine list. Hopefully someone will rapidly tell me my concern is garbage ;)
Thanks for looping me in
Quoted 96 lines
IIO folk who are familiar with dmaengine channels etc please take a look at this as well. I think all the upstream drivers we have doing similar things to this predate
devm_management being a common thing.Jonathan
drivers/iio/adc/nxp-sar-adc.c line 579
Quoted 12 lines
drivers/iio/adc/nxp-sar-adc.c new file +5
-0@@ -0,0 +1,1006 @@ + +static void nxp_sar_adc_dma_cb (void *data) +{ +struct nxp_sar_adc *info =iio_priv (data); +struct iio_dev *indio_dev = data;Trivial but it would slightly more intuitive to do.
struct iio_dev *indio_dev = data; struct nxp_sar_adc *info = iio_priv(indio_dev);Quoted 36 lines
drivers/iio/adc/nxp-sar-adc.c new file +36
-0+struct dma_tx_state state; +struct circ_buf *dma_buf; +struct device *dev_dma; +u32 *dma_samples; +s64 timestamp; +int idx, ret; + +guard (spinlock_irqsave)(&info->lock); + + dma_buf = &info->dma_buf; + dma_samples = (u32 *)dma_buf->buf; + dev_dma = info->dma_chan->device->dev; + +dmaengine_tx_status (info->dma_chan, info->cookie, &state); + +dma_sync_single_for_cpu (dev_dma, info->rx_dma_buf, +NXP_SAR_ADC_DMA_BUFF_SZ ,DMA_FROM_DEVICE ); + +/* Current head position. */ + dma_buf->head = (NXP_SAR_ADC_DMA_BUFF_SZ - state.residue) / +NXP_SAR_ADC_DMA_SAMPLE_SZ ; + +/* If everything was transferred, avoid an off by one error. */ +if (!state.residue) + dma_buf->head--; + +/* Something went wrong and nothing transferred. */ +if (state.residue ==NXP_SAR_ADC_DMA_BUFF_SZ ) + goto out; + +/* Make sure that head is multiple of info->channels_used. */ + dma_buf->head -= dma_buf->head % info->channels_used; + +/* +* dma_buf->tail != dma_buf->head condition will become false +* because dma_buf->tail will be incremented with 1. drivers/iio/adc/nxp-sar-adc.c line 625
Quoted 10 lines
drivers/iio/adc/nxp-sar-adc.c new file +10
-0+*/ +while (dma_buf->tail != dma_buf->head) { + idx = dma_buf->tail % info->channels_used; + info->buffer[idx] = dma_samples[dma_buf->tail]; + dma_buf->tail = (dma_buf->tail +1 ) %NXP_SAR_ADC_DMA_SAMPLE_CNT ; +if (idx != info->channels_used -1 ) +continue ; + +/* +* iio_push_to_buffers_with_timestamp should not be Comment needs an update as using
with_ts()Quoted 18 lines
drivers/iio/adc/nxp-sar-adc.c new file +18
-0+* called with dma_samples as parameter. The samples +* will be smashed if timestamp is enabled. +*/ + timestamp =iio_get_time_ns (indio_dev); + ret =iio_push_to_buffers_with_ts (indio_dev, info->buffer, +sizeof (info->buffer), + timestamp); +if (ret <0 && ret != -EBUSY ) +dev_err_ratelimited (&indio_dev->dev, +"failed to push iio buffer: %d" , + ret); + } + + dma_buf->tail = dma_buf->head; +out: +dma_sync_single_for_device (dev_dma, info->rx_dma_buf, +NXP_SAR_ADC_DMA_BUFF_SZ ,DMA_FROM_DEVICE ); +}
Quoted 16 lines
drivers/iio/adc/nxp-sar-adc.c line 845
Quoted 12 lines
drivers/iio/adc/nxp-sar-adc.c new file +12
-0+static int nxp_sar_adc_dma_probe (struct device *dev,struct nxp_sar_adc *info) +{ +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 =dmam_alloc_coherent (dev_dma,NXP_SAR_ADC_DMA_BUFF_SZ , + &info->rx_dma_buf,GFP_KERNEL );Is this setting up the right life time? Superficially it looks to be associating the buffer lifetime with a device related to the dma engine rather than the device we are dealing with here.
Absolutely! the buffer ought to be mapped always with dmaengine device. The transaction are performed by the dmaengine device and that is why mapping should always be done with dmaengine device, it will not work otherwise.
Quoted 2 lines
This particular pattern with
devm_dma_request_chan()is vanishingly rare so not much prior art to rely on.
I personally do not like that, dmaengine channel is a shared resource and it should be grabbed at runtime and freed up after use. Ideally would be good if we grab the channel when we need i
But I know people like to grab channels at startup :-(
Quoted 18 lines
If the
info->dma_chan->device->devis instantiated bydevm_dma_request_chan()and hence torn down as that is unwound it will be fine as this is simply nested devm handling, but it seems a structdma_devicehas many chans so I think that isn't the case.Given that device parameter is also needed for the buffer allocation and making sure we have the right properties / iommu magic etc, I'm not sure how to make this work. One option would be to use
dma_alloc_coherent()and tear down with adevm_add_action_or_reset()handler on dev rather thandev_dma.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 0 ; +}
Signature · 1 line
~Vinod
Jonathan Cameron
maintainerRe: [PATCH v5 2/2] iio: adc: Add the NXP SAR ADC support for the s32g2/3 platforms
Quoted 161 lines · On Fri, 7 Nov 2025 12:15:31 +0100 Vinod Koul <[email protected]> wrote:
Quoted 38 lines · Jonathan Cameron
Quoted 25 lines · On Fri, 17 Oct 2025 18:42:38 +0200 Daniel Lezcano <[email protected]>…
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.
Quoted 9 lines
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 7 lines
Hi Daniel,
Only significant question in here is around lifetimes of the dma buffer.
+CC Vinod and dmaengine list. Hopefully someone will rapidly tell me my concern is garbage ;)
Thanks for looping me in
Quoted 96 lines
IIO folk who are familiar with dmaengine channels etc please take a look at this as well. I think all the upstream drivers we have doing similar things to this predate
devm_management being a common thing.Jonathan
drivers/iio/adc/nxp-sar-adc.c line 579
Quoted 12 lines
drivers/iio/adc/nxp-sar-adc.c new file +5
-0@@ -0,0 +1,1006 @@ + +static void nxp_sar_adc_dma_cb (void *data) +{ +struct nxp_sar_adc *info =iio_priv (data); +struct iio_dev *indio_dev = data;Trivial but it would slightly more intuitive to do.
struct iio_dev *indio_dev = data; struct nxp_sar_adc *info = iio_priv(indio_dev);Quoted 36 lines
drivers/iio/adc/nxp-sar-adc.c new file +36
-0+struct dma_tx_state state; +struct circ_buf *dma_buf; +struct device *dev_dma; +u32 *dma_samples; +s64 timestamp; +int idx, ret; + +guard (spinlock_irqsave)(&info->lock); + + dma_buf = &info->dma_buf; + dma_samples = (u32 *)dma_buf->buf; + dev_dma = info->dma_chan->device->dev; + +dmaengine_tx_status (info->dma_chan, info->cookie, &state); + +dma_sync_single_for_cpu (dev_dma, info->rx_dma_buf, +NXP_SAR_ADC_DMA_BUFF_SZ ,DMA_FROM_DEVICE ); + +/* Current head position. */ + dma_buf->head = (NXP_SAR_ADC_DMA_BUFF_SZ - state.residue) / +NXP_SAR_ADC_DMA_SAMPLE_SZ ; + +/* If everything was transferred, avoid an off by one error. */ +if (!state.residue) + dma_buf->head--; + +/* Something went wrong and nothing transferred. */ +if (state.residue ==NXP_SAR_ADC_DMA_BUFF_SZ ) + goto out; + +/* Make sure that head is multiple of info->channels_used. */ + dma_buf->head -= dma_buf->head % info->channels_used; + +/* +* dma_buf->tail != dma_buf->head condition will become false +* because dma_buf->tail will be incremented with 1. drivers/iio/adc/nxp-sar-adc.c line 625
Quoted 10 lines
drivers/iio/adc/nxp-sar-adc.c new file +10
-0+*/ +while (dma_buf->tail != dma_buf->head) { + idx = dma_buf->tail % info->channels_used; + info->buffer[idx] = dma_samples[dma_buf->tail]; + dma_buf->tail = (dma_buf->tail +1 ) %NXP_SAR_ADC_DMA_SAMPLE_CNT ; +if (idx != info->channels_used -1 ) +continue ; + +/* +* iio_push_to_buffers_with_timestamp should not be Comment needs an update as using
with_ts()Quoted 18 lines
drivers/iio/adc/nxp-sar-adc.c new file +18
-0+* called with dma_samples as parameter. The samples +* will be smashed if timestamp is enabled. +*/ + timestamp =iio_get_time_ns (indio_dev); + ret =iio_push_to_buffers_with_ts (indio_dev, info->buffer, +sizeof (info->buffer), + timestamp); +if (ret <0 && ret != -EBUSY ) +dev_err_ratelimited (&indio_dev->dev, +"failed to push iio buffer: %d" , + ret); + } + + dma_buf->tail = dma_buf->head; +out: +dma_sync_single_for_device (dev_dma, info->rx_dma_buf, +NXP_SAR_ADC_DMA_BUFF_SZ ,DMA_FROM_DEVICE ); +}Quoted 16 lines
drivers/iio/adc/nxp-sar-adc.c line 845
Quoted 12 lines
drivers/iio/adc/nxp-sar-adc.c new file +12
-0+static int nxp_sar_adc_dma_probe (struct device *dev,struct nxp_sar_adc *info) +{ +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 =dmam_alloc_coherent (dev_dma,NXP_SAR_ADC_DMA_BUFF_SZ , + &info->rx_dma_buf,GFP_KERNEL );Is this setting up the right life time? Superficially it looks to be associating the buffer lifetime with a device related to the dma engine rather than the device we are dealing with here.
Quoted 4 lines
Absolutely! the buffer ought to be mapped always with dmaengine device. The transaction are performed by the dmaengine device and that is why mapping should always be done with dmaengine device, it will not work otherwise.
This to me is confusing two things. The mapping establishment (in the dma engine driver) and what driver looks after the mapping/triggers cleaning it up (here the ADC driver). This dmam_alloc_coherent() seems to be allocating stuff in the probe of this driver, but unless more complex management is going on than I think (always possible!) the remove won't happen until he dmaengine driver is unbound - so very much out of order with the tear down in this driver.
That is different from what we'd have if we just had dma_alloc_coherent(dev_dma, ...) in probe
and dma_free_coherent(dev_dma ...) in remove() for this driver.
There are other kernel interfaces that provide a management related struct device and a struct device needed to perform the operation. Seems like the same should be true here as they don't seem to be the same struct device.
Jonathan
Quoted 29 lines
Quoted 3 lines
This particular pattern with
devm_dma_request_chan()is vanishingly rare so not much prior art to rely on.I personally do not like that, dmaengine channel is a shared resource and it should be grabbed at runtime and freed up after use. Ideally would be good if we grab the channel when we need i
But I know people like to grab channels at startup :-(
Quoted 18 lines
If the
info->dma_chan->device->devis instantiated bydevm_dma_request_chan()and hence torn down as that is unwound it will be fine as this is simply nested devm handling, but it seems a structdma_devicehas many chans so I think that isn't the case.Given that device parameter is also needed for the buffer allocation and making sure we have the right properties / iommu magic etc, I'm not sure how to make this work. One option would be to use
dma_alloc_coherent()and tear down with adevm_add_action_or_reset()handler on dev rather thandev_dma.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 0 ; +}