clk-fixed-factor.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2011 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>
  4. */
  5. #include <linux/module.h>
  6. #include <linux/clk-provider.h>
  7. #include <linux/slab.h>
  8. #include <linux/err.h>
  9. #include <linux/of.h>
  10. #include <linux/platform_device.h>
  11. /*
  12. * DOC: basic fixed multiplier and divider clock that cannot gate
  13. *
  14. * Traits of this clock:
  15. * prepare - clk_prepare only ensures that parents are prepared
  16. * enable - clk_enable only ensures that parents are enabled
  17. * rate - rate is fixed. clk->rate = parent->rate / div * mult
  18. * parent - fixed parent. No clk_set_parent support
  19. */
  20. static unsigned long clk_factor_recalc_rate(struct clk_hw *hw,
  21. unsigned long parent_rate)
  22. {
  23. struct clk_fixed_factor *fix = to_clk_fixed_factor(hw);
  24. unsigned long long int rate;
  25. rate = (unsigned long long int)parent_rate * fix->mult;
  26. do_div(rate, fix->div);
  27. return (unsigned long)rate;
  28. }
  29. static long clk_factor_round_rate(struct clk_hw *hw, unsigned long rate,
  30. unsigned long *prate)
  31. {
  32. struct clk_fixed_factor *fix = to_clk_fixed_factor(hw);
  33. if (clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT) {
  34. unsigned long best_parent;
  35. best_parent = (rate / fix->mult) * fix->div;
  36. *prate = clk_hw_round_rate(clk_hw_get_parent(hw), best_parent);
  37. }
  38. return (*prate / fix->div) * fix->mult;
  39. }
  40. static int clk_factor_set_rate(struct clk_hw *hw, unsigned long rate,
  41. unsigned long parent_rate)
  42. {
  43. /*
  44. * We must report success but we can do so unconditionally because
  45. * clk_factor_round_rate returns values that ensure this call is a
  46. * nop.
  47. */
  48. return 0;
  49. }
  50. const struct clk_ops clk_fixed_factor_ops = {
  51. .round_rate = clk_factor_round_rate,
  52. .set_rate = clk_factor_set_rate,
  53. .recalc_rate = clk_factor_recalc_rate,
  54. };
  55. EXPORT_SYMBOL_GPL(clk_fixed_factor_ops);
  56. static struct clk_hw *
  57. __clk_hw_register_fixed_factor(struct device *dev, struct device_node *np,
  58. const char *name, const char *parent_name, int index,
  59. unsigned long flags, unsigned int mult, unsigned int div)
  60. {
  61. struct clk_fixed_factor *fix;
  62. struct clk_init_data init = { };
  63. struct clk_parent_data pdata = { .index = index };
  64. struct clk_hw *hw;
  65. int ret;
  66. fix = kmalloc(sizeof(*fix), GFP_KERNEL);
  67. if (!fix)
  68. return ERR_PTR(-ENOMEM);
  69. /* struct clk_fixed_factor assignments */
  70. fix->mult = mult;
  71. fix->div = div;
  72. fix->hw.init = &init;
  73. init.name = name;
  74. init.ops = &clk_fixed_factor_ops;
  75. init.flags = flags;
  76. if (parent_name)
  77. init.parent_names = &parent_name;
  78. else
  79. init.parent_data = &pdata;
  80. init.num_parents = 1;
  81. hw = &fix->hw;
  82. if (dev)
  83. ret = clk_hw_register(dev, hw);
  84. else
  85. ret = of_clk_hw_register(np, hw);
  86. if (ret) {
  87. kfree(fix);
  88. hw = ERR_PTR(ret);
  89. }
  90. return hw;
  91. }
  92. struct clk_hw *clk_hw_register_fixed_factor(struct device *dev,
  93. const char *name, const char *parent_name, unsigned long flags,
  94. unsigned int mult, unsigned int div)
  95. {
  96. return __clk_hw_register_fixed_factor(dev, NULL, name, parent_name, -1,
  97. flags, mult, div);
  98. }
  99. EXPORT_SYMBOL_GPL(clk_hw_register_fixed_factor);
  100. struct clk *clk_register_fixed_factor(struct device *dev, const char *name,
  101. const char *parent_name, unsigned long flags,
  102. unsigned int mult, unsigned int div)
  103. {
  104. struct clk_hw *hw;
  105. hw = clk_hw_register_fixed_factor(dev, name, parent_name, flags, mult,
  106. div);
  107. if (IS_ERR(hw))
  108. return ERR_CAST(hw);
  109. return hw->clk;
  110. }
  111. EXPORT_SYMBOL_GPL(clk_register_fixed_factor);
  112. void clk_unregister_fixed_factor(struct clk *clk)
  113. {
  114. struct clk_hw *hw;
  115. hw = __clk_get_hw(clk);
  116. if (!hw)
  117. return;
  118. clk_unregister(clk);
  119. kfree(to_clk_fixed_factor(hw));
  120. }
  121. EXPORT_SYMBOL_GPL(clk_unregister_fixed_factor);
  122. void clk_hw_unregister_fixed_factor(struct clk_hw *hw)
  123. {
  124. struct clk_fixed_factor *fix;
  125. fix = to_clk_fixed_factor(hw);
  126. clk_hw_unregister(hw);
  127. kfree(fix);
  128. }
  129. EXPORT_SYMBOL_GPL(clk_hw_unregister_fixed_factor);
  130. #ifdef CONFIG_OF
  131. static const struct of_device_id set_rate_parent_matches[] = {
  132. { .compatible = "allwinner,sun4i-a10-pll3-2x-clk" },
  133. { /* Sentinel */ },
  134. };
  135. static struct clk_hw *_of_fixed_factor_clk_setup(struct device_node *node)
  136. {
  137. struct clk_hw *hw;
  138. const char *clk_name = node->name;
  139. unsigned long flags = 0;
  140. u32 div, mult;
  141. int ret;
  142. if (of_property_read_u32(node, "clock-div", &div)) {
  143. pr_err("%s Fixed factor clock <%pOFn> must have a clock-div property\n",
  144. __func__, node);
  145. return ERR_PTR(-EIO);
  146. }
  147. if (of_property_read_u32(node, "clock-mult", &mult)) {
  148. pr_err("%s Fixed factor clock <%pOFn> must have a clock-mult property\n",
  149. __func__, node);
  150. return ERR_PTR(-EIO);
  151. }
  152. of_property_read_string(node, "clock-output-names", &clk_name);
  153. if (of_match_node(set_rate_parent_matches, node))
  154. flags |= CLK_SET_RATE_PARENT;
  155. hw = __clk_hw_register_fixed_factor(NULL, node, clk_name, NULL, 0,
  156. flags, mult, div);
  157. if (IS_ERR(hw)) {
  158. /*
  159. * Clear OF_POPULATED flag so that clock registration can be
  160. * attempted again from probe function.
  161. */
  162. of_node_clear_flag(node, OF_POPULATED);
  163. return ERR_CAST(hw);
  164. }
  165. ret = of_clk_add_hw_provider(node, of_clk_hw_simple_get, hw);
  166. if (ret) {
  167. clk_hw_unregister_fixed_factor(hw);
  168. return ERR_PTR(ret);
  169. }
  170. return hw;
  171. }
  172. /**
  173. * of_fixed_factor_clk_setup() - Setup function for simple fixed factor clock
  174. * @node: device node for the clock
  175. */
  176. void __init of_fixed_factor_clk_setup(struct device_node *node)
  177. {
  178. _of_fixed_factor_clk_setup(node);
  179. }
  180. CLK_OF_DECLARE(fixed_factor_clk, "fixed-factor-clock",
  181. of_fixed_factor_clk_setup);
  182. static int of_fixed_factor_clk_remove(struct platform_device *pdev)
  183. {
  184. struct clk_hw *clk = platform_get_drvdata(pdev);
  185. of_clk_del_provider(pdev->dev.of_node);
  186. clk_hw_unregister_fixed_factor(clk);
  187. return 0;
  188. }
  189. static int of_fixed_factor_clk_probe(struct platform_device *pdev)
  190. {
  191. struct clk_hw *clk;
  192. /*
  193. * This function is not executed when of_fixed_factor_clk_setup
  194. * succeeded.
  195. */
  196. clk = _of_fixed_factor_clk_setup(pdev->dev.of_node);
  197. if (IS_ERR(clk))
  198. return PTR_ERR(clk);
  199. platform_set_drvdata(pdev, clk);
  200. return 0;
  201. }
  202. static const struct of_device_id of_fixed_factor_clk_ids[] = {
  203. { .compatible = "fixed-factor-clock" },
  204. { }
  205. };
  206. MODULE_DEVICE_TABLE(of, of_fixed_factor_clk_ids);
  207. static struct platform_driver of_fixed_factor_clk_driver = {
  208. .driver = {
  209. .name = "of_fixed_factor_clk",
  210. .of_match_table = of_fixed_factor_clk_ids,
  211. },
  212. .probe = of_fixed_factor_clk_probe,
  213. .remove = of_fixed_factor_clk_remove,
  214. };
  215. builtin_platform_driver(of_fixed_factor_clk_driver);
  216. #endif