From: Rupesh Majhi <zoone.rupert@gmail.com>
To: "Andy Shevchenko" <andy@kernel.org>,
"Bill Wendling" <morbo@google.com>,
"David Lechner" <dlechner@baylibre.com>,
"Eddie James" <eajames@linux.ibm.com>,
"Joel Stanley" <joel@jms.id.au>,
"Jonathan Cameron" <jic23@kernel.org>,
"Justin Stitt" <justinstitt@google.com>,
"Nathan Chancellor" <nathan@kernel.org>,
"Nick Desaulniers" <nick.desaulniers+lkml@gmail.com>,
"Nuno Sá" <nuno.sa@analog.com>
Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org,
llvm@lists.linux.dev, Rupesh Majhi <zoone.rupert@gmail.com>
Subject: [PATCH v6 3/6] iio: pressure: dps310: rework the raw read paths
Date: Mon, 24 Aug 2026 23:12:00 +0300 [thread overview]
Message-ID: <20260824201203.396651-4-zoone.rupert@gmail.com> (raw)
In-Reply-To: <20260824201203.396651-1-zoone.rupert@gmail.com>
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
next prev parent reply other threads:[~2026-08-24 20:14 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
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 [this message]
2026-08-25 8:59 ` [PATCH v6 3/6] iio: pressure: dps310: rework the raw read paths 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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260824201203.396651-4-zoone.rupert@gmail.com \
--to=zoone.rupert@gmail.com \
--cc=andy@kernel.org \
--cc=dlechner@baylibre.com \
--cc=eajames@linux.ibm.com \
--cc=jic23@kernel.org \
--cc=joel@jms.id.au \
--cc=justinstitt@google.com \
--cc=linux-iio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=llvm@lists.linux.dev \
--cc=morbo@google.com \
--cc=nathan@kernel.org \
--cc=nick.desaulniers+lkml@gmail.com \
--cc=nuno.sa@analog.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox