power: supply: ab8500_fg: Break loop for measurement
authorLinus Walleij <linus.walleij@linaro.org>
Sat, 29 Jan 2022 00:49:18 +0000 (01:49 +0100)
committerSebastian Reichel <sebastian.reichel@collabora.com>
Fri, 11 Feb 2022 19:24:53 +0000 (20:24 +0100)
In the Samsung code tree we find that it can happen that this
measurement loop goes on for a long time, and it seems like a
good idea to break it after 70 iterations if it goes on for
too long.

Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
Signed-off-by: Sebastian Reichel <sebastian.reichel@collabora.com>
drivers/power/supply/ab8500_fg.c

index 236fd9f9d6f142d1d8b7b81d192dd4509d17496c..29896f09fd175cacc9c73858e4916fba752560b9 100644 (file)
@@ -45,6 +45,7 @@
 #define SEC_TO_SAMPLE(S)               (S * 4)
 
 #define NBR_AVG_SAMPLES                        20
+#define WAIT_FOR_INST_CURRENT_MAX      70
 
 #define LOW_BAT_CHECK_INTERVAL         (HZ / 16) /* 62.5 ms */
 
@@ -926,10 +927,18 @@ static int ab8500_fg_load_comp_volt_to_capacity(struct ab8500_fg *di)
                vbat_uv += ab8500_fg_bat_voltage(di);
                i++;
                usleep_range(5000, 6000);
-       } while (!ab8500_fg_inst_curr_done(di));
+       } while (!ab8500_fg_inst_curr_done(di) &&
+                i <= WAIT_FOR_INST_CURRENT_MAX);
+
+       if (i > WAIT_FOR_INST_CURRENT_MAX) {
+               dev_err(di->dev,
+                       "TIMEOUT: return capacity based on uncompensated measurement of VBAT\n");
+               goto calc_cap;
+       }
 
        ab8500_fg_inst_curr_finalize(di, &di->inst_curr_ua);
 
+calc_cap:
        di->vbat_uv = vbat_uv / i;
        res = ab8500_fg_battery_resistance(di);