[PATCH] power: supply: bq27xxx: Recheck the status after an external power change
From: Alexey Charkov
Date: Mon Nov 16 2026 - 05:42:02 EST
The status of the signed-current gauges follows the sign of the current
they measure, and that only flips once the gauge's next measurement picks
up the charger change. A BQ28Z620 behind a BQ25798 charger shows positive
current up to 0.8s after the charger reports charging, and negative current
about 1.1s after its input goes away. The single check 0.5s after the
change often sees the old sign, raises no uevent, and leaves the status to
the next poll, by default 360s later, so userspace that only watches the
battery (upower, for one) keeps showing the old status for minutes.
A property read in that window loses the check outright: its refresh
pushes the pending work out to poll_interval. upowerd reads the battery
right after the charger's uevent, so on unplug the check never ran at all.
Keep the first check 0.5s after the change, but repeat it up to five
times, 0.5s apart, until the status changes. Count a refresh from a
property read as one of those checks instead of letting it postpone the
rest.
Stop at the first status change rather than running every check. Each
uevent makes userspace reread all battery properties, and the window right
after a replug is when a USB PD port controller on the same bus needs it
most. A status that only lasts a moment then holds until the next refresh,
the price of keeping the bus quiet.
Leave the gauges without a signed current (BQ27XXX_O_ZERO) with the single
check. Their status follows a flag rather than the current, so it never
counts as changed here and they would always use up all five checks.
Signed-off-by: Alexey Charkov <alchark@xxxxxxxxxxx>
---
drivers/power/supply/bq27xxx_battery.c | 39 +++++++++++++++++++++++++++++-----
include/linux/power/bq27xxx_battery.h | 1 +
2 files changed, 35 insertions(+), 5 deletions(-)
diff --git a/drivers/power/supply/bq27xxx_battery.c b/drivers/power/supply/bq27xxx_battery.c
index a955feb6cdfb..e4ea92e78a61 100644
--- a/drivers/power/supply/bq27xxx_battery.c
+++ b/drivers/power/supply/bq27xxx_battery.c
@@ -1124,6 +1124,10 @@ module_param_cb(poll_interval, ¶m_ops_poll_interval, &poll_interval, 0644);
MODULE_PARM_DESC(poll_interval,
"battery poll interval in seconds - 0 disables polling");
+/* Status checks after an external power change, and the delay between them */
+#define BQ27XXX_STATUS_CHECKS 5
+#define BQ27XXX_STATUS_CHECK_DELAY (HZ / 2)
+
/*
* Common code for BQ27xxx devices
*/
@@ -1898,6 +1902,7 @@ static void bq27xxx_battery_update_unlocked(struct bq27xxx_device_info *di)
union power_supply_propval status = di->last_status;
struct bq27xxx_reg_cache cache = {0, };
bool has_singe_flag = di->opts & BQ27XXX_O_ZERO;
+ bool status_changed;
cache.flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, has_singe_flag);
if (di->chip == BQ27000 && (cache.flags & 0xff) == 0xff)
@@ -1913,9 +1918,10 @@ static void bq27xxx_battery_update_unlocked(struct bq27xxx_device_info *di)
bq27xxx_battery_current_and_status(di, NULL, &status, &cache);
}
+ status_changed = di->last_status.intval != status.intval;
+
if ((di->cache.capacity != cache.capacity) ||
- (di->cache.flags != cache.flags) ||
- (di->last_status.intval != status.intval)) {
+ (di->cache.flags != cache.flags) || status_changed) {
di->last_status.intval = status.intval;
power_supply_changed(di->bat);
}
@@ -1925,7 +1931,22 @@ static void bq27xxx_battery_update_unlocked(struct bq27xxx_device_info *di)
di->last_update = jiffies;
- if (!di->removed && poll_interval > 0)
+ if (di->removed)
+ return;
+
+ /*
+ * Once the status reflects an external power change, stop checking for
+ * it; otherwise use up the remaining checks before going back to polling.
+ */
+ if (status_changed)
+ di->status_checks = 0;
+ else if (di->status_checks)
+ di->status_checks--;
+
+ if (di->status_checks)
+ mod_delayed_work(system_percpu_wq, &di->work,
+ BQ27XXX_STATUS_CHECK_DELAY);
+ else if (poll_interval > 0)
mod_delayed_work(system_percpu_wq, &di->work, poll_interval * HZ);
}
@@ -2201,8 +2222,16 @@ static void bq27xxx_external_power_changed(struct power_supply *psy)
{
struct bq27xxx_device_info *di = power_supply_get_drvdata(psy);
- /* After charger plug in/out wait 0.5s for things to stabilize */
- mod_delayed_work(system_percpu_wq, &di->work, HZ / 2);
+ /*
+ * After charger plug in/out the current takes a varying time to settle,
+ * so check the status a few times rather than once on gauges that derive
+ * it from the sign of the current.
+ */
+ mutex_lock(&di->lock);
+ if (!(di->opts & BQ27XXX_O_ZERO))
+ di->status_checks = BQ27XXX_STATUS_CHECKS;
+ mod_delayed_work(system_percpu_wq, &di->work, BQ27XXX_STATUS_CHECK_DELAY);
+ mutex_unlock(&di->lock);
}
int bq27xxx_battery_setup(struct bq27xxx_device_info *di)
diff --git a/include/linux/power/bq27xxx_battery.h b/include/linux/power/bq27xxx_battery.h
index 731ea837aac3..dad3119454c6 100644
--- a/include/linux/power/bq27xxx_battery.h
+++ b/include/linux/power/bq27xxx_battery.h
@@ -68,6 +68,7 @@ struct bq27xxx_device_info {
bool removed;
unsigned long last_update;
union power_supply_propval last_status;
+ unsigned int status_checks;
struct delayed_work work;
struct power_supply *bat;
struct list_head list;
---
base-commit: 84abb955b1015477aa11a96e2f3ac1b938b77f48
change-id: 20261116-bq28z620-status-recheck-66ce9cb05d23
Best regards,
--
Alexey Charkov <alchark@xxxxxxxxxxx>