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;
32 for (i = 0; i < num_data_channels; i++) {
33 unsigned int bytes_to_read;
35 if (test_bit(i, indio_dev->active_scan_mask)) {
36 bytes_to_read = indio_dev->channels[i].scan_type.storagebits >> 3;
37 len = sdata->tf->read_multiple_byte(&sdata->tb,
38 sdata->dev, indio_dev->channels[i].address,
40 buf + total, sdata->multiread_bit);
42 if (len < bytes_to_read)
45 /* Advance the buffer pointer */
52 EXPORT_SYMBOL(st_sensors_get_buffer_element);
54 irqreturn_t st_sensors_trigger_handler(int irq, void *p)
57 struct iio_poll_func *pf = p;
58 struct iio_dev *indio_dev = pf->indio_dev;
59 struct st_sensor_data *sdata = iio_priv(indio_dev);
61 /* If we have a status register, check if this IRQ came from us */
62 if (sdata->sensor_settings->drdy_irq.addr_stat_drdy) {
65 len = sdata->tf->read_byte(&sdata->tb, sdata->dev,
66 sdata->sensor_settings->drdy_irq.addr_stat_drdy,
69 dev_err(sdata->dev, "could not read channel status\n");
72 * If this was not caused by any channels on this sensor,
75 if (!(status & (u8)indio_dev->active_scan_mask[0]))
79 len = st_sensors_get_buffer_element(indio_dev, sdata->buffer_data);
81 goto st_sensors_get_buffer_element_error;
83 iio_push_to_buffers_with_timestamp(indio_dev, sdata->buffer_data,
86 st_sensors_get_buffer_element_error:
87 iio_trigger_notify_done(indio_dev->trig);
91 EXPORT_SYMBOL(st_sensors_trigger_handler);
93 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
94 MODULE_DESCRIPTION("STMicroelectronics ST-sensors buffer");
95 MODULE_LICENSE("GPL v2");