PM / Runtime: Implement autosuspend support
[pandora-kernel.git] / drivers / base / power / runtime.c
index 5bd4daa..cd4e100 100644 (file)
@@ -9,7 +9,6 @@
 
 #include <linux/sched.h>
 #include <linux/pm_runtime.h>
-#include <linux/jiffies.h>
 #include "power.h"
 
 static int rpm_resume(struct device *dev, int rpmflags);
@@ -79,6 +78,53 @@ static void pm_runtime_cancel_pending(struct device *dev)
        dev->power.request = RPM_REQ_NONE;
 }
 
+/*
+ * pm_runtime_autosuspend_expiration - Get a device's autosuspend-delay expiration time.
+ * @dev: Device to handle.
+ *
+ * Compute the autosuspend-delay expiration time based on the device's
+ * power.last_busy time.  If the delay has already expired or is disabled
+ * (negative) or the power.use_autosuspend flag isn't set, return 0.
+ * Otherwise return the expiration time in jiffies (adjusted to be nonzero).
+ *
+ * This function may be called either with or without dev->power.lock held.
+ * Either way it can be racy, since power.last_busy may be updated at any time.
+ */
+unsigned long pm_runtime_autosuspend_expiration(struct device *dev)
+{
+       int autosuspend_delay;
+       long elapsed;
+       unsigned long last_busy;
+       unsigned long expires = 0;
+
+       if (!dev->power.use_autosuspend)
+               goto out;
+
+       autosuspend_delay = ACCESS_ONCE(dev->power.autosuspend_delay);
+       if (autosuspend_delay < 0)
+               goto out;
+
+       last_busy = ACCESS_ONCE(dev->power.last_busy);
+       elapsed = jiffies - last_busy;
+       if (elapsed < 0)
+               goto out;       /* jiffies has wrapped around. */
+
+       /*
+        * If the autosuspend_delay is >= 1 second, align the timer by rounding
+        * up to the nearest second.
+        */
+       expires = last_busy + msecs_to_jiffies(autosuspend_delay);
+       if (autosuspend_delay >= 1000)
+               expires = round_jiffies(expires);
+       expires += !expires;
+       if (elapsed >= expires - last_busy)
+               expires = 0;    /* Already expired. */
+
+ out:
+       return expires;
+}
+EXPORT_SYMBOL_GPL(pm_runtime_autosuspend_expiration);
+
 /**
  * rpm_check_suspend_allowed - Test whether a device may be suspended.
  * @dev: Device to test.
@@ -234,6 +280,32 @@ static int rpm_suspend(struct device *dev, int rpmflags)
        if (retval)
                goto out;
 
+       /* If the autosuspend_delay time hasn't expired yet, reschedule. */
+       if ((rpmflags & RPM_AUTO)
+           && dev->power.runtime_status != RPM_SUSPENDING) {
+               unsigned long expires = pm_runtime_autosuspend_expiration(dev);
+
+               if (expires != 0) {
+                       /* Pending requests need to be canceled. */
+                       dev->power.request = RPM_REQ_NONE;
+
+                       /*
+                        * Optimization: If the timer is already running and is
+                        * set to expire at or before the autosuspend delay,
+                        * avoid the overhead of resetting it.  Just let it
+                        * expire; pm_suspend_timer_fn() will take care of the
+                        * rest.
+                        */
+                       if (!(dev->power.timer_expires && time_before_eq(
+                           dev->power.timer_expires, expires))) {
+                               dev->power.timer_expires = expires;
+                               mod_timer(&dev->power.suspend_timer, expires);
+                       }
+                       dev->power.timer_autosuspends = 1;
+                       goto out;
+               }
+       }
+
        /* Other scheduled or pending requests need to be canceled. */
        pm_runtime_cancel_pending(dev);
 
