summaryrefslogtreecommitdiff
path: root/common/usb_common.c
diff options
context:
space:
mode:
Diffstat (limited to 'common/usb_common.c')
-rw-r--r--common/usb_common.c85
1 files changed, 85 insertions, 0 deletions
diff --git a/common/usb_common.c b/common/usb_common.c
new file mode 100644
index 0000000000..b8659c4641
--- /dev/null
+++ b/common/usb_common.c
@@ -0,0 +1,85 @@
+/* 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;
+ }
+
+#ifdef CONFIG_USBC_DISABLE_CHARGE_FROM_RP_DEF
+ if (charge == 500)
+ charge = 0;
+#endif
+
+ 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;
+}