From: Guenter Roeck <linux@roeck-us.net>
To: Cong Nguyen <congnt264@gmail.com>, Vadim Pasternak <vadimp@mellanox.com>
Cc: linux-hwmon@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] hwmon: (max6621) fix negative temperature offset and crit readings
Date: Sat, 8 Aug 2026 07:48:54 -0700 [thread overview]
Message-ID: <2651758d-e588-4c2e-aa1d-13772e09874b@roeck-us.net> (raw)
In-Reply-To: <20260808083718.2612205-1-congnt264@gmail.com>
On 8/8/26 01:37, Cong Nguyen wrote:
> max6621_read() reads the temperature input, offset and critical alert
> registers into a u32 and assigns them to the output without sign
> extension. The device reports these values in two's complement (the
> driver comment and the write path, which clamps to
> MAX6621_TEMP_INPUT_MIN == -127000 and encodes negatives, both rely on
> this), so negative values are misreported as large positive numbers:
>
> - temp_offset: *val = (regval >> MAX6621_REG_TEMP_SHIFT) * 1000, an
> unsigned shift, so e.g. a -10 degrees C offset (register 0xfd80)
> reads back as ~+1014000 millidegrees.
> - temp_crit: *val = regval * 1000, so a negative critical threshold
> reads back as a large positive value.
> - temp_input used an s8 intermediate, which is correct for the
> -127..127 range but reports the documented +128 degrees C maximum as
> -128 degrees C.
>
This problem does not exist. The AI is hallucinating a bit. There is no mention
of +128 degrees C in the datasheet.
The problem here is that MAX6621_TEMP_INPUT_MIN (-127) and MAX6621_TEMP_INPUT_MAX (128)
are wrong. That should be -128 and (+)127. I guess the AI doesn't see that.
> The registers are 16-bit (the driver's own PECI error codes occupy
> 0x8000-0x80ff), so sign-extend from bit 15 before scaling. This fixes
> all three reads and drops the now-unused s8 intermediate.
>
The PECI error code reference is just confusing AI slop and completely irrelevant
for the patch.
> Fixes: 92b64580f14b ("hwmon: (max6621) Add support for Maxim MAX6621 temperature sensor")
> Cc: stable@vger.kernel.org
> Assisted-by: Claude:claude-opus-4
> Signed-off-by: Cong Nguyen <congnt264@gmail.com>
> ---
> drivers/hwmon/max6621.c | 11 +++++------
> 1 file changed, 5 insertions(+), 6 deletions(-)
>
> diff --git a/drivers/hwmon/max6621.c b/drivers/hwmon/max6621.c
> index a7066f3a0bb4..03ac3eb5a594 100644
> --- a/drivers/hwmon/max6621.c
> +++ b/drivers/hwmon/max6621.c
> @@ -204,7 +204,6 @@ max6621_read(struct device *dev, enum hwmon_sensor_types type, u32 attr,
> struct max6621_data *data = dev_get_drvdata(dev);
> u32 regval;
> int reg;
> - s8 temp;
> int ret;
>
> switch (type) {
> @@ -225,8 +224,8 @@ max6621_read(struct device *dev, enum hwmon_sensor_types type, u32 attr,
> * The temperature is given in two's complement and 8
> * bits is used for the register conversion.
> */
> - temp = (regval >> MAX6621_REG_TEMP_SHIFT);
> - *val = temp * 1000L;
> + *val = (sign_extend32(regval, 15) >>
> + MAX6621_REG_TEMP_SHIFT) * 1000L;
*val = ((s16)regval >> MAX6621_REG_TEMP_SHIFT) * 1000L;
However, as mentioned above, this is not a problem in the first place
but AI not understanding the code and the real problem..
Guenter
>
> break;
> case hwmon_temp_offset:
> @@ -239,8 +238,8 @@ max6621_read(struct device *dev, enum hwmon_sensor_types type, u32 attr,
> if (ret)
> return ret;
>
> - *val = (regval >> MAX6621_REG_TEMP_SHIFT) *
> - 1000L;
> + *val = (sign_extend32(regval, 15) >>
> + MAX6621_REG_TEMP_SHIFT) * 1000L;
>
> break;
> case hwmon_temp_crit:
> @@ -254,7 +253,7 @@ max6621_read(struct device *dev, enum hwmon_sensor_types type, u32 attr,
> if (ret)
> return ret;
>
> - *val = regval * 1000L;
> + *val = sign_extend32(regval, 15) * 1000L;
>
> break;
> case hwmon_temp_crit_alarm:
next prev parent reply other threads:[~2026-08-08 14:48 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-08 8:37 Cong Nguyen
2026-08-08 14:48 ` Guenter Roeck [this message]
2026-08-08 15:54 ` Nguyễn Công
2026-08-09 2:45 ` Guenter Roeck
2026-08-10 4:33 ` Nguyễn Công
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=2651758d-e588-4c2e-aa1d-13772e09874b@roeck-us.net \
--to=linux@roeck-us.net \
--cc=congnt264@gmail.com \
--cc=linux-hwmon@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=vadimp@mellanox.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®