* [PATCH] power: supply: max17042_battery: Make the fractional LSB macros safe
@ 2026-09-15 5:25 Babanpreet Singh
0 siblings, 0 replies; only message in thread
From: Babanpreet Singh @ 2026-09-15 5:25 UTC (permalink / raw)
To: Sebastian Reichel
Cc: Hans de Goede, Krzysztof Kozlowski, Marek Szyprowski,
Sebastian Krzyszkowiak, Purism Kernel Team, linux-pm,
linux-kernel, Babanpreet Singh, kernel test robot, Dan Carpenter
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 <lkp@intel.com>
Reported-by: Dan Carpenter <error27@gmail.com>
Closes: https://lore.kernel.org/r/202606062322.TyvCPB3l-lkp@intel.com/
Assisted-by: Claude:claude-opus-5
Assisted-by: Claude:claude-fable-5
Signed-off-by: Babanpreet Singh <bbnpreetsingh@gmail.com>
---
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
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2026-09-15 5:25 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-09-15 5:25 [PATCH] power: supply: max17042_battery: Make the fractional LSB macros safe Babanpreet Singh
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®