Skip to contentSkip to conversation

iio: pressure: dps310: cleanups

PATCH In review Rupesh Majhi 4 participants 19 messages Nov 26 – Nov 28

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v2 cover iio: pressure: dps310: cleanups

The cleanups promised in the FIFO series cover letter [1], plus the rest of Andy's list from his review of the ACPI fix.

Patches 1, 3, 4 and 7 build to the same object code. Patch 2 gives the same coefficients as the old parsing, checked on random input. Patch 5 changes the wait after a reset from 15 to 55 ms to 20 ms. Tested on a DPS310 on a BeagleBone Black.

Based on iio testing.

v2:
- 1: sort high to low, fewer lines move and it is the datasheet order (Andy)
- 2: "No functional change intended." (Andy)
- 5: wrap the poll call on logical boundaries, msleep(20) after a reset (Andy)
- 7: drop bits.h and errno.h (Andy), drop mod_devicetable.h (Uwe, Joshua)
- Reviewed-by from Andy on 2, 4 and 6

v1: https://lore.kernel.org/linux-iio/[email protected]/
[1] https://lore.kernel.org/linux-iio/[email protected]/

Rupesh Majhi (7):
iio: pressure: dps310: sort register field defines high to low
iio: pressure: dps310: read the coefficients with get_unaligned_be*()
iio: pressure: dps310: use min() instead of min_t()
iio: pressure: dps310: use KILO for the Pa to kPa conversion
iio: pressure: dps310: use time unit macros for sleeps and timeouts
iio: pressure: dps310: drop the unused i2c_set_clientdata()
iio: pressure: dps310: include what the driver uses

 drivers/iio/pressure/dps310.c | 86 +++++++++++++++--------------------
 1 file changed, 37 insertions(+), 49 deletions(-)

base-commit: f2cbef9101b9fc979a324bb1242b8b802a2484b6

Signature · 1 line
2.43.0
#1 lore raw

PATCH v2 1/7 iio: pressure: dps310: sort register field defines high to low

Some field defines run from the high bit down, others from the low bit up. Sort them all high to low, as the datasheet lists them.

---
v2:
- sort high to low instead of low to high, fewer lines move and it is the datasheet order (Andy)

 drivers/iio/pressure/dps310.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
drivers/iio/pressure/dps310.c +7 -7
index 9ffef150cc4c..ce83bd3691af 100644
@@ -54,20 +54,20 @@
 #define  DPS310_PRS_RATE_BITS	GENMASK(6, 4)
 #define  DPS310_PRS_PRC_BITS	GENMASK(3, 0)
 #define DPS310_TMP_CFG		0x07
+#define  DPS310_TMP_EXT		BIT(7)
 #define  DPS310_TMP_RATE_BITS	GENMASK(6, 4)
 #define   DPS310_TMP_RATE_1SPS	0
 #define  DPS310_TMP_PRC_BITS	GENMASK(3, 0)
 #define   DPS310_TMP_PRC_1	0
-#define  DPS310_TMP_EXT		BIT(7)
 #define DPS310_MEAS_CFG		0x08
+#define  DPS310_COEF_RDY	BIT(7)
+#define  DPS310_SENSOR_RDY	BIT(6)
+#define  DPS310_TMP_RDY		BIT(5)
+#define  DPS310_PRS_RDY		BIT(4)
 #define  DPS310_MEAS_CTRL_BITS	GENMASK(2, 0)
-#define   DPS310_PRS_EN		BIT(0)
-#define   DPS310_TEMP_EN	BIT(1)
 #define   DPS310_BACKGROUND	BIT(2)
-#define  DPS310_PRS_RDY		BIT(4)
-#define  DPS310_TMP_RDY		BIT(5)
-#define  DPS310_SENSOR_RDY	BIT(6)
-#define  DPS310_COEF_RDY	BIT(7)
+#define   DPS310_TEMP_EN	BIT(1)
+#define   DPS310_PRS_EN		BIT(0)
 #define DPS310_CFG_REG		0x09
 #define  DPS310_INT_HL		BIT(7)
 #define  DPS310_INT_TMP		BIT(5)
