From patchwork Mon Nov 25 12:40:21 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ulf Hansson X-Patchwork-Id: 3229901 X-Patchwork-Delegate: bhelgaas@google.com Return-Path: X-Original-To: patchwork-linux-pci@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork2.web.kernel.org (Postfix) with ESMTP id C37F7C045B for ; Mon, 25 Nov 2013 12:40:43 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id E691320278 for ; Mon, 25 Nov 2013 12:40:38 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 601C82022F for ; Mon, 25 Nov 2013 12:40:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753482Ab3KYMke (ORCPT ); Mon, 25 Nov 2013 07:40:34 -0500 Received: from mail-bk0-f44.google.com ([209.85.214.44]:44604 "EHLO mail-bk0-f44.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753273Ab3KYMkd (ORCPT ); Mon, 25 Nov 2013 07:40:33 -0500 Received: by mail-bk0-f44.google.com with SMTP id d7so1976958bkh.3 for ; Mon, 25 Nov 2013 04:40:32 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=rQYr9BSCftqaPrUD/rP29IY2HQ/40C9hr4HrnsVvdtk=; b=PRgFAK9bj3R1LN24dWs+x5kdrm1WKj2lGhXSgjfg+fNjfZ6lg6PkFbfbptOFsjdCyf OXMTz0zzsJW5hazbyM0s2tVLrtkcf2kdrRIQPhk5PBpuXNksI0u/029airXWiDhegMz5 Nu/LMRlgwFfZ1WMyWbat3C2KmhkqzcELIR6r9YZbfgftSUneog3PFVEeQ7IVF1Qz+Dbf CidNLaubvs691iAHvoIgPS6CI77LxESaHDe5HZOKfi8Esm95HgNElAzT3bD5VwufiC8Z voc0HkpdKXrZxrsplOobPRevKQwe2gG5K7MhwiVdokB3XV+3j2rmliIsVJiZUSPImq4H i0Gg== X-Gm-Message-State: ALoCoQmpUeUesjcAb7MpxK5uVHAAi8EjrtvzRQC8DTZqiZCjO6wMLTnPSkvxA0ZGL6XaFWvEpjKn X-Received: by 10.205.33.16 with SMTP id sm16mr1238629bkb.55.1385383232233; Mon, 25 Nov 2013 04:40:32 -0800 (PST) Received: from localhost.localdomain ([85.235.11.236]) by mx.google.com with ESMTPSA id pk7sm46072683bkb.2.2013.11.25.04.40.30 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 25 Nov 2013 04:40:31 -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 v2] PM / Sleep: Add pm_generic functions to re-use runtime PM callbacks Date: Mon, 25 Nov 2013 13:40:21 +0100 Message-Id: <1385383221-20372-1-git-send-email-ulf.hansson@linaro.org> X-Mailer: git-send-email 1.7.9.5 Sender: linux-pci-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pci@vger.kernel.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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 Cc: Alan Stern Signed-off-by: Ulf Hansson --- 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);