clk-fixed-rate.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2010-2011 Canonical Ltd <[email protected]>
  4. * Copyright (C) 2011-2012 Mike Turquette, Linaro Ltd <[email protected]>
  5. *
  6. * Fixed rate clock implementation
  7. */
  8. #include <linux/clk-provider.h>
  9. #include <linux/module.h>
  10. #include <linux/slab.h>
  11. #include <linux/io.h>
  12. #include <linux/err.h>
  13. #include <linux/of.h>
  14. #include <linux/platform_device.h>
  15. /*
  16. * DOC: basic fixed-rate clock that cannot gate
  17. *
  18. * Traits of this clock:
  19. * prepare - clk_(un)prepare only ensures parents are prepared
  20. * enable - clk_enable only ensures parents are enabled
  21. * rate - rate is always a fixed value. No clk_set_rate support
  22. * parent - fixed parent. No clk_set_parent support
  23. */
  24. #define to_clk_fixed_rate(_hw) container_of(_hw, struct clk_fixed_rate, hw)
  25. static unsigned long clk_fixed_rate_recalc_rate(struct clk_hw *hw,
  26. unsigned long parent_rate)
  27. {
  28. return to_clk_fixed_rate(hw)->fixed_rate;
  29. }
  30. static unsigned long clk_fixed_rate_recalc_accuracy(struct clk_hw *hw,
  31. unsigned long parent_accuracy)
  32. {
  33. struct clk_fixed_rate *fixed = to_clk_fixed_rate(hw);
  34. if (fixed->flags & CLK_FIXED_RATE_PARENT_ACCURACY)
  35. return parent_accuracy;
  36. return fixed->fixed_accuracy;
  37. }
  38. const struct clk_ops clk_fixed_rate_ops = {
  39. .recalc_rate = clk_fixed_rate_recalc_rate,
  40. .recalc_accuracy = clk_fixed_rate_recalc_accuracy,
  41. };
  42. EXPORT_SYMBOL_GPL(clk_fixed_rate_ops);
  43. static void devm_clk_hw_register_fixed_rate_release(struct device *dev, void *res)
  44. {
  45. struct clk_fixed_rate *fix = res;
  46. /*
  47. * We can not use clk_hw_unregister_fixed_rate, since it will kfree()
  48. * the hw, resulting in double free. Just unregister the hw and let
  49. * devres code kfree() it.
  50. */
  51. clk_hw_unregister(&fix->hw);
  52. }
  53. struct clk_hw *__clk_hw_register_fixed_rate(struct device *dev,
  54. struct device_node *np, const char *name,
  55. const char *parent_name, const struct clk_hw *parent_hw,
  56. const struct clk_parent_data *parent_data, unsigned long flags,
  57. unsigned long fixed_rate, unsigned long fixed_accuracy,
  58. unsigned long clk_fixed_flags, bool devm)
  59. {
  60. struct clk_fixed_rate *fixed;
  61. struct clk_hw *hw;
  62. struct clk_init_data init = {};
  63. int ret = -EINVAL;
  64. /* allocate fixed-rate clock */
  65. if (devm)
  66. fixed = devres_alloc(devm_clk_hw_register_fixed_rate_release,
  67. sizeof(*fixed), GFP_KERNEL);
  68. else
  69. fixed = kzalloc(sizeof(*fixed), GFP_KERNEL);
  70. if (!fixed)
  71. return ERR_PTR(-ENOMEM);
  72. init.name = name;
  73. init.ops = &clk_fixed_rate_ops;
  74. init.flags = flags;
  75. init.parent_names = parent_name ? &parent_name : NULL;
  76. init.parent_hws = parent_hw ? &parent_hw : NULL;
  77. init.parent_data = parent_data;
  78. if (parent_name || parent_hw || parent_data)
  79. init.num_parents = 1;
  80. else
  81. init.num_parents = 0;
  82. /* struct clk_fixed_rate assignments */
  83. fixed->flags = clk_fixed_flags;
  84. fixed->fixed_rate = fixed_rate;
  85. fixed->fixed_accuracy = fixed_accuracy;
  86. fixed->hw.init = &init;
  87. /* register the clock */
  88. hw = &fixed->hw;
  89. if (dev || !np)
  90. ret = clk_hw_register(dev, hw);
  91. else
  92. ret = of_clk_hw_register(np, hw);
  93. if (ret) {
  94. if (devm)
  95. devres_free(fixed);
  96. else
  97. kfree(fixed);
  98. hw = ERR_PTR(ret);
  99. } else if (devm)
  100. devres_add(dev, fixed);
  101. return hw;
  102. }
  103. EXPORT_SYMBOL_GPL(__clk_hw_register_fixed_rate);
  104. struct clk *clk_register_fixed_rate(struct device *dev, const char *name,
  105. const char *parent_name, unsigned long flags,
  106. unsigned long fixed_rate)
  107. {
  108. struct clk_hw *hw;
  109. hw = clk_hw_register_fixed_rate_with_accuracy(dev, name, parent_name,
  110. flags, fixed_rate, 0);
  111. if (IS_ERR(hw))
  112. return ERR_CAST(hw);
  113. return hw->clk;
  114. }
  115. EXPORT_SYMBOL_GPL(clk_register_fixed_rate);
  116. void clk_unregister_fixed_rate(struct clk *clk)
  117. {
  118. struct clk_hw *hw;
  119. hw = __clk_get_hw(clk);
  120. if (!hw)
  121. return;
  122. clk_unregister(clk);
  123. kfree(to_clk_fixed_rate(hw));
  124. }
  125. EXPORT_SYMBOL_GPL(clk_unregister_fixed_rate);
  126. void clk_hw_unregister_fixed_rate(struct clk_hw *hw)
  127. {
  128. struct clk_fixed_rate *fixed;
  129. fixed = to_clk_fixed_rate(hw);
  130. clk_hw_unregister(hw);
  131. kfree(fixed);
  132. }
  133. EXPORT_SYMBOL_GPL(clk_hw_unregister_fixed_rate);
  134. #ifdef CONFIG_OF
  135. static struct clk_hw *_of_fixed_clk_setup(struct device_node *node)
  136. {
  137. struct clk_hw *hw;
  138. const char *clk_name = node->name;
  139. u32 rate;
  140. u32 accuracy = 0;
  141. int ret;
  142. if (of_property_read_u32(node, "clock-frequency", &rate))
  143. return ERR_PTR(-EIO);
  144. of_property_read_u32(node, "clock-accuracy", &accuracy);
  145. of_property_read_string(node, "clock-output-names", &clk_name);
  146. hw = clk_hw_register_fixed_rate_with_accuracy(NULL, clk_name, NULL,
  147. 0, rate, accuracy);
  148. if (IS_ERR(hw))
  149. return hw;
  150. ret = of_clk_add_hw_provider(node, of_clk_hw_simple_get, hw);
  151. if (ret) {
  152. clk_hw_unregister_fixed_rate(hw);
  153. return ERR_PTR(ret);
  154. }
  155. return hw;
  156. }
  157. /**
  158. * of_fixed_clk_setup() - Setup function for simple fixed rate clock
  159. * @node: device node for the clock
  160. */
  161. void __init of_fixed_clk_setup(struct device_node *node)
  162. {
  163. _of_fixed_clk_setup(node);
  164. }
  165. CLK_OF_DECLARE(fixed_clk, "fixed-clock", of_fixed_clk_setup);
  166. static int of_fixed_clk_remove(struct platform_device *pdev)
  167. {
  168. struct clk_hw *hw = platform_get_drvdata(pdev);
  169. of_clk_del_provider(pdev->dev.of_node);
  170. clk_hw_unregister_fixed_rate(hw);
  171. return 0;
  172. }
  173. static int of_fixed_clk_probe(struct platform_device *pdev)
  174. {
  175. struct clk_hw *hw;
  176. /*
  177. * This function is not executed when of_fixed_clk_setup
  178. * succeeded.
  179. */
  180. hw = _of_fixed_clk_setup(pdev->dev.of_node);
  181. if (IS_ERR(hw))
  182. return PTR_ERR(hw);
  183. platform_set_drvdata(pdev, hw);
  184. return 0;
  185. }
  186. static const struct of_device_id of_fixed_clk_ids[] = {
  187. { .compatible = "fixed-clock" },
  188. { }
  189. };
  190. static struct platform_driver of_fixed_clk_driver = {
  191. .driver = {
  192. .name = "of_fixed_clk",
  193. .of_match_table = of_fixed_clk_ids,
  194. },
  195. .probe = of_fixed_clk_probe,
  196. .remove = of_fixed_clk_remove,
  197. };
  198. builtin_platform_driver(of_fixed_clk_driver);
  199. #endif