1 #include <linux/interrupt.h>
2 #include <linux/mutex.h>
3 #include <linux/kernel.h>
4 #include <linux/spi/spi.h>
5 #include <linux/slab.h>
8 #include "../ring_sw.h"
9 #include "../trigger_consumer.h"
10 #include "adis16203.h"
13 * adis16203_read_ring_data() read data registers which will be placed into ring
14 * @dev: device associated with child of actual device (iio_dev or iio_trig)
15 * @rx: somewhere to pass back the value read
17 static int adis16203_read_ring_data(struct device *dev, u8 *rx)
19 struct spi_message msg;
20 struct iio_dev *indio_dev = dev_get_drvdata(dev);
21 struct adis16203_state *st = iio_priv(indio_dev);
22 struct spi_transfer xfers[ADIS16203_OUTPUTS + 1];
26 mutex_lock(&st->buf_lock);
28 spi_message_init(&msg);
30 memset(xfers, 0, sizeof(xfers));
31 for (i = 0; i <= ADIS16203_OUTPUTS; i++) {
32 xfers[i].bits_per_word = 8;
33 xfers[i].cs_change = 1;
35 xfers[i].delay_usecs = 20;
36 xfers[i].tx_buf = st->tx + 2 * i;
37 if (i < 1) /* SUPPLY_OUT: 0x02, AUX_ADC: 0x08 */
38 st->tx[2 * i] = ADIS16203_READ_REG(ADIS16203_SUPPLY_OUT + 2 * i);
40 st->tx[2 * i] = ADIS16203_READ_REG(ADIS16203_SUPPLY_OUT + 2 * i + 6);
41 st->tx[2 * i + 1] = 0;
43 xfers[i].rx_buf = rx + 2 * (i - 1);
44 spi_message_add_tail(&xfers[i], &msg);
47 ret = spi_sync(st->us, &msg);
49 dev_err(&st->us->dev, "problem when burst reading");
51 mutex_unlock(&st->buf_lock);
56 /* Whilst this makes a lot of calls to iio_sw_ring functions - it is to device
57 * specific to be rolled into the core.
59 static irqreturn_t adis16203_trigger_handler(int irq, void *p)
61 struct iio_poll_func *pf = p;
62 struct iio_dev *indio_dev = pf->indio_dev;
63 struct adis16203_state *st = iio_priv(indio_dev);
64 struct iio_ring_buffer *ring = indio_dev->ring;
68 size_t datasize = ring->access->get_bytes_per_datum(ring);
70 data = kmalloc(datasize, GFP_KERNEL);
72 dev_err(&st->us->dev, "memory alloc failed in ring bh");
77 if (adis16203_read_ring_data(&indio_dev->dev, st->rx) >= 0)
78 for (; i < ring->scan_count; i++)
79 data[i] = be16_to_cpup(
80 (__be16 *)&(st->rx[i*2]));
82 /* Guaranteed to be aligned with 8 byte boundary */
83 if (ring->scan_timestamp)
84 *((s64 *)(data + ((i + 3)/4)*4)) = pf->timestamp;
86 ring->access->store_to(ring,
90 iio_trigger_notify_done(indio_dev->trig);
96 void adis16203_unconfigure_ring(struct iio_dev *indio_dev)
98 iio_dealloc_pollfunc(indio_dev->pollfunc);
99 iio_sw_rb_free(indio_dev->ring);
102 static const struct iio_ring_setup_ops adis16203_ring_setup_ops = {
103 .preenable = &iio_sw_ring_preenable,
104 .postenable = &iio_triggered_ring_postenable,
105 .predisable = &iio_triggered_ring_predisable,
108 int adis16203_configure_ring(struct iio_dev *indio_dev)
111 struct iio_ring_buffer *ring;
113 ring = iio_sw_rb_allocate(indio_dev);
118 indio_dev->ring = ring;
119 /* Effectively select the ring buffer implementation */
121 ring->scan_timestamp = true;
122 ring->access = &ring_sw_access_funcs;
123 ring->setup_ops = &adis16203_ring_setup_ops;
124 ring->owner = THIS_MODULE;
126 /* Set default scan mode */
127 iio_scan_mask_set(ring, ADIS16203_SCAN_SUPPLY);
128 iio_scan_mask_set(ring, ADIS16203_SCAN_TEMP);
129 iio_scan_mask_set(ring, ADIS16203_SCAN_AUX_ADC);
130 iio_scan_mask_set(ring, ADIS16203_SCAN_INCLI_X);
131 iio_scan_mask_set(ring, ADIS16203_SCAN_INCLI_Y);
133 indio_dev->pollfunc = iio_alloc_pollfunc(&iio_pollfunc_store_time,
134 &adis16203_trigger_handler,
137 "adis16203_consumer%d",
139 if (indio_dev->pollfunc == NULL) {
141 goto error_iio_sw_rb_free;
144 indio_dev->modes |= INDIO_RING_TRIGGERED;
147 error_iio_sw_rb_free:
148 iio_sw_rb_free(indio_dev->ring);