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 if (irq < 0) 72 return -EINVAL; 73 74 wirq = kzalloc(sizeof(*wirq), GFP_KERNEL); 75 if (!wirq) 76 return -ENOMEM; 77 78 wirq->dev = dev; 79 wirq->irq = irq; 80 81 err = dev_pm_attach_wake_irq(dev, irq, wirq); 82 if (err) 83 kfree(wirq); 84 85 return err; 86 } 87 EXPORT_SYMBOL_GPL(dev_pm_set_wake_irq); 88 89 /** 90 * dev_pm_clear_wake_irq - Detach a device IO interrupt wake IRQ 91 * @dev: Device entry 92 * 93 * Detach a device wake IRQ and free resources. 94 * 95 * Note that it's OK for drivers to call this without calling 96 * dev_pm_set_wake_irq() as all the driver instances may not have 97 * a wake IRQ configured. This avoid adding wake IRQ specific 98 * checks into the drivers. 99 */ 100 void dev_pm_clear_wake_irq(struct device *dev) 101 { 102 struct wake_irq *wirq = dev->power.wakeirq; 103 unsigned long flags; 104 105 if (!wirq) 106 return; 107 108 spin_lock_irqsave(&dev->power.lock, flags); 109 device_wakeup_detach_irq(dev); 110 dev->power.wakeirq = NULL; 111 spin_unlock_irqrestore(&dev->power.lock, flags); 112 113 if (wirq->status & WAKE_IRQ_DEDICATED_ALLOCATED) { 114 free_irq(wirq->irq, wirq); 115 wirq->status &= ~WAKE_IRQ_DEDICATED_MASK; 116 } 117 kfree(wirq); 118 } 119 EXPORT_SYMBOL_GPL(dev_pm_clear_wake_irq); 120 121 /** 122 * handle_threaded_wake_irq - Handler for dedicated wake-up interrupts 123 * @irq: Device specific dedicated wake-up interrupt 124 * @_wirq: Wake IRQ data 125 * 126 * Some devices have a separate wake-up interrupt in addition to the 127 * device IO interrupt. The wake-up interrupt signals that a device 128 * should be woken up from it's idle state. This handler uses device 129 * specific pm_runtime functions to wake the device, and then it's 130 * up to the device to do whatever it needs to. Note that as the 131 * device may need to restore context and start up regulators, we 132 * use a threaded IRQ. 133 * 134 * Also note that we are not resending the lost device interrupts. 135 * We assume that the wake-up interrupt just needs to wake-up the 136 * device, and then device's pm_runtime_resume() can deal with the 137 * situation. 138 */ 139 static irqreturn_t handle_threaded_wake_irq(int irq, void *_wirq) 140 { 141 struct wake_irq *wirq = _wirq; 142 int res; 143 144 /* Maybe abort suspend? */ 145 if (irqd_is_wakeup_set(irq_get_irq_data(irq))) { 146 pm_wakeup_event(wirq->dev, 0); 147 148 return IRQ_HANDLED; 149 } 150 151 /* We don't want RPM_ASYNC or RPM_NOWAIT here */ 152 res = pm_runtime_resume(wirq->dev); 153 if (res < 0) 154 dev_warn(wirq->dev, 155 "wake IRQ with no resume: %i\n", res); 156 157 return IRQ_HANDLED; 158 } 159 160 /** 161 * dev_pm_set_dedicated_wake_irq - Request a dedicated wake-up interrupt 162 * @dev: Device entry 163 * @irq: Device wake-up interrupt 164 * 165 * Unless your hardware has separate wake-up interrupts in addition 166 * to the device IO interrupts, you don't need this. 167 * 168 * Sets up a threaded interrupt handler for a device that has 169 * a dedicated wake-up interrupt in addition to the device IO 170 * interrupt. 171 * 172 * The interrupt starts disabled, and needs to be managed for 173 * the device by the bus code or the device driver using 174 * dev_pm_enable_wake_irq() and dev_pm_disable_wake_irq() 175 * functions. 176 */ 177 int dev_pm_set_dedicated_wake_irq(struct device *dev, int irq) 178 { 179 struct wake_irq *wirq; 180 int err; 181 182 if (irq < 0) 183 return -EINVAL; 184 185 wirq = kzalloc(sizeof(*wirq), GFP_KERNEL); 186 if (!wirq) 187 return -ENOMEM; 188 189 wirq->dev = dev; 190 wirq->irq = irq; 191 irq_set_status_flags(irq, IRQ_NOAUTOEN); 192 193 /* Prevent deferred spurious wakeirqs with disable_irq_nosync() */ 194 irq_set_status_flags(irq, IRQ_DISABLE_UNLAZY); 195 196 /* 197 * Consumer device may need to power up and restore state 198 * so we use a threaded irq. 199 */ 200 err = request_threaded_irq(irq, NULL, handle_threaded_wake_irq, 201 IRQF_ONESHOT, dev_name(dev), wirq); 202 if (err) 203 goto err_free; 204 205 err = dev_pm_attach_wake_irq(dev, irq, wirq); 206 if (err) 207 goto err_free_irq; 208 209 wirq->status = WAKE_IRQ_DEDICATED_ALLOCATED; 210 211 return err; 212 213 err_free_irq: 214 free_irq(irq, wirq); 215 err_free: 216 kfree(wirq); 217 218 return err; 219 } 220 EXPORT_SYMBOL_GPL(dev_pm_set_dedicated_wake_irq); 221 222 /** 223 * dev_pm_enable_wake_irq - Enable device wake-up interrupt 224 * @dev: Device 225 * 226 * Optionally called from the bus code or the device driver for 227 * runtime_resume() to override the PM runtime core managed wake-up 228 * interrupt handling to enable the wake-up interrupt. 229 * 230 * Note that for runtime_suspend()) the wake-up interrupts 231 * should be unconditionally enabled unlike for suspend() 232 * that is conditional. 233 */ 234 void dev_pm_enable_wake_irq(struct device *dev) 235 { 236 struct wake_irq *wirq = dev->power.wakeirq; 237 238 if (wirq && (wirq->status & WAKE_IRQ_DEDICATED_ALLOCATED)) 239 enable_irq(wirq->irq); 240 } 241 EXPORT_SYMBOL_GPL(dev_pm_enable_wake_irq); 242 243 /** 244 * dev_pm_disable_wake_irq - Disable device wake-up interrupt 245 * @dev: Device 246 * 247 * Optionally called from the bus code or the device driver for 248 * runtime_suspend() to override the PM runtime core managed wake-up 249 * interrupt handling to disable the wake-up interrupt. 250 */ 251 void dev_pm_disable_wake_irq(struct device *dev) 252 { 253 struct wake_irq *wirq = dev->power.wakeirq; 254 255 if (wirq && (wirq->status & WAKE_IRQ_DEDICATED_ALLOCATED)) 256 disable_irq_nosync(wirq->irq); 257 } 258 EXPORT_SYMBOL_GPL(dev_pm_disable_wake_irq); 259 260 /** 261 * dev_pm_enable_wake_irq_check - Checks and enables wake-up interrupt 262 * @dev: Device 263 * @can_change_status: Can change wake-up interrupt status 264 * 265 * Enables wakeirq conditionally. We need to enable wake-up interrupt 266 * lazily on the first rpm_suspend(). This is needed as the consumer device 267 * starts in RPM_SUSPENDED state, and the the first pm_runtime_get() would 268 * otherwise try to disable already disabled wakeirq. The wake-up interrupt 269 * starts disabled with IRQ_NOAUTOEN set. 270 * 271 * Should be only called from rpm_suspend() and rpm_resume() path. 272 * Caller must hold &dev->power.lock to change wirq->status 273 */ 274 void dev_pm_enable_wake_irq_check(struct device *dev, 275 bool can_change_status) 276 { 277 struct wake_irq *wirq = dev->power.wakeirq; 278 279 if (!wirq || !((wirq->status & WAKE_IRQ_DEDICATED_MASK))) 280 return; 281 282 if (likely(wirq->status & WAKE_IRQ_DEDICATED_MANAGED)) { 283 goto enable; 284 } else if (can_change_status) { 285 wirq->status |= WAKE_IRQ_DEDICATED_MANAGED; 286 goto enable; 287 } 288 289 return; 290 291 enable: 292 enable_irq(wirq->irq); 293 } 294 295 /** 296 * dev_pm_disable_wake_irq_check - Checks and disables wake-up interrupt 297 * @dev: Device 298 * 299 * Disables wake-up interrupt conditionally based on status. 300 * Should be only called from rpm_suspend() and rpm_resume() path. 301 */ 302 void dev_pm_disable_wake_irq_check(struct device *dev) 303 { 304 struct wake_irq *wirq = dev->power.wakeirq; 305 306 if (!wirq || !((wirq->status & WAKE_IRQ_DEDICATED_MASK))) 307 return; 308 309 if (wirq->status & WAKE_IRQ_DEDICATED_MANAGED) 310 disable_irq_nosync(wirq->irq); 311 } 312 313 /** 314 * dev_pm_arm_wake_irq - Arm device wake-up 315 * @wirq: Device wake-up interrupt 316 * 317 * Sets up the wake-up event conditionally based on the 318 * device_may_wake(). 319 */ 320 void dev_pm_arm_wake_irq(struct wake_irq *wirq) 321 { 322 if (!wirq) 323 return; 324 325 if (device_may_wakeup(wirq->dev)) { 326 if (wirq->status & WAKE_IRQ_DEDICATED_ALLOCATED) 327 enable_irq(wirq->irq); 328 329 enable_irq_wake(wirq->irq); 330 } 331 } 332 333 /** 334 * dev_pm_disarm_wake_irq - Disarm device wake-up 335 * @wirq: Device wake-up interrupt 336 * 337 * Clears up the wake-up event conditionally based on the 338 * device_may_wake(). 339 */ 340 void dev_pm_disarm_wake_irq(struct wake_irq *wirq) 341 { 342 if (!wirq) 343 return; 344 345 if (device_may_wakeup(wirq->dev)) { 346 disable_irq_wake(wirq->irq); 347 348 if (wirq->status & WAKE_IRQ_DEDICATED_ALLOCATED) 349 disable_irq_nosync(wirq->irq); 350 } 351 } 352