From: Ezequiel Garcia <ezequiel.garcia@imgtec.com>
To: <linux-mips@linux-mips.org>, <linux-kernel@vger.kernel.org>,
"Mike Turquette" <mturquette@linaro.org>, <sboyd@codeaurora.org>
Cc: Andrew Bresticker <abrestic@chromium.org>,
James Hartley <james.hartley@imgtec.com>,
<Govindraj.Raja@imgtec.com>, <Damien.Horsley@imgtec.com>,
<cernekee@chromium.org>, James Hogan <james.hogan@imgtec.com>,
Ezequiel Garcia <ezequiel.garcia@imgtec.com>
Subject: [PATCH 3/9] clk: pistachio: Implement PLL rate adjustment
Date: Thu, 21 May 2015 20:57:37 -0300 [thread overview]
Message-ID: <1432252663-31318-4-git-send-email-ezequiel.garcia@imgtec.com> (raw)
In-Reply-To: <1432252663-31318-1-git-send-email-ezequiel.garcia@imgtec.com>
This commit implements small rate changes to the fractional PLL.
This is done using the PLL frac parameter. The .set_rate function
first finds the parameters associated to the closest nominal rate.
Then the new rate is set, using parameters from the table entry,
except for the frac parameter, which is calculated from the rate
using the fractional PLL rate formula.
Using .round_rate, the driver guarantees that only rates near
a table nominal rate is applied. To this extent, add two parameters
fout_min and fout_max, which allows to define the allowed rate
adjustment.
Signed-off-by: Ezequiel Garcia <ezequiel.garcia@imgtec.com>
---
drivers/clk/pistachio/clk-pll.c | 48 +++++++++++++++++++++++++++++++----------
drivers/clk/pistachio/clk.h | 2 ++
2 files changed, 39 insertions(+), 11 deletions(-)
diff --git a/drivers/clk/pistachio/clk-pll.c b/drivers/clk/pistachio/clk-pll.c
index f12d520..cf000bb 100644
--- a/drivers/clk/pistachio/clk-pll.c
+++ b/drivers/clk/pistachio/clk-pll.c
@@ -90,29 +90,50 @@ static struct pistachio_pll_rate_table *
pll_get_params(struct pistachio_clk_pll *pll, unsigned long fref,
unsigned long fout)
{
- unsigned int i;
+ unsigned int i, best;
+ unsigned long err, best_err = ~0;
for (i = 0; i < pll->nr_rates; i++) {
- if (pll->rates[i].fref == fref && pll->rates[i].fout == fout)
- return &pll->rates[i];
+ err = abs(pll->rates[i].fout - fout);
+ if (pll->rates[i].fref == fref && err < best_err) {
+ best = i;
+ best_err = err;
+ }
}
- return NULL;
+ return &pll->rates[best];
}
static long pll_round_rate(struct clk_hw *hw, unsigned long rate,
unsigned long *parent_rate)
{
struct pistachio_clk_pll *pll = to_pistachio_pll(hw);
- unsigned int i;
+ unsigned int i, best;
+ unsigned long err, best_err = ~0;
for (i = 0; i < pll->nr_rates; i++) {
- if (i > 0 && pll->rates[i].fref == *parent_rate &&
- pll->rates[i].fout <= rate)
- return pll->rates[i - 1].fout;
+ err = abs(pll->rates[i].fout - rate);
+ if (pll->rates[i].fref == *parent_rate && err < best_err) {
+ best = i;
+ best_err = err;
+ }
}
- return pll->rates[0].fout;
+ /* Make sure fout_{min,max} parameters have sane values */
+ if (!pll->rates[best].fout_min)
+ pll->rates[best].fout_min = pll->rates[best].fout;
+ if (!pll->rates[best].fout_max)
+ pll->rates[best].fout_max = pll->rates[best].fout;
+
+ /*
+ * If the chosen rate is within the maximum allowed PLL adjustment
+ * then we accept it.
+ * Otherwise, just return the closest nominal table rate.
+ */
+ if (rate <= pll->rates[best].fout_max &&
+ rate >= pll->rates[best].fout_min)
+ return rate;
+ return pll->rates[best].fout;
}
static int pll_gf40lp_frac_enable(struct clk_hw *hw)
@@ -158,12 +179,17 @@ static int pll_gf40lp_frac_set_rate(struct clk_hw *hw, unsigned long rate,
struct pistachio_clk_pll *pll = to_pistachio_pll(hw);
struct pistachio_pll_rate_table *params;
int enabled = pll_gf40lp_frac_is_enabled(hw);
- u32 val;
+ u32 val, frac;
params = pll_get_params(pll, parent_rate, rate);
if (!params)
return -EINVAL;
+ /* Calculate the frac parameter */
+ frac = rate * params->refdiv * params->postdiv1 * params->postdiv2;
+ frac -= (params->fbdiv * parent_rate);
+ frac = do_div_round_closest((u64)frac << 24, parent_rate);
+
val = pll_readl(pll, PLL_CTRL1);
val &= ~((PLL_CTRL1_REFDIV_MASK << PLL_CTRL1_REFDIV_SHIFT) |
(PLL_CTRL1_FBDIV_MASK << PLL_CTRL1_FBDIV_SHIFT));
@@ -177,7 +203,7 @@ static int pll_gf40lp_frac_set_rate(struct clk_hw *hw, unsigned long rate,
PLL_FRAC_CTRL2_POSTDIV1_SHIFT) |
(PLL_FRAC_CTRL2_POSTDIV2_MASK <<
PLL_FRAC_CTRL2_POSTDIV2_SHIFT));
- val |= (params->frac << PLL_FRAC_CTRL2_FRAC_SHIFT) |
+ val |= (frac << PLL_FRAC_CTRL2_FRAC_SHIFT) |
(params->postdiv1 << PLL_FRAC_CTRL2_POSTDIV1_SHIFT) |
(params->postdiv2 << PLL_FRAC_CTRL2_POSTDIV2_SHIFT);
pll_writel(pll, val, PLL_CTRL2);
diff --git a/drivers/clk/pistachio/clk.h b/drivers/clk/pistachio/clk.h
index 52fabbc..ea48d15 100644
--- a/drivers/clk/pistachio/clk.h
+++ b/drivers/clk/pistachio/clk.h
@@ -97,6 +97,8 @@ struct pistachio_fixed_factor {
struct pistachio_pll_rate_table {
unsigned long fref;
unsigned long fout;
+ unsigned long fout_min;
+ unsigned long fout_max;
unsigned int refdiv;
unsigned int fbdiv;
unsigned int postdiv1;
--
2.3.3
next prev parent reply other threads:[~2015-05-22 0:03 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-05-21 23:57 [PATCH 0/9] clk: pistachio: Assorted changes Ezequiel Garcia
2015-05-21 23:57 ` [PATCH 1/9] clk: pistachio: Add a pll_lock() helper for clarity Ezequiel Garcia
2015-05-21 23:57 ` [PATCH 2/9] clk: pistachio: Lock the PLL when enabled upon rate change Ezequiel Garcia
2015-05-21 23:57 ` Ezequiel Garcia [this message]
2015-05-22 17:04 ` [PATCH 3/9] clk: pistachio: Implement PLL rate adjustment Andrew Bresticker
2015-05-21 23:57 ` [PATCH 4/9] clk: pistachio: Extend DIV_F to pass clk_flags as well Ezequiel Garcia
2015-05-22 17:05 ` Andrew Bresticker
2015-05-21 23:57 ` [PATCH 5/9] clk: pistachio: Add a MUX_F macro to pass clk_flags Ezequiel Garcia
2015-05-22 17:07 ` Andrew Bresticker
2015-05-21 23:57 ` [PATCH 6/9] clk: pistachio: Propagate rate changes in the MIPS PLL clock sub-tree Ezequiel Garcia
2015-05-22 17:42 ` Andrew Bresticker
2015-05-22 17:52 ` Ezequiel Garcia
2015-05-22 18:07 ` Andrew Bresticker
2015-05-21 23:57 ` [PATCH 7/9] clk: pistachio: Add a rate table for the MIPS PLL Ezequiel Garcia
2015-05-22 17:45 ` Andrew Bresticker
2015-05-22 17:45 ` Ezequiel Garcia
2015-05-21 23:57 ` [PATCH 8/9] clk: pistachio: Add sanity checks on PLL configuration Ezequiel Garcia
2015-05-22 17:48 ` Andrew Bresticker
2015-05-21 23:57 ` [PATCH 9/9] clk: pistachio: Correct critical clock list Ezequiel Garcia
2015-05-22 17:56 ` Andrew Bresticker
2015-05-22 17:54 ` Ezequiel Garcia
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=1432252663-31318-4-git-send-email-ezequiel.garcia@imgtec.com \
--to=ezequiel.garcia@imgtec.com \
--cc=Damien.Horsley@imgtec.com \
--cc=Govindraj.Raja@imgtec.com \
--cc=abrestic@chromium.org \
--cc=cernekee@chromium.org \
--cc=james.hartley@imgtec.com \
--cc=james.hogan@imgtec.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mips@linux-mips.org \
--cc=mturquette@linaro.org \
--cc=sboyd@codeaurora.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
all inboxes | Powered by JetHome®