devicetree.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: kernel@martin.sperl.org
To: Rob Herring <robh+dt@kernel.org>,
	Stephen Warren <swarren@wwwdotorg.org>,
	Lee Jones <lee@kernel.org>, Eric Anholt <eric@anholt.net>,
	Michael Turquette <mturquette@baylibre.com>,
	Stephen Boyd <sboyd@codeaurora.org>,
	Remi Pommarel <repk@triplefau.lt>,
	devicetree@vger.kernel.org, linux-rpi-kernel@lists.infradead.org,
	linux-arm-kernel@lists.infradead.org, linux-clk@vger.kernel.org
Cc: Martin Sperl <kernel@martin.sperl.org>
Subject: [RFC 4/9] clk: bcm2835: reorganize bcm2835_clock_determine_rate
Date: Tue, 19 Jan 2016 14:51:35 +0000	[thread overview]
Message-ID: <1453215100-2382-5-git-send-email-kernel@martin.sperl.org> (raw)
In-Reply-To: <1453215100-2382-1-git-send-email-kernel@martin.sperl.org>

From: Martin Sperl <kernel@martin.sperl.org>

Reorganize bcm2835_clock_determine_rate so that the per
clock frequency calculation is separated from the
selection logic.

This allows for different clock selection policies
to be use while keeping code common.

Signed-off-by: Martin Sperl <kernel@martin.sperl.org>
---
 drivers/clk/bcm/clk-bcm2835.c |   98 +++++++++++++++++++++++++++++------------
 1 file changed, 70 insertions(+), 28 deletions(-)

diff --git a/drivers/clk/bcm/clk-bcm2835.c b/drivers/clk/bcm/clk-bcm2835.c
index 48c1ad0..f1ab525 100644
--- a/drivers/clk/bcm/clk-bcm2835.c
+++ b/drivers/clk/bcm/clk-bcm2835.c
@@ -1668,44 +1668,86 @@ unlock_exit:
 	return 0;
 }
 
