clk-fixed-rate.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2010-2011 Canonical Ltd <jeremy.kerr@canonical.com>
  4. * Copyright (C) 2011-2012 Mike Turquette, Linaro Ltd <mturquette@linaro.org>
  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. struct clk_hw *__clk_hw_register_fixed_rate(struct device *dev,
  44. struct device_node *np, const char *name,
  45. const char *parent_name, const struct clk_hw *parent_hw,
  46. const struct clk_parent_data *parent_data, unsigned long flags,
  47. unsigned long fixed_rate, unsigned long fixed_accuracy,
  48. unsigned long clk_fixed_flags)
  49. {
  50. struct clk_fixed_rate *fixed;
  51. struct clk_hw *hw;
  52. struct clk_init_data init = {};
  53. int ret = -EINVAL;
  54. /* allocate fixed-rate clock */
  55. fixed = kzalloc(sizeof(*fixed), GFP_KERNEL);
  56. if (!fixed)
  57. return ERR_PTR(-ENOMEM);
  58. init.name = name;
  59. init.ops = &clk_fixed_rate_ops;
  60. init.flags = flags;
  61. init.parent_names = parent_name ? &parent_name : NULL;
  62. init.parent_hws = parent_hw ? &parent_hw : NULL;
  63. init.parent_data = parent_data;
  64. if (parent_name || parent_hw || parent_data)
  65. init.num_parents = 1;
  66. else
  67. init.num_parents = 0;
  68. /* struct clk_fixed_rate assignments */
  69. fixed->flags = clk_fixed_flags;
  70. fixed->fixed_rate = fixed_rate;
  71. fixed->fixed_accuracy = fixed_accuracy;
  72. fixed->hw.init = &init;
  73. /* register the clock */
  74. hw = &fixed->hw;
  75. if (dev || !np)
  76. ret = clk_hw_register(dev, hw);
  77. else if (np)
  78. ret = of_clk_hw_register(np, hw);
  79. if (ret) {
  80. kfree(fixed);
  81. hw = ERR_PTR(ret);
  82. }
  83. return hw;
  84. }
  85. EXPORT_SYMBOL_GPL(__clk_hw_register_fixed_rate);
  86. struct clk *clk_register_fixed_rate(struct device *dev, const char *name,
  87. const char *parent_name, unsigned long flags,
  88. unsigned long fixed_rate)
  89. {
  90. struct clk_hw *hw;
  91. hw = clk_hw_register_fixed_rate_with_accuracy(dev, name, parent_name,
  92. flags, fixed_rate, 0);
  93. if (IS_ERR(hw))
  94. return ERR_CAST(hw);
  95. return hw->clk;
  96. }
  97. EXPORT_SYMBOL_GPL(clk_register_fixed_rate);
  98. void clk_unregister_fixed_rate(struct clk *clk)
  99. {
  100. struct clk_hw *hw;
  101. hw = __clk_get_hw(clk);
  102. if (!hw)
  103. return;
  104. clk_unregister(clk);
  105. kfree(to_clk_fixed_rate(hw));
  106. }
  107. EXPORT_SYMBOL_GPL(clk_unregister_fixed_rate);
  108. void clk_hw_unregister_fixed_rate(struct clk_hw *hw)
  109. {
  110. struct clk_fixed_rate *fixed;
  111. fixed = to_clk_fixed_rate(hw);
  112. clk_hw_unregister(hw);
  113. kfree(fixed);
  114. }
  115. EXPORT_SYMBOL_GPL(clk_hw_unregister_fixed_rate);
  116. #ifdef CONFIG_OF
  117. static struct clk_hw *_of_fixed_clk_setup(struct device_node *node)
  118. {
  119. struct clk_hw *hw;
  120. const char *clk_name = node->name;
  121. u32 rate;
  122. u32 accuracy = 0;
  123. int ret;
  124. if (of_property_read_u32(node, "clock-frequency", &rate))
  125. return ERR_PTR(-EIO);
  126. of_property_read_u32(node, "clock-accuracy", &accuracy);
  127. of_property_read_string(node, "clock-output-names", &clk_name);
  128. hw = clk_hw_register_fixed_rate_with_accuracy(NULL, clk_name, NULL,
  129. 0, rate, accuracy);
  130. if (IS_ERR(hw))
  131. return hw;
  132. ret = of_clk_add_hw_provider(node, of_clk_hw_simple_get, hw);
  133. if (ret) {
  134. clk_hw_unregister_fixed_rate(hw);
  135. return ERR_PTR(ret);
  136. }
  137. return hw;
  138. }
  139. /**
  140. * of_fixed_clk_setup() - Setup function for simple fixed rate clock
  141. * @node: device node for the clock
  142. */
  143. void __init of_fixed_clk_setup(struct device_node *node)
  144. {
  145. _of_fixed_clk_setup(node);
  146. }
  147. CLK_OF_DECLARE(fixed_clk, "fixed-clock", of_fixed_clk_setup);
  148. static int of_fixed_clk_remove(struct platform_device *pdev)
  149. {
  150. struct clk_hw *hw = platform_get_drvdata(pdev);
  151. of_clk_del_provider(pdev->dev.of_node);
  152. clk_hw_unregister_fixed_rate(hw);
  153. return 0;
  154. }
  155. static int of_fixed_clk_probe(struct platform_device *pdev)
  156. {
  157. struct clk_hw *hw;
  158. /*
  159. * This function is not executed when of_fixed_clk_setup
  160. * succeeded.
  161. */
  162. hw = _of_fixed_clk_setup(pdev->dev.of_node);
  163. if (IS_ERR(hw))
  164. return PTR_ERR(hw);
  165. platform_set_drvdata(pdev, hw);
  166. return 0;
  167. }
  168. static const struct of_device_id of_fixed_clk_ids[] = {
  169. { .compatible = "fixed-clock" },
  170. { }
  171. };
  172. static struct platform_driver of_fixed_clk_driver = {
  173. .driver = {
  174. .name = "of_fixed_clk",
  175. .of_match_table = of_fixed_clk_ids,
  176. },
  177. .probe = of_fixed_clk_probe,
  178. .remove = of_fixed_clk_remove,
  179. };
  180. builtin_platform_driver(of_fixed_clk_driver);
  181. #endif