All of lore.kernel.org
 help / color / mirror / Atom feed
From: Rupesh Majhi <zoone.rupert@gmail.com>
To: "Andy Shevchenko" <andy@kernel.org>,
	"David Lechner" <dlechner@baylibre.com>,
	"Eddie James" <eajames@linux.ibm.com>,
	"Jonathan Cameron" <jic23@kernel.org>,
	"Nuno Sá" <nuno.sa@analog.com>
Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org,
	Rupesh Majhi <zoone.rupert@gmail.com>,
	Andy Shevchenko <andriy.shevchenko@intel.com>
Subject: [PATCH v2 2/7] iio: pressure: dps310: read the coefficients with get_unaligned_be*()
Date: Thu, 26 Nov 2026 14:13:25 +0200	[thread overview]
Message-ID: <20261126121331.10438-3-zoone.rupert@gmail.com> (raw)
In-Reply-To: <20261126121331.10438-1-zoone.rupert@gmail.com>

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.

Reviewed-by: Andy Shevchenko <andriy.shevchenko@intel.com>
Signed-off-by: Rupesh Majhi <zoone.rupert@gmail.com>
---
v2:
- "No functional change intended." (Andy)

 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 ce83bd3691af..abf1652980ea 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


  parent reply	other threads:[~2026-11-26 12:13 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-26 12:13 [PATCH v2 0/7] iio: pressure: dps310: cleanups Rupesh Majhi
2026-11-26 12:13 ` [PATCH v2 1/7] iio: pressure: dps310: sort register field defines high to low Rupesh Majhi
2026-11-26 13:20   ` Joshua Crofts
2026-11-26 13:44   ` Andy Shevchenko
2026-11-26 12:13 ` Rupesh Majhi [this message]
2026-11-26 12:13 ` [PATCH v2 3/7] iio: pressure: dps310: use min() instead of min_t() Rupesh Majhi
2026-11-26 13:18   ` Joshua Crofts
2026-11-26 13:44   ` Andy Shevchenko
2026-11-26 12:13 ` [PATCH v2 4/7] iio: pressure: dps310: use KILO for the Pa to kPa conversion Rupesh Majhi
2026-11-26 12:13 ` [PATCH v2 5/7] iio: pressure: dps310: use time unit macros for sleeps and timeouts Rupesh Majhi
2026-11-26 13:17   ` Joshua Crofts
2026-11-26 13:49     ` Andy Shevchenko
2026-11-26 14:19       ` Joshua Crofts
2026-11-26 14:37         ` Andy Shevchenko
2026-11-26 13:49   ` Andy Shevchenko
2026-11-26 12:13 ` [PATCH v2 6/7] iio: pressure: dps310: drop the unused i2c_set_clientdata() Rupesh Majhi
2026-11-26 12:13 ` [PATCH v2 7/7] iio: pressure: dps310: include what the driver uses Rupesh Majhi
2026-11-26 13:12   ` Joshua Crofts
2026-11-28  3:56 ` [PATCH v2 0/7] iio: pressure: dps310: cleanups Jonathan Cameron

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=20261126121331.10438-3-zoone.rupert@gmail.com \
    --to=zoone.rupert@gmail.com \
    --cc=andriy.shevchenko@intel.com \
    --cc=andy@kernel.org \
    --cc=dlechner@baylibre.com \
    --cc=eajames@linux.ibm.com \
    --cc=jic23@kernel.org \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.