clk-composite.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. static u8 clk_composite_get_parent(struct clk_hw *hw)
  21. {
  22. struct clk_composite *composite = to_clk_composite(hw);
  23. const struct clk_ops *mux_ops = composite->mux_ops;
  24. struct clk_hw *mux_hw = composite->mux_hw;
  25. __clk_hw_set_clk(mux_hw, hw);
  26. return mux_ops->get_parent(mux_hw);
  27. }
  28. static int clk_composite_set_parent(struct clk_hw *hw, u8 index)
  29. {
  30. struct clk_composite *composite = to_clk_composite(hw);
  31. const struct clk_ops *mux_ops = composite->mux_ops;
  32. struct clk_hw *mux_hw = composite->mux_hw;
  33. __clk_hw_set_clk(mux_hw, hw);
  34. return mux_ops->set_parent(mux_hw, index);
  35. }
  36. static unsigned long clk_composite_recalc_rate(struct clk_hw *hw,
  37. unsigned long parent_rate)
  38. {
  39. struct clk_composite *composite = to_clk_composite(hw);
  40. const struct clk_ops *rate_ops = composite->rate_ops;
  41. struct clk_hw *rate_hw = composite->rate_hw;
  42. __clk_hw_set_clk(rate_hw, hw);
  43. return rate_ops->recalc_rate(rate_hw, parent_rate);
  44. }
  45. static int clk_composite_determine_rate(struct clk_hw *hw,
  46. struct clk_rate_request *req)
  47. {
  48. struct clk_composite *composite = to_clk_composite(hw);
  49. const struct clk_ops *rate_ops = composite->rate_ops;
  50. const struct clk_ops *mux_ops = composite->mux_ops;
  51. struct clk_hw *rate_hw = composite->rate_hw;
  52. struct clk_hw *mux_hw = composite->mux_hw;
  53. struct clk_hw *parent;
  54. unsigned long parent_rate;
  55. long tmp_rate, best_rate = 0;
  56. unsigned long rate_diff;
  57. unsigned long best_rate_diff = ULONG_MAX;
  58. long rate;
  59. int i;
  60. if (rate_hw && rate_ops && rate_ops->determine_rate) {
  61. __clk_hw_set_clk(rate_hw, hw);
  62. return rate_ops->determine_rate(rate_hw, req);
  63. } else if (rate_hw && rate_ops && rate_ops->round_rate &&
  64. mux_hw && mux_ops && mux_ops->set_parent) {
  65. req->best_parent_hw = NULL;
  66. if (clk_hw_get_flags(hw) & CLK_SET_RATE_NO_REPARENT) {
  67. parent = clk_hw_get_parent(mux_hw);
  68. req->best_parent_hw = parent;
  69. req->best_parent_rate = clk_hw_get_rate(parent);
  70. rate = rate_ops->round_rate(rate_hw, req->rate,
  71. &req->best_parent_rate);
  72. if (rate < 0)
  73. return rate;
  74. req->rate = rate;
  75. return 0;
  76. }
  77. for (i = 0; i < clk_hw_get_num_parents(mux_hw); i++) {
  78. parent = clk_hw_get_parent_by_index(mux_hw, i);
  79. if (!parent)
  80. continue;
  81. parent_rate = clk_hw_get_rate(parent);
  82. tmp_rate = rate_ops->round_rate(rate_hw, req->rate,
  83. &parent_rate);
  84. if (tmp_rate < 0)
  85. continue;
  86. rate_diff = abs(req->rate - tmp_rate);
  87. if (!rate_diff || !req->best_parent_hw
  88. || best_rate_diff > rate_diff) {
  89. req->best_parent_hw = parent;
  90. req->best_parent_rate = parent_rate;
  91. best_rate_diff = rate_diff;
  92. best_rate = tmp_rate;
  93. }
  94. if (!rate_diff)
  95. return 0;
  96. }
  97. req->rate = best_rate;
  98. return 0;
  99. } else if (mux_hw && mux_ops && mux_ops->determine_rate) {
  100. __clk_hw_set_clk(mux_hw, hw);
  101. return mux_ops->determine_rate(mux_hw, req);
  102. } else {
  103. pr_err("clk: clk_composite_determine_rate function called, but no mux or rate callback set!\n");
  104. return -EINVAL;
  105. }
  106. }
  107. static long clk_composite_round_rate(struct clk_hw *hw, unsigned long rate,
  108. unsigned long *prate)
  109. {
  110. struct clk_composite *composite = to_clk_composite(hw);
  111. const struct clk_ops *rate_ops = composite->rate_ops;
  112. struct clk_hw *rate_hw = composite->rate_hw;
  113. __clk_hw_set_clk(rate_hw, hw);
  114. return rate_ops->round_rate(rate_hw, rate, prate);
  115. }
  116. static int clk_composite_set_rate(struct clk_hw *hw, unsigned long rate,
  117. unsigned long parent_rate)
  118. {
  119. struct clk_composite *composite = to_clk_composite(hw);
  120. const struct clk_ops *rate_ops = composite->rate_ops;
  121. struct clk_hw *rate_hw = composite->rate_hw;
  122. __clk_hw_set_clk(rate_hw, hw);
  123. return rate_ops->set_rate(rate_hw, rate, parent_rate);
  124. }
  125. static int clk_composite_set_rate_and_parent(struct clk_hw *hw,
  126. unsigned long rate,
  127. unsigned long parent_rate,
  128. u8 index)
  129. {
  130. struct clk_composite *composite = to_clk_composite(hw);
  131. const struct clk_ops *rate_ops = composite->rate_ops;
  132. const struct clk_ops *mux_ops = composite->mux_ops;
  133. struct clk_hw *rate_hw = composite->rate_hw;
  134. struct clk_hw *mux_hw = composite->mux_hw;
  135. unsigned long temp_rate;
  136. __clk_hw_set_clk(rate_hw, hw);
  137. __clk_hw_set_clk(mux_hw, hw);
  138. temp_rate = rate_ops->recalc_rate(rate_hw, parent_rate);
  139. if (temp_rate > rate) {
  140. rate_ops->set_rate(rate_hw, rate, parent_rate);
  141. mux_ops->set_parent(mux_hw, index);
  142. } else {
  143. mux_ops->set_parent(mux_hw, index);
  144. rate_ops->set_rate(rate_hw, rate, parent_rate);
  145. }
  146. return 0;
  147. }
  148. static int clk_composite_is_enabled(struct clk_hw *hw)
  149. {
  150. struct clk_composite *composite = to_clk_composite(hw);
  151. const struct clk_ops *gate_ops = composite->gate_ops;
  152. struct clk_hw *gate_hw = composite->gate_hw;
  153. __clk_hw_set_clk(gate_hw, hw);
  154. return gate_ops->is_enabled(gate_hw);
  155. }
  156. static int clk_composite_enable(struct clk_hw *hw)
  157. {
  158. struct clk_composite *composite = to_clk_composite(hw);
  159. const struct clk_ops *gate_ops = composite->gate_ops;
  160. struct clk_hw *gate_hw = composite->gate_hw;
  161. __clk_hw_set_clk(gate_hw, hw);
  162. return gate_ops->enable(gate_hw);
  163. }
  164. static void clk_composite_disable(struct clk_hw *hw)
  165. {
  166. struct clk_composite *composite = to_clk_composite(hw);
  167. const struct clk_ops *gate_ops = composite->gate_ops;
  168. struct clk_hw *gate_hw = composite->gate_hw;
  169. __clk_hw_set_clk(gate_hw, hw);
  170. gate_ops->disable(gate_hw);
  171. }
  172. struct clk_hw *clk_hw_register_composite(struct device *dev, const char *name,
  173. const char * const *parent_names, int num_parents,
  174. struct clk_hw *mux_hw, const struct clk_ops *mux_ops,
  175. struct clk_hw *rate_hw, const struct clk_ops *rate_ops,
  176. struct clk_hw *gate_hw, const struct clk_ops *gate_ops,
  177. unsigned long flags)
  178. {
  179. struct clk_hw *hw;
  180. struct clk_init_data init;
  181. struct clk_composite *composite;
  182. struct clk_ops *clk_composite_ops;
  183. int ret;
  184. composite = kzalloc(sizeof(*composite), GFP_KERNEL);
  185. if (!composite)
  186. return ERR_PTR(-ENOMEM);
  187. init.name = name;
  188. init.flags = flags | CLK_IS_BASIC;
  189. init.parent_names = parent_names;
  190. init.num_parents = num_parents;
  191. hw = &composite->hw;
  192. clk_composite_ops = &composite->ops;
  193. if (mux_hw && mux_ops) {
  194. if (!mux_ops->get_parent) {
  195. hw = ERR_PTR(-EINVAL);
  196. goto err;
  197. }
  198. composite->mux_hw = mux_hw;
  199. composite->mux_ops = mux_ops;
  200. clk_composite_ops->get_parent = clk_composite_get_parent;
  201. if (mux_ops->set_parent)
  202. clk_composite_ops->set_parent = clk_composite_set_parent;
  203. if (mux_ops->determine_rate)
  204. clk_composite_ops->determine_rate = clk_composite_determine_rate;
  205. }
  206. if (rate_hw && rate_ops) {
  207. if (!rate_ops->recalc_rate) {
  208. hw = ERR_PTR(-EINVAL);
  209. goto err;
  210. }
  211. clk_composite_ops->recalc_rate = clk_composite_recalc_rate;
  212. if (rate_ops->determine_rate)
  213. clk_composite_ops->determine_rate =
  214. clk_composite_determine_rate;
  215. else if (rate_ops->round_rate)
  216. clk_composite_ops->round_rate =
  217. clk_composite_round_rate;
  218. /* .set_rate requires either .round_rate or .determine_rate */
  219. if (rate_ops->set_rate) {
  220. if (rate_ops->determine_rate || rate_ops->round_rate)
  221. clk_composite_ops->set_rate =
  222. clk_composite_set_rate;
  223. else
  224. WARN(1, "%s: missing round_rate op is required\n",
  225. __func__);
  226. }
  227. composite->rate_hw = rate_hw;
  228. composite->rate_ops = rate_ops;
  229. }
  230. if (mux_hw && mux_ops && rate_hw && rate_ops) {
  231. if (mux_ops->set_parent && rate_ops->set_rate)
  232. clk_composite_ops->set_rate_and_parent =
  233. clk_composite_set_rate_and_parent;
  234. }
  235. if (gate_hw && gate_ops) {
  236. if (!gate_ops->is_enabled || !gate_ops->enable ||
  237. !gate_ops->disable) {
  238. hw = ERR_PTR(-EINVAL);
  239. goto err;
  240. }
  241. composite->gate_hw = gate_hw;
  242. composite->gate_ops = gate_ops;
  243. clk_composite_ops->is_enabled = clk_composite_is_enabled;
  244. clk_composite_ops->enable = clk_composite_enable;
  245. clk_composite_ops->disable = clk_composite_disable;
  246. }
  247. init.ops = clk_composite_ops;
  248. composite->hw.init = &init;
  249. ret = clk_hw_register(dev, hw);
  250. if (ret) {
  251. hw = ERR_PTR(ret);
  252. goto err;
  253. }
  254. if (composite->mux_hw)
  255. composite->mux_hw->clk = hw->clk;
  256. if (composite->rate_hw)
  257. composite->rate_hw->clk = hw->clk;
  258. if (composite->gate_hw)
  259. composite->gate_hw->clk = hw->clk;
  260. return hw;
  261. err:
  262. kfree(composite);
  263. return hw;
  264. }
  265. struct clk *clk_register_composite(struct device *dev, const char *name,
  266. const char * const *parent_names, int num_parents,
  267. struct clk_hw *mux_hw, const struct clk_ops *mux_ops,
  268. struct clk_hw *rate_hw, const struct clk_ops *rate_ops,
  269. struct clk_hw *gate_hw, const struct clk_ops *gate_ops,
  270. unsigned long flags)
  271. {
  272. struct clk_hw *hw;
  273. hw = clk_hw_register_composite(dev, name, parent_names, num_parents,
  274. mux_hw, mux_ops, rate_hw, rate_ops, gate_hw, gate_ops,
  275. flags);
  276. if (IS_ERR(hw))
  277. return ERR_CAST(hw);
  278. return hw->clk;
  279. }
  280. void clk_unregister_composite(struct clk *clk)
  281. {
  282. struct clk_composite *composite;
  283. struct clk_hw *hw;
  284. hw = __clk_get_hw(clk);
  285. if (!hw)
  286. return;
  287. composite = to_clk_composite(hw);
  288. clk_unregister(clk);
  289. kfree(composite);
  290. }