pm.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * Copyright (c) 2006-2024 RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2012-06-02 Bernard the first version
  9. * 2018-08-02 Tanek split run and sleep modes, support custom mode
  10. * 2019-04-28 Zero-Free improve PM mode and device ops interface
  11. * 2020-11-23 zhangsz update pm mode select
  12. * 2020-11-27 zhangsz update pm 2.0
  13. * 2024-07-04 wdfk-prog The device is registered and uninstalled by linked list
  14. */
  15. #ifndef __PM_H__
  16. #define __PM_H__
  17. #include <stdint.h>
  18. #include <rtthread.h>
  19. #include <drivers/lptimer.h>
  20. /* All modes used for rt_pm_request() and rt_pm_release() */
  21. enum
  22. {
  23. /* sleep modes */
  24. PM_SLEEP_MODE_NONE = 0,
  25. PM_SLEEP_MODE_IDLE,
  26. PM_SLEEP_MODE_LIGHT,
  27. PM_SLEEP_MODE_DEEP,
  28. PM_SLEEP_MODE_STANDBY,
  29. PM_SLEEP_MODE_SHUTDOWN,
  30. PM_SLEEP_MODE_MAX,
  31. };
  32. enum
  33. {
  34. /* run modes*/
  35. PM_RUN_MODE_HIGH_SPEED = 0,
  36. PM_RUN_MODE_NORMAL_SPEED,
  37. PM_RUN_MODE_MEDIUM_SPEED,
  38. PM_RUN_MODE_LOW_SPEED,
  39. PM_RUN_MODE_MAX,
  40. };
  41. enum
  42. {
  43. RT_PM_FREQUENCY_PENDING = 0x01,
  44. };
  45. /* The name of all modes used in the msh command "pm_dump" */
  46. #define PM_SLEEP_MODE_NAMES \
  47. { \
  48. "None Mode", \
  49. "Idle Mode", \
  50. "LightSleep Mode", \
  51. "DeepSleep Mode", \
  52. "Standby Mode", \
  53. "Shutdown Mode", \
  54. }
  55. #define PM_RUN_MODE_NAMES \
  56. { \
  57. "High Speed", \
  58. "Normal Speed", \
  59. "Medium Speed", \
  60. "Low Mode", \
  61. }
  62. #ifndef PM_USING_CUSTOM_CONFIG
  63. /**
  64. * Modules used for
  65. * pm_module_request(PM_BOARD_ID, PM_SLEEP_MODE_IDLE)
  66. * pm_module_release(PM_BOARD_ID, PM_SLEEP_MODE_IDLE)
  67. * pm_module_release_all(PM_BOARD_ID, PM_SLEEP_MODE_IDLE)
  68. */
  69. enum pm_module_id {
  70. PM_NONE_ID = 0,
  71. PM_POWER_ID,
  72. PM_BOARD_ID,
  73. PM_BSP_ID,
  74. PM_MAIN_ID,
  75. PM_PMS_ID,
  76. PM_PMC_ID,
  77. PM_TASK_ID,
  78. PM_SPI_ID,
  79. PM_I2C_ID,
  80. PM_UART_ID,
  81. PM_CAN_ID,
  82. PM_ETH_ID,
  83. PM_SENSOR_ID,
  84. PM_LCD_ID,
  85. PM_KEY_ID,
  86. PM_TP_ID,
  87. PM_MODULE_MAX_ID, /* enum must! */
  88. };
  89. #else
  90. #include <pm_cfg.h>
  91. #endif /* PM_USING_CUSTOM_CONFIG */
  92. #ifndef RT_PM_DEFAULT_SLEEP_MODE
  93. #define RT_PM_DEFAULT_SLEEP_MODE PM_SLEEP_MODE_NONE
  94. #endif
  95. #ifndef RT_PM_DEFAULT_DEEPSLEEP_MODE
  96. #define RT_PM_DEFAULT_DEEPSLEEP_MODE PM_SLEEP_MODE_DEEP
  97. #endif
  98. #ifndef RT_PM_DEFAULT_RUN_MODE
  99. #define RT_PM_DEFAULT_RUN_MODE PM_RUN_MODE_NORMAL_SPEED
  100. #endif
  101. /**
  102. * device control flag to request or release power
  103. */
  104. #define RT_PM_DEVICE_CTRL_RELEASE (RT_DEVICE_CTRL_BASE(PM) + 0x00)
  105. #define RT_PM_DEVICE_CTRL_REQUEST (RT_DEVICE_CTRL_BASE(PM) + 0x01)
  106. struct rt_pm;
  107. /**
  108. * low power mode operations
  109. */
  110. struct rt_pm_ops
  111. {
  112. void (*sleep)(struct rt_pm *pm, rt_uint8_t mode);
  113. void (*run)(struct rt_pm *pm, rt_uint8_t mode);
  114. void (*timer_start)(struct rt_pm *pm, rt_uint32_t timeout);
  115. void (*timer_stop)(struct rt_pm *pm);
  116. rt_tick_t (*timer_get_tick)(struct rt_pm *pm);
  117. };
  118. struct rt_device_pm_ops
  119. {
  120. rt_err_t (*suspend)(const struct rt_device *device, rt_uint8_t mode);
  121. void (*resume)(const struct rt_device *device, rt_uint8_t mode);
  122. rt_err_t (*frequency_change)(const struct rt_device *device, rt_uint8_t mode);
  123. };
  124. struct rt_device_pm
  125. {
  126. const struct rt_device *device;
  127. const struct rt_device_pm_ops *ops;
  128. rt_slist_t list;
  129. };
  130. struct rt_pm_module
  131. {
  132. rt_uint8_t req_status;
  133. rt_bool_t busy_flag;
  134. rt_uint32_t timeout;
  135. rt_uint32_t start_time;
  136. };
  137. /**
  138. * power management
  139. */
  140. struct rt_pm
  141. {
  142. struct rt_device parent;
  143. /* modes */
  144. rt_uint8_t modes[PM_SLEEP_MODE_MAX];
  145. rt_uint8_t sleep_mode; /* current sleep mode */
  146. rt_uint8_t run_mode; /* current running mode */
  147. /* modules request status*/
  148. struct rt_pm_module module_status[PM_MODULE_MAX_ID];
  149. /* sleep request table */
  150. rt_uint32_t sleep_status[PM_SLEEP_MODE_MAX - 1][(PM_MODULE_MAX_ID + 31) / 32];
  151. /* the list of device, which has PM feature */
  152. rt_slist_t device_list;
  153. struct rt_device_pm *device_pm;
  154. /* if the mode has timer, the corresponding bit is 1*/
  155. rt_uint8_t timer_mask;
  156. rt_uint8_t flags;
  157. const struct rt_pm_ops *ops;
  158. };
  159. enum
  160. {
  161. RT_PM_ENTER_SLEEP = 0,
  162. RT_PM_EXIT_SLEEP,
  163. };
  164. struct rt_pm_notify
  165. {
  166. void (*notify)(rt_uint8_t event, rt_uint8_t mode, void *data);
  167. void *data;
  168. };
  169. rt_err_t rt_pm_request(rt_uint8_t sleep_mode);
  170. rt_err_t rt_pm_release(rt_uint8_t sleep_mode);
  171. rt_err_t rt_pm_release_all(rt_uint8_t sleep_mode);
  172. rt_err_t rt_pm_run_enter(rt_uint8_t run_mode);
  173. void rt_pm_device_register(struct rt_device *device, const struct rt_device_pm_ops *ops);
  174. void rt_pm_device_unregister(struct rt_device *device);
  175. void rt_pm_notify_set(void (*notify)(rt_uint8_t event, rt_uint8_t mode, void *data), void *data);
  176. void rt_pm_default_set(rt_uint8_t sleep_mode);
  177. void rt_system_pm_init(const struct rt_pm_ops *ops,
  178. rt_uint8_t timer_mask,
  179. void *user_data);
  180. rt_err_t rt_pm_module_request(uint8_t module_id, rt_uint8_t sleep_mode);
  181. rt_err_t rt_pm_module_release(uint8_t module_id, rt_uint8_t sleep_mode);
  182. rt_err_t rt_pm_module_release_all(uint8_t module_id, rt_uint8_t sleep_mode);
  183. void rt_pm_module_delay_sleep(rt_uint8_t module_id, rt_tick_t timeout);
  184. rt_uint32_t rt_pm_module_get_status(void);
  185. rt_uint8_t rt_pm_get_sleep_mode(void);
  186. struct rt_pm *rt_pm_get_handle(void);
  187. /* sleep : request or release */
  188. rt_err_t rt_pm_sleep_request(rt_uint16_t module_id, rt_uint8_t mode);
  189. rt_err_t rt_pm_sleep_release(rt_uint16_t module_id, rt_uint8_t mode);
  190. rt_err_t rt_pm_sleep_none_request(rt_uint16_t module_id);
  191. rt_err_t rt_pm_sleep_none_release(rt_uint16_t module_id);
  192. rt_err_t rt_pm_sleep_idle_request(rt_uint16_t module_id);
  193. rt_err_t rt_pm_sleep_idle_release(rt_uint16_t module_id);
  194. rt_err_t rt_pm_sleep_light_request(rt_uint16_t module_id);
  195. rt_err_t rt_pm_sleep_light_release(rt_uint16_t module_id);
  196. #endif /* __PM_H__ */