diff options
-rw-r--r-- | common/charge_state.c | 14 | ||||
-rw-r--r-- | common/charger_common.c | 8 | ||||
-rw-r--r-- | include/charger.h | 9 |
3 files changed, 28 insertions, 3 deletions
diff --git a/common/charge_state.c b/common/charge_state.c index e0646c80c5..2d342c1764 100644 --- a/common/charge_state.c +++ b/common/charge_state.c @@ -404,6 +404,7 @@ static enum power_state state_charge(struct power_state_context *ctx) struct batt_params *batt = &ctx->curr.batt; int debounce = 0; int want_current; + int want_voltage; timestamp_t now; if (curr->error) @@ -425,9 +426,16 @@ static enum power_state state_charge(struct power_state_context *ctx) now = get_time(); - if (batt->desired_voltage != curr->charging_voltage) { - CPRINTF("[%T Charge voltage %dmV]\n", batt->desired_voltage); - if (charger_set_voltage(batt->desired_voltage)) + /* + * Adjust desired voltage to one the charger can actually supply + * or else we'll keep asking for a voltage the charger can't actually + * supply. + */ + want_voltage = charger_closest_voltage(batt->desired_voltage); + + if (want_voltage != curr->charging_voltage) { + CPRINTF("[%T Charge voltage %dmV]\n", want_voltage); + if (charger_set_voltage(want_voltage)) return PWR_STATE_ERROR; update_charger_time(ctx, now); } diff --git a/common/charger_common.c b/common/charger_common.c index 0e57c8c02c..bb301c48a5 100644 --- a/common/charger_common.c +++ b/common/charger_common.c @@ -17,6 +17,14 @@ #define CPUTS(outstr) cputs(CC_CHARGER, outstr) #define CPRINTF(format, args...) cprintf(CC_CHARGER, format, ## args) +int charger_closest_voltage(int voltage) +{ + const struct charger_info *info; + + info = charger_get_info(); + return voltage - (voltage % info->voltage_step); +} + static int print_info(void) { int rv; diff --git a/include/charger.h b/include/charger.h index 661433d21a..9b75bb95b2 100644 --- a/include/charger.h +++ b/include/charger.h @@ -53,6 +53,15 @@ int charger_set_mode(int mode); */ int charger_closest_current(int current); +/** + * Return the closest match the charger can supply to the requested voltage. + * + * @param voltage Requested voltage in mV. + * + * @return Voltage the charger will actually supply if <voltage> is requested. + */ +int charger_closest_voltage(int voltage); + /* Get/set charge current limit in mA */ int charger_get_current(int *current); int charger_set_current(int current); |