summaryrefslogtreecommitdiff
path: root/zephyr/projects/volteer/src/usbc_config.c
diff options
context:
space:
mode:
Diffstat (limited to 'zephyr/projects/volteer/src/usbc_config.c')
-rw-r--r--zephyr/projects/volteer/src/usbc_config.c148
1 files changed, 148 insertions, 0 deletions
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);
+}