summaryrefslogtreecommitdiff
path: root/baseboard
diff options
context:
space:
mode:
authorJett Rink <jettrink@chromium.org>2018-04-20 10:08:44 -0600
committerchrome-bot <chrome-bot@chromium.org>2018-04-26 17:00:45 -0700
commita34e253d28d8ac4d787856ca9f4c790b4a5c2e27 (patch)
tree5547f0baa64c029503b5c36c584ba6546c0b35e2 /baseboard
parent043ee28da36f45eeda1015a65827ff5feca00b5d (diff)
downloadchrome-ec-a34e253d28d8ac4d787856ca9f4c790b4a5c2e27.tar.gz
octopus: move common usb/battery code into baseboard
Also implement a few remaining usb function for bip BRANCH=none BUG=b:75972988,b:76218141,b:74132235,b:78344554 TEST=verified yorp still functions Change-Id: I201408b5db689ac4a5bcab0011bc38698271b851 Signed-off-by: Jett Rink <jettrink@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/1024279 Reviewed-by: Furquan Shaikh <furquan@chromium.org>
Diffstat (limited to 'baseboard')
-rw-r--r--baseboard/octopus/baseboard.c73
-rw-r--r--baseboard/octopus/baseboard_battery.h44
-rw-r--r--baseboard/octopus/battery.c273
-rw-r--r--baseboard/octopus/build.mk2
-rw-r--r--baseboard/octopus/usb_pd_policy.c407
5 files changed, 799 insertions, 0 deletions
diff --git a/baseboard/octopus/baseboard.c b/baseboard/octopus/baseboard.c
index aeae096cc3..55d9f635fb 100644
--- a/baseboard/octopus/baseboard.c
+++ b/baseboard/octopus/baseboard.c
@@ -4,3 +4,76 @@
*/
/* Octopus family-specific configuration */
+
+#include "common.h"
+#include "console.h"
+#include "charge_manager.h"
+#include "charge_state.h"
+#include "usbc_ppc.h"
+#include "util.h"
+
+#define CPRINTSUSB(format, args...) cprints(CC_USBCHARGE, format, ## args)
+#define CPRINTFUSB(format, args...) cprintf(CC_USBCHARGE, format, ## args)
+
+int board_set_active_charge_port(int port)
+{
+ int is_valid_port = (port >= 0 &&
+ port < CONFIG_USB_PD_PORT_COUNT);
+ int i;
+
+ if (!is_valid_port && port != CHARGE_PORT_NONE)
+ return EC_ERROR_INVAL;
+
+
+ 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;
+ }
+
+ /* 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;
+}
+
+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);
+}
diff --git a/baseboard/octopus/baseboard_battery.h b/baseboard/octopus/baseboard_battery.h
new file mode 100644
index 0000000000..53e3a4df65
--- /dev/null
+++ b/baseboard/octopus/baseboard_battery.h
@@ -0,0 +1,44 @@
+/* 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.
+ */
+
+/* Octopus baseboard battery configuration */
+
+#ifndef __CROS_EC_BASEBOARD_BATTERY_H
+#define __CROS_EC_BASEBOARD_BATTERY_H
+
+#include "battery.h"
+
+/* Number of writes needed to invoke battery cutoff command */
+#define SHIP_MODE_WRITES 2
+
+struct ship_mode_info {
+ const uint8_t reg_addr;
+ const uint16_t reg_data[SHIP_MODE_WRITES];
+};
+
+struct fet_info {
+ const int mfgacc_support;
+ const uint8_t reg_addr;
+ const uint16_t reg_mask;
+ const uint16_t disconnect_val;
+};
+
+struct fuel_gauge_info {
+ const char *manuf_name;
+ const char *device_name;
+ const uint8_t override_nil;
+ const struct ship_mode_info ship_mode;
+ const struct fet_info fet;
+};
+struct board_batt_params {
+ const struct fuel_gauge_info fuel_gauge;
+ const struct battery_info batt_info;
+};
+
+/* Forward declare variant specific data used by common octopus code */
+extern const struct board_batt_params board_battery_info[];
+extern const enum battery_type DEFAULT_BATTERY_TYPE;
+
+#endif /* __CROS_EC_BASEBOARD_BATTERY_H */
diff --git a/baseboard/octopus/battery.c b/baseboard/octopus/battery.c
new file mode 100644
index 0000000000..99d357e553
--- /dev/null
+++ b/baseboard/octopus/battery.c
@@ -0,0 +1,273 @@
+/* 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"
+#include "baseboard_battery.h"
+#include "battery_smart.h"
+#include "charge_state.h"
+#include "common.h"
+#include "console.h"
+#include "gpio.h"
+#include "hooks.h"
+#include "util.h"
+
+#define CPRINTS(format, args...) cprints(CC_CHARGER, format, ## args)
+
+
+static enum battery_present batt_pres_prev = BP_NOT_SURE;
+
+/* Get type of the battery connected on the board */
+static int board_get_battery_type(void)
+{
+ char manu_name[32], device_name[32];
+ int i;
+ static enum battery_type board_battery_type = BATTERY_TYPE_COUNT;
+
+ /*
+ * If board_battery_type is not the default value, then can return here
+ * as there is no need to query the fuel gauge.
+ */
+ if (board_battery_type != BATTERY_TYPE_COUNT)
+ return board_battery_type;
+
+ /* Get the manufacture name. If can't read then just exit */
+ if (battery_manufacturer_name(manu_name, sizeof(manu_name)))
+ return board_battery_type;
+
+ /*
+ * Compare the manufacturer name read from the fuel gauge to the
+ * manufacture names defined in the board_battery_info table above. If
+ * a device name has been specified in the board_battery_info table,
+ * then both the manufacture and device name must match.
+ */
+ for (i = 0; i < BATTERY_TYPE_COUNT; i++) {
+ const struct fuel_gauge_info * const fuel_gauge =
+ &board_battery_info[i].fuel_gauge;
+
+ if (strcasecmp(manu_name, fuel_gauge->manuf_name))
+ continue;
+
+ if (fuel_gauge->device_name == NULL) {
+ board_battery_type = i;
+ break;
+ }
+
+ if (battery_device_name(device_name, sizeof(device_name)))
+ continue;
+
+ if (strcasecmp(device_name, fuel_gauge->device_name))
+ continue;
+
+ CPRINTS("found batt:%s", fuel_gauge->manuf_name);
+ board_battery_type = i;
+
+ break;
+ }
+
+ return board_battery_type;
+}
+
+/*
+ * Initialize the battery type for the board.
+ *
+ * Very first battery board_battery_info is called by the charger driver to
+ * initialize the charger parameters hence initialize the battery type for the
+ * board as soon as the I2C is initialized.
+ */
+static void board_init_battery_type(void)
+{
+ if (board_get_battery_type() == BATTERY_TYPE_COUNT)
+ CPRINTS("battery not found");
+}
+DECLARE_HOOK(HOOK_INIT, board_init_battery_type, HOOK_PRIO_INIT_I2C + 1);
+
+static inline const struct board_batt_params *board_get_batt_params(void)
+{
+ int type = board_get_battery_type();
+
+ return &board_battery_info[type == BATTERY_TYPE_COUNT ?
+ DEFAULT_BATTERY_TYPE : type];
+}
+
+const struct battery_info *battery_get_info(void)
+{
+ return &board_get_batt_params()->batt_info;
+}
+
+int board_cut_off_battery(void)
+{
+ int rv;
+ int cmd;
+ int data;
+ int type = board_get_battery_type();
+
+ /* If battery type is unknown can't send ship mode command */
+ if (type == BATTERY_TYPE_COUNT)
+ return EC_RES_ERROR;
+
+ /* Ship mode command must be sent twice to take effect */
+ cmd = board_battery_info[type].fuel_gauge.ship_mode.reg_addr;
+ data = board_battery_info[type].fuel_gauge.ship_mode.reg_data[0];
+ rv = sb_write(cmd, data);
+ if (rv != EC_SUCCESS)
+ return EC_RES_ERROR;
+
+ data = board_battery_info[type].fuel_gauge.ship_mode.reg_data[1];
+ rv = sb_write(cmd, data);
+
+ return rv ? EC_RES_ERROR : EC_RES_SUCCESS;
+}
+
+int charger_profile_override(struct charge_state_data *curr)
+{
+ int type = board_get_battery_type();
+
+ /*
+ * Some batteries, when fully discharged, may request 0 voltage/current
+ * which can then inadvertently disable the charger leading to the
+ * battery not waking up. For this type of battery, marked by
+ * override_nil being set, if SOC is 0 and requested voltage/current is
+ * 0, then use precharge current and max voltage instead.
+ */
+ if (type != BATTERY_TYPE_COUNT &&
+ board_battery_info[type].fuel_gauge.override_nil) {
+ int v = board_battery_info[type].batt_info.voltage_max;
+ int i = board_battery_info[type].batt_info.precharge_current;
+
+ if (curr->requested_voltage == 0 &&
+ curr->requested_current == 0 &&
+ curr->batt.state_of_charge == 0) {
+ /*
+ * Battery is dead, override with precharge current and
+ * max voltage setting for the battery.
+ */
+ curr->requested_voltage = v;
+ curr->requested_current = i;
+ }
+ }
+
+ return 0;
+}
+
+enum battery_present battery_hw_present(void)
+{
+ /* The GPIO is low when the battery is physically present */
+ return gpio_get_level(GPIO_EC_BATT_PRES_L) ? BP_NO : BP_YES;
+}
+
+static int battery_init(void)
+{
+ int batt_status;
+
+ return battery_status(&batt_status) ? 0 :
+ !!(batt_status & STATUS_INITIALIZED);
+}
+
+/*
+ * This function checks the charge/discharge FET status bits. Each battery type
+ * supported provides the register address, mask, and disconnect value for these
+ * 2 FET status bits. If the FET status matches the disconnected value, then
+ * BATTERY_DISCONNECTED is returned. This function is required to handle the
+ * cases when the fuel gauge is awake and will return a non-zero state of
+ * charge, but is not able yet to provide power (i.e. discharge FET is not
+ * active). By returning BATTERY_DISCONNECTED the AP will not be powered up
+ * until either the external charger is able to provided enough power, or
+ * the battery is able to provide power and thus prevent a brownout when the
+ * AP is powered on by the EC.
+ */
+static int battery_check_disconnect(void)
+{
+ int rv;
+ int reg;
+ uint8_t data[6];
+ int type = board_get_battery_type();
+
+ /* If battery type is not known, can't check CHG/DCHG FETs */
+ if (type == BATTERY_TYPE_COUNT) {
+ /* Still don't know, so return here */
+ return BATTERY_DISCONNECT_ERROR;
+ }
+
+ /* Read the status of charge/discharge FETs */
+ if (board_battery_info[type].fuel_gauge.fet.mfgacc_support == 1) {
+ rv = sb_read_mfgacc(PARAM_OPERATION_STATUS,
+ SB_ALT_MANUFACTURER_ACCESS, data, sizeof(data));
+ /* Get the lowest 16bits of the OperationStatus() data */
+ reg = data[2] | data[3] << 8;
+ } else
+ rv = sb_read(board_battery_info[type].fuel_gauge.fet.reg_addr,
+ &reg);
+
+ if (rv)
+ return BATTERY_DISCONNECT_ERROR;
+
+ CPRINTS("Battery FET: reg 0x%04x mask 0x%04x disc 0x%04x", reg,
+ board_battery_info[type].fuel_gauge.fet.reg_mask,
+ board_battery_info[type].fuel_gauge.fet.disconnect_val);
+ reg &= board_battery_info[type].fuel_gauge.fet.reg_mask;
+ if (reg == board_battery_info[type].fuel_gauge.fet.disconnect_val)
+ return BATTERY_DISCONNECTED;
+
+ return BATTERY_NOT_DISCONNECTED;
+}
+
+/*
+ * Physical detection of battery.
+ */
+static enum battery_present battery_check_present_status(void)
+{
+ enum battery_present batt_pres;
+ int batt_disconnect_status;
+
+ /* Get the physical hardware status */
+ batt_pres = battery_hw_present();
+
+ /*
+ * If the battery is not physically connected, then no need to perform
+ * any more checks.
+ */
+ if (batt_pres != BP_YES)
+ return batt_pres;
+
+ /*
+ * If the battery is present now and was present last time we checked,
+ * return early.
+ */
+ if (batt_pres == batt_pres_prev)
+ return batt_pres;
+
+ /*
+ * Check battery disconnect status. If we are unable to read battery
+ * disconnect status, then return BP_NOT_SURE. Battery could be in ship
+ * mode and might require pre-charge current to wake it up. BP_NO is not
+ * returned here because charger state machine will not provide
+ * pre-charge current assuming that battery is not present.
+ */
+ batt_disconnect_status = battery_check_disconnect();
+ if (batt_disconnect_status == BATTERY_DISCONNECT_ERROR)
+ return BP_NOT_SURE;
+
+ /*
+ * Ensure that battery is:
+ * 1. Not in cutoff
+ * 2. Not disconnected
+ * 3. Initialized
+ */
+ if (battery_is_cut_off() != BATTERY_CUTOFF_STATE_NORMAL ||
+ batt_disconnect_status != BATTERY_NOT_DISCONNECTED ||
+ battery_init() == 0) {
+ batt_pres = BP_NO;
+ }
+
+ return batt_pres;
+}
+
+enum battery_present battery_is_present(void)
+{
+ batt_pres_prev = battery_check_present_status();
+ return batt_pres_prev;
+}
diff --git a/baseboard/octopus/build.mk b/baseboard/octopus/build.mk
index 24e4cced5f..5a5942a0c6 100644
--- a/baseboard/octopus/build.mk
+++ b/baseboard/octopus/build.mk
@@ -7,3 +7,5 @@
#
baseboard-y=baseboard.o
+baseboard-$(CONFIG_BATTERY_SMART)+=battery.o
+baseboard-$(CONFIG_USB_POWER_DELIVERY)+=usb_pd_policy.o
diff --git a/baseboard/octopus/usb_pd_policy.c b/baseboard/octopus/usb_pd_policy.c
new file mode 100644
index 0000000000..6c90a53d56
--- /dev/null
+++ b/baseboard/octopus/usb_pd_policy.c
@@ -0,0 +1,407 @@
+/* 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.
+ */
+
+/* Shared USB-C policy for octopus boards */
+
+#include "charge_manager.h"
+#include "common.h"
+#include "compile_time_macros.h"
+#include "console.h"
+#include "ec_commands.h"
+#include "gpio.h"
+#include "system.h"
+#include "usb_mux.h"
+#include "usb_pd.h"
+#include "usbc_ppc.h"
+#include "util.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)
+
+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)
+{
+ /*
+ * Allow data swap if we are a UFP, otherwise don't allow.
+ *
+ * When we are still in the Read-Only firmware, avoid swapping roles
+ * so we don't jump in RW as a SNK/DFP and potentially confuse the
+ * power supply by sending a soft-reset with wrong data role.
+ */
+ return (data_role == PD_ROLE_UFP) &&
+ (system_get_image_copy() != SYSTEM_IMAGE_RO) ? 1 : 0;
+}
+
+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)
+{
+ /* Only allow vconn swap if pp5000_A rail is enabled */
+ return gpio_get_level(GPIO_EN_PP5000);
+}
+
+void pd_execute_data_swap(int port, int data_role)
+{
+ /* Do nothing */
+}
+
+int pd_is_valid_input_voltage(int mv)
+{
+ return 1;
+}
+
+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;
+}
+
+void pd_transition_voltage(int idx)
+{
+ /* No-operation: we are always 5V */
+}
+
+#ifdef CONFIG_USB_PD_VBUS_DETECT_PPC
+int pd_snk_is_vbus_provided(int port)
+{
+ return ppc_is_vbus_present(port);
+}
+#endif
+
+void typec_set_source_current_limit(int port, int rp)
+{
+ ppc_set_vbus_source_current_limit(port, rp);
+}
+
+int board_vbus_source_enabled(int port)
+{
+ return ppc_is_sourcing_vbus(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];
+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 */