clk-fixed-rate.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. /*
  18. * DOC: basic fixed-rate clock that cannot gate
  19. *
  20. * Traits of this clock:
  21. * prepare - clk_(un)prepare only ensures parents are prepared
  22. * enable - clk_enable only ensures parents are enabled
  23. * rate - rate is always a fixed value. No clk_set_rate support
  24. * parent - fixed parent. No clk_set_parent support
  25. */
  26. static unsigned long clk_fixed_rate_recalc_rate(struct clk_hw *hw,
  27. unsigned long parent_rate)
  28. {
  29. return to_clk_fixed_rate(hw)->fixed_rate;
  30. }
  31. static unsigned long clk_fixed_rate_recalc_accuracy(struct clk_hw *hw,
  32. unsigned long parent_accuracy)
  33. {
  34. return to_clk_fixed_rate(hw)->fixed_accuracy;
  35. }
  36. const struct clk_ops clk_fixed_rate_ops = {
  37. .recalc_rate = clk_fixed_rate_recalc_rate,
  38. .recalc_accuracy = clk_fixed_rate_recalc_accuracy,
  39. };
  40. EXPORT_SYMBOL_GPL(clk_fixed_rate_ops);
  41. /**
  42. * clk_hw_register_fixed_rate_with_accuracy - register fixed-rate clock with
  43. * the clock framework
  44. * @dev: device that is registering this clock
  45. * @name: name of this clock
  46. * @parent_name: name of clock's parent
  47. * @flags: framework-specific flags
  48. * @fixed_rate: non-adjustable clock rate
  49. * @fixed_accuracy: non-adjustable clock rate
  50. */
  51. struct clk_hw *clk_hw_register_fixed_rate_with_accuracy(struct device *dev,
  52. const char *name, const char *parent_name, unsigned long flags,
  53. unsigned long fixed_rate, unsigned long fixed_accuracy)
  54. {
  55. struct clk_fixed_rate *fixed;
  56. struct clk_hw *hw;
  57. struct clk_init_data init;
  58. int ret;
  59. /* allocate fixed-rate clock */
  60. fixed = kzalloc(sizeof(*fixed), GFP_KERNEL);
  61. if (!fixed)
  62. return ERR_PTR(-ENOMEM);
  63. init.name = name;
  64. init.ops = &clk_fixed_rate_ops;
  65. init.flags = flags | CLK_IS_BASIC;
  66. init.parent_names = (parent_name ? &parent_name: NULL);
  67. init.num_parents = (parent_name ? 1 : 0);
  68. /* struct clk_fixed_rate assignments */
  69. fixed->fixed_rate = fixed_rate;
  70. fixed->fixed_accuracy = fixed_accuracy;
  71. fixed->hw.init = &init;
  72. /* register the clock */
  73. hw = &fixed->hw;
  74. ret = clk_hw_register(dev, hw);
  75. if (ret) {
  76. kfree(fixed);
  77. hw = ERR_PTR(ret);
  78. }
  79. return hw;
  80. }
  81. EXPORT_SYMBOL_GPL(clk_hw_register_fixed_rate_with_accuracy);
  82. struct clk *clk_register_fixed_rate_with_accuracy(struct device *dev,
  83. const char *name, const char *parent_name, unsigned long flags,
  84. unsigned long fixed_rate, unsigned long fixed_accuracy)
  85. {
  86. struct clk_hw *hw;
  87. hw = clk_hw_register_fixed_rate_with_accuracy(dev, name, parent_name,
  88. flags, fixed_rate, fixed_accuracy);
  89. if (IS_ERR(hw))
  90. return ERR_CAST(hw);
  91. return hw->clk;
  92. }
  93. EXPORT_SYMBOL_GPL(clk_register_fixed_rate_with_accuracy);
  94. /**
  95. * clk_hw_register_fixed_rate - register fixed-rate clock with the clock
  96. * framework
  97. * @dev: device that is registering this clock
  98. * @name: name of this clock
  99. * @parent_name: name of clock's parent
  100. * @flags: framework-specific flags
  101. * @fixed_rate: non-adjustable clock rate
  102. */
  103. struct clk_hw *clk_hw_register_fixed_rate(struct device *dev, const char *name,
  104. const char *parent_name, unsigned long flags,
  105. unsigned long fixed_rate)
  106. {
  107. return clk_hw_register_fixed_rate_with_accuracy(dev, name, parent_name,
  108. flags, fixed_rate, 0);
  109. }
  110. EXPORT_SYMBOL_GPL(clk_hw_register_fixed_rate);
  111. struct clk *clk_register_fixed_rate(struct device *dev, const char *name,
  112. const char *parent_name, unsigned long flags,
  113. unsigned long fixed_rate)
  114. {
  115. return clk_register_fixed_rate_with_accuracy(dev, name, parent_name,
  116. flags, fixed_rate, 0);
  117. }
  118. EXPORT_SYMBOL_GPL(clk_register_fixed_rate);
  119. void clk_unregister_fixed_rate(struct clk *clk)
  120. {
  121. struct clk_hw *hw;
  122. hw = __clk_get_hw(clk);
  123. if (!hw)
  124. return;
  125. clk_unregister(clk);
  126. kfree(to_clk_fixed_rate(hw));
  127. }
  128. EXPORT_SYMBOL_GPL(clk_unregister_fixed_rate);
  129. void clk_hw_unregister_fixed_rate(struct clk_hw *hw)
  130. {
  131. struct clk_fixed_rate *fixed;
  132. fixed = to_clk_fixed_rate(hw);
  133. clk_hw_unregister(hw);
  134. kfree(fixed);
  135. }
  136. EXPORT_SYMBOL_GPL(clk_hw_unregister_fixed_rate);
  137. #ifdef CONFIG_OF
  138. /**
  139. * of_fixed_clk_setup() - Setup function for simple fixed rate clock
  140. */
  141. void of_fixed_clk_setup(struct device_node *node)
  142. {
  143. struct clk *clk;
  144. const char *clk_name = node->name;
  145. u32 rate;
  146. u32 accuracy = 0;
  147. if (of_property_read_u32(node, "clock-frequency", &rate))
  148. return;
  149. of_property_read_u32(node, "clock-accuracy", &accuracy);
  150. of_property_read_string(node, "clock-output-names", &clk_name);
  151. clk = clk_register_fixed_rate_with_accuracy(NULL, clk_name, NULL,
  152. 0, rate, accuracy);
  153. if (!IS_ERR(clk))
  154. of_clk_add_provider(node, of_clk_src_simple_get, clk);
  155. }
  156. EXPORT_SYMBOL_GPL(of_fixed_clk_setup);
  157. CLK_OF_DECLARE(fixed_clk, "fixed-clock", of_fixed_clk_setup);
  158. #endif