Signature · 1 line
2.43.0
Reviewed-by on v2 1/7
#2 lore raw

Re: [PATCH v2 1/7] iio: pressure: dps310: sort register field defines high to low

Quoted 6 lines · Rupesh Majhi

Some field defines run from the high bit down, others from the low bit up. Sort them all high to low, as the datasheet lists them.

---

Signature · 2 lines
Kind regards,
Joshua Crofts
Reviewed-by on v2 1/7
#3 lore raw

Re: [PATCH v2 1/7] iio: pressure: dps310: sort register field defines high to low

In reply to Rupesh Majhi
Quoted 2 lines · Rupesh Majhi

Some field defines run from the high bit down, others from the low bit up. Sort them all high to low, as the datasheet lists them.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#4 lore raw

PATCH v2 2/7 iio: pressure: dps310: read the coefficients with get_unaligned_be*()

In reply to Rupesh Majhi

The coefficients are big endian fields, some of them 12 or 20 bits wide and not byte aligned. Read each with get_unaligned_be16() or get_unaligned_be24() and let sign_extend32() drop the bits of the neighbouring field, as the ZTS8032 coefficients already do.

No functional change intended.

---
v2:
- "No functional change intended." (Andy)

 drivers/iio/pressure/dps310.c | 36 +++++++++--------------------------
 1 file changed, 9 insertions(+), 27 deletions(-)
