clockdomain.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * OMAP clockdomain support
  4. *
  5. * Copyright (C) 2013 Texas Instruments, Inc.
  6. *
  7. * Tero Kristo <[email protected]>
  8. */
  9. #include <linux/clk.h>
  10. #include <linux/clk-provider.h>
  11. #include <linux/slab.h>
  12. #include <linux/of.h>
  13. #include <linux/of_address.h>
  14. #include <linux/clk/ti.h>
  15. #include "clock.h"
  16. #undef pr_fmt
  17. #define pr_fmt(fmt) "%s: " fmt, __func__
  18. /**
  19. * omap2_clkops_enable_clkdm - increment usecount on clkdm of @hw
  20. * @hw: struct clk_hw * of the clock being enabled
  21. *
  22. * Increment the usecount of the clockdomain of the clock pointed to
  23. * by @hw; if the usecount is 1, the clockdomain will be "enabled."
  24. * Only needed for clocks that don't use omap2_dflt_clk_enable() as
  25. * their enable function pointer. Passes along the return value of
  26. * clkdm_clk_enable(), -EINVAL if @hw is not associated with a
  27. * clockdomain, or 0 if clock framework-based clockdomain control is
  28. * not implemented.
  29. */
  30. int omap2_clkops_enable_clkdm(struct clk_hw *hw)
  31. {
  32. struct clk_hw_omap *clk;
  33. int ret = 0;
  34. clk = to_clk_hw_omap(hw);
  35. if (unlikely(!clk->clkdm)) {
  36. pr_err("%s: %s: no clkdm set ?!\n", __func__,
  37. clk_hw_get_name(hw));
  38. return -EINVAL;
  39. }
  40. if (ti_clk_get_features()->flags & TI_CLK_DISABLE_CLKDM_CONTROL) {
  41. pr_err("%s: %s: clkfw-based clockdomain control disabled ?!\n",
  42. __func__, clk_hw_get_name(hw));
  43. return 0;
  44. }
  45. ret = ti_clk_ll_ops->clkdm_clk_enable(clk->clkdm, hw->clk);
  46. WARN(ret, "%s: could not enable %s's clockdomain %s: %d\n",
  47. __func__, clk_hw_get_name(hw), clk->clkdm_name, ret);
  48. return ret;
  49. }
  50. /**
  51. * omap2_clkops_disable_clkdm - decrement usecount on clkdm of @hw
  52. * @hw: struct clk_hw * of the clock being disabled
  53. *
  54. * Decrement the usecount of the clockdomain of the clock pointed to
  55. * by @hw; if the usecount is 0, the clockdomain will be "disabled."
  56. * Only needed for clocks that don't use omap2_dflt_clk_disable() as their
  57. * disable function pointer. No return value.
  58. */
  59. void omap2_clkops_disable_clkdm(struct clk_hw *hw)
  60. {
  61. struct clk_hw_omap *clk;
  62. clk = to_clk_hw_omap(hw);
  63. if (unlikely(!clk->clkdm)) {
  64. pr_err("%s: %s: no clkdm set ?!\n", __func__,
  65. clk_hw_get_name(hw));
  66. return;
  67. }
  68. if (ti_clk_get_features()->flags & TI_CLK_DISABLE_CLKDM_CONTROL) {
  69. pr_err("%s: %s: clkfw-based clockdomain control disabled ?!\n",
  70. __func__, clk_hw_get_name(hw));
  71. return;
  72. }
  73. ti_clk_ll_ops->clkdm_clk_disable(clk->clkdm, hw->clk);
  74. }
  75. /**
  76. * omap2_init_clk_clkdm - look up a clockdomain name, store pointer in clk
  77. * @hw: Pointer to clk_hw_omap used to obtain OMAP clock struct ptr to use
  78. *
  79. * Convert a clockdomain name stored in a struct clk 'clk' into a
  80. * clockdomain pointer, and save it into the struct clk. Intended to be
  81. * called during clk_register(). Returns 0 on success, -EERROR otherwise.
  82. */
  83. int omap2_init_clk_clkdm(struct clk_hw *hw)
  84. {
  85. struct clk_hw_omap *clk = to_clk_hw_omap(hw);
  86. struct clockdomain *clkdm;
  87. const char *clk_name;
  88. if (!clk->clkdm_name)
  89. return 0;
  90. clk_name = __clk_get_name(hw->clk);
  91. clkdm = ti_clk_ll_ops->clkdm_lookup(clk->clkdm_name);
  92. if (clkdm) {
  93. pr_debug("clock: associated clk %s to clkdm %s\n",
  94. clk_name, clk->clkdm_name);
  95. clk->clkdm = clkdm;
  96. } else {
  97. pr_debug("clock: could not associate clk %s to clkdm %s\n",
  98. clk_name, clk->clkdm_name);
  99. }
  100. return 0;
  101. }
  102. static void __init of_ti_clockdomain_setup(struct device_node *node)
  103. {
  104. struct clk *clk;
  105. struct clk_hw *clk_hw;
  106. const char *clkdm_name = ti_dt_clk_name(node);
  107. int i;
  108. unsigned int num_clks;
  109. num_clks = of_clk_get_parent_count(node);
  110. for (i = 0; i < num_clks; i++) {
  111. clk = of_clk_get(node, i);
  112. if (IS_ERR(clk)) {
  113. pr_err("%s: Failed get %pOF' clock nr %d (%ld)\n",
  114. __func__, node, i, PTR_ERR(clk));
  115. continue;
  116. }
  117. clk_hw = __clk_get_hw(clk);
  118. if (!omap2_clk_is_hw_omap(clk_hw)) {
  119. pr_warn("can't setup clkdm for basic clk %s\n",
  120. __clk_get_name(clk));
  121. clk_put(clk);
  122. continue;
  123. }
  124. to_clk_hw_omap(clk_hw)->clkdm_name = clkdm_name;
  125. omap2_init_clk_clkdm(clk_hw);
  126. clk_put(clk);
  127. }
  128. }
  129. static const struct of_device_id ti_clkdm_match_table[] __initconst = {
  130. { .compatible = "ti,clockdomain" },
  131. { }
  132. };
  133. /**
  134. * ti_dt_clockdomains_setup - setup device tree clockdomains
  135. *
  136. * Initializes clockdomain nodes for a SoC. This parses through all the
  137. * nodes with compatible = "ti,clockdomain", and add the clockdomain
  138. * info for all the clocks listed under these. This function shall be
  139. * called after rest of the DT clock init has completed and all
  140. * clock nodes have been registered.
  141. */
  142. void __init ti_dt_clockdomains_setup(void)
  143. {
  144. struct device_node *np;
  145. for_each_matching_node(np, ti_clkdm_match_table) {
  146. of_ti_clockdomain_setup(np);
  147. }
  148. }