blob: 6d5b6e901b96752279a086e92cdf0625e3668e49 [file] [log] [blame]
Tony Prisk85814d62012-08-22 02:01:39 +12001/*
2 * Clock implementation for VIA/Wondermedia SoC's
3 * Copyright (C) 2012 Tony Prisk <linux@prisktech.co.nz>
4 *
5 * This software is licensed under the terms of the GNU General Public
6 * License version 2, as published by the Free Software Foundation, and
7 * may be copied, distributed, and modified under those terms.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 */
15
16#include <linux/io.h>
17#include <linux/of.h>
18#include <linux/slab.h>
19#include <linux/bitops.h>
20#include <linux/clkdev.h>
21#include <linux/clk-provider.h>
22
23/* All clocks share the same lock as none can be changed concurrently */
24static DEFINE_SPINLOCK(_lock);
25
26struct clk_device {
27 struct clk_hw hw;
28 void __iomem *div_reg;
29 unsigned int div_mask;
30 void __iomem *en_reg;
31 int en_bit;
32 spinlock_t *lock;
33};
34
35/*
36 * Add new PLL_TYPE_x definitions here as required. Use the first known model
37 * to support the new type as the name.
38 * Add case statements to vtwm_pll_recalc_rate(), vtwm_pll_round_round() and
39 * vtwm_pll_set_rate() to handle the new PLL_TYPE_x
40 */
41
42#define PLL_TYPE_VT8500 0
43#define PLL_TYPE_WM8650 1
Tony Priskabb165a2012-12-28 14:24:41 +130044#define PLL_TYPE_WM8750 2
Tony Prisk518d4702013-05-13 20:20:59 +120045#define PLL_TYPE_WM8850 3
Tony Prisk85814d62012-08-22 02:01:39 +120046
47struct clk_pll {
48 struct clk_hw hw;
49 void __iomem *reg;
50 spinlock_t *lock;
51 int type;
52};
53
54static void __iomem *pmc_base;
55
56#define to_clk_device(_hw) container_of(_hw, struct clk_device, hw)
57
58#define VT8500_PMC_BUSY_MASK 0x18
59
60static void vt8500_pmc_wait_busy(void)
61{
62 while (readl(pmc_base) & VT8500_PMC_BUSY_MASK)
63 cpu_relax();
64}
65
66static int vt8500_dclk_enable(struct clk_hw *hw)
67{
68 struct clk_device *cdev = to_clk_device(hw);
69 u32 en_val;
70 unsigned long flags = 0;
71
72 spin_lock_irqsave(cdev->lock, flags);
73
74 en_val = readl(cdev->en_reg);
75 en_val |= BIT(cdev->en_bit);
76 writel(en_val, cdev->en_reg);
77
78 spin_unlock_irqrestore(cdev->lock, flags);
79 return 0;
80}
81
82static void vt8500_dclk_disable(struct clk_hw *hw)
83{
84 struct clk_device *cdev = to_clk_device(hw);
85 u32 en_val;
86 unsigned long flags = 0;
87
88 spin_lock_irqsave(cdev->lock, flags);
89
90 en_val = readl(cdev->en_reg);
91 en_val &= ~BIT(cdev->en_bit);
92 writel(en_val, cdev->en_reg);
93
94 spin_unlock_irqrestore(cdev->lock, flags);
95}
96
97static int vt8500_dclk_is_enabled(struct clk_hw *hw)
98{
99 struct clk_device *cdev = to_clk_device(hw);
100 u32 en_val = (readl(cdev->en_reg) & BIT(cdev->en_bit));
101
102 return en_val ? 1 : 0;
103}
104
105static unsigned long vt8500_dclk_recalc_rate(struct clk_hw *hw,
106 unsigned long parent_rate)
107{
108 struct clk_device *cdev = to_clk_device(hw);
109 u32 div = readl(cdev->div_reg) & cdev->div_mask;
110
111 /* Special case for SDMMC devices */
112 if ((cdev->div_mask == 0x3F) && (div & BIT(5)))
113 div = 64 * (div & 0x1f);
114
115 /* div == 0 is actually the highest divisor */
116 if (div == 0)
117 div = (cdev->div_mask + 1);
118
119 return parent_rate / div;
120}
121
122static long vt8500_dclk_round_rate(struct clk_hw *hw, unsigned long rate,
123 unsigned long *prate)
124{
Tony Prisk973e1d12012-10-18 22:26:53 +1300125 struct clk_device *cdev = to_clk_device(hw);
Tony Prisk58eb5a62012-12-27 13:14:31 +1300126 u32 divisor;
127
128 if (rate == 0)
129 return 0;
130
131 divisor = *prate / rate;
Tony Prisk85814d62012-08-22 02:01:39 +1200132
Tony Prisk72480012012-12-27 13:14:30 +1300133 /* If prate / rate would be decimal, incr the divisor */
134 if (rate * divisor < *prate)
135 divisor++;
136
Tony Prisk973e1d12012-10-18 22:26:53 +1300137 /*
138 * If this is a request for SDMMC we have to adjust the divisor
139 * when >31 to use the fixed predivisor
140 */
141 if ((cdev->div_mask == 0x3F) && (divisor > 31)) {
142 divisor = 64 * ((divisor / 64) + 1);
143 }
144
Tony Prisk85814d62012-08-22 02:01:39 +1200145 return *prate / divisor;
146}
147
148static int vt8500_dclk_set_rate(struct clk_hw *hw, unsigned long rate,
149 unsigned long parent_rate)
150{
151 struct clk_device *cdev = to_clk_device(hw);
Tony Prisk58eb5a62012-12-27 13:14:31 +1300152 u32 divisor;
Tony Prisk85814d62012-08-22 02:01:39 +1200153 unsigned long flags = 0;
154
Tony Prisk58eb5a62012-12-27 13:14:31 +1300155 if (rate == 0)
156 return 0;
157
158 divisor = parent_rate / rate;
159
Tony Prisk72480012012-12-27 13:14:30 +1300160 /* If prate / rate would be decimal, incr the divisor */
Arnd Bergmannd6d10532013-03-01 14:12:01 +0100161 if (rate * divisor < parent_rate)
Tony Prisk72480012012-12-27 13:14:30 +1300162 divisor++;
163
Tony Prisk85814d62012-08-22 02:01:39 +1200164 if (divisor == cdev->div_mask + 1)
165 divisor = 0;
166
Tony Prisk973e1d12012-10-18 22:26:53 +1300167 /* SDMMC mask may need to be corrected before testing if its valid */
168 if ((cdev->div_mask == 0x3F) && (divisor > 31)) {
169 /*
170 * Bit 5 is a fixed /64 predivisor. If the requested divisor
171 * is >31 then correct for the fixed divisor being required.
172 */
173 divisor = 0x20 + (divisor / 64);
174 }
175
Tony Prisk85814d62012-08-22 02:01:39 +1200176 if (divisor > cdev->div_mask) {
177 pr_err("%s: invalid divisor for clock\n", __func__);
178 return -EINVAL;
179 }
180
181 spin_lock_irqsave(cdev->lock, flags);
182
183 vt8500_pmc_wait_busy();
184 writel(divisor, cdev->div_reg);
185 vt8500_pmc_wait_busy();
186
187 spin_lock_irqsave(cdev->lock, flags);
188
189 return 0;
190}
191
192
193static const struct clk_ops vt8500_gated_clk_ops = {
194 .enable = vt8500_dclk_enable,
195 .disable = vt8500_dclk_disable,
196 .is_enabled = vt8500_dclk_is_enabled,
197};
198
199static const struct clk_ops vt8500_divisor_clk_ops = {
200 .round_rate = vt8500_dclk_round_rate,
201 .set_rate = vt8500_dclk_set_rate,
202 .recalc_rate = vt8500_dclk_recalc_rate,
203};
204
205static const struct clk_ops vt8500_gated_divisor_clk_ops = {
206 .enable = vt8500_dclk_enable,
207 .disable = vt8500_dclk_disable,
208 .is_enabled = vt8500_dclk_is_enabled,
209 .round_rate = vt8500_dclk_round_rate,
210 .set_rate = vt8500_dclk_set_rate,
211 .recalc_rate = vt8500_dclk_recalc_rate,
212};
213
214#define CLK_INIT_GATED BIT(0)
215#define CLK_INIT_DIVISOR BIT(1)
216#define CLK_INIT_GATED_DIVISOR (CLK_INIT_DIVISOR | CLK_INIT_GATED)
217
218static __init void vtwm_device_clk_init(struct device_node *node)
219{
220 u32 en_reg, div_reg;
221 struct clk *clk;
222 struct clk_device *dev_clk;
223 const char *clk_name = node->name;
224 const char *parent_name;
225 struct clk_init_data init;
226 int rc;
227 int clk_init_flags = 0;
228
229 dev_clk = kzalloc(sizeof(*dev_clk), GFP_KERNEL);
230 if (WARN_ON(!dev_clk))
231 return;
232
233 dev_clk->lock = &_lock;
234
235 rc = of_property_read_u32(node, "enable-reg", &en_reg);
236 if (!rc) {
237 dev_clk->en_reg = pmc_base + en_reg;
238 rc = of_property_read_u32(node, "enable-bit", &dev_clk->en_bit);
239 if (rc) {
240 pr_err("%s: enable-bit property required for gated clock\n",
241 __func__);
242 return;
243 }
244 clk_init_flags |= CLK_INIT_GATED;
245 }
246
247 rc = of_property_read_u32(node, "divisor-reg", &div_reg);
248 if (!rc) {
249 dev_clk->div_reg = pmc_base + div_reg;
250 /*
251 * use 0x1f as the default mask since it covers
252 * almost all the clocks and reduces dts properties
253 */
254 dev_clk->div_mask = 0x1f;
255
256 of_property_read_u32(node, "divisor-mask", &dev_clk->div_mask);
257 clk_init_flags |= CLK_INIT_DIVISOR;
258 }
259
260 of_property_read_string(node, "clock-output-names", &clk_name);
261
262 switch (clk_init_flags) {
263 case CLK_INIT_GATED:
264 init.ops = &vt8500_gated_clk_ops;
265 break;
266 case CLK_INIT_DIVISOR:
267 init.ops = &vt8500_divisor_clk_ops;
268 break;
269 case CLK_INIT_GATED_DIVISOR:
270 init.ops = &vt8500_gated_divisor_clk_ops;
271 break;
272 default:
273 pr_err("%s: Invalid clock description in device tree\n",
274 __func__);
275 kfree(dev_clk);
276 return;
277 }
278
279 init.name = clk_name;
280 init.flags = 0;
281 parent_name = of_clk_get_parent_name(node, 0);
282 init.parent_names = &parent_name;
283 init.num_parents = 1;
284
285 dev_clk->hw.init = &init;
286
287 clk = clk_register(NULL, &dev_clk->hw);
288 if (WARN_ON(IS_ERR(clk))) {
289 kfree(dev_clk);
290 return;
291 }
292 rc = of_clk_add_provider(node, of_clk_src_simple_get, clk);
293 clk_register_clkdev(clk, clk_name, NULL);
294}
Prashant Gaikwad5b6e0ad2013-01-04 12:30:56 +0530295CLK_OF_DECLARE(vt8500_device, "via,vt8500-device-clock", vtwm_device_clk_init);
Tony Prisk85814d62012-08-22 02:01:39 +1200296
297/* PLL clock related functions */
298
299#define to_clk_pll(_hw) container_of(_hw, struct clk_pll, hw)
300
301/* Helper macros for PLL_VT8500 */
302#define VT8500_PLL_MUL(x) ((x & 0x1F) << 1)
303#define VT8500_PLL_DIV(x) ((x & 0x100) ? 1 : 2)
304
305#define VT8500_BITS_TO_FREQ(r, m, d) \
306 ((r / d) * m)
307
308#define VT8500_BITS_TO_VAL(m, d) \
309 ((d == 2 ? 0 : 0x100) | ((m >> 1) & 0x1F))
310
311/* Helper macros for PLL_WM8650 */
312#define WM8650_PLL_MUL(x) (x & 0x3FF)
313#define WM8650_PLL_DIV(x) (((x >> 10) & 7) * (1 << ((x >> 13) & 3)))
314
315#define WM8650_BITS_TO_FREQ(r, m, d1, d2) \
316 (r * m / (d1 * (1 << d2)))
317
318#define WM8650_BITS_TO_VAL(m, d1, d2) \
319 ((d2 << 13) | (d1 << 10) | (m & 0x3FF))
320
Tony Priskabb165a2012-12-28 14:24:41 +1300321/* Helper macros for PLL_WM8750 */
322#define WM8750_PLL_MUL(x) (((x >> 16) & 0xFF) + 1)
323#define WM8750_PLL_DIV(x) ((((x >> 8) & 1) + 1) * (1 << (x & 7)))
324
325#define WM8750_BITS_TO_FREQ(r, m, d1, d2) \
326 (r * (m+1) / ((d1+1) * (1 << d2)))
327
328#define WM8750_BITS_TO_VAL(f, m, d1, d2) \
329 ((f << 24) | ((m - 1) << 16) | ((d1 - 1) << 8) | d2)
330
Tony Prisk518d4702013-05-13 20:20:59 +1200331/* Helper macros for PLL_WM8850 */
332#define WM8850_PLL_MUL(x) ((((x >> 16) & 0x7F) + 1) * 2)
333#define WM8850_PLL_DIV(x) ((((x >> 8) & 1) + 1) * (1 << (x & 3)))
334
335#define WM8850_BITS_TO_FREQ(r, m, d1, d2) \
336 (r * ((m + 1) * 2) / ((d1+1) * (1 << d2)))
337
338#define WM8850_BITS_TO_VAL(m, d1, d2) \
339 ((((m / 2) - 1) << 16) | ((d1 - 1) << 8) | d2)
Tony Prisk85814d62012-08-22 02:01:39 +1200340
341static void vt8500_find_pll_bits(unsigned long rate, unsigned long parent_rate,
342 u32 *multiplier, u32 *prediv)
343{
344 unsigned long tclk;
345
346 /* sanity check */
347 if ((rate < parent_rate * 4) || (rate > parent_rate * 62)) {
348 pr_err("%s: requested rate out of range\n", __func__);
349 *multiplier = 0;
350 *prediv = 1;
351 return;
352 }
353 if (rate <= parent_rate * 31)
354 /* use the prediv to double the resolution */
355 *prediv = 2;
356 else
357 *prediv = 1;
358
359 *multiplier = rate / (parent_rate / *prediv);
360 tclk = (parent_rate / *prediv) * *multiplier;
361
362 if (tclk != rate)
363 pr_warn("%s: requested rate %lu, found rate %lu\n", __func__,
364 rate, tclk);
365}
366
367static void wm8650_find_pll_bits(unsigned long rate, unsigned long parent_rate,
368 u32 *multiplier, u32 *divisor1, u32 *divisor2)
369{
370 u32 mul, div1, div2;
371 u32 best_mul, best_div1, best_div2;
372 unsigned long tclk, rate_err, best_err;
373
374 best_err = (unsigned long)-1;
375
376 /* Find the closest match (lower or equal to requested) */
377 for (div1 = 5; div1 >= 3; div1--)
378 for (div2 = 3; div2 >= 0; div2--)
379 for (mul = 3; mul <= 1023; mul++) {
380 tclk = parent_rate * mul / (div1 * (1 << div2));
381 if (tclk > rate)
382 continue;
383 /* error will always be +ve */
384 rate_err = rate - tclk;
385 if (rate_err == 0) {
386 *multiplier = mul;
387 *divisor1 = div1;
388 *divisor2 = div2;
389 return;
390 }
391
392 if (rate_err < best_err) {
393 best_err = rate_err;
394 best_mul = mul;
395 best_div1 = div1;
396 best_div2 = div2;
397 }
398 }
399
400 /* if we got here, it wasn't an exact match */
401 pr_warn("%s: requested rate %lu, found rate %lu\n", __func__, rate,
402 rate - best_err);
Tony Prisk35a5db52012-12-27 13:14:29 +1300403 *multiplier = best_mul;
404 *divisor1 = best_div1;
405 *divisor2 = best_div2;
Tony Prisk85814d62012-08-22 02:01:39 +1200406}
407
Tony Priskabb165a2012-12-28 14:24:41 +1300408static u32 wm8750_get_filter(u32 parent_rate, u32 divisor1)
409{
410 /* calculate frequency (MHz) after pre-divisor */
411 u32 freq = (parent_rate / 1000000) / (divisor1 + 1);
412
413 if ((freq < 10) || (freq > 200))
414 pr_warn("%s: PLL recommended input frequency 10..200Mhz (requested %d Mhz)\n",
415 __func__, freq);
416
417 if (freq >= 166)
418 return 7;
419 else if (freq >= 104)
420 return 6;
421 else if (freq >= 65)
422 return 5;
423 else if (freq >= 42)
424 return 4;
425 else if (freq >= 26)
426 return 3;
427 else if (freq >= 16)
428 return 2;
429 else if (freq >= 10)
430 return 1;
431
432 return 0;
433}
434
435static void wm8750_find_pll_bits(unsigned long rate, unsigned long parent_rate,
436 u32 *filter, u32 *multiplier, u32 *divisor1, u32 *divisor2)
437{
438 u32 mul, div1, div2;
439 u32 best_mul, best_div1, best_div2;
440 unsigned long tclk, rate_err, best_err;
441
442 best_err = (unsigned long)-1;
443
444 /* Find the closest match (lower or equal to requested) */
445 for (div1 = 1; div1 >= 0; div1--)
446 for (div2 = 7; div2 >= 0; div2--)
447 for (mul = 0; mul <= 255; mul++) {
448 tclk = parent_rate * (mul + 1) / ((div1 + 1) * (1 << div2));
449 if (tclk > rate)
450 continue;
451 /* error will always be +ve */
452 rate_err = rate - tclk;
453 if (rate_err == 0) {
454 *filter = wm8750_get_filter(parent_rate, div1);
455 *multiplier = mul;
456 *divisor1 = div1;
457 *divisor2 = div2;
458 return;
459 }
460
461 if (rate_err < best_err) {
462 best_err = rate_err;
463 best_mul = mul;
464 best_div1 = div1;
465 best_div2 = div2;
466 }
467 }
468
469 /* if we got here, it wasn't an exact match */
470 pr_warn("%s: requested rate %lu, found rate %lu\n", __func__, rate,
471 rate - best_err);
472
473 *filter = wm8750_get_filter(parent_rate, best_div1);
474 *multiplier = best_mul;
475 *divisor1 = best_div1;
476 *divisor2 = best_div2;
477}
478
Tony Prisk518d4702013-05-13 20:20:59 +1200479static void wm8850_find_pll_bits(unsigned long rate, unsigned long parent_rate,
480 u32 *multiplier, u32 *divisor1, u32 *divisor2)
481{
482 u32 mul, div1, div2;
483 u32 best_mul, best_div1, best_div2;
484 unsigned long tclk, rate_err, best_err;
485
486 best_err = (unsigned long)-1;
487
488 /* Find the closest match (lower or equal to requested) */
489 for (div1 = 1; div1 >= 0; div1--)
490 for (div2 = 3; div2 >= 0; div2--)
491 for (mul = 0; mul <= 127; mul++) {
492 tclk = parent_rate * ((mul + 1) * 2) /
493 ((div1 + 1) * (1 << div2));
494 if (tclk > rate)
495 continue;
496 /* error will always be +ve */
497 rate_err = rate - tclk;
498 if (rate_err == 0) {
499 *multiplier = mul;
500 *divisor1 = div1;
501 *divisor2 = div2;
502 return;
503 }
504
505 if (rate_err < best_err) {
506 best_err = rate_err;
507 best_mul = mul;
508 best_div1 = div1;
509 best_div2 = div2;
510 }
511 }
512
513 /* if we got here, it wasn't an exact match */
514 pr_warn("%s: requested rate %lu, found rate %lu\n", __func__, rate,
515 rate - best_err);
516
517 *multiplier = best_mul;
518 *divisor1 = best_div1;
519 *divisor2 = best_div2;
520}
521
Tony Prisk85814d62012-08-22 02:01:39 +1200522static int vtwm_pll_set_rate(struct clk_hw *hw, unsigned long rate,
523 unsigned long parent_rate)
524{
525 struct clk_pll *pll = to_clk_pll(hw);
Tony Priskabb165a2012-12-28 14:24:41 +1300526 u32 filter, mul, div1, div2;
Tony Prisk85814d62012-08-22 02:01:39 +1200527 u32 pll_val;
528 unsigned long flags = 0;
529
530 /* sanity check */
531
532 switch (pll->type) {
533 case PLL_TYPE_VT8500:
534 vt8500_find_pll_bits(rate, parent_rate, &mul, &div1);
535 pll_val = VT8500_BITS_TO_VAL(mul, div1);
536 break;
537 case PLL_TYPE_WM8650:
538 wm8650_find_pll_bits(rate, parent_rate, &mul, &div1, &div2);
539 pll_val = WM8650_BITS_TO_VAL(mul, div1, div2);
540 break;
Tony Priskabb165a2012-12-28 14:24:41 +1300541 case PLL_TYPE_WM8750:
542 wm8750_find_pll_bits(rate, parent_rate, &filter, &mul, &div1, &div2);
543 pll_val = WM8750_BITS_TO_VAL(filter, mul, div1, div2);
Tony Priskbdca21e2013-04-14 17:28:35 +1200544 break;
Tony Prisk518d4702013-05-13 20:20:59 +1200545 case PLL_TYPE_WM8850:
546 wm8850_find_pll_bits(rate, parent_rate, &mul, &div1, &div2);
547 pll_val = WM8850_BITS_TO_VAL(mul, div1, div2);
548 break;
Tony Prisk85814d62012-08-22 02:01:39 +1200549 default:
550 pr_err("%s: invalid pll type\n", __func__);
551 return 0;
552 }
553
554 spin_lock_irqsave(pll->lock, flags);
555
556 vt8500_pmc_wait_busy();
557 writel(pll_val, pll->reg);
558 vt8500_pmc_wait_busy();
559
560 spin_unlock_irqrestore(pll->lock, flags);
561
562 return 0;
563}
564
565static long vtwm_pll_round_rate(struct clk_hw *hw, unsigned long rate,
566 unsigned long *prate)
567{
568 struct clk_pll *pll = to_clk_pll(hw);
Tony Priskabb165a2012-12-28 14:24:41 +1300569 u32 filter, mul, div1, div2;
Tony Prisk85814d62012-08-22 02:01:39 +1200570 long round_rate;
571
572 switch (pll->type) {
573 case PLL_TYPE_VT8500:
574 vt8500_find_pll_bits(rate, *prate, &mul, &div1);
575 round_rate = VT8500_BITS_TO_FREQ(*prate, mul, div1);
576 break;
577 case PLL_TYPE_WM8650:
578 wm8650_find_pll_bits(rate, *prate, &mul, &div1, &div2);
579 round_rate = WM8650_BITS_TO_FREQ(*prate, mul, div1, div2);
580 break;
Tony Priskabb165a2012-12-28 14:24:41 +1300581 case PLL_TYPE_WM8750:
582 wm8750_find_pll_bits(rate, *prate, &filter, &mul, &div1, &div2);
583 round_rate = WM8750_BITS_TO_FREQ(*prate, mul, div1, div2);
Tony Priskbdca21e2013-04-14 17:28:35 +1200584 break;
Tony Prisk518d4702013-05-13 20:20:59 +1200585 case PLL_TYPE_WM8850:
586 wm8850_find_pll_bits(rate, *prate, &mul, &div1, &div2);
587 round_rate = WM8850_BITS_TO_FREQ(*prate, mul, div1, div2);
588 break;
Tony Prisk85814d62012-08-22 02:01:39 +1200589 default:
590 round_rate = 0;
591 }
592
593 return round_rate;
594}
595
596static unsigned long vtwm_pll_recalc_rate(struct clk_hw *hw,
597 unsigned long parent_rate)
598{
599 struct clk_pll *pll = to_clk_pll(hw);
600 u32 pll_val = readl(pll->reg);
601 unsigned long pll_freq;
602
603 switch (pll->type) {
604 case PLL_TYPE_VT8500:
605 pll_freq = parent_rate * VT8500_PLL_MUL(pll_val);
606 pll_freq /= VT8500_PLL_DIV(pll_val);
607 break;
608 case PLL_TYPE_WM8650:
609 pll_freq = parent_rate * WM8650_PLL_MUL(pll_val);
610 pll_freq /= WM8650_PLL_DIV(pll_val);
611 break;
Tony Priskabb165a2012-12-28 14:24:41 +1300612 case PLL_TYPE_WM8750:
613 pll_freq = parent_rate * WM8750_PLL_MUL(pll_val);
614 pll_freq /= WM8750_PLL_DIV(pll_val);
615 break;
Tony Prisk518d4702013-05-13 20:20:59 +1200616 case PLL_TYPE_WM8850:
617 pll_freq = parent_rate * WM8850_PLL_MUL(pll_val);
618 pll_freq /= WM8850_PLL_DIV(pll_val);
619 break;
Tony Prisk85814d62012-08-22 02:01:39 +1200620 default:
621 pll_freq = 0;
622 }
623
624 return pll_freq;
625}
626
627const struct clk_ops vtwm_pll_ops = {
628 .round_rate = vtwm_pll_round_rate,
629 .set_rate = vtwm_pll_set_rate,
630 .recalc_rate = vtwm_pll_recalc_rate,
631};
632
633static __init void vtwm_pll_clk_init(struct device_node *node, int pll_type)
634{
635 u32 reg;
636 struct clk *clk;
637 struct clk_pll *pll_clk;
638 const char *clk_name = node->name;
639 const char *parent_name;
640 struct clk_init_data init;
641 int rc;
642
643 rc = of_property_read_u32(node, "reg", &reg);
644 if (WARN_ON(rc))
645 return;
646
647 pll_clk = kzalloc(sizeof(*pll_clk), GFP_KERNEL);
648 if (WARN_ON(!pll_clk))
649 return;
650
651 pll_clk->reg = pmc_base + reg;
652 pll_clk->lock = &_lock;
653 pll_clk->type = pll_type;
654
655 of_property_read_string(node, "clock-output-names", &clk_name);
656
657 init.name = clk_name;
658 init.ops = &vtwm_pll_ops;
659 init.flags = 0;
660 parent_name = of_clk_get_parent_name(node, 0);
661 init.parent_names = &parent_name;
662 init.num_parents = 1;
663
664 pll_clk->hw.init = &init;
665
666 clk = clk_register(NULL, &pll_clk->hw);
667 if (WARN_ON(IS_ERR(clk))) {
668 kfree(pll_clk);
669 return;
670 }
671 rc = of_clk_add_provider(node, of_clk_src_simple_get, clk);
672 clk_register_clkdev(clk, clk_name, NULL);
673}
674
675
676/* Wrappers for initialization functions */
677
678static void __init vt8500_pll_init(struct device_node *node)
679{
680 vtwm_pll_clk_init(node, PLL_TYPE_VT8500);
681}
Prashant Gaikwad5b6e0ad2013-01-04 12:30:56 +0530682CLK_OF_DECLARE(vt8500_pll, "via,vt8500-pll-clock", vt8500_pll_init);
Tony Prisk85814d62012-08-22 02:01:39 +1200683
684static void __init wm8650_pll_init(struct device_node *node)
685{
686 vtwm_pll_clk_init(node, PLL_TYPE_WM8650);
687}
Prashant Gaikwad5b6e0ad2013-01-04 12:30:56 +0530688CLK_OF_DECLARE(wm8650_pll, "wm,wm8650-pll-clock", wm8650_pll_init);
Tony Prisk85814d62012-08-22 02:01:39 +1200689
Tony Priskabb165a2012-12-28 14:24:41 +1300690static void __init wm8750_pll_init(struct device_node *node)
691{
692 vtwm_pll_clk_init(node, PLL_TYPE_WM8750);
693}
Prashant Gaikwad5b6e0ad2013-01-04 12:30:56 +0530694CLK_OF_DECLARE(wm8750_pll, "wm,wm8750-pll-clock", wm8750_pll_init);
Tony Prisk85814d62012-08-22 02:01:39 +1200695
Tony Prisk518d4702013-05-13 20:20:59 +1200696static void __init wm8850_pll_init(struct device_node *node)
697{
698 vtwm_pll_clk_init(node, PLL_TYPE_WM8850);
699}
700CLK_OF_DECLARE(wm8850_pll, "wm,wm8850-pll-clock", wm8850_pll_init);
701
Tony Prisk85814d62012-08-22 02:01:39 +1200702void __init vtwm_clk_init(void __iomem *base)
703{
704 if (!base)
705 return;
706
707 pmc_base = base;
708
Prashant Gaikwad5b6e0ad2013-01-04 12:30:56 +0530709 of_clk_init(NULL);
Tony Prisk85814d62012-08-22 02:01:39 +1200710}