diff options
-rw-r--r-- | common/charge_manager.c | 19 | ||||
-rw-r--r-- | common/usb_pd_protocol.c | 13 | ||||
-rw-r--r-- | include/charge_manager.h | 10 | ||||
-rw-r--r-- | test/charge_manager.c | 8 |
4 files changed, 47 insertions, 3 deletions
diff --git a/common/charge_manager.c b/common/charge_manager.c index a4d9d8dea4..cd59c93007 100644 --- a/common/charge_manager.c +++ b/common/charge_manager.c @@ -817,6 +817,25 @@ void charge_manager_set_ceil(int port, enum ceil_requestor requestor, int ceil) } } +void charge_manager_force_ceil(int port, int ceil) +{ + ASSERT(port >= 0 && port < CONFIG_USB_PD_PORT_COUNT); + + /* + * Force our input current to ceil if we're exceeding it, without + * waiting for our deferred task to run. + */ + if (port == charge_port && ceil < charge_current) + board_set_charge_limit(port, CHARGE_SUPPLIER_PD, + ceil, charge_current_uncapped); + + /* + * Now inform charge_manager so it stays in sync with the state of + * the world. + */ + charge_manager_set_ceil(port, CEIL_REQUESTOR_PD, ceil); +} + /** * Select an 'override port', a port which is always the preferred charge port. * Returns EC_SUCCESS on success, ec_error_list status on failure. diff --git a/common/usb_pd_protocol.c b/common/usb_pd_protocol.c index 4370835fe1..3ea1dbf723 100644 --- a/common/usb_pd_protocol.c +++ b/common/usb_pd_protocol.c @@ -659,9 +659,16 @@ static int pd_send_request_msg(int port, int always_send_request) */ return -1; - /* Don't re-request the same voltage */ - if (!always_send_request && pd[port].prev_request_mv == supply_voltage) - return EC_SUCCESS; + if (!always_send_request) { + /* Don't re-request the same voltage */ + if (pd[port].prev_request_mv == supply_voltage) + return EC_SUCCESS; +#ifdef CONFIG_CHARGE_MANAGER + /* Limit current to PD_MIN_MA during transition */ + else + charge_manager_force_ceil(port, PD_MIN_MA); +#endif + } CPRINTF("Req C%d [%d] %dmV %dmA", port, RDO_POS(rdo), supply_voltage, curr_limit); diff --git a/include/charge_manager.h b/include/charge_manager.h index 939471163b..71cc8589aa 100644 --- a/include/charge_manager.h +++ b/include/charge_manager.h @@ -73,6 +73,16 @@ enum ceil_requestor { /* Update charge ceiling for a given port / requestor */ void charge_manager_set_ceil(int port, enum ceil_requestor requestor, int ceil); +/* + * Update PD charge ceiling for a given port. In the event that our ceiling + * is currently above ceil, change the current limit before returning, without + * waiting for a charge manager refresh. This function should only be used in + * time-critical situations where we absolutely cannot proceed without limiting + * our input current, and it should only be called from the PD tasks. + * If you ever call this function then you are a terrible person. + */ +void charge_manager_force_ceil(int port, int ceil); + /* Select an 'override port', which is always the preferred charge port */ int charge_manager_set_override(int port); int charge_manager_get_override(void); diff --git a/test/charge_manager.c b/test/charge_manager.c index bb65c2a3f0..1c08245b92 100644 --- a/test/charge_manager.c +++ b/test/charge_manager.c @@ -288,6 +288,14 @@ static int test_charge_ceil(void) TEST_ASSERT(active_charge_port == 1); TEST_ASSERT(active_charge_limit == 2500); + /* Verify forced ceil takes effect immediately */ + charge_manager_force_ceil(1, 500); + TEST_ASSERT(active_charge_port == 1); + TEST_ASSERT(active_charge_limit == 500); + wait_for_charge_manager_refresh(); + TEST_ASSERT(active_charge_port == 1); + TEST_ASSERT(active_charge_limit == 500); + return EC_SUCCESS; } |