diff options
author | Jett Rink <jettrink@chromium.org> | 2019-08-08 10:06:40 -0600 |
---|---|---|
committer | Commit Bot <commit-bot@chromium.org> | 2020-06-25 15:17:21 +0000 |
commit | 305310aa4430c205bb0319aa3f975cd4b95417f8 (patch) | |
tree | 516a7c526092fb443c82aacecc90022c21d1cd9b | |
parent | 38c590953a5caf27b7089f9858491373dfa4445a (diff) | |
download | chrome-ec-305310aa4430c205bb0319aa3f975cd4b95417f8.tar.gz |
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.
BUG=b:137493121
BRANCH=firmware-grunt-11031.B
TEST=With other PD Policies patches, flash grunt and run faft_ec&pd
Change-Id: Ibd9dda1e544e06f02aa3dde48ca7de1539700cfa
Signed-off-by: Jett Rink <jettrink@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/1744655
Reviewed-by: Denis Brockus <dbrockus@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2213572
Tested-by: Dawid Niedźwiecki <dn@semihalf.com>
Reviewed-by: Paul Fagerburg <pfagerburg@chromium.org>
Reviewed-by: Aseda Aboagye <aaboagye@chromium.org>
Commit-Queue: Denis Brockus <dbrockus@chromium.org>
-rw-r--r-- | baseboard/kalista/baseboard.h | 1 | ||||
-rw-r--r-- | board/dragonegg/board.h | 1 | ||||
-rw-r--r-- | board/fizz/board.h | 1 | ||||
-rw-r--r-- | board/it83xx_evb/board.h | 1 | ||||
-rw-r--r-- | board/samus_pd/board.h | 3 | ||||
-rw-r--r-- | common/build.mk | 1 | ||||
-rw-r--r-- | common/usb_common.c | 83 | ||||
-rw-r--r-- | common/usb_pd_protocol.c | 60 | ||||
-rw-r--r-- | include/charge_state.h | 7 | ||||
-rw-r--r-- | include/usb_common.h | 37 | ||||
-rw-r--r-- | include/usb_pd.h | 6 |
11 files changed, 142 insertions, 59 deletions
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 0f6c83a3ee..6946a62101 100644 --- a/board/dragonegg/board.h +++ b/board/dragonegg/board.h @@ -62,7 +62,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 352daeb7b5..b099c38920 100644 --- a/board/fizz/board.h +++ b/board/fizz/board.h @@ -266,7 +266,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 b018fb65c6..4e100e4fcd 100644 --- a/board/it83xx_evb/board.h +++ b/board/it83xx_evb/board.h @@ -106,7 +106,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 89b6f516e1..50d58f75df 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 01069a4bbf..39e111956f 100644 --- a/common/build.mk +++ b/common/build.mk @@ -124,6 +124,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 common-$(CONFIG_USB_POWER_DELIVERY)+=usb_pd_protocol.o usb_pd_policy.o common-$(CONFIG_USB_PD_LOGGING)+=event_log.o pd_log.o common-$(CONFIG_USB_PD_TCPC)+=usb_pd_tcpc.o 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 b661dd5c42..e09d37f759 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" @@ -2455,54 +2456,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(int cc1, int 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 - 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. @@ -3677,8 +3631,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 @@ -3811,13 +3765,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/include/charge_state.h b/include/charge_state.h index bcf9071d6b..4ac41cbe4d 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 /** * Check if board is consuming full input current 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 99e897a35d..7043d85ab7 100644 --- a/include/usb_pd.h +++ b/include/usb_pd.h @@ -890,6 +890,12 @@ enum pd_data_msg_type { PD_DATA_VENDOR_DEF = 15, }; +/* CC Polarity type */ +enum pd_cc_polarity_type { + POLARITY_CC1 = 0, + POLARITY_CC2 = 1, +}; + /* Protocol revision */ #define PD_REV10 0 #define PD_REV20 1 |