drivers/iio/pressure/dps310.c +9 -27
index ce83bd3691af..abf1652980ea 100644
@@ -239,8 +239,6 @@ static int dps310_get_coefs(struct dps310_data *data)
 {
 	int rc;
 	u8 coef[MAX_COEF_LEN];
-	u32 c0, c1;
-	u32 c00, c10, c20, c30, c01, c11, c21;
 
 	/* Read all sensor calibration coefficients from the COEF registers. */
 	rc = regmap_bulk_read(data->regmap, DPS310_COEF_BASE, coef,
@@ -252,37 +250,21 @@ static int dps310_get_coefs(struct dps310_data *data)
 	 * Calculate temperature calibration coefficients c0 and c1. The
 	 * numbers are 12-bit 2's complement numbers.
 	 */
-	c0 = (coef[0] << 4) | (coef[1] >> 4);
-	data->c0 = sign_extend32(c0, 11);
-
-	c1 = ((coef[1] & GENMASK(3, 0)) << 8) | coef[2];
-	data->c1 = sign_extend32(c1, 11);
+	data->c0 = sign_extend32(get_unaligned_be16(&coef[0]) >> 4, 11);
+	data->c1 = sign_extend32(get_unaligned_be16(&coef[1]), 11);
 
 	/*
 	 * Calculate pressure calibration coefficients. c00 and c10 are 20 bit
 	 * 2's complement numbers, while c01, c11, c20, c21 and c30 are 16 bit
 	 * 2's complement numbers.
 	 */
-	c00 = (coef[3] << 12) | (coef[4] << 4) | (coef[5] >> 4);
-	data->c00 = sign_extend32(c00, 19);
-
-	c10 = ((coef[5] & GENMASK(3, 0)) << 16) | (coef[6] << 8) | coef[7];
-	data->c10 = sign_extend32(c10, 19);
-
-	c01 = (coef[8] << 8) | coef[9];
-	data->c01 = sign_extend32(c01, 15);
-
-	c11 = (coef[10] << 8) | coef[11];
-	data->c11 = sign_extend32(c11, 15);
-
-	c20 = (coef[12] << 8) | coef[13];
-	data->c20 = sign_extend32(c20, 15);
-
-	c21 = (coef[14] << 8) | coef[15];
-	data->c21 = sign_extend32(c21, 15);
-
-	c30 = (coef[16] << 8) | coef[17];
-	data->c30 = sign_extend32(c30, 15);
+	data->c00 = sign_extend32(get_unaligned_be24(&coef[3]) >> 4, 19);
+	data->c10 = sign_extend32(get_unaligned_be24(&coef[5]), 19);
+	data->c01 = sign_extend32(get_unaligned_be16(&coef[8]), 15);
+	data->c11 = sign_extend32(get_unaligned_be16(&coef[10]), 15);
+	data->c20 = sign_extend32(get_unaligned_be16(&coef[12]), 15);
+	data->c21 = sign_extend32(get_unaligned_be16(&coef[14]), 15);
+	data->c30 = sign_extend32(get_unaligned_be16(&coef[16]), 15);
 
 	if (!data->info->has_c31_c40)
 		return 0;
Signature · 1 line
2.43.0
#5 lore raw

PATCH v2 3/7 iio: pressure: dps310: use min() instead of min_t()

In reply to Rupesh Majhi

min() accepts s64 and int, both signed, so min_t() and the cast are not needed.

---
 drivers/iio/pressure/dps310.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/pressure/dps310.c +1 -1
index abf1652980ea..493eb56bde98 100644
@@ -848,7 +848,7 @@ static int dps310_calculate_pressure(struct dps310_data *data, int *val)
 	if (pressure < 0)
 		return -ERANGE;
 
-	*val = (int)min_t(s64, pressure, INT_MAX);
+	*val = min(pressure, INT_MAX);
 
 	return 0;
 }
Signature · 1 line
2.43.0
#8 lore raw

PATCH v2 4/7 iio: pressure: dps310: use KILO for the Pa to kPa conversion

In reply to Rupesh Majhi

Spell the Pa to kPa divisor as KILO from units.h, in both places.

---
 drivers/iio/pressure/dps310.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/iio/pressure/dps310.c +3 -2
index 493eb56bde98..f802c2c59072 100644
@@ -33,6 +33,7 @@
 #include <linux/slab.h>
 #include <linux/time.h>
 #include <linux/unaligned.h>
+#include <linux/units.h>
 #include <linux/workqueue.h>
 
 #include <linux/iio/buffer.h>
@@ -895,13 +896,13 @@ static int dps310_read_pressure(struct dps310_data *data, int *val, int *val2,
 		if (rc)
 			return rc;
 
-		*val2 = 1000; /* Convert Pa to KPa per IIO ABI */
+		*val2 = KILO; /* 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;
+		*val2 = KILO;
 		return IIO_VAL_FRACTIONAL;
 
 	case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
Signature · 1 line
2.43.0
#9 lore raw

PATCH v2 5/7 iio: pressure: dps310: use time unit macros for sleeps and timeouts

In reply to Rupesh Majhi

Write the microsecond values as multiples of USEC_PER_MSEC, like the poll timeout next to them already uses USEC_PER_SEC.

After a reset wait 20 ms with msleep() instead of 15 to 55 ms. The specification says 15 ms, so this keeps some margin.

---
v2:
- wrap the poll call on logical boundaries (Andy)
- msleep(20) after a reset (Andy)

 drivers/iio/pressure/dps310.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
drivers/iio/pressure/dps310.c +6 -4
index f802c2c59072..1d46817ad04a 100644
@@ -20,6 +20,7 @@
 
 #include <linux/bitfield.h>
 #include <linux/cleanup.h>
+#include <linux/delay.h>
 #include <linux/devm-helpers.h>
 #include <linux/i2c.h>
 #include <linux/interrupt.h>
@@ -103,7 +104,7 @@
 #define DPS310_DRAIN_MAX_MS	(2 * MSEC_PER_SEC)
 
 /* Make sure sleep time is <= 30ms for usleep_range */
-#define DPS310_POLL_SLEEP_US(t)		min(30000, (t) / 8)
+#define DPS310_POLL_SLEEP_US(t)		min(30 * USEC_PER_MSEC, (t) / 8)
 /* A period plus a measurement time; silently handle error in rate value */
 #define DPS310_POLL_TIMEOUT_US(rc)	(2 * USEC_PER_SEC / max(rc, 1))
 
@@ -369,8 +370,9 @@ static int dps310_startup(struct dps310_data *data)
 	 * Calibration coefficients required for reporting temperature.
 	 * They are available 40ms after the device has started
 	 */
-	rc = regmap_read_poll_timeout(data->regmap, DPS310_MEAS_CFG, ready,
-				      ready & DPS310_COEF_RDY, 10000, 40000);
+	rc = regmap_read_poll_timeout(data->regmap, DPS310_MEAS_CFG,
+				      ready, ready & DPS310_COEF_RDY,
+				      10 * USEC_PER_MSEC, 40 * USEC_PER_MSEC);
 	if (rc)
 		return rc;
 
@@ -558,7 +560,7 @@ static int dps310_reset_wait(struct dps310_data *data)
 		return rc;
 
 	/* Wait for device chip access: 15ms in specification */
-	usleep_range(15000, 55000);
+	msleep(20);
 	return 0;
 }
 
Signature · 1 line
2.43.0
Reviewed-by on v2 5/7
#10 lore raw

Re: [PATCH v2 5/7] iio: pressure: dps310: use time unit macros for sleeps and timeouts

Quoted 49 lines · Rupesh Majhi

Write the microsecond values as multiples of USEC_PER_MSEC, like the poll timeout next to them already uses USEC_PER_SEC.

After a reset wait 20 ms with msleep() instead of 15 to 55 ms. The specification says 15 ms, so this keeps some margin.

---
v2:
- wrap the poll call on logical boundaries (Andy)
- msleep(20) after a reset (Andy)

 drivers/iio/pressure/dps310.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
drivers/iio/pressure/dps310.c +5 -3
index f802c2c59072..1d46817ad04a 100644
@@ -20,6 +20,7 @@
 
 #include <linux/bitfield.h>
 #include <linux/cleanup.h>
+#include <linux/delay.h>
 #include <linux/devm-helpers.h>
 #include <linux/i2c.h>
 #include <linux/interrupt.h>
@@ -103,7 +104,7 @@
 #define DPS310_DRAIN_MAX_MS	(2 * MSEC_PER_SEC)
 
 /* Make sure sleep time is <= 30ms for usleep_range */
-#define DPS310_POLL_SLEEP_US(t)		min(30000, (t) / 8)
+#define DPS310_POLL_SLEEP_US(t)		min(30 * USEC_PER_MSEC, (t) / 8)
 /* A period plus a measurement time; silently handle error in rate value */
 #define DPS310_POLL_TIMEOUT_US(rc)	(2 * USEC_PER_SEC / max(rc, 1))
 
@@ -369,8 +370,9 @@ static int dps310_startup(struct dps310_data *data)
 	 * Calibration coefficients required for reporting temperature.
 	 * They are available 40ms after the device has started
 	 */
-	rc = regmap_read_poll_timeout(data->regmap, DPS310_MEAS_CFG, ready,
-				      ready & DPS310_COEF_RDY, 10000, 40000);
+	rc = regmap_read_poll_timeout(data->regmap, DPS310_MEAS_CFG,
+				      ready, ready & DPS310_COEF_RDY,
+				      10 * USEC_PER_MSEC, 40 * USEC_PER_MSEC);
 	if (rc)
 		return rc;
 

drivers/iio/pressure/dps310.c static int dps310_reset_wait(struct dps310_data *data) · line 563

Quoted 5 lines
drivers/iio/pressure/dps310.c +1 -1
@@ -558,7 +560,7 @@ static int dps310_reset_wait(struct dps310_data *data)
 		return rc;
 
 	/* Wait for device chip access: 15ms in specification */
-	usleep_range(15000, 55000);
+	msleep(20);

I think it's safe to use 15 as specified in the datasheet, as msleep() guarantees sleep for the specified time + a max of 12.5% slack.

This can be tweaked whilst applying though (or not, at it's pretty minuscule).

Either way,

Signature · 2 lines
Kind regards,
Joshua Crofts
#11 lore raw

Re: [PATCH v2 5/7] iio: pressure: dps310: use time unit macros for sleeps and timeouts

Quoted 1 line · Joshua Crofts

On 26-Nov-26 13:13, Rupesh Majhi wrote:

...

Quoted 4 lines

drivers/iio/pressure/dps310.c static int dps310_reset_wait(struct dps310_data *data) · line 563

Quoted 2 lines
drivers/iio/pressure/dps310.c +1 -1
-	usleep_range(15000, 55000);
+	msleep(20);

I think it's safe to use 15 as specified in the datasheet, as msleep() guarantees sleep for the specified time + a max of 12.5% slack.

Wouldn't be better to have some margin still? You know that time goes differently on different CPUs and in general time can go backwards (in computers), so the long sleeps can (I haven't checked though) go both ways, and a minimum sleeping time can be less. Also on the other side firmware might have issues in some circumstances and require slightly bigger sleep than specified. That's why I proposed margin.

Quoted 1 line

This can be tweaked whilst applying though (or not, at it's pretty minuscule).

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#12 lore raw

Re: [PATCH v2 5/7] iio: pressure: dps310: use time unit macros for sleeps and timeouts

Quoted 10 lines · Andy Shevchenko
Quoted 1 line · Joshua Crofts

On 26-Nov-26 13:13, Rupesh Majhi wrote:

...

Quoted 4 lines

drivers/iio/pressure/dps310.c static int dps310_reset_wait(struct dps310_data *data) · line 563

Quoted 2 lines
drivers/iio/pressure/dps310.c +1 -1
-	usleep_range(15000, 55000);
+	msleep(20);

I think it's safe to use 15 as specified in the datasheet, as msleep() guarantees sleep for the specified time + a max of 12.5% slack.

Quoted 6 lines

Wouldn't be better to have some margin still? You know that time goes differently on different CPUs and in general time can go backwards (in computers), so the long sleeps can (I haven't checked though) go both ways, and a minimum sleeping time can be less. Also on the other side firmware might have issues in some circumstances and require slightly bigger sleep than specified. That's why I proposed margin.

Sure, it's just that some patches add a margin and some patches ignore this and just add the default datasheet value. A lot of times slack is an adequate enough margin. I was always for just the default value.

Signature · 2 lines
Kind regards,
Joshua Crofts
#13 lore raw

Re: [PATCH v2 5/7] iio: pressure: dps310: use time unit macros for sleeps and timeouts

Quoted 3 lines · Joshua Crofts
Quoted 2 lines · Andy Shevchenko
Quoted 1 line · Joshua Crofts

On 26-Nov-26 13:13, Rupesh Majhi wrote:

...

Quoted 11 lines
Quoted 11 lines
Quoted 4 lines

drivers/iio/pressure/dps310.c static int dps310_reset_wait(struct dps310_data *data) · line 563

Quoted 2 lines
drivers/iio/pressure/dps310.c +1 -1
-	usleep_range(15000, 55000);
+	msleep(20);

I think it's safe to use 15 as specified in the datasheet, as msleep() guarantees sleep for the specified time + a max of 12.5% slack.

Wouldn't be better to have some margin still? You know that time goes differently on different CPUs and in general time can go backwards (in computers), so the long sleeps can (I haven't checked though) go both ways, and a minimum sleeping time can be less. Also on the other side firmware might have issues in some circumstances and require slightly bigger sleep than specified. That's why I proposed margin.