@@ -268,7 +340,8 @@ static int rpm_suspend(struct device *dev, int rpmflags)
 
        /* Carry out an asynchronous or a synchronous suspend. */
        if (rpmflags & RPM_ASYNC) {
-               dev->power.request = RPM_REQ_SUSPEND;
+               dev->power.request = (rpmflags & RPM_AUTO) ?
+                   RPM_REQ_AUTOSUSPEND : RPM_REQ_SUSPEND;
                if (!dev->power.request_pending) {
                        dev->power.request_pending = true;
                        queue_work(pm_wq, &dev->power.work);
@@ -383,8 +456,15 @@ static int rpm_resume(struct device *dev, int rpmflags)
        if (retval)
                goto out;
 
-       /* Other scheduled or pending requests need to be canceled. */
-       pm_runtime_cancel_pending(dev);
+       /*
+        * Other scheduled or pending requests need to be canceled.  Small
+        * optimization: If an autosuspend timer is running, leave it running
+        * rather than cancelling it now only to restart it again in the near
+        * future.
+        */
+       dev->power.request = RPM_REQ_NONE;
+       if (!dev->power.timer_autosuspends)
+               pm_runtime_deactivate_timer(dev);
 
        if (dev->power.runtime_status == RPM_ACTIVE) {
                retval = 1;
@@ -568,6 +648,9 @@ static void pm_runtime_work(struct work_struct *work)
        case RPM_REQ_SUSPEND:
                rpm_suspend(dev, RPM_NOWAIT);
                break;
+       case RPM_REQ_AUTOSUSPEND:
+               rpm_suspend(dev, RPM_NOWAIT | RPM_AUTO);
+               break;
        case RPM_REQ_RESUME:
                rpm_resume(dev, RPM_NOWAIT);
                break;
@@ -595,7 +678,8 @@ static void pm_suspend_timer_fn(unsigned long data)
        /* If 'expire' is after 'jiffies' we've been called too early. */
        if (expires > 0 && !time_after(expires, jiffies)) {
                dev->power.timer_expires = 0;
-               rpm_suspend(dev, RPM_ASYNC);
+               rpm_suspend(dev, dev->power.timer_autosuspends ?
+                   (RPM_ASYNC | RPM_AUTO) : RPM_ASYNC);
        }
 
        spin_unlock_irqrestore(&dev->power.lock, flags);
@@ -627,6 +711,7 @@ int pm_schedule_suspend(struct device *dev, unsigned int delay)
 
        dev->power.timer_expires = jiffies + msecs_to_jiffies(delay);
        dev->power.timer_expires += !dev->power.timer_expires;
+       dev->power.timer_autosuspends = 0;
        mod_timer(&dev->power.suspend_timer, dev->power.timer_expires);
 
  out:
@@ -670,7 +755,9 @@ EXPORT_SYMBOL_GPL(__pm_runtime_idle);
  * @dev: Device to suspend.
  * @rpmflags: Flag bits.
  *
- * Carry out a suspend, either synchronous or asynchronous.
+ * If the RPM_GET_PUT flag is set, decrement the device's usage count and
+ * return immediately if it is larger than zero.  Then carry out a suspend,
+ * either synchronous or asynchronous.
  *
  * This routine may be called in atomic context if the RPM_ASYNC flag is set.
  */
@@ -679,6 +766,11 @@ int __pm_runtime_suspend(struct device *dev, int rpmflags)
        unsigned long flags;
        int retval;
 
+       if (rpmflags & RPM_GET_PUT) {
+               if (!atomic_dec_and_test(&dev->power.usage_count))
+                       return 0;
+       }
+
        spin_lock_irqsave(&dev->power.lock, flags);
        retval = rpm_suspend(dev, rpmflags);
        spin_unlock_irqrestore(&dev->power.lock, flags);
@@ -980,7 +1072,7 @@ void pm_runtime_allow(struct device *dev)
 
        dev->power.runtime_auto = true;
        if (atomic_dec_and_test(&dev->power.usage_count))
-               rpm_idle(dev, 0);
+               rpm_idle(dev, RPM_AUTO);
 
  out:
        spin_unlock_irq(&dev->power.lock);
@@ -1006,6 +1098,86 @@ void pm_runtime_no_callbacks(struct device *dev)
 }
 EXPORT_SYMBOL_GPL(pm_runtime_no_callbacks);
 
+/**
+ * update_autosuspend - Handle a change to a device's autosuspend settings.
+ * @dev: Device to handle.
+ * @old_delay: The former autosuspend_delay value.
+ * @old_use: The former use_autosuspend value.
+ *
+ * Prevent runtime suspend if the new delay is negative and use_autosuspend is
+ * set; otherwise allow it.  Send an idle notification if suspends are allowed.
+ *
+ * This function must be called under dev->power.lock with interrupts disabled.
+ */
+static void update_autosuspend(struct device *dev, int old_delay, int old_use)
+{
+       int delay = dev->power.autosuspend_delay;
+
+       /* Should runtime suspend be prevented now? */
+       if (dev->power.use_autosuspend && delay < 0) {
+
+               /* If it used to be allowed then prevent it. */
+               if (!old_use || old_delay >= 0) {
+                       atomic_inc(&dev->power.usage_count);
+                       rpm_resume(dev, 0);
+               }
+       }
+
+       /* Runtime suspend should be allowed now. */
+       else {
+
+               /* If it used to be prevented then allow it. */
+               if (old_use && old_delay < 0)
+                       atomic_dec(&dev->power.usage_count);
+
+               /* Maybe we can autosuspend now. */
+               rpm_idle(dev, RPM_AUTO);
+       }
+}
+
+/**
+ * pm_runtime_set_autosuspend_delay - Set a device's autosuspend_delay value.
+ * @dev: Device to handle.
+ * @delay: Value of the new delay in milliseconds.
+ *
+ * Set the device's power.autosuspend_delay value.  If it changes to negative
+ * and the power.use_autosuspend flag is set, prevent run-time suspends.  If it
+ * changes the other way, allow run-time suspends.
+ */
+void pm_runtime_set_autosuspend_delay(struct device *dev, int delay)
+{
+       int old_delay, old_use;
+
+       spin_lock_irq(&dev->power.lock);
+       old_delay = dev->power.autosuspend_delay;
+       old_use = dev->power.use_autosuspend;
+       dev->power.autosuspend_delay = delay;
+       update_autosuspend(dev, old_delay, old_use);
+       spin_unlock_irq(&dev->power.lock);
+}
+EXPORT_SYMBOL_GPL(pm_runtime_set_autosuspend_delay);
+
+/**
+ * __pm_runtime_use_autosuspend - Set a device's use_autosuspend flag.
+ * @dev: Device to handle.
+ * @use: New value for use_autosuspend.
+ *
+ * Set the device's power.use_autosuspend flag, and allow or prevent run-time
+ * suspends as needed.
+ */
+void __pm_runtime_use_autosuspend(struct device *dev, bool use)
+{
+       int old_delay, old_use;
+
+       spin_lock_irq(&dev->power.lock);
+       old_delay = dev->power.autosuspend_delay;
+       old_use = dev->power.use_autosuspend;
+       dev->power.use_autosuspend = use;
+       update_autosuspend(dev, old_delay, old_use);
+       spin_unlock_irq(&dev->power.lock);
+}
+EXPORT_SYMBOL_GPL(__pm_runtime_use_autosuspend);
+
 /**
  * pm_runtime_init - Initialize run-time PM fields in given device object.
  * @dev: Device object to initialize.