2 * Clock implementation for VIA/Wondermedia SoC's
3 * Copyright (C) 2012 Tony Prisk <linux@prisktech.co.nz>
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.
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.
18 #include <linux/slab.h>
19 #include <linux/bitops.h>
20 #include <linux/clkdev.h>
21 #include <linux/clk-provider.h>
23 /* All clocks share the same lock as none can be changed concurrently */
24 static DEFINE_SPINLOCK(_lock
);
28 void __iomem
*div_reg
;
29 unsigned int div_mask
;
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
42 #define PLL_TYPE_VT8500 0
43 #define PLL_TYPE_WM8650 1
44 #define PLL_TYPE_WM8750 2
45 #define PLL_TYPE_WM8850 3
54 static void __iomem
*pmc_base
;
56 #define to_clk_device(_hw) container_of(_hw, struct clk_device, hw)
58 #define VT8500_PMC_BUSY_MASK 0x18
60 static void vt8500_pmc_wait_busy(void)
62 while (readl(pmc_base
) & VT8500_PMC_BUSY_MASK
)
66 static int vt8500_dclk_enable(struct clk_hw
*hw
)
68 struct clk_device
*cdev
= to_clk_device(hw
);
70 unsigned long flags
= 0;
72 spin_lock_irqsave(cdev
->lock
, flags
);
74 en_val
= readl(cdev
->en_reg
);
75 en_val
|= BIT(cdev
->en_bit
);
76 writel(en_val
, cdev
->en_reg
);
78 spin_unlock_irqrestore(cdev
->lock
, flags
);
82 static void vt8500_dclk_disable(struct clk_hw
*hw
)
84 struct clk_device
*cdev
= to_clk_device(hw
);
86 unsigned long flags
= 0;
88 spin_lock_irqsave(cdev
->lock
, flags
);
90 en_val
= readl(cdev
->en_reg
);
91 en_val
&= ~BIT(cdev
->en_bit
);
92 writel(en_val
, cdev
->en_reg
);
94 spin_unlock_irqrestore(cdev
->lock
, flags
);
97 static int vt8500_dclk_is_enabled(struct clk_hw
*hw
)
99 struct clk_device
*cdev
= to_clk_device(hw
);
100 u32 en_val
= (readl(cdev
->en_reg
) & BIT(cdev
->en_bit
));
102 return en_val
? 1 : 0;
105 static unsigned long vt8500_dclk_recalc_rate(struct clk_hw
*hw
,
106 unsigned long parent_rate
)
108 struct clk_device
*cdev
= to_clk_device(hw
);
109 u32 div
= readl(cdev
->div_reg
) & cdev
->div_mask
;
111 /* Special case for SDMMC devices */
112 if ((cdev
->div_mask
== 0x3F) && (div
& BIT(5)))
113 div
= 64 * (div
& 0x1f);
115 /* div == 0 is actually the highest divisor */
117 div
= (cdev
->div_mask
+ 1);
119 return parent_rate
/ div
;
122 static long vt8500_dclk_round_rate(struct clk_hw
*hw
, unsigned long rate
,
123 unsigned long *prate
)
125 struct clk_device
*cdev
= to_clk_device(hw
);
131 divisor
= *prate
/ rate
;
133 /* If prate / rate would be decimal, incr the divisor */
134 if (rate
* divisor
< *prate
)
138 * If this is a request for SDMMC we have to adjust the divisor
139 * when >31 to use the fixed predivisor
141 if ((cdev
->div_mask
== 0x3F) && (divisor
> 31)) {
142 divisor
= 64 * ((divisor
/ 64) + 1);
145 return *prate
/ divisor
;
148 static int vt8500_dclk_set_rate(struct clk_hw
*hw
, unsigned long rate
,
149 unsigned long parent_rate
)
151 struct clk_device
*cdev
= to_clk_device(hw
);
153 unsigned long flags
= 0;
158 divisor
= parent_rate
/ rate
;
160 if (divisor
== cdev
->div_mask
+ 1)
163 /* SDMMC mask may need to be corrected before testing if its valid */
164 if ((cdev
->div_mask
== 0x3F) && (divisor
> 31)) {
166 * Bit 5 is a fixed /64 predivisor. If the requested divisor
167 * is >31 then correct for the fixed divisor being required.
169 divisor
= 0x20 + (divisor
/ 64);
172 if (divisor
> cdev
->div_mask
) {
173 pr_err("%s: invalid divisor for clock\n", __func__
);
177 spin_lock_irqsave(cdev
->lock
, flags
);
179 vt8500_pmc_wait_busy();
180 writel(divisor
, cdev
->div_reg
);
181 vt8500_pmc_wait_busy();
183 spin_unlock_irqrestore(cdev
->lock
, flags
);
189 static const struct clk_ops vt8500_gated_clk_ops
= {
190 .enable
= vt8500_dclk_enable
,
191 .disable
= vt8500_dclk_disable
,
192 .is_enabled
= vt8500_dclk_is_enabled
,
195 static const struct clk_ops vt8500_divisor_clk_ops
= {
196 .round_rate
= vt8500_dclk_round_rate
,
197 .set_rate
= vt8500_dclk_set_rate
,
198 .recalc_rate
= vt8500_dclk_recalc_rate
,
201 static const struct clk_ops vt8500_gated_divisor_clk_ops
= {
202 .enable
= vt8500_dclk_enable
,
203 .disable
= vt8500_dclk_disable
,
204 .is_enabled
= vt8500_dclk_is_enabled
,
205 .round_rate
= vt8500_dclk_round_rate
,
206 .set_rate
= vt8500_dclk_set_rate
,
207 .recalc_rate
= vt8500_dclk_recalc_rate
,
210 #define CLK_INIT_GATED BIT(0)
211 #define CLK_INIT_DIVISOR BIT(1)
212 #define CLK_INIT_GATED_DIVISOR (CLK_INIT_DIVISOR | CLK_INIT_GATED)
214 static __init
void vtwm_device_clk_init(struct device_node
*node
)
218 struct clk_device
*dev_clk
;
219 const char *clk_name
= node
->name
;
220 const char *parent_name
;
221 struct clk_init_data init
;
223 int clk_init_flags
= 0;
225 dev_clk
= kzalloc(sizeof(*dev_clk
), GFP_KERNEL
);
226 if (WARN_ON(!dev_clk
))
229 dev_clk
->lock
= &_lock
;
231 rc
= of_property_read_u32(node
, "enable-reg", &en_reg
);
233 dev_clk
->en_reg
= pmc_base
+ en_reg
;
234 rc
= of_property_read_u32(node
, "enable-bit", &dev_clk
->en_bit
);
236 pr_err("%s: enable-bit property required for gated clock\n",
240 clk_init_flags
|= CLK_INIT_GATED
;
243 rc
= of_property_read_u32(node
, "divisor-reg", &div_reg
);
245 dev_clk
->div_reg
= pmc_base
+ div_reg
;
247 * use 0x1f as the default mask since it covers
248 * almost all the clocks and reduces dts properties
250 dev_clk
->div_mask
= 0x1f;
252 of_property_read_u32(node
, "divisor-mask", &dev_clk
->div_mask
);
253 clk_init_flags
|= CLK_INIT_DIVISOR
;
256 of_property_read_string(node
, "clock-output-names", &clk_name
);
258 switch (clk_init_flags
) {
260 init
.ops
= &vt8500_gated_clk_ops
;
262 case CLK_INIT_DIVISOR
:
263 init
.ops
= &vt8500_divisor_clk_ops
;
265 case CLK_INIT_GATED_DIVISOR
:
266 init
.ops
= &vt8500_gated_divisor_clk_ops
;
269 pr_err("%s: Invalid clock description in device tree\n",
275 init
.name
= clk_name
;
277 parent_name
= of_clk_get_parent_name(node
, 0);
278 init
.parent_names
= &parent_name
;
279 init
.num_parents
= 1;
281 dev_clk
->hw
.init
= &init
;
283 clk
= clk_register(NULL
, &dev_clk
->hw
);
284 if (WARN_ON(IS_ERR(clk
))) {
288 rc
= of_clk_add_provider(node
, of_clk_src_simple_get
, clk
);
289 clk_register_clkdev(clk
, clk_name
, NULL
);
291 CLK_OF_DECLARE(vt8500_device
, "via,vt8500-device-clock", vtwm_device_clk_init
);
293 /* PLL clock related functions */
295 #define to_clk_pll(_hw) container_of(_hw, struct clk_pll, hw)
297 /* Helper macros for PLL_VT8500 */
298 #define VT8500_PLL_MUL(x) ((x & 0x1F) << 1)
299 #define VT8500_PLL_DIV(x) ((x & 0x100) ? 1 : 2)
301 #define VT8500_BITS_TO_FREQ(r, m, d) \
304 #define VT8500_BITS_TO_VAL(m, d) \
305 ((d == 2 ? 0 : 0x100) | ((m >> 1) & 0x1F))
307 /* Helper macros for PLL_WM8650 */
308 #define WM8650_PLL_MUL(x) (x & 0x3FF)
309 #define WM8650_PLL_DIV(x) (((x >> 10) & 7) * (1 << ((x >> 13) & 3)))
311 #define WM8650_BITS_TO_FREQ(r, m, d1, d2) \
312 (r * m / (d1 * (1 << d2)))
314 #define WM8650_BITS_TO_VAL(m, d1, d2) \
315 ((d2 << 13) | (d1 << 10) | (m & 0x3FF))
317 /* Helper macros for PLL_WM8750 */
318 #define WM8750_PLL_MUL(x) (((x >> 16) & 0xFF) + 1)
319 #define WM8750_PLL_DIV(x) ((((x >> 8) & 1) + 1) * (1 << (x & 7)))
321 #define WM8750_BITS_TO_FREQ(r, m, d1, d2) \
322 (r * (m+1) / ((d1+1) * (1 << d2)))
324 #define WM8750_BITS_TO_VAL(f, m, d1, d2) \
325 ((f << 24) | ((m - 1) << 16) | ((d1 - 1) << 8) | d2)
327 /* Helper macros for PLL_WM8850 */
328 #define WM8850_PLL_MUL(x) ((((x >> 16) & 0x7F) + 1) * 2)
329 #define WM8850_PLL_DIV(x) ((((x >> 8) & 1) + 1) * (1 << (x & 3)))
331 #define WM8850_BITS_TO_FREQ(r, m, d1, d2) \
332 (r * ((m + 1) * 2) / ((d1+1) * (1 << d2)))
334 #define WM8850_BITS_TO_VAL(m, d1, d2) \
335 ((((m / 2) - 1) << 16) | ((d1 - 1) << 8) | d2)
337 static void vt8500_find_pll_bits(unsigned long rate
, unsigned long parent_rate
,
338 u32
*multiplier
, u32
*prediv
)
343 if ((rate
< parent_rate
* 4) || (rate
> parent_rate
* 62)) {
344 pr_err("%s: requested rate out of range\n", __func__
);
349 if (rate
<= parent_rate
* 31)
350 /* use the prediv to double the resolution */
355 *multiplier
= rate
/ (parent_rate
/ *prediv
);
356 tclk
= (parent_rate
/ *prediv
) * *multiplier
;
359 pr_warn("%s: requested rate %lu, found rate %lu\n", __func__
,
363 static void wm8650_find_pll_bits(unsigned long rate
, unsigned long parent_rate
,
364 u32
*multiplier
, u32
*divisor1
, u32
*divisor2
)
367 u32 best_mul
, best_div1
, best_div2
;
368 unsigned long tclk
, rate_err
, best_err
;
370 best_err
= (unsigned long)-1;
372 /* Find the closest match (lower or equal to requested) */
373 for (div1
= 5; div1
>= 3; div1
--)
374 for (div2
= 3; div2
>= 0; div2
--)
375 for (mul
= 3; mul
<= 1023; mul
++) {
376 tclk
= parent_rate
* mul
/ (div1
* (1 << div2
));
379 /* error will always be +ve */
380 rate_err
= rate
- tclk
;
388 if (rate_err
< best_err
) {
396 /* if we got here, it wasn't an exact match */
397 pr_warn("%s: requested rate %lu, found rate %lu\n", __func__
, rate
,
399 *multiplier
= best_mul
;
400 *divisor1
= best_div1
;
401 *divisor2
= best_div2
;
404 static u32
wm8750_get_filter(u32 parent_rate
, u32 divisor1
)
406 /* calculate frequency (MHz) after pre-divisor */
407 u32 freq
= (parent_rate
/ 1000000) / (divisor1
+ 1);
409 if ((freq
< 10) || (freq
> 200))
410 pr_warn("%s: PLL recommended input frequency 10..200Mhz (requested %d Mhz)\n",
415 else if (freq
>= 104)
431 static void wm8750_find_pll_bits(unsigned long rate
, unsigned long parent_rate
,
432 u32
*filter
, u32
*multiplier
, u32
*divisor1
, u32
*divisor2
)
435 u32 best_mul
, best_div1
, best_div2
;
436 unsigned long tclk
, rate_err
, best_err
;
438 best_err
= (unsigned long)-1;
440 /* Find the closest match (lower or equal to requested) */
441 for (div1
= 1; div1
>= 0; div1
--)
442 for (div2
= 7; div2
>= 0; div2
--)
443 for (mul
= 0; mul
<= 255; mul
++) {
444 tclk
= parent_rate
* (mul
+ 1) / ((div1
+ 1) * (1 << div2
));
447 /* error will always be +ve */
448 rate_err
= rate
- tclk
;
450 *filter
= wm8750_get_filter(parent_rate
, div1
);
457 if (rate_err
< best_err
) {
465 /* if we got here, it wasn't an exact match */
466 pr_warn("%s: requested rate %lu, found rate %lu\n", __func__
, rate
,
469 *filter
= wm8750_get_filter(parent_rate
, best_div1
);
470 *multiplier
= best_mul
;
471 *divisor1
= best_div1
;
472 *divisor2
= best_div2
;
475 static void wm8850_find_pll_bits(unsigned long rate
, unsigned long parent_rate
,
476 u32
*multiplier
, u32
*divisor1
, u32
*divisor2
)
479 u32 best_mul
, best_div1
, best_div2
;
480 unsigned long tclk
, rate_err
, best_err
;
482 best_err
= (unsigned long)-1;
484 /* Find the closest match (lower or equal to requested) */
485 for (div1
= 1; div1
>= 0; div1
--)
486 for (div2
= 3; div2
>= 0; div2
--)
487 for (mul
= 0; mul
<= 127; mul
++) {
488 tclk
= parent_rate
* ((mul
+ 1) * 2) /
489 ((div1
+ 1) * (1 << div2
));
492 /* error will always be +ve */
493 rate_err
= rate
- tclk
;
501 if (rate_err
< best_err
) {
509 /* if we got here, it wasn't an exact match */
510 pr_warn("%s: requested rate %lu, found rate %lu\n", __func__
, rate
,
513 *multiplier
= best_mul
;
514 *divisor1
= best_div1
;
515 *divisor2
= best_div2
;
518 static int vtwm_pll_set_rate(struct clk_hw
*hw
, unsigned long rate
,
519 unsigned long parent_rate
)
521 struct clk_pll
*pll
= to_clk_pll(hw
);
522 u32 filter
, mul
, div1
, div2
;
524 unsigned long flags
= 0;
529 case PLL_TYPE_VT8500
:
530 vt8500_find_pll_bits(rate
, parent_rate
, &mul
, &div1
);
531 pll_val
= VT8500_BITS_TO_VAL(mul
, div1
);
533 case PLL_TYPE_WM8650
:
534 wm8650_find_pll_bits(rate
, parent_rate
, &mul
, &div1
, &div2
);
535 pll_val
= WM8650_BITS_TO_VAL(mul
, div1
, div2
);
537 case PLL_TYPE_WM8750
:
538 wm8750_find_pll_bits(rate
, parent_rate
, &filter
, &mul
, &div1
, &div2
);
539 pll_val
= WM8750_BITS_TO_VAL(filter
, mul
, div1
, div2
);
541 case PLL_TYPE_WM8850
:
542 wm8850_find_pll_bits(rate
, parent_rate
, &mul
, &div1
, &div2
);
543 pll_val
= WM8850_BITS_TO_VAL(mul
, div1
, div2
);
546 pr_err("%s: invalid pll type\n", __func__
);
550 spin_lock_irqsave(pll
->lock
, flags
);
552 vt8500_pmc_wait_busy();
553 writel(pll_val
, pll
->reg
);
554 vt8500_pmc_wait_busy();
556 spin_unlock_irqrestore(pll
->lock
, flags
);
561 static long vtwm_pll_round_rate(struct clk_hw
*hw
, unsigned long rate
,
562 unsigned long *prate
)
564 struct clk_pll
*pll
= to_clk_pll(hw
);
565 u32 filter
, mul
, div1
, div2
;
569 case PLL_TYPE_VT8500
:
570 vt8500_find_pll_bits(rate
, *prate
, &mul
, &div1
);
571 round_rate
= VT8500_BITS_TO_FREQ(*prate
, mul
, div1
);
573 case PLL_TYPE_WM8650
:
574 wm8650_find_pll_bits(rate
, *prate
, &mul
, &div1
, &div2
);
575 round_rate
= WM8650_BITS_TO_FREQ(*prate
, mul
, div1
, div2
);
577 case PLL_TYPE_WM8750
:
578 wm8750_find_pll_bits(rate
, *prate
, &filter
, &mul
, &div1
, &div2
);
579 round_rate
= WM8750_BITS_TO_FREQ(*prate
, mul
, div1
, div2
);
581 case PLL_TYPE_WM8850
:
582 wm8850_find_pll_bits(rate
, *prate
, &mul
, &div1
, &div2
);
583 round_rate
= WM8850_BITS_TO_FREQ(*prate
, mul
, div1
, div2
);
592 static unsigned long vtwm_pll_recalc_rate(struct clk_hw
*hw
,
593 unsigned long parent_rate
)
595 struct clk_pll
*pll
= to_clk_pll(hw
);
596 u32 pll_val
= readl(pll
->reg
);
597 unsigned long pll_freq
;
600 case PLL_TYPE_VT8500
:
601 pll_freq
= parent_rate
* VT8500_PLL_MUL(pll_val
);
602 pll_freq
/= VT8500_PLL_DIV(pll_val
);
604 case PLL_TYPE_WM8650
:
605 pll_freq
= parent_rate
* WM8650_PLL_MUL(pll_val
);
606 pll_freq
/= WM8650_PLL_DIV(pll_val
);
608 case PLL_TYPE_WM8750
:
609 pll_freq
= parent_rate
* WM8750_PLL_MUL(pll_val
);
610 pll_freq
/= WM8750_PLL_DIV(pll_val
);
612 case PLL_TYPE_WM8850
:
613 pll_freq
= parent_rate
* WM8850_PLL_MUL(pll_val
);
614 pll_freq
/= WM8850_PLL_DIV(pll_val
);
623 const struct clk_ops vtwm_pll_ops
= {
624 .round_rate
= vtwm_pll_round_rate
,
625 .set_rate
= vtwm_pll_set_rate
,
626 .recalc_rate
= vtwm_pll_recalc_rate
,
629 static __init
void vtwm_pll_clk_init(struct device_node
*node
, int pll_type
)
633 struct clk_pll
*pll_clk
;
634 const char *clk_name
= node
->name
;
635 const char *parent_name
;
636 struct clk_init_data init
;
639 rc
= of_property_read_u32(node
, "reg", ®
);
643 pll_clk
= kzalloc(sizeof(*pll_clk
), GFP_KERNEL
);
644 if (WARN_ON(!pll_clk
))
647 pll_clk
->reg
= pmc_base
+ reg
;
648 pll_clk
->lock
= &_lock
;
649 pll_clk
->type
= pll_type
;
651 of_property_read_string(node
, "clock-output-names", &clk_name
);
653 init
.name
= clk_name
;
654 init
.ops
= &vtwm_pll_ops
;
656 parent_name
= of_clk_get_parent_name(node
, 0);
657 init
.parent_names
= &parent_name
;
658 init
.num_parents
= 1;
660 pll_clk
->hw
.init
= &init
;
662 clk
= clk_register(NULL
, &pll_clk
->hw
);
663 if (WARN_ON(IS_ERR(clk
))) {
667 rc
= of_clk_add_provider(node
, of_clk_src_simple_get
, clk
);
668 clk_register_clkdev(clk
, clk_name
, NULL
);
672 /* Wrappers for initialization functions */
674 static void __init
vt8500_pll_init(struct device_node
*node
)
676 vtwm_pll_clk_init(node
, PLL_TYPE_VT8500
);
678 CLK_OF_DECLARE(vt8500_pll
, "via,vt8500-pll-clock", vt8500_pll_init
);
680 static void __init
wm8650_pll_init(struct device_node
*node
)
682 vtwm_pll_clk_init(node
, PLL_TYPE_WM8650
);
684 CLK_OF_DECLARE(wm8650_pll
, "wm,wm8650-pll-clock", wm8650_pll_init
);
686 static void __init
wm8750_pll_init(struct device_node
*node
)
688 vtwm_pll_clk_init(node
, PLL_TYPE_WM8750
);
690 CLK_OF_DECLARE(wm8750_pll
, "wm,wm8750-pll-clock", wm8750_pll_init
);
692 static void __init
wm8850_pll_init(struct device_node
*node
)
694 vtwm_pll_clk_init(node
, PLL_TYPE_WM8850
);
696 CLK_OF_DECLARE(wm8850_pll
, "wm,wm8850-pll-clock", wm8850_pll_init
);
698 void __init
vtwm_clk_init(void __iomem
*base
)