All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] iio: imu: st_lsm6dsx: fix edge-trigger interrupts
@ 2020-11-02 18:59 Lorenzo Bianconi
  2020-11-14 15:49 ` Jonathan Cameron
  0 siblings, 1 reply; 2+ messages in thread
From: Lorenzo Bianconi @ 2020-11-02 18:59 UTC (permalink / raw)
  To: jic23
  Cc: lorenzo.bianconi, linux-iio, mario.tesi, denis.ciocca, armando.visconti

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 but it has already read the fifo
status register. Poll FIFO status register to check if there is any pending
interrupt to process.

Fixes: 89ca88a7cdf2 ("iio: imu: st_lsm6dsx: support active-low interrupts")
Fixes: 290a6ce11d93 ("iio: imu: add support to lsm6dsx driver")
Signed-off-by: Lorenzo Bianconi <lorenzo@kernel.org>
---
Changes since v1:
- add missing fixes tags
- keep IRQF_ONESHOT even for edge-interrupts
---
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 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..86c50136219b 100644
--- a/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c
+++ b/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c
@@ -2460,19 +2460,29 @@ st_lsm6dsx_report_motion_event(struct st_lsm6dsx_hw *hw)
 static irqreturn_t st_lsm6dsx_handler_thread(int irq, void *private)
 {
 	struct st_lsm6dsx_hw *hw = private;
+	int fifo_len = 0, len;
 	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);
+
+		if (len > 0)
+			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)
-- 
2.26.2


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

* Re: [PATCH v2] iio: imu: st_lsm6dsx: fix edge-trigger interrupts
  2020-11-02 18:59 [PATCH v2] iio: imu: st_lsm6dsx: fix edge-trigger interrupts Lorenzo Bianconi
@ 2020-11-14 15:49 ` Jonathan Cameron
  0 siblings, 0 replies; 2+ messages in thread
From: Jonathan Cameron @ 2020-11-14 15:49 UTC (permalink / raw)
  To: Lorenzo Bianconi
  Cc: lorenzo.bianconi, linux-iio, mario.tesi, denis.ciocca, armando.visconti

On Mon,  2 Nov 2020 19:59:46 +0100
Lorenzo Bianconi <lorenzo@kernel.org> wrote:

> 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 but it has already read the fifo
> status register. Poll FIFO status register to check if there is any pending
> interrupt to process.
> 
> Fixes: 89ca88a7cdf2 ("iio: imu: st_lsm6dsx: support active-low interrupts")
> Fixes: 290a6ce11d93 ("iio: imu: add support to lsm6dsx driver")
> Signed-off-by: Lorenzo Bianconi <lorenzo@kernel.org>

Sorry, I've been a bit slow on replying to the v1 patch.  Lets finish
the discussion in that thread before we move on with this.
I suspect the patch may end up the same, but the explanation may well
be different!

Jonathan


> ---
> Changes since v1:
> - add missing fixes tags
> - keep IRQF_ONESHOT even for edge-interrupts
> ---
>  drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c | 20 +++++++++++++++-----
>  1 file changed, 15 insertions(+), 5 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..86c50136219b 100644
> --- a/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c
> +++ b/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c
> @@ -2460,19 +2460,29 @@ st_lsm6dsx_report_motion_event(struct st_lsm6dsx_hw *hw)
>  static irqreturn_t st_lsm6dsx_handler_thread(int irq, void *private)
>  {
>  	struct st_lsm6dsx_hw *hw = private;
> +	int fifo_len = 0, len;
>  	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);
> +
> +		if (len > 0)
> +			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)


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

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

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-02 18:59 [PATCH v2] iio: imu: st_lsm6dsx: fix edge-trigger interrupts Lorenzo Bianconi
2020-11-14 15:49 ` Jonathan Cameron

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.