clk.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * TI 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/clkdev.h>
  19. #include <linux/clk/ti.h>
  20. #include <linux/of.h>
  21. #include <linux/of_address.h>
  22. #include <linux/list.h>
  23. #undef pr_fmt
  24. #define pr_fmt(fmt) "%s: " fmt, __func__
  25. static int ti_dt_clk_memmap_index;
  26. struct ti_clk_ll_ops *ti_clk_ll_ops;
  27. /**
  28. * ti_dt_clocks_register - register DT alias clocks during boot
  29. * @oclks: list of clocks to register
  30. *
  31. * Register alias or non-standard DT clock entries during boot. By
  32. * default, DT clocks are found based on their node name. If any
  33. * additional con-id / dev-id -> clock mapping is required, use this
  34. * function to list these.
  35. */
  36. void __init ti_dt_clocks_register(struct ti_dt_clk oclks[])
  37. {
  38. struct ti_dt_clk *c;
  39. struct device_node *node;
  40. struct clk *clk;
  41. struct of_phandle_args clkspec;
  42. for (c = oclks; c->node_name != NULL; c++) {
  43. node = of_find_node_by_name(NULL, c->node_name);
  44. clkspec.np = node;
  45. clk = of_clk_get_from_provider(&clkspec);
  46. if (!IS_ERR(clk)) {
  47. c->lk.clk = clk;
  48. clkdev_add(&c->lk);
  49. } else {
  50. pr_warn("failed to lookup clock node %s\n",
  51. c->node_name);
  52. }
  53. }
  54. }
  55. struct clk_init_item {
  56. struct device_node *node;
  57. struct clk_hw *hw;
  58. ti_of_clk_init_cb_t func;
  59. struct list_head link;
  60. };
  61. static LIST_HEAD(retry_list);
  62. /**
  63. * ti_clk_retry_init - retries a failed clock init at later phase
  64. * @node: device not for the clock
  65. * @hw: partially initialized clk_hw struct for the clock
  66. * @func: init function to be called for the clock
  67. *
  68. * Adds a failed clock init to the retry list. The retry list is parsed
  69. * once all the other clocks have been initialized.
  70. */
  71. int __init ti_clk_retry_init(struct device_node *node, struct clk_hw *hw,
  72. ti_of_clk_init_cb_t func)
  73. {
  74. struct clk_init_item *retry;
  75. pr_debug("%s: adding to retry list...\n", node->name);
  76. retry = kzalloc(sizeof(*retry), GFP_KERNEL);
  77. if (!retry)
  78. return -ENOMEM;
  79. retry->node = node;
  80. retry->func = func;
  81. retry->hw = hw;
  82. list_add(&retry->link, &retry_list);
  83. return 0;
  84. }
  85. /**
  86. * ti_clk_get_reg_addr - get register address for a clock register
  87. * @node: device node for the clock
  88. * @index: register index from the clock node
  89. *
  90. * Builds clock register address from device tree information. This
  91. * is a struct of type clk_omap_reg.
  92. */
  93. void __iomem *ti_clk_get_reg_addr(struct device_node *node, int index)
  94. {
  95. struct clk_omap_reg *reg;
  96. u32 val;
  97. u32 tmp;
  98. reg = (struct clk_omap_reg *)&tmp;
  99. reg->index = ti_dt_clk_memmap_index;
  100. if (of_property_read_u32_index(node, "reg", index, &val)) {
  101. pr_err("%s must have reg[%d]!\n", node->name, index);
  102. return NULL;
  103. }
  104. reg->offset = val;
  105. return (void __iomem *)tmp;
  106. }
  107. /**
  108. * ti_dt_clk_init_provider - init master clock provider
  109. * @parent: master node
  110. * @index: internal index for clk_reg_ops
  111. *
  112. * Initializes a master clock IP block and its child clock nodes.
  113. * Regmap is provided for accessing the register space for the
  114. * IP block and all the clocks under it.
  115. */
  116. void ti_dt_clk_init_provider(struct device_node *parent, int index)
  117. {
  118. const struct of_device_id *match;
  119. struct device_node *np;
  120. struct device_node *clocks;
  121. of_clk_init_cb_t clk_init_cb;
  122. struct clk_init_item *retry;
  123. struct clk_init_item *tmp;
  124. ti_dt_clk_memmap_index = index;
  125. /* get clocks for this parent */
  126. clocks = of_get_child_by_name(parent, "clocks");
  127. if (!clocks) {
  128. pr_err("%s missing 'clocks' child node.\n", parent->name);
  129. return;
  130. }
  131. for_each_child_of_node(clocks, np) {
  132. match = of_match_node(&__clk_of_table, np);
  133. if (!match)
  134. continue;
  135. clk_init_cb = (of_clk_init_cb_t)match->data;
  136. pr_debug("%s: initializing: %s\n", __func__, np->name);
  137. clk_init_cb(np);
  138. }
  139. list_for_each_entry_safe(retry, tmp, &retry_list, link) {
  140. pr_debug("retry-init: %s\n", retry->node->name);
  141. retry->func(retry->hw, retry->node);
  142. list_del(&retry->link);
  143. kfree(retry);
  144. }
  145. }