From patchwork Thu May 8 12:45:07 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tomasz Figa X-Patchwork-Id: 4136011 Return-Path: X-Original-To: patchwork-linux-arm@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 42F59BFF02 for ; Thu, 8 May 2014 12:51:25 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id E73922024F for ; Thu, 8 May 2014 12:51:23 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.9]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 9AA43201F9 for ; Thu, 8 May 2014 12:51:22 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1WiNke-0008AM-UX; Thu, 08 May 2014 12:48:40 +0000 Received: from mailout4.w1.samsung.com ([210.118.77.14]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1WiNka-00087l-SG for linux-arm-kernel@lists.infradead.org; Thu, 08 May 2014 12:48:38 +0000 Received: from eucpsbgm1.samsung.com (unknown [203.254.199.244]) by mailout4.w1.samsung.com (Oracle Communications Messaging Server 7u4-24.01(7.0.4.24.0) 64bit (built Nov 17 2011)) with ESMTP id <0N59006FAA89U700@mailout4.w1.samsung.com> for linux-arm-kernel@lists.infradead.org; Thu, 08 May 2014 13:48:09 +0100 (BST) X-AuditID: cbfec7f4-b7fb36d000006ff7-93-536b7d0da2ea Received: from eusync4.samsung.com ( [203.254.199.214]) by eucpsbgm1.samsung.com (EUCPMTA) with SMTP id 3E.1A.28663.D0D7B635; Thu, 08 May 2014 13:48:13 +0100 (BST) Received: from AMDC1227.digital.local ([106.116.147.199]) by eusync4.samsung.com (Oracle Communications Messaging Server 7u4-24.01(7.0.4.24.0) 64bit (built Nov 17 2011)) with ESMTPA id <0N5900EP6A88X460@eusync4.samsung.com>; Thu, 08 May 2014 13:48:13 +0100 (BST) From: Tomasz Figa To: linux-pm@vger.kernel.org Subject: [PATCH v4 1/3] base: power: Add generic OF-based power domain look-up Date: Thu, 08 May 2014 14:45:07 +0200 Message-id: <1399553109-6487-2-git-send-email-t.figa@samsung.com> X-Mailer: git-send-email 1.9.2 In-reply-to: <1399553109-6487-1-git-send-email-t.figa@samsung.com> References: <1399553109-6487-1-git-send-email-t.figa@samsung.com> X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFvrJLMWRmVeSWpSXmKPExsVy+t/xa7q8tdnBBotOCllsnLGe1WL+kXOs Fv1vFrJaNC9ez2bRu+Aqm8XXwysYLWZN2ctksenxNVaLy7vmsFl87j3CaDHj/D4mi9uXeS3e /H7BbrH2yF12i6XXLzJZ3D11lM1iwvS1LBZb551jtOhf2Mtk0br3CLvFjzPdLBavDraxWKyf 8ZrFYtWuP4wWx9eGO0h6rJm3htGjpbmHzeNyXy+Tx85Zd9k9Fu95yeSxaVUnm8eda3vYPPbP XcPusXlJvUffllWMHo8WtzB6rFj9nd3j8yY5j41zQwP4orhsUlJzMstSi/TtErgyrnVcZCu4 W1hxcP13lgbGCTFdjJwcEgImEkf+zGaCsMUkLtxbz9bFyMUhJLCUUWLS41dMEE4fk8TKC+0s IFVsAmoSnxsesYHYIgIyElOv7GcFKWIWWM8u8ejMDEaQhLCAn8SsWevAbBYBVYlza7vBbF4B R4nn21tYIdbJSfx/uQJoAwcHp4CTxKq9uiBhIaCS+d1v2Scw8i5gZFjFKJpamlxQnJSea6hX nJhbXJqXrpecn7uJERJdX3YwLj5mdYhRgINRiYf3RXRGsBBrYllxZe4hRgkOZiUR3iuV2cFC vCmJlVWpRfnxRaU5qcWHGJk4OKUaGBdtYFdpTLZUPLX95y6XvT9rVUwabtluTHm2YuH3P/kB 16ZK/TVQnSFkHbHf9HDNjamP9j6e6+1y8+yWvV9zzuTwquvd2FsTnL9LK/ZMfOzsT9PM/hfO urj6woQeIaOUXynpH381HVzynUV1yqX5kYe/zFS9+9i0YPomBe4VV6T5p9/JebA5yyFNiaU4 I9FQi7moOBEA//W9PIwCAAA= X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20140508_054837_232935_66DEC689 X-CRM114-Status: GOOD ( 27.12 ) X-Spam-Score: -5.7 (-----) Cc: Mark Rutland , Ulf Hansson , Pavel Machek , Len Brown , Tomasz Figa , Tomasz Figa , Philipp Zabel , Kukjin Kim , Marek Szyprowski , linux-samsung-soc@vger.kernel.org, Russell King - ARM Linux , Stephen Warren , Lorenzo Pieralisi , devicetree@vger.kernel.org, Kevin Hilman , Pawel Moll , Bartlomiej Zolnierkiewicz , "Rafael J. Wysocki" , Rob Herring , linux-arm-kernel@lists.infradead.org, Greg Kroah-Hartman , Stephen Boyd , linux-kernel@vger.kernel.org, Kumar Gala X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Spam-Status: No, score=-2.5 required=5.0 tests=BAYES_00,RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable 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 This patch introduces generic code to perform power domain look-up using device tree and automatically bind devices to their power domains. Generic device tree binding is introduced to specify power domains of devices in their device tree nodes. Backwards compatibility with legacy Samsung-specific power domain bindings is provided, but for now the new code is not compiled when CONFIG_ARCH_EXYNOS is selected to avoid collision with legacy code. This will change as soon as Exynos power domain code gets converted to use the generic framework in further patch. Signed-off-by: Tomasz Figa Reviewed-by: Mark Brown Reviewed-by: Kevin Hilman Reviewed-by: Philipp Zabel [on i.MX6 GK802] Tested-by: Philipp Zabel Reviewed-by: Ulf Hansson --- .../devicetree/bindings/power/power_domain.txt | 51 ++++ drivers/base/power/domain.c | 283 +++++++++++++++++++++ include/linux/pm_domain.h | 46 ++++ kernel/power/Kconfig | 4 + 4 files changed, 384 insertions(+) create mode 100644 Documentation/devicetree/bindings/power/power_domain.txt diff --git a/Documentation/devicetree/bindings/power/power_domain.txt b/Documentation/devicetree/bindings/power/power_domain.txt new file mode 100644 index 0000000..303d0a9 --- /dev/null +++ b/Documentation/devicetree/bindings/power/power_domain.txt @@ -0,0 +1,51 @@ +* Generic power domains + +System on chip designs are often divided into multiple power domains that +can be used for power gating of selected IP blocks for power saving by +reduced leakage current. + +This device tree binding can be used to bind power domain consumer devices +with their power domains provided by power domain providers. A power domain +provider can be represented by any node in the device tree and can provide +one or more power domains. A consumer node can refer to the provider by +a phandle and a set of phandle arguments (so called power domain specifier) +of length specified by #power-domain-cells property in the power domain +provider node. + +==Power domain providers== + +Required properties: + - #power-domain-cells : Number of cells in a power domain specifier; + Typically 0 for nodes representing a single power domain and 1 for nodes + providing multiple power domains (e.g. power controllers), but can be + any value as specified by device tree binding documentation of particular + provider. + +Example: + + power: power-controller@12340000 { + compatible = "foo,power-controller"; + reg = <0x12340000 0x1000>; + #power-domain-cells = <1>; + }; + +The node above defines a power controller that is a power domain provider +and expects one cell as its phandle argument. + +==Power domain consumers== + +Required properties: + - power-domains : A phandle and power domain specifier as defined by bindings + of power controller specified by phandle. + +Example: + + leaky-device@12350000 { + compatible = "foo,i-leak-current"; + reg = <0x12350000 0x1000>; + power-domains = <&power 0>; + }; + +The node above defines a typical power domain consumer device, which is located +inside power domain with index 0 of power controller represented by node with +label "power". diff --git a/drivers/base/power/domain.c b/drivers/base/power/domain.c index ae098a2..de88e1e 100644 --- a/drivers/base/power/domain.c +++ b/drivers/base/power/domain.c @@ -8,6 +8,7 @@ #include #include +#include #include #include #include @@ -2189,3 +2190,285 @@ void pm_genpd_init(struct generic_pm_domain *genpd, list_add(&genpd->gpd_list_node, &gpd_list); mutex_unlock(&gpd_list_lock); } + +#ifdef CONFIG_PM_GENERIC_DOMAINS_OF +/* + * Device Tree based power domain providers. + * + * The code below implements generic device tree based power domain providers + * that bind device tree nodes with generic power domains registered in the + * system. + * + * Any driver that registers generic power domains and need to support binding + * of devices to these domains is supposed to register a power domain provider, + * which maps a power domain specifier retrieved from device tree to a power + * domain. + * + * Two simple mapping functions have been provided for convenience: + * - of_genpd_xlate_simple() for 1:1 device tree node to domain mapping, + * - of_genpd_xlate_onecell() for mapping of multiple domains per node + * by index. + */ + +/** + * struct of_genpd_provider - Power domain provider registration structure + * @link: Entry in global list of domain providers + * @node: Pointer to device tree node of domain provider + * @xlate: Provider-specific xlate callback mapping a set of specifier cells + * into a power domain. + * @data: context pointer to be passed into @xlate callback + */ +struct of_genpd_provider { + struct list_head link; + + struct device_node *node; + genpd_xlate_t xlate; + void *data; +}; + +/* List of registered power domain providers. */ +static LIST_HEAD(of_genpd_providers); +/* Mutex to protect the list above. */ +static DEFINE_MUTEX(of_genpd_mutex); + +/** + * of_genpd_xlate_simple() - Xlate function for direct node-domain mapping + * @genpdspec: OF phandle args to map into a power domain + * @data: xlate function private data - pointer to struct generic_pm_domain + * + * This is a generic xlate function that can be used to model power domains + * that have their own device tree nodes. The private data of xlate function + * needs to be a valid pointer to struct generic_pm_domain. + */ +struct generic_pm_domain *of_genpd_xlate_simple( + struct of_phandle_args *genpdspec, + void *data) +{ + if (genpdspec->args_count != 0) + return ERR_PTR(-EINVAL); + return data; +} +EXPORT_SYMBOL_GPL(of_genpd_xlate_simple); + +/** + * of_genpd_xlate_onecell() - Xlate function for providers using single index. + * @genpdspec: OF phandle args to map into a power domain + * @data: xlate function private data - pointer to struct genpd_onecell_data + * + * This is a generic xlate function that can be used to model simple power + * domain controllers that have one device tree node and provide multiple + * power domains. A single cell is used as an index to an array of power + * domains specified in genpd_onecell_data struct when registering the + * provider. + */ +struct generic_pm_domain *of_genpd_xlate_onecell( + struct of_phandle_args *genpdspec, + void *data) +{ + struct genpd_onecell_data *genpd_data = data; + unsigned int idx = genpdspec->args[0]; + + if (genpdspec->args_count != 1) + return ERR_PTR(-EINVAL); + + if (idx >= genpd_data->domain_num) { + pr_err("%s: invalid domain index %d\n", __func__, idx); + return ERR_PTR(-EINVAL); + } + + return genpd_data->domains[idx]; +} +EXPORT_SYMBOL_GPL(of_genpd_xlate_onecell); + +/** + * of_genpd_add_provider() - Register a domain provider for a node + * @np: Device node pointer associated with domain provider. + * @xlate: Callback for decoding domain from phandle arguments. + * @data: Context pointer for @genpd_src_get callback. + */ +int of_genpd_add_provider(struct device_node *np, genpd_xlate_t xlate, + void *data) +{ + struct of_genpd_provider *cp; + + cp = kzalloc(sizeof(*cp), GFP_KERNEL); + if (!cp) + return -ENOMEM; + + cp->node = of_node_get(np); + cp->data = data; + cp->xlate = xlate; + + mutex_lock(&of_genpd_mutex); + list_add(&cp->link, &of_genpd_providers); + mutex_unlock(&of_genpd_mutex); + pr_debug("Added domain provider from %s\n", np->full_name); + + return 0; +} +EXPORT_SYMBOL_GPL(of_genpd_add_provider); + +/** + * of_genpd_del_provider() - Remove a previously registered domain provider + * @np: Device node pointer associated with domain provider + */ +void of_genpd_del_provider(struct device_node *np) +{ + struct of_genpd_provider *cp; + + mutex_lock(&of_genpd_mutex); + list_for_each_entry(cp, &of_genpd_providers, link) { + if (cp->node == np) { + list_del(&cp->link); + of_node_put(cp->node); + kfree(cp); + break; + } + } + mutex_unlock(&of_genpd_mutex); +} +EXPORT_SYMBOL_GPL(of_genpd_del_provider); + +/** + * of_genpd_get_from_provider() - Look-up power domain + * @genpdspec: OF phandle args to use for look-up + * + * Looks for domain provider under node specified by @genpdspec and if found + * uses xlate function of the provider to map phandle args to a power domain. + * + * Returns a valid pointer to struct generic_pm_domain on success or ERR_PTR() + * on failure. + */ +static struct generic_pm_domain *of_genpd_get_from_provider( + struct of_phandle_args *genpdspec) +{ + struct generic_pm_domain *genpd = ERR_PTR(-EPROBE_DEFER); + struct of_genpd_provider *provider; + + mutex_lock(&of_genpd_mutex); + + /* Check if we have such a provider in our array */ + list_for_each_entry(provider, &of_genpd_providers, link) { + if (provider->node == genpdspec->np) + genpd = provider->xlate(genpdspec, provider->data); + if (!IS_ERR(genpd)) + break; + } + + mutex_unlock(&of_genpd_mutex); + + return genpd; +} + +/* + * Device<->domain binding using Device Tree look-up. + * + * The purpose of code below is to manage assignment of devices to their + * power domains in an automatic fashion, based on data read from device tree. + * The two functions, genpd_bind_domain() and genpd_unbind_domain() are + * intended to be called by higher level code that manages devices, i.e. + * really_probe() and __device_release_driver() to respectively bind and + * unbind device from its power domain. + * + * Both generic and legacy Samsung-specific DT bindings are supported to + * keep backwards compatibility with existing DTBs. + */ + +/** + * genpd_bind_domain - Bind device to its power domain using Device Tree. + * @dev: Device to bind to its power domain. + * + * Tries to parse power domain specifier from device's OF node and if succeeds + * attaches the device to retrieved power domain. + * + * Returns 0 on success or negative error code otherwise. + */ +int genpd_bind_domain(struct device *dev) +{ + struct of_phandle_args pd_args; + struct generic_pm_domain *pd; + int ret; + + if (!dev->of_node) + return 0; + + ret = of_parse_phandle_with_args(dev->of_node, "power-domains", + "#power-domain-cells", 0, &pd_args); + if (ret < 0) { + if (ret != -ENOENT) + return ret; + + /* + * Try legacy Samsung-specific bindings + * (for backwards compatibility of DT ABI) + */ + pd_args.args_count = 0; + pd_args.np = of_parse_phandle(dev->of_node, + "samsung,power-domain", 0); + if (!pd_args.np) + return 0; + } + + pd = of_genpd_get_from_provider(&pd_args); + if (IS_ERR(pd)) { + if (PTR_ERR(pd) != -EPROBE_DEFER) + dev_err(dev, "failed to find power domain: %ld\n", + PTR_ERR(pd)); + return PTR_ERR(pd); + } + + dev_dbg(dev, "adding to power domain %s\n", pd->name); + + while (1) { + ret = pm_genpd_add_device(pd, dev); + if (ret != -EAGAIN) + break; + cond_resched(); + } + + if (ret < 0) { + dev_err(dev, "failed to add to power domain %s: %d", + pd->name, ret); + return ret; + } + + return 0; +} + +/** + * genpd_unbind_domain - Unbind device from its power domain. + * @dev: Device to unbind from its power domain. + * + * Unbinds device from power domain previously bound to it. + * + * Returns 0 on success or negative error code otherwise. + */ +int genpd_unbind_domain(struct device *dev) +{ + struct generic_pm_domain *pd = dev_to_genpd(dev); + int ret; + + if (!dev->of_node || IS_ERR(pd)) + return 0; + + dev_dbg(dev, "removing from power domain %s\n", pd->name); + + while (1) { + ret = pm_genpd_remove_device(pd, dev); + if (ret != -EAGAIN) + break; + cond_resched(); + } + + if (ret < 0) { + dev_err(dev, "failed to remove from power domain %s: %d", + pd->name, ret); + return ret; + } + + /* Check if domain can be powered off after removing this device. */ + genpd_queue_power_off_work(pd); + + return 0; +} +#endif diff --git a/include/linux/pm_domain.h b/include/linux/pm_domain.h index 7c1d252..04473d4 100644 --- a/include/linux/pm_domain.h +++ b/include/linux/pm_domain.h @@ -310,4 +310,50 @@ static inline void pm_genpd_syscore_poweron(struct device *dev) pm_genpd_syscore_switch(dev, false); } +/* OF power domain providers */ +struct of_device_id; + +struct genpd_onecell_data { + struct generic_pm_domain **domains; + unsigned int domain_num; +}; + +typedef struct generic_pm_domain *(*genpd_xlate_t)(struct of_phandle_args *args, + void *data); + +#ifdef CONFIG_PM_GENERIC_DOMAINS_OF +int of_genpd_add_provider(struct device_node *np, genpd_xlate_t xlate, + void *data); +void of_genpd_del_provider(struct device_node *np); + +struct generic_pm_domain *of_genpd_xlate_simple( + struct of_phandle_args *genpdspec, + void *data); +struct generic_pm_domain *of_genpd_xlate_onecell( + struct of_phandle_args *genpdspec, + void *data); + +int genpd_bind_domain(struct device *dev); +int genpd_unbind_domain(struct device *dev); +#else /* !CONFIG_PM_GENERIC_DOMAINS_OF */ +static inline int of_genpd_add_provider(struct device_node *np, + genpd_xlate_t xlate, void *data) +{ + return 0; +} +static inline void of_genpd_del_provider(struct device_node *np) {} + +#define of_genpd_xlate_simple NULL +#define of_genpd_xlate_onecell NULL + +static inline int genpd_bind_domain(struct device *dev) +{ + return 0; +} +static inline int genpd_unbind_domain(struct device *dev) +{ + return 0; +} +#endif /* CONFIG_PM_GENERIC_DOMAINS_OF */ + #endif /* _LINUX_PM_DOMAIN_H */ diff --git a/kernel/power/Kconfig b/kernel/power/Kconfig index 2fac9cc..45aa98e 100644 --- a/kernel/power/Kconfig +++ b/kernel/power/Kconfig @@ -306,6 +306,10 @@ config PM_GENERIC_DOMAINS_RUNTIME def_bool y depends on PM_RUNTIME && PM_GENERIC_DOMAINS +config PM_GENERIC_DOMAINS_OF + def_bool y + depends on PM_GENERIC_DOMAINS && OF && !ARCH_EXYNOS + config CPU_PM bool depends on SUSPEND || CPU_IDLE