Merge cherrypicks of ['partner-android-review.googlesource.com/2926709', 'partner-android-review.googlesource.com/3004348'] into sparse-12859464-L96400030008637458.
SPARSE_CHANGE: I5b7db63b70ab65c6fa5434ad2eacf66fad0ee3b4
SPARSE_CHANGE: I4451084eb0f1054124f56123fc53b2f7bf78d8b9


Change-Id: I3cb7a9debbc96793986fda1ade608c93030fc163
Signed-off-by: Coastguard Worker <[email protected]>
diff --git a/google_battery.c b/google_battery.c
index 4543540..05a41bd 100644
--- a/google_battery.c
+++ b/google_battery.c
@@ -3618,7 +3618,6 @@
 	return false;
 }
 
-
 /*
  * for logging, userspace should use
  *   deadline == 0 on fast replug (leave initial deadline ok)
@@ -3628,6 +3627,7 @@
  * return true if there was a change
  */
 static bool batt_health_set_chg_deadline(struct batt_chg_health *chg_health,
+					 long long ttf_with_margin,
 					 long long deadline_s)
 {
 	enum chg_health_state rest_state = chg_health->rest_state;
@@ -3659,10 +3659,15 @@
 
 	} else { /* enabled from any previous state */
 		const ktime_t rest_deadline = get_boot_sec() + deadline_s;
+		enum chg_health_state new_rest_state = CHG_HEALTH_ENABLED;
 
 		/* ->always_on SOC overrides the deadline */
 		new_deadline = chg_health->rest_deadline != rest_deadline;
-		chg_health->rest_state = CHG_HEALTH_ENABLED;
+
+		if (rest_deadline < ttf_with_margin)
+			new_rest_state = CHG_HEALTH_DISABLED;
+
+		chg_health->rest_state = new_rest_state;
 		chg_health->rest_deadline = rest_deadline;
 	}
 
@@ -3682,7 +3687,7 @@
 	const ktime_t now = get_boot_sec();
 	int fv_uv = -1, cc_max = -1;
 	bool changed = false;
-	ktime_t ttf = 0;
+	ktime_t ttf = 0, safety_margin = 0;
 	int ret;
 
 	/* move to ENABLED if INACTIVE when aon_enabled is set */
@@ -3733,6 +3738,9 @@
 		goto done_exit;
 	}
 
+	if (batt_drv->health_safety_margin > 0)
+		safety_margin = batt_drv->health_safety_margin;
+
 	/*
 	 * rest_state here is either ENABLED or ACTIVE, transition to DISABLED
 	 * when the deadline cannot be met with the current rate. set a new
@@ -3743,8 +3751,9 @@
 	 * from CHG_HEALTH_USER_DISABLED.
 	 * TODO: consider adding a margin or debounce it.
 	 */
-	if (aon_enabled == false && rest_state == CHG_HEALTH_ACTIVE &&
-	    deadline > 0 && ttf != -1 && now + ttf > deadline) {
+	if (aon_enabled == false &&
+	    (rest_state == CHG_HEALTH_ACTIVE || rest_state == CHG_HEALTH_ENABLED) &&
+	    deadline > 0 && ttf != -1 && now + ttf + safety_margin > deadline) {
 		rest_state = CHG_HEALTH_DISABLED;
 		goto done_exit;
 	}
@@ -7128,6 +7137,7 @@
 	struct power_supply *psy = container_of(dev, struct power_supply, dev);
 	struct batt_drv *batt_drv =(struct batt_drv *)
 					power_supply_get_drvdata(psy);
+	ktime_t ttf_with_margin = 0;
 	long long deadline_s;
 	bool changed;
 
@@ -7141,7 +7151,12 @@
 		return -EINVAL;
 	}
 
-	changed = batt_health_set_chg_deadline(&batt_drv->chg_health,
+	if (batt_ttf_estimate(&ttf_with_margin, batt_drv) < 0)
+		ttf_with_margin = LLONG_MAX;
+	else if (batt_drv->health_safety_margin > 0)
+		ttf_with_margin += batt_drv->health_safety_margin;
+
+	changed = batt_health_set_chg_deadline(&batt_drv->chg_health, (long long)ttf_with_margin,
 					       deadline_s);
 	mutex_unlock(&batt_drv->chg_lock);
 
diff --git a/max1720x_battery.c b/max1720x_battery.c
index f85e030..4833bad 100644
--- a/max1720x_battery.c
+++ b/max1720x_battery.c
@@ -2754,11 +2754,11 @@
 
 		} else if (ce->cable_in) {
 			if (ce->estimate_state == ESTIMATE_PENDING)
-				cancel_delayed_work_sync(&ce->settle_timer);
+				cancel_delayed_work(&ce->settle_timer);
 
 			/* race with batt_ce_capacityfiltered_work() */
-			batt_ce_stop_estimation(ce, ESTIMATE_NONE);
 			batt_ce_dump_data(ce, chip->ce_log);
+			batt_ce_stop_estimation(ce, ESTIMATE_NONE);
 			ce->cable_in = false;
 		}
 		mutex_unlock(&ce->batt_ce_lock);
diff --git a/max77779_fg.c b/max77779_fg.c
index 61e60b8..e3f26b0 100644
--- a/max77779_fg.c
+++ b/max77779_fg.c
@@ -1722,11 +1722,11 @@
 
 		} else if (ce->cable_in) {
 			if (ce->estimate_state == ESTIMATE_PENDING)
-				cancel_delayed_work_sync(&ce->settle_timer);
+				cancel_delayed_work(&ce->settle_timer);
 
 			/* race with batt_ce_capacityfiltered_work() */
-			batt_ce_stop_estimation(ce, ESTIMATE_NONE);
 			batt_ce_dump_data(ce, chip->ce_log);
+			batt_ce_stop_estimation(ce, ESTIMATE_NONE);
 			ce->cable_in = false;
 		}
 		mutex_unlock(&ce->batt_ce_lock);