| 
 | 1 | +/*  | 
 | 2 | + * Copyright (c) 2021 STMicroelectronics.  | 
 | 3 | + * Copyright (c) 2021 G-Technologies Sdn. Bhd.  | 
 | 4 | + *  | 
 | 5 | + * SPDX-License-Identifier: Apache-2.0  | 
 | 6 | + */  | 
 | 7 | +#include <zephyr.h>  | 
 | 8 | +#include <pm/pm.h>  | 
 | 9 | +#include <soc.h>  | 
 | 10 | +#include <init.h>  | 
 | 11 | + | 
 | 12 | +#include <stm32g0xx_ll_utils.h>  | 
 | 13 | +#include <stm32g0xx_ll_bus.h>  | 
 | 14 | +#include <stm32g0xx_ll_cortex.h>  | 
 | 15 | +#include <stm32g0xx_ll_pwr.h>  | 
 | 16 | +#include <stm32g0xx_ll_rcc.h>  | 
 | 17 | +#include <stm32g0xx_ll_system.h>  | 
 | 18 | +#include <clock_control/clock_stm32_ll_common.h>  | 
 | 19 | + | 
 | 20 | +#include <logging/log.h>  | 
 | 21 | +LOG_MODULE_DECLARE(soc, CONFIG_SOC_LOG_LEVEL);  | 
 | 22 | + | 
 | 23 | +/* Invoke Low Power/System Off specific Tasks */  | 
 | 24 | +void pm_power_state_set(struct pm_state_info info)  | 
 | 25 | +{  | 
 | 26 | +	if (info.state != PM_STATE_SUSPEND_TO_IDLE) {  | 
 | 27 | +		LOG_DBG("Unsupported power state %u", info.state);  | 
 | 28 | +		return;  | 
 | 29 | +	}  | 
 | 30 | + | 
 | 31 | +	switch (info.substate_id) {  | 
 | 32 | +	case 1: /* this corresponds to the STOP0 mode: */  | 
 | 33 | +		/* enter STOP0 mode */  | 
 | 34 | +		LL_PWR_SetPowerMode(LL_PWR_MODE_STOP0);  | 
 | 35 | +		LL_LPM_EnableDeepSleep();  | 
 | 36 | +		/* enter SLEEP mode : WFE or WFI */  | 
 | 37 | +		k_cpu_idle();  | 
 | 38 | +		break;  | 
 | 39 | +	case 2: /* this corresponds to the STOP1 mode: */  | 
 | 40 | +		/* enter STOP1 mode */  | 
 | 41 | +		LL_PWR_SetPowerMode(LL_PWR_MODE_STOP1);  | 
 | 42 | +		LL_LPM_EnableDeepSleep();  | 
 | 43 | +		/* enter SLEEP mode : WFE or WFI */  | 
 | 44 | +		k_cpu_idle();  | 
 | 45 | +		break;  | 
 | 46 | +	default:  | 
 | 47 | +		LOG_DBG("Unsupported power state substate-id %u",  | 
 | 48 | +			info.substate_id);  | 
 | 49 | +		break;  | 
 | 50 | +	}  | 
 | 51 | +}  | 
 | 52 | + | 
 | 53 | +/* Handle SOC specific activity after Low Power Mode Exit */  | 
 | 54 | +void pm_power_state_exit_post_ops(struct pm_state_info info)  | 
 | 55 | +{  | 
 | 56 | +	if (info.state != PM_STATE_SUSPEND_TO_IDLE) {  | 
 | 57 | +		LOG_DBG("Unsupported power substate-id %u", info.state);  | 
 | 58 | +	} else {  | 
 | 59 | +		switch (info.substate_id) {  | 
 | 60 | +		case 1:	/* STOP0 */  | 
 | 61 | +			__fallthrough;  | 
 | 62 | +		case 2:	/* STOP1 */  | 
 | 63 | +			LL_PWR_SetPowerMode(LL_PWR_MODE_STOP0);  | 
 | 64 | +			LL_LPM_EnableSleep();  | 
 | 65 | +			__fallthrough;  | 
 | 66 | +		default:  | 
 | 67 | +			LOG_DBG("Unsupported power substate-id %u",  | 
 | 68 | +				info.substate_id);  | 
 | 69 | +			break;  | 
 | 70 | +		}  | 
 | 71 | +		/* need to restore the clock */  | 
 | 72 | +		stm32_clock_control_init(NULL);  | 
 | 73 | +	}  | 
 | 74 | + | 
 | 75 | +	/*  | 
 | 76 | +	 * System is now in active mode.  | 
 | 77 | +	 * Reenable interrupts which were disabled  | 
 | 78 | +	 * when OS started idling code.  | 
 | 79 | +	 */  | 
 | 80 | +	irq_unlock(0);  | 
 | 81 | +}  | 
 | 82 | + | 
 | 83 | +/* Initialize STM32 Power */  | 
 | 84 | +static int stm32_power_init(const struct device *dev)  | 
 | 85 | +{  | 
 | 86 | +	ARG_UNUSED(dev);  | 
 | 87 | + | 
 | 88 | +	/* enable Power clock */  | 
 | 89 | +	LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_PWR);  | 
 | 90 | + | 
 | 91 | +#ifdef CONFIG_DEBUG  | 
 | 92 | +	/* Enable the Debug Module during all and any Low power mode */  | 
 | 93 | +	LL_DBGMCU_EnableDBGStopMode();  | 
 | 94 | +#endif /* CONFIG_DEBUG */  | 
 | 95 | + | 
 | 96 | +	return 0;  | 
 | 97 | +}  | 
 | 98 | + | 
 | 99 | +SYS_INIT(stm32_power_init, POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT);  | 
0 commit comments