Skip to content

Commit

Permalink
hwmon: (ina2xx) Cast to s16 on shunt and current regs
Browse files Browse the repository at this point in the history
All devices supported by ina2xx are bidirectional and report 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 userspace in
ina2xx_get_value().

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

Signed-off-by: Fabio Baltieri <[email protected]>
Cc: [email protected] # 3.10+
Signed-off-by: Guenter Roeck <[email protected]>
  • Loading branch information
fabiobaltieri authored and groeck committed Jun 12, 2014
1 parent 5c02c39 commit c0214f9
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions drivers/hwmon/ina2xx.c
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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 */
Expand Down

0 comments on commit c0214f9

Please sign in to comment.