clk-fixed-factor.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2011 Sascha Hauer, Pengutronix <[email protected]>
  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 void devm_clk_hw_register_fixed_factor_release(struct device *dev, void *res)
  57. {
  58. struct clk_fixed_factor *fix = res;
  59. /*
  60. * We can not use clk_hw_unregister_fixed_factor, since it will kfree()
  61. * the hw, resulting in double free. Just unregister the hw and let
  62. * devres code kfree() it.
  63. */
  64. clk_hw_unregister(&fix->hw);
  65. }
  66. static struct clk_hw *
  67. __clk_hw_register_fixed_factor(struct device *dev, struct device_node *np,
  68. const char *name, const char *parent_name,
  69. const struct clk_hw *parent_hw, int index,
  70. unsigned long flags, unsigned int mult, unsigned int div,
  71. bool devm)
  72. {
  73. struct clk_fixed_factor *fix;
  74. struct clk_init_data init = { };
  75. struct clk_parent_data pdata = { .index = index };
  76. struct clk_hw *hw;
  77. int ret;
  78. /* You can't use devm without a dev */
  79. if (devm && !dev)
  80. return ERR_PTR(-EINVAL);
  81. if (devm)
  82. fix = devres_alloc(devm_clk_hw_register_fixed_factor_release,
  83. sizeof(*fix), GFP_KERNEL);
  84. else
  85. fix = kmalloc(sizeof(*fix), GFP_KERNEL);
  86. if (!fix)
  87. return ERR_PTR(-ENOMEM);
  88. /* struct clk_fixed_factor assignments */
  89. fix->mult = mult;
  90. fix->div = div;
  91. fix->hw.init = &init;
  92. init.name = name;
  93. init.ops = &clk_fixed_factor_ops;
  94. init.flags = flags;
  95. if (parent_name)
  96. init.parent_names = &parent_name;
  97. else if (parent_hw)
  98. init.parent_hws = &parent_hw;
  99. else
  100. init.parent_data = &pdata;
  101. init.num_parents = 1;
  102. hw = &fix->hw;
  103. if (dev)
  104. ret = clk_hw_register(dev, hw);
  105. else
  106. ret = of_clk_hw_register(np, hw);
  107. if (ret) {
  108. if (devm)
  109. devres_free(fix);
  110. else
  111. kfree(fix);
  112. hw = ERR_PTR(ret);
  113. } else if (devm)
  114. devres_add(dev, fix);
  115. return hw;
  116. }
  117. /**
  118. * devm_clk_hw_register_fixed_factor_index - Register a fixed factor clock with
  119. * parent from DT index
  120. * @dev: device that is registering this clock
  121. * @name: name of this clock
  122. * @index: index of phandle in @dev 'clocks' property
  123. * @flags: fixed factor flags
  124. * @mult: multiplier
  125. * @div: divider
  126. *
  127. * Return: Pointer to fixed factor clk_hw structure that was registered or
  128. * an error pointer.
  129. */
  130. struct clk_hw *devm_clk_hw_register_fixed_factor_index(struct device *dev,
  131. const char *name, unsigned int index, unsigned long flags,
  132. unsigned int mult, unsigned int div)
  133. {
  134. return __clk_hw_register_fixed_factor(dev, NULL, name, NULL, NULL, index,
  135. flags, mult, div, true);
  136. }
  137. EXPORT_SYMBOL_GPL(devm_clk_hw_register_fixed_factor_index);
  138. /**
  139. * devm_clk_hw_register_fixed_factor_parent_hw - Register a fixed factor clock with
  140. * pointer to parent clock
  141. * @dev: device that is registering this clock
  142. * @name: name of this clock
  143. * @parent_hw: pointer to parent clk
  144. * @flags: fixed factor flags
  145. * @mult: multiplier
  146. * @div: divider
  147. *
  148. * Return: Pointer to fixed factor clk_hw structure that was registered or
  149. * an error pointer.
  150. */
  151. struct clk_hw *devm_clk_hw_register_fixed_factor_parent_hw(struct device *dev,
  152. const char *name, const struct clk_hw *parent_hw,
  153. unsigned long flags, unsigned int mult, unsigned int div)
  154. {
  155. return __clk_hw_register_fixed_factor(dev, NULL, name, NULL, parent_hw,
  156. -1, flags, mult, div, true);
  157. }
  158. EXPORT_SYMBOL_GPL(devm_clk_hw_register_fixed_factor_parent_hw);
  159. struct clk_hw *clk_hw_register_fixed_factor_parent_hw(struct device *dev,
  160. const char *name, const struct clk_hw *parent_hw,
  161. unsigned long flags, unsigned int mult, unsigned int div)
  162. {
  163. return __clk_hw_register_fixed_factor(dev, NULL, name, NULL,
  164. parent_hw, -1, flags, mult, div,
  165. false);
  166. }
  167. EXPORT_SYMBOL_GPL(clk_hw_register_fixed_factor_parent_hw);
  168. struct clk_hw *clk_hw_register_fixed_factor(struct device *dev,
  169. const char *name, const char *parent_name, unsigned long flags,
  170. unsigned int mult, unsigned int div)
  171. {
  172. return __clk_hw_register_fixed_factor(dev, NULL, name, parent_name, NULL, -1,
  173. flags, mult, div, false);
  174. }
  175. EXPORT_SYMBOL_GPL(clk_hw_register_fixed_factor);
  176. struct clk *clk_register_fixed_factor(struct device *dev, const char *name,
  177. const char *parent_name, unsigned long flags,
  178. unsigned int mult, unsigned int div)
  179. {
  180. struct clk_hw *hw;
  181. hw = clk_hw_register_fixed_factor(dev, name, parent_name, flags, mult,
  182. div);
  183. if (IS_ERR(hw))
  184. return ERR_CAST(hw);
  185. return hw->clk;
  186. }
  187. EXPORT_SYMBOL_GPL(clk_register_fixed_factor);
  188. void clk_unregister_fixed_factor(struct clk *clk)
  189. {
  190. struct clk_hw *hw;
  191. hw = __clk_get_hw(clk);
  192. if (!hw)
  193. return;
  194. clk_unregister(clk);
  195. kfree(to_clk_fixed_factor(hw));
  196. }
  197. EXPORT_SYMBOL_GPL(clk_unregister_fixed_factor);
  198. void clk_hw_unregister_fixed_factor(struct clk_hw *hw)
  199. {
  200. struct clk_fixed_factor *fix;
  201. fix = to_clk_fixed_factor(hw);
  202. clk_hw_unregister(hw);
  203. kfree(fix);
  204. }
  205. EXPORT_SYMBOL_GPL(clk_hw_unregister_fixed_factor);
  206. struct clk_hw *devm_clk_hw_register_fixed_factor(struct device *dev,
  207. const char *name, const char *parent_name, unsigned long flags,
  208. unsigned int mult, unsigned int div)
  209. {
  210. return __clk_hw_register_fixed_factor(dev, NULL, name, parent_name, NULL, -1,
  211. flags, mult, div, true);
  212. }
  213. EXPORT_SYMBOL_GPL(devm_clk_hw_register_fixed_factor);
  214. #ifdef CONFIG_OF
  215. static struct clk_hw *_of_fixed_factor_clk_setup(struct device_node *node)
  216. {
  217. struct clk_hw *hw;
  218. const char *clk_name = node->name;
  219. u32 div, mult;
  220. int ret;
  221. if (of_property_read_u32(node, "clock-div", &div)) {
  222. pr_err("%s Fixed factor clock <%pOFn> must have a clock-div property\n",
  223. __func__, node);
  224. return ERR_PTR(-EIO);
  225. }
  226. if (of_property_read_u32(node, "clock-mult", &mult)) {
  227. pr_err("%s Fixed factor clock <%pOFn> must have a clock-mult property\n",
  228. __func__, node);
  229. return ERR_PTR(-EIO);
  230. }
  231. of_property_read_string(node, "clock-output-names", &clk_name);
  232. hw = __clk_hw_register_fixed_factor(NULL, node, clk_name, NULL, NULL, 0,
  233. 0, mult, div, false);
  234. if (IS_ERR(hw)) {
  235. /*
  236. * Clear OF_POPULATED flag so that clock registration can be
  237. * attempted again from probe function.
  238. */
  239. of_node_clear_flag(node, OF_POPULATED);
  240. return ERR_CAST(hw);
  241. }
  242. ret = of_clk_add_hw_provider(node, of_clk_hw_simple_get, hw);
  243. if (ret) {
  244. clk_hw_unregister_fixed_factor(hw);
  245. return ERR_PTR(ret);
  246. }
  247. return hw;
  248. }
  249. /**
  250. * of_fixed_factor_clk_setup() - Setup function for simple fixed factor clock
  251. * @node: device node for the clock
  252. */
  253. void __init of_fixed_factor_clk_setup(struct device_node *node)
  254. {
  255. _of_fixed_factor_clk_setup(node);
  256. }
  257. CLK_OF_DECLARE(fixed_factor_clk, "fixed-factor-clock",
  258. of_fixed_factor_clk_setup);
  259. static int of_fixed_factor_clk_remove(struct platform_device *pdev)
  260. {
  261. struct clk_hw *clk = platform_get_drvdata(pdev);
  262. of_clk_del_provider(pdev->dev.of_node);
  263. clk_hw_unregister_fixed_factor(clk);
  264. return 0;
  265. }
  266. static int of_fixed_factor_clk_probe(struct platform_device *pdev)
  267. {
  268. struct clk_hw *clk;
  269. /*
  270. * This function is not executed when of_fixed_factor_clk_setup
  271. * succeeded.
  272. */
  273. clk = _of_fixed_factor_clk_setup(pdev->dev.of_node);
  274. if (IS_ERR(clk))
  275. return PTR_ERR(clk);
  276. platform_set_drvdata(pdev, clk);
  277. return 0;
  278. }
  279. static const struct of_device_id of_fixed_factor_clk_ids[] = {
  280. { .compatible = "fixed-factor-clock" },
  281. { }
  282. };
  283. MODULE_DEVICE_TABLE(of, of_fixed_factor_clk_ids);
  284. static struct platform_driver of_fixed_factor_clk_driver = {
  285. .driver = {
  286. .name = "of_fixed_factor_clk",
  287. .of_match_table = of_fixed_factor_clk_ids,
  288. },
  289. .probe = of_fixed_factor_clk_probe,
  290. .remove = of_fixed_factor_clk_remove,
  291. };
  292. builtin_platform_driver(of_fixed_factor_clk_driver);
  293. #endif