* [PATCH] power: supply: max17042_battery: infer the status when no supplier is described
@ 2026-10-19 14:33 Derek Ross
2026-10-19 14:37 ` Hans de Goede
0 siblings, 1 reply; 2+ messages in thread
From: Derek Ross @ 2026-10-19 14:33 UTC (permalink / raw)
To: Sebastian Reichel
Cc: Hans de Goede, Krzysztof Kozlowski, Marek Szyprowski,
Sebastian Krzyszkowiak, kernel, linux-pm, linux-kernel,
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.
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.
Signed-off-by: Derek Ross <derekross@gmail.com>
---
drivers/power/supply/max17042_battery.c | 21 ++++++++++++++++-----
1 file changed, 16 insertions(+), 5 deletions(-)
diff --git a/drivers/power/supply/max17042_battery.c b/drivers/power/supply/max17042_battery.c
index 8138b52e05e1..c437d80700a6 100644
--- a/drivers/power/supply/max17042_battery.c
+++ b/drivers/power/supply/max17042_battery.c
@@ -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.56.0
^ permalink raw reply related [flat|nested] 2+ messages in thread* Re: [PATCH] power: supply: max17042_battery: infer the status when no supplier is described
2026-10-19 14:33 [PATCH] power: supply: max17042_battery: infer the status when no supplier is described Derek Ross
@ 2026-10-19 14:37 ` Hans de Goede
0 siblings, 0 replies; 2+ messages in thread
From: Hans de Goede @ 2026-10-19 14:37 UTC (permalink / raw)
To: Derek Ross, Sebastian Reichel
Cc: Krzysztof Kozlowski, Marek Szyprowski, Sebastian Krzyszkowiak,
kernel, linux-pm, linux-kernel
Hi,
On 19-Oct-26 4:33 PM, Derek Ross wrote:
> 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.
>
> Signed-off-by: Derek Ross <derekross@gmail.com>
Thanks, patch looks good to me:
Reviewed-by: Hans de Goede <johannes.goede@oss.qualcomm.com>
Regards,
Hans
> ---
> drivers/power/supply/max17042_battery.c | 21 ++++++++++++++++-----
> 1 file changed, 16 insertions(+), 5 deletions(-)
>
> diff --git a/drivers/power/supply/max17042_battery.c b/drivers/power/supply/max17042_battery.c
> index 8138b52e05e1..c437d80700a6 100644
> --- a/drivers/power/supply/max17042_battery.c
> +++ b/drivers/power/supply/max17042_battery.c
> @@ -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;
>
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-10-19 14:37 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-10-19 14:33 [PATCH] power: supply: max17042_battery: infer the status when no supplier is described Derek Ross
2026-10-19 14:37 ` Hans de Goede
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.