Skip to contentSkip to conversation

power: supply: sbs-battery: only return health when battery present

PATCH In review Michael Nosthoff 3 participants 6 messages Aug 16, 2019 – Sep 2, 2019

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 power: supply: sbs-battery: only return health when battery present

when the battery is set to sbs-mode and no gpio detection is enabled "health" is always returning a value even when the battery is not present. All other fields return "not present".
This leads to a scenario where the driver is constantly switching between "present" and "not present" state. This generates a lot of constant traffic on the i2c.

This commit changes the response of "health" to an error when the battery is not responding leading to a consistent "not present" state.

Fixes 76b16f4cdfb8 ("power: supply: sbs-battery: don't assume

MANUFACTURER_DATA formats")

---
 drivers/power/supply/sbs-battery.c | 25 ++++++++++++++++---------
 1 file changed, 16 insertions(+), 9 deletions(-)
drivers/power/supply/sbs-battery.c +16 -9
index 2e86cc1e0e35..f8d74e9f7931 100644
@@ -314,17 +314,22 @@ static int sbs_get_battery_presence_and_health(
 {
 	int ret;
 
-	if (psp == POWER_SUPPLY_PROP_PRESENT) {
-		/* Dummy command; if it succeeds, battery is present. */
-		ret = sbs_read_word_data(client, sbs_data[REG_STATUS].addr);
-		if (ret < 0)
-			val->intval = 0; /* battery disconnected */
-		else
-			val->intval = 1; /* battery present */
-	} else { /* POWER_SUPPLY_PROP_HEALTH */
+	/* Dummy command; if it succeeds, battery is present. */
+	ret = sbs_read_word_data(client, sbs_data[REG_STATUS].addr);
+
+	if (ret < 0) { /* battery not present*/
+		if (psp == POWER_SUPPLY_PROP_PRESENT) {
+			val->intval = 0;
+			return 0;
+		}
+		return ret;
+	}
+
+	if (psp == POWER_SUPPLY_PROP_PRESENT)
+		val->intval = 1; /* battery present */
+	else /* POWER_SUPPLY_PROP_HEALTH */
 		/* SBS spec doesn't have a general health command. */
 		val->intval = POWER_SUPPLY_HEALTH_UNKNOWN;
-	}
 
 	return 0;
 }
@@ -626,6 +631,8 @@ static int sbs_get_property(struct power_supply *psy,
 		else
 			ret = sbs_get_battery_presence_and_health(client, psp,
 								  val);
+
+		/* this can only be true if no gpio is used */
 		if (psp == POWER_SUPPLY_PROP_PRESENT)
 			return 0;
 		break;
Signature · 1 line
2.20.1
Reviewed-by Tested-by on v1 1/1
#1 lore raw
Quoted 6 lines · Michael Nosthoff

when the battery is set to sbs-mode and no gpio detection is enabled "health" is always returning a value even when the battery is not present. All other fields return "not present".
This leads to a scenario where the driver is constantly switching between "present" and "not present" state. This generates a lot of constant traffic on the i2c.

That depends on how often you're checking the "health" attribute, doesn't it? But anyway, the bug is real.

Quoted 2 lines

This commit changes the response of "health" to an error when the battery is not responding leading to a consistent "not present" state.

Ack, and thanks for the fix.

Quoted 6 lines
Fixes 76b16f4cdfb8 ("power: supply: sbs-battery: don't assume

MANUFACTURER_DATA formats")

Quoted 17 lines
---
 drivers/power/supply/sbs-battery.c | 25 ++++++++++++++++---------
 1 file changed, 16 insertions(+), 9 deletions(-)
drivers/power/supply/sbs-battery.c +0 -5
index 2e86cc1e0e35..f8d74e9f7931 100644
@@ -314,17 +314,22 @@ static int sbs_get_battery_presence_and_health(
 {
 	int ret;
 
-	if (psp == POWER_SUPPLY_PROP_PRESENT) {
-		/* Dummy command; if it succeeds, battery is present. */
-		ret = sbs_read_word_data(client, sbs_data[REG_STATUS].addr);
-		if (ret < 0)
-			val->intval = 0; /* battery disconnected */

drivers/power/supply/sbs-battery.c static int sbs_get_battery_presence_and_health( · line 323

Quoted 10 lines
drivers/power/supply/sbs-battery.c +7 -3
-		else
-			val->intval = 1; /* battery present */
-	} else { /* POWER_SUPPLY_PROP_HEALTH */
+	/* Dummy command; if it succeeds, battery is present. */
+	ret = sbs_read_word_data(client, sbs_data[REG_STATUS].addr);
+
+	if (ret < 0) { /* battery not present*/
+		if (psp == POWER_SUPPLY_PROP_PRESENT) {
+			val->intval = 0;
+			return 0;

Technically, you don't need the 'return 0' (and if we care about
symmetry: the TI version doesn't), since the caller knows that "not present" will yield errors. I'm not sure which version makes more sense.

Quoted 25 lines
drivers/power/supply/sbs-battery.c +9 -1
+		}
+		return ret;
+	}
+
+	if (psp == POWER_SUPPLY_PROP_PRESENT)
+		val->intval = 1; /* battery present */
+	else /* POWER_SUPPLY_PROP_HEALTH */
 		/* SBS spec doesn't have a general health command. */
 		val->intval = POWER_SUPPLY_HEALTH_UNKNOWN;
-	}
 
 	return 0;
 }
@@ -626,6 +631,8 @@ static int sbs_get_property(struct power_supply *psy,
 		else
 			ret = sbs_get_battery_presence_and_health(client, psp,
 								  val);
+
+		/* this can only be true if no gpio is used */
 		if (psp == POWER_SUPPLY_PROP_PRESENT)
 			return 0;
 		break;
Signature · 1 line
2.20.1
#2 lore raw

+ Sebastian

I'm not sure if he expects to be on the To/Cc, but that usually is the way to get a maintainer to take your patch.

Brian

Quoted 83 lines · Brian Norris
Quoted 6 lines · Michael Nosthoff

when the battery is set to sbs-mode and no gpio detection is enabled "health" is always returning a value even when the battery is not present. All other fields return "not present".
This leads to a scenario where the driver is constantly switching between "present" and "not present" state. This generates a lot of constant traffic on the i2c.

That depends on how often you're checking the "health" attribute, doesn't it? But anyway, the bug is real.

Quoted 2 lines

This commit changes the response of "health" to an error when the battery is not responding leading to a consistent "not present" state.

Ack, and thanks for the fix.

Quoted 6 lines
Fixes 76b16f4cdfb8 ("power: supply: sbs-battery: don't assume

MANUFACTURER_DATA formats")

Quoted 17 lines
---
 drivers/power/supply/sbs-battery.c | 25 ++++++++++++++++---------
 1 file changed, 16 insertions(+), 9 deletions(-)
drivers/power/supply/sbs-battery.c +0 -5
index 2e86cc1e0e35..f8d74e9f7931 100644
@@ -314,17 +314,22 @@ static int sbs_get_battery_presence_and_health(
 {
 	int ret;
 
-	if (psp == POWER_SUPPLY_PROP_PRESENT) {
-		/* Dummy command; if it succeeds, battery is present. */
-		ret = sbs_read_word_data(client, sbs_data[REG_STATUS].addr);
-		if (ret < 0)
-			val->intval = 0; /* battery disconnected */

drivers/power/supply/sbs-battery.c static int sbs_get_battery_presence_and_health( · line 323

Quoted 10 lines
drivers/power/supply/sbs-battery.c +7 -3
-		else
-			val->intval = 1; /* battery present */
-	} else { /* POWER_SUPPLY_PROP_HEALTH */
+	/* Dummy command; if it succeeds, battery is present. */
+	ret = sbs_read_word_data(client, sbs_data[REG_STATUS].addr);
+
+	if (ret < 0) { /* battery not present*/
+		if (psp == POWER_SUPPLY_PROP_PRESENT) {
+			val->intval = 0;
+			return 0;

Technically, you don't need the 'return 0' (and if we care about
symmetry: the TI version doesn't), since the caller knows that "not present" will yield errors. I'm not sure which version makes more sense.

Quoted 25 lines
drivers/power/supply/sbs-battery.c +9 -1
+		}
+		return ret;
+	}
+
+	if (psp == POWER_SUPPLY_PROP_PRESENT)
+		val->intval = 1; /* battery present */
+	else /* POWER_SUPPLY_PROP_HEALTH */
 		/* SBS spec doesn't have a general health command. */
 		val->intval = POWER_SUPPLY_HEALTH_UNKNOWN;
-	}
 
 	return 0;
 }
@@ -626,6 +631,8 @@ static int sbs_get_property(struct power_supply *psy,
 		else
 			ret = sbs_get_battery_presence_and_health(client, psp,
 								  val);
+
+		/* this can only be true if no gpio is used */
 		if (psp == POWER_SUPPLY_PROP_PRESENT)
 			return 0;
 		break;
Signature · 1 line
2.20.1
#3 lore raw
In reply to Brian Norris
Quoted 10 lines · Brian Norris
Quoted 6 lines · Michael Nosthoff

when the battery is set to sbs-mode and no gpio detection is enabled "health" is always returning a value even when the battery is not present. All other fields return "not present".
This leads to a scenario where the driver is constantly switching between "present" and "not present" state. This generates a lot of constant traffic on the i2c.

That depends on how often you're checking the "health" attribute, doesn't it? But anyway, the bug is real.

At least on my Hardware I had constant traffic from the moment the device was probed. I have no userland process accessing the device.
I'm guessing that it has something todo with the call to 'power_supply_changed'. This done at the end of 'sbs_get_property' if the presence state changed and no gpio is used. I suspect it triggers a readout of all the properties and leads to this endless loop?

Quoted 8 lines
Quoted 2 lines

This commit changes the response of "health" to an error when the battery is not responding leading to a consistent "not present" state.

Ack, and thanks for the fix.

Quoted 3 lines
Fixes 76b16f4cdfb8 ("power: supply: sbs-battery: don't assume

MANUFACTURER_DATA formats")

Quoted 6 lines
Quoted 3 lines

thanks for review!

Quoted 58 lines
Quoted 18 lines
---
  drivers/power/supply/sbs-battery.c | 25 ++++++++++++++++---------
  1 file changed, 16 insertions(+), 9 deletions(-)
drivers/power/supply/sbs-battery.c +0 -5
index 2e86cc1e0e35..f8d74e9f7931 100644
@@ -314,17 +314,22 @@ static int sbs_get_battery_presence_and_health(
  {
  	int ret;
 
-	if (psp == POWER_SUPPLY_PROP_PRESENT) {
-		/* Dummy command; if it succeeds, battery is present. */
-		ret = sbs_read_word_data(client, sbs_data[REG_STATUS].addr);
-		if (ret<  0)
-			val->intval = 0; /* battery disconnected */

drivers/power/supply/sbs-battery.c static int sbs_get_battery_presence_and_health( · line 323

Quoted 10 lines
drivers/power/supply/sbs-battery.c +7 -3
-		else
-			val->intval = 1; /* battery present */
-	} else { /* POWER_SUPPLY_PROP_HEALTH */
+	/* Dummy command; if it succeeds, battery is present. */
+	ret = sbs_read_word_data(client, sbs_data[REG_STATUS].addr);
+
+	if (ret<  0) { /* battery not present*/
+		if (psp == POWER_SUPPLY_PROP_PRESENT) {
+			val->intval = 0;
+			return 0;

Technically, you don't need the 'return 0' (and if we care about
symmetry: the TI version doesn't), since the caller knows that "not present" will yield errors. I'm not sure which version makes more sense.

Quoted 25 lines
drivers/power/supply/sbs-battery.c +9 -1
+		}
+		return ret;
+	}
+
+	if (psp == POWER_SUPPLY_PROP_PRESENT)
+		val->intval = 1; /* battery present */
+	else /* POWER_SUPPLY_PROP_HEALTH */
  		/* SBS spec doesn't have a general health command. */
  		val->intval = POWER_SUPPLY_HEALTH_UNKNOWN;
-	}
 
  	return 0;
  }
@@ -626,6 +631,8 @@ static int sbs_get_property(struct power_supply *psy,
  		else
  			ret = sbs_get_battery_presence_and_health(client, psp,
  								  val);
+
+		/* this can only be true if no gpio is used */
  		if (psp == POWER_SUPPLY_PROP_PRESENT)
  			return 0;
  		break;
Signature · 1 line
2.20.1
#4 lore raw
Quoted 4 lines · Michael Nosthoff

This done at the end of 'sbs_get_property' if the presence state changed and no gpio is used. I suspect it triggers a readout of all the properties and leads to this
endless loop?

Ah, it's not all properties IIUC, but it does lead to power_supply_update_leds() -> power_supply_update_bat_leds() -> power_supply_get_property(... POWER_SUPPLY_PROP_STATUS ...). That would be enough to kick off the loop you're noticing.

Brian

#5 lore raw
In reply to Michael Nosthoff

Hi,

Quoted 10 lines · Michael Nosthoff

when the battery is set to sbs-mode and no gpio detection is enabled "health" is always returning a value even when the battery is not present. All other fields return "not present".
This leads to a scenario where the driver is constantly switching between "present" and "not present" state. This generates a lot of constant traffic on the i2c.

This commit changes the response of "health" to an error when the battery is not responding leading to a consistent "not present" state.

Quoted 7 lines
Fixes 76b16f4cdfb8 ("power: supply: sbs-battery: don't assume

MANUFACTURER_DATA formats")

---

Thanks, queued.

-- Sebastian

Quoted 51 lines
 drivers/power/supply/sbs-battery.c | 25 ++++++++++++++++---------
 1 file changed, 16 insertions(+), 9 deletions(-)
drivers/power/supply/sbs-battery.c +16 -9
index 2e86cc1e0e35..f8d74e9f7931 100644
@@ -314,17 +314,22 @@ static int sbs_get_battery_presence_and_health(
 {
 	int ret;
 
-	if (psp == POWER_SUPPLY_PROP_PRESENT) {
-		/* Dummy command; if it succeeds, battery is present. */
-		ret = sbs_read_word_data(client, sbs_data[REG_STATUS].addr);
-		if (ret < 0)
-			val->intval = 0; /* battery disconnected */
-		else
-			val->intval = 1; /* battery present */
-	} else { /* POWER_SUPPLY_PROP_HEALTH */
+	/* Dummy command; if it succeeds, battery is present. */
+	ret = sbs_read_word_data(client, sbs_data[REG_STATUS].addr);
+
+	if (ret < 0) { /* battery not present*/
+		if (psp == POWER_SUPPLY_PROP_PRESENT) {
+			val->intval = 0;
+			return 0;
+		}
+		return ret;
+	}
+
+	if (psp == POWER_SUPPLY_PROP_PRESENT)
+		val->intval = 1; /* battery present */
+	else /* POWER_SUPPLY_PROP_HEALTH */
 		/* SBS spec doesn't have a general health command. */
 		val->intval = POWER_SUPPLY_HEALTH_UNKNOWN;
-	}
 
 	return 0;
 }
@@ -626,6 +631,8 @@ static int sbs_get_property(struct power_supply *psy,
 		else
 			ret = sbs_get_battery_presence_and_health(client, psp,
 								  val);
+
+		/* this can only be true if no gpio is used */
 		if (psp == POWER_SUPPLY_PROP_PRESENT)
 			return 0;
 		break;
Signature · 1 line
2.20.1