iio: humidity: am2315: fix negative temperature decoding
PATCH 1/1 iio: humidity: am2315: fix negative temperature decoding
The AM2315/AM2320 report temperature as a sign-magnitude value, with bit 15 as the sign and bits 14-0 as the magnitude. am2315_read_data() stores the raw value directly in an s16, causing negative temperatures to be interpreted as two's complement. Both the read_raw and the triggered buffer paths are affected.
Decode the sign bit before storing the temperature value. Positive readings are unaffected.
---
Other implementations [1] [2] for the same sensor have hit this bug when the sensor was placed in a freezer, although neither is kernel code.
[1] https://github.com/adafruit/Adafruit_CircuitPython_AM2320/issues/1
[2] https://github.com/letscontrolit/ESPEasy/issues/307
drivers/iio/humidity/am2315.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-)
drivers/iio/humidity/am2315.c +10 -2
@@ -113,6 +113,7 @@ static int am2315_read_data(struct am2315_data *data,*/ u8 rx_buf[8 ];u16 crc; +u16 temp_raw;/* First wake up the device. */ am2315_ping (data->client);
@@ -144,8 +145,15 @@ static int am2315_read_data(struct am2315_data *data, sensor_data->hum_data = (rx_buf[AM2315_HUM_OFFSET ] <<8 ) | rx_buf[AM2315_HUM_OFFSET +1 ]; - sensor_data->temp_data = (rx_buf[AM2315_TEMP_OFFSET ] <<8 ) | - rx_buf[AM2315_TEMP_OFFSET +1 ]; + +/* +* Temperature is sign-magnitude, not two's complement: +* bit 15 is the sign, bits 14-0 are the magnitude (x0.1 degC). +*/ + temp_raw = (rx_buf[AM2315_TEMP_OFFSET ] <<8 ) | + rx_buf[AM2315_TEMP_OFFSET +1 ]; + sensor_data->temp_data = (temp_raw &0x8000 ) ? + -(temp_raw &0x7fff ) : temp_raw;return ret;
Signature · 1 line
2.53.0
Quoted 9 lines · Vidhu Sarwal
The AM2315/AM2320 report temperature as a sign-magnitude value, with bit 15 as the sign and bits 14-0 as the magnitude.
am2315_read_data()stores the raw value directly in an s16, causing negative temperatures to be interpreted as two's complement. Both theread_rawand the triggered buffer paths are affected.Decode the sign bit before storing the temperature value. Positive readings are unaffected.
...
drivers/iio/humidity/am2315.c static int am2315_read_data(struct am2315_data *data, · line 156
Quoted 11 lines
drivers/iio/humidity/am2315.c +9
-2- sensor_data->temp_data = (rx_buf[AM2315_TEMP_OFFSET ] <<8 ) | - rx_buf[AM2315_TEMP_OFFSET +1 ]; + +/* +* Temperature is sign-magnitude, not two's complement: +* bit 15 is the sign, bits 14-0 are the magnitude (x0.1 degC). +*/ + temp_raw = (rx_buf[AM2315_TEMP_OFFSET ] <<8 ) | + rx_buf[AM2315_TEMP_OFFSET +1 ]; + sensor_data->temp_data = (temp_raw &0x8000 ) ? + -(temp_raw &0x7fff ) : temp_raw;
Can you use sign_extend32()?
Signature · 2 lines
With Best Regards, Andy Shevchenko
Hi Andy,
Quoted 1 line · Andy Shevchenko
Can you use
sign_extend32()?
I checked whether sign_extend32() can be used here, but I don't think itcan be applied since as per datasheet [1], The temperature value is sign-magnitude such that bit 15 is the sign and bits 14 to 0 contain the magnitude. For eg., 0x8065 represents -101. sign_extend32(temp_raw, 15) would instead interpret 0x8065 as a two's-complement value, giving us -32667.
So I think the sign bit needs to be decoded explicitly by masking bit 15 and negating the magnitude when set.
[1] https://cdn-shop.adafruit.com/datasheets/AM2315.pdf
Regards,
Vidhu Sarwal
Quoted 2 lines · Vidhu Sarwal
On Mon, Sep 28, 2026 at 9:01 PM Andy Shevchenko
<[email protected]> wrote:
...
Quoted 11 lines
Quoted 1 line
Can you use
sign_extend32()?I checked whether
sign_extend32()can be used here, but I don't think itcan be applied since as per datasheet [1], The temperature value is sign-magnitude such that bit 15 is the sign and bits 14 to 0 contain the magnitude. For eg.,0x8065represents -101.sign_extend32(temp_raw, 15)would instead interpret0x8065as a two's-complement value, giving us -32667.So I think the sign bit needs to be decoded explicitly by masking bit 15 and negating the magnitude when set.
Oh, okay, we (IIO community) already discussed the possibility to have a helper like sign_extend32() for this type of cases, but it appears not so worth it, IIRC.
Quoted 1 line
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 2 lines · Vidhu Sarwal
Decode the sign bit before storing the temperature value. Positive readings are unaffected.
Hey,
A gentle ping on this patch. Please let me know if any further changes are needed.
Thanks,
Vidhu
Jonathan Cameron
maintainerQuoted 9 lines · On Tue, 13 Oct 2026 22:14:56 +0530 Vidhu Sarwal <[email protected]> wrote:
Quoted 2 lines · Vidhu Sarwal
Decode the sign bit before storing the temperature value. Positive readings are unaffected.
Hey,
A gentle ping on this patch. Please let me know if any further changes are needed.
LGTM
Applied and marked for stable.
Thanks,
Jonathan
Quoted 2 lines
Thanks,
Vidhu