1 /* SPDX-License-Identifier: BSD-3-Clause */
2 /*
3  * Copyright (c) 2020-2021 Rockchip Electronics Co., Ltd.
4  */
5 
6 #include "hal_conf.h"
7 
8 /** @addtogroup RK_HAL_Driver
9  *  @{
10  */
11 
12 /** @addtogroup PM
13  *  @{
14  */
15 
16 #ifndef _HAL_PM_H_
17 #define _HAL_PM_H_
18 
19 #include "hal_def.h"
20 
21 /***************************** MACRO Definition ******************************/
22 /** @defgroup DEMO_Exported_Definition_Group1 Basic Definition
23  *  @{
24  */
25 
26 #define PM_RUNTIME_TYPE_MUTI_SFT (3)
27 #define PM_RUNTIME_PER_TYPE_NUM  (8)
28 
29 #define PM_RUNTIME_TYPE_TO_FIRST_ID(type) ((type) << PM_RUNTIME_TYPE_MUTI_SFT)
30 #define PM_RUNTIME_ID_TO_TYPE(id)         ((id) >> PM_RUNTIME_TYPE_MUTI_SFT)
31 #define PM_RUNTIME_ID_TO_TYPE_OFFSET(id)  ((id) % PM_RUNTIME_PER_TYPE_NUM)
32 #define PM_RUNTIME_ID_TYPE_BIT_MSK(id)    HAL_BIT(((id) % PM_RUNTIME_PER_TYPE_NUM))
33 
34 #define PM_DISPLAY_REQUESTED(pdata) ((pdata)->bits[PM_RUNTIME_TYPE_DISPLAY])
35 #define PM_UART_REQUESTED(pdata)    ((pdata)->bits[PM_RUNTIME_TYPE_UART])
36 #define PM_I2C_REQUESTED(pdata)     ((pdata)->bits[PM_RUNTIME_TYPE_I2C])
37 #define PM_INTF_REQUESTED(pdata)    ((pdata)->bits[PM_RUNTIME_TYPE_INTF])
38 #define PM_HS_INTF_REQUESTED(pdata) ((pdata)->bits[PM_RUNTIME_TYPE_HS_INTF])
39 #define PM_SPI_REQUESTED(pdata)     ((pdata)->bits[PM_RUNTIME_TYPE_SPI])
40 
41 /* suspend config id */
42 #define PM_SLEEP_MODE_CONFIG   0x01
43 #define PM_SLEEP_WAKEUP_SOURCE 0x02
44 
45 enum {
46     PM_RUNTIME_TYPE_INTF = 0, /**< normal interface */
47     PM_RUNTIME_TYPE_DISPLAY,
48     PM_RUNTIME_TYPE_AUDIO,
49     PM_RUNTIME_TYPE_HS_INTF, /**< high speed interface */
50     PM_RUNTIME_TYPE_STORAGE,
51     PM_RUNTIME_TYPE_UART,
52     PM_RUNTIME_TYPE_I2C,
53     PM_RUNTIME_TYPE_SPI,
54     PM_RUNTIME_TYPE_DEVICE,
55     PM_RUNTIME_TYPE_END,
56 };
57 
58 typedef enum {
59     PM_RUNTIME_IDLE_ONLY = 0,
60     PM_RUNTIME_IDLE_NORMAL,
61     PM_RUNTIME_IDLE_DEEP,
62     PM_RUNTIME_IDLE_DEEP1,
63     PM_RUNTIME_IDLE_DEEP2,
64 } ePM_RUNTIME_idleMode;
65 
66 typedef enum {
67     PM_RUNTIME_ID_INTF_INVLD = PM_RUNTIME_TYPE_TO_FIRST_ID(PM_RUNTIME_TYPE_INTF), /**< the id = 0, is means invalid */
68     PM_RUNTIME_ID_SPI_APB,
69     PM_RUNTIME_ID_VOP = PM_RUNTIME_TYPE_TO_FIRST_ID(PM_RUNTIME_TYPE_DISPLAY),
70     PM_RUNTIME_ID_MIPI,
71 
72     PM_RUNTIME_ID_I2S = PM_RUNTIME_TYPE_TO_FIRST_ID(PM_RUNTIME_TYPE_AUDIO),
73     PM_RUNTIME_ID_I2S1,
74     PM_RUNTIME_ID_I2S2,
75     PM_RUNTIME_ID_ADC,
76     PM_RUNTIME_ID_DMA,
77 
78     PM_RUNTIME_ID_USB = PM_RUNTIME_TYPE_TO_FIRST_ID(PM_RUNTIME_TYPE_HS_INTF),
79     PM_RUNTIME_ID_SDIO,
80 
81     PM_RUNTIME_ID_UART0 = PM_RUNTIME_TYPE_TO_FIRST_ID(PM_RUNTIME_TYPE_UART),
82     PM_RUNTIME_ID_UART1,
83     PM_RUNTIME_ID_UART2,
84 
85     PM_RUNTIME_ID_I2C0 = PM_RUNTIME_TYPE_TO_FIRST_ID(PM_RUNTIME_TYPE_I2C),
86     PM_RUNTIME_ID_I2C1,
87     PM_RUNTIME_ID_I2C2,
88     PM_RUNTIME_ID_I2C3,
89     PM_RUNTIME_ID_I2C4,
90     PM_RUNTIME_ID_I2C5,
91 
92     PM_RUNTIME_ID_SPI = PM_RUNTIME_TYPE_TO_FIRST_ID(PM_RUNTIME_TYPE_SPI),
93     PM_RUNTIME_ID_END,
94 } ePM_RUNTIME_ID;
95 
96 /***************************** Structure Definition **************************/
97 struct PM_RUNTIME_INFO {
98     uint8_t bits[PM_RUNTIME_TYPE_END];
99 };
100 
101 #ifdef HAL_PM_SLEEP_MODULE_ENABLED
102 struct PM_SUSPEND_INFO {
103     union {
104         struct {
105             uint32_t uartChannel : 4;   /*!< bit:  0.. 3  uart debug channel num */
106             uint32_t uartValid : 1;     /*!< bit:      4  uart channel valid flag */
107             uint32_t _reserved : 27;    /*!< bit:  5..31  Reserved */
108         } flag;
109         uint32_t suspendFlag;
110     };
111 };
112 
113 struct SLEEP_CONFIG_DATA {
114     uint32_t suspendMode;
115     uint32_t suspendWkupSrc;
116 };
117 #endif
118 
119 /** @} */
120 /***************************** Function Declare ******************************/
121 /** @defgroup PM_Public_Function_Declare Public Function Declare
122  *  @{
123  */
124 #ifdef HAL_PM_SLEEP_MODULE_ENABLED
125 /**
126  * @brief  it is the enterpoint for suspend invoked by a os's powermanager implement.
127  * @param  suspendInfo: suspend information for controlling
128  * @return HAL_Status
129  */
130 int HAL_SYS_Suspend(struct PM_SUSPEND_INFO *suspendInfo);
131 struct SLEEP_CONFIG_DATA *HAL_SYS_GetSuspendConfig(void);
132 HAL_Status HAL_SYS_SuspendConfig(uint32_t id, uint32_t data);
133 #endif
134 
135 #ifdef HAL_PM_CPU_SLEEP_MODULE_ENABLED
136 void HAL_CPU_ArchSuspend(uint32_t *ptr);
137 void HAL_CPU_ArchResume(void);
138 void HAL_CPU_DoResume(void);
139 
140 void HAL_NVIC_SuspendSave(void);
141 void HAL_NVIC_ResumeRestore(void);
142 
143 void HAL_SCB_SuspendSave(void);
144 void HAL_SCB_ResumeRestore(void);
145 
146 int HAL_CPU_SuspendEnter(uint32_t flag, int (*suspend)(uint32_t));
147 void HAL_CPU_SuspendSave(uint32_t *ptr, uint32_t ptrsz, uint32_t sp, uint32_t *ptrSave);
148 #endif
149 
150 #ifdef HAL_PM_RUNTIME_MODULE_ENABLED
151 HAL_Status HAL_PM_RuntimeRequest(ePM_RUNTIME_ID runtimeId);
152 HAL_Status HAL_PM_RuntimeRelease(ePM_RUNTIME_ID runtimeId);
153 const struct PM_RUNTIME_INFO *HAL_PM_RuntimeGetData(void);
154 
155 /**
156  * @brief  it is for runtime power manager.
157  * @param  idleMode: the soc pm mode will be config
158  * @return the mask bits indicate request source.
159  */
160 uint32_t HAL_PM_RuntimeEnter(ePM_RUNTIME_idleMode idleMode);
161 #endif
162 
163 /**
164  * @brief  it is for statting a pm timer .
165  * @param  timeoutCount: the next timeout count
166  * @param  needTimeout:  if ture, need to start a timer.
167  * @return HAL_Status.
168  */
169 HAL_Status HAL_PM_TimerStart(uint64_t timeoutCount, bool needTimeout);
170 
171 /**
172  * @brief  it is for stopping a pm timer .
173  * @return HAL_Status.
174  */
175 HAL_Status HAL_PM_TimerStop(void);
176 
177 /**
178  * @brief  it is for getting the sleep time.
179  * @return the sleep time.
180  */
181 uint64_t HAL_PM_GetTimerCount(void);
182 
183 /** @} */
184 
185 #endif
186 
187 /** @} */
188 
189 /** @} */
190