clk-conf.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 %s\n",
  24. node->full_name);
  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. pr_warn("clk: couldn't get parent clock %d for %s\n",
  40. index, node->full_name);
  41. return PTR_ERR(pclk);
  42. }
  43. rc = of_parse_phandle_with_args(node, "assigned-clocks",
  44. "#clock-cells", index, &clkspec);
  45. if (rc < 0)
  46. goto err;
  47. if (clkspec.np == node && !clk_supplier) {
  48. rc = 0;
  49. goto err;
  50. }
  51. clk = of_clk_get_from_provider(&clkspec);
  52. if (IS_ERR(clk)) {
  53. pr_warn("clk: couldn't get parent clock %d for %s\n",
  54. index, node->full_name);
  55. rc = PTR_ERR(clk);
  56. goto err;
  57. }
  58. rc = clk_set_parent(clk, pclk);
  59. if (rc < 0)
  60. pr_err("clk: failed to reparent %s to %s: %d\n",
  61. __clk_get_name(clk), __clk_get_name(pclk), rc);
  62. clk_put(clk);
  63. clk_put(pclk);
  64. }
  65. return 0;
  66. err:
  67. clk_put(pclk);
  68. return rc;
  69. }
  70. static int __set_clk_rates(struct device_node *node, bool clk_supplier)
  71. {
  72. struct of_phandle_args clkspec;
  73. struct property *prop;
  74. const __be32 *cur;
  75. int rc, index = 0;
  76. struct clk *clk;
  77. u32 rate;
  78. of_property_for_each_u32(node, "assigned-clock-rates", prop, cur, rate) {
  79. if (rate) {
  80. rc = of_parse_phandle_with_args(node, "assigned-clocks",
  81. "#clock-cells", index, &clkspec);
  82. if (rc < 0) {
  83. /* skip empty (null) phandles */
  84. if (rc == -ENOENT)
  85. continue;
  86. else
  87. return rc;
  88. }
  89. if (clkspec.np == node && !clk_supplier)
  90. return 0;
  91. clk = of_clk_get_from_provider(&clkspec);
  92. if (IS_ERR(clk)) {
  93. pr_warn("clk: couldn't get clock %d for %s\n",
  94. index, node->full_name);
  95. return PTR_ERR(clk);
  96. }
  97. rc = clk_set_rate(clk, rate);
  98. if (rc < 0)
  99. pr_err("clk: couldn't set %s clk rate to %u (%d), current rate: %lu\n",
  100. __clk_get_name(clk), rate, rc,
  101. clk_get_rate(clk));
  102. clk_put(clk);
  103. }
  104. index++;
  105. }
  106. return 0;
  107. }
  108. /**
  109. * of_clk_set_defaults() - parse and set assigned clocks configuration
  110. * @node: device node to apply clock settings for
  111. * @clk_supplier: true if clocks supplied by @node should also be considered
  112. *
  113. * This function parses 'assigned-{clocks/clock-parents/clock-rates}' properties
  114. * and sets any specified clock parents and rates. The @clk_supplier argument
  115. * should be set to true if @node may be also a clock supplier of any clock
  116. * listed in its 'assigned-clocks' or 'assigned-clock-parents' properties.
  117. * If @clk_supplier is false the function exits returning 0 as soon as it
  118. * determines the @node is also a supplier of any of the clocks.
  119. */
  120. int of_clk_set_defaults(struct device_node *node, bool clk_supplier)
  121. {
  122. int rc;
  123. if (!node)
  124. return 0;
  125. rc = __set_clk_parents(node, clk_supplier);
  126. if (rc < 0)
  127. return rc;
  128. return __set_clk_rates(node, clk_supplier);
  129. }
  130. EXPORT_SYMBOL_GPL(of_clk_set_defaults);