1 /*
2  * Copyright (c) 2022 Espressif Systems (Shanghai) Co., Ltd.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/kernel.h>
8 #include <zephyr/pm/pm.h>
9 #include <esp_sleep.h>
10 #include <soc/rtc_io_channel.h>
11 #include <hal/rtc_io_hal.h>
12 
13 #include <zephyr/logging/log.h>
14 LOG_MODULE_DECLARE(soc, CONFIG_SOC_LOG_LEVEL);
15 
16 static uint32_t intenable;
17 
18 /* Invoke Low Power/System Off specific Tasks */
pm_state_set(enum pm_state state,uint8_t substate_id)19 void pm_state_set(enum pm_state state, uint8_t substate_id)
20 {
21 	ARG_UNUSED(substate_id);
22 
23 	switch (state) {
24 	case PM_STATE_STANDBY:
25 		intenable = XTENSA_RSR("INTENABLE");
26 		__asm__ volatile ("waiti 0");
27 		break;
28 	default:
29 		LOG_DBG("Unsupported power state %u", state);
30 		break;
31 	}
32 }
33 
34 /* Handle SOC specific activity after Low Power Mode Exit */
pm_state_exit_post_ops(enum pm_state state,uint8_t substate_id)35 void pm_state_exit_post_ops(enum pm_state state, uint8_t substate_id)
36 {
37 	ARG_UNUSED(substate_id);
38 
39 	switch (state) {
40 	case PM_STATE_STANDBY:
41 		z_xt_ints_on(intenable);
42 		esp_light_sleep_start();
43 		break;
44 	default:
45 		LOG_DBG("Unsupported power state %u", state);
46 		break;
47 	}
48 }
49