1 /*
2  * Copyright 2022, NXP
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 #include <zephyr/kernel.h>
7 #include <zephyr/device.h>
8 #include <zephyr/pm/pm.h>
9 #include "fsl_power.h"
10 
11 #include <zephyr/logging/log.h>
12 LOG_MODULE_DECLARE(soc, CONFIG_SOC_LOG_LEVEL);
13 
14 /*!< Power down all unnecessary blocks */
15 #define NODE_ID DT_INST(0, nxp_pdcfg_power)
16 #define EXCLUDE_FROM_DEEPSLEEP ((const uint32_t[]) \
17 					DT_PROP_OR(NODE_ID, deep_sleep_config, {}))
18 
19 /* System clock frequency. */
20 extern uint32_t SystemCoreClock;
21 
22 /* Invoke Low Power/System Off specific Tasks */
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 	/* FIXME: When this function is entered the Kernel has disabled
28 	 * interrupts using BASEPRI register. This is incorrect as it prevents
29 	 * waking up from any interrupt which priority is not 0. Work around the
30 	 * issue and disable interrupts using PRIMASK register as recommended
31 	 * by ARM.
32 	 */
33 
34 	/* Set PRIMASK */
35 	__disable_irq();
36 
37 	/* Set BASEPRI to 0 */
38 	irq_unlock(0);
39 
40 	switch (state) {
41 	case PM_STATE_RUNTIME_IDLE:
42 		POWER_EnterSleep();
43 		break;
44 	case PM_STATE_SUSPEND_TO_IDLE:
45 		POWER_EnterDeepSleep(EXCLUDE_FROM_DEEPSLEEP);
46 		break;
47 	default:
48 		LOG_DBG("Unsupported power state %u", state);
49 		break;
50 	}
51 }
52 
53 /* Handle SOC specific activity after Low Power Mode Exit */
pm_state_exit_post_ops(enum pm_state state,uint8_t substate_id)54 void pm_state_exit_post_ops(enum pm_state state, uint8_t substate_id)
55 {
56 	ARG_UNUSED(state);
57 	ARG_UNUSED(substate_id);
58 
59 	/* Clear PRIMASK */
60 	__enable_irq();
61 }
62 
63 /* Initialize power system */
rt5xx_power_init(void)64 void rt5xx_power_init(void)
65 {
66 	/* This function is called to set vddcore low voltage detection
67 	 * falling trip voltage, this is not impacting the voltage in anyway.
68 	 */
69 	POWER_SetLdoVoltageForFreq(SystemCoreClock, 0);
70 
71 #if CONFIG_REGULATOR
72 	/* Indicate to power library that PMIC is used. */
73 	POWER_UpdatePmicRecoveryTime(1);
74 #endif
75 
76 }
77