diff options
-rw-r--r-- | board/samus/board.c | 21 | ||||
-rw-r--r-- | board/samus/board.h | 2 | ||||
-rw-r--r-- | include/charge_state_v2.h | 6 | ||||
-rw-r--r-- | include/chipset.h | 17 | ||||
-rw-r--r-- | include/config.h | 10 | ||||
-rw-r--r-- | include/timer.h | 1 | ||||
-rw-r--r-- | power/common.c | 33 |
7 files changed, 64 insertions, 26 deletions
diff --git a/board/samus/board.c b/board/samus/board.c index 9f5bcf743a..6c948459a5 100644 --- a/board/samus/board.c +++ b/board/samus/board.c @@ -461,3 +461,24 @@ enum ec_error_list keyboard_scancode_callback(uint16_t *make_code, } return EC_SUCCESS; } + +/* + * Use to define going in to hibernate early if low on battery. + * HIBERNATE_BATT_PCT specifies the low battery threshold + * for going into hibernate early, and HIBERNATE_BATT_SEC defines + * the minimum amount of time to stay in G3 before checking for low + * battery hibernate. + */ +#define HIBERNATE_BATT_PCT 10 +#define HIBERNATE_BATT_SEC (3600 * 24) + +enum critical_shutdown board_system_is_idle(uint64_t last_shutdown_time, + uint64_t *target, uint64_t now) +{ + if (charge_get_percent() <= HIBERNATE_BATT_PCT) { + uint64_t t = last_shutdown_time + HIBERNATE_BATT_SEC * SEC_UL; + *target = MIN(*target, t); + } + return now > *target ? + CRITICAL_SHUTDOWN_HIBERNATE : CRITICAL_SHUTDOWN_IGNORE; +} diff --git a/board/samus/board.h b/board/samus/board.h index ac76ff0d4c..2bfb1574c8 100644 --- a/board/samus/board.h +++ b/board/samus/board.h @@ -64,8 +64,6 @@ #define CONFIG_GESTURE_SAMPLING_INTERVAL_MS 5 #undef CONFIG_HIBERNATE_DELAY_SEC #define CONFIG_HIBERNATE_DELAY_SEC (3600 * 24 * 7) -#define CONFIG_HIBERNATE_BATT_PCT 10 -#define CONFIG_HIBERNATE_BATT_SEC (3600 * 24) #define CONFIG_HOSTCMD_PD #define CONFIG_HOSTCMD_PD_CHG_CTRL #define CONFIG_HOSTCMD_PD_PANIC diff --git a/include/charge_state_v2.h b/include/charge_state_v2.h index 800ba4e1cd..24d44da7b1 100644 --- a/include/charge_state_v2.h +++ b/include/charge_state_v2.h @@ -6,6 +6,7 @@ #include "battery.h" #include "battery_smart.h" #include "charger.h" +#include "chipset.h" #include "ec_ec_comm_master.h" #include "timer.h" @@ -118,11 +119,6 @@ void board_base_reset(void); * @param curr Pointer to struct charge_state_data * @return Action to take. */ -enum critical_shutdown { - CRITICAL_SHUTDOWN_IGNORE, - CRITICAL_SHUTDOWN_HIBERNATE, - CRITICAL_SHUTDOWN_CUTOFF, -}; enum critical_shutdown board_critical_shutdown_check( struct charge_state_data *curr); diff --git a/include/chipset.h b/include/chipset.h index 77695c8303..2cf21277cb 100644 --- a/include/chipset.h +++ b/include/chipset.h @@ -104,6 +104,12 @@ enum chipset_shutdown_reason { CHIPSET_SHUTDOWN_COUNT, }; +enum critical_shutdown { + CRITICAL_SHUTDOWN_IGNORE, + CRITICAL_SHUTDOWN_HIBERNATE, + CRITICAL_SHUTDOWN_CUTOFF, +}; + #ifdef HAS_TASK_CHIPSET /** @@ -232,6 +238,17 @@ void chipset_warm_reset_interrupt(enum gpio_signal signal); */ void chipset_watchdog_interrupt(enum gpio_signal signal); +/** + * Callback which allows board to take custom action on G3 timer expiration + * + * @param last_shutdown_time Last shutdown time + * @param target Expiration time. Can be modified by board. + * @param now Current time + * @return Action to take + */ +enum critical_shutdown board_system_is_idle(uint64_t last_shutdown_time, + uint64_t *target, uint64_t now); + #ifdef CONFIG_CMD_AP_RESET_LOG /** diff --git a/include/config.h b/include/config.h index a83e7473fb..ea68efd2d0 100644 --- a/include/config.h +++ b/include/config.h @@ -1982,16 +1982,6 @@ /* Default delay after shutting down before hibernating */ #define CONFIG_HIBERNATE_DELAY_SEC 3600 -/* - * Use to define going in to hibernate early if low on battery. - * CONFIG_HIBERNATE_BATT_PCT specifies the low battery threshold - * for going into hibernate early, and CONFIG_HIBERNATE_BATT_SEC defines - * the minimum amount of time to stay in G3 before checking for low - * battery hibernate. - */ -#undef CONFIG_HIBERNATE_BATT_PCT -#undef CONFIG_HIBERNATE_BATT_SEC - /* For ECs with multiple wakeup pins, define enabled wakeup pins */ #undef CONFIG_HIBERNATE_WAKEUP_PINS diff --git a/include/timer.h b/include/timer.h index fb7800ce2e..02a50070c1 100644 --- a/include/timer.h +++ b/include/timer.h @@ -14,6 +14,7 @@ /* Time units in microseconds */ #define MSEC 1000 #define SECOND 1000000 +#define SEC_UL 1000000ul #define MINUTE 60000000 #define HOUR 3600000000ull /* Too big to fit in a signed int */ diff --git a/power/common.c b/power/common.c index c548425296..cccb34911a 100644 --- a/power/common.c +++ b/power/common.c @@ -5,6 +5,7 @@ /* Common functionality across all chipsets */ +#include "battery.h" #include "charge_state.h" #include "chipset.h" #include "common.h" @@ -281,6 +282,14 @@ static void power_set_active_wake_mask(void) static void power_set_active_wake_mask(void) { } #endif +__attribute__((weak)) +enum critical_shutdown board_system_is_idle(uint64_t last_shutdown_time, + uint64_t *target, uint64_t now) +{ + return now > *target ? + CRITICAL_SHUTDOWN_HIBERNATE : CRITICAL_SHUTDOWN_IGNORE; +} + /** * Common handler for steady states * @@ -300,22 +309,28 @@ static enum power_state power_common_state(enum power_state state) #ifdef CONFIG_HIBERNATE { uint64_t target, now, wait; - uint32_t delay = hibernate_delay; if (extpower_is_present()) { task_wait_event(-1); break; } now = get_time().val; -#ifdef CONFIG_HIBERNATE_BATT_PCT - if (charge_get_percent() <= CONFIG_HIBERNATE_BATT_PCT - && CONFIG_HIBERNATE_BATT_SEC < delay) - delay = CONFIG_HIBERNATE_BATT_SEC; -#endif - target = last_shutdown_time + delay * SECOND; - if (now > target) { - CPRINTS("hibernating"); + target = last_shutdown_time + hibernate_delay * SECOND; + switch (board_system_is_idle(last_shutdown_time, + &target, now)) { + case CRITICAL_SHUTDOWN_HIBERNATE: + CPRINTS("Hibernate due to G3 idle"); system_hibernate(0, 0); + break; +#ifdef CONFIG_BATTERY_CUT_OFF + case CRITICAL_SHUTDOWN_CUTOFF: + CPRINTS("Cutoff due to G3 idle"); + board_cut_off_battery(); + break; +#endif + case CRITICAL_SHUTDOWN_IGNORE: + default: + break; } wait = MIN(target - now, TASK_MAX_WAIT_US); |