clk.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * Hisilicon clock driver
  3. *
  4. * Copyright (c) 2012-2013 Hisilicon Limited.
  5. * Copyright (c) 2012-2013 Linaro Limited.
  6. *
  7. * Author: Haojian Zhuang <haojian.zhuang@linaro.org>
  8. * Xin Li <li.xin@linaro.org>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License along
  21. * with this program; if not, write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  23. *
  24. */
  25. #include <linux/kernel.h>
  26. #include <linux/clkdev.h>
  27. #include <linux/clk-provider.h>
  28. #include <linux/delay.h>
  29. #include <linux/io.h>
  30. #include <linux/of.h>
  31. #include <linux/of_address.h>
  32. #include <linux/of_device.h>
  33. #include <linux/slab.h>
  34. #include "clk.h"
  35. static DEFINE_SPINLOCK(hisi_clk_lock);
  36. struct hisi_clock_data __init *hisi_clk_init(struct device_node *np,
  37. int nr_clks)
  38. {
  39. struct hisi_clock_data *clk_data;
  40. struct clk **clk_table;
  41. void __iomem *base;
  42. base = of_iomap(np, 0);
  43. if (!base) {
  44. pr_err("%s: failed to map clock registers\n", __func__);
  45. goto err;
  46. }
  47. clk_data = kzalloc(sizeof(*clk_data), GFP_KERNEL);
  48. if (!clk_data) {
  49. pr_err("%s: could not allocate clock data\n", __func__);
  50. goto err;
  51. }
  52. clk_data->base = base;
  53. clk_table = kzalloc(sizeof(struct clk *) * nr_clks, GFP_KERNEL);
  54. if (!clk_table) {
  55. pr_err("%s: could not allocate clock lookup table\n", __func__);
  56. goto err_data;
  57. }
  58. clk_data->clk_data.clks = clk_table;
  59. clk_data->clk_data.clk_num = nr_clks;
  60. of_clk_add_provider(np, of_clk_src_onecell_get, &clk_data->clk_data);
  61. return clk_data;
  62. err_data:
  63. kfree(clk_data);
  64. err:
  65. return NULL;
  66. }
  67. void __init hisi_clk_register_fixed_rate(struct hisi_fixed_rate_clock *clks,
  68. int nums, struct hisi_clock_data *data)
  69. {
  70. struct clk *clk;
  71. int i;
  72. for (i = 0; i < nums; i++) {
  73. clk = clk_register_fixed_rate(NULL, clks[i].name,
  74. clks[i].parent_name,
  75. clks[i].flags,
  76. clks[i].fixed_rate);
  77. if (IS_ERR(clk)) {
  78. pr_err("%s: failed to register clock %s\n",
  79. __func__, clks[i].name);
  80. continue;
  81. }
  82. data->clk_data.clks[clks[i].id] = clk;
  83. }
  84. }
  85. void __init hisi_clk_register_fixed_factor(struct hisi_fixed_factor_clock *clks,
  86. int nums,
  87. struct hisi_clock_data *data)
  88. {
  89. struct clk *clk;
  90. int i;
  91. for (i = 0; i < nums; i++) {
  92. clk = clk_register_fixed_factor(NULL, clks[i].name,
  93. clks[i].parent_name,
  94. clks[i].flags, clks[i].mult,
  95. clks[i].div);
  96. if (IS_ERR(clk)) {
  97. pr_err("%s: failed to register clock %s\n",
  98. __func__, clks[i].name);
  99. continue;
  100. }
  101. data->clk_data.clks[clks[i].id] = clk;
  102. }
  103. }
  104. void __init hisi_clk_register_mux(struct hisi_mux_clock *clks,
  105. int nums, struct hisi_clock_data *data)
  106. {
  107. struct clk *clk;
  108. void __iomem *base = data->base;
  109. int i;
  110. for (i = 0; i < nums; i++) {
  111. u32 mask = BIT(clks[i].width) - 1;
  112. clk = clk_register_mux_table(NULL, clks[i].name,
  113. clks[i].parent_names,
  114. clks[i].num_parents, clks[i].flags,
  115. base + clks[i].offset, clks[i].shift,
  116. mask, clks[i].mux_flags,
  117. clks[i].table, &hisi_clk_lock);
  118. if (IS_ERR(clk)) {
  119. pr_err("%s: failed to register clock %s\n",
  120. __func__, clks[i].name);
  121. continue;
  122. }
  123. if (clks[i].alias)
  124. clk_register_clkdev(clk, clks[i].alias, NULL);
  125. data->clk_data.clks[clks[i].id] = clk;
  126. }
  127. }
  128. void __init hisi_clk_register_divider(struct hisi_divider_clock *clks,
  129. int nums, struct hisi_clock_data *data)
  130. {
  131. struct clk *clk;
  132. void __iomem *base = data->base;
  133. int i;
  134. for (i = 0; i < nums; i++) {
  135. clk = clk_register_divider_table(NULL, clks[i].name,
  136. clks[i].parent_name,
  137. clks[i].flags,
  138. base + clks[i].offset,
  139. clks[i].shift, clks[i].width,
  140. clks[i].div_flags,
  141. clks[i].table,
  142. &hisi_clk_lock);
  143. if (IS_ERR(clk)) {
  144. pr_err("%s: failed to register clock %s\n",
  145. __func__, clks[i].name);
  146. continue;
  147. }
  148. if (clks[i].alias)
  149. clk_register_clkdev(clk, clks[i].alias, NULL);
  150. data->clk_data.clks[clks[i].id] = clk;
  151. }
  152. }
  153. void __init hisi_clk_register_gate(struct hisi_gate_clock *clks,
  154. int nums, struct hisi_clock_data *data)
  155. {
  156. struct clk *clk;
  157. void __iomem *base = data->base;
  158. int i;
  159. for (i = 0; i < nums; i++) {
  160. clk = clk_register_gate(NULL, clks[i].name,
  161. clks[i].parent_name,
  162. clks[i].flags,
  163. base + clks[i].offset,
  164. clks[i].bit_idx,
  165. clks[i].gate_flags,
  166. &hisi_clk_lock);
  167. if (IS_ERR(clk)) {
  168. pr_err("%s: failed to register clock %s\n",
  169. __func__, clks[i].name);
  170. continue;
  171. }
  172. if (clks[i].alias)
  173. clk_register_clkdev(clk, clks[i].alias, NULL);
  174. data->clk_data.clks[clks[i].id] = clk;
  175. }
  176. }
  177. void __init hisi_clk_register_gate_sep(struct hisi_gate_clock *clks,
  178. int nums, struct hisi_clock_data *data)
  179. {
  180. struct clk *clk;
  181. void __iomem *base = data->base;
  182. int i;
  183. for (i = 0; i < nums; i++) {
  184. clk = hisi_register_clkgate_sep(NULL, clks[i].name,
  185. clks[i].parent_name,
  186. clks[i].flags,
  187. base + clks[i].offset,
  188. clks[i].bit_idx,
  189. clks[i].gate_flags,
  190. &hisi_clk_lock);
  191. if (IS_ERR(clk)) {
  192. pr_err("%s: failed to register clock %s\n",
  193. __func__, clks[i].name);
  194. continue;
  195. }
  196. if (clks[i].alias)
  197. clk_register_clkdev(clk, clks[i].alias, NULL);
  198. data->clk_data.clks[clks[i].id] = clk;
  199. }
  200. }
  201. void __init hi6220_clk_register_divider(struct hi6220_divider_clock *clks,
  202. int nums, struct hisi_clock_data *data)
  203. {
  204. struct clk *clk;
  205. void __iomem *base = data->base;
  206. int i;
  207. for (i = 0; i < nums; i++) {
  208. clk = hi6220_register_clkdiv(NULL, clks[i].name,
  209. clks[i].parent_name,
  210. clks[i].flags,
  211. base + clks[i].offset,
  212. clks[i].shift,
  213. clks[i].width,
  214. clks[i].mask_bit,
  215. &hisi_clk_lock);
  216. if (IS_ERR(clk)) {
  217. pr_err("%s: failed to register clock %s\n",
  218. __func__, clks[i].name);
  219. continue;
  220. }
  221. if (clks[i].alias)
  222. clk_register_clkdev(clk, clks[i].alias, NULL);
  223. data->clk_data.clks[clks[i].id] = clk;
  224. }
  225. }