diff --git a/board/reef/battery.c b/board/reef/battery.c index 2b86f7f012..38801bd4be 100644 --- a/board/reef/battery.c +++ b/board/reef/battery.c @@ -10,6 +10,7 @@ #include "charge_state.h" #include "console.h" #include "ec_commands.h" +#include "gpio.h" #include "i2c.h" #include "util.h" @@ -230,3 +231,32 @@ DECLARE_CONSOLE_COMMAND(fastcharge, command_fastcharge, NULL); #endif /* CONFIG_CHARGER_PROFILE_OVERRIDE */ + +#ifdef CONFIG_BATTERY_PRESENT_CUSTOM +/* + * Physical detection of battery. + */ +enum battery_present battery_is_present(void) +{ + enum battery_present batt_pres; + int batt_status; + + /* The GPIO is low when the battery is physically present */ + batt_pres = gpio_get_level(GPIO_EC_BATT_PRES_L) ? BP_NO : BP_YES; + + /* + * Make sure battery status is implemented, I2C transactions are + * success & the battery status is Initialized to find out if it + * is a working battery and it is not in the cut-off mode. + * + * FETs are turned off after Power Shutdown time. + * The device will wake up when a voltage is applied to PACK. + * Battery status will be inactive until it is initialized. + */ + if (batt_pres == BP_YES && !battery_status(&batt_status)) + if (!(batt_status & STATUS_INITIALIZED)) + batt_pres = BP_NO; + + return batt_pres; +} +#endif diff --git a/board/reef/board.c b/board/reef/board.c index 692d522b23..87bcc263fa 100644 --- a/board/reef/board.c +++ b/board/reef/board.c @@ -268,6 +268,12 @@ const struct button_config buttons[CONFIG_BUTTON_COUNT] = { 30 * MSEC, 0}, }; +static const enum bd99955_charge_port + pd_port_to_bd99955_port[CONFIG_USB_PD_PORT_COUNT] = { + [0] = BD99955_CHARGE_PORT_VBUS, + [1] = BD99955_CHARGE_PORT_VCC, +}; + /* Called by APL power state machine when transitioning from G3 to S5 */ static void chipset_pre_init(void) { @@ -337,6 +343,23 @@ static void board_init(void) /* PP3300 needs to be enabled before TCPC init hooks */ DECLARE_HOOK(HOOK_INIT, board_init, HOOK_PRIO_FIRST); +int pd_snk_is_vbus_provided(int port) +{ + enum bd99955_charge_port bd99955_port; + + switch (port) { + case 0: + case 1: + bd99955_port = pd_port_to_bd99955_port[port]; + break; + default: + panic("Invalid charge port\n"); + break; + } + + return bd99955_is_vbus_provided(bd99955_port); +} + /** * Set active charge port -- only one port can be active at a time. * @@ -348,6 +371,7 @@ DECLARE_HOOK(HOOK_INIT, board_init, HOOK_PRIO_FIRST); int board_set_active_charge_port(int charge_port) { enum bd99955_charge_port bd99955_port; + static int initialized; /* charge port is a physical port */ int is_real_port = (charge_port >= 0 && @@ -360,15 +384,25 @@ int board_set_active_charge_port(int charge_port) CPRINTF("Skip enable p%d", charge_port); return EC_ERROR_INVAL; } + /* + * Reject charge port disable if our battery is critical and we + * have yet to initialize a charge port - continue to charge using + * charger ROM / POR settings. + */ + if (!initialized && + charge_port == CHARGE_PORT_NONE && + charge_get_percent() < 2) { + CPRINTS("Battery critical, don't disable charging"); + return -1; + } + CPRINTS("New chg p%d", charge_port); switch (charge_port) { case 0: - bd99955_port = BD99955_CHARGE_PORT_VBUS; - break; case 1: - bd99955_port = BD99955_CHARGE_PORT_VCC; + bd99955_port = pd_port_to_bd99955_port[charge_port]; break; case CHARGE_PORT_NONE: bd99955_port = BD99955_CHARGE_PORT_NONE; @@ -378,6 +412,8 @@ int board_set_active_charge_port(int charge_port) break; } + initialized = 1; + return bd99955_select_input_port(bd99955_port); } diff --git a/board/reef/board.h b/board/reef/board.h index b51dabf5ed..8f418c7af5 100644 --- a/board/reef/board.h +++ b/board/reef/board.h @@ -16,7 +16,7 @@ /* Battery */ #define CONFIG_BATTERY_CUT_OFF -#define CONFIG_BATTERY_PRESENT_GPIO GPIO_EC_BATT_PRES_L +#define CONFIG_BATTERY_PRESENT_CUSTOM #define CONFIG_BATTERY_SMART /* Charger */ diff --git a/board/reef/usb_pd_policy.c b/board/reef/usb_pd_policy.c index b972cb52e0..92a84c9b64 100644 --- a/board/reef/usb_pd_policy.c +++ b/board/reef/usb_pd_policy.c @@ -106,29 +106,6 @@ void typec_set_input_current_limit(int port, uint32_t max_ma, pd_send_host_event(PD_EVENT_POWER_CHANGE); } -int pd_snk_is_vbus_provided(int port) -{ - enum bd99955_charge_port bd99955_port; - - /* - * TODO: Add a compile-time mapping to prevent - * duplication of this code. - */ - switch (port) { - case 0: - bd99955_port = BD99955_CHARGE_PORT_VBUS; - break; - case 1: - bd99955_port = BD99955_CHARGE_PORT_VCC; - break; - default: - panic("Invalid charge port\n"); - break; - } - - return bd99955_is_vbus_provided(bd99955_port); -} - int pd_board_checks(void) { return EC_SUCCESS;