summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJett Rink <jettrink@chromium.org>2019-08-08 10:06:40 -0600
committerCommit Bot <commit-bot@chromium.org>2020-07-14 06:13:26 +0000
commita46c594678542b230c04709f537b5cdba590e8e4 (patch)
treec8a3dde6a01e1e26a47dd314bdd141ffa9b27332
parent4a4b7ee70d2a693e96aa185fa4c25b57d9f6a51e (diff)
downloadchrome-ec-a46c594678542b230c04709f537b5cdba590e8e4.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. BRANCH=firmware-nami-10775.B BUG=b:137493121 TEST=With other PD Policies patches, flash nami 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/+/2274384 Tested-by: Dawid Niedźwiecki <dn@semihalf.com> Commit-Queue: Dossym Nurmukhanov <dossym@chromium.org> Reviewed-by: Daisuke Nojiri <dnojiri@chromium.org>
-rw-r--r--board/fizz/board.h1
-rw-r--r--board/it83xx_evb/board.h1
-rw-r--r--board/samus_pd/board.h3
-rw-r--r--common/build.mk1
-rw-r--r--common/usb_common.c83
-rw-r--r--common/usb_pd_protocol.c69
-rw-r--r--include/charge_state.h7
-rw-r--r--include/usb_common.h37
-rw-r--r--include/usb_pd.h6
-rw-r--r--include/usb_pd_tcpm.h9
10 files changed, 151 insertions, 66 deletions
diff --git a/board/fizz/board.h b/board/fizz/board.h
index 30b2ef8276..b6bf124b75 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 2efeb35d56..3686e3decf 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 671adfa53e..263d416f91 100644
--- a/common/build.mk
+++ b/common/build.mk
@@ -119,6 +119,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..f5d24cce5b
--- /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_SNK_3_0:
+ if (!cc_is_rp(cc_alt) || cc_alt == TYPEC_CC_VOLT_SNK_DEF)
+ charge = 3000;
+ else if (cc_alt == TYPEC_CC_VOLT_SNK_1_5)
+ charge = 500;
+ break;
+ case TYPEC_CC_VOLT_SNK_1_5:
+ charge = 1500;
+ break;
+ case TYPEC_CC_VOLT_SNK_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_SNK_3_0 > TYPEC_CC_VOLT_SNK_1_5
+ * TYPEC_CC_VOLT_RP_1_5 > TYPEC_CC_VOLT_SNK_DEF
+ * TYPEC_CC_VOLT_SNK_DEF > TYPEC_CC_VOLT_OPEN
+ */
+ return cc2 > cc1;
+}
diff --git a/common/usb_pd_protocol.c b/common/usb_pd_protocol.c
index 945faddd8e..55257b17a2 100644
--- a/common/usb_pd_protocol.c
+++ b/common/usb_pd_protocol.c
@@ -21,6 +21,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"
@@ -2058,63 +2059,7 @@ void pd_ping_enable(int port, int enable)
pd[port].flags &= ~PD_FLAGS_PING_ENABLED;
}
-/**
- * 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_SNK_DEF) || (cc == TYPEC_CC_VOLT_SNK_1_5) ||
- (cc == TYPEC_CC_VOLT_SNK_3_0);
-}
-
-/*
- * 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_SNK_3_0 > TYPEC_CC_VOLT_SNK_1_5
- * TYPEC_CC_VOLT_SNK_1_5 > TYPEC_CC_VOLT_SNK_DEF
- * TYPEC_CC_VOLT_SNK_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_SNK_3_0 && cc_alt != TYPEC_CC_VOLT_SNK_1_5)
- charge = 3000;
- else if (cc == TYPEC_CC_VOLT_SNK_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.
@@ -3121,8 +3066,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
@@ -3255,13 +3200,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 e19a2a9ab1..7d71c68cd2 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 a1c75bca75..9ac8c82cc2 100644
--- a/include/usb_pd.h
+++ b/include/usb_pd.h
@@ -864,6 +864,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
diff --git a/include/usb_pd_tcpm.h b/include/usb_pd_tcpm.h
index 35987e3965..8e458b9765 100644
--- a/include/usb_pd_tcpm.h
+++ b/include/usb_pd_tcpm.h
@@ -56,6 +56,15 @@ 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_SNK_DEF) || (cc == TYPEC_CC_VOLT_SNK_1_5) ||
+ (cc == TYPEC_CC_VOLT_SNK_3_0);
+}
+
struct tcpm_drv {
/**
* Initialize TCPM driver and wait for TCPC readiness.