clk-composite.c 7.8 KB

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