power: supply: cpcap-battery: Implement capacity reporting
authorArthur Demchenkov <spinal.by@gmail.com>
Sun, 10 Jan 2021 19:54:01 +0000 (21:54 +0200)
committerSebastian Reichel <sre@kernel.org>
Thu, 14 Jan 2021 22:42:42 +0000 (23:42 +0100)
Calculate percentage using charge_full value provided.

Cc: Arthur Demchenkov <spinal.by@gmail.com>
Cc: Carl Philipp Klemm <philipp@uvos.xyz>
Cc: Merlijn Wajer <merlijn@wizzup.org>
Cc: Pavel Machek <pavel@ucw.cz>
Signed-off-by: Arthur Demchenkov <spinal.by@gmail.com>
[tony@atomide.com: updated to apply after dropping my earlier patch]
Signed-off-by: Tony Lindgren <tony@atomide.com>
Signed-off-by: Sebastian Reichel <sebastian.reichel@collabora.com>
drivers/power/supply/cpcap-battery.c

index bd89e50..bb27016 100644 (file)
@@ -546,6 +546,7 @@ static enum power_supply_property cpcap_battery_props[] = {
        POWER_SUPPLY_PROP_CHARGE_COUNTER,
        POWER_SUPPLY_PROP_POWER_NOW,
        POWER_SUPPLY_PROP_POWER_AVG,
+       POWER_SUPPLY_PROP_CAPACITY,
        POWER_SUPPLY_PROP_CAPACITY_LEVEL,
        POWER_SUPPLY_PROP_SCOPE,
        POWER_SUPPLY_PROP_TEMP,
@@ -556,7 +557,7 @@ static int cpcap_battery_get_property(struct power_supply *psy,
                                      union power_supply_propval *val)
 {
        struct cpcap_battery_ddata *ddata = power_supply_get_drvdata(psy);
-       struct cpcap_battery_state_data *latest, *previous;
+       struct cpcap_battery_state_data *latest, *previous, *empty;
        u32 sample;
        s32 accumulator;
        int cached;
@@ -636,6 +637,16 @@ static int cpcap_battery_get_property(struct power_supply *psy,
                tmp *= ((latest->voltage + previous->voltage) / 20000);
                val->intval = div64_s64(tmp, 100);
                break;
+       case POWER_SUPPLY_PROP_CAPACITY:
+               empty = cpcap_battery_get_empty(ddata);
+               if (!empty->voltage || !ddata->charge_full)
+                       return -ENODATA;
+               /* (ddata->charge_full / 200) is needed for rounding */
+               val->intval = empty->counter_uah - latest->counter_uah +
+                       ddata->charge_full / 200;
+               val->intval = clamp(val->intval, 0, ddata->charge_full);
+               val->intval = val->intval * 100 / ddata->charge_full;
+               break;
        case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
                if (cpcap_battery_full(ddata))
                        val->intval = POWER_SUPPLY_CAPACITY_LEVEL_FULL;