From patchwork Thu Nov 22 05:34:42 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmitry Torokhov X-Patchwork-Id: 1782961 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork1.kernel.org Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) by patchwork1.kernel.org (Postfix) with ESMTP id 453B43FD1A for ; Thu, 22 Nov 2012 05:38:20 +0000 (UTC) Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.76 #1 (Red Hat Linux)) id 1TbPRg-0004gb-50; Thu, 22 Nov 2012 05:35:28 +0000 Received: from mail-pa0-f49.google.com ([209.85.220.49]) by merlin.infradead.org with esmtps (Exim 4.76 #1 (Red Hat Linux)) id 1TbPR6-0004dH-ED for linux-arm-kernel@lists.infradead.org; Thu, 22 Nov 2012 05:34:53 +0000 Received: by mail-pa0-f49.google.com with SMTP id bi1so2515426pad.36 for ; Wed, 21 Nov 2012 21:34:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references; bh=zFQC3I1Wn0nMpAuneWiBnXKEvB6Ve8G2v5Ny1btJpUU=; b=VjK/Mx2SaQTpiqnTm7zTv1uuS1s6t9t4uX6zlaDrkgBv7lGX88NyLrmsePvz5XrAlH So2FqgUW0wONHvfagRT9ayt2Ec9fkvsRImV0732/8fNV19GT+fPib0nlHTaw0SWksXYD h6mWc+0Rd3LI2tDsE2Y1w2dWCgRFiw/oWnQtNfOGiJkhCVHoc52AYv1Fx0GESNQPYuYk SNDPfgOBo9LLBMrtAugWG/YevcS2CYe6wth2QCqHYAvpmC1YRKUrAuZnOIPGaIOIO4Id qraPLjuDVpG7byNCLQdQUx+HMFwBGYW5Gnq8pZxUyWmUttzEx+me82+BQx6ejIT0GpX2 telw== Received: by 10.68.204.103 with SMTP id kx7mr1536741pbc.33.1353562492186; Wed, 21 Nov 2012 21:34:52 -0800 (PST) Received: from mailhub.coreip.homeip.net (c-67-188-112-76.hsd1.ca.comcast.net. [67.188.112.76]) by mx.google.com with ESMTPS id f2sm1279003paz.25.2012.11.21.21.34.50 (version=TLSv1/SSLv3 cipher=OTHER); Wed, 21 Nov 2012 21:34:51 -0800 (PST) From: Dmitry Torokhov To: linux-arm-kernel@lists.infradead.org Subject: [PATCH v2 3/3] CLK: add more managed APIs Date: Wed, 21 Nov 2012 21:34:42 -0800 Message-Id: <1353562482-12422-4-git-send-email-dmitry.torokhov@gmail.com> X-Mailer: git-send-email 1.7.11.7 In-Reply-To: <1353562482-12422-1-git-send-email-dmitry.torokhov@gmail.com> References: <1353403339-11679-1-git-send-email-dmitry.torokhov@gmail.com> <1353562482-12422-1-git-send-email-dmitry.torokhov@gmail.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20121122_003452_664874_94A7F031 X-CRM114-Status: GOOD ( 20.50 ) X-Spam-Score: -2.7 (--) X-Spam-Report: SpamAssassin version 3.3.2 on merlin.infradead.org summary: Content analysis details: (-2.7 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.7 RCVD_IN_DNSWL_LOW RBL: Sender listed at http://www.dnswl.org/, low trust [209.85.220.49 listed in list.dnswl.org] 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider (dmitry.torokhov[at]gmail.com) -0.0 SPF_PASS SPF: sender matches SPF record -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature Cc: Viresh Kumar , Shawn Guo , Russell King , linux-kernel@vger.kernel.org, Mike Turquette X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: linux-arm-kernel-bounces@lists.infradead.org Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org When converting a driver to managed resources it is desirable to be able to manage all resources in the same fashion. This change allows managing clocks in the same way we manage all other resources. This adds the following managed APIs: - devm_clk_prepare()/devm_clk_unprepare(); - devm_clk_enable()/devm_clk_disable(); - devm_clk_preapre_enable()/devm_clk_diable_unprepare(). Reviewed-by: Viresh Kumar Signed-off-by: Dmitry Torokhov --- drivers/clk/clk-devres.c | 90 +++++++++++++++++++++++++++++++--------- include/linux/clk.h | 105 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 176 insertions(+), 19 deletions(-) diff --git a/drivers/clk/clk-devres.c b/drivers/clk/clk-devres.c index 8f57154..3a2286b 100644 --- a/drivers/clk/clk-devres.c +++ b/drivers/clk/clk-devres.c @@ -9,6 +9,32 @@ #include #include +static int devm_clk_match(struct device *dev, void *res, void *data) +{ + struct clk **c = res; + + if (WARN_ON(!c || !*c)) + return 0; + + return *c == data; +} + + +static int devm_clk_create_devres(struct device *dev, struct clk *clk, + void (*release)(struct device *, void *)) +{ + struct clk **ptr; + + ptr = devres_alloc(release, sizeof(*ptr), GFP_KERNEL); + if (!ptr) + return -ENOMEM; + + *ptr = clk; + devres_add(dev, ptr); + + return 0; +} + static void devm_clk_release(struct device *dev, void *res) { clk_put(*(struct clk **)res); @@ -16,34 +42,22 @@ static void devm_clk_release(struct device *dev, void *res) struct clk *devm_clk_get(struct device *dev, const char *id) { - struct clk **ptr, *clk; - - ptr = devres_alloc(devm_clk_release, sizeof(*ptr), GFP_KERNEL); - if (!ptr) - return ERR_PTR(-ENOMEM); + struct clk *clk; + int error; clk = clk_get(dev, id); if (!IS_ERR(clk)) { - *ptr = clk; - devres_add(dev, ptr); - } else { - devres_free(ptr); + error = devm_clk_create_devres(dev, clk, devm_clk_release); + if (error) { + clk_put(clk); + return ERR_PTR(error); + } } return clk; } EXPORT_SYMBOL(devm_clk_get); -static int devm_clk_match(struct device *dev, void *res, void *data) -{ - struct clk **c = res; - if (!c || !*c) { - WARN_ON(!c || !*c); - return 0; - } - return *c == data; -} - void devm_clk_put(struct device *dev, struct clk *clk) { int ret; @@ -53,3 +67,41 @@ void devm_clk_put(struct device *dev, struct clk *clk) WARN_ON(ret); } EXPORT_SYMBOL(devm_clk_put); + +#define DEFINE_DEVM_CLK_OP(create_op, destroy_op) \ +static void devm_##destroy_op##_release(struct device *devm, void *res) \ +{ \ + destroy_op(*(struct clk **)res); \ +} \ + \ +int devm_##create_op(struct device *dev, struct clk *clk) \ +{ \ + int error; \ + \ + error = devm_clk_create_devres(dev, clk, \ + devm_##destroy_op##_release); \ + if (error) \ + return error; \ + \ + error = create_op(clk); \ + if (error) { \ + WARN_ON(devres_destroy(dev, \ + devm_##destroy_op##_release, \ + devm_clk_match, clk)); \ + return error; \ + } \ + \ + return 0; \ +} \ +EXPORT_SYMBOL(devm_##create_op); \ + \ +void devm_##destroy_op(struct device *dev, struct clk *clk) \ +{ \ + WARN_ON(devres_release(dev, devm_##destroy_op##_release, \ + devm_clk_match, clk)); \ +} \ +EXPORT_SYMBOL(devm_##destroy_op) + +DEFINE_DEVM_CLK_OP(clk_prepare, clk_unprepare); +DEFINE_DEVM_CLK_OP(clk_prepare_enable, clk_disable_unprepare); +DEFINE_DEVM_CLK_OP(clk_enable, clk_disable); diff --git a/include/linux/clk.h b/include/linux/clk.h index 8bf149e..04b6300 100644 --- a/include/linux/clk.h +++ b/include/linux/clk.h @@ -133,6 +133,17 @@ struct clk *devm_clk_get(struct device *dev, const char *id); int clk_prepare(struct clk *clk); /** + * devm_clk_prepare - prepare a clock source as managed resource + * @dev: device owning the resource + * @clk: clock source + * + * This prepares the clock source for use. + * + * Must not be called from within atomic context. + */ +int devm_clk_prepare(struct device *dev, struct clk *clk); + +/** * clk_unprepare - undo preparation of a clock source * @clk: clock source * @@ -144,6 +155,18 @@ int clk_prepare(struct clk *clk); void clk_unprepare(struct clk *clk); /** + * devm_clk_unprepare - undo preparation of a managed clock source. + * @dev: device used to prepare the clock + * @clk: clock source + * + * This undoes preparation of a clock previously prepared with call + * to devm_clk_pepare(). + * + * Must not be called from within atomic context. + */ +void devm_clk_unprepare(struct device *dev, struct clk *clk); + +/** * clk_enable - inform the system when the clock source should be running. * @clk: clock source * @@ -156,6 +179,19 @@ void clk_unprepare(struct clk *clk); int clk_enable(struct clk *clk); /** + * devm_clk_enable - enable the clock source as managed resource + * @dev: device owning the resource + * @clk: clock source + * + * If the clock can not be enabled/disabled, this should return success. + * + * May be not called from atomic contexts. + * + * Returns success (0) or negative errno. + */ +int devm_clk_enable(struct device *dev, struct clk *clk); + +/** * clk_disable - inform the system when the clock source is no longer required. * @clk: clock source * @@ -172,6 +208,18 @@ int clk_enable(struct clk *clk); void clk_disable(struct clk *clk); /** + * devm_clk_disable - disable managed clock source resource + * @dev: device used to enable the clock + * @clk: clock source + * + * Inform the system that a clock source is no longer required by + * a driver and may be shut down. + * + * Must not be called from atomic contexts. + */ +void devm_clk_disable(struct device *dev, struct clk *clk); + +/** * clk_prepare_enable - prepare and enable a clock source * @clk: clock source * @@ -182,6 +230,17 @@ void clk_disable(struct clk *clk); int clk_prepare_enable(struct clk *clk); /** + * devm_clk_prepare_enable - prepare and enable a managed clock source + * @dev: device owning the clock source + * @clk: clock source + * + * This prepares the clock source for use and enables it. + * + * Must not be called from within atomic context. + */ +int devm_clk_prepare_enable(struct device *dev, struct clk *clk); + +/** * clk_disable_unprepare - disable and undo preparation of a clock source * @clk: clock source * @@ -192,6 +251,17 @@ int clk_prepare_enable(struct clk *clk); void clk_disable_unprepare(struct clk *clk); /** + * clk_disable_unprepare - disable and undo preparation of a managed clock source + * @dev: device used to prepare and enable the clock + * @clk: clock source + * + * This disables and undoes a previously prepared clock. + * + * Must not be called from within atomic context. + */ +void devm_clk_disable_unprepare(struct device *dev, struct clk *clk); + +/** * clk_get_rate - obtain the current clock rate (in Hz) for a clock source. * This is only valid once the clock source has been enabled. * @clk: clock source @@ -303,29 +373,64 @@ static inline int clk_prepare(struct clk *clk) return 0; } +static inline int devm_clk_prepare(struct device *dev, struct clk *clk) +{ + might_sleep(); + return 0; +} + static inline void clk_unprepare(struct clk *clk) { might_sleep(); } +static inline void devm_clk_unprepare(struct device *dev, struct clk *clk) +{ + might_sleep(); +} + static inline int clk_enable(struct clk *clk) { return 0; } +static inline int devm_clk_enable(struct device *dev, struct clk *clk) +{ + might_sleep(); + return 0; +} + static inline void clk_disable(struct clk *clk) {} +static inline void devm_clk_disable(struct device *dev, struct clk *clk) +{ + might_sleep(); + return 0; +} + static inline int clk_prepare_enable(struct clk *clk) { might_sleep(); return 0; } +static inline int devm_clk_prepare_enable(struct device *dev, struct clk *clk) +{ + might_sleep(); + return 0; +} + static inline void clk_disable_unprepare(struct clk *clk) { might_sleep(); } +static inline void devm_clk_disable_unprepare(struct device *dev, + struct clk *clk) +{ + might_sleep(); +} + static inline unsigned long clk_get_rate(struct clk *clk) { return 0;