Merge branch 'ras-core-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-block.git] / drivers / iio / common / st_sensors / st_sensors_buffer.c
CommitLineData
23491b51
DC
1/*
2 * STMicroelectronics sensors buffer library driver
3 *
4 * Copyright 2012-2013 STMicroelectronics Inc.
5 *
6 * Denis Ciocca <denis.ciocca@st.com>
7 *
8 * Licensed under the GPL-2.
9 */
10
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>
21
22#include <linux/iio/common/st_sensors.h>
23
24
dfe3ab1a 25static int st_sensors_get_buffer_element(struct iio_dev *indio_dev, u8 *buf)
23491b51 26{
e7385de5 27 int i;
23491b51 28 struct st_sensor_data *sdata = iio_priv(indio_dev);
607a568a 29 unsigned int num_data_channels = sdata->num_data_channels;
23491b51 30
e7385de5
GB
31 for_each_set_bit(i, indio_dev->active_scan_mask, num_data_channels) {
32 const struct iio_chan_spec *channel = &indio_dev->channels[i];
33 unsigned int bytes_to_read = channel->scan_type.realbits >> 3;
34 unsigned int storage_bytes =
35 channel->scan_type.storagebits >> 3;
36
37 buf = PTR_ALIGN(buf, storage_bytes);
38 if (sdata->tf->read_multiple_byte(&sdata->tb, sdata->dev,
39 channel->address,
40 bytes_to_read, buf,
41 sdata->multiread_bit) <
42 bytes_to_read)
43 return -EIO;
44
45 /* Advance the buffer pointer */
46 buf += storage_bytes;
23491b51
DC
47 }
48
e7385de5 49 return 0;
23491b51 50}
23491b51
DC
51
52irqreturn_t st_sensors_trigger_handler(int irq, void *p)
53{
54 int len;
55 struct iio_poll_func *pf = p;
56 struct iio_dev *indio_dev = pf->indio_dev;
57 struct st_sensor_data *sdata = iio_priv(indio_dev);
65925b65 58 s64 timestamp;
23491b51 59
90efe055
LW
60 /*
61 * If we do timetamping here, do it before reading the values, because
62 * once we've read the values, new interrupts can occur (when using
63 * the hardware trigger) and the hw_timestamp may get updated.
64 * By storing it in a local variable first, we are safe.
65 */
65925b65
LW
66 if (sdata->hw_irq_trigger)
67 timestamp = sdata->hw_timestamp;
68 else
bc2b7dab 69 timestamp = iio_get_time_ns(indio_dev);
97865fe4 70
23491b51
DC
71 len = st_sensors_get_buffer_element(indio_dev, sdata->buffer_data);
72 if (len < 0)
73 goto st_sensors_get_buffer_element_error;
74
aa4e2427 75 iio_push_to_buffers_with_timestamp(indio_dev, sdata->buffer_data,
65925b65 76 timestamp);
23491b51
DC
77
78st_sensors_get_buffer_element_error:
79 iio_trigger_notify_done(indio_dev->trig);
80
81 return IRQ_HANDLED;
82}
83EXPORT_SYMBOL(st_sensors_trigger_handler);
84
85MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
86MODULE_DESCRIPTION("STMicroelectronics ST-sensors buffer");
87MODULE_LICENSE("GPL v2");