google_battery: lower charging current when Adaptive Charge is active

To improve thermal impact

Bug: 246810709
Signed-off-by: Jenny Ho <[email protected]>
Change-Id: Ib484143f7e4e224c02050908948b34cb8d76acf6
diff --git a/google_battery.c b/google_battery.c
index ad284ba..b722fc6 100644
--- a/google_battery.c
+++ b/google_battery.c
@@ -2912,13 +2912,7 @@
 	return new_deadline || rest_state != chg_health->rest_state;
 }
 
-/* cc_max in ua: capacity in mAh, rest_rate in deciPct */
-static int msc_logic_health_get_rate(const struct batt_chg_health *rest,
-				     int capacity_ma)
-{
-	return capacity_ma * rest->rest_rate * 10;
-}
-
+#define HEALTH_CHG_RATE_BEFORE_TRIGGER 80
 /* health based charging trade charging speed for battery cycle life. */
 static bool msc_logic_health(struct batt_drv *batt_drv)
 {
@@ -2927,6 +2921,7 @@
 	const ktime_t deadline = rest->rest_deadline;
 	enum chg_health_state rest_state = rest->rest_state;
 	const bool aon_enabled = rest->always_on_soc != -1;
+	const int capacity_ma = batt_drv->battery_capacity;
 	const ktime_t now = get_boot_sec();
 	int fv_uv = -1, cc_max = -1;
 	bool changed = false;
@@ -3015,9 +3010,8 @@
 
 done_exit:
 	if (rest_state == CHG_HEALTH_ACTIVE || rest_state == CHG_HEALTH_DONE) {
-		const int capacity_ma = batt_drv->battery_capacity;
-
-		cc_max = msc_logic_health_get_rate(rest, capacity_ma);
+		/* cc_max in ua: capacity in mAh, rest_rate in deciPct */
+		cc_max = capacity_ma * rest->rest_rate * 10;
 
 		/*
 		 * default FV_UV to the last charge tier since fv_uv will be
@@ -3028,6 +3022,8 @@
 		fv_uv = profile->volt_limits[profile->volt_nb_limits - 1];
 
 		/* TODO: make sure that we wakeup when we are close to ttf */
+	} else if (rest_state == CHG_HEALTH_ENABLED) {
+		cc_max = capacity_ma * rest->rest_rate_before_trigger * 10;
 	} else if (rest_state == CHG_HEALTH_PAUSE) {
 		/*
 		 * pause charging behavior when the the deadline is longer than
@@ -4771,7 +4767,7 @@
 /* Adaptive Charging */
 static int debug_chg_health_rest_rate_read(void *data, u64 *val)
 {
-	struct batt_drv *batt_drv = (struct batt_drv *)data;
+	struct batt_drv *batt_drv = data;
 
 	if (!batt_drv->psy)
 		return -EINVAL;
@@ -4783,7 +4779,7 @@
 /* Adaptive Charging */
 static int debug_chg_health_rest_rate_write(void *data, u64 val)
 {
-	struct batt_drv *batt_drv = (struct batt_drv *)data;
+	struct batt_drv *batt_drv = data;
 
 	if (!batt_drv->psy)
 		return -EINVAL;
@@ -4797,6 +4793,36 @@
 			debug_chg_health_rest_rate_read,
 			debug_chg_health_rest_rate_write, "%llu\n");
 
+
+/* Adaptive Charging */
+static int debug_chg_health_rest_rate_before_trigger_read(void *data, u64 *val)
+{
+	struct batt_drv *batt_drv = data;
+
+	if (!batt_drv->psy)
+		return -EINVAL;
+
+	*val = batt_drv->chg_health.rest_rate_before_trigger;
+	return 0;
+}
+
+/* Adaptive Charging */
+static int debug_chg_health_rest_rate_before_trigger_write(void *data, u64 val)
+{
+	struct batt_drv *batt_drv = data;
+
+	if (!batt_drv->psy)
+		return -EINVAL;
+
+	batt_drv->chg_health.rest_rate_before_trigger = val;
+	return 0;
+}
+
+/* Adaptive Charging */
+DEFINE_SIMPLE_ATTRIBUTE(debug_chg_health_rest_rate_before_trigger_fops,
+			debug_chg_health_rest_rate_before_trigger_read,
+			debug_chg_health_rest_rate_before_trigger_write, "%llu\n");
+
 /* Adaptive Charging */
 static int debug_chg_health_thr_soc_read(void *data, u64 *val)
 {
@@ -6828,6 +6854,8 @@
 			    &debug_chg_health_thr_soc_fops);
 	debugfs_create_file("chg_health_rest_rate", 0600, de, batt_drv,
 			    &debug_chg_health_rest_rate_fops);
+	debugfs_create_file("chg_health_rest_rate_before_trigger", 0600, de, batt_drv,
+			    &debug_chg_health_rest_rate_before_trigger_fops);
 	debugfs_create_file("chg_health_stage", 0600, de, batt_drv,
 			    &debug_chg_health_stage_fops);
 
@@ -8411,6 +8439,12 @@
 	if (ret < 0)
 		batt_drv->chg_health.rest_rate = 0;
 
+	ret = of_property_read_u32(batt_drv->device->of_node,
+				   "google,chg-rest-rate-before-trigger",
+				   &batt_drv->chg_health.rest_rate_before_trigger);
+	if (ret < 0)
+		batt_drv->chg_health.rest_rate_before_trigger = HEALTH_CHG_RATE_BEFORE_TRIGGER;
+
 	/* override setting google,battery-roundtrip = 0 in device tree */
 	batt_drv->disable_votes =
 		of_property_read_bool(node, "google,disable-votes");
diff --git a/google_bms.h b/google_bms.h
index 379ab99..228a3d6 100644
--- a/google_bms.h
+++ b/google_bms.h
@@ -322,6 +322,7 @@
 	ktime_t rest_deadline;	/* full by this in seconds */
 	ktime_t dry_run_deadline; /* full by this in seconds (prediction) */
 	int rest_rate;		/* centirate once enter */
+	int rest_rate_before_trigger;
 
 	enum chg_health_state rest_state;
 	int rest_cc_max;