summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Glass <sjg@chromium.org>2023-05-03 13:49:19 -0600
committerChromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com>2023-05-05 17:37:53 +0000
commit7aa2988fb94ae0f8b791aaac7867df3e89fc093b (patch)
treeedc6ad2385d5af0b01a377e02159222c9f96c2f8
parentba0dfab0c9c17d94b9a34c70df07d53511ef1948 (diff)
downloadchrome-ec-7aa2988fb94ae0f8b791aaac7867df3e89fc093b.tar.gz
charger: Move latter part of charge-deciding into a func
This goes through various steps to decide what charge state to use, using a goto to get to the final state. Move the code into a function instead, so we can use a return. This makes no functional change. BUG=b:218332694 TEST=zmake build dev-posix Change-Id: Ibb46b172c5560e64cac8861f4330ddbc5b35649f Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/4500688 Tested-by: Simon Glass <sjg@chromium.org> Reviewed-by: Tomasz Michalec <tmichalec@google.com> Commit-Queue: Simon Glass <sjg@chromium.org>
-rw-r--r--common/charge_state_v2.c94
1 files changed, 50 insertions, 44 deletions
diff --git a/common/charge_state_v2.c b/common/charge_state_v2.c
index eac1023e0b..abfba5eae5 100644
--- a/common/charge_state_v2.c
+++ b/common/charge_state_v2.c
@@ -1777,6 +1777,55 @@ static void process_battery_present_change(const struct charger_info *info,
hook_notify(HOOK_BATTERY_SOC_CHANGE);
}
+/* Decide on the charge state we are in */
+static void decide_charge_state(int *need_staticp)
+{
+ if (!curr.ac) {
+ set_charge_state(ST_DISCHARGE);
+ return;
+ }
+
+ /* Okay, we're on AC and we should have a battery. */
+
+ /* Used for factory tests. */
+ if (get_chg_ctrl_mode() != CHARGE_CONTROL_NORMAL) {
+ set_charge_state(ST_IDLE);
+ return;
+ }
+
+ /* If the battery is not responsive, try to wake it up. */
+ if (!(curr.batt.flags & BATT_FLAG_RESPONSIVE)) {
+ wakeup_battery(need_staticp);
+ return;
+ }
+
+ /*
+ * When the battery voltage is lower than voltage_min,precharge first to
+ * protect the battery
+ */
+ if (IS_ENABLED(CONFIG_BATTERY_LOW_VOLTAGE_PROTECTION)) {
+ if (!(curr.batt.flags & BATT_FLAG_BAD_VOLTAGE) &&
+ curr.batt.voltage <= batt_info->voltage_min) {
+ deep_charge_battery(need_staticp);
+ return;
+ }
+
+ /*
+ * Finished deep charge before timeout. Clear the flag so that
+ * we can do deep charge again (when it's deeply discharged
+ * again).
+ */
+ if ((curr.batt.flags & BATT_FLAG_DEEP_CHARGE)) {
+ curr.batt.flags &= ~BATT_FLAG_DEEP_CHARGE;
+ }
+ }
+ /* The battery is responding. Yay. Try to use it. */
+
+ revive_battery(need_staticp);
+
+ set_charge_state(ST_CHARGE);
+}
+
/* Main loop */
void charger_task(void *u)
{
@@ -1883,50 +1932,7 @@ void charger_task(void *u)
/* Don't let the battery hurt itself. */
battery_critical = shutdown_on_critical_battery();
- if (!curr.ac) {
- set_charge_state(ST_DISCHARGE);
- goto wait_for_it;
- }
-
- /* Okay, we're on AC and we should have a battery. */
-
- /* Used for factory tests. */
- if (get_chg_ctrl_mode() != CHARGE_CONTROL_NORMAL) {
- set_charge_state(ST_IDLE);
- goto wait_for_it;
- }
-
- /* If the battery is not responsive, try to wake it up. */
- if (!(curr.batt.flags & BATT_FLAG_RESPONSIVE)) {
- wakeup_battery(&need_static);
- goto wait_for_it;
- }
-
- /*
- * When the battery voltage is lower than voltage_min,precharge
- * first to protect the battery
- */
- if (IS_ENABLED(CONFIG_BATTERY_LOW_VOLTAGE_PROTECTION)) {
- if (!(curr.batt.flags & BATT_FLAG_BAD_VOLTAGE) &&
- (curr.batt.voltage <= batt_info->voltage_min)) {
- deep_charge_battery(&need_static);
- goto wait_for_it;
- }
-
- /*
- * Finished deep charge before timeout. Clear the flag
- * so that we can do deep charge again (when it's deeply
- * discharged again).
- */
- if ((curr.batt.flags & BATT_FLAG_DEEP_CHARGE)) {
- curr.batt.flags &= ~BATT_FLAG_DEEP_CHARGE;
- }
- }
- /* The battery is responding. Yay. Try to use it. */
-
- revive_battery(&need_static);
-
- set_charge_state(ST_CHARGE);
+ decide_charge_state(&need_static);
wait_for_it:
if (IS_ENABLED(CONFIG_CHARGER_PROFILE_OVERRIDE) &&