Skip to content

Commit b228fa6

Browse files
tq-schifferngregkh
authored andcommitted
power: supply: bq27xxx: fix power_avg for newer ICs
[ Upstream commit c4d57c2 ] On all newer bq27xxx ICs, the AveragePower register contains a signed value; in addition to handling the raw value as unsigned, the driver code also didn't convert it to µW as expected. At least for the BQ28Z610, the reference manual incorrectly states that the value is in units of 1mW and not 10mW. I have no way of knowing whether the manuals of other supported ICs contain the same error, or if there are models that actually use 1mW. At least, the new code shouldn't be *less* correct than the old version for any device. power_avg is removed from the cache structure, se we don't have to extend it to store both a signed value and an error code. Always getting an up-to-date value may be desirable anyways, as it avoids inconsistent current and power readings when switching between charging and discharging. Signed-off-by: Matthias Schiffer <matthias.schiffer@ew.tq-group.com> Signed-off-by: Sebastian Reichel <sebastian.reichel@collabora.com> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent a2bffd9 commit b228fa6

2 files changed

Lines changed: 27 additions & 25 deletions

File tree

‎drivers/power/supply/bq27xxx_battery.c‎

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1631,27 +1631,6 @@ static int bq27xxx_battery_read_time(struct bq27xxx_device_info *di, u8 reg)
16311631
return tval * 60;
16321632
}
16331633

1634-
/*
1635-
* Read an average power register.
1636-
* Return < 0 if something fails.
1637-
*/
1638-
static int bq27xxx_battery_read_pwr_avg(struct bq27xxx_device_info *di)
1639-
{
1640-
int tval;
1641-
1642-
tval = bq27xxx_read(di, BQ27XXX_REG_AP, false);
1643-
if (tval < 0) {
1644-
dev_err(di->dev, "error reading average power register %02x: %d\n",
1645-
BQ27XXX_REG_AP, tval);
1646-
return tval;
1647-
}
1648-
1649-
if (di->opts & BQ27XXX_O_ZERO)
1650-
return (tval * BQ27XXX_POWER_CONSTANT) / BQ27XXX_RS;
1651-
else
1652-
return tval;
1653-
}
1654-
16551634
/*
16561635
* Returns true if a battery over temperature condition is detected
16571636
*/
@@ -1739,8 +1718,6 @@ void bq27xxx_battery_update(struct bq27xxx_device_info *di)
17391718
}
17401719
if (di->regs[BQ27XXX_REG_CYCT] != INVALID_REG_ADDR)
17411720
cache.cycle_count = bq27xxx_battery_read_cyct(di);
1742-
if (di->regs[BQ27XXX_REG_AP] != INVALID_REG_ADDR)
1743-
cache.power_avg = bq27xxx_battery_read_pwr_avg(di);
17441721

17451722
/* We only have to read charge design full once */
17461723
if (di->charge_design_full <= 0)
@@ -1803,6 +1780,32 @@ static int bq27xxx_battery_current(struct bq27xxx_device_info *di,
18031780
return 0;
18041781
}
18051782

1783+
/*
1784+
* Get the average power in µW
1785+
* Return < 0 if something fails.
1786+
*/
1787+
static int bq27xxx_battery_pwr_avg(struct bq27xxx_device_info *di,
1788+
union power_supply_propval *val)
1789+
{
1790+
int power;
1791+
1792+
power = bq27xxx_read(di, BQ27XXX_REG_AP, false);
1793+
if (power < 0) {
1794+
dev_err(di->dev,
1795+
"error reading average power register %02x: %d\n",
1796+
BQ27XXX_REG_AP, power);
1797+
return power;
1798+
}
1799+
1800+
if (di->opts & BQ27XXX_O_ZERO)
1801+
val->intval = (power * BQ27XXX_POWER_CONSTANT) / BQ27XXX_RS;
1802+
else
1803+
/* Other gauges return a signed value in units of 10mW */
1804+
val->intval = (int)((s16)power) * 10000;
1805+
1806+
return 0;
1807+
}
1808+
18061809
static int bq27xxx_battery_status(struct bq27xxx_device_info *di,
18071810
union power_supply_propval *val)
18081811
{
@@ -1987,7 +1990,7 @@ static int bq27xxx_battery_get_property(struct power_supply *psy,
19871990
ret = bq27xxx_simple_value(di->cache.energy, val);
19881991
break;
19891992
case POWER_SUPPLY_PROP_POWER_AVG:
1990-
ret = bq27xxx_simple_value(di->cache.power_avg, val);
1993+
ret = bq27xxx_battery_pwr_avg(di, val);
19911994
break;
19921995
case POWER_SUPPLY_PROP_HEALTH:
19931996
ret = bq27xxx_simple_value(di->cache.health, val);

‎include/linux/power/bq27xxx_battery.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ struct bq27xxx_reg_cache {
5353
int capacity;
5454
int energy;
5555
int flags;
56-
int power_avg;
5756
int health;
5857
};
5958

0 commit comments

Comments
 (0)