summaryrefslogtreecommitdiff
path: root/zephyr/shim/chip/npcx/power_policy.c
blob: 29ebcbd5425da75e8201eec51488c1bae8354f28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/* Copyright 2021 The ChromiumOS Authors
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "console.h"
#include "cros_version.h"
#include "system.h"

#include <zephyr/kernel.h>
#include <zephyr/pm/pm.h>
#include <zephyr/pm/policy.h>

#include <soc.h>

static const struct pm_state_info residency_info[] =
	PM_STATE_INFO_LIST_FROM_DT_CPU(DT_NODELABEL(cpu0));

/* CROS PM policy handler */
const struct pm_state_info *pm_policy_next_state(uint8_t cpu, int32_t ticks)
{
	ARG_UNUSED(cpu);

	if (DEEP_SLEEP_ALLOWED) {
		for (int i = ARRAY_SIZE(residency_info) - 1; i >= 0; i--) {
			if (pm_policy_state_lock_is_active(
				    residency_info[i].state,
				    PM_ALL_SUBSTATES)) {
				continue;
			}

			/* Find suitable power state by residency time */
			if (ticks == K_TICKS_FOREVER ||
			    ticks >= k_us_to_ticks_ceil32(
					     residency_info[i]
						     .min_residency_us)) {
				return &residency_info[i];
			}
		}
	}

	return NULL;
}