summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKarthikeyan Ramasubramanian <kramasub@google.com>2019-10-02 15:09:45 -0600
committerCommit Bot <commit-bot@chromium.org>2019-11-19 00:51:46 +0000
commitaedd6b99c9740d72368261bd75060599ede4ab95 (patch)
tree250883a58d20ce6cdd4c293149ef9188915aff16
parent737737e711786d69555825291e0f0f6a1779073c (diff)
downloadchrome-ec-aedd6b99c9740d72368261bd75060599ede4ab95.tar.gz
Add a board specific helper to return USB PD port count
Certain SKUs of certain boards have less number of USB PD ports than configured in CONFIG_USB_PD_PORT_MAX_COUNT. Hence define an overrideable board specific helper to return the number of USB PD ports. This helps to avoid initiating a PD firmware update in SKUs where there are less number of USB PD ports. Also update charge manager to ensure that absent/ invalid PD ports are skipped during port initialization and management. BUG=b:140816510, b:143196487 BRANCH=octopus TEST=make -j buildall; Boot to ChromeOS in bobba(2A + 2C config) and garg(2A + 1C + 1HDMI config). Change-Id: Ie345cef470ad878ec443ddf4797e5d17cfe1f61e Signed-off-by: Karthikeyan Ramasubramanian <kramasub@google.com> Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/1879338 Tested-by: Karthikeyan Ramasubramanian <kramasub@chromium.org> Reviewed-by: Jett Rink <jettrink@chromium.org> Commit-Queue: Karthikeyan Ramasubramanian <kramasub@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/1922235 Reviewed-by: Karthikeyan Ramasubramanian <kramasub@chromium.org>
-rw-r--r--chip/mec1322/system.c26
-rw-r--r--chip/stm32/usb_pd_phy.c2
-rw-r--r--common/charge_manager.c60
-rw-r--r--common/charge_ramp_sw.c4
-rw-r--r--common/host_command_pd.c2
-rw-r--r--common/i2c_master.c3
-rw-r--r--common/pd_log.c4
-rw-r--r--common/peripheral.c3
-rw-r--r--common/system.c2
-rw-r--r--common/usb_charger.c2
-rw-r--r--common/usb_pd_policy.c11
-rw-r--r--common/usb_pd_protocol.c46
-rw-r--r--common/usb_pd_tcpc.c14
-rw-r--r--driver/charger/bd9995x.c4
-rw-r--r--driver/ppc/nx20p348x.c2
-rw-r--r--driver/ppc/sn5s330.c2
-rw-r--r--driver/tcpm/it83xx.c4
-rw-r--r--driver/tcpm/tcpci.c5
-rw-r--r--driver/usb_mux.c4
-rw-r--r--include/usb_pd.h17
-rw-r--r--test/charge_manager.c10
-rw-r--r--test/usb_pd.c2
22 files changed, 160 insertions, 69 deletions
diff --git a/chip/mec1322/system.c b/chip/mec1322/system.c
index db5e0934c3..d948e98b6f 100644
--- a/chip/mec1322/system.c
+++ b/chip/mec1322/system.c
@@ -17,6 +17,7 @@
#include "hooks.h"
#include "task.h"
#include "timer.h"
+#include "usb_pd.h"
#include "util.h"
#include "spi.h"
@@ -296,14 +297,27 @@ void system_hibernate(uint32_t seconds, uint32_t microseconds)
* Leave USB-C charging enabled in hibernate, in order to
* allow wake-on-plug. 5V enable must be pulled low.
*/
-#if CONFIG_USB_PD_PORT_MAX_COUNT > 0
- gpio_set_flags(GPIO_USB_C0_5V_EN, GPIO_PULL_DOWN | GPIO_INPUT);
- gpio_set_level(GPIO_USB_C0_CHARGE_EN_L, 0);
+ switch (board_get_usb_pd_port_count()) {
+#if CONFIG_USB_PD_PORT_MAX_COUNT >= 2
+ case 2:
+ gpio_set_flags(GPIO_USB_C1_5V_EN, GPIO_PULL_DOWN | GPIO_INPUT);
+ gpio_set_level(GPIO_USB_C1_CHARGE_EN_L, 0);
+ /* Fall through */
#endif
-#if CONFIG_USB_PD_PORT_MAX_COUNT > 1
- gpio_set_flags(GPIO_USB_C1_5V_EN, GPIO_PULL_DOWN | GPIO_INPUT);
- gpio_set_level(GPIO_USB_C1_CHARGE_EN_L, 0);
+#if CONFIG_USB_PD_PORT_MAX_COUNT >= 1
+ case 1:
+ gpio_set_flags(GPIO_USB_C0_5V_EN, GPIO_PULL_DOWN | GPIO_INPUT);
+ gpio_set_level(GPIO_USB_C0_CHARGE_EN_L, 0);
+ /* Fall through */
#endif
+ case 0:
+ /* Nothing to do but break */
+ break;
+ default:
+ /* More ports needs to be defined */
+ ASSERT(false);
+ break;
+ }
#endif /* CONFIG_USB_PD_PORT_MAX_COUNT */
if (hibernate_wake_pins_used > 0) {
diff --git a/chip/stm32/usb_pd_phy.c b/chip/stm32/usb_pd_phy.c
index 43e0b923e8..7bc43497dc 100644
--- a/chip/stm32/usb_pd_phy.c
+++ b/chip/stm32/usb_pd_phy.c
@@ -452,7 +452,7 @@ void pd_rx_handler(void)
int next_idx;
pending = STM32_EXTI_PR;
- for (i = 0; i < CONFIG_USB_PD_PORT_MAX_COUNT; i++) {
+ for (i = 0; i < board_get_usb_pd_port_count(); i++) {
if (pending & EXTI_COMP_MASK(i)) {
rx_edge_ts[i][rx_edge_ts_idx[i]].val = get_time().val;
next_idx = (rx_edge_ts_idx[i] ==
diff --git a/common/charge_manager.c b/common/charge_manager.c
index 1ccc6b8b71..88befb345f 100644
--- a/common/charge_manager.c
+++ b/common/charge_manager.c
@@ -123,7 +123,7 @@ enum charge_manager_change_type {
static int is_pd_port(int port)
{
- return port >= 0 && port < CONFIG_USB_PD_PORT_MAX_COUNT;
+ return port >= 0 && port < board_get_usb_pd_port_count();
}
static int is_sink(int port)
@@ -134,6 +134,25 @@ static int is_sink(int port)
return pd_get_role(port) == PD_ROLE_SINK;
}
+/**
+ * Some of the SKUs in certain boards have less number of USB PD ports than
+ * defined in CONFIG_USB_PD_PORT_MAX_COUNT. With the charge port configuration
+ * for DEDICATED_PORT towards the end, this will lead to holes in the static
+ * configuration. The ports that fall in that hole are invalid and this function
+ * is used to check the validity of the ports.
+ */
+static int is_valid_port(int port)
+{
+ if (port < 0 || port >= CHARGE_PORT_COUNT)
+ return 0;
+
+ /* Check if the port falls in the hole */
+ if (port >= board_get_usb_pd_port_count() &&
+ port < CONFIG_USB_PD_PORT_MAX_COUNT)
+ return 0;
+ return 1;
+}
+
#ifndef TEST_BUILD
static int is_connected(int port)
{
@@ -173,6 +192,8 @@ static void charge_manager_init(void)
int i, j;
for (i = 0; i < CHARGE_PORT_COUNT; ++i) {
+ if (!is_valid_port(i))
+ continue;
for (j = 0; j < CHARGE_SUPPLIER_COUNT; ++j) {
available_charge[j][i].current =
CHARGE_CURRENT_UNINITIALIZED;
@@ -204,14 +225,17 @@ static int charge_manager_is_seeded(void)
if (is_seeded)
return 1;
- for (i = 0; i < CHARGE_SUPPLIER_COUNT; ++i)
- for (j = 0; j < CHARGE_PORT_COUNT; ++j)
+ for (i = 0; i < CHARGE_SUPPLIER_COUNT; ++i) {
+ for (j = 0; j < CHARGE_PORT_COUNT; ++j) {
+ if (!is_valid_port(j))
+ continue;
if (available_charge[i][j].current ==
CHARGE_CURRENT_UNINITIALIZED ||
available_charge[i][j].voltage ==
CHARGE_VOLTAGE_UNINITIALIZED)
return 0;
-
+ }
+ }
is_seeded = 1;
return 1;
}
@@ -468,6 +492,9 @@ static int charge_manager_get_ceil(int port)
int ceil = CHARGE_CEIL_NONE;
int val, i;
+ if (!is_valid_port(port))
+ return ceil;
+
for (i = 0; i < CEIL_REQUESTOR_COUNT; ++i) {
val = charge_ceil[port][i];
if (val != CHARGE_CEIL_NONE &&
@@ -506,6 +533,10 @@ static void charge_manager_get_best_charge_port(int *new_port,
*/
for (i = 0; i < CHARGE_SUPPLIER_COUNT; ++i)
for (j = 0; j < CHARGE_PORT_COUNT; ++j) {
+ /* Skip this port if it is not valid. */
+ if (!is_valid_port(j))
+ continue;
+
/*
* Skip this supplier if there is no
* available charge.
@@ -723,7 +754,7 @@ static void charge_manager_refresh(void)
if (updated_old_port != CHARGE_PORT_NONE)
save_log[updated_old_port] = 1;
- for (i = 0; i < CONFIG_USB_PD_PORT_MAX_COUNT; ++i)
+ for (i = 0; i < board_get_usb_pd_port_count(); ++i)
if (save_log[i])
charge_manager_save_log(i);
#endif
@@ -776,6 +807,11 @@ static void charge_manager_make_change(enum charge_manager_change_type change,
int i;
int clear_override = 0;
+ if (!is_valid_port(port)) {
+ CPRINTS("%s: p%d invalid", __func__, port);
+ return;
+ }
+
/* Determine if this is a change which can affect charge status */
switch (change) {
case CHANGE_CHARGE:
@@ -947,6 +983,9 @@ void charge_manager_leave_safe_mode(void)
void charge_manager_set_ceil(int port, enum ceil_requestor requestor, int ceil)
{
+ if (!is_valid_port(port))
+ return;
+
if (charge_ceil[port][requestor] != ceil) {
charge_ceil[port][requestor] = ceil;
if (port == charge_port && charge_manager_is_seeded())
@@ -1073,7 +1112,7 @@ static int can_supply_max_current(int port)
if (!is_active_source(port))
/* Non-active ports don't get 3A */
return 0;
- for (p = 0; p < CONFIG_USB_PD_PORT_MAX_COUNT; p++) {
+ for (p = 0; p < board_get_usb_pd_port_count(); p++) {
if (p == port)
continue;
if (source_port_rp[p] ==
@@ -1101,7 +1140,7 @@ void charge_manager_source_port(int port, int enable)
return;
/* Set port limit according to policy */
- for (p = 0; p < CONFIG_USB_PD_PORT_MAX_COUNT; p++) {
+ for (p = 0; p < board_get_usb_pd_port_count(); p++) {
rp = can_supply_max_current(p) ?
CONFIG_USB_PD_MAX_SINGLE_SOURCE_CURRENT :
CONFIG_USB_PD_PULLUP;
@@ -1141,6 +1180,11 @@ static int hc_pd_power_info(struct host_cmd_handler_args *args)
if (port == PD_POWER_CHARGING_PORT)
port = charge_port;
+ /*
+ * Not checking for invalid port here, because it might break existing
+ * contract with ectool users. The invalid ports will have the response
+ * voltage, current and power parameters set to 0.
+ */
if (port >= CHARGE_PORT_COUNT)
return EC_RES_INVALID_PARAM;
@@ -1243,7 +1287,7 @@ static void charge_manager_set_external_power_limit(int current_lim,
if (voltage_lim == EC_POWER_LIMIT_NONE)
voltage_lim = PD_MAX_VOLTAGE_MV;
- for (port = 0; port < CONFIG_USB_PD_PORT_MAX_COUNT; ++port) {
+ for (port = 0; port < board_get_usb_pd_port_count(); ++port) {
charge_manager_set_ceil(port, CEIL_REQUESTOR_HOST, current_lim);
pd_set_external_voltage_limit(port, voltage_lim);
}
diff --git a/common/charge_ramp_sw.c b/common/charge_ramp_sw.c
index 817ebbec95..aece42642f 100644
--- a/common/charge_ramp_sw.c
+++ b/common/charge_ramp_sw.c
@@ -162,7 +162,7 @@ void chg_ramp_task(void *u)
static uint8_t values_have_changed_at_least_once;
/* Clear last OCP supplier to guarantee we ramp on first connect */
- for (i = 0; i < CONFIG_USB_PD_PORT_MAX_COUNT; i++)
+ for (i = 0; i < board_get_usb_pd_port_count(); i++)
oc_info[i][0].sup = CHARGE_SUPPLIER_NONE;
while (1) {
@@ -367,7 +367,7 @@ static int command_chgramp(int argc, char **argv)
ccprintf("Chg Ramp:\nState: %d\nMin ICL: %d\nActive ICL: %d\n",
ramp_st, min_icl, active_icl);
- for (port = 0; port < CONFIG_USB_PD_PORT_MAX_COUNT; port++) {
+ for (port = 0; port < board_get_usb_pd_port_count(); port++) {
ccprintf("Port %d:\n", port);
ccprintf(" OC idx:%d\n", oc_info_idx[port]);
for (i = 0; i < RAMP_COUNT; i++) {
diff --git a/common/host_command_pd.c b/common/host_command_pd.c
index 3e51943311..56ca71fc71 100644
--- a/common/host_command_pd.c
+++ b/common/host_command_pd.c
@@ -139,7 +139,7 @@ static void pd_service_tcpc_ports(uint16_t port_status)
{
int i;
- for (i = 0; i < CONFIG_USB_PD_PORT_MAX_COUNT; i++) {
+ for (i = 0; i < board_get_usb_pd_port_count(); i++) {
if ((port_status & (PD_STATUS_TCPC_ALERT_0 << i)) &&
pd_is_port_enabled(i))
tcpc_alert(i);
diff --git a/common/i2c_master.c b/common/i2c_master.c
index f86e5aafa3..89c5be5335 100644
--- a/common/i2c_master.c
+++ b/common/i2c_master.c
@@ -14,6 +14,7 @@
#include "i2c.h"
#include "system.h"
#include "task.h"
+#include "usb_pd.h"
#include "usb_pd_tcpm.h"
#include "util.h"
#include "watchdog.h"
@@ -784,7 +785,7 @@ static void i2c_passthru_protect_tcpc_ports(void)
return;
}
- for (i = 0; i < CONFIG_USB_PD_PORT_MAX_COUNT; i++) {
+ for (i = 0; i < board_get_usb_pd_port_count(); i++) {
/* TCPC tunnel not configured. No need to protect anything */
if (!tcpc_config[i].i2c_slave_addr)
continue;
diff --git a/common/pd_log.c b/common/pd_log.c
index 72bf8e7765..7b6a1d2c2c 100644
--- a/common/pd_log.c
+++ b/common/pd_log.c
@@ -68,7 +68,7 @@ dequeue_retry:
if (r->type == PD_EVENT_NO_ENTRY) {
int i, res;
incoming_logs = 0;
- for (i = 0; i < CONFIG_USB_PD_PORT_MAX_COUNT; ++i) {
+ for (i = 0; i < board_get_usb_pd_port_count(); ++i) {
/* only accessories who knows Google logging format */
if (pd_get_identity_vid(i) != USB_VID_GOOGLE)
continue;
@@ -96,7 +96,7 @@ static int hc_pd_write_log_entry(struct host_cmd_handler_args *args)
if (type < PD_EVENT_MCU_BASE || type >= PD_EVENT_ACC_BASE)
return EC_RES_INVALID_PARAM;
- if (port > 0 && port >= CONFIG_USB_PD_PORT_MAX_COUNT)
+ if (port > 0 && port >= board_get_usb_pd_port_count())
return EC_RES_INVALID_PARAM;
switch (type) {
diff --git a/common/peripheral.c b/common/peripheral.c
index 0a76e3a0df..810b1279fd 100644
--- a/common/peripheral.c
+++ b/common/peripheral.c
@@ -7,6 +7,7 @@
#include "compile_time_macros.h"
#include "ec_commands.h"
#include "host_command.h"
+#include "usb_pd.h"
#include "usb_pd_tcpm.h"
#ifdef CONFIG_HOSTCMD_LOCATE_CHIP
@@ -31,7 +32,7 @@ static int hc_locate_chip(struct host_cmd_handler_args *args)
break;
case EC_CHIP_TYPE_TCPC:
#if defined(CONFIG_USB_PD_PORT_MAX_COUNT) && !defined(CONFIG_USB_PD_TCPC)
- if (params->index >= CONFIG_USB_PD_PORT_MAX_COUNT)
+ if (params->index >= board_get_usb_pd_port_count())
return EC_RES_OVERFLOW;
resp->bus_type = EC_BUS_TYPE_I2C;
resp->i2c_info.port = tcpc_config[params->index].i2c_host_port;
diff --git a/common/system.c b/common/system.c
index 454f0daebf..80d41f5022 100644
--- a/common/system.c
+++ b/common/system.c
@@ -916,7 +916,7 @@ static int handle_pending_reboot(enum ec_reboot_cmd cmd)
{
int port;
- for (port = 0; port < CONFIG_USB_PD_PORT_MAX_COUNT;
+ for (port = 0; port < board_get_usb_pd_port_count();
port++)
pd_set_suspend(port, 1);
}
diff --git a/common/usb_charger.c b/common/usb_charger.c
index aba3c92134..0de12c856a 100644
--- a/common/usb_charger.c
+++ b/common/usb_charger.c
@@ -86,7 +86,7 @@ static void usb_charger_init(void)
int i;
/* Initialize all charge suppliers */
- for (i = 0; i < CONFIG_USB_PD_PORT_MAX_COUNT; i++) {
+ for (i = 0; i < board_get_usb_pd_port_count(); i++) {
charge_manager_update_charge(CHARGE_SUPPLIER_PROPRIETARY,
i, NULL);
charge_manager_update_charge(CHARGE_SUPPLIER_BC12_CDP,
diff --git a/common/usb_pd_policy.c b/common/usb_pd_policy.c
index e38195bf14..5566c25214 100644
--- a/common/usb_pd_policy.c
+++ b/common/usb_pd_policy.c
@@ -654,7 +654,7 @@ static int command_pe(int argc, char **argv)
return EC_ERROR_PARAM_COUNT;
/* command: pe <port> <subcmd> <args> */
port = strtoi(argv[1], &e, 10);
- if (*e || port >= CONFIG_USB_PD_PORT_MAX_COUNT)
+ if (*e || port >= board_get_usb_pd_port_count())
return EC_ERROR_PARAM2;
if (!strncasecmp(argv[2], "dump", 4))
dump_pe(port);
@@ -871,7 +871,7 @@ static int hc_remote_pd_discovery(struct host_cmd_handler_args *args)
const uint8_t *port = args->params;
struct ec_params_usb_pd_discovery_entry *r = args->response;
- if (*port >= CONFIG_USB_PD_PORT_MAX_COUNT)
+ if (*port >= board_get_usb_pd_port_count())
return EC_RES_INVALID_PARAM;
r->vid = pd_get_identity_vid(*port);
@@ -893,7 +893,7 @@ static int hc_remote_pd_get_amode(struct host_cmd_handler_args *args)
const struct ec_params_usb_pd_get_mode_request *p = args->params;
struct ec_params_usb_pd_get_mode_response *r = args->response;
- if (p->port >= CONFIG_USB_PD_PORT_MAX_COUNT)
+ if (p->port >= board_get_usb_pd_port_count())
return EC_RES_INVALID_PARAM;
/* no more to send */
@@ -1043,3 +1043,8 @@ void pd_set_vbus_discharge(int port, int enable)
mutex_unlock(&discharge_lock[port]);
}
#endif /* CONFIG_USB_PD_DISCHARGE */
+
+__attribute__((weak)) uint8_t board_get_usb_pd_port_count(void)
+{
+ return CONFIG_USB_PD_PORT_MAX_COUNT;
+}
diff --git a/common/usb_pd_protocol.c b/common/usb_pd_protocol.c
index 38845c287a..753839fb32 100644
--- a/common/usb_pd_protocol.c
+++ b/common/usb_pd_protocol.c
@@ -760,11 +760,11 @@ static inline void set_state(int port, enum pd_states next_state)
#ifdef CONFIG_LOW_POWER_IDLE
/* If a PD device is attached then disable deep sleep */
- for (i = 0; i < CONFIG_USB_PD_PORT_MAX_COUNT; i++) {
+ for (i = 0; i < board_get_usb_pd_port_count(); i++) {
if (pd_capable(i))
break;
}
- if (i == CONFIG_USB_PD_PORT_MAX_COUNT)
+ if (i == board_get_usb_pd_port_count())
enable_sleep(SLEEP_MASK_USB_PD);
else
disable_sleep(SLEEP_MASK_USB_PD);
@@ -1350,7 +1350,7 @@ void pd_soft_reset(void)
{
int i;
- for (i = 0; i < CONFIG_USB_PD_PORT_MAX_COUNT; ++i)
+ for (i = 0; i < board_get_usb_pd_port_count(); ++i)
if (pd_is_connected(i)) {
set_state(i, PD_STATE_SOFT_RESET);
task_wake(PD_PORT_TO_TASK_ID(i));
@@ -2219,7 +2219,7 @@ static void pd_update_try_source(void)
int batt_soc = get_battery_soc();
try_src = 0;
- for (i = 0; i < CONFIG_USB_PD_PORT_MAX_COUNT; i++)
+ for (i = 0; i < board_get_usb_pd_port_count(); i++)
try_src |= drp_state[i] == PD_DRP_TOGGLE_ON;
/*
@@ -2243,7 +2243,7 @@ static void pd_update_try_source(void)
* mode went from enabled to disabled and trying_source
* was active at that time.
*/
- for (i = 0; i < CONFIG_USB_PD_PORT_MAX_COUNT; i++)
+ for (i = 0; i < board_get_usb_pd_port_count(); i++)
pd[i].flags &= ~PD_FLAGS_TRY_SRC;
}
@@ -2258,7 +2258,7 @@ static void pd_update_snk_reset(void)
if (batt_soc < CONFIG_USB_PD_RESET_MIN_BATT_SOC)
return;
- for (i = 0; i < CONFIG_USB_PD_PORT_MAX_COUNT; i++) {
+ for (i = 0; i < board_get_usb_pd_port_count(); i++) {
if (pd[i].flags & PD_FLAGS_SNK_WAITING_BATT) {
/*
* Battery has gained sufficient charge to kick off PD
@@ -2579,13 +2579,13 @@ static void pd_init_tasks(void)
#if defined(HAS_TASK_CHIPSET) && defined(CONFIG_USB_PD_DUAL_ROLE)
/* Set dual-role state based on chipset power state */
if (chipset_in_state(CHIPSET_STATE_ANY_OFF))
- for (i = 0; i < CONFIG_USB_PD_PORT_MAX_COUNT; i++)
+ for (i = 0; i < board_get_usb_pd_port_count(); i++)
drp_state[i] = PD_DRP_FORCE_SINK;
else if (chipset_in_state(CHIPSET_STATE_ANY_SUSPEND))
- for (i = 0; i < CONFIG_USB_PD_PORT_MAX_COUNT; i++)
+ for (i = 0; i < board_get_usb_pd_port_count(); i++)
drp_state[i] = PD_DRP_TOGGLE_OFF;
else /* CHIPSET_STATE_ON */
- for (i = 0; i < CONFIG_USB_PD_PORT_MAX_COUNT; i++)
+ for (i = 0; i < board_get_usb_pd_port_count(); i++)
drp_state[i] = PD_DRP_TOGGLE_ON;
#endif
@@ -2600,7 +2600,7 @@ static void pd_init_tasks(void)
enable = 1;
#endif
#endif
- for (i = 0; i < CONFIG_USB_PD_PORT_MAX_COUNT; i++)
+ for (i = 0; i < board_get_usb_pd_port_count(); i++)
pd_comm_enabled[i] = enable;
CPRINTS("PD comm %sabled", enable ? "en" : "dis");
@@ -4438,7 +4438,7 @@ static void pd_chipset_resume(void)
{
int i;
- for (i = 0; i < CONFIG_USB_PD_PORT_MAX_COUNT; i++) {
+ for (i = 0; i < board_get_usb_pd_port_count(); i++) {
#ifdef CONFIG_CHARGE_MANAGER
if (charge_manager_get_active_charge_port() != i)
#endif
@@ -4455,7 +4455,7 @@ static void pd_chipset_suspend(void)
{
int i;
- for (i = 0; i < CONFIG_USB_PD_PORT_MAX_COUNT; i++)
+ for (i = 0; i < board_get_usb_pd_port_count(); i++)
pd_set_dual_role(i, PD_DRP_TOGGLE_OFF);
CPRINTS("PD:S0->S3");
}
@@ -4465,7 +4465,7 @@ static void pd_chipset_startup(void)
{
int i;
- for (i = 0; i < CONFIG_USB_PD_PORT_MAX_COUNT; i++) {
+ for (i = 0; i < board_get_usb_pd_port_count(); i++) {
pd_set_dual_role_no_wakeup(i, PD_DRP_TOGGLE_OFF);
pd[i].flags |= PD_FLAGS_CHECK_IDENTITY;
task_set_event(PD_PORT_TO_TASK_ID(i),
@@ -4481,7 +4481,7 @@ static void pd_chipset_shutdown(void)
{
int i;
- for (i = 0; i < CONFIG_USB_PD_PORT_MAX_COUNT; i++) {
+ for (i = 0; i < board_get_usb_pd_port_count(); i++) {
pd_set_dual_role_no_wakeup(i, PD_DRP_FORCE_SINK);
task_set_event(PD_PORT_TO_TASK_ID(i),
PD_EVENT_POWER_STATE_CHANGE |
@@ -4606,7 +4606,7 @@ static int remote_flashing(int argc, char **argv)
return EC_ERROR_PARAM_COUNT;
port = strtoi(argv[1], &e, 10);
- if (*e || port >= CONFIG_USB_PD_PORT_MAX_COUNT)
+ if (*e || port >= board_get_usb_pd_port_count())
return EC_ERROR_PARAM2;
cnt = 0;
@@ -4848,7 +4848,7 @@ static int command_pd(int argc, char **argv)
port = strtoi(argv[1], &e, 10);
if (argc < 3)
return EC_ERROR_PARAM_COUNT;
- if (*e || port >= CONFIG_USB_PD_PORT_MAX_COUNT)
+ if (*e || port >= board_get_usb_pd_port_count())
return EC_ERROR_PARAM2;
#if defined(CONFIG_CMD_PD) && defined(CONFIG_USB_PD_DUAL_ROLE)
@@ -5035,7 +5035,7 @@ DECLARE_CONSOLE_COMMAND(pd, command_pd,
static int hc_pd_ports(struct host_cmd_handler_args *args)
{
struct ec_response_usb_pd_ports *r = args->response;
- r->num_ports = CONFIG_USB_PD_PORT_MAX_COUNT;
+ r->num_ports = board_get_usb_pd_port_count();
args->response_size = sizeof(*r);
return EC_RES_SUCCESS;
@@ -5070,7 +5070,7 @@ static int hc_usb_pd_control(struct host_cmd_handler_args *args)
struct ec_response_usb_pd_control_v1 *r_v1 = args->response;
struct ec_response_usb_pd_control *r = args->response;
- if (p->port >= CONFIG_USB_PD_PORT_MAX_COUNT)
+ if (p->port >= board_get_usb_pd_port_count())
return EC_RES_INVALID_PARAM;
if (p->role >= USB_PD_CTRL_ROLE_COUNT ||
@@ -5151,7 +5151,7 @@ static int hc_remote_flash(struct host_cmd_handler_args *args)
int i, size, rv = EC_RES_SUCCESS;
timestamp_t timeout;
- if (port >= CONFIG_USB_PD_PORT_MAX_COUNT)
+ if (port >= board_get_usb_pd_port_count())
return EC_RES_INVALID_PARAM;
if (p->size + sizeof(*p) > args->params_size)
@@ -5276,7 +5276,7 @@ static int hc_remote_pd_dev_info(struct host_cmd_handler_args *args)
const uint8_t *port = args->params;
struct ec_params_usb_pd_rw_hash_entry *r = args->response;
- if (*port >= CONFIG_USB_PD_PORT_MAX_COUNT)
+ if (*port >= board_get_usb_pd_port_count())
return EC_RES_INVALID_PARAM;
r->dev_id = pd[*port].dev_id;
@@ -5303,7 +5303,7 @@ static int hc_remote_pd_chip_info(struct host_cmd_handler_args *args)
const struct ec_params_pd_chip_info *p = args->params;
struct ec_response_pd_chip_info_v1 *info;
- if (p->port >= CONFIG_USB_PD_PORT_MAX_COUNT)
+ if (p->port >= board_get_usb_pd_port_count())
return EC_RES_INVALID_PARAM;
if (tcpm_get_chip_info(p->port, p->live, &info))
@@ -5332,7 +5332,7 @@ static int hc_remote_pd_set_amode(struct host_cmd_handler_args *args)
{
const struct ec_params_usb_pd_set_mode_request *p = args->params;
- if ((p->port >= CONFIG_USB_PD_PORT_MAX_COUNT) ||
+ if ((p->port >= board_get_usb_pd_port_count()) ||
(!p->svid) || (!p->opos))
return EC_RES_INVALID_PARAM;
@@ -5371,7 +5371,7 @@ static int pd_control(struct host_cmd_handler_args *args)
const struct ec_params_pd_control *cmd = args->params;
int enable = 0;
- if (cmd->chip >= CONFIG_USB_PD_PORT_MAX_COUNT)
+ if (cmd->chip >= board_get_usb_pd_port_count())
return EC_RES_INVALID_PARAM;
/* Always allow disable command */
diff --git a/common/usb_pd_tcpc.c b/common/usb_pd_tcpc.c
index 0d53901acf..3e341f44c5 100644
--- a/common/usb_pd_tcpc.c
+++ b/common/usb_pd_tcpc.c
@@ -1047,11 +1047,11 @@ int tcpc_set_rx_enable(int port, int enable)
#if defined(CONFIG_LOW_POWER_IDLE) && !defined(CONFIG_USB_POWER_DELIVERY)
/* If any PD port is connected, then disable deep sleep */
- for (i = 0; i < CONFIG_USB_PD_PORT_MAX_COUNT; ++i)
+ for (i = 0; i < board_get_usb_pd_port_count(); ++i)
if (pd[i].rx_enabled)
break;
- if (i == CONFIG_USB_PD_PORT_MAX_COUNT)
+ if (i == board_get_usb_pd_port_count())
enable_sleep(SLEEP_MASK_USB_PD);
else
disable_sleep(SLEEP_MASK_USB_PD);
@@ -1099,7 +1099,7 @@ void tcpc_pre_init(void)
int i;
/* Mark as uninitialized */
- for (i = 0; i < CONFIG_USB_PD_PORT_MAX_COUNT; i++)
+ for (i = 0; i < board_get_usb_pd_port_count(); i++)
pd[i].power_status |= TCPC_REG_POWER_STATUS_UNINIT |
TCPC_REG_POWER_STATUS_VBUS_DET;
}
@@ -1110,6 +1110,9 @@ void tcpc_init(int port)
{
int i;
+ if (port >= board_get_usb_pd_port_count())
+ return;
+
/* Initialize physical layer */
pd_hw_init(port, PD_ROLE_DEFAULT(port));
pd[port].cc_pull = PD_ROLE_DEFAULT(port) ==
@@ -1158,6 +1161,9 @@ void pd_vbus_evt_p0(enum gpio_signal signal)
#if CONFIG_USB_PD_PORT_MAX_COUNT >= 2
void pd_vbus_evt_p1(enum gpio_signal signal)
{
+ if (board_get_usb_pd_port_count() == 1)
+ return;
+
tcpc_set_power_status(TASK_ID_TO_PD_PORT(TASK_ID_PD_C1),
!gpio_get_level(GPIO_USB_C1_VBUS_WAKE_L));
task_wake(TASK_ID_PD_C1);
@@ -1359,7 +1365,7 @@ static int command_tcpc(int argc, char **argv)
port = strtoi(argv[1], &e, 10);
if (argc < 3)
return EC_ERROR_PARAM_COUNT;
- if (*e || port >= CONFIG_USB_PD_PORT_MAX_COUNT)
+ if (*e || port >= board_get_usb_pd_port_count())
return EC_ERROR_PARAM2;
if (!strcasecmp(argv[2], "clock")) {
diff --git a/driver/charger/bd9995x.c b/driver/charger/bd9995x.c
index 4eea28b506..3064f8c93f 100644
--- a/driver/charger/bd9995x.c
+++ b/driver/charger/bd9995x.c
@@ -1271,7 +1271,7 @@ void usb_charger_task(void *u)
vbus_voltage = 0;
#endif
- for (port = 0; port < CONFIG_USB_PD_PORT_MAX_COUNT; port++) {
+ for (port = 0; port < board_get_usb_pd_port_count(); port++) {
bc12_detected_type[port] = CHARGE_SUPPLIER_NONE;
bd9995x_enable_vbus_detect_interrupts(port, 1);
bc12_det_mark[port] = 0;
@@ -1280,7 +1280,7 @@ void usb_charger_task(void *u)
while (1) {
sleep_usec = -1;
changed = 0;
- for (port = 0; port < CONFIG_USB_PD_PORT_MAX_COUNT; port++) {
+ for (port = 0; port < board_get_usb_pd_port_count(); port++) {
/* Get port interrupts */
interrupts = bd9995x_get_interrupts(port);
if (interrupts & BD9995X_CMD_INT_VBUS_DET ||
diff --git a/driver/ppc/nx20p348x.c b/driver/ppc/nx20p348x.c
index 6836e336d3..bdd023134b 100644
--- a/driver/ppc/nx20p348x.c
+++ b/driver/ppc/nx20p348x.c
@@ -384,7 +384,7 @@ static void nx20p348x_irq_deferred(void)
int i;
uint32_t pending = atomic_read_clear(&irq_pending);
- for (i = 0; i < CONFIG_USB_PD_PORT_MAX_COUNT; i++)
+ for (i = 0; i < board_get_usb_pd_port_count(); i++)
if ((1 << i) & pending)
nx20p348x_handle_interrupt(i);
}
diff --git a/driver/ppc/sn5s330.c b/driver/ppc/sn5s330.c
index 5f5e877ae2..f94908039d 100644
--- a/driver/ppc/sn5s330.c
+++ b/driver/ppc/sn5s330.c
@@ -672,7 +672,7 @@ static void sn5s330_irq_deferred(void)
int i;
uint32_t pending = atomic_read_clear(&irq_pending);
- for (i = 0; i < CONFIG_USB_PD_PORT_MAX_COUNT; i++)
+ for (i = 0; i < board_get_usb_pd_port_count(); i++)
if ((1 << i) & pending)
sn5s330_handle_interrupt(i);
}
diff --git a/driver/tcpm/it83xx.c b/driver/tcpm/it83xx.c
index faa0859e56..4ea2ec565f 100644
--- a/driver/tcpm/it83xx.c
+++ b/driver/tcpm/it83xx.c
@@ -527,11 +527,11 @@ static int it83xx_tcpm_set_rx_enable(int port, int enable)
}
/* If any PD port is connected, then disable deep sleep */
- for (i = 0; i < CONFIG_USB_PD_PORT_MAX_COUNT; ++i)
+ for (i = 0; i < board_get_usb_pd_port_count(); ++i)
if (IT83XX_USBPD_GCR(i) | USBPD_REG_MASK_BMC_PHY)
break;
- if (i == CONFIG_USB_PD_PORT_MAX_COUNT)
+ if (i == board_get_usb_pd_port_count())
enable_sleep(SLEEP_MASK_USB_PD);
else
disable_sleep(SLEEP_MASK_USB_PD);
diff --git a/driver/tcpm/tcpci.c b/driver/tcpm/tcpci.c
index a20601a958..67a4f2b1f2 100644
--- a/driver/tcpm/tcpci.c
+++ b/driver/tcpm/tcpci.c
@@ -615,7 +615,7 @@ int tcpci_get_chip_info(int port, int live,
int error;
int val;
- if (port >= CONFIG_USB_PD_PORT_MAX_COUNT)
+ if (port >= board_get_usb_pd_port_count())
return EC_ERROR_INVAL;
i = &info[port];
@@ -687,6 +687,9 @@ int tcpci_tcpm_init(int port)
int power_status;
int tries = TCPM_INIT_TRIES;
+ if (port >= board_get_usb_pd_port_count())
+ return EC_ERROR_INVAL;
+
while (1) {
error = tcpc_read(port, TCPC_REG_POWER_STATUS, &power_status);
/*
diff --git a/driver/usb_mux.c b/driver/usb_mux.c
index ba683e98dd..9f81c7674e 100644
--- a/driver/usb_mux.c
+++ b/driver/usb_mux.c
@@ -194,7 +194,7 @@ static int command_typec(int argc, char **argv)
return EC_ERROR_PARAM_COUNT;
port = strtoi(argv[1], &e, 10);
- if (*e || port >= CONFIG_USB_PD_PORT_MAX_COUNT)
+ if (*e || port >= board_get_usb_pd_port_count())
return EC_ERROR_PARAM1;
if (argc < 3) {
@@ -233,7 +233,7 @@ static int hc_usb_pd_mux_info(struct host_cmd_handler_args *args)
int port = p->port;
const struct usb_mux *mux;
- if (port >= CONFIG_USB_PD_PORT_MAX_COUNT)
+ if (port >= board_get_usb_pd_port_count())
return EC_RES_INVALID_PARAM;
mux = &usb_muxes[port];
diff --git a/include/usb_pd.h b/include/usb_pd.h
index e8c196d897..05b95a0588 100644
--- a/include/usb_pd.h
+++ b/include/usb_pd.h
@@ -1848,6 +1848,23 @@ int pd_capable(int port);
*/
int pd_is_vbus_present(int port);
+#ifdef CONFIG_USB_PD_PORT_MAX_COUNT
+#ifdef CONFIG_USB_POWER_DELIVERY
+/**
+ * Get board specific usb pd port count
+ *
+ * @return <= CONFIG_USB_PD_PORT_MAX_COUNT if configured in board file,
+ * else return CONFIG_USB_PD_PORT_MAX_COUNT
+ */
+uint8_t board_get_usb_pd_port_count(void);
+#else
+static inline uint8_t board_get_usb_pd_port_count(void)
+{
+ return CONFIG_USB_PD_PORT_MAX_COUNT;
+}
+#endif /* CONFIG_USB_POWER_DELIVERY */
+#endif /* CONFIG_USB_PD_PORT_MAX_COUNT */
+
/* ----- Logging ----- */
#ifdef CONFIG_USB_PD_LOGGING
/**
diff --git a/test/charge_manager.c b/test/charge_manager.c
index f58850fe50..f8e16012b9 100644
--- a/test/charge_manager.c
+++ b/test/charge_manager.c
@@ -77,7 +77,7 @@ enum battery_present battery_is_present(void)
static void clear_new_power_requests(void)
{
int i;
- for (i = 0; i < CONFIG_USB_PD_PORT_MAX_COUNT; ++i)
+ for (i = 0; i < board_get_usb_pd_port_count(); ++i)
new_power_request[i] = 0;
}
@@ -114,7 +114,7 @@ static void initialize_charge_table(int current, int voltage, int ceil)
charge.current = current;
charge.voltage = voltage;
- for (i = 0; i < CONFIG_USB_PD_PORT_MAX_COUNT; ++i) {
+ for (i = 0; i < board_get_usb_pd_port_count(); ++i) {
for (j = 0; j < CEIL_REQUESTOR_COUNT; ++j)
charge_manager_set_ceil(i, j, ceil);
charge_manager_update_dualrole(i, CAP_DEDICATED);
@@ -140,12 +140,12 @@ static int test_initialization(void)
/* Initialize all supplier/port pairs, except for the last one */
for (i = 0; i < CHARGE_SUPPLIER_COUNT; ++i)
- for (j = 0; j < CONFIG_USB_PD_PORT_MAX_COUNT; ++j) {
+ for (j = 0; j < board_get_usb_pd_port_count(); ++j) {
if (i == 0)
charge_manager_update_dualrole(j,
CAP_DEDICATED);
if (i == CHARGE_SUPPLIER_COUNT - 1 &&
- j == CONFIG_USB_PD_PORT_MAX_COUNT - 1)
+ j == board_get_usb_pd_port_count() - 1)
break;
charge_manager_update_charge(i, j, &charge);
}
@@ -156,7 +156,7 @@ static int test_initialization(void)
/* Update last pair and verify a charge port has been selected */
charge_manager_update_charge(CHARGE_SUPPLIER_COUNT-1,
- CONFIG_USB_PD_PORT_MAX_COUNT-1,
+ board_get_usb_pd_port_count()-1,
&charge);
wait_for_charge_manager_refresh();
TEST_ASSERT(active_charge_port != CHARGE_PORT_NONE);
diff --git a/test/usb_pd.c b/test/usb_pd.c
index bc6a3072f1..ec9b834679 100644
--- a/test/usb_pd.c
+++ b/test/usb_pd.c
@@ -147,7 +147,7 @@ static void init_ports(void)
{
int i;
- for (i = 0; i < CONFIG_USB_PD_PORT_MAX_COUNT; ++i) {
+ for (i = 0; i < board_get_usb_pd_port_count(); ++i) {
pd_port[i].host_mode = 0;
pd_port[i].partner_role = -1;
pd_port[i].has_vbus = 0;