mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Michael Auer <mick@yoxcu.de>
To: Sebastian Reichel <sre@kernel.org>
Cc: "Pali Rohár" <pali@kernel.org>,
	linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org,
	"Michael Auer" <mick@yoxcu.de>
Subject: [PATCH 3/3] power: supply: bq27xxx: keep the last values over a short read failure
Date: Sat, 21 Nov 2026 21:41:37 +0100	[thread overview]
Message-ID: <20261121204137.848173-4-mick@yoxcu.de> (raw)
In-Reply-To: <20261121204137.848173-1-mick@yoxcu.de>

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@yoxcu.de>
---
 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


      parent reply	other threads:[~2026-11-21 20:41 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-21 20:41 [PATCH 0/3] power: supply: bq27xxx: fix wrong battery states after resume Michael Auer
2026-11-21 20:41 ` [PATCH 1/3] power: supply: bq27xxx: report discharging at zero current without a charger Michael Auer
2026-11-21 20:41 ` [PATCH 2/3] power: supply: bq27xxx: retry a failed register read once Michael Auer
2026-11-21 20:41 ` Michael Auer [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261121204137.848173-4-mick@yoxcu.de \
    --to=mick@yoxcu.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=pali@kernel.org \
    --cc=sre@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®