Skip to content

Commit aecd127

Browse files
IMbackKsre
authored andcommitted
power: supply: cpcap-battery: invalidate config when incompatible measurements are read
This invalidates empty->counter_uah and charge_full when charge_now indicates that they are grossly wrong and adds some tolerance to POWER_SUPPLY_PROP_CHARGE_FULL to allow for inaccuracies in the charge counter and manufacturing tolerances in the battery. Signed-off-by: Carl Philipp Klemm <[email protected]> Signed-off-by: Sebastian Reichel <[email protected]>
1 parent 6a0fcc8 commit aecd127

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

drivers/power/supply/cpcap-battery.c

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -667,10 +667,23 @@ static int cpcap_battery_get_property(struct power_supply *psy,
667667
if (!empty->voltage)
668668
return -ENODATA;
669669
val->intval = empty->counter_uah - latest->counter_uah;
670-
if (val->intval < 0)
670+
if (val->intval < 0) {
671+
/* Assume invalid config if CHARGE_NOW is -20% */
672+
if (ddata->charge_full && abs(val->intval) > ddata->charge_full/5) {
673+
empty->voltage = 0;
674+
ddata->charge_full = 0;
675+
return -ENODATA;
676+
}
671677
val->intval = 0;
672-
else if (ddata->charge_full && ddata->charge_full < val->intval)
678+
} else if (ddata->charge_full && ddata->charge_full < val->intval) {
679+
/* Assume invalid config if CHARGE_NOW exceeds CHARGE_FULL by 20% */
680+
if (val->intval > (6*ddata->charge_full)/5) {
681+
empty->voltage = 0;
682+
ddata->charge_full = 0;
683+
return -ENODATA;
684+
}
673685
val->intval = ddata->charge_full;
686+
}
674687
break;
675688
case POWER_SUPPLY_PROP_CHARGE_FULL:
676689
if (!ddata->charge_full)
@@ -747,7 +760,7 @@ static int cpcap_battery_set_property(struct power_supply *psy,
747760
case POWER_SUPPLY_PROP_CHARGE_FULL:
748761
if (val->intval < 0)
749762
return -EINVAL;
750-
if (val->intval > ddata->config.info.charge_full_design)
763+
if (val->intval > (6*ddata->config.info.charge_full_design)/5)
751764
return -EINVAL;
752765

753766
ddata->charge_full = val->intval;

0 commit comments

Comments
 (0)