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

From: Matti Vaittinen
Date: Mon Sep 11 2023 - 01:16:01 EST


On 9/8/23 21:45, Jonathan Cameron wrote:

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

Great. Then I think it means we really can treat the combined value of the registers as s16 BE. Should make this much simpler :)


+ * 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;
+}

--
Matti Vaittinen
Linux kernel developer at ROHM Semiconductors
Oulu Finland

~~ When things go utterly wrong vim users can always type :help! ~~