clk-fixed-factor.c 5.8 KB

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