clk-a10-pll2.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright 2013 Emilio López
  4. * Emilio López <[email protected]>
  5. *
  6. * Copyright 2015 Maxime Ripard
  7. * Maxime Ripard <[email protected]>
  8. */
  9. #include <linux/clk-provider.h>
  10. #include <linux/io.h>
  11. #include <linux/of.h>
  12. #include <linux/of_address.h>
  13. #include <linux/slab.h>
  14. #include <dt-bindings/clock/sun4i-a10-pll2.h>
  15. #define SUN4I_PLL2_ENABLE 31
  16. #define SUN4I_PLL2_PRE_DIV_SHIFT 0
  17. #define SUN4I_PLL2_PRE_DIV_WIDTH 5
  18. #define SUN4I_PLL2_PRE_DIV_MASK GENMASK(SUN4I_PLL2_PRE_DIV_WIDTH - 1, 0)
  19. #define SUN4I_PLL2_N_SHIFT 8
  20. #define SUN4I_PLL2_N_WIDTH 7
  21. #define SUN4I_PLL2_N_MASK GENMASK(SUN4I_PLL2_N_WIDTH - 1, 0)
  22. #define SUN4I_PLL2_POST_DIV_SHIFT 26
  23. #define SUN4I_PLL2_POST_DIV_WIDTH 4
  24. #define SUN4I_PLL2_POST_DIV_MASK GENMASK(SUN4I_PLL2_POST_DIV_WIDTH - 1, 0)
  25. #define SUN4I_PLL2_POST_DIV_VALUE 4
  26. #define SUN4I_PLL2_OUTPUTS 4
  27. static DEFINE_SPINLOCK(sun4i_a10_pll2_lock);
  28. static void __init sun4i_pll2_setup(struct device_node *node,
  29. int post_div_offset)
  30. {
  31. const char *clk_name = node->name, *parent;
  32. struct clk **clks, *base_clk, *prediv_clk;
  33. struct clk_onecell_data *clk_data;
  34. struct clk_multiplier *mult;
  35. struct clk_gate *gate;
  36. void __iomem *reg;
  37. u32 val;
  38. reg = of_io_request_and_map(node, 0, of_node_full_name(node));
  39. if (IS_ERR(reg))
  40. return;
  41. clk_data = kzalloc(sizeof(*clk_data), GFP_KERNEL);
  42. if (!clk_data)
  43. goto err_unmap;
  44. clks = kcalloc(SUN4I_PLL2_OUTPUTS, sizeof(struct clk *), GFP_KERNEL);
  45. if (!clks)
  46. goto err_free_data;
  47. parent = of_clk_get_parent_name(node, 0);
  48. prediv_clk = clk_register_divider(NULL, "pll2-prediv",
  49. parent, 0, reg,
  50. SUN4I_PLL2_PRE_DIV_SHIFT,
  51. SUN4I_PLL2_PRE_DIV_WIDTH,
  52. CLK_DIVIDER_ONE_BASED | CLK_DIVIDER_ALLOW_ZERO,
  53. &sun4i_a10_pll2_lock);
  54. if (IS_ERR(prediv_clk)) {
  55. pr_err("Couldn't register the prediv clock\n");
  56. goto err_free_array;
  57. }
  58. /* Setup the gate part of the PLL2 */
  59. gate = kzalloc(sizeof(struct clk_gate), GFP_KERNEL);
  60. if (!gate)
  61. goto err_unregister_prediv;
  62. gate->reg = reg;
  63. gate->bit_idx = SUN4I_PLL2_ENABLE;
  64. gate->lock = &sun4i_a10_pll2_lock;
  65. /* Setup the multiplier part of the PLL2 */
  66. mult = kzalloc(sizeof(struct clk_multiplier), GFP_KERNEL);
  67. if (!mult)
  68. goto err_free_gate;
  69. mult->reg = reg;
  70. mult->shift = SUN4I_PLL2_N_SHIFT;
  71. mult->width = 7;
  72. mult->flags = CLK_MULTIPLIER_ZERO_BYPASS |
  73. CLK_MULTIPLIER_ROUND_CLOSEST;
  74. mult->lock = &sun4i_a10_pll2_lock;
  75. parent = __clk_get_name(prediv_clk);
  76. base_clk = clk_register_composite(NULL, "pll2-base",
  77. &parent, 1,
  78. NULL, NULL,
  79. &mult->hw, &clk_multiplier_ops,
  80. &gate->hw, &clk_gate_ops,
  81. CLK_SET_RATE_PARENT);
  82. if (IS_ERR(base_clk)) {
  83. pr_err("Couldn't register the base multiplier clock\n");
  84. goto err_free_multiplier;
  85. }
  86. parent = __clk_get_name(base_clk);
  87. /*
  88. * PLL2-1x
  89. *
  90. * This is supposed to have a post divider, but we won't need
  91. * to use it, we just need to initialise it to 4, and use a
  92. * fixed divider.
  93. */
  94. val = readl(reg);
  95. val &= ~(SUN4I_PLL2_POST_DIV_MASK << SUN4I_PLL2_POST_DIV_SHIFT);
  96. val |= (SUN4I_PLL2_POST_DIV_VALUE - post_div_offset) << SUN4I_PLL2_POST_DIV_SHIFT;
  97. writel(val, reg);
  98. of_property_read_string_index(node, "clock-output-names",
  99. SUN4I_A10_PLL2_1X, &clk_name);
  100. clks[SUN4I_A10_PLL2_1X] = clk_register_fixed_factor(NULL, clk_name,
  101. parent,
  102. CLK_SET_RATE_PARENT,
  103. 1,
  104. SUN4I_PLL2_POST_DIV_VALUE);
  105. WARN_ON(IS_ERR(clks[SUN4I_A10_PLL2_1X]));
  106. /*
  107. * PLL2-2x
  108. *
  109. * This clock doesn't use the post divider, and really is just
  110. * a fixed divider from the PLL2 base clock.
  111. */
  112. of_property_read_string_index(node, "clock-output-names",
  113. SUN4I_A10_PLL2_2X, &clk_name);
  114. clks[SUN4I_A10_PLL2_2X] = clk_register_fixed_factor(NULL, clk_name,
  115. parent,
  116. CLK_SET_RATE_PARENT,
  117. 1, 2);
  118. WARN_ON(IS_ERR(clks[SUN4I_A10_PLL2_2X]));
  119. /* PLL2-4x */
  120. of_property_read_string_index(node, "clock-output-names",
  121. SUN4I_A10_PLL2_4X, &clk_name);
  122. clks[SUN4I_A10_PLL2_4X] = clk_register_fixed_factor(NULL, clk_name,
  123. parent,
  124. CLK_SET_RATE_PARENT,
  125. 1, 1);
  126. WARN_ON(IS_ERR(clks[SUN4I_A10_PLL2_4X]));
  127. /* PLL2-8x */
  128. of_property_read_string_index(node, "clock-output-names",
  129. SUN4I_A10_PLL2_8X, &clk_name);
  130. clks[SUN4I_A10_PLL2_8X] = clk_register_fixed_factor(NULL, clk_name,
  131. parent,
  132. CLK_SET_RATE_PARENT,
  133. 2, 1);
  134. WARN_ON(IS_ERR(clks[SUN4I_A10_PLL2_8X]));
  135. clk_data->clks = clks;
  136. clk_data->clk_num = SUN4I_PLL2_OUTPUTS;
  137. of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
  138. return;
  139. err_free_multiplier:
  140. kfree(mult);
  141. err_free_gate:
  142. kfree(gate);
  143. err_unregister_prediv:
  144. clk_unregister_divider(prediv_clk);
  145. err_free_array:
  146. kfree(clks);
  147. err_free_data:
  148. kfree(clk_data);
  149. err_unmap:
  150. iounmap(reg);
  151. }
  152. static void __init sun4i_a10_pll2_setup(struct device_node *node)
  153. {
  154. sun4i_pll2_setup(node, 0);
  155. }
  156. CLK_OF_DECLARE(sun4i_a10_pll2, "allwinner,sun4i-a10-pll2-clk",
  157. sun4i_a10_pll2_setup);
  158. static void __init sun5i_a13_pll2_setup(struct device_node *node)
  159. {
  160. sun4i_pll2_setup(node, 1);
  161. }
  162. CLK_OF_DECLARE(sun5i_a13_pll2, "allwinner,sun5i-a13-pll2-clk",
  163. sun5i_a13_pll2_setup);