[RESEND PATCH 3/3] power: supply: max17040: clamp the reported capacity to 100 %

From: Hugo Karlsson

Date: Sun Nov 08 2026 - 10:44:51 EST


The SOC register can read above 100 % when the cell is fully charged,
and max17040_get_soc() passes the raw value through, so
POWER_SUPPLY_PROP_CAPACITY reports values above 100. The ABI defines
capacity as a percentage, and userspace that rejects values above 100
then shows a full battery as empty.

Clamp the value to 100.

Assisted-by: Claude:claude-opus-5-5 [Claude Code]
Signed-off-by: Hugo Karlsson <huggek@xxxxxxxxxxx>
---
drivers/power/supply/max17040_battery.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/power/supply/max17040_battery.c b/drivers/power/supply/max17040_battery.c
index d93c3daebb..98574dcfb5 100644
--- a/drivers/power/supply/max17040_battery.c
+++ b/drivers/power/supply/max17040_battery.c
@@ -14,6 +14,7 @@
#include <linux/i2c.h>
#include <linux/delay.h>
#include <linux/interrupt.h>
+#include <linux/minmax.h>
#include <linux/power_supply.h>
#include <linux/of.h>
#include <linux/regmap.h>
@@ -210,7 +211,8 @@ static int max17040_get_soc(struct max17040_chip *chip)
if (ret)
return ret;

- return soc >> (chip->quirk_double_soc ? 9 : 8);
+ /* the register overshoots 100 % when the cell is full */
+ return min(soc >> (chip->quirk_double_soc ? 9 : 8), 100U);
}

static int max17040_get_version(struct max17040_chip *chip)
--
2.55.0.windows.5