diff options
author | Simon Glass <sjg@chromium.org> | 2020-12-11 17:42:20 -0700 |
---|---|---|
committer | Commit Bot <commit-bot@chromium.org> | 2021-01-15 03:56:13 +0000 |
commit | 458ee6e3fef4b582f930a488e012d5bb549288ed (patch) | |
tree | 6980389cb1f2b0ce02c4b980fb516e2a4e057526 | |
parent | ff48c55fa23433a4beb5e057cab01ebfd60bbcea (diff) | |
download | chrome-ec-458ee6e3fef4b582f930a488e012d5bb549288ed.tar.gz |
Add volteer USB-C support code
This enables various options on volteer so that some of the USB-C code
builds.
BUG=b:175434113
TEST=build and run on volteer
Cq-Depend: chromium:2587225
Change-Id: I48cc7a0e50a76338d86ba03f24f5fb169c907143
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/zephyr-chrome/+/2585918
Reviewed-by: Keith Short <keithshort@chromium.org>
Commit-Queue: Simon Glass <sjg@chromium.org>
Tested-by: Simon Glass <sjg@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2630165
Tested-by: Jack Rosenthal <jrosenth@chromium.org>
Reviewed-by: Simon Glass <sjg@chromium.org>
Commit-Queue: Jack Rosenthal <jrosenth@chromium.org>
-rw-r--r-- | zephyr/projects/volteer/CMakeLists.txt | 2 | ||||
-rw-r--r-- | zephyr/projects/volteer/prj.conf | 11 | ||||
-rw-r--r-- | zephyr/projects/volteer/src/usb_pd_policy.c | 81 | ||||
-rw-r--r-- | zephyr/projects/volteer/src/usbc_config.c | 148 |
4 files changed, 242 insertions, 0 deletions
diff --git a/zephyr/projects/volteer/CMakeLists.txt b/zephyr/projects/volteer/CMakeLists.txt index 20928eaea9..b477118445 100644 --- a/zephyr/projects/volteer/CMakeLists.txt +++ b/zephyr/projects/volteer/CMakeLists.txt @@ -24,3 +24,5 @@ zephyr_include_directories(include) target_sources(app PRIVATE "src/battery.c") target_sources(app PRIVATE "src/battery_presence.c") target_sources(app PRIVATE "src/pwrok_signals.c") +target_sources(app PRIVATE "src/usbc_config.c") +target_sources(app PRIVATE "src/usb_pd_policy.c") diff --git a/zephyr/projects/volteer/prj.conf b/zephyr/projects/volteer/prj.conf index acfa0bdaad..4ffac7112e 100644 --- a/zephyr/projects/volteer/prj.conf +++ b/zephyr/projects/volteer/prj.conf @@ -29,3 +29,14 @@ CONFIG_PLATFORM_EC_POWERSEQ_RTC_RESET=y # Miscellaneous tasks CONFIG_HAS_TASK_KEYPROTO=y CONFIG_HAS_TASK_POWERBTN=y + +# USB-C and charging +CONFIG_HAS_TASK_CHARGER=y +CONFIG_PLATFORM_EC_USBC_PPC_SN5S330=y +CONFIG_PLATFORM_EC_USBC_PPC_SYV682X=y +CONFIG_PLATFORM_EC_USB_PD_VBUS_MEASURE_NOT_PRESENT=y +CONFIG_PLATFORM_EC_USB_PD_DISCHARGE_PPC=y +CONFIG_PLATFORM_EC_CONFIG_USB_PD_REV30=y +CONFIG_PLATFORM_EC_BATTERY_FUEL_GAUGE=y +CONFIG_PLATFORM_EC_USB_PD_VBUS_DETECT_PPC=y +CONFIG_PLATFORM_EC_USB_PD_TCPM_TUSB422=y diff --git a/zephyr/projects/volteer/src/usb_pd_policy.c b/zephyr/projects/volteer/src/usb_pd_policy.c new file mode 100644 index 0000000000..39bbd918a5 --- /dev/null +++ b/zephyr/projects/volteer/src/usb_pd_policy.c @@ -0,0 +1,81 @@ +/* Copyright 2019 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. + */ +/* Shared USB-C policy for Volteer boards */ +#include "charge_manager.h" +#include "compile_time_macros.h" +#include "console.h" +#include "gpio.h" +#include "usb_mux.h" +#include "usbc_ppc.h" +#include "usb_pd.h" +#include "system.h" + +#define CPRINTF(format, args...) cprintf(CC_USBPD, format, ## args) +#define CPRINTS(format, args...) cprints(CC_USBPD, format, ## args) + +int pd_check_vconn_swap(int port) +{ + /* Only allow vconn swap if pp5000_A rail is enabled */ + return gpio_get_level(GPIO_EN_PP5000_A); +} + +void pd_power_supply_reset(int port) +{ + int prev_en; + + prev_en = ppc_is_sourcing_vbus(port); + + /* Disable VBUS. */ + ppc_vbus_source_enable(port, 0); + + /* 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) +{ + int rv; + + /* Disable charging. */ + rv = ppc_vbus_sink_enable(port, 0); + if (rv) + return rv; + + pd_set_vbus_discharge(port, 0); + + /* Provide Vbus. */ + rv = ppc_vbus_source_enable(port, 1); + if (rv) + return rv; + +#ifdef CONFIG_USB_PD_MAX_SINGLE_SOURCE_CURRENT + /* Ensure we advertise the proper available current quota */ + charge_manager_source_port(port, 1); +#endif /* defined(CONFIG_USB_PD_MAX_SINGLE_SOURCE_CURRENT) */ + + /* Notify host of power info change. */ + pd_send_host_event(PD_EVENT_POWER_CHANGE); + + return EC_SUCCESS; +} + +int pd_snk_is_vbus_provided(int port) +{ + return ppc_is_vbus_present(port); +} + +int board_vbus_source_enabled(int port) +{ + return ppc_is_sourcing_vbus(port); +} diff --git a/zephyr/projects/volteer/src/usbc_config.c b/zephyr/projects/volteer/src/usbc_config.c new file mode 100644 index 0000000000..8a9d6da384 --- /dev/null +++ b/zephyr/projects/volteer/src/usbc_config.c @@ -0,0 +1,148 @@ +/* Copyright 2020 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. + */ + +/* USBC PPC chip list. Copied from volteer board.c in platform/ec. */ + +#include <stdbool.h> +#include <zephyr.h> + +#include "charge_manager.h" +#include "charge_state.h" +#include "gpio_map.h" +#include "gpio_signal.h" +#include "usbc_ocp.h" +#include "usbc_ppc.h" +#include "../driver/ppc/sn5s330.h" +#include "../driver/ppc/syv682x.h" +#include "../driver/tcpm/tusb422.h" + +#define SN5S330_ADDR0_FLAGS 0x40 +#define SYV682X_ADDR0_FLAGS 0x40 + +#define CPRINTSUSB(format, args...) cprints(CC_USBCHARGE, format, ## args) +#define CPRINTFUSB(format, args...) cprintf(CC_USBCHARGE, format, ## args) + +enum usbc_port { + USBC_PORT_C0 = 0, + USBC_PORT_C1, + USBC_PORT_COUNT +}; + +struct ppc_config_t ppc_chips[] = { + [USBC_PORT_C0] = { + .i2c_port = I2C_PORT_USB_C0, + .i2c_addr_flags = SN5S330_ADDR0_FLAGS, + .drv = &sn5s330_drv, + }, + [USBC_PORT_C1] = { + .i2c_port = I2C_PORT_USB_C1, + .i2c_addr_flags = SYV682X_ADDR0_FLAGS, + .drv = &syv682x_drv, + }, +}; +BUILD_ASSERT(ARRAY_SIZE(ppc_chips) == USBC_PORT_COUNT); +unsigned int ppc_cnt = ARRAY_SIZE(ppc_chips); + +/* FIXME: For some reason these don't get defined by gpio_signal.h */ +#define GPIO_USB_C0_OC_ODL 0 +#define GPIO_USB_C1_OC_ODL 1 + +void board_overcurrent_event(int port, int is_overcurrented) +{ + /* Note that the level is inverted because the pin is active low. */ + switch (port) { + case USBC_PORT_C0: + gpio_set_level(GPIO_USB_C0_OC_ODL, !is_overcurrented); + break; + case USBC_PORT_C1: + gpio_set_level(GPIO_USB_C1_OC_ODL, !is_overcurrented); + break; + } +} + +/******************************************************************************/ +/* USBC TCPC configuration */ +const struct tcpc_config_t tcpc_config[] = { + [USBC_PORT_C0] = { + .bus_type = EC_BUS_TYPE_I2C, + .i2c_info = { + .port = I2C_PORT_USB_C0, + .addr_flags = TUSB422_I2C_ADDR_FLAGS, + }, + .drv = &tusb422_tcpm_drv, + }, + [USBC_PORT_C1] = { + .bus_type = EC_BUS_TYPE_I2C, + .i2c_info = { + .port = I2C_PORT_USB_C1, + .addr_flags = TUSB422_I2C_ADDR_FLAGS, + }, + .drv = &tusb422_tcpm_drv, + }, +}; +BUILD_ASSERT(ARRAY_SIZE(tcpc_config) == USBC_PORT_COUNT); +BUILD_ASSERT(CONFIG_USB_PD_PORT_MAX_COUNT == USBC_PORT_COUNT); + +int board_set_active_charge_port(int port) +{ + int is_valid_port = (port >= 0 && + port < CONFIG_USB_PD_PORT_MAX_COUNT); + int i; + + if (port == CHARGE_PORT_NONE) { + CPRINTSUSB("Disabling all charger ports"); + + /* Disable all ports. */ + for (i = 0; i < ppc_cnt; i++) { + /* + * Do not return early if one fails otherwise we can + * get into a boot loop assertion failure. + */ + if (ppc_vbus_sink_enable(i, 0)) + CPRINTSUSB("Disabling C%d as sink failed.", i); + } + + return EC_SUCCESS; + } else if (!is_valid_port) { + return EC_ERROR_INVAL; + } + + + /* Check if the port is sourcing VBUS. */ + if (ppc_is_sourcing_vbus(port)) { + CPRINTFUSB("Skip enable C%d", port); + return EC_ERROR_INVAL; + } + + CPRINTSUSB("New charge port: C%d", port); + + /* + * Turn off the other ports' sink path FETs, before enabling the + * requested charge port. + */ + for (i = 0; i < ppc_cnt; i++) { + if (i == port) + continue; + + if (ppc_vbus_sink_enable(i, 0)) + CPRINTSUSB("C%d: sink path disable failed.", i); + } + + /* Enable requested charge port. */ + if (ppc_vbus_sink_enable(port, 1)) { + CPRINTSUSB("C%d: sink path enable failed.", port); + return EC_ERROR_UNKNOWN; + } + + return EC_SUCCESS; +} + +__overridable 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); +} |