pm_wakeup.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * pm_wakeup.h - Power management wakeup interface
  4. *
  5. * Copyright (C) 2008 Alan Stern
  6. * Copyright (C) 2010 Rafael J. Wysocki, Novell Inc.
  7. */
  8. #ifndef _LINUX_PM_WAKEUP_H
  9. #define _LINUX_PM_WAKEUP_H
  10. #ifndef _DEVICE_H_
  11. # error "please don't include this file directly"
  12. #endif
  13. #include <linux/types.h>
  14. struct wake_irq;
  15. /**
  16. * struct wakeup_source - Representation of wakeup sources
  17. *
  18. * @name: Name of the wakeup source
  19. * @id: Wakeup source id
  20. * @entry: Wakeup source list entry
  21. * @lock: Wakeup source lock
  22. * @wakeirq: Optional device specific wakeirq
  23. * @timer: Wakeup timer list
  24. * @timer_expires: Wakeup timer expiration
  25. * @total_time: Total time this wakeup source has been active.
  26. * @max_time: Maximum time this wakeup source has been continuously active.
  27. * @last_time: Monotonic clock when the wakeup source's was touched last time.
  28. * @prevent_sleep_time: Total time this source has been preventing autosleep.
  29. * @event_count: Number of signaled wakeup events.
  30. * @active_count: Number of times the wakeup source was activated.
  31. * @relax_count: Number of times the wakeup source was deactivated.
  32. * @expire_count: Number of times the wakeup source's timeout has expired.
  33. * @wakeup_count: Number of times the wakeup source might abort suspend.
  34. * @dev: Struct device for sysfs statistics about the wakeup source.
  35. * @active: Status of the wakeup source.
  36. * @autosleep_enabled: Autosleep is active, so update @prevent_sleep_time.
  37. */
  38. struct wakeup_source {
  39. const char *name;
  40. int id;
  41. struct list_head entry;
  42. spinlock_t lock;
  43. struct wake_irq *wakeirq;
  44. struct timer_list timer;
  45. unsigned long timer_expires;
  46. ktime_t total_time;
  47. ktime_t max_time;
  48. ktime_t last_time;
  49. ktime_t start_prevent_time;
  50. ktime_t prevent_sleep_time;
  51. unsigned long event_count;
  52. unsigned long active_count;
  53. unsigned long relax_count;
  54. unsigned long expire_count;
  55. unsigned long wakeup_count;
  56. struct device *dev;
  57. bool active:1;
  58. bool autosleep_enabled:1;
  59. };
  60. #define for_each_wakeup_source(ws) \
  61. for ((ws) = wakeup_sources_walk_start(); \
  62. (ws); \
  63. (ws) = wakeup_sources_walk_next((ws)))
  64. #ifdef CONFIG_PM_SLEEP
  65. /*
  66. * Changes to device_may_wakeup take effect on the next pm state change.
  67. */
  68. static inline bool device_can_wakeup(struct device *dev)
  69. {
  70. return dev->power.can_wakeup;
  71. }
  72. static inline bool device_may_wakeup(struct device *dev)
  73. {
  74. return dev->power.can_wakeup && !!dev->power.wakeup;
  75. }
  76. static inline bool device_wakeup_path(struct device *dev)
  77. {
  78. return dev->power.wakeup_path;
  79. }
  80. static inline void device_set_wakeup_path(struct device *dev)
  81. {
  82. dev->power.wakeup_path = true;
  83. }
  84. /* drivers/base/power/wakeup.c */
  85. extern struct wakeup_source *wakeup_source_create(const char *name);
  86. extern void wakeup_source_destroy(struct wakeup_source *ws);
  87. extern void wakeup_source_add(struct wakeup_source *ws);
  88. extern void wakeup_source_remove(struct wakeup_source *ws);
  89. extern struct wakeup_source *wakeup_source_register(struct device *dev,
  90. const char *name);
  91. extern void wakeup_source_unregister(struct wakeup_source *ws);
  92. extern int wakeup_sources_read_lock(void);
  93. extern void wakeup_sources_read_unlock(int idx);
  94. extern struct wakeup_source *wakeup_sources_walk_start(void);
  95. extern struct wakeup_source *wakeup_sources_walk_next(struct wakeup_source *ws);
  96. extern int device_wakeup_enable(struct device *dev);
  97. extern int device_wakeup_disable(struct device *dev);
  98. extern void device_set_wakeup_capable(struct device *dev, bool capable);
  99. extern int device_set_wakeup_enable(struct device *dev, bool enable);
  100. extern void __pm_stay_awake(struct wakeup_source *ws);
  101. extern void pm_stay_awake(struct device *dev);
  102. extern void __pm_relax(struct wakeup_source *ws);
  103. extern void pm_relax(struct device *dev);
  104. extern void pm_wakeup_ws_event(struct wakeup_source *ws, unsigned int msec, bool hard);
  105. extern void pm_wakeup_dev_event(struct device *dev, unsigned int msec, bool hard);
  106. #else /* !CONFIG_PM_SLEEP */
  107. static inline void device_set_wakeup_capable(struct device *dev, bool capable)
  108. {
  109. dev->power.can_wakeup = capable;
  110. }
  111. static inline bool device_can_wakeup(struct device *dev)
  112. {
  113. return dev->power.can_wakeup;
  114. }
  115. static inline struct wakeup_source *wakeup_source_create(const char *name)
  116. {
  117. return NULL;
  118. }
  119. static inline void wakeup_source_destroy(struct wakeup_source *ws) {}
  120. static inline void wakeup_source_add(struct wakeup_source *ws) {}
  121. static inline void wakeup_source_remove(struct wakeup_source *ws) {}
  122. static inline struct wakeup_source *wakeup_source_register(struct device *dev,
  123. const char *name)
  124. {
  125. return NULL;
  126. }
  127. static inline void wakeup_source_unregister(struct wakeup_source *ws) {}
  128. static inline int device_wakeup_enable(struct device *dev)
  129. {
  130. dev->power.should_wakeup = true;
  131. return 0;
  132. }
  133. static inline int device_wakeup_disable(struct device *dev)
  134. {
  135. dev->power.should_wakeup = false;
  136. return 0;
  137. }
  138. static inline int device_set_wakeup_enable(struct device *dev, bool enable)
  139. {
  140. dev->power.should_wakeup = enable;
  141. return 0;
  142. }
  143. static inline bool device_may_wakeup(struct device *dev)
  144. {
  145. return dev->power.can_wakeup && dev->power.should_wakeup;
  146. }
  147. static inline bool device_wakeup_path(struct device *dev)
  148. {
  149. return false;
  150. }
  151. static inline void device_set_wakeup_path(struct device *dev) {}
  152. static inline void __pm_stay_awake(struct wakeup_source *ws) {}
  153. static inline void pm_stay_awake(struct device *dev) {}
  154. static inline void __pm_relax(struct wakeup_source *ws) {}
  155. static inline void pm_relax(struct device *dev) {}
  156. static inline void pm_wakeup_ws_event(struct wakeup_source *ws,
  157. unsigned int msec, bool hard) {}
  158. static inline void pm_wakeup_dev_event(struct device *dev, unsigned int msec,
  159. bool hard) {}
  160. #endif /* !CONFIG_PM_SLEEP */
  161. static inline void __pm_wakeup_event(struct wakeup_source *ws, unsigned int msec)
  162. {
  163. return pm_wakeup_ws_event(ws, msec, false);
  164. }
  165. static inline void pm_wakeup_event(struct device *dev, unsigned int msec)
  166. {
  167. return pm_wakeup_dev_event(dev, msec, false);
  168. }
  169. static inline void pm_wakeup_hard_event(struct device *dev)
  170. {
  171. return pm_wakeup_dev_event(dev, 0, true);
  172. }
  173. /**
  174. * device_init_wakeup - Device wakeup initialization.
  175. * @dev: Device to handle.
  176. * @enable: Whether or not to enable @dev as a wakeup device.
  177. *
  178. * By default, most devices should leave wakeup disabled. The exceptions are
  179. * devices that everyone expects to be wakeup sources: keyboards, power buttons,
  180. * possibly network interfaces, etc. Also, devices that don't generate their
  181. * own wakeup requests but merely forward requests from one bus to another
  182. * (like PCI bridges) should have wakeup enabled by default.
  183. */
  184. static inline int device_init_wakeup(struct device *dev, bool enable)
  185. {
  186. if (enable) {
  187. device_set_wakeup_capable(dev, true);
  188. return device_wakeup_enable(dev);
  189. } else {
  190. device_wakeup_disable(dev);
  191. device_set_wakeup_capable(dev, false);
  192. return 0;
  193. }
  194. }
  195. #endif /* _LINUX_PM_WAKEUP_H */