1 #include <linux/interrupt.h>
3 #include <linux/gpio.h>
4 #include <linux/workqueue.h>
5 #include <linux/mutex.h>
6 #include <linux/device.h>
7 #include <linux/kernel.h>
8 #include <linux/spi/spi.h>
9 #include <linux/slab.h>
10 #include <linux/sysfs.h>
11 #include <linux/list.h>
15 #include "../ring_sw.h"
16 #include "../accel/accel.h"
17 #include "../trigger.h"
18 #include "adis16260.h"
22 * adis16260_read_ring_data() read data registers which will be placed into ring
23 * @dev: device associated with child of actual device (iio_dev or iio_trig)
24 * @rx: somewhere to pass back the value read
26 static int adis16260_read_ring_data(struct device *dev, u8 *rx)
28 struct spi_message msg;
29 struct iio_dev *indio_dev = dev_get_drvdata(dev);
30 struct adis16260_state *st = iio_dev_get_devdata(indio_dev);
31 struct spi_transfer xfers[ADIS16260_OUTPUTS + 1];
35 mutex_lock(&st->buf_lock);
37 spi_message_init(&msg);
39 memset(xfers, 0, sizeof(xfers));
40 for (i = 0; i <= ADIS16260_OUTPUTS; i++) {
41 xfers[i].bits_per_word = 8;
42 xfers[i].cs_change = 1;
44 xfers[i].delay_usecs = 30;
45 xfers[i].tx_buf = st->tx + 2 * i;
46 if (i < 2) /* SUPPLY_OUT:0x02 GYRO_OUT:0x04 */
48 = ADIS16260_READ_REG(ADIS16260_SUPPLY_OUT
50 else /* 0x06 to 0x09 is reserved */
52 = ADIS16260_READ_REG(ADIS16260_SUPPLY_OUT
54 st->tx[2 * i + 1] = 0;
56 xfers[i].rx_buf = rx + 2 * (i - 1);
57 spi_message_add_tail(&xfers[i], &msg);
60 ret = spi_sync(st->us, &msg);
62 dev_err(&st->us->dev, "problem when burst reading");
64 mutex_unlock(&st->buf_lock);
69 static irqreturn_t adis16260_trigger_handler(int irq, void *p)
71 struct iio_poll_func *pf = p;
72 struct iio_dev *indio_dev = pf->private_data;
73 struct adis16260_state *st = iio_dev_get_devdata(indio_dev);
74 struct iio_ring_buffer *ring = indio_dev->ring;
77 size_t datasize = ring->access->get_bytes_per_datum(ring);
79 data = kmalloc(datasize , GFP_KERNEL);
81 dev_err(&st->us->dev, "memory alloc failed in ring bh");
85 if (ring->scan_count &&
86 adis16260_read_ring_data(&st->indio_dev->dev, st->rx) >= 0)
87 for (; i < ring->scan_count; i++)
88 data[i] = be16_to_cpup((__be16 *)&(st->rx[i*2]));
90 /* Guaranteed to be aligned with 8 byte boundary */
91 if (ring->scan_timestamp)
92 *((s64 *)(data + ((i + 3)/4)*4)) = pf->timestamp;
94 ring->access->store_to(ring, (u8 *)data, pf->timestamp);
96 iio_trigger_notify_done(st->indio_dev->trig);
102 void adis16260_unconfigure_ring(struct iio_dev *indio_dev)
104 kfree(indio_dev->pollfunc->name);
105 kfree(indio_dev->pollfunc);
106 iio_sw_rb_free(indio_dev->ring);
109 static const struct iio_ring_setup_ops adis16260_ring_setup_ops = {
110 .preenable = &iio_sw_ring_preenable,
111 .postenable = &iio_triggered_ring_postenable,
112 .predisable = &iio_triggered_ring_predisable,
115 int adis16260_configure_ring(struct iio_dev *indio_dev)
118 struct iio_ring_buffer *ring;
120 ring = iio_sw_rb_allocate(indio_dev);
125 indio_dev->ring = ring;
126 /* Effectively select the ring buffer implementation */
127 ring->access = &ring_sw_access_funcs;
129 ring->scan_timestamp = true;
130 ring->setup_ops = &adis16260_ring_setup_ops;
131 ring->owner = THIS_MODULE;
133 /* Set default scan mode */
134 iio_scan_mask_set(ring, ADIS16260_SCAN_SUPPLY);
135 iio_scan_mask_set(ring, ADIS16260_SCAN_GYRO);
136 iio_scan_mask_set(ring, ADIS16260_SCAN_AUX_ADC);
137 iio_scan_mask_set(ring, ADIS16260_SCAN_TEMP);
138 iio_scan_mask_set(ring, ADIS16260_SCAN_ANGL);
140 indio_dev->pollfunc = kzalloc(sizeof(*indio_dev->pollfunc), GFP_KERNEL);
141 if (indio_dev->pollfunc == NULL) {
143 goto error_iio_sw_rb_free;
145 indio_dev->pollfunc->private_data = indio_dev;
146 indio_dev->pollfunc->h = &iio_pollfunc_store_time;
147 indio_dev->pollfunc->thread = &adis16260_trigger_handler;
148 indio_dev->pollfunc->type = IRQF_ONESHOT;
149 indio_dev->pollfunc->name =
150 kasprintf(GFP_KERNEL, "adis16260_consumer%d", indio_dev->id);
151 if (indio_dev->pollfunc->name == NULL) {
153 goto error_free_pollfunc;
156 indio_dev->modes |= INDIO_RING_TRIGGERED;
159 kfree(indio_dev->pollfunc);
160 error_iio_sw_rb_free:
161 iio_sw_rb_free(indio_dev->ring);