Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754342AbcJNDAv (ORCPT ); Thu, 13 Oct 2016 23:00:51 -0400 Received: from mail-sn1nam01on0073.outbound.protection.outlook.com ([104.47.32.73]:58804 "EHLO NAM01-SN1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1751437AbcJNDAi (ORCPT ); Thu, 13 Oct 2016 23:00:38 -0400 Authentication-Results: spf=fail (sender IP is 192.88.168.50) smtp.mailfrom=nxp.com; nxp.com; dkim=none (message not signed) header.d=none;nxp.com; dmarc=fail action=none header.from=nxp.com;nxp.com; dkim=none (message not signed) header.d=none; From: Peter Chen To: , , , , , , , CC: , , , , , , , , , , , , , , , , , , , Peter Chen Subject: [PATCH v8 2/8] power: add power sequence library Date: Fri, 14 Oct 2016 10:59:49 +0800 Message-ID: <1476413995-20361-3-git-send-email-peter.chen@nxp.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1476413995-20361-1-git-send-email-peter.chen@nxp.com> References: <1476413995-20361-1-git-send-email-peter.chen@nxp.com> X-EOPAttributedMessage: 0 X-Matching-Connectors: 131208876338577911;(91ab9b29-cfa4-454e-5278-08d120cd25b8);() X-Forefront-Antispam-Report: CIP:192.88.168.50;IPV:NLI;CTRY:US;EFV:NLI;SFV:NSPM;SFS:(10009020)(6009001)(7916002)(2980300002)(1110001)(1109001)(339900001)(199003)(189002)(106466001)(105606002)(356003)(36756003)(76176999)(2171001)(229853001)(48376002)(50466002)(87936001)(4326007)(189998001)(2906002)(50986999)(33646002)(104016004)(68736007)(19580405001)(85426001)(5660300001)(19580395003)(50226002)(92566002)(86362001)(8936002)(7846002)(81166006)(81156014)(626004)(8666005)(2950100002)(15975445007)(11100500001)(305945005)(77096005)(8676002)(586003)(97736004)(6666003)(5001770100001)(2201001)(7416002)(5003940100001)(47776003)(7059030)(2004002);DIR:OUT;SFP:1101;SCL:1;SRVR:BY2PR03MB329;H:tx30smr01.am.freescale.net;FPR:;SPF:Fail;PTR:InfoDomainNonexistent;A:1;MX:1;LANG:en; X-Microsoft-Exchange-Diagnostics: 1;BN1AFFO11FD020;1:uueb35smP41Plg/paOLCLZmADnvqpPXlkZNmfieCPBYDUhEviogsNpRhEitZFEKgTT4f8dwdBuseNXugWvMGlylOGoARvvpK2yetUlU0uYOOqdIHtLIckok3KhTeONWsaqFzDbQjmZ2taK1yus4eC9EdxmYJl1r/zytio4QOG9URAtKmDlbnSjCJLkVnImRaSF4I3qsmxEx7/Cu3tDXfQuzVqmgRjJNzB6hO9OIkfaBNu434C1PwjD50JOafUDlQ/q63ViDWQ11YvWajJYkEs6q2KzU9fjA5wOikcXWWiE76zz2Lbtvz0sq4xUM5guCnjqg9nkwmKBFdnJeA1FxJ8haOvmYOywI/kpTR+QdOxOVl3dgSnfdCVmAWZiHxkZ7igAoKR/QETGusd5eOFMHDBjzdu4AnY4KVWhIdMjwyt/8GU5j2UmONgnPTuhuiP62yMC6yKCVIVcB7woiVaKOT3pArq27NYL6M39JLR87Dn13f5DD01VAi5ALaXHWHZMboBYeibSldmfYMxcu0x9PjlwqPAF1d2VPuZKMYhWkRTSKZhr57/Jon1xQXpKvFgcF4ZNtomoe5noQO7VQk5AW6rbU4yWuQO2Gv7GGXcJThfJm9tZc5xH7CCLwxcWmvRVJSLmm6CM3gkuzzl+UG6HFxgqNxqqZeMTowNm3y5V4JxVnrLlO/t3F8BBUym/mhs99Wr4HKeNnPFpq+htAJrqirdyQ6Bep4ZGlAQCiFObokSnPCqRcM0Nci3XzplX/ohDzB MIME-Version: 1.0 Content-Type: text/plain X-MS-Office365-Filtering-Correlation-Id: edff531e-675c-440c-a097-08d3f3de43fe X-Microsoft-Exchange-Diagnostics: 1;BY2PR03MB329;2:I5eODvUt9xM1pJMdAlsoQbLBGEk1R2dZQA1r6eoyAsjYAbH5+Vd8FhV2ojjqJbK5tkjkRmM+dbL1SOv0OODmDDGwhNrPi9be60DfMnI56l/Mrml9qjtBTPV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jIC9Adk980u0G3ht0ZQTBzSeYPvI9wAjuFxw9ngTejEeDvcFgQp5X46OeXGe8xi3y5uBv9p/WbKlHvdf9gqWw/LoeInSiecco+1rZsnMyOl87rjVounp63SrtJP6B1gbSW37bAvkcpwE26VE3E5LD3cfFZdYtqh6uWPPQEGm7MZBY+ZChspii9rbLB7mS7ODUW2jBsAuuzjLuIU4P/bDsl4j7OLEMh9YyiqFkfBVWu2Ju9DYnN83pRfdUizMJ1R7iiIMT0J1 X-Microsoft-Exchange-Diagnostics: 1;BY2PR03MB329;31:04Pi6puGRECRuM7rfpu7MiOgqbzciTOq17HDZipHpKRMmokQNhJK1D0eo559f3T7YXudAUO54CKyrNeUK0piOroYgIV3ofurxZ4xlCvdXXkgKYV9uUPTdCwBzDQz+lCXhoBsAJFOlBmulIGIMHF6nk8HBUkcF2E0IfNN9PWvq20hLqgy9I4ynZmdzoOxaXs/1xhu6PLyXNGk8DEJc8TEMw53/n0UHLZz7BeD6hFuXMc7m1cVxvCl4T+LMQ3TYiB+hGw5yBSyz0LEIZbqBUOOavn8UTNRKyINO/kggO6qwm4= X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(250305191791016)(22074186197030)(9452136761055)(185117386973197)(84791874153150); X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(6040176)(601004)(2401047)(13017025)(13015025)(13023025)(13018025)(5005006)(13024025)(8121501046)(10201501046)(3002001)(6055026);SRVR:BY2PR03MB329;BCL:0;PCL:0;RULEID:(400006);SRVR:BY2PR03MB329; X-Microsoft-Exchange-Diagnostics: 1;BY2PR03MB329;4:pUpGzzfJP7DAxHtxvXcMt9er325jmmPvQOH9IQe+uhhd3w5XOXhi2PozaokOgJfqjemDr/KhXKOD1f/TsxOE1kSUCriH1GozNFzQeggFXrfAzQufxwNidrS9+NnPX3T7rFiLfBcJHjaVVyYAgcE+1PGjbW2gRk0Ub1+/IrBmWAvYxG0rgeRUGZF1WBRs2KIGyl3VICiQ2uvfEXXhj1VdNPPkCtQY+m1LnMeKPt2PgpDF8L1PQT8BQglgSW9LrazloxDfyLsj5U4YT0O4BUODftbJ26glW09WagTjjM5W8owO+gTCj5rDgU9+7+H6xZkViK7I7Ve2AzsC8nzDRarVPPNQql/5rEZyLzF+Jz+V25gGVcPHIWIyDAZXnf091ONkTZrNzCxjkG/tLwhEVXZ3BCnGc3S4rbTDQ7rnVecG6AXfYEmT2oyTb700JUpMAvJH7gLKIJDyL6BtyhzJOfCjq4YXPywbfDr/pi0bcDEVm35kTPddirQeZWagfi3BdJH2D6Rm1uouH8gV4GB6rEb+FmfNZR/H37rMgSRKAaR09rS/tQ+v7owQ229k3awTBiOIIyfOryvMc/pckLRiEP5T7hC7vaLmaajL3txhUB5qv2+L6gBMU0Y3hnHQqO2uDnKnVWXs0RxM51SneqBIfhrXLnnM1doSm7LD2ta+mV1ckTvFH1xjh+1J4yYbE1eVY6Aw X-Forefront-PRVS: 0095BCF226 X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1;BY2PR03MB329;23:vV46bvPip36AafCzKJOi7NKogg6nF5K0NalXFAm+kQ?= =?us-ascii?Q?HXpQehVNN3/6oNf2A/XFPfnnkssrG+S/qehcUpUMBeDezBxzOaD2cvWEawIR?= =?us-ascii?Q?PHh3o8myaRh1HZ14z4w1haudCLnN+7DA1YU9+SYsndjk13fFelES20HP8vH/?= =?us-ascii?Q?mgEj4HjnGcGe424NovOcdxOEttMrzCl2NPkdTtov1z1cYqkrCpOyVNh0FR5k?= =?us-ascii?Q?HiQlz0k3jThTeuu9hGV8rxpeOZ8QPltqOrREDzHlvuwOic9RLTC3Aa+foG+M?= =?us-ascii?Q?PjtuEGXnTf+vC7I+BX6vuC3LUaiHGWTSWR8AZlxedrKPHdRWWJCb8d5vyE+l?= =?us-ascii?Q?KiLascSu6n3cog5a/KlguWPXcTF8SHyvAIogqMJU0On5cJcAN6PPxk4Qo1Ml?= =?us-ascii?Q?W7abHA2aBkNLLBTWpE6F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OHCikvbnfPg7Q6vy8VZWnHx0sUsyNA3XjZC9fz8= X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Oct 2016 03:00:33.5301 (UTC) X-MS-Exchange-CrossTenant-Id: 5afe0b00-7697-4969-b663-5eab37d5f47e X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=5afe0b00-7697-4969-b663-5eab37d5f47e;Ip=[192.88.168.50];Helo=[tx30smr01.am.freescale.net] X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY2PR03MB329 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 16246 Lines: 579 We have an well-known problem that the device needs to do some power sequence before it can be recognized by related host, the typical example like hard-wired mmc devices and usb devices. This power sequence is hard to be described at device tree and handled by related host driver, so we have created a common power sequence library to cover this requirement. The core code has supplied some common helpers for host driver, and individual power sequence libraries handle kinds of power sequence for devices. The pwrseq librares always need to allocate extra instance for compatible string match. pwrseq_generic is intended for general purpose of power sequence, which handles gpios and clocks currently, and can cover other controls in future. The host driver just needs to call of_pwrseq_on/of_pwrseq_off if only one power sequence is needed, else call of_pwrseq_on_list /of_pwrseq_off_list instead (eg, USB hub driver). For new power sequence library, it can add its compatible string to pwrseq_of_match_table, then the pwrseq core will match it with DT's, and choose this library at runtime. Signed-off-by: Peter Chen Tested-by Joshua Clayton Reviewed-by: Matthias Kaehlcke Tested-by: Matthias Kaehlcke --- MAINTAINERS | 9 ++ drivers/power/Kconfig | 1 + drivers/power/Makefile | 1 + drivers/power/pwrseq/Kconfig | 19 ++++ drivers/power/pwrseq/Makefile | 2 + drivers/power/pwrseq/core.c | 191 ++++++++++++++++++++++++++++++++++ drivers/power/pwrseq/pwrseq_generic.c | 183 ++++++++++++++++++++++++++++++++ include/linux/power/pwrseq.h | 72 +++++++++++++ 8 files changed, 478 insertions(+) create mode 100644 drivers/power/pwrseq/Kconfig create mode 100644 drivers/power/pwrseq/Makefile create mode 100644 drivers/power/pwrseq/core.c create mode 100644 drivers/power/pwrseq/pwrseq_generic.c create mode 100644 include/linux/power/pwrseq.h diff --git a/MAINTAINERS b/MAINTAINERS index 3bb6640..7e1fcb9 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -9384,6 +9384,15 @@ F: include/linux/pm_* F: include/linux/powercap.h F: drivers/powercap/ +POWER SEQUENCE LIBRARY +M: Peter Chen +T: git git://git.kernel.org/pub/scm/linux/kernel/git/peter.chen/usb.git +L: linux-pm@vger.kernel.org +S: Maintained +F: Documentation/devicetree/bindings/power/pwrseq/ +F: drivers/power/pwrseq/ +F: include/linux/power/pwrseq.h/ + POWER SUPPLY CLASS/SUBSYSTEM and DRIVERS M: Sebastian Reichel M: Dmitry Eremin-Solenikov diff --git a/drivers/power/Kconfig b/drivers/power/Kconfig index acd4a15..f6aa4fd 100644 --- a/drivers/power/Kconfig +++ b/drivers/power/Kconfig @@ -515,3 +515,4 @@ endif # POWER_SUPPLY source "drivers/power/reset/Kconfig" source "drivers/power/avs/Kconfig" +source "drivers/power/pwrseq/Kconfig" diff --git a/drivers/power/Makefile b/drivers/power/Makefile index e46b75d..4ed2e12 100644 --- a/drivers/power/Makefile +++ b/drivers/power/Makefile @@ -74,3 +74,4 @@ obj-$(CONFIG_CHARGER_TPS65217) += tps65217_charger.o obj-$(CONFIG_POWER_RESET) += reset/ obj-$(CONFIG_AXP288_FUEL_GAUGE) += axp288_fuel_gauge.o obj-$(CONFIG_AXP288_CHARGER) += axp288_charger.o +obj-$(CONFIG_POWER_SEQUENCE) += pwrseq/ diff --git a/drivers/power/pwrseq/Kconfig b/drivers/power/pwrseq/Kconfig new file mode 100644 index 0000000..3859a67 --- /dev/null +++ b/drivers/power/pwrseq/Kconfig @@ -0,0 +1,19 @@ +# +# Power Sequence library +# + +config POWER_SEQUENCE + bool + +menu "Power Sequence Support" + +config PWRSEQ_GENERIC + bool "Generic power sequence control" + depends on OF + select POWER_SEQUENCE + help + It is used for drivers which needs to do power sequence + (eg, turn on clock, toggle reset gpio) before the related + devices can be found by hardware. This generic one can be + used for common power sequence control. +endmenu diff --git a/drivers/power/pwrseq/Makefile b/drivers/power/pwrseq/Makefile new file mode 100644 index 0000000..ad82389 --- /dev/null +++ b/drivers/power/pwrseq/Makefile @@ -0,0 +1,2 @@ +obj-$(CONFIG_POWER_SEQUENCE) += core.o +obj-$(CONFIG_PWRSEQ_GENERIC) += pwrseq_generic.o diff --git a/drivers/power/pwrseq/core.c b/drivers/power/pwrseq/core.c new file mode 100644 index 0000000..9cb1223 --- /dev/null +++ b/drivers/power/pwrseq/core.c @@ -0,0 +1,191 @@ +/* + * core.c power sequence core file + * + * Copyright (C) 2016 Freescale Semiconductor, Inc. + * Author: Peter Chen + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 of + * the License as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include +#include +#include +#include + +static DEFINE_MUTEX(pwrseq_list_mutex); +static LIST_HEAD(pwrseq_list); + +int pwrseq_get(struct device_node *np, struct pwrseq *p) +{ + if (p && p->get) + return p->get(np, p); + + return -ENOTSUPP; +} +EXPORT_SYMBOL_GPL(pwrseq_get); + +int pwrseq_on(struct pwrseq *p) +{ + if (p && p->on) + return p->on(p); + + return -ENOTSUPP; +} +EXPORT_SYMBOL_GPL(pwrseq_on); + +void pwrseq_off(struct pwrseq *p) +{ + if (p && p->off) + p->off(p); +} +EXPORT_SYMBOL_GPL(pwrseq_off); + +void pwrseq_put(struct pwrseq *p) +{ + if (p && p->put) + p->put(p); +} +EXPORT_SYMBOL_GPL(pwrseq_put); + +int pwrseq_suspend(struct pwrseq *p) +{ + if (p && p->suspend) + return p->suspend(p); + + return 0; +} +EXPORT_SYMBOL_GPL(pwrseq_suspend); + +int pwrseq_resume(struct pwrseq *p) +{ + if (p && p->resume) + return p->resume(p); + + return 0; +} +EXPORT_SYMBOL_GPL(pwrseq_resume); + +void pwrseq_register(struct pwrseq *pwrseq) +{ + mutex_lock(&pwrseq_list_mutex); + list_add(&pwrseq->node, &pwrseq_list); + mutex_unlock(&pwrseq_list_mutex); +} +EXPORT_SYMBOL_GPL(pwrseq_register); + +void pwrseq_unregister(struct pwrseq *pwrseq) +{ + mutex_lock(&pwrseq_list_mutex); + list_del(&pwrseq->node); + mutex_unlock(&pwrseq_list_mutex); +} +EXPORT_SYMBOL_GPL(pwrseq_unregister); + +static struct pwrseq *pwrseq_find_available_instance(struct device_node *np) +{ + struct pwrseq *pwrseq; + + list_for_each_entry(pwrseq, &pwrseq_list, node) { + if (pwrseq->used) + continue; + + /* compare compatible string for pwrseq node */ + if (of_match_node(pwrseq->pwrseq_of_match_table, np)) { + pwrseq->used = true; + return pwrseq; + } + + /* return generic pwrseq instance */ + if (!strcmp(pwrseq->pwrseq_of_match_table->compatible, + "generic")) { + pr_debug("using generic pwrseq instance for %s\n", + np->full_name); + pwrseq->used = true; + return pwrseq; + } + } + pr_warn("Can't find any pwrseq instances for %s\n", np->full_name); + + return NULL; +} + +struct pwrseq *of_pwrseq_on(struct device_node *np) +{ + struct pwrseq *pwrseq; + int ret; + + pwrseq = pwrseq_find_available_instance(np); + if (!pwrseq) + return ERR_PTR(-ENONET); + + ret = pwrseq_get(np, pwrseq); + if (ret) { + /* Mark current pwrseq as unused */ + pwrseq->used = false; + return ERR_PTR(ret); + } + + ret = pwrseq_on(pwrseq); + if (ret) + goto pwr_put; + + return pwrseq; + +pwr_put: + pwrseq_put(pwrseq); + return ERR_PTR(ret); +} +EXPORT_SYMBOL_GPL(of_pwrseq_on); + +void of_pwrseq_off(struct pwrseq *pwrseq) +{ + pwrseq_off(pwrseq); + pwrseq_put(pwrseq); +} +EXPORT_SYMBOL_GPL(of_pwrseq_off); + +int of_pwrseq_on_list(struct device_node *np, struct list_head *head) +{ + struct pwrseq *pwrseq; + struct pwrseq_list_per_dev *pwrseq_list_node; + + pwrseq = of_pwrseq_on(np); + if (IS_ERR(pwrseq)) + return PTR_ERR(pwrseq); + + pwrseq_list_node = kzalloc(sizeof(*pwrseq_list_node), GFP_KERNEL); + if (!pwrseq_list_node) { + of_pwrseq_off(pwrseq); + return -ENOMEM; + } + pwrseq_list_node->pwrseq = pwrseq; + list_add(&pwrseq_list_node->list, head); + + return 0; +} +EXPORT_SYMBOL_GPL(of_pwrseq_on_list); + +void of_pwrseq_off_list(struct list_head *head) +{ + struct pwrseq *pwrseq; + struct pwrseq_list_per_dev *pwrseq_list_node, *tmp_node; + + list_for_each_entry_safe(pwrseq_list_node, tmp_node, head, list) { + pwrseq = pwrseq_list_node->pwrseq; + of_pwrseq_off(pwrseq); + list_del(&pwrseq_list_node->list); + kfree(pwrseq_list_node); + } +} +EXPORT_SYMBOL_GPL(of_pwrseq_off_list); diff --git a/drivers/power/pwrseq/pwrseq_generic.c b/drivers/power/pwrseq/pwrseq_generic.c new file mode 100644 index 0000000..d7a77f2 --- /dev/null +++ b/drivers/power/pwrseq/pwrseq_generic.c @@ -0,0 +1,183 @@ +/* + * pwrseq_generic.c Generic power sequence handling + * + * Copyright (C) 2016 Freescale Semiconductor, Inc. + * Author: Peter Chen + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 of + * the License as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include + +#include + +struct pwrseq_generic { + struct pwrseq pwrseq; + struct gpio_desc *gpiod_reset; + struct clk *clks[PWRSEQ_MAX_CLKS]; + u32 duration_us; +}; + +#define to_generic_pwrseq(p) container_of(p, struct pwrseq_generic, pwrseq) + +static int pwrseq_generic_alloc_instance(void); +static const struct of_device_id generic_id_table[] = { + { .compatible = "generic",}, + { /* sentinel */ } +}; + +static void pwrseq_generic_put(struct pwrseq *pwrseq) +{ + struct pwrseq_generic *pwrseq_gen = to_generic_pwrseq(pwrseq); + int clk; + + if (pwrseq_gen->gpiod_reset) + gpiod_put(pwrseq_gen->gpiod_reset); + + for (clk = 0; clk < PWRSEQ_MAX_CLKS; clk++) + clk_put(pwrseq_gen->clks[clk]); + + pwrseq_unregister(&pwrseq_gen->pwrseq); + kfree(pwrseq_gen); +} + +static void pwrseq_generic_off(struct pwrseq *pwrseq) +{ + struct pwrseq_generic *pwrseq_gen = to_generic_pwrseq(pwrseq); + int clk; + + for (clk = PWRSEQ_MAX_CLKS - 1; clk >= 0; clk--) + clk_disable_unprepare(pwrseq_gen->clks[clk]); +} + +static int pwrseq_generic_on(struct pwrseq *pwrseq) +{ + struct pwrseq_generic *pwrseq_gen = to_generic_pwrseq(pwrseq); + int clk, ret = 0; + struct gpio_desc *gpiod_reset = pwrseq_gen->gpiod_reset; + + for (clk = 0; clk < PWRSEQ_MAX_CLKS && pwrseq_gen->clks[clk]; clk++) { + ret = clk_prepare_enable(pwrseq_gen->clks[clk]); + if (ret) { + pr_err("Can't enable clock, ret=%d\n", ret); + goto err_disable_clks; + } + } + + if (gpiod_reset) { + u32 duration_us = pwrseq_gen->duration_us; + + if (duration_us <= 10) + udelay(10); + else + usleep_range(duration_us, duration_us + 100); + gpiod_set_value(gpiod_reset, 0); + } + + return ret; + +err_disable_clks: + while (--clk >= 0) + clk_disable_unprepare(pwrseq_gen->clks[clk]); + + return ret; +} + +static int pwrseq_generic_get(struct device_node *np, struct pwrseq *pwrseq) +{ + struct pwrseq_generic *pwrseq_gen = to_generic_pwrseq(pwrseq); + enum of_gpio_flags flags; + int reset_gpio, clk, ret = 0; + + for (clk = 0; clk < PWRSEQ_MAX_CLKS; clk++) { + pwrseq_gen->clks[clk] = of_clk_get(np, clk); + if (IS_ERR(pwrseq_gen->clks[clk])) { + ret = PTR_ERR(pwrseq_gen->clks[clk]); + if (ret != -ENOENT) + goto err_put_clks; + pwrseq_gen->clks[clk] = NULL; + break; + } + } + + reset_gpio = of_get_named_gpio_flags(np, "reset-gpios", 0, &flags); + if (gpio_is_valid(reset_gpio)) { + unsigned long gpio_flags; + + if (flags & OF_GPIO_ACTIVE_LOW) + gpio_flags = GPIOF_ACTIVE_LOW | GPIOF_OUT_INIT_LOW; + else + gpio_flags = GPIOF_OUT_INIT_HIGH; + + ret = gpio_request_one(reset_gpio, gpio_flags, + "pwrseq-reset-gpios"); + if (ret) + goto err_put_clks; + + pwrseq_gen->gpiod_reset = gpio_to_desc(reset_gpio); + of_property_read_u32(np, "reset-duration-us", + &pwrseq_gen->duration_us); + } else if (reset_gpio == -ENOENT) { + ; /* no such gpio */ + } else { + ret = reset_gpio; + pr_err("Failed to get reset gpio on %s, err = %d\n", + np->full_name, reset_gpio); + goto err_put_clks; + } + + /* allocate new one for later pwrseq instance request */ + ret = pwrseq_generic_alloc_instance(); + if (ret) + goto err_put_gpio; + + return 0; + +err_put_gpio: + if (pwrseq_gen->gpiod_reset) + gpiod_put(pwrseq_gen->gpiod_reset); +err_put_clks: + while (--clk >= 0) + clk_put(pwrseq_gen->clks[clk]); + return ret; +} + +static int pwrseq_generic_alloc_instance(void) +{ + struct pwrseq_generic *pwrseq_gen; + + pwrseq_gen = kzalloc(sizeof(*pwrseq_gen), GFP_KERNEL); + if (!pwrseq_gen) + return -ENOMEM; + + pwrseq_gen->pwrseq.pwrseq_of_match_table = generic_id_table; + pwrseq_gen->pwrseq.get = pwrseq_generic_get; + pwrseq_gen->pwrseq.on = pwrseq_generic_on; + pwrseq_gen->pwrseq.off = pwrseq_generic_off; + pwrseq_gen->pwrseq.put = pwrseq_generic_put; + + pwrseq_register(&pwrseq_gen->pwrseq); + return 0; +} + +static int __init pwrseq_generic_register(void) +{ + return pwrseq_generic_alloc_instance(); +} +postcore_initcall(pwrseq_generic_register) diff --git a/include/linux/power/pwrseq.h b/include/linux/power/pwrseq.h new file mode 100644 index 0000000..4f37275 --- /dev/null +++ b/include/linux/power/pwrseq.h @@ -0,0 +1,72 @@ +#ifndef __LINUX_PWRSEQ_H +#define __LINUX_PWRSEQ_H + +#include + +#define PWRSEQ_MAX_CLKS 3 + +struct pwrseq { + const struct of_device_id *pwrseq_of_match_table; + struct list_head node; + int (*get)(struct device_node *np, struct pwrseq *p); + int (*on)(struct pwrseq *p); + void (*off)(struct pwrseq *p); + void (*put)(struct pwrseq *p); + int (*suspend)(struct pwrseq *p); + int (*resume)(struct pwrseq *p); + bool used; +}; + +/* used for power sequence instance list in one driver */ +struct pwrseq_list_per_dev { + struct pwrseq *pwrseq; + struct list_head list; +}; + +#if IS_ENABLED(CONFIG_POWER_SEQUENCE) +int pwrseq_get(struct device_node *np, struct pwrseq *p); +int pwrseq_on(struct pwrseq *p); +void pwrseq_off(struct pwrseq *p); +void pwrseq_put(struct pwrseq *p); +int pwrseq_suspend(struct pwrseq *p); +int pwrseq_resume(struct pwrseq *p); +void pwrseq_register(struct pwrseq *pwrseq); +void pwrseq_unregister(struct pwrseq *pwrseq); +struct pwrseq *of_pwrseq_on(struct device_node *np); +void of_pwrseq_off(struct pwrseq *pwrseq); +int of_pwrseq_on_list(struct device_node *np, struct list_head *head); +void of_pwrseq_off_list(struct list_head *head); +#else +static inline int pwrseq_get(struct device_node *np, struct pwrseq *p) +{ + return 0; +} +static inline int pwrseq_on(struct pwrseq *p) +{ + return 0; +} +static inline void pwrseq_off(struct pwrseq *p) {} +static inline void pwrseq_put(struct pwrseq *p) {} +static inline int pwrseq_suspend(struct pwrseq *p) +{ + return 0; +} +static inline int pwrseq_resume(struct pwrseq *p) +{ + return 0; +} +static inline void pwrseq_register(struct pwrseq *pwrseq) {} +static inline void pwrseq_unregister(struct pwrseq *pwrseq) {} +static inline struct pwrseq *of_pwrseq_on(struct device_node *np) +{ + return NULL; +} +void of_pwrseq_off(struct pwrseq *pwrseq) {} +int of_pwrseq_on_list(struct device_node *np, struct list_head *head) +{ + return 0; +} +void of_pwrseq_off_list(struct list_head *head) {} +#endif /* CONFIG_POWER_SEQUENCE */ + +#endif /* __LINUX_PWRSEQ_H */ -- 2.7.4