[PATCH 3/3] iio: adc: ad7768-1: add support for SPI offload

Jonathan Santos posted 3 patches 1 week ago
[PATCH 3/3] iio: adc: ad7768-1: add support for SPI offload
Posted by Jonathan Santos 1 week ago
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 <Jonathan.Santos@analog.com>
---
 drivers/iio/adc/Kconfig    |   2 +
 drivers/iio/adc/ad7768-1.c | 187 ++++++++++++++++++++++++++++++++++++-
 2 files changed, 185 insertions(+), 4 deletions(-)

diff --git a/drivers/iio/adc/Kconfig b/drivers/iio/adc/Kconfig
index 60038ae8dfc4..a1c3226c3631 100644
--- a/drivers/iio/adc/Kconfig
+++ b/drivers/iio/adc/Kconfig
@@ -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).
diff --git a/drivers/iio/adc/ad7768-1.c b/drivers/iio/adc/ad7768-1.c
index 374614ea97ac..fc497fb639d0 100644
--- a/drivers/iio/adc/ad7768-1.c
+++ b/drivers/iio/adc/ad7768-1.c
@@ -25,12 +25,15 @@
 #include <linux/regulator/consumer.h>
 #include <linux/regulator/driver.h>
 #include <linux/sysfs.h>
+#include <linux/spi/offload/consumer.h>
+#include <linux/spi/offload/provider.h>
 #include <linux/spi/spi.h>
 #include <linux/unaligned.h>
 #include <linux/units.h>
 #include <linux/util_macros.h>
 
 #include <linux/iio/buffer.h>
+#include <linux/iio/buffer-dmaengine.h>
 #include <linux/iio/iio.h>
 #include <linux/iio/sysfs.h>
 #include <linux/iio/trigger.h>
@@ -161,6 +164,8 @@ enum ad7768_filter_regval {
 enum ad7768_scan_type {
 	AD7768_SCAN_TYPE_NORMAL,
 	AD7768_SCAN_TYPE_HIGH_SPEED,
+	AD7768_SCAN_TYPE_OFFLOAD_NORMAL,
+	AD7768_SCAN_TYPE_OFFLOAD_HIGH_SPEED,
 };
 
 enum {
@@ -266,6 +271,18 @@ static const struct iio_scan_type ad7768_scan_type[] = {
 		.storagebits = 16,
 		.endianness = IIO_BE,
 	},
+	[AD7768_SCAN_TYPE_OFFLOAD_NORMAL] = {
+		.sign = 's',
+		.realbits = 24,
+		.storagebits = 32,
+		.endianness = IIO_CPU,
+	},
+	[AD7768_SCAN_TYPE_OFFLOAD_HIGH_SPEED] = {
+		.sign = 's',
+		.realbits = 16,
+		.storagebits = 32,
+		.endianness = IIO_CPU,
+	},
 };
 
 struct ad7768_chip_info {
@@ -283,6 +300,8 @@ struct ad7768_chip_info {
 
 struct ad7768_state {
 	struct spi_device *spi;
+	struct spi_offload *offload;
+	struct spi_offload_trigger *offload_trigger;
 	struct regmap *regmap;
 	struct regmap *regmap24;
 	int vref_uv;
@@ -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;
 	struct 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;
 }
@@ -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)
 		iio_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 = {
+		.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));
+	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.
+	 */
+	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);
+		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;
+}
+
+static int ad7768_offload_buffer_predisable(struct iio_dev *indio_dev)
+{
+	struct ad7768_state *st = iio_priv(indio_dev);
+	unsigned int unused;
+
+	spi_offload_trigger_disable(st->offload, st->offload_trigger);
+	spi_unoptimize_message(&st->offload_msg);
+
+	/*
+	 * To exit continuous read mode, perform a single read of the ADC_DATA
+	 * reg (0x2C), which allows further configuration of the device.
+	 */
+	return regmap_read(st->regmap24, AD7768_REG24_ADC_DATA, &unused);
+}
+
+static const struct iio_buffer_setup_ops ad7768_offload_buffer_ops = {
+	.postenable = ad7768_offload_buffer_postenable,
+	.predisable = ad7768_offload_buffer_predisable,
+};
+
 static const struct iio_trigger_ops ad7768_trigger_ops = {
 	.validate_device = iio_trigger_validate_own_device,
 };
@@ -1591,6 +1683,36 @@ static int ad7768_parse_aaf_gain(struct device *dev, struct ad7768_state *st)
 	return 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 */
