From patchwork Wed Aug 24 13:15:50 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 1092562 Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) by demeter1.kernel.org (8.14.4/8.14.4) with ESMTP id p7ODQSNm006852 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Wed, 24 Aug 2011 13:26:49 GMT Received: from canuck.infradead.org ([2001:4978:20e::1]) by merlin.infradead.org with esmtps (Exim 4.76 #1 (Red Hat Linux)) id 1QwDP0-000167-DX; Wed, 24 Aug 2011 13:21:58 +0000 Received: from localhost ([127.0.0.1] helo=canuck.infradead.org) by canuck.infradead.org with esmtp (Exim 4.76 #1 (Red Hat Linux)) id 1QwDKP-0005t5-QO; Wed, 24 Aug 2011 13:17:09 +0000 Received: from opensource.wolfsonmicro.com ([80.75.67.52] helo=opensource2.wolfsonmicro.com) by canuck.infradead.org with esmtps (Exim 4.76 #1 (Red Hat Linux)) id 1QwDJM-0005Yq-Cq for linux-arm-kernel@lists.infradead.org; Wed, 24 Aug 2011 13:16:09 +0000 Received: from finisterre.wolfsonmicro.main (unknown [87.246.78.26]) by opensource2.wolfsonmicro.com (Postfix) with ESMTPSA id 4B5B61101E8; Wed, 24 Aug 2011 14:16:01 +0100 (BST) Received: from broonie by finisterre.wolfsonmicro.main with local (Exim 4.76) (envelope-from ) id 1QwDJI-0004Q0-Kk; Wed, 24 Aug 2011 14:16:00 +0100 From: Mark Brown To: linux-arm-kernel@lists.infradead.org Subject: [PATCH 02/11] clk: Implement clk_set_rate Date: Wed, 24 Aug 2011 14:15:50 +0100 Message-Id: <1314191759-16941-2-git-send-email-broonie@opensource.wolfsonmicro.com> X-Mailer: git-send-email 1.7.5.4 In-Reply-To: <1314191759-16941-1-git-send-email-broonie@opensource.wolfsonmicro.com> References: <20110824131324.GB16520@opensource.wolfsonmicro.com> <1314191759-16941-1-git-send-email-broonie@opensource.wolfsonmicro.com> X-CRM114-Version: 20090807-BlameThorstenAndJenny ( TRE 0.7.6 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20110824_091604_791373_B569540B X-CRM114-Status: GOOD ( 18.88 ) X-Spam-Score: -0.5 (/) X-Spam-Report: SpamAssassin version 3.3.1 on canuck.infradead.org summary: Content analysis details: (-0.5 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.5 RP_MATCHES_RCVD Envelope sender domain matches handover relay domain Cc: Jeremy Kerr , Mark Brown , Linus Walleij , Mike Turquette X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.12 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 X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.6 (demeter1.kernel.org [140.211.167.41]); Wed, 24 Aug 2011 13:27:13 +0000 (UTC) From: Jeremy Kerr Implemenent clk_set_rate by adding a set_rate callback to clk_hw_ops, and core code to handle propagation of rate changes up and down the clock tree. Signed-off-by: Jeremy Kerr Signed-off-by: Mark Brown --- drivers/clk/clk.c | 74 ++++++++++++++++++++++++++++++++++++++++++++++---- include/linux/clk.h | 12 ++++++++ 2 files changed, 80 insertions(+), 6 deletions(-) diff --git a/drivers/clk/clk.c b/drivers/clk/clk.c index ad90a90..3a4d70e 100644 --- a/drivers/clk/clk.c +++ b/drivers/clk/clk.c @@ -21,6 +21,8 @@ struct clk { unsigned int enable_count; unsigned int prepare_count; struct clk *parent; + struct hlist_head children; + struct hlist_node child_node; unsigned long rate; }; @@ -176,10 +178,61 @@ long clk_round_rate(struct clk *clk, unsigned long rate) } EXPORT_SYMBOL_GPL(clk_round_rate); +/* + * clk_recalc_rates - Given a clock (with a recently updated clk->rate), + * notify its children that the rate may need to be recalculated, using + * ops->recalc_rate(). + */ +static void clk_recalc_rates(struct clk *clk) +{ + struct hlist_node *tmp; + struct clk *child; + + if (clk->ops->recalc_rate) + clk->rate = clk->ops->recalc_rate(clk->hw); + + hlist_for_each_entry(child, tmp, &clk->children, child_node) + clk_recalc_rates(child); +} + int clk_set_rate(struct clk *clk, unsigned long rate) { - /* not yet implemented */ - return -ENOSYS; + unsigned long parent_rate, new_rate; + int ret; + + if (!clk->ops->set_rate) + return -ENOSYS; + + new_rate = rate; + + /* prevent racing with updates to the clock topology */ + mutex_lock(&prepare_lock); + +propagate: + ret = clk->ops->set_rate(clk->hw, new_rate, &parent_rate); + + if (ret < 0) + return ret; + + /* ops->set_rate may require the parent's rate to change (to + * parent_rate), we need to propagate the set_rate call to the + * parent. + */ + if (ret == CLK_SET_RATE_PROPAGATE) { + new_rate = parent_rate; + clk = clk->parent; + goto propagate; + } + + /* If successful (including propagation to the parent clock(s)), + * recalculate the rates of the clock, including children. We'll be + * calling this on the 'parent-most' clock that we propagated to. + */ + clk_recalc_rates(clk); + + mutex_unlock(&prepare_lock); + + return 0; } EXPORT_SYMBOL_GPL(clk_set_rate); @@ -213,16 +266,25 @@ struct clk *clk_register(struct clk_hw_ops *ops, struct clk_hw *hw, clk->hw = hw; hw->clk = clk; - /* Query the hardware for parent and initial rate */ + /* Query the hardware for parent and initial rate. We may alter + * the clock topology, making this clock available from the parent's + * children list. So, we need to protect against concurrent + * accesses through set_rate + */ + mutex_lock(&prepare_lock); - if (clk->ops->get_parent) - /* We don't to lock against prepare/enable here, as - * the clock is not yet accessible from anywhere */ + if (clk->ops->get_parent) { clk->parent = clk->ops->get_parent(clk->hw); + if (clk->parent) + hlist_add_head(&clk->child_node, + &clk->parent->children); + } if (clk->ops->recalc_rate) clk->rate = clk->ops->recalc_rate(clk->hw); + mutex_unlock(&prepare_lock); + return clk; } diff --git a/include/linux/clk.h b/include/linux/clk.h index 93ff870..e0969d2 100644 --- a/include/linux/clk.h +++ b/include/linux/clk.h @@ -58,6 +58,12 @@ struct clk_hw { * parent. Currently only called when the clock is first * registered. * + * @set_rate Change the rate of this clock. If this callback returns + * CLK_SET_RATE_PROPAGATE, the rate change will be propagated to + * the parent clock (which may propagate again). The requested + * rate of the parent is passed back from the callback in the + * second 'unsigned long *' argument. + * * The clk_enable/clk_disable and clk_prepare/clk_unprepare pairs allow * implementations to split any work between atomic (enable) and sleepable * (prepare) contexts. If a clock requires sleeping code to be turned on, this @@ -76,9 +82,15 @@ struct clk_hw_ops { void (*disable)(struct clk_hw *); unsigned long (*recalc_rate)(struct clk_hw *); long (*round_rate)(struct clk_hw *, unsigned long); + int (*set_rate)(struct clk_hw *, + unsigned long, unsigned long *); struct clk * (*get_parent)(struct clk_hw *); }; +enum { + CLK_SET_RATE_PROPAGATE = 1, +}; + /** * clk_prepare - prepare clock for atomic enabling. *