Skip to content

Commit f8b6c1e

Browse files
tititiou36sre
authored andcommitted
power: supply: ingenic: Fix some error handling paths in ingenic_battery_get_property()
If iio_read_channel_processed() fails, 'val->intval' is not updated, but it is still *1000 just after. So, in case of error, the *1000 accumulate and 'val->intval' becomes erroneous. So instead of rescaling the value after the fact, use the dedicated scaling API. This way the result is updated only when needed. In case of error, the previous value is kept, unmodified. This should also reduce any inaccuracies resulting from the scaling. Finally, this is also slightly more efficient as it saves a function call and a multiplication. Fixes: fb24ccf ("power: supply: add Ingenic JZ47xx battery driver.") Signed-off-by: Christophe JAILLET <[email protected]> Acked-by: Artur Rojek <[email protected]> Link: https://lore.kernel.org/r/51e49c18574003db1e20c9299061a5ecd1661a3c.1719121781.git.christophe.jaillet@wanadoo.fr Signed-off-by: Sebastian Reichel <[email protected]>
1 parent f62b267 commit f8b6c1e

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

drivers/power/supply/ingenic-battery.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ static int ingenic_battery_get_property(struct power_supply *psy,
3131

3232
switch (psp) {
3333
case POWER_SUPPLY_PROP_HEALTH:
34-
ret = iio_read_channel_processed(bat->channel, &val->intval);
35-
val->intval *= 1000;
34+
ret = iio_read_channel_processed_scale(bat->channel,
35+
&val->intval,
36+
1000);
3637
if (val->intval < info->voltage_min_design_uv)
3738
val->intval = POWER_SUPPLY_HEALTH_DEAD;
3839
else if (val->intval > info->voltage_max_design_uv)
@@ -41,8 +42,9 @@ static int ingenic_battery_get_property(struct power_supply *psy,
4142
val->intval = POWER_SUPPLY_HEALTH_GOOD;
4243
return ret;
4344
case POWER_SUPPLY_PROP_VOLTAGE_NOW:
44-
ret = iio_read_channel_processed(bat->channel, &val->intval);
45-
val->intval *= 1000;
45+
ret = iio_read_channel_processed_scale(bat->channel,
46+
&val->intval,
47+
1000);
4648
return ret;
4749
case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
4850
val->intval = info->voltage_min_design_uv;

0 commit comments

Comments
 (0)