From patchwork Fri Nov 22 12:43:55 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ulf Hansson X-Patchwork-Id: 21680 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-yh0-f69.google.com (mail-yh0-f69.google.com [209.85.213.69]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 8FD782096D for ; Fri, 22 Nov 2013 12:44:08 +0000 (UTC) Received: by mail-yh0-f69.google.com with SMTP id a41sf398806yho.0 for ; Fri, 22 Nov 2013 04:44:07 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:delivered-to:from:to:cc:subject :date:message-id:x-original-sender:x-original-authentication-results :precedence:mailing-list:list-id:list-post:list-help:list-archive :list-unsubscribe; bh=ZdCb7ht1S2+w8jo7XzmCNJIg0pE2RcHU5NL68FVgaVo=; b=Bk6dIcwjdKj8fCJLZ95kmGVMjDFCWnzEUXkyf5uVirIGk2Hx9arQ5TNjmc5JGVDwbD GPe9jz82Xu8C7IfC2Qx2IG7DPQ5CIuHNpJcSeafMsO7sBgsvD8mYow7BCNb8HpFSOmdd ADu9L33rxEDYuelOh5zjUnFMTZneLl/VVA/cp3yr9eT6P9s8LVfr8F920prhhNF8l8kU 4L0cbaAQDiEe9FlIAEgZQ6DFZ0jl60xzGCosJgvlUjSB8tudyVI8i+GMfPYSOLK4h26x xyMeRUwDRT6DgEnlXT38gViETL8QWsTX6NjqegXk8P8zH3VZSo9YyhCvW3vduqK7pREM Avhg== X-Gm-Message-State: ALoCoQmrYM3158D/JibFOnW+XGuUYsJwwQj2RyTZk7NjeukTATusgPacXjgu6zXIwES4BgiWp40D X-Received: by 10.52.31.69 with SMTP id y5mr3464879vdh.4.1385124247568; Fri, 22 Nov 2013 04:44:07 -0800 (PST) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.49.121.170 with SMTP id ll10ls978081qeb.34.gmail; Fri, 22 Nov 2013 04:44:07 -0800 (PST) X-Received: by 10.221.51.206 with SMTP id vj14mr11279350vcb.17.1385124247456; Fri, 22 Nov 2013 04:44:07 -0800 (PST) Received: from mail-ve0-f173.google.com (mail-ve0-f173.google.com [209.85.128.173]) by mx.google.com with ESMTPS id xt2si12623983vcb.33.2013.11.22.04.44.07 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Fri, 22 Nov 2013 04:44:07 -0800 (PST) Received-SPF: neutral (google.com: 209.85.128.173 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) client-ip=209.85.128.173; Received: by mail-ve0-f173.google.com with SMTP id oz11so841564veb.32 for ; Fri, 22 Nov 2013 04:44:07 -0800 (PST) X-Received: by 10.220.11.7 with SMTP id r7mr11280942vcr.12.1385124247365; Fri, 22 Nov 2013 04:44:07 -0800 (PST) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patches@linaro.org Received: by 10.220.174.196 with SMTP id u4csp37392vcz; Fri, 22 Nov 2013 04:44:06 -0800 (PST) X-Received: by 10.112.236.103 with SMTP id ut7mr1286305lbc.39.1385124245968; Fri, 22 Nov 2013 04:44:05 -0800 (PST) Received: from mail-la0-f48.google.com (mail-la0-f48.google.com [209.85.215.48]) by mx.google.com with ESMTPS id x7si11328634lag.96.2013.11.22.04.44.05 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Fri, 22 Nov 2013 04:44:05 -0800 (PST) Received-SPF: neutral (google.com: 209.85.215.48 is neither permitted nor denied by best guess record for domain of ulf.hansson@linaro.org) client-ip=209.85.215.48; Received: by mail-la0-f48.google.com with SMTP id n7so889245lam.35 for ; Fri, 22 Nov 2013 04:44:05 -0800 (PST) X-Received: by 10.112.134.71 with SMTP id pi7mr985898lbb.44.1385124245245; Fri, 22 Nov 2013 04:44:05 -0800 (PST) Received: from linaro-ulf.lan (90-231-160-185-no158.tbcn.telia.com. [90.231.160.185]) by mx.google.com with ESMTPSA id mq10sm25601604lbb.12.2013.11.22.04.44.00 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Fri, 22 Nov 2013 04:44:04 -0800 (PST) From: Ulf Hansson To: "Rafael J. Wysocki" , Len Brown , Pavel Machek , linux-pm@vger.kernel.org Cc: Greg Kroah-Hartman , linux-pci@vger.kernel.org, linux-usb@vger.kernel.org, Ulf Hansson , Kevin Hilman , Alan Stern Subject: [PATCH] PM / Sleep: Add pm_generic functions to re-use runtime PM callbacks Date: Fri, 22 Nov 2013 13:43:55 +0100 Message-Id: <1385124235-25484-1-git-send-email-ulf.hansson@linaro.org> X-Mailer: git-send-email 1.7.9.5 X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: ulf.hansson@linaro.org X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 209.85.128.173 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Precedence: list Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org List-ID: X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , To put devices into low power state during sleep, it sometimes makes sense at subsystem-level to re-use device's runtime PM callbacks. The PM core will at device_suspend_late disable runtime PM, after that we can safely operate on these callbacks. At suspend_late the device will be put into low power state by invoking the device's runtime_suspend callback, unless the runtime status is already suspended. At resume_early the state is restored by invoking the device's runtime_resume callback. Soon after the PM core will re-enable runtime PM before returning from device_resume_early. These are new pm_generic functions, which are supposed to be used in pairs: - pm_generic_suspend_late_runtime / pm_generic_resume_early_runtime - pm_generic_freeze_late_runtime / pm_generic_thaw_early_runtime - pm_generic_poweroff_late_runtime / pm_generic_restore_early_runtime Do note that these new pm_generic late and early callbacks will work smothly even with and without CONFIG_PM_RUNTIME, as long as the runtime PM callbacks are implemented inside CONFIG_PM instead of CONFIG_PM_RUNTIME. A special thanks to Alan Stern who came up with this idea. Cc: Kevin Hilman Cc: Alan Stern Signed-off-by: Ulf Hansson --- drivers/base/power/generic_ops.c | 130 ++++++++++++++++++++++++++++++++++++++ include/linux/pm.h | 6 ++ 2 files changed, 136 insertions(+) diff --git a/drivers/base/power/generic_ops.c b/drivers/base/power/generic_ops.c index 5ee030a..b8d31f6 100644 --- a/drivers/base/power/generic_ops.c +++ b/drivers/base/power/generic_ops.c @@ -93,6 +93,49 @@ int pm_generic_suspend_late(struct device *dev) EXPORT_SYMBOL_GPL(pm_generic_suspend_late); /** + * pm_generic_suspend_late_runtime - Generic suspend_late callback for + * subsystems that wants to use runtime_suspend callbacks at suspend_late. + * @dev: Device to suspend. + */ +int pm_generic_suspend_late_runtime(struct device *dev) +{ + int (*callback)(struct device *); + int ret = 0; + + /* + * PM core has disabled runtime PM in device_suspend_late, thus we can + * safely check the device's runtime status and decide whether + * additional actions are needed to put the device into low power state. + * If so, we invoke the device's runtime_suspend callback. + * For the !CONFIG_PM_RUNTIME case, pm_runtime_status_suspended() always + * returns false and therefore the runtime_suspend callback will be + * invoked. + */ + if (pm_runtime_status_suspended(dev)) + return 0; + + if (dev->pm_domain) + callback = dev->pm_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; + + if (!callback && dev->driver && dev->driver->pm) + callback = dev->driver->pm->runtime_suspend; + + if (callback) + ret = callback(dev); + + return ret; +} +EXPORT_SYMBOL_GPL(pm_generic_suspend_late_runtime); + +/** * pm_generic_suspend - Generic suspend callback for subsystems. * @dev: Device to suspend. */ @@ -129,6 +172,17 @@ int pm_generic_freeze_late(struct device *dev) EXPORT_SYMBOL_GPL(pm_generic_freeze_late); /** + * pm_generic_freeze_late_runtime - Generic freeze_late callback for + * subsystems that wants to use runtime_suspend callbacks at freeze_late. + * @dev: Device to freeze. + */ +int pm_generic_freeze_late_runtime(struct device *dev) +{ + return pm_generic_suspend_late_runtime(dev); +} +EXPORT_SYMBOL_GPL(pm_generic_freeze_late_runtime); + +/** * pm_generic_freeze - Generic freeze callback for subsystems. * @dev: Device to freeze. */ @@ -165,6 +219,17 @@ int pm_generic_poweroff_late(struct device *dev) EXPORT_SYMBOL_GPL(pm_generic_poweroff_late); /** + * pm_generic_poweroff_late_runtime - Generic poweroff_late callback for + * subsystems that wants to use runtime_suspend callbacks at poweroff_late. + * @dev: Device to handle. + */ +int pm_generic_poweroff_late_runtime(struct device *dev) +{ + return pm_generic_suspend_late_runtime(dev); +} +EXPORT_SYMBOL_GPL(pm_generic_poweroff_late_runtime); + +/** * pm_generic_poweroff - Generic poweroff callback for subsystems. * @dev: Device to handle. */ @@ -201,6 +266,17 @@ int pm_generic_thaw_early(struct device *dev) EXPORT_SYMBOL_GPL(pm_generic_thaw_early); /** + * pm_generic_thaw_early_runtime - Generic thaw_early callback for subsystems + * that wants to use runtime_resume callbacks at thaw_early. + * @dev: Device to thaw. + */ +int pm_generic_thaw_early_runtime(struct device *dev) +{ + return pm_generic_resume_early_runtime(dev); +} +EXPORT_SYMBOL_GPL(pm_generic_thaw_early_runtime); + +/** * pm_generic_thaw - Generic thaw callback for subsystems. * @dev: Device to thaw. */ @@ -237,6 +313,49 @@ int pm_generic_resume_early(struct device *dev) EXPORT_SYMBOL_GPL(pm_generic_resume_early); /** + * pm_generic_resume_early_runtime - Generic resume_early callback for + * subsystems that wants to use runtime_resume callbacks at resume_early. + * @dev: Device to resume. + */ +int pm_generic_resume_early_runtime(struct device *dev) +{ + int (*callback)(struct device *); + int ret = 0; + + /* + * PM core has not yet enabled runtime PM in device_resume_early, + * thus we can safely check the device's runtime status and restore the + * previous state we had in device_suspend_late. If restore is needed + * we invoke the device's runtime_resume callback. + * For the !CONFIG_PM_RUNTIME case, pm_runtime_status_suspended() always + * returns false and therefore the runtime_resume callback will be + * invoked. + */ + if (pm_runtime_status_suspended(dev)) + return 0; + + if (dev->pm_domain) + callback = dev->pm_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; + + if (!callback && dev->driver && dev->driver->pm) + callback = dev->driver->pm->runtime_resume; + + if (callback) + ret = callback(dev); + + return ret; +} +EXPORT_SYMBOL_GPL(pm_generic_resume_early_runtime); + +/** * pm_generic_resume - Generic resume callback for subsystems. * @dev: Device to resume. */ @@ -273,6 +392,17 @@ int pm_generic_restore_early(struct device *dev) EXPORT_SYMBOL_GPL(pm_generic_restore_early); /** + * pm_generic_restore_early_runtime - Generic restore_early callback for + * subsystems that wants to use runtime_resume callbacks at restore_early. + * @dev: Device to restore + */ +int pm_generic_restore_early_runtime(struct device *dev) +{ + return pm_generic_resume_early_runtime(dev); +} +EXPORT_SYMBOL_GPL(pm_generic_restore_early_runtime); + +/** * pm_generic_restore - Generic restore callback for subsystems. * @dev: Device to restore. */ diff --git a/include/linux/pm.h b/include/linux/pm.h index a224c7f..c7c2db7 100644 --- a/include/linux/pm.h +++ b/include/linux/pm.h @@ -656,22 +656,28 @@ extern void dpm_for_each_dev(void *data, void (*fn)(struct device *, void *)); extern int pm_generic_prepare(struct device *dev); extern int pm_generic_suspend_late(struct device *dev); +extern int pm_generic_suspend_late_runtime(struct device *dev); extern int pm_generic_suspend_noirq(struct device *dev); extern int pm_generic_suspend(struct device *dev); extern int pm_generic_resume_early(struct device *dev); +extern int pm_generic_resume_early_runtime(struct device *dev); extern int pm_generic_resume_noirq(struct device *dev); extern int pm_generic_resume(struct device *dev); extern int pm_generic_freeze_noirq(struct device *dev); extern int pm_generic_freeze_late(struct device *dev); +extern int pm_generic_freeze_late_runtime(struct device *dev); extern int pm_generic_freeze(struct device *dev); extern int pm_generic_thaw_noirq(struct device *dev); extern int pm_generic_thaw_early(struct device *dev); +extern int pm_generic_thaw_early_runtime(struct device *dev); extern int pm_generic_thaw(struct device *dev); extern int pm_generic_restore_noirq(struct device *dev); extern int pm_generic_restore_early(struct device *dev); +extern int pm_generic_restore_early_runtime(struct device *dev); extern int pm_generic_restore(struct device *dev); extern int pm_generic_poweroff_noirq(struct device *dev); extern int pm_generic_poweroff_late(struct device *dev); +extern int pm_generic_poweroff_late_runtime(struct device *dev); extern int pm_generic_poweroff(struct device *dev); extern void pm_generic_complete(struct device *dev);