+	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)
+		return -EINVAL;
+
+	return 0;
+}
+
+static const struct spi_offload_trigger_ops ad7768_offload_trigger_ops = {
+	.match = ad7768_offload_trigger_match,
+	.request = ad7768_offload_trigger_request,
+};
+
 static const struct ad7768_chip_info ad7768_chip_info = {
 	.name = "ad7768-1",
 	.channel_spec = ad7768_channels,
@@ -1628,6 +1750,51 @@ static const struct ad7768_chip_info adaq7769_chip_info = {
 	.has_variable_aaf = true,
 };
 
+static const struct spi_offload_config ad7768_spi_offload_config = {
+	.capability_flags = SPI_OFFLOAD_CAP_TRIGGER |
+			    SPI_OFFLOAD_CAP_RX_STREAM_DMA,
+};
+
+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;
+	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");
+
+	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, 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;
+}
+
 static int ad7768_probe(struct spi_device *spi)
 {
 	struct ad7768_state *st;
@@ -1727,9 +1894,21 @@ static int ad7768_probe(struct spi_device *spi)
 	if (ret)
 		return ret;
 
-	ret = ad7768_triggered_buffer_alloc(indio_dev);
-	if (ret)
-		return ret;
+	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");
+
+	/* If not using SPI offload, fall back to low speed usage. */
+	if (ret == -ENODEV) {
+		ret = ad7768_triggered_buffer_alloc(indio_dev);
+		if (ret)
+			return ret;
+	} else {
+		ret = ad7768_spi_offload_probe(indio_dev, st);
+		if (ret)
+			return ret;
+	}
 
 	return devm_iio_device_register(&spi->dev, indio_dev);
 }
-- 
2.34.1
Re: [PATCH 3/3] iio: adc: ad7768-1: add support for SPI offload
Posted by kernel test robot 3 days, 12 hours ago
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/202602051436.PfHU6nPa-lkp@intel.com/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/202602051436.PfHU6nPa-lkp@intel.com/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 <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202602051436.PfHU6nPa-lkp@intel.com/

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.

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
Re: [PATCH 3/3] iio: adc: ad7768-1: add support for SPI offload
Posted by Dan Carpenter 3 days, 13 hours ago
Hi Jonathan,

kernel test robot noticed the following build warnings:

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-161-20260205 (https://download.01.org/0day-ci/archive/20260205/202602051234.5gArzLyZ-lkp@intel.com/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 <lkp@intel.com>
| Reported-by: Dan Carpenter <dan.carpenter@linaro.org>
| Closes: https://lore.kernel.org/r/202602051234.5gArzLyZ-lkp@intel.com/

New smatch warnings:
drivers/iio/adc/ad7768-1.c:1789 ad7768_spi_offload_probe() warn: passing zero to 'PTR_ERR'

vim +/PTR_ERR +1789 drivers/iio/adc/ad7768-1.c

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),

s/PTR_ERR(rx_dma)/ret/

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  }

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
Re: [PATCH 3/3] iio: adc: ad7768-1: add support for SPI offload
Posted by kernel test robot 3 days, 13 hours ago
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/202602051328.Qsu5tg2C-lkp@intel.com/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/202602051328.Qsu5tg2C-lkp@intel.com/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 <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202602051328.Qsu5tg2C-lkp@intel.com/

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.

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
Re: [PATCH 3/3] iio: adc: ad7768-1: add support for SPI offload
Posted by David Lechner 4 days, 3 hours ago
On 1/31/26 7:35 PM, Jonathan Santos wrote:
> 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 <Jonathan.Santos@analog.com>
> ---

...

>  struct ad7768_state {
>  	struct spi_device *spi;
> +	struct spi_offload *offload;
> +	struct spi_offload_trigger *offload_trigger;
>  	struct regmap *regmap;
>  	struct regmap *regmap24;
>  	int vref_uv;
> @@ -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.

>  	struct 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;
>  }
> @@ -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.

>  		iio_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 = {
> +		.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.

> +	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.
> +	 */
> +	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?

> +		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;
> +}
> +

...

> +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).

> +	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.

> +		return -EINVAL;
> +
> +	return 0;
> +}
> +
Re: [PATCH 3/3] iio: adc: ad7768-1: add support for SPI offload
Posted by Andy Shevchenko 4 days, 5 hours ago
On Sat, Jan 31, 2026 at 10:35:39PM -0300, Jonathan Santos wrote:
> 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.

...

> +	if (st->offload_en)
> +		return st->oversampling_ratio == 8 ?
> +			AD7768_SCAN_TYPE_OFFLOAD_HIGH_SPEED : AD7768_SCAN_TYPE_OFFLOAD_NORMAL;

Wrong indentation.

...

> +	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).

> +	if (ret) {
> +		spi_unoptimize_message(&st->offload_msg);

> +		return ret;
> +	}

> +	return 0;

	if (ret)
		spi_unoptimize_message(&st->offload_msg);

	return ret;

?

...

> +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.

> +	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.

> +	if (nargs != 1)
> +		return -EINVAL;
> +
> +	return 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.

> +};

...

> +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;
> +	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.

> +
> +	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.

> +	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.

> +	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;
> +}

...

> +	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.

> +	/* 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 {
		...
	}

> +		ret = ad7768_triggered_buffer_alloc(indio_dev);
> +		if (ret)
> +			return ret;
> +	} else {
> +		ret = ad7768_spi_offload_probe(indio_dev, st);
> +		if (ret)
> +			return ret;
> +	}

-- 
With Best Regards,
Andy Shevchenko