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 "adis16260.h"
20 static IIO_SCAN_EL_C(supply
, ADIS16260_SCAN_SUPPLY
, IIO_UNSIGNED(12),
21 ADIS16260_SUPPLY_OUT
, NULL
);
22 static IIO_SCAN_EL_C(gyro
, ADIS16260_SCAN_GYRO
, IIO_SIGNED(14),
23 ADIS16260_GYRO_OUT
, NULL
);
24 static IIO_SCAN_EL_C(aux_adc
, ADIS16260_SCAN_AUX_ADC
, IIO_SIGNED(14),
25 ADIS16260_AUX_ADC
, NULL
);
26 static IIO_SCAN_EL_C(temp
, ADIS16260_SCAN_TEMP
, IIO_UNSIGNED(12),
27 ADIS16260_TEMP_OUT
, NULL
);
28 static IIO_SCAN_EL_C(angl
, ADIS16260_SCAN_ANGL
, IIO_UNSIGNED(12),
29 ADIS16260_ANGL_OUT
, NULL
);
31 static IIO_SCAN_EL_TIMESTAMP(5);
33 static struct attribute
*adis16260_scan_el_attrs
[] = {
34 &iio_scan_el_supply
.dev_attr
.attr
,
35 &iio_scan_el_gyro
.dev_attr
.attr
,
36 &iio_scan_el_aux_adc
.dev_attr
.attr
,
37 &iio_scan_el_temp
.dev_attr
.attr
,
38 &iio_scan_el_angl
.dev_attr
.attr
,
39 &iio_scan_el_timestamp
.dev_attr
.attr
,
43 static struct attribute_group adis16260_scan_el_group
= {
44 .attrs
= adis16260_scan_el_attrs
,
45 .name
= "scan_elements",
49 * adis16260_poll_func_th() top half interrupt handler called by trigger
50 * @private_data: iio_dev
52 static void adis16260_poll_func_th(struct iio_dev
*indio_dev
, s64 time
)
54 struct adis16260_state
*st
= iio_dev_get_devdata(indio_dev
);
55 st
->last_timestamp
= time
;
56 schedule_work(&st
->work_trigger_to_ring
);
60 * adis16260_read_ring_data() read data registers which will be placed into ring
61 * @dev: device associated with child of actual device (iio_dev or iio_trig)
62 * @rx: somewhere to pass back the value read
64 static int adis16260_read_ring_data(struct device
*dev
, u8
*rx
)
66 struct spi_message msg
;
67 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
68 struct adis16260_state
*st
= iio_dev_get_devdata(indio_dev
);
69 struct spi_transfer xfers
[ADIS16260_OUTPUTS
+ 1];
73 mutex_lock(&st
->buf_lock
);
75 spi_message_init(&msg
);
77 memset(xfers
, 0, sizeof(xfers
));
78 for (i
= 0; i
<= ADIS16260_OUTPUTS
; i
++) {
79 xfers
[i
].bits_per_word
= 8;
80 xfers
[i
].cs_change
= 1;
82 xfers
[i
].delay_usecs
= 30;
83 xfers
[i
].tx_buf
= st
->tx
+ 2 * i
;
84 if (i
< 2) /* SUPPLY_OUT:0x02 GYRO_OUT:0x04 */
86 = ADIS16260_READ_REG(ADIS16260_SUPPLY_OUT
88 else /* 0x06 to 0x09 is reserved */
90 = ADIS16260_READ_REG(ADIS16260_SUPPLY_OUT
92 st
->tx
[2 * i
+ 1] = 0;
94 xfers
[i
].rx_buf
= rx
+ 2 * (i
- 1);
95 spi_message_add_tail(&xfers
[i
], &msg
);
98 ret
= spi_sync(st
->us
, &msg
);
100 dev_err(&st
->us
->dev
, "problem when burst reading");
102 mutex_unlock(&st
->buf_lock
);
108 static void adis16260_trigger_bh_to_ring(struct work_struct
*work_s
)
110 struct adis16260_state
*st
111 = container_of(work_s
, struct adis16260_state
,
112 work_trigger_to_ring
);
116 size_t datasize
= st
->indio_dev
117 ->ring
->access
.get_bpd(st
->indio_dev
->ring
);
119 data
= kmalloc(datasize
, GFP_KERNEL
);
121 dev_err(&st
->us
->dev
, "memory alloc failed in ring bh");
125 if (st
->indio_dev
->scan_count
)
126 if (adis16260_read_ring_data(&st
->indio_dev
->dev
, st
->rx
) >= 0)
127 for (; i
< st
->indio_dev
->scan_count
; i
++)
128 data
[i
] = be16_to_cpup(
129 (__be16
*)&(st
->rx
[i
*2]));
131 /* Guaranteed to be aligned with 8 byte boundary */
132 if (st
->indio_dev
->scan_timestamp
)
133 *((s64
*)(data
+ ((i
+ 3)/4)*4)) = st
->last_timestamp
;
135 st
->indio_dev
->ring
->access
.store_to(st
->indio_dev
->ring
,
139 iio_trigger_notify_done(st
->indio_dev
->trig
);
145 void adis16260_unconfigure_ring(struct iio_dev
*indio_dev
)
147 kfree(indio_dev
->pollfunc
);
148 iio_sw_rb_free(indio_dev
->ring
);
151 int adis16260_configure_ring(struct iio_dev
*indio_dev
)
154 struct adis16260_state
*st
= indio_dev
->dev_data
;
155 struct iio_ring_buffer
*ring
;
156 INIT_WORK(&st
->work_trigger_to_ring
, adis16260_trigger_bh_to_ring
);
157 /* Set default scan mode */
159 iio_scan_mask_set(indio_dev
, iio_scan_el_supply
.number
);
160 iio_scan_mask_set(indio_dev
, iio_scan_el_gyro
.number
);
161 iio_scan_mask_set(indio_dev
, iio_scan_el_aux_adc
.number
);
162 iio_scan_mask_set(indio_dev
, iio_scan_el_temp
.number
);
163 iio_scan_mask_set(indio_dev
, iio_scan_el_angl
.number
);
164 indio_dev
->scan_timestamp
= true;
166 indio_dev
->scan_el_attrs
= &adis16260_scan_el_group
;
168 ring
= iio_sw_rb_allocate(indio_dev
);
173 indio_dev
->ring
= ring
;
174 /* Effectively select the ring buffer implementation */
175 iio_ring_sw_register_funcs(&ring
->access
);
177 ring
->preenable
= &iio_sw_ring_preenable
;
178 ring
->postenable
= &iio_triggered_ring_postenable
;
179 ring
->predisable
= &iio_triggered_ring_predisable
;
180 ring
->owner
= THIS_MODULE
;
182 ret
= iio_alloc_pollfunc(indio_dev
, NULL
, &adis16260_poll_func_th
);
184 goto error_iio_sw_rb_free
;
186 indio_dev
->modes
|= INDIO_RING_TRIGGERED
;
189 error_iio_sw_rb_free
:
190 iio_sw_rb_free(indio_dev
->ring
);