mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 0/3] power: supply: bq27xxx: fix wrong battery states after resume
@ 2026-11-21 20:41 Michael Auer
  2026-11-21 20:41 ` [PATCH 1/3] power: supply: bq27xxx: report discharging at zero current without a charger Michael Auer
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Michael Auer @ 2026-11-21 20:41 UTC (permalink / raw)
  To: Sebastian Reichel; +Cc: Pali Rohár, linux-pm, linux-kernel, Michael Auer

On a OnePlus 6 (bq27541 on i2c-qcom-geni) user space sometimes sees a
wrong battery state right after resume.

Patch 1 fixes "Not charging" while on battery, when the current reads 0
after deep sleep. Patches 2 and 3 fix a battery that is reported as
missing at 0 % after a failed read, which made the desktop start its
critical battery action. It happened in about 1 of 350 resumes. Patch 2
covers a single missed transfer, patch 3 a failure that lasts longer.

All three are in daily use on a OnePlus 6 with an sdm845-mainline
7.1-rc1 based kernel, patch 1 since late September, patches 2 and 3
since early October. On power-supply for-next they were only build
tested (W=1, arm64 and x86_64 allmodconfig).

Tool use: the analysis, the code and the commit messages were
written with an LLM coding assistant (Claude Code). I reviewed all
of it and tested it on my phone as described above.

Michael Auer (3):
  power: supply: bq27xxx: report discharging at zero current without a
    charger
  power: supply: bq27xxx: retry a failed register read once
  power: supply: bq27xxx: keep the last values over a short read failure

 drivers/power/supply/bq27xxx_battery.c | 60 ++++++++++++++++++++++++++
 include/linux/power/bq27xxx_battery.h  |  2 +
 2 files changed, 62 insertions(+)


base-commit: abbe0a96bdbadae9e29489a1528fa436c55aca25
-- 
2.43.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 1/3] power: supply: bq27xxx: report discharging at zero current without a charger
  2026-11-21 20:41 [PATCH 0/3] power: supply: bq27xxx: fix wrong battery states after resume Michael Auer
@ 2026-11-21 20:41 ` 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 ` [PATCH 3/3] power: supply: bq27xxx: keep the last values over a short read failure Michael Auer
  2 siblings, 0 replies; 4+ messages in thread
From: Michael Auer @ 2026-11-21 20:41 UTC (permalink / raw)
  To: Sebastian Reichel; +Cc: Pali Rohár, linux-pm, linux-kernel, Michael Auer

The status is derived from the sign of the current, so a current of 0
is reported as "Not charging". On a OnePlus 6 (bq27541) the first
current reading after a resume from deep sleep is 0. User space then
sees "Not charging" while on battery: UPower no longer reports the
device as on battery, and the desktop plays its "power adapter plugged
in" sound after such resumes, until the next reading shows a negative
current again.

"Not charging" means a charger is present but not charging, so report
"Discharging" for a zero current when no power supply is online. Do it
only for the direct property read: the cached update runs under
di->lock, and power_supply_is_system_supplied() also reads properties
of this battery.

Signed-off-by: Michael Auer <mick@yoxcu.de>
---
 drivers/power/supply/bq27xxx_battery.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/drivers/power/supply/bq27xxx_battery.c b/drivers/power/supply/bq27xxx_battery.c
index 747a38d3390d..032eea030f2f 100644
--- a/drivers/power/supply/bq27xxx_battery.c
+++ b/drivers/power/supply/bq27xxx_battery.c
@@ -1942,6 +1942,14 @@ static int bq27xxx_battery_current_and_status(
 			val_status->intval = POWER_SUPPLY_STATUS_CHARGING;
 		else if (curr < 0)
 			val_status->intval = POWER_SUPPLY_STATUS_DISCHARGING;
+		else if (!cache && power_supply_is_system_supplied() == 0)
+			/*
+			 * No charger online: |current| is below the resolution (e.g. after
+			 * deep sleep). Only for the direct property read: the cached update
+			 * runs under di->lock, and power_supply_is_system_supplied() calls
+			 * get_property() on every supply, this battery included.
+			 */
+			val_status->intval = POWER_SUPPLY_STATUS_DISCHARGING;
 		else
 			val_status->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
 	}
-- 
2.43.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 2/3] power: supply: bq27xxx: retry a failed register read once
  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 ` Michael Auer
  2026-11-21 20:41 ` [PATCH 3/3] power: supply: bq27xxx: keep the last values over a short read failure Michael Auer
  2 siblings, 0 replies; 4+ messages in thread
From: Michael Auer @ 2026-11-21 20:41 UTC (permalink / raw)
  To: Sebastian Reichel; +Cc: Pali Rohár, linux-pm, linux-kernel, Michael Auer

A bq27541 on a OnePlus 6 now and then NAKs an I2C read right after
system resume (-ENXIO on i2c-qcom-geni). If that is the flags read of
the update queued at resume, the driver caches the error: for the next
5 s "present" reads 0 and every other property fails. UPower then
reports the battery as missing at 0 %, and the desktop starts its
critical battery action on a battery that is at 25 %:

  power_supply bq27541-0: driver failed to report `status' property: -6

The I2C read already retries -EBUSY. Retry any other failed read once
after 10 ms, in the bus independent read helper, but not -ENODEV (no
I2C adapter). A gauge that does not answer at all now costs 10 ms more
per read.

Signed-off-by: Michael Auer <mick@yoxcu.de>
---
I did not put the retry into the I2C read next to the -EBUSY retry,
because the error code for a NAK differs between adapters (-ENXIO,
-EREMOTEIO, -EIO).

 drivers/power/supply/bq27xxx_battery.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/drivers/power/supply/bq27xxx_battery.c b/drivers/power/supply/bq27xxx_battery.c
index 032eea030f2f..dccb0f84ea1d 100644
--- a/drivers/power/supply/bq27xxx_battery.c
+++ b/drivers/power/supply/bq27xxx_battery.c
@@ -1141,6 +1141,17 @@ static inline int bq27xxx_read(struct bq27xxx_device_info *di, int reg_index,
 		return -EINVAL;
 
 	ret = di->bus.read(di, di->regs[reg_index], single);
+	if (ret < 0 && ret != -ENODEV) {
+		/*
+		 * The gauge can miss a single transfer, e.g. right after system
+		 * resume (bq27541 NAKs it). Without the retry the whole update
+		 * fails and userspace sees the battery as absent.
+		 */
+		dev_dbg(di->dev, "failed to read register 0x%02x (index %d): %d, retrying\n",
+			di->regs[reg_index], reg_index, ret);
+		usleep_range(10000, 11000);
+		ret = di->bus.read(di, di->regs[reg_index], single);
+	}
 	if (ret < 0)
 		dev_dbg(di->dev, "failed to read register 0x%02x (index %d)\n",
 			di->regs[reg_index], reg_index);
-- 
2.43.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 3/3] power: supply: bq27xxx: keep the last values over a short read failure
  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
  2 siblings, 0 replies; 4+ messages in thread
From: Michael Auer @ 2026-11-21 20:41 UTC (permalink / raw)
  To: Sebastian Reichel; +Cc: Pali Rohár, linux-pm, linux-kernel, Michael Auer

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


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-11-21 20:41 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 ` [PATCH 3/3] power: supply: bq27xxx: keep the last values over a short read failure Michael Auer

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®