Linux Power Management development
 help / color / mirror / Atom feed
From: "Sicelo A. Mhlongo" <absicsz@gmail.com>
To: Sebastian Reichel <sre@kernel.org>, linux-pm@vger.kernel.org
Cc: hns@goldelico.com, pali@kernel.org, phone-devel@vger.kernel.org,
	letux-kernel@openphoenux.org, akemnade@kernel.org,
	"Sicelo A. Mhlongo" <absicsz@gmail.com>
Subject: [PATCH v2] power: supply: bq27xxx: do not report incorrect zero values
Date: Sat, 22 Mar 2025 17:43:31 +0200	[thread overview]
Message-ID: <20250322161718.253093-1-absicsz@gmail.com> (raw)

On the bq27x00 and bq27x10 variants, a number of conditions can reset the
value stored in the NAC register. This will cause capacity, time-to-empty,
energy, and charge to report the value 0, even when the battery is full.
On the other hand, the chips provide a flag, EDVF, which reliably reports
the true battery empty condition, when these properties are really zero.
Therefore, discard readings for these properties if their value is 0 while
EDVF is unset.

Tested on the Nokia N900 with bq27200.

Signed-off-by: Sicelo A. Mhlongo <absicsz@gmail.com>
---
 drivers/power/supply/bq27xxx_battery.c | 24 ++++++++++++++++++++++--
 1 file changed, 22 insertions(+), 2 deletions(-)

diff --git a/drivers/power/supply/bq27xxx_battery.c b/drivers/power/supply/bq27xxx_battery.c
index 2f31d750a4c1..8e5795c5754e 100644
--- a/drivers/power/supply/bq27xxx_battery.c
+++ b/drivers/power/supply/bq27xxx_battery.c
@@ -2107,6 +2107,15 @@ static int bq27xxx_battery_read_dmin_volt(struct bq27xxx_device_info *di,
 	return 0;
 }
 
+static bool bq27xxx_value_is_valid(struct bq27xxx_device_info *di, int value)
+{
+	/*
+	 * On bq27xxx_0_zero, consider zero values invalid if EDVF is not set
+	 */
+	return value || !(di->opts & BQ27XXX_O_ZERO) ||
+	       (di->cache.flags & BQ27000_FLAG_EDVF);
+}
+
 static int bq27xxx_simple_value(int value,
 				union power_supply_propval *val)
 {
@@ -2147,6 +2156,8 @@ static int bq27xxx_battery_get_property(struct power_supply *psy,
 		ret = bq27xxx_battery_current_and_status(di, val, NULL, NULL);
 		break;
 	case POWER_SUPPLY_PROP_CAPACITY:
+		if (!bq27xxx_value_is_valid(di, di->cache.capacity))
+			return -EINVAL;
 		ret = bq27xxx_simple_value(di->cache.capacity, val);
 		break;
 	case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
@@ -2157,9 +2168,13 @@ static int bq27xxx_battery_get_property(struct power_supply *psy,
 		break;
 	case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
 		ret = bq27xxx_battery_read_time(di, BQ27XXX_REG_TTE, val);
+		if (!ret && !bq27xxx_value_is_valid(di, val->intval))
+			return -EINVAL;
 		break;
 	case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
 		ret = bq27xxx_battery_read_time(di, BQ27XXX_REG_TTECP, val);
+		if (!ret && !bq27xxx_value_is_valid(di, val->intval))
+			return -EINVAL;
 		break;
 	case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
 		ret = bq27xxx_battery_read_time(di, BQ27XXX_REG_TTF, val);
@@ -2171,10 +2186,13 @@ static int bq27xxx_battery_get_property(struct power_supply *psy,
 			val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
 		break;
 	case POWER_SUPPLY_PROP_CHARGE_NOW:
-		if (di->regs[BQ27XXX_REG_NAC] != INVALID_REG_ADDR)
+		if (di->regs[BQ27XXX_REG_NAC] != INVALID_REG_ADDR) {
 			ret = bq27xxx_battery_read_nac(di, val);
-		else
+			if (!ret && !bq27xxx_value_is_valid(di, val->intval))
+				return -EINVAL;
+		} else {
 			ret = bq27xxx_battery_read_rc(di, val);
+		}
 		break;
 	case POWER_SUPPLY_PROP_CHARGE_FULL:
 		ret = bq27xxx_battery_read_fcc(di, val);
@@ -2199,6 +2217,8 @@ static int bq27xxx_battery_get_property(struct power_supply *psy,
 		break;
 	case POWER_SUPPLY_PROP_ENERGY_NOW:
 		ret = bq27xxx_battery_read_energy(di, val);
+		if (!ret && !bq27xxx_value_is_valid(di, val->intval))
+			return -EINVAL;
 		break;
 	case POWER_SUPPLY_PROP_POWER_AVG:
 		ret = bq27xxx_battery_pwr_avg(di, val);
-- 
2.49.0


             reply	other threads:[~2025-03-22 16:17 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-22 15:43 Sicelo A. Mhlongo [this message]
2025-03-24 15:12 ` [PATCH v2] power: supply: bq27xxx: do not report incorrect zero values H. Nikolaus Schaller
2025-03-24 15:42   ` Sicelo

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=20250322161718.253093-1-absicsz@gmail.com \
    --to=absicsz@gmail.com \
    --cc=akemnade@kernel.org \
    --cc=hns@goldelico.com \
    --cc=letux-kernel@openphoenux.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=pali@kernel.org \
    --cc=phone-devel@vger.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