2 * STMicroelectronics sensors buffer library driver
4 * Copyright 2012-2013 STMicroelectronics Inc.
6 * Denis Ciocca <denis.ciocca@st.com>
8 * Licensed under the GPL-2.
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/iio/iio.h>
15 #include <linux/iio/trigger.h>
16 #include <linux/interrupt.h>
17 #include <linux/iio/buffer.h>
18 #include <linux/iio/trigger_consumer.h>
19 #include <linux/iio/triggered_buffer.h>
20 #include <linux/irqreturn.h>
22 #include <linux/iio/common/st_sensors.h>
25 int st_sensors_get_buffer_element(struct iio_dev
*indio_dev
, u8
*buf
)
29 struct st_sensor_data
*sdata
= iio_priv(indio_dev
);
30 unsigned int num_data_channels
= sdata
->num_data_channels
;
31 unsigned int byte_for_channel
=
32 indio_dev
->channels
[0].scan_type
.storagebits
>> 3;
34 addr
= kmalloc(num_data_channels
, GFP_KERNEL
);
37 goto st_sensors_get_buffer_element_error
;
40 for (i
= 0; i
< num_data_channels
; i
++) {
41 if (test_bit(i
, indio_dev
->active_scan_mask
)) {
42 addr
[n
] = indio_dev
->channels
[i
].address
;
48 len
= sdata
->tf
->read_multiple_byte(&sdata
->tb
, sdata
->dev
,
49 addr
[0], byte_for_channel
, buf
, sdata
->multiread_bit
);
52 if ((addr
[1] - addr
[0]) == byte_for_channel
) {
53 len
= sdata
->tf
->read_multiple_byte(&sdata
->tb
,
54 sdata
->dev
, addr
[0], byte_for_channel
* n
,
55 buf
, sdata
->multiread_bit
);
58 rx_array
= kmalloc(byte_for_channel
* num_data_channels
,
62 goto st_sensors_free_memory
;
65 len
= sdata
->tf
->read_multiple_byte(&sdata
->tb
,
67 byte_for_channel
* num_data_channels
,
68 rx_array
, sdata
->multiread_bit
);
71 goto st_sensors_free_memory
;
74 for (i
= 0; i
< n
* num_data_channels
; i
++) {
78 buf
[i
] = rx_array
[n
+ i
];
81 len
= byte_for_channel
* n
;
85 len
= sdata
->tf
->read_multiple_byte(&sdata
->tb
, sdata
->dev
,
86 addr
[0], byte_for_channel
* num_data_channels
,
87 buf
, sdata
->multiread_bit
);
91 goto st_sensors_free_memory
;
93 if (len
!= byte_for_channel
* n
) {
95 goto st_sensors_free_memory
;
98 st_sensors_free_memory
:
100 st_sensors_get_buffer_element_error
:
103 EXPORT_SYMBOL(st_sensors_get_buffer_element
);
105 irqreturn_t
st_sensors_trigger_handler(int irq
, void *p
)
108 struct iio_poll_func
*pf
= p
;
109 struct iio_dev
*indio_dev
= pf
->indio_dev
;
110 struct st_sensor_data
*sdata
= iio_priv(indio_dev
);
112 len
= st_sensors_get_buffer_element(indio_dev
, sdata
->buffer_data
);
114 goto st_sensors_get_buffer_element_error
;
116 if (indio_dev
->scan_timestamp
)
117 *(s64
*)((u8
*)sdata
->buffer_data
+
118 ALIGN(len
, sizeof(s64
))) = pf
->timestamp
;
120 iio_push_to_buffers(indio_dev
, sdata
->buffer_data
);
122 st_sensors_get_buffer_element_error
:
123 iio_trigger_notify_done(indio_dev
->trig
);
127 EXPORT_SYMBOL(st_sensors_trigger_handler
);
129 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
130 MODULE_DESCRIPTION("STMicroelectronics ST-sensors buffer");
131 MODULE_LICENSE("GPL v2");