1 /*
2  * Copyright (c) 2022 ITE Corporation. All Rights Reserved.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/kernel.h>
8 #include <zephyr/pm/policy.h>
9 #include <soc.h>
10 
pm_policy_next_state(uint8_t cpu,int32_t ticks)11 __weak const struct pm_state_info *pm_policy_next_state(uint8_t cpu, int32_t ticks)
12 {
13 	const struct pm_state_info *cpu_states;
14 	uint8_t num_cpu_states;
15 
16 	num_cpu_states = pm_state_cpu_get_all(cpu, &cpu_states);
17 
18 	for (int16_t i = (int16_t)num_cpu_states - 1; i >= 0; i--) {
19 		const struct pm_state_info *state = &cpu_states[i];
20 		uint32_t min_residency;
21 
22 		/* check if there is a lock on state + substate */
23 		if (pm_policy_state_lock_is_active(
24 			state->state, state->substate_id)) {
25 			continue;
26 		}
27 
28 		min_residency = k_us_to_ticks_ceil32(state->min_residency_us);
29 		/*
30 		 * The tick interval for the system to enter sleep mode needs
31 		 * to be longer than or equal to the minimum residency.
32 		 */
33 		if (ticks >= min_residency) {
34 			return state;
35 		}
36 	}
37 
38 	return NULL;
39 }
40