clk-fixed-rate.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * Copyright (C) 2010-2011 Canonical Ltd <jeremy.kerr@canonical.com>
  3. * Copyright (C) 2011-2012 Mike Turquette, Linaro Ltd <mturquette@linaro.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Fixed rate clock implementation
  10. */
  11. #include <linux/clk-provider.h>
  12. #include <linux/module.h>
  13. #include <linux/slab.h>
  14. #include <linux/io.h>
  15. #include <linux/err.h>
  16. #include <linux/of.h>
  17. #include <linux/platform_device.h>
  18. /*
  19. * DOC: basic fixed-rate clock that cannot gate
  20. *
  21. * Traits of this clock:
  22. * prepare - clk_(un)prepare only ensures parents are prepared
  23. * enable - clk_enable only ensures parents are enabled
  24. * rate - rate is always a fixed value. No clk_set_rate support
  25. * parent - fixed parent. No clk_set_parent support
  26. */
  27. static unsigned long clk_fixed_rate_recalc_rate(struct clk_hw *hw,
  28. unsigned long parent_rate)
  29. {
  30. return to_clk_fixed_rate(hw)->fixed_rate;
  31. }
  32. static unsigned long clk_fixed_rate_recalc_accuracy(struct clk_hw *hw,
  33. unsigned long parent_accuracy)
  34. {
  35. return to_clk_fixed_rate(hw)->fixed_accuracy;
  36. }
  37. const struct clk_ops clk_fixed_rate_ops = {
  38. .recalc_rate = clk_fixed_rate_recalc_rate,
  39. .recalc_accuracy = clk_fixed_rate_recalc_accuracy,
  40. };
  41. EXPORT_SYMBOL_GPL(clk_fixed_rate_ops);
  42. /**
  43. * clk_hw_register_fixed_rate_with_accuracy - register fixed-rate clock with
  44. * the clock framework
  45. * @dev: device that is registering this clock
  46. * @name: name of this clock
  47. * @parent_name: name of clock's parent
  48. * @flags: framework-specific flags
  49. * @fixed_rate: non-adjustable clock rate
  50. * @fixed_accuracy: non-adjustable clock rate
  51. */
  52. struct clk_hw *clk_hw_register_fixed_rate_with_accuracy(struct device *dev,
  53. const char *name, const char *parent_name, unsigned long flags,
  54. unsigned long fixed_rate, unsigned long fixed_accuracy)
  55. {
  56. struct clk_fixed_rate *fixed;
  57. struct clk_hw *hw;
  58. struct clk_init_data init;
  59. int ret;
  60. /* allocate fixed-rate clock */
  61. fixed = kzalloc(sizeof(*fixed), GFP_KERNEL);
  62. if (!fixed)
  63. return ERR_PTR(-ENOMEM);
  64. init.name = name;
  65. init.ops = &clk_fixed_rate_ops;
  66. init.flags = flags | CLK_IS_BASIC;
  67. init.parent_names = (parent_name ? &parent_name: NULL);
  68. init.num_parents = (parent_name ? 1 : 0);
  69. /* struct clk_fixed_rate assignments */
  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. ret = clk_hw_register(dev, hw);
  76. if (ret) {
  77. kfree(fixed);
  78. hw = ERR_PTR(ret);
  79. }
  80. return hw;
  81. }
  82. EXPORT_SYMBOL_GPL(clk_hw_register_fixed_rate_with_accuracy);
  83. struct clk *clk_register_fixed_rate_with_accuracy(struct device *dev,
  84. const char *name, const char *parent_name, unsigned long flags,
  85. unsigned long fixed_rate, unsigned long fixed_accuracy)
  86. {
  87. struct clk_hw *hw;
  88. hw = clk_hw_register_fixed_rate_with_accuracy(dev, name, parent_name,
  89. flags, fixed_rate, fixed_accuracy);
  90. if (IS_ERR(hw))
  91. return ERR_CAST(hw);
  92. return hw->clk;
  93. }
  94. EXPORT_SYMBOL_GPL(clk_register_fixed_rate_with_accuracy);
  95. /**
  96. * clk_hw_register_fixed_rate - register fixed-rate clock with the clock
  97. * framework
  98. * @dev: device that is registering this clock
  99. * @name: name of this clock
  100. * @parent_name: name of clock's parent
  101. * @flags: framework-specific flags
  102. * @fixed_rate: non-adjustable clock rate
  103. */
  104. struct clk_hw *clk_hw_register_fixed_rate(struct device *dev, const char *name,
  105. const char *parent_name, unsigned long flags,
  106. unsigned long fixed_rate)
  107. {
  108. return clk_hw_register_fixed_rate_with_accuracy(dev, name, parent_name,
  109. flags, fixed_rate, 0);
  110. }
  111. EXPORT_SYMBOL_GPL(clk_hw_register_fixed_rate);
  112. struct clk *clk_register_fixed_rate(struct device *dev, const char *name,
  113. const char *parent_name, unsigned long flags,
  114. unsigned long fixed_rate)
  115. {
  116. return clk_register_fixed_rate_with_accuracy(dev, name, parent_name,
  117. flags, fixed_rate, 0);
  118. }
  119. EXPORT_SYMBOL_GPL(clk_register_fixed_rate);
  120. void clk_unregister_fixed_rate(struct clk *clk)
  121. {
  122. struct clk_hw *hw;
  123. hw = __clk_get_hw(clk);
  124. if (!hw)
  125. return;
  126. clk_unregister(clk);
  127. kfree(to_clk_fixed_rate(hw));
  128. }
  129. EXPORT_SYMBOL_GPL(clk_unregister_fixed_rate);
  130. void clk_hw_unregister_fixed_rate(struct clk_hw *hw)
  131. {
  132. struct clk_fixed_rate *fixed;
  133. fixed = to_clk_fixed_rate(hw);
  134. clk_hw_unregister(hw);
  135. kfree(fixed);
  136. }
  137. EXPORT_SYMBOL_GPL(clk_hw_unregister_fixed_rate);
  138. #ifdef CONFIG_OF
  139. static struct clk *_of_fixed_clk_setup(struct device_node *node)
  140. {
  141. struct clk *clk;
  142. const char *clk_name = node->name;
  143. u32 rate;
  144. u32 accuracy = 0;
  145. int ret;
  146. if (of_property_read_u32(node, "clock-frequency", &rate))
  147. return ERR_PTR(-EIO);
  148. of_property_read_u32(node, "clock-accuracy", &accuracy);
  149. of_property_read_string(node, "clock-output-names", &clk_name);
  150. clk = clk_register_fixed_rate_with_accuracy(NULL, clk_name, NULL,
  151. 0, rate, accuracy);
  152. if (IS_ERR(clk))
  153. return clk;
  154. ret = of_clk_add_provider(node, of_clk_src_simple_get, clk);
  155. if (ret) {
  156. clk_unregister(clk);
  157. return ERR_PTR(ret);
  158. }
  159. return clk;
  160. }
  161. /**
  162. * of_fixed_clk_setup() - Setup function for simple fixed rate clock
  163. */
  164. void __init of_fixed_clk_setup(struct device_node *node)
  165. {
  166. _of_fixed_clk_setup(node);
  167. }
  168. CLK_OF_DECLARE(fixed_clk, "fixed-clock", of_fixed_clk_setup);
  169. static int of_fixed_clk_remove(struct platform_device *pdev)
  170. {
  171. struct clk *clk = platform_get_drvdata(pdev);
  172. clk_unregister_fixed_rate(clk);
  173. return 0;
  174. }
  175. static int of_fixed_clk_probe(struct platform_device *pdev)
  176. {
  177. struct clk *clk;
  178. /*
  179. * This function is not executed when of_fixed_clk_setup
  180. * succeeded.
  181. */
  182. clk = _of_fixed_clk_setup(pdev->dev.of_node);
  183. if (IS_ERR(clk))
  184. return PTR_ERR(clk);
  185. platform_set_drvdata(pdev, clk);
  186. return 0;
  187. }
  188. static const struct of_device_id of_fixed_clk_ids[] = {
  189. { .compatible = "fixed-clock" },
  190. { }
  191. };
  192. MODULE_DEVICE_TABLE(of, of_fixed_clk_ids);
  193. static struct platform_driver of_fixed_clk_driver = {
  194. .driver = {
  195. .name = "of_fixed_clk",
  196. .of_match_table = of_fixed_clk_ids,
  197. },
  198. .probe = of_fixed_clk_probe,
  199. .remove = of_fixed_clk_remove,
  200. };
  201. builtin_platform_driver(of_fixed_clk_driver);
  202. #endif