blob: 27be66a2a358ec2a22ac343483058bb9af66a70f [file] [log] [blame]
Heiko Stübner90c59022014-07-03 01:59:10 +02001/*
2 * Copyright (c) 2014 MundoReader S.L.
3 * Author: Heiko Stuebner <heiko@sntech.de>
4 *
Xing Zheng9c4d6e552015-11-05 15:33:57 +08005 * Copyright (c) 2015 Rockchip Electronics Co. Ltd.
6 * Author: Xing Zheng <zhengxing@rock-chips.com>
7 *
Heiko Stübner90c59022014-07-03 01:59:10 +02008 * 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>
Heiko Stübner90c59022014-07-03 01:59:10 +020023#include <linux/clk-provider.h>
24#include <linux/regmap.h>
Xing Zheng9c4d6e552015-11-05 15:33:57 +080025#include <linux/clk.h>
Heiko Stübner90c59022014-07-03 01:59:10 +020026#include "clk.h"
27
28#define PLL_MODE_MASK 0x3
29#define PLL_MODE_SLOW 0x0
30#define PLL_MODE_NORM 0x1
31#define PLL_MODE_DEEP 0x2
32
33struct rockchip_clk_pll {
34 struct clk_hw hw;
35
36 struct clk_mux pll_mux;
37 const struct clk_ops *pll_mux_ops;
38
39 struct notifier_block clk_nb;
Heiko Stübner90c59022014-07-03 01:59:10 +020040
41 void __iomem *reg_base;
42 int lock_offset;
43 unsigned int lock_shift;
44 enum rockchip_pll_type type;
Heiko Stuebner4f8a7c52014-11-20 20:38:50 +010045 u8 flags;
Heiko Stübner90c59022014-07-03 01:59:10 +020046 const struct rockchip_pll_rate_table *rate_table;
47 unsigned int rate_count;
48 spinlock_t *lock;
Xing Zhengef1d9fe2016-03-09 10:37:04 +080049
50 struct rockchip_clk_provider *ctx;
Heiko Stübner90c59022014-07-03 01:59:10 +020051};
52
53#define to_rockchip_clk_pll(_hw) container_of(_hw, struct rockchip_clk_pll, hw)
54#define to_rockchip_clk_pll_nb(nb) \
55 container_of(nb, struct rockchip_clk_pll, clk_nb)
56
57static const struct rockchip_pll_rate_table *rockchip_get_pll_settings(
58 struct rockchip_clk_pll *pll, unsigned long rate)
59{
60 const struct rockchip_pll_rate_table *rate_table = pll->rate_table;
61 int i;
62
63 for (i = 0; i < pll->rate_count; i++) {
64 if (rate == rate_table[i].rate)
65 return &rate_table[i];
66 }
67
68 return NULL;
69}
70
71static long rockchip_pll_round_rate(struct clk_hw *hw,
72 unsigned long drate, unsigned long *prate)
73{
74 struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
75 const struct rockchip_pll_rate_table *rate_table = pll->rate_table;
76 int i;
77
78 /* Assumming rate_table is in descending order */
79 for (i = 0; i < pll->rate_count; i++) {
80 if (drate >= rate_table[i].rate)
81 return rate_table[i].rate;
82 }
83
84 /* return minimum supported value */
85 return rate_table[i - 1].rate;
86}
87
88/*
89 * Wait for the pll to reach the locked state.
90 * The calling set_rate function is responsible for making sure the
91 * grf regmap is available.
92 */
93static int rockchip_pll_wait_lock(struct rockchip_clk_pll *pll)
94{
Xing Zhengef1d9fe2016-03-09 10:37:04 +080095 struct regmap *grf = rockchip_clk_get_grf(pll->ctx);
Heiko Stübner90c59022014-07-03 01:59:10 +020096 unsigned int val;
97 int delay = 24000000, ret;
98
Shawn Lineb4e10c2016-02-15 11:33:25 +080099 if (IS_ERR(grf)) {
100 pr_err("%s: grf regmap not available\n", __func__);
101 return PTR_ERR(grf);
102 }
103
Heiko Stübner90c59022014-07-03 01:59:10 +0200104 while (delay > 0) {
105 ret = regmap_read(grf, pll->lock_offset, &val);
106 if (ret) {
107 pr_err("%s: failed to read pll lock status: %d\n",
108 __func__, ret);
109 return ret;
110 }
111
112 if (val & BIT(pll->lock_shift))
113 return 0;
114 delay--;
115 }
116
117 pr_err("%s: timeout waiting for pll to lock\n", __func__);
118 return -ETIMEDOUT;
119}
120
121/**
Xing Zheng9c4d6e552015-11-05 15:33:57 +0800122 * PLL used in RK3036
123 */
124
125#define RK3036_PLLCON(i) (i * 0x4)
126#define RK3036_PLLCON0_FBDIV_MASK 0xfff
127#define RK3036_PLLCON0_FBDIV_SHIFT 0
128#define RK3036_PLLCON0_POSTDIV1_MASK 0x7
129#define RK3036_PLLCON0_POSTDIV1_SHIFT 12
130#define RK3036_PLLCON1_REFDIV_MASK 0x3f
131#define RK3036_PLLCON1_REFDIV_SHIFT 0
132#define RK3036_PLLCON1_POSTDIV2_MASK 0x7
133#define RK3036_PLLCON1_POSTDIV2_SHIFT 6
134#define RK3036_PLLCON1_DSMPD_MASK 0x1
135#define RK3036_PLLCON1_DSMPD_SHIFT 12
136#define RK3036_PLLCON2_FRAC_MASK 0xffffff
137#define RK3036_PLLCON2_FRAC_SHIFT 0
138
139#define RK3036_PLLCON1_PWRDOWN (1 << 13)
140
141static void rockchip_rk3036_pll_get_params(struct rockchip_clk_pll *pll,
142 struct rockchip_pll_rate_table *rate)
143{
144 u32 pllcon;
145
146 pllcon = readl_relaxed(pll->reg_base + RK3036_PLLCON(0));
147 rate->fbdiv = ((pllcon >> RK3036_PLLCON0_FBDIV_SHIFT)
148 & RK3036_PLLCON0_FBDIV_MASK);
149 rate->postdiv1 = ((pllcon >> RK3036_PLLCON0_POSTDIV1_SHIFT)
150 & RK3036_PLLCON0_POSTDIV1_MASK);
151
152 pllcon = readl_relaxed(pll->reg_base + RK3036_PLLCON(1));
153 rate->refdiv = ((pllcon >> RK3036_PLLCON1_REFDIV_SHIFT)
154 & RK3036_PLLCON1_REFDIV_MASK);
155 rate->postdiv2 = ((pllcon >> RK3036_PLLCON1_POSTDIV2_SHIFT)
156 & RK3036_PLLCON1_POSTDIV2_MASK);
157 rate->dsmpd = ((pllcon >> RK3036_PLLCON1_DSMPD_SHIFT)
158 & RK3036_PLLCON1_DSMPD_MASK);
159
160 pllcon = readl_relaxed(pll->reg_base + RK3036_PLLCON(2));
161 rate->frac = ((pllcon >> RK3036_PLLCON2_FRAC_SHIFT)
162 & RK3036_PLLCON2_FRAC_MASK);
163}
164
165static unsigned long rockchip_rk3036_pll_recalc_rate(struct clk_hw *hw,
166 unsigned long prate)
167{
168 struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
169 struct rockchip_pll_rate_table cur;
170 u64 rate64 = prate;
171
172 rockchip_rk3036_pll_get_params(pll, &cur);
173
174 rate64 *= cur.fbdiv;
175 do_div(rate64, cur.refdiv);
176
177 if (cur.dsmpd == 0) {
178 /* fractional mode */
179 u64 frac_rate64 = prate * cur.frac;
180
181 do_div(frac_rate64, cur.refdiv);
182 rate64 += frac_rate64 >> 24;
183 }
184
185 do_div(rate64, cur.postdiv1);
186 do_div(rate64, cur.postdiv2);
187
188 return (unsigned long)rate64;
189}
190
191static int rockchip_rk3036_pll_set_params(struct rockchip_clk_pll *pll,
192 const struct rockchip_pll_rate_table *rate)
193{
194 const struct clk_ops *pll_mux_ops = pll->pll_mux_ops;
195 struct clk_mux *pll_mux = &pll->pll_mux;
196 struct rockchip_pll_rate_table cur;
197 u32 pllcon;
198 int rate_change_remuxed = 0;
199 int cur_parent;
200 int ret;
201
202 pr_debug("%s: rate settings for %lu fbdiv: %d, postdiv1: %d, refdiv: %d, postdiv2: %d, dsmpd: %d, frac: %d\n",
203 __func__, rate->rate, rate->fbdiv, rate->postdiv1, rate->refdiv,
204 rate->postdiv2, rate->dsmpd, rate->frac);
205
206 rockchip_rk3036_pll_get_params(pll, &cur);
207 cur.rate = 0;
208
209 cur_parent = pll_mux_ops->get_parent(&pll_mux->hw);
210 if (cur_parent == PLL_MODE_NORM) {
211 pll_mux_ops->set_parent(&pll_mux->hw, PLL_MODE_SLOW);
212 rate_change_remuxed = 1;
213 }
214
215 /* update pll values */
216 writel_relaxed(HIWORD_UPDATE(rate->fbdiv, RK3036_PLLCON0_FBDIV_MASK,
217 RK3036_PLLCON0_FBDIV_SHIFT) |
218 HIWORD_UPDATE(rate->postdiv1, RK3036_PLLCON0_POSTDIV1_MASK,
219 RK3036_PLLCON0_POSTDIV1_SHIFT),
220 pll->reg_base + RK3036_PLLCON(0));
221
222 writel_relaxed(HIWORD_UPDATE(rate->refdiv, RK3036_PLLCON1_REFDIV_MASK,
223 RK3036_PLLCON1_REFDIV_SHIFT) |
224 HIWORD_UPDATE(rate->postdiv2, RK3036_PLLCON1_POSTDIV2_MASK,
225 RK3036_PLLCON1_POSTDIV2_SHIFT) |
226 HIWORD_UPDATE(rate->dsmpd, RK3036_PLLCON1_DSMPD_MASK,
227 RK3036_PLLCON1_DSMPD_SHIFT),
228 pll->reg_base + RK3036_PLLCON(1));
229
230 /* GPLL CON2 is not HIWORD_MASK */
231 pllcon = readl_relaxed(pll->reg_base + RK3036_PLLCON(2));
232 pllcon &= ~(RK3036_PLLCON2_FRAC_MASK << RK3036_PLLCON2_FRAC_SHIFT);
233 pllcon |= rate->frac << RK3036_PLLCON2_FRAC_SHIFT;
234 writel_relaxed(pllcon, pll->reg_base + RK3036_PLLCON(2));
235
236 /* wait for the pll to lock */
237 ret = rockchip_pll_wait_lock(pll);
238 if (ret) {
239 pr_warn("%s: pll update unsucessful, trying to restore old params\n",
240 __func__);
241 rockchip_rk3036_pll_set_params(pll, &cur);
242 }
243
244 if (rate_change_remuxed)
245 pll_mux_ops->set_parent(&pll_mux->hw, PLL_MODE_NORM);
246
247 return ret;
248}
249
250static int rockchip_rk3036_pll_set_rate(struct clk_hw *hw, unsigned long drate,
251 unsigned long prate)
252{
253 struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
254 const struct rockchip_pll_rate_table *rate;
255 unsigned long old_rate = rockchip_rk3036_pll_recalc_rate(hw, prate);
Xing Zhengef1d9fe2016-03-09 10:37:04 +0800256 struct regmap *grf = rockchip_clk_get_grf(pll->ctx);
Xing Zheng9c4d6e552015-11-05 15:33:57 +0800257
258 if (IS_ERR(grf)) {
259 pr_debug("%s: grf regmap not available, aborting rate change\n",
260 __func__);
261 return PTR_ERR(grf);
262 }
263
264 pr_debug("%s: changing %s from %lu to %lu with a parent rate of %lu\n",
265 __func__, __clk_get_name(hw->clk), old_rate, drate, prate);
266
267 /* Get required rate settings from table */
268 rate = rockchip_get_pll_settings(pll, drate);
269 if (!rate) {
270 pr_err("%s: Invalid rate : %lu for pll clk %s\n", __func__,
271 drate, __clk_get_name(hw->clk));
272 return -EINVAL;
273 }
274
275 return rockchip_rk3036_pll_set_params(pll, rate);
276}
277
278static int rockchip_rk3036_pll_enable(struct clk_hw *hw)
279{
280 struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
281
282 writel(HIWORD_UPDATE(0, RK3036_PLLCON1_PWRDOWN, 0),
283 pll->reg_base + RK3036_PLLCON(1));
284
285 return 0;
286}
287
288static void rockchip_rk3036_pll_disable(struct clk_hw *hw)
289{
290 struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
291
292 writel(HIWORD_UPDATE(RK3036_PLLCON1_PWRDOWN,
293 RK3036_PLLCON1_PWRDOWN, 0),
294 pll->reg_base + RK3036_PLLCON(1));
295}
296
297static int rockchip_rk3036_pll_is_enabled(struct clk_hw *hw)
298{
299 struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
300 u32 pllcon = readl(pll->reg_base + RK3036_PLLCON(1));
301
302 return !(pllcon & RK3036_PLLCON1_PWRDOWN);
303}
304
305static void rockchip_rk3036_pll_init(struct clk_hw *hw)
306{
307 struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
308 const struct rockchip_pll_rate_table *rate;
309 struct rockchip_pll_rate_table cur;
310 unsigned long drate;
311
312 if (!(pll->flags & ROCKCHIP_PLL_SYNC_RATE))
313 return;
314
315 drate = clk_hw_get_rate(hw);
316 rate = rockchip_get_pll_settings(pll, drate);
317
318 /* when no rate setting for the current rate, rely on clk_set_rate */
319 if (!rate)
320 return;
321
322 rockchip_rk3036_pll_get_params(pll, &cur);
323
324 pr_debug("%s: pll %s@%lu: Hz\n", __func__, __clk_get_name(hw->clk),
325 drate);
326 pr_debug("old - fbdiv: %d, postdiv1: %d, refdiv: %d, postdiv2: %d, dsmpd: %d, frac: %d\n",
327 cur.fbdiv, cur.postdiv1, cur.refdiv, cur.postdiv2,
328 cur.dsmpd, cur.frac);
329 pr_debug("new - fbdiv: %d, postdiv1: %d, refdiv: %d, postdiv2: %d, dsmpd: %d, frac: %d\n",
330 rate->fbdiv, rate->postdiv1, rate->refdiv, rate->postdiv2,
331 rate->dsmpd, rate->frac);
332
333 if (rate->fbdiv != cur.fbdiv || rate->postdiv1 != cur.postdiv1 ||
334 rate->refdiv != cur.refdiv || rate->postdiv2 != cur.postdiv2 ||
335 rate->dsmpd != cur.dsmpd || rate->frac != cur.frac) {
336 struct clk *parent = clk_get_parent(hw->clk);
337
338 if (!parent) {
339 pr_warn("%s: parent of %s not available\n",
340 __func__, __clk_get_name(hw->clk));
341 return;
342 }
343
344 pr_debug("%s: pll %s: rate params do not match rate table, adjusting\n",
345 __func__, __clk_get_name(hw->clk));
346 rockchip_rk3036_pll_set_params(pll, rate);
347 }
348}
349
350static const struct clk_ops rockchip_rk3036_pll_clk_norate_ops = {
351 .recalc_rate = rockchip_rk3036_pll_recalc_rate,
352 .enable = rockchip_rk3036_pll_enable,
353 .disable = rockchip_rk3036_pll_disable,
354 .is_enabled = rockchip_rk3036_pll_is_enabled,
355};
356
357static const struct clk_ops rockchip_rk3036_pll_clk_ops = {
358 .recalc_rate = rockchip_rk3036_pll_recalc_rate,
359 .round_rate = rockchip_pll_round_rate,
360 .set_rate = rockchip_rk3036_pll_set_rate,
361 .enable = rockchip_rk3036_pll_enable,
362 .disable = rockchip_rk3036_pll_disable,
363 .is_enabled = rockchip_rk3036_pll_is_enabled,
364 .init = rockchip_rk3036_pll_init,
365};
366
367/**
Heiko Stübner90c59022014-07-03 01:59:10 +0200368 * PLL used in RK3066, RK3188 and RK3288
369 */
370
371#define RK3066_PLL_RESET_DELAY(nr) ((nr * 500) / 24 + 1)
372
373#define RK3066_PLLCON(i) (i * 0x4)
374#define RK3066_PLLCON0_OD_MASK 0xf
375#define RK3066_PLLCON0_OD_SHIFT 0
376#define RK3066_PLLCON0_NR_MASK 0x3f
377#define RK3066_PLLCON0_NR_SHIFT 8
378#define RK3066_PLLCON1_NF_MASK 0x1fff
379#define RK3066_PLLCON1_NF_SHIFT 0
Douglas Anderson2bbfe002015-07-21 13:41:23 -0700380#define RK3066_PLLCON2_NB_MASK 0xfff
381#define RK3066_PLLCON2_NB_SHIFT 0
Heiko Stübner90c59022014-07-03 01:59:10 +0200382#define RK3066_PLLCON3_RESET (1 << 5)
383#define RK3066_PLLCON3_PWRDOWN (1 << 1)
384#define RK3066_PLLCON3_BYPASS (1 << 0)
385
Heiko Stübner8334c0e2015-10-01 11:38:35 +0200386static void rockchip_rk3066_pll_get_params(struct rockchip_clk_pll *pll,
387 struct rockchip_pll_rate_table *rate)
388{
389 u32 pllcon;
390
391 pllcon = readl_relaxed(pll->reg_base + RK3066_PLLCON(0));
392 rate->nr = ((pllcon >> RK3066_PLLCON0_NR_SHIFT)
393 & RK3066_PLLCON0_NR_MASK) + 1;
394 rate->no = ((pllcon >> RK3066_PLLCON0_OD_SHIFT)
395 & RK3066_PLLCON0_OD_MASK) + 1;
396
397 pllcon = readl_relaxed(pll->reg_base + RK3066_PLLCON(1));
398 rate->nf = ((pllcon >> RK3066_PLLCON1_NF_SHIFT)
399 & RK3066_PLLCON1_NF_MASK) + 1;
400
401 pllcon = readl_relaxed(pll->reg_base + RK3066_PLLCON(2));
402 rate->nb = ((pllcon >> RK3066_PLLCON2_NB_SHIFT)
403 & RK3066_PLLCON2_NB_MASK) + 1;
404}
405
Heiko Stübner90c59022014-07-03 01:59:10 +0200406static unsigned long rockchip_rk3066_pll_recalc_rate(struct clk_hw *hw,
407 unsigned long prate)
408{
409 struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
Heiko Stübner8334c0e2015-10-01 11:38:35 +0200410 struct rockchip_pll_rate_table cur;
411 u64 rate64 = prate;
Heiko Stübner90c59022014-07-03 01:59:10 +0200412 u32 pllcon;
413
414 pllcon = readl_relaxed(pll->reg_base + RK3066_PLLCON(3));
415 if (pllcon & RK3066_PLLCON3_BYPASS) {
416 pr_debug("%s: pll %s is bypassed\n", __func__,
Stephen Boyd4c348752015-07-30 17:20:57 -0700417 clk_hw_get_name(hw));
Heiko Stübner90c59022014-07-03 01:59:10 +0200418 return prate;
419 }
420
Heiko Stübner8334c0e2015-10-01 11:38:35 +0200421 rockchip_rk3066_pll_get_params(pll, &cur);
Heiko Stübner90c59022014-07-03 01:59:10 +0200422
Heiko Stübner8334c0e2015-10-01 11:38:35 +0200423 rate64 *= cur.nf;
424 do_div(rate64, cur.nr);
425 do_div(rate64, cur.no);
Heiko Stübner90c59022014-07-03 01:59:10 +0200426
427 return (unsigned long)rate64;
428}
429
Heiko Stübner8334c0e2015-10-01 11:38:35 +0200430static int rockchip_rk3066_pll_set_params(struct rockchip_clk_pll *pll,
431 const struct rockchip_pll_rate_table *rate)
Heiko Stübner90c59022014-07-03 01:59:10 +0200432{
Doug Anderson9c030ea2014-09-15 21:07:57 -0700433 const struct clk_ops *pll_mux_ops = pll->pll_mux_ops;
Heiko Stübner8334c0e2015-10-01 11:38:35 +0200434 struct clk_mux *pll_mux = &pll->pll_mux;
435 struct rockchip_pll_rate_table cur;
Doug Anderson9c030ea2014-09-15 21:07:57 -0700436 int rate_change_remuxed = 0;
437 int cur_parent;
Heiko Stübner90c59022014-07-03 01:59:10 +0200438 int ret;
439
Heiko Stübner90c59022014-07-03 01:59:10 +0200440 pr_debug("%s: rate settings for %lu (nr, no, nf): (%d, %d, %d)\n",
441 __func__, rate->rate, rate->nr, rate->no, rate->nf);
442
Heiko Stübner8334c0e2015-10-01 11:38:35 +0200443 rockchip_rk3066_pll_get_params(pll, &cur);
444 cur.rate = 0;
445
Doug Anderson9c030ea2014-09-15 21:07:57 -0700446 cur_parent = pll_mux_ops->get_parent(&pll_mux->hw);
447 if (cur_parent == PLL_MODE_NORM) {
448 pll_mux_ops->set_parent(&pll_mux->hw, PLL_MODE_SLOW);
449 rate_change_remuxed = 1;
450 }
451
Heiko Stübner90c59022014-07-03 01:59:10 +0200452 /* enter reset mode */
453 writel(HIWORD_UPDATE(RK3066_PLLCON3_RESET, RK3066_PLLCON3_RESET, 0),
454 pll->reg_base + RK3066_PLLCON(3));
455
456 /* update pll values */
457 writel(HIWORD_UPDATE(rate->nr - 1, RK3066_PLLCON0_NR_MASK,
458 RK3066_PLLCON0_NR_SHIFT) |
459 HIWORD_UPDATE(rate->no - 1, RK3066_PLLCON0_OD_MASK,
460 RK3066_PLLCON0_OD_SHIFT),
461 pll->reg_base + RK3066_PLLCON(0));
462
463 writel_relaxed(HIWORD_UPDATE(rate->nf - 1, RK3066_PLLCON1_NF_MASK,
464 RK3066_PLLCON1_NF_SHIFT),
465 pll->reg_base + RK3066_PLLCON(1));
Douglas Anderson2bbfe002015-07-21 13:41:23 -0700466 writel_relaxed(HIWORD_UPDATE(rate->nb - 1, RK3066_PLLCON2_NB_MASK,
467 RK3066_PLLCON2_NB_SHIFT),
Heiko Stübner90c59022014-07-03 01:59:10 +0200468 pll->reg_base + RK3066_PLLCON(2));
469
470 /* leave reset and wait the reset_delay */
471 writel(HIWORD_UPDATE(0, RK3066_PLLCON3_RESET, 0),
472 pll->reg_base + RK3066_PLLCON(3));
473 udelay(RK3066_PLL_RESET_DELAY(rate->nr));
474
475 /* wait for the pll to lock */
476 ret = rockchip_pll_wait_lock(pll);
477 if (ret) {
Heiko Stübner8334c0e2015-10-01 11:38:35 +0200478 pr_warn("%s: pll update unsucessful, trying to restore old params\n",
479 __func__);
480 rockchip_rk3066_pll_set_params(pll, &cur);
Heiko Stübner90c59022014-07-03 01:59:10 +0200481 }
482
Doug Anderson9c030ea2014-09-15 21:07:57 -0700483 if (rate_change_remuxed)
484 pll_mux_ops->set_parent(&pll_mux->hw, PLL_MODE_NORM);
485
Heiko Stübner90c59022014-07-03 01:59:10 +0200486 return ret;
487}
488
Heiko Stübner8334c0e2015-10-01 11:38:35 +0200489static int rockchip_rk3066_pll_set_rate(struct clk_hw *hw, unsigned long drate,
490 unsigned long prate)
491{
492 struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
493 const struct rockchip_pll_rate_table *rate;
494 unsigned long old_rate = rockchip_rk3066_pll_recalc_rate(hw, prate);
Xing Zhengef1d9fe2016-03-09 10:37:04 +0800495 struct regmap *grf = rockchip_clk_get_grf(pll->ctx);
Heiko Stübner8334c0e2015-10-01 11:38:35 +0200496
497 if (IS_ERR(grf)) {
498 pr_debug("%s: grf regmap not available, aborting rate change\n",
499 __func__);
500 return PTR_ERR(grf);
501 }
502
503 pr_debug("%s: changing %s from %lu to %lu with a parent rate of %lu\n",
504 __func__, clk_hw_get_name(hw), old_rate, drate, prate);
505
506 /* Get required rate settings from table */
507 rate = rockchip_get_pll_settings(pll, drate);
508 if (!rate) {
509 pr_err("%s: Invalid rate : %lu for pll clk %s\n", __func__,
510 drate, clk_hw_get_name(hw));
511 return -EINVAL;
512 }
513
514 return rockchip_rk3066_pll_set_params(pll, rate);
515}
516
Heiko Stübner90c59022014-07-03 01:59:10 +0200517static int rockchip_rk3066_pll_enable(struct clk_hw *hw)
518{
519 struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
520
521 writel(HIWORD_UPDATE(0, RK3066_PLLCON3_PWRDOWN, 0),
522 pll->reg_base + RK3066_PLLCON(3));
523
524 return 0;
525}
526
527static void rockchip_rk3066_pll_disable(struct clk_hw *hw)
528{
529 struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
530
531 writel(HIWORD_UPDATE(RK3066_PLLCON3_PWRDOWN,
532 RK3066_PLLCON3_PWRDOWN, 0),
533 pll->reg_base + RK3066_PLLCON(3));
534}
535
536static int rockchip_rk3066_pll_is_enabled(struct clk_hw *hw)
537{
538 struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
539 u32 pllcon = readl(pll->reg_base + RK3066_PLLCON(3));
540
541 return !(pllcon & RK3066_PLLCON3_PWRDOWN);
542}
543
Heiko Stuebner0bb66d32014-11-20 20:38:52 +0100544static void rockchip_rk3066_pll_init(struct clk_hw *hw)
545{
546 struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
547 const struct rockchip_pll_rate_table *rate;
Heiko Stübner8334c0e2015-10-01 11:38:35 +0200548 struct rockchip_pll_rate_table cur;
Heiko Stuebner0bb66d32014-11-20 20:38:52 +0100549 unsigned long drate;
Heiko Stuebner0bb66d32014-11-20 20:38:52 +0100550
551 if (!(pll->flags & ROCKCHIP_PLL_SYNC_RATE))
552 return;
553
Stephen Boyd4c348752015-07-30 17:20:57 -0700554 drate = clk_hw_get_rate(hw);
Heiko Stuebner0bb66d32014-11-20 20:38:52 +0100555 rate = rockchip_get_pll_settings(pll, drate);
556
557 /* when no rate setting for the current rate, rely on clk_set_rate */
558 if (!rate)
559 return;
560
Heiko Stübner8334c0e2015-10-01 11:38:35 +0200561 rockchip_rk3066_pll_get_params(pll, &cur);
Heiko Stuebner0bb66d32014-11-20 20:38:52 +0100562
Douglas Anderson2bbfe002015-07-21 13:41:23 -0700563 pr_debug("%s: pll %s@%lu: nr (%d:%d); no (%d:%d); nf(%d:%d), nb(%d:%d)\n",
Heiko Stübner8334c0e2015-10-01 11:38:35 +0200564 __func__, clk_hw_get_name(hw), drate, rate->nr, cur.nr,
565 rate->no, cur.no, rate->nf, cur.nf, rate->nb, cur.nb);
566 if (rate->nr != cur.nr || rate->no != cur.no || rate->nf != cur.nf
567 || rate->nb != cur.nb) {
Xing Zhengef1d9fe2016-03-09 10:37:04 +0800568 struct regmap *grf = rockchip_clk_get_grf(pll->ctx);
Heiko Stuebner0bb66d32014-11-20 20:38:52 +0100569
Heiko Stübner8334c0e2015-10-01 11:38:35 +0200570 if (IS_ERR(grf))
Heiko Stuebner0bb66d32014-11-20 20:38:52 +0100571 return;
Heiko Stuebner0bb66d32014-11-20 20:38:52 +0100572
573 pr_debug("%s: pll %s: rate params do not match rate table, adjusting\n",
Stephen Boyd4c348752015-07-30 17:20:57 -0700574 __func__, clk_hw_get_name(hw));
Heiko Stübner8334c0e2015-10-01 11:38:35 +0200575 rockchip_rk3066_pll_set_params(pll, rate);
Heiko Stuebner0bb66d32014-11-20 20:38:52 +0100576 }
577}
578
Heiko Stübner90c59022014-07-03 01:59:10 +0200579static const struct clk_ops rockchip_rk3066_pll_clk_norate_ops = {
580 .recalc_rate = rockchip_rk3066_pll_recalc_rate,
581 .enable = rockchip_rk3066_pll_enable,
582 .disable = rockchip_rk3066_pll_disable,
583 .is_enabled = rockchip_rk3066_pll_is_enabled,
584};
585
586static const struct clk_ops rockchip_rk3066_pll_clk_ops = {
587 .recalc_rate = rockchip_rk3066_pll_recalc_rate,
588 .round_rate = rockchip_pll_round_rate,
589 .set_rate = rockchip_rk3066_pll_set_rate,
590 .enable = rockchip_rk3066_pll_enable,
591 .disable = rockchip_rk3066_pll_disable,
592 .is_enabled = rockchip_rk3066_pll_is_enabled,
Heiko Stuebner0bb66d32014-11-20 20:38:52 +0100593 .init = rockchip_rk3066_pll_init,
Heiko Stübner90c59022014-07-03 01:59:10 +0200594};
595
596/*
597 * Common registering of pll clocks
598 */
599
Xing Zhengef1d9fe2016-03-09 10:37:04 +0800600struct clk *rockchip_clk_register_pll(struct rockchip_clk_provider *ctx,
601 enum rockchip_pll_type pll_type,
Uwe Kleine-König4a1caed2015-05-28 10:45:51 +0200602 const char *name, const char *const *parent_names,
Xing Zhengef1d9fe2016-03-09 10:37:04 +0800603 u8 num_parents, int con_offset, int grf_lock_offset,
604 int lock_shift, int mode_offset, int mode_shift,
605 struct rockchip_pll_rate_table *rate_table,
606 u8 clk_pll_flags)
Heiko Stübner90c59022014-07-03 01:59:10 +0200607{
608 const char *pll_parents[3];
609 struct clk_init_data init;
610 struct rockchip_clk_pll *pll;
611 struct clk_mux *pll_mux;
612 struct clk *pll_clk, *mux_clk;
613 char pll_name[20];
Heiko Stübner90c59022014-07-03 01:59:10 +0200614
615 if (num_parents != 2) {
616 pr_err("%s: needs two parent clocks\n", __func__);
617 return ERR_PTR(-EINVAL);
618 }
619
620 /* name the actual pll */
621 snprintf(pll_name, sizeof(pll_name), "pll_%s", name);
622
623 pll = kzalloc(sizeof(*pll), GFP_KERNEL);
624 if (!pll)
625 return ERR_PTR(-ENOMEM);
626
Heiko Stuebner10897372015-08-19 15:06:55 +0200627 /* create the mux on top of the real pll */
628 pll->pll_mux_ops = &clk_mux_ops;
629 pll_mux = &pll->pll_mux;
Xing Zhengef1d9fe2016-03-09 10:37:04 +0800630 pll_mux->reg = ctx->reg_base + mode_offset;
Heiko Stuebner10897372015-08-19 15:06:55 +0200631 pll_mux->shift = mode_shift;
632 pll_mux->mask = PLL_MODE_MASK;
633 pll_mux->flags = 0;
Xing Zhengef1d9fe2016-03-09 10:37:04 +0800634 pll_mux->lock = &ctx->lock;
Heiko Stuebner10897372015-08-19 15:06:55 +0200635 pll_mux->hw.init = &init;
636
Xing Zheng9c4d6e552015-11-05 15:33:57 +0800637 if (pll_type == pll_rk3036 || pll_type == pll_rk3066)
Heiko Stuebner10897372015-08-19 15:06:55 +0200638 pll_mux->flags |= CLK_MUX_HIWORD_MASK;
639
640 /* the actual muxing is xin24m, pll-output, xin32k */
641 pll_parents[0] = parent_names[0];
642 pll_parents[1] = pll_name;
643 pll_parents[2] = parent_names[1];
644
645 init.name = name;
646 init.flags = CLK_SET_RATE_PARENT;
647 init.ops = pll->pll_mux_ops;
648 init.parent_names = pll_parents;
649 init.num_parents = ARRAY_SIZE(pll_parents);
650
651 mux_clk = clk_register(NULL, &pll_mux->hw);
652 if (IS_ERR(mux_clk))
653 goto err_mux;
654
655 /* now create the actual pll */
Heiko Stübner90c59022014-07-03 01:59:10 +0200656 init.name = pll_name;
657
658 /* keep all plls untouched for now */
659 init.flags = CLK_IGNORE_UNUSED;
660
661 init.parent_names = &parent_names[0];
662 init.num_parents = 1;
663
664 if (rate_table) {
665 int len;
666
667 /* find count of rates in rate_table */
668 for (len = 0; rate_table[len].rate != 0; )
669 len++;
670
671 pll->rate_count = len;
672 pll->rate_table = kmemdup(rate_table,
673 pll->rate_count *
674 sizeof(struct rockchip_pll_rate_table),
675 GFP_KERNEL);
676 WARN(!pll->rate_table,
677 "%s: could not allocate rate table for %s\n",
678 __func__, name);
679 }
680
681 switch (pll_type) {
Xing Zheng9c4d6e552015-11-05 15:33:57 +0800682 case pll_rk3036:
683 if (!pll->rate_table)
684 init.ops = &rockchip_rk3036_pll_clk_norate_ops;
685 else
686 init.ops = &rockchip_rk3036_pll_clk_ops;
687 break;
Heiko Stübner90c59022014-07-03 01:59:10 +0200688 case pll_rk3066:
689 if (!pll->rate_table)
690 init.ops = &rockchip_rk3066_pll_clk_norate_ops;
691 else
692 init.ops = &rockchip_rk3066_pll_clk_ops;
693 break;
694 default:
695 pr_warn("%s: Unknown pll type for pll clk %s\n",
696 __func__, name);
697 }
698
699 pll->hw.init = &init;
700 pll->type = pll_type;
Xing Zhengef1d9fe2016-03-09 10:37:04 +0800701 pll->reg_base = ctx->reg_base + con_offset;
Heiko Stübner90c59022014-07-03 01:59:10 +0200702 pll->lock_offset = grf_lock_offset;
703 pll->lock_shift = lock_shift;
Heiko Stuebner4f8a7c52014-11-20 20:38:50 +0100704 pll->flags = clk_pll_flags;
Xing Zhengef1d9fe2016-03-09 10:37:04 +0800705 pll->lock = &ctx->lock;
706 pll->ctx = ctx;
Heiko Stübner90c59022014-07-03 01:59:10 +0200707
708 pll_clk = clk_register(NULL, &pll->hw);
709 if (IS_ERR(pll_clk)) {
710 pr_err("%s: failed to register pll clock %s : %ld\n",
711 __func__, name, PTR_ERR(pll_clk));
Heiko Stübner90c59022014-07-03 01:59:10 +0200712 goto err_pll;
713 }
714
Heiko Stübner90c59022014-07-03 01:59:10 +0200715 return mux_clk;
716
Heiko Stübner90c59022014-07-03 01:59:10 +0200717err_pll:
Heiko Stuebner10897372015-08-19 15:06:55 +0200718 clk_unregister(mux_clk);
719 mux_clk = pll_clk;
720err_mux:
Heiko Stübner90c59022014-07-03 01:59:10 +0200721 kfree(pll);
722 return mux_clk;
723}