composite.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. * TI composite clock support
  3. *
  4. * Copyright (C) 2013 Texas Instruments, Inc.
  5. *
  6. * Tero Kristo <t-kristo@ti.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  13. * kind, whether express or implied; without even the implied warranty
  14. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. */
  17. #include <linux/clk-provider.h>
  18. #include <linux/slab.h>
  19. #include <linux/io.h>
  20. #include <linux/of.h>
  21. #include <linux/of_address.h>
  22. #include <linux/clk/ti.h>
  23. #include <linux/list.h>
  24. #include "clock.h"
  25. #undef pr_fmt
  26. #define pr_fmt(fmt) "%s: " fmt, __func__
  27. static unsigned long ti_composite_recalc_rate(struct clk_hw *hw,
  28. unsigned long parent_rate)
  29. {
  30. return ti_clk_divider_ops.recalc_rate(hw, parent_rate);
  31. }
  32. static long ti_composite_round_rate(struct clk_hw *hw, unsigned long rate,
  33. unsigned long *prate)
  34. {
  35. return -EINVAL;
  36. }
  37. static int ti_composite_set_rate(struct clk_hw *hw, unsigned long rate,
  38. unsigned long parent_rate)
  39. {
  40. return -EINVAL;
  41. }
  42. static const struct clk_ops ti_composite_divider_ops = {
  43. .recalc_rate = &ti_composite_recalc_rate,
  44. .round_rate = &ti_composite_round_rate,
  45. .set_rate = &ti_composite_set_rate,
  46. };
  47. static const struct clk_ops ti_composite_gate_ops = {
  48. .enable = &omap2_dflt_clk_enable,
  49. .disable = &omap2_dflt_clk_disable,
  50. .is_enabled = &omap2_dflt_clk_is_enabled,
  51. };
  52. struct component_clk {
  53. int num_parents;
  54. const char **parent_names;
  55. struct device_node *node;
  56. int type;
  57. struct clk_hw *hw;
  58. struct list_head link;
  59. };
  60. static const char * const component_clk_types[] __initconst = {
  61. "gate", "divider", "mux"
  62. };
  63. static LIST_HEAD(component_clks);
  64. static struct device_node *_get_component_node(struct device_node *node, int i)
  65. {
  66. int rc;
  67. struct of_phandle_args clkspec;
  68. rc = of_parse_phandle_with_args(node, "clocks", "#clock-cells", i,
  69. &clkspec);
  70. if (rc)
  71. return NULL;
  72. return clkspec.np;
  73. }
  74. static struct component_clk *_lookup_component(struct device_node *node)
  75. {
  76. struct component_clk *comp;
  77. list_for_each_entry(comp, &component_clks, link) {
  78. if (comp->node == node)
  79. return comp;
  80. }
  81. return NULL;
  82. }
  83. struct clk_hw_omap_comp {
  84. struct clk_hw hw;
  85. struct device_node *comp_nodes[CLK_COMPONENT_TYPE_MAX];
  86. struct component_clk *comp_clks[CLK_COMPONENT_TYPE_MAX];
  87. };
  88. static inline struct clk_hw *_get_hw(struct clk_hw_omap_comp *clk, int idx)
  89. {
  90. if (!clk)
  91. return NULL;
  92. if (!clk->comp_clks[idx])
  93. return NULL;
  94. return clk->comp_clks[idx]->hw;
  95. }
  96. #define to_clk_hw_comp(_hw) container_of(_hw, struct clk_hw_omap_comp, hw)
  97. #if defined(CONFIG_ARCH_OMAP3) && defined(CONFIG_ATAGS)
  98. struct clk *ti_clk_register_composite(struct ti_clk *setup)
  99. {
  100. struct ti_clk_composite *comp;
  101. struct clk_hw *gate;
  102. struct clk_hw *mux;
  103. struct clk_hw *div;
  104. int num_parents = 1;
  105. const char * const *parent_names = NULL;
  106. struct clk *clk;
  107. int ret;
  108. comp = setup->data;
  109. div = ti_clk_build_component_div(comp->divider);
  110. gate = ti_clk_build_component_gate(comp->gate);
  111. mux = ti_clk_build_component_mux(comp->mux);
  112. if (div)
  113. parent_names = &comp->divider->parent;
  114. if (gate)
  115. parent_names = &comp->gate->parent;
  116. if (mux) {
  117. num_parents = comp->mux->num_parents;
  118. parent_names = comp->mux->parents;
  119. }
  120. clk = clk_register_composite(NULL, setup->name,
  121. parent_names, num_parents, mux,
  122. &ti_clk_mux_ops, div,
  123. &ti_composite_divider_ops, gate,
  124. &ti_composite_gate_ops, 0);
  125. ret = ti_clk_add_alias(NULL, clk, setup->name);
  126. if (ret) {
  127. clk_unregister(clk);
  128. return ERR_PTR(ret);
  129. }
  130. return clk;
  131. }
  132. #endif
  133. static void __init _register_composite(struct clk_hw *hw,
  134. struct device_node *node)
  135. {
  136. struct clk *clk;
  137. struct clk_hw_omap_comp *cclk = to_clk_hw_comp(hw);
  138. struct component_clk *comp;
  139. int num_parents = 0;
  140. const char **parent_names = NULL;
  141. int i;
  142. int ret;
  143. /* Check for presence of each component clock */
  144. for (i = 0; i < CLK_COMPONENT_TYPE_MAX; i++) {
  145. if (!cclk->comp_nodes[i])
  146. continue;
  147. comp = _lookup_component(cclk->comp_nodes[i]);
  148. if (!comp) {
  149. pr_debug("component %s not ready for %s, retry\n",
  150. cclk->comp_nodes[i]->name, node->name);
  151. if (!ti_clk_retry_init(node, hw,
  152. _register_composite))
  153. return;
  154. goto cleanup;
  155. }
  156. if (cclk->comp_clks[comp->type] != NULL) {
  157. pr_err("duplicate component types for %s (%s)!\n",
  158. node->name, component_clk_types[comp->type]);
  159. goto cleanup;
  160. }
  161. cclk->comp_clks[comp->type] = comp;
  162. /* Mark this node as found */
  163. cclk->comp_nodes[i] = NULL;
  164. }
  165. /* All components exists, proceed with registration */
  166. for (i = CLK_COMPONENT_TYPE_MAX - 1; i >= 0; i--) {
  167. comp = cclk->comp_clks[i];
  168. if (!comp)
  169. continue;
  170. if (comp->num_parents) {
  171. num_parents = comp->num_parents;
  172. parent_names = comp->parent_names;
  173. break;
  174. }
  175. }
  176. if (!num_parents) {
  177. pr_err("%s: no parents found for %s!\n", __func__, node->name);
  178. goto cleanup;
  179. }
  180. clk = clk_register_composite(NULL, node->name,
  181. parent_names, num_parents,
  182. _get_hw(cclk, CLK_COMPONENT_TYPE_MUX),
  183. &ti_clk_mux_ops,
  184. _get_hw(cclk, CLK_COMPONENT_TYPE_DIVIDER),
  185. &ti_composite_divider_ops,
  186. _get_hw(cclk, CLK_COMPONENT_TYPE_GATE),
  187. &ti_composite_gate_ops, 0);
  188. if (!IS_ERR(clk)) {
  189. ret = ti_clk_add_alias(NULL, clk, node->name);
  190. if (ret) {
  191. clk_unregister(clk);
  192. goto cleanup;
  193. }
  194. of_clk_add_provider(node, of_clk_src_simple_get, clk);
  195. }
  196. cleanup:
  197. /* Free component clock list entries */
  198. for (i = 0; i < CLK_COMPONENT_TYPE_MAX; i++) {
  199. if (!cclk->comp_clks[i])
  200. continue;
  201. list_del(&cclk->comp_clks[i]->link);
  202. kfree(cclk->comp_clks[i]);
  203. }
  204. kfree(cclk);
  205. }
  206. static void __init of_ti_composite_clk_setup(struct device_node *node)
  207. {
  208. unsigned int num_clks;
  209. int i;
  210. struct clk_hw_omap_comp *cclk;
  211. /* Number of component clocks to be put inside this clock */
  212. num_clks = of_clk_get_parent_count(node);
  213. if (!num_clks) {
  214. pr_err("composite clk %s must have component(s)\n", node->name);
  215. return;
  216. }
  217. cclk = kzalloc(sizeof(*cclk), GFP_KERNEL);
  218. if (!cclk)
  219. return;
  220. /* Get device node pointers for each component clock */
  221. for (i = 0; i < num_clks; i++)
  222. cclk->comp_nodes[i] = _get_component_node(node, i);
  223. _register_composite(&cclk->hw, node);
  224. }
  225. CLK_OF_DECLARE(ti_composite_clock, "ti,composite-clock",
  226. of_ti_composite_clk_setup);
  227. /**
  228. * ti_clk_add_component - add a component clock to the pool
  229. * @node: device node of the component clock
  230. * @hw: hardware clock definition for the component clock
  231. * @type: type of the component clock
  232. *
  233. * Adds a component clock to the list of available components, so that
  234. * it can be registered by a composite clock.
  235. */
  236. int __init ti_clk_add_component(struct device_node *node, struct clk_hw *hw,
  237. int type)
  238. {
  239. unsigned int num_parents;
  240. const char **parent_names;
  241. struct component_clk *clk;
  242. num_parents = of_clk_get_parent_count(node);
  243. if (!num_parents) {
  244. pr_err("component-clock %s must have parent(s)\n", node->name);
  245. return -EINVAL;
  246. }
  247. parent_names = kzalloc((sizeof(char *) * num_parents), GFP_KERNEL);
  248. if (!parent_names)
  249. return -ENOMEM;
  250. of_clk_parent_fill(node, parent_names, num_parents);
  251. clk = kzalloc(sizeof(*clk), GFP_KERNEL);
  252. if (!clk) {
  253. kfree(parent_names);
  254. return -ENOMEM;
  255. }
  256. clk->num_parents = num_parents;
  257. clk->parent_names = parent_names;
  258. clk->hw = hw;
  259. clk->node = node;
  260. clk->type = type;
  261. list_add(&clk->link, &component_clks);
  262. return 0;
  263. }