hal_pm.h 5.3 KB

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