Skip to content

Commit 2a86487

Browse files
Sean Nyekjaerjic23
authored andcommitted
iio: adc: ti-ads8688: add trigger and buffer support
Signed-off-by: Sean Nyekjaer <sean.nyekjaer@prevas.dk> Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
1 parent 0cb2aab commit 2a86487

File tree

1 file changed

+45
-3
lines changed

1 file changed

+45
-3
lines changed

drivers/iio/adc/ti-ads8688.c

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
#include <linux/of.h>
1818

1919
#include <linux/iio/iio.h>
20+
#include <linux/iio/buffer.h>
21+
#include <linux/iio/trigger_consumer.h>
22+
#include <linux/iio/triggered_buffer.h>
2023
#include <linux/iio/sysfs.h>
2124

2225
#define ADS8688_CMD_REG(x) (x << 8)
@@ -155,6 +158,13 @@ static const struct attribute_group ads8688_attribute_group = {
155158
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) \
156159
| BIT(IIO_CHAN_INFO_SCALE) \
157160
| BIT(IIO_CHAN_INFO_OFFSET), \
161+
.scan_index = index, \
162+
.scan_type = { \
163+
.sign = 'u', \
164+
.realbits = 16, \
165+
.storagebits = 16, \
166+
.endianness = IIO_BE, \
167+
}, \
158168
}
159169

160170
static const struct iio_chan_spec ads8684_channels[] = {
@@ -371,6 +381,28 @@ static const struct iio_info ads8688_info = {
371381
.attrs = &ads8688_attribute_group,
372382
};
373383

384+
static irqreturn_t ads8688_trigger_handler(int irq, void *p)
385+
{
386+
struct iio_poll_func *pf = p;
387+
struct iio_dev *indio_dev = pf->indio_dev;
388+
u16 buffer[8];
389+
int i, j = 0;
390+
391+
for (i = 0; i < indio_dev->masklength; i++) {
392+
if (!test_bit(i, indio_dev->active_scan_mask))
393+
continue;
394+
buffer[j] = ads8688_read(indio_dev, i);
395+
j++;
396+
}
397+
398+
iio_push_to_buffers_with_timestamp(indio_dev, buffer,
399+
pf->timestamp);
400+
401+
iio_trigger_notify_done(indio_dev->trig);
402+
403+
return IRQ_HANDLED;
404+
}
405+
374406
static const struct ads8688_chip_info ads8688_chip_info_tbl[] = {
375407
[ID_ADS8684] = {
376408
.channels = ads8684_channels,
@@ -402,7 +434,7 @@ static int ads8688_probe(struct spi_device *spi)
402434

403435
ret = regulator_get_voltage(st->reg);
404436
if (ret < 0)
405-
goto error_out;
437+
goto err_regulator_disable;
406438

407439
st->vref_mv = ret / 1000;
408440
} else {
@@ -430,13 +462,22 @@ static int ads8688_probe(struct spi_device *spi)
430462

431463
mutex_init(&st->lock);
432464

465+
ret = iio_triggered_buffer_setup(indio_dev, NULL, ads8688_trigger_handler, NULL);
466+
if (ret < 0) {
467+
dev_err(&spi->dev, "iio triggered buffer setup failed\n");
468+
goto err_regulator_disable;
469+
}
470+
433471
ret = iio_device_register(indio_dev);
434472
if (ret)
435-
goto error_out;
473+
goto err_buffer_cleanup;
436474

437475
return 0;
438476

439-
error_out:
477+
err_buffer_cleanup:
478+
iio_triggered_buffer_cleanup(indio_dev);
479+
480+
err_regulator_disable:
440481
if (!IS_ERR(st->reg))
441482
regulator_disable(st->reg);
442483

@@ -449,6 +490,7 @@ static int ads8688_remove(struct spi_device *spi)
449490
struct ads8688_state *st = iio_priv(indio_dev);
450491

451492
iio_device_unregister(indio_dev);
493+
iio_triggered_buffer_cleanup(indio_dev);
452494

453495
if (!IS_ERR(st->reg))
454496
regulator_disable(st->reg);

0 commit comments

Comments
 (0)