Skip to content

Commit

Permalink
hwmon: (ina2xx) use DIV_ROUND_CLOSEST() to avoid rounding errors
Browse files Browse the repository at this point in the history
Use DIV_ROUND_CLOSEST() when dealing with the calibration values to make the
calculations less error prone.

Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
  • Loading branch information
Bartosz Golaszewski authored and Guenter Roeck committed Jan 26, 2015
1 parent 71eb7c4 commit b721fe2
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions drivers/hwmon/ina2xx.c
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,11 @@ static void ina226_set_update_interval(struct ina2xx_data *data)

static int ina2xx_calibrate(struct ina2xx_data *data)
{
return i2c_smbus_write_word_swapped(data->client, INA2XX_CALIBRATION,
data->config->calibration_factor / data->rshunt);
u16 val = DIV_ROUND_CLOSEST(data->config->calibration_factor,
data->rshunt);

return i2c_smbus_write_word_swapped(data->client,
INA2XX_CALIBRATION, val);
}

/*
Expand Down Expand Up @@ -307,7 +310,8 @@ static int ina2xx_get_value(struct ina2xx_data *data, u8 reg)
val = (s16)data->regs[reg];
break;
case INA2XX_CALIBRATION:
val = data->config->calibration_factor / data->regs[reg];
val = DIV_ROUND_CLOSEST(data->config->calibration_factor,
data->regs[reg]);
break;
default:
/* programmer goofed */
Expand Down

0 comments on commit b721fe2

Please sign in to comment.