summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Lin <tim2.lin@ite.corp-partner.google.com>2021-11-08 17:17:46 +0800
committerCommit Bot <commit-bot@chromium.org>2021-12-09 22:15:17 +0000
commit169c7ea6227f162f3c5c7f7c609e9a6477377cc2 (patch)
treefbdc7b32b3eff613e4df6ea6fc5226deead9c747
parente39912bb447e34790d84de1298a0c43aaeb55dd2 (diff)
downloadchrome-ec-169c7ea6227f162f3c5c7f7c609e9a6477377cc2.tar.gz
zephyr: pm: it8xxx2: add power policy
This power policy defines that if deep sleep is allowed and an interval of five seconds, the system can enter the suspend state. The UART driver uses pm_constraint_set counter to indicate that UART Tx is in use. And use constraint_get in the power policy to get the counter determine if the system can enter the suspend state. BUG=b:192354255 BRANCH=none TEST=zmake testall And test on hayato. If CONFIG_PM, CONFIG_DEVICE, CONFIG_PM_POLICY_APP are enabled, the console messages normally before system enters suspend. Signed-off-by: Tim Lin <tim2.lin@ite.corp-partner.google.com> Change-Id: I25a9de8202c84fbb069fe66e95ce0415b2b87470 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3267759 Reviewed-by: Denis Brockus <dbrockus@chromium.org> Reviewed-by: Keith Short <keithshort@chromium.org> Commit-Queue: Keith Short <keithshort@chromium.org>
-rw-r--r--zephyr/shim/chip/it8xxx2/CMakeLists.txt1
-rw-r--r--zephyr/shim/chip/it8xxx2/power_policy.c76
2 files changed, 77 insertions, 0 deletions
diff --git a/zephyr/shim/chip/it8xxx2/CMakeLists.txt b/zephyr/shim/chip/it8xxx2/CMakeLists.txt
index 7a92a3cfb6..f72f715acf 100644
--- a/zephyr/shim/chip/it8xxx2/CMakeLists.txt
+++ b/zephyr/shim/chip/it8xxx2/CMakeLists.txt
@@ -8,3 +8,4 @@ zephyr_library_sources(clock.c)
zephyr_library_sources_ifdef(CONFIG_CROS_EC system.c)
zephyr_library_sources_ifdef(CONFIG_CROS_EC pinmux.c)
zephyr_library_sources_ifdef(CONFIG_CROS_KB_RAW_ITE keyboard_raw.c)
+zephyr_library_sources_ifdef(CONFIG_PM_POLICY_APP power_policy.c)
diff --git a/zephyr/shim/chip/it8xxx2/power_policy.c b/zephyr/shim/chip/it8xxx2/power_policy.c
new file mode 100644
index 0000000000..92613710fe
--- /dev/null
+++ b/zephyr/shim/chip/it8xxx2/power_policy.c
@@ -0,0 +1,76 @@
+/* Copyright 2021 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.
+ */
+
+#include <pm/pm.h>
+#include <pm/policy.h>
+#include <soc.h>
+#include <zephyr.h>
+
+#include "system.h"
+#include "timer.h"
+
+static const struct pm_state_info pm_states[] =
+ PM_STATE_INFO_LIST_FROM_DT_CPU(DT_NODELABEL(cpu0));
+
+#define CONSOLE_IN_USE_ON_BOOT_TIME (5 * SECOND)
+#define CONSOLE_IN_USE_TIMEOUT_SEC (5 * SECOND)
+
+static timestamp_t console_expire_time;
+static timestamp_t sleep_mode_t0;
+
+void uart1_wui_isr_late(void)
+{
+ /* Set console in use expire time. */
+ console_expire_time = get_time();
+ console_expire_time.val += CONSOLE_IN_USE_TIMEOUT_SEC;
+}
+
+static int clock_allow_low_power_idle(void)
+{
+ sleep_mode_t0 = get_time();
+
+ /* If we are waked up by console, then keep awake at least 5s. */
+ if (sleep_mode_t0.val < console_expire_time.val)
+ return 0;
+
+ return 1;
+}
+
+/* CROS PM policy handler */
+struct pm_state_info pm_policy_next_state(uint8_t cpu, int32_t ticks)
+{
+ ARG_UNUSED(cpu);
+
+ /* Deep sleep is allowed and an interval of five seconds. */
+ if (DEEP_SLEEP_ALLOWED && clock_allow_low_power_idle()) {
+ /*
+ * If there are multiple power states, iterating backward
+ * is needed to take priority into account.
+ */
+ for (int i = 0; i < ARRAY_SIZE(pm_states); i++) {
+ /*
+ * To check if given power state is enabled and
+ * could be used.
+ */
+ if (!pm_constraint_get(pm_states[i].state)) {
+ continue;
+ }
+
+ return pm_states[i];
+ }
+ }
+
+ return (struct pm_state_info){PM_STATE_ACTIVE, 0, 0};
+}
+
+static int power_policy_init(const struct device *arg)
+{
+ ARG_UNUSED(arg);
+
+ console_expire_time.val = get_time().val + CONSOLE_IN_USE_ON_BOOT_TIME;
+
+ return 0;
+}
+SYS_INIT(power_policy_init, PRE_KERNEL_1, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT);