composite.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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 **parent_names = NULL;
  106. struct clk *clk;
  107. comp = setup->data;
  108. div = ti_clk_build_component_div(comp->divider);
  109. gate = ti_clk_build_component_gate(comp->gate);
  110. mux = ti_clk_build_component_mux(comp->mux);
  111. if (div)
  112. parent_names = &comp->divider->parent;
  113. if (gate)
  114. parent_names = &comp->gate->parent;
  115. if (mux) {
  116. num_parents = comp->mux->num_parents;
  117. parent_names = comp->mux->parents;
  118. }
  119. clk = clk_register_composite(NULL, setup->name,
  120. parent_names, num_parents, mux,
  121. &ti_clk_mux_ops, div,
  122. &ti_composite_divider_ops, gate,
  123. &ti_composite_gate_ops, 0);
  124. return clk;
  125. }
  126. #endif
  127. static void __init _register_composite(struct clk_hw *hw,
  128. struct device_node *node)
  129. {
  130. struct clk *clk;
  131. struct clk_hw_omap_comp *cclk = to_clk_hw_comp(hw);
  132. struct component_clk *comp;
  133. int num_parents = 0;
  134. const char **parent_names = NULL;
  135. int i;
  136. /* Check for presence of each component clock */
  137. for (i = 0; i < CLK_COMPONENT_TYPE_MAX; i++) {
  138. if (!cclk->comp_nodes[i])
  139. continue;
  140. comp = _lookup_component(cclk->comp_nodes[i]);
  141. if (!comp) {
  142. pr_debug("component %s not ready for %s, retry\n",
  143. cclk->comp_nodes[i]->name, node->name);
  144. if (!ti_clk_retry_init(node, hw,
  145. _register_composite))
  146. return;
  147. goto cleanup;
  148. }
  149. if (cclk->comp_clks[comp->type] != NULL) {
  150. pr_err("duplicate component types for %s (%s)!\n",
  151. node->name, component_clk_types[comp->type]);
  152. goto cleanup;
  153. }
  154. cclk->comp_clks[comp->type] = comp;
  155. /* Mark this node as found */
  156. cclk->comp_nodes[i] = NULL;
  157. }
  158. /* All components exists, proceed with registration */
  159. for (i = CLK_COMPONENT_TYPE_MAX - 1; i >= 0; i--) {
  160. comp = cclk->comp_clks[i];
  161. if (!comp)
  162. continue;
  163. if (comp->num_parents) {
  164. num_parents = comp->num_parents;
  165. parent_names = comp->parent_names;
  166. break;
  167. }
  168. }
  169. if (!num_parents) {
  170. pr_err("%s: no parents found for %s!\n", __func__, node->name);
  171. goto cleanup;
  172. }
  173. clk = clk_register_composite(NULL, node->name,
  174. parent_names, num_parents,
  175. _get_hw(cclk, CLK_COMPONENT_TYPE_MUX),
  176. &ti_clk_mux_ops,
  177. _get_hw(cclk, CLK_COMPONENT_TYPE_DIVIDER),
  178. &ti_composite_divider_ops,
  179. _get_hw(cclk, CLK_COMPONENT_TYPE_GATE),
  180. &ti_composite_gate_ops, 0);
  181. if (!IS_ERR(clk))
  182. of_clk_add_provider(node, of_clk_src_simple_get, clk);
  183. cleanup:
  184. /* Free component clock list entries */
  185. for (i = 0; i < CLK_COMPONENT_TYPE_MAX; i++) {
  186. if (!cclk->comp_clks[i])
  187. continue;
  188. list_del(&cclk->comp_clks[i]->link);
  189. kfree(cclk->comp_clks[i]);
  190. }
  191. kfree(cclk);
  192. }
  193. static void __init of_ti_composite_clk_setup(struct device_node *node)
  194. {
  195. unsigned int num_clks;
  196. int i;
  197. struct clk_hw_omap_comp *cclk;
  198. /* Number of component clocks to be put inside this clock */
  199. num_clks = of_clk_get_parent_count(node);
  200. if (!num_clks) {
  201. pr_err("composite clk %s must have component(s)\n", node->name);
  202. return;
  203. }
  204. cclk = kzalloc(sizeof(*cclk), GFP_KERNEL);
  205. if (!cclk)
  206. return;
  207. /* Get device node pointers for each component clock */
  208. for (i = 0; i < num_clks; i++)
  209. cclk->comp_nodes[i] = _get_component_node(node, i);
  210. _register_composite(&cclk->hw, node);
  211. }
  212. CLK_OF_DECLARE(ti_composite_clock, "ti,composite-clock",
  213. of_ti_composite_clk_setup);
  214. /**
  215. * ti_clk_add_component - add a component clock to the pool
  216. * @node: device node of the component clock
  217. * @hw: hardware clock definition for the component clock
  218. * @type: type of the component clock
  219. *
  220. * Adds a component clock to the list of available components, so that
  221. * it can be registered by a composite clock.
  222. */
  223. int __init ti_clk_add_component(struct device_node *node, struct clk_hw *hw,
  224. int type)
  225. {
  226. unsigned int num_parents;
  227. const char **parent_names;
  228. struct component_clk *clk;
  229. num_parents = of_clk_get_parent_count(node);
  230. if (!num_parents) {
  231. pr_err("component-clock %s must have parent(s)\n", node->name);
  232. return -EINVAL;
  233. }
  234. parent_names = kzalloc((sizeof(char *) * num_parents), GFP_KERNEL);
  235. if (!parent_names)
  236. return -ENOMEM;
  237. of_clk_parent_fill(node, parent_names, num_parents);
  238. clk = kzalloc(sizeof(*clk), GFP_KERNEL);
  239. if (!clk) {
  240. kfree(parent_names);
  241. return -ENOMEM;
  242. }
  243. clk->num_parents = num_parents;
  244. clk->parent_names = parent_names;
  245. clk->hw = hw;
  246. clk->node = node;
  247. clk->type = type;
  248. list_add(&clk->link, &component_clks);
  249. return 0;
  250. }