summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJett Rink <jettrink@chromium.org>2019-04-30 09:21:40 -0600
committerChromeOS Commit Bot <chromeos-commit-bot@chromium.org>2019-05-08 16:16:20 +0000
commit4c16e7c91bc819acc95a008e018acae8aa62356a (patch)
tree8148a15096e39b47c6213c6d076d579f7e441895
parentf4e321b6fddae70a1a284f8e872f8315654f0bf8 (diff)
downloadchrome-ec-4c16e7c91bc819acc95a008e018acae8aa62356a.tar.gz
usb: add inline helper method for CC lines
Expressing logic for CC lines can get very verbose. Add helper inline methods that logical describe the condition we are testing to clean up call sites. BRANCH=none BUG=none TEST=Builds, no functional change. Change-Id: I48c117437bc14f3c55473df7f7c778b55af2706d Signed-off-by: Jett Rink <jettrink@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/1589906 Reviewed-by: Daisuke Nojiri <dnojiri@chromium.org> Reviewed-by: Sam Hurst <shurst@google.com> Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/1600941 Reviewed-by: Diana Z <dzigterman@chromium.org> Commit-Queue: Diana Z <dzigterman@chromium.org> Tested-by: Diana Z <dzigterman@chromium.org>
-rw-r--r--common/usb_pd_protocol.c44
-rw-r--r--common/usb_pd_tcpc.c6
-rw-r--r--include/usb_pd_tcpm.h41
3 files changed, 56 insertions, 35 deletions
diff --git a/common/usb_pd_protocol.c b/common/usb_pd_protocol.c
index 2062ff8047..e245ddbd56 100644
--- a/common/usb_pd_protocol.c
+++ b/common/usb_pd_protocol.c
@@ -675,7 +675,7 @@ static inline void set_state(int port, enum pd_states next_state)
* Neither a debug accessory nor UFP attached.
* Tell the PPC module that there is no sink connected.
*/
- if (cc1 != TYPEC_CC_VOLT_RD && cc2 != TYPEC_CC_VOLT_RD) {
+ if (!cc_is_at_least_one_rd(cc1, cc2)) {
ppc_sink_is_connected(port, 0);
/*
* Clear the overcurrent event counter
@@ -2329,33 +2329,21 @@ static void pd_partner_port_reset(int port)
}
#endif /* CONFIG_USB_PD_DUAL_ROLE */
-/**
- * Returns whether the sink has detected a Rp resistor on the other side.
- */
-static inline int cc_is_rp(int cc)
-{
- return (cc == TYPEC_CC_VOLT_RP_DEF) || (cc == TYPEC_CC_VOLT_RP_1_5) ||
- (cc == TYPEC_CC_VOLT_RP_3_0);
-}
-
#ifdef CONFIG_USB_PD_DUAL_ROLE_AUTO_TOGGLE
static enum pd_states drp_auto_toggle_next_state(int port, int cc1, int cc2)
{
enum pd_states next_state;
/* Set to appropriate port state */
- if (cc1 == TYPEC_CC_VOLT_OPEN &&
- cc2 == TYPEC_CC_VOLT_OPEN)
+ if (cc_is_open(cc1, cc2))
/* nothing connected, keep toggling*/
next_state = PD_STATE_DRP_AUTO_TOGGLE;
else if ((cc_is_rp(cc1) || cc_is_rp(cc2)) &&
drp_state[port] != PD_DRP_FORCE_SOURCE) {
/* SNK allowed unless ForceSRC */
next_state = PD_STATE_SNK_DISCONNECTED;
- } else if ((cc1 == TYPEC_CC_VOLT_RD ||
- cc2 == TYPEC_CC_VOLT_RD) ||
- (cc1 == TYPEC_CC_VOLT_RA &&
- cc2 == TYPEC_CC_VOLT_RA)) {
+ } else if (cc_is_at_least_one_rd(cc1, cc2) ||
+ cc_is_audio_acc(cc1, cc2)) {
/*
* SRC allowed unless ForceSNK or Toggle Off
*
@@ -2942,8 +2930,7 @@ void pd_task(void *u)
if (auto_toggle_supported &&
!(pd[port].flags & PD_FLAGS_TCPC_DRP_TOGGLE) &&
!(pd[port].flags & PD_FLAGS_TRY_SRC) &&
- (cc1 == TYPEC_CC_VOLT_OPEN &&
- cc2 == TYPEC_CC_VOLT_OPEN)) {
+ cc_is_open(cc1, cc2)) {
set_state(port, PD_STATE_DRP_AUTO_TOGGLE);
timeout = 2*MSEC;
break;
@@ -2951,10 +2938,8 @@ void pd_task(void *u)
#endif
/* Vnc monitoring */
- if ((cc1 == TYPEC_CC_VOLT_RD ||
- cc2 == TYPEC_CC_VOLT_RD) ||
- (cc1 == TYPEC_CC_VOLT_RA &&
- cc2 == TYPEC_CC_VOLT_RA)) {
+ if (cc_is_at_least_one_rd(cc1, cc2) ||
+ cc_is_audio_acc(cc1, cc2)) {
#ifdef CONFIG_USBC_BACKWARDS_COMPATIBLE_DFP
/* Enable VBUS */
if (pd_set_power_supply_ready(port))
@@ -3021,16 +3006,13 @@ void pd_task(void *u)
timeout = 20*MSEC;
tcpm_get_cc(port, &cc1, &cc2);
- if (cc1 == TYPEC_CC_VOLT_RD &&
- cc2 == TYPEC_CC_VOLT_RD) {
+ if (cc_is_snk_dbg_acc(cc1, cc2)) {
/* Debug accessory */
new_cc_state = PD_CC_DEBUG_ACC;
- } else if (cc1 == TYPEC_CC_VOLT_RD ||
- cc2 == TYPEC_CC_VOLT_RD) {
+ } else if (cc_is_at_least_one_rd(cc1, cc2)) {
/* UFP attached */
new_cc_state = PD_CC_UFP_ATTACHED;
- } else if (cc1 == TYPEC_CC_VOLT_RA &&
- cc2 == TYPEC_CC_VOLT_RA) {
+ } else if (cc_is_audio_acc(cc1, cc2)) {
/* Audio accessory */
new_cc_state = PD_CC_AUDIO_ACC;
} else {
@@ -3502,8 +3484,7 @@ void pd_task(void *u)
if (auto_toggle_supported &&
!(pd[port].flags & PD_FLAGS_TCPC_DRP_TOGGLE) &&
!(pd[port].flags & PD_FLAGS_TRY_SRC) &&
- (cc1 == TYPEC_CC_VOLT_OPEN &&
- cc2 == TYPEC_CC_VOLT_OPEN)) {
+ cc_is_open(cc1, cc2)) {
set_state(port, PD_STATE_DRP_AUTO_TOGGLE);
timeout = 2*MSEC;
break;
@@ -3511,8 +3492,7 @@ void pd_task(void *u)
#endif
/* Source connection monitoring */
- if (cc1 != TYPEC_CC_VOLT_OPEN ||
- cc2 != TYPEC_CC_VOLT_OPEN) {
+ if (!cc_is_open(cc1, cc2)) {
pd[port].cc_state = PD_CC_NONE;
hard_reset_count = 0;
new_cc_state = PD_CC_NONE;
diff --git a/common/usb_pd_tcpc.c b/common/usb_pd_tcpc.c
index 3e79569e2e..d50b39baa3 100644
--- a/common/usb_pd_tcpc.c
+++ b/common/usb_pd_tcpc.c
@@ -861,9 +861,9 @@ int tcpc_run(int port, int evt)
*/
return (get_time().val >= pd[port].low_power_ts.val &&
pd[port].cc_pull == TYPEC_CC_RD &&
- pd[port].cc_status[0] == TYPEC_CC_VOLT_OPEN &&
- pd[port].cc_status[1] == TYPEC_CC_VOLT_OPEN) ? 200 * MSEC :
- 10 * MSEC;
+ cc_is_open(pd[port].cc_status[0], pd[port].cc_status[1]))
+ ? 200 * MSEC
+ : 10 * MSEC;
#else
return 10*MSEC;
#endif
diff --git a/include/usb_pd_tcpm.h b/include/usb_pd_tcpm.h
index d46aef454d..0a824cc657 100644
--- a/include/usb_pd_tcpm.h
+++ b/include/usb_pd_tcpm.h
@@ -59,6 +59,47 @@ enum tcpc_transmit_complete {
TCPC_TX_COMPLETE_FAILED = 2,
};
+/**
+ * Returns whether the sink has detected a Rp resistor on the other side.
+ */
+static inline int cc_is_rp(int cc)
+{
+ return (cc == TYPEC_CC_VOLT_RP_DEF) || (cc == TYPEC_CC_VOLT_RP_1_5) ||
+ (cc == TYPEC_CC_VOLT_RP_3_0);
+}
+
+/**
+ * Returns true if both CC lines are completely open.
+ */
+static inline int cc_is_open(int cc1, int cc2)
+{
+ return cc1 == TYPEC_CC_VOLT_OPEN && cc2 == TYPEC_CC_VOLT_OPEN;
+}
+
+/**
+ * Returns true if we detect the port partner is a snk debug accessory.
+ */
+static inline int cc_is_snk_dbg_acc(int cc1, int cc2)
+{
+ return cc1 == TYPEC_CC_VOLT_RD && cc2 == TYPEC_CC_VOLT_RD;
+}
+
+/**
+ * Returns true if the port partner is an audio accessory.
+ */
+static inline int cc_is_audio_acc(int cc1, int cc2)
+{
+ return cc1 == TYPEC_CC_VOLT_RA && cc2 == TYPEC_CC_VOLT_RA;
+}
+
+/**
+ * Returns true if the port partner is presenting at least one Rd
+ */
+static inline int cc_is_at_least_one_rd(int cc1, int cc2)
+{
+ return cc1 == TYPEC_CC_VOLT_RD || cc2 == TYPEC_CC_VOLT_RD;
+}
+
struct tcpm_drv {
/**
* Initialize TCPM driver and wait for TCPC readiness.