Skip to contentSkip to conversation

power: supply: bq27xxx: fix wrong battery states after resume

PATCH In review Michael Auer 2 participants 7 messages Nov 21 – Nov 22

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover power: supply: bq27xxx: fix wrong battery states after resume

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

Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/3 power: supply: bq27xxx: report discharging at zero current without a charger

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.

---
 drivers/power/supply/bq27xxx_battery.c | 8 ++++++++
 1 file changed, 8 insertions(+)
drivers/power/supply/bq27xxx_battery.c +8 -0
index 747a38d3390d..032eea030f2f 100644
@@ -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;
 	}
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH 1/3] power: supply: bq27xxx: report discharging at zero current without a charger

Hi,

A sashiko review of this series (not mailed to the list) found a problem in this patch that I can confirm:
power_supply_is_system_supplied() also counts this battery, so on a board whose charger is not registered in Linux it returns 0. A full battery on such a charger at zero current would then report Discharging instead of Not charging, the case that commit c3a6d6a1dfc8 ("power: supply: bq27xxx: make status more robust") fixed.

For v2 I plan to use power_supply_am_i_supplied(di->bat) instead. It returns -ENODEV when no supplier is linked, so those boards keep Not charging, and it does not read this battery, so the check can also run in the cached update. On the OnePlus 6 this needs the gauge linked to the charger in the DT (power-supplies), which I would send as a separate patch.

Patch 3/3 needs a fix too: its status fallback returns the status of the cached update, which has the same zero current problem, and which is never set on BQ27XXX_O_ZERO gauges.

If you prefer another way, please tell me before v2.

Michael

#3 lore raw

PATCH 2/3 power: supply: bq27xxx: retry a failed register read once

In reply to 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.

---
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(+)
drivers/power/supply/bq27xxx_battery.c +11 -0
index 032eea030f2f..dccb0f84ea1d 100644
@@ -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);
Signature · 1 line
2.43.0
reviewer
#4 lore raw

Re: [PATCH 2/3] power: supply: bq27xxx: retry a failed register read once

Quoted 4 lines · On Saturday 21 November 2026 21:41:36 Michael Auer wrote:

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.

So... should not be there rather a resume hook in the driver which waits until the battery is ready (instead of adding retires on many places in the driver)?

Quoted 45 lines

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.

---
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,

(quoted diff) +0 -1
-EREMOTEIO, -EIO).
 
 drivers/power/supply/bq27xxx_battery.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
drivers/power/supply/bq27xxx_battery.c +11 -0
index 032eea030f2f..dccb0f84ea1d 100644
@@ -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);
Signature · 1 line
2.43.0
#5 lore raw

Re: [PATCH 2/3] power: supply: bq27xxx: retry a failed register read once

Hi Pali,

Quoted 3 lines · Pali Rohár

So... should not be there rather a resume hook in the driver which waits until the battery is ready (instead of adding retires on many places in the driver)?

Thanks for looking at it.

The read that fails is the first one after resume: the flags read of the update that bq27xxx_battery_resume() queues without a delay. The OnePlus vendor driver waits 1 s after resume before it reads the gauge, so a delay there would probably avoid it too.

What I do not know is how long the gauge needs. It failed in about 1 of 350 resumes, and the NAK is the only sign, so I could not measure it. A fixed delay would be a guess, the retry does not depend on one. The retry is in one place, bq27xxx_read(), which all register reads go through.

For v2 I can either delay the update queued at resume (for example by 1 s) instead of the retry, or do both. Which do you prefer? I will also log on the phone when the retry fires, which should show how long the gauge is not ready after resume.

Michael

#6 lore raw

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

In reply to 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.

---
 drivers/power/supply/bq27xxx_battery.c | 41 ++++++++++++++++++++++++++
 include/linux/power/bq27xxx_battery.h  |  2 ++
 2 files changed, 43 insertions(+)
drivers/power/supply/bq27xxx_battery.c +41 -0
index dccb0f84ea1d..6a0a916ec05c 100644
@@ -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)
include/linux/power/bq27xxx_battery.h +2 -0
index 731ea837aac3..9c2a55f40f76 100644
@@ -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;
Signature · 1 line
2.43.0