clk-conf.c 3.9 KB

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