summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWai-Hong Tam <waihong@google.com>2021-07-20 11:23:49 -0700
committerCommit Bot <commit-bot@chromium.org>2021-07-23 19:01:33 +0000
commita2ac77f849fdd9e821b3f39c8d9d8d8e1eebd76a (patch)
tree830b0282f05de56676fca2e766f347f3006512c7
parent2e3d89e009584037331a50a07540fa5fcd49e89f (diff)
downloadchrome-ec-a2ac77f849fdd9e821b3f39c8d9d8d8e1eebd76a.tar.gz
zephyr: herobrine: Unlink the board/baseboard C files in EC-OS
Duplicate the baseboard-specific and board-specific C files to the Zephyr project src directory. So the Zephyr image can be built standalone and not depend on any EC-OS code. BRANCH=None BUG=b:193573005 TEST=Built the herobrine_npcx9 image successfully. Change-Id: I7940deccb0fe98f7c8e72ae21c4d170bcd96377f Signed-off-by: Wai-Hong Tam <waihong@google.com> Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3046512 Reviewed-by: Yuval Peress <peress@chromium.org>
-rw-r--r--zephyr/projects/herobrine/herobrine_npcx9/CMakeLists.txt24
-rw-r--r--zephyr/projects/herobrine/herobrine_npcx9/src/battery.c68
-rw-r--r--zephyr/projects/herobrine/herobrine_npcx9/src/led.c163
-rw-r--r--zephyr/projects/herobrine/herobrine_npcx9/src/switchcap.c22
-rw-r--r--zephyr/projects/herobrine/herobrine_npcx9/src/usb_pd_policy.c258
-rw-r--r--zephyr/projects/herobrine/herobrine_npcx9/src/usbc_config.c364
6 files changed, 883 insertions, 16 deletions
diff --git a/zephyr/projects/herobrine/herobrine_npcx9/CMakeLists.txt b/zephyr/projects/herobrine/herobrine_npcx9/CMakeLists.txt
index fa503185af..9dcd46db31 100644
--- a/zephyr/projects/herobrine/herobrine_npcx9/CMakeLists.txt
+++ b/zephyr/projects/herobrine/herobrine_npcx9/CMakeLists.txt
@@ -9,25 +9,17 @@ project(herobrine_npcx9)
zephyr_library_include_directories(include)
-set(PLATFORM_EC_BASEBOARD "${PLATFORM_EC}/baseboard/herobrine" CACHE PATH
- "Path to the platform/ec baseboard directory")
-set(PLATFORM_EC_BOARD "${PLATFORM_EC}/board/herobrine_npcx9" CACHE PATH
- "Path to the platform/ec board directory")
-
+# Board specific implementation
zephyr_library_sources_ifdef(CONFIG_PLATFORM_EC_USBC
- "${PLATFORM_EC_BASEBOARD}/usbc_config.c"
- "${PLATFORM_EC_BASEBOARD}/usb_pd_policy.c")
-
+ "src/usbc_config.c"
+ "src/usb_pd_policy.c")
zephyr_library_sources_ifdef(CONFIG_PLATFORM_EC_LED_COMMON
- "${PLATFORM_EC_BOARD}/led.c")
-
-zephyr_library_sources(
- "${PLATFORM_EC_BOARD}/battery.c"
- "${PLATFORM_EC_BOARD}/switchcap.c"
- "${PLATFORM_EC_BOARD}/usbc_config.c")
-
-# Board specific implementation
+ "src/led.c")
zephyr_library_sources_ifdef(CONFIG_PLATFORM_EC_MOTIONSENSE
"src/sensors.c")
zephyr_library_sources_ifdef(CONFIG_PLATFORM_EC_I2C
"src/i2c.c")
+
+zephyr_library_sources(
+ "src/battery.c"
+ "src/switchcap.c")
diff --git a/zephyr/projects/herobrine/herobrine_npcx9/src/battery.c b/zephyr/projects/herobrine/herobrine_npcx9/src/battery.c
new file mode 100644
index 0000000000..f6622f11ee
--- /dev/null
+++ b/zephyr/projects/herobrine/herobrine_npcx9/src/battery.c
@@ -0,0 +1,68 @@
+/* Copyright 2021 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_fuel_gauge.h"
+#include "common.h"
+#include "util.h"
+
+/*
+ * Battery info for all herobrine_npcx9 battery types. Note that the fields
+ * start_charging_min/max and charging_min/max are not used for the charger.
+ * The effective temperature limits are given by discharging_min/max_c.
+ *
+ * Fuel Gauge (FG) parameters which are used for determining if the battery
+ * is connected, the appropriate ship mode (battery cutoff) command, and the
+ * charge/discharge FETs status.
+ *
+ * Ship mode (battery cutoff) requires 2 writes to the appropriate smart battery
+ * register. For some batteries, the charge/discharge FET bits are set when
+ * charging/discharging is active, in other types, these bits set mean that
+ * charging/discharging is disabled. Therefore, in addition to the mask for
+ * these bits, a disconnect value must be specified. Note that for TI fuel
+ * gauge, the charge/discharge FET status is found in Operation Status (0x54),
+ * but a read of Manufacturer Access (0x00) will return the lower 16 bits of
+ * Operation status which contains the FET status bits.
+ *
+ * The assumption for battery types supported is that the charge/discharge FET
+ * status can be read with a sb_read() command and therefore, only the register
+ * address, mask, and disconnect value need to be provided.
+ */
+
+const struct board_batt_params board_battery_info[] = {
+ /* AP16L5J */
+ [BATTERY_AP16L5J] = {
+ .fuel_gauge = {
+ .manuf_name = "PANASONIC",
+ .device_name = "AP16L5J",
+ .ship_mode = {
+ .reg_addr = 0x3A,
+ .reg_data = { 0xC574, 0xC574 },
+ },
+ .fet = {
+ .mfgacc_support = 0,
+ .reg_addr = 0x0,
+ .reg_mask = 0x4000,
+ .disconnect_val = 0x0,
+ }
+ },
+ .batt_info = {
+ .voltage_max = 8800,
+ .voltage_normal = 7700, /* mV */
+ .voltage_min = 6000, /* mV */
+ .precharge_current = 256, /* mA */
+ .start_charging_min_c = 0,
+ .start_charging_max_c = 50,
+ .charging_min_c = 0,
+ .charging_max_c = 60,
+ .discharging_min_c = -20,
+ .discharging_max_c = 75,
+ },
+ },
+};
+BUILD_ASSERT(ARRAY_SIZE(board_battery_info) == BATTERY_TYPE_COUNT);
+
+const enum battery_type DEFAULT_BATTERY_TYPE = BATTERY_AP16L5J;
diff --git a/zephyr/projects/herobrine/herobrine_npcx9/src/led.c b/zephyr/projects/herobrine/herobrine_npcx9/src/led.c
new file mode 100644
index 0000000000..295c8effeb
--- /dev/null
+++ b/zephyr/projects/herobrine/herobrine_npcx9/src/led.c
@@ -0,0 +1,163 @@
+/* Copyright 2021 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.
+ *
+ * Power and battery LED control.
+ */
+
+#include "battery.h"
+#include "charge_manager.h"
+#include "charge_state.h"
+#include "chipset.h"
+#include "ec_commands.h"
+#include "gpio.h"
+#include "hooks.h"
+#include "host_command.h"
+#include "led_common.h"
+#include "system.h"
+#include "util.h"
+
+#define BAT_LED_ON 1
+#define BAT_LED_OFF 0
+
+const enum ec_led_id supported_led_ids[] = {
+ EC_LED_ID_RIGHT_LED,
+ EC_LED_ID_LEFT_LED,
+};
+
+const int supported_led_ids_count = ARRAY_SIZE(supported_led_ids);
+
+enum led_color {
+ LED_OFF = 0,
+ LED_AMBER,
+ LED_WHITE,
+ LED_COLOR_COUNT /* Number of colors, not a color itself */
+};
+
+static void side_led_set_color(int port, enum led_color color)
+{
+ gpio_set_level(port ? GPIO_EC_CHG_LED_Y_C1 : GPIO_EC_CHG_LED_Y_C0,
+ (color == LED_AMBER) ? BAT_LED_ON : BAT_LED_OFF);
+ gpio_set_level(port ? GPIO_EC_CHG_LED_W_C1 : GPIO_EC_CHG_LED_W_C0,
+ (color == LED_WHITE) ? BAT_LED_ON : BAT_LED_OFF);
+}
+
+void led_get_brightness_range(enum ec_led_id led_id, uint8_t *brightness_range)
+{
+ brightness_range[EC_LED_COLOR_AMBER] = 1;
+ brightness_range[EC_LED_COLOR_WHITE] = 1;
+}
+
+int led_set_brightness(enum ec_led_id led_id, const uint8_t *brightness)
+{
+ int port;
+
+ switch (led_id) {
+ case EC_LED_ID_RIGHT_LED:
+ port = 0;
+ break;
+ case EC_LED_ID_LEFT_LED:
+ port = 1;
+ break;
+ default:
+ return EC_ERROR_PARAM1;
+ }
+
+ if (brightness[EC_LED_COLOR_WHITE] != 0)
+ side_led_set_color(port, LED_WHITE);
+ else if (brightness[EC_LED_COLOR_AMBER] != 0)
+ side_led_set_color(port, LED_AMBER);
+ else
+ side_led_set_color(port, LED_OFF);
+
+ return EC_SUCCESS;
+}
+
+/*
+ * Set active charge port color to the parameter, turn off all others.
+ * If no port is active (-1), turn off all LEDs.
+ */
+static void set_active_port_color(enum led_color color)
+{
+ int port = charge_manager_get_active_charge_port();
+
+ if (led_auto_control_is_enabled(EC_LED_ID_RIGHT_LED))
+ side_led_set_color(0, (port == 0) ? color : LED_OFF);
+ if (led_auto_control_is_enabled(EC_LED_ID_LEFT_LED))
+ side_led_set_color(1, (port == 1) ? color : LED_OFF);
+}
+
+static void board_led_set_battery(void)
+{
+ static int battery_ticks;
+ uint32_t chflags = charge_get_flags();
+
+ battery_ticks++;
+
+ switch (charge_get_state()) {
+ case PWR_STATE_CHARGE:
+ /* Always indicate when charging, even in suspend. */
+ set_active_port_color(LED_AMBER);
+ break;
+ case PWR_STATE_DISCHARGE:
+ if (led_auto_control_is_enabled(EC_LED_ID_RIGHT_LED)) {
+ if (charge_get_percent() <= 10)
+ side_led_set_color(0,
+ (battery_ticks & 0x4) ? LED_WHITE : LED_OFF);
+ else
+ side_led_set_color(0, LED_OFF);
+ }
+
+ if (led_auto_control_is_enabled(EC_LED_ID_LEFT_LED))
+ side_led_set_color(1, LED_OFF);
+ break;
+ case PWR_STATE_ERROR:
+ set_active_port_color((battery_ticks & 0x2) ?
+ LED_WHITE : LED_OFF);
+ break;
+ case PWR_STATE_CHARGE_NEAR_FULL:
+ set_active_port_color(LED_WHITE);
+ break;
+ case PWR_STATE_IDLE: /* External power connected in IDLE */
+ if (chflags & CHARGE_FLAG_FORCE_IDLE)
+ set_active_port_color((battery_ticks & 0x4) ?
+ LED_AMBER : LED_OFF);
+ else
+ set_active_port_color(LED_WHITE);
+ break;
+ default:
+ /* Other states don't alter LED behavior */
+ break;
+ }
+}
+
+/* Called by hook task every TICK */
+static void led_tick(void)
+{
+ board_led_set_battery();
+}
+DECLARE_HOOK(HOOK_TICK, led_tick, HOOK_PRIO_DEFAULT);
+
+void led_control(enum ec_led_id led_id, enum ec_led_state state)
+{
+ enum led_color color;
+
+ if ((led_id != EC_LED_ID_RECOVERY_HW_REINIT_LED) &&
+ (led_id != EC_LED_ID_SYSRQ_DEBUG_LED))
+ return;
+
+ if (state == LED_STATE_RESET) {
+ led_auto_control(EC_LED_ID_LEFT_LED, 1);
+ led_auto_control(EC_LED_ID_RIGHT_LED, 1);
+ board_led_set_battery();
+ return;
+ }
+
+ color = state ? LED_WHITE : LED_OFF;
+
+ led_auto_control(EC_LED_ID_LEFT_LED, 0);
+ led_auto_control(EC_LED_ID_RIGHT_LED, 0);
+
+ side_led_set_color(0, color);
+ side_led_set_color(1, color);
+}
diff --git a/zephyr/projects/herobrine/herobrine_npcx9/src/switchcap.c b/zephyr/projects/herobrine/herobrine_npcx9/src/switchcap.c
new file mode 100644
index 0000000000..16b0db6ef6
--- /dev/null
+++ b/zephyr/projects/herobrine/herobrine_npcx9/src/switchcap.c
@@ -0,0 +1,22 @@
+/* Copyright 2021 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 "gpio.h"
+#include "power/qcom.h"
+
+void board_set_switchcap_power(int enable)
+{
+ gpio_set_level(GPIO_SWITCHCAP_ON, enable);
+}
+
+int board_is_switchcap_enabled(void)
+{
+ return gpio_get_level(GPIO_SWITCHCAP_ON);
+}
+
+int board_is_switchcap_power_good(void)
+{
+ return gpio_get_level(GPIO_SWITCHCAP_PG);
+}
diff --git a/zephyr/projects/herobrine/herobrine_npcx9/src/usb_pd_policy.c b/zephyr/projects/herobrine/herobrine_npcx9/src/usb_pd_policy.c
new file mode 100644
index 0000000000..87d47c32e2
--- /dev/null
+++ b/zephyr/projects/herobrine/herobrine_npcx9/src/usb_pd_policy.c
@@ -0,0 +1,258 @@
+/* Copyright 2021 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 "chipset.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)
+
+int pd_check_vconn_swap(int port)
+{
+ /* In G3, do not allow vconn swap since PP5000 rail is off */
+ return gpio_get_level(GPIO_EN_PP5000);
+}
+
+static uint8_t vbus_en[CONFIG_USB_PD_PORT_MAX_COUNT];
+#if CONFIG_USB_PD_PORT_MAX_COUNT == 1
+static uint8_t vbus_rp[CONFIG_USB_PD_PORT_MAX_COUNT] = {TYPEC_RP_1A5};
+#else
+static uint8_t vbus_rp[CONFIG_USB_PD_PORT_MAX_COUNT] = {TYPEC_RP_1A5,
+ TYPEC_RP_1A5};
+#endif
+
+static void board_vbus_update_source_current(int port)
+{
+ /* Both port are controlled by PPC SN5S330. */
+ ppc_set_vbus_source_current_limit(port, vbus_rp[port]);
+ ppc_vbus_source_enable(port, 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);
+
+ /* 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);
+
+ /* notify host of power info change */
+ pd_send_host_event(PD_EVENT_POWER_CHANGE);
+
+ return EC_SUCCESS; /* we are ready */
+}
+
+int board_vbus_source_enabled(int port)
+{
+ return vbus_en[port];
+}
+
+__override void typec_set_source_current_limit(int port, enum tcpc_rp_value rp)
+{
+ vbus_rp[port] = rp;
+ board_vbus_update_source_current(port);
+}
+
+int pd_snk_is_vbus_provided(int port)
+{
+ return tcpm_check_vbus_level(port, VBUS_PRESENT);
+}
+
+/* ----------------- Vendor Defined Messages ------------------ */
+#ifdef CONFIG_USB_PD_ALT_MODE_DFP
+__override int svdm_dp_config(int port, uint32_t *payload)
+{
+ int opos = pd_alt_mode(port, TCPC_TX_SOP, USB_SID_DISPLAYPORT);
+ uint8_t pin_mode = get_dp_pin_mode(port);
+
+ if (!pin_mode)
+ return 0;
+
+ /*
+ * Defer setting the usb_mux until HPD goes high, svdm_dp_attention().
+ * The AP only supports one DP phy. An external DP mux switches between
+ * the two ports. Should switch those muxes when it is really used,
+ * i.e. HPD high; otherwise, the real use case is preempted, like:
+ * (1) plug a dongle without monitor connected to port-0,
+ * (2) plug a dongle without monitor connected to port-1,
+ * (3) plug a monitor to the port-1 dongle.
+ */
+
+ 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;
+};
+
+__override void svdm_dp_post_config(int port)
+{
+ dp_flags[port] |= DP_FLAGS_DP_ON;
+}
+
+/**
+ * Is the port fine to be muxed its DisplayPort lines?
+ *
+ * Only one port can be muxed to DisplayPort at a time.
+ *
+ * @param port Port number of TCPC.
+ * @return 1 is fine; 0 is bad as other port is already muxed;
+ */
+static int is_dp_muxable(int port)
+{
+ int i;
+
+ for (i = 0; i < CONFIG_USB_PD_PORT_MAX_COUNT; i++)
+ if (i != port) {
+ if (usb_mux_get(i) & USB_PD_MUX_DP_ENABLED)
+ return 0;
+ }
+
+ return 1;
+}
+
+__override int svdm_dp_attention(int port, uint32_t *payload)
+{
+ enum gpio_signal hpd = GPIO_DP_HOT_PLUG_DET;
+ int lvl = PD_VDO_DPSTS_HPD_LVL(payload[1]);
+ int irq = PD_VDO_DPSTS_HPD_IRQ(payload[1]);
+ int cur_lvl = gpio_get_level(hpd);
+
+ dp_status[port] = payload[1];
+
+ if (!is_dp_muxable(port)) {
+ /* TODO(waihong): Info user? */
+ CPRINTS("p%d: The other port is already muxed.", port);
+ return 0;
+ }
+
+ /*
+ * Initial implementation to handle HPD. Only the first-plugged port
+ * works, i.e. sending HPD signal to AP. The second-plugged port
+ * will be ignored.
+ *
+ * TODO(waihong): Continue the above case, if the first-plugged port
+ * is then unplugged, switch to the second-plugged port and signal AP?
+ */
+ if (lvl) {
+ /*
+ * Enable and switch the DP port selection mux to the
+ * correct port.
+ *
+ * TODO(waihong): Better to move switching DP mux to
+ * the usb_mux abstraction.
+ */
+ gpio_set_level(GPIO_DP_MUX_SEL, port == 1);
+ gpio_set_level(GPIO_DP_MUX_OE_L, 0);
+
+ /* Connect the SBU lines in PPC chip. */
+ if (IS_ENABLED(CONFIG_USBC_PPC_SBU))
+ ppc_set_sbu(port, 1);
+
+ /*
+ * Connect the USB SS/DP lines in TCPC chip.
+ *
+ * When mf_pref not true, still use the dock muxing
+ * because of the board USB-C topology (limited to 2
+ * lanes DP).
+ */
+ usb_mux_set(port, USB_PD_MUX_DOCK,
+ USB_SWITCH_CONNECT,
+ polarity_rm_dts(pd_get_polarity(port)));
+ } else {
+ /* Disconnect the DP port selection mux. */
+ gpio_set_level(GPIO_DP_MUX_OE_L, 1);
+ gpio_set_level(GPIO_DP_MUX_SEL, 0);
+
+ /* Disconnect the SBU lines in PPC chip. */
+ if (IS_ENABLED(CONFIG_USBC_PPC_SBU))
+ ppc_set_sbu(port, 0);
+
+ /* Disconnect the DP but keep the USB SS lines in TCPC chip. */
+ usb_mux_set(port, USB_PD_MUX_USB_ENABLED,
+ USB_SWITCH_CONNECT,
+ polarity_rm_dts(pd_get_polarity(port)));
+ }
+
+ if (chipset_in_state(CHIPSET_STATE_ANY_SUSPEND) &&
+ (irq || lvl))
+ /*
+ * Wake up the AP. IRQ or level high indicates a DP sink is now
+ * present.
+ */
+ pd_notify_dp_alt_mode_entry(port);
+
+ /* Configure TCPC for the HPD event, for proper muxing */
+ usb_mux_hpd_update(port, lvl, irq);
+
+ /* Signal AP for the HPD event, through GPIO to AP */
+ if (irq & cur_lvl) {
+ uint64_t now = get_time().val;
+ /* Wait for the minimum spacing between IRQ_HPD if needed */
+ if (now < svdm_hpd_deadline[port])
+ usleep(svdm_hpd_deadline[port] - now);
+
+ /* Generate IRQ_HPD pulse */
+ gpio_set_level(hpd, 0);
+ usleep(HPD_DSTREAM_DEBOUNCE_IRQ);
+ gpio_set_level(hpd, 1);
+
+ /* Set the minimum time delay (2ms) for the next HPD IRQ */
+ svdm_hpd_deadline[port] = get_time().val +
+ HPD_USTREAM_DEBOUNCE_LVL;
+ } else if (irq & !lvl) {
+ CPRINTF("ERR:HPD:IRQ&LOW\n");
+ return 0;
+ } else {
+ gpio_set_level(hpd, lvl);
+ /* Set the minimum time delay (2ms) for the next HPD IRQ */
+ svdm_hpd_deadline[port] = get_time().val +
+ HPD_USTREAM_DEBOUNCE_LVL;
+ }
+
+ return 1;
+}
+
+__override void svdm_exit_dp_mode(int port)
+{
+ if (is_dp_muxable(port)) {
+ /* Disconnect the DP port selection mux. */
+ gpio_set_level(GPIO_DP_MUX_OE_L, 1);
+ gpio_set_level(GPIO_DP_MUX_SEL, 0);
+
+ /* Signal AP for the HPD low event */
+ usb_mux_hpd_update(port, 0, 0);
+ gpio_set_level(GPIO_DP_HOT_PLUG_DET, 0);
+ }
+}
+#endif /* CONFIG_USB_PD_ALT_MODE_DFP */
diff --git a/zephyr/projects/herobrine/herobrine_npcx9/src/usbc_config.c b/zephyr/projects/herobrine/herobrine_npcx9/src/usbc_config.c
new file mode 100644
index 0000000000..a3da4b5592
--- /dev/null
+++ b/zephyr/projects/herobrine/herobrine_npcx9/src/usbc_config.c
@@ -0,0 +1,364 @@
+/* Copyright 2021 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.
+ */
+
+/* Herobrine board-specific USB-C configuration */
+
+#include "bc12/pi3usb9201_public.h"
+#include "charger.h"
+#include "charger/isl923x_public.h"
+#include "charge_manager.h"
+#include "charge_state.h"
+#include "common.h"
+#include "config.h"
+#include "gpio.h"
+#include "hooks.h"
+#include "ppc/sn5s330_public.h"
+#include "system.h"
+#include "tcpm/ps8xxx_public.h"
+#include "tcpm/tcpci.h"
+#include "timer.h"
+#include "usb_pd.h"
+#include "usb_mux.h"
+#include "usbc_ocp.h"
+#include "usbc_ppc.h"
+
+#define CPRINTS(format, args...) cprints(CC_USBCHARGE, format, ## args)
+#define CPRINTF(format, args...) cprintf(CC_USBCHARGE, format, ## args)
+
+
+/* GPIO Interrupt Handlers */
+void tcpc_alert_event(enum gpio_signal signal)
+{
+ int port = -1;
+
+ switch (signal) {
+ case GPIO_USB_C0_PD_INT_ODL:
+ port = 0;
+ break;
+ case GPIO_USB_C1_PD_INT_ODL:
+ port = 1;
+ break;
+ default:
+ return;
+ }
+
+ schedule_deferred_pd_interrupt(port);
+}
+
+void usb0_evt(enum gpio_signal signal)
+{
+ task_set_event(TASK_ID_USB_CHG_P0, USB_CHG_EVENT_BC12);
+}
+
+void usb1_evt(enum gpio_signal signal)
+{
+ task_set_event(TASK_ID_USB_CHG_P1, USB_CHG_EVENT_BC12);
+}
+
+static void usba_oc_deferred(void)
+{
+ /* Use next number after all USB-C ports to indicate the USB-A port */
+ board_overcurrent_event(CONFIG_USB_PD_PORT_MAX_COUNT,
+ !gpio_get_level(GPIO_USB_A0_OC_ODL));
+}
+DECLARE_DEFERRED(usba_oc_deferred);
+
+void usba_oc_interrupt(enum gpio_signal signal)
+{
+ hook_call_deferred(&usba_oc_deferred_data, 0);
+}
+
+void ppc_interrupt(enum gpio_signal signal)
+{
+ switch (signal) {
+ case GPIO_USB_C0_SWCTL_INT_ODL:
+ sn5s330_interrupt(0);
+ break;
+ case GPIO_USB_C1_SWCTL_INT_ODL:
+ sn5s330_interrupt(1);
+ break;
+ default:
+ break;
+ }
+}
+
+const struct charger_config_t chg_chips[] = {
+ {
+ .i2c_port = I2C_PORT_CHARGER,
+ .i2c_addr_flags = ISL923X_ADDR_FLAGS,
+ .drv = &isl923x_drv,
+ },
+};
+
+int charger_profile_override(struct charge_state_data *curr)
+{
+ int usb_mv;
+ int port;
+
+ if (curr->state != ST_CHARGE)
+ return 0;
+
+ /* Lower the max requested voltage to 5V when battery is full. */
+ if (chipset_in_state(CHIPSET_STATE_ANY_OFF) &&
+ !(curr->batt.flags & BATT_FLAG_BAD_STATUS) &&
+ !(curr->batt.flags & BATT_FLAG_WANT_CHARGE) &&
+ (curr->batt.status & STATUS_FULLY_CHARGED))
+ usb_mv = 5000;
+ else
+ usb_mv = PD_MAX_VOLTAGE_MV;
+
+ if (pd_get_max_voltage() != usb_mv) {
+ CPRINTS("VBUS limited to %dmV", usb_mv);
+ for (port = 0; port < CONFIG_USB_PD_PORT_MAX_COUNT; port++)
+ pd_set_external_voltage_limit(port, usb_mv);
+ }
+
+ return 0;
+}
+
+enum ec_status charger_profile_override_get_param(uint32_t param,
+ uint32_t *value)
+{
+ return EC_RES_INVALID_PARAM;
+}
+
+enum ec_status charger_profile_override_set_param(uint32_t param,
+ uint32_t value)
+{
+ return EC_RES_INVALID_PARAM;
+}/* Power Path Controller */
+struct ppc_config_t ppc_chips[] = {
+ {
+ .i2c_port = I2C_PORT_TCPC0,
+ .i2c_addr_flags = SN5S330_ADDR0_FLAGS,
+ .drv = &sn5s330_drv
+ },
+ {
+ .i2c_port = I2C_PORT_TCPC1,
+ .i2c_addr_flags = SN5S330_ADDR0_FLAGS,
+ .drv = &sn5s330_drv
+ },
+};
+unsigned int ppc_cnt = ARRAY_SIZE(ppc_chips);
+
+/* TCPC mux configuration */
+const struct tcpc_config_t tcpc_config[CONFIG_USB_PD_PORT_MAX_COUNT] = {
+ {
+ .bus_type = EC_BUS_TYPE_I2C,
+ .i2c_info = {
+ .port = I2C_PORT_TCPC0,
+ .addr_flags = PS8751_I2C_ADDR1_FLAGS,
+ },
+ .drv = &ps8xxx_tcpm_drv,
+ },
+ {
+ .bus_type = EC_BUS_TYPE_I2C,
+ .i2c_info = {
+ .port = I2C_PORT_TCPC1,
+ .addr_flags = PS8751_I2C_ADDR1_FLAGS,
+ },
+ .drv = &ps8xxx_tcpm_drv,
+ },
+};
+
+/*
+ * Port-0/1 USB mux driver.
+ *
+ * The USB mux is handled by TCPC chip and the HPD update is through a GPIO
+ * to AP. But the TCPC chip is also needed to know the HPD status; otherwise,
+ * the mux misbehaves.
+ */
+const struct usb_mux usb_muxes[CONFIG_USB_PD_PORT_MAX_COUNT] = {
+ {
+ .usb_port = 0,
+ .driver = &tcpci_tcpm_usb_mux_driver,
+ .hpd_update = &ps8xxx_tcpc_update_hpd_status,
+ },
+ {
+ .usb_port = 1,
+ .driver = &tcpci_tcpm_usb_mux_driver,
+ .hpd_update = &ps8xxx_tcpc_update_hpd_status,
+ }
+};
+
+const int usb_port_enable[USB_PORT_COUNT] = {
+ GPIO_EN_USB_A_5V,
+};
+
+/* BC1.2 */
+const struct pi3usb9201_config_t pi3usb9201_bc12_chips[] = {
+ {
+ .i2c_port = I2C_PORT_POWER,
+ .i2c_addr_flags = PI3USB9201_I2C_ADDR_3_FLAGS,
+ },
+ {
+ .i2c_port = I2C_PORT_EEPROM,
+ .i2c_addr_flags = PI3USB9201_I2C_ADDR_3_FLAGS,
+ },
+};
+
+/* Initialize board USC-C things */
+static void board_init_usbc(void)
+{
+ /* Enable BC1.2 interrupts */
+ gpio_enable_interrupt(GPIO_USB_C0_BC12_INT_L);
+ gpio_enable_interrupt(GPIO_USB_C1_BC12_INT_L);
+
+ /* Enable USB-A overcurrent interrupt */
+ gpio_enable_interrupt(GPIO_USB_A0_OC_ODL);
+}
+DECLARE_HOOK(HOOK_INIT, board_init_usbc, HOOK_PRIO_DEFAULT);
+
+void board_tcpc_init(void)
+{
+ /* Only reset TCPC if not sysjump */
+ if (!system_jumped_late()) {
+ /* 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);
+
+ /*
+ * Initialize HPD to low; after sysjump SOC needs to see
+ * HPD pulse to enable video path
+ */
+ for (int port = 0; port < CONFIG_USB_PD_PORT_MAX_COUNT; ++port)
+ usb_mux_hpd_update(port, 0, 0);
+}
+DECLARE_HOOK(HOOK_INIT, board_tcpc_init, HOOK_PRIO_INIT_I2C + 1);
+
+void board_reset_pd_mcu(void)
+{
+ cprints(CC_USB, "Resetting TCPCs...");
+ cflush();
+
+ gpio_set_level(GPIO_USB_C0_PD_RST_L, 0);
+ gpio_set_level(GPIO_USB_C1_PD_RST_L, 0);
+ msleep(PS8XXX_RESET_DELAY_MS);
+ gpio_set_level(GPIO_USB_C0_PD_RST_L, 1);
+ gpio_set_level(GPIO_USB_C1_PD_RST_L, 1);
+ msleep(PS8805_FW_INIT_DELAY_MS);
+}
+
+void board_set_tcpc_power_mode(int port, int mode)
+{
+ /* Ignore the "mode" to turn the chip on. We can only do a reset. */
+ if (mode)
+ return;
+
+ board_reset_pd_mcu();
+}
+
+int board_vbus_sink_enable(int port, int enable)
+{
+ /* Both ports are controlled by PPC SN5S330 */
+ return ppc_vbus_sink_enable(port, enable);
+}
+
+int board_is_sourcing_vbus(int port)
+{
+ /* Both ports are controlled by PPC SN5S330 */
+ return ppc_is_sourcing_vbus(port);
+}
+
+void board_overcurrent_event(int port, int is_overcurrented)
+{
+ /* TODO(b/120231371): 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_MAX_COUNT);
+ int i;
+
+ if (!is_real_port && port != CHARGE_PORT_NONE)
+ return EC_ERROR_INVAL;
+
+ if (port == CHARGE_PORT_NONE) {
+ CPRINTS("Disabling all charging port");
+
+ /* Disable all ports. */
+ for (i = 0; i < CONFIG_USB_PD_PORT_MAX_COUNT; i++) {
+ /*
+ * Do not return early if one fails otherwise we can
+ * get into a boot loop assertion failure.
+ */
+ if (board_vbus_sink_enable(i, 0))
+ CPRINTS("Disabling p%d sink path failed.", i);
+ }
+
+ return EC_SUCCESS;
+ }
+
+ /* Check if the port is sourcing VBUS. */
+ if (board_is_sourcing_vbus(port)) {
+ CPRINTS("Skip enable p%d", port);
+ return EC_ERROR_INVAL;
+ }
+
+
+ CPRINTS("New charge port: p%d", port);
+
+ /*
+ * Turn off the other ports' sink path FETs, before enabling the
+ * requested charge port.
+ */
+ for (i = 0; i < CONFIG_USB_PD_PORT_MAX_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.", port);
+ 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))
+ if (gpio_get_level(GPIO_USB_C0_PD_RST_L))
+ status |= PD_STATUS_TCPC_ALERT_0;
+ if (!gpio_get_level(GPIO_USB_C1_PD_INT_ODL))
+ if (gpio_get_level(GPIO_USB_C1_PD_RST_L))
+ status |= PD_STATUS_TCPC_ALERT_1;
+
+ return status;
+}