Quoted 3 lines

Sure, it's just that some patches add a margin and some patches ignore this and just add the default datasheet value. A lot of times slack is an adequate enough margin. I was always for just the default value.

Yeah, probably for 10+ ms delays this is fine.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
Reviewed-by on v2 5/7
#14 lore raw

Re: [PATCH v2 5/7] iio: pressure: dps310: use time unit macros for sleeps and timeouts

In reply to Rupesh Majhi
Quoted 5 lines · Rupesh Majhi

Write the microsecond values as multiples of USEC_PER_MSEC, like the poll timeout next to them already uses USEC_PER_SEC.

After a reset wait 20 ms with msleep() instead of 15 to 55 ms. The specification says 15 ms, so this keeps some margin.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#15 lore raw

PATCH v2 6/7 iio: pressure: dps310: drop the unused i2c_set_clientdata()

In reply to Rupesh Majhi

Nothing reads the client data back.

---
 drivers/iio/pressure/dps310.c | 8 +-------
 1 file changed, 1 insertion(+), 7 deletions(-)
drivers/iio/pressure/dps310.c +1 -7
index 1d46817ad04a..72f62a1aa3ea 100644
@@ -1633,13 +1633,7 @@ static int dps310_probe(struct i2c_client *client)
 			return rc;
 	}
 
