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/usb_pd_protocol.c | 81 +++++++----------------------------------------- 1 file changed, 11 insertions(+), 70 deletions(-) (limited to 'common/usb_pd_protocol.c') 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 { -- cgit v1.2.1