Skip to contentSkip to conversation

hwmon: (ina2xx) Cast to s16 on shunt and current regs

PATCH Applied Fabio Baltieri 2 participants 2 messages Jun 8, 2014 – Jun 9, 2014 Hardware monitoring

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 hwmon: (ina2xx) Cast to s16 on shunt and current regs

All devices supported by ina2xx are bidirectional and reports the measured shunt voltage and power values as a signed 16 bit, but the current driver implementation caches all registers as u16, leading to an incorrect sign extension when reporting to the userspace in ina2xx_get_value().

This patch fixes the problem by casting the signed registers to s16. Tested on an INA219.

---
 drivers/hwmon/ina2xx.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/hwmon/ina2xx.c +4 -3
index 93d26e8..bfd3f3e 100644
@@ -148,7 +148,8 @@ static int ina2xx_get_value(struct ina2xx_data *data, u8 reg)
 
 	switch (reg) {
 	case INA2XX_SHUNT_VOLTAGE:
-		val = DIV_ROUND_CLOSEST(data->regs[reg],
+		/* signed register */
+		val = DIV_ROUND_CLOSEST((s16)data->regs[reg],
 					data->config->shunt_div);
 		break;
 	case INA2XX_BUS_VOLTAGE:
@@ -160,8 +161,8 @@ static int ina2xx_get_value(struct ina2xx_data *data, u8 reg)
 		val = data->regs[reg] * data->config->power_lsb;
 		break;
 	case INA2XX_CURRENT:
-		/* LSB=1mA (selected). Is in mA */
-		val = data->regs[reg];
+		/* signed register, LSB=1mA (selected), in mA */
+		val = (s16)data->regs[reg];
 		break;
 	default:
 		/* programmer goofed */
Signature · 7 lines
1.8.4


_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
maintainer Applied
#1 lore raw
Quoted 10 lines · Fabio Baltieri

All devices supported by ina2xx are bidirectional and reports the measured shunt voltage and power values as a signed 16 bit, but the current driver implementation caches all registers as u16, leading to an incorrect sign extension when reporting to the userspace in ina2xx_get_value().

This patch fixes the problem by casting the signed registers to s16. Tested on an INA219.

Applied.

Thanks,
Guenter

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors