Skip to content

Commit 3f87baa

Browse files
korneldulebasre
authored andcommitted
power: supply: qcom_battmgr: Report battery capacity
Battery charge can be reported in several different ways. One of them is is charge percentage referred to as POWER_SUPPLY_PROP_CAPACITY in the power supply API. Currently the driver reports the capacity in this way on SM8350, but not on the newer variants referred to as SC8280XP in the driver. Although this is not a bug in itself, not reporting the percentage can confuse some userspace consumers. Mimic what is done in the ACPI driver (drivers/acpi/battery.c) and calculate the percentage capacity by dividing the current charge value by the full charge. Signed-off-by: Kornel Dulęba <[email protected]> Reviewed-by: Dmitry Baryshkov <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Sebastian Reichel <[email protected]>
1 parent 6aa1c3a commit 3f87baa

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

drivers/power/supply/qcom_battmgr.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,8 @@ static int qcom_battmgr_bat_get_property(struct power_supply *psy,
577577
val->intval = battmgr->status.capacity;
578578
break;
579579
case POWER_SUPPLY_PROP_CAPACITY:
580+
if (battmgr->status.percent == (unsigned int)-1)
581+
return -ENODATA;
580582
val->intval = battmgr->status.percent;
581583
break;
582584
case POWER_SUPPLY_PROP_TEMP:
@@ -617,6 +619,7 @@ static const enum power_supply_property sc8280xp_bat_props[] = {
617619
POWER_SUPPLY_PROP_STATUS,
618620
POWER_SUPPLY_PROP_PRESENT,
619621
POWER_SUPPLY_PROP_TECHNOLOGY,
622+
POWER_SUPPLY_PROP_CAPACITY,
620623
POWER_SUPPLY_PROP_CYCLE_COUNT,
621624
POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
622625
POWER_SUPPLY_PROP_VOLTAGE_NOW,
@@ -1063,6 +1066,26 @@ static void qcom_battmgr_sc8280xp_callback(struct qcom_battmgr *battmgr,
10631066
battmgr->ac.online = source == BATTMGR_CHARGING_SOURCE_AC;
10641067
battmgr->usb.online = source == BATTMGR_CHARGING_SOURCE_USB;
10651068
battmgr->wireless.online = source == BATTMGR_CHARGING_SOURCE_WIRELESS;
1069+
if (battmgr->info.last_full_capacity != 0) {
1070+
/*
1071+
* 100 * battmgr->status.capacity can overflow a 32bit
1072+
* unsigned integer. FW readings are in m{W/A}h, which
1073+
* are multiplied by 1000 converting them to u{W/A}h,
1074+
* the format the power_supply API expects.
1075+
* To avoid overflow use the original value for dividend
1076+
* and convert the divider back to m{W/A}h, which can be
1077+
* done without any loss of precision.
1078+
*/
1079+
battmgr->status.percent =
1080+
(100 * le32_to_cpu(resp->status.capacity)) /
1081+
(battmgr->info.last_full_capacity / 1000);
1082+
} else {
1083+
/*
1084+
* Let the sysfs handler know no data is available at
1085+
* this time.
1086+
*/
1087+
battmgr->status.percent = (unsigned int)-1;
1088+
}
10661089
break;
10671090
case BATTMGR_BAT_DISCHARGE_TIME:
10681091
battmgr->status.discharge_time = le32_to_cpu(resp->time);

0 commit comments

Comments
 (0)