* [PATCH v6 0/6] iio: pressure: dps310: FIFO and triggered buffer support
@ 2026-08-24 20:11 Rupesh Majhi
2026-08-24 20:11 ` [PATCH v6 1/6] iio: pressure: dps310: fix CFG_REG bit definitions Rupesh Majhi
` (5 more replies)
0 siblings, 6 replies; 10+ messages in thread
From: Rupesh Majhi @ 2026-08-24 20:11 UTC (permalink / raw)
To: Andy Shevchenko, Bill Wendling, David Lechner, Eddie James,
Joel Stanley, Jonathan Cameron, Justin Stitt, Nathan Chancellor,
Nick Desaulniers, Nuno Sá
Cc: linux-iio, linux-kernel, llvm, Rupesh Majhi
dps310 has no buffer support today. This series adds it, hardware FIFO
when no external trigger is attached, FIFO left disabled in favor of the
trigger when one is.
Patch 1 fixes CFG_REG bit definitions. It replaces the standalone fix I
sent 27 July, which Jonathan asked to fold in here instead:
Link: https://lore.kernel.org/linux-iio/20260728223009.0cb86996@jic23-huawei/
FIFO_EN is needed by patch 5.
Patches 2 and 3 are precursors Jonathan asked for on v5, so the buffer
patches carry only buffer work. Patch 4 adds the triggered buffer, patch
5 the FIFO and selection between the two. Patch 6 turns on clang context
analysis for this driver, so the lock markings are checked and not just
documented. It is last, drop it if IIO would rather not opt in yet.
Tested on an Infineon DPS310 wired to a BeagleBone Black. Lock markings
clean under clang 23.1.0.
Changes since v5:
- split probe device pointer and read path rework into their own patches
- __must_hold() markings instead of comments saying a lock is held, plus
a last patch opting the driver into the analysis that checks them
- get_unaligned_be24() for the 24 bit results
- scan is a structure now, not a byte buffer with memcpys
- timestamp taken in the trigger handler, not iio_pollfunc_store_time,
which works with nested triggers too
- plain mutex_lock() where the guard hurt readability
- clamp() instead of clamp_t()
- commit messages cut down, Assisted-by tags added
Rupesh Majhi (6):
iio: pressure: dps310: fix CFG_REG bit definitions
iio: pressure: dps310: use a local device pointer in probe
iio: pressure: dps310: rework the raw read paths
iio: pressure: dps310: add triggered buffer support
iio: pressure: dps310: add hardware FIFO support
iio: pressure: dps310: check the lock markings with context analysis
drivers/iio/pressure/Kconfig | 2 +
drivers/iio/pressure/Makefile | 2 +
drivers/iio/pressure/dps310.c | 738 ++++++++++++++++++++++++++++++----
3 files changed, 654 insertions(+), 88 deletions(-)
--
2.43.0
^ permalink raw reply [flat|nested] 10+ messages in thread
* [PATCH v6 1/6] iio: pressure: dps310: fix CFG_REG bit definitions
2026-08-24 20:11 [PATCH v6 0/6] iio: pressure: dps310: FIFO and triggered buffer support Rupesh Majhi
@ 2026-08-24 20:11 ` Rupesh Majhi
2026-08-24 20:11 ` [PATCH v6 2/6] iio: pressure: dps310: use a local device pointer in probe Rupesh Majhi
` (4 subsequent siblings)
5 siblings, 0 replies; 10+ messages in thread
From: Rupesh Majhi @ 2026-08-24 20:11 UTC (permalink / raw)
To: Andy Shevchenko, Bill Wendling, David Lechner, Eddie James,
Joel Stanley, Jonathan Cameron, Justin Stitt, Nathan Chancellor,
Nick Desaulniers, Nuno Sá
Cc: linux-iio, linux-kernel, llvm, Rupesh Majhi, stable
Driver defines them as BIT(4), BIT(5) and BIT(6). Per datasheet P_SHIFT
is bit 2, FIFO_EN is bit 1 and SPI_MODE is bit 0.
Only P_SHIFT has a user. dps310_set_pres_precision() sets it at
oversampling 16 or above, so with wrong bit the result register is never
shifted and stops matching the scale factor compensation divides by.
in_pressure_input returns -ERANGE at oversampling 16, 32 and 64.
FIFO_EN needed by FIFO support later in this series.
Fixes: ba6ec48e76bc ("iio: Add driver for Infineon DPS310")
Fixes: d711a3c7dc82 ("iio: dps310: Add pressure sensing capability")
Cc: stable@vger.kernel.org
Assisted-by: Claude:claude-opus-5
Signed-off-by: Rupesh Majhi <zoone.rupert@gmail.com>
---
drivers/iio/pressure/dps310.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/drivers/iio/pressure/dps310.c b/drivers/iio/pressure/dps310.c
index f45af72a0554..68382960382f 100644
--- a/drivers/iio/pressure/dps310.c
+++ b/drivers/iio/pressure/dps310.c
@@ -50,9 +50,9 @@
#define DPS310_CFG_REG 0x09
#define DPS310_INT_HL BIT(7)
#define DPS310_TMP_SHIFT_EN BIT(3)
-#define DPS310_PRS_SHIFT_EN BIT(4)
-#define DPS310_FIFO_EN BIT(5)
-#define DPS310_SPI_EN BIT(6)
+#define DPS310_PRS_SHIFT_EN BIT(2)
+#define DPS310_FIFO_EN BIT(1)
+#define DPS310_SPI_EN BIT(0)
#define DPS310_RESET 0x0c
#define DPS310_RESET_MAGIC 0x09
#define DPS310_COEF_BASE 0x10
--
2.43.0
^ permalink raw reply related [flat|nested] 10+ messages in thread
* [PATCH v6 2/6] iio: pressure: dps310: use a local device pointer in probe
2026-08-24 20:11 [PATCH v6 0/6] iio: pressure: dps310: FIFO and triggered buffer support Rupesh Majhi
2026-08-24 20:11 ` [PATCH v6 1/6] iio: pressure: dps310: fix CFG_REG bit definitions Rupesh Majhi
@ 2026-08-24 20:11 ` Rupesh Majhi
2026-08-24 20:12 ` [PATCH v6 3/6] iio: pressure: dps310: rework the raw read paths Rupesh Majhi
` (3 subsequent siblings)
5 siblings, 0 replies; 10+ messages in thread
From: Rupesh Majhi @ 2026-08-24 20:11 UTC (permalink / raw)
To: Andy Shevchenko, Bill Wendling, David Lechner, Eddie James,
Joel Stanley, Jonathan Cameron, Justin Stitt, Nathan Chancellor,
Nick Desaulniers, Nuno Sá
Cc: linux-iio, linux-kernel, llvm, Rupesh Majhi
probe() spells out &client->dev for every devm call. Take it into a local
once instead, keeps lines short as more calls are added later in this
series.
No functional change.
Assisted-by: Claude:claude-opus-5
Signed-off-by: Rupesh Majhi <zoone.rupert@gmail.com>
---
drivers/iio/pressure/dps310.c | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)
diff --git a/drivers/iio/pressure/dps310.c b/drivers/iio/pressure/dps310.c
index 68382960382f..25d1c79876e6 100644
--- a/drivers/iio/pressure/dps310.c
+++ b/drivers/iio/pressure/dps310.c
@@ -846,11 +846,12 @@ static const struct iio_info dps310_info = {
static int dps310_probe(struct i2c_client *client)
{
const struct i2c_device_id *id = i2c_client_get_device_id(client);
+ struct device *dev = &client->dev;
struct dps310_data *data;
struct iio_dev *iio;
int rc;
- iio = devm_iio_device_alloc(&client->dev, sizeof(*data));
+ iio = devm_iio_device_alloc(dev, sizeof(*data));
if (!iio)
return -ENOMEM;
@@ -869,7 +870,7 @@ static int dps310_probe(struct i2c_client *client)
return PTR_ERR(data->regmap);
/* Register to run the device reset when the device is removed */
- rc = devm_add_action_or_reset(&client->dev, dps310_reset, data);
+ rc = devm_add_action_or_reset(dev, dps310_reset, data);
if (rc)
return rc;
@@ -877,7 +878,7 @@ static int dps310_probe(struct i2c_client *client)
if (rc)
return rc;
- rc = devm_iio_device_register(&client->dev, iio);
+ rc = devm_iio_device_register(dev, iio);
if (rc)
return rc;
--
2.43.0
^ permalink raw reply related [flat|nested] 10+ messages in thread
* [PATCH v6 3/6] iio: pressure: dps310: rework the raw read paths
2026-08-24 20:11 [PATCH v6 0/6] iio: pressure: dps310: FIFO and triggered buffer support Rupesh Majhi
2026-08-24 20:11 ` [PATCH v6 1/6] iio: pressure: dps310: fix CFG_REG bit definitions Rupesh Majhi
2026-08-24 20:11 ` [PATCH v6 2/6] iio: pressure: dps310: use a local device pointer in probe Rupesh Majhi
@ 2026-08-24 20:12 ` Rupesh Majhi
2026-08-25 8:59 ` Andy Shevchenko
2026-08-24 20:12 ` [PATCH v6 4/6] iio: pressure: dps310: add triggered buffer support Rupesh Majhi
` (2 subsequent siblings)
5 siblings, 1 reply; 10+ messages in thread
From: Rupesh Majhi @ 2026-08-24 20:12 UTC (permalink / raw)
To: Andy Shevchenko, Bill Wendling, David Lechner, Eddie James,
Joel Stanley, Jonathan Cameron, Justin Stitt, Nathan Chancellor,
Nick Desaulniers, Nuno Sá
Cc: linux-iio, linux-kernel, llvm, Rupesh Majhi
Raw reads take the lock themselves, so a processed pressure read takes it
twice: once for the raw read, then again when
dps310_calculate_pressure() trylocks to refresh temperature. Refresh is
skipped whenever lock is busy.
Split raw reads into variants that expect lock held, one helper per
channel taking it once for the whole sequence. Temperature refresh is
unconditional now. Buffered capture later in this series needs the same
shape.
Also use get_unaligned_be24() for 24-bit results, and mark functions that
need the lock with __must_hold() rather than a comment.
Assisted-by: Claude:claude-opus-5
Signed-off-by: Rupesh Majhi <zoone.rupert@gmail.com>
---
drivers/iio/pressure/dps310.c | 159 +++++++++++++++++++---------------
1 file changed, 87 insertions(+), 72 deletions(-)
diff --git a/drivers/iio/pressure/dps310.c b/drivers/iio/pressure/dps310.c
index 25d1c79876e6..269a71ea3e7a 100644
--- a/drivers/iio/pressure/dps310.c
+++ b/drivers/iio/pressure/dps310.c
@@ -19,6 +19,7 @@
#include <linux/math64.h>
#include <linux/module.h>
#include <linux/regmap.h>
+#include <linux/unaligned.h>
#include <linux/iio/iio.h>
#include <linux/iio/sysfs.h>
@@ -286,8 +287,8 @@ static int dps310_get_temp_precision(struct dps310_data *data, int *val)
return 0;
}
-/* Called with lock held */
static int dps310_set_pres_precision(struct dps310_data *data, int val)
+ __must_hold(&data->lock)
{
int rc;
u8 shift_en;
@@ -305,8 +306,8 @@ static int dps310_set_pres_precision(struct dps310_data *data, int val)
DPS310_PRS_PRC_BITS, ilog2(val));
}
-/* Called with lock held */
static int dps310_set_temp_precision(struct dps310_data *data, int val)
+ __must_hold(&data->lock)
{
int rc;
u8 shift_en;
@@ -324,8 +325,8 @@ static int dps310_set_temp_precision(struct dps310_data *data, int val)
DPS310_TMP_PRC_BITS, ilog2(val));
}
-/* Called with lock held */
static int dps310_set_pres_samp_freq(struct dps310_data *data, int freq)
+ __must_hold(&data->lock)
{
u8 val;
@@ -338,8 +339,8 @@ static int dps310_set_pres_samp_freq(struct dps310_data *data, int freq)
DPS310_PRS_RATE_BITS, val);
}
-/* Called with lock held */
static int dps310_set_temp_samp_freq(struct dps310_data *data, int freq)
+ __must_hold(&data->lock)
{
u8 val;
@@ -438,6 +439,7 @@ static int dps310_ready_status(struct dps310_data *data, int ready_bit, int time
}
static int dps310_ready(struct dps310_data *data, int ready_bit, int timeout)
+ __must_hold(&data->lock)
{
int rc;
@@ -463,82 +465,87 @@ static int dps310_ready(struct dps310_data *data, int ready_bit, int timeout)
return 0;
}
-static int dps310_read_pres_raw(struct dps310_data *data)
+static int dps310_read_pres_raw_locked(struct dps310_data *data)
+ __must_hold(&data->lock)
{
int rc;
int rate;
int timeout;
- s32 raw;
u8 val[3];
- if (mutex_lock_interruptible(&data->lock))
- return -EINTR;
-
rc = dps310_get_pres_samp_freq(data, &rate);
if (rc)
- goto done;
+ return rc;
timeout = DPS310_POLL_TIMEOUT_US(rate);
/* Poll for sensor readiness; base the timeout upon the sample rate. */
rc = dps310_ready(data, DPS310_PRS_RDY, timeout);
if (rc)
- goto done;
+ return rc;
rc = regmap_bulk_read(data->regmap, DPS310_PRS_BASE, val, sizeof(val));
if (rc < 0)
- goto done;
+ return rc;
- raw = (val[0] << 16) | (val[1] << 8) | val[2];
- data->pressure_raw = sign_extend32(raw, 23);
+ data->pressure_raw = sign_extend32(get_unaligned_be24(val), 23);
-done:
- mutex_unlock(&data->lock);
- return rc;
+ return 0;
}
-/* Called with lock held */
static int dps310_read_temp_ready(struct dps310_data *data)
+ __must_hold(&data->lock)
{
int rc;
u8 val[3];
- s32 raw;
rc = regmap_bulk_read(data->regmap, DPS310_TMP_BASE, val, sizeof(val));
if (rc < 0)
return rc;
- raw = (val[0] << 16) | (val[1] << 8) | val[2];
- data->temp_raw = sign_extend32(raw, 23);
+ data->temp_raw = sign_extend32(get_unaligned_be24(val), 23);
return 0;
}
-static int dps310_read_temp_raw(struct dps310_data *data)
+static int dps310_read_temp_raw_locked(struct dps310_data *data)
+ __must_hold(&data->lock)
{
int rc;
int rate;
int timeout;
- if (mutex_lock_interruptible(&data->lock))
- return -EINTR;
-
rc = dps310_get_temp_samp_freq(data, &rate);
if (rc)
- goto done;
+ return rc;
timeout = DPS310_POLL_TIMEOUT_US(rate);
/* Poll for sensor readiness; base the timeout upon the sample rate. */
rc = dps310_ready(data, DPS310_TMP_RDY, timeout);
if (rc)
- goto done;
+ return rc;
+
+ return dps310_read_temp_ready(data);
+}
+
+/*
+ * Refresh the cached temperature if a new measurement is ready, so that the
+ * pressure compensation uses a recent value. An error is not fatal here, the
+ * previous temperature is used instead.
+ */
+static void dps310_refresh_temp_locked(struct dps310_data *data)
+ __must_hold(&data->lock)
+{
+ int rc;
+ int t_ready;
- rc = dps310_read_temp_ready(data);
+ rc = regmap_read(data->regmap, DPS310_MEAS_CFG, &t_ready);
+ if (rc)
+ return;
-done:
- mutex_unlock(&data->lock);
- return rc;
+ if (t_ready & DPS310_TMP_RDY)
+ dps310_read_temp_ready(data);
}
static bool dps310_is_writeable_reg(struct device *dev, unsigned int reg)
@@ -580,59 +587,47 @@ static int dps310_write_raw(struct iio_dev *iio,
struct iio_chan_spec const *chan, int val,
int val2, long mask)
{
- int rc;
struct dps310_data *data = iio_priv(iio);
- if (mutex_lock_interruptible(&data->lock))
+ ACQUIRE(mutex_intr, lock)(&data->lock);
+ if (ACQUIRE_ERR(mutex_intr, &lock))
return -EINTR;
switch (mask) {
case IIO_CHAN_INFO_SAMP_FREQ:
switch (chan->type) {
case IIO_PRESSURE:
- rc = dps310_set_pres_samp_freq(data, val);
- break;
+ return dps310_set_pres_samp_freq(data, val);
case IIO_TEMP:
- rc = dps310_set_temp_samp_freq(data, val);
- break;
+ return dps310_set_temp_samp_freq(data, val);
default:
- rc = -EINVAL;
- break;
+ return -EINVAL;
}
- break;
case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
switch (chan->type) {
case IIO_PRESSURE:
- rc = dps310_set_pres_precision(data, val);
- break;
+ return dps310_set_pres_precision(data, val);
case IIO_TEMP:
- rc = dps310_set_temp_precision(data, val);
- break;
+ return dps310_set_temp_precision(data, val);
default:
- rc = -EINVAL;
- break;
+ return -EINVAL;
}
- break;
default:
- rc = -EINVAL;
- break;
+ return -EINVAL;
}
-
- mutex_unlock(&data->lock);
- return rc;
}
static int dps310_calculate_pressure(struct dps310_data *data, int *val)
+ __must_hold(&data->lock)
{
int i;
int rc;
- int t_ready;
int kpi;
int kti;
s64 rem = 0ULL;
@@ -656,15 +651,6 @@ static int dps310_calculate_pressure(struct dps310_data *data, int *val)
kp = (s64)kpi;
kt = (s64)kti;
- /* Refresh temp if it's ready, otherwise just use the latest value */
- if (mutex_trylock(&data->lock)) {
- rc = regmap_read(data->regmap, DPS310_MEAS_CFG, &t_ready);
- if (rc >= 0 && t_ready & DPS310_TMP_RDY)
- dps310_read_temp_ready(data);
-
- mutex_unlock(&data->lock);
- }
-
p = (s64)data->pressure_raw;
t = (s64)data->temp_raw;
@@ -710,6 +696,27 @@ static int dps310_calculate_pressure(struct dps310_data *data, int *val)
return 0;
}
+/*
+ * Sample the pressure and compensate it, taking the lock once for the whole
+ * sequence rather than once per register read.
+ */
+static int dps310_read_pressure_value(struct dps310_data *data, int *val)
+{
+ int rc;
+
+ ACQUIRE(mutex_intr, lock)(&data->lock);
+ if (ACQUIRE_ERR(mutex_intr, &lock))
+ return -EINTR;
+
+ rc = dps310_read_pres_raw_locked(data);
+ if (rc)
+ return rc;
+
+ dps310_refresh_temp_locked(data);
+
+ return dps310_calculate_pressure(data, val);
+}
+
static int dps310_read_pressure(struct dps310_data *data, int *val, int *val2,
long mask)
{
@@ -724,11 +731,7 @@ static int dps310_read_pressure(struct dps310_data *data, int *val, int *val2,
return IIO_VAL_INT;
case IIO_CHAN_INFO_PROCESSED:
- rc = dps310_read_pres_raw(data);
- if (rc)
- return rc;
-
- rc = dps310_calculate_pressure(data, val);
+ rc = dps310_read_pressure_value(data, val);
if (rc)
return rc;
@@ -747,6 +750,7 @@ static int dps310_read_pressure(struct dps310_data *data, int *val, int *val2,
}
static int dps310_calculate_temp(struct dps310_data *data, int *val)
+ __must_hold(&data->lock)
{
s64 c0;
s64 t;
@@ -768,6 +772,21 @@ static int dps310_calculate_temp(struct dps310_data *data, int *val)
return 0;
}
+static int dps310_read_temp_value(struct dps310_data *data, int *val)
+{
+ int rc;
+
+ ACQUIRE(mutex_intr, lock)(&data->lock);
+ if (ACQUIRE_ERR(mutex_intr, &lock))
+ return -EINTR;
+
+ rc = dps310_read_temp_raw_locked(data);
+ if (rc)
+ return rc;
+
+ return dps310_calculate_temp(data, val);
+}
+
static int dps310_read_temp(struct dps310_data *data, int *val, int *val2,
long mask)
{
@@ -782,11 +801,7 @@ static int dps310_read_temp(struct dps310_data *data, int *val, int *val2,
return IIO_VAL_INT;
case IIO_CHAN_INFO_PROCESSED:
- rc = dps310_read_temp_raw(data);
- if (rc)
- return rc;
-
- rc = dps310_calculate_temp(data, val);
+ rc = dps310_read_temp_value(data, val);
if (rc)
return rc;
--
2.43.0
^ permalink raw reply related [flat|nested] 10+ messages in thread
* [PATCH v6 4/6] iio: pressure: dps310: add triggered buffer support
2026-08-24 20:11 [PATCH v6 0/6] iio: pressure: dps310: FIFO and triggered buffer support Rupesh Majhi
` (2 preceding siblings ...)
2026-08-24 20:12 ` [PATCH v6 3/6] iio: pressure: dps310: rework the raw read paths Rupesh Majhi
@ 2026-08-24 20:12 ` Rupesh Majhi
2026-08-25 9:06 ` Andy Shevchenko
2026-08-24 20:12 ` [PATCH v6 5/6] iio: pressure: dps310: add hardware FIFO support Rupesh Majhi
2026-08-24 20:12 ` [PATCH v6 6/6] iio: pressure: dps310: check the lock markings with context analysis Rupesh Majhi
5 siblings, 1 reply; 10+ messages in thread
From: Rupesh Majhi @ 2026-08-24 20:12 UTC (permalink / raw)
To: Andy Shevchenko, Bill Wendling, David Lechner, Eddie James,
Joel Stanley, Jonathan Cameron, Justin Stitt, Nathan Chancellor,
Nick Desaulniers, Nuno Sá
Cc: linux-iio, linux-kernel, llvm, Rupesh Majhi
Add a triggered buffer in order to capture continuously on both channels
instead of one sysfs read at a time.
Raw register value is not useful on its own, pressure has to go through
the compensation polynomial and needs a temperature reading. Report raw
in Pa with 1/1000 scale to keep full resolution in the buffer without
changing what the existing processed attribute reports.
Raw and processed reads return -EBUSY while buffer is on, so does any
reconfiguration.
Assisted-by: Claude:claude-opus-5
Signed-off-by: Rupesh Majhi <zoone.rupert@gmail.com>
---
drivers/iio/pressure/Kconfig | 2 +
drivers/iio/pressure/dps310.c | 169 ++++++++++++++++++++++++++++++++--
2 files changed, 165 insertions(+), 6 deletions(-)
diff --git a/drivers/iio/pressure/Kconfig b/drivers/iio/pressure/Kconfig
index 838a8340c4c0..cef8b90b9ae7 100644
--- a/drivers/iio/pressure/Kconfig
+++ b/drivers/iio/pressure/Kconfig
@@ -112,6 +112,8 @@ config DPS310
tristate "Infineon DPS310 pressure and temperature sensor"
depends on I2C
select REGMAP_I2C
+ select IIO_BUFFER
+ select IIO_TRIGGERED_BUFFER
help
Support for the Infineon DPS310 digital barometric pressure sensor.
It can be accessed over I2C bus.
diff --git a/drivers/iio/pressure/dps310.c b/drivers/iio/pressure/dps310.c
index 269a71ea3e7a..f122acd72b0c 100644
--- a/drivers/iio/pressure/dps310.c
+++ b/drivers/iio/pressure/dps310.c
@@ -21,8 +21,11 @@
#include <linux/regmap.h>
#include <linux/unaligned.h>
+#include <linux/iio/buffer.h>
#include <linux/iio/iio.h>
#include <linux/iio/sysfs.h>
+#include <linux/iio/trigger_consumer.h>
+#include <linux/iio/triggered_buffer.h>
#define DPS310_DEV_NAME "dps310"
@@ -93,19 +96,60 @@ struct dps310_data {
bool timeout_recovery_failed;
};
+enum dps310_scan_index {
+ DPS310_SCAN_TEMP,
+ DPS310_SCAN_PRESSURE,
+};
+
+struct dps310_scan {
+ s32 channels[2];
+ aligned_s64 ts;
+};
+
static const struct iio_chan_spec dps310_channels[] = {
{
.type = IIO_TEMP,
.info_mask_separate = BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO) |
BIT(IIO_CHAN_INFO_SAMP_FREQ) |
BIT(IIO_CHAN_INFO_PROCESSED),
+ .scan_index = DPS310_SCAN_TEMP,
+ .scan_type = {
+ .sign = 's',
+ .realbits = 32,
+ .storagebits = 32,
+ .endianness = IIO_CPU,
+ },
},
{
.type = IIO_PRESSURE,
+ /*
+ * Pressure is only meaningful once the raw register value has
+ * been run through the compensation polynomial in section 4.9.1
+ * of the datasheet, which needs a temperature reading as well.
+ * So what is reported as _raw here is already compensated, in
+ * Pa, and _scale converts it to the kPa the ABI asks for. The
+ * _processed attribute reports the same value and predates
+ * buffer support, so it has to stay.
+ *
+ * Do not copy this pattern into other drivers. A raw attribute
+ * that is not the raw register value is only tolerable here
+ * because the alternative is either losing resolution in the
+ * buffer or breaking existing users of _processed.
+ */
.info_mask_separate = BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO) |
BIT(IIO_CHAN_INFO_SAMP_FREQ) |
- BIT(IIO_CHAN_INFO_PROCESSED),
+ BIT(IIO_CHAN_INFO_PROCESSED) |
+ BIT(IIO_CHAN_INFO_RAW) |
+ BIT(IIO_CHAN_INFO_SCALE),
+ .scan_index = DPS310_SCAN_PRESSURE,
+ .scan_type = {
+ .sign = 's',
+ .realbits = 32,
+ .storagebits = 32,
+ .endianness = IIO_CPU,
+ },
},
+ IIO_CHAN_SOFT_TIMESTAMP(2),
};
/* To be called after checking the COEF_RDY bit in MEAS_CFG */
@@ -589,6 +633,11 @@ static int dps310_write_raw(struct iio_dev *iio,
{
struct dps310_data *data = iio_priv(iio);
+ /* Reconfiguring mid-capture would change the values being captured */
+ IIO_DEV_ACQUIRE_DIRECT_MODE(iio, claim);
+ if (IIO_DEV_ACQUIRE_FAILED(claim))
+ return -EBUSY;
+
ACQUIRE(mutex_intr, lock)(&data->lock);
if (ACQUIRE_ERR(mutex_intr, &lock))
return -EINTR;
@@ -730,6 +779,13 @@ static int dps310_read_pressure(struct dps310_data *data, int *val, int *val2,
return IIO_VAL_INT;
+ case IIO_CHAN_INFO_RAW:
+ rc = dps310_read_pressure_value(data, val);
+ if (rc)
+ return rc;
+
+ return IIO_VAL_INT;
+
case IIO_CHAN_INFO_PROCESSED:
rc = dps310_read_pressure_value(data, val);
if (rc)
@@ -738,6 +794,12 @@ static int dps310_read_pressure(struct dps310_data *data, int *val, int *val2,
*val2 = 1000; /* Convert Pa to KPa per IIO ABI */
return IIO_VAL_FRACTIONAL;
+ case IIO_CHAN_INFO_SCALE:
+ /* The raw value is in Pa, the ABI wants kPa */
+ *val = 1;
+ *val2 = 1000;
+ return IIO_VAL_FRACTIONAL;
+
case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
rc = dps310_get_pres_precision(data, val);
if (rc)
@@ -819,12 +881,10 @@ static int dps310_read_temp(struct dps310_data *data, int *val, int *val2,
}
}
-static int dps310_read_raw(struct iio_dev *iio,
- struct iio_chan_spec const *chan,
- int *val, int *val2, long mask)
+static int dps310_read_channel(struct dps310_data *data,
+ struct iio_chan_spec const *chan,
+ int *val, int *val2, long mask)
{
- struct dps310_data *data = iio_priv(iio);
-
switch (chan->type) {
case IIO_PRESSURE:
return dps310_read_pressure(data, val, val2, mask);
@@ -837,6 +897,92 @@ static int dps310_read_raw(struct iio_dev *iio,
}
}
+static int dps310_read_raw(struct iio_dev *iio,
+ struct iio_chan_spec const *chan,
+ int *val, int *val2, long mask)
+{
+ struct dps310_data *data = iio_priv(iio);
+
+ switch (mask) {
+ case IIO_CHAN_INFO_RAW:
+ case IIO_CHAN_INFO_PROCESSED: {
+ /*
+ * Sampling here consumes the same measurement the capture path
+ * reads, so refuse while the buffer is enabled.
+ */
+ IIO_DEV_ACQUIRE_DIRECT_MODE(iio, claim);
+ if (IIO_DEV_ACQUIRE_FAILED(claim))
+ return -EBUSY;
+
+ return dps310_read_channel(data, chan, val, val2, mask);
+ }
+ default:
+ return dps310_read_channel(data, chan, val, val2, mask);
+ }
+}
+
+static int dps310_fill_scan(struct dps310_data *data,
+ const unsigned long *scan_mask,
+ struct dps310_scan *scan)
+ __must_hold(&data->lock)
+{
+ int rc;
+ int i = 0;
+
+ /*
+ * The pressure compensation needs a temperature reading, so temperature
+ * is sampled even when only the pressure channel is enabled.
+ */
+ rc = dps310_read_temp_raw_locked(data);
+ if (rc)
+ return rc;
+
+ if (test_bit(DPS310_SCAN_TEMP, scan_mask)) {
+ /* Millidegrees Celsius */
+ rc = dps310_calculate_temp(data, &scan->channels[i]);
+ if (rc)
+ return rc;
+
+ i++;
+ }
+
+ if (test_bit(DPS310_SCAN_PRESSURE, scan_mask)) {
+ rc = dps310_read_pres_raw_locked(data);
+ if (rc)
+ return rc;
+
+ /* Pascals, see the comment on the channel definition */
+ rc = dps310_calculate_pressure(data, &scan->channels[i]);
+ if (rc)
+ return rc;
+ }
+
+ return 0;
+}
+
+static irqreturn_t dps310_trigger_handler(int irq, void *p)
+{
+ struct iio_poll_func *pf = p;
+ struct iio_dev *iio = pf->indio_dev;
+ struct dps310_data *data = iio_priv(iio);
+ struct dps310_scan scan = { };
+ int rc;
+
+ mutex_lock(&data->lock);
+ rc = dps310_fill_scan(data, iio->active_scan_mask, &scan);
+ mutex_unlock(&data->lock);
+ if (rc)
+ goto err;
+
+ iio_push_to_buffers_with_ts(iio, &scan, sizeof(scan),
+ iio_get_time_ns(iio));
+
+err:
+ iio_trigger_notify_done(iio->trig);
+
+ return IRQ_HANDLED;
+}
+
static void dps310_reset(void *action_data)
{
struct dps310_data *data = action_data;
@@ -893,6 +1039,17 @@ static int dps310_probe(struct i2c_client *client)
if (rc)
return rc;
+ /*
+ * The device measures continuously in background mode, so a capture is
+ * just a read of the latest results and no buffer setup ops are needed.
+ * The trigger is not aligned with the measurements either way, so the
+ * timestamp is taken in the handler rather than by a top half.
+ */
+ rc = devm_iio_triggered_buffer_setup(dev, iio, NULL,
+ dps310_trigger_handler, NULL);
+ if (rc)
+ return rc;
+
rc = devm_iio_device_register(dev, iio);
if (rc)
return rc;
--
2.43.0
^ permalink raw reply related [flat|nested] 10+ messages in thread
* [PATCH v6 5/6] iio: pressure: dps310: add hardware FIFO support
2026-08-24 20:11 [PATCH v6 0/6] iio: pressure: dps310: FIFO and triggered buffer support Rupesh Majhi
` (3 preceding siblings ...)
2026-08-24 20:12 ` [PATCH v6 4/6] iio: pressure: dps310: add triggered buffer support Rupesh Majhi
@ 2026-08-24 20:12 ` Rupesh Majhi
2026-08-25 9:39 ` Andy Shevchenko
2026-08-24 20:12 ` [PATCH v6 6/6] iio: pressure: dps310: check the lock markings with context analysis Rupesh Majhi
5 siblings, 1 reply; 10+ messages in thread
From: Rupesh Majhi @ 2026-08-24 20:12 UTC (permalink / raw)
To: Andy Shevchenko, Bill Wendling, David Lechner, Eddie James,
Joel Stanley, Jonathan Cameron, Justin Stitt, Nathan Chancellor,
Nick Desaulniers, Nuno Sá
Cc: linux-iio, linux-kernel, llvm, Rupesh Majhi
Use the 32 entry FIFO for buffered capture, so a reader wakes once per
batch of samples instead of once per sample.
FIFO runs when no trigger is attached and stays off when one is.
iio_verify_update() already picks the mode, so buffer setup ops just
branch on iio_device_get_current_mode(), as rohm-bm1390.c does.
Drain is on a timer because there is no interrupt to use and nothing in
tree wires the INT pin. hwfifo_flush_to_buffer is not enough on its own:
iio_buffer_read() sleeps until something is pushed, so a blocking reader
would hang with samples still sitting in the FIFO. Hook is kept for
poll() and non-blocking reads.
Pressure entries drive the scans and reuse last temperature, so the two
configured rates stay independent. FIFO does not timestamp entries, they
are estimated from the sample rate.
Assisted-by: Claude:claude-opus-5
Signed-off-by: Rupesh Majhi <zoone.rupert@gmail.com>
---
drivers/iio/pressure/dps310.c | 411 +++++++++++++++++++++++++++++++++-
1 file changed, 400 insertions(+), 11 deletions(-)
diff --git a/drivers/iio/pressure/dps310.c b/drivers/iio/pressure/dps310.c
index f122acd72b0c..d05aa05d6222 100644
--- a/drivers/iio/pressure/dps310.c
+++ b/drivers/iio/pressure/dps310.c
@@ -2,16 +2,11 @@
// Copyright IBM Corp 2019
/*
* The DPS310 is a barometric pressure and temperature sensor.
- * Currently only reading a single temperature is supported by
- * this driver.
*
* https://www.infineon.com/dgdl/?fileId=5546d462576f34750157750826c42242
*
* Temperature calculation:
* c0 * 0.5 + c1 * T_raw / kT °C
- *
- * TODO:
- * - Optionally support the FIFO
*/
#include <linux/i2c.h>
@@ -20,6 +15,8 @@
#include <linux/module.h>
#include <linux/regmap.h>
#include <linux/unaligned.h>
+#include <linux/units.h>
+#include <linux/workqueue.h>
#include <linux/iio/buffer.h>
#include <linux/iio/iio.h>
@@ -58,9 +55,31 @@
#define DPS310_FIFO_EN BIT(1)
#define DPS310_SPI_EN BIT(0)
#define DPS310_RESET 0x0c
+#define DPS310_FIFO_FLUSH BIT(7)
#define DPS310_RESET_MAGIC 0x09
#define DPS310_COEF_BASE 0x10
+/*
+ * Section 4.8: the FIFO holds 32 entries shared between the pressure and
+ * temperature streams, and stops recording once full rather than overwriting.
+ * A late drain therefore loses the newest samples, not the oldest.
+ */
+#define DPS310_FIFO_DEPTH 32
+
+/* Read back in place of a sample once the FIFO has been drained */
+#define DPS310_FIFO_EMPTY_VAL 0x800000
+
+/* The LSB of a FIFO entry tags which measurement produced it */
+#define DPS310_FIFO_TAG_PRS BIT(0)
+
+/*
+ * Bounds on the drain interval. The lower bound keeps a fast rate from
+ * flooding the workqueue; the upper bound keeps the FIFO from filling while
+ * nothing is looking at it.
+ */
+#define DPS310_DRAIN_MIN_MS 20
+#define DPS310_DRAIN_MAX_MS 2000
+
/* Make sure sleep time is <= 30ms for usleep_range */
#define DPS310_POLL_SLEEP_US(t) min(30000, (t) / 8)
/* Silently handle error in rate value here */
@@ -94,6 +113,15 @@ struct dps310_data {
s32 pressure_raw;
s32 temp_raw;
bool timeout_recovery_failed;
+
+ /* FIFO capture state, used only while the hardware FIFO is enabled */
+ struct iio_dev *iio;
+ struct delayed_work fifo_work;
+ unsigned int watermark;
+ unsigned int drain_interval_ms;
+ s64 fifo_timestamp;
+ s32 fifo_temp_raw;
+ bool fifo_temp_valid;
};
enum dps310_scan_index {
@@ -960,6 +988,339 @@ static int dps310_fill_scan(struct dps310_data *data,
return 0;
}
+static int dps310_fifo_hw_flush(struct dps310_data *data)
+ __must_hold(&data->lock)
+{
+ return regmap_write(data->regmap, DPS310_RESET, DPS310_FIFO_FLUSH);
+}
+
+static int dps310_fifo_set_enable(struct dps310_data *data, bool enable)
+ __must_hold(&data->lock)
+{
+ return regmap_write_bits(data->regmap, DPS310_CFG_REG, DPS310_FIFO_EN,
+ enable ? DPS310_FIFO_EN : 0);
+}
+
+/*
+ * There is no interrupt wired on any in-tree platform and the binding has no
+ * interrupts property, so the FIFO is drained on a timer, at an interval below
+ * the time it takes to fill. See DPS310_FIFO_DEPTH for why late is bad.
+ */
+static int dps310_fifo_interval(struct dps310_data *data, unsigned int *ms)
+ __must_hold(&data->lock)
+{
+ bool pressure_enabled = test_bit(DPS310_SCAN_PRESSURE,
+ data->iio->active_scan_mask);
+ unsigned int fill_ms, want_ms;
+ int rc, prs_rate, tmp_rate;
+
+ rc = dps310_get_pres_samp_freq(data, &prs_rate);
+ if (rc)
+ return rc;
+
+ rc = dps310_get_temp_samp_freq(data, &tmp_rate);
+ if (rc)
+ return rc;
+
+ /* Both streams share the same entries, so they fill it together. */
+ fill_ms = MSEC_PER_SEC * DPS310_FIFO_DEPTH / (prs_rate + tmp_rate);
+
+ /*
+ * The DPS310 has no configurable hardware watermark, only a FIFO-full
+ * condition, so the watermark is taken as the number of scans the user
+ * is prepared to wait for and drives the drain interval instead. Scans
+ * come at the rate of whichever measurement drives them, which is not
+ * the pressure rate when only the temperature channel is enabled.
+ */
+ want_ms = data->watermark * MSEC_PER_SEC /
+ (pressure_enabled ? prs_rate : tmp_rate);
+
+ *ms = clamp(min(want_ms, fill_ms / 2), DPS310_DRAIN_MIN_MS,
+ DPS310_DRAIN_MAX_MS);
+
+ return 0;
+}
+
+/*
+ * Read a single FIFO entry. Returns 1 if a sample was read, 0 once the FIFO is
+ * empty, or a negative error.
+ */
+static int dps310_fifo_read_entry(struct dps310_data *data, s32 *value,
+ bool *is_pressure)
+ __must_hold(&data->lock)
+{
+ u8 val[3];
+ s32 raw;
+ int rc;
+
+ /*
+ * Every entry is read through the pressure registers regardless of
+ * which measurement produced it, with the type tagged in the LSB.
+ */
+ rc = regmap_bulk_read(data->regmap, DPS310_PRS_BASE, val, sizeof(val));
+ if (rc < 0)
+ return rc;
+
+ raw = get_unaligned_be24(val);
+ if (raw == DPS310_FIFO_EMPTY_VAL)
+ return 0;
+
+ *is_pressure = raw & DPS310_FIFO_TAG_PRS;
+ *value = sign_extend32(raw, 23);
+
+ return 1;
+}
+
+static int dps310_fifo_push_scan(struct dps310_data *data, s32 temp_raw,
+ s32 pressure_raw, s64 timestamp)
+ __must_hold(&data->lock)
+{
+ struct iio_dev *iio = data->iio;
+ struct dps310_scan scan = { };
+ int i = 0;
+ int rc;
+
+ /*
+ * The compensation helpers read the cached raw values. Sysfs reads take
+ * the direct-mode claim, so they cannot be looking at these while a
+ * buffered capture is running.
+ */
+ data->temp_raw = temp_raw;
+ data->pressure_raw = pressure_raw;
+
+ if (test_bit(DPS310_SCAN_TEMP, iio->active_scan_mask)) {
+ rc = dps310_calculate_temp(data, &scan.channels[i]);
+ if (rc)
+ return rc;
+
+ i++;
+ }
+
+ if (test_bit(DPS310_SCAN_PRESSURE, iio->active_scan_mask)) {
+ rc = dps310_calculate_pressure(data, &scan.channels[i]);
+ if (rc)
+ return rc;
+ }
+
+ iio_push_to_buffers_with_ts(iio, &scan, sizeof(scan), timestamp);
+
+ return 0;
+}
+
+/*
+ * Drain the FIFO and push the samples it held, stopping once max_scans scans
+ * are in hand or draining everything when max_scans is zero. Stopping at the
+ * read rather than after it matters: entries leave the hardware as they are
+ * read, so any collected beyond the caller's limit would have to be discarded.
+ *
+ * Returns the number of scans pushed.
+ */
+static int dps310_fifo_drain(struct dps310_data *data, s64 now,
+ unsigned int max_scans)
+ __must_hold(&data->lock)
+{
+ bool pressure_enabled = test_bit(DPS310_SCAN_PRESSURE,
+ data->iio->active_scan_mask);
+ bool temp_valid = data->fifo_temp_valid;
+ bool is_pressure[DPS310_FIFO_DEPTH];
+ s32 raw[DPS310_FIFO_DEPTH];
+ unsigned int i, n = 0, scans = 0, pushed = 0;
+ s64 interval, first;
+ int rc, rate;
+
+ /*
+ * Empty the hardware first and compensate afterwards, so the time spent
+ * in the polynomial is not time the FIFO spends filling.
+ *
+ * Pressure entries drive the scans and reuse the most recent
+ * temperature, so the two rates stay independent; entries arriving
+ * before any temperature cannot be compensated and are dropped. With
+ * only the temperature channel enabled there is nothing to pair with
+ * and temperature drives the scans itself.
+ */
+ for (i = 0; i < DPS310_FIFO_DEPTH; i++) {
+ rc = dps310_fifo_read_entry(data, &raw[n], &is_pressure[n]);
+ if (rc < 0)
+ return rc;
+ if (!rc)
+ break;
+
+ if (!is_pressure[n]) {
+ temp_valid = true;
+ if (!pressure_enabled)
+ scans++;
+ } else if (pressure_enabled && temp_valid) {
+ scans++;
+ }
+ n++;
+
+ if (max_scans && scans >= max_scans)
+ break;
+ }
+
+ if (!scans)
+ return 0;
+
+ /*
+ * FIFO entries carry no timestamps. They are synthesised by working
+ * back from the drain with the configured period of whichever
+ * measurement drives the scans, so the spacing matches the sampling
+ * frequency the user asked for instead of varying with how much each
+ * drain happened to collect. These are estimates, not hardware
+ * timestamps.
+ */
+ rc = pressure_enabled ? dps310_get_pres_samp_freq(data, &rate) :
+ dps310_get_temp_samp_freq(data, &rate);
+ if (rc)
+ return rc;
+
+ interval = div_s64(NSEC_PER_SEC, rate);
+ first = now - (s64)(scans - 1) * interval;
+
+ /*
+ * A batch must not start before the previous one ended, or the buffer
+ * would carry timestamps that go backwards. If this drain collected
+ * more than the configured rate accounts for, spread it across the
+ * window since the last sample instead.
+ */
+ if (data->fifo_timestamp && first <= data->fifo_timestamp) {
+ interval = max_t(s64, div_s64(now - data->fifo_timestamp, scans), 1);
+ first = data->fifo_timestamp + interval;
+ }
+
+ for (i = 0; i < n; i++) {
+ s64 timestamp;
+
+ if (!is_pressure[i]) {
+ data->fifo_temp_raw = raw[i];
+ data->fifo_temp_valid = true;
+
+ if (pressure_enabled)
+ continue;
+ } else if (!pressure_enabled || !data->fifo_temp_valid) {
+ continue;
+ }
+
+ timestamp = first + (s64)pushed * interval;
+
+ rc = dps310_fifo_push_scan(data,
+ is_pressure[i] ? data->fifo_temp_raw
+ : raw[i],
+ is_pressure[i] ? raw[i] : 0,
+ timestamp);
+ if (rc)
+ return rc;
+
+ data->fifo_timestamp = timestamp;
+ pushed++;
+ }
+
+ return pushed;
+}
+
+static void dps310_fifo_work(struct work_struct *work)
+{
+ struct dps310_data *data = container_of(to_delayed_work(work),
+ struct dps310_data, fifo_work);
+ int rc;
+
+ mutex_lock(&data->lock);
+ rc = dps310_fifo_drain(data, iio_get_time_ns(data->iio), 0);
+ mutex_unlock(&data->lock);
+
+ if (rc < 0)
+ dev_dbg(&data->client->dev, "FIFO drain failed: %d\n", rc);
+
+ schedule_delayed_work(&data->fifo_work,
+ msecs_to_jiffies(data->drain_interval_ms));
+}
+
+static int dps310_hwfifo_set_watermark(struct iio_dev *iio, unsigned int val)
+{
+ struct dps310_data *data = iio_priv(iio);
+
+ data->watermark = clamp(val, 1, DPS310_FIFO_DEPTH);
+
+ return 0;
+}
+
+static int dps310_hwfifo_flush(struct iio_dev *iio, unsigned int count)
+{
+ struct dps310_data *data = iio_priv(iio);
+
+ /*
+ * With a trigger attached the FIFO is left disabled, and the pressure
+ * registers then hold the latest measurement rather than queued entries
+ * with an empty marker to stop on. There is nothing to drain.
+ */
+ if (iio_device_get_current_mode(iio) != INDIO_BUFFER_SOFTWARE)
+ return 0;
+
+ guard(mutex)(&data->lock);
+
+ return dps310_fifo_drain(data, iio_get_time_ns(iio), count);
+}
+
+static int dps310_buffer_postenable(struct iio_dev *iio)
+{
+ struct dps310_data *data = iio_priv(iio);
+ int rc;
+
+ /*
+ * An attached trigger drives the capture instead, so the FIFO stays
+ * disabled and the two never both feed the buffer.
+ */
+ if (iio_device_get_current_mode(iio) == INDIO_BUFFER_TRIGGERED)
+ return 0;
+
+ guard(mutex)(&data->lock);
+
+ data->fifo_temp_valid = false;
+ data->fifo_timestamp = 0;
+
+ rc = dps310_fifo_interval(data, &data->drain_interval_ms);
+ if (rc)
+ return rc;
+
+ /* Drop whatever accumulated before the buffer was enabled */
+ rc = dps310_fifo_hw_flush(data);
+ if (rc)
+ return rc;
+
+ rc = dps310_fifo_set_enable(data, true);
+ if (rc)
+ return rc;
+
+ schedule_delayed_work(&data->fifo_work,
+ msecs_to_jiffies(data->drain_interval_ms));
+
+ return 0;
+}
+
+static int dps310_buffer_predisable(struct iio_dev *iio)
+{
+ struct dps310_data *data = iio_priv(iio);
+ int rc;
+
+ if (iio_device_get_current_mode(iio) == INDIO_BUFFER_TRIGGERED)
+ return 0;
+
+ cancel_delayed_work_sync(&data->fifo_work);
+
+ guard(mutex)(&data->lock);
+
+ rc = dps310_fifo_set_enable(data, false);
+ if (rc)
+ return rc;
+
+ return dps310_fifo_hw_flush(data);
+}
+
+static const struct iio_buffer_setup_ops dps310_buffer_setup_ops = {
+ .postenable = dps310_buffer_postenable,
+ .predisable = dps310_buffer_predisable,
+};
+
static irqreturn_t dps310_trigger_handler(int irq, void *p)
{
struct iio_poll_func *pf = p;
@@ -990,6 +1351,17 @@ static void dps310_reset(void *action_data)
dps310_reset_wait(data);
}
+/*
+ * The drain reschedules itself, so make sure it is stopped before the device
+ * goes away even if the buffer was never disabled cleanly.
+ */
+static void dps310_cancel_fifo_work(void *action_data)
+{
+ struct dps310_data *data = action_data;
+
+ cancel_delayed_work_sync(&data->fifo_work);
+}
+
static const struct regmap_config dps310_regmap_config = {
.reg_bits = 8,
.val_bits = 8,
@@ -1002,6 +1374,8 @@ static const struct regmap_config dps310_regmap_config = {
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)
@@ -1018,13 +1392,22 @@ static int dps310_probe(struct i2c_client *client)
data = iio_priv(iio);
data->client = client;
+ data->iio = iio;
+ data->watermark = 1;
mutex_init(&data->lock);
+ INIT_DELAYED_WORK(&data->fifo_work, dps310_fifo_work);
iio->name = id->name;
iio->channels = dps310_channels;
iio->num_channels = ARRAY_SIZE(dps310_channels);
iio->info = &dps310_info;
- iio->modes = INDIO_DIRECT_MODE;
+ /*
+ * Both buffer modes are advertised so that iio_verify_update() picks
+ * INDIO_BUFFER_TRIGGERED when a trigger is attached and falls back to
+ * INDIO_BUFFER_SOFTWARE, which the FIFO path uses, when one is not.
+ */
+ iio->modes = INDIO_DIRECT_MODE | INDIO_BUFFER_TRIGGERED |
+ INDIO_BUFFER_SOFTWARE;
data->regmap = devm_regmap_init_i2c(client, &dps310_regmap_config);
if (IS_ERR(data->regmap))
@@ -1040,13 +1423,19 @@ static int dps310_probe(struct i2c_client *client)
return rc;
/*
- * The device measures continuously in background mode, so a capture is
- * just a read of the latest results and no buffer setup ops are needed.
- * The trigger is not aligned with the measurements either way, so the
- * timestamp is taken in the handler rather than by a top half.
+ * The device measures continuously in background mode, so a triggered
+ * capture is just a read of the latest results. The setup ops start and
+ * stop the FIFO drain when no trigger is attached. The trigger is not
+ * aligned with the measurements either way, so the timestamp is taken
+ * in the handler rather than by a top half.
*/
rc = devm_iio_triggered_buffer_setup(dev, iio, NULL,
- dps310_trigger_handler, NULL);
+ dps310_trigger_handler,
+ &dps310_buffer_setup_ops);
+ if (rc)
+ return rc;
+
+ rc = devm_add_action_or_reset(dev, dps310_cancel_fifo_work, data);
if (rc)
return rc;
--
2.43.0
^ permalink raw reply related [flat|nested] 10+ messages in thread
* [PATCH v6 6/6] iio: pressure: dps310: check the lock markings with context analysis
2026-08-24 20:11 [PATCH v6 0/6] iio: pressure: dps310: FIFO and triggered buffer support Rupesh Majhi
` (4 preceding siblings ...)
2026-08-24 20:12 ` [PATCH v6 5/6] iio: pressure: dps310: add hardware FIFO support Rupesh Majhi
@ 2026-08-24 20:12 ` Rupesh Majhi
5 siblings, 0 replies; 10+ messages in thread
From: Rupesh Majhi @ 2026-08-24 20:12 UTC (permalink / raw)
To: Andy Shevchenko, Bill Wendling, David Lechner, Eddie James,
Joel Stanley, Jonathan Cameron, Justin Stitt, Nathan Chancellor,
Nick Desaulniers, Nuno Sá
Cc: linux-iio, linux-kernel, llvm, Rupesh Majhi
__must_hold() markings added earlier in this series are only
documentation unless the file opts in, so switch the analysis on for
dps310.o.
Clean with clang 23.1.0 at W=1. It does run: dropping the lock around
dps310_fill_scan() fails the build rather than going quietly.
Assisted-by: Claude:claude-opus-5
Signed-off-by: Rupesh Majhi <zoone.rupert@gmail.com>
---
drivers/iio/pressure/Makefile | 2 ++
1 file changed, 2 insertions(+)
diff --git a/drivers/iio/pressure/Makefile b/drivers/iio/pressure/Makefile
index bc0d11a20acc..4b1a05f7a0bd 100644
--- a/drivers/iio/pressure/Makefile
+++ b/drivers/iio/pressure/Makefile
@@ -3,6 +3,8 @@
# Makefile for industrial I/O pressure drivers
#
+CONTEXT_ANALYSIS_dps310.o := y
+
# When adding new entries keep the list in alphabetical order
obj-$(CONFIG_ABP060MG) += abp060mg.o
obj-$(CONFIG_ABP2030PA) += abp2030pa.o
--
2.43.0
^ permalink raw reply related [flat|nested] 10+ messages in thread
* Re: [PATCH v6 3/6] iio: pressure: dps310: rework the raw read paths
2026-08-24 20:12 ` [PATCH v6 3/6] iio: pressure: dps310: rework the raw read paths Rupesh Majhi
@ 2026-08-25 8:59 ` Andy Shevchenko
0 siblings, 0 replies; 10+ messages in thread
From: Andy Shevchenko @ 2026-08-25 8:59 UTC (permalink / raw)
To: Rupesh Majhi
Cc: Andy Shevchenko, Bill Wendling, David Lechner, Eddie James,
Joel Stanley, Jonathan Cameron, Justin Stitt, Nathan Chancellor,
Nick Desaulniers, Nuno Sá, linux-iio, linux-kernel, llvm
On Mon, Aug 24, 2026 at 11:12:00PM +0300, Rupesh Majhi wrote:
> Raw reads take the lock themselves, so a processed pressure read takes it
> twice: once for the raw read, then again when
> dps310_calculate_pressure() trylocks to refresh temperature. Refresh is
> skipped whenever lock is busy.
>
> Split raw reads into variants that expect lock held, one helper per
> channel taking it once for the whole sequence. Temperature refresh is
> unconditional now. Buffered capture later in this series needs the same
> shape.
> Also use get_unaligned_be24() for 24-bit results, and mark functions that
> need the lock with __must_hold() rather than a comment.
The first part should be in a separate patch (24-bit value handling).
...
You also need cleanup.h for ACQUIRE().
> #include <linux/math64.h>
> #include <linux/module.h>
> #include <linux/regmap.h>
> +#include <linux/unaligned.h>
--
With Best Regards,
Andy Shevchenko
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH v6 4/6] iio: pressure: dps310: add triggered buffer support
2026-08-24 20:12 ` [PATCH v6 4/6] iio: pressure: dps310: add triggered buffer support Rupesh Majhi
@ 2026-08-25 9:06 ` Andy Shevchenko
0 siblings, 0 replies; 10+ messages in thread
From: Andy Shevchenko @ 2026-08-25 9:06 UTC (permalink / raw)
To: Rupesh Majhi
Cc: Andy Shevchenko, Bill Wendling, David Lechner, Eddie James,
Joel Stanley, Jonathan Cameron, Justin Stitt, Nathan Chancellor,
Nick Desaulniers, Nuno Sá, linux-iio, linux-kernel, llvm
On Mon, Aug 24, 2026 at 11:12:01PM +0300, Rupesh Majhi wrote:
> Add a triggered buffer in order to capture continuously on both channels
> instead of one sysfs read at a time.
>
> Raw register value is not useful on its own, pressure has to go through
> the compensation polynomial and needs a temperature reading. Report raw
> in Pa with 1/1000 scale to keep full resolution in the buffer without
> changing what the existing processed attribute reports.
>
> Raw and processed reads return -EBUSY while buffer is on, so does any
> reconfiguration.
...
> +struct dps310_scan {
> + s32 channels[2];
> + aligned_s64 ts;
> +};
Wondering if using macro here would make sense...
...
> static const struct iio_chan_spec dps310_channels[] = {
> {
> .type = IIO_TEMP,
> .info_mask_separate = BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO) |
> BIT(IIO_CHAN_INFO_SAMP_FREQ) |
> BIT(IIO_CHAN_INFO_PROCESSED),
> + .scan_index = DPS310_SCAN_TEMP,
> + .scan_type = {
> + .sign = 's',
> + .realbits = 32,
> + .storagebits = 32,
> + .endianness = IIO_CPU,
> + },
> },
> {
> .type = IIO_PRESSURE,
> + /*
> + * Pressure is only meaningful once the raw register value has
> + * been run through the compensation polynomial in section 4.9.1
> + * of the datasheet, which needs a temperature reading as well.
> + * So what is reported as _raw here is already compensated, in
> + * Pa, and _scale converts it to the kPa the ABI asks for. The
> + * _processed attribute reports the same value and predates
> + * buffer support, so it has to stay.
> + *
> + * Do not copy this pattern into other drivers. A raw attribute
> + * that is not the raw register value is only tolerable here
> + * because the alternative is either losing resolution in the
> + * buffer or breaking existing users of _processed.
> + */
> .info_mask_separate = BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO) |
> BIT(IIO_CHAN_INFO_SAMP_FREQ) |
> - BIT(IIO_CHAN_INFO_PROCESSED),
> + BIT(IIO_CHAN_INFO_PROCESSED) |
Leave it untouched, squeeze the new ones above this.
> + BIT(IIO_CHAN_INFO_RAW) |
> + BIT(IIO_CHAN_INFO_SCALE),
> + .scan_index = DPS310_SCAN_PRESSURE,
> + .scan_type = {
> + .sign = 's',
> + .realbits = 32,
> + .storagebits = 32,
> + .endianness = IIO_CPU,
> + },
> },
> + IIO_CHAN_SOFT_TIMESTAMP(2),
> };
...
> static int dps310_read_pressure(struct dps310_data *data, int *val, int *val2,
> *val2 = 1000; /* Convert Pa to KPa per IIO ABI */
Side note, at some point makes sense to move from 1000 to KILO.
> return IIO_VAL_FRACTIONAL;
>
> + case IIO_CHAN_INFO_SCALE:
> + /* The raw value is in Pa, the ABI wants kPa */
> + *val = 1;
> + *val2 = 1000;
Ditto.
> + return IIO_VAL_FRACTIONAL;
...
> +static int dps310_fill_scan(struct dps310_data *data,
> + const unsigned long *scan_mask,
> + struct dps310_scan *scan)
> + __must_hold(&data->lock)
> +{
> + int rc;
> + int i = 0;
Why is 'i' signed? Also, move assignment closer to its first user.
> + /*
> + * The pressure compensation needs a temperature reading, so temperature
> + * is sampled even when only the pressure channel is enabled.
> + */
> + rc = dps310_read_temp_raw_locked(data);
> + if (rc)
> + return rc;
> +
> + if (test_bit(DPS310_SCAN_TEMP, scan_mask)) {
> + /* Millidegrees Celsius */
> + rc = dps310_calculate_temp(data, &scan->channels[i]);
> + if (rc)
> + return rc;
> +
> + i++;
Wouldn't
rc = dps310_calculate_temp(data, &scan->channels[i++]);
work?
> + }
> +
> + if (test_bit(DPS310_SCAN_PRESSURE, scan_mask)) {
> + rc = dps310_read_pres_raw_locked(data);
> + if (rc)
> + return rc;
> +
> + /* Pascals, see the comment on the channel definition */
> + rc = dps310_calculate_pressure(data, &scan->channels[i]);
For the consistency's sake
rc = dps310_calculate_pressure(data, &scan->channels[i++]);
> + if (rc)
> + return rc;
> + }
> +
> + return 0;
> +}
...
> +static irqreturn_t dps310_trigger_handler(int irq, void *p)
> +{
> + struct iio_poll_func *pf = p;
> + struct iio_dev *iio = pf->indio_dev;
> + struct dps310_data *data = iio_priv(iio);
> + struct dps310_scan scan = { };
> + int rc;
> +
> + mutex_lock(&data->lock);
> + rc = dps310_fill_scan(data, iio->active_scan_mask, &scan);
> + mutex_unlock(&data->lock);
Hmm... Is scoped_guard() too bad here?
> + if (rc)
> + goto err;
> + iio_push_to_buffers_with_ts(iio, &scan, sizeof(scan),
> + iio_get_time_ns(iio));
I would go with a single line.
> +err:
> + iio_trigger_notify_done(iio->trig);
> + return IRQ_HANDLED;
> +}
--
With Best Regards,
Andy Shevchenko
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH v6 5/6] iio: pressure: dps310: add hardware FIFO support
2026-08-24 20:12 ` [PATCH v6 5/6] iio: pressure: dps310: add hardware FIFO support Rupesh Majhi
@ 2026-08-25 9:39 ` Andy Shevchenko
0 siblings, 0 replies; 10+ messages in thread
From: Andy Shevchenko @ 2026-08-25 9:39 UTC (permalink / raw)
To: Rupesh Majhi
Cc: Andy Shevchenko, Bill Wendling, David Lechner, Eddie James,
Joel Stanley, Jonathan Cameron, Justin Stitt, Nathan Chancellor,
Nick Desaulniers, Nuno Sá, linux-iio, linux-kernel, llvm
On Mon, Aug 24, 2026 at 11:12:02PM +0300, Rupesh Majhi wrote:
> Use the 32 entry FIFO for buffered capture, so a reader wakes once per
When referring to the size, use dash: 32-entry
> batch of samples instead of once per sample.
> FIFO runs when no trigger is attached and stays off when one is.
> iio_verify_update() already picks the mode, so buffer setup ops just
> branch on iio_device_get_current_mode(), as rohm-bm1390.c does.
>
> Drain is on a timer because there is no interrupt to use and nothing in
"Drain is on a timer..." --> What is this supposed to mean?
> tree wires the INT pin. hwfifo_flush_to_buffer is not enough on its own:
"The .hwfifo_flush_to_buffer()..."
> iio_buffer_read() sleeps until something is pushed, so a blocking reader
> would hang with samples still sitting in the FIFO. Hook is kept for
> poll() and non-blocking reads.
>
> Pressure entries drive the scans and reuse last temperature, so the two
> configured rates stay independent. FIFO does not timestamp entries, they
> are estimated from the sample rate.
...
> +/*
> + * Bounds on the drain interval. The lower bound keeps a fast rate from
> + * flooding the workqueue; the upper bound keeps the FIFO from filling while
> + * nothing is looking at it.
> + */
> +#define DPS310_DRAIN_MIN_MS 20
> +#define DPS310_DRAIN_MAX_MS 2000
2 * MSEC_PER_SEC
> +
> /* Make sure sleep time is <= 30ms for usleep_range */
> #define DPS310_POLL_SLEEP_US(t) min(30000, (t) / 8)
30 * USEC_PER_MSEC
...
> struct dps310_data {
Run `pahole` and check if there are gaps to fill. In such a case think if it
would make sense to shuffle a bit the added fields to make the structure more
compact.
> s32 pressure_raw;
> s32 temp_raw;
> bool timeout_recovery_failed;
> +
> + /* FIFO capture state, used only while the hardware FIFO is enabled */
> + struct iio_dev *iio;
> + struct delayed_work fifo_work;
> + unsigned int watermark;
> + unsigned int drain_interval_ms;
> + s64 fifo_timestamp;
> + s32 fifo_temp_raw;
> + bool fifo_temp_valid;
> };
...
> +static int dps310_fifo_set_enable(struct dps310_data *data, bool enable)
> + __must_hold(&data->lock)
> +{
> + return regmap_write_bits(data->regmap, DPS310_CFG_REG, DPS310_FIFO_EN,
> + enable ? DPS310_FIFO_EN : 0);
_assign_bits()?
> +}
...
> +/*
> + * There is no interrupt wired on any in-tree platform and the binding has no
> + * interrupts property, so the FIFO is drained on a timer, at an interval below
> + * the time it takes to fill. See DPS310_FIFO_DEPTH for why late is bad.
> + */
> +static int dps310_fifo_interval(struct dps310_data *data, unsigned int *ms)
> + __must_hold(&data->lock)
> +{
> + bool pressure_enabled = test_bit(DPS310_SCAN_PRESSURE,
> + data->iio->active_scan_mask);
> + unsigned int fill_ms, want_ms;
> + int rc, prs_rate, tmp_rate;
Can _rate:s be negative?
> + rc = dps310_get_pres_samp_freq(data, &prs_rate);
> + if (rc)
> + return rc;
> +
> + rc = dps310_get_temp_samp_freq(data, &tmp_rate);
> + if (rc)
> + return rc;
> +
> + /* Both streams share the same entries, so they fill it together. */
> + fill_ms = MSEC_PER_SEC * DPS310_FIFO_DEPTH / (prs_rate + tmp_rate);
> +
> + /*
> + * The DPS310 has no configurable hardware watermark, only a FIFO-full
> + * condition, so the watermark is taken as the number of scans the user
> + * is prepared to wait for and drives the drain interval instead. Scans
> + * come at the rate of whichever measurement drives them, which is not
> + * the pressure rate when only the temperature channel is enabled.
> + */
> + want_ms = data->watermark * MSEC_PER_SEC /
> + (pressure_enabled ? prs_rate : tmp_rate);
With plain if-else this becomes more readable.
> + *ms = clamp(min(want_ms, fill_ms / 2), DPS310_DRAIN_MIN_MS,
> + DPS310_DRAIN_MAX_MS);
> +
> + return 0;
> +}
...
> +/*
> + * Read a single FIFO entry. Returns 1 if a sample was read, 0 once the FIFO is
> + * empty, or a negative error.
> + */
> +static int dps310_fifo_read_entry(struct dps310_data *data, s32 *value,
> + bool *is_pressure)
Instead of using this boolean, use return 1 or return 2.
> + __must_hold(&data->lock)
> +{
> + u8 val[3];
> + s32 raw;
> + int rc;
> +
> + /*
> + * Every entry is read through the pressure registers regardless of
> + * which measurement produced it, with the type tagged in the LSB.
> + */
> + rc = regmap_bulk_read(data->regmap, DPS310_PRS_BASE, val, sizeof(val));
> + if (rc < 0)
> + return rc;
> +
> + raw = get_unaligned_be24(val);
> + if (raw == DPS310_FIFO_EMPTY_VAL)
> + return 0;
> +
> + *is_pressure = raw & DPS310_FIFO_TAG_PRS;
> + *value = sign_extend32(raw, 23);
> +
> + return 1;
> +}
...
> +static int dps310_fifo_push_scan(struct dps310_data *data, s32 temp_raw,
> + s32 pressure_raw, s64 timestamp)
> + __must_hold(&data->lock)
> +{
> + struct iio_dev *iio = data->iio;
> + struct dps310_scan scan = { };
> + int i = 0;
Same comments as earlier in the series.
> + int rc;
> +
> + /*
> + * The compensation helpers read the cached raw values. Sysfs reads take
> + * the direct-mode claim, so they cannot be looking at these while a
> + * buffered capture is running.
> + */
> + data->temp_raw = temp_raw;
> + data->pressure_raw = pressure_raw;
> +
> + if (test_bit(DPS310_SCAN_TEMP, iio->active_scan_mask)) {
> + rc = dps310_calculate_temp(data, &scan.channels[i]);
> + if (rc)
> + return rc;
> +
> + i++;
> + }
> +
> + if (test_bit(DPS310_SCAN_PRESSURE, iio->active_scan_mask)) {
> + rc = dps310_calculate_pressure(data, &scan.channels[i]);
> + if (rc)
> + return rc;
> + }
> +
> + iio_push_to_buffers_with_ts(iio, &scan, sizeof(scan), timestamp);
> +
> + return 0;
> +}
...
I stopped here, I think you should understand what this code is all doing. Now
it's an AI mess. Split this patch to a few smaller ones each of them you understand.
--
With Best Regards,
Andy Shevchenko
^ permalink raw reply [flat|nested] 10+ messages in thread
end of thread, other threads:[~2026-08-25 9:39 UTC | newest]
Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-08-24 20:11 [PATCH v6 0/6] iio: pressure: dps310: FIFO and triggered buffer support Rupesh Majhi
2026-08-24 20:11 ` [PATCH v6 1/6] iio: pressure: dps310: fix CFG_REG bit definitions Rupesh Majhi
2026-08-24 20:11 ` [PATCH v6 2/6] iio: pressure: dps310: use a local device pointer in probe Rupesh Majhi
2026-08-24 20:12 ` [PATCH v6 3/6] iio: pressure: dps310: rework the raw read paths Rupesh Majhi
2026-08-25 8:59 ` Andy Shevchenko
2026-08-24 20:12 ` [PATCH v6 4/6] iio: pressure: dps310: add triggered buffer support Rupesh Majhi
2026-08-25 9:06 ` Andy Shevchenko
2026-08-24 20:12 ` [PATCH v6 5/6] iio: pressure: dps310: add hardware FIFO support Rupesh Majhi
2026-08-25 9:39 ` Andy Shevchenko
2026-08-24 20:12 ` [PATCH v6 6/6] iio: pressure: dps310: check the lock markings with context analysis Rupesh Majhi
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox