summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEdward Hill <ecgh@chromium.org>2018-02-01 15:18:00 -0700
committerchrome-bot <chrome-bot@chromium.org>2018-03-12 15:24:50 -0700
commit7aac7a43cc146049bea6ce871be53948650f81fa (patch)
tree82c12879cfacecf822eabc50a22839a188f2513e
parent99bcab486dff32c646abed906cf37dfe68a6ead7 (diff)
downloadchrome-ec-7aac7a43cc146049bea6ce871be53948650f81fa.tar.gz
grunt: Improve battery presence checks
Use CONFIG_BATTERY_HW_PRESENT_CUSTOM and CONFIG_BATTERY_PRESENT_CUSTOM to make Grunt closer to Coral and Eve. This gets auto-power-on-with-no-battery closer to working, because charge_prevent_power_on uses battery_hw_present for its factory override check: "Factory override: Always allow power on if WP is disabled, except when auto-power-on at EC startup and the battery is physically present." BUG=b:72645441 BRANCH=none TEST=cold reset Grunt with battery connected and disconnected Change-Id: I2b158bedcad7dffc340992dc020145a4c6e60802 Signed-off-by: Edward Hill <ecgh@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/900048 Reviewed-by: Aaron Durbin <adurbin@chromium.org> Reviewed-by: Duncan Laurie <dlaurie@google.com>
-rw-r--r--board/grunt/battery.c86
-rw-r--r--board/grunt/board.c10
-rw-r--r--board/grunt/board.h3
3 files changed, 97 insertions, 2 deletions
diff --git a/board/grunt/battery.c b/board/grunt/battery.c
index 49e943165d..d3a10e75f5 100644
--- a/board/grunt/battery.c
+++ b/board/grunt/battery.c
@@ -6,12 +6,22 @@
*/
#include "battery.h"
#include "battery_smart.h"
+#include "console.h"
#include "extpower.h"
#include "gpio.h"
+#include "hooks.h"
+#include "timer.h"
+
+#define CPRINTS(format, args...) cprints(CC_CHARGER, format, ## args)
/* Shutdown mode parameter to write to manufacturer access register */
#define SB_SHUTDOWN_DATA 0x0010
+static enum battery_present batt_pres_prev = BP_NOT_SURE;
+
+/* Battery may delay reporting battery present */
+static int battery_report_present = 1;
+
static const struct battery_info info = {
.voltage_max = 13200, /* mV */
.voltage_normal = 11550,
@@ -24,10 +34,12 @@ static const struct battery_info info = {
.discharging_min_c = -20,
.discharging_max_c = 75,
};
+
const struct battery_info *battery_get_info(void)
{
return &info;
}
+
int board_cut_off_battery(void)
{
int rv;
@@ -37,3 +49,77 @@ int board_cut_off_battery(void)
return rv;
return sb_write(SB_MANUFACTURER_ACCESS, SB_SHUTDOWN_DATA);
}
+
+enum battery_present battery_hw_present(void)
+{
+ /* The GPIO is low when the battery is physically present */
+ return gpio_get_level(GPIO_EC_BATT_PRES_ODL) ? BP_NO : BP_YES;
+}
+
+static int battery_init(void)
+{
+ int batt_status;
+
+ return battery_status(&batt_status) ? 0 :
+ !!(batt_status & STATUS_INITIALIZED);
+}
+
+/* Allow booting now that the battery has woke up */
+static void battery_now_present(void)
+{
+ CPRINTS("battery will now report present");
+ battery_report_present = 1;
+}
+DECLARE_DEFERRED(battery_now_present);
+
+static int battery_check_disconnect(void)
+{
+ /* TODO(ecgh): Read the status of charge/discharge FETs */
+ return BATTERY_NOT_DISCONNECTED;
+}
+
+enum battery_present battery_is_present(void)
+{
+ enum battery_present batt_pres;
+ static int battery_report_present_timer_started;
+
+ /* Get the physical hardware status */
+ batt_pres = battery_hw_present();
+
+ /*
+ * Make sure battery status is implemented, I2C transactions are
+ * success & the battery status is Initialized to find out if it
+ * is a working battery and it is not in the cut-off mode.
+ */
+ if (batt_pres == BP_YES && batt_pres_prev != batt_pres &&
+ (battery_is_cut_off() != BATTERY_CUTOFF_STATE_NORMAL ||
+ battery_check_disconnect() != BATTERY_NOT_DISCONNECTED ||
+ battery_init() == 0)) {
+ battery_report_present = 0;
+ /*
+ * When this path is taken, the _timer_started flag must be
+ * reset so the 'else if' path will be entered and the
+ * battery_report_present flag can be set by the deferred
+ * call. This handles the case of the battery being disconected
+ * and reconnected while running or if battery_init() returns an
+ * error due to a failed sb_read.
+ */
+ battery_report_present_timer_started = 0;
+ } else if (batt_pres == BP_YES && batt_pres_prev == BP_NO &&
+ !battery_report_present_timer_started) {
+ /*
+ * Wait 1/2 second before reporting present if it was
+ * previously reported as not present
+ */
+ battery_report_present_timer_started = 1;
+ battery_report_present = 0;
+ hook_call_deferred(&battery_now_present_data, 500 * MSEC);
+ }
+
+ if (!battery_report_present)
+ batt_pres = BP_NO;
+
+ batt_pres_prev = batt_pres;
+
+ return batt_pres;
+}
diff --git a/board/grunt/board.c b/board/grunt/board.c
index 597ff0fa10..1257a74a88 100644
--- a/board/grunt/board.c
+++ b/board/grunt/board.c
@@ -324,9 +324,17 @@ void board_reset_pd_mcu(void)
void board_tcpc_init(void)
{
+ int count = 0;
int port;
- /* TODO(ecgh): need to wait for disconnected battery? */
+ /* Wait for disconnected battery to wake up */
+ while (battery_hw_present() == BP_YES &&
+ battery_is_present() == BP_NO) {
+ usleep(100 * MSEC);
+ /* Give up waiting after 1 second */
+ if (++count > 10)
+ break;
+ }
/* Only reset TCPC if not sysjump */
if (!system_jumped_to_this_image())
diff --git a/board/grunt/board.h b/board/grunt/board.h
index f656c73b9c..824e7d7714 100644
--- a/board/grunt/board.h
+++ b/board/grunt/board.h
@@ -45,7 +45,8 @@
#define CONFIG_VOLUME_BUTTONS
#define CONFIG_BATTERY_CUT_OFF
-#define CONFIG_BATTERY_PRESENT_GPIO GPIO_EC_BATT_PRES_ODL
+#define CONFIG_BATTERY_HW_PRESENT_CUSTOM
+#define CONFIG_BATTERY_PRESENT_CUSTOM
#define CONFIG_BATTERY_SMART
#define CONFIG_BC12_DETECT_BQ24392