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 "adis16350.h"
21 * combine_8_to_16() utility function to munge to u8s into u16
23 static inline u16 combine_8_to_16(u8 lower, u8 upper)
27 return _lower | (_upper << 8);
30 static IIO_SCAN_EL_C(supply, ADIS16350_SCAN_SUPPLY, IIO_UNSIGNED(12),
31 ADIS16350_SUPPLY_OUT, NULL);
33 static IIO_SCAN_EL_C(gyro_x, ADIS16350_SCAN_GYRO_X, IIO_SIGNED(14),
34 ADIS16350_XGYRO_OUT, NULL);
35 static IIO_SCAN_EL_C(gyro_y, ADIS16350_SCAN_GYRO_Y, IIO_SIGNED(14),
36 ADIS16350_YGYRO_OUT, NULL);
37 static IIO_SCAN_EL_C(gyro_z, ADIS16350_SCAN_GYRO_Z, IIO_SIGNED(14),
38 ADIS16350_ZGYRO_OUT, NULL);
40 static IIO_SCAN_EL_C(accel_x, ADIS16350_SCAN_ACC_X, IIO_SIGNED(14),
41 ADIS16350_XACCL_OUT, NULL);
42 static IIO_SCAN_EL_C(accel_y, ADIS16350_SCAN_ACC_Y, IIO_SIGNED(14),
43 ADIS16350_YACCL_OUT, NULL);
44 static IIO_SCAN_EL_C(accel_z, ADIS16350_SCAN_ACC_Z, IIO_SIGNED(14),
45 ADIS16350_ZACCL_OUT, NULL);
47 static IIO_SCAN_EL_C(temp_x, ADIS16350_SCAN_TEMP_X, IIO_SIGNED(12),
48 ADIS16350_XTEMP_OUT, NULL);
49 static IIO_SCAN_EL_C(temp_y, ADIS16350_SCAN_TEMP_Y, IIO_SIGNED(12),
50 ADIS16350_YTEMP_OUT, NULL);
51 static IIO_SCAN_EL_C(temp_z, ADIS16350_SCAN_TEMP_Z, IIO_SIGNED(12),
52 ADIS16350_ZTEMP_OUT, NULL);
54 static IIO_SCAN_EL_C(adc_0, ADIS16350_SCAN_ADC_0, IIO_UNSIGNED(12),
55 ADIS16350_AUX_ADC, NULL);
57 static IIO_SCAN_EL_TIMESTAMP(11);
59 static struct attribute *adis16350_scan_el_attrs[] = {
60 &iio_scan_el_supply.dev_attr.attr,
61 &iio_scan_el_gyro_x.dev_attr.attr,
62 &iio_scan_el_gyro_y.dev_attr.attr,
63 &iio_scan_el_gyro_z.dev_attr.attr,
64 &iio_scan_el_accel_x.dev_attr.attr,
65 &iio_scan_el_accel_y.dev_attr.attr,
66 &iio_scan_el_accel_z.dev_attr.attr,
67 &iio_scan_el_temp_x.dev_attr.attr,
68 &iio_scan_el_temp_y.dev_attr.attr,
69 &iio_scan_el_temp_z.dev_attr.attr,
70 &iio_scan_el_adc_0.dev_attr.attr,
71 &iio_scan_el_timestamp.dev_attr.attr,
75 static struct attribute_group adis16350_scan_el_group = {
76 .attrs = adis16350_scan_el_attrs,
77 .name = "scan_elements",
81 * adis16350_poll_func_th() top half interrupt handler called by trigger
82 * @private_data: iio_dev
84 static void adis16350_poll_func_th(struct iio_dev *indio_dev)
86 struct adis16350_state *st = iio_dev_get_devdata(indio_dev);
87 st->last_timestamp = indio_dev->trig->timestamp;
88 schedule_work(&st->work_trigger_to_ring);
92 * adis16350_spi_read_burst() - read all data registers
93 * @dev: device associated with child of actual device (iio_dev or iio_trig)
94 * @rx: somewhere to pass back the value read (min size is 24 bytes)
96 static int adis16350_spi_read_burst(struct device *dev, u8 *rx)
98 struct spi_message msg;
99 struct iio_dev *indio_dev = dev_get_drvdata(dev);
100 struct adis16350_state *st = iio_dev_get_devdata(indio_dev);
101 u32 old_speed_hz = st->us->max_speed_hz;
104 struct spi_transfer xfers[] = {
118 mutex_lock(&st->buf_lock);
119 st->tx[0] = ADIS16350_READ_REG(ADIS16350_GLOB_CMD);
122 spi_message_init(&msg);
123 spi_message_add_tail(&xfers[0], &msg);
124 spi_message_add_tail(&xfers[1], &msg);
126 st->us->max_speed_hz = ADIS16350_SPI_BURST;
129 ret = spi_sync(st->us, &msg);
131 dev_err(&st->us->dev, "problem when burst reading");
133 st->us->max_speed_hz = old_speed_hz;
135 mutex_unlock(&st->buf_lock);
139 /* Whilst this makes a lot of calls to iio_sw_ring functions - it is to device
140 * specific to be rolled into the core.
142 static void adis16350_trigger_bh_to_ring(struct work_struct *work_s)
144 struct adis16350_state *st
145 = container_of(work_s, struct adis16350_state,
146 work_trigger_to_ring);
150 size_t datasize = st->indio_dev
151 ->ring->access.get_bpd(st->indio_dev->ring);
153 data = kmalloc(datasize , GFP_KERNEL);
155 dev_err(&st->us->dev, "memory alloc failed in ring bh");
159 if (st->indio_dev->scan_count)
160 if (adis16350_spi_read_burst(&st->indio_dev->dev, st->rx) >= 0)
161 for (; i < st->indio_dev->scan_count; i++) {
162 data[i] = combine_8_to_16(st->rx[i*2+1],
166 /* Guaranteed to be aligned with 8 byte boundary */
167 if (st->indio_dev->scan_timestamp)
168 *((s64 *)(data + ((i + 3)/4)*4)) = st->last_timestamp;
170 st->indio_dev->ring->access.store_to(st->indio_dev->ring,
174 iio_trigger_notify_done(st->indio_dev->trig);
180 static int adis16350_data_rdy_ring_preenable(struct iio_dev *indio_dev)
183 dev_dbg(&indio_dev->dev, "%s\n", __func__);
184 /* Check if there are any scan elements enabled, if not fail*/
185 if (!(indio_dev->scan_count || indio_dev->scan_timestamp))
188 if (indio_dev->ring->access.set_bpd) {
189 if (indio_dev->scan_timestamp)
190 if (indio_dev->scan_count)
191 /* Timestamp (aligned sizeof(s64) and data */
192 size = (((indio_dev->scan_count * sizeof(s16))
194 & ~(sizeof(s64) - 1))
196 else /* Timestamp only */
199 size = indio_dev->scan_count*sizeof(s16);
200 indio_dev->ring->access.set_bpd(indio_dev->ring, size);
206 static int adis16350_data_rdy_ring_postenable(struct iio_dev *indio_dev)
208 return indio_dev->trig
209 ? iio_trigger_attach_poll_func(indio_dev->trig,
214 static int adis16350_data_rdy_ring_predisable(struct iio_dev *indio_dev)
216 return indio_dev->trig
217 ? iio_trigger_dettach_poll_func(indio_dev->trig,
222 void adis16350_unconfigure_ring(struct iio_dev *indio_dev)
224 kfree(indio_dev->pollfunc);
225 iio_sw_rb_free(indio_dev->ring);
228 int adis16350_configure_ring(struct iio_dev *indio_dev)
231 struct adis16350_state *st = indio_dev->dev_data;
232 struct iio_ring_buffer *ring;
233 INIT_WORK(&st->work_trigger_to_ring, adis16350_trigger_bh_to_ring);
234 /* Set default scan mode */
236 iio_scan_mask_set(indio_dev, iio_scan_el_supply.number);
237 iio_scan_mask_set(indio_dev, iio_scan_el_gyro_x.number);
238 iio_scan_mask_set(indio_dev, iio_scan_el_gyro_y.number);
239 iio_scan_mask_set(indio_dev, iio_scan_el_gyro_z.number);
240 iio_scan_mask_set(indio_dev, iio_scan_el_accel_x.number);
241 iio_scan_mask_set(indio_dev, iio_scan_el_accel_y.number);
242 iio_scan_mask_set(indio_dev, iio_scan_el_accel_z.number);
243 iio_scan_mask_set(indio_dev, iio_scan_el_temp_x.number);
244 iio_scan_mask_set(indio_dev, iio_scan_el_temp_y.number);
245 iio_scan_mask_set(indio_dev, iio_scan_el_temp_z.number);
246 iio_scan_mask_set(indio_dev, iio_scan_el_adc_0.number);
247 indio_dev->scan_timestamp = true;
249 indio_dev->scan_el_attrs = &adis16350_scan_el_group;
251 ring = iio_sw_rb_allocate(indio_dev);
256 indio_dev->ring = ring;
257 /* Effectively select the ring buffer implementation */
258 iio_ring_sw_register_funcs(&ring->access);
259 ring->preenable = &adis16350_data_rdy_ring_preenable;
260 ring->postenable = &adis16350_data_rdy_ring_postenable;
261 ring->predisable = &adis16350_data_rdy_ring_predisable;
262 ring->owner = THIS_MODULE;
264 indio_dev->pollfunc = kzalloc(sizeof(*indio_dev->pollfunc), GFP_KERNEL);
265 if (indio_dev->pollfunc == NULL) {
267 goto error_iio_sw_rb_free;;
269 indio_dev->pollfunc->poll_func_main = &adis16350_poll_func_th;
270 indio_dev->pollfunc->private_data = indio_dev;
271 indio_dev->modes |= INDIO_RING_TRIGGERED;
274 error_iio_sw_rb_free:
275 iio_sw_rb_free(indio_dev->ring);
279 int adis16350_initialize_ring(struct iio_ring_buffer *ring)
281 return iio_ring_buffer_register(ring, 0);
284 void adis16350_uninitialize_ring(struct iio_ring_buffer *ring)
286 iio_ring_buffer_unregister(ring);