iio: adc: ad7768-1: add support for SPI offload
Subsystems (3)
cover iio: adc: ad7768-1: add support for SPI offload
This series adds SPI offload support to the AD7768-1 driver and includes two critical fixes for one-shot mode operation discovered during testing.
Jonathan Santos (3):
iio: adc: ad7768-1: fix one-shot mode data acquisition
iio: adc: ad7768-1: prevent one-shot mode with wideband filter
iio: adc: ad7768-1: add support for SPI offload
drivers/iio/adc/Kconfig | 2 + drivers/iio/adc/ad7768-1.c | 211 +++++++++++++++++++++++++++++++++++-- 2 files changed, 202 insertions(+), 11 deletions(-)
base-commit: d820183f371d9aa8517a1cd21fe6edacf0f94b7f
Signature · 1 line
2.34.1
PATCH 1/3 iio: adc: ad7768-1: fix one-shot mode data acquisition
According to the datasheet, one-shot mode requires a SYNC_IN pulse to trigger a new sample conversion. In the current implementation, No sync pulse was sent after switching to one-shot mode and reinit_completion() was called before mode switching, creating a race condition where spurious interrupts during mode change could trigger completion prematurely.
Fix by sending a sync pulse after configuring one-shot mode and moving reinit_completion() after the pulse to ensure it only waits for the actual conversion completion.
--- drivers/iio/adc/ad7768-1.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/iio/adc/ad7768-1.c +7 -2
@@ -463,12 +463,17 @@ static int ad7768_scan_direct(struct iio_dev *indio_dev)struct ad7768_state *st =iio_priv (indio_dev);int readval, ret; -reinit_completion (&st->completion); - ret =ad7768_set_mode (st,AD7768_ONE_SHOT );if (ret <0 )return ret; +/* One-shot mode requires a SYNC pulse to generate a new sample */ + ret =ad7768_send_sync_pulse (st); +if (ret) +return ret; + +reinit_completion (&st->completion); + ret =wait_for_completion_timeout (&st->completion,msecs_to_jiffies (1000 ));if (!ret)
Signature · 1 line
2.34.1
Jonathan Cameron
maintainerRe: [PATCH 1/3] iio: adc: ad7768-1: fix one-shot mode data acquisition
Quoted 9 lines · On Sat, 31 Jan 2026 22:35:20 -0300 Jonathan Santos…
According to the datasheet, one-shot mode requires a
SYNC_INpulse to trigger a new sample conversion. In the current implementation, No sync pulse was sent after switching to one-shot mode andreinit_completion()was called before mode switching, creating a race condition where spurious interrupts during mode change could trigger completion prematurely.Fix by sending a sync pulse after configuring one-shot mode and moving
reinit_completion()after the pulse to ensure it only waits for the actual conversion completion.
That smells like a race...
What stops us taking a long break between ending that sync pulse and the reinit_completition() such that we have initialized if before the complete()?
You always have to do it before what ever ultimately makes the complete() happen. Have you seen an sign of the spurious interrupts or is that more a conjecture of what might happen?
Jonathan
Quoted 31 lines
Fixes a5f8c7da3dbe ("iio: adc: Add AD7768-1 ADC basic support")Signed-off-by Jonathan Santos <[email protected]>--- drivers/iio/adc/ad7768-1.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)drivers/iio/adc/ad7768-1.c +7
-2@@ -463,12 +463,17 @@ static int ad7768_scan_direct(struct iio_dev *indio_dev)struct ad7768_state *st =iio_priv (indio_dev);int readval, ret; -reinit_completion (&st->completion); - ret =ad7768_set_mode (st,AD7768_ONE_SHOT );if (ret <0 )return ret; +/* One-shot mode requires a SYNC pulse to generate a new sample */ + ret =ad7768_send_sync_pulse (st); +if (ret) +return ret; + +reinit_completion (&st->completion); + ret =wait_for_completion_timeout (&st->completion,msecs_to_jiffies (1000 ));if (!ret)
Re: [PATCH 1/3] iio: adc: ad7768-1: fix one-shot mode data acquisition
Quoted 9 lines · Jonathan Cameron
Quoted 6 lines · On Sat, 31 Jan 2026 22:35:20 -0300 Jonathan Santos…
According to the datasheet, one-shot mode requires a
SYNC_INpulse to trigger a new sample conversion. In the current implementation, No sync pulse was sent after switching to one-shot mode andreinit_completion()was called before mode switching, creating a race condition where spurious interrupts during mode change could trigger completion prematurely.
Quoted 9 lines
Quoted 3 lines
Fix by sending a sync pulse after configuring one-shot mode and moving
reinit_completion()after the pulse to ensure it only waits for the actual conversion completion.That smells like a race...
What stops us taking a long break between ending that sync pulse and the
reinit_completition()such that we have initialized if before thecomplete()?
Yes, you are right, we have a race here. The inital problem was having the ad7768_set_mode() before the reinit_completion() because, almost everytime, a new conversion is done before setting the one-shot mode, so the complete() happens before the expected trigger (that never happened because ad7768_send_sync_pulse() was not present).
Quoted 4 lines
You always have to do it before what ever ultimately makes the
complete()happen. Have you seen an sign of the spurious interrupts or is that more a conjecture of what might happen?
Agreed, I can put reinit_completion() before sending the sync pulse to avoid this situation. But maybe it is better just to remove the one-shot mode as discussed in the patch 2/3. What do you think?
Quoted 33 lines
Jonathan
Quoted 31 lines
Fixes a5f8c7da3dbe ("iio: adc: Add AD7768-1 ADC basic support")Signed-off-by Jonathan Santos <[email protected]>--- drivers/iio/adc/ad7768-1.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)drivers/iio/adc/ad7768-1.c +7
-2@@ -463,12 +463,17 @@ static int ad7768_scan_direct(struct iio_dev *indio_dev)struct ad7768_state *st =iio_priv (indio_dev);int readval, ret; -reinit_completion (&st->completion); - ret =ad7768_set_mode (st,AD7768_ONE_SHOT );if (ret <0 )return ret; +/* One-shot mode requires a SYNC pulse to generate a new sample */ + ret =ad7768_send_sync_pulse (st); +if (ret) +return ret; + +reinit_completion (&st->completion); + ret =wait_for_completion_timeout (&st->completion,msecs_to_jiffies (1000 ));if (!ret)
Jonathan Cameron
maintainerRe: [PATCH 1/3] iio: adc: ad7768-1: fix one-shot mode data acquisition
Quoted 26 lines · On Mon, 9 Feb 2026 18:38:31 -0300 Jonathan Santos <[email protected]> wrote:
Quoted 9 lines · Jonathan Cameron
Quoted 6 lines · On Sat, 31 Jan 2026 22:35:20 -0300 Jonathan Santos…
According to the datasheet, one-shot mode requires a
SYNC_INpulse to trigger a new sample conversion. In the current implementation, No sync pulse was sent after switching to one-shot mode andreinit_completion()was called before mode switching, creating a race condition where spurious interrupts during mode change could trigger completion prematurely.Quoted 9 lines
Quoted 3 lines
Fix by sending a sync pulse after configuring one-shot mode and moving
reinit_completion()after the pulse to ensure it only waits for the actual conversion completion.That smells like a race...
What stops us taking a long break between ending that sync pulse and the
reinit_completition()such that we have initialized if before thecomplete()?Yes, you are right, we have a race here. The inital problem was having the
ad7768_set_mode()before thereinit_completion()because, almost everytime, a new conversion is done before setting the one-shot mode, so thecomplete()happens before the expected trigger (that never happened becausead7768_send_sync_pulse()was not present).
Quoted 8 lines
Quoted 4 lines
You always have to do it before what ever ultimately makes the
complete()happen. Have you seen an sign of the spurious interrupts or is that more a conjecture of what might happen?Agreed, I can put
reinit_completion()before sending the sync pulse to avoid this situation. But maybe it is better just to remove the one-shot mode as discussed in the patch 2/3. What do you think?
I don't have a strong view either way as the trade offs of using continuous type modes for oneshot and not tend to be rather driver and usecase specific. So I'll leave that decision to the driver author and anyone else who cares about a particular part.
Fix wise you probably want the minor change of adding the reinit before the pulse as that gives us something small to backport, even if you then rip the code out again.
Thanks,
Jonathan
Quoted 34 lines
Quoted 34 lines
Jonathan
Quoted 31 lines
Fixes a5f8c7da3dbe ("iio: adc: Add AD7768-1 ADC basic support")Signed-off-by Jonathan Santos <[email protected]>--- drivers/iio/adc/ad7768-1.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)drivers/iio/adc/ad7768-1.c +7
-2@@ -463,12 +463,17 @@ static int ad7768_scan_direct(struct iio_dev *indio_dev)struct ad7768_state *st =iio_priv (indio_dev);int readval, ret; -reinit_completion (&st->completion); - ret =ad7768_set_mode (st,AD7768_ONE_SHOT );if (ret <0 )return ret; +/* One-shot mode requires a SYNC pulse to generate a new sample */ + ret =ad7768_send_sync_pulse (st); +if (ret) +return ret; + +reinit_completion (&st->completion); + ret =wait_for_completion_timeout (&st->completion,msecs_to_jiffies (1000 ));if (!ret)
PATCH 2/3 iio: adc: ad7768-1: prevent one-shot mode with wideband filter
In reply to Jonathan SantosThe AD7768-1 datasheet specifies that the wideband low ripple FIR filter is only available in continuous conversion mode and should not be used with one-shot mode to avoid malfunction and incorrect data.
Add filter type checks in ad7768_scan_direct() to skip one-shot mode switching when wideband filter is configured.
--- drivers/iio/adc/ad7768-1.c | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-)
drivers/iio/adc/ad7768-1.c +15 -10
@@ -463,14 +463,17 @@ static int ad7768_scan_direct(struct iio_dev *indio_dev)struct ad7768_state *st =iio_priv (indio_dev);int readval, ret; - ret =ad7768_set_mode (st,AD7768_ONE_SHOT ); -if (ret <0 ) -return ret; +/* Wideband filter is not available in One-Shot conversion mode */ +if (st->filter_type !=AD7768_FILTER_WIDEBAND ) { + ret =ad7768_set_mode (st,AD7768_ONE_SHOT ); +if (ret <0 ) +return ret; -/* One-shot mode requires a SYNC pulse to generate a new sample */ - ret =ad7768_send_sync_pulse (st); -if (ret) -return ret; +/* One-shot mode requires a SYNC pulse to generate a new sample */ + ret =ad7768_send_sync_pulse (st); +if (ret) +return ret; + }reinit_completion (&st->completion);
@@ -496,9 +499,11 @@ static int ad7768_scan_direct(struct iio_dev *indio_dev)* Any SPI configuration of the AD7768-1 can only be * performed in continuous conversion mode. */ - ret =ad7768_set_mode (st,AD7768_CONTINUOUS ); -if (ret <0 ) -return ret; +if (st->filter_type !=AD7768_FILTER_WIDEBAND ) { + ret =ad7768_set_mode (st,AD7768_CONTINUOUS ); +if (ret <0 ) +return ret; + }return readval; }
Signature · 1 line
2.34.1
Re: [PATCH 2/3] iio: adc: ad7768-1: prevent one-shot mode with wideband filter
Quoted 43 lines · Jonathan Santos
The AD7768-1 datasheet specifies that the wideband low ripple FIR filter is only available in continuous conversion mode and should not be used with one-shot mode to avoid malfunction and incorrect data.
Add filter type checks in
ad7768_scan_direct()to skip one-shot mode switching when wideband filter is configured.Signed-off-by Jonathan Santos <[email protected]>---
drivers/iio/adc/ad7768-1.c | 25 +++++++++++++++----------1 file changed, 15 insertions(+), 10 deletions(-)drivers/iio/adc/ad7768-1.c +0
-0@@ -463,14 +463,17 @@ static int ad7768_scan_direct(struct iio_dev *indio_dev) struct ad7768_state *st = iio_priv(indio_dev); int readval, ret;drivers/iio/adc/ad7768-1.c +5
-3- ret =ad7768_set_mode (st,AD7768_ONE_SHOT ); -if (ret <0 ) -return ret; +/* Wideband filter is not available in One-Shot conversion mode */ +if (st->filter_type !=AD7768_FILTER_WIDEBAND ) { + ret =ad7768_set_mode (st,AD7768_ONE_SHOT ); +if (ret <0 ) +return ret;drivers/iio/adc/ad7768-1.c +5
-4-/* One-shot mode requires a SYNC pulse to generate a new sample */ - ret =ad7768_send_sync_pulse (st); -if (ret) -return ret; +/* One-shot mode requires a SYNC pulse to generate a new sample */ + ret =ad7768_send_sync_pulse (st); +if (ret) +return ret; + }
reinit_completion(&st->completion);drivers/iio/adc/ad7768-1.c +0
-0@@ -496,9 +499,11 @@ static int ad7768_scan_direct(struct iio_dev *indio_dev) * Any SPI configuration of the AD7768-1 can only be
drivers/iio/adc/ad7768-1.c static int ad7768_scan_direct(struct iio_dev *indio_dev) · line 506
Quoted 10 lines
drivers/iio/adc/ad7768-1.c +5
-3* performed in continuous conversion mode. */ - ret =ad7768_set_mode (st,AD7768_CONTINUOUS ); -if (ret <0 ) -return ret; +if (st->filter_type !=AD7768_FILTER_WIDEBAND ) { + ret =ad7768_set_mode (st,AD7768_CONTINUOUS ); +if (ret <0 ) +return ret; + }
So the idea is to still have continuous mode running and get the latest sample after we do reinit_completion()? If that's the case, why do we have the one shot logic? Does it bring that much added value? Asking because I'm just wondering we could just let it in continuous mode all the time.
This also sounds like a fix so maybe a Fixes tag?
- Nuno Sá
Re: [PATCH 2/3] iio: adc: ad7768-1: prevent one-shot mode with wideband filter
Quoted 52 lines · Nuno Sá
Quoted 51 lines · Jonathan Santos
The AD7768-1 datasheet specifies that the wideband low ripple FIR filter is only available in continuous conversion mode and should not be used with one-shot mode to avoid malfunction and incorrect data.
Add filter type checks in
ad7768_scan_direct()to skip one-shot mode switching when wideband filter is configured.Signed-off-by Jonathan Santos <[email protected]>---
drivers/iio/adc/ad7768-1.c | 25 +++++++++++++++----------1 file changed, 15 insertions(+), 10 deletions(-)drivers/iio/adc/ad7768-1.c +0
-0@@ -463,14 +463,17 @@ static int ad7768_scan_direct(struct iio_dev *indio_dev) struct ad7768_state *st = iio_priv(indio_dev); int readval, ret;drivers/iio/adc/ad7768-1.c +5
-3- ret =ad7768_set_mode (st,AD7768_ONE_SHOT ); -if (ret <0 ) -return ret; +/* Wideband filter is not available in One-Shot conversion mode */ +if (st->filter_type !=AD7768_FILTER_WIDEBAND ) { + ret =ad7768_set_mode (st,AD7768_ONE_SHOT ); +if (ret <0 ) +return ret;drivers/iio/adc/ad7768-1.c +5
-4-/* One-shot mode requires a SYNC pulse to generate a new sample */ - ret =ad7768_send_sync_pulse (st); -if (ret) -return ret; +/* One-shot mode requires a SYNC pulse to generate a new sample */ + ret =ad7768_send_sync_pulse (st); +if (ret) +return ret; + }
reinit_completion(&st->completion);drivers/iio/adc/ad7768-1.c +3
-3@@ -496,9 +499,11 @@ static int ad7768_scan_direct(struct iio_dev *indio_dev)* Any SPI configuration of the AD7768-1 can only be * performed in continuous conversion mode. */ - ret =ad7768_set_mode (st,AD7768_CONTINUOUS ); -if (ret <0 ) -return ret; +if (st->filter_type !=AD7768_FILTER_WIDEBAND ) { + ret =ad7768_set_mode (st,AD7768_CONTINUOUS ); +if (ret <0 )
Quoted 8 lines
drivers/iio/adc/ad7768-1.c static int ad7768_scan_direct(struct iio_dev *indio_dev) · line 506
Quoted 2 lines
drivers/iio/adc/ad7768-1.c +2
-0+return ret; + }So the idea is to still have continuous mode running and get the latest sample after we do
reinit_completion()? If that's the case, why do we have the one shot logic? Does it bring that much added value? Asking because I'm just wondering we could just let it in continuous mode all the time.
The concern on having continuous conversion mode all the time was that sometimes, especially at higher sampling rates, a new conversion (DRDY) occurs while the controller is still reading the ADC_DATA register. I was initially worried the data register might be refreshed mid-transfer. However, after reviewing the datasheet more carefully, that does not appear to be the case. As the ADAQ7768-1 datasheet on page 76 states:
"The register driving DOUT resets by the last SCLK rising edge in an SPI read frame, or a rising CS edge, whichever occurs first. -> The number of SCLKs required to reset the SPI interface depends on the read configuration. The 16th rising SCLK edge resets the SPI interface for a normal read operation and up to 40 SCLKs when reading back ADC conversion data, plus the status and CRC headers."
Then I guess we could just remove the one-shot mode.
Quoted 4 lines
This also sounds like a fix so maybe a Fixes tag?
- Nuno Sá
PATCH 3/3 iio: adc: ad7768-1: add support for SPI offload
In reply to Jonathan SantosThe AD7768-1 family supports sampling rates up to 1 MSPS, which exceeds the capabilities of conventional triggered buffer operations due to SPI transaction overhead and interrupt latency.
Add SPI offload support to enable hardware-accelerated data acquisition that bypasses software SPI transactions using continuous data streaming.
--- drivers/iio/adc/Kconfig | 2 + drivers/iio/adc/ad7768-1.c | 187 ++++++++++++++++++++++++++++++++++++- 2 files changed, 185 insertions(+), 4 deletions(-)
drivers/iio/adc/Kconfig +2 -0
@@ -413,8 +413,10 @@ config AD7768_1 select REGMAP_SPI select RATIONAL select IIO_BUFFER + select IIO_BUFFER_DMAENGINE select IIO_TRIGGER select IIO_TRIGGERED_BUFFER + select SPI_OFFLOAD help Say yes here to build support for Analog Devices AD7768-1 SPI simultaneously sampling sigma-delta analog to digital converter (ADC).
drivers/iio/adc/ad7768-1.c +183 -4
261 lines not on this page · raw message
Signature · 1 line
2.34.1
Quoted 6 lines · Jonathan Santos
The AD7768-1 family supports sampling rates up to 1 MSPS, which exceeds the capabilities of conventional triggered buffer operations due to SPI transaction overhead and interrupt latency.
Add SPI offload support to enable hardware-accelerated data acquisition that bypasses software SPI transactions using continuous data streaming.
...
drivers/iio/adc/ad7768-1.c static int ad7768_get_current_scan_type(const struct iio_dev *indio_dev, · line 1166
Quoted 3 lines
drivers/iio/adc/ad7768-1.c +3
-0+if (st->offload_en) +return st->oversampling_ratio ==8 ? +AD7768_SCAN_TYPE_OFFLOAD_HIGH_SPEED :AD7768_SCAN_TYPE_OFFLOAD_NORMAL ;
Wrong indentation.
...
drivers/iio/adc/ad7768-1.c static const struct iio_buffer_setup_ops ad7768_buffer_ops = { · line 1423
Quoted 3 lines
drivers/iio/adc/ad7768-1.c +3
-0+ ret =spi_offload_trigger_enable (st->offload, + st->offload_trigger, + &config);
One of the parameters can be moved to the previous line (either 2nd or 3rd).
drivers/iio/adc/ad7768-1.c static const struct iio_buffer_setup_ops ad7768_buffer_ops = { · line 1429
Quoted 7 lines
drivers/iio/adc/ad7768-1.c +5
-0+if (ret) { +spi_unoptimize_message (&st->offload_msg); +return ret; + } +return 0 ;
if (ret) spi_unoptimize_message(&st->offload_msg); return ret;
?
...
drivers/iio/adc/ad7768-1.c static int ad7768_parse_aaf_gain(struct device *dev, struct ad7768_state *st) · line 1693
Quoted 8 lines
drivers/iio/adc/ad7768-1.c +8
-0+static bool ad7768_offload_trigger_match (struct spi_offload_trigger *trigger, +enum spi_offload_trigger_type type, +u64 *args,u32 nargs) +{ +if (type !=SPI_OFFLOAD_TRIGGER_DATA_READY ) +return false; + +/* Requires 1 arg to indicate the trigger output signal */
^^^ for the context to below remark.
drivers/iio/adc/ad7768-1.c static int ad7768_parse_aaf_gain(struct device *dev, struct ad7768_state *st) · line 1704
Quoted 11 lines
drivers/iio/adc/ad7768-1.c +11
-0+if (nargs !=1 || args[0 ] !=AD7768_TRIGGER_SOURCE_DRDY ) +return false; + +return true; +} + +static int ad7768_offload_trigger_request (struct spi_offload_trigger *trigger, +enum spi_offload_trigger_type type, +u64 *args,u32 nargs) +{ +/* Should already be validated by match, but just in case. */
Please, be consistent with a style for one-line comments (pay attention to the period at the end and capitalisation of the sentence). Choose one (whatever you prefer or which one is already used) style and follow it everywhere.
Quoted 5 lines
drivers/iio/adc/ad7768-1.c +5
-0+if (nargs !=1 ) +return -EINVAL ; + +return 0 ; +}
...
drivers/iio/adc/ad7768-1.c static const struct ad7768_chip_info adaq7769_chip_info = { · line 1755
Quoted 3 lines
drivers/iio/adc/ad7768-1.c +3
-0+static const struct spi_offload_config ad7768_spi_offload_config = { + .capability_flags =SPI_OFFLOAD_CAP_TRIGGER | +SPI_OFFLOAD_CAP_RX_STREAM_DMA ,
It can be one line out of 84 characters.
Quoted 1 line
+};
...
Quoted 6 lines
drivers/iio/adc/ad7768-1.c +6
-0+static int ad7768_spi_offload_probe (struct iio_dev *indio_dev, +struct ad7768_state *st) +{ +struct device *dev = &st->spi->dev; +struct spi_offload_trigger_info trigger_info = { + .fwnode =dev_fwnode (dev),
drivers/iio/adc/ad7768-1.c static const struct ad7768_chip_info adaq7769_chip_info = { · line 1773
Quoted 10 lines
drivers/iio/adc/ad7768-1.c +10
-0+ .ops = &ad7768_offload_trigger_ops, + .priv = st, + }; +struct dma_chan *rx_dma; +int ret; + + ret =devm_spi_offload_trigger_register (dev, &trigger_info); +if (ret) +return dev_err_probe (dev, ret, +"failed to register offload trigger\n" );
Can be one line, checkpatch for more than 10 years do not complain on the long string literals (even in --strict mode). Yes, not all lines can follow this, I pointed out this one, because it's okay balance between 80 limit and the overhead.
drivers/iio/adc/ad7768-1.c static const struct ad7768_chip_info adaq7769_chip_info = { · line 1776
Quoted 3 lines
drivers/iio/adc/ad7768-1.c +3
-0+ + st->offload_trigger =devm_spi_offload_trigger_get (dev, st->offload, +SPI_OFFLOAD_TRIGGER_DATA_READY );
Something wrong with the indentation at least second time. Please, review the code and make sure it indents accordingly.
drivers/iio/adc/ad7768-1.c static const struct ad7768_chip_info adaq7769_chip_info = { · line 1787
Quoted 11 lines
drivers/iio/adc/ad7768-1.c +11
-0+if (IS_ERR (st->offload_trigger)) +return dev_err_probe (dev,PTR_ERR (st->offload_trigger), +"failed to get offload trigger\n" ); + + rx_dma =devm_spi_offload_rx_stream_request_dma_chan (dev, st->offload); +if (IS_ERR (rx_dma)) +return dev_err_probe (dev,PTR_ERR (rx_dma), +"failed to get offload RX DMA\n" ); + + ret =devm_iio_dmaengine_buffer_setup_with_handle (dev, indio_dev, + rx_dma,IIO_BUFFER_DIRECTION_IN );
Indentation.
Quoted 9 lines
drivers/iio/adc/ad7768-1.c +9
-0+if (ret) +return dev_err_probe (dev,PTR_ERR (rx_dma), +"failed to setup offload RX DMA\n" ); + + indio_dev->setup_ops = &ad7768_offload_buffer_ops; + st->offload_en = true; + +return 0 ; +}
...
drivers/iio/adc/ad7768-1.c static int ad7768_probe(struct spi_device *spi) · line 1900
Quoted 4 lines
drivers/iio/adc/ad7768-1.c +4
-0+ st->offload =devm_spi_offload_get (&spi->dev, spi, &ad7768_spi_offload_config); + ret =PTR_ERR_OR_ZERO (st->offload); +if (ret && ret != -ENODEV ) +return dev_err_probe (&spi->dev, ret,"failed to get SPI offload\n" );
Add
struct device *dev = &spi->dev;
at the top of the function and make some lines shorter and easier to follow.
drivers/iio/adc/ad7768-1.c static int ad7768_probe(struct spi_device *spi) · line 1903
Quoted 2 lines
drivers/iio/adc/ad7768-1.c +2
-0+/* If not using SPI offload, fall back to low speed usage. */ +if (ret == -ENODEV ) {
No need to check for ENODEV twice.
if (ret == -ENODEV) {
...
} else if (ret) {
return dev_err_probe(...);
} else {
...
}Quoted 8 lines
drivers/iio/adc/ad7768-1.c +8
-0+ ret =ad7768_triggered_buffer_alloc (indio_dev); +if (ret) +return ret; + }else { + ret =ad7768_spi_offload_probe (indio_dev, st); +if (ret) +return ret; + }
Signature · 2 lines
With Best Regards, Andy Shevchenko
David Lechner
reviewerQuoted 9 lines · Jonathan Santos
The AD7768-1 family supports sampling rates up to 1 MSPS, which exceeds the capabilities of conventional triggered buffer operations due to SPI transaction overhead and interrupt latency.
Add SPI offload support to enable hardware-accelerated data acquisition that bypasses software SPI transactions using continuous data streaming.
Signed-off-by Jonathan Santos <[email protected]>---
...
Quoted 8 lines
struct
ad7768_state{drivers/iio/adc/ad7768-1.c +2
-0struct spi_device *spi; +struct spi_offload *offload; +struct spi_offload_trigger *offload_trigger;struct regmap *regmap;struct regmap *regmap24;int vref_uv;
drivers/iio/adc/ad7768-1.c struct ad7768_state { · line 332
Quoted 8 lines
drivers/iio/adc/ad7768-1.c +3
-0@@ -306,8 +325,11 @@ struct ad7768_state {struct gpio_desc *gpio_reset;const char *labels[AD7768_MAX_CHANNELS ];struct gpio_chip gpiochip; +struct spi_transfer offload_xfer; +struct spi_message offload_msg;const struct ad7768_chip_info *chip;bool en_spi_sync; +bool offload_en;
offload_en makes it sound like the offload could be enabled or disabled at runtime, but that doesn't seem to be the case. I would just check if offload != NULL rather than add a new field. Or change this to has_offload.
Quoted 15 lines
drivers/iio/adc/ad7768-1.c +4
-0struct mutex pga_lock;/* protect device internal state (PGA) */ /* * DMA (thus cache coherency maintenance) may require the @@ -1139,6 +1161,10 @@ static int ad7768_get_current_scan_type(const struct iio_dev *indio_dev, {struct ad7768_state *st =iio_priv (indio_dev); +if (st->offload_en) +return st->oversampling_ratio ==8 ? +AD7768_SCAN_TYPE_OFFLOAD_HIGH_SPEED :AD7768_SCAN_TYPE_OFFLOAD_NORMAL ; +return st->oversampling_ratio ==8 ?AD7768_SCAN_TYPE_HIGH_SPEED :AD7768_SCAN_TYPE_NORMAL ; }
drivers/iio/adc/ad7768-1.c static irqreturn_t ad7768_interrupt(int irq, void *dev_id) · line 1349
Quoted 5 lines
drivers/iio/adc/ad7768-1.c +1
-1@@ -1320,7 +1346,7 @@ static irqreturn_t ad7768_interrupt(int irq, void *dev_id)struct iio_dev *indio_dev = dev_id;struct ad7768_state *st =iio_priv (indio_dev); -if (iio_buffer_enabled (indio_dev)) +if (iio_buffer_enabled (indio_dev) && !st->offload_en)
I would expect that the interrupt is just disabled when doing a buffered read with SPI offload. No sense in wasting CPU time. So no special handling should be needed here.
Quoted 11 lines
drivers/iio/adc/ad7768-1.c +4
-0iio_trigger_poll (st->trig); else complete(&st->completion);@@ -1357,6 +1383,72 @@ static const struct iio_buffer_setup_ops ad7768_buffer_ops = { .predisable = &ad7768_buffer_predisable, }; +static int ad7768_offload_buffer_postenable (struct iio_dev *indio_dev) +{ +struct ad7768_state *st =iio_priv (indio_dev); +struct spi_offload_trigger_config config = {
drivers/iio/adc/ad7768-1.c static const struct iio_buffer_setup_ops ad7768_buffer_ops = { · line 1399
Quoted 10 lines
drivers/iio/adc/ad7768-1.c +10
-0+ .type = SPI_OFFLOAD_TRIGGER_DATA_READY, + }; +const struct iio_scan_type *scan_type; +int ret; + + scan_type =iio_get_current_scan_type (indio_dev, &indio_dev->channels[0 ]); +if (IS_ERR (scan_type)) +return PTR_ERR (scan_type); + + st->offload_xfer.len =roundup_pow_of_two (BITS_TO_BYTES (scan_type->realbits));
We have spi_bpw_to_bytes() for this.
Quoted 8 lines
drivers/iio/adc/ad7768-1.c +8
-0+ st->offload_xfer.bits_per_word = scan_type->realbits; + st->offload_xfer.offload_flags =SPI_OFFLOAD_XFER_RX_STREAM ; + +/* +* Write a 1 to the LSB of the INTERFACE_FORMAT register to enter +* continuous read mode. Subsequent data reads do not require an +* initial 8-bit write to query the ADC_DATA register. +*/
drivers/iio/adc/ad7768-1.c static const struct iio_buffer_setup_ops ad7768_buffer_ops = { · line 1417
Quoted 10 lines
drivers/iio/adc/ad7768-1.c +10
-0+ ret =regmap_write (st->regmap,AD7768_REG_INTERFACE_FORMAT ,0x01 ); +if (ret) +return ret; + +spi_message_init_with_transfers (&st->offload_msg, &st->offload_xfer,1 ); + st->offload_msg.offload = st->offload; + + ret =spi_optimize_message (st->spi, &st->offload_msg); +if (ret) { +dev_err (&st->spi->dev,"failed to prepare offload, err: %d\n" , ret);
Do we need to do someting to undo AD7768_REG_INTERFACE_FORMAT write if this fails?
Quoted 14 lines
drivers/iio/adc/ad7768-1.c +14
-0+return ret; + } + + ret =spi_offload_trigger_enable (st->offload, + st->offload_trigger, + &config); +if (ret) { +spi_unoptimize_message (&st->offload_msg); +return ret; + } + +return 0 ; +} +
...
drivers/iio/adc/ad7768-1.c static int ad7768_parse_aaf_gain(struct device *dev, struct ad7768_state *st) · line 1693
Quoted 8 lines
drivers/iio/adc/ad7768-1.c +8
-0+static bool ad7768_offload_trigger_match (struct spi_offload_trigger *trigger, +enum spi_offload_trigger_type type, +u64 *args,u32 nargs) +{ +if (type !=SPI_OFFLOAD_TRIGGER_DATA_READY ) +return false; + +/* Requires 1 arg to indicate the trigger output signal */
The DT bindings allow 1 or 2 args, so we should also allow nargs == 2 even (the 2nd arg is just ignored).
drivers/iio/adc/ad7768-1.c static int ad7768_parse_aaf_gain(struct device *dev, struct ad7768_state *st) · line 1705
Quoted 12 lines
drivers/iio/adc/ad7768-1.c +12
-0+if (nargs !=1 || args[0 ] !=AD7768_TRIGGER_SOURCE_DRDY ) +return false; + +return true; +} + +static int ad7768_offload_trigger_request (struct spi_offload_trigger *trigger, +enum spi_offload_trigger_type type, +u64 *args,u32 nargs) +{ +/* Should already be validated by match, but just in case. */ +if (nargs !=1 )
As above.
Quoted 5 lines
drivers/iio/adc/ad7768-1.c +5
-0+return -EINVAL ; + +return 0 ; +} +
Dan Carpenter
build botBCC: [email protected] CC: [email protected] In-Reply-To: <9f9aedbe374461e48f1f1e64d5487b5b6c1fc992.1769889074.git.Jonathan.Santos@analog.com> References…
BCC: [email protected]
CC: [email protected]
In-Reply-To: <9f9aedbe374461e48f1f1e64d5487b5b6c1fc992.1769889074.git.Jonathan.Santos@analog.com>
References: <9f9aedbe374461e48f1f1e64d5487b5b6c1fc992.1769889074.git.Jonathan.Santos@analog.com>
TO: Jonathan Santos <[email protected]>
TO: [email protected]
TO: [email protected]
CC: Jonathan Santos <[email protected]>
CC: [email protected]
CC: [email protected]
CC: [email protected]
CC: [email protected]
CC: [email protected]
CC: [email protected]
Hi Jonathan,
kernel test robot noticed the following build warnings:
[auto build test WARNING on d820183f371d9aa8517a1cd21fe6edacf0f94b7f]
url: https://github.com/intel-lab-lkp/linux/commits/Jonathan-Santos/iio-adc-ad7768-1-fix-one-shot-mode-data-acquisition/20260204-203950 base: d820183f371d9aa8517a1cd21fe6edacf0f94b7f patch link: https://lore.kernel.org/r/9f9aedbe374461e48f1f1e64d5487b5b6c1fc992.1769889074.git.Jonathan.Santos%40analog.com patch subject: [PATCH 3/3] iio: adc: ad7768-1: add support for SPI offload
:::::: branch date: 16 hours ago
:::::: commit date: 16 hours ago
config: x86_64-randconfig-161-20260205 (https://download.01.org/0day-ci/archive/20260205/[email protected]/config)
compiler: gcc-14 (Debian 14.2.0-19) 14.2.0
smatch version: v0.5.0-8994-gd50c5a4c
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]> | Reported-by: Dan Carpenter <[email protected]> | Closes: https://lore.kernel.org/r/[email protected]/
New smatch warnings:drivers/iio/adc/ad7768-1.c:1789 ad7768_spi_offload_probe() warn: passing zero to 'PTR_ERR'
Old smatch warnings:drivers/iio/adc/ad7768-1.c:576 ad7768_fill_scale_tbl() error: 'scan_type' dereferencing possible ERR_PTR()
vim +/PTR_ERR +1789 drivers/iio/adc/ad7768-1.c
0cac6d2893a658 Jonathan Santos 2026-01-31 1757
0cac6d2893a658 Jonathan Santos 2026-01-31 1758 static int ad7768_spi_offload_probe(struct iio_dev *indio_dev,
0cac6d2893a658 Jonathan Santos 2026-01-31 1759 struct ad7768_state *st)
0cac6d2893a658 Jonathan Santos 2026-01-31 1760 {
0cac6d2893a658 Jonathan Santos 2026-01-31 1761 struct device *dev = &st->spi->dev;
0cac6d2893a658 Jonathan Santos 2026-01-31 1762 struct spi_offload_trigger_info trigger_info = {
0cac6d2893a658 Jonathan Santos 2026-01-31 1763 .fwnode = dev_fwnode(dev),
0cac6d2893a658 Jonathan Santos 2026-01-31 1764 .ops = &ad7768_offload_trigger_ops,
0cac6d2893a658 Jonathan Santos 2026-01-31 1765 .priv = st,
0cac6d2893a658 Jonathan Santos 2026-01-31 1766 };
0cac6d2893a658 Jonathan Santos 2026-01-31 1767 struct dma_chan *rx_dma;
0cac6d2893a658 Jonathan Santos 2026-01-31 1768 int ret;
0cac6d2893a658 Jonathan Santos 2026-01-31 1769
0cac6d2893a658 Jonathan Santos 2026-01-31 1770 ret = devm_spi_offload_trigger_register(dev, &trigger_info);
0cac6d2893a658 Jonathan Santos 2026-01-31 1771 if (ret)
0cac6d2893a658 Jonathan Santos 2026-01-31 1772 return dev_err_probe(dev, ret,
0cac6d2893a658 Jonathan Santos 2026-01-31 1773 "failed to register offload trigger\n");
0cac6d2893a658 Jonathan Santos 2026-01-31 1774
0cac6d2893a658 Jonathan Santos 2026-01-31 1775 st->offload_trigger = devm_spi_offload_trigger_get(dev, st->offload,
0cac6d2893a658 Jonathan Santos 2026-01-31 1776 SPI_OFFLOAD_TRIGGER_DATA_READY);
0cac6d2893a658 Jonathan Santos 2026-01-31 1777 if (IS_ERR(st->offload_trigger))
0cac6d2893a658 Jonathan Santos 2026-01-31 1778 return dev_err_probe(dev, PTR_ERR(st->offload_trigger),
0cac6d2893a658 Jonathan Santos 2026-01-31 1779 "failed to get offload trigger\n");
0cac6d2893a658 Jonathan Santos 2026-01-31 1780
0cac6d2893a658 Jonathan Santos 2026-01-31 1781 rx_dma = devm_spi_offload_rx_stream_request_dma_chan(dev, st->offload);
0cac6d2893a658 Jonathan Santos 2026-01-31 1782 if (IS_ERR(rx_dma))
0cac6d2893a658 Jonathan Santos 2026-01-31 1783 return dev_err_probe(dev, PTR_ERR(rx_dma),
0cac6d2893a658 Jonathan Santos 2026-01-31 1784 "failed to get offload RX DMA\n");
0cac6d2893a658 Jonathan Santos 2026-01-31 1785
0cac6d2893a658 Jonathan Santos 2026-01-31 1786 ret = devm_iio_dmaengine_buffer_setup_with_handle(dev, indio_dev,
0cac6d2893a658 Jonathan Santos 2026-01-31 1787 rx_dma, IIO_BUFFER_DIRECTION_IN);
0cac6d2893a658 Jonathan Santos 2026-01-31 1788 if (ret)
0cac6d2893a658 Jonathan Santos 2026-01-31 @1789 return dev_err_probe(dev, PTR_ERR(rx_dma),
0cac6d2893a658 Jonathan Santos 2026-01-31 1790 "failed to setup offload RX DMA\n");
0cac6d2893a658 Jonathan Santos 2026-01-31 1791
0cac6d2893a658 Jonathan Santos 2026-01-31 1792 indio_dev->setup_ops = &ad7768_offload_buffer_ops;
0cac6d2893a658 Jonathan Santos 2026-01-31 1793 st->offload_en = true;
0cac6d2893a658 Jonathan Santos 2026-01-31 1794
0cac6d2893a658 Jonathan Santos 2026-01-31 1795 return 0;
0cac6d2893a658 Jonathan Santos 2026-01-31 1796 }
0cac6d2893a658 Jonathan Santos 2026-01-31 1797 Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
kernel test robot
build botkernel test robot noticed the following build warnings:
Hi Jonathan,
kernel test robot noticed the following build warnings:
[auto build test WARNING on d820183f371d9aa8517a1cd21fe6edacf0f94b7f]
url: https://github.com/intel-lab-lkp/linux/commits/Jonathan-Santos/iio-adc-ad7768-1-fix-one-shot-mode-data-acquisition/20260204-203950 base: d820183f371d9aa8517a1cd21fe6edacf0f94b7f patch link: https://lore.kernel.org/r/9f9aedbe374461e48f1f1e64d5487b5b6c1fc992.1769889074.git.Jonathan.Santos%40analog.com patch subject: [PATCH 3/3] iio: adc: ad7768-1: add support for SPI offload config: x86_64-randconfig-001-20260205 (https://download.01.org/0day-ci/archive/20260205/[email protected]/config) compiler: gcc-12 (Debian 12.4.0-5) 12.4.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260205/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>, old ones prefixed by <<):
>> WARNING: modpost: module ad7768-1 uses symbol devm_iio_dmaengine_buffer_setup_with_handle from namespace IIO_DMAENGINE_BUFFER, but does not import it.
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
kernel test robot
build botkernel test robot noticed the following build errors:
Hi Jonathan,
kernel test robot noticed the following build errors:
[auto build test ERROR on d820183f371d9aa8517a1cd21fe6edacf0f94b7f]
url: https://github.com/intel-lab-lkp/linux/commits/Jonathan-Santos/iio-adc-ad7768-1-fix-one-shot-mode-data-acquisition/20260204-203950 base: d820183f371d9aa8517a1cd21fe6edacf0f94b7f patch link: https://lore.kernel.org/r/9f9aedbe374461e48f1f1e64d5487b5b6c1fc992.1769889074.git.Jonathan.Santos%40analog.com patch subject: [PATCH 3/3] iio: adc: ad7768-1: add support for SPI offload config: i386-randconfig-054-20260205 (https://download.01.org/0day-ci/archive/20260205/[email protected]/config) compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260205/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>, old ones prefixed by <<):
>> ERROR: modpost: module ad7768-1 uses symbol devm_iio_dmaengine_buffer_setup_with_handle from namespace IIO_DMAENGINE_BUFFER, but does not import it.
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki