ARM64: DTS: Add rk3399-firefly uart4 device, node as /dev/ttyS1
[firefly-linux-kernel-4.4.55.git] / drivers / clk / rockchip / clk-pll.c
1 /*
2  * Copyright (c) 2014 MundoReader S.L.
3  * Author: Heiko Stuebner <heiko@sntech.de>
4  *
5  * Copyright (c) 2015 Rockchip Electronics Co. Ltd.
6  * Author: Xing Zheng <zhengxing@rock-chips.com>
7  *
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.
12  *
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.
17  */
18
19 #include <asm/div64.h>
20 #include <linux/slab.h>
21 #include <linux/io.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>
27 #include "clk.h"
28
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
34
35 struct rockchip_clk_pll {
36         struct clk_hw           hw;
37
38         struct clk_mux          pll_mux;
39         const struct clk_ops    *pll_mux_ops;
40
41         struct notifier_block   clk_nb;
42
43         void __iomem            *reg_base;
44         int                     lock_offset;
45         unsigned int            lock_shift;
46         enum rockchip_pll_type  type;
47         u8                      flags;
48         const struct rockchip_pll_rate_table *rate_table;
49         unsigned int            rate_count;
50         spinlock_t              *lock;
51
52         struct rockchip_clk_provider *ctx;
53 };
54
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)
58
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);
63
64 #define MHZ                     (1000UL * 1000UL)
65 #define KHZ                     (1000UL)
66
67 /* CLK_PLL_TYPE_RK3066_AUTO type ops */
68 #define PLL_FREF_MIN            (269 * KHZ)
69 #define PLL_FREF_MAX            (2200 * MHZ)
70
71 #define PLL_FVCO_MIN            (440 * MHZ)
72 #define PLL_FVCO_MAX            (2200 * MHZ)
73
74 #define PLL_FOUT_MIN            (27500 * KHZ)
75 #define PLL_FOUT_MAX            (2200 * MHZ)
76
77 #define PLL_NF_MAX              (4096)
78 #define PLL_NR_MAX              (64)
79 #define PLL_NO_MAX              (16)
80
81 /* CLK_PLL_TYPE_RK3036/3366/3399_AUTO type ops */
82 #define MIN_FOUTVCO_FREQ        (800 * MHZ)
83 #define MAX_FOUTVCO_FREQ        (2000 * MHZ)
84
85 static struct rockchip_pll_rate_table auto_table;
86
87 static struct rockchip_pll_rate_table *rk_pll_rate_table_get(void)
88 {
89         return &auto_table;
90 }
91
92 static int rockchip_pll_clk_set_postdiv(unsigned long fout_hz,
93                                         u32 *postdiv1,
94                                         u32 *postdiv2,
95                                         u32 *foutvco)
96 {
97         unsigned long freq;
98
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) {
105                                         *foutvco = freq;
106                                         return 0;
107                                 }
108                         }
109                 }
110                 pr_err("CANNOT FIND postdiv1/2 to make fout in range from 800M to 2000M,fout = %lu\n",
111                        fout_hz);
112         } else {
113                 *postdiv1 = 1;
114                 *postdiv2 = 1;
115         }
116         return 0;
117 }
118
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)
123 {
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;
127         u64 fin_64, frac_64;
128         u32 f_frac, postdiv1, postdiv2;
129         unsigned long clk_gcd = 0;
130
131         if (fin_hz == 0 || fout_hz == 0 || fout_hz == fin_hz)
132                 return NULL;
133
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;
138
139         if (fin_hz / MHZ * MHZ == fin_hz && fout_hz / MHZ * MHZ == fout_hz) {
140                 fin_hz /= MHZ;
141                 foutvco /= MHZ;
142                 clk_gcd = gcd(fin_hz, foutvco);
143                 rate_table->refdiv = fin_hz / clk_gcd;
144                 rate_table->fbdiv = foutvco / clk_gcd;
145
146                 rate_table->frac = 0;
147
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);
152         } else {
153                 pr_debug("frac div running, fin_hz = %lu, fout_hz = %lu, fin_INT_mhz = %lu, fout_INT_mhz = %lu\n",
154                          fin_hz, fout_hz,
155                          fin_hz / MHZ * MHZ,
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);
164
165                 rate_table->frac = 0;
166
167                 f_frac = (foutvco % MHZ);
168                 fin_64 = fin_hz;
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);
176         }
177         return rate_table;
178 }
179
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)
184 {
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;
188         u32 n;
189         u32 numerator, denominator;
190         u64 fref, fvco, fout;
191         unsigned long clk_gcd = 0;
192
193         nr_out = PLL_NR_MAX + 1;
194         no_out = 0;
195         nf_out = 0;
196
197         if (fin_hz == 0 || fout_hz == 0 || fout_hz == fin_hz)
198                 return NULL;
199
200         clk_gcd = gcd(fin_hz, fout_hz);
201
202         numerator = fout_hz / clk_gcd;
203         denominator = fin_hz / clk_gcd;
204
205         for (n = 1;; n++) {
206                 nf = numerator * n;
207                 nonr = denominator * n;
208                 if (nf > PLL_NF_MAX || nonr > (PLL_NO_MAX * PLL_NR_MAX))
209                         break;
210
211                 for (no = 1; no <= PLL_NO_MAX; no++) {
212                         if (!(no == 1 || !(no % 2)))
213                                 continue;
214
215                         if (nonr % no)
216                                 continue;
217                         nr = nonr / no;
218
219                         if (nr > PLL_NR_MAX)
220                                 continue;
221
222                         fref = fin_hz / nr;
223                         if (fref < PLL_FREF_MIN || fref > PLL_FREF_MAX)
224                                 continue;
225
226                         fvco = fref * nf;
227                         if (fvco < PLL_FVCO_MIN || fvco > PLL_FVCO_MAX)
228                                 continue;
229
230                         fout = fvco / no;
231                         if (fout < PLL_FOUT_MIN || fout > PLL_FOUT_MAX)
232                                 continue;
233
234                         /* select the best from all available PLL settings */
235                         if ((no > no_out) ||
236                             ((no == no_out) && (nr < nr_out))) {
237                                 nr_out = nr;
238                                 nf_out = nf;
239                                 no_out = no;
240                         }
241                 }
242         }
243
244         /* output the best PLL setting */
245         if ((nr_out <= PLL_NR_MAX) && (no_out > 0)) {
246                 rate_table->nr = nr_out;
247                 rate_table->nf = nf_out;
248                 rate_table->no = no_out;
249         } else {
250                 return NULL;
251         }
252
253         return rate_table;
254 }
255
256 static const struct rockchip_pll_rate_table *rockchip_get_pll_settings(
257                             struct rockchip_clk_pll *pll, unsigned long rate)
258 {
259         const struct rockchip_pll_rate_table  *rate_table = pll->rate_table;
260         int i;
261
262         for (i = 0; i < pll->rate_count; i++) {
263                 if (rate == rate_table[i].rate)
264                         return &rate_table[i];
265         }
266
267         if (pll->type == pll_rk3066)
268                 return rockchip_rk3066_pll_clk_set_by_auto(pll, 24 * MHZ, rate);
269         else
270                 return rockchip_pll_clk_set_by_auto(pll, 24 * MHZ, rate);
271 }
272
273 static long rockchip_pll_round_rate(struct clk_hw *hw,
274                             unsigned long drate, unsigned long *prate)
275 {
276         return drate;
277 }
278
279 /*
280  * Wait for the pll to reach the locked state.
281  * The calling set_rate function is responsible for making sure the
282  * grf regmap is available.
283  */
284 static int rockchip_pll_wait_lock(struct rockchip_clk_pll *pll)
285 {
286         struct regmap *grf = rockchip_clk_get_grf(pll->ctx);
287         unsigned int val;
288         int delay = 24000000, ret;
289
290         while (delay > 0) {
291                 ret = regmap_read(grf, pll->lock_offset, &val);
292                 if (ret) {
293                         pr_err("%s: failed to read pll lock status: %d\n",
294                                __func__, ret);
295                         return ret;
296                 }
297
298                 if (val & BIT(pll->lock_shift))
299                         return 0;
300                 delay--;
301         }
302
303         pr_err("%s: timeout waiting for pll to lock\n", __func__);
304         return -ETIMEDOUT;
305 }
306
307 /**
308  * PLL used in RK3036
309  */
310
311 #define RK3036_PLLCON(i)                        (i * 0x4)
312 #define RK3036_PLLCON0_FBDIV_MASK               0xfff
313 #define RK3036_PLLCON0_FBDIV_SHIFT              0
314 #define RK3036_PLLCON0_POSTDIV1_MASK            0x7
315 #define RK3036_PLLCON0_POSTDIV1_SHIFT           12
316 #define RK3036_PLLCON1_REFDIV_MASK              0x3f
317 #define RK3036_PLLCON1_REFDIV_SHIFT             0
318 #define RK3036_PLLCON1_POSTDIV2_MASK            0x7
319 #define RK3036_PLLCON1_POSTDIV2_SHIFT           6
320 #define RK3036_PLLCON1_DSMPD_MASK               0x1
321 #define RK3036_PLLCON1_DSMPD_SHIFT              12
322 #define RK3036_PLLCON2_FRAC_MASK                0xffffff
323 #define RK3036_PLLCON2_FRAC_SHIFT               0
324
325 #define RK3036_PLLCON1_PWRDOWN                  (1 << 13)
326
327 static void rockchip_rk3036_pll_get_params(struct rockchip_clk_pll *pll,
328                                         struct rockchip_pll_rate_table *rate)
329 {
330         u32 pllcon;
331
332         pllcon = readl_relaxed(pll->reg_base + RK3036_PLLCON(0));
333         rate->fbdiv = ((pllcon >> RK3036_PLLCON0_FBDIV_SHIFT)
334                                 & RK3036_PLLCON0_FBDIV_MASK);
335         rate->postdiv1 = ((pllcon >> RK3036_PLLCON0_POSTDIV1_SHIFT)
336                                 & RK3036_PLLCON0_POSTDIV1_MASK);
337
338         pllcon = readl_relaxed(pll->reg_base + RK3036_PLLCON(1));
339         rate->refdiv = ((pllcon >> RK3036_PLLCON1_REFDIV_SHIFT)
340                                 & RK3036_PLLCON1_REFDIV_MASK);
341         rate->postdiv2 = ((pllcon >> RK3036_PLLCON1_POSTDIV2_SHIFT)
342                                 & RK3036_PLLCON1_POSTDIV2_MASK);
343         rate->dsmpd = ((pllcon >> RK3036_PLLCON1_DSMPD_SHIFT)
344                                 & RK3036_PLLCON1_DSMPD_MASK);
345
346         pllcon = readl_relaxed(pll->reg_base + RK3036_PLLCON(2));
347         rate->frac = ((pllcon >> RK3036_PLLCON2_FRAC_SHIFT)
348                                 & RK3036_PLLCON2_FRAC_MASK);
349 }
350
351 static unsigned long rockchip_rk3036_pll_recalc_rate(struct clk_hw *hw,
352                                                      unsigned long prate)
353 {
354         struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
355         struct rockchip_pll_rate_table cur;
356         u64 rate64 = prate;
357
358         if (pll->type == pll_rk3366)
359                 rockchip_rk3366_pll_get_params(pll, &cur);
360         else
361                 rockchip_rk3036_pll_get_params(pll, &cur);
362
363         rate64 *= cur.fbdiv;
364         do_div(rate64, cur.refdiv);
365
366         if (cur.dsmpd == 0) {
367                 /* fractional mode */
368                 u64 frac_rate64 = prate * cur.frac;
369
370                 do_div(frac_rate64, cur.refdiv);
371                 rate64 += frac_rate64 >> 24;
372         }
373
374         do_div(rate64, cur.postdiv1);
375         do_div(rate64, cur.postdiv2);
376
377         return (unsigned long)rate64;
378 }
379
380 static int rockchip_rk3036_pll_set_params(struct rockchip_clk_pll *pll,
381                                 const struct rockchip_pll_rate_table *rate)
382 {
383         const struct clk_ops *pll_mux_ops = pll->pll_mux_ops;
384         struct clk_mux *pll_mux = &pll->pll_mux;
385         struct rockchip_pll_rate_table cur;
386         u32 pllcon;
387         int rate_change_remuxed = 0;
388         int cur_parent;
389         int ret;
390
391         pr_debug("%s: rate settings for %lu fbdiv: %d, postdiv1: %d, refdiv: %d, postdiv2: %d, dsmpd: %d, frac: %d\n",
392                 __func__, rate->rate, rate->fbdiv, rate->postdiv1, rate->refdiv,
393                 rate->postdiv2, rate->dsmpd, rate->frac);
394
395         rockchip_rk3036_pll_get_params(pll, &cur);
396         cur.rate = 0;
397
398         cur_parent = pll_mux_ops->get_parent(&pll_mux->hw);
399         if (cur_parent == PLL_MODE_NORM) {
400                 pll_mux_ops->set_parent(&pll_mux->hw, PLL_MODE_SLOW);
401                 rate_change_remuxed = 1;
402         }
403
404         /* update pll values */
405         writel_relaxed(HIWORD_UPDATE(rate->fbdiv, RK3036_PLLCON0_FBDIV_MASK,
406                                           RK3036_PLLCON0_FBDIV_SHIFT) |
407                        HIWORD_UPDATE(rate->postdiv1, RK3036_PLLCON0_POSTDIV1_MASK,
408                                              RK3036_PLLCON0_POSTDIV1_SHIFT),
409                        pll->reg_base + RK3036_PLLCON(0));
410
411         writel_relaxed(HIWORD_UPDATE(rate->refdiv, RK3036_PLLCON1_REFDIV_MASK,
412                                                    RK3036_PLLCON1_REFDIV_SHIFT) |
413                        HIWORD_UPDATE(rate->postdiv2, RK3036_PLLCON1_POSTDIV2_MASK,
414                                                      RK3036_PLLCON1_POSTDIV2_SHIFT) |
415                        HIWORD_UPDATE(rate->dsmpd, RK3036_PLLCON1_DSMPD_MASK,
416                                                   RK3036_PLLCON1_DSMPD_SHIFT),
417                        pll->reg_base + RK3036_PLLCON(1));
418
419         /* GPLL CON2 is not HIWORD_MASK */
420         pllcon = readl_relaxed(pll->reg_base + RK3036_PLLCON(2));
421         pllcon &= ~(RK3036_PLLCON2_FRAC_MASK << RK3036_PLLCON2_FRAC_SHIFT);
422         pllcon |= rate->frac << RK3036_PLLCON2_FRAC_SHIFT;
423         writel_relaxed(pllcon, pll->reg_base + RK3036_PLLCON(2));
424
425         /* wait for the pll to lock */
426         ret = rockchip_pll_wait_lock(pll);
427         if (ret) {
428                 pr_warn("%s: pll update unsucessful, trying to restore old params\n",
429                         __func__);
430                 rockchip_rk3036_pll_set_params(pll, &cur);
431         }
432
433         if (rate_change_remuxed)
434                 pll_mux_ops->set_parent(&pll_mux->hw, PLL_MODE_NORM);
435
436         return ret;
437 }
438
439 static int rockchip_rk3036_pll_set_rate(struct clk_hw *hw, unsigned long drate,
440                                         unsigned long prate)
441 {
442         struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
443         const struct rockchip_pll_rate_table *rate;
444         unsigned long old_rate = rockchip_rk3036_pll_recalc_rate(hw, prate);
445         struct regmap *grf = rockchip_clk_get_grf(pll->ctx);
446
447         if (IS_ERR(grf)) {
448                 pr_debug("%s: grf regmap not available, aborting rate change\n",
449                          __func__);
450                 return PTR_ERR(grf);
451         }
452
453         pr_debug("%s: changing %s from %lu to %lu with a parent rate of %lu\n",
454                  __func__, __clk_get_name(hw->clk), old_rate, drate, prate);
455
456         /* Get required rate settings from table */
457         rate = rockchip_get_pll_settings(pll, drate);
458         if (!rate) {
459                 pr_err("%s: Invalid rate : %lu for pll clk %s\n", __func__,
460                         drate, __clk_get_name(hw->clk));
461                 return -EINVAL;
462         }
463
464         if (pll->type == pll_rk3366)
465                 return rockchip_rk3366_pll_set_params(pll, rate);
466
467         return rockchip_rk3036_pll_set_params(pll, rate);
468 }
469
470 static int rockchip_rk3036_pll_enable(struct clk_hw *hw)
471 {
472         struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
473
474         writel(HIWORD_UPDATE(0, RK3036_PLLCON1_PWRDOWN, 0),
475                pll->reg_base + RK3036_PLLCON(1));
476         rockchip_pll_wait_lock(pll);
477
478         return 0;
479 }
480
481 static void rockchip_rk3036_pll_disable(struct clk_hw *hw)
482 {
483         struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
484
485         writel(HIWORD_UPDATE(RK3036_PLLCON1_PWRDOWN,
486                              RK3036_PLLCON1_PWRDOWN, 0),
487                pll->reg_base + RK3036_PLLCON(1));
488 }
489
490 static int rockchip_rk3036_pll_is_enabled(struct clk_hw *hw)
491 {
492         struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
493         u32 pllcon = readl(pll->reg_base + RK3036_PLLCON(1));
494
495         return !(pllcon & RK3036_PLLCON1_PWRDOWN);
496 }
497
498 static void rockchip_rk3036_pll_init(struct clk_hw *hw)
499 {
500         struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
501         const struct rockchip_pll_rate_table *rate;
502         struct rockchip_pll_rate_table cur;
503         unsigned long drate;
504
505         if (!(pll->flags & ROCKCHIP_PLL_SYNC_RATE))
506                 return;
507
508         drate = clk_hw_get_rate(hw);
509         rate = rockchip_get_pll_settings(pll, drate);
510
511         /* when no rate setting for the current rate, rely on clk_set_rate */
512         if (!rate)
513                 return;
514
515         if (pll->type == pll_rk3366)
516                 rockchip_rk3366_pll_get_params(pll, &cur);
517         else
518                 rockchip_rk3036_pll_get_params(pll, &cur);
519
520         pr_debug("%s: pll %s@%lu: Hz\n", __func__, __clk_get_name(hw->clk),
521                  drate);
522         pr_debug("old - fbdiv: %d, postdiv1: %d, refdiv: %d, postdiv2: %d, dsmpd: %d, frac: %d\n",
523                  cur.fbdiv, cur.postdiv1, cur.refdiv, cur.postdiv2,
524                  cur.dsmpd, cur.frac);
525         pr_debug("new - fbdiv: %d, postdiv1: %d, refdiv: %d, postdiv2: %d, dsmpd: %d, frac: %d\n",
526                  rate->fbdiv, rate->postdiv1, rate->refdiv, rate->postdiv2,
527                  rate->dsmpd, rate->frac);
528
529         if (rate->fbdiv != cur.fbdiv || rate->postdiv1 != cur.postdiv1 ||
530                 rate->refdiv != cur.refdiv || rate->postdiv2 != cur.postdiv2 ||
531                 rate->dsmpd != cur.dsmpd || rate->frac != cur.frac) {
532                 struct clk *parent = clk_get_parent(hw->clk);
533
534                 if (!parent) {
535                         pr_warn("%s: parent of %s not available\n",
536                                 __func__, __clk_get_name(hw->clk));
537                         return;
538                 }
539
540                 pr_debug("%s: pll %s: rate params do not match rate table, adjusting\n",
541                          __func__, __clk_get_name(hw->clk));
542                 if (pll->type == pll_rk3366)
543                         rockchip_rk3366_pll_set_params(pll, rate);
544                 else
545                         rockchip_rk3036_pll_set_params(pll, rate);
546         }
547 }
548
549 static const struct clk_ops rockchip_rk3036_pll_clk_norate_ops = {
550         .recalc_rate = rockchip_rk3036_pll_recalc_rate,
551         .enable = rockchip_rk3036_pll_enable,
552         .disable = rockchip_rk3036_pll_disable,
553         .is_enabled = rockchip_rk3036_pll_is_enabled,
554 };
555
556 static const struct clk_ops rockchip_rk3036_pll_clk_ops = {
557         .recalc_rate = rockchip_rk3036_pll_recalc_rate,
558         .round_rate = rockchip_pll_round_rate,
559         .set_rate = rockchip_rk3036_pll_set_rate,
560         .enable = rockchip_rk3036_pll_enable,
561         .disable = rockchip_rk3036_pll_disable,
562         .is_enabled = rockchip_rk3036_pll_is_enabled,
563         .init = rockchip_rk3036_pll_init,
564 };
565
566 /**
567  * PLL used in RK3066, RK3188 and RK3288
568  */
569
570 #define RK3066_PLL_RESET_DELAY(nr)      ((nr * 500) / 24 + 1)
571
572 #define RK3066_PLLCON(i)                (i * 0x4)
573 #define RK3066_PLLCON0_OD_MASK          0xf
574 #define RK3066_PLLCON0_OD_SHIFT         0
575 #define RK3066_PLLCON0_NR_MASK          0x3f
576 #define RK3066_PLLCON0_NR_SHIFT         8
577 #define RK3066_PLLCON1_NF_MASK          0x1fff
578 #define RK3066_PLLCON1_NF_SHIFT         0
579 #define RK3066_PLLCON2_NB_MASK          0xfff
580 #define RK3066_PLLCON2_NB_SHIFT         0
581 #define RK3066_PLLCON3_RESET            (1 << 5)
582 #define RK3066_PLLCON3_PWRDOWN          (1 << 1)
583 #define RK3066_PLLCON3_BYPASS           (1 << 0)
584
585 static void rockchip_rk3066_pll_get_params(struct rockchip_clk_pll *pll,
586                                         struct rockchip_pll_rate_table *rate)
587 {
588         u32 pllcon;
589
590         pllcon = readl_relaxed(pll->reg_base + RK3066_PLLCON(0));
591         rate->nr = ((pllcon >> RK3066_PLLCON0_NR_SHIFT)
592                                 & RK3066_PLLCON0_NR_MASK) + 1;
593         rate->no = ((pllcon >> RK3066_PLLCON0_OD_SHIFT)
594                                 & RK3066_PLLCON0_OD_MASK) + 1;
595
596         pllcon = readl_relaxed(pll->reg_base + RK3066_PLLCON(1));
597         rate->nf = ((pllcon >> RK3066_PLLCON1_NF_SHIFT)
598                                 & RK3066_PLLCON1_NF_MASK) + 1;
599
600         pllcon = readl_relaxed(pll->reg_base + RK3066_PLLCON(2));
601         rate->nb = ((pllcon >> RK3066_PLLCON2_NB_SHIFT)
602                                 & RK3066_PLLCON2_NB_MASK) + 1;
603 }
604
605 static unsigned long rockchip_rk3066_pll_recalc_rate(struct clk_hw *hw,
606                                                      unsigned long prate)
607 {
608         struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
609         struct rockchip_pll_rate_table cur;
610         u64 rate64 = prate;
611         u32 pllcon;
612
613         pllcon = readl_relaxed(pll->reg_base + RK3066_PLLCON(3));
614         if (pllcon & RK3066_PLLCON3_BYPASS) {
615                 pr_debug("%s: pll %s is bypassed\n", __func__,
616                         clk_hw_get_name(hw));
617                 return prate;
618         }
619
620         rockchip_rk3066_pll_get_params(pll, &cur);
621
622         rate64 *= cur.nf;
623         do_div(rate64, cur.nr);
624         do_div(rate64, cur.no);
625
626         return (unsigned long)rate64;
627 }
628
629 static int rockchip_rk3066_pll_set_params(struct rockchip_clk_pll *pll,
630                                 const struct rockchip_pll_rate_table *rate)
631 {
632         const struct clk_ops *pll_mux_ops = pll->pll_mux_ops;
633         struct clk_mux *pll_mux = &pll->pll_mux;
634         struct rockchip_pll_rate_table cur;
635         int rate_change_remuxed = 0;
636         int cur_parent;
637         int ret;
638
639         pr_debug("%s: rate settings for %lu (nr, no, nf): (%d, %d, %d)\n",
640                  __func__, rate->rate, rate->nr, rate->no, rate->nf);
641
642         rockchip_rk3066_pll_get_params(pll, &cur);
643         cur.rate = 0;
644
645         cur_parent = pll_mux_ops->get_parent(&pll_mux->hw);
646         if (cur_parent == PLL_MODE_NORM) {
647                 pll_mux_ops->set_parent(&pll_mux->hw, PLL_MODE_SLOW);
648                 rate_change_remuxed = 1;
649         }
650
651         /* enter reset mode */
652         writel(HIWORD_UPDATE(RK3066_PLLCON3_RESET, RK3066_PLLCON3_RESET, 0),
653                pll->reg_base + RK3066_PLLCON(3));
654
655         /* update pll values */
656         writel(HIWORD_UPDATE(rate->nr - 1, RK3066_PLLCON0_NR_MASK,
657                                            RK3066_PLLCON0_NR_SHIFT) |
658                HIWORD_UPDATE(rate->no - 1, RK3066_PLLCON0_OD_MASK,
659                                            RK3066_PLLCON0_OD_SHIFT),
660                pll->reg_base + RK3066_PLLCON(0));
661
662         writel_relaxed(HIWORD_UPDATE(rate->nf - 1, RK3066_PLLCON1_NF_MASK,
663                                                    RK3066_PLLCON1_NF_SHIFT),
664                        pll->reg_base + RK3066_PLLCON(1));
665         writel_relaxed(HIWORD_UPDATE(rate->nb - 1, RK3066_PLLCON2_NB_MASK,
666                                                    RK3066_PLLCON2_NB_SHIFT),
667                        pll->reg_base + RK3066_PLLCON(2));
668
669         /* leave reset and wait the reset_delay */
670         writel(HIWORD_UPDATE(0, RK3066_PLLCON3_RESET, 0),
671                pll->reg_base + RK3066_PLLCON(3));
672         udelay(RK3066_PLL_RESET_DELAY(rate->nr));
673
674         /* wait for the pll to lock */
675         ret = rockchip_pll_wait_lock(pll);
676         if (ret) {
677                 pr_warn("%s: pll update unsucessful, trying to restore old params\n",
678                         __func__);
679                 rockchip_rk3066_pll_set_params(pll, &cur);
680         }
681
682         if (rate_change_remuxed)
683                 pll_mux_ops->set_parent(&pll_mux->hw, PLL_MODE_NORM);
684
685         return ret;
686 }
687
688 static int rockchip_rk3066_pll_set_rate(struct clk_hw *hw, unsigned long drate,
689                                         unsigned long prate)
690 {
691         struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
692         const struct rockchip_pll_rate_table *rate;
693         unsigned long old_rate = rockchip_rk3066_pll_recalc_rate(hw, prate);
694         struct regmap *grf = rockchip_clk_get_grf(pll->ctx);
695
696         if (IS_ERR(grf)) {
697                 pr_debug("%s: grf regmap not available, aborting rate change\n",
698                          __func__);
699                 return PTR_ERR(grf);
700         }
701
702         pr_debug("%s: changing %s from %lu to %lu with a parent rate of %lu\n",
703                  __func__, clk_hw_get_name(hw), old_rate, drate, prate);
704
705         /* Get required rate settings from table */
706         rate = rockchip_get_pll_settings(pll, drate);
707         if (!rate) {
708                 pr_err("%s: Invalid rate : %lu for pll clk %s\n", __func__,
709                         drate, clk_hw_get_name(hw));
710                 return -EINVAL;
711         }
712
713         return rockchip_rk3066_pll_set_params(pll, rate);
714 }
715
716 static int rockchip_rk3066_pll_enable(struct clk_hw *hw)
717 {
718         struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
719
720         writel(HIWORD_UPDATE(0, RK3066_PLLCON3_PWRDOWN, 0),
721                pll->reg_base + RK3066_PLLCON(3));
722         rockchip_pll_wait_lock(pll);
723
724         return 0;
725 }
726
727 static void rockchip_rk3066_pll_disable(struct clk_hw *hw)
728 {
729         struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
730
731         writel(HIWORD_UPDATE(RK3066_PLLCON3_PWRDOWN,
732                              RK3066_PLLCON3_PWRDOWN, 0),
733                pll->reg_base + RK3066_PLLCON(3));
734 }
735
736 static int rockchip_rk3066_pll_is_enabled(struct clk_hw *hw)
737 {
738         struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
739         u32 pllcon = readl(pll->reg_base + RK3066_PLLCON(3));
740
741         return !(pllcon & RK3066_PLLCON3_PWRDOWN);
742 }
743
744 static void rockchip_rk3066_pll_init(struct clk_hw *hw)
745 {
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;
749         unsigned long drate;
750
751         if (!(pll->flags & ROCKCHIP_PLL_SYNC_RATE))
752                 return;
753
754         drate = clk_hw_get_rate(hw);
755         rate = rockchip_get_pll_settings(pll, drate);
756
757         /* when no rate setting for the current rate, rely on clk_set_rate */
758         if (!rate)
759                 return;
760
761         rockchip_rk3066_pll_get_params(pll, &cur);
762
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);
769
770                 if (IS_ERR(grf))
771                         return;
772
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);
776         }
777 }
778
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,
784 };
785
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,
794 };
795
796 /**
797  * PLL used in RK3366
798  */
799
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
813
814 #define RK3366_PLLCON3_PWRDOWN                  (1 << 0)
815
816 static void rockchip_rk3366_pll_get_params(struct rockchip_clk_pll *pll,
817                                         struct rockchip_pll_rate_table *rate)
818 {
819         u32 pllcon;
820
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);
826
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);
832
833         pllcon = readl_relaxed(pll->reg_base + RK3366_PLLCON(2));
834         rate->frac = ((pllcon >> RK3366_PLLCON2_FRAC_SHIFT)
835                                 & RK3366_PLLCON2_FRAC_MASK);
836
837         pllcon = readl_relaxed(pll->reg_base + RK3366_PLLCON(3));
838         rate->dsmpd = ((pllcon >> RK3366_PLLCON3_DSMPD_SHIFT)
839                                 & RK3366_PLLCON3_DSMPD_MASK);
840 }
841
842 static int rockchip_rk3366_pll_set_params(struct rockchip_clk_pll *pll,
843                                 const struct rockchip_pll_rate_table *rate)
844 {
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;
848         u32 pllcon;
849         int rate_change_remuxed = 0;
850         int cur_parent;
851         int ret;
852
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);
856
857         rockchip_rk3366_pll_get_params(pll, &cur);
858         cur.rate = 0;
859
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;
864         }
865
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));
872
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));
878
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));
884
885         writel_relaxed(HIWORD_UPDATE(rate->dsmpd, RK3366_PLLCON3_DSMPD_MASK,
886                                                   RK3366_PLLCON3_DSMPD_SHIFT),
887                        pll->reg_base + RK3366_PLLCON(3));
888
889         /* wait for the pll to lock */
890         ret = rockchip_pll_wait_lock(pll);
891         if (ret) {
892                 pr_warn("%s: pll update unsucessful, trying to restore old params\n",
893                         __func__);
894                 rockchip_rk3366_pll_set_params(pll, &cur);
895         }
896
897         if (rate_change_remuxed)
898                 pll_mux_ops->set_parent(&pll_mux->hw, PLL_MODE_NORM);
899
900         return ret;
901 }
902
903 static int rockchip_rk3366_pll_enable(struct clk_hw *hw)
904 {
905         struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
906
907         writel(HIWORD_UPDATE(0, RK3366_PLLCON3_PWRDOWN, 0),
908                pll->reg_base + RK3366_PLLCON(3));
909
910         return 0;
911 }
912
913 static void rockchip_rk3366_pll_disable(struct clk_hw *hw)
914 {
915         struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
916
917         writel(HIWORD_UPDATE(RK3366_PLLCON3_PWRDOWN,
918                              RK3366_PLLCON3_PWRDOWN, 0),
919                pll->reg_base + RK3366_PLLCON(3));
920 }
921
922 static int rockchip_rk3366_pll_is_enabled(struct clk_hw *hw)
923 {
924         struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
925         u32 pllcon = readl(pll->reg_base + RK3366_PLLCON(3));
926
927         return !(pllcon & RK3366_PLLCON3_PWRDOWN);
928 }
929
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,
935 };
936
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,
945 };
946
947 /**
948  * PLL used in RK3399
949  */
950
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
966
967 static int rockchip_rk3399_pll_wait_lock(struct rockchip_clk_pll *pll)
968 {
969         u32 pllcon;
970         int delay = 24000000;
971
972         /* poll check the lock status in rk3399 xPLLCON2 */
973         while (delay > 0) {
974                 pllcon = readl_relaxed(pll->reg_base + RK3399_PLLCON(2));
975                 if (pllcon & RK3399_PLLCON2_LOCK_STATUS)
976                         return 0;
977
978                 delay--;
979         }
980
981         pr_err("%s: timeout waiting for pll to lock\n", __func__);
982         return -ETIMEDOUT;
983 }
984
985 static void rockchip_rk3399_pll_get_params(struct rockchip_clk_pll *pll,
986                                         struct rockchip_pll_rate_table *rate)
987 {
988         u32 pllcon;
989
990         pllcon = readl_relaxed(pll->reg_base + RK3399_PLLCON(0));
991         rate->fbdiv = ((pllcon >> RK3399_PLLCON0_FBDIV_SHIFT)
992                                 & RK3399_PLLCON0_FBDIV_MASK);
993
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);
1001
1002         pllcon = readl_relaxed(pll->reg_base + RK3399_PLLCON(2));
1003         rate->frac = ((pllcon >> RK3399_PLLCON2_FRAC_SHIFT)
1004                                 & RK3399_PLLCON2_FRAC_MASK);
1005
1006         pllcon = readl_relaxed(pll->reg_base + RK3399_PLLCON(3));
1007         rate->dsmpd = ((pllcon >> RK3399_PLLCON3_DSMPD_SHIFT)
1008                                 & RK3399_PLLCON3_DSMPD_MASK);
1009 }
1010
1011 static unsigned long rockchip_rk3399_pll_recalc_rate(struct clk_hw *hw,
1012                                                      unsigned long prate)
1013 {
1014         struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
1015         struct rockchip_pll_rate_table cur;
1016         u64 rate64 = prate;
1017
1018         rockchip_rk3399_pll_get_params(pll, &cur);
1019
1020         rate64 *= cur.fbdiv;
1021         do_div(rate64, cur.refdiv);
1022
1023         if (cur.dsmpd == 0) {
1024                 /* fractional mode */
1025                 u64 frac_rate64 = prate * cur.frac;
1026
1027                 do_div(frac_rate64, cur.refdiv);
1028                 rate64 += frac_rate64 >> 24;
1029         }
1030
1031         do_div(rate64, cur.postdiv1);
1032         do_div(rate64, cur.postdiv2);
1033
1034         return (unsigned long)rate64;
1035 }
1036
1037 static int rockchip_rk3399_pll_set_params(struct rockchip_clk_pll *pll,
1038                                 const struct rockchip_pll_rate_table *rate)
1039 {
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;
1043         u32 pllcon;
1044         int rate_change_remuxed = 0;
1045         int cur_parent;
1046         int ret;
1047
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);
1051
1052         rockchip_rk3399_pll_get_params(pll, &cur);
1053         cur.rate = 0;
1054
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;
1059         }
1060
1061         /* set pll power down */
1062         writel(HIWORD_UPDATE(RK3399_PLLCON3_PWRDOWN,
1063                              RK3399_PLLCON3_PWRDOWN, 0),
1064                pll->reg_base + RK3399_PLLCON(3));
1065
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));
1070
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));
1078
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));
1084
1085         writel_relaxed(HIWORD_UPDATE(rate->dsmpd, RK3399_PLLCON3_DSMPD_MASK,
1086                                             RK3399_PLLCON3_DSMPD_SHIFT),
1087                        pll->reg_base + RK3399_PLLCON(3));
1088
1089         /* set pll power up */
1090         writel(HIWORD_UPDATE(0,
1091                              RK3399_PLLCON3_PWRDOWN, 0),
1092                pll->reg_base + RK3399_PLLCON(3));
1093
1094         /* wait for the pll to lock */
1095         ret = rockchip_rk3399_pll_wait_lock(pll);
1096         if (ret) {
1097                 pr_warn("%s: pll update unsucessful, trying to restore old params\n",
1098                         __func__);
1099                 rockchip_rk3399_pll_set_params(pll, &cur);
1100         }
1101
1102         if (rate_change_remuxed)
1103                 pll_mux_ops->set_parent(&pll_mux->hw, PLL_MODE_NORM);
1104
1105         return ret;
1106 }
1107
1108 static int rockchip_rk3399_pll_set_rate(struct clk_hw *hw, unsigned long drate,
1109                                         unsigned long prate)
1110 {
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);
1114
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);
1117
1118         /* Get required rate settings from table */
1119         rate = rockchip_get_pll_settings(pll, drate);
1120         if (!rate) {
1121                 pr_err("%s: Invalid rate : %lu for pll clk %s\n", __func__,
1122                         drate, __clk_get_name(hw->clk));
1123                 return -EINVAL;
1124         }
1125
1126         return rockchip_rk3399_pll_set_params(pll, rate);
1127 }
1128
1129 static int rockchip_rk3399_pll_enable(struct clk_hw *hw)
1130 {
1131         struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
1132
1133         writel(HIWORD_UPDATE(0, RK3399_PLLCON3_PWRDOWN, 0),
1134                pll->reg_base + RK3399_PLLCON(3));
1135         rockchip_rk3399_pll_wait_lock(pll);
1136
1137         return 0;
1138 }
1139
1140 static void rockchip_rk3399_pll_disable(struct clk_hw *hw)
1141 {
1142         struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
1143
1144         writel(HIWORD_UPDATE(RK3399_PLLCON3_PWRDOWN,
1145                              RK3399_PLLCON3_PWRDOWN, 0),
1146                pll->reg_base + RK3399_PLLCON(3));
1147 }
1148
1149 static int rockchip_rk3399_pll_is_enabled(struct clk_hw *hw)
1150 {
1151         struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
1152         u32 pllcon = readl(pll->reg_base + RK3399_PLLCON(3));
1153
1154         return !(pllcon & RK3399_PLLCON3_PWRDOWN);
1155 }
1156
1157 static void rockchip_rk3399_pll_init(struct clk_hw *hw)
1158 {
1159         struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
1160         const struct rockchip_pll_rate_table *rate;
1161         struct rockchip_pll_rate_table cur;
1162         unsigned long drate;
1163
1164         if (!(pll->flags & ROCKCHIP_PLL_SYNC_RATE))
1165                 return;
1166
1167         drate = clk_hw_get_rate(hw);
1168         rate = rockchip_get_pll_settings(pll, drate);
1169
1170         /* when no rate setting for the current rate, rely on clk_set_rate */
1171         if (!rate)
1172                 return;
1173
1174         rockchip_rk3399_pll_get_params(pll, &cur);
1175
1176         pr_debug("%s: pll %s@%lu: Hz\n", __func__, __clk_get_name(hw->clk),
1177                  drate);
1178         pr_debug("old - fbdiv: %d, postdiv1: %d, refdiv: %d, postdiv2: %d, dsmpd: %d, frac: %d\n",
1179                  cur.fbdiv, cur.postdiv1, cur.refdiv, cur.postdiv2,
1180                  cur.dsmpd, cur.frac);
1181         pr_debug("new - fbdiv: %d, postdiv1: %d, refdiv: %d, postdiv2: %d, dsmpd: %d, frac: %d\n",
1182                  rate->fbdiv, rate->postdiv1, rate->refdiv, rate->postdiv2,
1183                  rate->dsmpd, rate->frac);
1184
1185         if (rate->fbdiv != cur.fbdiv || rate->postdiv1 != cur.postdiv1 ||
1186                 rate->refdiv != cur.refdiv || rate->postdiv2 != cur.postdiv2 ||
1187                 rate->dsmpd != cur.dsmpd || rate->frac != cur.frac) {
1188                 struct clk *parent = clk_get_parent(hw->clk);
1189
1190                 if (!parent) {
1191                         pr_warn("%s: parent of %s not available\n",
1192                                 __func__, __clk_get_name(hw->clk));
1193                         return;
1194                 }
1195
1196                 pr_debug("%s: pll %s: rate params do not match rate table, adjusting\n",
1197                          __func__, __clk_get_name(hw->clk));
1198                 rockchip_rk3399_pll_set_params(pll, rate);
1199         }
1200 }
1201
1202 static const struct clk_ops rockchip_rk3399_pll_clk_norate_ops = {
1203         .recalc_rate = rockchip_rk3399_pll_recalc_rate,
1204         .enable = rockchip_rk3399_pll_enable,
1205         .disable = rockchip_rk3399_pll_disable,
1206         .is_enabled = rockchip_rk3399_pll_is_enabled,
1207 };
1208
1209 static const struct clk_ops rockchip_rk3399_pll_clk_ops = {
1210         .recalc_rate = rockchip_rk3399_pll_recalc_rate,
1211         .round_rate = rockchip_pll_round_rate,
1212         .set_rate = rockchip_rk3399_pll_set_rate,
1213         .enable = rockchip_rk3399_pll_enable,
1214         .disable = rockchip_rk3399_pll_disable,
1215         .is_enabled = rockchip_rk3399_pll_is_enabled,
1216         .init = rockchip_rk3399_pll_init,
1217 };
1218
1219 /*
1220  * Common registering of pll clocks
1221  */
1222
1223 struct clk *rockchip_clk_register_pll(struct rockchip_clk_provider *ctx,
1224                 enum rockchip_pll_type pll_type,
1225                 const char *name, const char *const *parent_names,
1226                 u8 num_parents, int con_offset, int grf_lock_offset,
1227                 int lock_shift, int mode_offset, int mode_shift,
1228                 struct rockchip_pll_rate_table *rate_table,
1229                 unsigned long flags, u8 clk_pll_flags)
1230 {
1231         const char *pll_parents[3];
1232         struct clk_init_data init;
1233         struct rockchip_clk_pll *pll;
1234         struct clk_mux *pll_mux;
1235         struct clk *pll_clk, *mux_clk;
1236         char pll_name[20];
1237
1238         if ((pll_type != pll_rk3328 && num_parents != 2) ||
1239             (pll_type == pll_rk3328 && num_parents != 1)) {
1240                 pr_err("%s: needs two parent clocks\n", __func__);
1241                 return ERR_PTR(-EINVAL);
1242         }
1243
1244         /* name the actual pll */
1245         snprintf(pll_name, sizeof(pll_name), "pll_%s", name);
1246
1247         pll = kzalloc(sizeof(*pll), GFP_KERNEL);
1248         if (!pll)
1249                 return ERR_PTR(-ENOMEM);
1250
1251         /* create the mux on top of the real pll */
1252         pll->pll_mux_ops = &clk_mux_ops;
1253         pll_mux = &pll->pll_mux;
1254         pll_mux->reg = ctx->reg_base + mode_offset;
1255         pll_mux->shift = mode_shift;
1256         if (pll_type == pll_rk3328)
1257                 pll_mux->mask = PLL_RK3328_MODE_MASK;
1258         else
1259                 pll_mux->mask = PLL_MODE_MASK;
1260         pll_mux->flags = 0;
1261         pll_mux->lock = &ctx->lock;
1262         pll_mux->hw.init = &init;
1263
1264         if (pll_type == pll_rk3036 ||
1265             pll_type == pll_rk3066 ||
1266             pll_type == pll_rk3328 ||
1267             pll_type == pll_rk3366 ||
1268             pll_type == pll_rk3399)
1269                 pll_mux->flags |= CLK_MUX_HIWORD_MASK;
1270
1271         /* the actual muxing is xin24m, pll-output, xin32k */
1272         pll_parents[0] = parent_names[0];
1273         pll_parents[1] = pll_name;
1274         pll_parents[2] = parent_names[1];
1275
1276         init.name = name;
1277         init.flags = CLK_SET_RATE_PARENT;
1278         init.ops = pll->pll_mux_ops;
1279         init.parent_names = pll_parents;
1280         if (pll_type == pll_rk3328)
1281                 init.num_parents = 2;
1282         else
1283                 init.num_parents = ARRAY_SIZE(pll_parents);
1284
1285         mux_clk = clk_register(NULL, &pll_mux->hw);
1286         if (IS_ERR(mux_clk))
1287                 goto err_mux;
1288
1289         /* now create the actual pll */
1290         init.name = pll_name;
1291
1292         /* keep all plls untouched for now */
1293         init.flags = flags | CLK_IGNORE_UNUSED;
1294
1295         init.parent_names = &parent_names[0];
1296         init.num_parents = 1;
1297
1298         if (rate_table) {
1299                 int len;
1300
1301                 /* find count of rates in rate_table */
1302                 for (len = 0; rate_table[len].rate != 0; )
1303                         len++;
1304
1305                 pll->rate_count = len;
1306                 pll->rate_table = kmemdup(rate_table,
1307                                         pll->rate_count *
1308                                         sizeof(struct rockchip_pll_rate_table),
1309                                         GFP_KERNEL);
1310                 WARN(!pll->rate_table,
1311                         "%s: could not allocate rate table for %s\n",
1312                         __func__, name);
1313         }
1314
1315         switch (pll_type) {
1316         case pll_rk3036:
1317         case pll_rk3328:
1318                 if (!pll->rate_table)
1319                         init.ops = &rockchip_rk3036_pll_clk_norate_ops;
1320                 else
1321                         init.ops = &rockchip_rk3036_pll_clk_ops;
1322                 break;
1323         case pll_rk3066:
1324                 if (!pll->rate_table)
1325                         init.ops = &rockchip_rk3066_pll_clk_norate_ops;
1326                 else
1327                         init.ops = &rockchip_rk3066_pll_clk_ops;
1328                 break;
1329         case pll_rk3366:
1330                 if (!pll->rate_table)
1331                         init.ops = &rockchip_rk3366_pll_clk_norate_ops;
1332                 else
1333                         init.ops = &rockchip_rk3366_pll_clk_ops;
1334                 break;
1335         case pll_rk3399:
1336                 if (!pll->rate_table)
1337                         init.ops = &rockchip_rk3399_pll_clk_norate_ops;
1338                 else
1339                         init.ops = &rockchip_rk3399_pll_clk_ops;
1340                 break;
1341         default:
1342                 pr_warn("%s: Unknown pll type for pll clk %s\n",
1343                         __func__, name);
1344         }
1345
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;
1353         pll->ctx = ctx;
1354
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));
1359                 goto err_pll;
1360         }
1361
1362         return mux_clk;
1363
1364 err_pll:
1365         clk_unregister(mux_clk);
1366         mux_clk = pll_clk;
1367 err_mux:
1368         kfree(pll);
1369         return mux_clk;
1370 }