pm.h 5.8 KB

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