composite.c 6.5 KB

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