linux-arm-kernel.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
From: mturquette@ti.com (Mike Turquette)
To: linux-arm-kernel@lists.infradead.org
Subject: [PATCH v2 2/7] clk: Implement clk_set_rate
Date: Thu, 22 Sep 2011 15:26:57 -0700	[thread overview]
Message-ID: <1316730422-20027-3-git-send-email-mturquette@ti.com> (raw)
In-Reply-To: <1316730422-20027-1-git-send-email-mturquette@ti.com>

From: Jeremy Kerr <jeremy.kerr@canonical.com>

Implement clk_set_rate by adding a set_rate callback to clk_hw_ops.
Rates are propagated down the clock tree and recalculated.  Also adds a
flag for signaling that parents must change rates to achieve the desired
frequency (upstream propagation).

TODO:
Upstream propagation is not yet implemented.
Device pre-change and post-change notifications are not implemented, but
are marked up as FIXME comments.

Signed-off-by: Jeremy Kerr <jeremy.kerr@canonical.com>
Signed-off-by: Mark Brown <broonie@opensource.wolfsonmicro.com>
Signed-off-by: Mike Turquette <mturquette@ti.com>
---
Changes since v1:
Remove upstream propagation (for now)
Rename CLK_SET_RATE_PROPAGATE to CLK_PARENT_RATE_CHANGE

 drivers/clk/clk.c   |   71 ++++++++++++++++++++++++++++++++++++++++++++++----
 include/linux/clk.h |   12 ++++++++
 2 files changed, 77 insertions(+), 6 deletions(-)

diff --git a/drivers/clk/clk.c b/drivers/clk/clk.c
index 1cd7315..86636c2 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,57 @@ 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);
+
+	/* FIXME add post-rate change notification here */
+
+	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 = 0;
+
+	if (!clk->ops->set_rate)
+		return -ENOSYS;
+
+	new_rate = rate;
+
+	/* prevent racing with updates to the clock topology */
+	mutex_lock(&prepare_lock);
+
+	/* FIXME add pre-rate change notification here */
+
+	ret = clk->ops->set_rate(clk->hw, new_rate, &parent_rate);
+
+	/* FIXME ignores CLK_PARENT_RATE_CHANGE */
+	if (ret < 0)
+		/* FIXME add rate change abort notification here */
+		goto out;
+
+	/*
+	 * If successful recalculate the rates of the clock, including
+	 * children.
+	 */
+	clk_recalc_rates(clk);
+
+out:
+	mutex_unlock(&prepare_lock);
+
+	return ret;
 }
 EXPORT_SYMBOL_GPL(clk_set_rate);
 
@@ -216,16 +265,26 @@ struct clk *clk_register(const 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 d6ae10b..0d2cd5e 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_PARENT_RATE_CHANGE = 1,
+};
+
 /**
  * clk_prepare - prepare clock for atomic enabling.
  *
-- 
1.7.4.1

  parent reply	other threads:[~2011-09-22 22:26 UTC|newest]

Thread overview: 72+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-09-22 22:26 [PATCH v2 0/7] Add a generic struct clk Mike Turquette
2011-09-22 22:26 ` [PATCH v2 1/7] clk: Add a generic clock infrastructure Mike Turquette
2011-09-25  3:55   ` Grant Likely
2011-09-25  5:26     ` Turquette, Mike
2011-10-03 14:17   ` Rob Herring
2011-10-03 14:25     ` Mark Brown
2011-10-03 15:24       ` Rob Herring
2011-10-03 16:31         ` Mark Brown
2011-10-03 16:43           ` Russell King - ARM Linux
2011-10-03 17:05             ` Mark Brown
2011-10-04 18:09     ` Grant Likely
2011-10-27 11:54     ` Domenico Andreoli
2011-10-03 22:02   ` Rob Herring
2011-10-03 22:15     ` Turquette, Mike
2011-10-06  1:17   ` Saravana Kannan
2011-10-06 16:11     ` Turquette, Mike
2011-10-11 11:25   ` Richard Zhao
2011-10-13 14:44   ` Russell King - ARM Linux
2011-10-13 17:16     ` Turquette, Mike
2011-10-14  8:10   ` Richard Zhao
2011-10-14 10:05     ` Mark Brown
2011-10-14 10:32       ` Richard Zhao
2011-10-16 17:55         ` Sascha Hauer
2011-10-17  8:48           ` Richard Zhao
2011-10-17  9:20             ` Mark Brown
2011-10-17 10:53               ` Richard Zhao
2011-10-17 11:05                 ` Sascha Hauer
2011-10-17 11:30                   ` Russell King - ARM Linux
2011-10-14 18:14   ` Turquette, Mike
2011-10-15  2:24     ` Richard Zhao
2011-10-15  2:34       ` Richard Zhao
2011-10-16 21:17       ` Turquette, Mike
2011-10-17 11:31         ` Richard Zhao
2011-10-21  9:00   ` Richard Zhao
2011-10-23 12:55   ` Shawn Guo
2011-10-23 16:49     ` Turquette, Mike
2011-09-22 22:26 ` Mike Turquette [this message]
2011-10-11 11:49   ` [PATCH v2 2/7] clk: Implement clk_set_rate Richard Zhao
2011-10-23 14:24   ` Shawn Guo
2011-10-23 16:50     ` Turquette, Mike
2011-09-22 22:26 ` [PATCH v2 3/7] clk: Add fixed-rate clock Mike Turquette
2011-10-23 14:30   ` Shawn Guo
2011-10-23 16:51     ` Turquette, Mike
2011-09-22 22:26 ` [PATCH v2 4/7] clk: Add simple gated clock Mike Turquette
2011-09-25  4:02   ` Grant Likely
2011-09-25  5:27     ` Turquette, Mike
2011-09-26 18:33   ` Rob Herring
2011-09-26 18:40     ` Jamie Iles
2011-09-26 19:10       ` Rob Herring
2011-09-26 19:37         ` Jamie Iles
2011-09-26 22:37           ` Turquette, Mike
2011-09-26 23:30             ` Rob Herring
2011-10-05  1:41               ` Saravana Kannan
2011-10-12  6:46   ` Richard Zhao
2011-10-12 14:59     ` Turquette, Mike
2011-10-16 18:26       ` Sascha Hauer
2011-10-17  6:42         ` Richard Zhao
2011-10-17 17:46           ` Turquette, Mike
2011-10-13 14:45   ` Russell King - ARM Linux
2011-10-13 17:18     ` Turquette, Mike
2011-09-22 22:27 ` [PATCH v2 5/7] clk: Add Kconfig option to build all generic clk drivers Mike Turquette
2011-09-22 22:27 ` [PATCH v2 6/7] clk: Add initial WM831x clock driver Mike Turquette
2011-09-25  4:08   ` Grant Likely
2011-09-25  5:29     ` Turquette, Mike
2011-09-26  9:38     ` Mark Brown
2011-10-04 18:18       ` Grant Likely
2011-10-04 20:50         ` Mark Brown
2011-10-04 23:22           ` Grant Likely
2011-09-22 22:27 ` [PATCH v2 7/7] x86: Enable generic clk API on x86 Mike Turquette
2011-09-22 23:17 ` [PATCH v2 0/7] Add a generic struct clk Turquette, Mike
2011-09-25  4:10 ` Grant Likely
2011-09-29 18:54 ` Mark Brown

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1316730422-20027-3-git-send-email-mturquette@ti.com \
    --to=mturquette@ti.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).