xref: /linux/include/linux/pm_wakeup.h (revision 52206f82d9244546e5790f5ad64465343aa7ffd5)
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 
9 #ifndef _LINUX_PM_WAKEUP_H
10 #define _LINUX_PM_WAKEUP_H
11 
12 #ifndef _DEVICE_H_
13 # error "Please do not include this file directly."
14 #endif
15 
16 #include <linux/types.h>
17 
18 struct wake_irq;
19 
20 /**
21  * struct wakeup_source - Representation of wakeup sources
22  *
23  * @name: Name of the wakeup source
24  * @id: Wakeup source id
25  * @entry: Wakeup source list entry
26  * @lock: Wakeup source lock
27  * @wakeirq: Optional device specific wakeirq
28  * @timer: Wakeup timer list
29  * @timer_expires: Wakeup timer expiration
30  * @total_time: Total time this wakeup source has been active.
31  * @max_time: Maximum time this wakeup source has been continuously active.
32  * @last_time: Monotonic clock when the wakeup source's was touched last time.
33  * @prevent_sleep_time: Total time this source has been preventing autosleep.
34  * @event_count: Number of signaled wakeup events.
35  * @active_count: Number of times the wakeup source was activated.
36  * @relax_count: Number of times the wakeup source was deactivated.
37  * @expire_count: Number of times the wakeup source's timeout has expired.
38  * @wakeup_count: Number of times the wakeup source might abort suspend.
39  * @dev: Struct device for sysfs statistics about the wakeup source.
40  * @active: Status of the wakeup source.
41  * @autosleep_enabled: Autosleep is active, so update @prevent_sleep_time.
42  */
43 struct wakeup_source {
44 	const char 		*name;
45 	int			id;
46 	struct list_head	entry;
47 	spinlock_t		lock;
48 	struct wake_irq		*wakeirq;
49 	struct timer_list	timer;
50 	unsigned long		timer_expires;
51 	ktime_t total_time;
52 	ktime_t max_time;
53 	ktime_t last_time;
54 	ktime_t start_prevent_time;
55 	ktime_t prevent_sleep_time;
56 	unsigned long		event_count;
57 	unsigned long		active_count;
58 	unsigned long		relax_count;
59 	unsigned long		expire_count;
60 	unsigned long		wakeup_count;
61 	struct device		*dev;
62 	bool			active:1;
63 	bool			autosleep_enabled:1;
64 };
65 
66 #define for_each_wakeup_source(ws) \
67 	for ((ws) = wakeup_sources_walk_start();	\
68 	     (ws);					\
69 	     (ws) = wakeup_sources_walk_next((ws)))
70 
71 #ifdef CONFIG_PM_SLEEP
72 
73 /*
74  * Changes to device_may_wakeup take effect on the next pm state change.
75  */
76 
77 static inline bool device_can_wakeup(struct device *dev)
78 {
79 	return dev->power.can_wakeup;
80 }
81 
82 static inline bool device_may_wakeup(struct device *dev)
83 {
84 	return dev->power.can_wakeup && !!dev->power.wakeup;
85 }
86 
87 static inline bool device_wakeup_path(struct device *dev)
88 {
89 	return dev->power.wakeup_path;
90 }
91 
92 static inline void device_set_wakeup_path(struct device *dev)
93 {
94 	dev->power.wakeup_path = true;
95 }
96 
97 static inline void device_set_out_band_wakeup(struct device *dev)
98 {
99 	dev->power.out_band_wakeup = true;
100 }
101 
102 static inline bool device_out_band_wakeup(struct device *dev)
103 {
104 	return dev->power.out_band_wakeup;
105 }
106 
107 /* drivers/base/power/wakeup.c */
108 extern struct wakeup_source *wakeup_source_register(struct device *dev,
109 						    const char *name);
110 extern void wakeup_source_unregister(struct wakeup_source *ws);
111 extern int wakeup_sources_read_lock(void);
112 extern void wakeup_sources_read_unlock(int idx);
113 extern struct wakeup_source *wakeup_sources_walk_start(void);
114 extern struct wakeup_source *wakeup_sources_walk_next(struct wakeup_source *ws);
115 extern int device_wakeup_enable(struct device *dev);
116 extern void device_wakeup_disable(struct device *dev);
117 extern void device_set_wakeup_capable(struct device *dev, bool capable);
118 extern int device_set_wakeup_enable(struct device *dev, bool enable);
119 extern void __pm_stay_awake(struct wakeup_source *ws);
120 extern void pm_stay_awake(struct device *dev);
121 extern void __pm_relax(struct wakeup_source *ws);
122 extern void pm_relax(struct device *dev);
123 extern void pm_wakeup_ws_event(struct wakeup_source *ws, unsigned int msec, bool hard);
124 extern void pm_wakeup_dev_event(struct device *dev, unsigned int msec, bool hard);
125 
126 #else /* !CONFIG_PM_SLEEP */
127 
128 static inline void device_set_wakeup_capable(struct device *dev, bool capable)
129 {
130 	dev->power.can_wakeup = capable;
131 }
132 
133 static inline bool device_can_wakeup(struct device *dev)
134 {
135 	return dev->power.can_wakeup;
136 }
137 
138 static inline struct wakeup_source *wakeup_source_register(struct device *dev,
139 							   const char *name)
140 {
141 	return NULL;
142 }
143 
144 static inline void wakeup_source_unregister(struct wakeup_source *ws) {}
145 
146 static inline int device_wakeup_enable(struct device *dev)
147 {
148 	dev->power.should_wakeup = true;
149 	return 0;
150 }
151 
152 static inline void device_wakeup_disable(struct device *dev)
153 {
154 	dev->power.should_wakeup = false;
155 }
156 
157 static inline int device_set_wakeup_enable(struct device *dev, bool enable)
158 {
159 	dev->power.should_wakeup = enable;
160 	return 0;
161 }
162 
163 static inline bool device_may_wakeup(struct device *dev)
164 {
165 	return dev->power.can_wakeup && dev->power.should_wakeup;
166 }
167 
168 static inline bool device_wakeup_path(struct device *dev)
169 {
170 	return false;
171 }
172 
173 static inline void device_set_wakeup_path(struct device *dev) {}
174 
175 static inline void device_set_out_band_wakeup(struct device *dev) {}
176 
177 static inline bool device_out_band_wakeup(struct device *dev)
178 {
179 	return false;
180 }
181 
182 static inline void __pm_stay_awake(struct wakeup_source *ws) {}
183 
184 static inline void pm_stay_awake(struct device *dev) {}
185 
186 static inline void __pm_relax(struct wakeup_source *ws) {}
187 
188 static inline void pm_relax(struct device *dev) {}
189 
190 static inline void pm_wakeup_ws_event(struct wakeup_source *ws,
191 				      unsigned int msec, bool hard) {}
192 
193 static inline void pm_wakeup_dev_event(struct device *dev, unsigned int msec,
194 				       bool hard) {}
195 
196 #endif /* !CONFIG_PM_SLEEP */
197 
198 static inline bool device_awake_path(struct device *dev)
199 {
200 	return device_wakeup_path(dev);
201 }
202 
203 static inline void device_set_awake_path(struct device *dev)
204 {
205 	device_set_wakeup_path(dev);
206 }
207 
208 static inline void __pm_wakeup_event(struct wakeup_source *ws, unsigned int msec)
209 {
210 	pm_wakeup_ws_event(ws, msec, false);
211 }
212 
213 static inline void pm_wakeup_event(struct device *dev, unsigned int msec)
214 {
215 	pm_wakeup_dev_event(dev, msec, false);
216 }
217 
218 static inline void pm_wakeup_hard_event(struct device *dev)
219 {
220 	pm_wakeup_dev_event(dev, 0, true);
221 }
222 
223 /**
224  * device_init_wakeup - Device wakeup initialization.
225  * @dev: Device to handle.
226  * @enable: Whether or not to enable @dev as a wakeup device.
227  *
228  * By default, most devices should leave wakeup disabled.  The exceptions are
229  * devices that everyone expects to be wakeup sources: keyboards, power buttons,
230  * possibly network interfaces, etc.  Also, devices that don't generate their
231  * own wakeup requests but merely forward requests from one bus to another
232  * (like PCI bridges) should have wakeup enabled by default.
233  */
234 static inline int device_init_wakeup(struct device *dev, bool enable)
235 {
236 	if (enable) {
237 		device_set_wakeup_capable(dev, true);
238 		return device_wakeup_enable(dev);
239 	}
240 	device_wakeup_disable(dev);
241 	device_set_wakeup_capable(dev, false);
242 	return 0;
243 }
244 
245 static void device_disable_wakeup(void *dev)
246 {
247 	device_init_wakeup(dev, false);
248 }
249 
250 /**
251  * devm_device_init_wakeup - Resource managed device wakeup initialization.
252  * @dev: Device to handle.
253  *
254  * This function is the devm managed version of device_init_wakeup(dev, true).
255  */
256 static inline int devm_device_init_wakeup(struct device *dev)
257 {
258 	device_init_wakeup(dev, true);
259 	return devm_add_action_or_reset(dev, device_disable_wakeup, dev);
260 }
261 
262 #endif /* _LINUX_PM_WAKEUP_H */
263