1 /*
2  * Copyright (c) 2024 STMicroelectronics.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 #include <zephyr/kernel.h>
7 #include <zephyr/pm/pm.h>
8 #include <soc.h>
9 #include <zephyr/init.h>
10 
11 #include <stm32h5xx_ll_cortex.h>
12 #include <stm32h5xx_ll_pwr.h>
13 #include <clock_control/clock_stm32_ll_common.h>
14 
15 #include <zephyr/logging/log.h>
16 LOG_MODULE_DECLARE(soc, CONFIG_SOC_LOG_LEVEL);
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 	if (state != PM_STATE_SUSPEND_TO_IDLE) {
22 		LOG_DBG("Unsupported power state %u", state);
23 		return;
24 	}
25 
26 	switch (substate_id) {
27 	case 1: /* this corresponds to the STOP mode: */
28 		/* enter STOP mode */
29 		LL_PWR_SetPowerMode(LL_PWR_STOP_MODE);
30 		LL_LPM_EnableDeepSleep();
31 		/* enter SLEEP mode : WFE or WFI */
32 		k_cpu_idle();
33 		break;
34 	default:
35 		LOG_DBG("Unsupported power state substate-id %u",
36 			substate_id);
37 		break;
38 	}
39 }
40 
41 /* Handle SOC specific activity after Low Power Mode Exit */
pm_state_exit_post_ops(enum pm_state state,uint8_t substate_id)42 void pm_state_exit_post_ops(enum pm_state state, uint8_t substate_id)
43 {
44 	if (state != PM_STATE_SUSPEND_TO_IDLE) {
45 		LOG_DBG("Unsupported power substate %u", state);
46 	} else {
47 		switch (substate_id) {
48 		case 1:	/* STOP */
49 			LL_LPM_DisableSleepOnExit();
50 			/* Clear SLEEPDEEP bit */
51 			LL_LPM_EnableSleep();
52 			break;
53 		default:
54 			LOG_DBG("Unsupported power substate-id %u",
55 				substate_id);
56 			break;
57 		}
58 		/* need to restore the clock */
59 		stm32_clock_control_init(NULL);
60 	}
61 
62 	/*
63 	 * System is now in active mode.
64 	 * Reenable interrupts which were disabled
65 	 * when OS started idling code.
66 	 */
67 	irq_unlock(0);
68 }
69 
70 /* Initialize STM32 Power */
stm32_power_init(void)71 void stm32_power_init(void)
72 {
73 }
74