clk-bulk.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Copyright 2017 NXP
  3. *
  4. * Dong Aisheng <aisheng.dong@nxp.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <linux/clk.h>
  19. #include <linux/device.h>
  20. #include <linux/export.h>
  21. void clk_bulk_put(int num_clks, struct clk_bulk_data *clks)
  22. {
  23. while (--num_clks >= 0) {
  24. clk_put(clks[num_clks].clk);
  25. clks[num_clks].clk = NULL;
  26. }
  27. }
  28. EXPORT_SYMBOL_GPL(clk_bulk_put);
  29. int __must_check clk_bulk_get(struct device *dev, int num_clks,
  30. struct clk_bulk_data *clks)
  31. {
  32. int ret;
  33. int i;
  34. for (i = 0; i < num_clks; i++)
  35. clks[i].clk = NULL;
  36. for (i = 0; i < num_clks; i++) {
  37. clks[i].clk = clk_get(dev, clks[i].id);
  38. if (IS_ERR(clks[i].clk)) {
  39. ret = PTR_ERR(clks[i].clk);
  40. if (ret != -EPROBE_DEFER)
  41. dev_err(dev, "Failed to get clk '%s': %d\n",
  42. clks[i].id, ret);
  43. clks[i].clk = NULL;
  44. goto err;
  45. }
  46. }
  47. return 0;
  48. err:
  49. clk_bulk_put(i, clks);
  50. return ret;
  51. }
  52. EXPORT_SYMBOL(clk_bulk_get);
  53. #ifdef CONFIG_HAVE_CLK_PREPARE
  54. /**
  55. * clk_bulk_unprepare - undo preparation of a set of clock sources
  56. * @num_clks: the number of clk_bulk_data
  57. * @clks: the clk_bulk_data table being unprepared
  58. *
  59. * clk_bulk_unprepare may sleep, which differentiates it from clk_bulk_disable.
  60. * Returns 0 on success, -EERROR otherwise.
  61. */
  62. void clk_bulk_unprepare(int num_clks, const struct clk_bulk_data *clks)
  63. {
  64. while (--num_clks >= 0)
  65. clk_unprepare(clks[num_clks].clk);
  66. }
  67. EXPORT_SYMBOL_GPL(clk_bulk_unprepare);
  68. /**
  69. * clk_bulk_prepare - prepare a set of clocks
  70. * @num_clks: the number of clk_bulk_data
  71. * @clks: the clk_bulk_data table being prepared
  72. *
  73. * clk_bulk_prepare may sleep, which differentiates it from clk_bulk_enable.
  74. * Returns 0 on success, -EERROR otherwise.
  75. */
  76. int __must_check clk_bulk_prepare(int num_clks,
  77. const struct clk_bulk_data *clks)
  78. {
  79. int ret;
  80. int i;
  81. for (i = 0; i < num_clks; i++) {
  82. ret = clk_prepare(clks[i].clk);
  83. if (ret) {
  84. pr_err("Failed to prepare clk '%s': %d\n",
  85. clks[i].id, ret);
  86. goto err;
  87. }
  88. }
  89. return 0;
  90. err:
  91. clk_bulk_unprepare(i, clks);
  92. return ret;
  93. }
  94. EXPORT_SYMBOL_GPL(clk_bulk_prepare);
  95. #endif /* CONFIG_HAVE_CLK_PREPARE */
  96. /**
  97. * clk_bulk_disable - gate a set of clocks
  98. * @num_clks: the number of clk_bulk_data
  99. * @clks: the clk_bulk_data table being gated
  100. *
  101. * clk_bulk_disable must not sleep, which differentiates it from
  102. * clk_bulk_unprepare. clk_bulk_disable must be called before
  103. * clk_bulk_unprepare.
  104. */
  105. void clk_bulk_disable(int num_clks, const struct clk_bulk_data *clks)
  106. {
  107. while (--num_clks >= 0)
  108. clk_disable(clks[num_clks].clk);
  109. }
  110. EXPORT_SYMBOL_GPL(clk_bulk_disable);
  111. /**
  112. * clk_bulk_enable - ungate a set of clocks
  113. * @num_clks: the number of clk_bulk_data
  114. * @clks: the clk_bulk_data table being ungated
  115. *
  116. * clk_bulk_enable must not sleep
  117. * Returns 0 on success, -EERROR otherwise.
  118. */
  119. int __must_check clk_bulk_enable(int num_clks, const struct clk_bulk_data *clks)
  120. {
  121. int ret;
  122. int i;
  123. for (i = 0; i < num_clks; i++) {
  124. ret = clk_enable(clks[i].clk);
  125. if (ret) {
  126. pr_err("Failed to enable clk '%s': %d\n",
  127. clks[i].id, ret);
  128. goto err;
  129. }
  130. }
  131. return 0;
  132. err:
  133. clk_bulk_disable(i, clks);
  134. return ret;
  135. }
  136. EXPORT_SYMBOL_GPL(clk_bulk_enable);