git: 99cde78e00c2 - main - TMP461 sensor: Fix temperature reading in extended range mode
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Wed, 25 Nov 2026 11:08:07 UTC
The branch main has been updated by olce:
URL: https://cgit.FreeBSD.org/src/commit/?id=99cde78e00c2e460d0e10313e14ef6915ff772f0
commit 99cde78e00c2e460d0e10313e14ef6915ff772f0
Author: Olivier Certner <olce@FreeBSD.org>
AuthorDate: 2026-11-24 09:18:14 +0000
Commit: Olivier Certner <olce@FreeBSD.org>
CommitDate: 2026-11-25 11:07:41 +0000
TMP461 sensor: Fix temperature reading in extended range mode
In extended range mode, the temperature's integral part is stored as an
unsigned byte offset by 64, not as a signed byte. Stop treating it as
a signed value in this case, in particular sign extending it. Doing so
would cause temperatures above 128°C (and in particular nearing the
maximum operating point of 150°C) to be treated as negative ones, as the
sign extension happened after substracting the offset. In the original
commit introducing the driver, sign extension was done before, in which
case the problem manifested as soon as 64°C. It may have been noticed
while introducing support for ADT7461, but was not completely fixed at
that time.
While here, fix typos in two comments.
Reported by: llm-postcr
Reviewed by: manu
Fixes: f89f6f9581bd ("TMP461: Add thermal sensor driver")
Fixes: 6665c7a62839 ("TMP461: Add support for ADT7461 sensor")
MFC after: 1 week
Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D61976
---
sys/dev/iicbus/sensor/tmp461.c | 24 +++++++++++++-----------
1 file changed, 13 insertions(+), 11 deletions(-)
diff --git a/sys/dev/iicbus/sensor/tmp461.c b/sys/dev/iicbus/sensor/tmp461.c
index 69849742cbc1..802b39c3a714 100644
--- a/sys/dev/iicbus/sensor/tmp461.c
+++ b/sys/dev/iicbus/sensor/tmp461.c
@@ -226,7 +226,7 @@ tmp461_write_1(device_t dev, uint8_t reg, uint8_t data)
static int
tmp461_read_temperature(device_t dev, int32_t *temperature, bool remote_measure)
{
- uint8_t data, offset, reg;
+ uint8_t data, config_reg, reg;
struct tmp461_softc *sc;
int error;
@@ -238,7 +238,7 @@ tmp461_read_temperature(device_t dev, int32_t *temperature, bool remote_measure)
if (error != 0)
goto fail;
- /* trigger sample*/
+ /* trigger sample */
error = tmp461_write_1(dev, TMP461_ONESHOT_REG, 0xFF);
if (error != 0)
goto fail;
@@ -247,27 +247,29 @@ tmp461_read_temperature(device_t dev, int32_t *temperature, bool remote_measure)
DELAY(TMP461_SENSOR_MAX_CONV_TIME/(1UL<<data));
/* read config register offset */
- error = tmp461_read_1(dev, TMP461_CONFIG_REG_R, &data);
+ error = tmp461_read_1(dev, TMP461_CONFIG_REG_R, &config_reg);
if (error != 0)
goto fail;
- offset = (data & TMP461_CONFIG_REG_TEMP_RANGE_BIT ?
- TMP461_EXTENDED_TEMP_MODIFIER : 0);
-
reg = remote_measure ?
TMP461_GLOBAL_TEMP_REG_MSB : TMP461_LOCAL_TEMP_REG_MSB;
- /* read temeperature value*/
+ /* read temperature value */
error = tmp461_read_1(dev, reg, &data);
if (error != 0)
goto fail;
- data -= offset;
/*
- * Sign extension has to happen at the end, as left shift of negative
- * signed integer has undefined behavior.
+ * Left shift has to happen first. Indeed, at this point, 'data' is
+ * a non-negative integer fitting in an 'int' and whose shifted value
+ * also fits. By contrast, the subsequent operations can result in
+ * a negative integer, and left shifting such an integer is undefined
+ * behavior.
*/
- *temperature = sign_extend_32(data << 4, 11);
+ *temperature = data << 4;
+ *temperature = (config_reg & TMP461_CONFIG_REG_TEMP_RANGE_BIT) != 0 ?
+ *temperature - (TMP461_EXTENDED_TEMP_MODIFIER << 4) :
+ sign_extend_32(*temperature, 11);
if (remote_measure) {
if (sc->conf & TMP461_REMOTE_TEMP_DOUBLE_REG) {