clk-composite.c 7.8 KB

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