summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTom Levens <tom.levens@cern.ch>2017-07-03 00:28:58 -0400
committerGuenter Roeck <linux@roeck-us.net>2018-05-21 10:52:00 -0400
commita6282d170339f86e05fd24bdf3104ca644a4b3a4 (patch)
tree2540ca650a1368e51f40914d3df8f0d156453224
parent59df4f4e8e0b9a0abb14ad5128d36d844c3e2689 (diff)
hwmon: (ltc2990) Fix incorrect conversion of negative temperatures
Fix incorrect conversion of negative temperatures by using sign_extend32() instead of a home-grown conversion function. Fixes: df922703574e ("hwmon: Add LTC2990 sensor driver") Signed-off-by: Tom Levens <tom.levens@cern.ch> [groeck: Updated subject and description] Signed-off-by: Guenter Roeck <linux@roeck-us.net>
-rw-r--r--drivers/hwmon/ltc2990.c18
1 files changed, 4 insertions, 14 deletions
diff --git a/drivers/hwmon/ltc2990.c b/drivers/hwmon/ltc2990.c
index 8f8fe059ab48..e320d212ac27 100644
--- a/drivers/hwmon/ltc2990.c
+++ b/drivers/hwmon/ltc2990.c
@@ -11,6 +11,7 @@
11 * the chip's internal temperature and Vcc power supply voltage. 11 * the chip's internal temperature and Vcc power supply voltage.
12 */ 12 */
13 13
14#include <linux/bitops.h>
14#include <linux/err.h> 15#include <linux/err.h>
15#include <linux/hwmon.h> 16#include <linux/hwmon.h>
16#include <linux/hwmon-sysfs.h> 17#include <linux/hwmon-sysfs.h>
@@ -34,15 +35,6 @@
34#define LTC2990_CONTROL_MODE_CURRENT 0x06 35#define LTC2990_CONTROL_MODE_CURRENT 0x06
35#define LTC2990_CONTROL_MODE_VOLTAGE 0x07 36#define LTC2990_CONTROL_MODE_VOLTAGE 0x07
36 37
37/* convert raw register value to sign-extended integer in 16-bit range */
38static int ltc2990_voltage_to_int(int raw)
39{
40 if (raw & BIT(14))
41 return -(0x4000 - (raw & 0x3FFF)) << 2;
42 else
43 return (raw & 0x3FFF) << 2;
44}
45
46/* Return the converted value from the given register in uV or mC */ 38/* Return the converted value from the given register in uV or mC */
47static int ltc2990_get_value(struct i2c_client *i2c, u8 reg, int *result) 39static int ltc2990_get_value(struct i2c_client *i2c, u8 reg, int *result)
48{ 40{
@@ -55,18 +47,16 @@ static int ltc2990_get_value(struct i2c_client *i2c, u8 reg, int *result)
55 switch (reg) { 47 switch (reg) {
56 case LTC2990_TINT_MSB: 48 case LTC2990_TINT_MSB:
57 /* internal temp, 0.0625 degrees/LSB, 13-bit */ 49 /* internal temp, 0.0625 degrees/LSB, 13-bit */
58 val = (val & 0x1FFF) << 3; 50 *result = sign_extend32(val, 12) * 1000 / 16;
59 *result = (val * 1000) >> 7;
60 break; 51 break;
61 case LTC2990_V1_MSB: 52 case LTC2990_V1_MSB:
62 case LTC2990_V3_MSB: 53 case LTC2990_V3_MSB:
63 /* Vx-Vy, 19.42uV/LSB. Depends on mode. */ 54 /* Vx-Vy, 19.42uV/LSB. Depends on mode. */
64 *result = ltc2990_voltage_to_int(val) * 1942 / (4 * 100); 55 *result = sign_extend32(val, 14) * 1942 / 100;
65 break; 56 break;
66 case LTC2990_VCC_MSB: 57 case LTC2990_VCC_MSB:
67 /* Vcc, 305.18μV/LSB, 2.5V offset */ 58 /* Vcc, 305.18μV/LSB, 2.5V offset */
68 *result = (ltc2990_voltage_to_int(val) * 30518 / 59 *result = sign_extend32(val, 14) * 30518 / (100 * 1000) + 2500;
69 (4 * 100 * 1000)) + 2500;
70 break; 60 break;
71 default: 61 default:
72 return -EINVAL; /* won't happen, keep compiler happy */ 62 return -EINVAL; /* won't happen, keep compiler happy */