Hi,

On Wed, Mar 12, 2025 at 02:14:09PM +0200, Sicelo A. Mhlongo wrote:
> 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 <absi...@gmail.com>
> ---
>  drivers/power/supply/bq27xxx_battery.c | 25 +++++++++++++++++++++++--
>  1 file changed, 23 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/power/supply/bq27xxx_battery.c 
> b/drivers/power/supply/bq27xxx_battery.c
> index 2f31d750a4c1..29fc51653b5f 100644
> --- a/drivers/power/supply/bq27xxx_battery.c
> +++ b/drivers/power/supply/bq27xxx_battery.c
> @@ -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

>               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);
> -- 
> 2.47.2
> 

Attachment: signature.asc
Description: PGP signature

Reply via email to