google_battery: disable AC when BD triggers

Disable AdaptiveCharging when BatteryDefender triggers. Report in log
with vtier_idx equal to -5

Bug: 171086376
Test: trigger AC, trigger BD, check state
Signed-off-by: AleX Pelosi <[email protected]>
Change-Id: I1c062aafa522b471edfb4c75f21a3c45a0f9d0f4
Signed-off-by: Ken Tsou <[email protected]>
(cherry picked from commit 8fe4222a7a6ccc7dddc05fea2bfe4a24dc49cb9d)
diff --git a/google_battery.c b/google_battery.c
index fc0d20e..02766fe 100644
--- a/google_battery.c
+++ b/google_battery.c
@@ -1226,6 +1226,10 @@
 	bool aon_enabled = chg_health->always_on_soc != -1;
 
 	switch (rest_state) {
+	/* battery defender did it */
+	case CHG_HEALTH_BD_DISABLED:
+		tier_idx = GBMS_STATS_AC_TI_DEFENDER;
+		break;
 	/* user disabled with deadline */
 	case CHG_HEALTH_USER_DISABLED:
 		if (rest_deadline == CHG_DEADLINE_SETTING)
@@ -2090,7 +2094,8 @@
 	 * on disconnect batt_reset_rest_state() will set rest_state to
 	 * CHG_HEALTH_USER_DISABLED if the deadline is negative.
 	 */
-	if (rest_state == CHG_HEALTH_USER_DISABLED ||
+	if (rest_state == CHG_HEALTH_BD_DISABLED ||
+	    rest_state == CHG_HEALTH_USER_DISABLED ||
 	    rest_state == CHG_HEALTH_DISABLED ||
 	    rest_state == CHG_HEALTH_INACTIVE)
 		goto done_no_op;
@@ -2099,10 +2104,18 @@
 	if (rest_state == CHG_HEALTH_DONE)
 		goto done_exit;
 
+	/* disable AC because BD triggered */
+	if (batt_drv->batt_health == POWER_SUPPLY_HEALTH_OVERHEAT) {
+		rest_state = CHG_HEALTH_BD_DISABLED;
+		goto done_exit;
+	}
+
 	/*
 	 * ret < 0 right after plug-in or when the device is discharging due
 	 * to a large sysload or an underpowered adapter (or both). Current
-	 * strategy leaves everything as is (hoping) that the load is temporary
+	 * strategy leaves everything as is (hoping) that the load is temporary.
+	 * The estimate will be negative when BD is triggered and during the
+	 * debounce period.
 	 */
 	ret = batt_ttf_estimate(&ttf, batt_drv);
 	if (ret < 0)
@@ -2125,7 +2138,7 @@
 	 * TODO: consider adding a margin or debounce it.
 	 */
 	if (aon_enabled == false && rest_state == CHG_HEALTH_ACTIVE &&
-	    deadline > 0 && now + ttf > deadline) {
+	    deadline > 0 && ttf != -1 && now + ttf > deadline) {
 		rest_state = CHG_HEALTH_DISABLED;
 		goto done_exit;
 	}
@@ -4534,6 +4547,14 @@
 	batt_drv->fake_capacity = capacity;
 }
 
+static void gbatt_set_health(struct batt_drv *batt_drv, int health)
+{
+	batt_drv->batt_health = health;
+
+	/* disable health charging if in overheat */
+	if (health == POWER_SUPPLY_HEALTH_OVERHEAT)
+		msc_logic_health(batt_drv);
+}
 
 static int gbatt_get_property(struct power_supply *psy,
 				 enum power_supply_property psp,
@@ -4729,11 +4750,13 @@
 		break;
 
 	case POWER_SUPPLY_PROP_CAPACITY:
+		mutex_lock(&batt_drv->chg_lock);
 		if (val->intval != batt_drv->fake_capacity) {
 			gbatt_set_capacity(batt_drv, val->intval);
 			if (batt_drv->psy)
 				power_supply_changed(batt_drv->psy);
 		}
+		mutex_unlock(&batt_drv->chg_lock);
 		break;
 
 	case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
@@ -4746,11 +4769,13 @@
 			power_supply_changed(batt_drv->psy);
 		break;
 	case POWER_SUPPLY_PROP_HEALTH:
+		mutex_lock(&batt_drv->chg_lock);
 		if (batt_drv->batt_health != val->intval) {
-			batt_drv->batt_health = val->intval;
+			gbatt_set_health(batt_drv, val->intval);
 			if (batt_drv->psy)
 				power_supply_changed(batt_drv->psy);
 		}
+		mutex_unlock(&batt_drv->chg_lock);
 		break;
 	default:
 		ret = -EINVAL;