xref: /openbmc/linux/drivers/base/power/wakeirq.c (revision e3b9f1e8)
1 /*
2  * wakeirq.c - Device wakeirq helper functions
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed "as is" WITHOUT ANY WARRANTY of any
9  * kind, whether express or implied; without even the implied warranty
10  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  */
13 
14 #include <linux/device.h>
15 #include <linux/interrupt.h>
16 #include <linux/irq.h>
17 #include <linux/slab.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/pm_wakeirq.h>
20 
21 #include "power.h"
22 
23 /**
24  * dev_pm_attach_wake_irq - Attach device interrupt as a wake IRQ
25  * @dev: Device entry
26  * @irq: Device wake-up capable interrupt
27  * @wirq: Wake irq specific data
28  *
29  * Internal function to attach either a device IO interrupt or a
30  * dedicated wake-up interrupt as a wake IRQ.
31  */
32 static int dev_pm_attach_wake_irq(struct device *dev, int irq,
33 				  struct wake_irq *wirq)
34 {
35 	unsigned long flags;
36 
37 	if (!dev || !wirq)
38 		return -EINVAL;
39 
40 	spin_lock_irqsave(&dev->power.lock, flags);
41 	if (dev_WARN_ONCE(dev, dev->power.wakeirq,
42 			  "wake irq already initialized\n")) {
43 		spin_unlock_irqrestore(&dev->power.lock, flags);
44 		return -EEXIST;
45 	}
46 
47 	dev->power.wakeirq = wirq;
48 	device_wakeup_attach_irq(dev, wirq);
49 
50 	spin_unlock_irqrestore(&dev->power.lock, flags);
51 	return 0;
52 }
53 
54 /**
55  * dev_pm_set_wake_irq - Attach device IO interrupt as wake IRQ
56  * @dev: Device entry
57  * @irq: Device IO interrupt
58  *
59  * Attach a device IO interrupt as a wake IRQ. The wake IRQ gets
60  * automatically configured for wake-up from suspend  based
61  * on the device specific sysfs wakeup entry. Typically called
62  * during driver probe after calling device_init_wakeup().
63  */
64 int dev_pm_set_wake_irq(struct device *dev, int irq)
65 {
66 	struct wake_irq *wirq;
67 	int err;
68 
69 	if (irq < 0)
70 		return -EINVAL;
71 
72 	wirq = kzalloc(sizeof(*wirq), GFP_KERNEL);
73 	if (!wirq)
74 		return -ENOMEM;
75 
76 	wirq->dev = dev;
77 	wirq->irq = irq;
78 
79 	err = dev_pm_attach_wake_irq(dev, irq, wirq);
80 	if (err)
81 		kfree(wirq);
82 
83 	return err;
84 }
85 EXPORT_SYMBOL_GPL(dev_pm_set_wake_irq);
86 
87 /**
88  * dev_pm_clear_wake_irq - Detach a device IO interrupt wake IRQ
89  * @dev: Device entry
90  *
91  * Detach a device wake IRQ and free resources.
92  *
93  * Note that it's OK for drivers to call this without calling
94  * dev_pm_set_wake_irq() as all the driver instances may not have
95  * a wake IRQ configured. This avoid adding wake IRQ specific
96  * checks into the drivers.
97  */
98 void dev_pm_clear_wake_irq(struct device *dev)
99 {
100 	struct wake_irq *wirq = dev->power.wakeirq;
101 	unsigned long flags;
102 
103 	if (!wirq)
104 		return;
105 
106 	spin_lock_irqsave(&dev->power.lock, flags);
107 	device_wakeup_detach_irq(dev);
108 	dev->power.wakeirq = NULL;
109 	spin_unlock_irqrestore(&dev->power.lock, flags);
110 
111 	if (wirq->status & WAKE_IRQ_DEDICATED_ALLOCATED) {
112 		free_irq(wirq->irq, wirq);
113 		wirq->status &= ~WAKE_IRQ_DEDICATED_MASK;
114 	}
115 	kfree(wirq);
116 }
117 EXPORT_SYMBOL_GPL(dev_pm_clear_wake_irq);
118 
119 /**
120  * handle_threaded_wake_irq - Handler for dedicated wake-up interrupts
121  * @irq: Device specific dedicated wake-up interrupt
122  * @_wirq: Wake IRQ data
123  *
124  * Some devices have a separate wake-up interrupt in addition to the
125  * device IO interrupt. The wake-up interrupt signals that a device
126  * should be woken up from it's idle state. This handler uses device
127  * specific pm_runtime functions to wake the device, and then it's
128  * up to the device to do whatever it needs to. Note that as the
129  * device may need to restore context and start up regulators, we
130  * use a threaded IRQ.
131  *
132  * Also note that we are not resending the lost device interrupts.
133  * We assume that the wake-up interrupt just needs to wake-up the
134  * device, and then device's pm_runtime_resume() can deal with the
135  * situation.
136  */
137 static irqreturn_t handle_threaded_wake_irq(int irq, void *_wirq)
138 {
139 	struct wake_irq *wirq = _wirq;
140 	int res;
141 
142 	/* Maybe abort suspend? */
143 	if (irqd_is_wakeup_set(irq_get_irq_data(irq))) {
144 		pm_wakeup_event(wirq->dev, 0);
145 
146 		return IRQ_HANDLED;
147 	}
148 
149 	/* We don't want RPM_ASYNC or RPM_NOWAIT here */
150 	res = pm_runtime_resume(wirq->dev);
151 	if (res < 0)
152 		dev_warn(wirq->dev,
153 			 "wake IRQ with no resume: %i\n", res);
154 
155 	return IRQ_HANDLED;
156 }
157 
158 /**
159  * dev_pm_set_dedicated_wake_irq - Request a dedicated wake-up interrupt
160  * @dev: Device entry
161  * @irq: Device wake-up interrupt
162  *
163  * Unless your hardware has separate wake-up interrupts in addition
164  * to the device IO interrupts, you don't need this.
165  *
166  * Sets up a threaded interrupt handler for a device that has
167  * a dedicated wake-up interrupt in addition to the device IO
168  * interrupt.
169  *
170  * The interrupt starts disabled, and needs to be managed for
171  * the device by the bus code or the device driver using
172  * dev_pm_enable_wake_irq() and dev_pm_disable_wake_irq()
173  * functions.
174  */
175 int dev_pm_set_dedicated_wake_irq(struct device *dev, int irq)
176 {
177 	struct wake_irq *wirq;
178 	int err;
179 
180 	if (irq < 0)
181 		return -EINVAL;
182 
183 	wirq = kzalloc(sizeof(*wirq), GFP_KERNEL);
184 	if (!wirq)
185 		return -ENOMEM;
186 
187 	wirq->dev = dev;
188 	wirq->irq = irq;
189 	irq_set_status_flags(irq, IRQ_NOAUTOEN);
190 
191 	/* Prevent deferred spurious wakeirqs with disable_irq_nosync() */
192 	irq_set_status_flags(irq, IRQ_DISABLE_UNLAZY);
193 
194 	/*
195 	 * Consumer device may need to power up and restore state
196 	 * so we use a threaded irq.
197 	 */
198 	err = request_threaded_irq(irq, NULL, handle_threaded_wake_irq,
199 				   IRQF_ONESHOT, dev_name(dev), wirq);
200 	if (err)
201 		goto err_free;
202 
203 	err = dev_pm_attach_wake_irq(dev, irq, wirq);
204 	if (err)
205 		goto err_free_irq;
206 
207 	wirq->status = WAKE_IRQ_DEDICATED_ALLOCATED;
208 
209 	return err;
210 
211 err_free_irq:
212 	free_irq(irq, wirq);
213 err_free:
214 	kfree(wirq);
215 
216 	return err;
217 }
218 EXPORT_SYMBOL_GPL(dev_pm_set_dedicated_wake_irq);
219 
220 /**
221  * dev_pm_enable_wake_irq - Enable device wake-up interrupt
222  * @dev: Device
223  *
224  * Optionally called from the bus code or the device driver for
225  * runtime_resume() to override the PM runtime core managed wake-up
226  * interrupt handling to enable the wake-up interrupt.
227  *
228  * Note that for runtime_suspend()) the wake-up interrupts
229  * should be unconditionally enabled unlike for suspend()
230  * that is conditional.
231  */
232 void dev_pm_enable_wake_irq(struct device *dev)
233 {
234 	struct wake_irq *wirq = dev->power.wakeirq;
235 
236 	if (wirq && (wirq->status & WAKE_IRQ_DEDICATED_ALLOCATED))
237 		enable_irq(wirq->irq);
238 }
239 EXPORT_SYMBOL_GPL(dev_pm_enable_wake_irq);
240 
241 /**
242  * dev_pm_disable_wake_irq - Disable device wake-up interrupt
243  * @dev: Device
244  *
245  * Optionally called from the bus code or the device driver for
246  * runtime_suspend() to override the PM runtime core managed wake-up
247  * interrupt handling to disable the wake-up interrupt.
248  */
249 void dev_pm_disable_wake_irq(struct device *dev)
250 {
251 	struct wake_irq *wirq = dev->power.wakeirq;
252 
253 	if (wirq && (wirq->status & WAKE_IRQ_DEDICATED_ALLOCATED))
254 		disable_irq_nosync(wirq->irq);
255 }
256 EXPORT_SYMBOL_GPL(dev_pm_disable_wake_irq);
257 
258 /**
259  * dev_pm_enable_wake_irq_check - Checks and enables wake-up interrupt
260  * @dev: Device
261  * @can_change_status: Can change wake-up interrupt status
262  *
263  * Enables wakeirq conditionally. We need to enable wake-up interrupt
264  * lazily on the first rpm_suspend(). This is needed as the consumer device
265  * starts in RPM_SUSPENDED state, and the the first pm_runtime_get() would
266  * otherwise try to disable already disabled wakeirq. The wake-up interrupt
267  * starts disabled with IRQ_NOAUTOEN set.
268  *
269  * Should be only called from rpm_suspend() and rpm_resume() path.
270  * Caller must hold &dev->power.lock to change wirq->status
271  */
272 void dev_pm_enable_wake_irq_check(struct device *dev,
273 				  bool can_change_status)
274 {
275 	struct wake_irq *wirq = dev->power.wakeirq;
276 
277 	if (!wirq || !((wirq->status & WAKE_IRQ_DEDICATED_MASK)))
278 		return;
279 
280 	if (likely(wirq->status & WAKE_IRQ_DEDICATED_MANAGED)) {
281 		goto enable;
282 	} else if (can_change_status) {
283 		wirq->status |= WAKE_IRQ_DEDICATED_MANAGED;
284 		goto enable;
285 	}
286 
287 	return;
288 
289 enable:
290 	enable_irq(wirq->irq);
291 }
292 
293 /**
294  * dev_pm_disable_wake_irq_check - Checks and disables wake-up interrupt
295  * @dev: Device
296  *
297  * Disables wake-up interrupt conditionally based on status.
298  * Should be only called from rpm_suspend() and rpm_resume() path.
299  */
300 void dev_pm_disable_wake_irq_check(struct device *dev)
301 {
302 	struct wake_irq *wirq = dev->power.wakeirq;
303 
304 	if (!wirq || !((wirq->status & WAKE_IRQ_DEDICATED_MASK)))
305 		return;
306 
307 	if (wirq->status & WAKE_IRQ_DEDICATED_MANAGED)
308 		disable_irq_nosync(wirq->irq);
309 }
310 
311 /**
312  * dev_pm_arm_wake_irq - Arm device wake-up
313  * @wirq: Device wake-up interrupt
314  *
315  * Sets up the wake-up event conditionally based on the
316  * device_may_wake().
317  */
318 void dev_pm_arm_wake_irq(struct wake_irq *wirq)
319 {
320 	if (!wirq)
321 		return;
322 
323 	if (device_may_wakeup(wirq->dev)) {
324 		if (wirq->status & WAKE_IRQ_DEDICATED_ALLOCATED &&
325 		    !pm_runtime_status_suspended(wirq->dev))
326 			enable_irq(wirq->irq);
327 
328 		enable_irq_wake(wirq->irq);
329 	}
330 }
331 
332 /**
333  * dev_pm_disarm_wake_irq - Disarm device wake-up
334  * @wirq: Device wake-up interrupt
335  *
336  * Clears up the wake-up event conditionally based on the
337  * device_may_wake().
338  */
339 void dev_pm_disarm_wake_irq(struct wake_irq *wirq)
340 {
341 	if (!wirq)
342 		return;
343 
344 	if (device_may_wakeup(wirq->dev)) {
345 		disable_irq_wake(wirq->irq);
346 
347 		if (wirq->status & WAKE_IRQ_DEDICATED_ALLOCATED &&
348 		    !pm_runtime_status_suspended(wirq->dev))
349 			disable_irq_nosync(wirq->irq);
350 	}
351 }
352