[PATCH] power: supply: max17042_battery: Make the fractional LSB macros safe
flat view
COOLING6d
IN LINUX-NEXT
From: Babanpreet Singh <hidden>
Date: 2026-09-15 05:25:32
Also in:
lkml
Subsystem:
maxim max17042 family fuel gauge drivers, power supply class/subsystem and drivers, the rest · Maintainers:
Sebastian Reichel, Linus Torvalds
Queued in linux-next as df2914e20485 on 2026-10-04.
The fractional LSB macros expand to a plain division, so they only work when multiplied first and evaluate to 0 anywhere else. Take the value as a macro argument and apply it with mult_frac(). No functional change. Reported-by: kernel test robot <redacted> Reported-by: Dan Carpenter <redacted> Closes: https://lore.kernel.org/r/202606062322.TyvCPB3l-lkp@intel.com/ (local) Assisted-by: Claude:claude-opus-5 Assisted-by: Claude:claude-fable-5 Signed-off-by: Babanpreet Singh <redacted> --- drivers/power/supply/max17042_battery.c | 30 +++++++++++++++---------- 1 file changed, 18 insertions(+), 12 deletions(-)
diff --git a/drivers/power/supply/max17042_battery.c b/drivers/power/supply/max17042_battery.c
index a53970af56cbb..861b5dbc11c1e 100644
--- a/drivers/power/supply/max17042_battery.c
+++ b/drivers/power/supply/max17042_battery.c@@ -16,6 +16,7 @@ #include <linux/i2c.h> #include <linux/delay.h> #include <linux/interrupt.h> +#include <linux/math.h> #include <linux/platform_device.h> #include <linux/pm.h> #include <linux/power_supply.h>
@@ -57,10 +58,15 @@ #define MAX17042_CURRENT_LSB 1562500ll /* 1.5625µV/Rsense */ #define MAX17042_CAPACITY_LSB 5000000ll /* 5.0µVH/Rsense */ -#define MAX17042_TIME_LSB 5625 / 1000 /* s */ -#define MAX17042_VOLTAGE_LSB 625 / 8 /* µV */ -#define MAX17042_RESISTANCE_LSB 1 / 4096 /* Ω */ -#define MAX17042_TEMPERATURE_LSB 1 / 256 /* °C */ +/* + * These LSBs are fractions, so they take their argument rather than + * expanding to a bare "n / d" token sequence, which is only correct in a + * "value * LSB" context. + */ +#define MAX17042_TIME_LSB(val) mult_frac(val, 5625, 1000) /* s */ +#define MAX17042_VOLTAGE_LSB(val) mult_frac(val, 625, 8) /* µV */ +#define MAX17042_RESISTANCE_LSB(val) mult_frac(val, 1, 4096) /* Ω */ +#define MAX17042_TEMPERATURE_LSB(val) mult_frac(val, 1, 256) /* °C */ #define MAX17055_DQACC_DIV 32 #define MAX17055_DPACC_FACTOR 44138
@@ -138,7 +144,7 @@ static int max17042_get_temperature(struct max17042_chip *chip, int *temp) *temp = sign_extend32(data, 15); /* The value is converted into deci-centigrade scale */ - *temp = *temp * 10 * MAX17042_TEMPERATURE_LSB; + *temp = MAX17042_TEMPERATURE_LSB(*temp * 10); return 0; }
@@ -214,7 +220,7 @@ static int max17042_get_battery_health(struct max17042_chip *chip, int *health) goto health_error; /* bits [0-3] unused */ - vavg = val * MAX17042_VOLTAGE_LSB; + vavg = MAX17042_VOLTAGE_LSB(val); /* Convert to millivolts */ vavg /= 1000;
@@ -223,7 +229,7 @@ static int max17042_get_battery_health(struct max17042_chip *chip, int *health) goto health_error; /* bits [0-3] unused */ - vbatt = val * MAX17042_VOLTAGE_LSB; + vbatt = MAX17042_VOLTAGE_LSB(val); /* Convert to millivolts */ vbatt /= 1000;
@@ -330,21 +336,21 @@ static int max17042_get_property(struct power_supply *psy, if (ret < 0) return ret; - val->intval = data * MAX17042_VOLTAGE_LSB; + val->intval = MAX17042_VOLTAGE_LSB(data); break; case POWER_SUPPLY_PROP_VOLTAGE_AVG: ret = regmap_read(map, MAX17042_AvgVCELL, &data); if (ret < 0) return ret; - val->intval = data * MAX17042_VOLTAGE_LSB; + val->intval = MAX17042_VOLTAGE_LSB(data); break; case POWER_SUPPLY_PROP_VOLTAGE_OCV: ret = regmap_read(map, MAX17042_OCVInternal, &data); if (ret < 0) return ret; - val->intval = data * MAX17042_VOLTAGE_LSB; + val->intval = MAX17042_VOLTAGE_LSB(data); break; case POWER_SUPPLY_PROP_CAPACITY: if (chip->enable_current_sense)
@@ -473,7 +479,7 @@ static int max17042_get_property(struct power_supply *psy, if (data == U16_MAX) return -ENODATA; - val->intval = data * MAX17042_TIME_LSB; + val->intval = MAX17042_TIME_LSB(data); break; case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW: if (chip->chip_type != MAXIM_DEVICE_TYPE_MAX17055 &&
@@ -488,7 +494,7 @@ static int max17042_get_property(struct power_supply *psy, if (data == U16_MAX) return -ENODATA; - val->intval = data * MAX17042_TIME_LSB; + val->intval = MAX17042_TIME_LSB(data); break; default: return -EINVAL;
base-commit: 4fc88ba435dadbc05990951e3f3fbd8ccd2df140 -- 2.43.0