1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) 2014 MundoReader S.L.
4 * Author: Heiko Stuebner <heiko@sntech.de>
6 * Copyright (c) 2015 Rockchip Electronics Co. Ltd.
7 * Author: Xing Zheng <zhengxing@rock-chips.com>
10 #include <asm/div64.h>
11 #include <linux/slab.h>
13 #include <linux/delay.h>
14 #include <linux/clk-provider.h>
15 #include <linux/regmap.h>
16 #include <linux/clk.h>
19 #define PLL_MODE_MASK 0x3
20 #define PLL_MODE_SLOW 0x0
21 #define PLL_MODE_NORM 0x1
22 #define PLL_MODE_DEEP 0x2
23 #define PLL_RK3328_MODE_MASK 0x1
25 struct rockchip_clk_pll {
28 struct clk_mux pll_mux;
29 const struct clk_ops *pll_mux_ops;
31 struct notifier_block clk_nb;
33 void __iomem *reg_base;
35 unsigned int lock_shift;
36 enum rockchip_pll_type type;
38 const struct rockchip_pll_rate_table *rate_table;
39 unsigned int rate_count;
42 struct rockchip_clk_provider *ctx;
45 #define to_rockchip_clk_pll(_hw) container_of(_hw, struct rockchip_clk_pll, hw)
46 #define to_rockchip_clk_pll_nb(nb) \
47 container_of(nb, struct rockchip_clk_pll, clk_nb)
49 static const struct rockchip_pll_rate_table *rockchip_get_pll_settings(
50 struct rockchip_clk_pll *pll, unsigned long rate)
52 const struct rockchip_pll_rate_table *rate_table = pll->rate_table;
55 for (i = 0; i < pll->rate_count; i++) {
56 if (rate == rate_table[i].rate)
57 return &rate_table[i];
63 static long rockchip_pll_round_rate(struct clk_hw *hw,
64 unsigned long drate, unsigned long *prate)
66 struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
67 const struct rockchip_pll_rate_table *rate_table = pll->rate_table;
70 /* Assumming rate_table is in descending order */
71 for (i = 0; i < pll->rate_count; i++) {
72 if (drate >= rate_table[i].rate)
73 return rate_table[i].rate;
76 /* return minimum supported value */
77 return rate_table[i - 1].rate;
81 * Wait for the pll to reach the locked state.
82 * The calling set_rate function is responsible for making sure the
83 * grf regmap is available.
85 static int rockchip_pll_wait_lock(struct rockchip_clk_pll *pll)
87 struct regmap *grf = pll->ctx->grf;
89 int delay = 24000000, ret;
92 ret = regmap_read(grf, pll->lock_offset, &val);
94 pr_err("%s: failed to read pll lock status: %d\n",
99 if (val & BIT(pll->lock_shift))
104 pr_err("%s: timeout waiting for pll to lock\n", __func__);
112 #define RK3036_PLLCON(i) (i * 0x4)
113 #define RK3036_PLLCON0_FBDIV_MASK 0xfff
114 #define RK3036_PLLCON0_FBDIV_SHIFT 0
115 #define RK3036_PLLCON0_POSTDIV1_MASK 0x7
116 #define RK3036_PLLCON0_POSTDIV1_SHIFT 12
117 #define RK3036_PLLCON1_REFDIV_MASK 0x3f
118 #define RK3036_PLLCON1_REFDIV_SHIFT 0
119 #define RK3036_PLLCON1_POSTDIV2_MASK 0x7
120 #define RK3036_PLLCON1_POSTDIV2_SHIFT 6
121 #define RK3036_PLLCON1_DSMPD_MASK 0x1
122 #define RK3036_PLLCON1_DSMPD_SHIFT 12
123 #define RK3036_PLLCON2_FRAC_MASK 0xffffff
124 #define RK3036_PLLCON2_FRAC_SHIFT 0
126 #define RK3036_PLLCON1_PWRDOWN (1 << 13)
128 static void rockchip_rk3036_pll_get_params(struct rockchip_clk_pll *pll,
129 struct rockchip_pll_rate_table *rate)
133 pllcon = readl_relaxed(pll->reg_base + RK3036_PLLCON(0));
134 rate->fbdiv = ((pllcon >> RK3036_PLLCON0_FBDIV_SHIFT)
135 & RK3036_PLLCON0_FBDIV_MASK);
136 rate->postdiv1 = ((pllcon >> RK3036_PLLCON0_POSTDIV1_SHIFT)
137 & RK3036_PLLCON0_POSTDIV1_MASK);
139 pllcon = readl_relaxed(pll->reg_base + RK3036_PLLCON(1));
140 rate->refdiv = ((pllcon >> RK3036_PLLCON1_REFDIV_SHIFT)
141 & RK3036_PLLCON1_REFDIV_MASK);
142 rate->postdiv2 = ((pllcon >> RK3036_PLLCON1_POSTDIV2_SHIFT)
143 & RK3036_PLLCON1_POSTDIV2_MASK);
144 rate->dsmpd = ((pllcon >> RK3036_PLLCON1_DSMPD_SHIFT)
145 & RK3036_PLLCON1_DSMPD_MASK);
147 pllcon = readl_relaxed(pll->reg_base + RK3036_PLLCON(2));
148 rate->frac = ((pllcon >> RK3036_PLLCON2_FRAC_SHIFT)
149 & RK3036_PLLCON2_FRAC_MASK);
152 static unsigned long rockchip_rk3036_pll_recalc_rate(struct clk_hw *hw,
155 struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
156 struct rockchip_pll_rate_table cur;
159 rockchip_rk3036_pll_get_params(pll, &cur);
162 do_div(rate64, cur.refdiv);
164 if (cur.dsmpd == 0) {
165 /* fractional mode */
166 u64 frac_rate64 = prate * cur.frac;
168 do_div(frac_rate64, cur.refdiv);
169 rate64 += frac_rate64 >> 24;
172 do_div(rate64, cur.postdiv1);
173 do_div(rate64, cur.postdiv2);
175 return (unsigned long)rate64;
178 static int rockchip_rk3036_pll_set_params(struct rockchip_clk_pll *pll,
179 const struct rockchip_pll_rate_table *rate)
181 const struct clk_ops *pll_mux_ops = pll->pll_mux_ops;
182 struct clk_mux *pll_mux = &pll->pll_mux;
183 struct rockchip_pll_rate_table cur;
185 int rate_change_remuxed = 0;
189 pr_debug("%s: rate settings for %lu fbdiv: %d, postdiv1: %d, refdiv: %d, postdiv2: %d, dsmpd: %d, frac: %d\n",
190 __func__, rate->rate, rate->fbdiv, rate->postdiv1, rate->refdiv,
191 rate->postdiv2, rate->dsmpd, rate->frac);
193 rockchip_rk3036_pll_get_params(pll, &cur);
196 cur_parent = pll_mux_ops->get_parent(&pll_mux->hw);
197 if (cur_parent == PLL_MODE_NORM) {
198 pll_mux_ops->set_parent(&pll_mux->hw, PLL_MODE_SLOW);
199 rate_change_remuxed = 1;
202 /* update pll values */
203 writel_relaxed(HIWORD_UPDATE(rate->fbdiv, RK3036_PLLCON0_FBDIV_MASK,
204 RK3036_PLLCON0_FBDIV_SHIFT) |
205 HIWORD_UPDATE(rate->postdiv1, RK3036_PLLCON0_POSTDIV1_MASK,
206 RK3036_PLLCON0_POSTDIV1_SHIFT),
207 pll->reg_base + RK3036_PLLCON(0));
209 writel_relaxed(HIWORD_UPDATE(rate->refdiv, RK3036_PLLCON1_REFDIV_MASK,
210 RK3036_PLLCON1_REFDIV_SHIFT) |
211 HIWORD_UPDATE(rate->postdiv2, RK3036_PLLCON1_POSTDIV2_MASK,
212 RK3036_PLLCON1_POSTDIV2_SHIFT) |
213 HIWORD_UPDATE(rate->dsmpd, RK3036_PLLCON1_DSMPD_MASK,
214 RK3036_PLLCON1_DSMPD_SHIFT),
215 pll->reg_base + RK3036_PLLCON(1));
217 /* GPLL CON2 is not HIWORD_MASK */
218 pllcon = readl_relaxed(pll->reg_base + RK3036_PLLCON(2));
219 pllcon &= ~(RK3036_PLLCON2_FRAC_MASK << RK3036_PLLCON2_FRAC_SHIFT);
220 pllcon |= rate->frac << RK3036_PLLCON2_FRAC_SHIFT;
221 writel_relaxed(pllcon, pll->reg_base + RK3036_PLLCON(2));
223 /* wait for the pll to lock */
224 ret = rockchip_pll_wait_lock(pll);
226 pr_warn("%s: pll update unsuccessful, trying to restore old params\n",
228 rockchip_rk3036_pll_set_params(pll, &cur);
231 if (rate_change_remuxed)
232 pll_mux_ops->set_parent(&pll_mux->hw, PLL_MODE_NORM);
237 static int rockchip_rk3036_pll_set_rate(struct clk_hw *hw, unsigned long drate,
240 struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
241 const struct rockchip_pll_rate_table *rate;
243 pr_debug("%s: changing %s to %lu with a parent rate of %lu\n",
244 __func__, __clk_get_name(hw->clk), drate, prate);
246 /* Get required rate settings from table */
247 rate = rockchip_get_pll_settings(pll, drate);
249 pr_err("%s: Invalid rate : %lu for pll clk %s\n", __func__,
250 drate, __clk_get_name(hw->clk));
254 return rockchip_rk3036_pll_set_params(pll, rate);
257 static int rockchip_rk3036_pll_enable(struct clk_hw *hw)
259 struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
261 writel(HIWORD_UPDATE(0, RK3036_PLLCON1_PWRDOWN, 0),
262 pll->reg_base + RK3036_PLLCON(1));
263 rockchip_pll_wait_lock(pll);
268 static void rockchip_rk3036_pll_disable(struct clk_hw *hw)
270 struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
272 writel(HIWORD_UPDATE(RK3036_PLLCON1_PWRDOWN,
273 RK3036_PLLCON1_PWRDOWN, 0),
274 pll->reg_base + RK3036_PLLCON(1));
277 static int rockchip_rk3036_pll_is_enabled(struct clk_hw *hw)
279 struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
280 u32 pllcon = readl(pll->reg_base + RK3036_PLLCON(1));
282 return !(pllcon & RK3036_PLLCON1_PWRDOWN);
285 static int rockchip_rk3036_pll_init(struct clk_hw *hw)
287 struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
288 const struct rockchip_pll_rate_table *rate;
289 struct rockchip_pll_rate_table cur;
292 if (!(pll->flags & ROCKCHIP_PLL_SYNC_RATE))
295 drate = clk_hw_get_rate(hw);
296 rate = rockchip_get_pll_settings(pll, drate);
298 /* when no rate setting for the current rate, rely on clk_set_rate */
302 rockchip_rk3036_pll_get_params(pll, &cur);
304 pr_debug("%s: pll %s@%lu: Hz\n", __func__, __clk_get_name(hw->clk),
306 pr_debug("old - fbdiv: %d, postdiv1: %d, refdiv: %d, postdiv2: %d, dsmpd: %d, frac: %d\n",
307 cur.fbdiv, cur.postdiv1, cur.refdiv, cur.postdiv2,
308 cur.dsmpd, cur.frac);
309 pr_debug("new - fbdiv: %d, postdiv1: %d, refdiv: %d, postdiv2: %d, dsmpd: %d, frac: %d\n",
310 rate->fbdiv, rate->postdiv1, rate->refdiv, rate->postdiv2,
311 rate->dsmpd, rate->frac);
313 if (rate->fbdiv != cur.fbdiv || rate->postdiv1 != cur.postdiv1 ||
314 rate->refdiv != cur.refdiv || rate->postdiv2 != cur.postdiv2 ||
315 rate->dsmpd != cur.dsmpd ||
316 (!cur.dsmpd && (rate->frac != cur.frac))) {
317 struct clk *parent = clk_get_parent(hw->clk);
320 pr_warn("%s: parent of %s not available\n",
321 __func__, __clk_get_name(hw->clk));
325 pr_debug("%s: pll %s: rate params do not match rate table, adjusting\n",
326 __func__, __clk_get_name(hw->clk));
327 rockchip_rk3036_pll_set_params(pll, rate);
333 static const struct clk_ops rockchip_rk3036_pll_clk_norate_ops = {
334 .recalc_rate = rockchip_rk3036_pll_recalc_rate,
335 .enable = rockchip_rk3036_pll_enable,
336 .disable = rockchip_rk3036_pll_disable,
337 .is_enabled = rockchip_rk3036_pll_is_enabled,
340 static const struct clk_ops rockchip_rk3036_pll_clk_ops = {
341 .recalc_rate = rockchip_rk3036_pll_recalc_rate,
342 .round_rate = rockchip_pll_round_rate,
343 .set_rate = rockchip_rk3036_pll_set_rate,
344 .enable = rockchip_rk3036_pll_enable,
345 .disable = rockchip_rk3036_pll_disable,
346 .is_enabled = rockchip_rk3036_pll_is_enabled,
347 .init = rockchip_rk3036_pll_init,
351 * PLL used in RK3066, RK3188 and RK3288
354 #define RK3066_PLL_RESET_DELAY(nr) ((nr * 500) / 24 + 1)
356 #define RK3066_PLLCON(i) (i * 0x4)
357 #define RK3066_PLLCON0_OD_MASK 0xf
358 #define RK3066_PLLCON0_OD_SHIFT 0
359 #define RK3066_PLLCON0_NR_MASK 0x3f
360 #define RK3066_PLLCON0_NR_SHIFT 8
361 #define RK3066_PLLCON1_NF_MASK 0x1fff
362 #define RK3066_PLLCON1_NF_SHIFT 0
363 #define RK3066_PLLCON2_NB_MASK 0xfff
364 #define RK3066_PLLCON2_NB_SHIFT 0
365 #define RK3066_PLLCON3_RESET (1 << 5)
366 #define RK3066_PLLCON3_PWRDOWN (1 << 1)
367 #define RK3066_PLLCON3_BYPASS (1 << 0)
369 static void rockchip_rk3066_pll_get_params(struct rockchip_clk_pll *pll,
370 struct rockchip_pll_rate_table *rate)
374 pllcon = readl_relaxed(pll->reg_base + RK3066_PLLCON(0));
375 rate->nr = ((pllcon >> RK3066_PLLCON0_NR_SHIFT)
376 & RK3066_PLLCON0_NR_MASK) + 1;
377 rate->no = ((pllcon >> RK3066_PLLCON0_OD_SHIFT)
378 & RK3066_PLLCON0_OD_MASK) + 1;
380 pllcon = readl_relaxed(pll->reg_base + RK3066_PLLCON(1));
381 rate->nf = ((pllcon >> RK3066_PLLCON1_NF_SHIFT)
382 & RK3066_PLLCON1_NF_MASK) + 1;
384 pllcon = readl_relaxed(pll->reg_base + RK3066_PLLCON(2));
385 rate->nb = ((pllcon >> RK3066_PLLCON2_NB_SHIFT)
386 & RK3066_PLLCON2_NB_MASK) + 1;
389 static unsigned long rockchip_rk3066_pll_recalc_rate(struct clk_hw *hw,
392 struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
393 struct rockchip_pll_rate_table cur;
397 pllcon = readl_relaxed(pll->reg_base + RK3066_PLLCON(3));
398 if (pllcon & RK3066_PLLCON3_BYPASS) {
399 pr_debug("%s: pll %s is bypassed\n", __func__,
400 clk_hw_get_name(hw));
404 rockchip_rk3066_pll_get_params(pll, &cur);
407 do_div(rate64, cur.nr);
408 do_div(rate64, cur.no);
410 return (unsigned long)rate64;
413 static int rockchip_rk3066_pll_set_params(struct rockchip_clk_pll *pll,
414 const struct rockchip_pll_rate_table *rate)
416 const struct clk_ops *pll_mux_ops = pll->pll_mux_ops;
417 struct clk_mux *pll_mux = &pll->pll_mux;
418 struct rockchip_pll_rate_table cur;
419 int rate_change_remuxed = 0;
423 pr_debug("%s: rate settings for %lu (nr, no, nf): (%d, %d, %d)\n",
424 __func__, rate->rate, rate->nr, rate->no, rate->nf);
426 rockchip_rk3066_pll_get_params(pll, &cur);
429 cur_parent = pll_mux_ops->get_parent(&pll_mux->hw);
430 if (cur_parent == PLL_MODE_NORM) {
431 pll_mux_ops->set_parent(&pll_mux->hw, PLL_MODE_SLOW);
432 rate_change_remuxed = 1;
435 /* enter reset mode */
436 writel(HIWORD_UPDATE(RK3066_PLLCON3_RESET, RK3066_PLLCON3_RESET, 0),
437 pll->reg_base + RK3066_PLLCON(3));
439 /* update pll values */
440 writel(HIWORD_UPDATE(rate->nr - 1, RK3066_PLLCON0_NR_MASK,
441 RK3066_PLLCON0_NR_SHIFT) |
442 HIWORD_UPDATE(rate->no - 1, RK3066_PLLCON0_OD_MASK,
443 RK3066_PLLCON0_OD_SHIFT),
444 pll->reg_base + RK3066_PLLCON(0));
446 writel_relaxed(HIWORD_UPDATE(rate->nf - 1, RK3066_PLLCON1_NF_MASK,
447 RK3066_PLLCON1_NF_SHIFT),
448 pll->reg_base + RK3066_PLLCON(1));
449 writel_relaxed(HIWORD_UPDATE(rate->nb - 1, RK3066_PLLCON2_NB_MASK,
450 RK3066_PLLCON2_NB_SHIFT),
451 pll->reg_base + RK3066_PLLCON(2));
453 /* leave reset and wait the reset_delay */
454 writel(HIWORD_UPDATE(0, RK3066_PLLCON3_RESET, 0),
455 pll->reg_base + RK3066_PLLCON(3));
456 udelay(RK3066_PLL_RESET_DELAY(rate->nr));
458 /* wait for the pll to lock */
459 ret = rockchip_pll_wait_lock(pll);
461 pr_warn("%s: pll update unsuccessful, trying to restore old params\n",
463 rockchip_rk3066_pll_set_params(pll, &cur);
466 if (rate_change_remuxed)
467 pll_mux_ops->set_parent(&pll_mux->hw, PLL_MODE_NORM);
472 static int rockchip_rk3066_pll_set_rate(struct clk_hw *hw, unsigned long drate,
475 struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
476 const struct rockchip_pll_rate_table *rate;
478 pr_debug("%s: changing %s to %lu with a parent rate of %lu\n",
479 __func__, clk_hw_get_name(hw), drate, prate);
481 /* Get required rate settings from table */
482 rate = rockchip_get_pll_settings(pll, drate);
484 pr_err("%s: Invalid rate : %lu for pll clk %s\n", __func__,
485 drate, clk_hw_get_name(hw));
489 return rockchip_rk3066_pll_set_params(pll, rate);
492 static int rockchip_rk3066_pll_enable(struct clk_hw *hw)
494 struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
496 writel(HIWORD_UPDATE(0, RK3066_PLLCON3_PWRDOWN, 0),
497 pll->reg_base + RK3066_PLLCON(3));
498 rockchip_pll_wait_lock(pll);
503 static void rockchip_rk3066_pll_disable(struct clk_hw *hw)
505 struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
507 writel(HIWORD_UPDATE(RK3066_PLLCON3_PWRDOWN,
508 RK3066_PLLCON3_PWRDOWN, 0),
509 pll->reg_base + RK3066_PLLCON(3));
512 static int rockchip_rk3066_pll_is_enabled(struct clk_hw *hw)
514 struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
515 u32 pllcon = readl(pll->reg_base + RK3066_PLLCON(3));
517 return !(pllcon & RK3066_PLLCON3_PWRDOWN);
520 static int rockchip_rk3066_pll_init(struct clk_hw *hw)
522 struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
523 const struct rockchip_pll_rate_table *rate;
524 struct rockchip_pll_rate_table cur;
527 if (!(pll->flags & ROCKCHIP_PLL_SYNC_RATE))
530 drate = clk_hw_get_rate(hw);
531 rate = rockchip_get_pll_settings(pll, drate);
533 /* when no rate setting for the current rate, rely on clk_set_rate */
537 rockchip_rk3066_pll_get_params(pll, &cur);
539 pr_debug("%s: pll %s@%lu: nr (%d:%d); no (%d:%d); nf(%d:%d), nb(%d:%d)\n",
540 __func__, clk_hw_get_name(hw), drate, rate->nr, cur.nr,
541 rate->no, cur.no, rate->nf, cur.nf, rate->nb, cur.nb);
542 if (rate->nr != cur.nr || rate->no != cur.no || rate->nf != cur.nf
543 || rate->nb != cur.nb) {
544 pr_debug("%s: pll %s: rate params do not match rate table, adjusting\n",
545 __func__, clk_hw_get_name(hw));
546 rockchip_rk3066_pll_set_params(pll, rate);
552 static const struct clk_ops rockchip_rk3066_pll_clk_norate_ops = {
553 .recalc_rate = rockchip_rk3066_pll_recalc_rate,
554 .enable = rockchip_rk3066_pll_enable,
555 .disable = rockchip_rk3066_pll_disable,
556 .is_enabled = rockchip_rk3066_pll_is_enabled,
559 static const struct clk_ops rockchip_rk3066_pll_clk_ops = {
560 .recalc_rate = rockchip_rk3066_pll_recalc_rate,
561 .round_rate = rockchip_pll_round_rate,
562 .set_rate = rockchip_rk3066_pll_set_rate,
563 .enable = rockchip_rk3066_pll_enable,
564 .disable = rockchip_rk3066_pll_disable,
565 .is_enabled = rockchip_rk3066_pll_is_enabled,
566 .init = rockchip_rk3066_pll_init,
573 #define RK3399_PLLCON(i) (i * 0x4)
574 #define RK3399_PLLCON0_FBDIV_MASK 0xfff
575 #define RK3399_PLLCON0_FBDIV_SHIFT 0
576 #define RK3399_PLLCON1_REFDIV_MASK 0x3f
577 #define RK3399_PLLCON1_REFDIV_SHIFT 0
578 #define RK3399_PLLCON1_POSTDIV1_MASK 0x7
579 #define RK3399_PLLCON1_POSTDIV1_SHIFT 8
580 #define RK3399_PLLCON1_POSTDIV2_MASK 0x7
581 #define RK3399_PLLCON1_POSTDIV2_SHIFT 12
582 #define RK3399_PLLCON2_FRAC_MASK 0xffffff
583 #define RK3399_PLLCON2_FRAC_SHIFT 0
584 #define RK3399_PLLCON2_LOCK_STATUS BIT(31)
585 #define RK3399_PLLCON3_PWRDOWN BIT(0)
586 #define RK3399_PLLCON3_DSMPD_MASK 0x1
587 #define RK3399_PLLCON3_DSMPD_SHIFT 3
589 static int rockchip_rk3399_pll_wait_lock(struct rockchip_clk_pll *pll)
592 int delay = 24000000;
594 /* poll check the lock status in rk3399 xPLLCON2 */
596 pllcon = readl_relaxed(pll->reg_base + RK3399_PLLCON(2));
597 if (pllcon & RK3399_PLLCON2_LOCK_STATUS)
603 pr_err("%s: timeout waiting for pll to lock\n", __func__);
607 static void rockchip_rk3399_pll_get_params(struct rockchip_clk_pll *pll,
608 struct rockchip_pll_rate_table *rate)
612 pllcon = readl_relaxed(pll->reg_base + RK3399_PLLCON(0));
613 rate->fbdiv = ((pllcon >> RK3399_PLLCON0_FBDIV_SHIFT)
614 & RK3399_PLLCON0_FBDIV_MASK);
616 pllcon = readl_relaxed(pll->reg_base + RK3399_PLLCON(1));
617 rate->refdiv = ((pllcon >> RK3399_PLLCON1_REFDIV_SHIFT)
618 & RK3399_PLLCON1_REFDIV_MASK);
619 rate->postdiv1 = ((pllcon >> RK3399_PLLCON1_POSTDIV1_SHIFT)
620 & RK3399_PLLCON1_POSTDIV1_MASK);
621 rate->postdiv2 = ((pllcon >> RK3399_PLLCON1_POSTDIV2_SHIFT)
622 & RK3399_PLLCON1_POSTDIV2_MASK);
624 pllcon = readl_relaxed(pll->reg_base + RK3399_PLLCON(2));
625 rate->frac = ((pllcon >> RK3399_PLLCON2_FRAC_SHIFT)
626 & RK3399_PLLCON2_FRAC_MASK);
628 pllcon = readl_relaxed(pll->reg_base + RK3399_PLLCON(3));
629 rate->dsmpd = ((pllcon >> RK3399_PLLCON3_DSMPD_SHIFT)
630 & RK3399_PLLCON3_DSMPD_MASK);
633 static unsigned long rockchip_rk3399_pll_recalc_rate(struct clk_hw *hw,
636 struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
637 struct rockchip_pll_rate_table cur;
640 rockchip_rk3399_pll_get_params(pll, &cur);
643 do_div(rate64, cur.refdiv);
645 if (cur.dsmpd == 0) {
646 /* fractional mode */
647 u64 frac_rate64 = prate * cur.frac;
649 do_div(frac_rate64, cur.refdiv);
650 rate64 += frac_rate64 >> 24;
653 do_div(rate64, cur.postdiv1);
654 do_div(rate64, cur.postdiv2);
656 return (unsigned long)rate64;
659 static int rockchip_rk3399_pll_set_params(struct rockchip_clk_pll *pll,
660 const struct rockchip_pll_rate_table *rate)
662 const struct clk_ops *pll_mux_ops = pll->pll_mux_ops;
663 struct clk_mux *pll_mux = &pll->pll_mux;
664 struct rockchip_pll_rate_table cur;
666 int rate_change_remuxed = 0;
670 pr_debug("%s: rate settings for %lu fbdiv: %d, postdiv1: %d, refdiv: %d, postdiv2: %d, dsmpd: %d, frac: %d\n",
671 __func__, rate->rate, rate->fbdiv, rate->postdiv1, rate->refdiv,
672 rate->postdiv2, rate->dsmpd, rate->frac);
674 rockchip_rk3399_pll_get_params(pll, &cur);
677 cur_parent = pll_mux_ops->get_parent(&pll_mux->hw);
678 if (cur_parent == PLL_MODE_NORM) {
679 pll_mux_ops->set_parent(&pll_mux->hw, PLL_MODE_SLOW);
680 rate_change_remuxed = 1;
683 /* update pll values */
684 writel_relaxed(HIWORD_UPDATE(rate->fbdiv, RK3399_PLLCON0_FBDIV_MASK,
685 RK3399_PLLCON0_FBDIV_SHIFT),
686 pll->reg_base + RK3399_PLLCON(0));
688 writel_relaxed(HIWORD_UPDATE(rate->refdiv, RK3399_PLLCON1_REFDIV_MASK,
689 RK3399_PLLCON1_REFDIV_SHIFT) |
690 HIWORD_UPDATE(rate->postdiv1, RK3399_PLLCON1_POSTDIV1_MASK,
691 RK3399_PLLCON1_POSTDIV1_SHIFT) |
692 HIWORD_UPDATE(rate->postdiv2, RK3399_PLLCON1_POSTDIV2_MASK,
693 RK3399_PLLCON1_POSTDIV2_SHIFT),
694 pll->reg_base + RK3399_PLLCON(1));
696 /* xPLL CON2 is not HIWORD_MASK */
697 pllcon = readl_relaxed(pll->reg_base + RK3399_PLLCON(2));
698 pllcon &= ~(RK3399_PLLCON2_FRAC_MASK << RK3399_PLLCON2_FRAC_SHIFT);
699 pllcon |= rate->frac << RK3399_PLLCON2_FRAC_SHIFT;
700 writel_relaxed(pllcon, pll->reg_base + RK3399_PLLCON(2));
702 writel_relaxed(HIWORD_UPDATE(rate->dsmpd, RK3399_PLLCON3_DSMPD_MASK,
703 RK3399_PLLCON3_DSMPD_SHIFT),
704 pll->reg_base + RK3399_PLLCON(3));
706 /* wait for the pll to lock */
707 ret = rockchip_rk3399_pll_wait_lock(pll);
709 pr_warn("%s: pll update unsuccessful, trying to restore old params\n",
711 rockchip_rk3399_pll_set_params(pll, &cur);
714 if (rate_change_remuxed)
715 pll_mux_ops->set_parent(&pll_mux->hw, PLL_MODE_NORM);
720 static int rockchip_rk3399_pll_set_rate(struct clk_hw *hw, unsigned long drate,
723 struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
724 const struct rockchip_pll_rate_table *rate;
726 pr_debug("%s: changing %s to %lu with a parent rate of %lu\n",
727 __func__, __clk_get_name(hw->clk), drate, prate);
729 /* Get required rate settings from table */
730 rate = rockchip_get_pll_settings(pll, drate);
732 pr_err("%s: Invalid rate : %lu for pll clk %s\n", __func__,
733 drate, __clk_get_name(hw->clk));
737 return rockchip_rk3399_pll_set_params(pll, rate);
740 static int rockchip_rk3399_pll_enable(struct clk_hw *hw)
742 struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
744 writel(HIWORD_UPDATE(0, RK3399_PLLCON3_PWRDOWN, 0),
745 pll->reg_base + RK3399_PLLCON(3));
746 rockchip_rk3399_pll_wait_lock(pll);
751 static void rockchip_rk3399_pll_disable(struct clk_hw *hw)
753 struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
755 writel(HIWORD_UPDATE(RK3399_PLLCON3_PWRDOWN,
756 RK3399_PLLCON3_PWRDOWN, 0),
757 pll->reg_base + RK3399_PLLCON(3));
760 static int rockchip_rk3399_pll_is_enabled(struct clk_hw *hw)
762 struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
763 u32 pllcon = readl(pll->reg_base + RK3399_PLLCON(3));
765 return !(pllcon & RK3399_PLLCON3_PWRDOWN);
768 static int rockchip_rk3399_pll_init(struct clk_hw *hw)
770 struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
771 const struct rockchip_pll_rate_table *rate;
772 struct rockchip_pll_rate_table cur;
775 if (!(pll->flags & ROCKCHIP_PLL_SYNC_RATE))
778 drate = clk_hw_get_rate(hw);
779 rate = rockchip_get_pll_settings(pll, drate);
781 /* when no rate setting for the current rate, rely on clk_set_rate */
785 rockchip_rk3399_pll_get_params(pll, &cur);
787 pr_debug("%s: pll %s@%lu: Hz\n", __func__, __clk_get_name(hw->clk),
789 pr_debug("old - fbdiv: %d, postdiv1: %d, refdiv: %d, postdiv2: %d, dsmpd: %d, frac: %d\n",
790 cur.fbdiv, cur.postdiv1, cur.refdiv, cur.postdiv2,
791 cur.dsmpd, cur.frac);
792 pr_debug("new - fbdiv: %d, postdiv1: %d, refdiv: %d, postdiv2: %d, dsmpd: %d, frac: %d\n",
793 rate->fbdiv, rate->postdiv1, rate->refdiv, rate->postdiv2,
794 rate->dsmpd, rate->frac);
796 if (rate->fbdiv != cur.fbdiv || rate->postdiv1 != cur.postdiv1 ||
797 rate->refdiv != cur.refdiv || rate->postdiv2 != cur.postdiv2 ||
798 rate->dsmpd != cur.dsmpd ||
799 (!cur.dsmpd && (rate->frac != cur.frac))) {
800 struct clk *parent = clk_get_parent(hw->clk);
803 pr_warn("%s: parent of %s not available\n",
804 __func__, __clk_get_name(hw->clk));
808 pr_debug("%s: pll %s: rate params do not match rate table, adjusting\n",
809 __func__, __clk_get_name(hw->clk));
810 rockchip_rk3399_pll_set_params(pll, rate);
816 static const struct clk_ops rockchip_rk3399_pll_clk_norate_ops = {
817 .recalc_rate = rockchip_rk3399_pll_recalc_rate,
818 .enable = rockchip_rk3399_pll_enable,
819 .disable = rockchip_rk3399_pll_disable,
820 .is_enabled = rockchip_rk3399_pll_is_enabled,
823 static const struct clk_ops rockchip_rk3399_pll_clk_ops = {
824 .recalc_rate = rockchip_rk3399_pll_recalc_rate,
825 .round_rate = rockchip_pll_round_rate,
826 .set_rate = rockchip_rk3399_pll_set_rate,
827 .enable = rockchip_rk3399_pll_enable,
828 .disable = rockchip_rk3399_pll_disable,
829 .is_enabled = rockchip_rk3399_pll_is_enabled,
830 .init = rockchip_rk3399_pll_init,
834 * Common registering of pll clocks
837 struct clk *rockchip_clk_register_pll(struct rockchip_clk_provider *ctx,
838 enum rockchip_pll_type pll_type,
839 const char *name, const char *const *parent_names,
840 u8 num_parents, int con_offset, int grf_lock_offset,
841 int lock_shift, int mode_offset, int mode_shift,
842 struct rockchip_pll_rate_table *rate_table,
843 unsigned long flags, u8 clk_pll_flags)
845 const char *pll_parents[3];
846 struct clk_init_data init;
847 struct rockchip_clk_pll *pll;
848 struct clk_mux *pll_mux;
849 struct clk *pll_clk, *mux_clk;
852 if ((pll_type != pll_rk3328 && num_parents != 2) ||
853 (pll_type == pll_rk3328 && num_parents != 1)) {
854 pr_err("%s: needs two parent clocks\n", __func__);
855 return ERR_PTR(-EINVAL);
858 /* name the actual pll */
859 snprintf(pll_name, sizeof(pll_name), "pll_%s", name);
861 pll = kzalloc(sizeof(*pll), GFP_KERNEL);
863 return ERR_PTR(-ENOMEM);
865 /* create the mux on top of the real pll */
866 pll->pll_mux_ops = &clk_mux_ops;
867 pll_mux = &pll->pll_mux;
868 pll_mux->reg = ctx->reg_base + mode_offset;
869 pll_mux->shift = mode_shift;
870 if (pll_type == pll_rk3328)
871 pll_mux->mask = PLL_RK3328_MODE_MASK;
873 pll_mux->mask = PLL_MODE_MASK;
875 pll_mux->lock = &ctx->lock;
876 pll_mux->hw.init = &init;
878 if (pll_type == pll_rk3036 ||
879 pll_type == pll_rk3066 ||
880 pll_type == pll_rk3328 ||
881 pll_type == pll_rk3399)
882 pll_mux->flags |= CLK_MUX_HIWORD_MASK;
884 /* the actual muxing is xin24m, pll-output, xin32k */
885 pll_parents[0] = parent_names[0];
886 pll_parents[1] = pll_name;
887 pll_parents[2] = parent_names[1];
890 init.flags = CLK_SET_RATE_PARENT;
891 init.ops = pll->pll_mux_ops;
892 init.parent_names = pll_parents;
893 if (pll_type == pll_rk3328)
894 init.num_parents = 2;
896 init.num_parents = ARRAY_SIZE(pll_parents);
898 mux_clk = clk_register(NULL, &pll_mux->hw);
902 /* now create the actual pll */
903 init.name = pll_name;
905 /* keep all plls untouched for now */
906 init.flags = flags | CLK_IGNORE_UNUSED;
908 init.parent_names = &parent_names[0];
909 init.num_parents = 1;
914 /* find count of rates in rate_table */
915 for (len = 0; rate_table[len].rate != 0; )
918 pll->rate_count = len;
919 pll->rate_table = kmemdup(rate_table,
921 sizeof(struct rockchip_pll_rate_table),
923 WARN(!pll->rate_table,
924 "%s: could not allocate rate table for %s\n",
931 if (!pll->rate_table || IS_ERR(ctx->grf))
932 init.ops = &rockchip_rk3036_pll_clk_norate_ops;
934 init.ops = &rockchip_rk3036_pll_clk_ops;
937 if (!pll->rate_table || IS_ERR(ctx->grf))
938 init.ops = &rockchip_rk3066_pll_clk_norate_ops;
940 init.ops = &rockchip_rk3066_pll_clk_ops;
943 if (!pll->rate_table)
944 init.ops = &rockchip_rk3399_pll_clk_norate_ops;
946 init.ops = &rockchip_rk3399_pll_clk_ops;
949 pr_warn("%s: Unknown pll type for pll clk %s\n",
953 pll->hw.init = &init;
954 pll->type = pll_type;
955 pll->reg_base = ctx->reg_base + con_offset;
956 pll->lock_offset = grf_lock_offset;
957 pll->lock_shift = lock_shift;
958 pll->flags = clk_pll_flags;
959 pll->lock = &ctx->lock;
962 pll_clk = clk_register(NULL, &pll->hw);
963 if (IS_ERR(pll_clk)) {
964 pr_err("%s: failed to register pll clock %s : %ld\n",
965 __func__, name, PTR_ERR(pll_clk));
972 clk_unregister(mux_clk);