2 * Ingenic SoC CGU driver
4 * Copyright (c) 2013-2015 Imagination Technologies
5 * Author: Paul Burton <paul.burton@imgtec.com>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of
10 * the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
18 #include <linux/bitops.h>
19 #include <linux/clk-provider.h>
20 #include <linux/clkdev.h>
21 #include <linux/delay.h>
22 #include <linux/math64.h>
24 #include <linux/of_address.h>
25 #include <linux/slab.h>
26 #include <linux/spinlock.h>
29 #define MHZ (1000 * 1000)
32 * ingenic_cgu_gate_get() - get the value of clock gate register bit
33 * @cgu: reference to the CGU whose registers should be read
34 * @info: info struct describing the gate bit
36 * Retrieves the state of the clock gate bit described by info. The
37 * caller must hold cgu->lock.
39 * Return: true if the gate bit is set, else false.
42 ingenic_cgu_gate_get(struct ingenic_cgu
*cgu
,
43 const struct ingenic_cgu_gate_info
*info
)
45 return readl(cgu
->base
+ info
->reg
) & BIT(info
->bit
);
49 * ingenic_cgu_gate_set() - set the value of clock gate register bit
50 * @cgu: reference to the CGU whose registers should be modified
51 * @info: info struct describing the gate bit
52 * @val: non-zero to gate a clock, otherwise zero
54 * Sets the given gate bit in order to gate or ungate a clock.
56 * The caller must hold cgu->lock.
59 ingenic_cgu_gate_set(struct ingenic_cgu
*cgu
,
60 const struct ingenic_cgu_gate_info
*info
, bool val
)
62 u32 clkgr
= readl(cgu
->base
+ info
->reg
);
65 clkgr
|= BIT(info
->bit
);
67 clkgr
&= ~BIT(info
->bit
);
69 writel(clkgr
, cgu
->base
+ info
->reg
);
77 ingenic_pll_recalc_rate(struct clk_hw
*hw
, unsigned long parent_rate
)
79 struct ingenic_clk
*ingenic_clk
= to_ingenic_clk(hw
);
80 struct ingenic_cgu
*cgu
= ingenic_clk
->cgu
;
81 const struct ingenic_cgu_clk_info
*clk_info
;
82 const struct ingenic_cgu_pll_info
*pll_info
;
83 unsigned m
, n
, od_enc
, od
;
88 clk_info
= &cgu
->clock_info
[ingenic_clk
->idx
];
89 BUG_ON(clk_info
->type
!= CGU_CLK_PLL
);
90 pll_info
= &clk_info
->pll
;
92 spin_lock_irqsave(&cgu
->lock
, flags
);
93 ctl
= readl(cgu
->base
+ pll_info
->reg
);
94 spin_unlock_irqrestore(&cgu
->lock
, flags
);
96 m
= (ctl
>> pll_info
->m_shift
) & GENMASK(pll_info
->m_bits
- 1, 0);
97 m
+= pll_info
->m_offset
;
98 n
= (ctl
>> pll_info
->n_shift
) & GENMASK(pll_info
->n_bits
- 1, 0);
99 n
+= pll_info
->n_offset
;
100 od_enc
= ctl
>> pll_info
->od_shift
;
101 od_enc
&= GENMASK(pll_info
->od_bits
- 1, 0);
102 bypass
= !!(ctl
& BIT(pll_info
->bypass_bit
));
103 enable
= !!(ctl
& BIT(pll_info
->enable_bit
));
111 for (od
= 0; od
< pll_info
->od_max
; od
++) {
112 if (pll_info
->od_encoding
[od
] == od_enc
)
115 BUG_ON(od
== pll_info
->od_max
);
118 return div_u64((u64
)parent_rate
* m
, n
* od
);
122 ingenic_pll_calc(const struct ingenic_cgu_clk_info
*clk_info
,
123 unsigned long rate
, unsigned long parent_rate
,
124 unsigned *pm
, unsigned *pn
, unsigned *pod
)
126 const struct ingenic_cgu_pll_info
*pll_info
;
129 pll_info
= &clk_info
->pll
;
133 * The frequency after the input divider must be between 10 and 50 MHz.
134 * The highest divider yields the best resolution.
136 n
= parent_rate
/ (10 * MHZ
);
137 n
= min_t(unsigned, n
, 1 << clk_info
->pll
.n_bits
);
138 n
= max_t(unsigned, n
, pll_info
->n_offset
);
140 m
= (rate
/ MHZ
) * od
* n
/ (parent_rate
/ MHZ
);
141 m
= min_t(unsigned, m
, 1 << clk_info
->pll
.m_bits
);
142 m
= max_t(unsigned, m
, pll_info
->m_offset
);
151 return div_u64((u64
)parent_rate
* m
, n
* od
);
155 ingenic_pll_round_rate(struct clk_hw
*hw
, unsigned long req_rate
,
156 unsigned long *prate
)
158 struct ingenic_clk
*ingenic_clk
= to_ingenic_clk(hw
);
159 struct ingenic_cgu
*cgu
= ingenic_clk
->cgu
;
160 const struct ingenic_cgu_clk_info
*clk_info
;
162 clk_info
= &cgu
->clock_info
[ingenic_clk
->idx
];
163 BUG_ON(clk_info
->type
!= CGU_CLK_PLL
);
165 return ingenic_pll_calc(clk_info
, req_rate
, *prate
, NULL
, NULL
, NULL
);
169 ingenic_pll_set_rate(struct clk_hw
*hw
, unsigned long req_rate
,
170 unsigned long parent_rate
)
172 const unsigned timeout
= 100;
173 struct ingenic_clk
*ingenic_clk
= to_ingenic_clk(hw
);
174 struct ingenic_cgu
*cgu
= ingenic_clk
->cgu
;
175 const struct ingenic_cgu_clk_info
*clk_info
;
176 const struct ingenic_cgu_pll_info
*pll_info
;
177 unsigned long rate
, flags
;
178 unsigned m
, n
, od
, i
;
181 clk_info
= &cgu
->clock_info
[ingenic_clk
->idx
];
182 BUG_ON(clk_info
->type
!= CGU_CLK_PLL
);
183 pll_info
= &clk_info
->pll
;
185 rate
= ingenic_pll_calc(clk_info
, req_rate
, parent_rate
,
187 if (rate
!= req_rate
)
188 pr_info("ingenic-cgu: request '%s' rate %luHz, actual %luHz\n",
189 clk_info
->name
, req_rate
, rate
);
191 spin_lock_irqsave(&cgu
->lock
, flags
);
192 ctl
= readl(cgu
->base
+ pll_info
->reg
);
194 ctl
&= ~(GENMASK(pll_info
->m_bits
- 1, 0) << pll_info
->m_shift
);
195 ctl
|= (m
- pll_info
->m_offset
) << pll_info
->m_shift
;
197 ctl
&= ~(GENMASK(pll_info
->n_bits
- 1, 0) << pll_info
->n_shift
);
198 ctl
|= (n
- pll_info
->n_offset
) << pll_info
->n_shift
;
200 ctl
&= ~(GENMASK(pll_info
->od_bits
- 1, 0) << pll_info
->od_shift
);
201 ctl
|= pll_info
->od_encoding
[od
- 1] << pll_info
->od_shift
;
203 ctl
&= ~BIT(pll_info
->bypass_bit
);
204 ctl
|= BIT(pll_info
->enable_bit
);
206 writel(ctl
, cgu
->base
+ pll_info
->reg
);
208 /* wait for the PLL to stabilise */
209 for (i
= 0; i
< timeout
; i
++) {
210 ctl
= readl(cgu
->base
+ pll_info
->reg
);
211 if (ctl
& BIT(pll_info
->stable_bit
))
216 spin_unlock_irqrestore(&cgu
->lock
, flags
);
224 static const struct clk_ops ingenic_pll_ops
= {
225 .recalc_rate
= ingenic_pll_recalc_rate
,
226 .round_rate
= ingenic_pll_round_rate
,
227 .set_rate
= ingenic_pll_set_rate
,
231 * Operations for all non-PLL clocks
234 static u8
ingenic_clk_get_parent(struct clk_hw
*hw
)
236 struct ingenic_clk
*ingenic_clk
= to_ingenic_clk(hw
);
237 struct ingenic_cgu
*cgu
= ingenic_clk
->cgu
;
238 const struct ingenic_cgu_clk_info
*clk_info
;
240 u8 i
, hw_idx
, idx
= 0;
242 clk_info
= &cgu
->clock_info
[ingenic_clk
->idx
];
244 if (clk_info
->type
& CGU_CLK_MUX
) {
245 reg
= readl(cgu
->base
+ clk_info
->mux
.reg
);
246 hw_idx
= (reg
>> clk_info
->mux
.shift
) &
247 GENMASK(clk_info
->mux
.bits
- 1, 0);
250 * Convert the hardware index to the parent index by skipping
251 * over any -1's in the parents array.
253 for (i
= 0; i
< hw_idx
; i
++) {
254 if (clk_info
->parents
[i
] != -1)
262 static int ingenic_clk_set_parent(struct clk_hw
*hw
, u8 idx
)
264 struct ingenic_clk
*ingenic_clk
= to_ingenic_clk(hw
);
265 struct ingenic_cgu
*cgu
= ingenic_clk
->cgu
;
266 const struct ingenic_cgu_clk_info
*clk_info
;
268 u8 curr_idx
, hw_idx
, num_poss
;
271 clk_info
= &cgu
->clock_info
[ingenic_clk
->idx
];
273 if (clk_info
->type
& CGU_CLK_MUX
) {
275 * Convert the parent index to the hardware index by adding
276 * 1 for any -1 in the parents array preceding the given
277 * index. That is, we want the index of idx'th entry in
278 * clk_info->parents which does not equal -1.
280 hw_idx
= curr_idx
= 0;
281 num_poss
= 1 << clk_info
->mux
.bits
;
282 for (; hw_idx
< num_poss
; hw_idx
++) {
283 if (clk_info
->parents
[hw_idx
] == -1)
290 /* idx should always be a valid parent */
291 BUG_ON(curr_idx
!= idx
);
293 mask
= GENMASK(clk_info
->mux
.bits
- 1, 0);
294 mask
<<= clk_info
->mux
.shift
;
296 spin_lock_irqsave(&cgu
->lock
, flags
);
298 /* write the register */
299 reg
= readl(cgu
->base
+ clk_info
->mux
.reg
);
301 reg
|= hw_idx
<< clk_info
->mux
.shift
;
302 writel(reg
, cgu
->base
+ clk_info
->mux
.reg
);
304 spin_unlock_irqrestore(&cgu
->lock
, flags
);
308 return idx
? -EINVAL
: 0;
312 ingenic_clk_recalc_rate(struct clk_hw
*hw
, unsigned long parent_rate
)
314 struct ingenic_clk
*ingenic_clk
= to_ingenic_clk(hw
);
315 struct ingenic_cgu
*cgu
= ingenic_clk
->cgu
;
316 const struct ingenic_cgu_clk_info
*clk_info
;
317 unsigned long rate
= parent_rate
;
320 clk_info
= &cgu
->clock_info
[ingenic_clk
->idx
];
322 if (clk_info
->type
& CGU_CLK_DIV
) {
323 div_reg
= readl(cgu
->base
+ clk_info
->div
.reg
);
324 div
= (div_reg
>> clk_info
->div
.shift
) &
325 GENMASK(clk_info
->div
.bits
- 1, 0);
335 ingenic_clk_calc_div(const struct ingenic_cgu_clk_info
*clk_info
,
336 unsigned long parent_rate
, unsigned long req_rate
)
340 /* calculate the divide */
341 div
= DIV_ROUND_UP(parent_rate
, req_rate
);
343 /* and impose hardware constraints */
344 div
= min_t(unsigned, div
, 1 << clk_info
->div
.bits
);
345 div
= max_t(unsigned, div
, 1);
351 ingenic_clk_round_rate(struct clk_hw
*hw
, unsigned long req_rate
,
352 unsigned long *parent_rate
)
354 struct ingenic_clk
*ingenic_clk
= to_ingenic_clk(hw
);
355 struct ingenic_cgu
*cgu
= ingenic_clk
->cgu
;
356 const struct ingenic_cgu_clk_info
*clk_info
;
357 long rate
= *parent_rate
;
359 clk_info
= &cgu
->clock_info
[ingenic_clk
->idx
];
361 if (clk_info
->type
& CGU_CLK_DIV
)
362 rate
/= ingenic_clk_calc_div(clk_info
, *parent_rate
, req_rate
);
363 else if (clk_info
->type
& CGU_CLK_FIXDIV
)
364 rate
/= clk_info
->fixdiv
.div
;
370 ingenic_clk_set_rate(struct clk_hw
*hw
, unsigned long req_rate
,
371 unsigned long parent_rate
)
373 struct ingenic_clk
*ingenic_clk
= to_ingenic_clk(hw
);
374 struct ingenic_cgu
*cgu
= ingenic_clk
->cgu
;
375 const struct ingenic_cgu_clk_info
*clk_info
;
376 const unsigned timeout
= 100;
377 unsigned long rate
, flags
;
382 clk_info
= &cgu
->clock_info
[ingenic_clk
->idx
];
384 if (clk_info
->type
& CGU_CLK_DIV
) {
385 div
= ingenic_clk_calc_div(clk_info
, parent_rate
, req_rate
);
386 rate
= parent_rate
/ div
;
388 if (rate
!= req_rate
)
391 spin_lock_irqsave(&cgu
->lock
, flags
);
392 reg
= readl(cgu
->base
+ clk_info
->div
.reg
);
394 /* update the divide */
395 mask
= GENMASK(clk_info
->div
.bits
- 1, 0);
396 reg
&= ~(mask
<< clk_info
->div
.shift
);
397 reg
|= (div
- 1) << clk_info
->div
.shift
;
399 /* clear the stop bit */
400 if (clk_info
->div
.stop_bit
!= -1)
401 reg
&= ~BIT(clk_info
->div
.stop_bit
);
403 /* set the change enable bit */
404 if (clk_info
->div
.ce_bit
!= -1)
405 reg
|= BIT(clk_info
->div
.ce_bit
);
407 /* update the hardware */
408 writel(reg
, cgu
->base
+ clk_info
->div
.reg
);
410 /* wait for the change to take effect */
411 if (clk_info
->div
.busy_bit
!= -1) {
412 for (i
= 0; i
< timeout
; i
++) {
413 reg
= readl(cgu
->base
+ clk_info
->div
.reg
);
414 if (!(reg
& BIT(clk_info
->div
.busy_bit
)))
422 spin_unlock_irqrestore(&cgu
->lock
, flags
);
429 static int ingenic_clk_enable(struct clk_hw
*hw
)
431 struct ingenic_clk
*ingenic_clk
= to_ingenic_clk(hw
);
432 struct ingenic_cgu
*cgu
= ingenic_clk
->cgu
;
433 const struct ingenic_cgu_clk_info
*clk_info
;
436 clk_info
= &cgu
->clock_info
[ingenic_clk
->idx
];
438 if (clk_info
->type
& CGU_CLK_GATE
) {
439 /* ungate the clock */
440 spin_lock_irqsave(&cgu
->lock
, flags
);
441 ingenic_cgu_gate_set(cgu
, &clk_info
->gate
, false);
442 spin_unlock_irqrestore(&cgu
->lock
, flags
);
448 static void ingenic_clk_disable(struct clk_hw
*hw
)
450 struct ingenic_clk
*ingenic_clk
= to_ingenic_clk(hw
);
451 struct ingenic_cgu
*cgu
= ingenic_clk
->cgu
;
452 const struct ingenic_cgu_clk_info
*clk_info
;
455 clk_info
= &cgu
->clock_info
[ingenic_clk
->idx
];
457 if (clk_info
->type
& CGU_CLK_GATE
) {
459 spin_lock_irqsave(&cgu
->lock
, flags
);
460 ingenic_cgu_gate_set(cgu
, &clk_info
->gate
, true);
461 spin_unlock_irqrestore(&cgu
->lock
, flags
);
465 static int ingenic_clk_is_enabled(struct clk_hw
*hw
)
467 struct ingenic_clk
*ingenic_clk
= to_ingenic_clk(hw
);
468 struct ingenic_cgu
*cgu
= ingenic_clk
->cgu
;
469 const struct ingenic_cgu_clk_info
*clk_info
;
473 clk_info
= &cgu
->clock_info
[ingenic_clk
->idx
];
475 if (clk_info
->type
& CGU_CLK_GATE
) {
476 spin_lock_irqsave(&cgu
->lock
, flags
);
477 enabled
= !ingenic_cgu_gate_get(cgu
, &clk_info
->gate
);
478 spin_unlock_irqrestore(&cgu
->lock
, flags
);
484 static const struct clk_ops ingenic_clk_ops
= {
485 .get_parent
= ingenic_clk_get_parent
,
486 .set_parent
= ingenic_clk_set_parent
,
488 .recalc_rate
= ingenic_clk_recalc_rate
,
489 .round_rate
= ingenic_clk_round_rate
,
490 .set_rate
= ingenic_clk_set_rate
,
492 .enable
= ingenic_clk_enable
,
493 .disable
= ingenic_clk_disable
,
494 .is_enabled
= ingenic_clk_is_enabled
,
501 static int ingenic_register_clock(struct ingenic_cgu
*cgu
, unsigned idx
)
503 const struct ingenic_cgu_clk_info
*clk_info
= &cgu
->clock_info
[idx
];
504 struct clk_init_data clk_init
;
505 struct ingenic_clk
*ingenic_clk
= NULL
;
506 struct clk
*clk
, *parent
;
507 const char *parent_names
[4];
508 unsigned caps
, i
, num_possible
;
511 BUILD_BUG_ON(ARRAY_SIZE(clk_info
->parents
) > ARRAY_SIZE(parent_names
));
513 if (clk_info
->type
== CGU_CLK_EXT
) {
514 clk
= of_clk_get_by_name(cgu
->np
, clk_info
->name
);
516 pr_err("%s: no external clock '%s' provided\n",
517 __func__
, clk_info
->name
);
521 err
= clk_register_clkdev(clk
, clk_info
->name
, NULL
);
526 cgu
->clocks
.clks
[idx
] = clk
;
530 if (!clk_info
->type
) {
531 pr_err("%s: no clock type specified for '%s'\n", __func__
,
536 ingenic_clk
= kzalloc(sizeof(*ingenic_clk
), GFP_KERNEL
);
542 ingenic_clk
->hw
.init
= &clk_init
;
543 ingenic_clk
->cgu
= cgu
;
544 ingenic_clk
->idx
= idx
;
546 clk_init
.name
= clk_info
->name
;
548 clk_init
.parent_names
= parent_names
;
550 caps
= clk_info
->type
;
552 if (caps
& (CGU_CLK_MUX
| CGU_CLK_CUSTOM
)) {
553 clk_init
.num_parents
= 0;
555 if (caps
& CGU_CLK_MUX
)
556 num_possible
= 1 << clk_info
->mux
.bits
;
558 num_possible
= ARRAY_SIZE(clk_info
->parents
);
560 for (i
= 0; i
< num_possible
; i
++) {
561 if (clk_info
->parents
[i
] == -1)
564 parent
= cgu
->clocks
.clks
[clk_info
->parents
[i
]];
565 parent_names
[clk_init
.num_parents
] =
566 __clk_get_name(parent
);
567 clk_init
.num_parents
++;
570 BUG_ON(!clk_init
.num_parents
);
571 BUG_ON(clk_init
.num_parents
> ARRAY_SIZE(parent_names
));
573 BUG_ON(clk_info
->parents
[0] == -1);
574 clk_init
.num_parents
= 1;
575 parent
= cgu
->clocks
.clks
[clk_info
->parents
[0]];
576 parent_names
[0] = __clk_get_name(parent
);
579 if (caps
& CGU_CLK_CUSTOM
) {
580 clk_init
.ops
= clk_info
->custom
.clk_ops
;
582 caps
&= ~CGU_CLK_CUSTOM
;
585 pr_err("%s: custom clock may not be combined with type 0x%x\n",
589 } else if (caps
& CGU_CLK_PLL
) {
590 clk_init
.ops
= &ingenic_pll_ops
;
592 caps
&= ~CGU_CLK_PLL
;
595 pr_err("%s: PLL may not be combined with type 0x%x\n",
600 clk_init
.ops
= &ingenic_clk_ops
;
603 /* nothing to do for gates or fixed dividers */
604 caps
&= ~(CGU_CLK_GATE
| CGU_CLK_FIXDIV
);
606 if (caps
& CGU_CLK_MUX
) {
607 if (!(caps
& CGU_CLK_MUX_GLITCHFREE
))
608 clk_init
.flags
|= CLK_SET_PARENT_GATE
;
610 caps
&= ~(CGU_CLK_MUX
| CGU_CLK_MUX_GLITCHFREE
);
613 if (caps
& CGU_CLK_DIV
) {
614 caps
&= ~CGU_CLK_DIV
;
616 /* pass rate changes to the parent clock */
617 clk_init
.flags
|= CLK_SET_RATE_PARENT
;
621 pr_err("%s: unknown clock type 0x%x\n", __func__
, caps
);
625 clk
= clk_register(NULL
, &ingenic_clk
->hw
);
627 pr_err("%s: failed to register clock '%s'\n", __func__
,
633 err
= clk_register_clkdev(clk
, clk_info
->name
, NULL
);
637 cgu
->clocks
.clks
[idx
] = clk
;
645 ingenic_cgu_new(const struct ingenic_cgu_clk_info
*clock_info
,
646 unsigned num_clocks
, struct device_node
*np
)
648 struct ingenic_cgu
*cgu
;
650 cgu
= kzalloc(sizeof(*cgu
), GFP_KERNEL
);
654 cgu
->base
= of_iomap(np
, 0);
656 pr_err("%s: failed to map CGU registers\n", __func__
);
661 cgu
->clock_info
= clock_info
;
662 cgu
->clocks
.clk_num
= num_clocks
;
664 spin_lock_init(&cgu
->lock
);
674 int ingenic_cgu_register_clocks(struct ingenic_cgu
*cgu
)
679 cgu
->clocks
.clks
= kcalloc(cgu
->clocks
.clk_num
, sizeof(struct clk
*),
681 if (!cgu
->clocks
.clks
) {
686 for (i
= 0; i
< cgu
->clocks
.clk_num
; i
++) {
687 err
= ingenic_register_clock(cgu
, i
);
689 goto err_out_unregister
;
692 err
= of_clk_add_provider(cgu
->np
, of_clk_src_onecell_get
,
695 goto err_out_unregister
;
700 for (i
= 0; i
< cgu
->clocks
.clk_num
; i
++) {
701 if (!cgu
->clocks
.clks
[i
])
703 if (cgu
->clock_info
[i
].type
& CGU_CLK_EXT
)
704 clk_put(cgu
->clocks
.clks
[i
]);
706 clk_unregister(cgu
->clocks
.clks
[i
]);
708 kfree(cgu
->clocks
.clks
);