Merge tag 'pm-extra-4.8-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/rafael...
[linux-2.6-block.git] / drivers / clk / clk-vt8500.c
CommitLineData
85814d69
TP
1/*
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>
f9e4a18d 18#include <linux/of_address.h>
85814d69
TP
19#include <linux/slab.h>
20#include <linux/bitops.h>
21#include <linux/clkdev.h>
22#include <linux/clk-provider.h>
23
f9e4a18d
SH
24#define LEGACY_PMC_BASE 0xD8130000
25
85814d69
TP
26/* All clocks share the same lock as none can be changed concurrently */
27static DEFINE_SPINLOCK(_lock);
28
29struct clk_device {
30 struct clk_hw hw;
31 void __iomem *div_reg;
32 unsigned int div_mask;
33 void __iomem *en_reg;
34 int en_bit;
35 spinlock_t *lock;
36};
37
38/*
39 * Add new PLL_TYPE_x definitions here as required. Use the first known model
40 * to support the new type as the name.
41 * Add case statements to vtwm_pll_recalc_rate(), vtwm_pll_round_round() and
42 * vtwm_pll_set_rate() to handle the new PLL_TYPE_x
43 */
44
45#define PLL_TYPE_VT8500 0
46#define PLL_TYPE_WM8650 1
abb165a8 47#define PLL_TYPE_WM8750 2
518d4709 48#define PLL_TYPE_WM8850 3
85814d69
TP
49
50struct clk_pll {
51 struct clk_hw hw;
52 void __iomem *reg;
53 spinlock_t *lock;
54 int type;
55};
56
57static void __iomem *pmc_base;
58
f9e4a18d
SH
59static __init void vtwm_set_pmc_base(void)
60{
61 struct device_node *np =
62 of_find_compatible_node(NULL, NULL, "via,vt8500-pmc");
63
64 if (np)
65 pmc_base = of_iomap(np, 0);
66 else
67 pmc_base = ioremap(LEGACY_PMC_BASE, 0x1000);
68 of_node_put(np);
69
70 if (!pmc_base)
71 pr_err("%s:of_iomap(pmc) failed\n", __func__);
72}
73
85814d69
TP
74#define to_clk_device(_hw) container_of(_hw, struct clk_device, hw)
75
76#define VT8500_PMC_BUSY_MASK 0x18
77
78static void vt8500_pmc_wait_busy(void)
79{
80 while (readl(pmc_base) & VT8500_PMC_BUSY_MASK)
81 cpu_relax();
82}
83
84static int vt8500_dclk_enable(struct clk_hw *hw)
85{
86 struct clk_device *cdev = to_clk_device(hw);
87 u32 en_val;
88 unsigned long flags = 0;
89
90 spin_lock_irqsave(cdev->lock, flags);
91
92 en_val = readl(cdev->en_reg);
93 en_val |= BIT(cdev->en_bit);
94 writel(en_val, cdev->en_reg);
95
96 spin_unlock_irqrestore(cdev->lock, flags);
97 return 0;
98}
99
100static void vt8500_dclk_disable(struct clk_hw *hw)
101{
102 struct clk_device *cdev = to_clk_device(hw);
103 u32 en_val;
104 unsigned long flags = 0;
105
106 spin_lock_irqsave(cdev->lock, flags);
107
108 en_val = readl(cdev->en_reg);
109 en_val &= ~BIT(cdev->en_bit);
110 writel(en_val, cdev->en_reg);
111
112 spin_unlock_irqrestore(cdev->lock, flags);
113}
114
115static int vt8500_dclk_is_enabled(struct clk_hw *hw)
116{
117 struct clk_device *cdev = to_clk_device(hw);
118 u32 en_val = (readl(cdev->en_reg) & BIT(cdev->en_bit));
119
120 return en_val ? 1 : 0;
121}
122
123static unsigned long vt8500_dclk_recalc_rate(struct clk_hw *hw,
124 unsigned long parent_rate)
125{
126 struct clk_device *cdev = to_clk_device(hw);
127 u32 div = readl(cdev->div_reg) & cdev->div_mask;
128
129 /* Special case for SDMMC devices */
130 if ((cdev->div_mask == 0x3F) && (div & BIT(5)))
131 div = 64 * (div & 0x1f);
132
133 /* div == 0 is actually the highest divisor */
134 if (div == 0)
135 div = (cdev->div_mask + 1);
136
137 return parent_rate / div;
138}
139
140static long vt8500_dclk_round_rate(struct clk_hw *hw, unsigned long rate,
141 unsigned long *prate)
142{
973e1d1d 143 struct clk_device *cdev = to_clk_device(hw);
58eb5a67
TP
144 u32 divisor;
145
146 if (rate == 0)
147 return 0;
148
149 divisor = *prate / rate;
85814d69 150
72480014
TP
151 /* If prate / rate would be decimal, incr the divisor */
152 if (rate * divisor < *prate)
153 divisor++;
154
973e1d1d
TP
155 /*
156 * If this is a request for SDMMC we have to adjust the divisor
157 * when >31 to use the fixed predivisor
158 */
159 if ((cdev->div_mask == 0x3F) && (divisor > 31)) {
160 divisor = 64 * ((divisor / 64) + 1);
161 }
162
85814d69
TP
163 return *prate / divisor;
164}
165
166static int vt8500_dclk_set_rate(struct clk_hw *hw, unsigned long rate,
167 unsigned long parent_rate)
168{
169 struct clk_device *cdev = to_clk_device(hw);
58eb5a67 170 u32 divisor;
85814d69
TP
171 unsigned long flags = 0;
172
58eb5a67
TP
173 if (rate == 0)
174 return 0;
175
176 divisor = parent_rate / rate;
177
85814d69
TP
178 if (divisor == cdev->div_mask + 1)
179 divisor = 0;
180
973e1d1d
TP
181 /* SDMMC mask may need to be corrected before testing if its valid */
182 if ((cdev->div_mask == 0x3F) && (divisor > 31)) {
183 /*
184 * Bit 5 is a fixed /64 predivisor. If the requested divisor
185 * is >31 then correct for the fixed divisor being required.
186 */
187 divisor = 0x20 + (divisor / 64);
188 }
189
85814d69
TP
190 if (divisor > cdev->div_mask) {
191 pr_err("%s: invalid divisor for clock\n", __func__);
192 return -EINVAL;
193 }
194
195 spin_lock_irqsave(cdev->lock, flags);
196
197 vt8500_pmc_wait_busy();
198 writel(divisor, cdev->div_reg);
199 vt8500_pmc_wait_busy();
200
419e321d 201 spin_unlock_irqrestore(cdev->lock, flags);
85814d69
TP
202
203 return 0;
204}
205
206
207static const struct clk_ops vt8500_gated_clk_ops = {
208 .enable = vt8500_dclk_enable,
209 .disable = vt8500_dclk_disable,
210 .is_enabled = vt8500_dclk_is_enabled,
211};
212
213static const struct clk_ops vt8500_divisor_clk_ops = {
214 .round_rate = vt8500_dclk_round_rate,
215 .set_rate = vt8500_dclk_set_rate,
216 .recalc_rate = vt8500_dclk_recalc_rate,
217};
218
219static const struct clk_ops vt8500_gated_divisor_clk_ops = {
220 .enable = vt8500_dclk_enable,
221 .disable = vt8500_dclk_disable,
222 .is_enabled = vt8500_dclk_is_enabled,
223 .round_rate = vt8500_dclk_round_rate,
224 .set_rate = vt8500_dclk_set_rate,
225 .recalc_rate = vt8500_dclk_recalc_rate,
226};
227
228#define CLK_INIT_GATED BIT(0)
229#define CLK_INIT_DIVISOR BIT(1)
230#define CLK_INIT_GATED_DIVISOR (CLK_INIT_DIVISOR | CLK_INIT_GATED)
231
232static __init void vtwm_device_clk_init(struct device_node *node)
233{
234 u32 en_reg, div_reg;
235 struct clk *clk;
236 struct clk_device *dev_clk;
237 const char *clk_name = node->name;
238 const char *parent_name;
239 struct clk_init_data init;
240 int rc;
241 int clk_init_flags = 0;
242
f9e4a18d
SH
243 if (!pmc_base)
244 vtwm_set_pmc_base();
245
85814d69
TP
246 dev_clk = kzalloc(sizeof(*dev_clk), GFP_KERNEL);
247 if (WARN_ON(!dev_clk))
248 return;
249
250 dev_clk->lock = &_lock;
251
252 rc = of_property_read_u32(node, "enable-reg", &en_reg);
253 if (!rc) {
254 dev_clk->en_reg = pmc_base + en_reg;
255 rc = of_property_read_u32(node, "enable-bit", &dev_clk->en_bit);
256 if (rc) {
257 pr_err("%s: enable-bit property required for gated clock\n",
258 __func__);
259 return;
260 }
261 clk_init_flags |= CLK_INIT_GATED;
262 }
263
264 rc = of_property_read_u32(node, "divisor-reg", &div_reg);
265 if (!rc) {
266 dev_clk->div_reg = pmc_base + div_reg;
267 /*
268 * use 0x1f as the default mask since it covers
269 * almost all the clocks and reduces dts properties
270 */
271 dev_clk->div_mask = 0x1f;
272
273 of_property_read_u32(node, "divisor-mask", &dev_clk->div_mask);
274 clk_init_flags |= CLK_INIT_DIVISOR;
275 }
276
277 of_property_read_string(node, "clock-output-names", &clk_name);
278
279 switch (clk_init_flags) {
280 case CLK_INIT_GATED:
281 init.ops = &vt8500_gated_clk_ops;
282 break;
283 case CLK_INIT_DIVISOR:
284 init.ops = &vt8500_divisor_clk_ops;
285 break;
286 case CLK_INIT_GATED_DIVISOR:
287 init.ops = &vt8500_gated_divisor_clk_ops;
288 break;
289 default:
290 pr_err("%s: Invalid clock description in device tree\n",
291 __func__);
292 kfree(dev_clk);
293 return;
294 }
295
296 init.name = clk_name;
297 init.flags = 0;
298 parent_name = of_clk_get_parent_name(node, 0);
299 init.parent_names = &parent_name;
300 init.num_parents = 1;
301
302 dev_clk->hw.init = &init;
303
304 clk = clk_register(NULL, &dev_clk->hw);
305 if (WARN_ON(IS_ERR(clk))) {
306 kfree(dev_clk);
307 return;
308 }
309 rc = of_clk_add_provider(node, of_clk_src_simple_get, clk);
310 clk_register_clkdev(clk, clk_name, NULL);
311}
5b6e0adb 312CLK_OF_DECLARE(vt8500_device, "via,vt8500-device-clock", vtwm_device_clk_init);
85814d69
TP
313
314/* PLL clock related functions */
315
316#define to_clk_pll(_hw) container_of(_hw, struct clk_pll, hw)
317
318/* Helper macros for PLL_VT8500 */
319#define VT8500_PLL_MUL(x) ((x & 0x1F) << 1)
320#define VT8500_PLL_DIV(x) ((x & 0x100) ? 1 : 2)
321
322#define VT8500_BITS_TO_FREQ(r, m, d) \
323 ((r / d) * m)
324
325#define VT8500_BITS_TO_VAL(m, d) \
326 ((d == 2 ? 0 : 0x100) | ((m >> 1) & 0x1F))
327
328/* Helper macros for PLL_WM8650 */
329#define WM8650_PLL_MUL(x) (x & 0x3FF)
330#define WM8650_PLL_DIV(x) (((x >> 10) & 7) * (1 << ((x >> 13) & 3)))
331
332#define WM8650_BITS_TO_FREQ(r, m, d1, d2) \
333 (r * m / (d1 * (1 << d2)))
334
335#define WM8650_BITS_TO_VAL(m, d1, d2) \
336 ((d2 << 13) | (d1 << 10) | (m & 0x3FF))
337
abb165a8
TP
338/* Helper macros for PLL_WM8750 */
339#define WM8750_PLL_MUL(x) (((x >> 16) & 0xFF) + 1)
340#define WM8750_PLL_DIV(x) ((((x >> 8) & 1) + 1) * (1 << (x & 7)))
341
342#define WM8750_BITS_TO_FREQ(r, m, d1, d2) \
343 (r * (m+1) / ((d1+1) * (1 << d2)))
344
345#define WM8750_BITS_TO_VAL(f, m, d1, d2) \
346 ((f << 24) | ((m - 1) << 16) | ((d1 - 1) << 8) | d2)
347
518d4709
TP
348/* Helper macros for PLL_WM8850 */
349#define WM8850_PLL_MUL(x) ((((x >> 16) & 0x7F) + 1) * 2)
350#define WM8850_PLL_DIV(x) ((((x >> 8) & 1) + 1) * (1 << (x & 3)))
351
352#define WM8850_BITS_TO_FREQ(r, m, d1, d2) \
353 (r * ((m + 1) * 2) / ((d1+1) * (1 << d2)))
354
355#define WM8850_BITS_TO_VAL(m, d1, d2) \
356 ((((m / 2) - 1) << 16) | ((d1 - 1) << 8) | d2)
85814d69 357
7001ec56 358static int vt8500_find_pll_bits(unsigned long rate, unsigned long parent_rate,
85814d69
TP
359 u32 *multiplier, u32 *prediv)
360{
361 unsigned long tclk;
362
363 /* sanity check */
364 if ((rate < parent_rate * 4) || (rate > parent_rate * 62)) {
365 pr_err("%s: requested rate out of range\n", __func__);
366 *multiplier = 0;
367 *prediv = 1;
7001ec56 368 return -EINVAL;
85814d69
TP
369 }
370 if (rate <= parent_rate * 31)
371 /* use the prediv to double the resolution */
372 *prediv = 2;
373 else
374 *prediv = 1;
375
376 *multiplier = rate / (parent_rate / *prediv);
377 tclk = (parent_rate / *prediv) * *multiplier;
378
379 if (tclk != rate)
380 pr_warn("%s: requested rate %lu, found rate %lu\n", __func__,
381 rate, tclk);
7001ec56
AB
382
383 return 0;
85814d69
TP
384}
385
c03d795b
RV
386/*
387 * M * parent [O1] => / P [O2] => / D [O3]
388 * Where O1 is 900MHz...3GHz;
389 * O2 is 600MHz >= (M * parent) / P >= 300MHz;
390 * M is 36...120 [25MHz parent]; D is 1 or 2 or 4 or 8.
391 * Possible ranges (O3):
392 * D = 8: 37,5MHz...75MHz
393 * D = 4: 75MHz...150MHz
394 * D = 2: 150MHz...300MHz
395 * D = 1: 300MHz...600MHz
396 */
397static int wm8650_find_pll_bits(unsigned long rate,
398 unsigned long parent_rate, u32 *multiplier, u32 *divisor1,
399 u32 *divisor2)
85814d69 400{
c03d795b
RV
401 unsigned long O1, min_err, rate_err;
402
403 if (!parent_rate || (rate < 37500000) || (rate > 600000000))
7001ec56 404 return -EINVAL;
c03d795b
RV
405
406 *divisor2 = rate <= 75000000 ? 3 : rate <= 150000000 ? 2 :
407 rate <= 300000000 ? 1 : 0;
408 /*
409 * Divisor P cannot be calculated. Test all divisors and find where M
410 * will be as close as possible to the requested rate.
411 */
412 min_err = ULONG_MAX;
413 for (*divisor1 = 5; *divisor1 >= 3; (*divisor1)--) {
414 O1 = rate * *divisor1 * (1 << (*divisor2));
415 rate_err = O1 % parent_rate;
416 if (rate_err < min_err) {
417 *multiplier = O1 / parent_rate;
418 if (rate_err == 0)
419 return 0;
420
421 min_err = rate_err;
422 }
7001ec56
AB
423 }
424
c03d795b
RV
425 if ((*multiplier < 3) || (*multiplier > 1023))
426 return -EINVAL;
427
428 pr_warn("%s: rate error is %lu\n", __func__, min_err);
429
7001ec56 430 return 0;
85814d69
TP
431}
432
abb165a8
TP
433static u32 wm8750_get_filter(u32 parent_rate, u32 divisor1)
434{
435 /* calculate frequency (MHz) after pre-divisor */
436 u32 freq = (parent_rate / 1000000) / (divisor1 + 1);
437
438 if ((freq < 10) || (freq > 200))
439 pr_warn("%s: PLL recommended input frequency 10..200Mhz (requested %d Mhz)\n",
440 __func__, freq);
441
442 if (freq >= 166)
443 return 7;
444 else if (freq >= 104)
445 return 6;
446 else if (freq >= 65)
447 return 5;
448 else if (freq >= 42)
449 return 4;
450 else if (freq >= 26)
451 return 3;
452 else if (freq >= 16)
453 return 2;
454 else if (freq >= 10)
455 return 1;
456
457 return 0;
458}
459
7001ec56 460static int wm8750_find_pll_bits(unsigned long rate, unsigned long parent_rate,
abb165a8
TP
461 u32 *filter, u32 *multiplier, u32 *divisor1, u32 *divisor2)
462{
090341b0
AH
463 u32 mul;
464 int div1, div2;
abb165a8
TP
465 unsigned long tclk, rate_err, best_err;
466
467 best_err = (unsigned long)-1;
468
469 /* Find the closest match (lower or equal to requested) */
470 for (div1 = 1; div1 >= 0; div1--)
471 for (div2 = 7; div2 >= 0; div2--)
472 for (mul = 0; mul <= 255; mul++) {
473 tclk = parent_rate * (mul + 1) / ((div1 + 1) * (1 << div2));
474 if (tclk > rate)
475 continue;
476 /* error will always be +ve */
477 rate_err = rate - tclk;
478 if (rate_err == 0) {
479 *filter = wm8750_get_filter(parent_rate, div1);
480 *multiplier = mul;
481 *divisor1 = div1;
482 *divisor2 = div2;
7001ec56 483 return 0;
abb165a8
TP
484 }
485
486 if (rate_err < best_err) {
487 best_err = rate_err;
27a0becc
AB
488 *multiplier = mul;
489 *divisor1 = div1;
490 *divisor2 = div2;
abb165a8
TP
491 }
492 }
493
7001ec56
AB
494 if (best_err == (unsigned long)-1) {
495 pr_warn("%s: impossible rate %lu\n", __func__, rate);
496 return -EINVAL;
497 }
498
abb165a8
TP
499 /* if we got here, it wasn't an exact match */
500 pr_warn("%s: requested rate %lu, found rate %lu\n", __func__, rate,
501 rate - best_err);
502
27a0becc 503 *filter = wm8750_get_filter(parent_rate, *divisor1);
7001ec56
AB
504
505 return 0;
abb165a8
TP
506}
507
7001ec56 508static int wm8850_find_pll_bits(unsigned long rate, unsigned long parent_rate,
518d4709
TP
509 u32 *multiplier, u32 *divisor1, u32 *divisor2)
510{
090341b0
AH
511 u32 mul;
512 int div1, div2;
518d4709
TP
513 unsigned long tclk, rate_err, best_err;
514
515 best_err = (unsigned long)-1;
516
517 /* Find the closest match (lower or equal to requested) */
518 for (div1 = 1; div1 >= 0; div1--)
519 for (div2 = 3; div2 >= 0; div2--)
520 for (mul = 0; mul <= 127; mul++) {
521 tclk = parent_rate * ((mul + 1) * 2) /
522 ((div1 + 1) * (1 << div2));
523 if (tclk > rate)
524 continue;
525 /* error will always be +ve */
526 rate_err = rate - tclk;
527 if (rate_err == 0) {
528 *multiplier = mul;
529 *divisor1 = div1;
530 *divisor2 = div2;
7001ec56 531 return 0;
518d4709
TP
532 }
533
534 if (rate_err < best_err) {
535 best_err = rate_err;
27a0becc
AB
536 *multiplier = mul;
537 *divisor1 = div1;
538 *divisor2 = div2;
518d4709
TP
539 }
540 }
541
7001ec56
AB
542 if (best_err == (unsigned long)-1) {
543 pr_warn("%s: impossible rate %lu\n", __func__, rate);
544 return -EINVAL;
545 }
546
518d4709
TP
547 /* if we got here, it wasn't an exact match */
548 pr_warn("%s: requested rate %lu, found rate %lu\n", __func__, rate,
549 rate - best_err);
550
7001ec56 551 return 0;
518d4709
TP
552}
553
85814d69
TP
554static int vtwm_pll_set_rate(struct clk_hw *hw, unsigned long rate,
555 unsigned long parent_rate)
556{
557 struct clk_pll *pll = to_clk_pll(hw);
abb165a8 558 u32 filter, mul, div1, div2;
85814d69
TP
559 u32 pll_val;
560 unsigned long flags = 0;
7001ec56 561 int ret;
85814d69
TP
562
563 /* sanity check */
564
565 switch (pll->type) {
566 case PLL_TYPE_VT8500:
7001ec56
AB
567 ret = vt8500_find_pll_bits(rate, parent_rate, &mul, &div1);
568 if (!ret)
569 pll_val = VT8500_BITS_TO_VAL(mul, div1);
85814d69
TP
570 break;
571 case PLL_TYPE_WM8650:
7001ec56
AB
572 ret = wm8650_find_pll_bits(rate, parent_rate, &mul, &div1, &div2);
573 if (!ret)
574 pll_val = WM8650_BITS_TO_VAL(mul, div1, div2);
85814d69 575 break;
abb165a8 576 case PLL_TYPE_WM8750:
7001ec56
AB
577 ret = wm8750_find_pll_bits(rate, parent_rate, &filter, &mul, &div1, &div2);
578 if (!ret)
579 pll_val = WM8750_BITS_TO_VAL(filter, mul, div1, div2);
bdca21ec 580 break;
518d4709 581 case PLL_TYPE_WM8850:
7001ec56
AB
582 ret = wm8850_find_pll_bits(rate, parent_rate, &mul, &div1, &div2);
583 if (!ret)
584 pll_val = WM8850_BITS_TO_VAL(mul, div1, div2);
518d4709 585 break;
85814d69
TP
586 default:
587 pr_err("%s: invalid pll type\n", __func__);
7001ec56 588 ret = -EINVAL;
85814d69
TP
589 }
590
7001ec56
AB
591 if (ret)
592 return ret;
593
85814d69
TP
594 spin_lock_irqsave(pll->lock, flags);
595
596 vt8500_pmc_wait_busy();
597 writel(pll_val, pll->reg);
598 vt8500_pmc_wait_busy();
599
600 spin_unlock_irqrestore(pll->lock, flags);
601
602 return 0;
603}
604
605static long vtwm_pll_round_rate(struct clk_hw *hw, unsigned long rate,
606 unsigned long *prate)
607{
608 struct clk_pll *pll = to_clk_pll(hw);
abb165a8 609 u32 filter, mul, div1, div2;
85814d69 610 long round_rate;
7001ec56 611 int ret;
85814d69
TP
612
613 switch (pll->type) {
614 case PLL_TYPE_VT8500:
7001ec56
AB
615 ret = vt8500_find_pll_bits(rate, *prate, &mul, &div1);
616 if (!ret)
617 round_rate = VT8500_BITS_TO_FREQ(*prate, mul, div1);
85814d69
TP
618 break;
619 case PLL_TYPE_WM8650:
7001ec56
AB
620 ret = wm8650_find_pll_bits(rate, *prate, &mul, &div1, &div2);
621 if (!ret)
622 round_rate = WM8650_BITS_TO_FREQ(*prate, mul, div1, div2);
85814d69 623 break;
abb165a8 624 case PLL_TYPE_WM8750:
7001ec56
AB
625 ret = wm8750_find_pll_bits(rate, *prate, &filter, &mul, &div1, &div2);
626 if (!ret)
627 round_rate = WM8750_BITS_TO_FREQ(*prate, mul, div1, div2);
bdca21ec 628 break;
518d4709 629 case PLL_TYPE_WM8850:
7001ec56
AB
630 ret = wm8850_find_pll_bits(rate, *prate, &mul, &div1, &div2);
631 if (!ret)
632 round_rate = WM8850_BITS_TO_FREQ(*prate, mul, div1, div2);
518d4709 633 break;
85814d69 634 default:
7001ec56 635 ret = -EINVAL;
85814d69
TP
636 }
637
7001ec56
AB
638 if (ret)
639 return ret;
640
85814d69
TP
641 return round_rate;
642}
643
644static unsigned long vtwm_pll_recalc_rate(struct clk_hw *hw,
645 unsigned long parent_rate)
646{
647 struct clk_pll *pll = to_clk_pll(hw);
648 u32 pll_val = readl(pll->reg);
649 unsigned long pll_freq;
650
651 switch (pll->type) {
652 case PLL_TYPE_VT8500:
653 pll_freq = parent_rate * VT8500_PLL_MUL(pll_val);
654 pll_freq /= VT8500_PLL_DIV(pll_val);
655 break;
656 case PLL_TYPE_WM8650:
657 pll_freq = parent_rate * WM8650_PLL_MUL(pll_val);
658 pll_freq /= WM8650_PLL_DIV(pll_val);
659 break;
abb165a8
TP
660 case PLL_TYPE_WM8750:
661 pll_freq = parent_rate * WM8750_PLL_MUL(pll_val);
662 pll_freq /= WM8750_PLL_DIV(pll_val);
663 break;
518d4709
TP
664 case PLL_TYPE_WM8850:
665 pll_freq = parent_rate * WM8850_PLL_MUL(pll_val);
666 pll_freq /= WM8850_PLL_DIV(pll_val);
667 break;
85814d69
TP
668 default:
669 pll_freq = 0;
670 }
671
672 return pll_freq;
673}
674
7acc3bca 675static const struct clk_ops vtwm_pll_ops = {
85814d69
TP
676 .round_rate = vtwm_pll_round_rate,
677 .set_rate = vtwm_pll_set_rate,
678 .recalc_rate = vtwm_pll_recalc_rate,
679};
680
681static __init void vtwm_pll_clk_init(struct device_node *node, int pll_type)
682{
683 u32 reg;
684 struct clk *clk;
685 struct clk_pll *pll_clk;
686 const char *clk_name = node->name;
687 const char *parent_name;
688 struct clk_init_data init;
689 int rc;
690
f9e4a18d
SH
691 if (!pmc_base)
692 vtwm_set_pmc_base();
693
85814d69
TP
694 rc = of_property_read_u32(node, "reg", &reg);
695 if (WARN_ON(rc))
696 return;
697
698 pll_clk = kzalloc(sizeof(*pll_clk), GFP_KERNEL);
699 if (WARN_ON(!pll_clk))
700 return;
701
702 pll_clk->reg = pmc_base + reg;
703 pll_clk->lock = &_lock;
704 pll_clk->type = pll_type;
705
706 of_property_read_string(node, "clock-output-names", &clk_name);
707
708 init.name = clk_name;
709 init.ops = &vtwm_pll_ops;
710 init.flags = 0;
711 parent_name = of_clk_get_parent_name(node, 0);
712 init.parent_names = &parent_name;
713 init.num_parents = 1;
714
715 pll_clk->hw.init = &init;
716
717 clk = clk_register(NULL, &pll_clk->hw);
718 if (WARN_ON(IS_ERR(clk))) {
719 kfree(pll_clk);
720 return;
721 }
722 rc = of_clk_add_provider(node, of_clk_src_simple_get, clk);
723 clk_register_clkdev(clk, clk_name, NULL);
724}
725
726
727/* Wrappers for initialization functions */
728
729static void __init vt8500_pll_init(struct device_node *node)
730{
731 vtwm_pll_clk_init(node, PLL_TYPE_VT8500);
732}
5b6e0adb 733CLK_OF_DECLARE(vt8500_pll, "via,vt8500-pll-clock", vt8500_pll_init);
85814d69
TP
734
735static void __init wm8650_pll_init(struct device_node *node)
736{
737 vtwm_pll_clk_init(node, PLL_TYPE_WM8650);
738}
5b6e0adb 739CLK_OF_DECLARE(wm8650_pll, "wm,wm8650-pll-clock", wm8650_pll_init);
85814d69 740
abb165a8
TP
741static void __init wm8750_pll_init(struct device_node *node)
742{
743 vtwm_pll_clk_init(node, PLL_TYPE_WM8750);
744}
5b6e0adb 745CLK_OF_DECLARE(wm8750_pll, "wm,wm8750-pll-clock", wm8750_pll_init);
85814d69 746
518d4709
TP
747static void __init wm8850_pll_init(struct device_node *node)
748{
749 vtwm_pll_clk_init(node, PLL_TYPE_WM8850);
750}
751CLK_OF_DECLARE(wm8850_pll, "wm,wm8850-pll-clock", wm8850_pll_init);