通过pm_runtime , 我们可以很好的控制设备的状态, 设备suspend 的顺序为, 先父设备再子设备,
设备resume的顺序为, 先子设备,在父设备。
设备的回到函数的优先级从从高到低顺序为:
dev->pwr_domain
dev->type->pm
dev->class->pm
dev->bus->pm
- if (dev->pwr_domain)
- callback = dev->pwr_domain->ops.runtime_resume;
- else if (dev->type && dev->type->pm)
- callback = dev->type->pm->runtime_resume;
- else if (dev->class && dev->class->pm)
- callback = dev->class->pm->runtime_resume;
- else if (dev->bus && dev->bus->pm)
- callback = dev->bus->pm->runtime_resume;
- else
- callback = NULL;
也就是说,这个优先级顺序给我我们接口来安排各个设备的 resume 和 suspend顺序。
对于platform device 设备, 它调用的是通用的pm_runtime 接口。
- static const struct dev_pm_ops platform_dev_pm_ops = {
- .runtime_suspend = pm_generic_runtime_suspend,
- .runtime_resume = pm_generic_runtime_resume,
- .runtime_idle = pm_generic_runtime_idle,
- USE_PLATFORM_PM_SLEEP_OPS
- };
- /**
- * rpm_resume - Carry out run-time resume of given device.
- * @dev: Device to resume.
- * @rpmflags: Flag bits.
- *
- * Check if the device's run-time PM status allows it to be resumed. Cancel
- * any scheduled or pending requests. If another resume has been started
- * earlier, either return immediately or wait for it to finish, depending on the
- * RPM_NOWAIT and RPM_ASYNC flags. Similarly, if there's a suspend running in
- * parallel with this function, either tell the other process to resume after
- * suspending (deferred_resume) or wait for it to finish. If the RPM_ASYNC
- * flag is set then queue a resume request; otherwise run the
- * ->runtime_resume() callback directly. Queue an idle notification for the
- * device if the resume succeeded.
- *
- * This function must be called under dev->power.lock with interrupts disabled.
- */
-
- /× 正常情况下, 有先resume 负设备 , 在resume 子设备 ×/
- static int rpm_resume(struct device *dev, int rpmflags)
- __releases(&dev->power.lock) __acquires(&dev->power.lock)
- {
- int (*callback)(struct device *);
- struct device *parent = NULL;
- int retval = 0;
- dev_dbg(dev, "%s flags 0x%x\n", __func__, rpmflags);
- repeat:
- if (dev->power.runtime_error) /× 如果调用设备的callback 函数出错误, 则更新runtime_error */
- retval = -EINVAL;
- else if (dev->power.disable_depth > 0) /* disable_depth 初始值为1, pm_runtime_enable 将-- __pm_runtime_disable 将++ 这个值 ×/
- retval = -EAGAIN; /× 如果值不为零, 则直接返回 ×/
- if (retval)
- goto out;
- /*
- * 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;
- /× 如果不是自动suspend, 则 停止suspend timer */
- if (!dev->power.timer_autosuspends)
- pm_runtime_deactivate_timer(dev);
- /* 当设备创建后, runtime_status的初始化值为RPM_SUSPENDED , 此后这个值的调用是通过 __update_runtime_status×/
- /× 如果设备是active,则 直接返回 ×/
- if (dev->power.runtime_status == RPM_ACTIVE) {
- retval = 1;
- goto out;
- }
- /× 此时设备正在resuming or suspending */
- if (dev->power.runtime_status == RPM_RESUMING
- || dev->power.runtime_status == RPM_SUSPENDING) {
- DEFINE_WAIT(wait);
- /* 如果是异步的,并且不等待,则 设置deferred_resume, 让suspend 后直接resume */
- if (rpmflags & (RPM_ASYNC | RPM_NOWAIT)) {
- if (dev->power.runtime_status == RPM_SUSPENDING)
- dev->power.deferred_resume = true;
- else
- retval = -EINPROGRESS;
- goto out;
- }
- /* Wait for the operation carried out in parallel with us. */
- for (;;) {
- prepare_to_wait(&dev->power.wait_queue, &wait,
- TASK_UNINTERRUPTIBLE);
- if (dev->power.runtime_status != RPM_RESUMING
- && dev->power.runtime_status != RPM_SUSPENDING)
- break;
- spin_unlock_irq(&dev->power.lock);
- schedule();
- spin_lock_irq(&dev->power.lock);
- }
- finish_wait(&dev->power.wait_queue, &wait);
- goto repeat;
- }
- /*
- * See if we can skip waking up the parent. This is safe only if
- * power.no_callbacks is set, because otherwise we don't know whether
- * the resume will actually succeed.
- */
- if (dev->power.no_callbacks && !parent && dev->parent) {
- spin_lock_nested(&dev->parent->power.lock, SINGLE_DEPTH_NESTING);
- if (dev->parent->power.disable_depth > 0
- || dev->parent->power.ignore_children
- || dev->parent->power.runtime_status == RPM_ACTIVE) {
- atomic_inc(&dev->parent->power.child_count);
- spin_unlock(&dev->parent->power.lock);
- goto no_callback; /* Assume success. */
- }
- spin_unlock(&dev->parent->power.lock);
- }
- /* 如果是异步的, 有工作队列负责resume */
- /* Carry out an asynchronous or a synchronous resume. */
- if (rpmflags & RPM_ASYNC) {
- dev->power.request = RPM_REQ_RESUME;
- if (!dev->power.request_pending) {
- dev->power.request_pending = true;
- queue_work(pm_wq, &dev->power.work);
- }
- retval = 0;
- goto out;
- }
- if (!parent && dev->parent) {
- /*
- * Increment the parent's usage counter and resume it if
- * necessary. Not needed if dev is irq-safe; then the
- * parent is permanently resumed.
- */
- parent = dev->parent;
- if (dev->power.irq_safe)
- goto skip_parent;
- spin_unlock(&dev->power.lock);
- /* 增加父设备的引用计数 ×/
- pm_runtime_get_noresume(parent);
- spin_lock(&parent->power.lock);
- /*
- * We can resume if the parent's run-time PM is disabled or it
- * is set to ignore children.
- */
- if (!parent->power.disable_depth
- && !parent->power.ignore_children) {
- /× resume 负设备 ×/
- rpm_resume(parent, 0);
- if (parent->power.runtime_status != RPM_ACTIVE)
- retval = -EBUSY;
- }
- spin_unlock(&parent->power.lock);
- spin_lock(&dev->power.lock);
- if (retval)
- goto out;
- goto repeat;
- }
- skip_parent:
- if (dev->power.no_callbacks)
- goto no_callback; /* Assume success. */
- __update_runtime_status(dev, RPM_RESUMING);
- if (dev->pwr_domain)
- callback = dev->pwr_domain->ops.runtime_resume;
- else if (dev->type && dev->type->pm)
- callback = dev->type->pm->runtime_resume;
- else if (dev->class && dev->class->pm)
- callback = dev->class->pm->runtime_resume;
- else if (dev->bus && dev->bus->pm)
- callback = dev->bus->pm->runtime_resume;
- else
- callback = NULL;
- /× 执行真正的resume函数 ×/
- retval = rpm_callback(callback, dev);
- if (retval) {
- __update_runtime_status(dev, RPM_SUSPENDED);
- pm_runtime_cancel_pending(dev);
- } else {
- no_callback:
- __update_runtime_status(dev, RPM_ACTIVE);
- if (parent)
- atomic_inc(&parent->power.child_count);
- }
- wake_up_all(&dev->power.wait_queue);
- /× 如果正常, 则idle 设备 ×/
- if (!retval)
- rpm_idle(dev, RPM_ASYNC);
- out:
- if (parent && !dev->power.irq_safe) {
- spin_unlock_irq(&dev->power.lock);
- /* idle 父设别 ×/
- pm_runtime_put(parent);
- spin_lock_irq(&dev->power.lock);
- }
- dev_dbg(dev, "%s returns %d\n", __func__, retval);
- return retval;
- }
- /**
- * rpm_idle - Notify device bus type if the device can be suspended.
- * @dev: Device to notify the bus type about.
- * @rpmflags: Flag bits.
- *
- * Check if the device's run-time PM status allows it to be suspended. If
- * another idle notification has been started earlier, return immediately. If
- * the RPM_ASYNC flag is set then queue an idle-notification request; otherwise
- * run the ->runtime_idle() callback directly.
- *
- * This function must be called under dev->power.lock with interrupts disabled.
- */
- static int rpm_idle(struct device *dev, int rpmflags)
- {
- int (*callback)(struct device *);
- int retval;
- retval = rpm_check_suspend_allowed(dev);
- if (retval < 0)
- ; /* Conditions are wrong. */
- /* Idle notifications are allowed only in the RPM_ACTIVE state. */
- else if (dev->power.runtime_status != RPM_ACTIVE)
- retval = -EAGAIN;
- /*
- * Any pending request other than an idle notification takes
- * precedence over us, except that the timer may be running.
- */
- else if (dev->power.request_pending &&
- dev->power.request > RPM_REQ_IDLE)
- retval = -EAGAIN;
- /* Act as though RPM_NOWAIT is always set. */
- else if (dev->power.idle_notification)
- retval = -EINPROGRESS;
- if (retval)
- goto out;
- /* Pending requests need to be canceled. */
- dev->power.request = RPM_REQ_NONE;
- if (dev->power.no_callbacks) {
- /* Assume ->runtime_idle() callback would have suspended. */
- retval = rpm_suspend(dev, rpmflags);
- goto out;
- }
- /* Carry out an asynchronous or a synchronous idle notification. */
- if (rpmflags & RPM_ASYNC) {
- dev->power.request = RPM_REQ_IDLE;
- if (!dev->power.request_pending) {
- dev->power.request_pending = true;
- queue_work(pm_wq, &dev->power.work);
- }
- goto out;
- }
- dev->power.idle_notification = true;
- if (dev->pwr_domain)
- callback = dev->pwr_domain->ops.runtime_idle;
- else if (dev->type && dev->type->pm)
- callback = dev->type->pm->runtime_idle;
- else if (dev->class && dev->class->pm)
- callback = dev->class->pm->runtime_idle;
- else if (dev->bus && dev->bus->pm)
- callback = dev->bus->pm->runtime_idle;
- else
- callback = NULL;
- if (callback) {
- spin_unlock_irq(&dev->power.lock);
- callback(dev);
- spin_lock_irq(&dev->power.lock);
- }
- dev->power.idle_notification = false;
- wake_up_all(&dev->power.wait_queue);
- out:
- return retval;
- }
- /**
- * rpm_suspend - Carry out run-time suspend of given device.
- * @dev: Device to suspend.
- * @rpmflags: Flag bits.
- *
- * Check if the device's run-time PM status allows it to be suspended. If
- * another suspend has been started earlier, either return immediately or wait
- * for it to finish, depending on the RPM_NOWAIT and RPM_ASYNC flags. Cancel a
- * pending idle notification. If the RPM_ASYNC flag is set then queue a
- * suspend request; otherwise run the ->runtime_suspend() callback directly.
- * If a deferred resume was requested while the callback was running then carry
- * it out; otherwise send an idle notification for the device (if the suspend
- * failed) or for its parent (if the suspend succeeded).
- * If ->runtime_suspend failed with -EAGAIN or -EBUSY, and if the RPM_AUTO
- * flag is set and the next autosuspend-delay expiration time is in the
- * future, schedule another autosuspend attempt.
- *
- * This function must be called under dev->power.lock with interrupts disabled.
- */
- static int rpm_suspend(struct device *dev, int rpmflags)
- __releases(&dev->power.lock) __acquires(&dev->power.lock)
- {
- int (*callback)(struct device *);
- struct device *parent = NULL;
- int retval;
- dev_dbg(dev, "%s flags 0x%x\n", __func__, rpmflags);
- repeat:
- retval = rpm_check_suspend_allowed(dev);
- if (retval < 0)
- ; /* Conditions are wrong. */
- /* Synchronous suspends are not allowed in the RPM_RESUMING state. */
- else if (dev->power.runtime_status == RPM_RESUMING &&
- !(rpmflags & RPM_ASYNC))
- retval = -EAGAIN;
- 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);
- if (dev->power.runtime_status == RPM_SUSPENDING) {
- DEFINE_WAIT(wait);
- if (rpmflags & (RPM_ASYNC | RPM_NOWAIT)) {
- retval = -EINPROGRESS;
- goto out;
- }
- /* Wait for the other suspend running in parallel with us. */
- for (;;) {
- prepare_to_wait(&dev->power.wait_queue, &wait,
- TASK_UNINTERRUPTIBLE);
- if (dev->power.runtime_status != RPM_SUSPENDING)
- break;
- spin_unlock_irq(&dev->power.lock);
- schedule();
- spin_lock_irq(&dev->power.lock);
- }
- finish_wait(&dev->power.wait_queue, &wait);
- goto repeat;
- }
- dev->power.deferred_resume = false;
- if (dev->power.no_callbacks)
- goto no_callback; /* Assume success. */
- /* Carry out an asynchronous or a synchronous suspend. */
- if (rpmflags & RPM_ASYNC) {
- 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);
- }
- goto out;
- }
- __update_runtime_status(dev, RPM_SUSPENDING);
- if (dev->pwr_domain)
- callback = dev->pwr_domain->ops.runtime_suspend;
- else if (dev->type && dev->type->pm)
- callback = dev->type->pm->runtime_suspend;
- else if (dev->class && dev->class->pm)
- callback = dev->class->pm->runtime_suspend;
- else if (dev->bus && dev->bus->pm)
- callback = dev->bus->pm->runtime_suspend;
- else
- callback = NULL;
- retval = rpm_callback(callback, dev);
- if (retval) {
- __update_runtime_status(dev, RPM_ACTIVE);
- dev->power.deferred_resume = 0;
- if (retval == -EAGAIN || retval == -EBUSY) {
- dev->power.runtime_error = 0;
- /*
- * If the callback routine failed an autosuspend, and
- * if the last_busy time has been updated so that there
- * is a new autosuspend expiration time, automatically
- * reschedule another autosuspend.
- */
- if ((rpmflags & RPM_AUTO) &&
- pm_runtime_autosuspend_expiration(dev) != 0)
- goto repeat;
- } else {
- pm_runtime_cancel_pending(dev);
- }
- } else {
- no_callback:
- __update_runtime_status(dev, RPM_SUSPENDED);
- pm_runtime_deactivate_timer(dev);
- if (dev->parent) {
- parent = dev->parent;
- atomic_add_unless(&parent->power.child_count, -1, 0);
- }
- }
- wake_up_all(&dev->power.wait_queue);
- if (dev->power.deferred_resume) {
- rpm_resume(dev, 0);
- retval = -EAGAIN;
- goto out;
- }
- /* Maybe the parent is now able to suspend. */
- if (parent && !parent->power.ignore_children && !dev->power.irq_safe) {
- spin_unlock(&dev->power.lock);
- spin_lock(&parent->power.lock);
- rpm_idle(parent, RPM_ASYNC);
- spin_unlock(&parent->power.lock);
- spin_lock(&dev->power.lock);
- }
- out:
- dev_dbg(dev, "%s returns %d\n", __func__, retval);
- return retval;
- }
- /**
- * pm_generic_runtime_idle - Generic runtime idle callback for subsystems.
- * @dev: Device to handle.
- *
- * If PM operations are defined for the @dev's driver and they include
- * ->runtime_idle(), execute it and return its error code, if nonzero.
- * Otherwise, execute pm_runtime_suspend() for the device and return 0.
- */
- int pm_generic_runtime_idle(struct device *dev)
- {
- const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
- if (pm && pm->runtime_idle) {
- int ret = pm->runtime_idle(dev);
- if (ret)
- return ret;
- }
- pm_runtime_suspend(dev);
- return 0;
- }
设备接口的调用:
probe:
pm_runtime_use_autosuspend(&pdev->dev);
pm_runtime_set_autosuspend_delay(&pdev->dev,
omap_up_info->auto_sus_timeout);
if (device_may_wakeup(&pdev->dev))
pm_runtime_enable(&pdev->dev);
pm_runtime_irq_safe(&pdev->dev);
- int omap_uart_enable(u8 uart_num)
- {
- if (uart_num > OMAP_MAX_HSUART_PORTS)
- return -ENODEV;
- if (!ui[uart_num - 1])
- return -ENODEV;
- pm_runtime_get_sync(&ui[uart_num - 1]->pdev->dev);
- return 0;
- }
- int omap_uart_disable(u8 uart_num)
- {
- if (uart_num > OMAP_MAX_HSUART_PORTS)
- return -ENODEV;
- if (!ui[uart_num - 1])
- return -ENODEV;
- pm_runtime_put_sync_suspend(&ui[uart_num - 1]->pdev->dev);
- return 0;
- }
阅读(2511) | 评论(1) | 转发(0) |