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 "adis16400.h"
20 static IIO_SCAN_EL_C(in_supply, ADIS16400_SCAN_SUPPLY,
21 ADIS16400_SUPPLY_OUT, NULL);
22 static IIO_CONST_ATTR_SCAN_EL_TYPE(in_supply, u, 14, 16);
24 static IIO_SCAN_EL_C(gyro_x, ADIS16400_SCAN_GYRO_X, ADIS16400_XGYRO_OUT, NULL);
25 static IIO_SCAN_EL_C(gyro_y, ADIS16400_SCAN_GYRO_Y, ADIS16400_YGYRO_OUT, NULL);
26 static IIO_SCAN_EL_C(gyro_z, ADIS16400_SCAN_GYRO_Z, ADIS16400_ZGYRO_OUT, NULL);
27 static IIO_CONST_ATTR_SCAN_EL_TYPE(gyro, s, 14, 16);
29 static IIO_SCAN_EL_C(accel_x, ADIS16400_SCAN_ACC_X, ADIS16400_XACCL_OUT, NULL);
30 static IIO_SCAN_EL_C(accel_y, ADIS16400_SCAN_ACC_Y, ADIS16400_YACCL_OUT, NULL);
31 static IIO_SCAN_EL_C(accel_z, ADIS16400_SCAN_ACC_Z, ADIS16400_ZACCL_OUT, NULL);
32 static IIO_CONST_ATTR_SCAN_EL_TYPE(accel, s, 14, 16);
34 static IIO_SCAN_EL_C(magn_x, ADIS16400_SCAN_MAGN_X, ADIS16400_XMAGN_OUT, NULL);
35 static IIO_SCAN_EL_C(magn_y, ADIS16400_SCAN_MAGN_Y, ADIS16400_YMAGN_OUT, NULL);
36 static IIO_SCAN_EL_C(magn_z, ADIS16400_SCAN_MAGN_Z, ADIS16400_ZMAGN_OUT, NULL);
37 static IIO_CONST_ATTR_SCAN_EL_TYPE(magn, s, 14, 16);
39 static IIO_SCAN_EL_C(temp, ADIS16400_SCAN_TEMP, ADIS16400_TEMP_OUT, NULL);
40 static IIO_CONST_ATTR_SCAN_EL_TYPE(temp, s, 12, 16);
42 static IIO_SCAN_EL_C(in0, ADIS16400_SCAN_ADC_0, ADIS16400_AUX_ADC, NULL);
43 static IIO_CONST_ATTR_SCAN_EL_TYPE(in0, u, 12, 16);
45 static IIO_SCAN_EL_TIMESTAMP(12);
46 static IIO_CONST_ATTR_SCAN_EL_TYPE(timestamp, s, 64, 64);
48 static struct attribute *adis16400_scan_el_attrs[] = {
49 &iio_scan_el_in_supply.dev_attr.attr,
50 &iio_const_attr_in_supply_index.dev_attr.attr,
51 &iio_const_attr_in_supply_type.dev_attr.attr,
52 &iio_scan_el_gyro_x.dev_attr.attr,
53 &iio_const_attr_gyro_x_index.dev_attr.attr,
54 &iio_scan_el_gyro_y.dev_attr.attr,
55 &iio_const_attr_gyro_y_index.dev_attr.attr,
56 &iio_scan_el_gyro_z.dev_attr.attr,
57 &iio_const_attr_gyro_z_index.dev_attr.attr,
58 &iio_const_attr_gyro_type.dev_attr.attr,
59 &iio_scan_el_accel_x.dev_attr.attr,
60 &iio_const_attr_accel_x_index.dev_attr.attr,
61 &iio_scan_el_accel_y.dev_attr.attr,
62 &iio_const_attr_accel_y_index.dev_attr.attr,
63 &iio_scan_el_accel_z.dev_attr.attr,
64 &iio_const_attr_accel_z_index.dev_attr.attr,
65 &iio_const_attr_accel_type.dev_attr.attr,
66 &iio_scan_el_magn_x.dev_attr.attr,
67 &iio_const_attr_magn_x_index.dev_attr.attr,
68 &iio_scan_el_magn_y.dev_attr.attr,
69 &iio_const_attr_magn_y_index.dev_attr.attr,
70 &iio_scan_el_magn_z.dev_attr.attr,
71 &iio_const_attr_magn_z_index.dev_attr.attr,
72 &iio_const_attr_magn_type.dev_attr.attr,
73 &iio_scan_el_temp.dev_attr.attr,
74 &iio_const_attr_temp_index.dev_attr.attr,
75 &iio_const_attr_temp_type.dev_attr.attr,
76 &iio_scan_el_in0.dev_attr.attr,
77 &iio_const_attr_in0_index.dev_attr.attr,
78 &iio_const_attr_in0_type.dev_attr.attr,
79 &iio_scan_el_timestamp.dev_attr.attr,
80 &iio_const_attr_timestamp_index.dev_attr.attr,
81 &iio_const_attr_timestamp_type.dev_attr.attr,
85 static struct attribute_group adis16400_scan_el_group = {
86 .attrs = adis16400_scan_el_attrs,
87 .name = "scan_elements",
91 * adis16400_poll_func_th() top half interrupt handler called by trigger
92 * @private_data: iio_dev
94 static void adis16400_poll_func_th(struct iio_dev *indio_dev, s64 time)
96 struct adis16400_state *st = iio_dev_get_devdata(indio_dev);
97 st->last_timestamp = time;
98 schedule_work(&st->work_trigger_to_ring);
99 /* Indicate that this interrupt is being handled */
101 /* Technically this is trigger related, but without this
102 * handler running there is currently no way for the interrupt
108 * adis16400_spi_read_burst() - read all data registers
109 * @dev: device associated with child of actual device (iio_dev or iio_trig)
110 * @rx: somewhere to pass back the value read (min size is 24 bytes)
112 static int adis16400_spi_read_burst(struct device *dev, u8 *rx)
114 struct spi_message msg;
115 struct iio_dev *indio_dev = dev_get_drvdata(dev);
116 struct adis16400_state *st = iio_dev_get_devdata(indio_dev);
117 u32 old_speed_hz = st->us->max_speed_hz;
120 struct spi_transfer xfers[] = {
134 mutex_lock(&st->buf_lock);
135 st->tx[0] = ADIS16400_READ_REG(ADIS16400_GLOB_CMD);
138 spi_message_init(&msg);
139 spi_message_add_tail(&xfers[0], &msg);
140 spi_message_add_tail(&xfers[1], &msg);
142 st->us->max_speed_hz = min(ADIS16400_SPI_BURST, old_speed_hz);
145 ret = spi_sync(st->us, &msg);
147 dev_err(&st->us->dev, "problem when burst reading");
149 st->us->max_speed_hz = old_speed_hz;
151 mutex_unlock(&st->buf_lock);
155 /* Whilst this makes a lot of calls to iio_sw_ring functions - it is to device
156 * specific to be rolled into the core.
158 static void adis16400_trigger_bh_to_ring(struct work_struct *work_s)
160 struct adis16400_state *st
161 = container_of(work_s, struct adis16400_state,
162 work_trigger_to_ring);
163 struct iio_ring_buffer *ring = st->indio_dev->ring;
167 size_t datasize = ring->access.get_bytes_per_datum(ring);
169 data = kmalloc(datasize , GFP_KERNEL);
171 dev_err(&st->us->dev, "memory alloc failed in ring bh");
175 if (ring->scan_count)
176 if (adis16400_spi_read_burst(&st->indio_dev->dev, st->rx) >= 0)
177 for (; i < ring->scan_count; i++)
178 data[i] = be16_to_cpup(
179 (__be16 *)&(st->rx[i*2]));
181 /* Guaranteed to be aligned with 8 byte boundary */
182 if (ring->scan_timestamp)
183 *((s64 *)(data + ((i + 3)/4)*4)) = st->last_timestamp;
185 ring->access.store_to(ring,
189 iio_trigger_notify_done(st->indio_dev->trig);
195 void adis16400_unconfigure_ring(struct iio_dev *indio_dev)
197 kfree(indio_dev->pollfunc);
198 iio_sw_rb_free(indio_dev->ring);
201 int adis16400_configure_ring(struct iio_dev *indio_dev)
204 struct adis16400_state *st = indio_dev->dev_data;
205 struct iio_ring_buffer *ring;
206 INIT_WORK(&st->work_trigger_to_ring, adis16400_trigger_bh_to_ring);
208 ring = iio_sw_rb_allocate(indio_dev);
213 indio_dev->ring = ring;
214 /* Effectively select the ring buffer implementation */
215 iio_ring_sw_register_funcs(&ring->access);
217 ring->scan_el_attrs = &adis16400_scan_el_group;
218 ring->scan_timestamp = true;
219 ring->preenable = &iio_sw_ring_preenable;
220 ring->postenable = &iio_triggered_ring_postenable;
221 ring->predisable = &iio_triggered_ring_predisable;
222 ring->owner = THIS_MODULE;
224 /* Set default scan mode */
225 iio_scan_mask_set(ring, iio_scan_el_in_supply.number);
226 iio_scan_mask_set(ring, iio_scan_el_gyro_x.number);
227 iio_scan_mask_set(ring, iio_scan_el_gyro_y.number);
228 iio_scan_mask_set(ring, iio_scan_el_gyro_z.number);
229 iio_scan_mask_set(ring, iio_scan_el_accel_x.number);
230 iio_scan_mask_set(ring, iio_scan_el_accel_y.number);
231 iio_scan_mask_set(ring, iio_scan_el_accel_z.number);
232 iio_scan_mask_set(ring, iio_scan_el_magn_x.number);
233 iio_scan_mask_set(ring, iio_scan_el_magn_y.number);
234 iio_scan_mask_set(ring, iio_scan_el_magn_z.number);
235 iio_scan_mask_set(ring, iio_scan_el_temp.number);
236 iio_scan_mask_set(ring, iio_scan_el_in0.number);
238 ret = iio_alloc_pollfunc(indio_dev, NULL, &adis16400_poll_func_th);
240 goto error_iio_sw_rb_free;
242 indio_dev->modes |= INDIO_RING_TRIGGERED;
245 error_iio_sw_rb_free:
246 iio_sw_rb_free(indio_dev->ring);