google_battery: TRICKLE-DEFEND

drop the recharge threshold to 90% or lower
after the first recharge cycle

Bug: 170232321
Change-Id: I9cd23b791db7e50d4733b0cdcb791e6bed855bd6
Signed-off-by: Ken Tsou <[email protected]>
(cherry picked from commit a2e6cbed2b53e9eaa9dceae339d40fcaef9888e9)
diff --git a/google_battery.c b/google_battery.c
index 001599a..385f9bf 100644
--- a/google_battery.c
+++ b/google_battery.c
@@ -46,6 +46,7 @@
 #define DEFAULT_BATT_FAKE_CAPACITY		50
 #define DEFAULT_BATT_UPDATE_INTERVAL		30000
 #define DEFAULT_BATT_DRV_RL_SOC_THRESHOLD	97
+#define DEFAULT_BD_RL_SOC_THRESHOLD		90
 #define DEFAULT_HIGH_TEMP_UPDATE_THRESHOLD	550
 
 #define MSC_ERROR_UPDATE_INTERVAL		5000
@@ -147,6 +148,10 @@
 	int rl_soc_threshold;
 	enum batt_rl_status rl_status;
 
+	/* trickle defender */
+	int bd_rl_soc_threshold;
+	int bd_trickle_cnt;
+
 	/* buff */
 	char ssoc_state_cstr[SSOC_STATE_BUF_SZ];
 };
@@ -877,7 +882,10 @@
  */
 static void batt_rl_reset(struct batt_drv *batt_drv)
 {
-	batt_drv->ssoc_state.rl_status = BATT_RL_STATUS_NONE;
+	struct batt_ssoc_state *ssoc_state = &batt_drv->ssoc_state;
+
+	ssoc_state->rl_status = BATT_RL_STATUS_NONE;
+	ssoc_state->bd_trickle_cnt = 0;
 }
 
 /*
@@ -888,7 +896,7 @@
 static void batt_rl_update_status(struct batt_drv *batt_drv)
 {
 	struct batt_ssoc_state *ssoc_state = &batt_drv->ssoc_state;
-	int soc;
+	int soc, rl_soc_threshold;
 
 	/* already in _RECHARGE or _NONE, done */
 	if (ssoc_state->rl_status != BATT_RL_STATUS_DISCHARGE)
@@ -898,13 +906,17 @@
 		return;
 	/* recharge logic work on real soc */
 	soc = ssoc_get_real(ssoc_state);
-	if (soc > ssoc_state->rl_soc_threshold)
+	rl_soc_threshold = (ssoc_state->bd_trickle_cnt > 0) ?
+		ssoc_state->bd_rl_soc_threshold : ssoc_state->rl_soc_threshold;
+	if (soc > rl_soc_threshold)
 		return;
 
 	/* change state (will restart charge) on trigger */
 	ssoc_state->rl_status = BATT_RL_STATUS_RECHARGE;
 	if (batt_drv->psy)
 		power_supply_changed(batt_drv->psy);
+
+	ssoc_state->bd_trickle_cnt++;
 }
 
 /* ------------------------------------------------------------------------- */
@@ -4951,6 +4963,12 @@
 		batt_drv->ssoc_state.rl_soc_threshold =
 				DEFAULT_BATT_DRV_RL_SOC_THRESHOLD;
 
+	ret = of_property_read_u32(node, "google,bd-recharge-soc-threshold",
+				   &batt_drv->ssoc_state.bd_rl_soc_threshold);
+	if (ret < 0)
+		batt_drv->ssoc_state.bd_rl_soc_threshold =
+				DEFAULT_BD_RL_SOC_THRESHOLD;
+
 	ret = of_property_read_u32(node, "google,ssoc-delta",
 				   &batt_drv->ssoc_state.ssoc_delta);
 	if (ret < 0)