clk-bulk.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. dev_err(dev, "Failed to get clk '%s': %d\n",
  41. clks[i].id, ret);
  42. clks[i].clk = NULL;
  43. goto err;
  44. }
  45. }
  46. return 0;
  47. err:
  48. clk_bulk_put(i, clks);
  49. return ret;
  50. }
  51. EXPORT_SYMBOL(clk_bulk_get);
  52. #ifdef CONFIG_HAVE_CLK_PREPARE
  53. /**
  54. * clk_bulk_unprepare - undo preparation of a set of clock sources
  55. * @num_clks: the number of clk_bulk_data
  56. * @clks: the clk_bulk_data table being unprepared
  57. *
  58. * clk_bulk_unprepare may sleep, which differentiates it from clk_bulk_disable.
  59. * Returns 0 on success, -EERROR otherwise.
  60. */
  61. void clk_bulk_unprepare(int num_clks, const struct clk_bulk_data *clks)
  62. {
  63. while (--num_clks >= 0)
  64. clk_unprepare(clks[num_clks].clk);
  65. }
  66. EXPORT_SYMBOL_GPL(clk_bulk_unprepare);
  67. /**
  68. * clk_bulk_prepare - prepare a set of clocks
  69. * @num_clks: the number of clk_bulk_data
  70. * @clks: the clk_bulk_data table being prepared
  71. *
  72. * clk_bulk_prepare may sleep, which differentiates it from clk_bulk_enable.
  73. * Returns 0 on success, -EERROR otherwise.
  74. */
  75. int __must_check clk_bulk_prepare(int num_clks,
  76. const struct clk_bulk_data *clks)
  77. {
  78. int ret;
  79. int i;
  80. for (i = 0; i < num_clks; i++) {
  81. ret = clk_prepare(clks[i].clk);
  82. if (ret) {
  83. pr_err("Failed to prepare clk '%s': %d\n",
  84. clks[i].id, ret);
  85. goto err;
  86. }
  87. }
  88. return 0;
  89. err:
  90. clk_bulk_unprepare(i, clks);
  91. return ret;
  92. }
  93. EXPORT_SYMBOL_GPL(clk_bulk_prepare);
  94. #endif /* CONFIG_HAVE_CLK_PREPARE */
  95. /**
  96. * clk_bulk_disable - gate a set of clocks
  97. * @num_clks: the number of clk_bulk_data
  98. * @clks: the clk_bulk_data table being gated
  99. *
  100. * clk_bulk_disable must not sleep, which differentiates it from
  101. * clk_bulk_unprepare. clk_bulk_disable must be called before
  102. * clk_bulk_unprepare.
  103. */
  104. void clk_bulk_disable(int num_clks, const struct clk_bulk_data *clks)
  105. {
  106. while (--num_clks >= 0)
  107. clk_disable(clks[num_clks].clk);
  108. }
  109. EXPORT_SYMBOL_GPL(clk_bulk_disable);
  110. /**
  111. * clk_bulk_enable - ungate a set of clocks
  112. * @num_clks: the number of clk_bulk_data
  113. * @clks: the clk_bulk_data table being ungated
  114. *
  115. * clk_bulk_enable must not sleep
  116. * Returns 0 on success, -EERROR otherwise.
  117. */
  118. int __must_check clk_bulk_enable(int num_clks, const struct clk_bulk_data *clks)
  119. {
  120. int ret;
  121. int i;
  122. for (i = 0; i < num_clks; i++) {
  123. ret = clk_enable(clks[i].clk);
  124. if (ret) {
  125. pr_err("Failed to enable clk '%s': %d\n",
  126. clks[i].id, ret);
  127. goto err;
  128. }
  129. }
  130. return 0;
  131. err:
  132. clk_bulk_disable(i, clks);
  133. return ret;
  134. }
  135. EXPORT_SYMBOL_GPL(clk_bulk_enable);