-static int bcm2835_clock_determine_rate(struct clk_hw *hw,
-		struct clk_rate_request *req)
+struct bcm2835_rates {
+	struct clk_hw *parent;
+	unsigned long rate;
+	unsigned long prate;
+	u32 div;
+	divmash dmash;
+};
+
+static int bcm2835_clock_determine_rate_set(struct clk_rate_request *req,
+					    struct bcm2835_rates *best)
 {
-	struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
-	struct clk_hw *parent, *best_parent = NULL;
-	unsigned long rate, best_rate = 0;
-	unsigned long prate, best_prate = 0;
+	req->best_parent_hw = best->parent;
+	req->best_parent_rate = best->prate;
+	req->rate = best->rate;
+
+	return 0;
+}
+
+static int bcm2835_clock_determine_closest_rate(struct clk_hw *hw,
+						struct clk_rate_request *req,
+						struct bcm2835_rates *rates,
+						size_t rate_cnt)
+{
+	struct bcm2835_rates *best = NULL;
 	size_t i;
-	divmash dm;
-	u32 div;
 
-	/*
-	 * Select parent clock that results in the closest but lower rate
-	 */
-	for (i = 0; i < clk_hw_get_num_parents(hw); ++i) {
-		parent = clk_hw_get_parent_by_index(hw, i);
-		if (!parent)
+	/* find best matching */
+	for (i = 0; i < rate_cnt; i++) {
+		if (rates[i].rate > req->rate)
+			continue;
+		if (!best) {
+			best = &rates[i];
+			continue;
+		}
+		/* find the closest */
+		if (rates[i].rate > best->rate) {
+			best = &rates[i];
+			continue;
+		}
+		/* if identical then use highest divider */
+		if ((rates[i].rate == best->rate) &&
+		    (rates[i].div > best->div)) {
+			best = &rates[i];
 			continue;
-		prate = clk_hw_get_rate(parent);
-		dm = bcm2835_clock_choose_div(hw, req->rate, prate, true);
-		div = divmash_get_div(dm);
-		rate = bcm2835_clock_rate_from_divisor(clock, prate, div);
-		if (rate > best_rate && rate <= req->rate) {
-			best_parent = parent;
-			best_prate = prate;
-			best_rate = rate;
 		}
 	}
 
-	if (!best_parent)
-		return -EINVAL;
+	if (best)
+		return bcm2835_clock_determine_rate_set(req, best);
+
+	/* nothing found */
+	return -EINVAL;
+}
 
-	req->best_parent_hw = best_parent;
-	req->best_parent_rate = best_prate;
+static int bcm2835_clock_determine_rate(struct clk_hw *hw,
+					struct clk_rate_request *req)
+{
+	struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
+	struct bcm2835_rates rates[BIT(CM_SRC_BITS)];
+	size_t i, rate_cnt = 0;
+	divmash dm;
 
-	req->rate = best_rate;
+	/* fill in rates */
+	for (i = 0; i < clk_hw_get_num_parents(hw); ++i) {
+		rates[rate_cnt].parent = clk_hw_get_parent_by_index(hw, i);
+		if (!rates[rate_cnt].parent)
+			continue;
+		rates[rate_cnt].prate = clk_hw_get_rate(
+			rates[rate_cnt].parent);
+		dm = bcm2835_clock_choose_div(
+			hw, req->rate, rates[rate_cnt].prate, true);
+		rates[rate_cnt].div =  divmash_get_div(dm);
+		rates[rate_cnt].rate = bcm2835_clock_rate_from_divisor(
+			clock, rates[rate_cnt].prate, rates[rate_cnt].div);
+		rate_cnt++;
+	}
 
-	return 0;
+	/* choose the "closest" one */
+	return bcm2835_clock_determine_closest_rate(hw, req, rates,
+						    rate_cnt);
 }
 
 static int _bcm2835_clk_set_parent(struct bcm2835_cprman *cprman,
-- 
1.7.10.4


  parent reply	other threads:[~2016-01-19 14:51 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-01-19 14:51 [RFC 0/9] Allow modifications of specific clocks via DT and more kernel-TqfNSX0MhmxHKSADF0wUEw
2016-01-19 14:51 ` [RFC 2/9] clk: bcm2835: add support for parent selection in DT kernel
2016-01-21  8:26   ` Sascha Hauer
2016-02-08 13:30     ` Martin Sperl
2016-01-19 14:51 ` [RFC 3/9] clk: bcm2835: add ability to control mash level via device-tree kernel
2016-01-19 14:51 ` kernel [this message]
2016-01-19 14:51 ` [RFC 5/9] clk: bcm2835: prefer clocks that use integer dividers kernel
2016-01-19 14:51 ` [RFC 6/9] clk: bcm2835: allow to define a minimum fractional divider in DT kernel
     [not found] ` <1453215100-2382-1-git-send-email-kernel-TqfNSX0MhmxHKSADF0wUEw@public.gmane.org>
2016-01-19 14:51   ` [RFC 1/9] clk: bcm2835: add basic device tree support for per clock settings kernel-TqfNSX0MhmxHKSADF0wUEw
2016-01-19 14:51   ` [RFC 7/9] clk: bcm2835: allow clock choosing mechanims to be selected in DT kernel-TqfNSX0MhmxHKSADF0wUEw
2016-01-19 14:51 ` [RFC 8/9] dt-bindings: bcm2835: document optional per clock dt-nodes kernel
2016-01-19 14:51 ` [RFC 9/9] clk: bcm2835: expose raw clock-registers via debugfs kernel

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=1453215100-2382-5-git-send-email-kernel@martin.sperl.org \
    --to=kernel@martin.sperl.org \
    --cc=devicetree@vger.kernel.org \
    --cc=eric@anholt.net \
    --cc=lee@kernel.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-clk@vger.kernel.org \
    --cc=linux-rpi-kernel@lists.infradead.org \
    --cc=mturquette@baylibre.com \
    --cc=repk@triplefau.lt \
    --cc=robh+dt@kernel.org \
    --cc=sboyd@codeaurora.org \
    --cc=swarren@wwwdotorg.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).