2 * Copyright (c) 2014 MundoReader S.L.
3 * Author: Heiko Stuebner <heiko@sntech.de>
5 * Copyright (c) 2015 Rockchip Electronics Co. Ltd.
6 * Author: Xing Zheng <zhengxing@rock-chips.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
19 #include <asm/div64.h>
20 #include <linux/slab.h>
22 #include <linux/delay.h>
23 #include <linux/clk-provider.h>
24 #include <linux/regmap.h>
25 #include <linux/clk.h>
26 #include <linux/gcd.h>
29 #define PLL_MODE_MASK 0x3
30 #define PLL_MODE_SLOW 0x0
31 #define PLL_MODE_NORM 0x1
32 #define PLL_MODE_DEEP 0x2
33 #define PLL_RK3328_MODE_MASK 0x1
35 struct rockchip_clk_pll {
38 struct clk_mux pll_mux;
39 const struct clk_ops *pll_mux_ops;
41 struct notifier_block clk_nb;
43 void __iomem *reg_base;
45 unsigned int lock_shift;
46 enum rockchip_pll_type type;
48 const struct rockchip_pll_rate_table *rate_table;
49 unsigned int rate_count;
52 struct rockchip_clk_provider *ctx;
55 #define to_rockchip_clk_pll(_hw) container_of(_hw, struct rockchip_clk_pll, hw)
56 #define to_rockchip_clk_pll_nb(nb) \
57 container_of(nb, struct rockchip_clk_pll, clk_nb)
59 static void rockchip_rk3366_pll_get_params(struct rockchip_clk_pll *pll,
60 struct rockchip_pll_rate_table *rate);
61 static int rockchip_rk3366_pll_set_params(struct rockchip_clk_pll *pll,
62 const struct rockchip_pll_rate_table *rate);
64 #define MHZ (1000UL * 1000UL)
67 /* CLK_PLL_TYPE_RK3066_AUTO type ops */
68 #define PLL_FREF_MIN (269 * KHZ)
69 #define PLL_FREF_MAX (2200 * MHZ)
71 #define PLL_FVCO_MIN (440 * MHZ)
72 #define PLL_FVCO_MAX (2200 * MHZ)
74 #define PLL_FOUT_MIN (27500 * KHZ)
75 #define PLL_FOUT_MAX (2200 * MHZ)
77 #define PLL_NF_MAX (4096)
78 #define PLL_NR_MAX (64)
79 #define PLL_NO_MAX (16)
81 /* CLK_PLL_TYPE_RK3036/3366/3399_AUTO type ops */
82 #define MIN_FOUTVCO_FREQ (800 * MHZ)
83 #define MAX_FOUTVCO_FREQ (2000 * MHZ)
85 static struct rockchip_pll_rate_table auto_table;
87 static struct rockchip_pll_rate_table *rk_pll_rate_table_get(void)
92 static int rockchip_pll_clk_set_postdiv(unsigned long fout_hz,
99 if (fout_hz < MIN_FOUTVCO_FREQ) {
100 for (*postdiv1 = 1; *postdiv1 <= 7; (*postdiv1)++) {
101 for (*postdiv2 = 1; *postdiv2 <= 7; (*postdiv2)++) {
102 freq = fout_hz * (*postdiv1) * (*postdiv2);
103 if (freq >= MIN_FOUTVCO_FREQ &&
104 freq <= MAX_FOUTVCO_FREQ) {
109 pr_err("CANNOT FIND postdiv1/2 to make fout in range from 800M to 2000M,fout = %lu\n",
119 static struct rockchip_pll_rate_table *
120 rockchip_pll_clk_set_by_auto(struct rockchip_clk_pll *pll,
121 unsigned long fin_hz,
122 unsigned long fout_hz)
124 struct rockchip_pll_rate_table *rate_table = rk_pll_rate_table_get();
125 /* FIXME set postdiv1/2 always 1*/
126 u32 foutvco = fout_hz;
128 u32 f_frac, postdiv1, postdiv2;
129 unsigned long clk_gcd = 0;
131 if (fin_hz == 0 || fout_hz == 0 || fout_hz == fin_hz)
134 rockchip_pll_clk_set_postdiv(fout_hz, &postdiv1, &postdiv2, &foutvco);
135 rate_table->postdiv1 = postdiv1;
136 rate_table->postdiv2 = postdiv2;
137 rate_table->dsmpd = 1;
139 if (fin_hz / MHZ * MHZ == fin_hz && fout_hz / MHZ * MHZ == fout_hz) {
142 clk_gcd = gcd(fin_hz, foutvco);
143 rate_table->refdiv = fin_hz / clk_gcd;
144 rate_table->fbdiv = foutvco / clk_gcd;
146 rate_table->frac = 0;
148 pr_debug("fin = %lu, fout = %lu, clk_gcd = %lu, refdiv = %u, fbdiv = %u, postdiv1 = %u, postdiv2 = %u, frac = %u\n",
149 fin_hz, fout_hz, clk_gcd, rate_table->refdiv,
150 rate_table->fbdiv, rate_table->postdiv1,
151 rate_table->postdiv2, rate_table->frac);
153 pr_debug("frac div running, fin_hz = %lu, fout_hz = %lu, fin_INT_mhz = %lu, fout_INT_mhz = %lu\n",
156 fout_hz / MHZ * MHZ);
157 pr_debug("frac get postdiv1 = %u, postdiv2 = %u, foutvco = %u\n",
158 rate_table->postdiv1, rate_table->postdiv2, foutvco);
159 clk_gcd = gcd(fin_hz / MHZ, foutvco / MHZ);
160 rate_table->refdiv = fin_hz / MHZ / clk_gcd;
161 rate_table->fbdiv = foutvco / MHZ / clk_gcd;
162 pr_debug("frac get refdiv = %u, fbdiv = %u\n",
163 rate_table->refdiv, rate_table->fbdiv);
165 rate_table->frac = 0;
167 f_frac = (foutvco % MHZ);
169 do_div(fin_64, (u64)rate_table->refdiv);
170 frac_64 = (u64)f_frac << 24;
171 do_div(frac_64, fin_64);
172 rate_table->frac = (u32)frac_64;
173 if (rate_table->frac > 0)
174 rate_table->dsmpd = 0;
175 pr_debug("frac = %x\n", rate_table->frac);
180 static struct rockchip_pll_rate_table *
181 rockchip_rk3066_pll_clk_set_by_auto(struct rockchip_clk_pll *pll,
182 unsigned long fin_hz,
183 unsigned long fout_hz)
185 struct rockchip_pll_rate_table *rate_table = rk_pll_rate_table_get();
186 u32 nr, nf, no, nonr;
187 u32 nr_out, nf_out, no_out;
189 u32 numerator, denominator;
190 u64 fref, fvco, fout;
191 unsigned long clk_gcd = 0;
193 nr_out = PLL_NR_MAX + 1;
197 if (fin_hz == 0 || fout_hz == 0 || fout_hz == fin_hz)
200 clk_gcd = gcd(fin_hz, fout_hz);
202 numerator = fout_hz / clk_gcd;
203 denominator = fin_hz / clk_gcd;
207 nonr = denominator * n;
208 if (nf > PLL_NF_MAX || nonr > (PLL_NO_MAX * PLL_NR_MAX))
211 for (no = 1; no <= PLL_NO_MAX; no++) {
212 if (!(no == 1 || !(no % 2)))
223 if (fref < PLL_FREF_MIN || fref > PLL_FREF_MAX)
227 if (fvco < PLL_FVCO_MIN || fvco > PLL_FVCO_MAX)
231 if (fout < PLL_FOUT_MIN || fout > PLL_FOUT_MAX)
234 /* select the best from all available PLL settings */
236 ((no == no_out) && (nr < nr_out))) {
244 /* output the best PLL setting */
245 if ((nr_out <= PLL_NR_MAX) && (no_out > 0)) {
246 if (rate_table->nr && rate_table->nf && rate_table->no) {
247 rate_table->nr = nr_out;
248 rate_table->nf = nf_out;
249 rate_table->no = no_out;
258 static const struct rockchip_pll_rate_table *rockchip_get_pll_settings(
259 struct rockchip_clk_pll *pll, unsigned long rate)
261 const struct rockchip_pll_rate_table *rate_table = pll->rate_table;
264 for (i = 0; i < pll->rate_count; i++) {
265 if (rate == rate_table[i].rate)
266 return &rate_table[i];
269 if (pll->type == pll_rk3066 || pll->type == pll_rk3328)
270 return rockchip_rk3066_pll_clk_set_by_auto(pll, 24 * MHZ, rate);
272 return rockchip_pll_clk_set_by_auto(pll, 24 * MHZ, rate);
275 static long rockchip_pll_round_rate(struct clk_hw *hw,
276 unsigned long drate, unsigned long *prate)
282 * Wait for the pll to reach the locked state.
283 * The calling set_rate function is responsible for making sure the
284 * grf regmap is available.
286 static int rockchip_pll_wait_lock(struct rockchip_clk_pll *pll)
288 struct regmap *grf = rockchip_clk_get_grf(pll->ctx);
290 int delay = 24000000, ret;
293 ret = regmap_read(grf, pll->lock_offset, &val);
295 pr_err("%s: failed to read pll lock status: %d\n",
300 if (val & BIT(pll->lock_shift))
305 pr_err("%s: timeout waiting for pll to lock\n", __func__);
313 #define RK3036_PLLCON(i) (i * 0x4)
314 #define RK3036_PLLCON0_FBDIV_MASK 0xfff
315 #define RK3036_PLLCON0_FBDIV_SHIFT 0
316 #define RK3036_PLLCON0_POSTDIV1_MASK 0x7
317 #define RK3036_PLLCON0_POSTDIV1_SHIFT 12
318 #define RK3036_PLLCON1_REFDIV_MASK 0x3f
319 #define RK3036_PLLCON1_REFDIV_SHIFT 0
320 #define RK3036_PLLCON1_POSTDIV2_MASK 0x7
321 #define RK3036_PLLCON1_POSTDIV2_SHIFT 6
322 #define RK3036_PLLCON1_DSMPD_MASK 0x1
323 #define RK3036_PLLCON1_DSMPD_SHIFT 12
324 #define RK3036_PLLCON2_FRAC_MASK 0xffffff
325 #define RK3036_PLLCON2_FRAC_SHIFT 0
327 #define RK3036_PLLCON1_PWRDOWN (1 << 13)
329 static void rockchip_rk3036_pll_get_params(struct rockchip_clk_pll *pll,
330 struct rockchip_pll_rate_table *rate)
334 pllcon = readl_relaxed(pll->reg_base + RK3036_PLLCON(0));
335 rate->fbdiv = ((pllcon >> RK3036_PLLCON0_FBDIV_SHIFT)
336 & RK3036_PLLCON0_FBDIV_MASK);
337 rate->postdiv1 = ((pllcon >> RK3036_PLLCON0_POSTDIV1_SHIFT)
338 & RK3036_PLLCON0_POSTDIV1_MASK);
340 pllcon = readl_relaxed(pll->reg_base + RK3036_PLLCON(1));
341 rate->refdiv = ((pllcon >> RK3036_PLLCON1_REFDIV_SHIFT)
342 & RK3036_PLLCON1_REFDIV_MASK);
343 rate->postdiv2 = ((pllcon >> RK3036_PLLCON1_POSTDIV2_SHIFT)
344 & RK3036_PLLCON1_POSTDIV2_MASK);
345 rate->dsmpd = ((pllcon >> RK3036_PLLCON1_DSMPD_SHIFT)
346 & RK3036_PLLCON1_DSMPD_MASK);
348 pllcon = readl_relaxed(pll->reg_base + RK3036_PLLCON(2));
349 rate->frac = ((pllcon >> RK3036_PLLCON2_FRAC_SHIFT)
350 & RK3036_PLLCON2_FRAC_MASK);
353 static unsigned long rockchip_rk3036_pll_recalc_rate(struct clk_hw *hw,
356 struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
357 struct rockchip_pll_rate_table cur;
360 if (pll->type == pll_rk3366)
361 rockchip_rk3366_pll_get_params(pll, &cur);
363 rockchip_rk3036_pll_get_params(pll, &cur);
366 do_div(rate64, cur.refdiv);
368 if (cur.dsmpd == 0) {
369 /* fractional mode */
370 u64 frac_rate64 = prate * cur.frac;
372 do_div(frac_rate64, cur.refdiv);
373 rate64 += frac_rate64 >> 24;
376 do_div(rate64, cur.postdiv1);
377 do_div(rate64, cur.postdiv2);
379 return (unsigned long)rate64;
382 static int rockchip_rk3036_pll_set_params(struct rockchip_clk_pll *pll,
383 const struct rockchip_pll_rate_table *rate)
385 const struct clk_ops *pll_mux_ops = pll->pll_mux_ops;
386 struct clk_mux *pll_mux = &pll->pll_mux;
387 struct rockchip_pll_rate_table cur;
389 int rate_change_remuxed = 0;
393 pr_debug("%s: rate settings for %lu fbdiv: %d, postdiv1: %d, refdiv: %d, postdiv2: %d, dsmpd: %d, frac: %d\n",
394 __func__, rate->rate, rate->fbdiv, rate->postdiv1, rate->refdiv,
395 rate->postdiv2, rate->dsmpd, rate->frac);
397 rockchip_rk3036_pll_get_params(pll, &cur);
400 cur_parent = pll_mux_ops->get_parent(&pll_mux->hw);
401 if (cur_parent == PLL_MODE_NORM) {
402 pll_mux_ops->set_parent(&pll_mux->hw, PLL_MODE_SLOW);
403 rate_change_remuxed = 1;
406 /* update pll values */
407 writel_relaxed(HIWORD_UPDATE(rate->fbdiv, RK3036_PLLCON0_FBDIV_MASK,
408 RK3036_PLLCON0_FBDIV_SHIFT) |
409 HIWORD_UPDATE(rate->postdiv1, RK3036_PLLCON0_POSTDIV1_MASK,
410 RK3036_PLLCON0_POSTDIV1_SHIFT),
411 pll->reg_base + RK3036_PLLCON(0));
413 writel_relaxed(HIWORD_UPDATE(rate->refdiv, RK3036_PLLCON1_REFDIV_MASK,
414 RK3036_PLLCON1_REFDIV_SHIFT) |
415 HIWORD_UPDATE(rate->postdiv2, RK3036_PLLCON1_POSTDIV2_MASK,
416 RK3036_PLLCON1_POSTDIV2_SHIFT) |
417 HIWORD_UPDATE(rate->dsmpd, RK3036_PLLCON1_DSMPD_MASK,
418 RK3036_PLLCON1_DSMPD_SHIFT),
419 pll->reg_base + RK3036_PLLCON(1));
421 /* GPLL CON2 is not HIWORD_MASK */
422 pllcon = readl_relaxed(pll->reg_base + RK3036_PLLCON(2));
423 pllcon &= ~(RK3036_PLLCON2_FRAC_MASK << RK3036_PLLCON2_FRAC_SHIFT);
424 pllcon |= rate->frac << RK3036_PLLCON2_FRAC_SHIFT;
425 writel_relaxed(pllcon, pll->reg_base + RK3036_PLLCON(2));
427 /* wait for the pll to lock */
428 ret = rockchip_pll_wait_lock(pll);
430 pr_warn("%s: pll update unsucessful, trying to restore old params\n",
432 rockchip_rk3036_pll_set_params(pll, &cur);
435 if (rate_change_remuxed)
436 pll_mux_ops->set_parent(&pll_mux->hw, PLL_MODE_NORM);
441 static int rockchip_rk3036_pll_set_rate(struct clk_hw *hw, unsigned long drate,
444 struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
445 const struct rockchip_pll_rate_table *rate;
446 unsigned long old_rate = rockchip_rk3036_pll_recalc_rate(hw, prate);
447 struct regmap *grf = rockchip_clk_get_grf(pll->ctx);
450 pr_debug("%s: grf regmap not available, aborting rate change\n",
455 pr_debug("%s: changing %s from %lu to %lu with a parent rate of %lu\n",
456 __func__, __clk_get_name(hw->clk), old_rate, drate, prate);
458 /* Get required rate settings from table */
459 rate = rockchip_get_pll_settings(pll, drate);
461 pr_err("%s: Invalid rate : %lu for pll clk %s\n", __func__,
462 drate, __clk_get_name(hw->clk));
466 if (pll->type == pll_rk3366)
467 return rockchip_rk3366_pll_set_params(pll, rate);
469 return rockchip_rk3036_pll_set_params(pll, rate);
472 static int rockchip_rk3036_pll_enable(struct clk_hw *hw)
474 struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
476 writel(HIWORD_UPDATE(0, RK3036_PLLCON1_PWRDOWN, 0),
477 pll->reg_base + RK3036_PLLCON(1));
482 static void rockchip_rk3036_pll_disable(struct clk_hw *hw)
484 struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
486 writel(HIWORD_UPDATE(RK3036_PLLCON1_PWRDOWN,
487 RK3036_PLLCON1_PWRDOWN, 0),
488 pll->reg_base + RK3036_PLLCON(1));
491 static int rockchip_rk3036_pll_is_enabled(struct clk_hw *hw)
493 struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
494 u32 pllcon = readl(pll->reg_base + RK3036_PLLCON(1));
496 return !(pllcon & RK3036_PLLCON1_PWRDOWN);
499 static void rockchip_rk3036_pll_init(struct clk_hw *hw)
501 struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
502 const struct rockchip_pll_rate_table *rate;
503 struct rockchip_pll_rate_table cur;
506 if (!(pll->flags & ROCKCHIP_PLL_SYNC_RATE))
509 drate = clk_hw_get_rate(hw);
510 rate = rockchip_get_pll_settings(pll, drate);
512 /* when no rate setting for the current rate, rely on clk_set_rate */
516 if (pll->type == pll_rk3366)
517 rockchip_rk3366_pll_get_params(pll, &cur);
519 rockchip_rk3036_pll_get_params(pll, &cur);
521 pr_debug("%s: pll %s@%lu: Hz\n", __func__, __clk_get_name(hw->clk),
523 pr_debug("old - fbdiv: %d, postdiv1: %d, refdiv: %d, postdiv2: %d, dsmpd: %d, frac: %d\n",
524 cur.fbdiv, cur.postdiv1, cur.refdiv, cur.postdiv2,
525 cur.dsmpd, cur.frac);
526 pr_debug("new - fbdiv: %d, postdiv1: %d, refdiv: %d, postdiv2: %d, dsmpd: %d, frac: %d\n",
527 rate->fbdiv, rate->postdiv1, rate->refdiv, rate->postdiv2,
528 rate->dsmpd, rate->frac);
530 if (rate->fbdiv != cur.fbdiv || rate->postdiv1 != cur.postdiv1 ||
531 rate->refdiv != cur.refdiv || rate->postdiv2 != cur.postdiv2 ||
532 rate->dsmpd != cur.dsmpd || rate->frac != cur.frac) {
533 struct clk *parent = clk_get_parent(hw->clk);
536 pr_warn("%s: parent of %s not available\n",
537 __func__, __clk_get_name(hw->clk));
541 pr_debug("%s: pll %s: rate params do not match rate table, adjusting\n",
542 __func__, __clk_get_name(hw->clk));
543 if (pll->type == pll_rk3366)
544 rockchip_rk3366_pll_set_params(pll, rate);
546 rockchip_rk3036_pll_set_params(pll, rate);
550 static const struct clk_ops rockchip_rk3036_pll_clk_norate_ops = {
551 .recalc_rate = rockchip_rk3036_pll_recalc_rate,
552 .enable = rockchip_rk3036_pll_enable,
553 .disable = rockchip_rk3036_pll_disable,
554 .is_enabled = rockchip_rk3036_pll_is_enabled,
557 static const struct clk_ops rockchip_rk3036_pll_clk_ops = {
558 .recalc_rate = rockchip_rk3036_pll_recalc_rate,
559 .round_rate = rockchip_pll_round_rate,
560 .set_rate = rockchip_rk3036_pll_set_rate,
561 .enable = rockchip_rk3036_pll_enable,
562 .disable = rockchip_rk3036_pll_disable,
563 .is_enabled = rockchip_rk3036_pll_is_enabled,
564 .init = rockchip_rk3036_pll_init,
568 * PLL used in RK3066, RK3188 and RK3288
571 #define RK3066_PLL_RESET_DELAY(nr) ((nr * 500) / 24 + 1)
573 #define RK3066_PLLCON(i) (i * 0x4)
574 #define RK3066_PLLCON0_OD_MASK 0xf
575 #define RK3066_PLLCON0_OD_SHIFT 0
576 #define RK3066_PLLCON0_NR_MASK 0x3f
577 #define RK3066_PLLCON0_NR_SHIFT 8
578 #define RK3066_PLLCON1_NF_MASK 0x1fff
579 #define RK3066_PLLCON1_NF_SHIFT 0
580 #define RK3066_PLLCON2_NB_MASK 0xfff
581 #define RK3066_PLLCON2_NB_SHIFT 0
582 #define RK3066_PLLCON3_RESET (1 << 5)
583 #define RK3066_PLLCON3_PWRDOWN (1 << 1)
584 #define RK3066_PLLCON3_BYPASS (1 << 0)
586 static void rockchip_rk3066_pll_get_params(struct rockchip_clk_pll *pll,
587 struct rockchip_pll_rate_table *rate)
591 pllcon = readl_relaxed(pll->reg_base + RK3066_PLLCON(0));
592 rate->nr = ((pllcon >> RK3066_PLLCON0_NR_SHIFT)
593 & RK3066_PLLCON0_NR_MASK) + 1;
594 rate->no = ((pllcon >> RK3066_PLLCON0_OD_SHIFT)
595 & RK3066_PLLCON0_OD_MASK) + 1;
597 pllcon = readl_relaxed(pll->reg_base + RK3066_PLLCON(1));
598 rate->nf = ((pllcon >> RK3066_PLLCON1_NF_SHIFT)
599 & RK3066_PLLCON1_NF_MASK) + 1;
601 pllcon = readl_relaxed(pll->reg_base + RK3066_PLLCON(2));
602 rate->nb = ((pllcon >> RK3066_PLLCON2_NB_SHIFT)
603 & RK3066_PLLCON2_NB_MASK) + 1;
606 static unsigned long rockchip_rk3066_pll_recalc_rate(struct clk_hw *hw,
609 struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
610 struct rockchip_pll_rate_table cur;
614 pllcon = readl_relaxed(pll->reg_base + RK3066_PLLCON(3));
615 if (pllcon & RK3066_PLLCON3_BYPASS) {
616 pr_debug("%s: pll %s is bypassed\n", __func__,
617 clk_hw_get_name(hw));
621 rockchip_rk3066_pll_get_params(pll, &cur);
624 do_div(rate64, cur.nr);
625 do_div(rate64, cur.no);
627 return (unsigned long)rate64;
630 static int rockchip_rk3066_pll_set_params(struct rockchip_clk_pll *pll,
631 const struct rockchip_pll_rate_table *rate)
633 const struct clk_ops *pll_mux_ops = pll->pll_mux_ops;
634 struct clk_mux *pll_mux = &pll->pll_mux;
635 struct rockchip_pll_rate_table cur;
636 int rate_change_remuxed = 0;
640 pr_debug("%s: rate settings for %lu (nr, no, nf): (%d, %d, %d)\n",
641 __func__, rate->rate, rate->nr, rate->no, rate->nf);
643 rockchip_rk3066_pll_get_params(pll, &cur);
646 cur_parent = pll_mux_ops->get_parent(&pll_mux->hw);
647 if (cur_parent == PLL_MODE_NORM) {
648 pll_mux_ops->set_parent(&pll_mux->hw, PLL_MODE_SLOW);
649 rate_change_remuxed = 1;
652 /* enter reset mode */
653 writel(HIWORD_UPDATE(RK3066_PLLCON3_RESET, RK3066_PLLCON3_RESET, 0),
654 pll->reg_base + RK3066_PLLCON(3));
656 /* update pll values */
657 writel(HIWORD_UPDATE(rate->nr - 1, RK3066_PLLCON0_NR_MASK,
658 RK3066_PLLCON0_NR_SHIFT) |
659 HIWORD_UPDATE(rate->no - 1, RK3066_PLLCON0_OD_MASK,
660 RK3066_PLLCON0_OD_SHIFT),
661 pll->reg_base + RK3066_PLLCON(0));
663 writel_relaxed(HIWORD_UPDATE(rate->nf - 1, RK3066_PLLCON1_NF_MASK,
664 RK3066_PLLCON1_NF_SHIFT),
665 pll->reg_base + RK3066_PLLCON(1));
666 writel_relaxed(HIWORD_UPDATE(rate->nb - 1, RK3066_PLLCON2_NB_MASK,
667 RK3066_PLLCON2_NB_SHIFT),
668 pll->reg_base + RK3066_PLLCON(2));
670 /* leave reset and wait the reset_delay */
671 writel(HIWORD_UPDATE(0, RK3066_PLLCON3_RESET, 0),
672 pll->reg_base + RK3066_PLLCON(3));
673 udelay(RK3066_PLL_RESET_DELAY(rate->nr));
675 /* wait for the pll to lock */
676 ret = rockchip_pll_wait_lock(pll);
678 pr_warn("%s: pll update unsucessful, trying to restore old params\n",
680 rockchip_rk3066_pll_set_params(pll, &cur);
683 if (rate_change_remuxed)
684 pll_mux_ops->set_parent(&pll_mux->hw, PLL_MODE_NORM);
689 static int rockchip_rk3066_pll_set_rate(struct clk_hw *hw, unsigned long drate,
692 struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
693 const struct rockchip_pll_rate_table *rate;
694 unsigned long old_rate = rockchip_rk3066_pll_recalc_rate(hw, prate);
695 struct regmap *grf = rockchip_clk_get_grf(pll->ctx);
698 pr_debug("%s: grf regmap not available, aborting rate change\n",
703 pr_debug("%s: changing %s from %lu to %lu with a parent rate of %lu\n",
704 __func__, clk_hw_get_name(hw), old_rate, drate, prate);
706 /* Get required rate settings from table */
707 rate = rockchip_get_pll_settings(pll, drate);
709 pr_err("%s: Invalid rate : %lu for pll clk %s\n", __func__,
710 drate, clk_hw_get_name(hw));
714 return rockchip_rk3066_pll_set_params(pll, rate);
717 static int rockchip_rk3066_pll_enable(struct clk_hw *hw)
719 struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
721 writel(HIWORD_UPDATE(0, RK3066_PLLCON3_PWRDOWN, 0),
722 pll->reg_base + RK3066_PLLCON(3));
727 static void rockchip_rk3066_pll_disable(struct clk_hw *hw)
729 struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
731 writel(HIWORD_UPDATE(RK3066_PLLCON3_PWRDOWN,
732 RK3066_PLLCON3_PWRDOWN, 0),
733 pll->reg_base + RK3066_PLLCON(3));
736 static int rockchip_rk3066_pll_is_enabled(struct clk_hw *hw)
738 struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
739 u32 pllcon = readl(pll->reg_base + RK3066_PLLCON(3));
741 return !(pllcon & RK3066_PLLCON3_PWRDOWN);
744 static void rockchip_rk3066_pll_init(struct clk_hw *hw)
746 struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
747 const struct rockchip_pll_rate_table *rate;
748 struct rockchip_pll_rate_table cur;
751 if (!(pll->flags & ROCKCHIP_PLL_SYNC_RATE))
754 drate = clk_hw_get_rate(hw);
755 rate = rockchip_get_pll_settings(pll, drate);
757 /* when no rate setting for the current rate, rely on clk_set_rate */
761 rockchip_rk3066_pll_get_params(pll, &cur);
763 pr_debug("%s: pll %s@%lu: nr (%d:%d); no (%d:%d); nf(%d:%d), nb(%d:%d)\n",
764 __func__, clk_hw_get_name(hw), drate, rate->nr, cur.nr,
765 rate->no, cur.no, rate->nf, cur.nf, rate->nb, cur.nb);
766 if (rate->nr != cur.nr || rate->no != cur.no || rate->nf != cur.nf
767 || rate->nb != cur.nb) {
768 struct regmap *grf = rockchip_clk_get_grf(pll->ctx);
773 pr_debug("%s: pll %s: rate params do not match rate table, adjusting\n",
774 __func__, clk_hw_get_name(hw));
775 rockchip_rk3066_pll_set_params(pll, rate);
779 static const struct clk_ops rockchip_rk3066_pll_clk_norate_ops = {
780 .recalc_rate = rockchip_rk3066_pll_recalc_rate,
781 .enable = rockchip_rk3066_pll_enable,
782 .disable = rockchip_rk3066_pll_disable,
783 .is_enabled = rockchip_rk3066_pll_is_enabled,
786 static const struct clk_ops rockchip_rk3066_pll_clk_ops = {
787 .recalc_rate = rockchip_rk3066_pll_recalc_rate,
788 .round_rate = rockchip_pll_round_rate,
789 .set_rate = rockchip_rk3066_pll_set_rate,
790 .enable = rockchip_rk3066_pll_enable,
791 .disable = rockchip_rk3066_pll_disable,
792 .is_enabled = rockchip_rk3066_pll_is_enabled,
793 .init = rockchip_rk3066_pll_init,
800 #define RK3366_PLLCON(i) (i * 0x4)
801 #define RK3366_PLLCON0_FBDIV_MASK 0xfff
802 #define RK3366_PLLCON0_FBDIV_SHIFT 0
803 #define RK3366_PLLCON0_POSTDIV1_MASK 0x7
804 #define RK3366_PLLCON0_POSTDIV1_SHIFT 12
805 #define RK3366_PLLCON1_REFDIV_MASK 0x3f
806 #define RK3366_PLLCON1_REFDIV_SHIFT 0
807 #define RK3366_PLLCON1_POSTDIV2_MASK 0x7
808 #define RK3366_PLLCON1_POSTDIV2_SHIFT 6
809 #define RK3366_PLLCON2_FRAC_MASK 0xffffff
810 #define RK3366_PLLCON2_FRAC_SHIFT 0
811 #define RK3366_PLLCON3_DSMPD_MASK 0x1
812 #define RK3366_PLLCON3_DSMPD_SHIFT 2
814 #define RK3366_PLLCON3_PWRDOWN (1 << 0)
816 static void rockchip_rk3366_pll_get_params(struct rockchip_clk_pll *pll,
817 struct rockchip_pll_rate_table *rate)
821 pllcon = readl_relaxed(pll->reg_base + RK3366_PLLCON(0));
822 rate->fbdiv = ((pllcon >> RK3366_PLLCON0_FBDIV_SHIFT)
823 & RK3366_PLLCON0_FBDIV_MASK);
824 rate->postdiv1 = ((pllcon >> RK3366_PLLCON0_POSTDIV1_SHIFT)
825 & RK3366_PLLCON0_POSTDIV1_MASK);
827 pllcon = readl_relaxed(pll->reg_base + RK3366_PLLCON(1));
828 rate->refdiv = ((pllcon >> RK3366_PLLCON1_REFDIV_SHIFT)
829 & RK3366_PLLCON1_REFDIV_MASK);
830 rate->postdiv2 = ((pllcon >> RK3366_PLLCON1_POSTDIV2_SHIFT)
831 & RK3366_PLLCON1_POSTDIV2_MASK);
833 pllcon = readl_relaxed(pll->reg_base + RK3366_PLLCON(2));
834 rate->frac = ((pllcon >> RK3366_PLLCON2_FRAC_SHIFT)
835 & RK3366_PLLCON2_FRAC_MASK);
837 pllcon = readl_relaxed(pll->reg_base + RK3366_PLLCON(3));
838 rate->dsmpd = ((pllcon >> RK3366_PLLCON3_DSMPD_SHIFT)
839 & RK3366_PLLCON3_DSMPD_MASK);
842 static int rockchip_rk3366_pll_set_params(struct rockchip_clk_pll *pll,
843 const struct rockchip_pll_rate_table *rate)
845 const struct clk_ops *pll_mux_ops = pll->pll_mux_ops;
846 struct clk_mux *pll_mux = &pll->pll_mux;
847 struct rockchip_pll_rate_table cur;
849 int rate_change_remuxed = 0;
853 pr_debug("%s: rate settings for %lu fbdiv: %d, postdiv1: %d, refdiv: %d, postdiv2: %d, dsmpd: %d, frac: %d\n",
854 __func__, rate->rate, rate->fbdiv, rate->postdiv1, rate->refdiv,
855 rate->postdiv2, rate->dsmpd, rate->frac);
857 rockchip_rk3366_pll_get_params(pll, &cur);
860 cur_parent = pll_mux_ops->get_parent(&pll_mux->hw);
861 if (cur_parent == PLL_MODE_NORM) {
862 pll_mux_ops->set_parent(&pll_mux->hw, PLL_MODE_SLOW);
863 rate_change_remuxed = 1;
866 /* update pll values */
867 writel_relaxed(HIWORD_UPDATE(rate->fbdiv, RK3366_PLLCON0_FBDIV_MASK,
868 RK3366_PLLCON0_FBDIV_SHIFT) |
869 HIWORD_UPDATE(rate->postdiv1, RK3366_PLLCON0_POSTDIV1_MASK,
870 RK3366_PLLCON0_POSTDIV1_SHIFT),
871 pll->reg_base + RK3366_PLLCON(0));
873 writel_relaxed(HIWORD_UPDATE(rate->refdiv, RK3366_PLLCON1_REFDIV_MASK,
874 RK3366_PLLCON1_REFDIV_SHIFT) |
875 HIWORD_UPDATE(rate->postdiv2, RK3366_PLLCON1_POSTDIV2_MASK,
876 RK3366_PLLCON1_POSTDIV2_SHIFT),
877 pll->reg_base + RK3366_PLLCON(1));
879 /* GPLL CON2 is not HIWORD_MASK */
880 pllcon = readl_relaxed(pll->reg_base + RK3366_PLLCON(2));
881 pllcon &= ~(RK3366_PLLCON2_FRAC_MASK << RK3366_PLLCON2_FRAC_SHIFT);
882 pllcon |= rate->frac << RK3366_PLLCON2_FRAC_SHIFT;
883 writel_relaxed(pllcon, pll->reg_base + RK3366_PLLCON(2));
885 writel_relaxed(HIWORD_UPDATE(rate->dsmpd, RK3366_PLLCON3_DSMPD_MASK,
886 RK3366_PLLCON3_DSMPD_SHIFT),
887 pll->reg_base + RK3366_PLLCON(3));
889 /* wait for the pll to lock */
890 ret = rockchip_pll_wait_lock(pll);
892 pr_warn("%s: pll update unsucessful, trying to restore old params\n",
894 rockchip_rk3366_pll_set_params(pll, &cur);
897 if (rate_change_remuxed)
898 pll_mux_ops->set_parent(&pll_mux->hw, PLL_MODE_NORM);
903 static int rockchip_rk3366_pll_enable(struct clk_hw *hw)
905 struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
907 writel(HIWORD_UPDATE(0, RK3366_PLLCON3_PWRDOWN, 0),
908 pll->reg_base + RK3366_PLLCON(3));
913 static void rockchip_rk3366_pll_disable(struct clk_hw *hw)
915 struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
917 writel(HIWORD_UPDATE(RK3366_PLLCON3_PWRDOWN,
918 RK3366_PLLCON3_PWRDOWN, 0),
919 pll->reg_base + RK3366_PLLCON(3));
922 static int rockchip_rk3366_pll_is_enabled(struct clk_hw *hw)
924 struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
925 u32 pllcon = readl(pll->reg_base + RK3366_PLLCON(3));
927 return !(pllcon & RK3366_PLLCON3_PWRDOWN);
930 static const struct clk_ops rockchip_rk3366_pll_clk_norate_ops = {
931 .recalc_rate = rockchip_rk3036_pll_recalc_rate,
932 .enable = rockchip_rk3366_pll_enable,
933 .disable = rockchip_rk3366_pll_disable,
934 .is_enabled = rockchip_rk3366_pll_is_enabled,
937 static const struct clk_ops rockchip_rk3366_pll_clk_ops = {
938 .recalc_rate = rockchip_rk3036_pll_recalc_rate,
939 .round_rate = rockchip_pll_round_rate,
940 .set_rate = rockchip_rk3036_pll_set_rate,
941 .enable = rockchip_rk3366_pll_enable,
942 .disable = rockchip_rk3366_pll_disable,
943 .is_enabled = rockchip_rk3366_pll_is_enabled,
944 .init = rockchip_rk3036_pll_init,
951 #define RK3399_PLLCON(i) (i * 0x4)
952 #define RK3399_PLLCON0_FBDIV_MASK 0xfff
953 #define RK3399_PLLCON0_FBDIV_SHIFT 0
954 #define RK3399_PLLCON1_REFDIV_MASK 0x3f
955 #define RK3399_PLLCON1_REFDIV_SHIFT 0
956 #define RK3399_PLLCON1_POSTDIV1_MASK 0x7
957 #define RK3399_PLLCON1_POSTDIV1_SHIFT 8
958 #define RK3399_PLLCON1_POSTDIV2_MASK 0x7
959 #define RK3399_PLLCON1_POSTDIV2_SHIFT 12
960 #define RK3399_PLLCON2_FRAC_MASK 0xffffff
961 #define RK3399_PLLCON2_FRAC_SHIFT 0
962 #define RK3399_PLLCON2_LOCK_STATUS BIT(31)
963 #define RK3399_PLLCON3_PWRDOWN BIT(0)
964 #define RK3399_PLLCON3_DSMPD_MASK 0x1
965 #define RK3399_PLLCON3_DSMPD_SHIFT 3
967 static int rockchip_rk3399_pll_wait_lock(struct rockchip_clk_pll *pll)
970 int delay = 24000000;
972 /* poll check the lock status in rk3399 xPLLCON2 */
974 pllcon = readl_relaxed(pll->reg_base + RK3399_PLLCON(2));
975 if (pllcon & RK3399_PLLCON2_LOCK_STATUS)
981 pr_err("%s: timeout waiting for pll to lock\n", __func__);
985 static void rockchip_rk3399_pll_get_params(struct rockchip_clk_pll *pll,
986 struct rockchip_pll_rate_table *rate)
990 pllcon = readl_relaxed(pll->reg_base + RK3399_PLLCON(0));
991 rate->fbdiv = ((pllcon >> RK3399_PLLCON0_FBDIV_SHIFT)
992 & RK3399_PLLCON0_FBDIV_MASK);
994 pllcon = readl_relaxed(pll->reg_base + RK3399_PLLCON(1));
995 rate->refdiv = ((pllcon >> RK3399_PLLCON1_REFDIV_SHIFT)
996 & RK3399_PLLCON1_REFDIV_MASK);
997 rate->postdiv1 = ((pllcon >> RK3399_PLLCON1_POSTDIV1_SHIFT)
998 & RK3399_PLLCON1_POSTDIV1_MASK);
999 rate->postdiv2 = ((pllcon >> RK3399_PLLCON1_POSTDIV2_SHIFT)
1000 & RK3399_PLLCON1_POSTDIV2_MASK);
1002 pllcon = readl_relaxed(pll->reg_base + RK3399_PLLCON(2));
1003 rate->frac = ((pllcon >> RK3399_PLLCON2_FRAC_SHIFT)
1004 & RK3399_PLLCON2_FRAC_MASK);
1006 pllcon = readl_relaxed(pll->reg_base + RK3399_PLLCON(3));
1007 rate->dsmpd = ((pllcon >> RK3399_PLLCON3_DSMPD_SHIFT)
1008 & RK3399_PLLCON3_DSMPD_MASK);
1011 static unsigned long rockchip_rk3399_pll_recalc_rate(struct clk_hw *hw,
1012 unsigned long prate)
1014 struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
1015 struct rockchip_pll_rate_table cur;
1018 rockchip_rk3399_pll_get_params(pll, &cur);
1020 rate64 *= cur.fbdiv;
1021 do_div(rate64, cur.refdiv);
1023 if (cur.dsmpd == 0) {
1024 /* fractional mode */
1025 u64 frac_rate64 = prate * cur.frac;
1027 do_div(frac_rate64, cur.refdiv);
1028 rate64 += frac_rate64 >> 24;
1031 do_div(rate64, cur.postdiv1);
1032 do_div(rate64, cur.postdiv2);
1034 return (unsigned long)rate64;
1037 static int rockchip_rk3399_pll_set_params(struct rockchip_clk_pll *pll,
1038 const struct rockchip_pll_rate_table *rate)
1040 const struct clk_ops *pll_mux_ops = pll->pll_mux_ops;
1041 struct clk_mux *pll_mux = &pll->pll_mux;
1042 struct rockchip_pll_rate_table cur;
1044 int rate_change_remuxed = 0;
1048 pr_debug("%s: rate settings for %lu fbdiv: %d, postdiv1: %d, refdiv: %d, postdiv2: %d, dsmpd: %d, frac: %d\n",
1049 __func__, rate->rate, rate->fbdiv, rate->postdiv1, rate->refdiv,
1050 rate->postdiv2, rate->dsmpd, rate->frac);
1052 rockchip_rk3399_pll_get_params(pll, &cur);
1055 cur_parent = pll_mux_ops->get_parent(&pll_mux->hw);
1056 if (cur_parent == PLL_MODE_NORM) {
1057 pll_mux_ops->set_parent(&pll_mux->hw, PLL_MODE_SLOW);
1058 rate_change_remuxed = 1;
1061 /* set pll power down */
1062 writel(HIWORD_UPDATE(RK3399_PLLCON3_PWRDOWN,
1063 RK3399_PLLCON3_PWRDOWN, 0),
1064 pll->reg_base + RK3399_PLLCON(3));
1066 /* update pll values */
1067 writel_relaxed(HIWORD_UPDATE(rate->fbdiv, RK3399_PLLCON0_FBDIV_MASK,
1068 RK3399_PLLCON0_FBDIV_SHIFT),
1069 pll->reg_base + RK3399_PLLCON(0));
1071 writel_relaxed(HIWORD_UPDATE(rate->refdiv, RK3399_PLLCON1_REFDIV_MASK,
1072 RK3399_PLLCON1_REFDIV_SHIFT) |
1073 HIWORD_UPDATE(rate->postdiv1, RK3399_PLLCON1_POSTDIV1_MASK,
1074 RK3399_PLLCON1_POSTDIV1_SHIFT) |
1075 HIWORD_UPDATE(rate->postdiv2, RK3399_PLLCON1_POSTDIV2_MASK,
1076 RK3399_PLLCON1_POSTDIV2_SHIFT),
1077 pll->reg_base + RK3399_PLLCON(1));
1079 /* xPLL CON2 is not HIWORD_MASK */
1080 pllcon = readl_relaxed(pll->reg_base + RK3399_PLLCON(2));
1081 pllcon &= ~(RK3399_PLLCON2_FRAC_MASK << RK3399_PLLCON2_FRAC_SHIFT);
1082 pllcon |= rate->frac << RK3399_PLLCON2_FRAC_SHIFT;
1083 writel_relaxed(pllcon, pll->reg_base + RK3399_PLLCON(2));
1085 writel_relaxed(HIWORD_UPDATE(rate->dsmpd, RK3399_PLLCON3_DSMPD_MASK,
1086 RK3399_PLLCON3_DSMPD_SHIFT),
1087 pll->reg_base + RK3399_PLLCON(3));
1089 /* set pll power up */
1090 writel(HIWORD_UPDATE(0,
1091 RK3399_PLLCON3_PWRDOWN, 0),
1092 pll->reg_base + RK3399_PLLCON(3));
1094 /* wait for the pll to lock */
1095 ret = rockchip_rk3399_pll_wait_lock(pll);
1097 pr_warn("%s: pll update unsucessful, trying to restore old params\n",
1099 rockchip_rk3399_pll_set_params(pll, &cur);
1102 if (rate_change_remuxed)
1103 pll_mux_ops->set_parent(&pll_mux->hw, PLL_MODE_NORM);
1108 static int rockchip_rk3399_pll_set_rate(struct clk_hw *hw, unsigned long drate,
1109 unsigned long prate)
1111 struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
1112 const struct rockchip_pll_rate_table *rate;
1113 unsigned long old_rate = rockchip_rk3399_pll_recalc_rate(hw, prate);
1115 pr_debug("%s: changing %s from %lu to %lu with a parent rate of %lu\n",
1116 __func__, __clk_get_name(hw->clk), old_rate, drate, prate);
1118 /* Get required rate settings from table */
1119 rate = rockchip_get_pll_settings(pll, drate);
1121 pr_err("%s: Invalid rate : %lu for pll clk %s\n", __func__,
1122 drate, __clk_get_name(hw->clk));
1126 return rockchip_rk3399_pll_set_params(pll, rate);
1129 static int rockchip_rk3399_pll_enable(struct clk_hw *hw)
1131 struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
1133 writel(HIWORD_UPDATE(0, RK3399_PLLCON3_PWRDOWN, 0),
1134 pll->reg_base + RK3399_PLLCON(3));
1139 static void rockchip_rk3399_pll_disable(struct clk_hw *hw)
1141 struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
1143 writel(HIWORD_UPDATE(RK3399_PLLCON3_PWRDOWN,
1144 RK3399_PLLCON3_PWRDOWN, 0),
1145 pll->reg_base + RK3399_PLLCON(3));
1148 static int rockchip_rk3399_pll_is_enabled(struct clk_hw *hw)
1150 struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
1151 u32 pllcon = readl(pll->reg_base + RK3399_PLLCON(3));
1153 return !(pllcon & RK3399_PLLCON3_PWRDOWN);
1156 static void rockchip_rk3399_pll_init(struct clk_hw *hw)
1158 struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
1159 const struct rockchip_pll_rate_table *rate;
1160 struct rockchip_pll_rate_table cur;
1161 unsigned long drate;
1163 if (!(pll->flags & ROCKCHIP_PLL_SYNC_RATE))
1166 drate = clk_hw_get_rate(hw);
1167 rate = rockchip_get_pll_settings(pll, drate);
1169 /* when no rate setting for the current rate, rely on clk_set_rate */
1173 rockchip_rk3399_pll_get_params(pll, &cur);
1175 pr_debug("%s: pll %s@%lu: Hz\n", __func__, __clk_get_name(hw->clk),
1177 pr_debug("old - fbdiv: %d, postdiv1: %d, refdiv: %d, postdiv2: %d, dsmpd: %d, frac: %d\n",
1178 cur.fbdiv, cur.postdiv1, cur.refdiv, cur.postdiv2,
1179 cur.dsmpd, cur.frac);
1180 pr_debug("new - fbdiv: %d, postdiv1: %d, refdiv: %d, postdiv2: %d, dsmpd: %d, frac: %d\n",
1181 rate->fbdiv, rate->postdiv1, rate->refdiv, rate->postdiv2,
1182 rate->dsmpd, rate->frac);
1184 if (rate->fbdiv != cur.fbdiv || rate->postdiv1 != cur.postdiv1 ||
1185 rate->refdiv != cur.refdiv || rate->postdiv2 != cur.postdiv2 ||
1186 rate->dsmpd != cur.dsmpd || rate->frac != cur.frac) {
1187 struct clk *parent = clk_get_parent(hw->clk);
1190 pr_warn("%s: parent of %s not available\n",
1191 __func__, __clk_get_name(hw->clk));
1195 pr_debug("%s: pll %s: rate params do not match rate table, adjusting\n",
1196 __func__, __clk_get_name(hw->clk));
1197 rockchip_rk3399_pll_set_params(pll, rate);
1201 static const struct clk_ops rockchip_rk3399_pll_clk_norate_ops = {
1202 .recalc_rate = rockchip_rk3399_pll_recalc_rate,
1203 .enable = rockchip_rk3399_pll_enable,
1204 .disable = rockchip_rk3399_pll_disable,
1205 .is_enabled = rockchip_rk3399_pll_is_enabled,
1208 static const struct clk_ops rockchip_rk3399_pll_clk_ops = {
1209 .recalc_rate = rockchip_rk3399_pll_recalc_rate,
1210 .round_rate = rockchip_pll_round_rate,
1211 .set_rate = rockchip_rk3399_pll_set_rate,
1212 .enable = rockchip_rk3399_pll_enable,
1213 .disable = rockchip_rk3399_pll_disable,
1214 .is_enabled = rockchip_rk3399_pll_is_enabled,
1215 .init = rockchip_rk3399_pll_init,
1219 * Common registering of pll clocks
1222 struct clk *rockchip_clk_register_pll(struct rockchip_clk_provider *ctx,
1223 enum rockchip_pll_type pll_type,
1224 const char *name, const char *const *parent_names,
1225 u8 num_parents, int con_offset, int grf_lock_offset,
1226 int lock_shift, int mode_offset, int mode_shift,
1227 struct rockchip_pll_rate_table *rate_table,
1228 unsigned long flags, u8 clk_pll_flags)
1230 const char *pll_parents[3];
1231 struct clk_init_data init;
1232 struct rockchip_clk_pll *pll;
1233 struct clk_mux *pll_mux;
1234 struct clk *pll_clk, *mux_clk;
1237 if (num_parents != 2) {
1238 pr_err("%s: needs two parent clocks\n", __func__);
1239 return ERR_PTR(-EINVAL);
1242 /* name the actual pll */
1243 snprintf(pll_name, sizeof(pll_name), "pll_%s", name);
1245 pll = kzalloc(sizeof(*pll), GFP_KERNEL);
1247 return ERR_PTR(-ENOMEM);
1249 /* create the mux on top of the real pll */
1250 pll->pll_mux_ops = &clk_mux_ops;
1251 pll_mux = &pll->pll_mux;
1252 pll_mux->reg = ctx->reg_base + mode_offset;
1253 pll_mux->shift = mode_shift;
1254 if (pll_type == pll_rk3328)
1255 pll_mux->mask = PLL_RK3328_MODE_MASK;
1257 pll_mux->mask = PLL_MODE_MASK;
1259 pll_mux->lock = &ctx->lock;
1260 pll_mux->hw.init = &init;
1262 if (pll_type == pll_rk3036 ||
1263 pll_type == pll_rk3066 ||
1264 pll_type == pll_rk3328 ||
1265 pll_type == pll_rk3366 ||
1266 pll_type == pll_rk3399)
1267 pll_mux->flags |= CLK_MUX_HIWORD_MASK;
1269 /* the actual muxing is xin24m, pll-output, xin32k */
1270 pll_parents[0] = parent_names[0];
1271 pll_parents[1] = pll_name;
1272 pll_parents[2] = parent_names[1];
1275 init.flags = CLK_SET_RATE_PARENT;
1276 init.ops = pll->pll_mux_ops;
1277 init.parent_names = pll_parents;
1278 init.num_parents = ARRAY_SIZE(pll_parents);
1280 mux_clk = clk_register(NULL, &pll_mux->hw);
1281 if (IS_ERR(mux_clk))
1284 /* now create the actual pll */
1285 init.name = pll_name;
1287 /* keep all plls untouched for now */
1288 init.flags = flags | CLK_IGNORE_UNUSED;
1290 init.parent_names = &parent_names[0];
1291 init.num_parents = 1;
1296 /* find count of rates in rate_table */
1297 for (len = 0; rate_table[len].rate != 0; )
1300 pll->rate_count = len;
1301 pll->rate_table = kmemdup(rate_table,
1303 sizeof(struct rockchip_pll_rate_table),
1305 WARN(!pll->rate_table,
1306 "%s: could not allocate rate table for %s\n",
1312 if (!pll->rate_table)
1313 init.ops = &rockchip_rk3036_pll_clk_norate_ops;
1315 init.ops = &rockchip_rk3036_pll_clk_ops;
1318 if (!pll->rate_table)
1319 init.ops = &rockchip_rk3066_pll_clk_norate_ops;
1321 init.ops = &rockchip_rk3066_pll_clk_ops;
1324 if (!pll->rate_table)
1325 init.ops = &rockchip_rk3036_pll_clk_norate_ops;
1327 init.ops = &rockchip_rk3036_pll_clk_ops;
1330 if (!pll->rate_table)
1331 init.ops = &rockchip_rk3366_pll_clk_norate_ops;
1333 init.ops = &rockchip_rk3366_pll_clk_ops;
1336 if (!pll->rate_table)
1337 init.ops = &rockchip_rk3399_pll_clk_norate_ops;
1339 init.ops = &rockchip_rk3399_pll_clk_ops;
1342 pr_warn("%s: Unknown pll type for pll clk %s\n",
1346 pll->hw.init = &init;
1347 pll->type = pll_type;
1348 pll->reg_base = ctx->reg_base + con_offset;
1349 pll->lock_offset = grf_lock_offset;
1350 pll->lock_shift = lock_shift;
1351 pll->flags = clk_pll_flags;
1352 pll->lock = &ctx->lock;
1355 pll_clk = clk_register(NULL, &pll->hw);
1356 if (IS_ERR(pll_clk)) {
1357 pr_err("%s: failed to register pll clock %s : %ld\n",
1358 __func__, name, PTR_ERR(pll_clk));
1365 clk_unregister(mux_clk);