[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20220906083356.21067-6-farbere@amazon.com>
Date: Tue, 6 Sep 2022 08:33:40 +0000
From: Eliav Farber <farbere@...zon.com>
To: <jdelvare@...e.com>, <linux@...ck-us.net>, <robh+dt@...nel.org>,
<p.zabel@...gutronix.de>, <rtanwar@...linear.com>,
<linux-hwmon@...r.kernel.org>, <devicetree@...r.kernel.org>,
<linux-kernel@...r.kernel.org>
CC: <farbere@...zon.com>, <hhhawa@...zon.com>, <jonnyc@...zon.com>,
<andriy.shevchenko@...el.com>
Subject: [PATCH v4 05/21] hwmon: (mr75203) fix voltage equation for negative source input
According to Moortec Embedded Voltage Monitor (MEVM) series 3 data
sheet, the minimum input signal is -100mv and maximum input signal
is +1000mv.
The equation used to convert the digital word to voltage uses mixed
types (*val signed and n unsigned), and on 64 bit machines also has
different size, since sizeof(u32) = 4 and sizeof(long) = 8.
So when measuring a negative input, n will be small enough, such that
PVT_N_CONST * n < PVT_R_CONST, and the result of
(PVT_N_CONST * n - PVT_R_CONST) will overflow to a very big positive
32 bit number. Then when storing the result in *val it will be the same
value just in 64 bit (instead of it representing a negative number which
will what happen when sizeof(long) = 4).
When -1023 <= (PVT_N_CONST * n - PVT_R_CONST) <= -1
dividing the number by 1024 should result of in 0, but because ">> 10"
is used it results in -1 (0xf...fffff).
This change fixes the sign problem and supports negative values by
casting n to long and replacing the shift right with div operation.
Fixes: 9d823351a337 ("hwmon: Add hardware monitoring driver for Moortec MR75203 PVT controller")
Signed-off-by: Eliav Farber <farbere@...zon.com>
---
V4 -> V3:
- Remove unrelated change (add of empty line).
V3 -> V2:
- Fix equation to support negative values instead of limiting value to
zero.
drivers/hwmon/mr75203.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/drivers/hwmon/mr75203.c b/drivers/hwmon/mr75203.c
index a209f5d95f4b..78dc471e843c 100644
--- a/drivers/hwmon/mr75203.c
+++ b/drivers/hwmon/mr75203.c
@@ -202,7 +202,7 @@ static int pvt_read_in(struct device *dev, u32 attr, int channel, long *val)
n &= SAMPLE_DATA_MSK;
/* Convert the N bitstream count into voltage */
- *val = (PVT_N_CONST * n - PVT_R_CONST) >> PVT_CONV_BITS;
+ *val = (PVT_N_CONST * (long)n - PVT_R_CONST) / (1 << PVT_CONV_BITS);
return 0;
default:
--
2.37.1
Powered by blists - more mailing lists