From: Jerry Lv <Jerry.Lv@axis.com>
To: "Pali Rohár" <pali@kernel.org>, "Sebastian Reichel" <sre@kernel.org>
Cc: <linux-pm@vger.kernel.org>, <linux-kernel@vger.kernel.org>,
<kernel@axis.com>, Jerry Lv <Jerry.Lv@axis.com>
Subject: [PATCH] power: supply: bq27xxx_battery: Retrieve again when busy
Date: Tue, 29 Oct 2024 11:35:00 +0800 [thread overview]
Message-ID: <20241029-foo-fix-v1-1-1dbfed72d023@axis.com> (raw)
Multiple applications may access the battery gauge at the same time, so
the gauge may be busy and EBUSY will be returned. The driver will set a
flag to record the EBUSY state, and this flag will be kept until the next
periodic update. When this flag is set, bq27xxx_battery_get_property()
will just return ENODEV until the flag is updated.
Even if the gauge was busy during the last accessing attempt, returning
ENODEV is not ideal, and can cause confusion in the applications layer.
Instead, retry accessing the gauge to update the flag is as expected, for
the gauge typically recovers from busy state within a few milliseconds.
If still failed to access the gauge, the real error code would be returned
instead of ENODEV (as suggested by Pali Rohár).
Signed-off-by: Jerry Lv <Jerry.Lv@axis.com>
---
When the battery gauge is busy, retry to access 10 miliseconds later,
retry up to 3 times. When failed to access the gauge, return the real
error code.
Differences related to previous versions:
v2 (as suggested by Pali Rohár):
- retry up to 3 times when gauge is busy.
- return the real error code when fail to access the device.
v1:
- initial version for review.
---
drivers/power/supply/bq27xxx_battery.c | 12 ++++++++++--
1 file changed, 10 insertions(+), 2 deletions(-)
diff --git a/drivers/power/supply/bq27xxx_battery.c b/drivers/power/supply/bq27xxx_battery.c
index 750fda543308..9c40bbc292c1 100644
--- a/drivers/power/supply/bq27xxx_battery.c
+++ b/drivers/power/supply/bq27xxx_battery.c
@@ -1871,11 +1871,19 @@ static int bq27xxx_battery_current_and_status(
static void bq27xxx_battery_update_unlocked(struct bq27xxx_device_info *di)
{
+#define MAX_RETRY 3
+ int retry = 0, sleep = 10;
union power_supply_propval status = di->last_status;
struct bq27xxx_reg_cache cache = {0, };
bool has_singe_flag = di->opts & BQ27XXX_O_ZERO;
- cache.flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, has_singe_flag);
+ do {
+ cache.flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, has_singe_flag);
+ if (cache.flags == -EBUSY && retry < MAX_RETRY) {
+ retry++;
+ BQ27XXX_MSLEEP(sleep); /* sleep 10 miliseconds when busy */
+ }
+ } while (cache.flags == -EBUSY && retry < MAX_RETRY);
if ((cache.flags & 0xff) == 0xff)
cache.flags = -1; /* read error */
if (cache.flags >= 0) {
@@ -2030,7 +2038,7 @@ static int bq27xxx_battery_get_property(struct power_supply *psy,
mutex_unlock(&di->lock);
if (psp != POWER_SUPPLY_PROP_PRESENT && di->cache.flags < 0)
- return -ENODEV;
+ return di->cache.flags;
switch (psp) {
case POWER_SUPPLY_PROP_STATUS:
---
base-commit: 42f7652d3eb527d03665b09edac47f85fb600924
change-id: 20241008-foo-fix-b2244cbe6dce
Best regards,
--
Jerry Lv <Jerry.Lv@axis.com>
next reply other threads:[~2024-10-29 3:35 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-10-29 3:35 Jerry Lv [this message]
2024-10-29 16:56 ` Pali Rohár
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=20241029-foo-fix-v1-1-1dbfed72d023@axis.com \
--to=jerry.lv@axis.com \
--cc=kernel@axis.com \
--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®