Re: [PATCH] iio: humidity: am2315: fix negative temperature decoding

From: Andy Shevchenko

Date: Mon Sep 28 2026 - 17:47:46 EST


On Tue, Sep 29, 2026 at 02:37:41AM +0530, Vidhu Sarwal wrote:
> On Mon, Sep 28, 2026 at 9:01 PM Andy Shevchenko
> <andy.shevchenko@xxxxxxxxx> wrote:

...

> > 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.

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.

> [1] https://cdn-shop.adafruit.com/datasheets/AM2315.pdf

--
With Best Regards,
Andy Shevchenko