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 "adis16209.h"
20 static IIO_SCAN_EL_C(in_supply
, ADIS16209_SCAN_SUPPLY
,
21 ADIS16209_SUPPLY_OUT
, NULL
);
22 static IIO_CONST_ATTR_SCAN_EL_TYPE(in_supply
, u
, 14, 16)
23 static IIO_SCAN_EL_C(accel_x
, ADIS16209_SCAN_ACC_X
, ADIS16209_XACCL_OUT
, NULL
);
24 static IIO_SCAN_EL_C(accel_y
, ADIS16209_SCAN_ACC_Y
, ADIS16209_YACCL_OUT
, NULL
);
25 static IIO_CONST_ATTR_SCAN_EL_TYPE(accel
, s
, 14, 16);
26 static IIO_SCAN_EL_C(in0
, ADIS16209_SCAN_AUX_ADC
, ADIS16209_AUX_ADC
, NULL
);
27 static IIO_CONST_ATTR_SCAN_EL_TYPE(in0
, u
, 12, 16);
28 static IIO_SCAN_EL_C(temp
, ADIS16209_SCAN_TEMP
, ADIS16209_TEMP_OUT
, NULL
);
29 static IIO_CONST_ATTR_SCAN_EL_TYPE(temp
, u
, 12, 16);
30 static IIO_SCAN_EL_C(incli_x
, ADIS16209_SCAN_INCLI_X
,
31 ADIS16209_XINCL_OUT
, NULL
);
32 static IIO_SCAN_EL_C(incli_y
, ADIS16209_SCAN_INCLI_Y
,
33 ADIS16209_YINCL_OUT
, NULL
);
34 static IIO_CONST_ATTR_SCAN_EL_TYPE(incli
, s
, 14, 16);
35 static IIO_SCAN_EL_C(rot
, ADIS16209_SCAN_ROT
, ADIS16209_ROT_OUT
, NULL
);
36 static IIO_CONST_ATTR_SCAN_EL_TYPE(rot
, s
, 14, 16);
37 static IIO_SCAN_EL_TIMESTAMP(8);
38 static IIO_CONST_ATTR_SCAN_EL_TYPE(timestamp
, s
, 64, 64);
40 static struct attribute
*adis16209_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_const_attr_accel_type
.dev_attr
.attr
,
46 &iio_scan_el_in0
.dev_attr
.attr
,
47 &iio_const_attr_in0_type
.dev_attr
.attr
,
48 &iio_scan_el_temp
.dev_attr
.attr
,
49 &iio_const_attr_temp_type
.dev_attr
.attr
,
50 &iio_scan_el_incli_x
.dev_attr
.attr
,
51 &iio_scan_el_incli_y
.dev_attr
.attr
,
52 &iio_const_attr_incli_type
.dev_attr
.attr
,
53 &iio_scan_el_rot
.dev_attr
.attr
,
54 &iio_const_attr_rot_type
.dev_attr
.attr
,
55 &iio_scan_el_timestamp
.dev_attr
.attr
,
56 &iio_const_attr_timestamp_type
.dev_attr
.attr
,
60 static struct attribute_group adis16209_scan_el_group
= {
61 .attrs
= adis16209_scan_el_attrs
,
62 .name
= "scan_elements",
66 * adis16209_poll_func_th() top half interrupt handler called by trigger
67 * @private_data: iio_dev
69 static void adis16209_poll_func_th(struct iio_dev
*indio_dev
, s64 time
)
71 struct adis16209_state
*st
= iio_dev_get_devdata(indio_dev
);
72 st
->last_timestamp
= time
;
73 schedule_work(&st
->work_trigger_to_ring
);
77 * adis16209_read_ring_data() read data registers which will be placed into ring
78 * @dev: device associated with child of actual device (iio_dev or iio_trig)
79 * @rx: somewhere to pass back the value read
81 static int adis16209_read_ring_data(struct device
*dev
, u8
*rx
)
83 struct spi_message msg
;
84 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
85 struct adis16209_state
*st
= iio_dev_get_devdata(indio_dev
);
86 struct spi_transfer xfers
[ADIS16209_OUTPUTS
+ 1];
90 mutex_lock(&st
->buf_lock
);
92 spi_message_init(&msg
);
94 memset(xfers
, 0, sizeof(xfers
));
95 for (i
= 0; i
<= ADIS16209_OUTPUTS
; i
++) {
96 xfers
[i
].bits_per_word
= 8;
97 xfers
[i
].cs_change
= 1;
99 xfers
[i
].delay_usecs
= 20;
100 xfers
[i
].tx_buf
= st
->tx
+ 2 * i
;
102 = ADIS16209_READ_REG(ADIS16209_SUPPLY_OUT
+ 2 * i
);
103 st
->tx
[2 * i
+ 1] = 0;
105 xfers
[i
].rx_buf
= rx
+ 2 * (i
- 1);
106 spi_message_add_tail(&xfers
[i
], &msg
);
109 ret
= spi_sync(st
->us
, &msg
);
111 dev_err(&st
->us
->dev
, "problem when burst reading");
113 mutex_unlock(&st
->buf_lock
);
118 /* Whilst this makes a lot of calls to iio_sw_ring functions - it is to device
119 * specific to be rolled into the core.
121 static void adis16209_trigger_bh_to_ring(struct work_struct
*work_s
)
123 struct adis16209_state
*st
124 = container_of(work_s
, struct adis16209_state
,
125 work_trigger_to_ring
);
126 struct iio_ring_buffer
*ring
= st
->indio_dev
->ring
;
130 size_t datasize
= ring
->access
.get_bytes_per_datum(ring
);
132 data
= kmalloc(datasize
, GFP_KERNEL
);
134 dev_err(&st
->us
->dev
, "memory alloc failed in ring bh");
138 if (ring
->scan_count
)
139 if (adis16209_read_ring_data(&st
->indio_dev
->dev
, st
->rx
) >= 0)
140 for (; i
< ring
->scan_count
; i
++)
141 data
[i
] = be16_to_cpup(
142 (__be16
*)&(st
->rx
[i
*2]));
144 /* Guaranteed to be aligned with 8 byte boundary */
145 if (ring
->scan_timestamp
)
146 *((s64
*)(data
+ ((i
+ 3)/4)*4)) = st
->last_timestamp
;
148 ring
->access
.store_to(ring
,
152 iio_trigger_notify_done(st
->indio_dev
->trig
);
158 void adis16209_unconfigure_ring(struct iio_dev
*indio_dev
)
160 kfree(indio_dev
->pollfunc
);
161 iio_sw_rb_free(indio_dev
->ring
);
164 int adis16209_configure_ring(struct iio_dev
*indio_dev
)
167 struct adis16209_state
*st
= indio_dev
->dev_data
;
168 struct iio_ring_buffer
*ring
;
169 INIT_WORK(&st
->work_trigger_to_ring
, adis16209_trigger_bh_to_ring
);
171 ring
= iio_sw_rb_allocate(indio_dev
);
176 indio_dev
->ring
= ring
;
177 /* Effectively select the ring buffer implementation */
178 iio_ring_sw_register_funcs(&ring
->access
);
180 ring
->scan_el_attrs
= &adis16209_scan_el_group
;
181 ring
->scan_timestamp
= true;
182 ring
->preenable
= &iio_sw_ring_preenable
;
183 ring
->postenable
= &iio_triggered_ring_postenable
;
184 ring
->predisable
= &iio_triggered_ring_predisable
;
185 ring
->owner
= THIS_MODULE
;
187 /* Set default scan mode */
188 iio_scan_mask_set(ring
, iio_scan_el_in_supply
.number
);
189 iio_scan_mask_set(ring
, iio_scan_el_rot
.number
);
190 iio_scan_mask_set(ring
, iio_scan_el_accel_x
.number
);
191 iio_scan_mask_set(ring
, iio_scan_el_accel_y
.number
);
192 iio_scan_mask_set(ring
, iio_scan_el_temp
.number
);
193 iio_scan_mask_set(ring
, iio_scan_el_in0
.number
);
194 iio_scan_mask_set(ring
, iio_scan_el_incli_x
.number
);
195 iio_scan_mask_set(ring
, iio_scan_el_incli_y
.number
);
197 ret
= iio_alloc_pollfunc(indio_dev
, NULL
, &adis16209_poll_func_th
);
199 goto error_iio_sw_rb_free
;
201 indio_dev
->modes
|= INDIO_RING_TRIGGERED
;
204 error_iio_sw_rb_free
:
205 iio_sw_rb_free(indio_dev
->ring
);