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 --- baseboard/kalista/baseboard.h | 1 - board/dragonegg/board.h | 1 - board/fizz/board.h | 1 - board/it83xx_evb/board.h | 1 - board/samus_pd/board.h | 3 -- 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 ---------- include/charge_state.h | 7 +++ include/usb_common.h | 37 +++++++++++++++ include/usb_pd.h | 4 +- include/usb_tc_sm.h | 10 ---- 14 files changed, 147 insertions(+), 153 deletions(-) create mode 100644 common/usb_common.c create mode 100644 include/usb_common.h diff --git a/baseboard/kalista/baseboard.h b/baseboard/kalista/baseboard.h index e494b83e46..050d914681 100644 --- a/baseboard/kalista/baseboard.h +++ b/baseboard/kalista/baseboard.h @@ -230,7 +230,6 @@ enum OEM_ID { /* Board specific handlers */ void board_reset_pd_mcu(void); void board_set_tcpc_power_mode(int port, int mode); -int board_get_battery_soc(void); void led_alert(int enable); void led_critical(void); diff --git a/board/dragonegg/board.h b/board/dragonegg/board.h index e8aebe8061..c03d98b6b3 100644 --- a/board/dragonegg/board.h +++ b/board/dragonegg/board.h @@ -79,7 +79,6 @@ enum battery_type { BATTERY_TYPE_COUNT, }; -int board_get_battery_soc(void); void board_pd_vconn_ctrl(int port, int cc_pin, int enabled); #endif /* !__ASSEMBLER__ */ diff --git a/board/fizz/board.h b/board/fizz/board.h index 6071a163e8..28a70e546d 100644 --- a/board/fizz/board.h +++ b/board/fizz/board.h @@ -258,7 +258,6 @@ enum OEM_ID { /* Board specific handlers */ void board_reset_pd_mcu(void); void board_set_tcpc_power_mode(int port, int mode); -int board_get_battery_soc(void); void led_alert(int enable); void led_critical(void); diff --git a/board/it83xx_evb/board.h b/board/it83xx_evb/board.h index e3a3b4fbb0..6ac5e9f8ae 100644 --- a/board/it83xx_evb/board.h +++ b/board/it83xx_evb/board.h @@ -107,7 +107,6 @@ enum adc_channel { /* delay to turn on/off vconn */ #define PD_VCONN_SWAP_DELAY 5000 /* us */ -int board_get_battery_soc(void); void board_pd_vconn_ctrl(int port, int cc_pin, int enabled); void board_pd_vbus_ctrl(int port, int enabled); #endif diff --git a/board/samus_pd/board.h b/board/samus_pd/board.h index 075e531e35..a6e6d3ecec 100644 --- a/board/samus_pd/board.h +++ b/board/samus_pd/board.h @@ -144,9 +144,6 @@ enum pwm_channel { /* Map current in milli-amps to PWM duty cycle percentage */ #define MA_TO_PWM(curr) (((curr) - PWM_0_MA) * 100 / (PWM_100_MA - PWM_0_MA)) -/* Get the last received battery level. */ -int board_get_battery_soc(void); - #endif /* !__ASSEMBLER__ */ #endif /* __CROS_EC_BOARD_H */ 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); diff --git a/include/charge_state.h b/include/charge_state.h index 04ca4c6648..4a152032fc 100644 --- a/include/charge_state.h +++ b/include/charge_state.h @@ -86,10 +86,17 @@ int charge_keep_power_off(void); */ uint32_t charge_get_flags(void); +#if defined(CONFIG_CHARGER) /** * Return current battery charge percentage. */ int charge_get_percent(void); +#elif defined(CONFIG_BATTERY) +/** + * Return current battery charge if not using charge manager sub-system. + */ +int board_get_battery_soc(void); +#endif /** * Return current display charge in 10ths of a percent (e.g. 1000 = 100.0%) diff --git a/include/usb_common.h b/include/usb_common.h new file mode 100644 index 0000000000..1fbaa4bd50 --- /dev/null +++ b/include/usb_common.h @@ -0,0 +1,37 @@ +/* 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. + */ +#ifndef __CROS_EC_USB_COMMON_H +#define __CROS_EC_USB_COMMON_H + +/* Functions that are shared between old and new PD stacks */ +#include "usb_pd.h" +#include "usb_pd_tcpm.h" + +/* Returns the battery percentage [0-100] of the system. */ +int usb_get_battery_soc(void); + +/* + * Returns type C current limit (mA), potentially with the DTS flag, based upon + * states of the CC lines on the partner side. + * + * @param polarity 0 if cc1 is primary, otherwise 1 + * @param cc1 value of CC1 set by tcpm_get_cc + * @param cc2 value of CC2 set by tcpm_get_cc + * @return current limit (mA) with DTS flag set if appropriate + */ +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); + +/** + * Returns the polarity of a Sink. + * + * @param cc1 value of CC1 set by tcpm_get_cc + * @param cc2 value of CC2 set by tcpm_get_cc + * @return 0 if cc1 is primary, else 1 for cc2 being primary + */ +enum pd_cc_polarity_type get_snk_polarity(enum tcpc_cc_voltage_status cc1, + enum tcpc_cc_voltage_status cc2); + +#endif /* __CROS_EC_USB_COMMON_H */ diff --git a/include/usb_pd.h b/include/usb_pd.h index 4b36247a94..a1a1c0ba0f 100644 --- a/include/usb_pd.h +++ b/include/usb_pd.h @@ -1212,8 +1212,8 @@ enum pd_data_msg_type { /* CC Polarity type */ enum pd_cc_polarity_type { - POLARITY_CC1, - POLARITY_CC2 + POLARITY_CC1 = 0, + POLARITY_CC2 = 1, }; /* Protocol revision */ diff --git a/include/usb_tc_sm.h b/include/usb_tc_sm.h index 1019bf2c20..39ba6e026d 100644 --- a/include/usb_tc_sm.h +++ b/include/usb_tc_sm.h @@ -125,16 +125,6 @@ void set_usb_mux_with_current_data_role(int port); */ void tc_set_timeout(int port, uint64_t timeout); -/** - * Returns the polarity of a Sink. - * - * @param cc1 value of CC1 set by tcpm_get_cc - * @param cc2 value of CC2 set by tcpm_get_cc - * @return 0 if cc1 is connected, else 1 for cc2 - */ -enum pd_cc_polarity_type get_snk_polarity(enum tcpc_cc_voltage_status cc1, - enum tcpc_cc_voltage_status cc2); - /** * Restarts the TCPC * -- cgit v1.2.1