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>
12 #include <linux/bitops.h>
16 #include "../ring_sw.h"
17 #include "../accel/accel.h"
18 #include "../trigger.h"
19 #include "adis16400.h"
22 * adis16400_spi_read_burst() - read all data registers
23 * @dev: device associated with child of actual device (iio_dev or iio_trig)
24 * @rx: somewhere to pass back the value read (min size is 24 bytes)
26 static int adis16400_spi_read_burst(struct device
*dev
, u8
*rx
)
28 struct spi_message msg
;
29 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
30 struct adis16400_state
*st
= iio_priv(indio_dev
);
31 u32 old_speed_hz
= st
->us
->max_speed_hz
;
34 struct spi_transfer xfers
[] = {
46 mutex_lock(&st
->buf_lock
);
47 st
->tx
[0] = ADIS16400_READ_REG(ADIS16400_GLOB_CMD
);
50 spi_message_init(&msg
);
51 spi_message_add_tail(&xfers
[0], &msg
);
52 spi_message_add_tail(&xfers
[1], &msg
);
54 st
->us
->max_speed_hz
= min(ADIS16400_SPI_BURST
, old_speed_hz
);
57 ret
= spi_sync(st
->us
, &msg
);
59 dev_err(&st
->us
->dev
, "problem when burst reading");
61 st
->us
->max_speed_hz
= old_speed_hz
;
63 mutex_unlock(&st
->buf_lock
);
67 static const u16 read_all_tx_array
[] = {
68 cpu_to_be16(ADIS16400_READ_REG(ADIS16400_SUPPLY_OUT
)),
69 cpu_to_be16(ADIS16400_READ_REG(ADIS16400_XGYRO_OUT
)),
70 cpu_to_be16(ADIS16400_READ_REG(ADIS16400_YGYRO_OUT
)),
71 cpu_to_be16(ADIS16400_READ_REG(ADIS16400_ZGYRO_OUT
)),
72 cpu_to_be16(ADIS16400_READ_REG(ADIS16400_XACCL_OUT
)),
73 cpu_to_be16(ADIS16400_READ_REG(ADIS16400_YACCL_OUT
)),
74 cpu_to_be16(ADIS16400_READ_REG(ADIS16400_ZACCL_OUT
)),
75 cpu_to_be16(ADIS16400_READ_REG(ADIS16350_XTEMP_OUT
)),
76 cpu_to_be16(ADIS16400_READ_REG(ADIS16350_YTEMP_OUT
)),
77 cpu_to_be16(ADIS16400_READ_REG(ADIS16350_ZTEMP_OUT
)),
78 cpu_to_be16(ADIS16400_READ_REG(ADIS16400_AUX_ADC
)),
81 static int adis16350_spi_read_all(struct device
*dev
, u8
*rx
)
83 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
84 struct adis16400_state
*st
= iio_priv(indio_dev
);
86 struct spi_message msg
;
88 struct spi_transfer
*xfers
;
90 xfers
= kzalloc(sizeof(*xfers
)*indio_dev
->ring
->scan_count
+ 1,
95 for (i
= 0; i
< ARRAY_SIZE(read_all_tx_array
); i
++)
96 if (indio_dev
->ring
->scan_mask
& (1 << i
)) {
97 xfers
[j
].tx_buf
= &read_all_tx_array
[i
];
98 xfers
[j
].bits_per_word
= 16;
100 xfers
[j
+ 1].rx_buf
= rx
+ j
*2;
103 xfers
[j
].bits_per_word
= 16;
106 spi_message_init(&msg
);
107 for (j
= 0; j
< indio_dev
->ring
->scan_count
+ 1; j
++)
108 spi_message_add_tail(&xfers
[j
], &msg
);
110 ret
= spi_sync(st
->us
, &msg
);
116 /* Whilst this makes a lot of calls to iio_sw_ring functions - it is to device
117 * specific to be rolled into the core.
119 static irqreturn_t
adis16400_trigger_handler(int irq
, void *p
)
121 struct iio_poll_func
*pf
= p
;
122 struct iio_dev
*indio_dev
= pf
->private_data
;
123 struct adis16400_state
*st
= iio_priv(indio_dev
);
124 struct iio_ring_buffer
*ring
= indio_dev
->ring
;
125 int i
= 0, j
, ret
= 0;
127 size_t datasize
= ring
->access
->get_bytes_per_datum(ring
);
128 unsigned long mask
= ring
->scan_mask
;
130 data
= kmalloc(datasize
, GFP_KERNEL
);
132 dev_err(&st
->us
->dev
, "memory alloc failed in ring bh");
136 if (ring
->scan_count
) {
137 if (st
->variant
->flags
& ADIS16400_NO_BURST
) {
138 ret
= adis16350_spi_read_all(&indio_dev
->dev
, st
->rx
);
141 for (; i
< ring
->scan_count
; i
++)
142 data
[i
] = *(s16
*)(st
->rx
+ i
*2);
144 ret
= adis16400_spi_read_burst(&indio_dev
->dev
, st
->rx
);
147 for (; i
< indio_dev
->ring
->scan_count
; i
++) {
150 data
[i
] = be16_to_cpup(
151 (__be16
*)&(st
->rx
[j
*2]));
155 /* Guaranteed to be aligned with 8 byte boundary */
156 if (ring
->scan_timestamp
)
157 *((s64
*)(data
+ ((i
+ 3)/4)*4)) = pf
->timestamp
;
158 ring
->access
->store_to(indio_dev
->ring
, (u8
*) data
, pf
->timestamp
);
160 iio_trigger_notify_done(indio_dev
->trig
);
166 void adis16400_unconfigure_ring(struct iio_dev
*indio_dev
)
168 iio_dealloc_pollfunc(indio_dev
->pollfunc
);
169 iio_sw_rb_free(indio_dev
->ring
);
172 static const struct iio_ring_setup_ops adis16400_ring_setup_ops
= {
173 .preenable
= &iio_sw_ring_preenable
,
174 .postenable
= &iio_triggered_ring_postenable
,
175 .predisable
= &iio_triggered_ring_predisable
,
178 int adis16400_configure_ring(struct iio_dev
*indio_dev
)
181 struct adis16400_state
*st
= iio_priv(indio_dev
);
182 struct iio_ring_buffer
*ring
;
184 ring
= iio_sw_rb_allocate(indio_dev
);
189 indio_dev
->ring
= ring
;
190 /* Effectively select the ring buffer implementation */
191 ring
->access
= &ring_sw_access_funcs
;
193 ring
->scan_timestamp
= true;
194 ring
->setup_ops
= &adis16400_ring_setup_ops
;
195 ring
->owner
= THIS_MODULE
;
196 /* Set default scan mode */
197 ring
->scan_mask
= st
->variant
->default_scan_mask
;
198 ring
->scan_count
= hweight_long(st
->variant
->default_scan_mask
);
200 indio_dev
->pollfunc
= iio_alloc_pollfunc(&iio_pollfunc_store_time
,
201 &adis16400_trigger_handler
,
207 if (indio_dev
->pollfunc
== NULL
) {
209 goto error_iio_sw_rb_free
;
212 indio_dev
->modes
|= INDIO_RING_TRIGGERED
;
214 error_iio_sw_rb_free
:
215 iio_sw_rb_free(indio_dev
->ring
);