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