From: Sascha Hauer <s.hauer@pengutronix.de>
To: Abel Vesa <abel.vesa@nxp.com>
Cc: linux-clk@vger.kernel.org,
Michael Turquette <mturquette@baylibre.com>,
Stephen Boyd <sboyd@kernel.org>,
Pengutronix Kernel Team <kernel@pengutronix.de>,
Fabio Estevam <festevam@gmail.com>,
NXP Linux Team <linux-imx@nxp.com>,
Adrian Alonso <adrian.alonso@nxp.com>,
Mads Bligaard Nielsen <bli@bang-olufsen.dk>
Subject: Re: [PATCH v2 8/8] clk: imx: pll14xx: Support dynamic rates
Date: Fri, 4 Mar 2022 13:44:05 +0100 [thread overview]
Message-ID: <20220304124405.GK22780@pengutronix.de> (raw)
In-Reply-To: <Yhjj8o3vEQy0qysO@abelvesa>
On Fri, Feb 25, 2022 at 04:13:06PM +0200, Abel Vesa wrote:
> On 22-02-25 09:29:37, Sascha Hauer wrote:
> > The pll1443x PLL so far only supports rates from a rate table passed
> > during initialization. Calculating PLL settings dynamically helps audio
> > applications to get their desired rates, so support for this is added
> > in this patch.
> > + const struct imx_pll14xx_rate_table *tt;
> > +
> > + /*
> > + * Fractional PLL constrains:
> > + *
> > + * a) 6MHz <= prate <= 25MHz
> > + * b) 1 <= p <= 63 (1 <= p <= 4 prate = 24MHz)
> > + * c) 64 <= m <= 1023
> > + * d) 0 <= s <= 6
> > + * e) -32768 <= k <= 32767
> > + *
> > + * fvco = (m * 65536 + k) * prate / (p * 65536)
> > + */
> > +
> > + pll_div_ctl0 = readl_relaxed(pll->base + DIV_CTL0);
> > + mdiv = FIELD_GET(MDIV_MASK, pll_div_ctl0);
> > + pdiv = FIELD_GET(PDIV_MASK, pll_div_ctl0);
> > + sdiv = FIELD_GET(SDIV_MASK, pll_div_ctl0);
> > + pll_div_ctl1 = readl_relaxed(pll->base + DIV_CTL1);
> > +
> > + /* First see if we can get the desired rate by only adjusting kdiv (glitch free) */
> > + rate_min = pll14xx_calc_rate(pll, mdiv, pdiv, sdiv, KDIV_MIN, prate);
> > + rate_max = pll14xx_calc_rate(pll, mdiv, pdiv, sdiv, KDIV_MAX, prate);
> > +
> > + if (rate >= rate_min && rate <= rate_max) {
> > + kdiv = pll1443x_calc_kdiv(mdiv, pdiv, sdiv, rate, prate);
> > + pr_debug("%s: in=%ld, want=%ld Only adjust kdiv %ld -> %d\n",
> > + clk_hw_get_name(&pll->hw), prate, rate,
> > + FIELD_GET(KDIV_MASK, pll_div_ctl1), kdiv);
> > + fvco = pll14xx_calc_rate(pll, mdiv, pdiv, sdiv, kdiv, prate);
> > + t->rate = (unsigned int)fvco;
> > + t->mdiv = mdiv;
> > + t->pdiv = pdiv;
> > + t->sdiv = sdiv;
> > + t->kdiv = kdiv;
> > + return;
> > + }
> > +
> > + /* Then try if we can get the desired rate from one of the static entries */
> > + tt = imx_get_pll_settings(pll, rate);
>
> Shouldn't we try this one first? Maybe we don't need to compute kdiv at
> all.
Sorry, missed that part of your mail.
The intention was to try the glitchfree path first so that we switch
glitchfree when possible. For most cases the order doesn't make a
difference, I think we can change it if you like.
There's one case in which we end up with a glitch where we could do a
glitchfree switch: We come from some rate to a rate which is nearly but
not exactly the rate from one of the table entries. From there we then
switch to the exact rate from the table entry. The algorithm below will
find other values than the ones in the table entries, so the PLL
switches to the same output frequency with completely different values.
I don't know if anyone ever hits this and then if he has a problem with
the PLL being restartet, so yes, we can change the order.
Sascha
>
> > + if (tt) {
> > + pr_debug("%s: in=%ld, want=%ld, Using PLL setting from table\n",
> > + clk_hw_get_name(&pll->hw), prate, rate);
> > + t->rate = tt->rate;
> > + t->mdiv = tt->mdiv;
> > + t->pdiv = tt->pdiv;
> > + t->sdiv = tt->sdiv;
> > + t->kdiv = tt->kdiv;
> > + return;
> > + }
> > +
> > + /* Finally calculate best values */
> > + for (pdiv = 1; pdiv <= 7; pdiv++) {
> > + for (sdiv = 0; sdiv <= 6; sdiv++) {
> > + /* calc mdiv = round(rate * pdiv * 2^sdiv) / prate) */
> > + mdiv = DIV_ROUND_CLOSEST(rate * (pdiv << sdiv), prate);
> > + mdiv = clamp(mdiv, 64, 1023);
> > +
> > + kdiv = pll1443x_calc_kdiv(mdiv, pdiv, sdiv, rate, prate);
> > + fvco = pll14xx_calc_rate(pll, mdiv, pdiv, sdiv, kdiv, prate);
> > +
> > + /* best match */
> > + dist = abs((long)rate - (long)fvco);
> > + if (dist < best) {
> > + best = dist;
> > + t->rate = (unsigned int)fvco;
> > + t->mdiv = mdiv;
> > + t->pdiv = pdiv;
> > + t->sdiv = sdiv;
> > + t->kdiv = kdiv;
> > +
> > + if (!dist)
> > + goto found;
> > + }
> > + }
> > + }
--
Pengutronix e.K. | |
Steuerwalder Str. 21 | http://www.pengutronix.de/ |
31137 Hildesheim, Germany | Phone: +49-5121-206917-0 |
Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |
prev parent reply other threads:[~2022-03-04 12:44 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-02-25 8:29 [PATCH v2 0/8] clk: i.MX: PLL14xx: Support dynamic rates Sascha Hauer
2022-02-25 8:29 ` [PATCH v2 1/8] clk: imx: pll14xx: Use register defines consistently Sascha Hauer
2022-02-25 8:29 ` [PATCH v2 2/8] clk: imx: pll14xx: Drop wrong shifting Sascha Hauer
2022-02-25 8:29 ` [PATCH v2 3/8] clk: imx: pll14xx: Use FIELD_GET/FIELD_PREP Sascha Hauer
2022-02-25 8:29 ` [PATCH v2 4/8] clk: imx: pll14xx: consolidate rate calculation Sascha Hauer
2022-02-25 13:24 ` Abel Vesa
2022-02-25 8:29 ` [PATCH v2 5/8] clk: imx: pll14xx: name variables after usage Sascha Hauer
2022-02-25 13:25 ` Abel Vesa
2022-02-25 8:29 ` [PATCH v2 6/8] clk: imx: pll14xx: explicitly return lowest rate Sascha Hauer
2022-02-25 13:26 ` Abel Vesa
2022-02-25 8:29 ` [PATCH v2 7/8] clk: imx: pll14xx: Add pr_fmt Sascha Hauer
2022-02-25 13:27 ` Abel Vesa
2022-02-25 8:29 ` [PATCH v2 8/8] clk: imx: pll14xx: Support dynamic rates Sascha Hauer
2022-02-25 14:13 ` Abel Vesa
2022-03-04 8:39 ` Abel Vesa
2022-03-04 12:44 ` Sascha Hauer [this message]
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=20220304124405.GK22780@pengutronix.de \
--to=s.hauer@pengutronix.de \
--cc=abel.vesa@nxp.com \
--cc=adrian.alonso@nxp.com \
--cc=bli@bang-olufsen.dk \
--cc=festevam@gmail.com \
--cc=kernel@pengutronix.de \
--cc=linux-clk@vger.kernel.org \
--cc=linux-imx@nxp.com \
--cc=mturquette@baylibre.com \
--cc=sboyd@kernel.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