1 #include <linux/export.h>
2 #include <linux/interrupt.h>
3 #include <linux/mutex.h>
4 #include <linux/kernel.h>
5 #include <linux/spi/spi.h>
6 #include <linux/slab.h>
8 #include <linux/iio/iio.h>
9 #include "../ring_sw.h"
10 #include <linux/iio/trigger_consumer.h>
11 #include "adis16209.h"
14 * adis16209_read_ring_data() read data registers which will be placed into ring
15 * @indio_dev: the IIO device
16 * @rx: somewhere to pass back the value read
18 static int adis16209_read_ring_data(struct iio_dev *indio_dev, u8 *rx)
20 struct spi_message msg;
21 struct adis16209_state *st = iio_priv(indio_dev);
22 struct spi_transfer xfers[ADIS16209_OUTPUTS + 1];
26 mutex_lock(&st->buf_lock);
28 spi_message_init(&msg);
30 memset(xfers, 0, sizeof(xfers));
31 for (i = 0; i <= ADIS16209_OUTPUTS; i++) {
32 xfers[i].bits_per_word = 8;
33 xfers[i].cs_change = 1;
35 xfers[i].delay_usecs = 30;
36 xfers[i].tx_buf = st->tx + 2 * i;
38 = ADIS16209_READ_REG(ADIS16209_SUPPLY_OUT + 2 * i);
39 st->tx[2 * i + 1] = 0;
41 xfers[i].rx_buf = rx + 2 * (i - 1);
42 spi_message_add_tail(&xfers[i], &msg);
45 ret = spi_sync(st->us, &msg);
47 dev_err(&st->us->dev, "problem when burst reading");
49 mutex_unlock(&st->buf_lock);
54 /* Whilst this makes a lot of calls to iio_sw_ring functions - it is to device
55 * specific to be rolled into the core.
57 static irqreturn_t adis16209_trigger_handler(int irq, void *p)
59 struct iio_poll_func *pf = p;
60 struct iio_dev *indio_dev = pf->indio_dev;
61 struct adis16209_state *st = iio_priv(indio_dev);
62 struct iio_buffer *ring = indio_dev->buffer;
66 data = kmalloc(indio_dev->scan_bytes, GFP_KERNEL);
68 dev_err(&st->us->dev, "memory alloc failed in ring bh");
72 if (!bitmap_empty(indio_dev->active_scan_mask, indio_dev->masklength) &&
73 adis16209_read_ring_data(indio_dev, st->rx) >= 0)
74 for (; i < bitmap_weight(indio_dev->active_scan_mask,
75 indio_dev->masklength); i++)
76 data[i] = be16_to_cpup((__be16 *)&(st->rx[i*2]));
78 /* Guaranteed to be aligned with 8 byte boundary */
79 if (indio_dev->scan_timestamp)
80 *((s64 *)(data + ((i + 3)/4)*4)) = pf->timestamp;
82 ring->access->store_to(ring, (u8 *)data, pf->timestamp);
86 iio_trigger_notify_done(indio_dev->trig);
91 void adis16209_unconfigure_ring(struct iio_dev *indio_dev)
93 iio_dealloc_pollfunc(indio_dev->pollfunc);
94 iio_sw_rb_free(indio_dev->buffer);
97 static const struct iio_buffer_setup_ops adis16209_ring_setup_ops = {
98 .preenable = &iio_sw_buffer_preenable,
99 .postenable = &iio_triggered_buffer_postenable,
100 .predisable = &iio_triggered_buffer_predisable,
103 int adis16209_configure_ring(struct iio_dev *indio_dev)
106 struct iio_buffer *ring;
108 ring = iio_sw_rb_allocate(indio_dev);
113 indio_dev->buffer = ring;
114 ring->scan_timestamp = true;
115 indio_dev->setup_ops = &adis16209_ring_setup_ops;
117 indio_dev->pollfunc = iio_alloc_pollfunc(&iio_pollfunc_store_time,
118 &adis16209_trigger_handler,
124 if (indio_dev->pollfunc == NULL) {
126 goto error_iio_sw_rb_free;
129 indio_dev->modes |= INDIO_BUFFER_TRIGGERED;
132 error_iio_sw_rb_free:
133 iio_sw_rb_free(indio_dev->buffer);