clk-composite.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. __clk_hw_set_clk(mux_hw, hw);
  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. __clk_hw_set_clk(mux_hw, hw);
  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. __clk_hw_set_clk(rate_hw, hw);
  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 min_rate,
  48. unsigned long max_rate,
  49. unsigned long *best_parent_rate,
  50. struct clk_hw **best_parent_p)
  51. {
  52. struct clk_composite *composite = to_clk_composite(hw);
  53. const struct clk_ops *rate_ops = composite->rate_ops;
  54. const struct clk_ops *mux_ops = composite->mux_ops;
  55. struct clk_hw *rate_hw = composite->rate_hw;
  56. struct clk_hw *mux_hw = composite->mux_hw;
  57. struct clk *parent;
  58. unsigned long parent_rate;
  59. long tmp_rate, best_rate = 0;
  60. unsigned long rate_diff;
  61. unsigned long best_rate_diff = ULONG_MAX;
  62. int i;
  63. if (rate_hw && rate_ops && rate_ops->determine_rate) {
  64. __clk_hw_set_clk(rate_hw, hw);
  65. return rate_ops->determine_rate(rate_hw, rate, min_rate,
  66. max_rate,
  67. best_parent_rate,
  68. best_parent_p);
  69. } else if (rate_hw && rate_ops && rate_ops->round_rate &&
  70. mux_hw && mux_ops && mux_ops->set_parent) {
  71. *best_parent_p = NULL;
  72. if (__clk_get_flags(hw->clk) & CLK_SET_RATE_NO_REPARENT) {
  73. parent = clk_get_parent(mux_hw->clk);
  74. *best_parent_p = __clk_get_hw(parent);
  75. *best_parent_rate = __clk_get_rate(parent);
  76. return rate_ops->round_rate(rate_hw, rate,
  77. best_parent_rate);
  78. }
  79. for (i = 0; i < __clk_get_num_parents(mux_hw->clk); i++) {
  80. parent = clk_get_parent_by_index(mux_hw->clk, i);
  81. if (!parent)
  82. continue;
  83. parent_rate = __clk_get_rate(parent);
  84. tmp_rate = rate_ops->round_rate(rate_hw, rate,
  85. &parent_rate);
  86. if (tmp_rate < 0)
  87. continue;
  88. rate_diff = abs(rate - tmp_rate);
  89. if (!rate_diff || !*best_parent_p
  90. || best_rate_diff > rate_diff) {
  91. *best_parent_p = __clk_get_hw(parent);
  92. *best_parent_rate = parent_rate;
  93. best_rate_diff = rate_diff;
  94. best_rate = tmp_rate;
  95. }
  96. if (!rate_diff)
  97. return rate;
  98. }
  99. return best_rate;
  100. } else if (mux_hw && mux_ops && mux_ops->determine_rate) {
  101. __clk_hw_set_clk(mux_hw, hw);
  102. return mux_ops->determine_rate(mux_hw, rate, min_rate,
  103. max_rate, best_parent_rate,
  104. best_parent_p);
  105. } else {
  106. pr_err("clk: clk_composite_determine_rate function called, but no mux or rate callback set!\n");
  107. return 0;
  108. }
  109. }
  110. static long clk_composite_round_rate(struct clk_hw *hw, unsigned long rate,
  111. unsigned long *prate)
  112. {
  113. struct clk_composite *composite = to_clk_composite(hw);
  114. const struct clk_ops *rate_ops = composite->rate_ops;
  115. struct clk_hw *rate_hw = composite->rate_hw;
  116. __clk_hw_set_clk(rate_hw, hw);
  117. return rate_ops->round_rate(rate_hw, rate, prate);
  118. }
  119. static int clk_composite_set_rate(struct clk_hw *hw, unsigned long rate,
  120. unsigned long parent_rate)
  121. {
  122. struct clk_composite *composite = to_clk_composite(hw);
  123. const struct clk_ops *rate_ops = composite->rate_ops;
  124. struct clk_hw *rate_hw = composite->rate_hw;
  125. __clk_hw_set_clk(rate_hw, hw);
  126. return rate_ops->set_rate(rate_hw, rate, parent_rate);
  127. }
  128. static int clk_composite_is_enabled(struct clk_hw *hw)
  129. {
  130. struct clk_composite *composite = to_clk_composite(hw);
  131. const struct clk_ops *gate_ops = composite->gate_ops;
  132. struct clk_hw *gate_hw = composite->gate_hw;
  133. __clk_hw_set_clk(gate_hw, hw);
  134. return gate_ops->is_enabled(gate_hw);
  135. }
  136. static int clk_composite_enable(struct clk_hw *hw)
  137. {
  138. struct clk_composite *composite = to_clk_composite(hw);
  139. const struct clk_ops *gate_ops = composite->gate_ops;
  140. struct clk_hw *gate_hw = composite->gate_hw;
  141. __clk_hw_set_clk(gate_hw, hw);
  142. return gate_ops->enable(gate_hw);
  143. }
  144. static void clk_composite_disable(struct clk_hw *hw)
  145. {
  146. struct clk_composite *composite = to_clk_composite(hw);
  147. const struct clk_ops *gate_ops = composite->gate_ops;
  148. struct clk_hw *gate_hw = composite->gate_hw;
  149. __clk_hw_set_clk(gate_hw, hw);
  150. gate_ops->disable(gate_hw);
  151. }
  152. struct clk *clk_register_composite(struct device *dev, const char *name,
  153. const char * const *parent_names, int num_parents,
  154. struct clk_hw *mux_hw, const struct clk_ops *mux_ops,
  155. struct clk_hw *rate_hw, const struct clk_ops *rate_ops,
  156. struct clk_hw *gate_hw, const struct clk_ops *gate_ops,
  157. unsigned long flags)
  158. {
  159. struct clk *clk;
  160. struct clk_init_data init;
  161. struct clk_composite *composite;
  162. struct clk_ops *clk_composite_ops;
  163. composite = kzalloc(sizeof(*composite), GFP_KERNEL);
  164. if (!composite)
  165. return ERR_PTR(-ENOMEM);
  166. init.name = name;
  167. init.flags = flags | CLK_IS_BASIC;
  168. init.parent_names = parent_names;
  169. init.num_parents = num_parents;
  170. clk_composite_ops = &composite->ops;
  171. if (mux_hw && mux_ops) {
  172. if (!mux_ops->get_parent) {
  173. clk = ERR_PTR(-EINVAL);
  174. goto err;
  175. }
  176. composite->mux_hw = mux_hw;
  177. composite->mux_ops = mux_ops;
  178. clk_composite_ops->get_parent = clk_composite_get_parent;
  179. if (mux_ops->set_parent)
  180. clk_composite_ops->set_parent = clk_composite_set_parent;
  181. if (mux_ops->determine_rate)
  182. clk_composite_ops->determine_rate = clk_composite_determine_rate;
  183. }
  184. if (rate_hw && rate_ops) {
  185. if (!rate_ops->recalc_rate) {
  186. clk = ERR_PTR(-EINVAL);
  187. goto err;
  188. }
  189. clk_composite_ops->recalc_rate = clk_composite_recalc_rate;
  190. if (rate_ops->determine_rate)
  191. clk_composite_ops->determine_rate =
  192. clk_composite_determine_rate;
  193. else if (rate_ops->round_rate)
  194. clk_composite_ops->round_rate =
  195. clk_composite_round_rate;
  196. /* .set_rate requires either .round_rate or .determine_rate */
  197. if (rate_ops->set_rate) {
  198. if (rate_ops->determine_rate || rate_ops->round_rate)
  199. clk_composite_ops->set_rate =
  200. clk_composite_set_rate;
  201. else
  202. WARN(1, "%s: missing round_rate op is required\n",
  203. __func__);
  204. }
  205. composite->rate_hw = rate_hw;
  206. composite->rate_ops = rate_ops;
  207. }
  208. if (gate_hw && gate_ops) {
  209. if (!gate_ops->is_enabled || !gate_ops->enable ||
  210. !gate_ops->disable) {
  211. clk = ERR_PTR(-EINVAL);
  212. goto err;
  213. }
  214. composite->gate_hw = gate_hw;
  215. composite->gate_ops = gate_ops;
  216. clk_composite_ops->is_enabled = clk_composite_is_enabled;
  217. clk_composite_ops->enable = clk_composite_enable;
  218. clk_composite_ops->disable = clk_composite_disable;
  219. }
  220. init.ops = clk_composite_ops;
  221. composite->hw.init = &init;
  222. clk = clk_register(dev, &composite->hw);
  223. if (IS_ERR(clk))
  224. goto err;
  225. if (composite->mux_hw)
  226. composite->mux_hw->clk = clk;
  227. if (composite->rate_hw)
  228. composite->rate_hw->clk = clk;
  229. if (composite->gate_hw)
  230. composite->gate_hw->clk = clk;
  231. return clk;
  232. err:
  233. kfree(composite);
  234. return clk;
  235. }