1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2016 Atmel Corporation,
6 * Copyright (C) 2017 Free Electrons,
9 * The Sama5d2 SoC has two audio PLLs (PMC and PAD) that shares the same parent
10 * (FRAC). FRAC can output between 620 and 700MHz and only multiply the rate of
11 * its own parent. PMC and PAD can then divide the FRAC rate to best match the
14 * Traits of FRAC clock:
15 * enable - clk_enable writes nd, fracr parameters and enables PLL
16 * rate - rate is adjustable.
17 * clk->rate = parent->rate * ((nd + 1) + (fracr / 2^22))
18 * parent - fixed parent. No clk_set_parent support
20 * Traits of PMC clock:
21 * enable - clk_enable writes qdpmc, and enables PMC output
22 * rate - rate is adjustable.
23 * clk->rate = parent->rate / (qdpmc + 1)
24 * parent - fixed parent. No clk_set_parent support
26 * Traits of PAD clock:
27 * enable - clk_enable writes divisors and enables PAD output
28 * rate - rate is adjustable.
29 * clk->rate = parent->rate / (qdaudio * div))
30 * parent - fixed parent. No clk_set_parent support
33 #include <linux/clk.h>
34 #include <linux/clk-provider.h>
35 #include <linux/clk/at91_pmc.h>
37 #include <linux/mfd/syscon.h>
38 #include <linux/regmap.h>
39 #include <linux/slab.h>
43 #define AUDIO_PLL_DIV_FRAC BIT(22)
44 #define AUDIO_PLL_ND_MAX (AT91_PMC_AUDIO_PLL_ND_MASK >> \
45 AT91_PMC_AUDIO_PLL_ND_OFFSET)
47 #define AUDIO_PLL_QDPAD(qd, div) ((AT91_PMC_AUDIO_PLL_QDPAD_EXTDIV(qd) & \
48 AT91_PMC_AUDIO_PLL_QDPAD_EXTDIV_MASK) | \
49 (AT91_PMC_AUDIO_PLL_QDPAD_DIV(div) & \
50 AT91_PMC_AUDIO_PLL_QDPAD_DIV_MASK))
52 #define AUDIO_PLL_QDPMC_MAX (AT91_PMC_AUDIO_PLL_QDPMC_MASK >> \
53 AT91_PMC_AUDIO_PLL_QDPMC_OFFSET)
55 #define AUDIO_PLL_FOUT_MIN 620000000UL
56 #define AUDIO_PLL_FOUT_MAX 700000000UL
58 struct clk_audio_frac {
60 struct regmap *regmap;
65 struct clk_audio_pad {
67 struct regmap *regmap;
72 struct clk_audio_pmc {
74 struct regmap *regmap;
78 #define to_clk_audio_frac(hw) container_of(hw, struct clk_audio_frac, hw)
79 #define to_clk_audio_pad(hw) container_of(hw, struct clk_audio_pad, hw)
80 #define to_clk_audio_pmc(hw) container_of(hw, struct clk_audio_pmc, hw)
82 static int clk_audio_pll_frac_enable(struct clk_hw *hw)
84 struct clk_audio_frac *frac = to_clk_audio_frac(hw);
86 regmap_update_bits(frac->regmap, AT91_PMC_AUDIO_PLL0,
87 AT91_PMC_AUDIO_PLL_RESETN, 0);
88 regmap_update_bits(frac->regmap, AT91_PMC_AUDIO_PLL0,
89 AT91_PMC_AUDIO_PLL_RESETN,
90 AT91_PMC_AUDIO_PLL_RESETN);
91 regmap_update_bits(frac->regmap, AT91_PMC_AUDIO_PLL1,
92 AT91_PMC_AUDIO_PLL_FRACR_MASK, frac->fracr);
95 * reset and enable have to be done in 2 separated writes
96 * for AT91_PMC_AUDIO_PLL0
98 regmap_update_bits(frac->regmap, AT91_PMC_AUDIO_PLL0,
99 AT91_PMC_AUDIO_PLL_PLLEN |
100 AT91_PMC_AUDIO_PLL_ND_MASK,
101 AT91_PMC_AUDIO_PLL_PLLEN |
102 AT91_PMC_AUDIO_PLL_ND(frac->nd));
107 static int clk_audio_pll_pad_enable(struct clk_hw *hw)
109 struct clk_audio_pad *apad_ck = to_clk_audio_pad(hw);
111 regmap_update_bits(apad_ck->regmap, AT91_PMC_AUDIO_PLL1,
112 AT91_PMC_AUDIO_PLL_QDPAD_MASK,
113 AUDIO_PLL_QDPAD(apad_ck->qdaudio, apad_ck->div));
114 regmap_update_bits(apad_ck->regmap, AT91_PMC_AUDIO_PLL0,
115 AT91_PMC_AUDIO_PLL_PADEN, AT91_PMC_AUDIO_PLL_PADEN);
120 static int clk_audio_pll_pmc_enable(struct clk_hw *hw)
122 struct clk_audio_pmc *apmc_ck = to_clk_audio_pmc(hw);
124 regmap_update_bits(apmc_ck->regmap, AT91_PMC_AUDIO_PLL0,
125 AT91_PMC_AUDIO_PLL_PMCEN |
126 AT91_PMC_AUDIO_PLL_QDPMC_MASK,
127 AT91_PMC_AUDIO_PLL_PMCEN |
128 AT91_PMC_AUDIO_PLL_QDPMC(apmc_ck->qdpmc));
132 static void clk_audio_pll_frac_disable(struct clk_hw *hw)
134 struct clk_audio_frac *frac = to_clk_audio_frac(hw);
136 regmap_update_bits(frac->regmap, AT91_PMC_AUDIO_PLL0,
137 AT91_PMC_AUDIO_PLL_PLLEN, 0);
138 /* do it in 2 separated writes */
139 regmap_update_bits(frac->regmap, AT91_PMC_AUDIO_PLL0,
140 AT91_PMC_AUDIO_PLL_RESETN, 0);
143 static void clk_audio_pll_pad_disable(struct clk_hw *hw)
145 struct clk_audio_pad *apad_ck = to_clk_audio_pad(hw);
147 regmap_update_bits(apad_ck->regmap, AT91_PMC_AUDIO_PLL0,
148 AT91_PMC_AUDIO_PLL_PADEN, 0);
151 static void clk_audio_pll_pmc_disable(struct clk_hw *hw)
153 struct clk_audio_pmc *apmc_ck = to_clk_audio_pmc(hw);
155 regmap_update_bits(apmc_ck->regmap, AT91_PMC_AUDIO_PLL0,
156 AT91_PMC_AUDIO_PLL_PMCEN, 0);
159 static unsigned long clk_audio_pll_fout(unsigned long parent_rate,
160 unsigned long nd, unsigned long fracr)
162 unsigned long long fr = (unsigned long long)parent_rate * fracr;
164 pr_debug("A PLL: %s, fr = %llu\n", __func__, fr);
166 fr = DIV_ROUND_CLOSEST_ULL(fr, AUDIO_PLL_DIV_FRAC);
168 pr_debug("A PLL: %s, fr = %llu\n", __func__, fr);
170 return parent_rate * (nd + 1) + fr;
173 static unsigned long clk_audio_pll_frac_recalc_rate(struct clk_hw *hw,
174 unsigned long parent_rate)
176 struct clk_audio_frac *frac = to_clk_audio_frac(hw);
179 fout = clk_audio_pll_fout(parent_rate, frac->nd, frac->fracr);
181 pr_debug("A PLL: %s, fout = %lu (nd = %u, fracr = %lu)\n", __func__,
182 fout, frac->nd, (unsigned long)frac->fracr);
187 static unsigned long clk_audio_pll_pad_recalc_rate(struct clk_hw *hw,
188 unsigned long parent_rate)
190 struct clk_audio_pad *apad_ck = to_clk_audio_pad(hw);
191 unsigned long apad_rate = 0;
193 if (apad_ck->qdaudio && apad_ck->div)
194 apad_rate = parent_rate / (apad_ck->qdaudio * apad_ck->div);
196 pr_debug("A PLL/PAD: %s, apad_rate = %lu (div = %u, qdaudio = %u)\n",
197 __func__, apad_rate, apad_ck->div, apad_ck->qdaudio);
202 static unsigned long clk_audio_pll_pmc_recalc_rate(struct clk_hw *hw,
203 unsigned long parent_rate)
205 struct clk_audio_pmc *apmc_ck = to_clk_audio_pmc(hw);
206 unsigned long apmc_rate = 0;
208 apmc_rate = parent_rate / (apmc_ck->qdpmc + 1);
210 pr_debug("A PLL/PMC: %s, apmc_rate = %lu (qdpmc = %u)\n", __func__,
211 apmc_rate, apmc_ck->qdpmc);
216 static int clk_audio_pll_frac_compute_frac(unsigned long rate,
217 unsigned long parent_rate,
219 unsigned long *fracr)
221 unsigned long long tmp, rem;
227 rem = do_div(tmp, parent_rate);
228 if (!tmp || tmp >= AUDIO_PLL_ND_MAX)
233 tmp = rem * AUDIO_PLL_DIV_FRAC;
234 tmp = DIV_ROUND_CLOSEST_ULL(tmp, parent_rate);
235 if (tmp > AT91_PMC_AUDIO_PLL_FRACR_MASK)
238 /* we can cast here as we verified the bounds just above */
239 *fracr = (unsigned long)tmp;
244 static int clk_audio_pll_frac_determine_rate(struct clk_hw *hw,
245 struct clk_rate_request *req)
247 unsigned long fracr, nd;
250 pr_debug("A PLL: %s, rate = %lu (parent_rate = %lu)\n", __func__,
251 req->rate, req->best_parent_rate);
253 req->rate = clamp(req->rate, AUDIO_PLL_FOUT_MIN, AUDIO_PLL_FOUT_MAX);
255 req->min_rate = max(req->min_rate, AUDIO_PLL_FOUT_MIN);
256 req->max_rate = min(req->max_rate, AUDIO_PLL_FOUT_MAX);
258 ret = clk_audio_pll_frac_compute_frac(req->rate, req->best_parent_rate,
263 req->rate = clk_audio_pll_fout(req->best_parent_rate, nd, fracr);
265 req->best_parent_hw = clk_hw_get_parent(hw);
267 pr_debug("A PLL: %s, best_rate = %lu (nd = %lu, fracr = %lu)\n",
268 __func__, req->rate, nd, fracr);
273 static long clk_audio_pll_pad_round_rate(struct clk_hw *hw, unsigned long rate,
274 unsigned long *parent_rate)
276 struct clk_hw *pclk = clk_hw_get_parent(hw);
277 long best_rate = -EINVAL;
278 unsigned long best_parent_rate;
279 unsigned long tmp_qd;
285 pr_debug("A PLL/PAD: %s, rate = %lu (parent_rate = %lu)\n", __func__,
289 * Rate divisor is actually made of two different divisors, multiplied
290 * between themselves before dividing the rate.
291 * tmp_qd goes from 1 to 31 and div is either 2 or 3.
292 * In order to avoid testing twice the rate divisor (e.g. divisor 12 can
293 * be found with (tmp_qd, div) = (2, 6) or (3, 4)), we remove any loop
294 * for a rate divisor when div is 2 and tmp_qd is a multiple of 3.
295 * We cannot inverse it (condition div is 3 and tmp_qd is even) or we
296 * would miss some rate divisor that aren't reachable with div being 2
297 * (e.g. rate divisor 90 is made with div = 3 and tmp_qd = 30, thus
298 * tmp_qd is even so we skip it because we think div 2 could make this
299 * rate divisor which isn't possible since tmp_qd has to be <= 31).
301 for (tmp_qd = 1; tmp_qd < AT91_PMC_AUDIO_PLL_QDPAD_EXTDIV_MAX; tmp_qd++)
302 for (div = 2; div <= 3; div++) {
303 if (div == 2 && tmp_qd % 3 == 0)
306 best_parent_rate = clk_hw_round_rate(pclk,
307 rate * tmp_qd * div);
308 tmp_rate = best_parent_rate / (div * tmp_qd);
309 tmp_diff = abs(rate - tmp_rate);
311 if (best_diff < 0 || best_diff > tmp_diff) {
312 *parent_rate = best_parent_rate;
313 best_rate = tmp_rate;
314 best_diff = tmp_diff;
318 pr_debug("A PLL/PAD: %s, best_rate = %ld, best_parent_rate = %lu\n",
319 __func__, best_rate, best_parent_rate);
324 static long clk_audio_pll_pmc_round_rate(struct clk_hw *hw, unsigned long rate,
325 unsigned long *parent_rate)
327 struct clk_hw *pclk = clk_hw_get_parent(hw);
328 long best_rate = -EINVAL;
329 unsigned long best_parent_rate = 0;
335 pr_debug("A PLL/PMC: %s, rate = %lu (parent_rate = %lu)\n", __func__,
341 best_parent_rate = clk_round_rate(pclk->clk, 1);
342 div = max(best_parent_rate / rate, 1UL);
343 for (; div <= AUDIO_PLL_QDPMC_MAX; div++) {
344 best_parent_rate = clk_round_rate(pclk->clk, rate * div);
345 tmp_rate = best_parent_rate / div;
346 tmp_diff = abs(rate - tmp_rate);
348 if (best_diff < 0 || best_diff > tmp_diff) {
349 *parent_rate = best_parent_rate;
350 best_rate = tmp_rate;
351 best_diff = tmp_diff;
354 break; /* got exact match */
358 pr_debug("A PLL/PMC: %s, best_rate = %ld, best_parent_rate = %lu (qd = %d)\n",
359 __func__, best_rate, *parent_rate, tmp_qd - 1);
364 static int clk_audio_pll_frac_set_rate(struct clk_hw *hw, unsigned long rate,
365 unsigned long parent_rate)
367 struct clk_audio_frac *frac = to_clk_audio_frac(hw);
368 unsigned long fracr, nd;
371 pr_debug("A PLL: %s, rate = %lu (parent_rate = %lu)\n", __func__, rate,
374 if (rate < AUDIO_PLL_FOUT_MIN || rate > AUDIO_PLL_FOUT_MAX)
377 ret = clk_audio_pll_frac_compute_frac(rate, parent_rate, &nd, &fracr);
387 static int clk_audio_pll_pad_set_rate(struct clk_hw *hw, unsigned long rate,
388 unsigned long parent_rate)
390 struct clk_audio_pad *apad_ck = to_clk_audio_pad(hw);
393 pr_debug("A PLL/PAD: %s, rate = %lu (parent_rate = %lu)\n", __func__,
399 tmp_div = parent_rate / rate;
400 if (tmp_div % 3 == 0) {
401 apad_ck->qdaudio = tmp_div / 3;
404 apad_ck->qdaudio = tmp_div / 2;
411 static int clk_audio_pll_pmc_set_rate(struct clk_hw *hw, unsigned long rate,
412 unsigned long parent_rate)
414 struct clk_audio_pmc *apmc_ck = to_clk_audio_pmc(hw);
419 pr_debug("A PLL/PMC: %s, rate = %lu (parent_rate = %lu)\n", __func__,
422 apmc_ck->qdpmc = parent_rate / rate - 1;
427 static const struct clk_ops audio_pll_frac_ops = {
428 .enable = clk_audio_pll_frac_enable,
429 .disable = clk_audio_pll_frac_disable,
430 .recalc_rate = clk_audio_pll_frac_recalc_rate,
431 .determine_rate = clk_audio_pll_frac_determine_rate,
432 .set_rate = clk_audio_pll_frac_set_rate,
435 static const struct clk_ops audio_pll_pad_ops = {
436 .enable = clk_audio_pll_pad_enable,
437 .disable = clk_audio_pll_pad_disable,
438 .recalc_rate = clk_audio_pll_pad_recalc_rate,
439 .round_rate = clk_audio_pll_pad_round_rate,
440 .set_rate = clk_audio_pll_pad_set_rate,
443 static const struct clk_ops audio_pll_pmc_ops = {
444 .enable = clk_audio_pll_pmc_enable,
445 .disable = clk_audio_pll_pmc_disable,
446 .recalc_rate = clk_audio_pll_pmc_recalc_rate,
447 .round_rate = clk_audio_pll_pmc_round_rate,
448 .set_rate = clk_audio_pll_pmc_set_rate,
451 struct clk_hw * __init
452 at91_clk_register_audio_pll_frac(struct regmap *regmap, const char *name,
453 const char *parent_name)
455 struct clk_audio_frac *frac_ck;
456 struct clk_init_data init = {};
459 frac_ck = kzalloc(sizeof(*frac_ck), GFP_KERNEL);
461 return ERR_PTR(-ENOMEM);
464 init.ops = &audio_pll_frac_ops;
465 init.parent_names = &parent_name;
466 init.num_parents = 1;
467 init.flags = CLK_SET_RATE_GATE;
469 frac_ck->hw.init = &init;
470 frac_ck->regmap = regmap;
472 ret = clk_hw_register(NULL, &frac_ck->hw);
481 struct clk_hw * __init
482 at91_clk_register_audio_pll_pad(struct regmap *regmap, const char *name,
483 const char *parent_name)
485 struct clk_audio_pad *apad_ck;
486 struct clk_init_data init;
489 apad_ck = kzalloc(sizeof(*apad_ck), GFP_KERNEL);
491 return ERR_PTR(-ENOMEM);
494 init.ops = &audio_pll_pad_ops;
495 init.parent_names = &parent_name;
496 init.num_parents = 1;
497 init.flags = CLK_SET_RATE_GATE | CLK_SET_PARENT_GATE |
500 apad_ck->hw.init = &init;
501 apad_ck->regmap = regmap;
503 ret = clk_hw_register(NULL, &apad_ck->hw);
512 struct clk_hw * __init
513 at91_clk_register_audio_pll_pmc(struct regmap *regmap, const char *name,
514 const char *parent_name)
516 struct clk_audio_pmc *apmc_ck;
517 struct clk_init_data init;
520 apmc_ck = kzalloc(sizeof(*apmc_ck), GFP_KERNEL);
522 return ERR_PTR(-ENOMEM);
525 init.ops = &audio_pll_pmc_ops;
526 init.parent_names = &parent_name;
527 init.num_parents = 1;
528 init.flags = CLK_SET_RATE_GATE | CLK_SET_PARENT_GATE |
531 apmc_ck->hw.init = &init;
532 apmc_ck->regmap = regmap;
534 ret = clk_hw_register(NULL, &apmc_ck->hw);