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"
17 #include "../trigger.h"
18 #include "adis16240.h"
20 static IIO_SCAN_EL_C(in_supply, ADIS16240_SCAN_SUPPLY, IIO_UNSIGNED(10),
21 ADIS16240_SUPPLY_OUT, NULL);
22 static IIO_CONST_ATTR_SCAN_EL_TYPE(in_supply, u, 10, 16);
23 static IIO_SCAN_EL_C(accel_x, ADIS16240_SCAN_ACC_X, IIO_SIGNED(10),
24 ADIS16240_XACCL_OUT, NULL);
25 static IIO_SCAN_EL_C(accel_y, ADIS16240_SCAN_ACC_Y, IIO_SIGNED(10),
26 ADIS16240_YACCL_OUT, NULL);
27 static IIO_SCAN_EL_C(accel_z, ADIS16240_SCAN_ACC_Z, IIO_SIGNED(10),
28 ADIS16240_ZACCL_OUT, NULL);
29 static IIO_CONST_ATTR_SCAN_EL_TYPE(accel, s, 10, 16);
30 static IIO_SCAN_EL_C(in0, ADIS16240_SCAN_AUX_ADC, IIO_UNSIGNED(10),
31 ADIS16240_AUX_ADC, NULL);
32 static IIO_CONST_ATTR_SCAN_EL_TYPE(in0, u, 10, 16);
33 static IIO_SCAN_EL_C(temp, ADIS16240_SCAN_TEMP, IIO_UNSIGNED(10),
34 ADIS16240_TEMP_OUT, NULL);
35 static IIO_CONST_ATTR_SCAN_EL_TYPE(temp, u, 10, 16);
37 static IIO_SCAN_EL_TIMESTAMP(6);
38 static IIO_CONST_ATTR_SCAN_EL_TYPE(timestamp, s, 64, 64);
40 static struct attribute *adis16240_scan_el_attrs[] = {
41 &iio_scan_el_in_supply.dev_attr.attr,
42 &iio_const_attr_in_supply_type.dev_attr.attr,
43 &iio_scan_el_accel_x.dev_attr.attr,
44 &iio_scan_el_accel_y.dev_attr.attr,
45 &iio_scan_el_accel_z.dev_attr.attr,
46 &iio_const_attr_accel_type.dev_attr.attr,
47 &iio_scan_el_in0.dev_attr.attr,
48 &iio_const_attr_in0_type.dev_attr.attr,
49 &iio_scan_el_temp.dev_attr.attr,
50 &iio_const_attr_temp_type.dev_attr.attr,
51 &iio_scan_el_timestamp.dev_attr.attr,
52 &iio_const_attr_timestamp_type.dev_attr.attr,
56 static struct attribute_group adis16240_scan_el_group = {
57 .attrs = adis16240_scan_el_attrs,
58 .name = "scan_elements",
62 * adis16240_poll_func_th() top half interrupt handler called by trigger
63 * @private_data: iio_dev
65 static void adis16240_poll_func_th(struct iio_dev *indio_dev, s64 time)
67 struct adis16240_state *st = iio_dev_get_devdata(indio_dev);
68 st->last_timestamp = time;
69 schedule_work(&st->work_trigger_to_ring);
73 * adis16240_read_ring_data() read data registers which will be placed into ring
74 * @dev: device associated with child of actual device (iio_dev or iio_trig)
75 * @rx: somewhere to pass back the value read
77 static int adis16240_read_ring_data(struct device *dev, u8 *rx)
79 struct spi_message msg;
80 struct iio_dev *indio_dev = dev_get_drvdata(dev);
81 struct adis16240_state *st = iio_dev_get_devdata(indio_dev);
82 struct spi_transfer xfers[ADIS16240_OUTPUTS + 1];
86 mutex_lock(&st->buf_lock);
88 spi_message_init(&msg);
90 memset(xfers, 0, sizeof(xfers));
91 for (i = 0; i <= ADIS16240_OUTPUTS; i++) {
92 xfers[i].bits_per_word = 8;
93 xfers[i].cs_change = 1;
95 xfers[i].delay_usecs = 30;
96 xfers[i].tx_buf = st->tx + 2 * i;
98 = ADIS16240_READ_REG(ADIS16240_SUPPLY_OUT + 2 * i);
99 st->tx[2 * i + 1] = 0;
101 xfers[i].rx_buf = rx + 2 * (i - 1);
102 spi_message_add_tail(&xfers[i], &msg);
105 ret = spi_sync(st->us, &msg);
107 dev_err(&st->us->dev, "problem when burst reading");
109 mutex_unlock(&st->buf_lock);
115 static void adis16240_trigger_bh_to_ring(struct work_struct *work_s)
117 struct adis16240_state *st
118 = container_of(work_s, struct adis16240_state,
119 work_trigger_to_ring);
120 struct iio_ring_buffer *ring = st->indio_dev->ring;
124 size_t datasize = ring->access.get_bytes_per_datum(ring);
126 data = kmalloc(datasize , GFP_KERNEL);
128 dev_err(&st->us->dev, "memory alloc failed in ring bh");
132 if (ring->scan_count)
133 if (adis16240_read_ring_data(&st->indio_dev->dev, st->rx) >= 0)
134 for (; i < ring->scan_count; i++)
135 data[i] = be16_to_cpup(
136 (__be16 *)&(st->rx[i*2]));
138 /* Guaranteed to be aligned with 8 byte boundary */
139 if (ring->scan_timestamp)
140 *((s64 *)(data + ((i + 3)/4)*4)) = st->last_timestamp;
142 ring->access.store_to(ring,
146 iio_trigger_notify_done(st->indio_dev->trig);
152 void adis16240_unconfigure_ring(struct iio_dev *indio_dev)
154 kfree(indio_dev->pollfunc);
155 iio_sw_rb_free(indio_dev->ring);
158 int adis16240_configure_ring(struct iio_dev *indio_dev)
161 struct adis16240_state *st = indio_dev->dev_data;
162 struct iio_ring_buffer *ring;
163 INIT_WORK(&st->work_trigger_to_ring, adis16240_trigger_bh_to_ring);
165 ring = iio_sw_rb_allocate(indio_dev);
170 indio_dev->ring = ring;
171 /* Effectively select the ring buffer implementation */
172 iio_ring_sw_register_funcs(&ring->access);
174 ring->scan_el_attrs = &adis16240_scan_el_group;
175 ring->scan_timestamp = true;
176 ring->preenable = &iio_sw_ring_preenable;
177 ring->postenable = &iio_triggered_ring_postenable;
178 ring->predisable = &iio_triggered_ring_predisable;
179 ring->owner = THIS_MODULE;
181 /* Set default scan mode */
182 iio_scan_mask_set(ring, iio_scan_el_in_supply.number);
183 iio_scan_mask_set(ring, iio_scan_el_accel_x.number);
184 iio_scan_mask_set(ring, iio_scan_el_accel_y.number);
185 iio_scan_mask_set(ring, iio_scan_el_accel_z.number);
186 iio_scan_mask_set(ring, iio_scan_el_temp.number);
187 iio_scan_mask_set(ring, iio_scan_el_in0.number);
189 ret = iio_alloc_pollfunc(indio_dev, NULL, &adis16240_poll_func_th);
191 goto error_iio_sw_rb_free;
193 indio_dev->modes |= INDIO_RING_TRIGGERED;
196 error_iio_sw_rb_free:
197 iio_sw_rb_free(indio_dev->ring);