linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 2/2] power: supply: sbs-battery: don't assume every i2c errors as battery disconnect
@ 2020-08-11  6:59 Ikjoon Jang
  2020-08-13  4:06 ` Nicolas Boichat
  0 siblings, 1 reply; 2+ messages in thread
From: Ikjoon Jang @ 2020-08-11  6:59 UTC (permalink / raw)
  To: Sebastian Reichel, linux-pm; +Cc: linux-kernel, Ikjoon Jang

Current sbs-battery considers all smbus errors as diconnection events
when battery-detect pin isn't supplied, and restored to present state back
on any successful transaction were made.

This can leads to unlimited state changes between present and !present
when one unsupported command was requested and other following commands
were successful, e.g. udev rules tries to read multiple properties.

This patch checks battery presence by reading known good command to
check battery existence.

Signed-off-by: Ikjoon Jang <ikjn@chromium.org>
---
v2: fix return value checking of sbs_get_battery_presence_and_health()
---
 drivers/power/supply/sbs-battery.c | 26 +++++++++++++++++---------
 1 file changed, 17 insertions(+), 9 deletions(-)

diff --git a/drivers/power/supply/sbs-battery.c b/drivers/power/supply/sbs-battery.c
index 6acb4ea25d2a..db964a470ebc 100644
--- a/drivers/power/supply/sbs-battery.c
+++ b/drivers/power/supply/sbs-battery.c
@@ -878,10 +878,17 @@ static int sbs_get_property(struct power_supply *psy,
 	if (!chip->enable_detection)
 		goto done;
 
-	if (!chip->gpio_detect &&
-		chip->is_present != (ret >= 0)) {
-		sbs_update_presence(chip, (ret >= 0));
-		power_supply_changed(chip->power_supply);
+	if (!chip->gpio_detect && chip->is_present != (ret >=0)) {
+		bool old_present = chip->is_present;
+		union power_supply_propval val;
+
+		sbs_get_battery_presence_and_health(
+				client, POWER_SUPPLY_PROP_PRESENT, &val);
+
+		sbs_update_presence(chip, val.intval);
+
+		if (old_present != chip->is_present)
+			power_supply_changed(chip->power_supply);
 	}
 
 done:
@@ -1067,11 +1074,12 @@ static int sbs_probe(struct i2c_client *client)
 	 * to the battery.
 	 */
 	if (!(force_load || chip->gpio_detect)) {
-		rc = sbs_read_word_data(client, sbs_data[REG_STATUS].addr);
-
-		if (rc < 0) {
-			dev_err(&client->dev, "%s: Failed to get device status\n",
-				__func__);
+		union power_supply_propval val;
+		sbs_get_battery_presence_and_health(
+				client, POWER_SUPPLY_PROP_PRESENT, &val);
+		if (!val.intval) {
+			dev_err(&client->dev, "Failed to get present status\n");
+			rc = -ENODEV;
 			goto exit_psupply;
 		}
 	}
-- 
2.28.0.236.gb10cc79966-goog


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

* Re: [PATCH v2 2/2] power: supply: sbs-battery: don't assume every i2c errors as battery disconnect
  2020-08-11  6:59 [PATCH v2 2/2] power: supply: sbs-battery: don't assume every i2c errors as battery disconnect Ikjoon Jang
@ 2020-08-13  4:06 ` Nicolas Boichat
  0 siblings, 0 replies; 2+ messages in thread
From: Nicolas Boichat @ 2020-08-13  4:06 UTC (permalink / raw)
  To: Ikjoon Jang; +Cc: Sebastian Reichel, open list:THERMAL, lkml

On Tue, Aug 11, 2020 at 2:59 PM Ikjoon Jang <ikjn@chromium.org> wrote:
>
> Current sbs-battery considers all smbus errors as diconnection events

disconnection

> when battery-detect pin isn't supplied, and restored to present state back
> on any successful transaction were made.

when any... is made

>
> This can leads

lead

> to unlimited state changes between present and !present
> when one unsupported command was requested and other following commands
> were successful, e.g. udev rules tries to read multiple properties.
>
> This patch checks battery presence by reading known good command to
> check battery existence.
>
> Signed-off-by: Ikjoon Jang <ikjn@chromium.org>
> ---
> v2: fix return value checking of sbs_get_battery_presence_and_health()
> ---
>  drivers/power/supply/sbs-battery.c | 26 +++++++++++++++++---------
>  1 file changed, 17 insertions(+), 9 deletions(-)
>
> diff --git a/drivers/power/supply/sbs-battery.c b/drivers/power/supply/sbs-battery.c
> index 6acb4ea25d2a..db964a470ebc 100644
> --- a/drivers/power/supply/sbs-battery.c
> +++ b/drivers/power/supply/sbs-battery.c
> @@ -878,10 +878,17 @@ static int sbs_get_property(struct power_supply *psy,
>         if (!chip->enable_detection)
>                 goto done;
>
> -       if (!chip->gpio_detect &&
> -               chip->is_present != (ret >= 0)) {
> -               sbs_update_presence(chip, (ret >= 0));
> -               power_supply_changed(chip->power_supply);
> +       if (!chip->gpio_detect && chip->is_present != (ret >=0)) {
> +               bool old_present = chip->is_present;
> +               union power_supply_propval val;
> +
> +               sbs_get_battery_presence_and_health(
> +                               client, POWER_SUPPLY_PROP_PRESENT, &val);
> +
> +               sbs_update_presence(chip, val.intval);

I don't think you can/should assume that val.intval will be set
correctly if the return value is negative (even if that's what the
functions currently do, it'd be too easy to accidentally change them).

So I still think you need to have:

ret = sbs_get_battery_presence_and_health...

sbs_update_presence(chip, !ret && val.intval);

> +
> +               if (old_present != chip->is_present)
> +                       power_supply_changed(chip->power_supply);
>         }
>
>  done:
> @@ -1067,11 +1074,12 @@ static int sbs_probe(struct i2c_client *client)
>          * to the battery.
>          */
>         if (!(force_load || chip->gpio_detect)) {
> -               rc = sbs_read_word_data(client, sbs_data[REG_STATUS].addr);
> -
> -               if (rc < 0) {
> -                       dev_err(&client->dev, "%s: Failed to get device status\n",
> -                               __func__);
> +               union power_supply_propval val;
> +               sbs_get_battery_presence_and_health(
> +                               client, POWER_SUPPLY_PROP_PRESENT, &val);
> +               if (!val.intval) {
> +                       dev_err(&client->dev, "Failed to get present status\n");
> +                       rc = -ENODEV;
>                         goto exit_psupply;
>                 }
>         }
> --
> 2.28.0.236.gb10cc79966-goog
>

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

end of thread, other threads:[~2020-08-13  4:06 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-11  6:59 [PATCH v2 2/2] power: supply: sbs-battery: don't assume every i2c errors as battery disconnect Ikjoon Jang
2020-08-13  4:06 ` Nicolas Boichat

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).