]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
power: supply: bq27xxx: do not report bogus zero values
authorSicelo A. Mhlongo <absicsz@gmail.com>
Fri, 7 Feb 2025 21:51:44 +0000 (23:51 +0200)
committerSebastian Reichel <sebastian.reichel@collabora.com>
Wed, 19 Feb 2025 23:49:51 +0000 (00:49 +0100)
On the bq27x00 and bq27x10 variants, a number of conditions may reset the value
stored in the NAC register. This will cause capacity, energy, and charge to
report the value 0, even when the battery is full. On the other hand, the chip
also provides a flag, EDVF, which accurately detects the true battery empty
condition, when capacity, charge, and energy are really 0.  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>
Link: https://lore.kernel.org/r/20250207220605.106768-1-absicsz@gmail.com
Signed-off-by: Sebastian Reichel <sebastian.reichel@collabora.com>
drivers/power/supply/bq27xxx_battery.c

index abc6d9329776d79abf7b71301fc932f805613a53..a1d01791df010edbc4adb582dede3c3385898b15 100644 (file)
@@ -2149,6 +2149,10 @@ static int bq27xxx_battery_get_property(struct power_supply *psy,
                break;
        case POWER_SUPPLY_PROP_CAPACITY:
                ret = bq27xxx_simple_value(di->cache.capacity, val);
+               /* If 0 is reported, it is expected that EDVF is also set */
+               if (!ret && di->opts & BQ27XXX_O_ZERO &&
+                  !(di->cache.flags & BQ27000_FLAG_EDVF))
+                       return -EINVAL;
                break;
        case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
                ret = bq27xxx_battery_capacity_level(di, val);
@@ -2172,10 +2176,15 @@ 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 0 is reported, it is expected that EDVF is also set */
+                       if (!ret && di->opts & BQ27XXX_O_ZERO &&
+                          !(di->cache.flags & BQ27000_FLAG_EDVF))
+                               return -EINVAL;
+               } else {
                        ret = bq27xxx_battery_read_rc(di, val);
+               }
                break;
        case POWER_SUPPLY_PROP_CHARGE_FULL:
                ret = bq27xxx_battery_read_fcc(di, val);
@@ -2200,6 +2209,10 @@ 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 0 is reported, it is expected that EDVF is also set */
+               if (!ret && di->opts & BQ27XXX_O_ZERO &&
+                  !(di->cache.flags & BQ27000_FLAG_EDVF))
+                       return -EINVAL;
                break;
        case POWER_SUPPLY_PROP_POWER_AVG:
                ret = bq27xxx_battery_pwr_avg(di, val);