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"
20 static IIO_SCAN_EL_C(supply, ADIS16300_SCAN_SUPPLY, IIO_UNSIGNED(14),
21 ADIS16300_SUPPLY_OUT, NULL);
23 static IIO_SCAN_EL_C(gyro_x, ADIS16300_SCAN_GYRO_X, IIO_SIGNED(14),
24 ADIS16300_XGYRO_OUT, NULL);
26 static IIO_SCAN_EL_C(accel_x, ADIS16300_SCAN_ACC_X, IIO_SIGNED(14),
27 ADIS16300_XACCL_OUT, NULL);
28 static IIO_SCAN_EL_C(accel_y, ADIS16300_SCAN_ACC_Y, IIO_SIGNED(14),
29 ADIS16300_YACCL_OUT, NULL);
30 static IIO_SCAN_EL_C(accel_z, ADIS16300_SCAN_ACC_Z, IIO_SIGNED(14),
31 ADIS16300_ZACCL_OUT, NULL);
33 static IIO_SCAN_EL_C(temp, ADIS16300_SCAN_TEMP, IIO_UNSIGNED(12),
34 ADIS16300_TEMP_OUT, NULL);
35 static IIO_SCAN_EL_C(adc_0, ADIS16300_SCAN_ADC_0, IIO_UNSIGNED(12),
36 ADIS16300_AUX_ADC, NULL);
38 static IIO_SCAN_EL_C(incli_x, ADIS16300_SCAN_INCLI_X, IIO_SIGNED(12),
39 ADIS16300_XINCLI_OUT, NULL);
40 static IIO_SCAN_EL_C(incli_y, ADIS16300_SCAN_INCLI_Y, IIO_SIGNED(12),
41 ADIS16300_YINCLI_OUT, NULL);
43 static IIO_SCAN_EL_TIMESTAMP(9);
45 static struct attribute *adis16300_scan_el_attrs[] = {
46 &iio_scan_el_supply.dev_attr.attr,
47 &iio_scan_el_gyro_x.dev_attr.attr,
48 &iio_scan_el_temp.dev_attr.attr,
49 &iio_scan_el_accel_x.dev_attr.attr,
50 &iio_scan_el_accel_y.dev_attr.attr,
51 &iio_scan_el_accel_z.dev_attr.attr,
52 &iio_scan_el_incli_x.dev_attr.attr,
53 &iio_scan_el_incli_y.dev_attr.attr,
54 &iio_scan_el_adc_0.dev_attr.attr,
55 &iio_scan_el_timestamp.dev_attr.attr,
59 static struct attribute_group adis16300_scan_el_group = {
60 .attrs = adis16300_scan_el_attrs,
61 .name = "scan_elements",
65 * adis16300_poll_func_th() top half interrupt handler called by trigger
66 * @private_data: iio_dev
68 static void adis16300_poll_func_th(struct iio_dev *indio_dev, s64 time)
70 struct adis16300_state *st = iio_dev_get_devdata(indio_dev);
71 st->last_timestamp = time;
72 schedule_work(&st->work_trigger_to_ring);
73 /* Indicate that this interrupt is being handled */
75 /* Technically this is trigger related, but without this
76 * handler running there is currently no way for the interrupt
82 * adis16300_spi_read_burst() - read all data registers
83 * @dev: device associated with child of actual device (iio_dev or iio_trig)
84 * @rx: somewhere to pass back the value read (min size is 24 bytes)
86 static int adis16300_spi_read_burst(struct device *dev, u8 *rx)
88 struct spi_message msg;
89 struct iio_dev *indio_dev = dev_get_drvdata(dev);
90 struct adis16300_state *st = iio_dev_get_devdata(indio_dev);
91 u32 old_speed_hz = st->us->max_speed_hz;
94 struct spi_transfer xfers[] = {
108 mutex_lock(&st->buf_lock);
109 st->tx[0] = ADIS16300_READ_REG(ADIS16300_GLOB_CMD);
112 spi_message_init(&msg);
113 spi_message_add_tail(&xfers[0], &msg);
114 spi_message_add_tail(&xfers[1], &msg);
116 st->us->max_speed_hz = ADIS16300_SPI_BURST;
119 ret = spi_sync(st->us, &msg);
121 dev_err(&st->us->dev, "problem when burst reading");
123 st->us->max_speed_hz = old_speed_hz;
125 mutex_unlock(&st->buf_lock);
129 /* Whilst this makes a lot of calls to iio_sw_ring functions - it is to device
130 * specific to be rolled into the core.
132 static void adis16300_trigger_bh_to_ring(struct work_struct *work_s)
134 struct adis16300_state *st
135 = container_of(work_s, struct adis16300_state,
136 work_trigger_to_ring);
137 struct iio_ring_buffer *ring = st->indio_dev->ring;
141 size_t datasize = ring->access.get_bytes_per_datum(ring);
143 data = kmalloc(datasize , GFP_KERNEL);
145 dev_err(&st->us->dev, "memory alloc failed in ring bh");
149 if (ring->scan_count)
150 if (adis16300_spi_read_burst(&st->indio_dev->dev, st->rx) >= 0)
151 for (; i < ring->scan_count; i++)
152 data[i] = be16_to_cpup(
153 (__be16 *)&(st->rx[i*2]));
155 /* Guaranteed to be aligned with 8 byte boundary */
156 if (ring->scan_timestamp)
157 *((s64 *)(data + ((i + 3)/4)*4)) = st->last_timestamp;
159 ring->access.store_to(ring,
163 iio_trigger_notify_done(st->indio_dev->trig);
169 void adis16300_unconfigure_ring(struct iio_dev *indio_dev)
171 kfree(indio_dev->pollfunc);
172 iio_sw_rb_free(indio_dev->ring);
175 int adis16300_configure_ring(struct iio_dev *indio_dev)
178 struct adis16300_state *st = indio_dev->dev_data;
179 struct iio_ring_buffer *ring;
180 INIT_WORK(&st->work_trigger_to_ring, adis16300_trigger_bh_to_ring);
182 ring = iio_sw_rb_allocate(indio_dev);
187 indio_dev->ring = ring;
188 /* Effectively select the ring buffer implementation */
189 iio_ring_sw_register_funcs(&ring->access);
191 ring->scan_el_attrs = &adis16300_scan_el_group;
192 ring->scan_timestamp = true;
193 ring->preenable = &iio_sw_ring_preenable;
194 ring->postenable = &iio_triggered_ring_postenable;
195 ring->predisable = &iio_triggered_ring_predisable;
196 ring->owner = THIS_MODULE;
198 /* Set default scan mode */
199 iio_scan_mask_set(ring, iio_scan_el_supply.number);
200 iio_scan_mask_set(ring, iio_scan_el_gyro_x.number);
201 iio_scan_mask_set(ring, iio_scan_el_accel_x.number);
202 iio_scan_mask_set(ring, iio_scan_el_accel_y.number);
203 iio_scan_mask_set(ring, iio_scan_el_accel_z.number);
204 iio_scan_mask_set(ring, iio_scan_el_temp.number);
205 iio_scan_mask_set(ring, iio_scan_el_adc_0.number);
206 iio_scan_mask_set(ring, iio_scan_el_incli_x.number);
207 iio_scan_mask_set(ring, iio_scan_el_incli_y.number);
209 ret = iio_alloc_pollfunc(indio_dev, NULL, &adis16300_poll_func_th);
211 goto error_iio_sw_rb_free;
213 indio_dev->modes |= INDIO_RING_TRIGGERED;
216 error_iio_sw_rb_free:
217 iio_sw_rb_free(indio_dev->ring);