InPlay API
hal_power.h
1 
13 #ifndef HAL_POWER_H
14 #define HAL_POWER_H
15 
25 #include <stdint.h>
26 #include "in_mmap.h"
27 #include "in_arm.h"
28 
29 #include "./hal/hal_global.h"
30 
31 /*
32  * Defines
33  ****************************************************************************************
34  */
35 #define PM_MAX_SLEEP_TIME 0xFFFFFFFF
36 
37 /*
38  * Enumerations
39  ****************************************************************************************
40  */
41 
42 enum pm_state {
43  PM_ACTIVE,
44  PM_SLEEP,
45  PM_DEEP_SLEEP,
46  PM_NOP,
47 };
48 
49 enum pm_err {
50  PM_ERR_NO_ERROR,
51  PM_ERR_INVALID_PARAM = -1,
52  PM_ERR_NOT_AVAIL = -2,
53  PM_ERR_OS_ERROR = -3,
54 };
55 
56 enum em_retn {
57  PM_RETN_EM_4K_A = 0x1,
58  PM_RETN_EM_4K_B = 0x2,
59  PM_RETN_EM_8K_A = 0x4,
60  PM_RETN_EM_8K_B = 0x8,
61  PM_RETN_EM_16K = 0x10,
62  PM_RETN_EM_ALL = 0x1F,
63 };
64 
65 enum dm_retn {
66  PM_RETN_DM_4K_A = 0x1,
67  PM_RETN_DM_4K_B = 0x2,
68  PM_RETN_DM_8K = 0x4,
69  PM_RETN_DM_16K = 0x8,
70  PM_RETN_DM_32K_A = 0x10,
71  PM_RETN_DM_32K_B = 0x20,
72  PM_RETN_DM_32K_C = 0x40,
73  PM_RETN_DM_ALL = 0x7F,
74 };
75 
76 enum pm_ws {
77  PM_WS_TIMER_0 = 0,
78  PM_WS_TIMER_1,
79  PM_WS_TIMER_2,
80  PM_WS_TIMER_3,
81  PM_WS_WDT,
82  PM_WS_AON_TIMER2_EMIT0,
83  PM_WS_AON_TIMER2_EMIT1,
84  PM_WS_AON_SYSTICK_TIMER,
85  PM_WS_BOD,
86  PM_WS_BOD2,
87  PM_WS_BLE,
88  PM_WS_GPIO_0_0,
89  PM_WS_GPIO_0_1,
90  PM_WS_GPIO_0_2,
91  PM_WS_GPIO_0_3,
92  PM_WS_GPIO_0_4,
93  PM_WS_GPIO_0_5,
94  PM_WS_GPIO_0_6,
95  PM_WS_GPIO_0_7,
96  PM_WS_GPIO_0_8,
97  PM_WS_GPIO_1_0,
98  PM_WS_GPIO_1_1,
99  PM_WS_GPIO_1_2,
100  PM_WS_GPIO_1_3,
101  PM_WS_GPIO_1_4,
102  PM_WS_GPIO_1_5,
103  PM_WS_GPIO_1_6,
104  PM_WS_GPIO_1_7,
105  PM_WS_GPIO_1_8,
106  PM_WS_GPIO_1_9,
107  PM_WS_GPIO_2_0,
108  PM_WS_GPIO_2_1,
109  PM_WS_GPIO_2_2,
110  PM_WS_GPIO_2_3,
111  PM_WS_GPIO_2_4,
112  PM_WS_GPIO_2_5,
113  PM_WS_GPIO_2_6,
114  PM_WS_GPIO_2_7,
115  PM_WS_GPIO_2_8,
116  PM_WS_GPIO_2_9,
117  PM_WS_GPIO_3_0,
118  PM_WS_GPIO_3_1,
119  PM_WS_GPIO_3_2,
120  PM_WS_GPIO_3_3,
121  PM_WS_GPIO_3_4,
122  PM_WS_GPIO_3_5,
123  PM_WS_GPIO_4_0,
124  PM_WS_GPIO_4_1,
125  PM_WS_GPIO_4_2,
126  PM_WS_GPIO_4_3,
127 };
128 
129 /*
130  * Structures
131  ****************************************************************************************
132  */
133 struct pm_module {
134  struct pm_module *prev;
135  struct pm_module *next;
136  void *arg;
137  int (*power_state)(void *arg, uint32_t *slp_dur);
138  void (*power_down)(void *arg, uint32_t slp_dur);
139  void (*power_up)(void *arg);
140 };
141 
142 /*
143  * Inline Functions
144  ****************************************************************************************
145  */
146 static INLINE void aon_slp_tmr_wup_enable(void)
147 {
148  WR_WORD(AON_PS_REGS_AON_ST_WAKEUP_CTRL, 1);
149 }
150 
151 static INLINE void aon_slp_tmr_wup_disable(void)
152 {
153  WR_WORD(AON_PS_REGS_AON_ST_WAKEUP_CTRL, 0);
154 }
155 
156 static INLINE void aon_ble_wup_enable(void)
157 {
158  WR_WORD(AON_PS_REGS_BLE_ST_WAKEUP_CTRL, 1);
159 }
160 
161 static INLINE void aon_ble_wup_disable(void)
162 {
163  WR_WORD(AON_PS_REGS_BLE_ST_WAKEUP_CTRL, 0);
164 }
165 
166 static INLINE void aon_brown_out_wup_enable(void)
167 {
168  WR_WORD(AON_PS_REGS_BO_OUT_WAKEUP_CTRL, 1);
169 }
170 
171 static INLINE void aon_brown_out_wup_disable(void)
172 {
173  WR_WORD(AON_PS_REGS_BO_OUT_WAKEUP_CTRL, 0);
174 }
175 
176 static INLINE void aon_mixed_signal_wup_enable(void)
177 {
178  WR_WORD(AON_PS_REGS_MSIO_WAKEUP_CTRL, 1);
179 }
180 
181 static INLINE void aon_mixed_signal_wup_disable(void)
182 {
183  WR_WORD(AON_PS_REGS_MSIO_WAKEUP_CTRL, 0);
184 }
185 
186 static INLINE void aon_deep_sleep_enable(int force)
187 {
188  uint32_t reg = RD_WORD(AON_PS_REGS_PD_DOO_SLEEP_CTRL);
189 
190  if (force) { // force sleep even there is wak up signal
191  reg |= AON_PS_REGS_PD_DOO_SLEEP_CTRL_FORCE;
192  } else {
193  reg &= ~AON_PS_REGS_PD_DOO_SLEEP_CTRL_FORCE;
194  }
195  reg |= AON_PS_REGS_PD_DOO_SLEEP_CTRL_PD_DOO_CORE_ENABLE; /* PD_DOO_CORE Sleep */
196 
197  WR_WORD(AON_PS_REGS_PD_DOO_SLEEP_CTRL, reg);
198 }
199 
200 static INLINE void aon_deep_sleep_disable(void)
201 {
202  uint32_t reg = RD_WORD(AON_PS_REGS_PD_DOO_SLEEP_CTRL);
203 
204  reg &= ~AON_PS_REGS_PD_DOO_SLEEP_CTRL_PD_DOO_CORE_ENABLE;
205 
206  WR_WORD(AON_PS_REGS_PD_DOO_SLEEP_CTRL, reg);
207 }
208 
209 static INLINE void aon_dm_reten(uint32_t retn)
210 {
211  uint32_t reg = RD_WORD(AON_PS_REGS_PD_DOO_SLEEP_CTRL);
212 
213  if (retn & PM_RETN_DM_4K_A)
214  reg |= AON_PS_REGS_PD_DOO_SLEEP_CTRL_DM_4K_A_RET_ENABLE;
215  else
216  reg &= ~AON_PS_REGS_PD_DOO_SLEEP_CTRL_DM_4K_A_RET_ENABLE;
217 
218  if (retn & PM_RETN_DM_4K_B)
219  reg |= AON_PS_REGS_PD_DOO_SLEEP_CTRL_DM_4K_B_RET_ENABLE;
220  else
221  reg &= ~AON_PS_REGS_PD_DOO_SLEEP_CTRL_DM_4K_B_RET_ENABLE;
222 
223  if (retn & PM_RETN_DM_8K)
224  reg |= AON_PS_REGS_PD_DOO_SLEEP_CTRL_DM_8K_RET_ENABLE;
225  else
226  reg &= ~AON_PS_REGS_PD_DOO_SLEEP_CTRL_DM_8K_RET_ENABLE;
227 
228  if (retn & PM_RETN_DM_16K)
229  reg |= AON_PS_REGS_PD_DOO_SLEEP_CTRL_DM_16K_RET_ENABLE;
230  else
231  reg &= ~AON_PS_REGS_PD_DOO_SLEEP_CTRL_DM_16K_RET_ENABLE;
232 
233  if (retn & PM_RETN_DM_32K_A)
234  reg |= AON_PS_REGS_PD_DOO_SLEEP_CTRL_DM_32K_A_RET_ENABLE;
235  else
236  reg &= ~AON_PS_REGS_PD_DOO_SLEEP_CTRL_DM_32K_A_RET_ENABLE;
237 
238  if (retn & PM_RETN_DM_32K_B)
239  reg |= AON_PS_REGS_PD_DOO_SLEEP_CTRL_DM_32K_B_RET_ENABLE;
240  else
241  reg &= ~AON_PS_REGS_PD_DOO_SLEEP_CTRL_DM_32K_B_RET_ENABLE;
242 
243  if (retn & PM_RETN_DM_32K_C)
244  reg |= AON_PS_REGS_PD_DOO_SLEEP_CTRL_DM_32K_C_RET_ENABLE;
245  else
246  reg &= ~AON_PS_REGS_PD_DOO_SLEEP_CTRL_DM_32K_C_RET_ENABLE;
247 
248  WR_WORD(AON_PS_REGS_PD_DOO_SLEEP_CTRL, reg);
249 }
250 
251 static INLINE void aon_em_reten(uint32_t retn)
252 {
253  uint32_t reg = RD_WORD(AON_PS_REGS_PD_DOO_SLEEP_CTRL);
254 
255  if (retn & PM_RETN_EM_4K_A)
256  reg |= AON_PS_REGS_PD_DOO_SLEEP_CTRL_EM_4K_A_RET_ENABLE;
257  else
258  reg &= ~AON_PS_REGS_PD_DOO_SLEEP_CTRL_EM_4K_A_RET_ENABLE;
259 
260  if (retn & PM_RETN_EM_4K_B)
261  reg |= AON_PS_REGS_PD_DOO_SLEEP_CTRL_EM_4K_B_RET_ENABLE;
262  else
263  reg &= ~AON_PS_REGS_PD_DOO_SLEEP_CTRL_EM_4K_B_RET_ENABLE;
264 
265  if (retn & PM_RETN_EM_8K_A)
266  reg |= AON_PS_REGS_PD_DOO_SLEEP_CTRL_EM_8K_A_RET_ENABLE;
267  else
268  reg &= ~AON_PS_REGS_PD_DOO_SLEEP_CTRL_EM_8K_A_RET_ENABLE;
269 
270  if (retn & PM_RETN_EM_8K_B)
271  reg |= AON_PS_REGS_PD_DOO_SLEEP_CTRL_EM_8K_B_RET_ENABLE;
272  else
273  reg &= ~AON_PS_REGS_PD_DOO_SLEEP_CTRL_EM_8K_B_RET_ENABLE;
274 
275  if (retn & PM_RETN_EM_16K)
276  reg |= AON_PS_REGS_PD_DOO_SLEEP_CTRL_EM_16K_A_RET_ENABLE;
277  else
278  reg &= ~AON_PS_REGS_PD_DOO_SLEEP_CTRL_EM_16K_A_RET_ENABLE;
279 
280  WR_WORD(AON_PS_REGS_PD_DOO_SLEEP_CTRL, reg);
281 }
282 
283 static INLINE void aon_trig_mem_retn(int en)
284 {
285  uint32_t reg = RD_WORD(AON_PS_REGS_PD_DOO_SLEEP_CTRL);
286 
287  if (en) {
288  reg |= AON_PS_REGS_PD_DOO_SLEEP_CTRL_TRIG_RET_ENABLE;
289  } else {
290  reg &= ~AON_PS_REGS_PD_DOO_SLEEP_CTRL_TRIG_RET_ENABLE;
291  }
292 
293  WR_WORD(AON_PS_REGS_PD_DOO_SLEEP_CTRL, reg);
294 }
295 
296 static INLINE uint32_t aon_wup_raw_status(void)
297 {
298  return (RD_WORD(AON_PS_REGS_RAW_WAKEUP_BITS));
299 }
300 
301 static INLINE uint32_t aon_wup_service_req(void)
302 {
303  return (RD_WORD(AON_PS_REGS_SERVICED_REQUEST));
304 }
305 
306 static INLINE uint32_t aon_wup_active_req(void)
307 {
308  return (RD_WORD(AON_PS_REGS_ACTIVE_REQUEST));
309 }
310 
311 static INLINE uint32_t aon_wup_last_req(void)
312 {
313  return (RD_WORD(AON_PS_REGS_LAST_REQUEST));
314 }
315 
316 static INLINE uint32_t aon_wup_status_and_clear(void)
317 {
318  uint32_t reg = RD_WORD(AON_PS_REGS_REQUEST_WITH_SOFT_CLR);
319  WR_WORD(AON_PS_REGS_REQUEST_WITH_SOFT_CLR, (reg | AON_PS_REGS_REQUEST_WITH_SOFT_CLR_CTL_REQUEST_CLR));
320  return reg;
321 }
322 
323 static INLINE void aon_ble_ext_wup(int en)
324 {
325  uint32_t reg = RD_WORD(AON_REG_AON_MISC_CTRL);
326 
327  if (en) {
328  reg |= AON_REG_AON_MISC_CTRL_CTL_BLE_EXT_WAKEUP_ENABLE;
329  } else {
330  reg &= ~AON_REG_AON_MISC_CTRL_CTL_BLE_EXT_WAKEUP_ENABLE;
331  }
332 
333  WR_WORD(AON_REG_AON_MISC_CTRL, reg);
334 }
335 
336 static INLINE uint32_t pm_wakeup_source(void)
337 {
338  return (RD_WORD(GLOBAL_REG_PD0_WAKEUP_SRC_PD1_RST_INFO) & GLOBAL_REG_PD0_WAKEUP_SRC_PD1_RST_INFO_STS_WAKEUP_SRC_MASK);
339 }
340 
341 void pd1_tmr_init(void);
342 void pd1_tmr_stop(void);
343 void pd1_tmr_periodic_start(uint32_t usec);
344 /*
345  * APIs
346  ****************************************************************************************
347  */
348 
364 int hal_pm_init(int slp_wup, int ble_wup, int bod_wup, int mix_wup, int trig_retn, uint32_t dm_retn, uint32_t em_retn);
365 
375 int hal_pm_reg_mod(struct pm_module *module);
376 
386 int hal_pm_unreg_mod(struct pm_module *module);
387 
407 uint32_t hal_pm_suspend_and_resume(uint32_t os_sleep);
408 
418 int hal_pm_reg_ble_mod(struct pm_module *module);
419 
429 int hal_pm_unreg_ble_mod(struct pm_module *module);
430 
439 uint32_t hal_pm_get_wup_src(void);
440 
449 void hal_pm_mix_sig_wup_enable(void);
450 void hal_pm_mix_sig_wup_disable(void);
451 
452 
454 
455 #endif // HAL_POWER_H
int hal_pm_reg_mod(struct pm_module *module)
Register power module function.
uint32_t hal_pm_suspend_and_resume(uint32_t os_sleep)
System shutdown and resume function.
int hal_pm_reg_ble_mod(struct pm_module *module)
Register BLE power module function.
int hal_pm_init(int slp_wup, int ble_wup, int bod_wup, int mix_wup, int trig_retn, uint32_t dm_retn, uint32_t em_retn)
Power Management initialization function.
void hal_pm_mix_sig_wup_enable(void)
Enable/Disable mix signal (GPIO PORT 2) wake up.
uint32_t hal_pm_get_wup_src(void)
Read the wake up source.
int hal_pm_unreg_ble_mod(struct pm_module *module)
Unregister BLE power module function.
int hal_pm_unreg_mod(struct pm_module *module)
Unregister power module function.