mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Babanpreet Singh <bbnpreetsingh@gmail.com>
To: Sebastian Reichel <sre@kernel.org>
Cc: Hans de Goede <hansg@kernel.org>,
	Krzysztof Kozlowski <krzk@kernel.org>,
	Marek Szyprowski <m.szyprowski@samsung.com>,
	Sebastian Krzyszkowiak <sebastian.krzyszkowiak@puri.sm>,
	Purism Kernel Team <kernel@puri.sm>,
	linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org,
	Babanpreet Singh <bbnpreetsingh@gmail.com>,
	kernel test robot <lkp@intel.com>,
	Dan Carpenter <error27@gmail.com>
Subject: [PATCH] power: supply: max17042_battery: Make the fractional LSB macros safe
Date: Tue, 15 Sep 2026 05:25:29 +0000	[thread overview]
Message-ID: <20260915052529.7-1-bbnpreetsingh@gmail.com> (raw)

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


                 reply	other threads:[~2026-09-15  5:25 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260915052529.7-1-bbnpreetsingh@gmail.com \
    --to=bbnpreetsingh@gmail.com \
    --cc=error27@gmail.com \
    --cc=hansg@kernel.org \
    --cc=kernel@puri.sm \
    --cc=krzk@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=lkp@intel.com \
    --cc=m.szyprowski@samsung.com \
    --cc=sebastian.krzyszkowiak@puri.sm \
    --cc=sre@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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®