From a0ed5aebb22c4d9b83c257b4127a1df372751e14 Mon Sep 17 00:00:00 2001 From: Jett Rink Date: Thu, 8 Aug 2019 10:06:40 -0600 Subject: usb: de-dup common code from old and new PD stack We still need to pull out more common code between the two stacks, but this is scaffolding with a few examples. BRANCH=none BUG=b:137493121 TEST=unit tests pass Change-Id: Ibd9dda1e544e06f02aa3dde48ca7de1539700cfa Signed-off-by: Jett Rink Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/1744655 Reviewed-by: Denis Brockus --- common/build.mk | 1 + common/usb_common.c | 83 ++++++++++++++++++++++++++++++++++ common/usb_pd_protocol.c | 81 +++++---------------------------- common/usbc/usb_tc_drp_acc_trysrc_sm.c | 46 +++---------------- common/usbc/usbc_task.c | 24 ---------- 5 files changed, 101 insertions(+), 134 deletions(-) create mode 100644 common/usb_common.c (limited to 'common') diff --git a/common/build.mk b/common/build.mk index 63e5d052dc..6b999e1fe9 100644 --- a/common/build.mk +++ b/common/build.mk @@ -135,6 +135,7 @@ common-$(CONFIG_USB_I2C)+=usb_i2c.o common-$(CONFIG_USB_CHARGER)+=usb_charger.o common-$(CONFIG_USB_PORT_POWER_DUMB)+=usb_port_power_dumb.o common-$(CONFIG_USB_PORT_POWER_SMART)+=usb_port_power_smart.o +common-$(CONFIG_USB_POWER_DELIVERY)+=usb_common.o ifeq ($(CONFIG_USB_SM_FRAMEWORK),) common-$(CONFIG_USB_POWER_DELIVERY)+=usb_pd_protocol.o usb_pd_policy.o endif diff --git a/common/usb_common.c b/common/usb_common.c new file mode 100644 index 0000000000..86c85898dd --- /dev/null +++ b/common/usb_common.c @@ -0,0 +1,83 @@ +/* Copyright 2019 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. + */ + +/* + * Contains common USB functions shared between the old (i.e. usb_pd_protocol) + * and the new (i.e. usb_sm_*) USB-C PD stacks. + */ + +#include "common.h" +#include "charge_state.h" +#include "usb_pd.h" +#include "usb_pd_tcpm.h" + +int usb_get_battery_soc(void) +{ +#if defined(CONFIG_CHARGER) + return charge_get_percent(); +#elif defined(CONFIG_BATTERY) + return board_get_battery_soc(); +#else + return 0; +#endif +} + +/* + * CC values for regular sources and Debug sources (aka DTS) + * + * Source type Mode of Operation CC1 CC2 + * --------------------------------------------- + * Regular Default USB Power RpUSB Open + * Regular USB-C @ 1.5 A Rp1A5 Open + * Regular USB-C @ 3 A Rp3A0 Open + * DTS Default USB Power Rp3A0 Rp1A5 + * DTS USB-C @ 1.5 A Rp1A5 RpUSB + * DTS USB-C @ 3 A Rp3A0 RpUSB + */ + +typec_current_t usb_get_typec_current_limit(enum pd_cc_polarity_type polarity, + enum tcpc_cc_voltage_status cc1, enum tcpc_cc_voltage_status cc2) +{ + typec_current_t charge = 0; + enum tcpc_cc_voltage_status cc = polarity ? cc2 : cc1; + enum tcpc_cc_voltage_status cc_alt = polarity ? cc1 : cc2; + + switch (cc) { + case TYPEC_CC_VOLT_RP_3_0: + if (!cc_is_rp(cc_alt) || cc_alt == TYPEC_CC_VOLT_RP_DEF) + charge = 3000; + else if (cc_alt == TYPEC_CC_VOLT_RP_1_5) + charge = 500; + break; + case TYPEC_CC_VOLT_RP_1_5: + charge = 1500; + break; + case TYPEC_CC_VOLT_RP_DEF: + charge = 500; + break; + default: + break; + } + + if (IS_ENABLED(CONFIG_USBC_DISABLE_CHARGE_FROM_RP_DEF) && charge == 500) + charge = 0; + + if (cc_is_rp(cc_alt)) + charge |= TYPEC_CURRENT_DTS_MASK; + + return charge; +} + +enum pd_cc_polarity_type get_snk_polarity(enum tcpc_cc_voltage_status cc1, + enum tcpc_cc_voltage_status cc2) +{ + /* The following assumes: + * + * TYPEC_CC_VOLT_RP_3_0 > TYPEC_CC_VOLT_RP_1_5 + * TYPEC_CC_VOLT_RP_1_5 > TYPEC_CC_VOLT_RP_DEF + * TYPEC_CC_VOLT_RP_DEF > TYPEC_CC_VOLT_OPEN + */ + return cc2 > cc1; +} diff --git a/common/usb_pd_protocol.c b/common/usb_pd_protocol.c index a1d4ccb4c7..be0d43ced2 100644 --- a/common/usb_pd_protocol.c +++ b/common/usb_pd_protocol.c @@ -22,6 +22,7 @@ #include "timer.h" #include "util.h" #include "usb_charge.h" +#include "usb_common.h" #include "usb_mux.h" #include "usb_pd.h" #include "usb_pd_tcpm.h" @@ -2296,23 +2297,12 @@ enum pd_dual_role_states pd_get_dual_role(int port) return drp_state[port]; } -static inline __maybe_unused int get_battery_soc(void) -{ -#if defined(CONFIG_CHARGER) - return charge_get_percent(); -#elif defined(CONFIG_BATTERY) - return board_get_battery_soc(); -#else - return 0; -#endif -} - #ifdef CONFIG_USB_PD_TRY_SRC static void pd_update_try_source(void) { int i; int try_src = 0; - int batt_soc = get_battery_soc(); + int batt_soc = usb_get_battery_soc(); try_src = 0; for (i = 0; i < CONFIG_USB_PD_PORT_COUNT; i++) @@ -2357,7 +2347,7 @@ static void pd_update_try_source(void) static void pd_update_snk_reset(void) { int i; - int batt_soc = get_battery_soc(); + int batt_soc = usb_get_battery_soc(); if (batt_soc < CONFIG_USB_PD_RESET_MIN_BATT_SOC) return; @@ -2634,58 +2624,7 @@ void pd_ping_enable(int port, int enable) pd[port].flags &= ~PD_FLAGS_PING_ENABLED; } -/* - * CC values for regular sources and Debug sources (aka DTS) - * - * Source type Mode of Operation CC1 CC2 - * --------------------------------------------- - * Regular Default USB Power RpUSB Open - * Regular USB-C @ 1.5 A Rp1A5 Open - * Regular USB-C @ 3 A Rp3A0 Open - * DTS Default USB Power Rp3A0 Rp1A5 - * DTS USB-C @ 1.5 A Rp1A5 RpUSB - * DTS USB-C @ 3 A Rp3A0 RpUSB -*/ - -/** - * Returns the polarity of a Sink. - */ -static inline int get_snk_polarity(enum tcpc_cc_voltage_status cc1, - enum tcpc_cc_voltage_status cc2) -{ - /* the following assumes: - * TYPEC_CC_VOLT_RP_3_0 > TYPEC_CC_VOLT_RP_1_5 - * TYPEC_CC_VOLT_RP_1_5 > TYPEC_CC_VOLT_RP_DEF - * TYPEC_CC_VOLT_RP_DEF > TYPEC_CC_VOLT_OPEN - */ - return (cc2 > cc1); -} - #if defined(CONFIG_CHARGE_MANAGER) -/** - * Returns type C current limit (mA) based upon cc_voltage (mV). - */ -static typec_current_t get_typec_current_limit(int polarity, int cc1, int cc2) -{ - typec_current_t charge; - int cc = polarity ? cc2 : cc1; - int cc_alt = polarity ? cc1 : cc2; - - if (cc == TYPEC_CC_VOLT_RP_3_0 && cc_alt != TYPEC_CC_VOLT_RP_1_5) - charge = 3000; - else if (cc == TYPEC_CC_VOLT_RP_1_5) - charge = 1500; - else if (cc == TYPEC_CC_VOLT_RP_DEF) - charge = IS_ENABLED(CONFIG_USBC_DISABLE_CHARGE_FROM_RP_DEF) ? - 0 : 500; - else - charge = 0; - - if (cc_is_rp(cc_alt)) - charge |= TYPEC_CURRENT_DTS_MASK; - - return charge; -} /** * Signal power request to indicate a charger update that affects the port. @@ -4004,8 +3943,8 @@ void pd_task(void *u) /* initial data role for sink is UFP */ pd_set_data_role(port, PD_ROLE_UFP); #if defined(CONFIG_CHARGE_MANAGER) - typec_curr = get_typec_current_limit(pd[port].polarity, - cc1, cc2); + typec_curr = usb_get_typec_current_limit( + pd[port].polarity, cc1, cc2); typec_set_input_current_limit( port, typec_curr, TYPE_C_VOLTAGE); #endif @@ -4105,7 +4044,7 @@ void pd_task(void *u) * high-power chargers will stay at 15W until a * reset is sent, depending on boot timing. */ - int batt_soc = get_battery_soc(); + int batt_soc = usb_get_battery_soc(); if (batt_soc < CONFIG_USB_PD_RESET_MIN_BATT_SOC) pd[port].flags |= @@ -4176,13 +4115,15 @@ void pd_task(void *u) /* Check if CC pull-up has changed */ tcpm_get_cc(port, &cc1, &cc2); - if (typec_curr != get_typec_current_limit( + if (typec_curr != usb_get_typec_current_limit( pd[port].polarity, cc1, cc2)) { /* debounce signal by requiring two reads */ if (typec_curr_change) { /* set new input current limit */ - typec_curr = get_typec_current_limit( - pd[port].polarity, cc1, cc2); + typec_curr = + usb_get_typec_current_limit( + pd[port].polarity, + cc1, cc2); typec_set_input_current_limit( port, typec_curr, TYPE_C_VOLTAGE); } else { diff --git a/common/usbc/usb_tc_drp_acc_trysrc_sm.c b/common/usbc/usb_tc_drp_acc_trysrc_sm.c index 810fc5476e..4267e8e302 100644 --- a/common/usbc/usb_tc_drp_acc_trysrc_sm.c +++ b/common/usbc/usb_tc_drp_acc_trysrc_sm.c @@ -11,6 +11,7 @@ #include "system.h" #include "task.h" #include "tcpm.h" +#include "usb_common.h" #include "usb_mux.h" #include "usb_pd.h" #include "usb_tc_drp_acc_trysrc_sm.h" @@ -337,11 +338,7 @@ static void pd_update_try_source(void) int i; int try_src = 0; -#ifndef CONFIG_CHARGER - int batt_soc = board_get_battery_soc(); -#else - int batt_soc = charge_get_percent(); -#endif + int batt_soc = usb_get_battery_soc(); try_src = 0; for (i = 0; i < CONFIG_USB_PD_PORT_COUNT; i++) @@ -391,37 +388,6 @@ static inline void pd_dev_dump_info(uint16_t dev_id, uint8_t *hash) } #endif /* CONFIG_CMD_PD_DEV_DUMP_INFO */ -#if defined(CONFIG_CHARGE_MANAGER) -/* - * TODO(b/137493121): Move this function to a separate file that's shared - * between the this and the original stack. - */ - -/* - * Returns type C current limit (mA) based upon cc_voltage (mV). - */ -static typec_current_t get_typec_current_limit(int polarity, int cc1, int cc2) -{ - typec_current_t charge; - int cc = polarity ? cc2 : cc1; - int cc_alt = polarity ? cc1 : cc2; - - if (cc == TYPEC_CC_VOLT_RP_3_0 && cc_alt != TYPEC_CC_VOLT_RP_1_5) - charge = 3000; - else if (cc == TYPEC_CC_VOLT_RP_1_5) - charge = 1500; - else if (cc == TYPEC_CC_VOLT_RP_DEF) - charge = 500; - else - charge = 0; - - if (cc_is_rp(cc_alt)) - charge |= TYPEC_CURRENT_DTS_MASK; - - return charge; -} -#endif - static void set_vconn(int port, int enable) { if (enable == TC_CHK_FLAG(port, TC_FLAGS_VCONN_ON)) @@ -792,8 +758,8 @@ static void sink_power_sub_states(int port) tc[port].cc_debounce = 0; if (IS_ENABLED(CONFIG_CHARGE_MANAGER)) { - tc[port].typec_curr = - get_typec_current_limit(tc[port].polarity, cc1, cc2); + tc[port].typec_curr = usb_get_typec_current_limit( + tc[port].polarity, cc1, cc2); typec_set_input_current_limit(port, tc[port].typec_curr, TYPE_C_VOLTAGE); @@ -1074,8 +1040,8 @@ static int tc_attached_snk_entry(int port) tc_set_data_role(port, PD_ROLE_UFP); if (IS_ENABLED(CONFIG_CHARGE_MANAGER)) { - tc[port].typec_curr = - get_typec_current_limit(tc[port].polarity, cc1, cc2); + tc[port].typec_curr = usb_get_typec_current_limit( + tc[port].polarity, cc1, cc2); typec_set_input_current_limit(port, tc[port].typec_curr, TYPE_C_VOLTAGE); charge_manager_update_dualrole(port, CAP_DEDICATED); diff --git a/common/usbc/usbc_task.c b/common/usbc/usbc_task.c index b5b94c126a..c081f7649f 100644 --- a/common/usbc/usbc_task.c +++ b/common/usbc/usbc_task.c @@ -116,30 +116,6 @@ enum typec_state_id get_typec_state_id(int port) return tc[port].state_id; } -/* - * CC values for regular sources and Debug sources (aka DTS) - * - * Source type Mode of Operation CC1 CC2 - * --------------------------------------------- - * Regular Default USB Power RpUSB Open - * Regular USB-C @ 1.5 A Rp1A5 Open - * Regular USB-C @ 3 A Rp3A0 Open - * DTS Default USB Power Rp3A0 Rp1A5 - * DTS USB-C @ 1.5 A Rp1A5 RpUSB - * DTS USB-C @ 3 A Rp3A0 RpUSB - */ - -enum pd_cc_polarity_type get_snk_polarity(enum tcpc_cc_voltage_status cc1, - enum tcpc_cc_voltage_status cc2) -{ - /* the following assumes: - * TYPEC_CC_VOLT_RP_3_0 > TYPEC_CC_VOLT_RP_1_5 - * TYPEC_CC_VOLT_RP_1_5 > TYPEC_CC_VOLT_RP_DEF - * TYPEC_CC_VOLT_RP_DEF > TYPEC_CC_VOLT_OPEN - */ - return (cc2 > cc1) ? POLARITY_CC2 : POLARITY_CC1; -} - int tc_restart_tcpc(int port) { return tcpm_init(port); -- cgit v1.2.1