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>
7
8 #include "../iio.h"
9 #include "../ring_sw.h"
10 #include "../trigger_consumer.h"
11 #include "adis16203.h"
12
13 /**
14 * adis16203_read_ring_data() read data registers which will be placed into ring
15 * @dev: device associated with child of actual device (iio_dev or iio_trig)
16 * @rx: somewhere to pass back the value read
17 **/
adis16203_read_ring_data(struct device * dev,u8 * rx)18 static int adis16203_read_ring_data(struct device *dev, u8 *rx)
19 {
20 struct spi_message msg;
21 struct iio_dev *indio_dev = dev_get_drvdata(dev);
22 struct adis16203_state *st = iio_priv(indio_dev);
23 struct spi_transfer xfers[ADIS16203_OUTPUTS + 1];
24 int ret;
25 int i;
26
27 mutex_lock(&st->buf_lock);
28
29 spi_message_init(&msg);
30
31 memset(xfers, 0, sizeof(xfers));
32 for (i = 0; i <= ADIS16203_OUTPUTS; i++) {
33 xfers[i].bits_per_word = 8;
34 xfers[i].cs_change = 1;
35 xfers[i].len = 2;
36 xfers[i].delay_usecs = 20;
37 xfers[i].tx_buf = st->tx + 2 * i;
38 if (i < 1) /* SUPPLY_OUT: 0x02, AUX_ADC: 0x08 */
39 st->tx[2 * i] = ADIS16203_READ_REG(ADIS16203_SUPPLY_OUT + 2 * i);
40 else
41 st->tx[2 * i] = ADIS16203_READ_REG(ADIS16203_SUPPLY_OUT + 2 * i + 6);
42 st->tx[2 * i + 1] = 0;
43 if (i >= 1)
44 xfers[i].rx_buf = rx + 2 * (i - 1);
45 spi_message_add_tail(&xfers[i], &msg);
46 }
47
48 ret = spi_sync(st->us, &msg);
49 if (ret)
50 dev_err(&st->us->dev, "problem when burst reading");
51
52 mutex_unlock(&st->buf_lock);
53
54 return ret;
55 }
56
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.
59 */
adis16203_trigger_handler(int irq,void * p)60 static irqreturn_t adis16203_trigger_handler(int irq, void *p)
61 {
62 struct iio_poll_func *pf = p;
63 struct iio_dev *indio_dev = pf->indio_dev;
64 struct adis16203_state *st = iio_priv(indio_dev);
65 struct iio_buffer *ring = indio_dev->buffer;
66
67 int i = 0;
68 s16 *data;
69 size_t datasize = ring->access->get_bytes_per_datum(ring);
70
71 data = kmalloc(datasize, GFP_KERNEL);
72 if (data == NULL) {
73 dev_err(&st->us->dev, "memory alloc failed in ring bh");
74 return -ENOMEM;
75 }
76
77 if (!bitmap_empty(indio_dev->active_scan_mask, indio_dev->masklength) &&
78 adis16203_read_ring_data(&indio_dev->dev, st->rx) >= 0)
79 for (; i < bitmap_weight(indio_dev->active_scan_mask,
80 indio_dev->masklength); i++)
81 data[i] = be16_to_cpup((__be16 *)&(st->rx[i*2]));
82
83 /* Guaranteed to be aligned with 8 byte boundary */
84 if (ring->scan_timestamp)
85 *((s64 *)(data + ((i + 3)/4)*4)) = pf->timestamp;
86
87 ring->access->store_to(ring,
88 (u8 *)data,
89 pf->timestamp);
90
91 iio_trigger_notify_done(indio_dev->trig);
92 kfree(data);
93
94 return IRQ_HANDLED;
95 }
96
adis16203_unconfigure_ring(struct iio_dev * indio_dev)97 void adis16203_unconfigure_ring(struct iio_dev *indio_dev)
98 {
99 iio_dealloc_pollfunc(indio_dev->pollfunc);
100 iio_sw_rb_free(indio_dev->buffer);
101 }
102
103 static const struct iio_buffer_setup_ops adis16203_ring_setup_ops = {
104 .preenable = &iio_sw_buffer_preenable,
105 .postenable = &iio_triggered_buffer_postenable,
106 .predisable = &iio_triggered_buffer_predisable,
107 };
108
adis16203_configure_ring(struct iio_dev * indio_dev)109 int adis16203_configure_ring(struct iio_dev *indio_dev)
110 {
111 int ret = 0;
112 struct iio_buffer *ring;
113
114 ring = iio_sw_rb_allocate(indio_dev);
115 if (!ring) {
116 ret = -ENOMEM;
117 return ret;
118 }
119 indio_dev->buffer = ring;
120 ring->scan_timestamp = true;
121 indio_dev->setup_ops = &adis16203_ring_setup_ops;
122
123 indio_dev->pollfunc = iio_alloc_pollfunc(&iio_pollfunc_store_time,
124 &adis16203_trigger_handler,
125 IRQF_ONESHOT,
126 indio_dev,
127 "adis16203_consumer%d",
128 indio_dev->id);
129 if (indio_dev->pollfunc == NULL) {
130 ret = -ENOMEM;
131 goto error_iio_sw_rb_free;
132 }
133
134 indio_dev->modes |= INDIO_BUFFER_TRIGGERED;
135 return 0;
136
137 error_iio_sw_rb_free:
138 iio_sw_rb_free(indio_dev->buffer);
139 return ret;
140 }
141