From b4f69d8a0cf5c4bc55880befe7cd995b1ddd2926 Mon Sep 17 00:00:00 2001 From: Edward Hill Date: Tue, 12 Jun 2018 14:55:14 -0600 Subject: [PATCH] battery: Move presence checks out of common Undo some of CL:1072637 so that battery_is_present() and battery_hw_present() move back to baseboard. battery_fuel_gauge.c now only includes code which is directly involved with the fuel gauge. BUG=b:109894491,b:80299100 BRANCH=none TEST=make -j buildall Change-Id: I8fc5be3856564601019d94514dcfc8ffb3071c2e Signed-off-by: Edward Hill Reviewed-on: https://chromium-review.googlesource.com/1097954 Commit-Ready: Devin Lu Reviewed-by: Jett Rink --- baseboard/grunt/battery.c | 69 +++++++++++++++++++++++++++++++++++++ baseboard/grunt/build.mk | 1 + baseboard/octopus/battery.c | 69 +++++++++++++++++++++++++++++++++++++ baseboard/octopus/build.mk | 1 + common/battery_fuel_gauge.c | 62 --------------------------------- 5 files changed, 140 insertions(+), 62 deletions(-) create mode 100644 baseboard/grunt/battery.c create mode 100644 baseboard/octopus/battery.c diff --git a/baseboard/grunt/battery.c b/baseboard/grunt/battery.c new file mode 100644 index 0000000000..835132366b --- /dev/null +++ b/baseboard/grunt/battery.c @@ -0,0 +1,69 @@ +/* Copyright 2018 The Chromium OS Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + * + * Battery pack vendor provided charging profile + */ + +#include "battery.h" +#include "battery_smart.h" +#include "gpio.h" + +static enum battery_present batt_pres_prev = BP_NOT_SURE; + +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_L) ? BP_NO : BP_YES; +} + +static int battery_init(void) +{ + int batt_status; + + return battery_status(&batt_status) ? 0 : + !!(batt_status & STATUS_INITIALIZED); +} + +/* + * Physical detection of battery. + */ +static enum battery_present battery_check_present_status(void) +{ + enum battery_present batt_pres; + + /* Get the physical hardware status */ + batt_pres = battery_hw_present(); + + /* + * If the battery is not physically connected, then no need to perform + * any more checks. + */ + if (batt_pres != BP_YES) + return batt_pres; + + /* + * If the battery is present now and was present last time we checked, + * return early. + */ + if (batt_pres == batt_pres_prev) + return batt_pres; + + /* + * Ensure that battery is: + * 1. Not in cutoff + * 2. Initialized + */ + if (battery_is_cut_off() != BATTERY_CUTOFF_STATE_NORMAL || + battery_init() == 0) { + batt_pres = BP_NO; + } + + return batt_pres; +} + +enum battery_present battery_is_present(void) +{ + batt_pres_prev = battery_check_present_status(); + return batt_pres_prev; +} diff --git a/baseboard/grunt/build.mk b/baseboard/grunt/build.mk index cb9d607c36..5a5942a0c6 100644 --- a/baseboard/grunt/build.mk +++ b/baseboard/grunt/build.mk @@ -7,4 +7,5 @@ # baseboard-y=baseboard.o +baseboard-$(CONFIG_BATTERY_SMART)+=battery.o baseboard-$(CONFIG_USB_POWER_DELIVERY)+=usb_pd_policy.o diff --git a/baseboard/octopus/battery.c b/baseboard/octopus/battery.c new file mode 100644 index 0000000000..835132366b --- /dev/null +++ b/baseboard/octopus/battery.c @@ -0,0 +1,69 @@ +/* Copyright 2018 The Chromium OS Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + * + * Battery pack vendor provided charging profile + */ + +#include "battery.h" +#include "battery_smart.h" +#include "gpio.h" + +static enum battery_present batt_pres_prev = BP_NOT_SURE; + +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_L) ? BP_NO : BP_YES; +} + +static int battery_init(void) +{ + int batt_status; + + return battery_status(&batt_status) ? 0 : + !!(batt_status & STATUS_INITIALIZED); +} + +/* + * Physical detection of battery. + */ +static enum battery_present battery_check_present_status(void) +{ + enum battery_present batt_pres; + + /* Get the physical hardware status */ + batt_pres = battery_hw_present(); + + /* + * If the battery is not physically connected, then no need to perform + * any more checks. + */ + if (batt_pres != BP_YES) + return batt_pres; + + /* + * If the battery is present now and was present last time we checked, + * return early. + */ + if (batt_pres == batt_pres_prev) + return batt_pres; + + /* + * Ensure that battery is: + * 1. Not in cutoff + * 2. Initialized + */ + if (battery_is_cut_off() != BATTERY_CUTOFF_STATE_NORMAL || + battery_init() == 0) { + batt_pres = BP_NO; + } + + return batt_pres; +} + +enum battery_present battery_is_present(void) +{ + batt_pres_prev = battery_check_present_status(); + return batt_pres_prev; +} diff --git a/baseboard/octopus/build.mk b/baseboard/octopus/build.mk index 2bf9b7b83d..3189d7cb07 100644 --- a/baseboard/octopus/build.mk +++ b/baseboard/octopus/build.mk @@ -7,6 +7,7 @@ # baseboard-y=baseboard.o +baseboard-$(CONFIG_BATTERY_SMART)+=battery.o baseboard-$(CONFIG_USB_POWER_DELIVERY)+=usb_pd_policy.o baseboard-$(VARIANT_OCTOPUS_EC_NPCX796FB)+=variant_ec_npcx796fb.o baseboard-$(VARIANT_OCTOPUS_EC_ITE8320)+=variant_ec_ite8320.o diff --git a/common/battery_fuel_gauge.c b/common/battery_fuel_gauge.c index c14b9de4cb..204b562401 100644 --- a/common/battery_fuel_gauge.c +++ b/common/battery_fuel_gauge.c @@ -7,18 +7,13 @@ #include "battery_fuel_gauge.h" #include "battery_smart.h" -#include "charge_state.h" -#include "common.h" #include "console.h" -#include "gpio.h" #include "hooks.h" #include "util.h" #define CPRINTS(format, args...) cprints(CC_CHARGER, format, ## args) -static enum battery_present batt_pres_prev = BP_NOT_SURE; - /* Get type of the battery connected on the board */ static int get_battery_type(void) { @@ -118,20 +113,6 @@ int board_cut_off_battery(void) return rv ? EC_RES_ERROR : EC_RES_SUCCESS; } -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_L) ? BP_NO : BP_YES; -} - -static int battery_init(void) -{ - int batt_status; - - return battery_status(&batt_status) ? 0 : - !!(batt_status & STATUS_INITIALIZED); -} - /* * This function checks the charge/discharge FET status bits. Each battery type * supported provides the register address, mask, and disconnect value for these @@ -181,46 +162,3 @@ enum battery_disconnect_state battery_get_disconnect_state(void) return BATTERY_NOT_DISCONNECTED; } - -/* - * Physical detection of battery. - */ -static enum battery_present battery_check_present_status(void) -{ - enum battery_present batt_pres; - - /* Get the physical hardware status */ - batt_pres = battery_hw_present(); - - /* - * If the battery is not physically connected, then no need to perform - * any more checks. - */ - if (batt_pres != BP_YES) - return batt_pres; - - /* - * If the battery is present now and was present last time we checked, - * return early. - */ - if (batt_pres == batt_pres_prev) - return batt_pres; - - /* - * Ensure that battery is: - * 1. Not in cutoff - * 2. Initialized - */ - if (battery_is_cut_off() != BATTERY_CUTOFF_STATE_NORMAL || - battery_init() == 0) { - batt_pres = BP_NO; - } - - return batt_pres; -} - -enum battery_present battery_is_present(void) -{ - batt_pres_prev = battery_check_present_status(); - return batt_pres_prev; -}