pm.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Copyright (c) 2006-2018, 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. */
  12. #ifndef __PM_H__
  13. #define __PM_H__
  14. #include <rtthread.h>
  15. #ifndef PM_HAS_CUSTOM_CONFIG
  16. /* All modes used for rt_pm_request() and rt_pm_release() */
  17. enum
  18. {
  19. /* sleep modes */
  20. PM_SLEEP_MODE_NONE = 0,
  21. PM_SLEEP_MODE_IDLE,
  22. PM_SLEEP_MODE_LIGHT,
  23. PM_SLEEP_MODE_DEEP,
  24. PM_SLEEP_MODE_STANDBY,
  25. PM_SLEEP_MODE_SHUTDOWN,
  26. PM_SLEEP_MODE_MAX,
  27. };
  28. enum
  29. {
  30. /* run modes*/
  31. PM_RUN_MODE_HIGH_SPEED = 0,
  32. PM_RUN_MODE_NORMAL_SPEED,
  33. PM_RUN_MODE_MEDIUM_SPEED,
  34. PM_RUN_MODE_LOW_SPEED,
  35. PM_RUN_MODE_MAX,
  36. };
  37. enum
  38. {
  39. RT_PM_FREQUENCY_PENDING = 0x01,
  40. };
  41. #define RT_PM_DEFAULT_SLEEP_MODE PM_SLEEP_MODE_IDLE
  42. #define RT_PM_DEFAULT_RUN_MODE PM_RUN_MODE_NORMAL_SPEED
  43. /* The name of all modes used in the msh command "pm_dump" */
  44. #define PM_SLEEP_MODE_NAMES \
  45. { \
  46. "None Mode", \
  47. "Idle Mode", \
  48. "LightSleep Mode", \
  49. "DeepSleep Mode", \
  50. "Standby Mode", \
  51. "Shutdown Mode", \
  52. }
  53. #define PM_RUN_MODE_NAMES \
  54. { \
  55. "High Speed", \
  56. "Normal Speed", \
  57. "Medium Speed", \
  58. "Low Mode", \
  59. }
  60. #else /* PM_HAS_CUSTOM_CONFIG */
  61. #include <pm_cfg.h>
  62. #endif /* PM_HAS_CUSTOM_CONFIG */
  63. /**
  64. * device control flag to request or release power
  65. */
  66. #define RT_PM_DEVICE_CTRL_REQUEST 0x01
  67. #define RT_PM_DEVICE_CTRL_RELEASE 0x00
  68. struct rt_pm;
  69. /**
  70. * low power mode operations
  71. */
  72. struct rt_pm_ops
  73. {
  74. void (*sleep)(struct rt_pm *pm, rt_uint8_t mode);
  75. void (*run)(struct rt_pm *pm, rt_uint8_t mode);
  76. void (*timer_start)(struct rt_pm *pm, rt_uint32_t timeout);
  77. void (*timer_stop)(struct rt_pm *pm);
  78. rt_tick_t (*timer_get_tick)(struct rt_pm *pm);
  79. };
  80. struct rt_device_pm_ops
  81. {
  82. int (*suspend)(const struct rt_device *device, rt_uint8_t mode);
  83. void (*resume)(const struct rt_device *device, rt_uint8_t mode);
  84. int (*frequency_change)(const struct rt_device *device, rt_uint8_t mode);
  85. };
  86. struct rt_device_pm
  87. {
  88. const struct rt_device *device;
  89. const struct rt_device_pm_ops *ops;
  90. };
  91. /**
  92. * power management
  93. */
  94. struct rt_pm
  95. {
  96. struct rt_device parent;
  97. /* modes */
  98. rt_uint8_t modes[PM_SLEEP_MODE_MAX];
  99. rt_uint8_t sleep_mode; /* current sleep mode */
  100. rt_uint8_t run_mode; /* current running mode */
  101. /* the list of device, which has PM feature */
  102. rt_uint8_t device_pm_number;
  103. struct rt_device_pm *device_pm;
  104. /* if the mode has timer, the corresponding bit is 1*/
  105. rt_uint8_t timer_mask;
  106. rt_uint8_t flags;
  107. const struct rt_pm_ops *ops;
  108. };
  109. enum
  110. {
  111. RT_PM_ENTER_SLEEP = 0,
  112. RT_PM_EXIT_SLEEP,
  113. };
  114. struct rt_pm_notify
  115. {
  116. void (*notify)(rt_uint8_t event, rt_uint8_t mode, void *data);
  117. void *data;
  118. };
  119. void rt_pm_request(rt_uint8_t sleep_mode);
  120. void rt_pm_release(rt_uint8_t sleep_mode);
  121. int rt_pm_run_enter(rt_uint8_t run_mode);
  122. void rt_pm_device_register(struct rt_device *device, const struct rt_device_pm_ops *ops);
  123. void rt_pm_device_unregister(struct rt_device *device);
  124. void rt_pm_notify_set(void (*notify)(rt_uint8_t event, rt_uint8_t mode, void *data), void *data);
  125. void rt_pm_default_set(rt_uint8_t sleep_mode);
  126. void rt_system_pm_init(const struct rt_pm_ops *ops,
  127. rt_uint8_t timer_mask,
  128. void *user_data);
  129. #endif /* __PM_H__ */