Re: [PATCH 2/3] iio: pressure: Support ROHM BU1390

From: Jonathan Cameron
Date: Fri Sep 08 2023 - 14:53:51 EST



> +static int bm1390_read_temp(struct bm1390_data *data, int *temp)
> +{
> + u8 temp_reg[2] __aligned(2);
> + __be16 *temp_raw;
> + int ret;
> + s16 val;
> + bool negative;
> +
> + ret = regmap_bulk_read(data->regmap, BM1390_REG_TEMP_HI, &temp_reg,
> + sizeof(temp_reg));
> + if (ret)
> + return ret;
> +
> + if (temp_reg[0] & 0x80)
> + negative = true;
> + else
> + negative = false;
> +
> + temp_raw = (__be16 *)&temp_reg[0];
> + val = be16_to_cpu(*temp_raw);
> +
> + if (negative) {
> + /*
> + * Two's complement. I am not sure if all supported
> + * architectures actually use 2's complement represantation of

AFAIK they do. Many IIO drivers would be broken if not..

> + * signed ints. If yes, then we could just do the endianes
> + * conversion and say this is the s16 value. However, as I
> + * don't know, and as the conversion is pretty simple. let's
> + * just convert the signed 2's complement to absolute value and
> + * multiply by -1.
> + */
> + val = ~val + 1;
> + val *= -1;
> + }
> +
> + *temp = val;
> +
> + return 0;
> +}