// SPDX-License-Identifier: GPL-2.01/* Device wakeirq helper functions */2#include <linux/device.h>3#include <linux/interrupt.h>4#include <linux/irq.h>5#include <linux/slab.h>6#include <linux/pm_runtime.h>7#include <linux/pm_wakeirq.h>89#include "power.h"1011/**12* dev_pm_attach_wake_irq - Attach device interrupt as a wake IRQ13* @dev: Device entry14* @wirq: Wake irq specific data15*16* Internal function to attach a dedicated wake-up interrupt as a wake IRQ.17*/18static int dev_pm_attach_wake_irq(struct device *dev, struct wake_irq *wirq)19{20unsigned long flags;2122if (!dev || !wirq)23return -EINVAL;2425spin_lock_irqsave(&dev->power.lock, flags);26if (dev_WARN_ONCE(dev, dev->power.wakeirq,27"wake irq already initialized\n")) {28spin_unlock_irqrestore(&dev->power.lock, flags);29return -EEXIST;30}3132dev->power.wakeirq = wirq;33device_wakeup_attach_irq(dev, wirq);3435spin_unlock_irqrestore(&dev->power.lock, flags);36return 0;37}3839/**40* dev_pm_set_wake_irq - Attach device IO interrupt as wake IRQ41* @dev: Device entry42* @irq: Device IO interrupt43*44* Attach a device IO interrupt as a wake IRQ. The wake IRQ gets45* automatically configured for wake-up from suspend based46* on the device specific sysfs wakeup entry. Typically called47* during driver probe after calling device_init_wakeup().48*/49int dev_pm_set_wake_irq(struct device *dev, int irq)50{51struct wake_irq *wirq;52int err;5354if (irq < 0)55return -EINVAL;5657wirq = kzalloc(sizeof(*wirq), GFP_KERNEL);58if (!wirq)59return -ENOMEM;6061wirq->dev = dev;62wirq->irq = irq;6364err = dev_pm_attach_wake_irq(dev, wirq);65if (err)66kfree(wirq);6768return err;69}70EXPORT_SYMBOL_GPL(dev_pm_set_wake_irq);7172/**73* dev_pm_clear_wake_irq - Detach a device IO interrupt wake IRQ74* @dev: Device entry75*76* Detach a device wake IRQ and free resources.77*78* Note that it's OK for drivers to call this without calling79* dev_pm_set_wake_irq() as all the driver instances may not have80* a wake IRQ configured. This avoid adding wake IRQ specific81* checks into the drivers.82*/83void dev_pm_clear_wake_irq(struct device *dev)84{85struct wake_irq *wirq = dev->power.wakeirq;86unsigned long flags;8788if (!wirq)89return;9091spin_lock_irqsave(&dev->power.lock, flags);92device_wakeup_detach_irq(dev);93dev->power.wakeirq = NULL;94spin_unlock_irqrestore(&dev->power.lock, flags);9596if (wirq->status & WAKE_IRQ_DEDICATED_ALLOCATED) {97free_irq(wirq->irq, wirq);98wirq->status &= ~WAKE_IRQ_DEDICATED_MASK;99}100kfree(wirq->name);101kfree(wirq);102}103EXPORT_SYMBOL_GPL(dev_pm_clear_wake_irq);104105static void devm_pm_clear_wake_irq(void *dev)106{107dev_pm_clear_wake_irq(dev);108}109110/**111* devm_pm_set_wake_irq - device-managed variant of dev_pm_set_wake_irq112* @dev: Device entry113* @irq: Device IO interrupt114*115*116* Attach a device IO interrupt as a wake IRQ, same with dev_pm_set_wake_irq,117* but the device will be auto clear wake capability on driver detach.118*/119int devm_pm_set_wake_irq(struct device *dev, int irq)120{121int ret;122123ret = dev_pm_set_wake_irq(dev, irq);124if (ret)125return ret;126127return devm_add_action_or_reset(dev, devm_pm_clear_wake_irq, dev);128}129EXPORT_SYMBOL_GPL(devm_pm_set_wake_irq);130131/**132* handle_threaded_wake_irq - Handler for dedicated wake-up interrupts133* @irq: Device specific dedicated wake-up interrupt134* @_wirq: Wake IRQ data135*136* Some devices have a separate wake-up interrupt in addition to the137* device IO interrupt. The wake-up interrupt signals that a device138* should be woken up from it's idle state. This handler uses device139* specific pm_runtime functions to wake the device, and then it's140* up to the device to do whatever it needs to. Note that as the141* device may need to restore context and start up regulators, we142* use a threaded IRQ.143*144* Also note that we are not resending the lost device interrupts.145* We assume that the wake-up interrupt just needs to wake-up the146* device, and then device's pm_runtime_resume() can deal with the147* situation.148*/149static irqreturn_t handle_threaded_wake_irq(int irq, void *_wirq)150{151struct wake_irq *wirq = _wirq;152int res;153154/* Maybe abort suspend? */155if (irqd_is_wakeup_set(irq_get_irq_data(irq))) {156pm_wakeup_event(wirq->dev, 0);157158return IRQ_HANDLED;159}160161/* We don't want RPM_ASYNC or RPM_NOWAIT here */162res = pm_runtime_resume(wirq->dev);163if (res < 0)164dev_warn(wirq->dev,165"wake IRQ with no resume: %i\n", res);166167return IRQ_HANDLED;168}169170static int __dev_pm_set_dedicated_wake_irq(struct device *dev, int irq, unsigned int flag)171{172struct wake_irq *wirq;173int err;174175if (irq < 0)176return -EINVAL;177178wirq = kzalloc(sizeof(*wirq), GFP_KERNEL);179if (!wirq)180return -ENOMEM;181182wirq->name = kasprintf(GFP_KERNEL, "%s:wakeup", dev_name(dev));183if (!wirq->name) {184err = -ENOMEM;185goto err_free;186}187188wirq->dev = dev;189wirq->irq = irq;190191/* Prevent deferred spurious wakeirqs with disable_irq_nosync() */192irq_set_status_flags(irq, IRQ_DISABLE_UNLAZY);193194/*195* Consumer device may need to power up and restore state196* so we use a threaded irq.197*/198err = request_threaded_irq(irq, NULL, handle_threaded_wake_irq,199IRQF_ONESHOT | IRQF_NO_AUTOEN,200wirq->name, wirq);201if (err)202goto err_free_name;203204err = dev_pm_attach_wake_irq(dev, wirq);205if (err)206goto err_free_irq;207208wirq->status = WAKE_IRQ_DEDICATED_ALLOCATED | flag;209210return err;211212err_free_irq:213free_irq(irq, wirq);214err_free_name:215kfree(wirq->name);216err_free:217kfree(wirq);218219return err;220}221222/**223* dev_pm_set_dedicated_wake_irq - Request a dedicated wake-up interrupt224* @dev: Device entry225* @irq: Device wake-up interrupt226*227* Unless your hardware has separate wake-up interrupts in addition228* to the device IO interrupts, you don't need this.229*230* Sets up a threaded interrupt handler for a device that has231* a dedicated wake-up interrupt in addition to the device IO232* interrupt.233*/234int dev_pm_set_dedicated_wake_irq(struct device *dev, int irq)235{236return __dev_pm_set_dedicated_wake_irq(dev, irq, 0);237}238EXPORT_SYMBOL_GPL(dev_pm_set_dedicated_wake_irq);239240/**241* dev_pm_set_dedicated_wake_irq_reverse - Request a dedicated wake-up interrupt242* with reverse enable ordering243* @dev: Device entry244* @irq: Device wake-up interrupt245*246* Unless your hardware has separate wake-up interrupts in addition247* to the device IO interrupts, you don't need this.248*249* Sets up a threaded interrupt handler for a device that has a dedicated250* wake-up interrupt in addition to the device IO interrupt. It sets251* the status of WAKE_IRQ_DEDICATED_REVERSE to tell rpm_suspend()252* to enable dedicated wake-up interrupt after running the runtime suspend253* callback for @dev.254*/255int dev_pm_set_dedicated_wake_irq_reverse(struct device *dev, int irq)256{257return __dev_pm_set_dedicated_wake_irq(dev, irq, WAKE_IRQ_DEDICATED_REVERSE);258}259EXPORT_SYMBOL_GPL(dev_pm_set_dedicated_wake_irq_reverse);260261/**262* dev_pm_enable_wake_irq_check - Checks and enables wake-up interrupt263* @dev: Device264* @can_change_status: Can change wake-up interrupt status265*266* Enables wakeirq conditionally. We need to enable wake-up interrupt267* lazily on the first rpm_suspend(). This is needed as the consumer device268* starts in RPM_SUSPENDED state, and the first pm_runtime_get() would269* otherwise try to disable already disabled wakeirq. The wake-up interrupt270* starts disabled with IRQ_NOAUTOEN set.271*272* Should be only called from rpm_suspend() and rpm_resume() path.273* Caller must hold &dev->power.lock to change wirq->status274*/275void dev_pm_enable_wake_irq_check(struct device *dev,276bool can_change_status)277{278struct wake_irq *wirq = dev->power.wakeirq;279280if (!wirq || !(wirq->status & WAKE_IRQ_DEDICATED_MASK))281return;282283if (likely(wirq->status & WAKE_IRQ_DEDICATED_MANAGED)) {284goto enable;285} else if (can_change_status) {286wirq->status |= WAKE_IRQ_DEDICATED_MANAGED;287goto enable;288}289290return;291292enable:293if (!can_change_status || !(wirq->status & WAKE_IRQ_DEDICATED_REVERSE)) {294enable_irq(wirq->irq);295wirq->status |= WAKE_IRQ_DEDICATED_ENABLED;296}297}298299/**300* dev_pm_disable_wake_irq_check - Checks and disables wake-up interrupt301* @dev: Device302* @cond_disable: if set, also check WAKE_IRQ_DEDICATED_REVERSE303*304* Disables wake-up interrupt conditionally based on status.305* Should be only called from rpm_suspend() and rpm_resume() path.306*/307void dev_pm_disable_wake_irq_check(struct device *dev, bool cond_disable)308{309struct wake_irq *wirq = dev->power.wakeirq;310311if (!wirq || !(wirq->status & WAKE_IRQ_DEDICATED_MASK))312return;313314if (cond_disable && (wirq->status & WAKE_IRQ_DEDICATED_REVERSE))315return;316317if (wirq->status & WAKE_IRQ_DEDICATED_MANAGED) {318wirq->status &= ~WAKE_IRQ_DEDICATED_ENABLED;319disable_irq_nosync(wirq->irq);320}321}322323/**324* dev_pm_enable_wake_irq_complete - enable wake IRQ not enabled before325* @dev: Device using the wake IRQ326*327* Enable wake IRQ conditionally based on status, mainly used if want to328* enable wake IRQ after running ->runtime_suspend() which depends on329* WAKE_IRQ_DEDICATED_REVERSE.330*331* Should be only called from rpm_suspend() path.332*/333void dev_pm_enable_wake_irq_complete(struct device *dev)334{335struct wake_irq *wirq = dev->power.wakeirq;336337if (!wirq || !(wirq->status & WAKE_IRQ_DEDICATED_MASK))338return;339340if (wirq->status & WAKE_IRQ_DEDICATED_MANAGED &&341wirq->status & WAKE_IRQ_DEDICATED_REVERSE) {342enable_irq(wirq->irq);343wirq->status |= WAKE_IRQ_DEDICATED_ENABLED;344}345}346347/**348* dev_pm_arm_wake_irq - Arm device wake-up349* @wirq: Device wake-up interrupt350*351* Sets up the wake-up event conditionally based on the352* device_may_wake().353*/354void dev_pm_arm_wake_irq(struct wake_irq *wirq)355{356if (!wirq)357return;358359if (device_may_wakeup(wirq->dev)) {360if (wirq->status & WAKE_IRQ_DEDICATED_ALLOCATED &&361!(wirq->status & WAKE_IRQ_DEDICATED_ENABLED))362enable_irq(wirq->irq);363364enable_irq_wake(wirq->irq);365}366}367368/**369* dev_pm_disarm_wake_irq - Disarm device wake-up370* @wirq: Device wake-up interrupt371*372* Clears up the wake-up event conditionally based on the373* device_may_wake().374*/375void dev_pm_disarm_wake_irq(struct wake_irq *wirq)376{377if (!wirq)378return;379380if (device_may_wakeup(wirq->dev)) {381disable_irq_wake(wirq->irq);382383if (wirq->status & WAKE_IRQ_DEDICATED_ALLOCATED &&384!(wirq->status & WAKE_IRQ_DEDICATED_ENABLED))385disable_irq_nosync(wirq->irq);386}387}388389390