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 "adis16300.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, ADIS16300_SCAN_SUPPLY, IIO_SIGNED(14),
31 ADIS16300_SUPPLY_OUT, NULL);
33 static IIO_SCAN_EL_C(gyro_x, ADIS16300_SCAN_GYRO_X, IIO_SIGNED(14),
34 ADIS16300_XGYRO_OUT, NULL);
36 static IIO_SCAN_EL_C(accel_x, ADIS16300_SCAN_ACC_X, IIO_SIGNED(14),
37 ADIS16300_XACCL_OUT, NULL);
38 static IIO_SCAN_EL_C(accel_y, ADIS16300_SCAN_ACC_Y, IIO_SIGNED(14),
39 ADIS16300_YACCL_OUT, NULL);
40 static IIO_SCAN_EL_C(accel_z, ADIS16300_SCAN_ACC_Z, IIO_SIGNED(14),
41 ADIS16300_ZACCL_OUT, NULL);
43 static IIO_SCAN_EL_C(temp, ADIS16300_SCAN_TEMP, IIO_SIGNED(12),
44 ADIS16300_TEMP_OUT, NULL);
45 static IIO_SCAN_EL_C(adc_0, ADIS16300_SCAN_ADC_0, IIO_SIGNED(12),
46 ADIS16300_AUX_ADC, NULL);
48 static IIO_SCAN_EL_C(incli_x, ADIS16300_SCAN_INCLI_X, IIO_SIGNED(12),
49 ADIS16300_XINCLI_OUT, NULL);
50 static IIO_SCAN_EL_C(incli_y, ADIS16300_SCAN_INCLI_Y, IIO_SIGNED(12),
51 ADIS16300_YINCLI_OUT, NULL);
53 static IIO_SCAN_EL_TIMESTAMP(9);
55 static struct attribute *adis16300_scan_el_attrs[] = {
56 &iio_scan_el_supply.dev_attr.attr,
57 &iio_scan_el_gyro_x.dev_attr.attr,
58 &iio_scan_el_temp.dev_attr.attr,
59 &iio_scan_el_accel_x.dev_attr.attr,
60 &iio_scan_el_accel_y.dev_attr.attr,
61 &iio_scan_el_accel_z.dev_attr.attr,
62 &iio_scan_el_incli_x.dev_attr.attr,
63 &iio_scan_el_incli_y.dev_attr.attr,
64 &iio_scan_el_adc_0.dev_attr.attr,
65 &iio_scan_el_timestamp.dev_attr.attr,
69 static struct attribute_group adis16300_scan_el_group = {
70 .attrs = adis16300_scan_el_attrs,
71 .name = "scan_elements",
75 * adis16300_poll_func_th() top half interrupt handler called by trigger
76 * @private_data: iio_dev
78 static void adis16300_poll_func_th(struct iio_dev *indio_dev)
80 struct adis16300_state *st = iio_dev_get_devdata(indio_dev);
81 st->last_timestamp = indio_dev->trig->timestamp;
82 schedule_work(&st->work_trigger_to_ring);
83 /* Indicate that this interrupt is being handled */
85 /* Technically this is trigger related, but without this
86 * handler running there is currently no way for the interrupt
91 /* Whilst this makes a lot of calls to iio_sw_ring functions - it is to device
92 * specific to be rolled into the core.
94 static void adis16300_trigger_bh_to_ring(struct work_struct *work_s)
96 struct adis16300_state *st
97 = container_of(work_s, struct adis16300_state,
98 work_trigger_to_ring);
102 size_t datasize = st->indio_dev
103 ->ring->access.get_bpd(st->indio_dev->ring);
105 data = kmalloc(datasize , GFP_KERNEL);
107 dev_err(&st->us->dev, "memory alloc failed in ring bh");
111 if (st->indio_dev->scan_count)
112 if (adis16300_spi_read_burst(&st->indio_dev->dev, st->rx) >= 0)
113 for (; i < st->indio_dev->scan_count; i++) {
114 data[i] = combine_8_to_16(st->rx[i*2+1],
118 /* Guaranteed to be aligned with 8 byte boundary */
119 if (st->indio_dev->scan_timestamp)
120 *((s64 *)(data + ((i + 3)/4)*4)) = st->last_timestamp;
122 st->indio_dev->ring->access.store_to(st->indio_dev->ring,
126 iio_trigger_notify_done(st->indio_dev->trig);
131 /* in these circumstances is it better to go with unaligned packing and
132 * deal with the cost?*/
133 static int adis16300_data_rdy_ring_preenable(struct iio_dev *indio_dev)
136 dev_dbg(&indio_dev->dev, "%s\n", __func__);
137 /* Check if there are any scan elements enabled, if not fail*/
138 if (!(indio_dev->scan_count || indio_dev->scan_timestamp))
141 if (indio_dev->ring->access.set_bpd) {
142 if (indio_dev->scan_timestamp)
143 if (indio_dev->scan_count) /* Timestamp and data */
144 size = 4*sizeof(s64);
145 else /* Timestamp only */
148 size = indio_dev->scan_count*sizeof(s16);
149 indio_dev->ring->access.set_bpd(indio_dev->ring, size);
155 static int adis16300_data_rdy_ring_postenable(struct iio_dev *indio_dev)
157 return indio_dev->trig
158 ? iio_trigger_attach_poll_func(indio_dev->trig,
163 static int adis16300_data_rdy_ring_predisable(struct iio_dev *indio_dev)
165 return indio_dev->trig
166 ? iio_trigger_dettach_poll_func(indio_dev->trig,
171 void adis16300_unconfigure_ring(struct iio_dev *indio_dev)
173 kfree(indio_dev->pollfunc);
174 iio_sw_rb_free(indio_dev->ring);
177 int adis16300_configure_ring(struct iio_dev *indio_dev)
180 struct adis16300_state *st = indio_dev->dev_data;
181 struct iio_ring_buffer *ring;
182 INIT_WORK(&st->work_trigger_to_ring, adis16300_trigger_bh_to_ring);
183 /* Set default scan mode */
185 iio_scan_mask_set(indio_dev, iio_scan_el_supply.number);
186 iio_scan_mask_set(indio_dev, iio_scan_el_gyro_x.number);
187 iio_scan_mask_set(indio_dev, iio_scan_el_accel_x.number);
188 iio_scan_mask_set(indio_dev, iio_scan_el_accel_y.number);
189 iio_scan_mask_set(indio_dev, iio_scan_el_accel_z.number);
190 iio_scan_mask_set(indio_dev, iio_scan_el_temp.number);
191 iio_scan_mask_set(indio_dev, iio_scan_el_adc_0.number);
192 iio_scan_mask_set(indio_dev, iio_scan_el_incli_x.number);
193 iio_scan_mask_set(indio_dev, iio_scan_el_incli_y.number);
194 indio_dev->scan_timestamp = true;
196 indio_dev->scan_el_attrs = &adis16300_scan_el_group;
198 ring = iio_sw_rb_allocate(indio_dev);
203 indio_dev->ring = ring;
204 /* Effectively select the ring buffer implementation */
205 iio_ring_sw_register_funcs(&ring->access);
206 ring->preenable = &adis16300_data_rdy_ring_preenable;
207 ring->postenable = &adis16300_data_rdy_ring_postenable;
208 ring->predisable = &adis16300_data_rdy_ring_predisable;
209 ring->owner = THIS_MODULE;
211 indio_dev->pollfunc = kzalloc(sizeof(*indio_dev->pollfunc), GFP_KERNEL);
212 if (indio_dev->pollfunc == NULL) {
214 goto error_iio_sw_rb_free;;
216 indio_dev->pollfunc->poll_func_main = &adis16300_poll_func_th;
217 indio_dev->pollfunc->private_data = indio_dev;
218 indio_dev->modes |= INDIO_RING_TRIGGERED;
221 error_iio_sw_rb_free:
222 iio_sw_rb_free(indio_dev->ring);
226 int adis16300_initialize_ring(struct iio_ring_buffer *ring)
228 return iio_ring_buffer_register(ring, 0);
231 void adis16300_uninitialize_ring(struct iio_ring_buffer *ring)
233 iio_ring_buffer_unregister(ring);