composite.c 6.6 KB

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