composite.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. #define to_clk_divider(_hw) container_of(_hw, struct clk_divider, hw)
  28. static unsigned long ti_composite_recalc_rate(struct clk_hw *hw,
  29. unsigned long parent_rate)
  30. {
  31. return ti_clk_divider_ops.recalc_rate(hw, parent_rate);
  32. }
  33. static long ti_composite_round_rate(struct clk_hw *hw, unsigned long rate,
  34. unsigned long *prate)
  35. {
  36. return -EINVAL;
  37. }
  38. static int ti_composite_set_rate(struct clk_hw *hw, unsigned long rate,
  39. unsigned long parent_rate)
  40. {
  41. return -EINVAL;
  42. }
  43. static const struct clk_ops ti_composite_divider_ops = {
  44. .recalc_rate = &ti_composite_recalc_rate,
  45. .round_rate = &ti_composite_round_rate,
  46. .set_rate = &ti_composite_set_rate,
  47. };
  48. static const struct clk_ops ti_composite_gate_ops = {
  49. .enable = &omap2_dflt_clk_enable,
  50. .disable = &omap2_dflt_clk_disable,
  51. .is_enabled = &omap2_dflt_clk_is_enabled,
  52. };
  53. struct component_clk {
  54. int num_parents;
  55. const char **parent_names;
  56. struct device_node *node;
  57. int type;
  58. struct clk_hw *hw;
  59. struct list_head link;
  60. };
  61. static const char * const component_clk_types[] __initconst = {
  62. "gate", "divider", "mux"
  63. };
  64. static LIST_HEAD(component_clks);
  65. static struct device_node *_get_component_node(struct device_node *node, int i)
  66. {
  67. int rc;
  68. struct of_phandle_args clkspec;
  69. rc = of_parse_phandle_with_args(node, "clocks", "#clock-cells", i,
  70. &clkspec);
  71. if (rc)
  72. return NULL;
  73. return clkspec.np;
  74. }
  75. static struct component_clk *_lookup_component(struct device_node *node)
  76. {
  77. struct component_clk *comp;
  78. list_for_each_entry(comp, &component_clks, link) {
  79. if (comp->node == node)
  80. return comp;
  81. }
  82. return NULL;
  83. }
  84. struct clk_hw_omap_comp {
  85. struct clk_hw hw;
  86. struct device_node *comp_nodes[CLK_COMPONENT_TYPE_MAX];
  87. struct component_clk *comp_clks[CLK_COMPONENT_TYPE_MAX];
  88. };
  89. static inline struct clk_hw *_get_hw(struct clk_hw_omap_comp *clk, int idx)
  90. {
  91. if (!clk)
  92. return NULL;
  93. if (!clk->comp_clks[idx])
  94. return NULL;
  95. return clk->comp_clks[idx]->hw;
  96. }
  97. #define to_clk_hw_comp(_hw) container_of(_hw, struct clk_hw_omap_comp, hw)
  98. #if defined(CONFIG_ARCH_OMAP3) && defined(CONFIG_ATAGS)
  99. struct clk *ti_clk_register_composite(struct ti_clk *setup)
  100. {
  101. struct ti_clk_composite *comp;
  102. struct clk_hw *gate;
  103. struct clk_hw *mux;
  104. struct clk_hw *div;
  105. int num_parents = 1;
  106. const char **parent_names = NULL;
  107. struct clk *clk;
  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. return clk;
  126. }
  127. #endif
  128. static void __init _register_composite(struct clk_hw *hw,
  129. struct device_node *node)
  130. {
  131. struct clk *clk;
  132. struct clk_hw_omap_comp *cclk = to_clk_hw_comp(hw);
  133. struct component_clk *comp;
  134. int num_parents = 0;
  135. const char **parent_names = NULL;
  136. int i;
  137. /* Check for presence of each component clock */
  138. for (i = 0; i < CLK_COMPONENT_TYPE_MAX; i++) {
  139. if (!cclk->comp_nodes[i])
  140. continue;
  141. comp = _lookup_component(cclk->comp_nodes[i]);
  142. if (!comp) {
  143. pr_debug("component %s not ready for %s, retry\n",
  144. cclk->comp_nodes[i]->name, node->name);
  145. if (!ti_clk_retry_init(node, hw,
  146. _register_composite))
  147. return;
  148. goto cleanup;
  149. }
  150. if (cclk->comp_clks[comp->type] != NULL) {
  151. pr_err("duplicate component types for %s (%s)!\n",
  152. node->name, component_clk_types[comp->type]);
  153. goto cleanup;
  154. }
  155. cclk->comp_clks[comp->type] = comp;
  156. /* Mark this node as found */
  157. cclk->comp_nodes[i] = NULL;
  158. }
  159. /* All components exists, proceed with registration */
  160. for (i = CLK_COMPONENT_TYPE_MAX - 1; i >= 0; i--) {
  161. comp = cclk->comp_clks[i];
  162. if (!comp)
  163. continue;
  164. if (comp->num_parents) {
  165. num_parents = comp->num_parents;
  166. parent_names = comp->parent_names;
  167. break;
  168. }
  169. }
  170. if (!num_parents) {
  171. pr_err("%s: no parents found for %s!\n", __func__, node->name);
  172. goto cleanup;
  173. }
  174. clk = clk_register_composite(NULL, node->name,
  175. parent_names, num_parents,
  176. _get_hw(cclk, CLK_COMPONENT_TYPE_MUX),
  177. &ti_clk_mux_ops,
  178. _get_hw(cclk, CLK_COMPONENT_TYPE_DIVIDER),
  179. &ti_composite_divider_ops,
  180. _get_hw(cclk, CLK_COMPONENT_TYPE_GATE),
  181. &ti_composite_gate_ops, 0);
  182. if (!IS_ERR(clk))
  183. of_clk_add_provider(node, of_clk_src_simple_get, clk);
  184. cleanup:
  185. /* Free component clock list entries */
  186. for (i = 0; i < CLK_COMPONENT_TYPE_MAX; i++) {
  187. if (!cclk->comp_clks[i])
  188. continue;
  189. list_del(&cclk->comp_clks[i]->link);
  190. kfree(cclk->comp_clks[i]);
  191. }
  192. kfree(cclk);
  193. }
  194. static void __init of_ti_composite_clk_setup(struct device_node *node)
  195. {
  196. int num_clks;
  197. int i;
  198. struct clk_hw_omap_comp *cclk;
  199. /* Number of component clocks to be put inside this clock */
  200. num_clks = of_clk_get_parent_count(node);
  201. if (num_clks < 1) {
  202. pr_err("composite clk %s must have component(s)\n", node->name);
  203. return;
  204. }
  205. cclk = kzalloc(sizeof(*cclk), GFP_KERNEL);
  206. if (!cclk)
  207. return;
  208. /* Get device node pointers for each component clock */
  209. for (i = 0; i < num_clks; i++)
  210. cclk->comp_nodes[i] = _get_component_node(node, i);
  211. _register_composite(&cclk->hw, node);
  212. }
  213. CLK_OF_DECLARE(ti_composite_clock, "ti,composite-clock",
  214. of_ti_composite_clk_setup);
  215. /**
  216. * ti_clk_add_component - add a component clock to the pool
  217. * @node: device node of the component clock
  218. * @hw: hardware clock definition for the component clock
  219. * @type: type of the component clock
  220. *
  221. * Adds a component clock to the list of available components, so that
  222. * it can be registered by a composite clock.
  223. */
  224. int __init ti_clk_add_component(struct device_node *node, struct clk_hw *hw,
  225. int type)
  226. {
  227. int num_parents;
  228. const char **parent_names;
  229. struct component_clk *clk;
  230. num_parents = of_clk_get_parent_count(node);
  231. if (num_parents < 1) {
  232. pr_err("component-clock %s must have parent(s)\n", node->name);
  233. return -EINVAL;
  234. }
  235. parent_names = kzalloc((sizeof(char *) * num_parents), GFP_KERNEL);
  236. if (!parent_names)
  237. return -ENOMEM;
  238. of_clk_parent_fill(node, parent_names, num_parents);
  239. clk = kzalloc(sizeof(*clk), GFP_KERNEL);
  240. if (!clk) {
  241. kfree(parent_names);
  242. return -ENOMEM;
  243. }
  244. clk->num_parents = num_parents;
  245. clk->parent_names = parent_names;
  246. clk->hw = hw;
  247. clk->node = node;
  248. clk->type = type;
  249. list_add(&clk->link, &component_clks);
  250. return 0;
  251. }