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>
9 #include "../ring_sw.h"
10 #include "../trigger_consumer.h"
11 #include "adis16201.h"
15 * adis16201_read_ring_data() read data registers which will be placed into ring
16 * @dev: device associated with child of actual device (iio_dev or iio_trig)
17 * @rx: somewhere to pass back the value read
19 static int adis16201_read_ring_data(struct iio_dev *indio_dev, u8 *rx)
21 struct spi_message msg;
22 struct adis16201_state *st = iio_priv(indio_dev);
23 struct spi_transfer xfers[ADIS16201_OUTPUTS + 1];
27 mutex_lock(&st->buf_lock);
29 spi_message_init(&msg);
31 memset(xfers, 0, sizeof(xfers));
32 for (i = 0; i <= ADIS16201_OUTPUTS; i++) {
33 xfers[i].bits_per_word = 8;
34 xfers[i].cs_change = 1;
36 xfers[i].delay_usecs = 20;
37 if (i < ADIS16201_OUTPUTS) {
38 xfers[i].tx_buf = st->tx + 2 * i;
39 st->tx[2 * i] = ADIS16201_READ_REG(ADIS16201_SUPPLY_OUT +
41 st->tx[2 * i + 1] = 0;
44 xfers[i].rx_buf = rx + 2 * (i - 1);
45 spi_message_add_tail(&xfers[i], &msg);
48 ret = spi_sync(st->us, &msg);
50 dev_err(&st->us->dev, "problem when burst reading");
52 mutex_unlock(&st->buf_lock);
57 /* Whilst this makes a lot of calls to iio_sw_ring functions - it is to device
58 * specific to be rolled into the core.
60 static irqreturn_t adis16201_trigger_handler(int irq, void *p)
62 struct iio_poll_func *pf = p;
63 struct iio_dev *indio_dev = pf->indio_dev;
64 struct adis16201_state *st = iio_priv(indio_dev);
65 struct iio_buffer *ring = indio_dev->buffer;
69 size_t datasize = ring->access->get_bytes_per_datum(ring);
71 data = kmalloc(datasize, GFP_KERNEL);
73 dev_err(&st->us->dev, "memory alloc failed in ring bh");
78 if (adis16201_read_ring_data(indio_dev, st->rx) >= 0)
79 for (; i < ring->scan_count; i++)
80 data[i] = be16_to_cpup(
81 (__be16 *)&(st->rx[i*2]));
83 /* Guaranteed to be aligned with 8 byte boundary */
84 if (ring->scan_timestamp)
85 *((s64 *)(data + ((i + 3)/4)*4)) = pf->timestamp;
87 ring->access->store_to(ring, (u8 *)data, pf->timestamp);
89 iio_trigger_notify_done(indio_dev->trig);
95 void adis16201_unconfigure_ring(struct iio_dev *indio_dev)
97 iio_dealloc_pollfunc(indio_dev->pollfunc);
98 iio_sw_rb_free(indio_dev->buffer);
101 static const struct iio_buffer_setup_ops adis16201_ring_setup_ops = {
102 .preenable = &iio_sw_buffer_preenable,
103 .postenable = &iio_triggered_buffer_postenable,
104 .predisable = &iio_triggered_buffer_predisable,
107 int adis16201_configure_ring(struct iio_dev *indio_dev)
110 struct iio_buffer *ring;
112 ring = iio_sw_rb_allocate(indio_dev);
117 indio_dev->buffer = ring;
118 /* Effectively select the ring buffer implementation */
120 ring->scan_timestamp = true;
121 ring->access = &ring_sw_access_funcs;
122 ring->setup_ops = &adis16201_ring_setup_ops;
123 ring->owner = THIS_MODULE;
125 indio_dev->pollfunc = iio_alloc_pollfunc(&iio_pollfunc_store_time,
126 &adis16201_trigger_handler,
129 "adis16201_consumer%d",
131 if (indio_dev->pollfunc == NULL) {
133 goto error_iio_sw_rb_free;
136 indio_dev->modes |= INDIO_BUFFER_TRIGGERED;
138 error_iio_sw_rb_free:
139 iio_sw_rb_free(indio_dev->buffer);