clk-composite.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * Copyright (c) 2013 NVIDIA CORPORATION. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <linux/clk.h>
  17. #include <linux/clk-provider.h>
  18. #include <linux/err.h>
  19. #include <linux/slab.h>
  20. #define to_clk_composite(_hw) container_of(_hw, struct clk_composite, hw)
  21. static u8 clk_composite_get_parent(struct clk_hw *hw)
  22. {
  23. struct clk_composite *composite = to_clk_composite(hw);
  24. const struct clk_ops *mux_ops = composite->mux_ops;
  25. struct clk_hw *mux_hw = composite->mux_hw;
  26. mux_hw->clk = hw->clk;
  27. return mux_ops->get_parent(mux_hw);
  28. }
  29. static int clk_composite_set_parent(struct clk_hw *hw, u8 index)
  30. {
  31. struct clk_composite *composite = to_clk_composite(hw);
  32. const struct clk_ops *mux_ops = composite->mux_ops;
  33. struct clk_hw *mux_hw = composite->mux_hw;
  34. mux_hw->clk = hw->clk;
  35. return mux_ops->set_parent(mux_hw, index);
  36. }
  37. static unsigned long clk_composite_recalc_rate(struct clk_hw *hw,
  38. unsigned long parent_rate)
  39. {
  40. struct clk_composite *composite = to_clk_composite(hw);
  41. const struct clk_ops *rate_ops = composite->rate_ops;
  42. struct clk_hw *rate_hw = composite->rate_hw;
  43. rate_hw->clk = hw->clk;
  44. return rate_ops->recalc_rate(rate_hw, parent_rate);
  45. }
  46. static long clk_composite_determine_rate(struct clk_hw *hw, unsigned long rate,
  47. unsigned long *best_parent_rate,
  48. struct clk **best_parent_p)
  49. {
  50. struct clk_composite *composite = to_clk_composite(hw);
  51. const struct clk_ops *rate_ops = composite->rate_ops;
  52. const struct clk_ops *mux_ops = composite->mux_ops;
  53. struct clk_hw *rate_hw = composite->rate_hw;
  54. struct clk_hw *mux_hw = composite->mux_hw;
  55. if (rate_hw && rate_ops && rate_ops->determine_rate) {
  56. rate_hw->clk = hw->clk;
  57. return rate_ops->determine_rate(rate_hw, rate, best_parent_rate,
  58. best_parent_p);
  59. } else if (mux_hw && mux_ops && mux_ops->determine_rate) {
  60. mux_hw->clk = hw->clk;
  61. return mux_ops->determine_rate(mux_hw, rate, best_parent_rate,
  62. best_parent_p);
  63. } else {
  64. pr_err("clk: clk_composite_determine_rate function called, but no mux or rate callback set!\n");
  65. return 0;
  66. }
  67. }
  68. static long clk_composite_round_rate(struct clk_hw *hw, unsigned long rate,
  69. unsigned long *prate)
  70. {
  71. struct clk_composite *composite = to_clk_composite(hw);
  72. const struct clk_ops *rate_ops = composite->rate_ops;
  73. struct clk_hw *rate_hw = composite->rate_hw;
  74. rate_hw->clk = hw->clk;
  75. return rate_ops->round_rate(rate_hw, rate, prate);
  76. }
  77. static int clk_composite_set_rate(struct clk_hw *hw, unsigned long rate,
  78. unsigned long parent_rate)
  79. {
  80. struct clk_composite *composite = to_clk_composite(hw);
  81. const struct clk_ops *rate_ops = composite->rate_ops;
  82. struct clk_hw *rate_hw = composite->rate_hw;
  83. rate_hw->clk = hw->clk;
  84. return rate_ops->set_rate(rate_hw, rate, parent_rate);
  85. }
  86. static int clk_composite_is_enabled(struct clk_hw *hw)
  87. {
  88. struct clk_composite *composite = to_clk_composite(hw);
  89. const struct clk_ops *gate_ops = composite->gate_ops;
  90. struct clk_hw *gate_hw = composite->gate_hw;
  91. gate_hw->clk = hw->clk;
  92. return gate_ops->is_enabled(gate_hw);
  93. }
  94. static int clk_composite_enable(struct clk_hw *hw)
  95. {
  96. struct clk_composite *composite = to_clk_composite(hw);
  97. const struct clk_ops *gate_ops = composite->gate_ops;
  98. struct clk_hw *gate_hw = composite->gate_hw;
  99. gate_hw->clk = hw->clk;
  100. return gate_ops->enable(gate_hw);
  101. }
  102. static void clk_composite_disable(struct clk_hw *hw)
  103. {
  104. struct clk_composite *composite = to_clk_composite(hw);
  105. const struct clk_ops *gate_ops = composite->gate_ops;
  106. struct clk_hw *gate_hw = composite->gate_hw;
  107. gate_hw->clk = hw->clk;
  108. gate_ops->disable(gate_hw);
  109. }
  110. struct clk *clk_register_composite(struct device *dev, const char *name,
  111. const char **parent_names, int num_parents,
  112. struct clk_hw *mux_hw, const struct clk_ops *mux_ops,
  113. struct clk_hw *rate_hw, const struct clk_ops *rate_ops,
  114. struct clk_hw *gate_hw, const struct clk_ops *gate_ops,
  115. unsigned long flags)
  116. {
  117. struct clk *clk;
  118. struct clk_init_data init;
  119. struct clk_composite *composite;
  120. struct clk_ops *clk_composite_ops;
  121. composite = kzalloc(sizeof(*composite), GFP_KERNEL);
  122. if (!composite) {
  123. pr_err("%s: could not allocate composite clk\n", __func__);
  124. return ERR_PTR(-ENOMEM);
  125. }
  126. init.name = name;
  127. init.flags = flags | CLK_IS_BASIC;
  128. init.parent_names = parent_names;
  129. init.num_parents = num_parents;
  130. clk_composite_ops = &composite->ops;
  131. if (mux_hw && mux_ops) {
  132. if (!mux_ops->get_parent || !mux_ops->set_parent) {
  133. clk = ERR_PTR(-EINVAL);
  134. goto err;
  135. }
  136. composite->mux_hw = mux_hw;
  137. composite->mux_ops = mux_ops;
  138. clk_composite_ops->get_parent = clk_composite_get_parent;
  139. clk_composite_ops->set_parent = clk_composite_set_parent;
  140. if (mux_ops->determine_rate)
  141. clk_composite_ops->determine_rate = clk_composite_determine_rate;
  142. }
  143. if (rate_hw && rate_ops) {
  144. if (!rate_ops->recalc_rate) {
  145. clk = ERR_PTR(-EINVAL);
  146. goto err;
  147. }
  148. /* .round_rate is a prerequisite for .set_rate */
  149. if (rate_ops->round_rate) {
  150. clk_composite_ops->round_rate = clk_composite_round_rate;
  151. if (rate_ops->set_rate) {
  152. clk_composite_ops->set_rate = clk_composite_set_rate;
  153. }
  154. } else {
  155. WARN(rate_ops->set_rate,
  156. "%s: missing round_rate op is required\n",
  157. __func__);
  158. }
  159. composite->rate_hw = rate_hw;
  160. composite->rate_ops = rate_ops;
  161. clk_composite_ops->recalc_rate = clk_composite_recalc_rate;
  162. if (rate_ops->determine_rate)
  163. clk_composite_ops->determine_rate = clk_composite_determine_rate;
  164. }
  165. if (gate_hw && gate_ops) {
  166. if (!gate_ops->is_enabled || !gate_ops->enable ||
  167. !gate_ops->disable) {
  168. clk = ERR_PTR(-EINVAL);
  169. goto err;
  170. }
  171. composite->gate_hw = gate_hw;
  172. composite->gate_ops = gate_ops;
  173. clk_composite_ops->is_enabled = clk_composite_is_enabled;
  174. clk_composite_ops->enable = clk_composite_enable;
  175. clk_composite_ops->disable = clk_composite_disable;
  176. }
  177. init.ops = clk_composite_ops;
  178. composite->hw.init = &init;
  179. clk = clk_register(dev, &composite->hw);
  180. if (IS_ERR(clk))
  181. goto err;
  182. if (composite->mux_hw)
  183. composite->mux_hw->clk = clk;
  184. if (composite->rate_hw)
  185. composite->rate_hw->clk = clk;
  186. if (composite->gate_hw)
  187. composite->gate_hw->clk = clk;
  188. return clk;
  189. err:
  190. kfree(composite);
  191. return clk;
  192. }