summaryrefslogtreecommitdiff
path: root/board/agah/charger_isl9241.c
diff options
context:
space:
mode:
Diffstat (limited to 'board/agah/charger_isl9241.c')
-rw-r--r--board/agah/charger_isl9241.c236
1 files changed, 131 insertions, 105 deletions
diff --git a/board/agah/charger_isl9241.c b/board/agah/charger_isl9241.c
index 6f041a7455..47e8261f78 100644
--- a/board/agah/charger_isl9241.c
+++ b/board/agah/charger_isl9241.c
@@ -1,8 +1,38 @@
-/* Copyright 2022 The Chromium OS Authors. All rights reserved.
+/* Copyright 2022 The ChromiumOS Authors
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
+/*
+ *
+ * We need to deal with plug / unplug of AC chargers:
+ *
+ * +---------+ +USB +---------+
+ * | BATTERY |------------>| BATTERY |
+ * | |<------------| +USB |
+ * +---------+ -USB +---------+
+ * | ^ | ^
+ * +BJ | | -BJ +BJ | | -BJ
+ * v | v |
+ * +---------+ +USB +---------+
+ * | BATTERY |------------>| BATTERY |
+ * | +BJ |<------------| +BJ+USB |
+ * +---------+ -USB +---------+
+ *
+ * Depending on available battery charge, power rating of the new charger, and
+ * the system power state, transition/throttling may or may not occur but
+ * switching chargers is handled as follows:
+ *
+ * 1. Detects a new charger or removal of an existing charger.
+ * 2. charge_manager_update_charge is called with new charger's info.
+ * 3. board_set_active_charge_port is called.
+ * 3.1 It triggers hard & soft throttling for AP & GPU.
+ * 3.2 It disable active port then enables the new port.
+ * 4. HOOK_POWER_SUPPLY_CHANGE is called. We disables hard throttling.
+ * 5. charger task wakes up on HOOK_POWER_SUPPLY_CHANGE, enables (or disables)
+ * bypass mode.
+ */
+
#include "common.h"
#include "charge_manager.h"
@@ -15,12 +45,13 @@
#include "gpio.h"
#include "hooks.h"
#include "stdbool.h"
+#include "throttle_ap.h"
#include "usbc_ppc.h"
#include "usb_pd.h"
#include "util.h"
-#define CPRINTF(format, args...) cprintf(CC_CHARGER, format, ## args)
-#define CPRINTS(format, args...) cprints(CC_CHARGER, format, ## args)
+#define CPRINTF(format, args...) cprintf(CC_CHARGER, format, ##args)
+#define CPRINTS(format, args...) cprints(CC_CHARGER, format, ##args)
/* Charger Chip Configuration */
const struct charger_config_t chg_chips[] = {
@@ -32,71 +63,42 @@ const struct charger_config_t chg_chips[] = {
};
BUILD_ASSERT(ARRAY_SIZE(chg_chips) == CHARGER_NUM);
-static int board_disable_bj_port(void)
+static int board_enable_bj_port(bool enable)
{
- gpio_set_level(GPIO_EN_PPVAR_BJ_ADP_L, 1);
- /* If the current port is BJ, disable bypass mode. */
- if (charge_manager_get_supplier() == CHARGE_SUPPLIER_DEDICATED)
- return charger_enable_bypass_mode(0, 0);
+ if (enable) {
+ if (gpio_get_level(GPIO_BJ_ADP_PRESENT_ODL))
+ return EC_ERROR_INVAL;
+ gpio_set_level(GPIO_EN_PPVAR_BJ_ADP_L, 0);
+ } else {
+ gpio_set_level(GPIO_EN_PPVAR_BJ_ADP_L, 1);
+ }
- CPRINTS("BJ power is disabled");
+ CPRINTS("BJ power is %sabled", enable ? "en" : "dis");
return EC_SUCCESS;
}
-static int board_enable_bj_port(void)
-{
- if (gpio_get_level(GPIO_BJ_ADP_PRESENT_ODL))
- return EC_ERROR_INVAL;
- gpio_set_level(GPIO_EN_PPVAR_BJ_ADP_L, 0);
-
- CPRINTS("BJ power is enabled");
-
- return charger_enable_bypass_mode(0, 1);
-}
-
-/*
- * TODO:
- *
- * When AC is being plugged in (including switching source port),
- * 1. Deassert NVIDIA_GPU_ACOFF_ODL.
- * 2. Call evaluate_d_notify.
- *
- * When AC is being lost,
- * 1. Assert NVIDIA_GPU_ACOFF_ODL.
- * 2. Set D-Notify to D5.
- * 3. Differ-call
- * a. Deassert NVIDIA_GPU_ACOFF_ODL.
- * b. evaluate_d_notify
- */
-static int board_throttle_ap_gpu(bool enable)
+static void board_throttle_ap_gpu(void)
{
- int rv = EC_SUCCESS;
-
- if (!chipset_in_state(CHIPSET_STATE_ON))
- return EC_SUCCESS;
-
- CPRINTS("TODO: %s to %s AP & GPU (%d)", rv ? "Failed" : "Succeeded",
- enable ? "throttle" : "unthrottle", rv);
-
- return rv;
+ throttle_ap(THROTTLE_ON, THROTTLE_HARD, THROTTLE_SRC_AC);
+ throttle_gpu(THROTTLE_ON, THROTTLE_HARD, THROTTLE_SRC_AC);
}
/* Disable all VBUS sink ports except <port>. <port> = -1 disables all ports. */
-static int board_disable_vbus_sink(int port)
+static int board_disable_other_vbus_sink(int except_port)
{
int i, r, rv = EC_SUCCESS;
for (i = 0; i < ppc_cnt; i++) {
- if (i == port)
+ if (i == except_port)
continue;
/*
* Do not return early if one fails otherwise we can get into a
* boot loop assertion failure.
*/
r = ppc_vbus_sink_enable(i, 0);
- CPRINTS("%s to disable sink path C%d (%d).",
- r ? "Failed" : "Succeeded", i, r);
+ if (r)
+ CPRINTS("Failed to disable sink path C%d (%d)", i, r);
rv |= r;
}
@@ -104,77 +106,74 @@ static int board_disable_vbus_sink(int port)
}
/* Minimum battery SoC required for switching source port. */
-#define MIN_BATT_FOR_SWITCHING_SOURCE_PORT 1
+#define MIN_BATT_FOR_SWITCHING_SOURCE_PORT 1
/*
- * It should also work on POR with/without a battery:
- *
- * 1. EC gathers power info of all ports.
- * 2. Identify the highest power port.
- * 3. If
- * 1. battery soc = 0% --> Exit
- * 2. BJ_ADP_PRESENT_ODL = 1 --> Exit
- * 3. highest power port == active port --> Exit
- * 4. If
- * 1. in S0, throttle AP & GPU to the DC rating.
- * 5. Turn off the current active port.
- * 6. Turn on the highest power port.
- * 7. If
- * 1. in S0, throttle AP & GPU back.
- *
- * TODO: Are the following cases covered?
- * 1. Two AC adapters are plugged. Then, the active adapter is removed.
- *
* TODO: Recover from incomplete execution:
- * 1. Failed to turn on/off PPC.
*/
int board_set_active_charge_port(int port)
{
- enum charge_supplier supplier = charge_manager_get_supplier();
+ enum charge_supplier active_supplier = charge_manager_get_supplier();
int active_port = charge_manager_get_active_charge_port();
- CPRINTS("Changing charge port to %d (current port=%d supplier=%d)",
- port, active_port, supplier);
+ CPRINTS("Switching charger from P%d (supplier=%d) to P%d", active_port,
+ active_supplier, port);
if (port == CHARGE_PORT_NONE) {
CPRINTS("Disabling all charger ports");
- board_throttle_ap_gpu(1);
-
- board_disable_bj_port();
- board_disable_vbus_sink(-1);
+ board_enable_bj_port(false);
+ board_disable_other_vbus_sink(-1);
return EC_SUCCESS;
}
+ /* Return on invalid or no-op call. */
if (port < 0 || CHARGE_PORT_COUNT <= port) {
return EC_ERROR_INVAL;
} else if (port == active_port) {
return EC_SUCCESS;
} else if (board_vbus_source_enabled(port)) {
/* Don't charge from a USBC source port */
- CPRINTS("Don't enable C%d. It's sourcing.", port);
+ CPRINTS("Don't enable P%d. It's sourcing.", port);
return EC_ERROR_INVAL;
}
/*
+ * If we're in S0, throttle AP and GPU. They'll be unthrottled when
+ * a port/supply switch completes (via HOOK_POWER_SUPPLY_CHANGE).
+ *
+ * If we're running currently on a battery (active_supplier == NONE), we
+ * don't need to throttle because we're not disabling any port.
+ */
+ if (chipset_in_state(CHIPSET_STATE_ON) &&
+ active_supplier != CHARGE_SUPPLIER_NONE)
+ board_throttle_ap_gpu();
+
+ /*
+ * We're here for the two cases:
+ * 1. A new charger was connected.
+ * 2. One charger was disconnected and we're switching to another.
+ */
+
+ /*
* We need to check the battery if we're switching a source port. If
* we're just starting up or no AC was previously plugged, we shouldn't
* check the battery. Both cases can be caught by supplier == NONE.
*/
- if (supplier != CHARGE_SUPPLIER_NONE) {
+ if (active_supplier != CHARGE_SUPPLIER_NONE) {
if (charge_get_percent() < MIN_BATT_FOR_SWITCHING_SOURCE_PORT)
return EC_ERROR_NOT_POWERED;
}
/* Turn off other ports' sink paths before enabling requested port. */
- if (port == CHARGE_PORT_TYPEC0 || port == CHARGE_PORT_TYPEC1) {
+ if (is_pd_port(port)) {
/*
- * BJ port is on POR. So, we need to turn it off even if we're
- * not previously on BJ.
+ * BJ port is enabled on start-up. So, we need to turn it off
+ * even if we were not previously on BJ.
*/
- board_disable_bj_port();
- if (board_disable_vbus_sink(port))
+ board_enable_bj_port(false);
+ if (board_disable_other_vbus_sink(port))
return EC_ERROR_UNCHANGED;
/* Enable requested USBC charge port. */
@@ -187,26 +186,21 @@ int board_set_active_charge_port(int port)
* We can't proceed unless both ports are successfully
* disconnected as sources.
*/
- if (board_disable_vbus_sink(-1))
+ if (board_disable_other_vbus_sink(-1))
return EC_ERROR_UNKNOWN;
- board_enable_bj_port();
+ board_enable_bj_port(true);
}
- /* Switching port is complete. Turn off throttling. */
- if (supplier != CHARGE_SUPPLIER_NONE)
- board_throttle_ap_gpu(0);
-
- CPRINTS("New charger p%d", port);
+ CPRINTS("New charger P%d", port);
return EC_SUCCESS;
}
-void board_set_charge_limit(int port, int supplier, int charge_ma,
- int max_ma, int charge_mv)
+void board_set_charge_limit(int port, int supplier, int charge_ma, int max_ma,
+ int charge_mv)
{
- charge_set_input_current_limit(MAX(charge_ma,
- CONFIG_CHARGER_INPUT_CURRENT),
- charge_mv);
+ charge_set_input_current_limit(
+ MAX(charge_ma, CONFIG_CHARGER_INPUT_CURRENT), charge_mv);
}
static const struct charge_port_info bj_power = {
@@ -216,32 +210,62 @@ static const struct charge_port_info bj_power = {
};
/* Debounce time for BJ plug/unplug */
-#define BJ_DEBOUNCE_MS 1000
+#define BJ_DEBOUNCE_MS CONFIG_EXTPOWER_DEBOUNCE_MS
+
+int board_should_charger_bypass(void)
+{
+ return charge_manager_get_active_charge_port() == DEDICATED_CHARGE_PORT;
+}
-static void bj_connect_deferred(void)
+static void bj_connect(void)
{
static int8_t bj_connected = -1;
- const struct charge_port_info *pi = NULL;
int connected = !gpio_get_level(GPIO_BJ_ADP_PRESENT_ODL);
+ /* Debounce */
if (connected == bj_connected)
return;
- if (connected)
- pi = &bj_power;
+ bj_connected = connected;
+ CPRINTS("BJ %sconnected", connected ? "" : "dis");
charge_manager_update_charge(CHARGE_SUPPLIER_DEDICATED,
- DEDICATED_CHARGE_PORT, pi);
- bj_connected = connected;
- CPRINTS("BJ %s", connected ? "connected" : "disconnected");
+ DEDICATED_CHARGE_PORT,
+ connected ? &bj_power : NULL);
}
-DECLARE_DEFERRED(bj_connect_deferred);
+DECLARE_DEFERRED(bj_connect);
+/* This handler shouldn't be needed if ACOK from isl9241 is working. */
void bj_present_interrupt(enum gpio_signal signal)
{
- hook_call_deferred(&bj_connect_deferred_data, BJ_DEBOUNCE_MS * MSEC);
+ hook_call_deferred(&bj_connect_data, BJ_DEBOUNCE_MS * MSEC);
}
+void ac_change(void)
+{
+ /*
+ * Serialize. We don't handle USB-C here because we'll get a
+ * notification from TCPC.
+ */
+ hook_call_deferred(&bj_connect_data, 0);
+}
+DECLARE_HOOK(HOOK_AC_CHANGE, ac_change, HOOK_PRIO_DEFAULT);
+
+static void power_supply_changed(void)
+{
+ /*
+ * We've switched to a new charge port (or no port). Hardware throttles
+ * can be removed now. Software throttles may stay enabled and change
+ * as the situation changes.
+ */
+ throttle_ap(THROTTLE_OFF, THROTTLE_HARD, THROTTLE_SRC_AC);
+ /*
+ * Unthrottling GPU is done through a deferred call scheduled when it
+ * was throttled.
+ */
+}
+DECLARE_HOOK(HOOK_POWER_SUPPLY_CHANGE, power_supply_changed, HOOK_PRIO_DEFAULT);
+
static void bj_state_init(void)
{
/*
@@ -253,6 +277,8 @@ static void bj_state_init(void)
charge_manager_update_charge(j, i, NULL);
}
- bj_connect_deferred();
+ bj_connect();
+
+ isl9241_set_ac_prochot(CHARGER_SOLO, AGAH_AC_PROCHOT_CURRENT_MA);
}
DECLARE_HOOK(HOOK_INIT, bj_state_init, HOOK_PRIO_INIT_CHARGE_MANAGER + 1);