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

From: Rupesh Majhi

Date: Mon Nov 23 2026 - 17:07:04 EST


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.

Signed-off-by: Rupesh Majhi <zoone.rupert@xxxxxxxxx>
---
drivers/iio/pressure/dps310.c | 36 +++++++++--------------------------
1 file changed, 9 insertions(+), 27 deletions(-)

diff --git a/drivers/iio/pressure/dps310.c b/drivers/iio/pressure/dps310.c
index 02dbc5a8ac37..48df5a28c140 100644
--- a/drivers/iio/pressure/dps310.c
+++ b/drivers/iio/pressure/dps310.c
@@ -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;
--
2.43.0