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
, IIO_UNSIGNED(14),
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
, IIO_SIGNED(14),
24 ADIS16209_XACCL_OUT
, NULL
);
25 static IIO_SCAN_EL_C(accel_y
, ADIS16209_SCAN_ACC_Y
, IIO_SIGNED(14),
26 ADIS16209_YACCL_OUT
, NULL
);
27 static IIO_CONST_ATTR_SCAN_EL_TYPE(accel
, s
, 14, 16);
28 static IIO_SCAN_EL_C(in0
, ADIS16209_SCAN_AUX_ADC
, IIO_UNSIGNED(12),
29 ADIS16209_AUX_ADC
, NULL
);
30 static IIO_CONST_ATTR_SCAN_EL_TYPE(in0
, u
, 12, 16);
31 static IIO_SCAN_EL_C(temp
, ADIS16209_SCAN_TEMP
, IIO_UNSIGNED(12),
32 ADIS16209_TEMP_OUT
, NULL
);
33 static IIO_CONST_ATTR_SCAN_EL_TYPE(temp
, u
, 12, 16);
34 static IIO_SCAN_EL_C(incli_x
, ADIS16209_SCAN_INCLI_X
, IIO_SIGNED(14),
35 ADIS16209_XINCL_OUT
, NULL
);
36 static IIO_SCAN_EL_C(incli_y
, ADIS16209_SCAN_INCLI_Y
, IIO_SIGNED(14),
37 ADIS16209_YINCL_OUT
, NULL
);
38 static IIO_CONST_ATTR_SCAN_EL_TYPE(incli
, s
, 14, 16);
39 static IIO_SCAN_EL_C(rot
, ADIS16209_SCAN_ROT
, IIO_SIGNED(14),
40 ADIS16209_ROT_OUT
, NULL
);
41 static IIO_CONST_ATTR_SCAN_EL_TYPE(rot
, s
, 14, 16);
42 static IIO_SCAN_EL_TIMESTAMP(8);
43 static IIO_CONST_ATTR_SCAN_EL_TYPE(timestamp
, s
, 64, 64);
45 static struct attribute
*adis16209_scan_el_attrs
[] = {
46 &iio_scan_el_in_supply
.dev_attr
.attr
,
47 &iio_const_attr_in_supply_type
.dev_attr
.attr
,
48 &iio_scan_el_accel_x
.dev_attr
.attr
,
49 &iio_scan_el_accel_y
.dev_attr
.attr
,
50 &iio_const_attr_accel_type
.dev_attr
.attr
,
51 &iio_scan_el_in0
.dev_attr
.attr
,
52 &iio_const_attr_in0_type
.dev_attr
.attr
,
53 &iio_scan_el_temp
.dev_attr
.attr
,
54 &iio_const_attr_temp_type
.dev_attr
.attr
,
55 &iio_scan_el_incli_x
.dev_attr
.attr
,
56 &iio_scan_el_incli_y
.dev_attr
.attr
,
57 &iio_const_attr_incli_type
.dev_attr
.attr
,
58 &iio_scan_el_rot
.dev_attr
.attr
,
59 &iio_const_attr_rot_type
.dev_attr
.attr
,
60 &iio_scan_el_timestamp
.dev_attr
.attr
,
61 &iio_const_attr_timestamp_type
.dev_attr
.attr
,
65 static struct attribute_group adis16209_scan_el_group
= {
66 .attrs
= adis16209_scan_el_attrs
,
67 .name
= "scan_elements",
71 * adis16209_poll_func_th() top half interrupt handler called by trigger
72 * @private_data: iio_dev
74 static void adis16209_poll_func_th(struct iio_dev
*indio_dev
, s64 time
)
76 struct adis16209_state
*st
= iio_dev_get_devdata(indio_dev
);
77 st
->last_timestamp
= time
;
78 schedule_work(&st
->work_trigger_to_ring
);
82 * adis16209_read_ring_data() read data registers which will be placed into ring
83 * @dev: device associated with child of actual device (iio_dev or iio_trig)
84 * @rx: somewhere to pass back the value read
86 static int adis16209_read_ring_data(struct device
*dev
, u8
*rx
)
88 struct spi_message msg
;
89 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
90 struct adis16209_state
*st
= iio_dev_get_devdata(indio_dev
);
91 struct spi_transfer xfers
[ADIS16209_OUTPUTS
+ 1];
95 mutex_lock(&st
->buf_lock
);
97 spi_message_init(&msg
);
99 memset(xfers
, 0, sizeof(xfers
));
100 for (i
= 0; i
<= ADIS16209_OUTPUTS
; i
++) {
101 xfers
[i
].bits_per_word
= 8;
102 xfers
[i
].cs_change
= 1;
104 xfers
[i
].delay_usecs
= 20;
105 xfers
[i
].tx_buf
= st
->tx
+ 2 * i
;
107 = ADIS16209_READ_REG(ADIS16209_SUPPLY_OUT
+ 2 * i
);
108 st
->tx
[2 * i
+ 1] = 0;
110 xfers
[i
].rx_buf
= rx
+ 2 * (i
- 1);
111 spi_message_add_tail(&xfers
[i
], &msg
);
114 ret
= spi_sync(st
->us
, &msg
);
116 dev_err(&st
->us
->dev
, "problem when burst reading");
118 mutex_unlock(&st
->buf_lock
);
123 /* Whilst this makes a lot of calls to iio_sw_ring functions - it is to device
124 * specific to be rolled into the core.
126 static void adis16209_trigger_bh_to_ring(struct work_struct
*work_s
)
128 struct adis16209_state
*st
129 = container_of(work_s
, struct adis16209_state
,
130 work_trigger_to_ring
);
131 struct iio_ring_buffer
*ring
= st
->indio_dev
->ring
;
135 size_t datasize
= ring
->access
.get_bytes_per_datum(ring
);
137 data
= kmalloc(datasize
, GFP_KERNEL
);
139 dev_err(&st
->us
->dev
, "memory alloc failed in ring bh");
143 if (ring
->scan_count
)
144 if (adis16209_read_ring_data(&st
->indio_dev
->dev
, st
->rx
) >= 0)
145 for (; i
< ring
->scan_count
; i
++)
146 data
[i
] = be16_to_cpup(
147 (__be16
*)&(st
->rx
[i
*2]));
149 /* Guaranteed to be aligned with 8 byte boundary */
150 if (ring
->scan_timestamp
)
151 *((s64
*)(data
+ ((i
+ 3)/4)*4)) = st
->last_timestamp
;
153 ring
->access
.store_to(ring
,
157 iio_trigger_notify_done(st
->indio_dev
->trig
);
163 void adis16209_unconfigure_ring(struct iio_dev
*indio_dev
)
165 kfree(indio_dev
->pollfunc
);
166 iio_sw_rb_free(indio_dev
->ring
);
169 int adis16209_configure_ring(struct iio_dev
*indio_dev
)
172 struct adis16209_state
*st
= indio_dev
->dev_data
;
173 struct iio_ring_buffer
*ring
;
174 INIT_WORK(&st
->work_trigger_to_ring
, adis16209_trigger_bh_to_ring
);
176 ring
= iio_sw_rb_allocate(indio_dev
);
181 indio_dev
->ring
= ring
;
182 /* Effectively select the ring buffer implementation */
183 iio_ring_sw_register_funcs(&ring
->access
);
185 ring
->scan_el_attrs
= &adis16209_scan_el_group
;
186 ring
->scan_timestamp
= true;
187 ring
->preenable
= &iio_sw_ring_preenable
;
188 ring
->postenable
= &iio_triggered_ring_postenable
;
189 ring
->predisable
= &iio_triggered_ring_predisable
;
190 ring
->owner
= THIS_MODULE
;
192 /* Set default scan mode */
193 iio_scan_mask_set(ring
, iio_scan_el_in_supply
.number
);
194 iio_scan_mask_set(ring
, iio_scan_el_rot
.number
);
195 iio_scan_mask_set(ring
, iio_scan_el_accel_x
.number
);
196 iio_scan_mask_set(ring
, iio_scan_el_accel_y
.number
);
197 iio_scan_mask_set(ring
, iio_scan_el_temp
.number
);
198 iio_scan_mask_set(ring
, iio_scan_el_in0
.number
);
199 iio_scan_mask_set(ring
, iio_scan_el_incli_x
.number
);
200 iio_scan_mask_set(ring
, iio_scan_el_incli_y
.number
);
202 ret
= iio_alloc_pollfunc(indio_dev
, NULL
, &adis16209_poll_func_th
);
204 goto error_iio_sw_rb_free
;
206 indio_dev
->modes
|= INDIO_RING_TRIGGERED
;
209 error_iio_sw_rb_free
:
210 iio_sw_rb_free(indio_dev
->ring
);