power: supply: max17042_battery: Improve accuracy of current_now and current_avg...
authorSebastian Krzyszkowiak <sebastian.krzyszkowiak@puri.sm>
Wed, 25 Nov 2020 04:46:17 +0000 (05:46 +0100)
committerSebastian Reichel <sre@kernel.org>
Mon, 30 Nov 2020 00:14:42 +0000 (01:14 +0100)
Dividing 1562500 by r_sns value usually doesn't result in an integer.

Signed-off-by: Sebastian Krzyszkowiak <sebastian.krzyszkowiak@puri.sm>
Signed-off-by: Sebastian Reichel <sebastian.reichel@collabora.com>
drivers/power/supply/max17042_battery.c

index 2e9672f..86adccb 100644 (file)
@@ -395,8 +395,8 @@ static int max17042_get_property(struct power_supply *psy,
                        if (ret < 0)
                                return ret;
 
-                       val->intval = sign_extend32(data, 15);
-                       val->intval *= 1562500 / chip->pdata->r_sns;
+                       data64 = sign_extend64(data, 15) * 1562500ll;
+                       val->intval = div_s64(data64, chip->pdata->r_sns);
                } else {
                        return -EINVAL;
                }
@@ -407,8 +407,8 @@ static int max17042_get_property(struct power_supply *psy,
                        if (ret < 0)
                                return ret;
 
-                       val->intval = sign_extend32(data, 15);
-                       val->intval *= 1562500 / chip->pdata->r_sns;
+                       data64 = sign_extend64(data, 15) * 1562500ll;
+                       val->intval = div_s64(data64, chip->pdata->r_sns);
                } else {
                        return -EINVAL;
                }