Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /* Device wakeirq helper functions */
0003 #include <linux/device.h>
0004 #include <linux/interrupt.h>
0005 #include <linux/irq.h>
0006 #include <linux/slab.h>
0007 #include <linux/pm_runtime.h>
0008 #include <linux/pm_wakeirq.h>
0009 
0010 #include "power.h"
0011 
0012 /**
0013  * dev_pm_attach_wake_irq - Attach device interrupt as a wake IRQ
0014  * @dev: Device entry
0015  * @wirq: Wake irq specific data
0016  *
0017  * Internal function to attach a dedicated wake-up interrupt as a wake IRQ.
0018  */
0019 static int dev_pm_attach_wake_irq(struct device *dev, struct wake_irq *wirq)
0020 {
0021     unsigned long flags;
0022 
0023     if (!dev || !wirq)
0024         return -EINVAL;
0025 
0026     spin_lock_irqsave(&dev->power.lock, flags);
0027     if (dev_WARN_ONCE(dev, dev->power.wakeirq,
0028               "wake irq already initialized\n")) {
0029         spin_unlock_irqrestore(&dev->power.lock, flags);
0030         return -EEXIST;
0031     }
0032 
0033     dev->power.wakeirq = wirq;
0034     device_wakeup_attach_irq(dev, wirq);
0035 
0036     spin_unlock_irqrestore(&dev->power.lock, flags);
0037     return 0;
0038 }
0039 
0040 /**
0041  * dev_pm_set_wake_irq - Attach device IO interrupt as wake IRQ
0042  * @dev: Device entry
0043  * @irq: Device IO interrupt
0044  *
0045  * Attach a device IO interrupt as a wake IRQ. The wake IRQ gets
0046  * automatically configured for wake-up from suspend  based
0047  * on the device specific sysfs wakeup entry. Typically called
0048  * during driver probe after calling device_init_wakeup().
0049  */
0050 int dev_pm_set_wake_irq(struct device *dev, int irq)
0051 {
0052     struct wake_irq *wirq;
0053     int err;
0054 
0055     if (irq < 0)
0056         return -EINVAL;
0057 
0058     wirq = kzalloc(sizeof(*wirq), GFP_KERNEL);
0059     if (!wirq)
0060         return -ENOMEM;
0061 
0062     wirq->dev = dev;
0063     wirq->irq = irq;
0064 
0065     err = dev_pm_attach_wake_irq(dev, wirq);
0066     if (err)
0067         kfree(wirq);
0068 
0069     return err;
0070 }
0071 EXPORT_SYMBOL_GPL(dev_pm_set_wake_irq);
0072 
0073 /**
0074  * dev_pm_clear_wake_irq - Detach a device IO interrupt wake IRQ
0075  * @dev: Device entry
0076  *
0077  * Detach a device wake IRQ and free resources.
0078  *
0079  * Note that it's OK for drivers to call this without calling
0080  * dev_pm_set_wake_irq() as all the driver instances may not have
0081  * a wake IRQ configured. This avoid adding wake IRQ specific
0082  * checks into the drivers.
0083  */
0084 void dev_pm_clear_wake_irq(struct device *dev)
0085 {
0086     struct wake_irq *wirq = dev->power.wakeirq;
0087     unsigned long flags;
0088 
0089     if (!wirq)
0090         return;
0091 
0092     spin_lock_irqsave(&dev->power.lock, flags);
0093     device_wakeup_detach_irq(dev);
0094     dev->power.wakeirq = NULL;
0095     spin_unlock_irqrestore(&dev->power.lock, flags);
0096 
0097     if (wirq->status & WAKE_IRQ_DEDICATED_ALLOCATED) {
0098         free_irq(wirq->irq, wirq);
0099         wirq->status &= ~WAKE_IRQ_DEDICATED_MASK;
0100     }
0101     kfree(wirq->name);
0102     kfree(wirq);
0103 }
0104 EXPORT_SYMBOL_GPL(dev_pm_clear_wake_irq);
0105 
0106 /**
0107  * handle_threaded_wake_irq - Handler for dedicated wake-up interrupts
0108  * @irq: Device specific dedicated wake-up interrupt
0109  * @_wirq: Wake IRQ data
0110  *
0111  * Some devices have a separate wake-up interrupt in addition to the
0112  * device IO interrupt. The wake-up interrupt signals that a device
0113  * should be woken up from it's idle state. This handler uses device
0114  * specific pm_runtime functions to wake the device, and then it's
0115  * up to the device to do whatever it needs to. Note that as the
0116  * device may need to restore context and start up regulators, we
0117  * use a threaded IRQ.
0118  *
0119  * Also note that we are not resending the lost device interrupts.
0120  * We assume that the wake-up interrupt just needs to wake-up the
0121  * device, and then device's pm_runtime_resume() can deal with the
0122  * situation.
0123  */
0124 static irqreturn_t handle_threaded_wake_irq(int irq, void *_wirq)
0125 {
0126     struct wake_irq *wirq = _wirq;
0127     int res;
0128 
0129     /* Maybe abort suspend? */
0130     if (irqd_is_wakeup_set(irq_get_irq_data(irq))) {
0131         pm_wakeup_event(wirq->dev, 0);
0132 
0133         return IRQ_HANDLED;
0134     }
0135 
0136     /* We don't want RPM_ASYNC or RPM_NOWAIT here */
0137     res = pm_runtime_resume(wirq->dev);
0138     if (res < 0)
0139         dev_warn(wirq->dev,
0140              "wake IRQ with no resume: %i\n", res);
0141 
0142     return IRQ_HANDLED;
0143 }
0144 
0145 static int __dev_pm_set_dedicated_wake_irq(struct device *dev, int irq, unsigned int flag)
0146 {
0147     struct wake_irq *wirq;
0148     int err;
0149 
0150     if (irq < 0)
0151         return -EINVAL;
0152 
0153     wirq = kzalloc(sizeof(*wirq), GFP_KERNEL);
0154     if (!wirq)
0155         return -ENOMEM;
0156 
0157     wirq->name = kasprintf(GFP_KERNEL, "%s:wakeup", dev_name(dev));
0158     if (!wirq->name) {
0159         err = -ENOMEM;
0160         goto err_free;
0161     }
0162 
0163     wirq->dev = dev;
0164     wirq->irq = irq;
0165 
0166     /* Prevent deferred spurious wakeirqs with disable_irq_nosync() */
0167     irq_set_status_flags(irq, IRQ_DISABLE_UNLAZY);
0168 
0169     /*
0170      * Consumer device may need to power up and restore state
0171      * so we use a threaded irq.
0172      */
0173     err = request_threaded_irq(irq, NULL, handle_threaded_wake_irq,
0174                    IRQF_ONESHOT | IRQF_NO_AUTOEN,
0175                    wirq->name, wirq);
0176     if (err)
0177         goto err_free_name;
0178 
0179     err = dev_pm_attach_wake_irq(dev, wirq);
0180     if (err)
0181         goto err_free_irq;
0182 
0183     wirq->status = WAKE_IRQ_DEDICATED_ALLOCATED | flag;
0184 
0185     return err;
0186 
0187 err_free_irq:
0188     free_irq(irq, wirq);
0189 err_free_name:
0190     kfree(wirq->name);
0191 err_free:
0192     kfree(wirq);
0193 
0194     return err;
0195 }
0196 
0197 
0198 /**
0199  * dev_pm_set_dedicated_wake_irq - Request a dedicated wake-up interrupt
0200  * @dev: Device entry
0201  * @irq: Device wake-up interrupt
0202  *
0203  * Unless your hardware has separate wake-up interrupts in addition
0204  * to the device IO interrupts, you don't need this.
0205  *
0206  * Sets up a threaded interrupt handler for a device that has
0207  * a dedicated wake-up interrupt in addition to the device IO
0208  * interrupt.
0209  *
0210  * The interrupt starts disabled, and needs to be managed for
0211  * the device by the bus code or the device driver using
0212  * dev_pm_enable_wake_irq*() and dev_pm_disable_wake_irq*()
0213  * functions.
0214  */
0215 int dev_pm_set_dedicated_wake_irq(struct device *dev, int irq)
0216 {
0217     return __dev_pm_set_dedicated_wake_irq(dev, irq, 0);
0218 }
0219 EXPORT_SYMBOL_GPL(dev_pm_set_dedicated_wake_irq);
0220 
0221 /**
0222  * dev_pm_set_dedicated_wake_irq_reverse - Request a dedicated wake-up interrupt
0223  *                                         with reverse enable ordering
0224  * @dev: Device entry
0225  * @irq: Device wake-up interrupt
0226  *
0227  * Unless your hardware has separate wake-up interrupts in addition
0228  * to the device IO interrupts, you don't need this.
0229  *
0230  * Sets up a threaded interrupt handler for a device that has a dedicated
0231  * wake-up interrupt in addition to the device IO interrupt. It sets
0232  * the status of WAKE_IRQ_DEDICATED_REVERSE to tell rpm_suspend()
0233  * to enable dedicated wake-up interrupt after running the runtime suspend
0234  * callback for @dev.
0235  *
0236  * The interrupt starts disabled, and needs to be managed for
0237  * the device by the bus code or the device driver using
0238  * dev_pm_enable_wake_irq*() and dev_pm_disable_wake_irq*()
0239  * functions.
0240  */
0241 int dev_pm_set_dedicated_wake_irq_reverse(struct device *dev, int irq)
0242 {
0243     return __dev_pm_set_dedicated_wake_irq(dev, irq, WAKE_IRQ_DEDICATED_REVERSE);
0244 }
0245 EXPORT_SYMBOL_GPL(dev_pm_set_dedicated_wake_irq_reverse);
0246 
0247 /**
0248  * dev_pm_enable_wake_irq - Enable device wake-up interrupt
0249  * @dev: Device
0250  *
0251  * Optionally called from the bus code or the device driver for
0252  * runtime_resume() to override the PM runtime core managed wake-up
0253  * interrupt handling to enable the wake-up interrupt.
0254  *
0255  * Note that for runtime_suspend()) the wake-up interrupts
0256  * should be unconditionally enabled unlike for suspend()
0257  * that is conditional.
0258  */
0259 void dev_pm_enable_wake_irq(struct device *dev)
0260 {
0261     struct wake_irq *wirq = dev->power.wakeirq;
0262 
0263     if (wirq && (wirq->status & WAKE_IRQ_DEDICATED_ALLOCATED))
0264         enable_irq(wirq->irq);
0265 }
0266 EXPORT_SYMBOL_GPL(dev_pm_enable_wake_irq);
0267 
0268 /**
0269  * dev_pm_disable_wake_irq - Disable device wake-up interrupt
0270  * @dev: Device
0271  *
0272  * Optionally called from the bus code or the device driver for
0273  * runtime_suspend() to override the PM runtime core managed wake-up
0274  * interrupt handling to disable the wake-up interrupt.
0275  */
0276 void dev_pm_disable_wake_irq(struct device *dev)
0277 {
0278     struct wake_irq *wirq = dev->power.wakeirq;
0279 
0280     if (wirq && (wirq->status & WAKE_IRQ_DEDICATED_ALLOCATED))
0281         disable_irq_nosync(wirq->irq);
0282 }
0283 EXPORT_SYMBOL_GPL(dev_pm_disable_wake_irq);
0284 
0285 /**
0286  * dev_pm_enable_wake_irq_check - Checks and enables wake-up interrupt
0287  * @dev: Device
0288  * @can_change_status: Can change wake-up interrupt status
0289  *
0290  * Enables wakeirq conditionally. We need to enable wake-up interrupt
0291  * lazily on the first rpm_suspend(). This is needed as the consumer device
0292  * starts in RPM_SUSPENDED state, and the first pm_runtime_get() would
0293  * otherwise try to disable already disabled wakeirq. The wake-up interrupt
0294  * starts disabled with IRQ_NOAUTOEN set.
0295  *
0296  * Should be only called from rpm_suspend() and rpm_resume() path.
0297  * Caller must hold &dev->power.lock to change wirq->status
0298  */
0299 void dev_pm_enable_wake_irq_check(struct device *dev,
0300                   bool can_change_status)
0301 {
0302     struct wake_irq *wirq = dev->power.wakeirq;
0303 
0304     if (!wirq || !(wirq->status & WAKE_IRQ_DEDICATED_MASK))
0305         return;
0306 
0307     if (likely(wirq->status & WAKE_IRQ_DEDICATED_MANAGED)) {
0308         goto enable;
0309     } else if (can_change_status) {
0310         wirq->status |= WAKE_IRQ_DEDICATED_MANAGED;
0311         goto enable;
0312     }
0313 
0314     return;
0315 
0316 enable:
0317     if (!can_change_status || !(wirq->status & WAKE_IRQ_DEDICATED_REVERSE))
0318         enable_irq(wirq->irq);
0319 }
0320 
0321 /**
0322  * dev_pm_disable_wake_irq_check - Checks and disables wake-up interrupt
0323  * @dev: Device
0324  * @cond_disable: if set, also check WAKE_IRQ_DEDICATED_REVERSE
0325  *
0326  * Disables wake-up interrupt conditionally based on status.
0327  * Should be only called from rpm_suspend() and rpm_resume() path.
0328  */
0329 void dev_pm_disable_wake_irq_check(struct device *dev, bool cond_disable)
0330 {
0331     struct wake_irq *wirq = dev->power.wakeirq;
0332 
0333     if (!wirq || !(wirq->status & WAKE_IRQ_DEDICATED_MASK))
0334         return;
0335 
0336     if (cond_disable && (wirq->status & WAKE_IRQ_DEDICATED_REVERSE))
0337         return;
0338 
0339     if (wirq->status & WAKE_IRQ_DEDICATED_MANAGED)
0340         disable_irq_nosync(wirq->irq);
0341 }
0342 
0343 /**
0344  * dev_pm_enable_wake_irq_complete - enable wake IRQ not enabled before
0345  * @dev: Device using the wake IRQ
0346  *
0347  * Enable wake IRQ conditionally based on status, mainly used if want to
0348  * enable wake IRQ after running ->runtime_suspend() which depends on
0349  * WAKE_IRQ_DEDICATED_REVERSE.
0350  *
0351  * Should be only called from rpm_suspend() path.
0352  */
0353 void dev_pm_enable_wake_irq_complete(struct device *dev)
0354 {
0355     struct wake_irq *wirq = dev->power.wakeirq;
0356 
0357     if (!wirq || !(wirq->status & WAKE_IRQ_DEDICATED_MASK))
0358         return;
0359 
0360     if (wirq->status & WAKE_IRQ_DEDICATED_MANAGED &&
0361         wirq->status & WAKE_IRQ_DEDICATED_REVERSE)
0362         enable_irq(wirq->irq);
0363 }
0364 
0365 /**
0366  * dev_pm_arm_wake_irq - Arm device wake-up
0367  * @wirq: Device wake-up interrupt
0368  *
0369  * Sets up the wake-up event conditionally based on the
0370  * device_may_wake().
0371  */
0372 void dev_pm_arm_wake_irq(struct wake_irq *wirq)
0373 {
0374     if (!wirq)
0375         return;
0376 
0377     if (device_may_wakeup(wirq->dev)) {
0378         if (wirq->status & WAKE_IRQ_DEDICATED_ALLOCATED &&
0379             !pm_runtime_status_suspended(wirq->dev))
0380             enable_irq(wirq->irq);
0381 
0382         enable_irq_wake(wirq->irq);
0383     }
0384 }
0385 
0386 /**
0387  * dev_pm_disarm_wake_irq - Disarm device wake-up
0388  * @wirq: Device wake-up interrupt
0389  *
0390  * Clears up the wake-up event conditionally based on the
0391  * device_may_wake().
0392  */
0393 void dev_pm_disarm_wake_irq(struct wake_irq *wirq)
0394 {
0395     if (!wirq)
0396         return;
0397 
0398     if (device_may_wakeup(wirq->dev)) {
0399         disable_irq_wake(wirq->irq);
0400 
0401         if (wirq->status & WAKE_IRQ_DEDICATED_ALLOCATED &&
0402             !pm_runtime_status_suspended(wirq->dev))
0403             disable_irq_nosync(wirq->irq);
0404     }
0405 }