summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTing Shen <phoenixshen@google.com>2020-05-06 18:06:52 +0800
committerCommit Bot <commit-bot@chromium.org>2020-05-29 05:11:24 +0000
commit8975cbb645f1f07c3682334d02510a8a1d3f730b (patch)
tree1c65f1c460b25e785181b15c2d78b303b4cfacfc
parentfad8f404df3e1b2278326c08247a62edce434f32 (diff)
downloadchrome-ec-8975cbb645f1f07c3682334d02510a8a1d3f730b.tar.gz
bc12: create bc12 driver structure
To support linking multiple bc12 drivers into ec binary, we have to remove the common symbols in drivers. This CL creates a bc12_drv structure to hold the original driver-specific usb_charger_* functions, and implements common usb_charger_* functions which whill dispatch the function call based on the information in bc12_drv table. Also add a CONFIG_BC12_SINGLE_DRIVER and enabled by default for backward compatibility. If CONFIG_BC12_SINGLE_DRIVER is defined, a default bc12_drv array with proper size and content will be created for the board. BUG=b:155611686 TEST=1) make buildall. 2) verify single driver mode on krane (rt946x) and juniper (pi3usb9201). 3) verify multiple driver works on asurada, see CL:2189624 for example usage. 4) verify single driver multi chip on blooglet. BRANCH=none Signed-off-by: Ting Shen <phoenixshen@google.com> Change-Id: I8a96eda47c94aeb6cc150b498cfa1a6eefcc4a5b Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2187080 Reviewed-by: Jett Rink <jettrink@chromium.org> Commit-Queue: Ting Shen <phoenixshen@chromium.org> Tested-by: Ting Shen <phoenixshen@chromium.org>
-rw-r--r--board/glkrvp/chg_usb_pd.c5
-rw-r--r--board/glkrvp_ite/chg_usb_pd.c5
-rw-r--r--common/charge_manager.c10
-rw-r--r--common/charge_ramp.c10
-rw-r--r--common/charge_ramp_sw.c8
-rw-r--r--common/usb_charger.c9
-rw-r--r--driver/bc12/max14637.c31
-rw-r--r--driver/bc12/max14637.h1
-rw-r--r--driver/bc12/pi3usb9201.c32
-rw-r--r--driver/bc12/pi3usb9201.h1
-rw-r--r--driver/bc12/pi3usb9281.c34
-rw-r--r--driver/bc12/pi3usb9281.h1
-rw-r--r--driver/charger/bd9995x.c43
-rw-r--r--driver/charger/rt946x.c27
-rw-r--r--driver/charger/rt946x.h1
-rw-r--r--include/charge_ramp.h6
-rw-r--r--include/config.h9
-rw-r--r--include/usb_charge.h60
-rw-r--r--test/charge_ramp.c17
19 files changed, 226 insertions, 84 deletions
diff --git a/board/glkrvp/chg_usb_pd.c b/board/glkrvp/chg_usb_pd.c
index f396177d81..ff6aefbf97 100644
--- a/board/glkrvp/chg_usb_pd.c
+++ b/board/glkrvp/chg_usb_pd.c
@@ -65,11 +65,6 @@ const struct usb_mux usb_muxes[CONFIG_USB_PD_PORT_MAX_COUNT] = {
},
};
-/* TODO: Implement this function and move to appropriate file */
-void usb_charger_set_switches(int port, enum usb_switch setting)
-{
-}
-
static int board_charger_port_is_sourcing_vbus(int port)
{
int reg;
diff --git a/board/glkrvp_ite/chg_usb_pd.c b/board/glkrvp_ite/chg_usb_pd.c
index 0f425ecf7b..2e8d237e64 100644
--- a/board/glkrvp_ite/chg_usb_pd.c
+++ b/board/glkrvp_ite/chg_usb_pd.c
@@ -65,11 +65,6 @@ const struct usb_mux usb_muxes[CONFIG_USB_PD_PORT_MAX_COUNT] = {
},
};
-/* TODO: Implement this function and move to appropriate file */
-void usb_charger_set_switches(int port, enum usb_switch setting)
-{
-}
-
static int board_charger_port_is_sourcing_vbus(int port)
{
int reg;
diff --git a/common/charge_manager.c b/common/charge_manager.c
index c12fc12318..388e74fc1c 100644
--- a/common/charge_manager.c
+++ b/common/charge_manager.c
@@ -431,7 +431,7 @@ static void charge_manager_fill_power_info(int port,
#if defined(HAS_TASK_CHG_RAMP) || defined(CONFIG_CHARGE_RAMP_HW)
/* Read ramped current if active charging port */
use_ramp_current =
- (charge_port == port) && chg_ramp_allowed(sup);
+ (charge_port == port) && chg_ramp_allowed(port, sup);
#else
use_ramp_current = 0;
#endif
@@ -449,7 +449,7 @@ static void charge_manager_fill_power_info(int port,
* available charge current.
*/
r->meas.current_max = chg_ramp_is_stable() ?
- r->meas.current_lim : chg_ramp_max(sup,
+ r->meas.current_lim : chg_ramp_max(port, sup,
available_charge[sup][port].current);
r->max_power =
@@ -728,9 +728,9 @@ static void charge_manager_refresh(void)
* Allow to set the maximum current value, so the hardware can
* know the range of acceptable current values for its ramping.
*/
- if (chg_ramp_allowed(new_supplier))
+ if (chg_ramp_allowed(new_port, new_supplier))
new_charge_current_uncapped =
- chg_ramp_max(new_supplier,
+ chg_ramp_max(new_port, new_supplier,
new_charge_current_uncapped);
#endif /* CONFIG_CHARGE_RAMP_HW */
/* Enforce port charge ceiling. */
@@ -756,7 +756,7 @@ static void charge_manager_refresh(void)
#else
#ifdef CONFIG_CHARGE_RAMP_HW
/* Enable or disable charge ramp */
- charger_set_hw_ramp(chg_ramp_allowed(new_supplier));
+ charger_set_hw_ramp(chg_ramp_allowed(new_port, new_supplier));
#endif
board_set_charge_limit(new_port, new_supplier,
new_charge_current,
diff --git a/common/charge_ramp.c b/common/charge_ramp.c
index 3ca5bd89bb..e95cb0ed06 100644
--- a/common/charge_ramp.c
+++ b/common/charge_ramp.c
@@ -11,7 +11,7 @@
#include "usb_charge.h"
#include "util.h"
-test_mockable int chg_ramp_allowed(int supplier)
+test_mockable int chg_ramp_allowed(int port, int supplier)
{
/* Don't allow ramping in RO when write protected. */
if (!system_is_in_rw() && system_is_locked())
@@ -28,11 +28,11 @@ test_mockable int chg_ramp_allowed(int supplier)
/* default: fall through */
}
- /* Othewise ask the BC1.2 detect module */
- return usb_charger_ramp_allowed(supplier);
+ /* Otherwise ask the BC1.2 detect module */
+ return usb_charger_ramp_allowed(port, supplier);
}
-test_mockable int chg_ramp_max(int supplier, int sup_curr)
+test_mockable int chg_ramp_max(int port, int supplier, int sup_curr)
{
switch (supplier) {
#ifdef CONFIG_CHARGE_RAMP_HW
@@ -49,5 +49,5 @@ test_mockable int chg_ramp_max(int supplier, int sup_curr)
}
/* Otherwise ask the BC1.2 detect module */
- return usb_charger_ramp_max(supplier, sup_curr);
+ return usb_charger_ramp_max(port, supplier, sup_curr);
}
diff --git a/common/charge_ramp_sw.c b/common/charge_ramp_sw.c
index 2916894e8c..bfd6db057b 100644
--- a/common/charge_ramp_sw.c
+++ b/common/charge_ramp_sw.c
@@ -101,9 +101,9 @@ void chg_ramp_charge_supplier_change(int port, int supplier, int current,
active_vtg = voltage;
/* Set min and max input current limit based on if ramp is allowed */
- if (chg_ramp_allowed(active_sup)) {
+ if (chg_ramp_allowed(active_port, active_sup)) {
min_icl = RAMP_CURR_START_MA;
- max_icl = chg_ramp_max(active_sup, current);
+ max_icl = chg_ramp_max(active_port, active_sup, current);
} else {
min_icl = max_icl = current;
}
@@ -219,7 +219,7 @@ void chg_ramp_task(void *u)
task_wait_time = SECOND;
/* Skip ramp for specific suppliers */
- if (!chg_ramp_allowed(active_sup)) {
+ if (!chg_ramp_allowed(active_port, active_sup)) {
active_icl_new = min_icl;
ramp_st_new = CHG_RAMP_STABLE;
break;
@@ -315,7 +315,7 @@ void chg_ramp_task(void *u)
}
/* Keep an eye on VBUS and restart ramping if it dips */
- if (chg_ramp_allowed(active_sup) &&
+ if (chg_ramp_allowed(active_port, active_sup) &&
board_is_vbus_too_low(active_port,
CHG_RAMP_VBUS_STABLE)) {
CPRINTS("VBUS low; Re-ramp");
diff --git a/common/usb_charger.c b/common/usb_charger.c
index 4a85dc6daf..a8396b47bd 100644
--- a/common/usb_charger.c
+++ b/common/usb_charger.c
@@ -23,6 +23,7 @@
#include "usb_charge.h"
#include "usb_pd.h"
#include "usbc_ppc.h"
+#include "util.h"
static void update_vbus_supplier(int port, int vbus_level)
{
@@ -118,3 +119,11 @@ static void usb_charger_init(void)
}
}
DECLARE_HOOK(HOOK_INIT, usb_charger_init, HOOK_PRIO_CHARGE_MANAGER_INIT + 1);
+
+void usb_charger_task(void *u)
+{
+ int port = TASK_ID_TO_USB_CHG_PORT(task_get_current());
+
+ ASSERT(bc12_ports[port].drv->usb_charger_task);
+ bc12_ports[port].drv->usb_charger_task(port);
+}
diff --git a/driver/bc12/max14637.c b/driver/bc12/max14637.c
index 2aab525f00..91b5441a68 100644
--- a/driver/bc12/max14637.c
+++ b/driver/bc12/max14637.c
@@ -151,9 +151,8 @@ static void detect_or_power_down_ic(const int port)
}
}
-void usb_charger_task(void *u)
+static void max14637_usb_charger_task(const int port)
{
- const int port = (intptr_t)u;
uint32_t evt;
const struct max14637_config_t * const cfg = &max14637_config[port];
@@ -175,15 +174,8 @@ void usb_charger_task(void *u)
}
}
-void usb_charger_set_switches(int port, enum usb_switch setting)
-{
- /*
- * The MAX14637 automatically sets up the USB 2.0 high-speed switches.
- */
-}
-
#if defined(CONFIG_CHARGE_RAMP_SW) || defined(CONFIG_CHARGE_RAMP_HW)
-int usb_charger_ramp_allowed(int supplier)
+static int max14637_ramp_allowed(int supplier)
{
/*
* Due to the limitations in the application of the MAX14637, we
@@ -193,7 +185,7 @@ int usb_charger_ramp_allowed(int supplier)
return supplier == CHARGE_SUPPLIER_OTHER;
}
-int usb_charger_ramp_max(int supplier, int sup_curr)
+static int max14637_ramp_max(int supplier, int sup_curr)
{
/* Use the current limit that was decided by the MAX14637. */
if (supplier == CHARGE_SUPPLIER_OTHER)
@@ -220,3 +212,20 @@ static void bc12_chipset_startup(void)
}
DECLARE_HOOK(HOOK_CHIPSET_STARTUP, bc12_chipset_startup, HOOK_PRIO_DEFAULT);
DECLARE_HOOK(HOOK_CHIPSET_RESUME, bc12_chipset_startup, HOOK_PRIO_DEFAULT);
+
+const struct bc12_drv max14637_drv = {
+ .usb_charger_task = max14637_usb_charger_task,
+#if defined(CONFIG_CHARGE_RAMP_SW) || defined(CONFIG_CHARGE_RAMP_HW)
+ .ramp_allowed = max14637_ramp_allowed,
+ .ramp_max = max14637_ramp_max,
+#endif /* CONFIG_CHARGE_RAMP_SW || CONFIG_CHARGE_RAMP_HW */
+};
+
+#ifdef CONFIG_BC12_SINGLE_DRIVER
+/* provide a default bc12_ports[] for backward compatibility */
+struct bc12_config bc12_ports[CHARGE_PORT_COUNT] = {
+ [0 ... (CHARGE_PORT_COUNT - 1)] = {
+ .drv = &max14637_drv,
+ },
+};
+#endif /* CONFIG_BC12_SINGLE_DRIVER */
diff --git a/driver/bc12/max14637.h b/driver/bc12/max14637.h
index 789504b1eb..2b18bc222b 100644
--- a/driver/bc12/max14637.h
+++ b/driver/bc12/max14637.h
@@ -30,3 +30,4 @@ struct max14637_config_t {
*/
extern const struct max14637_config_t
max14637_config[CONFIG_USB_PD_PORT_MAX_COUNT];
+extern const struct bc12_drv max14637_drv;
diff --git a/driver/bc12/pi3usb9201.c b/driver/bc12/pi3usb9201.c
index f8dbadd5c7..5178d81dae 100644
--- a/driver/bc12/pi3usb9201.c
+++ b/driver/bc12/pi3usb9201.c
@@ -227,9 +227,8 @@ static void bc12_power_up(int port)
pi3usb9201_interrupt_mask(port, 1);
}
-void usb_charger_task(void *u)
+static void pi3usb9201_usb_charger_task(const int port)
{
- int port = (task_get_current() == TASK_ID_USB_CHG_P0 ? 0 : 1);
uint32_t evt;
int i;
@@ -325,16 +324,8 @@ void usb_charger_task(void *u)
}
}
-void usb_charger_set_switches(int port, enum usb_switch setting)
-{
- /*
- * Switches are controlled automatically based on whether the port is
- * acting as a source or as sink and the result of BC1.2 detection.
- */
-}
-
#if defined(CONFIG_CHARGE_RAMP_SW) || defined(CONFIG_CHARGE_RAMP_HW)
-int usb_charger_ramp_allowed(int supplier)
+static int pi3usb9201_ramp_allowed(int supplier)
{
/* Don't allow ramp if charge supplier is OTHER, SDP, or NONE */
return !(supplier == CHARGE_SUPPLIER_OTHER ||
@@ -342,7 +333,7 @@ int usb_charger_ramp_allowed(int supplier)
supplier == CHARGE_SUPPLIER_NONE);
}
-int usb_charger_ramp_max(int supplier, int sup_curr)
+static int pi3usb9201_ramp_max(int supplier, int sup_curr)
{
/*
* Use the level from the bc12_chg_limits table above except for
@@ -362,3 +353,20 @@ int usb_charger_ramp_max(int supplier, int sup_curr)
}
}
#endif /* CONFIG_CHARGE_RAMP_SW || CONFIG_CHARGE_RAMP_HW */
+
+const struct bc12_drv pi3usb9201_drv = {
+ .usb_charger_task = pi3usb9201_usb_charger_task,
+#if defined(CONFIG_CHARGE_RAMP_SW) || defined(CONFIG_CHARGE_RAMP_HW)
+ .ramp_allowed = pi3usb9201_ramp_allowed,
+ .ramp_max = pi3usb9201_ramp_max,
+#endif /* CONFIG_CHARGE_RAMP_SW || CONFIG_CHARGE_RAMP_HW */
+};
+
+#ifdef CONFIG_BC12_SINGLE_DRIVER
+/* provide a default bc12_ports[] for backward compatibility */
+struct bc12_config bc12_ports[CHARGE_PORT_COUNT] = {
+ [0 ... (CHARGE_PORT_COUNT - 1)] = {
+ .drv = &pi3usb9201_drv,
+ }
+};
+#endif /* CONFIG_BC12_SINGLE_DRIVER */
diff --git a/driver/bc12/pi3usb9201.h b/driver/bc12/pi3usb9201.h
index d9ee80a0d0..e3029999ca 100644
--- a/driver/bc12/pi3usb9201.h
+++ b/driver/bc12/pi3usb9201.h
@@ -50,3 +50,4 @@ enum pi3usb9201_mode {
/* Configuration struct defined at board level */
extern const struct pi3usb9201_config_t pi3usb9201_bc12_chips[];
+extern const struct bc12_drv pi3usb9201_drv;
diff --git a/driver/bc12/pi3usb9281.c b/driver/bc12/pi3usb9281.c
index 1e1304e4f3..85b6cf2282 100644
--- a/driver/bc12/pi3usb9281.c
+++ b/driver/bc12/pi3usb9281.c
@@ -259,7 +259,7 @@ static int pi3usb9281_set_pins(int port, uint8_t val)
return pi3usb9281_write(port, PI3USB9281_REG_MANUAL, val);
}
-static int pi3usb9281_set_switches(int port, int open)
+static int pi3usb9281_set_switches_impl(int port, int open)
{
int res = EC_ERROR_UNKNOWN;
uint8_t ctrl;
@@ -279,7 +279,7 @@ static int pi3usb9281_set_switches(int port, int open)
return res;
}
-void usb_charger_set_switches(int port, enum usb_switch setting)
+static void pi3usb9281_set_switches(int port, enum usb_switch setting)
{
/* If switch is not changing then return */
if (setting == usb_switch_state[port])
@@ -357,7 +357,7 @@ static uint32_t bc12_detect(int port)
* Restore data switch settings - switches return to
* closed on reset until restored.
*/
- usb_charger_set_switches(port, USB_SWITCH_RESTORE);
+ pi3usb9281_set_switches(port, USB_SWITCH_RESTORE);
/*
* Wait after reset, before re-enabling interrupt, so that
@@ -416,9 +416,8 @@ static uint32_t bc12_detect(int port)
return evt;
}
-void usb_charger_task(void *u)
+static void pi3usb9281_usb_charger_task(const int port)
{
- int port = (task_get_current() == TASK_ID_USB_CHG_P0 ? 0 : 1);
uint32_t evt;
/* Initialize chip and enable interrupts */
@@ -441,7 +440,8 @@ void usb_charger_task(void *u)
}
if (evt & USB_CHG_EVENT_MUX)
- pi3usb9281_set_switches(port, usb_switch_state[port]);
+ pi3usb9281_set_switches_impl(
+ port, usb_switch_state[port]);
/*
* Re-enable interrupts on pericom charger detector since the
@@ -462,7 +462,7 @@ void usb_charger_task(void *u)
}
#if defined(CONFIG_CHARGE_RAMP_SW) || defined(CONFIG_CHARGE_RAMP_HW)
-int usb_charger_ramp_allowed(int supplier)
+static int pi3usb9281_ramp_allowed(int supplier)
{
return supplier == CHARGE_SUPPLIER_BC12_DCP ||
supplier == CHARGE_SUPPLIER_BC12_SDP ||
@@ -470,7 +470,7 @@ int usb_charger_ramp_allowed(int supplier)
supplier == CHARGE_SUPPLIER_PROPRIETARY;
}
-int usb_charger_ramp_max(int supplier, int sup_curr)
+static int pi3usb9281_ramp_max(int supplier, int sup_curr)
{
switch (supplier) {
case CHARGE_SUPPLIER_BC12_DCP:
@@ -485,3 +485,21 @@ int usb_charger_ramp_max(int supplier, int sup_curr)
}
}
#endif /* CONFIG_CHARGE_RAMP_SW || CONFIG_CHARGE_RAMP_HW */
+
+const struct bc12_drv pi3usb9281_drv = {
+ .usb_charger_task = pi3usb9281_usb_charger_task,
+ .set_switches = pi3usb9281_set_switches,
+#if defined(CONFIG_CHARGE_RAMP_SW) || defined(CONFIG_CHARGE_RAMP_HW)
+ .ramp_allowed = pi3usb9281_ramp_allowed,
+ .ramp_max = pi3usb9281_ramp_max,
+#endif /* CONFIG_CHARGE_RAMP_SW || CONFIG_CHARGE_RAMP_HW */
+};
+
+#ifdef CONFIG_BC12_SINGLE_DRIVER
+/* provide a default bc12_ports[] for backward compatibility */
+struct bc12_config bc12_ports[CHARGE_PORT_COUNT] = {
+ [0 ... (CHARGE_PORT_COUNT - 1)] = {
+ .drv = &pi3usb9281_drv,
+ },
+};
+#endif /* CONFIG_BC12_SINGLE_DRIVER */
diff --git a/driver/bc12/pi3usb9281.h b/driver/bc12/pi3usb9281.h
index 69b671500e..ca1828f49c 100644
--- a/driver/bc12/pi3usb9281.h
+++ b/driver/bc12/pi3usb9281.h
@@ -79,4 +79,5 @@ int pi3usb9281_enable_interrupts(int port);
/* Get the device type */
int pi3usb9281_get_device_type(int port);
+extern const struct bc12_drv pi3usb9281_drv;
#endif /* __CROS_EC_PI3USB9281_H */
diff --git a/driver/charger/bd9995x.c b/driver/charger/bd9995x.c
index 58f13af6f6..2531d4225d 100644
--- a/driver/charger/bd9995x.c
+++ b/driver/charger/bd9995x.c
@@ -584,7 +584,7 @@ static int usb_charger_process(int chgnum, int port)
}
#ifdef CONFIG_CHARGE_RAMP_SW
-int usb_charger_ramp_allowed(int supplier)
+static int bd9995x_ramp_allowed(int supplier)
{
return supplier == CHARGE_SUPPLIER_BC12_DCP ||
supplier == CHARGE_SUPPLIER_BC12_SDP ||
@@ -592,7 +592,7 @@ int usb_charger_ramp_allowed(int supplier)
supplier == CHARGE_SUPPLIER_OTHER;
}
-int usb_charger_ramp_max(int supplier, int sup_curr)
+static int bd9995x_ramp_max(int supplier, int sup_curr)
{
return bd9995x_get_bc12_ilim(supplier);
}
@@ -1261,7 +1261,7 @@ int bd9995x_bc12_enable_charging(int port, int enable)
BD9995X_EXTENDED_COMMAND);
}
-void usb_charger_set_switches(int port, enum usb_switch setting)
+static void bd9995x_set_switches(int port, enum usb_switch setting)
{
/* If switch is not changing then return */
if (setting == usb_switch_state[port])
@@ -1288,7 +1288,7 @@ void bd9995x_vbus_interrupt(enum gpio_signal signal)
task_wake(TASK_ID_USB_CHG);
}
-void usb_charger_task(void *u)
+static void bd9995x_usb_charger_task(const int unused)
{
static int initialized;
int changed, port, interrupts;
@@ -1725,3 +1725,38 @@ const struct charger_drv bd9995x_drv = {
.get_option = &bd9995x_get_option,
.set_option = &bd9995x_set_option,
};
+
+#ifdef CONFIG_BC12_SINGLE_DRIVER
+/* provide a default bc12_ports[] for backward compatibility */
+struct bc12_config bc12_ports[BD9995X_CHARGE_PORT_COUNT] = {
+ {
+ .drv = &(const struct bc12_drv) {
+ .usb_charger_task = bd9995x_usb_charger_task,
+ .set_switches = bd9995x_set_switches,
+#if defined(CONFIG_CHARGE_RAMP_SW)
+ .ramp_allowed = bd9995x_ramp_allowed,
+ .ramp_max = bd9995x_ramp_max,
+#endif /* CONFIG_CHARGE_RAMP_SW */
+ },
+ },
+ {
+ .drv = &(const struct bc12_drv) {
+ /* bd9995x uses a single task thread for both ports */
+ .usb_charger_task = NULL,
+ .set_switches = bd9995x_set_switches,
+#if defined(CONFIG_CHARGE_RAMP_SW)
+ .ramp_allowed = bd9995x_ramp_allowed,
+ .ramp_max = bd9995x_ramp_max,
+#endif /* CONFIG_CHARGE_RAMP_SW */
+ },
+ },
+};
+BUILD_ASSERT(ARRAY_SIZE(bc12_ports) == CHARGE_PORT_COUNT);
+#else
+/*
+ * TODO:
+ * This driver assumes its two ports is always on number 0 and 1.
+ * Prohibit multiple driver for safety.
+ */
+#error config not supported
+#endif /* CONFIG_BC12_SINGLE_DRIVER */
diff --git a/driver/charger/rt946x.c b/driver/charger/rt946x.c
index 4d0bb39aa2..e139161a31 100644
--- a/driver/charger/rt946x.c
+++ b/driver/charger/rt946x.c
@@ -1515,7 +1515,7 @@ static void rt946x_bc12_workaround(void)
}
DECLARE_DEFERRED(rt946x_bc12_workaround);
-void usb_charger_task(void *u)
+static void rt946x_usb_charger_task(const int unused)
{
struct charge_port_info chg;
int bc12_type = CHARGE_SUPPLIER_NONE;
@@ -1590,16 +1590,12 @@ wait_event:
}
}
-void usb_charger_set_switches(int port, enum usb_switch setting)
-{
-}
-
-int usb_charger_ramp_allowed(int supplier)
+static int rt946x_ramp_allowed(int supplier)
{
return supplier == CHARGE_SUPPLIER_BC12_DCP;
}
-int usb_charger_ramp_max(int supplier, int sup_curr)
+static int rt946x_ramp_max(int supplier, int sup_curr)
{
return rt946x_get_bc12_ilim(supplier);
}
@@ -1899,3 +1895,20 @@ const struct charger_drv rt946x_drv = {
.ramp_get_current_limit = &rt946x_ramp_get_current_limit,
#endif
};
+
+#ifdef HAS_TASK_USB_CHG
+const struct bc12_drv rt946x_bc12_drv = {
+ .usb_charger_task = rt946x_usb_charger_task,
+ .ramp_allowed = rt946x_ramp_allowed,
+ .ramp_max = rt946x_ramp_max,
+};
+
+#ifdef CONFIG_BC12_SINGLE_DRIVER
+/* provide a default bc12_ports[] for backward compatibility */
+struct bc12_config bc12_ports[CHARGE_PORT_COUNT] = {
+ [0 ... (CHARGE_PORT_COUNT - 1)] = {
+ .drv = &rt946x_bc12_drv,
+ },
+};
+#endif /* CONFIG_BC12_SINGLE_DRIVER */
+#endif
diff --git a/driver/charger/rt946x.h b/driver/charger/rt946x.h
index c2522aaf1a..38ec200c77 100644
--- a/driver/charger/rt946x.h
+++ b/driver/charger/rt946x.h
@@ -861,5 +861,6 @@ int mt6370_reduce_db_bl_driving(void);
#endif
extern const struct charger_drv rt946x_drv;
+extern const struct bc12_drv rt946x_bc12_drv;
#endif /* __CROS_EC_RT946X_H */
diff --git a/include/charge_ramp.h b/include/charge_ramp.h
index c4082fdbf2..0745f5ef98 100644
--- a/include/charge_ramp.h
+++ b/include/charge_ramp.h
@@ -29,21 +29,23 @@ int board_is_vbus_too_low(int port, enum chg_ramp_vbus_state ramp_state);
/**
* Check if ramping is allowed for given supplier
*
+ * @param port Charge ramp port
* @supplier Supplier to check
*
* @return Ramping is allowed for given supplier
*/
-int chg_ramp_allowed(int supplier);
+int chg_ramp_allowed(int port, int supplier);
/**
* Get the maximum current limit that we are allowed to ramp to
*
+ * @param port Charge ramp port
* @supplier Active supplier type
* @sup_curr Input current limit based on supplier
*
* @return Maximum current in mA
*/
-int chg_ramp_max(int supplier, int sup_curr);
+int chg_ramp_max(int port, int supplier, int sup_curr);
/**
* Get the input current limit set by ramp module
diff --git a/include/config.h b/include/config.h
index bdd3bcd251..336b3dade6 100644
--- a/include/config.h
+++ b/include/config.h
@@ -4110,6 +4110,15 @@
*/
#undef CONFIG_BC12_DETECT_DATA_ROLE_TRIGGER
+/*
+ * Board only needs one bc12 driver. This includes the case that has multiple
+ * chips that use the same driver. Enabled by default.
+ *
+ * If undefined, board should define a bc12_ports array which associates
+ * each port to its bc12 driver.
+ */
+#define CONFIG_BC12_SINGLE_DRIVER
+
/* External BC1.2 charger detection devices. */
#undef CONFIG_BC12_DETECT_MAX14637
#undef CONFIG_BC12_DETECT_PI3USB9201
diff --git a/include/usb_charge.h b/include/usb_charge.h
index 28d1fd8c84..d5142ac8e9 100644
--- a/include/usb_charge.h
+++ b/include/usb_charge.h
@@ -8,6 +8,7 @@
#ifndef __CROS_EC_USB_CHARGE_H
#define __CROS_EC_USB_CHARGE_H
+#include "charge_manager.h"
#include "common.h"
#include "ec_commands.h"
@@ -61,6 +62,17 @@ int usb_charge_set_mode(int usb_port_id, enum usb_charge_mode mode,
#define USB_CHG_EVENT_CC_OPEN TASK_EVENT_CUSTOM_BIT(5)
#define USB_CHG_EVENT_MUX TASK_EVENT_CUSTOM_BIT(6)
+/* Number of USB_CHG_* tasks */
+#ifdef HAS_TASK_USB_CHG_P2
+#define USB_CHG_TASK_COUNT 3
+#elif defined(HAS_TASK_USB_CHG_P1)
+#define USB_CHG_TASK_COUNT 2
+#elif defined(HAS_TASK_USB_CHG_P0) || defined(HAS_TASK_USB_CHG)
+#define USB_CHG_TASK_COUNT 1
+#else
+#define USB_CHG_TASK_COUNT 0
+#endif
+
/*
* Define USB_CHG_PORT_TO_TASK_ID() and TASK_ID_TO_USB_CHG__PORT() macros to
* go between USB_CHG port number and task ID. Assume that TASK_ID_USB_CHG_P0,
@@ -88,13 +100,42 @@ enum usb_switch {
USB_SWITCH_RESTORE,
};
+struct bc12_drv {
+ /* All fields below are optional */
+
+ /* BC1.2 detection task for this chip */
+ void (*usb_charger_task)(int port);
+ /* Configure USB data switches on type-C port */
+ void (*set_switches)(int port, enum usb_switch setting);
+ /* Check if ramping is allowed for given supplier */
+ int (*ramp_allowed)(int supplier);
+ /* Get the maximum current limit that we are allowed to ramp to */
+ int (*ramp_max)(int supplier, int sup_curr);
+};
+
+struct bc12_config {
+ const struct bc12_drv *drv;
+};
+/**
+ * An array of length CHARGE_PORT_COUNT which associates each
+ * pd port / dedicated charge port to bc12 driver.
+ *
+ * If CONFIG_BC12_SINGLE_DRIVER is defined, bc12 driver will provide a
+ * definition of this array. Otherwise, board should define this by themselves.
+ */
+extern struct bc12_config bc12_ports[];
+
/**
* Configure USB data switches on type-C port.
*
* @param port port number.
* @param setting new switch setting to configure.
*/
-void usb_charger_set_switches(int port, enum usb_switch setting);
+static inline void usb_charger_set_switches(int port, enum usb_switch setting)
+{
+ if (bc12_ports[port].drv->set_switches)
+ bc12_ports[port].drv->set_switches(port, setting);
+}
/**
* Notify USB_CHG task that VBUS level has changed.
@@ -107,22 +148,33 @@ void usb_charger_vbus_change(int port, int vbus_level);
/**
* Check if ramping is allowed for given supplier
*
+ * @param port port number.
* @supplier Supplier to check
*
* @return Ramping is allowed for given supplier
*/
-int usb_charger_ramp_allowed(int supplier);
+static inline int usb_charger_ramp_allowed(int port, int supplier)
+{
+ if (port < 0 || !bc12_ports[port].drv->ramp_allowed)
+ return 0;
+ return bc12_ports[port].drv->ramp_allowed(supplier);
+}
/**
* Get the maximum current limit that we are allowed to ramp to
*
+ * @param port port number.
* @supplier Active supplier type
* @sup_curr Input current limit based on supplier
*
* @return Maximum current in mA
*/
-int usb_charger_ramp_max(int supplier, int sup_curr);
-
+static inline int usb_charger_ramp_max(int port, int supplier, int sup_curr)
+{
+ if (port < 0 || !bc12_ports[port].drv->ramp_max)
+ return 0;
+ return bc12_ports[port].drv->ramp_max(supplier, sup_curr);
+}
/**
* Reset available BC 1.2 chargers on all ports
diff --git a/test/charge_ramp.c b/test/charge_ramp.c
index f6f8bd542e..faec9c8888 100644
--- a/test/charge_ramp.c
+++ b/test/charge_ramp.c
@@ -14,6 +14,7 @@
#include "task.h"
#include "test_util.h"
#include "timer.h"
+#include "usb_charge.h"
#include "util.h"
#define TASK_EVENT_OVERCURRENT (1 << 0)
@@ -37,13 +38,13 @@ static int charge_limit_ma;
/* Mock functions */
/* Override test_mockable implementations in charge_ramp module */
-int chg_ramp_allowed(int supplier)
+int chg_ramp_allowed(int port, int supplier)
{
/* Ramp for TEST4-TEST8 */
return supplier > CHARGE_SUPPLIER_TEST3;
}
-int chg_ramp_max(int supplier, int sup_curr)
+int chg_ramp_max(int port, int supplier, int sup_curr)
{
if (supplier == CHARGE_SUPPLIER_TEST7)
return 1600;
@@ -53,16 +54,8 @@ int chg_ramp_max(int supplier, int sup_curr)
return 3000;
}
-/* These usb_charger functions are unused, but necessary to link */
-int usb_charger_ramp_allowed(int supplier)
-{
- return 0;
-}
-
-int usb_charger_ramp_max(int supplier, int sup_curr)
-{
- return 0;
-}
+/* Dummy bc12_ports[] array to make linker happy */
+struct bc12_config bc12_ports[0];
int charge_is_consuming_full_input_current(void)
{