[PATCH v2 4/4] iio: adc: ltc2378: Enable triggered buffer data capture

From: Marcelo Schmitt

Date: Thu May 28 2026 - 11:16:51 EST


Enable users to run triggered data captures with LTC2378 and similar ADCs.

Signed-off-by: Marcelo Schmitt <marcelo.schmitt@xxxxxxxxxx>
---
Change log v1 -> v2:
- Initialize init scan_type according to buffer selection.
- Use IIO_CHAN_SOFT_TIMESTAMP directly on right hand of assignment.

drivers/iio/adc/Kconfig | 1 +
drivers/iio/adc/ltc2378.c | 35 ++++++++++++++++++++++++++++++++++-
2 files changed, 35 insertions(+), 1 deletion(-)

diff --git a/drivers/iio/adc/Kconfig b/drivers/iio/adc/Kconfig
index 5f7038b5e731..65887c8f3171 100644
--- a/drivers/iio/adc/Kconfig
+++ b/drivers/iio/adc/Kconfig
@@ -945,6 +945,7 @@ config LTC2378
depends on GPIOLIB || PWM
select IIO_BUFFER
select LTC2378_OFFLOAD_BUFFER if SPI_OFFLOAD && PWM && IIO_BUFFER
+ select IIO_TRIGGERED_BUFFER
help
Say yes here to build support for Analog Devices LTC2378-20 and
similar analog to digital converters.
diff --git a/drivers/iio/adc/ltc2378.c b/drivers/iio/adc/ltc2378.c
index 6b01d8e96cc6..514645b847f0 100644
--- a/drivers/iio/adc/ltc2378.c
+++ b/drivers/iio/adc/ltc2378.c
@@ -18,8 +18,12 @@
#include <linux/regulator/consumer.h>
#include <linux/spi/spi.h>
#include <linux/types.h>
+#include <linux/units.h>

+#include <linux/iio/buffer.h>
#include <linux/iio/iio.h>
+#include <linux/iio/trigger_consumer.h>
+#include <linux/iio/triggered_buffer.h>
#include <linux/iio/types.h>

#include "ltc2378.h"
@@ -278,6 +282,25 @@ static const struct iio_info ltc2378_iio_info = {
.read_avail = LTC2378_READ_AVAIL,
};

+static irqreturn_t ltc2378_trigger_handler(int irq, void *p)
+{
+ struct iio_poll_func *pf = p;
+ struct iio_dev *indio_dev = pf->indio_dev;
+ struct ltc2378_state *st = iio_priv(indio_dev);
+ int ret;
+
+ ret = ltc2378_convert_and_acquire(st);
+ if (ret < 0)
+ goto err_out;
+
+ iio_push_to_buffers_with_ts(indio_dev, &st->scan, sizeof(st->scan),
+ pf->timestamp);
+
+err_out:
+ iio_trigger_notify_done(indio_dev->trig);
+ return IRQ_HANDLED;
+}
+
static int ltc2378_probe(struct spi_device *spi)
{
struct device *dev = &spi->dev;
@@ -314,17 +337,27 @@ static int ltc2378_probe(struct spi_device *spi)
st->chans[0].type = IIO_VOLTAGE;
st->chans[0].info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
BIT(IIO_CHAN_INFO_SCALE);
+ st->chans[0].scan_index = 0;

struct iio_scan_type ltc2378_scan;

ret = ltc2378_offload_buffer_setup(indio_dev, spi);
if (ret == -ENODEV) {
/* SPI offloading is unavailable. Fall back to triggered buffer. */
- dev_dbg(dev, "triggered data capture not supported\n");
ltc2378_scan.format = st->info->twos_comp ? IIO_SCAN_FORMAT_SIGNED_INT :
IIO_SCAN_FORMAT_UNSIGNED_INT;
ltc2378_scan.realbits = st->info->resolution;
ltc2378_scan.storagebits = st->info->resolution > 16 ? 32 : 16;
+
+ ret = devm_iio_triggered_buffer_setup(dev, indio_dev,
+ &iio_pollfunc_store_time,
+ &ltc2378_trigger_handler,
+ NULL);
+ if (ret)
+ return ret;
+
+ /* Add timestamp channel */
+ st->chans[num_iio_chans++] = IIO_CHAN_SOFT_TIMESTAMP(1);
} else if (ret) {
return dev_err_probe(dev, ret, "error on SPI offload setup\n");
} else {
--
2.53.0