Skip to contentSkip to conversation

iio: pressure: dps310: cleanups

PATCH In review Rupesh Majhi 3 participants 15 messages Nov 23 – Nov 24

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

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 shortens the longest wait after a reset. Tested on a DPS310 on a BeagleBone Black.

Based on iio testing.

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

Rupesh Majhi (7):
iio: pressure: dps310: sort register field defines low to high
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 | 95 ++++++++++++++++-------------------
 1 file changed, 43 insertions(+), 52 deletions(-)

base-commit: f2cbef9101b9fc979a324bb1242b8b802a2484b6

Signature · 1 line
2.43.0
#1 lore raw

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

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

---
 drivers/iio/pressure/dps310.c | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)
drivers/iio/pressure/dps310.c +11 -11
index 9ffef150cc4c..02dbc5a8ac37 100644
@@ -51,13 +51,13 @@
 #define DPS310_TMP_B1		0x04
 #define DPS310_TMP_B2		0x05
 #define DPS310_PRS_CFG		0x06
-#define  DPS310_PRS_RATE_BITS	GENMASK(6, 4)
 #define  DPS310_PRS_PRC_BITS	GENMASK(3, 0)
+#define  DPS310_PRS_RATE_BITS	GENMASK(6, 4)
 #define DPS310_TMP_CFG		0x07
-#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_RATE_BITS	GENMASK(6, 4)
+#define   DPS310_TMP_RATE_1SPS	0
 #define  DPS310_TMP_EXT		BIT(7)
 #define DPS310_MEAS_CFG		0x08
 #define  DPS310_MEAS_CTRL_BITS	GENMASK(2, 0)
@@ -69,19 +69,19 @@
 #define  DPS310_SENSOR_RDY	BIT(6)
 #define  DPS310_COEF_RDY	BIT(7)
 #define DPS310_CFG_REG		0x09
-#define  DPS310_INT_HL		BIT(7)
-#define  DPS310_INT_TMP		BIT(5)
-#define  DPS310_INT_PRS		BIT(4)
-#define  DPS310_TMP_SHIFT_EN	BIT(3)
-#define  DPS310_PRS_SHIFT_EN	BIT(2)
-#define  DPS310_FIFO_EN		BIT(1)
 #define  DPS310_SPI_EN		BIT(0)
+#define  DPS310_FIFO_EN		BIT(1)
+#define  DPS310_PRS_SHIFT_EN	BIT(2)
+#define  DPS310_TMP_SHIFT_EN	BIT(3)
+#define  DPS310_INT_PRS		BIT(4)
+#define  DPS310_INT_TMP		BIT(5)
+#define  DPS310_INT_HL		BIT(7)
 #define DPS310_INT_STS		0x0a
-#define  DPS310_INT_STS_TMP	BIT(1)
 #define  DPS310_INT_STS_PRS	BIT(0)
+#define  DPS310_INT_STS_TMP	BIT(1)
 #define DPS310_RESET		0x0c
-#define  DPS310_FIFO_FLUSH	BIT(7)
 #define  DPS310_RESET_MAGIC	0x09
+#define  DPS310_FIFO_FLUSH	BIT(7)
 #define DPS310_COEF_BASE	0x10
 #define DPS310_COEF_LEN		18
 #define SPL07_COEF_LEN		21
Signature · 1 line
2.43.0
#2 lore raw

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

Quoted 2 lines · Rupesh Majhi

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

Have you checked which change is less invasive? Can you do it locally and provide diffstat

Quoted 3 lines
---
 drivers/iio/pressure/dps310.c | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)

^^^ like this to see the impact?

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

PATCH 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.

---
 drivers/iio/pressure/dps310.c | 36 +++++++++--------------------------
 1 file changed, 9 insertions(+), 27 deletions(-)
drivers/iio/pressure/dps310.c +9 -27
index 02dbc5a8ac37..48df5a28c140 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
Reviewed-by on v1 2/7
#4 lore raw

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

Quoted 4 lines · 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.

I have checked all the lines (especially ones that use right shifts) and found no issues,

You should probably add
No functional change intended.

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

PATCH 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 48df5a28c140..535b77da7654 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
#6 lore raw

PATCH 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 535b77da7654..e5a89dac1325 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
Reviewed-by on v1 4/7
#7 lore raw

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

Quoted 1 line · Rupesh Majhi

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

Yes, looks good.

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

PATCH 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. Wait the 15 ms after a reset with fsleep(), which allows less slack than the old 55 ms upper bound.

---
 drivers/iio/pressure/dps310.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/iio/pressure/dps310.c +5 -3
index e5a89dac1325..eb5715f75e9e 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))
 
@@ -370,7 +371,8 @@ static int dps310_startup(struct dps310_data *data)
 	 * 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);
