1 /*
2  * Copyright (c) 2021 Cypress Semiconductor Corporation (an Infineon company) or
3  * an affiliate of Cypress Semiconductor Corporation
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 #include <zephyr/kernel.h>
9 #include <zephyr/device.h>
10 #include <zephyr/pm/pm.h>
11 #include <zephyr/logging/log.h>
12 
13 #include <cyhal_syspm.h>
14 #include <cyhal_lptimer.h>
15 
16 LOG_MODULE_REGISTER(soc_power, CONFIG_SOC_LOG_LEVEL);
17 
18 /*
19  * Called from pm_system_suspend(int32_t ticks) in subsys/power.c
20  * For deep sleep pm_system_suspend has executed all the driver
21  * power management call backs.
22  */
pm_state_set(enum pm_state state,uint8_t substate_id)23 void pm_state_set(enum pm_state state, uint8_t substate_id)
24 {
25 	ARG_UNUSED(substate_id);
26 
27 	/* Set BASEPRI to 0 */
28 	irq_unlock(0);
29 
30 	switch (state) {
31 	case PM_STATE_SUSPEND_TO_IDLE:
32 		LOG_DBG("Entering PM state suspend to idle");
33 		cyhal_syspm_sleep();
34 		break;
35 	case PM_STATE_SUSPEND_TO_RAM:
36 		LOG_DBG("Entering PM state suspend to RAM");
37 		cyhal_syspm_deepsleep();
38 
39 		/*
40 		 * The HAL function doesn't clear this bit.  It is a problem
41 		 * if the Zephyr idle function executes the wfi instruction
42 		 * with this bit set.  We will always clear it here to avoid
43 		 * that situation.
44 		 */
45 		SCB_SCR &= (uint32_t)~SCB_SCR_SLEEPDEEP_Msk;
46 		break;
47 	default:
48 		LOG_DBG("Unsupported power state %u", state);
49 		break;
50 	}
51 }
52 
pm_state_exit_post_ops(enum pm_state state,uint8_t substate_id)53 void pm_state_exit_post_ops(enum pm_state state, uint8_t substate_id)
54 {
55 	ARG_UNUSED(substate_id);
56 
57 	switch (state) {
58 	case PM_STATE_SUSPEND_TO_IDLE:
59 	case PM_STATE_SUSPEND_TO_RAM:
60 		break;
61 
62 	default:
63 		break;
64 	}
65 }
66 
ifx_pm_init(void)67 int ifx_pm_init(void)
68 {
69 	/* System Domain Idle Power Mode Configuration */
70 	Cy_SysPm_SetDeepSleepMode(CY_SYSPM_MODE_DEEPSLEEP);
71 
72 	return cyhal_syspm_init();
73 }
74