clk-fixed-factor.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * Copyright (C) 2011 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * Standard functionality for the common clock API.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/clk-provider.h>
  12. #include <linux/slab.h>
  13. #include <linux/err.h>
  14. #include <linux/of.h>
  15. /*
  16. * DOC: basic fixed multiplier and divider clock that cannot gate
  17. *
  18. * Traits of this clock:
  19. * prepare - clk_prepare only ensures that parents are prepared
  20. * enable - clk_enable only ensures that parents are enabled
  21. * rate - rate is fixed. clk->rate = parent->rate / div * mult
  22. * parent - fixed parent. No clk_set_parent support
  23. */
  24. static unsigned long clk_factor_recalc_rate(struct clk_hw *hw,
  25. unsigned long parent_rate)
  26. {
  27. struct clk_fixed_factor *fix = to_clk_fixed_factor(hw);
  28. unsigned long long int rate;
  29. rate = (unsigned long long int)parent_rate * fix->mult;
  30. do_div(rate, fix->div);
  31. return (unsigned long)rate;
  32. }
  33. static long clk_factor_round_rate(struct clk_hw *hw, unsigned long rate,
  34. unsigned long *prate)
  35. {
  36. struct clk_fixed_factor *fix = to_clk_fixed_factor(hw);
  37. if (clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT) {
  38. unsigned long best_parent;
  39. best_parent = (rate / fix->mult) * fix->div;
  40. *prate = clk_hw_round_rate(clk_hw_get_parent(hw), best_parent);
  41. }
  42. return (*prate / fix->div) * fix->mult;
  43. }
  44. static int clk_factor_set_rate(struct clk_hw *hw, unsigned long rate,
  45. unsigned long parent_rate)
  46. {
  47. /*
  48. * We must report success but we can do so unconditionally because
  49. * clk_factor_round_rate returns values that ensure this call is a
  50. * nop.
  51. */
  52. return 0;
  53. }
  54. const struct clk_ops clk_fixed_factor_ops = {
  55. .round_rate = clk_factor_round_rate,
  56. .set_rate = clk_factor_set_rate,
  57. .recalc_rate = clk_factor_recalc_rate,
  58. };
  59. EXPORT_SYMBOL_GPL(clk_fixed_factor_ops);
  60. struct clk_hw *clk_hw_register_fixed_factor(struct device *dev,
  61. const char *name, const char *parent_name, unsigned long flags,
  62. unsigned int mult, unsigned int div)
  63. {
  64. struct clk_fixed_factor *fix;
  65. struct clk_init_data init;
  66. struct clk_hw *hw;
  67. int ret;
  68. fix = kmalloc(sizeof(*fix), GFP_KERNEL);
  69. if (!fix)
  70. return ERR_PTR(-ENOMEM);
  71. /* struct clk_fixed_factor assignments */
  72. fix->mult = mult;
  73. fix->div = div;
  74. fix->hw.init = &init;
  75. init.name = name;
  76. init.ops = &clk_fixed_factor_ops;
  77. init.flags = flags | CLK_IS_BASIC;
  78. init.parent_names = &parent_name;
  79. init.num_parents = 1;
  80. hw = &fix->hw;
  81. ret = clk_hw_register(dev, hw);
  82. if (ret) {
  83. kfree(fix);
  84. hw = ERR_PTR(ret);
  85. }
  86. return hw;
  87. }
  88. EXPORT_SYMBOL_GPL(clk_hw_register_fixed_factor);
  89. struct clk *clk_register_fixed_factor(struct device *dev, const char *name,
  90. const char *parent_name, unsigned long flags,
  91. unsigned int mult, unsigned int div)
  92. {
  93. struct clk_hw *hw;
  94. hw = clk_hw_register_fixed_factor(dev, name, parent_name, flags, mult,
  95. div);
  96. if (IS_ERR(hw))
  97. return ERR_CAST(hw);
  98. return hw->clk;
  99. }
  100. EXPORT_SYMBOL_GPL(clk_register_fixed_factor);
  101. void clk_unregister_fixed_factor(struct clk *clk)
  102. {
  103. struct clk_hw *hw;
  104. hw = __clk_get_hw(clk);
  105. if (!hw)
  106. return;
  107. clk_unregister(clk);
  108. kfree(to_clk_fixed_factor(hw));
  109. }
  110. EXPORT_SYMBOL_GPL(clk_unregister_fixed_factor);
  111. void clk_hw_unregister_fixed_factor(struct clk_hw *hw)
  112. {
  113. struct clk_fixed_factor *fix;
  114. fix = to_clk_fixed_factor(hw);
  115. clk_hw_unregister(hw);
  116. kfree(fix);
  117. }
  118. EXPORT_SYMBOL_GPL(clk_hw_unregister_fixed_factor);
  119. #ifdef CONFIG_OF
  120. static const struct of_device_id set_rate_parent_matches[] = {
  121. { .compatible = "allwinner,sun4i-a10-pll3-2x-clk" },
  122. { /* Sentinel */ },
  123. };
  124. /**
  125. * of_fixed_factor_clk_setup() - Setup function for simple fixed factor clock
  126. */
  127. void __init of_fixed_factor_clk_setup(struct device_node *node)
  128. {
  129. struct clk *clk;
  130. const char *clk_name = node->name;
  131. const char *parent_name;
  132. unsigned long flags = 0;
  133. u32 div, mult;
  134. if (of_property_read_u32(node, "clock-div", &div)) {
  135. pr_err("%s Fixed factor clock <%s> must have a clock-div property\n",
  136. __func__, node->name);
  137. return;
  138. }
  139. if (of_property_read_u32(node, "clock-mult", &mult)) {
  140. pr_err("%s Fixed factor clock <%s> must have a clock-mult property\n",
  141. __func__, node->name);
  142. return;
  143. }
  144. of_property_read_string(node, "clock-output-names", &clk_name);
  145. parent_name = of_clk_get_parent_name(node, 0);
  146. if (of_match_node(set_rate_parent_matches, node))
  147. flags |= CLK_SET_RATE_PARENT;
  148. clk = clk_register_fixed_factor(NULL, clk_name, parent_name, flags,
  149. mult, div);
  150. if (!IS_ERR(clk))
  151. of_clk_add_provider(node, of_clk_src_simple_get, clk);
  152. }
  153. EXPORT_SYMBOL_GPL(of_fixed_factor_clk_setup);
  154. CLK_OF_DECLARE(fixed_factor_clk, "fixed-factor-clock",
  155. of_fixed_factor_clk_setup);
  156. #endif