Allow logging of Adaptive Charging settings when not connected to power

Bug: 165833837
Change-Id: If11433b728b811beb28b296ed4fcf32a33b9d5bc
Signed-off-by: Stephane Lee <[email protected]>
Signed-off-by: Ken Tsou <[email protected]>
(cherry picked from commit 760384ffb7ce165b19efcffcacc7c6e43179b1fe)
diff --git a/google_battery.c b/google_battery.c
index 601fae3..bac0d68 100644
--- a/google_battery.c
+++ b/google_battery.c
@@ -68,6 +68,11 @@
 /* Initial data of history cycle count */
 #define HCC_DEFAULT_DELTA_CYCLE_CNT	25
 
+/* Interval value used when health is settings disabled when not running */
+#define CHG_DEADLINE_SETTING -1
+/* Internal value used when health is settings disabled while running */
+#define CHG_DEADLINE_SETTING_STOP -2
+
 #undef MODULE_PARAM_PREFIX
 #define MODULE_PARAM_PREFIX	"androidboot."
 #define DEV_SN_LENGTH		20
@@ -1151,11 +1156,11 @@
 	switch (rest_state) {
 	/* user disabled with deadline */
 	case CHG_HEALTH_USER_DISABLED:
-		if (rest_deadline == -1)
+		if (rest_deadline == CHG_DEADLINE_SETTING)
 			tier_idx = GBMS_STATS_AC_TI_SETTING;
 		else if (rest_deadline == 0)
 			tier_idx = GBMS_STATS_AC_TI_PLUG;
-		else
+		else /* CHG_DEADLINE_SETTING_STOP */
 			tier_idx = GBMS_STATS_AC_TI_DISABLE;
 		break;
 	/* missed the deadline, TODO: log the deadline */
@@ -1454,7 +1459,7 @@
 			 ce_data->ce_health.rest_deadline,
 			 ce_data->ce_health.always_on_soc);
 
-	/* tier stats only when we have data or in verbose */
+	/* no additional tier stats when vti is invalid */
 	if (vti == GBMS_STATS_AC_TI_INVALID)
 		return len;
 
@@ -1645,6 +1650,17 @@
 
 /* ------------------------------------------------------------------------- */
 
+static inline void batt_reset_rest_state(struct batt_chg_health *chg_health)
+{
+	/* NOTE: should not reset always_on_soc */
+	chg_health->rest_state = CHG_HEALTH_INACTIVE;
+	chg_health->rest_cc_max = -1;
+	chg_health->rest_fv_uv = -1;
+
+	if (chg_health->rest_deadline > 0)
+		chg_health->rest_deadline = 0;
+}
+
 /* should not reset rl state */
 static inline void batt_reset_chg_drv_state(struct batt_drv *batt_drv)
 {
@@ -1672,11 +1688,7 @@
 	/* stats */
 	batt_drv->msc_state = -1;
 	/* health */
-	/* NOTE: should not reset always_on_soc */
-	batt_drv->chg_health.rest_state = CHG_HEALTH_INACTIVE;
-	batt_drv->chg_health.rest_deadline = 0;
-	batt_drv->chg_health.rest_cc_max = -1;
-	batt_drv->chg_health.rest_fv_uv = -1;
+	batt_reset_rest_state(&batt_drv->chg_health);
 }
 
 /*
@@ -1912,6 +1924,7 @@
  * for logging, userspace should use
  *   deadline == 0 on fast replug (leave initial deadline ok)
  *   deadline == -1 when the feature is disabled
+ *       if charge health was active/enabled, set to -2
  *   deadline == absolute requested deadline (if always_on is set)
  * return true if there was a change
  */
@@ -1925,7 +1938,12 @@
 	if (deadline_s < 0) {
 		new_deadline = chg_health->rest_deadline != deadline_s;
 		chg_health->rest_state = CHG_HEALTH_USER_DISABLED;
-		chg_health->rest_deadline = -1;
+
+		if (chg_health->rest_deadline > 0) /* was active */
+			chg_health->rest_deadline = CHG_DEADLINE_SETTING_STOP;
+		else
+			chg_health->rest_deadline = CHG_DEADLINE_SETTING;
+
 	/* disabled with replug */
 	} else if (deadline_s == 0) {
 		new_deadline = chg_health->rest_deadline != deadline_s;
@@ -2341,6 +2359,7 @@
 
 		/* change curve before changing the state */
 		ssoc_change_curve(&batt_drv->ssoc_state, SSOC_UIC_TYPE_DSG);
+		batt_drv->chg_health.rest_deadline = 0;
 		batt_reset_chg_drv_state(batt_drv);
 		batt_update_cycle_count(batt_drv);
 		batt_rl_reset(batt_drv);
@@ -3523,7 +3542,8 @@
 	deadline_s = simple_strtoll(buf, NULL, 10);
 
 	mutex_lock(&batt_drv->chg_lock);
-	if (!batt_drv->ssoc_state.buck_enabled) {
+	/* Let deadline < 0 pass to set stats */
+	if (!batt_drv->ssoc_state.buck_enabled && deadline_s >= 0) {
 		mutex_unlock(&batt_drv->chg_lock);
 		return -EINVAL;
 	}
@@ -4604,12 +4624,6 @@
 	if (ret < 0)
 		batt_drv->chg_health.rest_rate = 0;
 
-	/* health based charging, state */
-	batt_drv->chg_health.rest_state = CHG_HEALTH_INACTIVE;
-	batt_drv->chg_health.rest_deadline = 0;
-	batt_drv->chg_health.rest_cc_max = -1;
-	batt_drv->chg_health.rest_fv_uv = -1;
-
 	/* override setting google,battery-roundtrip = 0 in device tree */
 	batt_drv->disable_votes =
 		of_property_read_bool(node, "google,disable-votes");