[PATCH v2 10/11] power: supply: max17042: consider task period (max77759)
From: André Draszik
Date: Fri Feb 27 2026 - 02:15:50 EST
Several (register) values reported by the fuel gauge depend on its
internal task period and it needs to be taken into account when
calculating results. All relevant example formulas in the data sheet
assume the default task period (of 5760) and final results need to be
adjusted based on the task period in effect.
Update the code as and where necessary.
Signed-off-by: André Draszik <andre.draszik@xxxxxxxxxx>
---
While I do believe this should apply to all devices supported by this
driver, given the register description in max17042_battery.h, I've made
this change specific to max77759, as I have no way to confirm this
works as expected on those. I've found a data sheet for
max17047/max17050 online, which does describe the relevant register
0x3c as 'reserved', hence I'm a bit hesitant to enable this for all.
v2:
* update commit message subject prefix
---
drivers/power/supply/max17042_battery.c | 20 ++++++++++++++++++++
include/linux/power/max17042_battery.h | 1 +
2 files changed, 21 insertions(+)
diff --git a/drivers/power/supply/max17042_battery.c b/drivers/power/supply/max17042_battery.c
index 44626abdab34..89909b140cf9 100644
--- a/drivers/power/supply/max17042_battery.c
+++ b/drivers/power/supply/max17042_battery.c
@@ -61,6 +61,7 @@ struct max17042_chip {
struct work_struct work;
int init_complete;
int irq;
+ int task_period;
};
static enum power_supply_property max17042_battery_props[] = {
@@ -335,6 +336,8 @@ static int max17042_get_property(struct power_supply *psy,
return ret;
data64 = data * 5000000ll;
+ data64 *= chip->task_period;
+ do_div(data64, MAX17042_DEFAULT_TASK_PERIOD);
do_div(data64, chip->pdata->r_sns);
val->intval = data64;
break;
@@ -344,6 +347,8 @@ static int max17042_get_property(struct power_supply *psy,
return ret;
data64 = data * 5000000ll;
+ data64 *= chip->task_period;
+ do_div(data64, MAX17042_DEFAULT_TASK_PERIOD);
do_div(data64, chip->pdata->r_sns);
val->intval = data64;
break;
@@ -353,6 +358,8 @@ static int max17042_get_property(struct power_supply *psy,
return ret;
data64 = data * 5000000ll;
+ data64 *= chip->task_period;
+ do_div(data64, MAX17042_DEFAULT_TASK_PERIOD);
do_div(data64, chip->pdata->r_sns);
val->intval = data64;
break;
@@ -362,6 +369,8 @@ static int max17042_get_property(struct power_supply *psy,
return ret;
data64 = sign_extend64(data, 15) * 5000000ll;
+ data64 *= chip->task_period;
+ data64 = div_s64(data64, MAX17042_DEFAULT_TASK_PERIOD);
val->intval = div_s64(data64, chip->pdata->r_sns);
break;
case POWER_SUPPLY_PROP_TEMP:
@@ -1146,6 +1155,17 @@ static int max17042_probe(struct i2c_client *client, struct device *dev, int irq
regmap_write(chip->regmap, MAX17042_LearnCFG, 0x0007);
}
+ chip->task_period = MAX17042_DEFAULT_TASK_PERIOD;
+ if (chip->chip_type == MAXIM_DEVICE_TYPE_MAX77759) {
+ ret = regmap_read(chip->regmap, MAX17042_TaskPeriod, &val);
+ if (ret)
+ return dev_err_probe(dev, ret,
+ "failed to read task period\n");
+ chip->task_period = val;
+ }
+ dev_dbg(dev, "task period: %#.4x (%d)\n", chip->task_period,
+ chip->task_period);
+
chip->battery = devm_power_supply_register(dev, max17042_desc,
&psy_cfg);
if (IS_ERR(chip->battery))
diff --git a/include/linux/power/max17042_battery.h b/include/linux/power/max17042_battery.h
index 05097f08ea36..d5b08313cf11 100644
--- a/include/linux/power/max17042_battery.h
+++ b/include/linux/power/max17042_battery.h
@@ -17,6 +17,7 @@
#define MAX17042_DEFAULT_VMAX (4500) /* LiHV cell max */
#define MAX17042_DEFAULT_TEMP_MIN (0) /* For sys without temp sensor */
#define MAX17042_DEFAULT_TEMP_MAX (700) /* 70 degrees Celcius */
+#define MAX17042_DEFAULT_TASK_PERIOD (5760)
/* Consider RepCap which is less then 10 units below FullCAP full */
#define MAX17042_FULL_THRESHOLD 10
--
2.53.0.473.g4a7958ca14-goog