drm/tests: hdmi: Fix memory leaks in drm_display_mode_from_cea_vic()
[drm/drm-misc.git] / drivers / clk / ti / fapll.c
blob2db3fc4a443e052722d5c01e5ef38cc1417d818f
1 // SPDX-License-Identifier: GPL-2.0-only
3 #include <linux/clk.h>
4 #include <linux/clk-provider.h>
5 #include <linux/delay.h>
6 #include <linux/err.h>
7 #include <linux/io.h>
8 #include <linux/math64.h>
9 #include <linux/of.h>
10 #include <linux/of_address.h>
11 #include <linux/clk/ti.h>
13 #include "clock.h"
15 /* FAPLL Control Register PLL_CTRL */
16 #define FAPLL_MAIN_MULT_N_SHIFT 16
17 #define FAPLL_MAIN_DIV_P_SHIFT 8
18 #define FAPLL_MAIN_LOCK BIT(7)
19 #define FAPLL_MAIN_PLLEN BIT(3)
20 #define FAPLL_MAIN_BP BIT(2)
21 #define FAPLL_MAIN_LOC_CTL BIT(0)
23 #define FAPLL_MAIN_MAX_MULT_N 0xffff
24 #define FAPLL_MAIN_MAX_DIV_P 0xff
25 #define FAPLL_MAIN_CLEAR_MASK \
26 ((FAPLL_MAIN_MAX_MULT_N << FAPLL_MAIN_MULT_N_SHIFT) | \
27 (FAPLL_MAIN_DIV_P_SHIFT << FAPLL_MAIN_DIV_P_SHIFT) | \
28 FAPLL_MAIN_LOC_CTL)
30 /* FAPLL powerdown register PWD */
31 #define FAPLL_PWD_OFFSET 4
33 #define MAX_FAPLL_OUTPUTS 7
34 #define FAPLL_MAX_RETRIES 1000
36 #define to_fapll(_hw) container_of(_hw, struct fapll_data, hw)
37 #define to_synth(_hw) container_of(_hw, struct fapll_synth, hw)
39 /* The bypass bit is inverted on the ddr_pll.. */
40 #define fapll_is_ddr_pll(va) (((u32)(va) & 0xffff) == 0x0440)
43 * The audio_pll_clk1 input is hard wired to the 27MHz bypass clock,
44 * and the audio_pll_clk1 synthesizer is hardwared to 32KiHz output.
46 #define is_ddr_pll_clk1(va) (((u32)(va) & 0xffff) == 0x044c)
47 #define is_audio_pll_clk1(va) (((u32)(va) & 0xffff) == 0x04a8)
49 /* Synthesizer divider register */
50 #define SYNTH_LDMDIV1 BIT(8)
52 /* Synthesizer frequency register */
53 #define SYNTH_LDFREQ BIT(31)
55 #define SYNTH_PHASE_K 8
56 #define SYNTH_MAX_INT_DIV 0xf
57 #define SYNTH_MAX_DIV_M 0xff
59 struct fapll_data {
60 struct clk_hw hw;
61 void __iomem *base;
62 const char *name;
63 struct clk *clk_ref;
64 struct clk *clk_bypass;
65 struct clk_onecell_data outputs;
66 bool bypass_bit_inverted;
69 struct fapll_synth {
70 struct clk_hw hw;
71 struct fapll_data *fd;
72 int index;
73 void __iomem *freq;
74 void __iomem *div;
75 const char *name;
76 struct clk *clk_pll;
79 static bool ti_fapll_clock_is_bypass(struct fapll_data *fd)
81 u32 v = readl_relaxed(fd->base);
83 if (fd->bypass_bit_inverted)
84 return !(v & FAPLL_MAIN_BP);
85 else
86 return !!(v & FAPLL_MAIN_BP);
89 static void ti_fapll_set_bypass(struct fapll_data *fd)
91 u32 v = readl_relaxed(fd->base);
93 if (fd->bypass_bit_inverted)
94 v &= ~FAPLL_MAIN_BP;
95 else
96 v |= FAPLL_MAIN_BP;
97 writel_relaxed(v, fd->base);
100 static void ti_fapll_clear_bypass(struct fapll_data *fd)
102 u32 v = readl_relaxed(fd->base);
104 if (fd->bypass_bit_inverted)
105 v |= FAPLL_MAIN_BP;
106 else
107 v &= ~FAPLL_MAIN_BP;
108 writel_relaxed(v, fd->base);
111 static int ti_fapll_wait_lock(struct fapll_data *fd)
113 int retries = FAPLL_MAX_RETRIES;
114 u32 v;
116 while ((v = readl_relaxed(fd->base))) {
117 if (v & FAPLL_MAIN_LOCK)
118 return 0;
120 if (retries-- <= 0)
121 break;
123 udelay(1);
126 pr_err("%s failed to lock\n", fd->name);
128 return -ETIMEDOUT;
131 static int ti_fapll_enable(struct clk_hw *hw)
133 struct fapll_data *fd = to_fapll(hw);
134 u32 v = readl_relaxed(fd->base);
136 v |= FAPLL_MAIN_PLLEN;
137 writel_relaxed(v, fd->base);
138 ti_fapll_wait_lock(fd);
140 return 0;
143 static void ti_fapll_disable(struct clk_hw *hw)
145 struct fapll_data *fd = to_fapll(hw);
146 u32 v = readl_relaxed(fd->base);
148 v &= ~FAPLL_MAIN_PLLEN;
149 writel_relaxed(v, fd->base);
152 static int ti_fapll_is_enabled(struct clk_hw *hw)
154 struct fapll_data *fd = to_fapll(hw);
155 u32 v = readl_relaxed(fd->base);
157 return v & FAPLL_MAIN_PLLEN;
160 static unsigned long ti_fapll_recalc_rate(struct clk_hw *hw,
161 unsigned long parent_rate)
163 struct fapll_data *fd = to_fapll(hw);
164 u32 fapll_n, fapll_p, v;
165 u64 rate;
167 if (ti_fapll_clock_is_bypass(fd))
168 return parent_rate;
170 rate = parent_rate;
172 /* PLL pre-divider is P and multiplier is N */
173 v = readl_relaxed(fd->base);
174 fapll_p = (v >> 8) & 0xff;
175 if (fapll_p)
176 do_div(rate, fapll_p);
177 fapll_n = v >> 16;
178 if (fapll_n)
179 rate *= fapll_n;
181 return rate;
184 static u8 ti_fapll_get_parent(struct clk_hw *hw)
186 struct fapll_data *fd = to_fapll(hw);
188 if (ti_fapll_clock_is_bypass(fd))
189 return 1;
191 return 0;
194 static int ti_fapll_set_div_mult(unsigned long rate,
195 unsigned long parent_rate,
196 u32 *pre_div_p, u32 *mult_n)
199 * So far no luck getting decent clock with PLL divider,
200 * PLL does not seem to lock and the signal does not look
201 * right. It seems the divider can only be used together
202 * with the multiplier?
204 if (rate < parent_rate) {
205 pr_warn("FAPLL main divider rates unsupported\n");
206 return -EINVAL;
209 *mult_n = rate / parent_rate;
210 if (*mult_n > FAPLL_MAIN_MAX_MULT_N)
211 return -EINVAL;
212 *pre_div_p = 1;
214 return 0;
217 static long ti_fapll_round_rate(struct clk_hw *hw, unsigned long rate,
218 unsigned long *parent_rate)
220 u32 pre_div_p, mult_n;
221 int error;
223 if (!rate)
224 return -EINVAL;
226 error = ti_fapll_set_div_mult(rate, *parent_rate,
227 &pre_div_p, &mult_n);
228 if (error)
229 return error;
231 rate = *parent_rate / pre_div_p;
232 rate *= mult_n;
234 return rate;
237 static int ti_fapll_set_rate(struct clk_hw *hw, unsigned long rate,
238 unsigned long parent_rate)
240 struct fapll_data *fd = to_fapll(hw);
241 u32 pre_div_p, mult_n, v;
242 int error;
244 if (!rate)
245 return -EINVAL;
247 error = ti_fapll_set_div_mult(rate, parent_rate,
248 &pre_div_p, &mult_n);
249 if (error)
250 return error;
252 ti_fapll_set_bypass(fd);
253 v = readl_relaxed(fd->base);
254 v &= ~FAPLL_MAIN_CLEAR_MASK;
255 v |= pre_div_p << FAPLL_MAIN_DIV_P_SHIFT;
256 v |= mult_n << FAPLL_MAIN_MULT_N_SHIFT;
257 writel_relaxed(v, fd->base);
258 if (ti_fapll_is_enabled(hw))
259 ti_fapll_wait_lock(fd);
260 ti_fapll_clear_bypass(fd);
262 return 0;
265 static const struct clk_ops ti_fapll_ops = {
266 .enable = ti_fapll_enable,
267 .disable = ti_fapll_disable,
268 .is_enabled = ti_fapll_is_enabled,
269 .recalc_rate = ti_fapll_recalc_rate,
270 .get_parent = ti_fapll_get_parent,
271 .round_rate = ti_fapll_round_rate,
272 .set_rate = ti_fapll_set_rate,
275 static int ti_fapll_synth_enable(struct clk_hw *hw)
277 struct fapll_synth *synth = to_synth(hw);
278 u32 v = readl_relaxed(synth->fd->base + FAPLL_PWD_OFFSET);
280 v &= ~(1 << synth->index);
281 writel_relaxed(v, synth->fd->base + FAPLL_PWD_OFFSET);
283 return 0;
286 static void ti_fapll_synth_disable(struct clk_hw *hw)
288 struct fapll_synth *synth = to_synth(hw);
289 u32 v = readl_relaxed(synth->fd->base + FAPLL_PWD_OFFSET);
291 v |= 1 << synth->index;
292 writel_relaxed(v, synth->fd->base + FAPLL_PWD_OFFSET);
295 static int ti_fapll_synth_is_enabled(struct clk_hw *hw)
297 struct fapll_synth *synth = to_synth(hw);
298 u32 v = readl_relaxed(synth->fd->base + FAPLL_PWD_OFFSET);
300 return !(v & (1 << synth->index));
304 * See dm816x TRM chapter 1.10.3 Flying Adder PLL fore more info
306 static unsigned long ti_fapll_synth_recalc_rate(struct clk_hw *hw,
307 unsigned long parent_rate)
309 struct fapll_synth *synth = to_synth(hw);
310 u32 synth_div_m;
311 u64 rate;
313 /* The audio_pll_clk1 is hardwired to produce 32.768KiHz clock */
314 if (!synth->div)
315 return 32768;
318 * PLL in bypass sets the synths in bypass mode too. The PLL rate
319 * can be also be set to 27MHz, so we can't use parent_rate to
320 * check for bypass mode.
322 if (ti_fapll_clock_is_bypass(synth->fd))
323 return parent_rate;
325 rate = parent_rate;
328 * Synth frequency integer and fractional divider.
329 * Note that the phase output K is 8, so the result needs
330 * to be multiplied by SYNTH_PHASE_K.
332 if (synth->freq) {
333 u32 v, synth_int_div, synth_frac_div, synth_div_freq;
335 v = readl_relaxed(synth->freq);
336 synth_int_div = (v >> 24) & 0xf;
337 synth_frac_div = v & 0xffffff;
338 synth_div_freq = (synth_int_div * 10000000) + synth_frac_div;
339 rate *= 10000000;
340 do_div(rate, synth_div_freq);
341 rate *= SYNTH_PHASE_K;
344 /* Synth post-divider M */
345 synth_div_m = readl_relaxed(synth->div) & SYNTH_MAX_DIV_M;
347 return DIV_ROUND_UP_ULL(rate, synth_div_m);
350 static unsigned long ti_fapll_synth_get_frac_rate(struct clk_hw *hw,
351 unsigned long parent_rate)
353 struct fapll_synth *synth = to_synth(hw);
354 unsigned long current_rate, frac_rate;
355 u32 post_div_m;
357 current_rate = ti_fapll_synth_recalc_rate(hw, parent_rate);
358 post_div_m = readl_relaxed(synth->div) & SYNTH_MAX_DIV_M;
359 frac_rate = current_rate * post_div_m;
361 return frac_rate;
364 static u32 ti_fapll_synth_set_frac_rate(struct fapll_synth *synth,
365 unsigned long rate,
366 unsigned long parent_rate)
368 u32 post_div_m, synth_int_div = 0, synth_frac_div = 0, v;
370 post_div_m = DIV_ROUND_UP_ULL((u64)parent_rate * SYNTH_PHASE_K, rate);
371 post_div_m = post_div_m / SYNTH_MAX_INT_DIV;
372 if (post_div_m > SYNTH_MAX_DIV_M)
373 return -EINVAL;
374 if (!post_div_m)
375 post_div_m = 1;
377 for (; post_div_m < SYNTH_MAX_DIV_M; post_div_m++) {
378 synth_int_div = DIV_ROUND_UP_ULL((u64)parent_rate *
379 SYNTH_PHASE_K *
380 10000000,
381 rate * post_div_m);
382 synth_frac_div = synth_int_div % 10000000;
383 synth_int_div /= 10000000;
385 if (synth_int_div <= SYNTH_MAX_INT_DIV)
386 break;
389 if (synth_int_div > SYNTH_MAX_INT_DIV)
390 return -EINVAL;
392 v = readl_relaxed(synth->freq);
393 v &= ~0x1fffffff;
394 v |= (synth_int_div & SYNTH_MAX_INT_DIV) << 24;
395 v |= (synth_frac_div & 0xffffff);
396 v |= SYNTH_LDFREQ;
397 writel_relaxed(v, synth->freq);
399 return post_div_m;
402 static long ti_fapll_synth_round_rate(struct clk_hw *hw, unsigned long rate,
403 unsigned long *parent_rate)
405 struct fapll_synth *synth = to_synth(hw);
406 struct fapll_data *fd = synth->fd;
407 unsigned long r;
409 if (ti_fapll_clock_is_bypass(fd) || !synth->div || !rate)
410 return -EINVAL;
412 /* Only post divider m available with no fractional divider? */
413 if (!synth->freq) {
414 unsigned long frac_rate;
415 u32 synth_post_div_m;
417 frac_rate = ti_fapll_synth_get_frac_rate(hw, *parent_rate);
418 synth_post_div_m = DIV_ROUND_UP(frac_rate, rate);
419 r = DIV_ROUND_UP(frac_rate, synth_post_div_m);
420 goto out;
423 r = *parent_rate * SYNTH_PHASE_K;
424 if (rate > r)
425 goto out;
427 r = DIV_ROUND_UP_ULL(r, SYNTH_MAX_INT_DIV * SYNTH_MAX_DIV_M);
428 if (rate < r)
429 goto out;
431 r = rate;
432 out:
433 return r;
436 static int ti_fapll_synth_set_rate(struct clk_hw *hw, unsigned long rate,
437 unsigned long parent_rate)
439 struct fapll_synth *synth = to_synth(hw);
440 struct fapll_data *fd = synth->fd;
441 unsigned long frac_rate, post_rate = 0;
442 u32 post_div_m = 0, v;
444 if (ti_fapll_clock_is_bypass(fd) || !synth->div || !rate)
445 return -EINVAL;
447 /* Produce the rate with just post divider M? */
448 frac_rate = ti_fapll_synth_get_frac_rate(hw, parent_rate);
449 if (frac_rate < rate) {
450 if (!synth->freq)
451 return -EINVAL;
452 } else {
453 post_div_m = DIV_ROUND_UP(frac_rate, rate);
454 if (post_div_m && (post_div_m <= SYNTH_MAX_DIV_M))
455 post_rate = DIV_ROUND_UP(frac_rate, post_div_m);
456 if (!synth->freq && !post_rate)
457 return -EINVAL;
460 /* Need to recalculate the fractional divider? */
461 if ((post_rate != rate) && synth->freq)
462 post_div_m = ti_fapll_synth_set_frac_rate(synth,
463 rate,
464 parent_rate);
466 v = readl_relaxed(synth->div);
467 v &= ~SYNTH_MAX_DIV_M;
468 v |= post_div_m;
469 v |= SYNTH_LDMDIV1;
470 writel_relaxed(v, synth->div);
472 return 0;
475 static const struct clk_ops ti_fapll_synt_ops = {
476 .enable = ti_fapll_synth_enable,
477 .disable = ti_fapll_synth_disable,
478 .is_enabled = ti_fapll_synth_is_enabled,
479 .recalc_rate = ti_fapll_synth_recalc_rate,
480 .round_rate = ti_fapll_synth_round_rate,
481 .set_rate = ti_fapll_synth_set_rate,
484 static struct clk * __init ti_fapll_synth_setup(struct fapll_data *fd,
485 void __iomem *freq,
486 void __iomem *div,
487 int index,
488 const char *name,
489 const char *parent,
490 struct clk *pll_clk)
492 struct clk_init_data *init;
493 struct fapll_synth *synth;
494 struct clk *clk = ERR_PTR(-ENOMEM);
496 init = kzalloc(sizeof(*init), GFP_KERNEL);
497 if (!init)
498 return ERR_PTR(-ENOMEM);
500 init->ops = &ti_fapll_synt_ops;
501 init->name = name;
502 init->parent_names = &parent;
503 init->num_parents = 1;
505 synth = kzalloc(sizeof(*synth), GFP_KERNEL);
506 if (!synth)
507 goto free;
509 synth->fd = fd;
510 synth->index = index;
511 synth->freq = freq;
512 synth->div = div;
513 synth->name = name;
514 synth->hw.init = init;
515 synth->clk_pll = pll_clk;
517 clk = clk_register(NULL, &synth->hw);
518 if (IS_ERR(clk)) {
519 pr_err("failed to register clock\n");
520 goto free;
523 return clk;
525 free:
526 kfree(synth);
527 kfree(init);
529 return clk;
532 static void __init ti_fapll_setup(struct device_node *node)
534 struct fapll_data *fd;
535 struct clk_init_data *init = NULL;
536 const char *parent_name[2];
537 struct clk *pll_clk;
538 const char *name;
539 int i;
541 fd = kzalloc(sizeof(*fd), GFP_KERNEL);
542 if (!fd)
543 return;
545 fd->outputs.clks = kzalloc(sizeof(struct clk *) *
546 MAX_FAPLL_OUTPUTS + 1,
547 GFP_KERNEL);
548 if (!fd->outputs.clks)
549 goto free;
551 init = kzalloc(sizeof(*init), GFP_KERNEL);
552 if (!init)
553 goto free;
555 init->ops = &ti_fapll_ops;
556 name = ti_dt_clk_name(node);
557 init->name = name;
559 init->num_parents = of_clk_get_parent_count(node);
560 if (init->num_parents != 2) {
561 pr_err("%pOFn must have two parents\n", node);
562 goto free;
565 of_clk_parent_fill(node, parent_name, 2);
566 init->parent_names = parent_name;
568 fd->clk_ref = of_clk_get(node, 0);
569 if (IS_ERR(fd->clk_ref)) {
570 pr_err("%pOFn could not get clk_ref\n", node);
571 goto free;
574 fd->clk_bypass = of_clk_get(node, 1);
575 if (IS_ERR(fd->clk_bypass)) {
576 pr_err("%pOFn could not get clk_bypass\n", node);
577 goto free;
580 fd->base = of_iomap(node, 0);
581 if (!fd->base) {
582 pr_err("%pOFn could not get IO base\n", node);
583 goto free;
586 if (fapll_is_ddr_pll(fd->base))
587 fd->bypass_bit_inverted = true;
589 fd->name = name;
590 fd->hw.init = init;
592 /* Register the parent PLL */
593 pll_clk = clk_register(NULL, &fd->hw);
594 if (IS_ERR(pll_clk))
595 goto unmap;
597 fd->outputs.clks[0] = pll_clk;
598 fd->outputs.clk_num++;
601 * Set up the child synthesizers starting at index 1 as the
602 * PLL output is at index 0. We need to check the clock-indices
603 * for numbering in case there are holes in the synth mapping,
604 * and then probe the synth register to see if it has a FREQ
605 * register available.
607 for (i = 0; i < MAX_FAPLL_OUTPUTS; i++) {
608 const char *output_name;
609 void __iomem *freq, *div;
610 struct clk *synth_clk;
611 int output_instance;
612 u32 v;
614 if (of_property_read_string_index(node, "clock-output-names",
615 i, &output_name))
616 continue;
618 if (of_property_read_u32_index(node, "clock-indices", i,
619 &output_instance))
620 output_instance = i;
622 freq = fd->base + (output_instance * 8);
623 div = freq + 4;
625 /* Check for hardwired audio_pll_clk1 */
626 if (is_audio_pll_clk1(freq)) {
627 freq = NULL;
628 div = NULL;
629 } else {
630 /* Does the synthesizer have a FREQ register? */
631 v = readl_relaxed(freq);
632 if (!v)
633 freq = NULL;
635 synth_clk = ti_fapll_synth_setup(fd, freq, div, output_instance,
636 output_name, name, pll_clk);
637 if (IS_ERR(synth_clk))
638 continue;
640 fd->outputs.clks[output_instance] = synth_clk;
641 fd->outputs.clk_num++;
643 clk_register_clkdev(synth_clk, output_name, NULL);
646 /* Register the child synthesizers as the FAPLL outputs */
647 of_clk_add_provider(node, of_clk_src_onecell_get, &fd->outputs);
648 /* Add clock alias for the outputs */
650 kfree(init);
652 return;
654 unmap:
655 iounmap(fd->base);
656 free:
657 if (fd->clk_bypass)
658 clk_put(fd->clk_bypass);
659 if (fd->clk_ref)
660 clk_put(fd->clk_ref);
661 kfree(fd->outputs.clks);
662 kfree(fd);
663 kfree(init);
666 CLK_OF_DECLARE(ti_fapll_clock, "ti,dm816-fapll-clock", ti_fapll_setup);