1 /*
2  * Copyright 2023, NXP
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 #include <zephyr/kernel.h>
7 #include <zephyr/pm/pm.h>
8 #include <zephyr/init.h>
9 
10 #include "fsl_power.h"
11 
12 #include <zephyr/logging/log.h>
13 LOG_MODULE_DECLARE(soc, CONFIG_SOC_LOG_LEVEL);
14 
15 /* Active mode */
16 #define POWER_MODE0		0
17 /* Idle mode */
18 #define POWER_MODE1		1
19 /* Standby mode */
20 #define POWER_MODE2		2
21 /* Sleep mode */
22 #define POWER_MODE3		3
23 /* Deep Sleep mode */
24 #define POWER_MODE4		4
25 
26 #define NODE_ID DT_INST(0, nxp_pdcfg_power)
27 
28 power_sleep_config_t slp_cfg;
29 
30 /* Invoke Low Power/System Off specific Tasks */
pm_state_set(enum pm_state state,uint8_t substate_id)31 __weak void pm_state_set(enum pm_state state, uint8_t substate_id)
32 {
33 	ARG_UNUSED(substate_id);
34 
35 	/* Set PRIMASK */
36 	__disable_irq();
37 	/* Set BASEPRI to 0 */
38 	irq_unlock(0);
39 
40 	switch (state) {
41 	case PM_STATE_RUNTIME_IDLE:
42 		POWER_SetSleepMode(POWER_MODE1);
43 		__WFI();
44 		break;
45 	case PM_STATE_SUSPEND_TO_IDLE:
46 		POWER_EnterPowerMode(POWER_MODE2, &slp_cfg);
47 		break;
48 	default:
49 		LOG_DBG("Unsupported power state %u", state);
50 		break;
51 	}
52 }
53 
54 /* Handle SOC specific activity after Low Power Mode Exit */
pm_state_exit_post_ops(enum pm_state state,uint8_t substate_id)55 __weak void pm_state_exit_post_ops(enum pm_state state, uint8_t substate_id)
56 {
57 	ARG_UNUSED(state);
58 	ARG_UNUSED(substate_id);
59 
60 	/* Clear PRIMASK */
61 	__enable_irq();
62 }
63 
nxp_rw6xx_power_init(void)64 static int nxp_rw6xx_power_init(void)
65 {
66 	uint32_t suspend_sleepconfig[5] = DT_PROP_OR(NODE_ID, deep_sleep_config, {});
67 
68 	slp_cfg.pm2MemPuCfg = suspend_sleepconfig[0];
69 	slp_cfg.pm2AnaPuCfg = suspend_sleepconfig[1];
70 	slp_cfg.clkGate = suspend_sleepconfig[2];
71 	slp_cfg.memPdCfg = suspend_sleepconfig[3];
72 	slp_cfg.pm3BuckCfg = suspend_sleepconfig[4];
73 
74 	return 0;
75 }
76 
77 SYS_INIT(nxp_rw6xx_power_init, PRE_KERNEL_1, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT);
78