power: supply: bq27xxx: do not report incorrect zero values
cover power: supply: bq27xxx: do not report incorrect zero values
Some bq27xxx variants are unable to automatically determine the state of the battery, e.g. capacity, after a reset. Under these conditions, they will report incorrect zero values even when the battery is full. This is misleading to userspace, and leads to premature shutdowns.
In an effort to fix this, a patch [0] was submitted, which unfortunately introduced a regression [1]. This patch series resolves the regression and implements the fix correctly.
[0] https://lore.kernel.org/linux-pm/[email protected]/
[1] https://lore.kernel.org/linux-pm/[email protected]/
PATCH 1/2 Revert "power: supply: bq27xxx: do not report bogus zero values"
Commit f3974aca381e ("power: supply: bq27xxx: do not report bogus zero values") breaks property reporting on chips which are reporting normal values. During testing, excessive emphasis was evidently placed on the chip with non-working conditions, and not enough testing was done on a battery that had normal state.
A correct fix will be submitted in a separate patch.
--- drivers/power/supply/bq27xxx_battery.c | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-)
drivers/power/supply/bq27xxx_battery.c +2 -15
@@ -2148,10 +2148,6 @@ 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);
@@ -2175,15 +2171,10 @@ 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); -/* 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 { +else ret =bq27xxx_battery_read_rc (di, val); - }break ;case POWER_SUPPLY_PROP_CHARGE_FULL : ret =bq27xxx_battery_read_fcc (di, val);
@@ -2208,10 +2199,6 @@ 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);
Signature · 1 line
2.47.2
PATCH 2/2 power: supply: bq27xxx: do not report incorrect zero values
In reply to Sicelo A. MhlongoOn 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.
--- drivers/power/supply/bq27xxx_battery.c | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-)
drivers/power/supply/bq27xxx_battery.c +23 -2
@@ -2147,6 +2147,10 @@ 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 0 is reported, it is expected that EDVF is also set */ +if (!di->cache.capacity && di->opts &BQ27XXX_O_ZERO && + !(di->cache.flags &BQ27000_FLAG_EDVF )) +return -EINVAL ; ret =bq27xxx_simple_value (di->cache.capacity, val);break ;case POWER_SUPPLY_PROP_CAPACITY_LEVEL :
@@ -2157,9 +2161,17 @@ 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 0 is reported, it is expected that EDVF is also set */ +if (!ret && !val->intval && di->opts &BQ27XXX_O_ZERO && + !(di->cache.flags &BQ27000_FLAG_EDVF )) +return -EINVAL ;break ;case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG : ret =bq27xxx_battery_read_time (di,BQ27XXX_REG_TTECP , val); +/* If 0 is reported, it is expected that EDVF is also set */ +if (!ret && !val->intval && di->opts &BQ27XXX_O_ZERO && + !(di->cache.flags &BQ27000_FLAG_EDVF )) +return -EINVAL ;break ;case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW : ret =bq27xxx_battery_read_time (di,BQ27XXX_REG_TTF , val);
@@ -2171,10 +2183,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 && !val->intval && 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);
@@ -2199,6 +2216,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 && !val->intval && 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);
Signature · 1 line
2.47.2
Sebastian Reichel
AppliedRe: (subset) [PATCH 0/2] power: supply: bq27xxx: do not report incorrect zero values
In reply to Sicelo A. MhlongoQuoted 10 lines · Sicelo A. Mhlongo
Some bq27xxx variants are unable to automatically determine the state of the battery, e.g. capacity, after a reset. Under these conditions, they will report incorrect zero values even when the battery is full. This is misleading to userspace, and leads to premature shutdowns.
In an effort to fix this, a patch [0] was submitted, which unfortunately introduced a regression [1]. This patch series resolves the regression and implements the fix correctly.
[...]
Applied, thanks!
[1/2] Revert "power: supply: bq27xxx: do not report bogus zero values"
commit: 4ebeb27b727519c13d420b499becdbca272e1399
Best regards,
Signature · 1 line
Sebastian Reichel <[email protected]>
Hi,
Quoted 20 lines · Sicelo A. Mhlongo
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 <[email protected]>--- drivers/power/supply/bq27xxx_battery.c | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-)drivers/power/supply/bq27xxx_battery.c +0
-0
drivers/power/supply/bq27xxx_battery.c static int bq27xxx_battery_get_property(struct power_supply *psy, · line 2153
Quoted 7 lines
drivers/power/supply/bq27xxx_battery.c +4
-0@@ -2147,6 +2147,10 @@ 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 0 is reported, it is expected that EDVF is also set */ +if (!di->cache.capacity && di->opts &BQ27XXX_O_ZERO && + !(di->cache.flags &BQ27000_FLAG_EDVF )) +return -EINVAL ;
This has been replicated 5 times in this patch now. Please create a small helper function for it. Something like
/* If val == 0 is reported, it is expected that EDVF is also set */
void bq27xxx_check_zero_edvf(int *ret, int val, struct bq27xxx_device_info *di) {
if (*ret)
return;
if (val)
return;
if (!(di->opts & BQ27XXX_O_ZERO))
return;
if (di->cache.flags & BQ27000_FLAG_EDVF)
return;
*ret = -EINVAL;
}bq27xxx_check_zero_edvf(ret, di->cache.capacity, di);
Otherwise I think it is sensible to wait for H . Nikolaus Schaller to give this another test (possibly in v3).
-- Greetings,
-- Sebastian
Quoted 53 lines
drivers/power/supply/bq27xxx_battery.c +19
-2ret =bq27xxx_simple_value (di->cache.capacity, val);break ;case POWER_SUPPLY_PROP_CAPACITY_LEVEL :@@ -2157,9 +2161,17 @@ 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 0 is reported, it is expected that EDVF is also set */ +if (!ret && !val->intval && di->opts &BQ27XXX_O_ZERO && + !(di->cache.flags &BQ27000_FLAG_EDVF )) +return -EINVAL ;break ;case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG : ret =bq27xxx_battery_read_time (di,BQ27XXX_REG_TTECP , val); +/* If 0 is reported, it is expected that EDVF is also set */ +if (!ret && !val->intval && di->opts &BQ27XXX_O_ZERO && + !(di->cache.flags &BQ27000_FLAG_EDVF )) +return -EINVAL ;break ;case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW : ret =bq27xxx_battery_read_time (di,BQ27XXX_REG_TTF , val);@@ -2171,10 +2183,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 && !val->intval && 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);@@ -2199,6 +2216,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 && !val->intval && 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);Signature · 1 line
2.47.2