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(supply, ADIS16240_SCAN_SUPPLY, IIO_UNSIGNED(10),
21 ADIS16240_SUPPLY_OUT, NULL);
22 static IIO_SCAN_EL_C(accel_x, ADIS16240_SCAN_ACC_X, IIO_SIGNED(10),
23 ADIS16240_XACCL_OUT, NULL);
24 static IIO_SCAN_EL_C(accel_y, ADIS16240_SCAN_ACC_Y, IIO_SIGNED(10),
25 ADIS16240_YACCL_OUT, NULL);
26 static IIO_SCAN_EL_C(accel_z, ADIS16240_SCAN_ACC_Z, IIO_SIGNED(10),
27 ADIS16240_ZACCL_OUT, NULL);
28 static IIO_SCAN_EL_C(aux_adc, ADIS16240_SCAN_AUX_ADC, IIO_UNSIGNED(10),
29 ADIS16240_AUX_ADC, NULL);
30 static IIO_SCAN_EL_C(temp, ADIS16240_SCAN_TEMP, IIO_UNSIGNED(10),
31 ADIS16240_TEMP_OUT, NULL);
33 static IIO_SCAN_EL_TIMESTAMP(6);
35 static struct attribute *adis16240_scan_el_attrs[] = {
36 &iio_scan_el_supply.dev_attr.attr,
37 &iio_scan_el_accel_x.dev_attr.attr,
38 &iio_scan_el_accel_y.dev_attr.attr,
39 &iio_scan_el_accel_z.dev_attr.attr,
40 &iio_scan_el_aux_adc.dev_attr.attr,
41 &iio_scan_el_temp.dev_attr.attr,
42 &iio_scan_el_timestamp.dev_attr.attr,
46 static struct attribute_group adis16240_scan_el_group = {
47 .attrs = adis16240_scan_el_attrs,
48 .name = "scan_elements",
52 * adis16240_poll_func_th() top half interrupt handler called by trigger
53 * @private_data: iio_dev
55 static void adis16240_poll_func_th(struct iio_dev *indio_dev, s64 time)
57 struct adis16240_state *st = iio_dev_get_devdata(indio_dev);
58 st->last_timestamp = time;
59 schedule_work(&st->work_trigger_to_ring);
63 * adis16240_read_ring_data() read data registers which will be placed into ring
64 * @dev: device associated with child of actual device (iio_dev or iio_trig)
65 * @rx: somewhere to pass back the value read
67 static int adis16240_read_ring_data(struct device *dev, u8 *rx)
69 struct spi_message msg;
70 struct iio_dev *indio_dev = dev_get_drvdata(dev);
71 struct adis16240_state *st = iio_dev_get_devdata(indio_dev);
72 struct spi_transfer xfers[ADIS16240_OUTPUTS + 1];
76 mutex_lock(&st->buf_lock);
78 spi_message_init(&msg);
80 memset(xfers, 0, sizeof(xfers));
81 for (i = 0; i <= ADIS16240_OUTPUTS; i++) {
82 xfers[i].bits_per_word = 8;
83 xfers[i].cs_change = 1;
85 xfers[i].delay_usecs = 30;
86 xfers[i].tx_buf = st->tx + 2 * i;
88 = ADIS16240_READ_REG(ADIS16240_SUPPLY_OUT + 2 * i);
89 st->tx[2 * i + 1] = 0;
91 xfers[i].rx_buf = rx + 2 * (i - 1);
92 spi_message_add_tail(&xfers[i], &msg);
95 ret = spi_sync(st->us, &msg);
97 dev_err(&st->us->dev, "problem when burst reading");
99 mutex_unlock(&st->buf_lock);
105 static void adis16240_trigger_bh_to_ring(struct work_struct *work_s)
107 struct adis16240_state *st
108 = container_of(work_s, struct adis16240_state,
109 work_trigger_to_ring);
110 struct iio_ring_buffer *ring = st->indio_dev->ring;
114 size_t datasize = ring->access.get_bytes_per_datum(ring);
116 data = kmalloc(datasize , GFP_KERNEL);
118 dev_err(&st->us->dev, "memory alloc failed in ring bh");
122 if (ring->scan_count)
123 if (adis16240_read_ring_data(&st->indio_dev->dev, st->rx) >= 0)
124 for (; i < ring->scan_count; i++)
125 data[i] = be16_to_cpup(
126 (__be16 *)&(st->rx[i*2]));
128 /* Guaranteed to be aligned with 8 byte boundary */
129 if (ring->scan_timestamp)
130 *((s64 *)(data + ((i + 3)/4)*4)) = st->last_timestamp;
132 ring->access.store_to(ring,
136 iio_trigger_notify_done(st->indio_dev->trig);
142 void adis16240_unconfigure_ring(struct iio_dev *indio_dev)
144 kfree(indio_dev->pollfunc);
145 iio_sw_rb_free(indio_dev->ring);
148 int adis16240_configure_ring(struct iio_dev *indio_dev)
151 struct adis16240_state *st = indio_dev->dev_data;
152 struct iio_ring_buffer *ring;
153 INIT_WORK(&st->work_trigger_to_ring, adis16240_trigger_bh_to_ring);
155 ring = iio_sw_rb_allocate(indio_dev);
160 indio_dev->ring = ring;
161 /* Effectively select the ring buffer implementation */
162 iio_ring_sw_register_funcs(&ring->access);
164 ring->scan_el_attrs = &adis16240_scan_el_group;
165 ring->scan_timestamp = true;
166 ring->preenable = &iio_sw_ring_preenable;
167 ring->postenable = &iio_triggered_ring_postenable;
168 ring->predisable = &iio_triggered_ring_predisable;
169 ring->owner = THIS_MODULE;
171 /* Set default scan mode */
172 iio_scan_mask_set(ring, iio_scan_el_supply.number);
173 iio_scan_mask_set(ring, iio_scan_el_accel_x.number);
174 iio_scan_mask_set(ring, iio_scan_el_accel_y.number);
175 iio_scan_mask_set(ring, iio_scan_el_accel_z.number);
176 iio_scan_mask_set(ring, iio_scan_el_temp.number);
177 iio_scan_mask_set(ring, iio_scan_el_aux_adc.number);
179 ret = iio_alloc_pollfunc(indio_dev, NULL, &adis16240_poll_func_th);
181 goto error_iio_sw_rb_free;
183 indio_dev->modes |= INDIO_RING_TRIGGERED;
186 error_iio_sw_rb_free:
187 iio_sw_rb_free(indio_dev->ring);