[PATCH v7 03/10] iio: pressure: dps310: use get_unaligned_be24() for the 24-bit results
From: Rupesh Majhi
Date: Fri Sep 18 2026 - 08:29:03 EST
Pressure and temperature results are big-endian 24-bit values. Build them
with get_unaligned_be24() instead of open-coded shifts.
The coefficients are packed into the bytes rather than byte-aligned, so
they stay as they are.
Assisted-by: LLM
Signed-off-by: Rupesh Majhi <zoone.rupert@xxxxxxxxx>
---
drivers/iio/pressure/dps310.c | 9 +++------
1 file changed, 3 insertions(+), 6 deletions(-)
diff --git a/drivers/iio/pressure/dps310.c b/drivers/iio/pressure/dps310.c
index bd7da4f12749..e7f173e08e01 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>
@@ -468,7 +469,6 @@ static int dps310_read_pres_raw(struct dps310_data *data)
int rc;
int rate;
int timeout;
- s32 raw;
u8 val[3];
if (mutex_lock_interruptible(&data->lock))
@@ -489,8 +489,7 @@ static int dps310_read_pres_raw(struct dps310_data *data)
if (rc < 0)
goto done;
- 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);
@@ -502,14 +501,12 @@ static int dps310_read_temp_ready(struct dps310_data *data)
{
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;
}
--
2.43.0