4 #include <linux/kernel.h>
6 #include <linux/slab.h>
7 #include <linux/export.h>
10 #include "../ring_sw.h"
19 static inline u16 combine_8_to_16(
u8 lower,
u8 upper)
23 return _lower | (_upper << 8);
31 struct iio_dev *indio_dev =
private;
41 static const u8 read_all_tx_array[] = {
56 static int lis3l02dq_read_all(
struct iio_dev *indio_dev,
u8 *rx_array)
71 for (i = 0; i <
ARRAY_SIZE(read_all_tx_array)/4; i++)
75 st->tx[2*
j] = read_all_tx_array[i*4];
78 xfers[
j].
rx_buf = rx_array + j*2;
86 st->tx[2*
j] = read_all_tx_array[i*4 + 2];
89 xfers[
j].
rx_buf = rx_array + j*2;
99 spi_message_init(&
msg);
102 spi_message_add_tail(&xfers[j], &
msg);
111 static int lis3l02dq_get_buffer_element(
struct iio_dev *indio_dev,
120 rx_array = kzalloc(4 * scan_count,
GFP_KERNEL);
121 if (rx_array ==
NULL)
123 ret = lis3l02dq_read_all(indio_dev, rx_array);
128 for (i = 0; i < scan_count; i++)
129 data[i] = combine_8_to_16(rx_array[i*4+1],
133 return i*
sizeof(data[0]);
136 static irqreturn_t lis3l02dq_trigger_handler(
int irq,
void *
p)
146 "memory alloc failed in buffer bh");
151 len = lis3l02dq_get_buffer_element(indio_dev, data);
167 __lis3l02dq_write_data_ready_config(
struct iio_dev *indio_dev,
bool state)
185 if (!state && currentlyset) {
187 valold &= ~LIS3L02DQ_REG_CTRL_2_ENABLE_DATA_READY_GENERATION;
202 }
else if (state && !currentlyset) {
232 static int lis3l02dq_data_rdy_trigger_set_state(
struct iio_trigger *
trig,
239 __lis3l02dq_write_data_ready_config(indio_dev, state);
240 if (state ==
false) {
245 ret = lis3l02dq_read_all(indio_dev,
NULL);
257 static int lis3l02dq_trig_try_reen(
struct iio_trigger *trig)
265 for (i = 0; i < 5; i++)
267 lis3l02dq_read_all(indio_dev,
NULL);
272 "Failed to clear the interrupt for lis3l02dq\n");
280 .set_trigger_state = &lis3l02dq_data_rdy_trigger_set_state,
281 .try_reenable = &lis3l02dq_trig_try_reen,
295 st->
trig->dev.parent = &st->
us->dev;
296 st->
trig->ops = &lis3l02dq_trigger_ops;
297 st->
trig->private_data = indio_dev;
300 goto error_free_trig;
321 lis3l02dq_free_buf(indio_dev->
buffer);
324 static int lis3l02dq_buffer_postenable(
struct iio_dev *indio_dev)
329 bool oneenabled =
false;
367 static int lis3l02dq_buffer_predisable(
struct iio_dev *indio_dev)
395 .postenable = &lis3l02dq_buffer_postenable,
396 .predisable = &lis3l02dq_buffer_predisable,
402 struct iio_buffer *
buffer;
404 buffer = lis3l02dq_alloc_buf(indio_dev);
410 buffer->scan_timestamp =
true;
411 indio_dev->
setup_ops = &lis3l02dq_buffer_setup_ops;
415 &lis3l02dq_trigger_handler,
418 "lis3l02dq_consumer%d",
423 goto error_iio_sw_rb_free;
429 error_iio_sw_rb_free:
430 lis3l02dq_free_buf(indio_dev->
buffer);