clk-bulk.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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/clk-provider.h>
  20. #include <linux/device.h>
  21. #include <linux/export.h>
  22. #include <linux/of.h>
  23. #include <linux/slab.h>
  24. static int __must_check of_clk_bulk_get(struct device_node *np, int num_clks,
  25. struct clk_bulk_data *clks)
  26. {
  27. int ret;
  28. int i;
  29. for (i = 0; i < num_clks; i++)
  30. clks[i].clk = NULL;
  31. for (i = 0; i < num_clks; i++) {
  32. clks[i].clk = of_clk_get(np, i);
  33. if (IS_ERR(clks[i].clk)) {
  34. ret = PTR_ERR(clks[i].clk);
  35. pr_err("%pOF: Failed to get clk index: %d ret: %d\n",
  36. np, i, ret);
  37. clks[i].clk = NULL;
  38. goto err;
  39. }
  40. }
  41. return 0;
  42. err:
  43. clk_bulk_put(i, clks);
  44. return ret;
  45. }
  46. static int __must_check of_clk_bulk_get_all(struct device_node *np,
  47. struct clk_bulk_data **clks)
  48. {
  49. struct clk_bulk_data *clk_bulk;
  50. int num_clks;
  51. int ret;
  52. num_clks = of_clk_get_parent_count(np);
  53. if (!num_clks)
  54. return 0;
  55. clk_bulk = kmalloc_array(num_clks, sizeof(*clk_bulk), GFP_KERNEL);
  56. if (!clk_bulk)
  57. return -ENOMEM;
  58. ret = of_clk_bulk_get(np, num_clks, clk_bulk);
  59. if (ret) {
  60. kfree(clk_bulk);
  61. return ret;
  62. }
  63. *clks = clk_bulk;
  64. return num_clks;
  65. }
  66. void clk_bulk_put(int num_clks, struct clk_bulk_data *clks)
  67. {
  68. while (--num_clks >= 0) {
  69. clk_put(clks[num_clks].clk);
  70. clks[num_clks].clk = NULL;
  71. }
  72. }
  73. EXPORT_SYMBOL_GPL(clk_bulk_put);
  74. int __must_check clk_bulk_get(struct device *dev, int num_clks,
  75. struct clk_bulk_data *clks)
  76. {
  77. int ret;
  78. int i;
  79. for (i = 0; i < num_clks; i++)
  80. clks[i].clk = NULL;
  81. for (i = 0; i < num_clks; i++) {
  82. clks[i].clk = clk_get(dev, clks[i].id);
  83. if (IS_ERR(clks[i].clk)) {
  84. ret = PTR_ERR(clks[i].clk);
  85. if (ret != -EPROBE_DEFER)
  86. dev_err(dev, "Failed to get clk '%s': %d\n",
  87. clks[i].id, ret);
  88. clks[i].clk = NULL;
  89. goto err;
  90. }
  91. }
  92. return 0;
  93. err:
  94. clk_bulk_put(i, clks);
  95. return ret;
  96. }
  97. EXPORT_SYMBOL(clk_bulk_get);
  98. void clk_bulk_put_all(int num_clks, struct clk_bulk_data *clks)
  99. {
  100. if (IS_ERR_OR_NULL(clks))
  101. return;
  102. clk_bulk_put(num_clks, clks);
  103. kfree(clks);
  104. }
  105. EXPORT_SYMBOL(clk_bulk_put_all);
  106. int __must_check clk_bulk_get_all(struct device *dev,
  107. struct clk_bulk_data **clks)
  108. {
  109. struct device_node *np = dev_of_node(dev);
  110. if (!np)
  111. return 0;
  112. return of_clk_bulk_get_all(np, clks);
  113. }
  114. EXPORT_SYMBOL(clk_bulk_get_all);
  115. #ifdef CONFIG_HAVE_CLK_PREPARE
  116. /**
  117. * clk_bulk_unprepare - undo preparation of a set of clock sources
  118. * @num_clks: the number of clk_bulk_data
  119. * @clks: the clk_bulk_data table being unprepared
  120. *
  121. * clk_bulk_unprepare may sleep, which differentiates it from clk_bulk_disable.
  122. * Returns 0 on success, -EERROR otherwise.
  123. */
  124. void clk_bulk_unprepare(int num_clks, const struct clk_bulk_data *clks)
  125. {
  126. while (--num_clks >= 0)
  127. clk_unprepare(clks[num_clks].clk);
  128. }
  129. EXPORT_SYMBOL_GPL(clk_bulk_unprepare);
  130. /**
  131. * clk_bulk_prepare - prepare a set of clocks
  132. * @num_clks: the number of clk_bulk_data
  133. * @clks: the clk_bulk_data table being prepared
  134. *
  135. * clk_bulk_prepare may sleep, which differentiates it from clk_bulk_enable.
  136. * Returns 0 on success, -EERROR otherwise.
  137. */
  138. int __must_check clk_bulk_prepare(int num_clks,
  139. const struct clk_bulk_data *clks)
  140. {
  141. int ret;
  142. int i;
  143. for (i = 0; i < num_clks; i++) {
  144. ret = clk_prepare(clks[i].clk);
  145. if (ret) {
  146. pr_err("Failed to prepare clk '%s': %d\n",
  147. clks[i].id, ret);
  148. goto err;
  149. }
  150. }
  151. return 0;
  152. err:
  153. clk_bulk_unprepare(i, clks);
  154. return ret;
  155. }
  156. EXPORT_SYMBOL_GPL(clk_bulk_prepare);
  157. #endif /* CONFIG_HAVE_CLK_PREPARE */
  158. /**
  159. * clk_bulk_disable - gate a set of clocks
  160. * @num_clks: the number of clk_bulk_data
  161. * @clks: the clk_bulk_data table being gated
  162. *
  163. * clk_bulk_disable must not sleep, which differentiates it from
  164. * clk_bulk_unprepare. clk_bulk_disable must be called before
  165. * clk_bulk_unprepare.
  166. */
  167. void clk_bulk_disable(int num_clks, const struct clk_bulk_data *clks)
  168. {
  169. while (--num_clks >= 0)
  170. clk_disable(clks[num_clks].clk);
  171. }
  172. EXPORT_SYMBOL_GPL(clk_bulk_disable);
  173. /**
  174. * clk_bulk_enable - ungate a set of clocks
  175. * @num_clks: the number of clk_bulk_data
  176. * @clks: the clk_bulk_data table being ungated
  177. *
  178. * clk_bulk_enable must not sleep
  179. * Returns 0 on success, -EERROR otherwise.
  180. */
  181. int __must_check clk_bulk_enable(int num_clks, const struct clk_bulk_data *clks)
  182. {
  183. int ret;
  184. int i;
  185. for (i = 0; i < num_clks; i++) {
  186. ret = clk_enable(clks[i].clk);
  187. if (ret) {
  188. pr_err("Failed to enable clk '%s': %d\n",
  189. clks[i].id, ret);
  190. goto err;
  191. }
  192. }
  193. return 0;
  194. err:
  195. clk_bulk_disable(i, clks);
  196. return ret;
  197. }
  198. EXPORT_SYMBOL_GPL(clk_bulk_enable);