summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorScott Worley <scott.worley@microchip.corp-partner.google.com>2017-12-21 16:18:22 -0500
committerchrome-bot <chrome-bot@chromium.org>2018-02-08 23:41:19 -0800
commit5e18dfc34575e95b3c75de3e02a5d9d42b04c71b (patch)
treee11e87199c07bda9b5c7a4147a3a9069cadf499d
parent178e18a164d5b9b440d8823f659b8e995bb0f358 (diff)
downloadchrome-ec-5e18dfc34575e95b3c75de3e02a5d9d42b04c71b.tar.gz
mchpevb1: Add remaining board files
Add Microchip EVB plus SKL RVP3 remaining board files for battery, LED, and USB PD. BRANCH=none BUG= TEST=Review only. CQ-DEPEND=CL:840654,CL:841022 Change-Id: I34ccb33eb44e73ab841f96f4733bfe419b095678 Signed-off-by: Scott Worley <scott.worley@microchip.corp-partner.google.com> Reviewed-on: https://chromium-review.googlesource.com/841043 Commit-Ready: Randall Spangler <rspangler@chromium.org> Tested-by: Randall Spangler <rspangler@chromium.org> Reviewed-by: Randall Spangler <rspangler@chromium.org>
-rw-r--r--board/mchpevb1/battery.c230
-rw-r--r--board/mchpevb1/led.c174
-rw-r--r--board/mchpevb1/usb_pd_policy.c399
3 files changed, 803 insertions, 0 deletions
diff --git a/board/mchpevb1/battery.c b/board/mchpevb1/battery.c
new file mode 100644
index 0000000000..2372affb64
--- /dev/null
+++ b/board/mchpevb1/battery.c
@@ -0,0 +1,230 @@
+/* Copyright 2015 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"
+#include "battery_smart.h"
+#include "charge_state.h"
+#include "console.h"
+#include "ec_commands.h"
+#include "i2c.h"
+#include "util.h"
+
+/* Shutdown mode parameter to write to manufacturer access register */
+#define PARAM_CUT_OFF_LOW 0x10
+#define PARAM_CUT_OFF_HIGH 0x00
+
+/* Battery info for BQ40Z55 */
+static const struct battery_info info = {
+ .voltage_max = 8700, /* mV */
+ .voltage_normal = 7600,
+ .voltage_min = 6000,
+ .precharge_current = 256, /* mA */
+ .start_charging_min_c = 0,
+ .start_charging_max_c = 46,
+ .charging_min_c = 0,
+ .charging_max_c = 60,
+ .discharging_min_c = 0,
+ .discharging_max_c = 60,
+};
+
+const struct battery_info *battery_get_info(void)
+{
+ return &info;
+}
+
+int board_cut_off_battery(void)
+{
+ int rv;
+ uint8_t buf[3];
+
+ /* Ship mode command must be sent twice to take effect */
+ buf[0] = SB_MANUFACTURER_ACCESS & 0xff;
+ buf[1] = PARAM_CUT_OFF_LOW;
+ buf[2] = PARAM_CUT_OFF_HIGH;
+
+ i2c_lock(I2C_PORT_BATTERY, 1);
+ rv = i2c_xfer(I2C_PORT_BATTERY, BATTERY_ADDR, buf, 3, NULL, 0,
+ I2C_XFER_SINGLE);
+ rv |= i2c_xfer(I2C_PORT_BATTERY, BATTERY_ADDR, buf, 3, NULL, 0,
+ I2C_XFER_SINGLE);
+ i2c_lock(I2C_PORT_BATTERY, 0);
+
+ return rv;
+}
+
+#ifdef CONFIG_CHARGER_PROFILE_OVERRIDE
+
+static int fast_charging_allowed = 1;
+
+/*
+ * This can override the smart battery's charging profile. To make a change,
+ * modify one or more of requested_voltage, requested_current, or state.
+ * Leave everything else unchanged.
+ *
+ * Return the next poll period in usec, or zero to use the default (which is
+ * state dependent).
+ */
+int charger_profile_override(struct charge_state_data *curr)
+{
+ /* temp in 0.1 deg C */
+ int temp_c = curr->batt.temperature - 2731;
+ /* keep track of last temperature range for hysteresis */
+ static enum {
+ TEMP_RANGE_1,
+ TEMP_RANGE_2,
+ TEMP_RANGE_3,
+ TEMP_RANGE_4,
+ TEMP_RANGE_5,
+ } temp_range = TEMP_RANGE_3;
+ /* keep track of last voltage range for hysteresis */
+ static enum {
+ VOLTAGE_RANGE_LOW,
+ VOLTAGE_RANGE_HIGH,
+ } voltage_range = VOLTAGE_RANGE_LOW;
+
+ /* Current and previous battery voltage */
+ int batt_voltage;
+ static int prev_batt_voltage;
+
+ /*
+ * Determine temperature range. The five ranges are:
+ * < 10C
+ * 10-15C
+ * 15-23C
+ * 23-45C
+ * > 45C
+ *
+ * Add 0.2 degrees of hysteresis.
+ * If temp reading was bad, use last range.
+ */
+ if (!(curr->batt.flags & BATT_FLAG_BAD_TEMPERATURE)) {
+ if (temp_c < 99)
+ temp_range = TEMP_RANGE_1;
+ else if (temp_c > 101 && temp_c < 149)
+ temp_range = TEMP_RANGE_2;
+ else if (temp_c > 151 && temp_c < 229)
+ temp_range = TEMP_RANGE_3;
+ else if (temp_c > 231 && temp_c < 449)
+ temp_range = TEMP_RANGE_4;
+ else if (temp_c > 451)
+ temp_range = TEMP_RANGE_5;
+ }
+
+ /*
+ * If battery voltage reading is bad, use the last reading. Otherwise,
+ * determine voltage range with hysteresis.
+ */
+ if (curr->batt.flags & BATT_FLAG_BAD_VOLTAGE) {
+ batt_voltage = prev_batt_voltage;
+ } else {
+ batt_voltage = prev_batt_voltage = curr->batt.voltage;
+ if (batt_voltage < 8200)
+ voltage_range = VOLTAGE_RANGE_LOW;
+ else if (batt_voltage > 8300)
+ voltage_range = VOLTAGE_RANGE_HIGH;
+ }
+
+ /*
+ * If we are not charging or we aren't using fast charging profiles,
+ * then do not override desired current and voltage.
+ */
+ if (curr->state != ST_CHARGE || !fast_charging_allowed)
+ return 0;
+
+ /*
+ * Okay, impose our custom will:
+ * When battery is 0-10C:
+ * CC at 486mA @ 8.7V
+ * CV at 8.7V
+ *
+ * When battery is <15C:
+ * CC at 1458mA @ 8.7V
+ * CV at 8.7V
+ *
+ * When battery is <23C:
+ * CC at 3402mA until 8.3V @ 8.7V
+ * CC at 2430mA @ 8.7V
+ * CV at 8.7V
+ *
+ * When battery is <45C:
+ * CC at 4860mA until 8.3V @ 8.7V
+ * CC at 2430mA @ 8.7V
+ * CV at 8.7V until current drops to 450mA
+ *
+ * When battery is >45C:
+ * CC at 2430mA @ 8.3V
+ * CV at 8.3V (when battery is hot we don't go to fully charged)
+ */
+ switch (temp_range) {
+ case TEMP_RANGE_1:
+ curr->requested_current = 486;
+ curr->requested_voltage = 8700;
+ break;
+ case TEMP_RANGE_2:
+ curr->requested_current = 1458;
+ curr->requested_voltage = 8700;
+ break;
+ case TEMP_RANGE_3:
+ curr->requested_voltage = 8700;
+ if (voltage_range == VOLTAGE_RANGE_HIGH)
+ curr->requested_current = 2430;
+ else
+ curr->requested_current = 3402;
+ break;
+ case TEMP_RANGE_4:
+ curr->requested_voltage = 8700;
+ if (voltage_range == VOLTAGE_RANGE_HIGH)
+ curr->requested_current = 2430;
+ else
+ curr->requested_current = 4860;
+ break;
+ case TEMP_RANGE_5:
+ curr->requested_current = 2430;
+ curr->requested_voltage = 8300;
+ break;
+ }
+
+ return 0;
+}
+
+/* Customs options controllable by host command. */
+#define PARAM_FASTCHARGE (CS_PARAM_CUSTOM_PROFILE_MIN + 0)
+
+enum ec_status charger_profile_override_get_param(uint32_t param,
+ uint32_t *value)
+{
+ if (param == PARAM_FASTCHARGE) {
+ *value = fast_charging_allowed;
+ return EC_RES_SUCCESS;
+ }
+ return EC_RES_INVALID_PARAM;
+}
+
+enum ec_status charger_profile_override_set_param(uint32_t param,
+ uint32_t value)
+{
+ if (param == PARAM_FASTCHARGE) {
+ fast_charging_allowed = value;
+ return EC_RES_SUCCESS;
+ }
+ return EC_RES_INVALID_PARAM;
+}
+
+static int command_fastcharge(int argc, char **argv)
+{
+ if (argc > 1 && !parse_bool(argv[1], &fast_charging_allowed))
+ return EC_ERROR_PARAM1;
+
+ ccprintf("fastcharge %s\n", fast_charging_allowed ? "on" : "off");
+
+ return EC_SUCCESS;
+}
+DECLARE_CONSOLE_COMMAND(fastcharge, command_fastcharge,
+ "[on|off]",
+ "Get or set fast charging profile");
+
+#endif /* CONFIG_CHARGER_PROFILE_OVERRIDE */
diff --git a/board/mchpevb1/led.c b/board/mchpevb1/led.c
new file mode 100644
index 0000000000..7b9f7646cb
--- /dev/null
+++ b/board/mchpevb1/led.c
@@ -0,0 +1,174 @@
+/* Copyright 2017 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 for MEC1701 EVB.
+ */
+
+#include "battery.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 "util.h"
+
+#define BAT_LED_ON 1
+#define BAT_LED_OFF 0
+
+#define CRITICAL_LOW_BATTERY_PERCENTAGE 3
+#define LOW_BATTERY_PERCENTAGE 10
+
+#define LED_TOTAL_4SECS_TICKS 4
+#define LED_TOTAL_2SECS_TICKS 2
+#define LED_ON_1SEC_TICKS 1
+#define LED_ON_2SECS_TICKS 2
+
+/*
+ * NOTE: GPIO_BAT_LED_xxx defined in board.h
+ */
+
+const enum ec_led_id supported_led_ids[] = {
+ EC_LED_ID_BATTERY_LED};
+
+const int supported_led_ids_count = ARRAY_SIZE(supported_led_ids);
+
+enum led_color {
+ LED_OFF = 0,
+ LED_RED,
+ LED_AMBER,
+ LED_GREEN,
+ LED_COLOR_COUNT /* Number of colors, not a color itself */
+};
+
+static int bat_led_set_color(enum led_color color)
+{
+ switch (color) {
+ case LED_OFF:
+ gpio_set_level(GPIO_BAT_LED_RED, BAT_LED_OFF);
+ gpio_set_level(GPIO_BAT_LED_GREEN, BAT_LED_OFF);
+ break;
+ case LED_RED:
+ gpio_set_level(GPIO_BAT_LED_RED, BAT_LED_ON);
+ gpio_set_level(GPIO_BAT_LED_GREEN, BAT_LED_OFF);
+ break;
+ case LED_AMBER:
+ gpio_set_level(GPIO_BAT_LED_RED, BAT_LED_ON);
+ gpio_set_level(GPIO_BAT_LED_GREEN, BAT_LED_ON);
+ break;
+ case LED_GREEN:
+ gpio_set_level(GPIO_BAT_LED_RED, BAT_LED_OFF);
+ gpio_set_level(GPIO_BAT_LED_GREEN, BAT_LED_ON);
+ break;
+ default:
+ return EC_ERROR_UNKNOWN;
+ }
+ return EC_SUCCESS;
+}
+
+void led_get_brightness_range(enum ec_led_id led_id,
+ uint8_t *brightness_range)
+{
+ brightness_range[EC_LED_COLOR_RED] = 1;
+ brightness_range[EC_LED_COLOR_GREEN] = 1;
+}
+
+static int board_led_set_color_battery(enum led_color color)
+{
+ return bat_led_set_color(color);
+}
+
+static int board_led_set_color(enum ec_led_id led_id, enum led_color color)
+{
+ int rv;
+
+ led_auto_control(led_id, 0);
+ switch (led_id) {
+ case EC_LED_ID_BATTERY_LED:
+ rv = board_led_set_color_battery(color);
+ break;
+ default:
+ return EC_ERROR_UNKNOWN;
+ }
+ return rv;
+}
+
+int led_set_brightness(enum ec_led_id led_id, const uint8_t *brightness)
+{
+ if (brightness[EC_LED_COLOR_RED] != 0 &&
+ brightness[EC_LED_COLOR_GREEN] != 0)
+ board_led_set_color(led_id, LED_AMBER);
+ else if (brightness[EC_LED_COLOR_RED] != 0)
+ board_led_set_color(led_id, LED_RED);
+ else if (brightness[EC_LED_COLOR_GREEN] != 0)
+ board_led_set_color(led_id, LED_GREEN);
+ else
+ board_led_set_color(led_id, LED_OFF);
+
+ return EC_SUCCESS;
+}
+
+static void board_led_set_battery(void)
+{
+#ifdef CONFIG_CHARGER
+ static int battery_ticks;
+ uint32_t chflags = charge_get_flags();
+
+ battery_ticks++;
+
+ /* BAT LED behavior:
+ * Same as the chromeos spec
+ * Green/Amber for CHARGE_FLAG_FORCE_IDLE
+ */
+ switch (charge_get_state()) {
+ case PWR_STATE_CHARGE:
+ board_led_set_color_battery(LED_AMBER);
+ break;
+ case PWR_STATE_DISCHARGE:
+ /* Less than 3%, blink one second every two second */
+ if (!chipset_in_state(CHIPSET_STATE_ANY_OFF) &&
+ charge_get_percent() < CRITICAL_LOW_BATTERY_PERCENTAGE)
+ board_led_set_color_battery(
+ (battery_ticks % LED_TOTAL_2SECS_TICKS <
+ LED_ON_1SEC_TICKS) ? LED_AMBER : LED_OFF);
+ /* Less than 10%, blink one second every four seconds */
+ else if (!chipset_in_state(CHIPSET_STATE_ANY_OFF) &&
+ charge_get_percent() < LOW_BATTERY_PERCENTAGE)
+ board_led_set_color_battery(
+ (battery_ticks % LED_TOTAL_4SECS_TICKS <
+ LED_ON_1SEC_TICKS) ? LED_AMBER : LED_OFF);
+ else
+ board_led_set_color_battery(LED_OFF);
+ break;
+ case PWR_STATE_ERROR:
+ board_led_set_color_battery(
+ (battery_ticks % LED_TOTAL_2SECS_TICKS <
+ LED_ON_1SEC_TICKS) ? LED_RED : LED_OFF);
+ break;
+ case PWR_STATE_CHARGE_NEAR_FULL:
+ board_led_set_color_battery(LED_GREEN);
+ break;
+ case PWR_STATE_IDLE: /* External power connected in IDLE */
+ if (chflags & CHARGE_FLAG_FORCE_IDLE)
+ board_led_set_color_battery(
+ (battery_ticks % LED_TOTAL_4SECS_TICKS <
+ LED_ON_2SECS_TICKS) ? LED_GREEN : LED_AMBER);
+ else
+ board_led_set_color_battery(LED_GREEN);
+ break;
+ default:
+ /* Other states don't alter LED behavior */
+ break;
+ }
+#endif
+}
+
+
+static void led_second(void)
+{
+ if (led_auto_control_is_enabled(EC_LED_ID_BATTERY_LED))
+ board_led_set_battery();
+}
+DECLARE_HOOK(HOOK_SECOND, led_second, HOOK_PRIO_DEFAULT);
diff --git a/board/mchpevb1/usb_pd_policy.c b/board/mchpevb1/usb_pd_policy.c
new file mode 100644
index 0000000000..933cd54cba
--- /dev/null
+++ b/board/mchpevb1/usb_pd_policy.c
@@ -0,0 +1,399 @@
+/* Copyright 2017 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 "atomic.h"
+#include "charge_manager.h"
+#include "common.h"
+#include "console.h"
+#include "gpio.h"
+#include "hooks.h"
+#include "host_command.h"
+#include "registers.h"
+#include "system.h"
+#include "task.h"
+#include "timer.h"
+#include "util.h"
+#include "usb_mux.h"
+#include "usb_pd.h"
+
+
+#define CPRINTF(format, args...) cprintf(CC_USBPD, format, ## args)
+#define CPRINTS(format, args...) cprints(CC_USBPD, format, ## args)
+
+
+#define PDO_FIXED_FLAGS (PDO_FIXED_DUAL_ROLE | PDO_FIXED_DATA_SWAP |\
+ PDO_FIXED_COMM_CAP)
+
+/* 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_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_is_valid_input_voltage(int mv)
+{
+ return 1;
+}
+
+void pd_transition_voltage(int idx)
+{
+ /* No-operation: we are always 5V */
+}
+
+int pd_set_power_supply_ready(int port)
+{
+ /* Disable charging */
+ gpio_set_level(port ? GPIO_USB_C1_CHARGE_EN_L :
+ GPIO_USB_C0_CHARGE_EN_L, 1);
+ /* Provide VBUS */
+ gpio_set_level(port ? GPIO_USB_C1_5V_EN :
+ GPIO_USB_C0_5V_EN, 1);
+
+ /* notify host of power info change */
+ pd_send_host_event(PD_EVENT_POWER_CHANGE);
+
+ return EC_SUCCESS; /* we are ready */
+}
+
+void pd_power_supply_reset(int port)
+{
+ /* Disable VBUS */
+ gpio_set_level(port ? GPIO_USB_C1_5V_EN :
+ GPIO_USB_C0_5V_EN, 0);
+
+ /* notify host of power info change */
+ pd_send_host_event(PD_EVENT_POWER_CHANGE);
+}
+
+int pd_snk_is_vbus_provided(int port)
+{
+ return !gpio_get_level(port ? GPIO_USB_C1_VBUS_WAKE_L :
+ GPIO_USB_C0_VBUS_WAKE_L);
+}
+
+int pd_board_checks(void)
+{
+ return EC_SUCCESS;
+}
+
+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;
+}
+
+int pd_check_data_swap(int port, int data_role)
+{
+ /* Allow data swap if we are a UFP, otherwise don't allow */
+ return (data_role == PD_ROLE_UFP) ? 1 : 0;
+}
+
+int pd_check_vconn_swap(int port)
+{
+ /* in G3, do not allow vconn swap since pp5000_A rail is off */
+ return gpio_get_level(GPIO_PMIC_SLP_SUS_L);
+}
+
+void pd_execute_data_swap(int port, int data_role)
+{
+ /* Do nothing */
+}
+
+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);
+ }
+}
+
+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)
+ pd_request_data_swap(port);
+}
+/* ----------------- 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];
+/* DP Status VDM as returned by UFP */
+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;
+};
+
+/*
+ * timestamp of the next possible toggle to ensure the 2-ms spacing
+ * between IRQ_HPD.
+ */
+static uint64_t hpd_deadline[CONFIG_USB_PD_PORT_COUNT];
+
+#define PORT_TO_HPD(port) \
+ ((port) ? GPIO_USB_C1_DP_HPD : GPIO_USB_C0_DP_HPD)
+
+static void svdm_dp_post_config(int port)
+{
+ dp_flags[port] |= DP_FLAGS_DP_ON;
+ if (!(dp_flags[port] & DP_FLAGS_HPD_HI_PENDING))
+ return;
+
+ gpio_set_level(PORT_TO_HPD(port), 1);
+
+ /* set the minimum time delay (2ms) for the next HPD IRQ */
+ hpd_deadline[port] = get_time().val + HPD_USTREAM_DEBOUNCE_LVL;
+}
+
+static int svdm_dp_attention(int port, uint32_t *payload)
+{
+ int rc;
+ int cur_lvl;
+ int lvl = PD_VDO_DPSTS_HPD_LVL(payload[1]);
+ int irq = PD_VDO_DPSTS_HPD_IRQ(payload[1]);
+ enum gpio_signal hpd = PORT_TO_HPD(port);
+
+ cur_lvl = gpio_get_level(hpd);
+
+ dp_status[port] = payload[1];
+
+ rc = 1; /* ack */
+ /* Its initial DP status message prior to config */
+ if (!(dp_flags[port] & DP_FLAGS_DP_ON)) {
+ if (lvl)
+ dp_flags[port] |= DP_FLAGS_HPD_HI_PENDING;
+ return rc;
+ }
+
+ if (irq & cur_lvl) {
+ uint64_t now = get_time().val;
+ /* wait for the minimum spacing between IRQ_HPD if needed */
+ if (now < hpd_deadline[port])
+ usleep(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 */
+ hpd_deadline[port] = get_time().val + HPD_USTREAM_DEBOUNCE_LVL;
+ } else if (irq & !cur_lvl) {
+ CPRINTF("ERR:HPD:IRQ&LOW\n");
+ rc = 0; /* nak */
+ } else {
+ gpio_set_level(hpd, lvl);
+ /* set the minimum time delay (2ms) for the next HPD IRQ */
+ hpd_deadline[port] = get_time().val + HPD_USTREAM_DEBOUNCE_LVL;
+ }
+
+ return rc;
+}
+
+static void svdm_exit_dp_mode(int port)
+{
+ svdm_safe_dp_mode(port);
+ gpio_set_level(PORT_TO_HPD(port), 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 */
+