-	rc = devm_iio_device_register(dev, iio);
-	if (rc)
-		return rc;
-
-	i2c_set_clientdata(client, iio);
-
-	return 0;
+	return devm_iio_device_register(dev, iio);
 }
 
 static const struct i2c_device_id dps310_id[] = {
Signature · 1 line
2.43.0
#16 lore raw

PATCH v2 7/7 iio: pressure: dps310: include what the driver uses

In reply to Rupesh Majhi

Include the headers for what the driver uses directly and drop linux/iio/sysfs.h, which it does not use.

---
v2:
- drop bits.h and errno.h, bitops.h and err.h bring them (Andy)
- drop mod_devicetable.h, i2c.h brings the ID table types (Uwe, Joshua)

 drivers/iio/pressure/dps310.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
drivers/iio/pressure/dps310.c +10 -1
index 72f62a1aa3ea..74d4111b5e29 100644
@@ -18,28 +18,37 @@
  *   c0 * 0.5 + c1 * T_raw / kT °C
  */
 
+#include <linux/array_size.h>
 #include <linux/bitfield.h>
+#include <linux/bitops.h>
 #include <linux/cleanup.h>
+#include <linux/container_of.h>
 #include <linux/delay.h>
+#include <linux/dev_printk.h>
+#include <linux/device/devres.h>
 #include <linux/devm-helpers.h>
+#include <linux/err.h>
 #include <linux/i2c.h>
 #include <linux/interrupt.h>
 #include <linux/irq.h>
+#include <linux/jiffies.h>
 #include <linux/limits.h>
+#include <linux/log2.h>
 #include <linux/math64.h>
+#include <linux/minmax.h>
 #include <linux/module.h>
 #include <linux/mutex.h>
 #include <linux/overflow.h>
 #include <linux/regmap.h>
 #include <linux/slab.h>
 #include <linux/time.h>
+#include <linux/types.h>
 #include <linux/unaligned.h>
 #include <linux/units.h>
 #include <linux/workqueue.h>
 
 #include <linux/iio/buffer.h>
 #include <linux/iio/iio.h>
-#include <linux/iio/sysfs.h>
 #include <linux/iio/trigger.h>
 #include <linux/iio/trigger_consumer.h>
 #include <linux/iio/triggered_buffer.h>
Signature · 1 line
2.43.0
maintainer
#18 lore raw
In reply to Rupesh Majhi
Quoted 7 lines · On Thu, 26 Nov 2026 14:13:23 +0200 Rupesh Majhi <[email protected]> wrote:

The cleanups promised in the FIFO series cover letter [1], plus the rest of Andy's list from his review of the ACPI fix.

Patches 1, 3, 4 and 7 build to the same object code. Patch 2 gives the same coefficients as the old parsing, checked on random input. Patch 5 changes the wait after a reset from 15 to 55 ms to 20 ms. Tested on a DPS310 on a BeagleBone Black.

Applied to the testing branch of iio.git.

Thanks,

Jonathan