[PATCH v7 09/10] iio: pressure: dps310: implement .hwfifo_flush_to_buffer()

From: Rupesh Majhi

Date: Fri Sep 18 2026 - 08:30:07 EST


Let userspace drain the FIFO on demand rather than only from the
periodic drain.

With a trigger attached the FIFO is not running, so there is nothing to
flush and the hook returns 0.

Assisted-by: LLM
Signed-off-by: Rupesh Majhi <zoone.rupert@xxxxxxxxx>
---
drivers/iio/pressure/dps310.c | 34 ++++++++++++++++++++++++++++++----
1 file changed, 30 insertions(+), 4 deletions(-)

diff --git a/drivers/iio/pressure/dps310.c b/drivers/iio/pressure/dps310.c
index 024fcdc7f4e7..f888bff2c996 100644
--- a/drivers/iio/pressure/dps310.c
+++ b/drivers/iio/pressure/dps310.c
@@ -1092,18 +1092,22 @@ static int dps310_fifo_push_held(struct dps310_data *data)

/*
* Read the batch out before compensating it, so a pressure entry pairs with
- * the temperature preceding it rather than the last one in the batch.
+ * the temperature preceding it rather than the last one in the batch. Stop
+ * reading at max_scans rather than after it, since entries leave the hardware
+ * as they are read and any extra would have to be thrown away. max_scans of
+ * zero drains everything.
*
* Returns scans pushed.
*/
-static int dps310_fifo_drain(struct dps310_data *data)
+static int dps310_fifo_drain(struct dps310_data *data, unsigned int max_scans)
__must_hold(&data->lock)
{
bool pressure_enabled = test_bit(DPS310_SCAN_PRESSURE,
data->iio->active_scan_mask);
u8 kind[DPS310_FIFO_DEPTH];
s32 raw[DPS310_FIFO_DEPTH];
- unsigned int i, n = 0, pushed = 0;
+ unsigned int i, n = 0, scans = 0, pushed = 0;
+ bool is_pressure;
int rc;

for (i = 0; i < DPS310_FIFO_DEPTH; i++) {
@@ -1114,7 +1118,15 @@ static int dps310_fifo_drain(struct dps310_data *data)
if (rc == DPS310_FIFO_EMPTY)
break;

+ is_pressure = rc == DPS310_FIFO_PRESSURE;
kind[n++] = rc;
+
+ /* Only the measurement that drives the scans counts */
+ if (is_pressure == pressure_enabled)
+ scans++;
+
+ if (max_scans && scans >= max_scans)
+ break;
}

for (i = 0; i < n; i++) {
@@ -1164,7 +1176,7 @@ static void dps310_fifo_work(struct work_struct *work)
int rc;

mutex_lock(&data->lock);
- rc = dps310_fifo_drain(data);
+ rc = dps310_fifo_drain(data, 0);
mutex_unlock(&data->lock);

if (rc < 0)
@@ -1205,6 +1217,19 @@ static void dps310_fifo_hold_free(struct dps310_data *data)
data->fifo_hold_max = 0;
}

+static int dps310_hwfifo_flush(struct iio_dev *iio, unsigned int count)
+{
+ struct dps310_data *data = iio_priv(iio);
+
+ /* A trigger drives the capture instead and leaves the FIFO empty */
+ if (iio_device_get_current_mode(iio) != INDIO_BUFFER_SOFTWARE)
+ return 0;
+
+ guard(mutex)(&data->lock);
+
+ return dps310_fifo_drain(data, count);
+}
+
static int dps310_hwfifo_set_watermark(struct iio_dev *iio, unsigned int val)
{
struct dps310_data *data = iio_priv(iio);
@@ -1342,6 +1367,7 @@ static const struct iio_info dps310_info = {
.read_raw = dps310_read_raw,
.write_raw = dps310_write_raw,
.hwfifo_set_watermark = dps310_hwfifo_set_watermark,
+ .hwfifo_flush_to_buffer = dps310_hwfifo_flush,
};

static int dps310_probe(struct i2c_client *client)
--
2.43.0