battery: Don't repeatedly configure charger

Make enable/disable a no-op if the charger is already in that state to
allow calling outside of ACIN_N state changes.
This commit is contained in:
Tim Crawford 2020-08-12 16:46:58 -06:00 committed by Jeremy Soller
parent 83c219589a
commit 4f0aa600d7

View File

@ -1,5 +1,7 @@
#include <board/battery.h>
#include <board/smbus.h>
#include <common/debug.h>
#include <stdbool.h>
#define BATTERY_ADDRESS 0x0B
#define CHARGER_ADDRESS 0x09
@ -14,9 +16,14 @@
// IDCHG Amplifier Gain
#define SBC_IDCHC_GAIN ((uint16_t)(1 << 3))
// XXX: Assumption: ac_last is initialized high.
static bool charger_enabled = false;
int battery_charger_disable(void) {
int res = 0;
if (!charger_enabled) return 0;
// Set charge option 0 with 175s watchdog
res = smbus_write(
CHARGER_ADDRESS,
@ -39,12 +46,16 @@ int battery_charger_disable(void) {
res = smbus_write(CHARGER_ADDRESS, 0x3F, 0);
if (res < 0) return res;
DEBUG("Charged disabled\n");
charger_enabled = false;
return 0;
}
int battery_charger_enable(void) {
int res = 0;
if (charger_enabled) return 0;
res = battery_charger_disable();
if (res < 0) return res;
@ -69,6 +80,8 @@ int battery_charger_enable(void) {
SBC_IDCHC_GAIN
);
DEBUG("Charged enabled\n");
charger_enabled = true;
return 0;
}