[PATCH 3/3] power: supply: bq27xxx: keep the last values over a short read failure

From: Michael Auer

Date: Sat Nov 21 2026 - 15:41:54 EST


When the flags read of an update fails, the driver caches the error:
"present" reads 0 and every other property returns the error until an
update succeeds. Updates come from the poll work (every 360 s by
default), a charger change, or a property read more than 5 s after the
last update. So a gauge that misses one update makes the battery
disappear for at least 5 s, and user space treats it as empty.

If a gauge that answered before fails an update, keep the last capacity
and flags, so "present" stays 1, and re-check every second. Report the
failure as before only once it has lasted 30 s. In that window a
failed "status" read returns the status of the last good update.
-ENODEV (no I2C adapter, or a bq27000 without battery) is reported at
once.

Start with cache.flags = -ENODATA instead of 0, so that a failure of
the very first update is reported and not bridged with an all-zero
cache.

A battery that disappears for good is now reported 30 s later.

Signed-off-by: Michael Auer <mick@xxxxxxxx>
---
drivers/power/supply/bq27xxx_battery.c | 41 ++++++++++++++++++++++++++
include/linux/power/bq27xxx_battery.h | 2 ++
2 files changed, 43 insertions(+)

diff --git a/drivers/power/supply/bq27xxx_battery.c b/drivers/power/supply/bq27xxx_battery.c
index dccb0f84ea1d..6a0a916ec05c 100644
--- a/drivers/power/supply/bq27xxx_battery.c
+++ b/drivers/power/supply/bq27xxx_battery.c
@@ -1128,6 +1128,10 @@ module_param_cb(poll_interval, &param_ops_poll_interval, &poll_interval, 0644);
MODULE_PARM_DESC(poll_interval,
"battery poll interval in seconds - 0 disables polling");

+/* Keep the last values this long when the gauge stops answering, re-check this often */
+#define BQ27XXX_STALE_TIMEOUT (30 * HZ)
+#define BQ27XXX_STALE_RECHECK HZ
+
/*
* Common code for BQ27xxx devices
*/
@@ -1968,6 +1972,35 @@ static int bq27xxx_battery_current_and_status(
return 0;
}

+/*
+ * A gauge can fail a read for a moment, e.g. right after resume. Keep the last
+ * values and re-check soon, and report the error only once it has lasted
+ * BQ27XXX_STALE_TIMEOUT. -ENODEV is reported at once.
+ * Returns true if the failed update was bridged and must not be applied.
+ */
+static bool bq27xxx_battery_bridge_error(struct bq27xxx_device_info *di, int flags)
+{
+ if (flags >= 0 || flags == -ENODEV || di->cache.flags < 0) {
+ di->stale = false;
+ return false;
+ }
+
+ if (!di->stale) {
+ di->stale = true;
+ di->stale_since = jiffies;
+ dev_warn(di->dev, "gauge read failed (%d), keeping the last values\n", flags);
+ } else if (time_after(jiffies, di->stale_since + BQ27XXX_STALE_TIMEOUT)) {
+ di->stale = false;
+ return false;
+ }
+
+ di->last_update = jiffies;
+ if (!di->removed)
+ mod_delayed_work(system_percpu_wq, &di->work, BQ27XXX_STALE_RECHECK);
+
+ return true;
+}
+
static void bq27xxx_battery_update_unlocked(struct bq27xxx_device_info *di)
{
union power_supply_propval status = di->last_status;
@@ -1977,6 +2010,8 @@ static void bq27xxx_battery_update_unlocked(struct bq27xxx_device_info *di)
cache.flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, has_singe_flag);
if (di->chip == BQ27000 && (cache.flags & 0xff) == 0xff)
cache.flags = -ENODEV; /* bq27000 hdq read error */
+ if (bq27xxx_battery_bridge_error(di, cache.flags))
+ return;
if (cache.flags >= 0) {
cache.capacity = bq27xxx_battery_read_soc(di);

@@ -2192,6 +2227,11 @@ static int bq27xxx_battery_get_property(struct power_supply *psy,
switch (psp) {
case POWER_SUPPLY_PROP_STATUS:
ret = bq27xxx_battery_current_and_status(di, NULL, val, NULL);
+ if (ret < 0 && di->stale) {
+ /* Status of the last good update, like the cached capacity */
+ val->intval = di->last_status.intval;
+ ret = 0;
+ }
break;
case POWER_SUPPLY_PROP_VOLTAGE_NOW:
ret = bq27xxx_battery_voltage(di, val);
@@ -2304,6 +2344,7 @@ int bq27xxx_battery_setup(struct bq27xxx_device_info *di)
di->unseal_key = bq27xxx_chip_data[di->chip].unseal_key;
di->dm_regs = bq27xxx_chip_data[di->chip].dm_regs;
di->opts = bq27xxx_chip_data[di->chip].opts;
+ di->cache.flags = -ENODATA; /* nothing read yet: no values to bridge an error with */

psy_desc = devm_kzalloc(di->dev, sizeof(*psy_desc), GFP_KERNEL);
if (!psy_desc)
diff --git a/include/linux/power/bq27xxx_battery.h b/include/linux/power/bq27xxx_battery.h
index 731ea837aac3..9c2a55f40f76 100644
--- a/include/linux/power/bq27xxx_battery.h
+++ b/include/linux/power/bq27xxx_battery.h
@@ -67,6 +67,8 @@ struct bq27xxx_device_info {
int voltage_max_design;
bool removed;
unsigned long last_update;
+ bool stale; /* failed update(s) being bridged with the last values */
+ unsigned long stale_since; /* jiffies of the first of them */
union power_supply_propval last_status;
struct delayed_work work;
struct power_supply *bat;
--
2.43.0