1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Ingenic SoC CGU driver
4 *
5 * Copyright (c) 2013-2015 Imagination Technologies
6 * Author: Paul Burton <paul.burton@mips.com>
7 */
8
9 #include <linux/bitops.h>
10 #include <linux/clk.h>
11 #include <linux/clk-provider.h>
12 #include <linux/clkdev.h>
13 #include <linux/delay.h>
14 #include <linux/io.h>
15 #include <linux/math64.h>
16 #include <linux/of.h>
17 #include <linux/of_address.h>
18 #include <linux/slab.h>
19 #include <linux/spinlock.h>
20 #include "cgu.h"
21
22 #define MHZ (1000 * 1000)
23
24 /**
25 * ingenic_cgu_gate_get() - get the value of clock gate register bit
26 * @cgu: reference to the CGU whose registers should be read
27 * @info: info struct describing the gate bit
28 *
29 * Retrieves the state of the clock gate bit described by info. The
30 * caller must hold cgu->lock.
31 *
32 * Return: true if the gate bit is set, else false.
33 */
34 static inline bool
ingenic_cgu_gate_get(struct ingenic_cgu * cgu,const struct ingenic_cgu_gate_info * info)35 ingenic_cgu_gate_get(struct ingenic_cgu *cgu,
36 const struct ingenic_cgu_gate_info *info)
37 {
38 return !!(readl(cgu->base + info->reg) & BIT(info->bit))
39 ^ info->clear_to_gate;
40 }
41
42 /**
43 * ingenic_cgu_gate_set() - set the value of clock gate register bit
44 * @cgu: reference to the CGU whose registers should be modified
45 * @info: info struct describing the gate bit
46 * @val: non-zero to gate a clock, otherwise zero
47 *
48 * Sets the given gate bit in order to gate or ungate a clock.
49 *
50 * The caller must hold cgu->lock.
51 */
52 static inline void
ingenic_cgu_gate_set(struct ingenic_cgu * cgu,const struct ingenic_cgu_gate_info * info,bool val)53 ingenic_cgu_gate_set(struct ingenic_cgu *cgu,
54 const struct ingenic_cgu_gate_info *info, bool val)
55 {
56 u32 clkgr = readl(cgu->base + info->reg);
57
58 if (val ^ info->clear_to_gate)
59 clkgr |= BIT(info->bit);
60 else
61 clkgr &= ~BIT(info->bit);
62
63 writel(clkgr, cgu->base + info->reg);
64 }
65
66 /*
67 * PLL operations
68 */
69
70 static unsigned long
ingenic_pll_recalc_rate(struct clk_hw * hw,unsigned long parent_rate)71 ingenic_pll_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
72 {
73 struct ingenic_clk *ingenic_clk = to_ingenic_clk(hw);
74 struct ingenic_cgu *cgu = ingenic_clk->cgu;
75 const struct ingenic_cgu_clk_info *clk_info;
76 const struct ingenic_cgu_pll_info *pll_info;
77 unsigned m, n, od_enc, od;
78 bool bypass;
79 unsigned long flags;
80 u32 ctl;
81
82 clk_info = &cgu->clock_info[ingenic_clk->idx];
83 BUG_ON(clk_info->type != CGU_CLK_PLL);
84 pll_info = &clk_info->pll;
85
86 spin_lock_irqsave(&cgu->lock, flags);
87 ctl = readl(cgu->base + pll_info->reg);
88 spin_unlock_irqrestore(&cgu->lock, flags);
89
90 m = (ctl >> pll_info->m_shift) & GENMASK(pll_info->m_bits - 1, 0);
91 m += pll_info->m_offset;
92 n = (ctl >> pll_info->n_shift) & GENMASK(pll_info->n_bits - 1, 0);
93 n += pll_info->n_offset;
94 od_enc = ctl >> pll_info->od_shift;
95 od_enc &= GENMASK(pll_info->od_bits - 1, 0);
96 bypass = !pll_info->no_bypass_bit &&
97 !!(ctl & BIT(pll_info->bypass_bit));
98
99 if (bypass)
100 return parent_rate;
101
102 for (od = 0; od < pll_info->od_max; od++) {
103 if (pll_info->od_encoding[od] == od_enc)
104 break;
105 }
106 BUG_ON(od == pll_info->od_max);
107 od++;
108
109 return div_u64((u64)parent_rate * m, n * od);
110 }
111
112 static unsigned long
ingenic_pll_calc(const struct ingenic_cgu_clk_info * clk_info,unsigned long rate,unsigned long parent_rate,unsigned * pm,unsigned * pn,unsigned * pod)113 ingenic_pll_calc(const struct ingenic_cgu_clk_info *clk_info,
114 unsigned long rate, unsigned long parent_rate,
115 unsigned *pm, unsigned *pn, unsigned *pod)
116 {
117 const struct ingenic_cgu_pll_info *pll_info;
118 unsigned m, n, od;
119
120 pll_info = &clk_info->pll;
121 od = 1;
122
123 /*
124 * The frequency after the input divider must be between 10 and 50 MHz.
125 * The highest divider yields the best resolution.
126 */
127 n = parent_rate / (10 * MHZ);
128 n = min_t(unsigned, n, 1 << clk_info->pll.n_bits);
129 n = max_t(unsigned, n, pll_info->n_offset);
130
131 m = (rate / MHZ) * od * n / (parent_rate / MHZ);
132 m = min_t(unsigned, m, 1 << clk_info->pll.m_bits);
133 m = max_t(unsigned, m, pll_info->m_offset);
134
135 if (pm)
136 *pm = m;
137 if (pn)
138 *pn = n;
139 if (pod)
140 *pod = od;
141
142 return div_u64((u64)parent_rate * m, n * od);
143 }
144
to_clk_info(struct ingenic_clk * ingenic_clk)145 static inline const struct ingenic_cgu_clk_info *to_clk_info(
146 struct ingenic_clk *ingenic_clk)
147 {
148 struct ingenic_cgu *cgu = ingenic_clk->cgu;
149 const struct ingenic_cgu_clk_info *clk_info;
150
151 clk_info = &cgu->clock_info[ingenic_clk->idx];
152 BUG_ON(clk_info->type != CGU_CLK_PLL);
153
154 return clk_info;
155 }
156
157 static long
ingenic_pll_round_rate(struct clk_hw * hw,unsigned long req_rate,unsigned long * prate)158 ingenic_pll_round_rate(struct clk_hw *hw, unsigned long req_rate,
159 unsigned long *prate)
160 {
161 struct ingenic_clk *ingenic_clk = to_ingenic_clk(hw);
162 const struct ingenic_cgu_clk_info *clk_info = to_clk_info(ingenic_clk);
163
164 return ingenic_pll_calc(clk_info, req_rate, *prate, NULL, NULL, NULL);
165 }
166
167 static int
ingenic_pll_set_rate(struct clk_hw * hw,unsigned long req_rate,unsigned long parent_rate)168 ingenic_pll_set_rate(struct clk_hw *hw, unsigned long req_rate,
169 unsigned long parent_rate)
170 {
171 struct ingenic_clk *ingenic_clk = to_ingenic_clk(hw);
172 struct ingenic_cgu *cgu = ingenic_clk->cgu;
173 const struct ingenic_cgu_clk_info *clk_info = to_clk_info(ingenic_clk);
174 const struct ingenic_cgu_pll_info *pll_info = &clk_info->pll;
175 unsigned long rate, flags;
176 unsigned int m, n, od;
177 u32 ctl;
178
179 rate = ingenic_pll_calc(clk_info, req_rate, parent_rate,
180 &m, &n, &od);
181 if (rate != req_rate)
182 pr_info("ingenic-cgu: request '%s' rate %luHz, actual %luHz\n",
183 clk_info->name, req_rate, rate);
184
185 spin_lock_irqsave(&cgu->lock, flags);
186 ctl = readl(cgu->base + pll_info->reg);
187
188 ctl &= ~(GENMASK(pll_info->m_bits - 1, 0) << pll_info->m_shift);
189 ctl |= (m - pll_info->m_offset) << pll_info->m_shift;
190
191 ctl &= ~(GENMASK(pll_info->n_bits - 1, 0) << pll_info->n_shift);
192 ctl |= (n - pll_info->n_offset) << pll_info->n_shift;
193
194 ctl &= ~(GENMASK(pll_info->od_bits - 1, 0) << pll_info->od_shift);
195 ctl |= pll_info->od_encoding[od - 1] << pll_info->od_shift;
196
197 writel(ctl, cgu->base + pll_info->reg);
198 spin_unlock_irqrestore(&cgu->lock, flags);
199
200 return 0;
201 }
202
ingenic_pll_enable(struct clk_hw * hw)203 static int ingenic_pll_enable(struct clk_hw *hw)
204 {
205 struct ingenic_clk *ingenic_clk = to_ingenic_clk(hw);
206 struct ingenic_cgu *cgu = ingenic_clk->cgu;
207 const struct ingenic_cgu_clk_info *clk_info = to_clk_info(ingenic_clk);
208 const struct ingenic_cgu_pll_info *pll_info = &clk_info->pll;
209 const unsigned int timeout = 100;
210 unsigned long flags;
211 unsigned int i;
212 u32 ctl;
213
214 spin_lock_irqsave(&cgu->lock, flags);
215 ctl = readl(cgu->base + pll_info->reg);
216
217 ctl &= ~BIT(pll_info->bypass_bit);
218 ctl |= BIT(pll_info->enable_bit);
219
220 writel(ctl, cgu->base + pll_info->reg);
221
222 /* wait for the PLL to stabilise */
223 for (i = 0; i < timeout; i++) {
224 ctl = readl(cgu->base + pll_info->reg);
225 if (ctl & BIT(pll_info->stable_bit))
226 break;
227 mdelay(1);
228 }
229
230 spin_unlock_irqrestore(&cgu->lock, flags);
231
232 if (i == timeout)
233 return -EBUSY;
234
235 return 0;
236 }
237
ingenic_pll_disable(struct clk_hw * hw)238 static void ingenic_pll_disable(struct clk_hw *hw)
239 {
240 struct ingenic_clk *ingenic_clk = to_ingenic_clk(hw);
241 struct ingenic_cgu *cgu = ingenic_clk->cgu;
242 const struct ingenic_cgu_clk_info *clk_info = to_clk_info(ingenic_clk);
243 const struct ingenic_cgu_pll_info *pll_info = &clk_info->pll;
244 unsigned long flags;
245 u32 ctl;
246
247 spin_lock_irqsave(&cgu->lock, flags);
248 ctl = readl(cgu->base + pll_info->reg);
249
250 ctl &= ~BIT(pll_info->enable_bit);
251
252 writel(ctl, cgu->base + pll_info->reg);
253 spin_unlock_irqrestore(&cgu->lock, flags);
254 }
255
ingenic_pll_is_enabled(struct clk_hw * hw)256 static int ingenic_pll_is_enabled(struct clk_hw *hw)
257 {
258 struct ingenic_clk *ingenic_clk = to_ingenic_clk(hw);
259 struct ingenic_cgu *cgu = ingenic_clk->cgu;
260 const struct ingenic_cgu_clk_info *clk_info = to_clk_info(ingenic_clk);
261 const struct ingenic_cgu_pll_info *pll_info = &clk_info->pll;
262 unsigned long flags;
263 u32 ctl;
264
265 spin_lock_irqsave(&cgu->lock, flags);
266 ctl = readl(cgu->base + pll_info->reg);
267 spin_unlock_irqrestore(&cgu->lock, flags);
268
269 return !!(ctl & BIT(pll_info->enable_bit));
270 }
271
272 static const struct clk_ops ingenic_pll_ops = {
273 .recalc_rate = ingenic_pll_recalc_rate,
274 .round_rate = ingenic_pll_round_rate,
275 .set_rate = ingenic_pll_set_rate,
276
277 .enable = ingenic_pll_enable,
278 .disable = ingenic_pll_disable,
279 .is_enabled = ingenic_pll_is_enabled,
280 };
281
282 /*
283 * Operations for all non-PLL clocks
284 */
285
ingenic_clk_get_parent(struct clk_hw * hw)286 static u8 ingenic_clk_get_parent(struct clk_hw *hw)
287 {
288 struct ingenic_clk *ingenic_clk = to_ingenic_clk(hw);
289 struct ingenic_cgu *cgu = ingenic_clk->cgu;
290 const struct ingenic_cgu_clk_info *clk_info;
291 u32 reg;
292 u8 i, hw_idx, idx = 0;
293
294 clk_info = &cgu->clock_info[ingenic_clk->idx];
295
296 if (clk_info->type & CGU_CLK_MUX) {
297 reg = readl(cgu->base + clk_info->mux.reg);
298 hw_idx = (reg >> clk_info->mux.shift) &
299 GENMASK(clk_info->mux.bits - 1, 0);
300
301 /*
302 * Convert the hardware index to the parent index by skipping
303 * over any -1's in the parents array.
304 */
305 for (i = 0; i < hw_idx; i++) {
306 if (clk_info->parents[i] != -1)
307 idx++;
308 }
309 }
310
311 return idx;
312 }
313
ingenic_clk_set_parent(struct clk_hw * hw,u8 idx)314 static int ingenic_clk_set_parent(struct clk_hw *hw, u8 idx)
315 {
316 struct ingenic_clk *ingenic_clk = to_ingenic_clk(hw);
317 struct ingenic_cgu *cgu = ingenic_clk->cgu;
318 const struct ingenic_cgu_clk_info *clk_info;
319 unsigned long flags;
320 u8 curr_idx, hw_idx, num_poss;
321 u32 reg, mask;
322
323 clk_info = &cgu->clock_info[ingenic_clk->idx];
324
325 if (clk_info->type & CGU_CLK_MUX) {
326 /*
327 * Convert the parent index to the hardware index by adding
328 * 1 for any -1 in the parents array preceding the given
329 * index. That is, we want the index of idx'th entry in
330 * clk_info->parents which does not equal -1.
331 */
332 hw_idx = curr_idx = 0;
333 num_poss = 1 << clk_info->mux.bits;
334 for (; hw_idx < num_poss; hw_idx++) {
335 if (clk_info->parents[hw_idx] == -1)
336 continue;
337 if (curr_idx == idx)
338 break;
339 curr_idx++;
340 }
341
342 /* idx should always be a valid parent */
343 BUG_ON(curr_idx != idx);
344
345 mask = GENMASK(clk_info->mux.bits - 1, 0);
346 mask <<= clk_info->mux.shift;
347
348 spin_lock_irqsave(&cgu->lock, flags);
349
350 /* write the register */
351 reg = readl(cgu->base + clk_info->mux.reg);
352 reg &= ~mask;
353 reg |= hw_idx << clk_info->mux.shift;
354 writel(reg, cgu->base + clk_info->mux.reg);
355
356 spin_unlock_irqrestore(&cgu->lock, flags);
357 return 0;
358 }
359
360 return idx ? -EINVAL : 0;
361 }
362
363 static unsigned long
ingenic_clk_recalc_rate(struct clk_hw * hw,unsigned long parent_rate)364 ingenic_clk_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
365 {
366 struct ingenic_clk *ingenic_clk = to_ingenic_clk(hw);
367 struct ingenic_cgu *cgu = ingenic_clk->cgu;
368 const struct ingenic_cgu_clk_info *clk_info;
369 unsigned long rate = parent_rate;
370 u32 div_reg, div;
371
372 clk_info = &cgu->clock_info[ingenic_clk->idx];
373
374 if (clk_info->type & CGU_CLK_DIV) {
375 div_reg = readl(cgu->base + clk_info->div.reg);
376 div = (div_reg >> clk_info->div.shift) &
377 GENMASK(clk_info->div.bits - 1, 0);
378
379 if (clk_info->div.div_table)
380 div = clk_info->div.div_table[div];
381 else
382 div = (div + 1) * clk_info->div.div;
383
384 rate /= div;
385 } else if (clk_info->type & CGU_CLK_FIXDIV) {
386 rate /= clk_info->fixdiv.div;
387 }
388
389 return rate;
390 }
391
392 static unsigned int
ingenic_clk_calc_hw_div(const struct ingenic_cgu_clk_info * clk_info,unsigned int div)393 ingenic_clk_calc_hw_div(const struct ingenic_cgu_clk_info *clk_info,
394 unsigned int div)
395 {
396 unsigned int i, best_i = 0, best = (unsigned int)-1;
397
398 for (i = 0; i < (1 << clk_info->div.bits)
399 && clk_info->div.div_table[i]; i++) {
400 if (clk_info->div.div_table[i] >= div &&
401 clk_info->div.div_table[i] < best) {
402 best = clk_info->div.div_table[i];
403 best_i = i;
404
405 if (div == best)
406 break;
407 }
408 }
409
410 return best_i;
411 }
412
413 static unsigned
ingenic_clk_calc_div(const struct ingenic_cgu_clk_info * clk_info,unsigned long parent_rate,unsigned long req_rate)414 ingenic_clk_calc_div(const struct ingenic_cgu_clk_info *clk_info,
415 unsigned long parent_rate, unsigned long req_rate)
416 {
417 unsigned int div, hw_div;
418
419 /* calculate the divide */
420 div = DIV_ROUND_UP(parent_rate, req_rate);
421
422 if (clk_info->div.div_table) {
423 hw_div = ingenic_clk_calc_hw_div(clk_info, div);
424
425 return clk_info->div.div_table[hw_div];
426 }
427
428 /* Impose hardware constraints */
429 div = clamp_t(unsigned int, div, clk_info->div.div,
430 clk_info->div.div << clk_info->div.bits);
431
432 /*
433 * If the divider value itself must be divided before being written to
434 * the divider register, we must ensure we don't have any bits set that
435 * would be lost as a result of doing so.
436 */
437 div = DIV_ROUND_UP(div, clk_info->div.div);
438 div *= clk_info->div.div;
439
440 return div;
441 }
442
443 static long
ingenic_clk_round_rate(struct clk_hw * hw,unsigned long req_rate,unsigned long * parent_rate)444 ingenic_clk_round_rate(struct clk_hw *hw, unsigned long req_rate,
445 unsigned long *parent_rate)
446 {
447 struct ingenic_clk *ingenic_clk = to_ingenic_clk(hw);
448 struct ingenic_cgu *cgu = ingenic_clk->cgu;
449 const struct ingenic_cgu_clk_info *clk_info;
450 unsigned int div = 1;
451
452 clk_info = &cgu->clock_info[ingenic_clk->idx];
453
454 if (clk_info->type & CGU_CLK_DIV)
455 div = ingenic_clk_calc_div(clk_info, *parent_rate, req_rate);
456 else if (clk_info->type & CGU_CLK_FIXDIV)
457 div = clk_info->fixdiv.div;
458
459 return DIV_ROUND_UP(*parent_rate, div);
460 }
461
462 static int
ingenic_clk_set_rate(struct clk_hw * hw,unsigned long req_rate,unsigned long parent_rate)463 ingenic_clk_set_rate(struct clk_hw *hw, unsigned long req_rate,
464 unsigned long parent_rate)
465 {
466 struct ingenic_clk *ingenic_clk = to_ingenic_clk(hw);
467 struct ingenic_cgu *cgu = ingenic_clk->cgu;
468 const struct ingenic_cgu_clk_info *clk_info;
469 const unsigned timeout = 100;
470 unsigned long rate, flags;
471 unsigned int hw_div, div, i;
472 u32 reg, mask;
473 int ret = 0;
474
475 clk_info = &cgu->clock_info[ingenic_clk->idx];
476
477 if (clk_info->type & CGU_CLK_DIV) {
478 div = ingenic_clk_calc_div(clk_info, parent_rate, req_rate);
479 rate = DIV_ROUND_UP(parent_rate, div);
480
481 if (rate != req_rate)
482 return -EINVAL;
483
484 if (clk_info->div.div_table)
485 hw_div = ingenic_clk_calc_hw_div(clk_info, div);
486 else
487 hw_div = ((div / clk_info->div.div) - 1);
488
489 spin_lock_irqsave(&cgu->lock, flags);
490 reg = readl(cgu->base + clk_info->div.reg);
491
492 /* update the divide */
493 mask = GENMASK(clk_info->div.bits - 1, 0);
494 reg &= ~(mask << clk_info->div.shift);
495 reg |= hw_div << clk_info->div.shift;
496
497 /* clear the stop bit */
498 if (clk_info->div.stop_bit != -1)
499 reg &= ~BIT(clk_info->div.stop_bit);
500
501 /* set the change enable bit */
502 if (clk_info->div.ce_bit != -1)
503 reg |= BIT(clk_info->div.ce_bit);
504
505 /* update the hardware */
506 writel(reg, cgu->base + clk_info->div.reg);
507
508 /* wait for the change to take effect */
509 if (clk_info->div.busy_bit != -1) {
510 for (i = 0; i < timeout; i++) {
511 reg = readl(cgu->base + clk_info->div.reg);
512 if (!(reg & BIT(clk_info->div.busy_bit)))
513 break;
514 mdelay(1);
515 }
516 if (i == timeout)
517 ret = -EBUSY;
518 }
519
520 spin_unlock_irqrestore(&cgu->lock, flags);
521 return ret;
522 }
523
524 return -EINVAL;
525 }
526
ingenic_clk_enable(struct clk_hw * hw)527 static int ingenic_clk_enable(struct clk_hw *hw)
528 {
529 struct ingenic_clk *ingenic_clk = to_ingenic_clk(hw);
530 struct ingenic_cgu *cgu = ingenic_clk->cgu;
531 const struct ingenic_cgu_clk_info *clk_info;
532 unsigned long flags;
533
534 clk_info = &cgu->clock_info[ingenic_clk->idx];
535
536 if (clk_info->type & CGU_CLK_GATE) {
537 /* ungate the clock */
538 spin_lock_irqsave(&cgu->lock, flags);
539 ingenic_cgu_gate_set(cgu, &clk_info->gate, false);
540 spin_unlock_irqrestore(&cgu->lock, flags);
541
542 if (clk_info->gate.delay_us)
543 udelay(clk_info->gate.delay_us);
544 }
545
546 return 0;
547 }
548
ingenic_clk_disable(struct clk_hw * hw)549 static void ingenic_clk_disable(struct clk_hw *hw)
550 {
551 struct ingenic_clk *ingenic_clk = to_ingenic_clk(hw);
552 struct ingenic_cgu *cgu = ingenic_clk->cgu;
553 const struct ingenic_cgu_clk_info *clk_info;
554 unsigned long flags;
555
556 clk_info = &cgu->clock_info[ingenic_clk->idx];
557
558 if (clk_info->type & CGU_CLK_GATE) {
559 /* gate the clock */
560 spin_lock_irqsave(&cgu->lock, flags);
561 ingenic_cgu_gate_set(cgu, &clk_info->gate, true);
562 spin_unlock_irqrestore(&cgu->lock, flags);
563 }
564 }
565
ingenic_clk_is_enabled(struct clk_hw * hw)566 static int ingenic_clk_is_enabled(struct clk_hw *hw)
567 {
568 struct ingenic_clk *ingenic_clk = to_ingenic_clk(hw);
569 struct ingenic_cgu *cgu = ingenic_clk->cgu;
570 const struct ingenic_cgu_clk_info *clk_info;
571 unsigned long flags;
572 int enabled = 1;
573
574 clk_info = &cgu->clock_info[ingenic_clk->idx];
575
576 if (clk_info->type & CGU_CLK_GATE) {
577 spin_lock_irqsave(&cgu->lock, flags);
578 enabled = !ingenic_cgu_gate_get(cgu, &clk_info->gate);
579 spin_unlock_irqrestore(&cgu->lock, flags);
580 }
581
582 return enabled;
583 }
584
585 static const struct clk_ops ingenic_clk_ops = {
586 .get_parent = ingenic_clk_get_parent,
587 .set_parent = ingenic_clk_set_parent,
588
589 .recalc_rate = ingenic_clk_recalc_rate,
590 .round_rate = ingenic_clk_round_rate,
591 .set_rate = ingenic_clk_set_rate,
592
593 .enable = ingenic_clk_enable,
594 .disable = ingenic_clk_disable,
595 .is_enabled = ingenic_clk_is_enabled,
596 };
597
598 /*
599 * Setup functions.
600 */
601
ingenic_register_clock(struct ingenic_cgu * cgu,unsigned idx)602 static int ingenic_register_clock(struct ingenic_cgu *cgu, unsigned idx)
603 {
604 const struct ingenic_cgu_clk_info *clk_info = &cgu->clock_info[idx];
605 struct clk_init_data clk_init;
606 struct ingenic_clk *ingenic_clk = NULL;
607 struct clk *clk, *parent;
608 const char *parent_names[4];
609 unsigned caps, i, num_possible;
610 int err = -EINVAL;
611
612 BUILD_BUG_ON(ARRAY_SIZE(clk_info->parents) > ARRAY_SIZE(parent_names));
613
614 if (clk_info->type == CGU_CLK_EXT) {
615 clk = of_clk_get_by_name(cgu->np, clk_info->name);
616 if (IS_ERR(clk)) {
617 pr_err("%s: no external clock '%s' provided\n",
618 __func__, clk_info->name);
619 err = -ENODEV;
620 goto out;
621 }
622 err = clk_register_clkdev(clk, clk_info->name, NULL);
623 if (err) {
624 clk_put(clk);
625 goto out;
626 }
627 cgu->clocks.clks[idx] = clk;
628 return 0;
629 }
630
631 if (!clk_info->type) {
632 pr_err("%s: no clock type specified for '%s'\n", __func__,
633 clk_info->name);
634 goto out;
635 }
636
637 ingenic_clk = kzalloc(sizeof(*ingenic_clk), GFP_KERNEL);
638 if (!ingenic_clk) {
639 err = -ENOMEM;
640 goto out;
641 }
642
643 ingenic_clk->hw.init = &clk_init;
644 ingenic_clk->cgu = cgu;
645 ingenic_clk->idx = idx;
646
647 clk_init.name = clk_info->name;
648 clk_init.flags = 0;
649 clk_init.parent_names = parent_names;
650
651 caps = clk_info->type;
652
653 if (caps & (CGU_CLK_MUX | CGU_CLK_CUSTOM)) {
654 clk_init.num_parents = 0;
655
656 if (caps & CGU_CLK_MUX)
657 num_possible = 1 << clk_info->mux.bits;
658 else
659 num_possible = ARRAY_SIZE(clk_info->parents);
660
661 for (i = 0; i < num_possible; i++) {
662 if (clk_info->parents[i] == -1)
663 continue;
664
665 parent = cgu->clocks.clks[clk_info->parents[i]];
666 parent_names[clk_init.num_parents] =
667 __clk_get_name(parent);
668 clk_init.num_parents++;
669 }
670
671 BUG_ON(!clk_init.num_parents);
672 BUG_ON(clk_init.num_parents > ARRAY_SIZE(parent_names));
673 } else {
674 BUG_ON(clk_info->parents[0] == -1);
675 clk_init.num_parents = 1;
676 parent = cgu->clocks.clks[clk_info->parents[0]];
677 parent_names[0] = __clk_get_name(parent);
678 }
679
680 if (caps & CGU_CLK_CUSTOM) {
681 clk_init.ops = clk_info->custom.clk_ops;
682
683 caps &= ~CGU_CLK_CUSTOM;
684
685 if (caps) {
686 pr_err("%s: custom clock may not be combined with type 0x%x\n",
687 __func__, caps);
688 goto out;
689 }
690 } else if (caps & CGU_CLK_PLL) {
691 clk_init.ops = &ingenic_pll_ops;
692 clk_init.flags |= CLK_SET_RATE_GATE;
693
694 caps &= ~CGU_CLK_PLL;
695
696 if (caps) {
697 pr_err("%s: PLL may not be combined with type 0x%x\n",
698 __func__, caps);
699 goto out;
700 }
701 } else {
702 clk_init.ops = &ingenic_clk_ops;
703 }
704
705 /* nothing to do for gates or fixed dividers */
706 caps &= ~(CGU_CLK_GATE | CGU_CLK_FIXDIV);
707
708 if (caps & CGU_CLK_MUX) {
709 if (!(caps & CGU_CLK_MUX_GLITCHFREE))
710 clk_init.flags |= CLK_SET_PARENT_GATE;
711
712 caps &= ~(CGU_CLK_MUX | CGU_CLK_MUX_GLITCHFREE);
713 }
714
715 if (caps & CGU_CLK_DIV) {
716 caps &= ~CGU_CLK_DIV;
717 } else {
718 /* pass rate changes to the parent clock */
719 clk_init.flags |= CLK_SET_RATE_PARENT;
720 }
721
722 if (caps) {
723 pr_err("%s: unknown clock type 0x%x\n", __func__, caps);
724 goto out;
725 }
726
727 clk = clk_register(NULL, &ingenic_clk->hw);
728 if (IS_ERR(clk)) {
729 pr_err("%s: failed to register clock '%s'\n", __func__,
730 clk_info->name);
731 err = PTR_ERR(clk);
732 goto out;
733 }
734
735 err = clk_register_clkdev(clk, clk_info->name, NULL);
736 if (err)
737 goto out;
738
739 cgu->clocks.clks[idx] = clk;
740 out:
741 if (err)
742 kfree(ingenic_clk);
743 return err;
744 }
745
746 struct ingenic_cgu *
ingenic_cgu_new(const struct ingenic_cgu_clk_info * clock_info,unsigned num_clocks,struct device_node * np)747 ingenic_cgu_new(const struct ingenic_cgu_clk_info *clock_info,
748 unsigned num_clocks, struct device_node *np)
749 {
750 struct ingenic_cgu *cgu;
751
752 cgu = kzalloc(sizeof(*cgu), GFP_KERNEL);
753 if (!cgu)
754 goto err_out;
755
756 cgu->base = of_iomap(np, 0);
757 if (!cgu->base) {
758 pr_err("%s: failed to map CGU registers\n", __func__);
759 goto err_out_free;
760 }
761
762 cgu->np = np;
763 cgu->clock_info = clock_info;
764 cgu->clocks.clk_num = num_clocks;
765
766 spin_lock_init(&cgu->lock);
767
768 return cgu;
769
770 err_out_free:
771 kfree(cgu);
772 err_out:
773 return NULL;
774 }
775
ingenic_cgu_register_clocks(struct ingenic_cgu * cgu)776 int ingenic_cgu_register_clocks(struct ingenic_cgu *cgu)
777 {
778 unsigned i;
779 int err;
780
781 cgu->clocks.clks = kcalloc(cgu->clocks.clk_num, sizeof(struct clk *),
782 GFP_KERNEL);
783 if (!cgu->clocks.clks) {
784 err = -ENOMEM;
785 goto err_out;
786 }
787
788 for (i = 0; i < cgu->clocks.clk_num; i++) {
789 err = ingenic_register_clock(cgu, i);
790 if (err)
791 goto err_out_unregister;
792 }
793
794 err = of_clk_add_provider(cgu->np, of_clk_src_onecell_get,
795 &cgu->clocks);
796 if (err)
797 goto err_out_unregister;
798
799 return 0;
800
801 err_out_unregister:
802 for (i = 0; i < cgu->clocks.clk_num; i++) {
803 if (!cgu->clocks.clks[i])
804 continue;
805 if (cgu->clock_info[i].type & CGU_CLK_EXT)
806 clk_put(cgu->clocks.clks[i]);
807 else
808 clk_unregister(cgu->clocks.clks[i]);
809 }
810 kfree(cgu->clocks.clks);
811 err_out:
812 return err;
813 }
814