+				      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);
+	fsleep(15 * USEC_PER_MSEC);
 	return 0;
 }
 
Signature · 1 line
2.43.0
#9 lore raw

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

Quoted 4 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. Wait the 15 ms after a reset with fsleep(), which allows less slack than the old 55 ms upper bound.

...

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

Quoted 4 lines
drivers/iio/pressure/dps310.c +2 -1
 	rc = regmap_read_poll_timeout(data->regmap, DPS310_MEAS_CFG, ready,
-				      ready & DPS310_COEF_RDY, 10000, 40000);
+				      ready & DPS310_COEF_RDY,
+				      10 * USEC_PER_MSEC, 40 * USEC_PER_MSEC);

Wrap this on logical boundaries

	rc = regmap_read_poll_timeout(data->regmap, DPS310_MEAS_CFG,
				      ready, ready & DPS310_COEF_RDY,
				      10 * USEC_PER_MSEC, 40 * USEC_PER_MSEC);
Quoted 2 lines
drivers/iio/pressure/dps310.c +0 -0
 	if (rc)
 		return rc;

...

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

Quoted 3 lines
drivers/iio/pressure/dps310.c +1 -1
 	/* Wait for device chip access: 15ms in specification */
-	usleep_range(15000, 55000);
+	fsleep(15 * USEC_PER_MSEC);

Usually we give some margin, so putting here 20ms seems reasonable to me. With that in mind, msleep() is fine to use explicitly as delay is so-o long anyway.

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

PATCH 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 eb5715f75e9e..7581ea4c99ba 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
Reviewed-by on v1 6/7
#11 lore raw

Re: [PATCH 6/7] iio: pressure: dps310: drop the unused i2c_set_clientdata()

Quoted 1 line · Rupesh Majhi

Nothing reads the client data back.

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

PATCH 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.

---
 drivers/iio/pressure/dps310.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
drivers/iio/pressure/dps310.c +13 -1
index 7581ea4c99ba..fd17fa38e9fc 100644
@@ -18,28 +18,40 @@
  *   c0 * 0.5 + c1 * T_raw / kT °C
  */
 
+#include <linux/array_size.h>
 #include <linux/bitfield.h>
+#include <linux/bitops.h>
+#include <linux/bits.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/errno.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/mod_devicetable.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
#13 lore raw

Re: [PATCH 7/7] iio: pressure: dps310: include what the driver uses

Quoted 27 lines · Rupesh Majhi

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

---
 drivers/iio/pressure/dps310.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
drivers/iio/pressure/dps310.c +7 -0
index 7581ea4c99ba..fd17fa38e9fc 100644
@@ -18,28 +18,40 @@
  *   c0 * 0.5 + c1 * T_raw / kT °C
  */
 
+#include <linux/array_size.h>
 #include <linux/bitfield.h>
+#include <linux/bitops.h>
+#include <linux/bits.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>

drivers/iio/pressure/dps310.c line 41

Quoted 10 lines
drivers/iio/pressure/dps310.c +5 -0
+#include <linux/errno.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/mod_devicetable.h>

don't add <linux/mod_devicetable.h>. I don't know what made you think this is needed, but it is not. i2c_device_id is already provided by <linux/i2c.h> without the overhead (and useless dependencies).

Best regards
Uwe

#14 lore raw

Re: [PATCH 7/7] iio: pressure: dps310: include what the driver uses

In reply to Rupesh Majhi
Quoted 2 lines · Rupesh Majhi

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

Thanks, a few amendments may be made, see below.

...

drivers/iio/pressure/dps310.c

Quoted 5 lines
drivers/iio/pressure/dps310.c +3 -0
+#include <linux/array_size.h>
 #include <linux/bitfield.h>
 
+#include <linux/bitops.h>
+#include <linux/bits.h>

The former guarantees the latter, so drop bits.h.

drivers/iio/pressure/dps310.c

Quoted 9 lines

#include <linux/cleanup.h>

drivers/iio/pressure/dps310.c +5 -0
+#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/errno.h>

Same here as long as the driver doesn't use special Linux error codes ( >= 512). And for now it doesn't.

drivers/iio/pressure/dps310.c

Quoted 10 lines

#include <linux/i2c.h>
#include <linux/interrupt.h>
#include <linux/irq.h>

drivers/iio/pressure/dps310.c +4 -0
+#include <linux/jiffies.h>
 #include <linux/limits.h>
+#include <linux/log2.h>
 #include <linux/math64.h>
+#include <linux/minmax.h>
 
+#include <linux/mod_devicetable.h>

Uwe already pointed out on this.

Quoted 10 lines

#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>

Signature · 2 lines
With Best Regards,
Andy Shevchenko