From: Ulf Hansson <ulf.hansson-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org>
To: "Rafael J. Wysocki" <rjw-LthD3rsA81gm4RdzfppkhA@public.gmane.org>,
Len Brown <len.brown-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>,
Pavel Machek <pavel-+ZI9xUNit7I@public.gmane.org>,
linux-pm-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
Cc: Greg Kroah-Hartman
<gregkh-hQyY1W1yCW8ekmWlsbkhG0B+6BGkLq7r@public.gmane.org>,
linux-pci-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
linux-usb-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
Ulf Hansson <ulf.hansson-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org>,
Kevin Hilman <khilman-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org>,
Alan Stern
<stern-nwvwT67g6+6dFdvTe/nMLpVzexx5G7lz@public.gmane.org>
Subject: [PATCH v2] PM / Sleep: Add pm_generic functions to re-use runtime PM callbacks
Date: Mon, 25 Nov 2013 13:40:21 +0100 [thread overview]
Message-ID: <1385383221-20372-1-git-send-email-ulf.hansson@linaro.org> (raw)
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.
The new pm_generic functions are named pm_generic_suspend_late_runtime
and pm_generic_resume_early_runtime, they are supposed to be used in
pairs.
Do note that these new generic 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 <khilman-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org>
Cc: Alan Stern <stern-nwvwT67g6+6dFdvTe/nMLpVzexx5G7lz@public.gmane.org>
Signed-off-by: Ulf Hansson <ulf.hansson-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org>
---
drivers/base/power/generic_ops.c | 86 ++++++++++++++++++++++++++++++++++++++
include/linux/pm.h | 2 +
2 files changed, 88 insertions(+)
diff --git a/drivers/base/power/generic_ops.c b/drivers/base/power/generic_ops.c
index 5ee030a..3132768 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.
*/
@@ -237,6 +280,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.
*/
diff --git a/include/linux/pm.h b/include/linux/pm.h
index a224c7f..5bce0d4 100644
--- a/include/linux/pm.h
+++ b/include/linux/pm.h
@@ -656,9 +656,11 @@ 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);
--
1.7.9.5
--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
next reply other threads:[~2013-11-25 12:40 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-11-25 12:40 Ulf Hansson [this message]
2013-11-25 23:10 ` [PATCH v2] PM / Sleep: Add pm_generic functions to re-use runtime PM callbacks Rafael J. Wysocki
[not found] ` <5827021.HqYFigfrCJ-sKB8Sp2ER+y1GS7QM15AGw@public.gmane.org>
2013-11-26 9:48 ` Ulf Hansson
2013-11-26 20:34 ` Rafael J. Wysocki
2013-11-26 21:09 ` Alan Stern
2013-11-26 23:44 ` Ulf Hansson
2013-11-27 0:53 ` Rafael J. Wysocki
[not found] ` <3028580.oiUVTDjyao-sKB8Sp2ER+y1GS7QM15AGw@public.gmane.org>
2013-11-27 9:59 ` Ulf Hansson
[not found] ` <CAPDyKFow3Y7bWAWNxQyrKeHizk0TSoCuPLHD=p_1D=CAYQpm9A-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2013-11-27 14:12 ` Rafael J. Wysocki
2013-11-27 16:05 ` Ulf Hansson
[not found] ` <CAPDyKFp5gz8Jku=g9OqRiE6tojcmodaCdYaFWqnrzYwThhDMog-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2013-11-27 16:06 ` Ulf Hansson
2013-11-27 0:37 ` Rafael J. Wysocki
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1385383221-20372-1-git-send-email-ulf.hansson@linaro.org \
--to=ulf.hansson-qsej5fyqhm4dnm+yrofe0a@public.gmane.org \
--cc=gregkh-hQyY1W1yCW8ekmWlsbkhG0B+6BGkLq7r@public.gmane.org \
--cc=khilman-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org \
--cc=len.brown-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org \
--cc=linux-pci-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
--cc=linux-pm-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
--cc=linux-usb-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
--cc=pavel-+ZI9xUNit7I@public.gmane.org \
--cc=rjw-LthD3rsA81gm4RdzfppkhA@public.gmane.org \
--cc=stern-nwvwT67g6+6dFdvTe/nMLpVzexx5G7lz@public.gmane.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).