Re: [PATCH] iio: humidity: am2315: fix negative temperature decoding
From: Andy Shevchenko
Date: Mon Sep 28 2026 - 11:44:57 EST
On Mon, Sep 28, 2026 at 5:25 PM Vidhu Sarwal <vidhu.linux@xxxxxxxxx> wrote:
>
> 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.
...
> - 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()?
--
With Best Regards,
Andy Shevchenko