1 #include <linux/interrupt.h>
2 #include <linux/irq.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>
12
13 #include "../iio.h"
14 #include "../sysfs.h"
15 #include "../ring_sw.h"
16 #include "../accel/accel.h"
17 #include "../trigger.h"
18 #include "adis16300.h"
19
20 static IIO_SCAN_EL_C(in0_supply, ADIS16300_SCAN_SUPPLY,
21 ADIS16300_SUPPLY_OUT, NULL);
22 static IIO_CONST_ATTR_SCAN_EL_TYPE(in0_supply, u, 12, 16);
23 static IIO_SCAN_EL_C(gyro_x, ADIS16300_SCAN_GYRO_X, ADIS16300_XGYRO_OUT, NULL);
24 static IIO_CONST_ATTR_SCAN_EL_TYPE(gyro, s, 14, 16);
25
26 static IIO_SCAN_EL_C(accel_x, ADIS16300_SCAN_ACC_X, ADIS16300_XACCL_OUT, NULL);
27 static IIO_SCAN_EL_C(accel_y, ADIS16300_SCAN_ACC_Y, ADIS16300_YACCL_OUT, NULL);
28 static IIO_SCAN_EL_C(accel_z, ADIS16300_SCAN_ACC_Z, ADIS16300_ZACCL_OUT, NULL);
29 static IIO_CONST_ATTR_SCAN_EL_TYPE(accel, s, 14, 16);
30
31 static IIO_SCAN_EL_C(temp, ADIS16300_SCAN_TEMP, ADIS16300_TEMP_OUT, NULL);
32 static IIO_CONST_ATTR_SCAN_EL_TYPE(temp, s, 12, 16);
33
34 static IIO_SCAN_EL_C(in1, ADIS16300_SCAN_ADC_0, ADIS16300_AUX_ADC, NULL);
35 static IIO_CONST_ATTR_SCAN_EL_TYPE(in1, u, 12, 16);
36
37 static IIO_SCAN_EL_C(incli_x, ADIS16300_SCAN_INCLI_X,
38 ADIS16300_XINCLI_OUT, NULL);
39 static IIO_SCAN_EL_C(incli_y, ADIS16300_SCAN_INCLI_Y,
40 ADIS16300_YINCLI_OUT, NULL);
41 static IIO_CONST_ATTR_SCAN_EL_TYPE(incli, s, 13, 16);
42
43 static IIO_SCAN_EL_TIMESTAMP(9);
44 static IIO_CONST_ATTR_SCAN_EL_TYPE(timestamp, s, 64, 64);
45
46 static struct attribute *adis16300_scan_el_attrs[] = {
47 &iio_scan_el_in0_supply.dev_attr.attr,
48 &iio_const_attr_in0_supply_index.dev_attr.attr,
49 &iio_const_attr_in0_supply_type.dev_attr.attr,
50 &iio_scan_el_gyro_x.dev_attr.attr,
51 &iio_const_attr_gyro_x_index.dev_attr.attr,
52 &iio_const_attr_gyro_type.dev_attr.attr,
53 &iio_scan_el_temp.dev_attr.attr,
54 &iio_const_attr_temp_index.dev_attr.attr,
55 &iio_const_attr_temp_type.dev_attr.attr,
56 &iio_scan_el_accel_x.dev_attr.attr,
57 &iio_const_attr_accel_x_index.dev_attr.attr,
58 &iio_scan_el_accel_y.dev_attr.attr,
59 &iio_const_attr_accel_y_index.dev_attr.attr,
60 &iio_scan_el_accel_z.dev_attr.attr,
61 &iio_const_attr_accel_z_index.dev_attr.attr,
62 &iio_const_attr_accel_type.dev_attr.attr,
63 &iio_scan_el_incli_x.dev_attr.attr,
64 &iio_const_attr_incli_x_index.dev_attr.attr,
65 &iio_scan_el_incli_y.dev_attr.attr,
66 &iio_const_attr_incli_y_index.dev_attr.attr,
67 &iio_const_attr_incli_type.dev_attr.attr,
68 &iio_scan_el_in1.dev_attr.attr,
69 &iio_const_attr_in1_index.dev_attr.attr,
70 &iio_const_attr_in1_type.dev_attr.attr,
71 &iio_scan_el_timestamp.dev_attr.attr,
72 &iio_const_attr_timestamp_index.dev_attr.attr,
73 &iio_const_attr_timestamp_type.dev_attr.attr,
74 NULL,
75 };
76
77 static struct attribute_group adis16300_scan_el_group = {
78 .attrs = adis16300_scan_el_attrs,
79 .name = "scan_elements",
80 };
81
82 /**
83 * adis16300_poll_func_th() top half interrupt handler called by trigger
84 * @private_data: iio_dev
85 **/
adis16300_poll_func_th(struct iio_dev * indio_dev,s64 time)86 static void adis16300_poll_func_th(struct iio_dev *indio_dev, s64 time)
87 {
88 struct adis16300_state *st = iio_dev_get_devdata(indio_dev);
89 st->last_timestamp = time;
90 schedule_work(&st->work_trigger_to_ring);
91 /* Indicate that this interrupt is being handled */
92
93 /* Technically this is trigger related, but without this
94 * handler running there is currently no way for the interrupt
95 * to clear.
96 */
97 }
98
99 /**
100 * adis16300_spi_read_burst() - read all data registers
101 * @dev: device associated with child of actual device (iio_dev or iio_trig)
102 * @rx: somewhere to pass back the value read (min size is 24 bytes)
103 **/
adis16300_spi_read_burst(struct device * dev,u8 * rx)104 static int adis16300_spi_read_burst(struct device *dev, u8 *rx)
105 {
106 struct spi_message msg;
107 struct iio_dev *indio_dev = dev_get_drvdata(dev);
108 struct adis16300_state *st = iio_dev_get_devdata(indio_dev);
109 u32 old_speed_hz = st->us->max_speed_hz;
110 int ret;
111
112 struct spi_transfer xfers[] = {
113 {
114 .tx_buf = st->tx,
115 .bits_per_word = 8,
116 .len = 2,
117 .cs_change = 0,
118 }, {
119 .rx_buf = rx,
120 .bits_per_word = 8,
121 .len = 18,
122 .cs_change = 0,
123 },
124 };
125
126 mutex_lock(&st->buf_lock);
127 st->tx[0] = ADIS16300_READ_REG(ADIS16300_GLOB_CMD);
128 st->tx[1] = 0;
129
130 spi_message_init(&msg);
131 spi_message_add_tail(&xfers[0], &msg);
132 spi_message_add_tail(&xfers[1], &msg);
133
134 st->us->max_speed_hz = ADIS16300_SPI_BURST;
135 spi_setup(st->us);
136
137 ret = spi_sync(st->us, &msg);
138 if (ret)
139 dev_err(&st->us->dev, "problem when burst reading");
140
141 st->us->max_speed_hz = old_speed_hz;
142 spi_setup(st->us);
143 mutex_unlock(&st->buf_lock);
144 return ret;
145 }
146
147 /* Whilst this makes a lot of calls to iio_sw_ring functions - it is to device
148 * specific to be rolled into the core.
149 */
adis16300_trigger_bh_to_ring(struct work_struct * work_s)150 static void adis16300_trigger_bh_to_ring(struct work_struct *work_s)
151 {
152 struct adis16300_state *st
153 = container_of(work_s, struct adis16300_state,
154 work_trigger_to_ring);
155 struct iio_ring_buffer *ring = st->indio_dev->ring;
156
157 int i = 0;
158 s16 *data;
159 size_t datasize = ring->access.get_bytes_per_datum(ring);
160
161 data = kmalloc(datasize , GFP_KERNEL);
162 if (data == NULL) {
163 dev_err(&st->us->dev, "memory alloc failed in ring bh");
164 return;
165 }
166
167 if (ring->scan_count)
168 if (adis16300_spi_read_burst(&st->indio_dev->dev, st->rx) >= 0)
169 for (; i < ring->scan_count; i++)
170 data[i] = be16_to_cpup(
171 (__be16 *)&(st->rx[i*2]));
172
173 /* Guaranteed to be aligned with 8 byte boundary */
174 if (ring->scan_timestamp)
175 *((s64 *)(data + ((i + 3)/4)*4)) = st->last_timestamp;
176
177 ring->access.store_to(ring,
178 (u8 *)data,
179 st->last_timestamp);
180
181 iio_trigger_notify_done(st->indio_dev->trig);
182 kfree(data);
183
184 return;
185 }
186
adis16300_unconfigure_ring(struct iio_dev * indio_dev)187 void adis16300_unconfigure_ring(struct iio_dev *indio_dev)
188 {
189 kfree(indio_dev->pollfunc);
190 iio_sw_rb_free(indio_dev->ring);
191 }
192
adis16300_configure_ring(struct iio_dev * indio_dev)193 int adis16300_configure_ring(struct iio_dev *indio_dev)
194 {
195 int ret = 0;
196 struct adis16300_state *st = indio_dev->dev_data;
197 struct iio_ring_buffer *ring;
198 INIT_WORK(&st->work_trigger_to_ring, adis16300_trigger_bh_to_ring);
199
200 ring = iio_sw_rb_allocate(indio_dev);
201 if (!ring) {
202 ret = -ENOMEM;
203 return ret;
204 }
205 indio_dev->ring = ring;
206 /* Effectively select the ring buffer implementation */
207 iio_ring_sw_register_funcs(&ring->access);
208 ring->bpe = 2;
209 ring->scan_el_attrs = &adis16300_scan_el_group;
210 ring->scan_timestamp = true;
211 ring->preenable = &iio_sw_ring_preenable;
212 ring->postenable = &iio_triggered_ring_postenable;
213 ring->predisable = &iio_triggered_ring_predisable;
214 ring->owner = THIS_MODULE;
215
216 /* Set default scan mode */
217 iio_scan_mask_set(ring, iio_scan_el_in0_supply.number);
218 iio_scan_mask_set(ring, iio_scan_el_gyro_x.number);
219 iio_scan_mask_set(ring, iio_scan_el_accel_x.number);
220 iio_scan_mask_set(ring, iio_scan_el_accel_y.number);
221 iio_scan_mask_set(ring, iio_scan_el_accel_z.number);
222 iio_scan_mask_set(ring, iio_scan_el_temp.number);
223 iio_scan_mask_set(ring, iio_scan_el_in1.number);
224 iio_scan_mask_set(ring, iio_scan_el_incli_x.number);
225 iio_scan_mask_set(ring, iio_scan_el_incli_y.number);
226
227 ret = iio_alloc_pollfunc(indio_dev, NULL, &adis16300_poll_func_th);
228 if (ret)
229 goto error_iio_sw_rb_free;
230
231 indio_dev->modes |= INDIO_RING_TRIGGERED;
232 return 0;
233
234 error_iio_sw_rb_free:
235 iio_sw_rb_free(indio_dev->ring);
236 return ret;
237 }
238
239