[PATCH 1/6] power: supply: bq27xxx: Move temperature reading out of update loop

From: Andrew Davis
Date: Mon Mar 25 2024 - 17:43:01 EST


Most of the functions that read values return a status and put the value
itself in an a function parameter. Update temperature reading to match.

As temp is not checked for changes as part of the update loop, remove
the read of the temperature from the periodic update loop. This saves
I2C/1W bandwidth. It also means we do not have to cache it, fresh
values are read when requested.

Signed-off-by: Andrew Davis <afd@xxxxxx>
---
drivers/power/supply/bq27xxx_battery.c | 17 ++++++++++-------
include/linux/power/bq27xxx_battery.h | 1 -
2 files changed, 10 insertions(+), 8 deletions(-)

diff --git a/drivers/power/supply/bq27xxx_battery.c b/drivers/power/supply/bq27xxx_battery.c
index abca568344686..00b2a56039ac0 100644
--- a/drivers/power/supply/bq27xxx_battery.c
+++ b/drivers/power/supply/bq27xxx_battery.c
@@ -1652,10 +1652,11 @@ static int bq27xxx_battery_read_energy(struct bq27xxx_device_info *di)
}

/*
- * Return the battery temperature in tenths of degree Kelvin
+ * Return the battery temperature in tenths of degree Celsius
* Or < 0 if something fails.
*/
-static int bq27xxx_battery_read_temperature(struct bq27xxx_device_info *di)
+static int bq27xxx_battery_read_temperature(struct bq27xxx_device_info *di,
+ union power_supply_propval *val)
{
int temp;

@@ -1668,7 +1669,12 @@ static int bq27xxx_battery_read_temperature(struct bq27xxx_device_info *di)
if (di->opts & BQ27XXX_O_ZERO)
temp = 5 * temp / 2;

- return temp;
+ /* Convert decidegree Kelvin to Celsius */
+ temp -= 2731;
+
+ val->intval = temp;
+
+ return 0;
}

/*
@@ -1851,7 +1857,6 @@ static void bq27xxx_battery_update_unlocked(struct bq27xxx_device_info *di)
if ((cache.flags & 0xff) == 0xff)
cache.flags = -1; /* read error */
if (cache.flags >= 0) {
- cache.temperature = bq27xxx_battery_read_temperature(di);
if (di->regs[BQ27XXX_REG_TTE] != INVALID_REG_ADDR)
cache.time_to_empty = bq27xxx_battery_read_time(di, BQ27XXX_REG_TTE);
if (di->regs[BQ27XXX_REG_TTECP] != INVALID_REG_ADDR)
@@ -2038,9 +2043,7 @@ static int bq27xxx_battery_get_property(struct power_supply *psy,
ret = bq27xxx_battery_capacity_level(di, val);
break;
case POWER_SUPPLY_PROP_TEMP:
- ret = bq27xxx_simple_value(di->cache.temperature, val);
- if (ret == 0)
- val->intval -= 2731; /* convert decidegree k to c */
+ ret = bq27xxx_battery_read_temperature(di, val);
break;
case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
ret = bq27xxx_simple_value(di->cache.time_to_empty, val);
diff --git a/include/linux/power/bq27xxx_battery.h b/include/linux/power/bq27xxx_battery.h
index b9e5bd2b42d36..64b2749d9562b 100644
--- a/include/linux/power/bq27xxx_battery.h
+++ b/include/linux/power/bq27xxx_battery.h
@@ -47,7 +47,6 @@ struct bq27xxx_access_methods {
};

struct bq27xxx_reg_cache {
- int temperature;
int time_to_empty;
int time_to_empty_avg;
int time_to_full;
--
2.39.2