Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * STMicroelectronics sensors buffer library driver
0004  *
0005  * Copyright 2012-2013 STMicroelectronics Inc.
0006  *
0007  * Denis Ciocca <denis.ciocca@st.com>
0008  */
0009 
0010 #include <linux/kernel.h>
0011 #include <linux/iio/iio.h>
0012 #include <linux/iio/trigger.h>
0013 #include <linux/interrupt.h>
0014 #include <linux/iio/buffer.h>
0015 #include <linux/iio/trigger_consumer.h>
0016 #include <linux/irqreturn.h>
0017 #include <linux/regmap.h>
0018 
0019 #include <linux/iio/common/st_sensors.h>
0020 
0021 
0022 static int st_sensors_get_buffer_element(struct iio_dev *indio_dev, u8 *buf)
0023 {
0024     struct st_sensor_data *sdata = iio_priv(indio_dev);
0025     unsigned int num_data_channels = sdata->num_data_channels;
0026     int i;
0027 
0028     for_each_set_bit(i, indio_dev->active_scan_mask, num_data_channels) {
0029         const struct iio_chan_spec *channel = &indio_dev->channels[i];
0030         unsigned int bytes_to_read =
0031             DIV_ROUND_UP(channel->scan_type.realbits +
0032                      channel->scan_type.shift, 8);
0033         unsigned int storage_bytes =
0034             channel->scan_type.storagebits >> 3;
0035 
0036         buf = PTR_ALIGN(buf, storage_bytes);
0037         if (regmap_bulk_read(sdata->regmap, channel->address,
0038                      buf, bytes_to_read) < 0)
0039             return -EIO;
0040 
0041         /* Advance the buffer pointer */
0042         buf += storage_bytes;
0043     }
0044 
0045     return 0;
0046 }
0047 
0048 irqreturn_t st_sensors_trigger_handler(int irq, void *p)
0049 {
0050     int len;
0051     struct iio_poll_func *pf = p;
0052     struct iio_dev *indio_dev = pf->indio_dev;
0053     struct st_sensor_data *sdata = iio_priv(indio_dev);
0054     s64 timestamp;
0055 
0056     /*
0057      * If we do timestamping here, do it before reading the values, because
0058      * once we've read the values, new interrupts can occur (when using
0059      * the hardware trigger) and the hw_timestamp may get updated.
0060      * By storing it in a local variable first, we are safe.
0061      */
0062     if (iio_trigger_using_own(indio_dev))
0063         timestamp = sdata->hw_timestamp;
0064     else
0065         timestamp = iio_get_time_ns(indio_dev);
0066 
0067     len = st_sensors_get_buffer_element(indio_dev, sdata->buffer_data);
0068     if (len < 0)
0069         goto st_sensors_get_buffer_element_error;
0070 
0071     iio_push_to_buffers_with_timestamp(indio_dev, sdata->buffer_data,
0072                        timestamp);
0073 
0074 st_sensors_get_buffer_element_error:
0075     iio_trigger_notify_done(indio_dev->trig);
0076 
0077     return IRQ_HANDLED;
0078 }
0079 EXPORT_SYMBOL_NS(st_sensors_trigger_handler, IIO_ST_SENSORS);