iio: adc: ad7768-1: add SPI offload support and fixes
Subsystems (3)
v2 cover iio: adc: ad7768-1: add SPI offload support and fixes
This series adds SPI offload support to the AD7768-1 driver and includes fixes for one-shot mode operation and IRQ management discovered during development and testing.
---
Changes in v2:
* Refactor "fix one-shot mode data acquisition" patch and keep it for backporting
to older kernel versions.
* Instead of restricting the one-shot mode for some filter types, remove
it completely and guarantee the device is always in continuous
conversion mode.
* Add "disable IRQ autoenable" patch to fix an old CPU usage issue and to avoid unnecessary interrupts when buffering data in offload mode.
* Addressed other review comments and kernel test robot reports, see individual patches.
* Link to v1: https://lore.kernel.org/linux-iio/[email protected]/T/#t/
Jonathan Santos (4):
iio: adc: ad7768-1: fix one-shot mode data acquisition
iio: adc: ad7768-1: remove switch to one-shot mode
iio: adc: ad7768-1: disable IRQ autoenable
iio: adc: ad7768-1: add support for SPI offload
drivers/iio/adc/Kconfig | 2 + drivers/iio/adc/ad7768-1.c | 220 ++++++++++++++++++++++++++++++++++--- 2 files changed, 206 insertions(+), 16 deletions(-)
base-commit: 0713b26190addfa3a774b386c8658952ef9f7faf
Signature · 1 line
2.34.1
PATCH v2 1/4 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 reinit_completion() to ensure it only waits for the actual conversion completion.
---
Changes in v2:
* reinit_completion() is now called before sending the SYNC pulse, to ensure that the completion is properly reset before waiting for the conversion to complete.
* Patch kept for backporting, even though superseded by next patch.
--- 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; +reinit_completion (&st->completion); + +/* One-shot mode requires a SYNC pulse to generate a new sample */ + ret =ad7768_send_sync_pulse (st); +if (ret) +return ret; + ret =wait_for_completion_timeout (&st->completion,msecs_to_jiffies (1000 ));if (!ret)
Signature · 1 line
2.34.1
PATCH v2 2/4 iio: adc: ad7768-1: remove switch to one-shot mode
In reply to Jonathan Santoswideband low ripple FIR Filter is not available in one-shot mode. In order to make direct reads work for all filter options, remove the switch for one-shot mode and guarantee device is always in continuous conversion mode.
---
* Instead of restricting the one-shot mode for some filter types, remove it completely and guarantee the device is always in continuous conversion mode.
--- drivers/iio/adc/ad7768-1.c | 21 ++++----------------- 1 file changed, 4 insertions(+), 17 deletions(-)
drivers/iio/adc/ad7768-1.c +4 -17
@@ -463,17 +463,8 @@ 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; -reinit_completion (&st->completion); -/* One-shot mode requires a SYNC pulse to generate a new sample */ - ret =ad7768_send_sync_pulse (st); -if (ret) -return ret; - ret =wait_for_completion_timeout (&st->completion,msecs_to_jiffies (1000 ));if (!ret)
@@ -492,14 +483,6 @@ static int ad7768_scan_direct(struct iio_dev *indio_dev)if (st->oversampling_ratio ==8 ) readval >>=8 ; -/* -* 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; -return readval; }
@@ -1248,6 +1231,10 @@ static int ad7768_setup(struct iio_dev *indio_dev)return ret; } + ret =ad7768_set_mode (st,AD7768_CONTINUOUS ); +if (ret) +return ret; +/* For backwards compatibility, try the adi,sync-in-gpios property */ st->gpio_sync_in =devm_gpiod_get_optional (&st->spi->dev,"adi,sync-in" ,GPIOD_OUT_LOW );
Signature · 1 line
2.34.1
PATCH v2 3/4 iio: adc: ad7768-1: disable IRQ autoenable
In reply to Jonathan SantosThe device continuously converts data while powered up, generating interrupts in the background. Configure the IRQ to be enabled and disabled manually as needed to avoid unnecessary CPU load.
---
Changes in v2:
* New patch. It solves the excessive CPU load issue and addresses David's suggestion of having the interrupt disabled when using offload.
* This is a reproduction of and old patch sent last year. please refer to the thread [1].
[1]: https://lore.kernel.org/linux-iio/[email protected]/T/#t/
--- drivers/iio/adc/ad7768-1.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-)
drivers/iio/adc/ad7768-1.c +17 -1
@@ -464,9 +464,11 @@ static int ad7768_scan_direct(struct iio_dev *indio_dev)int readval, ret;reinit_completion (&st->completion); +enable_irq (st->spi->irq); ret =wait_for_completion_timeout (&st->completion,msecs_to_jiffies (1000 )); +disable_irq (st->spi->irq);if (!ret)return -ETIMEDOUT ;
@@ -1339,8 +1341,22 @@ static const struct iio_buffer_setup_ops ad7768_buffer_ops = { .predisable = &ad7768_buffer_predisable, }; +static int ad7768_set_trigger_state (struct iio_trigger *trig,bool enable) +{ +struct iio_dev *indio_dev =iio_trigger_get_drvdata (trig); +struct ad7768_state *st =iio_priv (indio_dev); + +if (enable) +enable_irq (st->spi->irq); +else +disable_irq (st->spi->irq); + +return 0 ; +} +static const struct iio_trigger_ops ad7768_trigger_ops = { .validate_device = iio_trigger_validate_own_device, + .set_trigger_state = ad7768_set_trigger_state, }; static int ad7768_set_channel_label(struct iio_dev *indio_dev,
@@ -1704,7 +1720,7 @@ static int ad7768_probe(struct spi_device *spi)return ret; ret =devm_request_irq (&spi->dev, spi->irq, &ad7768_interrupt, -IRQF_TRIGGER_RISING |IRQF_NO_THREAD , +IRQF_TRIGGER_RISING |IRQF_NO_THREAD |IRQF_NO_AUTOEN , indio_dev->name, indio_dev);if (ret)return ret;
Signature · 1 line
2.34.1
PATCH v2 4/4 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.
---
Changes in v2:
* Removed offload_en element in ad7768_state.
* Refactored ad7768_offload_buffer_postenable() to undo continuous read
mode in case something fails afterwards.
* Allow 1 or 2 arguments in the offload trigger, instead of only 1.
* Fixed indentation issues suggested by the Andy.
* Import IIO_DMAENGINE_BUFFER namespace (Reported by kernel test robot).
* Fixed error pointer reported in https://lore.kernel.org/all/[email protected]/
It was supposed to return ret instead of the rx_dma pointer.
--- drivers/iio/adc/Kconfig | 2 + drivers/iio/adc/ad7768-1.c | 186 ++++++++++++++++++++++++++++++++++++- 2 files changed, 185 insertions(+), 3 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 -3
259 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [PATCH v2 4/4] iio: adc: ad7768-1: add support for SPI offload
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.
...
Quoted 5 lines
drivers/iio/adc/ad7768-1.c +5
-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 = {
drivers/iio/adc/ad7768-1.c static const struct ad7768_chip_info adaq7769_chip_info = { · line 1774
Quoted 10 lines
drivers/iio/adc/ad7768-1.c +10
-0+ .fwnode = dev_fwnode(dev), + .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" );
Double space...
(do not resend just for addressing this)
Quoted 19 lines
drivers/iio/adc/ad7768-1.c +19
-0+ st->offload_trigger =devm_spi_offload_trigger_get (dev, st->offload, +SPI_OFFLOAD_TRIGGER_DATA_READY ); +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 ); +if (ret) +return dev_err_probe (dev, ret,"failed to setup offload RX DMA\n" ); + + indio_dev->setup_ops = &ad7768_offload_buffer_ops; + +return 0 ; +}
Signature · 2 lines
With Best Regards, Andy Shevchenko
Jonathan Cameron
maintainerRe: [PATCH v2 4/4] iio: adc: ad7768-1: add support for SPI offload
Quoted 20 lines · On Mon, 23 Feb 2026 15:46:24 +0200 Andy Shevchenko…
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.
...
Quoted 10 lines
drivers/iio/adc/ad7768-1.c +10
-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), + .ops = &ad7768_offload_trigger_ops, + .priv = st, + }; +struct dma_chan *rx_dma;
Quoted 8 lines
drivers/iio/adc/ad7768-1.c static const struct ad7768_chip_info adaq7769_chip_info = { · line 1774
Quoted 5 lines
drivers/iio/adc/ad7768-1.c +5
-0+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" );Double space...
(do not resend just for addressing this)
Tidied up.
Series applied to the togreg branch of iio.git and for now pushed out as testing
Thanks!
Jonathan
Quoted 21 lines
Quoted 20 lines
drivers/iio/adc/ad7768-1.c +19
-0+ st->offload_trigger =devm_spi_offload_trigger_get (dev, st->offload, +SPI_OFFLOAD_TRIGGER_DATA_READY ); +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 ); +if (ret) +return dev_err_probe (dev, ret,"failed to setup offload RX DMA\n" ); + + indio_dev->setup_ops = &ad7768_offload_buffer_ops; + +return 0 ; +}
David Lechner
reviewerRe: [PATCH v2 4/4] iio: adc: ad7768-1: add support for SPI offload
In reply to Jonathan SantosQuoted 4 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.
...
Quoted 34 lines
drivers/iio/adc/ad7768-1.c +34
-0+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 = { + .type =SPI_OFFLOAD_TRIGGER_DATA_READY , + }; +const struct iio_scan_type *scan_type; +unsigned int unused; +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 =spi_bpw_to_bytes (scan_type->realbits); + st->offload_xfer.bits_per_word = scan_type->realbits; + st->offload_xfer.offload_flags =SPI_OFFLOAD_XFER_RX_STREAM ; + +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); +return ret; + } + +/* +* 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. +*/ + ret =regmap_write (st->regmap,AD7768_REG_INTERFACE_FORMAT ,0x01 ); +if (ret)
drivers/iio/adc/ad7768-1.c static const struct iio_buffer_setup_ops ad7768_buffer_ops = { · line 1412
Quoted 10 lines
drivers/iio/adc/ad7768-1.c +10
-0+ goto err_unoptimize_message; + + ret =spi_offload_trigger_enable (st->offload, st->offload_trigger, + &config); +if (ret) + goto err_exit_continuous_read_mode; + +return 0 ; + +err_exit_continuous_read_mode:
I wouldn't mind a comment here explaining what this read does.
Quoted 7 lines
drivers/iio/adc/ad7768-1.c +7
-0+regmap_read (st->regmap24,AD7768_REG24_ADC_DATA , &unused); + +err_unoptimize_message: +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 1696
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 or 2 args to indicate the trigger output signal */
Would be more clear to say "up to 2 args are allowed, but only 1 is used".
Quoted 6 lines
drivers/iio/adc/ad7768-1.c +6
-0+if (nargs ==0 || nargs >2 || args[0 ] !=AD7768_TRIGGER_SOURCE_DRDY ) +return false; + +return true; +} +
Jonathan Cameron
maintainerRe: [PATCH v2 4/4] iio: adc: ad7768-1: add support for SPI offload
Quoted 46 lines · On Sat, 28 Feb 2026 11:18:22 -0600 David Lechner <[email protected]> wrote:
Quoted 4 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.
...
Quoted 38 lines
drivers/iio/adc/ad7768-1.c +38
-0+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 = { + .type =SPI_OFFLOAD_TRIGGER_DATA_READY , + }; +const struct iio_scan_type *scan_type; +unsigned int unused; +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 =spi_bpw_to_bytes (scan_type->realbits); + st->offload_xfer.bits_per_word = scan_type->realbits; + st->offload_xfer.offload_flags =SPI_OFFLOAD_XFER_RX_STREAM ; + +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); +return ret; + } + +/* +* 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. +*/ + ret =regmap_write (st->regmap,AD7768_REG_INTERFACE_FORMAT ,0x01 ); +if (ret) + goto err_unoptimize_message; + + ret =spi_offload_trigger_enable (st->offload, st->offload_trigger, + &config);
Quoted 8 lines
drivers/iio/adc/ad7768-1.c static const struct iio_buffer_setup_ops ad7768_buffer_ops = { · line 1412
Quoted 6 lines
drivers/iio/adc/ad7768-1.c +6
-0+if (ret) + goto err_exit_continuous_read_mode; + +return 0 ; + +err_exit_continuous_read_mode:I wouldn't mind a comment here explaining what this read does.
Given I'm not sure, Jonathan, can we have such a comment as a follow up patch please.
Quoted 11 lines
Quoted 8 lines
drivers/iio/adc/ad7768-1.c +7
-0+regmap_read (st->regmap24,AD7768_REG24_ADC_DATA , &unused); + +err_unoptimize_message: +spi_unoptimize_message (&st->offload_msg); + +return ret; +}...
Quoted 10 lines
drivers/iio/adc/ad7768-1.c static int ad7768_parse_aaf_gain(struct device *dev, struct ad7768_state *st) · line 1696
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 or 2 args to indicate the trigger output signal */ Would be more clear to say "up to 2 args are allowed, but only 1 is used".
I tweaked this.
Quoted 7 lines
Quoted 7 lines
drivers/iio/adc/ad7768-1.c +6
-0+if (nargs ==0 || nargs >2 || args[0 ] !=AD7768_TRIGGER_SOURCE_DRDY ) +return false; + +return true; +} +
Quoted 4 lines · Jonathan Santos
This series adds SPI offload support to the AD7768-1 driver and includes fixes for one-shot mode operation and IRQ management discovered during development and testing.