Skip to contentSkip to conversation

power: supply: max17042_battery: infer the status when no supplier is described

PATCH Applied Derek Ross 3 participants 3 messages Oct 19 – Nov 9

Subsystems (2)
lore
Share & tools
lore

Derek Ross

author
#0 lore raw

PATCH 1/1 power: supply: max17042_battery: infer the status when no supplier is described

When no power supply is described as supplying the battery, power_supply_am_i_supplied() returns -ENODEV and the driver reports POWER_SUPPLY_STATUS_UNKNOWN, even though the gauge measures the battery current itself.

That is the case on the Pixel 7 Pro, where the charger is left running by the bootloader and no charger driver is bound yet. The battery read "Unknown" while taking in 1.2 A, so the desktop showed no charging indicator and upower treated the phone as discharging: at its critical percentage it put a charging phone to sleep.

When no supplier is described and current sense is enabled, take the status from the measured current: charging while current flows into the battery, full when the gauge reports end of charge and no current flows, discharging otherwise. A described supplier keeps the existing logic, and without current sense the status stays unknown.

---
 drivers/power/supply/max17042_battery.c | 21 ++++++++++++++++-----
 1 file changed, 16 insertions(+), 5 deletions(-)
drivers/power/supply/max17042_battery.c +16 -5
index 8138b52e05e1..c437d80700a6 100644
@@ -148,18 +148,26 @@ static int max17042_get_status(struct max17042_chip *chip, int *status)
 {
 	int ret, charge_full, charge_now;
 	int current_now;
+	bool supplied, full;
 	u32 data;
 
 	ret = power_supply_am_i_supplied(chip->battery);
-	if (ret < 0) {
-		*status = POWER_SUPPLY_STATUS_UNKNOWN;
-		return 0;
-	}
 	if (ret == 0) {
 		*status = POWER_SUPPLY_STATUS_DISCHARGING;
 		return 0;
 	}
 
+	/*
+	 * A negative value means no supplier is described, e.g. a charger the
+	 * bootloader left running that no driver owns. The gauge's own current
+	 * sense still tells which way charge flows, so use it when it's there.
+	 */
+	supplied = ret > 0;
+	if (!supplied && !chip->enable_current_sense) {
+		*status = POWER_SUPPLY_STATUS_UNKNOWN;
+		return 0;
+	}
+
 	/*
 	 * The MAX170xx has builtin end-of-charge detection and will update
 	 * FullCAP to match RepCap when it detects end of charging.
@@ -178,7 +186,8 @@ static int max17042_get_status(struct max17042_chip *chip, int *status)
 	if (ret < 0)
 		return ret;
 
-	if ((charge_full - charge_now) <= MAX17042_FULL_THRESHOLD) {
+	full = (charge_full - charge_now) <= MAX17042_FULL_THRESHOLD;
+	if (supplied && full) {
 		*status = POWER_SUPPLY_STATUS_FULL;
 		return 0;
 	}
@@ -200,6 +209,8 @@ static int max17042_get_status(struct max17042_chip *chip, int *status)
 
 	if (current_now > 0)
 		*status = POWER_SUPPLY_STATUS_CHARGING;
+	else if (full && current_now == 0)
+		*status = POWER_SUPPLY_STATUS_FULL;
 	else
 		*status = POWER_SUPPLY_STATUS_DISCHARGING;
 
Signature · 1 line
2.56.0
reviewer Reviewed-by on v1 1/1
#1 lore raw

Hi,

Quoted 11 lines · Derek Ross

When no power supply is described as supplying the battery, power_supply_am_i_supplied() returns -ENODEV and the driver reports POWER_SUPPLY_STATUS_UNKNOWN, even though the gauge measures the battery current itself.

That is the case on the Pixel 7 Pro, where the charger is left running by the bootloader and no charger driver is bound yet. The battery read "Unknown" while taking in 1.2 A, so the desktop showed no charging indicator and upower treated the phone as discharging: at its critical percentage it put a charging phone to sleep.

Quoted 7 lines

When no supplier is described and current sense is enabled, take the status from the measured current: charging while current flows into the battery, full when the gauge reports end of charge and no current flows, discharging otherwise. A described supplier keeps the existing logic, and without current sense the status stays unknown.

Thanks, patch looks good to me:

Regards,

Hans

Quoted 58 lines
---
 drivers/power/supply/max17042_battery.c | 21 ++++++++++++++++-----
 1 file changed, 16 insertions(+), 5 deletions(-)
drivers/power/supply/max17042_battery.c +16 -5
index 8138b52e05e1..c437d80700a6 100644
@@ -148,18 +148,26 @@ static int max17042_get_status(struct max17042_chip *chip, int *status)
 {
 	int ret, charge_full, charge_now;
 	int current_now;
+	bool supplied, full;
 	u32 data;
 
 	ret = power_supply_am_i_supplied(chip->battery);
-	if (ret < 0) {
-		*status = POWER_SUPPLY_STATUS_UNKNOWN;
-		return 0;
-	}
 	if (ret == 0) {
 		*status = POWER_SUPPLY_STATUS_DISCHARGING;
 		return 0;
 	}
 
+	/*
+	 * A negative value means no supplier is described, e.g. a charger the
+	 * bootloader left running that no driver owns. The gauge's own current
+	 * sense still tells which way charge flows, so use it when it's there.
+	 */
+	supplied = ret > 0;
+	if (!supplied && !chip->enable_current_sense) {
+		*status = POWER_SUPPLY_STATUS_UNKNOWN;
+		return 0;
+	}
+
 	/*
 	 * The MAX170xx has builtin end-of-charge detection and will update
 	 * FullCAP to match RepCap when it detects end of charging.
@@ -178,7 +186,8 @@ static int max17042_get_status(struct max17042_chip *chip, int *status)
 	if (ret < 0)
 		return ret;
 
-	if ((charge_full - charge_now) <= MAX17042_FULL_THRESHOLD) {
+	full = (charge_full - charge_now) <= MAX17042_FULL_THRESHOLD;
+	if (supplied && full) {
 		*status = POWER_SUPPLY_STATUS_FULL;
 		return 0;
 	}
@@ -200,6 +209,8 @@ static int max17042_get_status(struct max17042_chip *chip, int *status)
 
 	if (current_now > 0)
 		*status = POWER_SUPPLY_STATUS_CHARGING;
+	else if (full && current_now == 0)
+		*status = POWER_SUPPLY_STATUS_FULL;
 	else
 		*status = POWER_SUPPLY_STATUS_DISCHARGING;
 
#2 lore raw
In reply to Derek Ross
Quoted 5 lines · Derek Ross

When no power supply is described as supplying the battery, power_supply_am_i_supplied() returns -ENODEV and the driver reports POWER_SUPPLY_STATUS_UNKNOWN, even though the gauge measures the battery current itself.

Quoted 7 lines

That is the case on the Pixel 7 Pro, where the charger is left running by the bootloader and no charger driver is bound yet. The battery read "Unknown" while taking in 1.2 A, so the desktop showed no charging indicator and upower treated the phone as discharging: at its critical percentage it put a charging phone to sleep.

[...]

Applied, thanks!

[1/1] power: supply: max17042_battery: infer the status when no supplier is described

      commit: c9797f02962f784d5920eacc4d6cad1369a8c20a

Best regards,

Signature · 1 line
Sebastian Reichel <[email protected]>