The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH] power: supply: max17040: propagate register read errors
@ 2026-07-24  9:47 Jianing Li
  2026-07-24 23:06 ` Sebastian Reichel
  0 siblings, 1 reply; 2+ messages in thread
From: Jianing Li @ 2026-07-24  9:47 UTC (permalink / raw)
  To: Sebastian Reichel
  Cc: Iskren Chernev, Krzysztof Kozlowski, Marek Szyprowski,
	Matheus Castello, linux-pm, linux-kernel, Jianing Li

max17040_get_vcell() and max17040_get_soc() ignore errors returned by
regmap_read().  When an I2C transfer fails, the uninitialized register
value is converted and reported to userspace as a valid voltage or state
of charge.  The polling worker can also replace the cached state of charge
with the bogus value and emit a spurious change event.

Propagate read errors through the power supply get_property callback and
keep the last valid cached state of charge when polling fails.

Fixes: c6f4a42de60b ("Add MAX17040 Fuel Gauge driver")
Cc: stable@vger.kernel.org
Signed-off-by: Jianing Li <m13940358460@163.com>
---
 drivers/power/supply/max17040_battery.c | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)

diff --git a/drivers/power/supply/max17040_battery.c b/drivers/power/supply/max17040_battery.c
index e94d53b36aa4..948670fb6c4a 100644
--- a/drivers/power/supply/max17040_battery.c
+++ b/drivers/power/supply/max17040_battery.c
@@ -192,19 +192,21 @@ static int max17040_raw_vcell_to_uvolts(struct max17040_chip *chip, u16 vcell)
 static int max17040_get_vcell(struct max17040_chip *chip)
 {
 	u32 vcell;
+	int ret;
 
-	regmap_read(chip->regmap, MAX17040_VCELL, &vcell);
+	ret = regmap_read(chip->regmap, MAX17040_VCELL, &vcell);
 
-	return max17040_raw_vcell_to_uvolts(chip, vcell);
+	return ret ? ret : max17040_raw_vcell_to_uvolts(chip, vcell);
 }
 
 static int max17040_get_soc(struct max17040_chip *chip)
 {
 	u32 soc;
+	int ret;
 
-	regmap_read(chip->regmap, MAX17040_SOC, &soc);
+	ret = regmap_read(chip->regmap, MAX17040_SOC, &soc);
 
-	return soc >> (chip->quirk_double_soc ? 9 : 8);
+	return ret ? ret : soc >> (chip->quirk_double_soc ? 9 : 8);
 }
 
 static int max17040_get_version(struct max17040_chip *chip)
@@ -261,7 +263,11 @@ static int max17040_get_of_data(struct max17040_chip *chip)
 
 static void max17040_check_changes(struct max17040_chip *chip)
 {
-	chip->soc = max17040_get_soc(chip);
+	int soc;
+
+	soc = max17040_get_soc(chip);
+	if (soc >= 0)
+		chip->soc = soc;
 }
 
 static void max17040_queue_work(struct max17040_chip *chip)
@@ -396,10 +402,16 @@ static int max17040_get_property(struct power_supply *psy,
 		val->intval = max17040_get_online(chip);
 		break;
 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
-		val->intval = max17040_get_vcell(chip);
+		ret = max17040_get_vcell(chip);
+		if (ret < 0)
+			return ret;
+		val->intval = ret;
 		break;
 	case POWER_SUPPLY_PROP_CAPACITY:
-		val->intval = max17040_get_soc(chip);
+		ret = max17040_get_soc(chip);
+		if (ret < 0)
+			return ret;
+		val->intval = ret;
 		break;
 	case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN:
 		val->intval = chip->low_soc_alert;
-- 
2.39.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] power: supply: max17040: propagate register read errors
  2026-07-24  9:47 [PATCH] power: supply: max17040: propagate register read errors Jianing Li
@ 2026-07-24 23:06 ` Sebastian Reichel
  0 siblings, 0 replies; 2+ messages in thread
From: Sebastian Reichel @ 2026-07-24 23:06 UTC (permalink / raw)
  To: Jianing Li
  Cc: Iskren Chernev, Krzysztof Kozlowski, Marek Szyprowski,
	Matheus Castello, linux-pm, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 1683 bytes --]

Hi,

On Fri, Jul 24, 2026 at 05:47:59PM +0800, Jianing Li wrote:
> max17040_get_vcell() and max17040_get_soc() ignore errors returned by
> regmap_read().  When an I2C transfer fails, the uninitialized register
> value is converted and reported to userspace as a valid voltage or state
> of charge.  The polling worker can also replace the cached state of charge
> with the bogus value and emit a spurious change event.
> 
> Propagate read errors through the power supply get_property callback and
> keep the last valid cached state of charge when polling fails.
> 
> Fixes: c6f4a42de60b ("Add MAX17040 Fuel Gauge driver")
> Cc: stable@vger.kernel.org
> Signed-off-by: Jianing Li <m13940358460@163.com>
> ---
>  drivers/power/supply/max17040_battery.c | 26 +++++++++++++++++++-------
>  1 file changed, 19 insertions(+), 7 deletions(-)
> 
> diff --git a/drivers/power/supply/max17040_battery.c b/drivers/power/supply/max17040_battery.c
> index e94d53b36aa4..948670fb6c4a 100644
> --- a/drivers/power/supply/max17040_battery.c
> +++ b/drivers/power/supply/max17040_battery.c
> @@ -192,19 +192,21 @@ static int max17040_raw_vcell_to_uvolts(struct max17040_chip *chip, u16 vcell)
>  static int max17040_get_vcell(struct max17040_chip *chip)
>  {
>  	u32 vcell;
> +	int ret;
>  
> -	regmap_read(chip->regmap, MAX17040_VCELL, &vcell);
> +	ret = regmap_read(chip->regmap, MAX17040_VCELL, &vcell);
>  
> -	return max17040_raw_vcell_to_uvolts(chip, vcell);
> +	return ret ? ret : max17040_raw_vcell_to_uvolts(chip, vcell);

Please follow the common convention, i.e.

ret = foo();
if (ret)
    return ret;

return bar();

Greetings,

-- Sebastian

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-07-24 23:07 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-24  9:47 [PATCH] power: supply: max17040: propagate register read errors Jianing Li
2026-07-24 23:06 ` Sebastian Reichel

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox