samus: ryu: fix charge state machine init of input current

Currently charge state machine resets input current limit to default
every time AC is connected. Problem is by the time charge state machine
gets around to setting input current, it could have already been set
by successful PD negotiation, and this ends up overriding that value.
This fix has the state machine store desired input current limit, as
determined from PD negotation or any other place, and send last desired
input current limit on AC connect.

BUG=chrome-os-partner:24461
BRANCH=none
TEST=load on samus, test toggling between "pd 0 dev 5" and "pd 0 dev 20",
and test plugging and unplugging zinger numerous times, and verify charger
command always gives the expected input current limit based on PD
negotiation.

Change-Id: I18d8acc9e2085739e783c9c70c682d46bcce7fdb
Signed-off-by: Alec Berg <alecaberg@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/211639
Reviewed-by: Bill Richardson <wfrichar@chromium.org>
This commit is contained in:
Alec Berg
2014-08-08 10:54:08 -07:00
committed by chrome-internal-fetch
parent adbd5a0c9a
commit 283fe98939
5 changed files with 37 additions and 16 deletions

View File

@@ -3,7 +3,7 @@
* found in the LICENSE file.
*/
#include "charger.h"
#include "charge_state.h"
#include "common.h"
#include "console.h"
#include "gpio.h"
@@ -80,7 +80,7 @@ int pd_choose_voltage(int cnt, uint32_t *src_caps, uint32_t *rdo)
void pd_set_input_current_limit(uint32_t max_ma)
{
int rv = charger_set_input_current(MAX(max_ma,
int rv = charge_set_input_current_limit(MAX(max_ma,
CONFIG_CHARGER_INPUT_CURRENT));
if (rv < 0)
CPRINTS("Failed to set input current limit for PD");

View File

@@ -53,6 +53,7 @@ enum problem_type {
PR_SET_VOLTAGE,
PR_SET_CURRENT,
PR_SET_MODE,
PR_SET_INPUT_CURR,
PR_POST_INIT,
PR_CHG_FLAGS,
PR_BATT_FLAGS,
@@ -65,6 +66,7 @@ static const char * const prob_text[] = {
"set voltage",
"set current",
"set mode",
"set input current",
"post init",
"chg params",
"batt params",
@@ -464,6 +466,7 @@ void charger_task(void)
/* Initialize all the state */
memset(&curr, 0, sizeof(curr));
curr.batt.is_present = BP_NOT_SURE;
curr.desired_input_current = CONFIG_CHARGER_INPUT_CURRENT;
prev_ac = prev_charge = -1;
state_machine_force_idle = 0;
shutdown_warning_time.val = 0UL;
@@ -481,13 +484,20 @@ void charger_task(void)
/*
* Some chargers are unpowered when the AC is
* off, so we'll reinitialize it when AC
* comes back. Try again if it fails.
* comes back and set the input current limit.
* Try again if it fails.
*/
int rv = charger_post_init();
if (rv != EC_SUCCESS)
if (rv != EC_SUCCESS) {
problem(PR_POST_INIT, rv);
else
prev_ac = curr.ac;
} else {
rv = charger_set_input_current(
curr.desired_input_current);
if (rv != EC_SUCCESS)
problem(PR_SET_INPUT_CURR, rv);
else
prev_ac = curr.ac;
}
} else {
/* Some things are only meaningful on AC */
state_machine_force_idle = 0;
@@ -811,6 +821,12 @@ int charge_temp_sensor_get_val(int idx, int *temp_ptr)
return EC_SUCCESS;
}
int charge_set_input_current_limit(int ma)
{
curr.desired_input_current = ma;
return charger_set_input_current(ma);
}
/*****************************************************************************/
/* Hooks */

View File

@@ -57,7 +57,7 @@ static void pd_exchange_status(void)
*/
pd_status.curr_lim_ma = pd_status.curr_lim_ma * 2 / 3;
#endif
rv = charger_set_input_current(MAX(pd_status.curr_lim_ma,
rv = charge_set_input_current_limit(MAX(pd_status.curr_lim_ma,
CONFIG_CHARGER_INPUT_CURRENT));
if (rv < 0)
CPRINTS("Failed to set input current limit from PD MCU");

View File

@@ -169,18 +169,12 @@ int charger_set_voltage(int voltage)
/* Charging power state initialization */
int charger_post_init(void)
{
int rv;
#ifdef CONFIG_CHARGER_ILIM_PIN_DISABLED
int rv;
int option2;
#endif
/* Set charger input current limit */
rv = charger_set_input_current(CONFIG_CHARGER_INPUT_CURRENT);
#ifdef CONFIG_CHARGER_ILIM_PIN_DISABLED
if (rv)
return rv;
/* Read the external ILIM pin enabled flag. */
rv = i2c_read16(I2C_PORT_CHARGER, BQ24773_ADDR,
BQ24773_CHARGE_OPTION2, &option2);
@@ -193,9 +187,10 @@ int charger_post_init(void)
rv = i2c_write16(I2C_PORT_CHARGER, BQ24773_ADDR,
BQ24773_CHARGE_OPTION2, option2);
}
#endif
return rv;
#else
return EC_SUCCESS;
#endif
}
int charger_discharge_on_ac(int enable)

View File

@@ -32,6 +32,7 @@ struct charge_state_data {
enum charge_state_v2 state;
int requested_voltage;
int requested_current;
int desired_input_current;
};
/*
@@ -57,5 +58,14 @@ enum ec_status charger_profile_override_get_param(uint32_t param,
enum ec_status charger_profile_override_set_param(uint32_t param,
uint32_t value);
/**
* Set the charge input current limit. This value is stored and sent every
* time AC is applied.
*
* @param ma New input current limit in mA
* @return EC_SUCCESS or error
*/
int charge_set_input_current_limit(int ma);
#endif /* __CROS_EC_CHARGE_STATE_V2_H */