google_battery: take the round value into ttf calculation

Bug: 160468282
Signed-off-by: Jenny Ho <[email protected]>
Signed-off-by: AleX Pelosi <[email protected]>
Change-Id: I42a32755bc06cefaf6585a88a98982f48d7727c7
Signed-off-by: Ken Tsou <[email protected]>
(cherry picked from commit 2b3269e17bff4fd00e88f6eb200c57da6bf2eb50)
diff --git a/google_battery.c b/google_battery.c
index 3aee743..dc09acf 100644
--- a/google_battery.c
+++ b/google_battery.c
@@ -546,11 +546,14 @@
 	return ssoc->ssoc_rl;
 }
 
+#define SOC_ROUND_BASE	0.5
+
 /* reported to userspace: call while holding batt_lock */
 static int ssoc_get_capacity(const struct batt_ssoc_state *ssoc)
 {
 	const qnum_t raw = ssoc_get_capacity_raw(ssoc);
-	return qnum_roundint(raw, 0.5);
+
+	return qnum_roundint(raw, SOC_ROUND_BASE);
 }
 
 /* ------------------------------------------------------------------------- */
@@ -574,11 +577,8 @@
 		  ssoc_uicurve_cstr(buff, sizeof(buff), ssoc_state->ssoc_curve),
 		  ssoc_state->rl_status);
 
-	if (log) {
-		logbuffer_log(log, "%s", ssoc_state->ssoc_state_cstr);
-	} else {
-		pr_info("%s\n", ssoc_state->ssoc_state_cstr);
-	}
+	logbuffer_log(log, "%s", ssoc_state->ssoc_state_cstr);
+	pr_debug("%s\n", ssoc_state->ssoc_state_cstr);
 }
 
 /* ------------------------------------------------------------------------- */
@@ -885,8 +885,9 @@
 
 static int batt_ttf_estimate(ktime_t *res, const struct batt_drv *batt_drv)
 {
-	int rc;
+	const qnum_t raw_full = ssoc_point_full - qnum_rconst(SOC_ROUND_BASE);
 	ktime_t estimate = batt_drv->ttf_stats.ttf_fake;
+	int rc;
 
 	if (batt_drv->ssoc_state.buck_enabled != 1)
 		return -EINVAL;
@@ -894,10 +895,21 @@
 	if (batt_drv->ttf_stats.ttf_fake != -1)
 		goto done;
 
+	/* TTF is 0 when UI shows 100% */
+	if (ssoc_get_capacity(&batt_drv->ssoc_state) == SSOC_FULL) {
+		estimate = 0;
+		goto done;
+	}
+
+	/*
+	 * example: 96.64% with SOC_ROUND_BASE = 0.5 -> UI = 97
+	 *    ttf = elap[96] * 0.36 + elap[97] + elap[98] +
+	 * 	    elap[99] * 0.5
+	 */
 	rc = ttf_soc_estimate(&estimate, &batt_drv->ttf_stats,
 			      &batt_drv->ce_data,
 			      ssoc_get_capacity_raw(&batt_drv->ssoc_state),
-			      ssoc_point_full);
+			      raw_full);
 	if (rc < 0)
 		estimate = -1;
 
diff --git a/google_ttf.c b/google_ttf.c
index 8a78fcc..7755d41 100644
--- a/google_ttf.c
+++ b/google_ttf.c
@@ -259,22 +259,29 @@
 {
 	const int ssoc_in = ce_data->charging_stats.ssoc_in;
 	const int end = qnum_toint(last);
-	const int frac = qnum_fracdgt(soc);
 	int i = qnum_toint(soc);
-	ktime_t estimate = 0;
-	int ret;
+	ktime_t elap, estimate = 0;
+	int ret, frac;
 
-	if (end > 100)
+	if (last > qnum_rconst(100) || last < soc)
 		return -EINVAL;
 
-	ret = ttf_elap(&estimate, i, stats, ce_data);
-	if (ret < 0)
-		return ret;
+	if (last == soc) {
+		*res = 0;
+		return 0;
+	}
 
-	/* add ttf_elap starting from i + 1 */
-	estimate = (estimate * (100 - frac)) / 100;
+	/* 100 - first 2 digits of the fractional part of starting soc if any */
+	frac = (int)qnum_nfracdgt(soc, 2);
+	if (frac) {
+
+		ret = ttf_elap(&elap, i, stats, ce_data);
+		if (ret == 0)
+			estimate += (elap * (100 - frac)) / 100;
+	}
+
+	/* ttf_elap starting from i + 1 until end */
 	for (i += 1; i < end; i++) {
-		ktime_t elap;
 
 		if (i >= ssoc_in && i < ce_data->last_soc) {
 			/* use real data if within charging event */
@@ -289,6 +296,14 @@
 		estimate += elap;
 	}
 
+	/* fist 2 digits of the fractional part of the last soc if any */
+	frac = (int)qnum_nfracdgt(last, 2);
+	if (frac) {
+		ret = ttf_elap(&elap, end, stats, ce_data);
+		if (ret == 0)
+			estimate += (elap * frac) / 100;
+	}
+
 	*res = estimate / 100;
 	return 0;
 }