Linux-libre 5.3.12-gnu
[librecmc/linux-libre.git] / drivers / iio / common / st_sensors / st_sensors_buffer.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * STMicroelectronics sensors buffer library driver
4  *
5  * Copyright 2012-2013 STMicroelectronics Inc.
6  *
7  * Denis Ciocca <denis.ciocca@st.com>
8  */
9
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/slab.h>
13 #include <linux/iio/iio.h>
14 #include <linux/iio/trigger.h>
15 #include <linux/interrupt.h>
16 #include <linux/iio/buffer.h>
17 #include <linux/iio/trigger_consumer.h>
18 #include <linux/iio/triggered_buffer.h>
19 #include <linux/irqreturn.h>
20
21 #include <linux/iio/common/st_sensors.h>
22
23
24 static int st_sensors_get_buffer_element(struct iio_dev *indio_dev, u8 *buf)
25 {
26         int i;
27         struct st_sensor_data *sdata = iio_priv(indio_dev);
28         unsigned int num_data_channels = sdata->num_data_channels;
29
30         for_each_set_bit(i, indio_dev->active_scan_mask, num_data_channels) {
31                 const struct iio_chan_spec *channel = &indio_dev->channels[i];
32                 unsigned int bytes_to_read =
33                         DIV_ROUND_UP(channel->scan_type.realbits +
34                                      channel->scan_type.shift, 8);
35                 unsigned int storage_bytes =
36                         channel->scan_type.storagebits >> 3;
37
38                 buf = PTR_ALIGN(buf, storage_bytes);
39                 if (sdata->tf->read_multiple_byte(&sdata->tb, sdata->dev,
40                                                   channel->address,
41                                                   bytes_to_read, buf,
42                                                   sdata->multiread_bit) <
43                     bytes_to_read)
44                         return -EIO;
45
46                 /* Advance the buffer pointer */
47                 buf += storage_bytes;
48         }
49
50         return 0;
51 }
52
53 irqreturn_t st_sensors_trigger_handler(int irq, void *p)
54 {
55         int len;
56         struct iio_poll_func *pf = p;
57         struct iio_dev *indio_dev = pf->indio_dev;
58         struct st_sensor_data *sdata = iio_priv(indio_dev);
59         s64 timestamp;
60
61         /*
62          * If we do timetamping here, do it before reading the values, because
63          * once we've read the values, new interrupts can occur (when using
64          * the hardware trigger) and the hw_timestamp may get updated.
65          * By storing it in a local variable first, we are safe.
66          */
67         if (iio_trigger_using_own(indio_dev))
68                 timestamp = sdata->hw_timestamp;
69         else
70                 timestamp = iio_get_time_ns(indio_dev);
71
72         len = st_sensors_get_buffer_element(indio_dev, sdata->buffer_data);
73         if (len < 0)
74                 goto st_sensors_get_buffer_element_error;
75
76         iio_push_to_buffers_with_timestamp(indio_dev, sdata->buffer_data,
77                                            timestamp);
78
79 st_sensors_get_buffer_element_error:
80         iio_trigger_notify_done(indio_dev->trig);
81
82         return IRQ_HANDLED;
83 }
84 EXPORT_SYMBOL(st_sensors_trigger_handler);
85
86 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
87 MODULE_DESCRIPTION("STMicroelectronics ST-sensors buffer");
88 MODULE_LICENSE("GPL v2");