linux-iio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] iio: imu: st_lsm6dsx: fix edge-trigger interrupts
@ 2020-10-22  9:26 Lorenzo Bianconi
  2020-11-01 16:33 ` Jonathan Cameron
  0 siblings, 1 reply; 11+ messages in thread
From: Lorenzo Bianconi @ 2020-10-22  9:26 UTC (permalink / raw)
  To: jic23; +Cc: linux-iio, lorenzo.bianconi, mario.tesi

If the device is configured to trigger edge interrupts it is possible to
miss samples since the sensor can generate an interrupt while the driver
is still processing the previous one.
Poll FIFO status register to process all pending interrupts.
Configure IRQF_ONESHOT only for level interrupts.

Signed-off-by: Lorenzo Bianconi <lorenzo@kernel.org>
---
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c | 33 +++++++++++++++-----
 1 file changed, 25 insertions(+), 8 deletions(-)

diff --git a/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c b/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c
index 5e584c6026f1..d43b08ceec01 100644
--- a/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c
+++ b/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c
@@ -2457,22 +2457,36 @@ st_lsm6dsx_report_motion_event(struct st_lsm6dsx_hw *hw)
 	return data & event_settings->wakeup_src_status_mask;
 }
 
+static irqreturn_t st_lsm6dsx_handler_irq(int irq, void *private)
+{
+	return IRQ_WAKE_THREAD;
+}
+
 static irqreturn_t st_lsm6dsx_handler_thread(int irq, void *private)
 {
 	struct st_lsm6dsx_hw *hw = private;
+	int fifo_len = 0, len = 0;
 	bool event;
-	int count;
 
 	event = st_lsm6dsx_report_motion_event(hw);
 
 	if (!hw->settings->fifo_ops.read_fifo)
 		return event ? IRQ_HANDLED : IRQ_NONE;
 
-	mutex_lock(&hw->fifo_lock);
-	count = hw->settings->fifo_ops.read_fifo(hw);
-	mutex_unlock(&hw->fifo_lock);
+	/*
+	 * If we are using edge IRQs, new samples can arrive while
+	 * processing current IRQ and those may be missed unless we
+	 * pick them here, so let's try read FIFO status again
+	 */
+	do {
+		mutex_lock(&hw->fifo_lock);
+		len = hw->settings->fifo_ops.read_fifo(hw);
+		mutex_unlock(&hw->fifo_lock);
+
+		fifo_len += len;
+	} while (len > 0);
 
-	return count || event ? IRQ_HANDLED : IRQ_NONE;
+	return fifo_len || event ? IRQ_HANDLED : IRQ_NONE;
 }
 
 static int st_lsm6dsx_irq_setup(struct st_lsm6dsx_hw *hw)
@@ -2488,10 +2502,14 @@ static int st_lsm6dsx_irq_setup(struct st_lsm6dsx_hw *hw)
 
 	switch (irq_type) {
 	case IRQF_TRIGGER_HIGH:
+		irq_type |= IRQF_ONESHOT;
+		fallthrough;
 	case IRQF_TRIGGER_RISING:
 		irq_active_low = false;
 		break;
 	case IRQF_TRIGGER_LOW:
+		irq_type |= IRQF_ONESHOT;
+		fallthrough;
 	case IRQF_TRIGGER_FALLING:
 		irq_active_low = true;
 		break;
@@ -2520,10 +2538,9 @@ static int st_lsm6dsx_irq_setup(struct st_lsm6dsx_hw *hw)
 	}
 
 	err = devm_request_threaded_irq(hw->dev, hw->irq,
-					NULL,
+					st_lsm6dsx_handler_irq,
 					st_lsm6dsx_handler_thread,
-					irq_type | IRQF_ONESHOT,
-					"lsm6dsx", hw);
+					irq_type, "lsm6dsx", hw);
 	if (err) {
 		dev_err(hw->dev, "failed to request trigger irq %d\n",
 			hw->irq);
-- 
2.26.2


^ permalink raw reply related	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2020-11-14 17:58 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-10-22  9:26 [PATCH] iio: imu: st_lsm6dsx: fix edge-trigger interrupts Lorenzo Bianconi
2020-11-01 16:33 ` Jonathan Cameron
2020-11-02 10:15   ` Lorenzo Bianconi
2020-11-02 17:44     ` Jonathan Cameron
2020-11-02 18:18       ` Lorenzo Bianconi
2020-11-08 16:49         ` Jonathan Cameron
2020-11-08 18:27           ` Lorenzo Bianconi
2020-11-14 15:06             ` Jonathan Cameron
2020-11-14 16:48               ` Lorenzo Bianconi
2020-11-14 17:31                 ` Jonathan Cameron
2020-11-14 17:58                   ` Lorenzo Bianconi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).