From: Tabrez Ahmed <tabreztalks@gmail.com>
To: linux@roeck-us.net
Cc: linux-hwmon@vger.kernel.org, linux-kernel@vger.kernel.org,
shuah@kernel.org, me@brighamcampbell.com,
Tabrez Ahmed <tabreztalks@gmail.com>,
Sashiko <sashiko@kernel.org>
Subject: [PATCH v3 1/3] hwmon: (ads7871) Fix endianness bug in 16-bit register reads
Date: Sat, 18 Apr 2026 09:15:59 +0530 [thread overview]
Message-ID: <20260418034601.90226-2-tabreztalks@gmail.com> (raw)
In-Reply-To: <20260418034601.90226-1-tabreztalks@gmail.com>
The ads7871_read_reg16() function relies on spi_w8r16() to read the
16-bit sensor output. The ADS7871 device transmits the Least Significant
Byte (LSB) first.
On Little-Endian architectures, spi_w8r16() correctly reconstructs the
16-bit value. However, on Big-Endian architectures, the byte swapping
causes the first received byte (LSB) to be placed in the most significant
byte of the u16, resulting in corrupted voltage readings.
Replace spi_w8r16() with a manual spi_write_then_read() into a byte array,
and safely reconstruct the integer using get_unaligned_le16() to ensure
correct behavior across all architectures.
Reported-by: Sashiko <sashiko@kernel.org>
Closes: https://sashiko.dev/#/patchset/20260329073352.270451-1-tabreztalks%40gmail.com
Signed-off-by: Tabrez Ahmed <tabreztalks@gmail.com>
---
drivers/hwmon/ads7871.c | 10 ++++++++--
1 file changed, 8 insertions(+), 2 deletions(-)
diff --git a/drivers/hwmon/ads7871.c b/drivers/hwmon/ads7871.c
index 9bfdf9e6bcd77..9b52aa496d522 100644
--- a/drivers/hwmon/ads7871.c
+++ b/drivers/hwmon/ads7871.c
@@ -59,6 +59,7 @@
#include <linux/hwmon-sysfs.h>
#include <linux/err.h>
#include <linux/delay.h>
+#include <asm/unaligned.h>
#define DEVICE_NAME "ads7871"
@@ -77,9 +78,14 @@ static int ads7871_read_reg8(struct spi_device *spi, int reg)
static int ads7871_read_reg16(struct spi_device *spi, int reg)
{
int ret;
+ u8 buf[2];
+
reg = reg | INST_READ_BM | INST_16BIT_BM;
- ret = spi_w8r16(spi, reg);
- return ret;
+ ret = spi_write_then_read(spi, ®, 1, buf, 2);
+ if (ret < 0)
+ return ret;
+
+ return get_unaligned_le16(buf);
}
static int ads7871_write_reg8(struct spi_device *spi, int reg, u8 val)
--
2.43.0
next prev parent reply other threads:[~2026-04-18 3:48 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-04-18 3:45 [PATCH v3 0/3] hwmon: (ads7871) Modernize and fix DMA safety Tabrez Ahmed
2026-04-18 3:45 ` Tabrez Ahmed [this message]
2026-04-18 4:11 ` [PATCH v3 1/3] hwmon: (ads7871) Fix endianness bug in 16-bit register reads sashiko-bot
2026-04-18 3:46 ` [PATCH v3 2/3] hwmon: (ads7871) Convert to hwmon_device_register_with_info Tabrez Ahmed
2026-04-18 3:46 ` [PATCH v3 3/3] hwmon: (ads7871) Use DMA-safe buffer for SPI writes Tabrez Ahmed
2026-04-18 4:56 ` sashiko-bot
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=20260418034601.90226-2-tabreztalks@gmail.com \
--to=tabreztalks@gmail.com \
--cc=linux-hwmon@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@roeck-us.net \
--cc=me@brighamcampbell.com \
--cc=sashiko@kernel.org \
--cc=shuah@kernel.org \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox