summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--board/flapjack/battery.c52
1 files changed, 35 insertions, 17 deletions
diff --git a/board/flapjack/battery.c b/board/flapjack/battery.c
index 506ea593c3..582b2d2e98 100644
--- a/board/flapjack/battery.c
+++ b/board/flapjack/battery.c
@@ -16,12 +16,14 @@
#include "extpower.h"
#include "gpio.h"
#include "hooks.h"
+#include "i2c.h"
#include "usb_pd.h"
#include "util.h"
#include "board.h"
#include "adc.h"
#include "adc_chip.h"
#include "math_util.h"
+#include "p9221.h"
#define CPRINTS(format, args...) cprints(CC_USBCHARGE, format, ## args)
#define CPRINTF(format, args...) cprintf(CC_USBCHARGE, format, ## args)
@@ -346,10 +348,43 @@ int charger_profile_override(struct charge_state_data *curr)
/* battery temp in 0.1 deg C */
int temp = curr->batt.temperature - 2731;
enum temp_zone zone;
+ int usb_mv, wpc_mv;
+ static int previous_usb_mv;
+ int val;
if (curr->state != ST_CHARGE)
return 0;
+ /* Limit input (=VBUS) to 5V when soc > 85% and charge current < 1A. */
+ if (!(curr->batt.flags & BATT_FLAG_BAD_CURRENT) &&
+ charge_get_percent() > BAT_LEVEL_PD_LIMIT &&
+ curr->batt.current < 1000 &&
+ curr->batt.current > 0) {
+ usb_mv = 5500;
+ wpc_mv = 5500;
+ } else {
+ usb_mv = PD_MAX_VOLTAGE_MV;
+ wpc_mv = P9221_DC_IVL_EPP_MV;
+ }
+
+ if (usb_mv != previous_usb_mv)
+ CPRINTS("VBUS limited to %dmV", usb_mv);
+ previous_usb_mv = usb_mv;
+
+ /* Pull down USB VBUS */
+ if (pd_get_max_voltage() != usb_mv)
+ pd_set_external_voltage_limit(0, usb_mv);
+
+ /*
+ * Pull down WPC VBUS. Need to use raw i2c APIs because RO
+ * doesn't have p9221 driver. If WPC is off, this is a no-op.
+ */
+ if (i2c_read_offset16(I2C_PORT_WPC, P9221_R7_ADDR,
+ P9221R7_VOUT_SET_REG, &val, 1) == EC_SUCCESS
+ && val * 100 != wpc_mv)
+ i2c_write_offset16(I2C_PORT_WPC, P9221_R7_ADDR,
+ P9221R7_VOUT_SET_REG, wpc_mv / 100, 1);
+
if ((curr->batt.flags & BATT_FLAG_BAD_TEMPERATURE) ||
(temp < temp_zones[batt_type][TEMP_ZONE_0].temp_min)) {
zone = TEMP_OUT_OF_RANGE;
@@ -398,23 +433,6 @@ DECLARE_HOOK(HOOK_BATTERY_SOC_CHANGE,
board_charge_termination,
HOOK_PRIO_DEFAULT);
-static void pd_limit_5v(uint8_t en)
-{
- int wanted_pd_voltage;
-
- wanted_pd_voltage = en ? 5500 : PD_MAX_VOLTAGE_MV;
-
- if (pd_get_max_voltage() != wanted_pd_voltage)
- pd_set_external_voltage_limit(0, wanted_pd_voltage);
-}
-
-/* When battery level > BAT_LEVEL_PD_LIMIT, we limit PD voltage to 5V. */
-static void board_pd_voltage(void)
-{
- pd_limit_5v(charge_get_percent() > BAT_LEVEL_PD_LIMIT);
-}
-DECLARE_HOOK(HOOK_BATTERY_SOC_CHANGE, board_pd_voltage, HOOK_PRIO_DEFAULT);
-
/* Customs options controllable by host command. */
#define PARAM_FASTCHARGE (CS_PARAM_CUSTOM_PROFILE_MIN + 0)