aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLinus Walleij <linus.walleij@linaro.org>2016-05-21 14:43:16 -0400
committerJonathan Cameron <jic23@kernel.org>2016-05-29 15:21:41 -0400
commit65925b65ed98ffdb277cf5ea1af45731dac0b30b (patch)
treecd1e54bcdd1d712c3c19dae58cfccf4299a652a1
parent0dd09ca419d712d315ffd864158f515e6c64261a (diff)
iio: st_sensors: switch to a threaded interrupt
commit 98ad8b41f58dff6b30713d7f09ae3834b8df7ded ("iio: st_sensors: verify interrupt event to status") caused a regression when reading ST sensors from a HRTimer trigger rather than the intrinsic interrupts: the HRTimer may trigger faster than the sensor provides new values, and as the check against new values available as a cause of the interrupt trigger was done in the poll function, this would bail out of the HRTimer interrupt with IRQ_NONE. So clearly we need to only check the new values available from the proper interrupt handler and not from the poll function, which should rather just read the raw values from the registers, put them into the buffer and be happy. To achieve this: switch the ST Sensors over to using a true threaded interrupt handler. In the interrupt thread, check if new values are available, else yield to the (potential) next device on the same interrupt line to check the registers. If the interrupt was ours, proceed to poll the values. Instead of relying on iio_trigger_generic_data_rdy_poll() as a top half to wake up the thread that polls the sensor for new data, have the thread call iio_trigger_poll_chained() after determining that is is the proper source of the interrupt. This is modelled on drivers/iio/accel/mma8452.c which is already using a properly threaded interrupt handler. In order to get the same precision in timestamps as previously, where samples would be timestamped in the poll function pf->timestamp when calling iio_trigger_generic_data_rdy_poll() we introduce a local timestamp in the sensor data, set it in the top half (fastpath) of the interrupt handler and provide that to the core when calling iio_push_to_buffers_with_timestamp(). Additionally: if the active scanmask is not set for the sensor no IRQs should be enabled and we need to bail out with IRQ_NONE. This can happen if spurious IRQs fire when installing the threaded interrupt handler. Tested with hard interrupt triggers on LIS331DL, then also tested with hrtimers on the same sensor by creating a 75Hz HRTimer and using it to poll the sensor. Signed-off-by: Linus Walleij <linus.walleij@linaro.org> Cc: Giuseppe Barba <giuseppe.barba@st.com> Cc: Denis Ciocca <denis.ciocca@st.com> Reported-by: Crestez Dan Leonard <cdleonard@gmail.com> Tested-by: Crestez Dan Leonard <cdleonard@gmail.com> Tested-by: Jonathan Cameron <jic23@kernel.org> Fixes: 97865fe41322 ("iio: st_sensors: verify interrupt event to status") Signed-off-by: Jonathan Cameron <jic23@kernel.org>
-rw-r--r--drivers/iio/accel/st_accel_buffer.c2
-rw-r--r--drivers/iio/accel/st_accel_core.c1
-rw-r--r--drivers/iio/common/st_sensors/st_sensors_buffer.c25
-rw-r--r--drivers/iio/common/st_sensors/st_sensors_core.c3
-rw-r--r--drivers/iio/common/st_sensors/st_sensors_trigger.c88
-rw-r--r--drivers/iio/gyro/st_gyro_buffer.c2
-rw-r--r--drivers/iio/gyro/st_gyro_core.c1
-rw-r--r--drivers/iio/magnetometer/st_magn_buffer.c2
-rw-r--r--drivers/iio/magnetometer/st_magn_core.c1
-rw-r--r--drivers/iio/pressure/st_pressure_buffer.c2
-rw-r--r--drivers/iio/pressure/st_pressure_core.c1
-rw-r--r--include/linux/iio/common/st_sensors.h9
12 files changed, 111 insertions, 26 deletions
diff --git a/drivers/iio/accel/st_accel_buffer.c b/drivers/iio/accel/st_accel_buffer.c
index a1e642ee13d6..7fddc137e91e 100644
--- a/drivers/iio/accel/st_accel_buffer.c
+++ b/drivers/iio/accel/st_accel_buffer.c
@@ -91,7 +91,7 @@ static const struct iio_buffer_setup_ops st_accel_buffer_setup_ops = {
91 91
92int st_accel_allocate_ring(struct iio_dev *indio_dev) 92int st_accel_allocate_ring(struct iio_dev *indio_dev)
93{ 93{
94 return iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time, 94 return iio_triggered_buffer_setup(indio_dev, NULL,
95 &st_sensors_trigger_handler, &st_accel_buffer_setup_ops); 95 &st_sensors_trigger_handler, &st_accel_buffer_setup_ops);
96} 96}
97 97
diff --git a/drivers/iio/accel/st_accel_core.c b/drivers/iio/accel/st_accel_core.c
index dc73f2d85e6d..4d95bfc4786c 100644
--- a/drivers/iio/accel/st_accel_core.c
+++ b/drivers/iio/accel/st_accel_core.c
@@ -741,6 +741,7 @@ static const struct iio_info accel_info = {
741static const struct iio_trigger_ops st_accel_trigger_ops = { 741static const struct iio_trigger_ops st_accel_trigger_ops = {
742 .owner = THIS_MODULE, 742 .owner = THIS_MODULE,
743 .set_trigger_state = ST_ACCEL_TRIGGER_SET_STATE, 743 .set_trigger_state = ST_ACCEL_TRIGGER_SET_STATE,
744 .validate_device = st_sensors_validate_device,
744}; 745};
745#define ST_ACCEL_TRIGGER_OPS (&st_accel_trigger_ops) 746#define ST_ACCEL_TRIGGER_OPS (&st_accel_trigger_ops)
746#else 747#else
diff --git a/drivers/iio/common/st_sensors/st_sensors_buffer.c b/drivers/iio/common/st_sensors/st_sensors_buffer.c
index c55898543a47..f1693dbebb8a 100644
--- a/drivers/iio/common/st_sensors/st_sensors_buffer.c
+++ b/drivers/iio/common/st_sensors/st_sensors_buffer.c
@@ -57,31 +57,20 @@ irqreturn_t st_sensors_trigger_handler(int irq, void *p)
57 struct iio_poll_func *pf = p; 57 struct iio_poll_func *pf = p;
58 struct iio_dev *indio_dev = pf->indio_dev; 58 struct iio_dev *indio_dev = pf->indio_dev;
59 struct st_sensor_data *sdata = iio_priv(indio_dev); 59 struct st_sensor_data *sdata = iio_priv(indio_dev);
60 s64 timestamp;
60 61
61 /* If we have a status register, check if this IRQ came from us */ 62 /* If we do timetamping here, do it before reading the values */
62 if (sdata->sensor_settings->drdy_irq.addr_stat_drdy) { 63 if (sdata->hw_irq_trigger)
63 u8 status; 64 timestamp = sdata->hw_timestamp;
64 65 else
65 len = sdata->tf->read_byte(&sdata->tb, sdata->dev, 66 timestamp = iio_get_time_ns();
66 sdata->sensor_settings->drdy_irq.addr_stat_drdy,
67 &status);
68 if (len < 0)
69 dev_err(sdata->dev, "could not read channel status\n");
70
71 /*
72 * If this was not caused by any channels on this sensor,
73 * return IRQ_NONE
74 */
75 if (!(status & (u8)indio_dev->active_scan_mask[0]))
76 return IRQ_NONE;
77 }
78 67
79 len = st_sensors_get_buffer_element(indio_dev, sdata->buffer_data); 68 len = st_sensors_get_buffer_element(indio_dev, sdata->buffer_data);
80 if (len < 0) 69 if (len < 0)
81 goto st_sensors_get_buffer_element_error; 70 goto st_sensors_get_buffer_element_error;
82 71
83 iio_push_to_buffers_with_timestamp(indio_dev, sdata->buffer_data, 72 iio_push_to_buffers_with_timestamp(indio_dev, sdata->buffer_data,
84 pf->timestamp); 73 timestamp);
85 74
86st_sensors_get_buffer_element_error: 75st_sensors_get_buffer_element_error:
87 iio_trigger_notify_done(indio_dev->trig); 76 iio_trigger_notify_done(indio_dev->trig);
diff --git a/drivers/iio/common/st_sensors/st_sensors_core.c b/drivers/iio/common/st_sensors/st_sensors_core.c
index dffe00692169..928ee68fcc5f 100644
--- a/drivers/iio/common/st_sensors/st_sensors_core.c
+++ b/drivers/iio/common/st_sensors/st_sensors_core.c
@@ -424,6 +424,9 @@ int st_sensors_set_dataready_irq(struct iio_dev *indio_dev, bool enable)
424 else 424 else
425 drdy_mask = sdata->sensor_settings->drdy_irq.mask_int2; 425 drdy_mask = sdata->sensor_settings->drdy_irq.mask_int2;
426 426
427 /* Flag to the poll function that the hardware trigger is in use */
428 sdata->hw_irq_trigger = enable;
429
427 /* Enable/Disable the interrupt generator for data ready. */ 430 /* Enable/Disable the interrupt generator for data ready. */
428 err = st_sensors_write_data_with_mask(indio_dev, 431 err = st_sensors_write_data_with_mask(indio_dev,
429 sdata->sensor_settings->drdy_irq.addr, 432 sdata->sensor_settings->drdy_irq.addr,
diff --git a/drivers/iio/common/st_sensors/st_sensors_trigger.c b/drivers/iio/common/st_sensors/st_sensors_trigger.c
index da72279fcf99..1f59bcc0f143 100644
--- a/drivers/iio/common/st_sensors/st_sensors_trigger.c
+++ b/drivers/iio/common/st_sensors/st_sensors_trigger.c
@@ -17,6 +17,73 @@
17#include <linux/iio/common/st_sensors.h> 17#include <linux/iio/common/st_sensors.h>
18#include "st_sensors_core.h" 18#include "st_sensors_core.h"
19 19
20/**
21 * st_sensors_irq_handler() - top half of the IRQ-based triggers
22 * @irq: irq number
23 * @p: private handler data
24 */
25irqreturn_t st_sensors_irq_handler(int irq, void *p)
26{
27 struct iio_trigger *trig = p;
28 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
29 struct st_sensor_data *sdata = iio_priv(indio_dev);
30
31 /* Get the time stamp as close in time as possible */
32 sdata->hw_timestamp = iio_get_time_ns();
33 return IRQ_WAKE_THREAD;
34}
35
36/**
37 * st_sensors_irq_thread() - bottom half of the IRQ-based triggers
38 * @irq: irq number
39 * @p: private handler data
40 */
41irqreturn_t st_sensors_irq_thread(int irq, void *p)
42{
43 struct iio_trigger *trig = p;
44 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
45 struct st_sensor_data *sdata = iio_priv(indio_dev);
46 int ret;
47
48 /*
49 * If this trigger is backed by a hardware interrupt and we have a
50 * status register, check if this IRQ came from us
51 */
52 if (sdata->sensor_settings->drdy_irq.addr_stat_drdy) {
53 u8 status;
54
55 ret = sdata->tf->read_byte(&sdata->tb, sdata->dev,
56 sdata->sensor_settings->drdy_irq.addr_stat_drdy,
57 &status);
58 if (ret < 0) {
59 dev_err(sdata->dev, "could not read channel status\n");
60 goto out_poll;
61 }
62 /*
63 * the lower bits of .active_scan_mask[0] is directly mapped
64 * to the channels on the sensor: either bit 0 for
65 * one-dimensional sensors, or e.g. x,y,z for accelerometers,
66 * gyroscopes or magnetometers. No sensor use more than 3
67 * channels, so cut the other status bits here.
68 */
69 status &= 0x07;
70
71 /*
72 * If this was not caused by any channels on this sensor,
73 * return IRQ_NONE
74 */
75 if (!indio_dev->active_scan_mask)
76 return IRQ_NONE;
77 if (!(status & (u8)indio_dev->active_scan_mask[0]))
78 return IRQ_NONE;
79 }
80
81out_poll:
82 /* It's our IRQ: proceed to handle the register polling */
83 iio_trigger_poll_chained(p);
84 return IRQ_HANDLED;
85}
86
20int st_sensors_allocate_trigger(struct iio_dev *indio_dev, 87int st_sensors_allocate_trigger(struct iio_dev *indio_dev,
21 const struct iio_trigger_ops *trigger_ops) 88 const struct iio_trigger_ops *trigger_ops)
22{ 89{
@@ -77,9 +144,12 @@ int st_sensors_allocate_trigger(struct iio_dev *indio_dev,
77 sdata->sensor_settings->drdy_irq.addr_stat_drdy) 144 sdata->sensor_settings->drdy_irq.addr_stat_drdy)
78 irq_trig |= IRQF_SHARED; 145 irq_trig |= IRQF_SHARED;
79 146
80 err = request_threaded_irq(irq, 147 /* Let's create an interrupt thread masking the hard IRQ here */
81 iio_trigger_generic_data_rdy_poll, 148 irq_trig |= IRQF_ONESHOT;
82 NULL, 149
150 err = request_threaded_irq(sdata->get_irq_data_ready(indio_dev),
151 st_sensors_irq_handler,
152 st_sensors_irq_thread,
83 irq_trig, 153 irq_trig,
84 sdata->trig->name, 154 sdata->trig->name,
85 sdata->trig); 155 sdata->trig);
@@ -119,6 +189,18 @@ void st_sensors_deallocate_trigger(struct iio_dev *indio_dev)
119} 189}
120EXPORT_SYMBOL(st_sensors_deallocate_trigger); 190EXPORT_SYMBOL(st_sensors_deallocate_trigger);
121 191
192int st_sensors_validate_device(struct iio_trigger *trig,
193 struct iio_dev *indio_dev)
194{
195 struct iio_dev *indio = iio_trigger_get_drvdata(trig);
196
197 if (indio != indio_dev)
198 return -EINVAL;
199
200 return 0;
201}
202EXPORT_SYMBOL(st_sensors_validate_device);
203
122MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>"); 204MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
123MODULE_DESCRIPTION("STMicroelectronics ST-sensors trigger"); 205MODULE_DESCRIPTION("STMicroelectronics ST-sensors trigger");
124MODULE_LICENSE("GPL v2"); 206MODULE_LICENSE("GPL v2");
diff --git a/drivers/iio/gyro/st_gyro_buffer.c b/drivers/iio/gyro/st_gyro_buffer.c
index d67b17b6a7aa..a5377044e42f 100644
--- a/drivers/iio/gyro/st_gyro_buffer.c
+++ b/drivers/iio/gyro/st_gyro_buffer.c
@@ -91,7 +91,7 @@ static const struct iio_buffer_setup_ops st_gyro_buffer_setup_ops = {
91 91
92int st_gyro_allocate_ring(struct iio_dev *indio_dev) 92int st_gyro_allocate_ring(struct iio_dev *indio_dev)
93{ 93{
94 return iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time, 94 return iio_triggered_buffer_setup(indio_dev, NULL,
95 &st_sensors_trigger_handler, &st_gyro_buffer_setup_ops); 95 &st_sensors_trigger_handler, &st_gyro_buffer_setup_ops);
96} 96}
97 97
diff --git a/drivers/iio/gyro/st_gyro_core.c b/drivers/iio/gyro/st_gyro_core.c
index 52a3c87c375c..a8012955a1f6 100644
--- a/drivers/iio/gyro/st_gyro_core.c
+++ b/drivers/iio/gyro/st_gyro_core.c
@@ -409,6 +409,7 @@ static const struct iio_info gyro_info = {
409static const struct iio_trigger_ops st_gyro_trigger_ops = { 409static const struct iio_trigger_ops st_gyro_trigger_ops = {
410 .owner = THIS_MODULE, 410 .owner = THIS_MODULE,
411 .set_trigger_state = ST_GYRO_TRIGGER_SET_STATE, 411 .set_trigger_state = ST_GYRO_TRIGGER_SET_STATE,
412 .validate_device = st_sensors_validate_device,
412}; 413};
413#define ST_GYRO_TRIGGER_OPS (&st_gyro_trigger_ops) 414#define ST_GYRO_TRIGGER_OPS (&st_gyro_trigger_ops)
414#else 415#else
diff --git a/drivers/iio/magnetometer/st_magn_buffer.c b/drivers/iio/magnetometer/st_magn_buffer.c
index ecd3bd0a9769..0a9e8fadfa9d 100644
--- a/drivers/iio/magnetometer/st_magn_buffer.c
+++ b/drivers/iio/magnetometer/st_magn_buffer.c
@@ -82,7 +82,7 @@ static const struct iio_buffer_setup_ops st_magn_buffer_setup_ops = {
82 82
83int st_magn_allocate_ring(struct iio_dev *indio_dev) 83int st_magn_allocate_ring(struct iio_dev *indio_dev)
84{ 84{
85 return iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time, 85 return iio_triggered_buffer_setup(indio_dev, NULL,
86 &st_sensors_trigger_handler, &st_magn_buffer_setup_ops); 86 &st_sensors_trigger_handler, &st_magn_buffer_setup_ops);
87} 87}
88 88
diff --git a/drivers/iio/magnetometer/st_magn_core.c b/drivers/iio/magnetometer/st_magn_core.c
index 62036d2a9956..8250fc322c56 100644
--- a/drivers/iio/magnetometer/st_magn_core.c
+++ b/drivers/iio/magnetometer/st_magn_core.c
@@ -572,6 +572,7 @@ static const struct iio_info magn_info = {
572static const struct iio_trigger_ops st_magn_trigger_ops = { 572static const struct iio_trigger_ops st_magn_trigger_ops = {
573 .owner = THIS_MODULE, 573 .owner = THIS_MODULE,
574 .set_trigger_state = ST_MAGN_TRIGGER_SET_STATE, 574 .set_trigger_state = ST_MAGN_TRIGGER_SET_STATE,
575 .validate_device = st_sensors_validate_device,
575}; 576};
576#define ST_MAGN_TRIGGER_OPS (&st_magn_trigger_ops) 577#define ST_MAGN_TRIGGER_OPS (&st_magn_trigger_ops)
577#else 578#else
diff --git a/drivers/iio/pressure/st_pressure_buffer.c b/drivers/iio/pressure/st_pressure_buffer.c
index 2ff53f222352..99468d0a64e7 100644
--- a/drivers/iio/pressure/st_pressure_buffer.c
+++ b/drivers/iio/pressure/st_pressure_buffer.c
@@ -82,7 +82,7 @@ static const struct iio_buffer_setup_ops st_press_buffer_setup_ops = {
82 82
83int st_press_allocate_ring(struct iio_dev *indio_dev) 83int st_press_allocate_ring(struct iio_dev *indio_dev)
84{ 84{
85 return iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time, 85 return iio_triggered_buffer_setup(indio_dev, NULL,
86 &st_sensors_trigger_handler, &st_press_buffer_setup_ops); 86 &st_sensors_trigger_handler, &st_press_buffer_setup_ops);
87} 87}
88 88
diff --git a/drivers/iio/pressure/st_pressure_core.c b/drivers/iio/pressure/st_pressure_core.c
index 257b58ac6779..92a118c3c4ac 100644
--- a/drivers/iio/pressure/st_pressure_core.c
+++ b/drivers/iio/pressure/st_pressure_core.c
@@ -445,6 +445,7 @@ static const struct iio_info press_info = {
445static const struct iio_trigger_ops st_press_trigger_ops = { 445static const struct iio_trigger_ops st_press_trigger_ops = {
446 .owner = THIS_MODULE, 446 .owner = THIS_MODULE,
447 .set_trigger_state = ST_PRESS_TRIGGER_SET_STATE, 447 .set_trigger_state = ST_PRESS_TRIGGER_SET_STATE,
448 .validate_device = st_sensors_validate_device,
448}; 449};
449#define ST_PRESS_TRIGGER_OPS (&st_press_trigger_ops) 450#define ST_PRESS_TRIGGER_OPS (&st_press_trigger_ops)
450#else 451#else
diff --git a/include/linux/iio/common/st_sensors.h b/include/linux/iio/common/st_sensors.h
index d029ffac0d69..99403b19092f 100644
--- a/include/linux/iio/common/st_sensors.h
+++ b/include/linux/iio/common/st_sensors.h
@@ -223,6 +223,8 @@ struct st_sensor_settings {
223 * @get_irq_data_ready: Function to get the IRQ used for data ready signal. 223 * @get_irq_data_ready: Function to get the IRQ used for data ready signal.
224 * @tf: Transfer function structure used by I/O operations. 224 * @tf: Transfer function structure used by I/O operations.
225 * @tb: Transfer buffers and mutex used by I/O operations. 225 * @tb: Transfer buffers and mutex used by I/O operations.
226 * @hw_irq_trigger: if we're using the hardware interrupt on the sensor.
227 * @hw_timestamp: Latest timestamp from the interrupt handler, when in use.
226 */ 228 */
227struct st_sensor_data { 229struct st_sensor_data {
228 struct device *dev; 230 struct device *dev;
@@ -247,6 +249,9 @@ struct st_sensor_data {
247 249
248 const struct st_sensor_transfer_function *tf; 250 const struct st_sensor_transfer_function *tf;
249 struct st_sensor_transfer_buffer tb; 251 struct st_sensor_transfer_buffer tb;
252
253 bool hw_irq_trigger;
254 s64 hw_timestamp;
250}; 255};
251 256
252#ifdef CONFIG_IIO_BUFFER 257#ifdef CONFIG_IIO_BUFFER
@@ -260,7 +265,8 @@ int st_sensors_allocate_trigger(struct iio_dev *indio_dev,
260 const struct iio_trigger_ops *trigger_ops); 265 const struct iio_trigger_ops *trigger_ops);
261 266
262void st_sensors_deallocate_trigger(struct iio_dev *indio_dev); 267void st_sensors_deallocate_trigger(struct iio_dev *indio_dev);
263 268int st_sensors_validate_device(struct iio_trigger *trig,
269 struct iio_dev *indio_dev);
264#else 270#else
265static inline int st_sensors_allocate_trigger(struct iio_dev *indio_dev, 271static inline int st_sensors_allocate_trigger(struct iio_dev *indio_dev,
266 const struct iio_trigger_ops *trigger_ops) 272 const struct iio_trigger_ops *trigger_ops)
@@ -271,6 +277,7 @@ static inline void st_sensors_deallocate_trigger(struct iio_dev *indio_dev)
271{ 277{
272 return; 278 return;
273} 279}
280#define st_sensors_validate_device NULL
274#endif 281#endif
275 282
276int st_sensors_init_sensor(struct iio_dev *indio_dev, 283int st_sensors_init_sensor(struct iio_dev *indio_dev,