clk-conf.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2014 Samsung Electronics Co., Ltd.
  4. * Sylwester Nawrocki <[email protected]>
  5. */
  6. #include <linux/clk.h>
  7. #include <linux/clk-provider.h>
  8. #include <linux/clk/clk-conf.h>
  9. #include <linux/device.h>
  10. #include <linux/of.h>
  11. #include <linux/printk.h>
  12. static int __set_clk_parents(struct device_node *node, bool clk_supplier)
  13. {
  14. struct of_phandle_args clkspec;
  15. int index, rc, num_parents;
  16. struct clk *clk, *pclk;
  17. num_parents = of_count_phandle_with_args(node, "assigned-clock-parents",
  18. "#clock-cells");
  19. if (num_parents == -EINVAL)
  20. pr_err("clk: invalid value of clock-parents property at %pOF\n",
  21. node);
  22. for (index = 0; index < num_parents; index++) {
  23. rc = of_parse_phandle_with_args(node, "assigned-clock-parents",
  24. "#clock-cells", index, &clkspec);
  25. if (rc < 0) {
  26. /* skip empty (null) phandles */
  27. if (rc == -ENOENT)
  28. continue;
  29. else
  30. return rc;
  31. }
  32. if (clkspec.np == node && !clk_supplier) {
  33. of_node_put(clkspec.np);
  34. return 0;
  35. }
  36. pclk = of_clk_get_from_provider(&clkspec);
  37. of_node_put(clkspec.np);
  38. if (IS_ERR(pclk)) {
  39. if (PTR_ERR(pclk) != -EPROBE_DEFER)
  40. pr_warn("clk: couldn't get parent clock %d for %pOF\n",
  41. index, node);
  42. return PTR_ERR(pclk);
  43. }
  44. rc = of_parse_phandle_with_args(node, "assigned-clocks",
  45. "#clock-cells", index, &clkspec);
  46. if (rc < 0)
  47. goto err;
  48. if (clkspec.np == node && !clk_supplier) {
  49. of_node_put(clkspec.np);
  50. rc = 0;
  51. goto err;
  52. }
  53. clk = of_clk_get_from_provider(&clkspec);
  54. of_node_put(clkspec.np);
  55. if (IS_ERR(clk)) {
  56. if (PTR_ERR(clk) != -EPROBE_DEFER)
  57. pr_warn("clk: couldn't get assigned clock %d for %pOF\n",
  58. index, node);
  59. rc = PTR_ERR(clk);
  60. goto err;
  61. }
  62. rc = clk_set_parent(clk, pclk);
  63. if (rc < 0)
  64. pr_err("clk: failed to reparent %s to %s: %d\n",
  65. __clk_get_name(clk), __clk_get_name(pclk), rc);
  66. clk_put(clk);
  67. clk_put(pclk);
  68. }
  69. return 0;
  70. err:
  71. clk_put(pclk);
  72. return rc;
  73. }
  74. static int __set_clk_rates(struct device_node *node, bool clk_supplier)
  75. {
  76. struct of_phandle_args clkspec;
  77. struct property *prop;
  78. const __be32 *cur;
  79. int rc, index = 0;
  80. struct clk *clk;
  81. u32 rate;
  82. of_property_for_each_u32(node, "assigned-clock-rates", prop, cur, rate) {
  83. if (rate) {
  84. rc = of_parse_phandle_with_args(node, "assigned-clocks",
  85. "#clock-cells", index, &clkspec);
  86. if (rc < 0) {
  87. /* skip empty (null) phandles */
  88. if (rc == -ENOENT)
  89. continue;
  90. else
  91. return rc;
  92. }
  93. if (clkspec.np == node && !clk_supplier) {
  94. of_node_put(clkspec.np);
  95. return 0;
  96. }
  97. clk = of_clk_get_from_provider(&clkspec);
  98. of_node_put(clkspec.np);
  99. if (IS_ERR(clk)) {
  100. if (PTR_ERR(clk) != -EPROBE_DEFER)
  101. pr_warn("clk: couldn't get clock %d for %pOF\n",
  102. index, node);
  103. return PTR_ERR(clk);
  104. }
  105. rc = clk_set_rate(clk, rate);
  106. if (rc < 0)
  107. pr_err("clk: couldn't set %s clk rate to %u (%d), current rate: %lu\n",
  108. __clk_get_name(clk), rate, rc,
  109. clk_get_rate(clk));
  110. clk_put(clk);
  111. }
  112. index++;
  113. }
  114. return 0;
  115. }
  116. /**
  117. * of_clk_set_defaults() - parse and set assigned clocks configuration
  118. * @node: device node to apply clock settings for
  119. * @clk_supplier: true if clocks supplied by @node should also be considered
  120. *
  121. * This function parses 'assigned-{clocks/clock-parents/clock-rates}' properties
  122. * and sets any specified clock parents and rates. The @clk_supplier argument
  123. * should be set to true if @node may be also a clock supplier of any clock
  124. * listed in its 'assigned-clocks' or 'assigned-clock-parents' properties.
  125. * If @clk_supplier is false the function exits returning 0 as soon as it
  126. * determines the @node is also a supplier of any of the clocks.
  127. */
  128. int of_clk_set_defaults(struct device_node *node, bool clk_supplier)
  129. {
  130. int rc;
  131. if (!node)
  132. return 0;
  133. rc = __set_clk_parents(node, clk_supplier);
  134. if (rc < 0)
  135. return rc;
  136. return __set_clk_rates(node, clk_supplier);
  137. }
  138. EXPORT_SYMBOL_GPL(of_clk_set_defaults);