diff options
author | Tom Wai-Hong Tam <waihong@google.com> | 2018-04-27 13:11:43 -0700 |
---|---|---|
committer | chrome-bot <chrome-bot@chromium.org> | 2018-05-09 14:40:02 -0700 |
commit | aafc4f5d1aa5ee0f67cf0e391acc68309eaa8c92 (patch) | |
tree | e23bf05ac731090beb01e907096ec332f034e96a | |
parent | 52726d7f7f6948626f2481e999afc9aa5246166b (diff) | |
download | chrome-ec-aafc4f5d1aa5ee0f67cf0e391acc68309eaa8c92.tar.gz |
cheza: Support PD and charging
Port 0:
TCPC: ANX3429
PPC: SN5S330
BC1.2: PI3USB9281
Port 1:
TCPC: PS8751
Power switch (sink): NX5P3290
Power switch (source): NX20P5090
BC1.2: PI3USB9281
Charger: ISL9238
BRANCH=none
BUG=b:74395451
TEST=make buildall -j
TEST=Did "gpioset EN_PP5000_A 1" before the folllowing tests:
* Plugged adapter to port-0/port-1/both and saw charging
* Plugged USB device to port-0/port-1/both and saw sourcing VBUS
* Plugged adapter to one port and USB device to another port
* Plugged USB disk to port-0 and booted into kernel
* When AP off, not sourcing VBUS to USB device
* Rebooting AP still works
Change-Id: Icde5e24c2cda3d0f2046486528a210af84befcca
Signed-off-by: Tom Wai-Hong Tam <waihong@google.com>
Reviewed-on: https://chromium-review.googlesource.com/969701
Commit-Ready: Wai-Hong Tam <waihong@google.com>
Tested-by: Wai-Hong Tam <waihong@google.com>
Reviewed-by: Wai-Hong Tam <waihong@google.com>
-rw-r--r-- | board/cheza/battery.c | 28 | ||||
-rw-r--r-- | board/cheza/board.c | 347 | ||||
-rw-r--r-- | board/cheza/board.h | 65 | ||||
-rw-r--r-- | board/cheza/build.mk | 2 | ||||
-rw-r--r-- | board/cheza/ec.tasklist | 8 | ||||
-rw-r--r-- | board/cheza/gpio.inc | 21 | ||||
-rw-r--r-- | board/cheza/usb_pd_policy.c | 417 | ||||
-rw-r--r-- | common/usb_charger.c | 4 | ||||
-rw-r--r-- | include/config.h | 6 |
9 files changed, 885 insertions, 13 deletions
diff --git a/board/cheza/battery.c b/board/cheza/battery.c new file mode 100644 index 0000000000..cc302a3e1a --- /dev/null +++ b/board/cheza/battery.c @@ -0,0 +1,28 @@ +/* Copyright 2018 The Chromium OS Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + * + * Battery pack vendor provided charging profile + */ + +#include "battery.h" + +/* Battery info for rev-0 */ +static const struct battery_info info = { + /* Copied from Lux */ + .voltage_max = 8800, + .voltage_normal = 7700, + .voltage_min = 6100, + .precharge_current = 256, /* mA */ + .start_charging_min_c = 0, + .start_charging_max_c = 45, + .charging_min_c = 0, + .charging_max_c = 45, + .discharging_min_c = -10, + .discharging_max_c = 60, +}; + +const struct battery_info *battery_get_info(void) +{ + return &info; +} diff --git a/board/cheza/board.c b/board/cheza/board.c index 42515b1d71..b3ebcf8bb9 100644 --- a/board/cheza/board.c +++ b/board/cheza/board.c @@ -7,8 +7,14 @@ #include "adc_chip.h" #include "button.h" +#include "charge_manager.h" +#include "charge_state.h" #include "chipset.h" #include "extpower.h" +#include "driver/ppc/sn5s330.h" +#include "driver/tcpm/anx74xx.h" +#include "driver/tcpm/ps8xxx.h" +#include "driver/tcpm/tcpci.h" #include "gpio.h" #include "hooks.h" #include "i2c.h" @@ -16,7 +22,29 @@ #include "pi3usb9281.h" #include "power.h" #include "power_button.h" +#include "system.h" #include "switch.h" +#include "task.h" +#include "usb_charge.h" +#include "usb_mux.h" +#include "usb_pd.h" +#include "usbc_ppc.h" +#include "util.h" + +#define CPRINTS(format, args...) cprints(CC_USBCHARGE, format, ## args) +#define CPRINTF(format, args...) cprintf(CC_USBCHARGE, format, ## args) + +#define USB_PD_PORT_ANX3429 0 +#define USB_PD_PORT_PS8751 1 + +/* Forward declaration */ +static void tcpc_alert_event(enum gpio_signal signal); +static void vbus0_evt(enum gpio_signal signal); +static void vbus1_evt(enum gpio_signal signal); +static void usb0_evt(enum gpio_signal signal); +static void usb1_evt(enum gpio_signal signal); +static void ppc_interrupt(enum gpio_signal signal); +static void anx74xx_cable_det_interrupt(enum gpio_signal signal); #include "gpio_list.h" @@ -25,6 +53,81 @@ #define DA9313_I2C_ADDR 0xd0 #define CHARGER_I2C_ADDR 0x12 +/* GPIO Interrupt Handlers */ +static void tcpc_alert_event(enum gpio_signal signal) +{ + /* Exchange status with TCPCs */ + host_command_pd_send_status(PD_CHARGE_NO_CHANGE); +} + +static void vbus0_handler(void) +{ + /* VBUS present GPIO is inverted */ + usb_charger_vbus_change(0, !gpio_get_level(GPIO_USB_C0_VBUS_DET_L)); + task_wake(TASK_ID_PD_C0); +} +DECLARE_DEFERRED(vbus0_handler); + +static void vbus1_handler(void) +{ + /* VBUS present GPIO is inverted */ + usb_charger_vbus_change(1, !gpio_get_level(GPIO_USB_C1_VBUS_DET_L)); + task_wake(TASK_ID_PD_C1); +} +DECLARE_DEFERRED(vbus1_handler); + +static void vbus0_evt(enum gpio_signal signal) +{ + hook_call_deferred(&vbus0_handler_data, 0); +} + +static void vbus1_evt(enum gpio_signal signal) +{ + hook_call_deferred(&vbus1_handler_data, 0); +} + +static void usb0_evt(enum gpio_signal signal) +{ + task_set_event(TASK_ID_USB_CHG_P0, USB_CHG_EVENT_BC12, 0); +} + +static void usb1_evt(enum gpio_signal signal) +{ + task_set_event(TASK_ID_USB_CHG_P1, USB_CHG_EVENT_BC12, 0); +} + +static void anx74xx_cable_det_handler(void) +{ + int cable_det = gpio_get_level(GPIO_USB_C0_CABLE_DET); + int reset_n = gpio_get_level(GPIO_USB_C0_PD_RST_R_L); + + /* + * A cable_det low->high transition was detected. If following the + * debounce time, cable_det is high, and reset_n is low, then ANX3429 is + * currently in standby mode and needs to be woken up. Set the + * TCPC_RESET event which will bring the ANX3429 out of standby + * mode. Setting this event is gated on reset_n being low because the + * ANX3429 will always set cable_det when transitioning to normal mode + * and if in normal mode, then there is no need to trigger a tcpc reset. + */ + if (cable_det && !reset_n) + task_set_event(TASK_ID_PD_C0, PD_EVENT_TCPC_RESET, 0); +} +DECLARE_DEFERRED(anx74xx_cable_det_handler); + +static void anx74xx_cable_det_interrupt(enum gpio_signal signal) +{ + /* debounce for 2 msec */ + hook_call_deferred(&anx74xx_cable_det_handler_data, (2 * MSEC)); +} + +static void ppc_interrupt(enum gpio_signal signal) +{ + int port = (signal == GPIO_USB_C0_SWCTL_INT_ODL) ? 0 : 1; + + sn5s330_interrupt(port); +} + /* Wake-up pins for hibernate */ const enum gpio_signal hibernate_wake_pins[] = { GPIO_LID_OPEN, @@ -75,6 +178,55 @@ const struct i2c_port_t i2c_ports[] = { }; const unsigned int i2c_ports_used = ARRAY_SIZE(i2c_ports); +/* Power Path Controller */ +const struct ppc_config_t ppc_chips[] = { + { + .i2c_port = I2C_PORT_TCPC0, + .i2c_addr = SN5S330_ADDR0, + .drv = &sn5s330_drv + }, + /* + * Port 1 uses two power switches instead: + * NX5P3290: to source VBUS + * NX20P5090: to sink VBUS (charge battery) + * which are controlled directly by EC GPIOs. + */ +}; +const unsigned int ppc_cnt = ARRAY_SIZE(ppc_chips); + +/* TCPC mux configuration */ +const struct tcpc_config_t tcpc_config[CONFIG_USB_PD_PORT_COUNT] = { + [USB_PD_PORT_ANX3429] = {I2C_PORT_TCPC0, 0x50, &anx74xx_tcpm_drv, + TCPC_ALERT_ACTIVE_LOW}, + [USB_PD_PORT_PS8751] = {I2C_PORT_TCPC1, 0x16, &ps8xxx_tcpm_drv, + TCPC_ALERT_ACTIVE_LOW}, +}; + +struct usb_mux usb_muxes[CONFIG_USB_PD_PORT_COUNT] = { + { + .port_addr = USB_PD_PORT_ANX3429, + .driver = &anx74xx_tcpm_usb_mux_driver, + .hpd_update = &anx74xx_tcpc_update_hpd_status, + }, + { + .port_addr = USB_PD_PORT_PS8751, + .driver = &tcpci_tcpm_usb_mux_driver, + .hpd_update = &ps8xxx_tcpc_update_hpd_status, + } +}; + +/* BC1.2 */ +struct pi3usb9281_config pi3usb9281_chips[] = { + { + .i2c_port = I2C_PORT_TCPC0, + }, + { + .i2c_port = I2C_PORT_TCPC1, + }, +}; +BUILD_ASSERT(ARRAY_SIZE(pi3usb9281_chips) == + CONFIG_BC12_DETECT_PI3USB9281_CHIP_COUNT); + /* Initialize board. */ static void board_init(void) { @@ -97,10 +249,205 @@ static void board_init(void) */ i2c_write8(I2C_PORT_POWER, DA9313_I2C_ADDR, 0x02, 0x34); + /* Enable BC1.2 VBUS detection */ + gpio_enable_interrupt(GPIO_USB_C0_VBUS_DET_L); + gpio_enable_interrupt(GPIO_USB_C1_VBUS_DET_L); + + /* Enable BC1.2 interrupts */ + gpio_enable_interrupt(GPIO_USB_C0_BC12_INT_L); + gpio_enable_interrupt(GPIO_USB_C1_BC12_INT_L); + /* * Increase AdapterCurrentLimit{1,2} to max (6080mA) */ i2c_write16(I2C_PORT_POWER, CHARGER_I2C_ADDR, 0x3B, 0x17c0); i2c_write16(I2C_PORT_POWER, CHARGER_I2C_ADDR, 0x3F, 0x17c0); + } DECLARE_HOOK(HOOK_INIT, board_init, HOOK_PRIO_DEFAULT); + +void board_tcpc_init(void) +{ + int port; + + /* Only reset TCPC if not sysjump */ + if (!system_jumped_to_this_image()) { + /* TODO(crosbug.com/p/61098): How long do we need to wait? */ + board_reset_pd_mcu(); + } + + /* Enable PPC interrupts */ + gpio_enable_interrupt(GPIO_USB_C0_SWCTL_INT_ODL); + + /* Enable TCPC interrupts */ + gpio_enable_interrupt(GPIO_USB_C0_PD_INT_ODL); + gpio_enable_interrupt(GPIO_USB_C1_PD_INT_ODL); + + /* Enable CABLE_DET interrupt for ANX3429 wake from standby */ + gpio_enable_interrupt(GPIO_USB_C0_CABLE_DET); + + /* + * Initialize HPD to low; after sysjump SOC needs to see + * HPD pulse to enable video path + */ + for (port = 0; port < CONFIG_USB_PD_PORT_COUNT; port++) { + const struct usb_mux *mux = &usb_muxes[port]; + + mux->hpd_update(port, 0, 0); + } +} +DECLARE_HOOK(HOOK_INIT, board_tcpc_init, HOOK_PRIO_INIT_I2C+1); + +/** + * Power on (or off) a single TCPC. + * minimum on/off delays are included. + * + * @param port Port number of TCPC. + * @param mode 0: power off, 1: power on. + */ +void board_set_tcpc_power_mode(int port, int mode) +{ + if (port != USB_PD_PORT_ANX3429) + return; + + if (mode) { + gpio_set_level(GPIO_EN_USB_C0_TCPC_PWR, 1); + msleep(ANX74XX_PWR_H_RST_H_DELAY_MS); + gpio_set_level(GPIO_USB_C0_PD_RST_R_L, 1); + } else { + gpio_set_level(GPIO_USB_C0_PD_RST_R_L, 0); + msleep(ANX74XX_RST_L_PWR_L_DELAY_MS); + gpio_set_level(GPIO_EN_USB_C0_TCPC_PWR, 0); + msleep(ANX74XX_PWR_L_PWR_H_DELAY_MS); + } +} + +void board_reset_pd_mcu(void) +{ + /* Assert reset */ + gpio_set_level(GPIO_USB_C0_PD_RST_R_L, 0); + gpio_set_level(GPIO_USB_C1_PD_RST_ODL, 0); + + msleep(MAX(1, ANX74XX_RST_L_PWR_L_DELAY_MS)); + gpio_set_level(GPIO_USB_C1_PD_RST_ODL, 1); + /* Disable TCPC0 (anx3429) power */ + gpio_set_level(GPIO_EN_USB_C0_TCPC_PWR, 0); + + msleep(ANX74XX_PWR_L_PWR_H_DELAY_MS); + board_set_tcpc_power_mode(USB_PD_PORT_ANX3429, 1); +} + +int board_vbus_sink_enable(int port, int enable) +{ + if (port == USB_PD_PORT_ANX3429) { + /* Port 0 is controlled by a PPC SN5S330 */ + return ppc_vbus_sink_enable(port, enable); + } else if (port == USB_PD_PORT_PS8751) { + /* Port 1 is controlled by a power switch NX20P5090 */ + gpio_set_level(GPIO_EN_USB_C1_CHARGE_EC_L, !enable); + return EC_SUCCESS; + } + return EC_ERROR_INVAL; +} + +int board_is_sourcing_vbus(int port) +{ + if (port == USB_PD_PORT_ANX3429) { + /* Port 0 is controlled by a PPC SN5S330 */ + return ppc_is_sourcing_vbus(port); + } else if (port == USB_PD_PORT_PS8751) { + /* Port 1 is controlled by a power switch NX5P3290 */ + return gpio_get_level(GPIO_EN_USB_C1_5V_OUT); + } + return EC_ERROR_INVAL; +} + +void board_overcurrent_event(int port) +{ + /* TODO(waihong): Notify AP? */ + CPRINTS("p%d: overcurrent!", port); +} + +int board_set_active_charge_port(int port) +{ + int is_real_port = (port >= 0 && + port < CONFIG_USB_PD_PORT_COUNT); + int i; + int rv; + + if (!is_real_port && port != CHARGE_PORT_NONE) + return EC_ERROR_INVAL; + + CPRINTS("New chg p%d", port); + + if (port == CHARGE_PORT_NONE) { + /* Disable all ports. */ + for (i = 0; i < CONFIG_USB_PD_PORT_COUNT; i++) { + rv = board_vbus_sink_enable(i, 0); + if (rv) { + CPRINTS("Disabling p%d sink path failed.", i); + return rv; + } + } + + return EC_SUCCESS; + } + + /* Check if the port is sourcing VBUS. */ + if (board_is_sourcing_vbus(port)) { + CPRINTF("Skip enable p%d", port); + return EC_ERROR_INVAL; + } + + /* + * Turn off the other ports' sink path FETs, before enabling the + * requested charge port. + */ + for (i = 0; i < CONFIG_USB_PD_PORT_COUNT; i++) { + if (i == port) + continue; + + if (board_vbus_sink_enable(i, 0)) + CPRINTS("p%d: sink path disable failed.", i); + } + + /* Enable requested charge port. */ + if (board_vbus_sink_enable(port, 1)) { + CPRINTS("p%d: sink path enable failed."); + return EC_ERROR_UNKNOWN; + } + + return EC_SUCCESS; +} + +void board_set_charge_limit(int port, int supplier, int charge_ma, + int max_ma, int charge_mv) +{ + /* + * Ignore lower charge ceiling on PD transition if our battery is + * critical, as we may brownout. + */ + if (supplier == CHARGE_SUPPLIER_PD && + charge_ma < 1500 && + charge_get_percent() < CONFIG_CHARGER_MIN_BAT_PCT_FOR_POWER_ON) { + CPRINTS("Using max ilim %d", max_ma); + charge_ma = max_ma; + } + + charge_set_input_current_limit(MAX(charge_ma, + CONFIG_CHARGER_INPUT_CURRENT), + charge_mv); +} + +uint16_t tcpc_get_alert_status(void) +{ + uint16_t status = 0; + + if (!gpio_get_level(GPIO_USB_C0_PD_INT_ODL)) + status |= PD_STATUS_TCPC_ALERT_0; + if (!gpio_get_level(GPIO_USB_C1_PD_INT_ODL)) + status |= PD_STATUS_TCPC_ALERT_1; + + return status; +} + diff --git a/board/cheza/board.h b/board/cheza/board.h index feec3a9468..ce7046fea7 100644 --- a/board/cheza/board.h +++ b/board/cheza/board.h @@ -36,6 +36,64 @@ #define CONFIG_LID_SWITCH #define CONFIG_EXTPOWER_GPIO +/* Battery */ +#define CONFIG_BATTERY_PRESENT_GPIO GPIO_BATT_PRES_ODL +#define CONFIG_BATTERY_SMART + +/* Charger */ +#define CONFIG_CHARGER +#define CONFIG_CHARGER_V2 +#define CONFIG_CHARGE_MANAGER +#define CONFIG_CHARGER_ISL9238 +#define CONFIG_CHARGE_RAMP_HW +#define CONFIG_USB_CHARGER + +/* TODO(b/79163120): Use correct charger values, copied from Lux for rev-0 */ +#define CONFIG_CHARGER_INPUT_CURRENT 512 +#define CONFIG_CHARGER_MIN_BAT_PCT_FOR_POWER_ON 2 +#define CONFIG_CHARGER_SENSE_RESISTOR 10 +#define CONFIG_CHARGER_SENSE_RESISTOR_AC 20 + +/* BC 1.2 Charger */ +#define CONFIG_BC12_DETECT_PI3USB9281 +#define CONFIG_BC12_DETECT_PI3USB9281_CHIP_COUNT 2 + +/* USB */ +#define CONFIG_USB_POWER_DELIVERY +#define CONFIG_CMD_PD_CONTROL +#define CONFIG_USB_PD_ALT_MODE +#define CONFIG_USB_PD_ALT_MODE_DFP +#define CONFIG_USB_PD_DISCHARGE_PPC +#define CONFIG_USB_PD_DUAL_ROLE +#define CONFIG_USB_PD_DUAL_ROLE_AUTO_TOGGLE +#define CONFIG_USB_PD_LOGGING +#define CONFIG_USB_PD_MAX_SINGLE_SOURCE_CURRENT TYPEC_RP_3A0 +#define CONFIG_USB_PD_PORT_COUNT 2 +#define CONFIG_USB_PD_TCPC_LOW_POWER +#define CONFIG_USB_PD_TCPM_ANX3429 +#define CONFIG_USB_PD_TCPM_PS8751 +#define CONFIG_USB_PD_TCPM_MUX +#define CONFIG_USB_PD_TCPM_TCPCI +#define CONFIG_USB_PD_TRY_SRC +#define CONFIG_USB_PD_VBUS_DETECT_TCPC +#define CONFIG_USB_PD_5V_EN_CUSTOM +#define CONFIG_USBC_PPC_SN5S330 +#define CONFIG_USBC_SS_MUX +#define CONFIG_USBC_SS_MUX_DFP_ONLY +#define CONFIG_USBC_VCONN +#define CONFIG_USBC_VCONN_SWAP + +/* TODO(b/79163120): Use correct PD delay values, copied from Lux for rev-0 */ +#define PD_POWER_SUPPLY_TURN_ON_DELAY 30000 /* us */ +#define PD_POWER_SUPPLY_TURN_OFF_DELAY 250000 /* us */ +#define PD_VCONN_SWAP_DELAY 5000 /* us */ + +/* TODO(b/79163120): Use correct PD power values, copied from Lux for rev-0 */ +#define PD_OPERATING_POWER_MW 15000 +#define PD_MAX_POWER_MW 45000 +#define PD_MAX_CURRENT_MA 3000 +#define PD_MAX_VOLTAGE_MV 20000 + #define CONFIG_CHIPSET_SDM845 #define CONFIG_CHIPSET_RESET_HOOK #define CONFIG_POWER_COMMON @@ -70,6 +128,13 @@ enum adc_channel { void board_set_switchcap(int asserted); +/* Custom function to indicate if sourcing VBUS */ +int board_is_sourcing_vbus(int port); +/* Enable VBUS sink for a given port */ +int board_vbus_sink_enable(int port, int enable); +/* Reset all TCPCs. */ +void board_reset_pd_mcu(void); + #endif /* !defined(__ASSEMBLER__) */ #endif /* __CROS_EC_BOARD_H */ diff --git a/board/cheza/build.mk b/board/cheza/build.mk index 92f46b88d9..7fcb2acffd 100644 --- a/board/cheza/build.mk +++ b/board/cheza/build.mk @@ -10,4 +10,4 @@ CHIP:=npcx CHIP_FAMILY:=npcx7 CHIP_VARIANT:=npcx7m7wb -board-y=board.o +board-y=battery.o board.o usb_pd_policy.o diff --git a/board/cheza/ec.tasklist b/board/cheza/ec.tasklist index fa12c7f617..06acec6971 100644 --- a/board/cheza/ec.tasklist +++ b/board/cheza/ec.tasklist @@ -22,6 +22,12 @@ #define CONFIG_TASK_LIST \ TASK_ALWAYS(HOOKS, hook_task, NULL, LARGER_TASK_STACK_SIZE) \ + TASK_ALWAYS(USB_CHG_P0, usb_charger_task, NULL, TASK_STACK_SIZE) \ + TASK_ALWAYS(USB_CHG_P1, usb_charger_task, NULL, TASK_STACK_SIZE) \ TASK_NOTEST(CHIPSET, chipset_task, NULL, LARGER_TASK_STACK_SIZE) \ + TASK_ALWAYS(CHARGER, charger_task, NULL, LARGER_TASK_STACK_SIZE) \ + TASK_NOTEST(PDCMD, pd_command_task, NULL, TASK_STACK_SIZE) \ TASK_ALWAYS(HOSTCMD, host_command_task, NULL, LARGER_TASK_STACK_SIZE) \ - TASK_ALWAYS(CONSOLE, console_task, NULL, LARGER_TASK_STACK_SIZE) + TASK_ALWAYS(CONSOLE, console_task, NULL, LARGER_TASK_STACK_SIZE) \ + TASK_ALWAYS(PD_C0, pd_task, NULL, LARGER_TASK_STACK_SIZE) \ + TASK_ALWAYS(PD_C1, pd_task, NULL, LARGER_TASK_STACK_SIZE) diff --git a/board/cheza/gpio.inc b/board/cheza/gpio.inc index 8e3394d586..fec65254c0 100644 --- a/board/cheza/gpio.inc +++ b/board/cheza/gpio.inc @@ -8,6 +8,17 @@ /* Declare symbolic names for all the GPIOs that we care about. * Note: Those with interrupt handlers must be declared first. */ +/* USB-C interrupts */ +GPIO_INT(USB_C0_PD_INT_ODL, PIN(A, 0), GPIO_INT_FALLING, tcpc_alert_event) /* Interrupt from port-0 TCPC */ +GPIO_INT(USB_C1_PD_INT_ODL, PIN(F, 5), GPIO_INT_FALLING, tcpc_alert_event) /* Interrupt from port-1 TCPC */ +GPIO_INT(USB_C0_SWCTL_INT_ODL, PIN(0, 3), GPIO_INT_FALLING, ppc_interrupt) /* Interrupt from port-0 PPC */ +GPIO_INT(USB_C0_BC12_INT_L, PIN(6, 1), GPIO_INT_FALLING, usb0_evt) /* Interrupt from port-0 BC1.2 */ +GPIO_INT(USB_C1_BC12_INT_L, PIN(8, 2), GPIO_INT_FALLING, usb1_evt) /* Interrupt from port-1 BC1.2 */ +GPIO_INT(USB_C0_VBUS_DET_L, PIN(6, 2), GPIO_INT_BOTH | GPIO_PULL_UP, vbus0_evt) /* BC1.2 VBUS detection on port-0 */ +GPIO_INT(USB_C1_VBUS_DET_L, PIN(8, 3), GPIO_INT_BOTH | GPIO_PULL_UP, vbus1_evt) /* BC1.2 VBUS detection on port-1 */ +GPIO_INT(USB_C0_CABLE_DET, PIN(3, 7), GPIO_INT_RISING, anx74xx_cable_det_interrupt) /* Cable detection from port-0 TCPC */ + +/* System interrupts */ /* * The ACOK_OD is also a PMIC power-on trigger, resulting some side-efforts. * Check the bug (b/78035750) for details. The next hardware rev will fix it. @@ -90,13 +101,6 @@ GPIO(USB_C1_HS_MUX_SEL, PIN(B, 7), GPIO_OUT_LOW) /* L:D1(hub), H:D2(AP) */ GPIO(USB_C0_PD_RST_R_L, PIN(F, 1), GPIO_OUT_HIGH) /* Port-0 TCPC chip reset */ GPIO(EN_USB_C0_TCPC_PWR, PIN(6, 0), GPIO_OUT_LOW) /* Port-0 TCPC power enable */ -/* USB-C port-0 interrupts */ -GPIO(USB_C0_VBUS_DET_L, PIN(6, 2), GPIO_INPUT | GPIO_PULL_UP) /* BC1.2 VBUS detection on port-0 */ -GPIO(USB_C0_BC12_INT_L, PIN(6, 1), GPIO_INPUT) /* Interrupt from port-0 BC1.2 */ -GPIO(USB_C0_PD_INT_ODL, PIN(A, 0), GPIO_INPUT) /* Interrupt from port-0 TCPC */ -GPIO(USB_C0_CABLE_DET, PIN(3, 7), GPIO_INPUT) /* Cable detection from port-0 TCPC */ -GPIO(USB_C0_SWCTL_INT_ODL, PIN(0, 3), GPIO_INPUT) /* Interrupt from port-0 PPC */ - /* USB-C port-1 controls */ GPIO(USB_C1_PD_RST_ODL, PIN(E, 4), GPIO_ODR_HIGH) /* Port-1 TCPC chip reset */ GPIO(EN_USB_C1_5V_OUT, PIN(C, 0), GPIO_OUT_LOW) /* Port-1 power switch 5V output */ @@ -104,9 +108,6 @@ GPIO(EN_USB_C1_3A, PIN(5, 6), GPIO_OUT_LOW) /* Port-1 power switch 3A GPIO(EN_USB_C1_CHARGE_EC_L, PIN(B, 1), GPIO_OUT_LOW) /* Port-1 enable charging */ /* USB-C port-1 interrupts */ -GPIO(USB_C1_VBUS_DET_L, PIN(8, 3), GPIO_INPUT | GPIO_PULL_UP) /* BC1.2 VBUS detection on port-1 */ -GPIO(USB_C1_BC12_INT_L, PIN(8, 2), GPIO_INPUT) /* Interrupt from port-1 BC1.2 */ -GPIO(USB_C1_PD_INT_ODL, PIN(F, 5), GPIO_INPUT) /* Interrupt from port-1 TCPC */ GPIO(USB_C1_DP_HPD, PIN(9, 6), GPIO_INPUT) /* DP HPD from port-1 TCPC */ GPIO(USB_C1_OC_ODL, PIN(D, 7), GPIO_INPUT) /* Port-1 power switch over-current */ diff --git a/board/cheza/usb_pd_policy.c b/board/cheza/usb_pd_policy.c new file mode 100644 index 0000000000..cf2d5e7428 --- /dev/null +++ b/board/cheza/usb_pd_policy.c @@ -0,0 +1,417 @@ +/* Copyright 2018 The Chromium OS Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "charge_manager.h" +#include "console.h" +#include "gpio.h" +#include "system.h" +#include "usb_mux.h" +#include "usbc_ppc.h" +#include "util.h" + +#define CPRINTS(format, args...) cprints(CC_USBCHARGE, format, ## args) +#define CPRINTF(format, args...) cprintf(CC_USBCHARGE, format, ## args) + +#define PDO_FIXED_FLAGS (PDO_FIXED_DUAL_ROLE | PDO_FIXED_DATA_SWAP |\ + PDO_FIXED_COMM_CAP) + +/* TODO(waihong): Fill in correct source and sink capabilities */ +const uint32_t pd_src_pdo[] = { + PDO_FIXED(5000, 1500, PDO_FIXED_FLAGS), +}; +const int pd_src_pdo_cnt = ARRAY_SIZE(pd_src_pdo); +const uint32_t pd_src_pdo_max[] = { + PDO_FIXED(5000, 3000, PDO_FIXED_FLAGS), +}; +const int pd_src_pdo_max_cnt = ARRAY_SIZE(pd_src_pdo_max); + +const uint32_t pd_snk_pdo[] = { + PDO_FIXED(5000, 500, PDO_FIXED_FLAGS), + PDO_BATT(4750, 21000, 15000), + PDO_VAR(4750, 21000, 3000), +}; +const int pd_snk_pdo_cnt = ARRAY_SIZE(pd_snk_pdo); + +int pd_board_checks(void) +{ + return EC_SUCCESS; +} + +int pd_check_data_swap(int port, int data_role) +{ + /* Always allow data swap */ + return 1; +} + +void pd_check_dr_role(int port, int dr_role, int flags) +{ + /* If UFP, try to switch to DFP */ + if ((flags & PD_FLAGS_PARTNER_DR_DATA) && + dr_role == PD_ROLE_UFP && + system_get_image_copy() != SYSTEM_IMAGE_RO) + pd_request_data_swap(port); +} + +int pd_check_power_swap(int port) +{ + /* + * Allow power swap as long as we are acting as a dual role device, + * otherwise assume our role is fixed (not in S0 or console command + * to fix our role). + */ + return pd_get_dual_role() == PD_DRP_TOGGLE_ON ? 1 : 0; +} + +void pd_check_pr_role(int port, int pr_role, int flags) +{ + /* + * If partner is dual-role power and dualrole toggling is on, consider + * if a power swap is necessary. + */ + if ((flags & PD_FLAGS_PARTNER_DR_POWER) && + pd_get_dual_role() == PD_DRP_TOGGLE_ON) { + /* + * If we are a sink and partner is not externally powered, then + * swap to become a source. If we are source and partner is + * externally powered, swap to become a sink. + */ + int partner_extpower = flags & PD_FLAGS_PARTNER_EXTPOWER; + + if ((!partner_extpower && pr_role == PD_ROLE_SINK) || + (partner_extpower && pr_role == PD_ROLE_SOURCE)) + pd_request_power_swap(port); + } +} + +int pd_check_vconn_swap(int port) +{ + /* TODO(waihong): Check any case we do not allow. */ + return 1; +} + +void pd_execute_data_swap(int port, int data_role) +{ + /* Do nothing */ +} + +int pd_is_valid_input_voltage(int mv) +{ + return 1; +} + +static uint8_t vbus_en[CONFIG_USB_PD_PORT_COUNT]; +static uint8_t vbus_rp[CONFIG_USB_PD_PORT_COUNT] = {TYPEC_RP_1A5, TYPEC_RP_1A5}; + +static void board_vbus_update_source_current(int port) +{ + if (port == 0) { + /* + * Port 0 is controlled by a USB-C PPC SN5S330. + */ + ppc_set_vbus_source_current_limit(port, vbus_rp[port]); + ppc_vbus_source_enable(port, vbus_en[port]); + } else if (port == 1) { + /* + * Port 1 is controlled by a USB-C current-limited power + * switch, NX5P3290. Change the GPIO driving the load switch. + * + * 1.5 vs 3.0 A limit is controlled by a dedicated gpio. + * If the GPIO is asserted, it shorts a n-MOSFET to put a + * 16.5k resistance (2x 33k in parallel) on the NX5P3290 load + * switch ILIM pin, setting a minimum OCP current of 3100 mA. + * If the GPIO is deasserted, the n-MOSFET is open that makes + * a single 33k resistor on ILIM, setting a minimum OCP + * current of 1505 mA. + */ + gpio_set_level(GPIO_EN_USB_C1_3A, + vbus_rp[port] == TYPEC_RP_3A0 ? 1 : 0); + gpio_set_level(GPIO_EN_USB_C1_5V_OUT, vbus_en[port]); + } +} + +void pd_power_supply_reset(int port) +{ + int prev_en; + + prev_en = vbus_en[port]; + + /* Disable VBUS */ + vbus_en[port] = 0; + board_vbus_update_source_current(port); + + /* Enable discharge if we were previously sourcing 5V */ + if (prev_en) + pd_set_vbus_discharge(port, 1); + +#ifdef CONFIG_USB_PD_MAX_SINGLE_SOURCE_CURRENT + /* Give back the current quota we are no longer using */ + charge_manager_source_port(port, 0); +#endif /* defined(CONFIG_USB_PD_MAX_SINGLE_SOURCE_CURRENT) */ + + /* notify host of power info change */ + pd_send_host_event(PD_EVENT_POWER_CHANGE); +} + +int pd_set_power_supply_ready(int port) +{ + /* Disable charging */ + board_vbus_sink_enable(port, 0); + + pd_set_vbus_discharge(port, 0); + + /* Provide VBUS */ + vbus_en[port] = 1; + board_vbus_update_source_current(port); + + /* Ensure we advertise the proper available current quota */ + charge_manager_source_port(port, 1); + + /* notify host of power info change */ + pd_send_host_event(PD_EVENT_POWER_CHANGE); + + return EC_SUCCESS; /* we are ready */ +} + +void pd_transition_voltage(int idx) +{ + /* No-operation: we are always 5V */ +} + +int board_vbus_source_enabled(int port) +{ + return vbus_en[port]; +} + +void typec_set_source_current_limit(int port, int rp) +{ + vbus_rp[port] = rp; + board_vbus_update_source_current(port); +} + +int pd_snk_is_vbus_provided(int port) +{ + return !gpio_get_level(port ? GPIO_USB_C1_VBUS_DET_L : + GPIO_USB_C0_VBUS_DET_L); +} + +/* ----------------- Vendor Defined Messages ------------------ */ +const struct svdm_response svdm_rsp = { + .identity = NULL, + .svids = NULL, + .modes = NULL, +}; + +int pd_custom_vdm(int port, int cnt, uint32_t *payload, + uint32_t **rpayload) +{ + int cmd = PD_VDO_CMD(payload[0]); + uint16_t dev_id = 0; + int is_rw, is_latest; + + /* make sure we have some payload */ + if (cnt == 0) + return 0; + + switch (cmd) { + case VDO_CMD_VERSION: + /* guarantee last byte of payload is null character */ + *(payload + cnt - 1) = 0; + CPRINTF("version: %s\n", (char *)(payload+1)); + break; + case VDO_CMD_READ_INFO: + case VDO_CMD_SEND_INFO: + /* copy hash */ + if (cnt == 7) { + dev_id = VDO_INFO_HW_DEV_ID(payload[6]); + is_rw = VDO_INFO_IS_RW(payload[6]); + + is_latest = pd_dev_store_rw_hash(port, + dev_id, + payload + 1, + is_rw ? + SYSTEM_IMAGE_RW : + SYSTEM_IMAGE_RO); + /* + * Send update host event unless our RW hash is + * already known to be the latest update RW. + */ + if (!is_rw || !is_latest) + pd_send_host_event(PD_EVENT_UPDATE_DEVICE); + + CPRINTF("DevId:%d.%d SW:%d RW:%d\n", + HW_DEV_ID_MAJ(dev_id), + HW_DEV_ID_MIN(dev_id), + VDO_INFO_SW_DBG_VER(payload[6]), + is_rw); + } else if (cnt == 6) { + /* really old devices don't have last byte */ + pd_dev_store_rw_hash(port, dev_id, payload + 1, + SYSTEM_IMAGE_UNKNOWN); + } + break; + case VDO_CMD_CURRENT: + CPRINTF("Current: %dmA\n", payload[1]); + break; + case VDO_CMD_FLIP: + usb_mux_flip(port); + break; +#ifdef CONFIG_USB_PD_LOGGING + case VDO_CMD_GET_LOG: + pd_log_recv_vdm(port, cnt, payload); + break; +#endif /* CONFIG_USB_PD_LOGGING */ + } + + return 0; +} + +#ifdef CONFIG_USB_PD_ALT_MODE_DFP +static int dp_flags[CONFIG_USB_PD_PORT_COUNT]; +static uint32_t dp_status[CONFIG_USB_PD_PORT_COUNT]; + +static void svdm_safe_dp_mode(int port) +{ + /* make DP interface safe until configure */ + dp_flags[port] = 0; + dp_status[port] = 0; + usb_mux_set(port, TYPEC_MUX_NONE, + USB_SWITCH_CONNECT, pd_get_polarity(port)); +} + +static int svdm_enter_dp_mode(int port, uint32_t mode_caps) +{ + /* Only enter mode if device is DFP_D capable */ + if (mode_caps & MODE_DP_SNK) { + svdm_safe_dp_mode(port); + return 0; + } + + return -1; +} + +static int svdm_dp_status(int port, uint32_t *payload) +{ + int opos = pd_alt_mode(port, USB_SID_DISPLAYPORT); + + payload[0] = VDO(USB_SID_DISPLAYPORT, 1, + CMD_DP_STATUS | VDO_OPOS(opos)); + payload[1] = VDO_DP_STATUS(0, /* HPD IRQ ... not applicable */ + 0, /* HPD level ... not applicable */ + 0, /* exit DP? ... no */ + 0, /* usb mode? ... no */ + 0, /* multi-function ... no */ + (!!(dp_flags[port] & DP_FLAGS_DP_ON)), + 0, /* power low? ... no */ + (!!(dp_flags[port] & DP_FLAGS_DP_ON))); + return 2; +}; + +static int svdm_dp_config(int port, uint32_t *payload) +{ + int opos = pd_alt_mode(port, USB_SID_DISPLAYPORT); + int mf_pref = PD_VDO_DPSTS_MF_PREF(dp_status[port]); + int pin_mode = pd_dfp_dp_get_pin_mode(port, dp_status[port]); + + if (!pin_mode) + return 0; + + usb_mux_set(port, mf_pref ? TYPEC_MUX_DOCK : TYPEC_MUX_DP, + USB_SWITCH_CONNECT, pd_get_polarity(port)); + + payload[0] = VDO(USB_SID_DISPLAYPORT, 1, + CMD_DP_CONFIG | VDO_OPOS(opos)); + payload[1] = VDO_DP_CFG(pin_mode, /* pin mode */ + 1, /* DPv1.3 signaling */ + 2); /* UFP connected */ + return 2; +}; + +static void svdm_dp_post_config(int port) +{ + const struct usb_mux *mux = &usb_muxes[port]; + + dp_flags[port] |= DP_FLAGS_DP_ON; + if (!(dp_flags[port] & DP_FLAGS_HPD_HI_PENDING)) + return; + mux->hpd_update(port, 1, 0); +} + +static int svdm_dp_attention(int port, uint32_t *payload) +{ + int lvl = PD_VDO_DPSTS_HPD_LVL(payload[1]); + int irq = PD_VDO_DPSTS_HPD_IRQ(payload[1]); + const struct usb_mux *mux = &usb_muxes[port]; + + dp_status[port] = payload[1]; + if (!(dp_flags[port] & DP_FLAGS_DP_ON)) { + if (lvl) + dp_flags[port] |= DP_FLAGS_HPD_HI_PENDING; + return 1; + } + mux->hpd_update(port, lvl, irq); + + /* ack */ + return 1; +} + +static void svdm_exit_dp_mode(int port) +{ + const struct usb_mux *mux = &usb_muxes[port]; + + svdm_safe_dp_mode(port); + mux->hpd_update(port, 0, 0); +} + +static int svdm_enter_gfu_mode(int port, uint32_t mode_caps) +{ + /* Always enter GFU mode */ + return 0; +} + +static void svdm_exit_gfu_mode(int port) +{ +} + +static int svdm_gfu_status(int port, uint32_t *payload) +{ + /* + * This is called after enter mode is successful, send unstructured + * VDM to read info. + */ + pd_send_vdm(port, USB_VID_GOOGLE, VDO_CMD_READ_INFO, NULL, 0); + return 0; +} + +static int svdm_gfu_config(int port, uint32_t *payload) +{ + return 0; +} + +static int svdm_gfu_attention(int port, uint32_t *payload) +{ + return 0; +} + +const struct svdm_amode_fx supported_modes[] = { + { + .svid = USB_SID_DISPLAYPORT, + .enter = &svdm_enter_dp_mode, + .status = &svdm_dp_status, + .config = &svdm_dp_config, + .post_config = &svdm_dp_post_config, + .attention = &svdm_dp_attention, + .exit = &svdm_exit_dp_mode, + }, + { + .svid = USB_VID_GOOGLE, + .enter = &svdm_enter_gfu_mode, + .status = &svdm_gfu_status, + .config = &svdm_gfu_config, + .attention = &svdm_gfu_attention, + .exit = &svdm_exit_gfu_mode, + } +}; +const int supported_modes_cnt = ARRAY_SIZE(supported_modes); +#endif /* CONFIG_USB_PD_ALT_MODE_DFP */ diff --git a/common/usb_charger.c b/common/usb_charger.c index 010485afa3..1533dc3167 100644 --- a/common/usb_charger.c +++ b/common/usb_charger.c @@ -40,7 +40,9 @@ static void update_vbus_supplier(int port, int vbus_level) } } -#ifdef CONFIG_USBC_PPC +#ifdef CONFIG_USB_PD_5V_EN_CUSTOM +#define USB_5V_EN(port) board_is_sourcing_vbus(port) +#elif defined(CONFIG_USBC_PPC) #define USB_5V_EN(port) ppc_is_sourcing_vbus(port) #elif defined(CONFIG_USB_PD_5V_CHARGER_CTRL) #define USB_5V_EN(port) charger_is_sourcing_otg_power(port) diff --git a/include/config.h b/include/config.h index 0ce98481e3..9589ad7fb7 100644 --- a/include/config.h +++ b/include/config.h @@ -2805,6 +2805,12 @@ /* Ask charger if VBUS is enabled on a source port, instead of using GPIO */ #undef CONFIG_USB_PD_5V_CHARGER_CTRL +/* + * If defined, use a custom function to determine if VBUS is enabled on a + * source port. The custom function is board_is_sourcing_vbus(port). + */ +#undef CONFIG_USB_PD_5V_EN_CUSTOM + /* Dynamic USB PD source capability */ #undef CONFIG_USB_PD_DYNAMIC_SRC_CAP |