pm.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 <rtthread.h>
  17. #ifndef PM_HAS_CUSTOM_CONFIG
  18. /* All modes used for rt_pm_request() and rt_pm_release() */
  19. enum
  20. {
  21. /* sleep modes */
  22. PM_SLEEP_MODE_NONE = 0,
  23. PM_SLEEP_MODE_IDLE,
  24. PM_SLEEP_MODE_LIGHT,
  25. PM_SLEEP_MODE_DEEP,
  26. PM_SLEEP_MODE_STANDBY,
  27. PM_SLEEP_MODE_SHUTDOWN,
  28. PM_SLEEP_MODE_MAX,
  29. };
  30. enum
  31. {
  32. /* run modes*/
  33. PM_RUN_MODE_HIGH_SPEED = 0,
  34. PM_RUN_MODE_NORMAL_SPEED,
  35. PM_RUN_MODE_MEDIUM_SPEED,
  36. PM_RUN_MODE_LOW_SPEED,
  37. PM_RUN_MODE_MAX,
  38. };
  39. enum
  40. {
  41. RT_PM_FREQUENCY_PENDING = 0x01,
  42. };
  43. #define RT_PM_DEFAULT_SLEEP_MODE PM_SLEEP_MODE_NONE
  44. #define RT_PM_DEFAULT_DEEPSLEEP_MODE PM_SLEEP_MODE_DEEP
  45. #define RT_PM_DEFAULT_RUN_MODE PM_RUN_MODE_NORMAL_SPEED
  46. /* The name of all modes used in the msh command "pm_dump" */
  47. #define PM_SLEEP_MODE_NAMES \
  48. { \
  49. "None Mode", \
  50. "Idle Mode", \
  51. "LightSleep Mode", \
  52. "DeepSleep Mode", \
  53. "Standby Mode", \
  54. "Shutdown Mode", \
  55. }
  56. #define PM_RUN_MODE_NAMES \
  57. { \
  58. "High Speed", \
  59. "Normal Speed", \
  60. "Medium Speed", \
  61. "Low Mode", \
  62. }
  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 /* PM_HAS_CUSTOM_CONFIG */
  90. #include <pm_cfg.h>
  91. #endif /* PM_HAS_CUSTOM_CONFIG */
  92. /**
  93. * device control flag to request or release power
  94. */
  95. #define RT_PM_DEVICE_CTRL_REQUEST 0x01
  96. #define RT_PM_DEVICE_CTRL_RELEASE 0x00
  97. struct rt_pm;
  98. /**
  99. * low power mode operations
  100. */
  101. struct rt_pm_ops
  102. {
  103. void (*sleep)(struct rt_pm *pm, rt_uint8_t mode);
  104. void (*run)(struct rt_pm *pm, rt_uint8_t mode);
  105. void (*timer_start)(struct rt_pm *pm, rt_uint32_t timeout);
  106. void (*timer_stop)(struct rt_pm *pm);
  107. rt_tick_t (*timer_get_tick)(struct rt_pm *pm);
  108. };
  109. struct rt_device_pm_ops
  110. {
  111. int (*suspend)(const struct rt_device *device, rt_uint8_t mode);
  112. void (*resume)(const struct rt_device *device, rt_uint8_t mode);
  113. int (*frequency_change)(const struct rt_device *device, rt_uint8_t mode);
  114. };
  115. struct rt_device_pm
  116. {
  117. const struct rt_device *device;
  118. const struct rt_device_pm_ops *ops;
  119. };
  120. struct rt_pm_module
  121. {
  122. rt_uint8_t req_status;
  123. rt_bool_t busy_flag;
  124. rt_uint32_t timeout;
  125. rt_uint32_t start_time;
  126. };
  127. /**
  128. * power management
  129. */
  130. struct rt_pm
  131. {
  132. struct rt_device parent;
  133. /* modes */
  134. rt_uint8_t modes[PM_SLEEP_MODE_MAX];
  135. rt_uint8_t sleep_mode; /* current sleep mode */
  136. rt_uint8_t run_mode; /* current running mode */
  137. /* modules request status*/
  138. struct rt_pm_module module_status[PM_MODULE_MAX_ID];
  139. /* the list of device, which has PM feature */
  140. rt_uint8_t device_pm_number;
  141. struct rt_device_pm *device_pm;
  142. /* if the mode has timer, the corresponding bit is 1*/
  143. rt_uint8_t timer_mask;
  144. rt_uint8_t flags;
  145. const struct rt_pm_ops *ops;
  146. };
  147. enum
  148. {
  149. RT_PM_ENTER_SLEEP = 0,
  150. RT_PM_EXIT_SLEEP,
  151. };
  152. struct rt_pm_notify
  153. {
  154. void (*notify)(rt_uint8_t event, rt_uint8_t mode, void *data);
  155. void *data;
  156. };
  157. void rt_pm_request(rt_uint8_t sleep_mode);
  158. void rt_pm_release(rt_uint8_t sleep_mode);
  159. void rt_pm_release_all(rt_uint8_t sleep_mode);
  160. int rt_pm_run_enter(rt_uint8_t run_mode);
  161. void rt_pm_device_register(struct rt_device *device, const struct rt_device_pm_ops *ops);
  162. void rt_pm_device_unregister(struct rt_device *device);
  163. void rt_pm_notify_set(void (*notify)(rt_uint8_t event, rt_uint8_t mode, void *data), void *data);
  164. void rt_pm_default_set(rt_uint8_t sleep_mode);
  165. void rt_system_pm_init(const struct rt_pm_ops *ops,
  166. rt_uint8_t timer_mask,
  167. void *user_data);
  168. void rt_pm_module_request(uint8_t module_id, rt_uint8_t sleep_mode);
  169. void rt_pm_module_release(uint8_t module_id, rt_uint8_t sleep_mode);
  170. void rt_pm_module_release_all(uint8_t module_id, rt_uint8_t sleep_mode);
  171. void rt_pm_module_delay_sleep(rt_uint8_t module_id, rt_tick_t timeout);
  172. rt_uint32_t rt_pm_module_get_status(void);
  173. rt_uint8_t rt_pm_get_sleep_mode(void);
  174. #endif /* __PM_H__ */