Merge remote-tracking branches 'regulator/fix/88pm800', 'regulator/fix/max8973',...
[linux-drm-fsl-dcu.git] / drivers / base / power / wakeirq.c
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         int err;
37
38         if (!dev || !wirq)
39                 return -EINVAL;
40
41         spin_lock_irqsave(&dev->power.lock, flags);
42         if (dev_WARN_ONCE(dev, dev->power.wakeirq,
43                           "wake irq already initialized\n")) {
44                 spin_unlock_irqrestore(&dev->power.lock, flags);
45                 return -EEXIST;
46         }
47
48         err = device_wakeup_attach_irq(dev, wirq);
49         if (!err)
50                 dev->power.wakeirq = wirq;
51
52         spin_unlock_irqrestore(&dev->power.lock, flags);
53         return err;
54 }
55
56 /**
57  * dev_pm_set_wake_irq - Attach device IO interrupt as wake IRQ
58  * @dev: Device entry
59  * @irq: Device IO interrupt
60  *
61  * Attach a device IO interrupt as a wake IRQ. The wake IRQ gets
62  * automatically configured for wake-up from suspend  based
63  * on the device specific sysfs wakeup entry. Typically called
64  * during driver probe after calling device_init_wakeup().
65  */
66 int dev_pm_set_wake_irq(struct device *dev, int irq)
67 {
68         struct wake_irq *wirq;
69         int err;
70
71         wirq = kzalloc(sizeof(*wirq), GFP_KERNEL);
72         if (!wirq)
73                 return -ENOMEM;
74
75         wirq->dev = dev;
76         wirq->irq = irq;
77
78         err = dev_pm_attach_wake_irq(dev, irq, wirq);
79         if (err)
80                 kfree(wirq);
81
82         return err;
83 }
84 EXPORT_SYMBOL_GPL(dev_pm_set_wake_irq);
85
86 /**
87  * dev_pm_clear_wake_irq - Detach a device IO interrupt wake IRQ
88  * @dev: Device entry
89  *
90  * Detach a device wake IRQ and free resources.
91  *
92  * Note that it's OK for drivers to call this without calling
93  * dev_pm_set_wake_irq() as all the driver instances may not have
94  * a wake IRQ configured. This avoid adding wake IRQ specific
95  * checks into the drivers.
96  */
97 void dev_pm_clear_wake_irq(struct device *dev)
98 {
99         struct wake_irq *wirq = dev->power.wakeirq;
100         unsigned long flags;
101
102         if (!wirq)
103                 return;
104
105         spin_lock_irqsave(&dev->power.lock, flags);
106         device_wakeup_detach_irq(dev);
107         dev->power.wakeirq = NULL;
108         spin_unlock_irqrestore(&dev->power.lock, flags);
109
110         if (wirq->dedicated_irq)
111                 free_irq(wirq->irq, wirq);
112         kfree(wirq);
113 }
114 EXPORT_SYMBOL_GPL(dev_pm_clear_wake_irq);
115
116 /**
117  * handle_threaded_wake_irq - Handler for dedicated wake-up interrupts
118  * @irq: Device specific dedicated wake-up interrupt
119  * @_wirq: Wake IRQ data
120  *
121  * Some devices have a separate wake-up interrupt in addition to the
122  * device IO interrupt. The wake-up interrupt signals that a device
123  * should be woken up from it's idle state. This handler uses device
124  * specific pm_runtime functions to wake the device, and then it's
125  * up to the device to do whatever it needs to. Note that as the
126  * device may need to restore context and start up regulators, we
127  * use a threaded IRQ.
128  *
129  * Also note that we are not resending the lost device interrupts.
130  * We assume that the wake-up interrupt just needs to wake-up the
131  * device, and then device's pm_runtime_resume() can deal with the
132  * situation.
133  */
134 static irqreturn_t handle_threaded_wake_irq(int irq, void *_wirq)
135 {
136         struct wake_irq *wirq = _wirq;
137         int res;
138
139         /* We don't want RPM_ASYNC or RPM_NOWAIT here */
140         res = pm_runtime_resume(wirq->dev);
141         if (res < 0)
142                 dev_warn(wirq->dev,
143                          "wake IRQ with no resume: %i\n", res);
144
145         return IRQ_HANDLED;
146 }
147
148 /**
149  * dev_pm_set_dedicated_wake_irq - Request a dedicated wake-up interrupt
150  * @dev: Device entry
151  * @irq: Device wake-up interrupt
152  *
153  * Unless your hardware has separate wake-up interrupts in addition
154  * to the device IO interrupts, you don't need this.
155  *
156  * Sets up a threaded interrupt handler for a device that has
157  * a dedicated wake-up interrupt in addition to the device IO
158  * interrupt.
159  *
160  * The interrupt starts disabled, and needs to be managed for
161  * the device by the bus code or the device driver using
162  * dev_pm_enable_wake_irq() and dev_pm_disable_wake_irq()
163  * functions.
164  */
165 int dev_pm_set_dedicated_wake_irq(struct device *dev, int irq)
166 {
167         struct wake_irq *wirq;
168         int err;
169
170         wirq = kzalloc(sizeof(*wirq), GFP_KERNEL);
171         if (!wirq)
172                 return -ENOMEM;
173
174         wirq->dev = dev;
175         wirq->irq = irq;
176         wirq->dedicated_irq = true;
177         irq_set_status_flags(irq, IRQ_NOAUTOEN);
178
179         /*
180          * Consumer device may need to power up and restore state
181          * so we use a threaded irq.
182          */
183         err = request_threaded_irq(irq, NULL, handle_threaded_wake_irq,
184                                    IRQF_ONESHOT, dev_name(dev), wirq);
185         if (err)
186                 goto err_free;
187
188         err = dev_pm_attach_wake_irq(dev, irq, wirq);
189         if (err)
190                 goto err_free_irq;
191
192         return err;
193
194 err_free_irq:
195         free_irq(irq, wirq);
196 err_free:
197         kfree(wirq);
198
199         return err;
200 }
201 EXPORT_SYMBOL_GPL(dev_pm_set_dedicated_wake_irq);
202
203 /**
204  * dev_pm_enable_wake_irq - Enable device wake-up interrupt
205  * @dev: Device
206  *
207  * Called from the bus code or the device driver for
208  * runtime_suspend() to enable the wake-up interrupt while
209  * the device is running.
210  *
211  * Note that for runtime_suspend()) the wake-up interrupts
212  * should be unconditionally enabled unlike for suspend()
213  * that is conditional.
214  */
215 void dev_pm_enable_wake_irq(struct device *dev)
216 {
217         struct wake_irq *wirq = dev->power.wakeirq;
218
219         if (wirq && wirq->dedicated_irq)
220                 enable_irq(wirq->irq);
221 }
222 EXPORT_SYMBOL_GPL(dev_pm_enable_wake_irq);
223
224 /**
225  * dev_pm_disable_wake_irq - Disable device wake-up interrupt
226  * @dev: Device
227  *
228  * Called from the bus code or the device driver for
229  * runtime_resume() to disable the wake-up interrupt while
230  * the device is running.
231  */
232 void dev_pm_disable_wake_irq(struct device *dev)
233 {
234         struct wake_irq *wirq = dev->power.wakeirq;
235
236         if (wirq && wirq->dedicated_irq)
237                 disable_irq_nosync(wirq->irq);
238 }
239 EXPORT_SYMBOL_GPL(dev_pm_disable_wake_irq);
240
241 /**
242  * dev_pm_arm_wake_irq - Arm device wake-up
243  * @wirq: Device wake-up interrupt
244  *
245  * Sets up the wake-up event conditionally based on the
246  * device_may_wake().
247  */
248 void dev_pm_arm_wake_irq(struct wake_irq *wirq)
249 {
250         if (!wirq)
251                 return;
252
253         if (device_may_wakeup(wirq->dev))
254                 enable_irq_wake(wirq->irq);
255 }
256
257 /**
258  * dev_pm_disarm_wake_irq - Disarm device wake-up
259  * @wirq: Device wake-up interrupt
260  *
261  * Clears up the wake-up event conditionally based on the
262  * device_may_wake().
263  */
264 void dev_pm_disarm_wake_irq(struct wake_irq *wirq)
265 {
266         if (!wirq)
267                 return;
268
269         if (device_may_wakeup(wirq->dev))
270                 disable_irq_wake(wirq->irq);
271 }