clk.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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/clk-provider.h>
  27. #include <linux/clkdev.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 <linux/clk.h>
  35. #include "clk.h"
  36. static DEFINE_SPINLOCK(hisi_clk_lock);
  37. struct hisi_clock_data __init *hisi_clk_init(struct device_node *np,
  38. int nr_clks)
  39. {
  40. struct hisi_clock_data *clk_data;
  41. struct clk **clk_table;
  42. void __iomem *base;
  43. if (np) {
  44. base = of_iomap(np, 0);
  45. if (!base) {
  46. pr_err("failed to map Hisilicon clock registers\n");
  47. goto err;
  48. }
  49. } else {
  50. pr_err("failed to find Hisilicon clock node in DTS\n");
  51. goto err;
  52. }
  53. clk_data = kzalloc(sizeof(*clk_data), GFP_KERNEL);
  54. if (!clk_data) {
  55. pr_err("%s: could not allocate clock data\n", __func__);
  56. goto err;
  57. }
  58. clk_data->base = base;
  59. clk_table = kzalloc(sizeof(struct clk *) * nr_clks, GFP_KERNEL);
  60. if (!clk_table) {
  61. pr_err("%s: could not allocate clock lookup table\n", __func__);
  62. goto err_data;
  63. }
  64. clk_data->clk_data.clks = clk_table;
  65. clk_data->clk_data.clk_num = nr_clks;
  66. of_clk_add_provider(np, of_clk_src_onecell_get, &clk_data->clk_data);
  67. return clk_data;
  68. err_data:
  69. kfree(clk_data);
  70. err:
  71. return NULL;
  72. }
  73. void __init hisi_clk_register_fixed_rate(struct hisi_fixed_rate_clock *clks,
  74. int nums, struct hisi_clock_data *data)
  75. {
  76. struct clk *clk;
  77. int i;
  78. for (i = 0; i < nums; i++) {
  79. clk = clk_register_fixed_rate(NULL, clks[i].name,
  80. clks[i].parent_name,
  81. clks[i].flags,
  82. clks[i].fixed_rate);
  83. if (IS_ERR(clk)) {
  84. pr_err("%s: failed to register clock %s\n",
  85. __func__, clks[i].name);
  86. continue;
  87. }
  88. data->clk_data.clks[clks[i].id] = clk;
  89. }
  90. }
  91. void __init hisi_clk_register_fixed_factor(struct hisi_fixed_factor_clock *clks,
  92. int nums,
  93. struct hisi_clock_data *data)
  94. {
  95. struct clk *clk;
  96. int i;
  97. for (i = 0; i < nums; i++) {
  98. clk = clk_register_fixed_factor(NULL, clks[i].name,
  99. clks[i].parent_name,
  100. clks[i].flags, clks[i].mult,
  101. clks[i].div);
  102. if (IS_ERR(clk)) {
  103. pr_err("%s: failed to register clock %s\n",
  104. __func__, clks[i].name);
  105. continue;
  106. }
  107. data->clk_data.clks[clks[i].id] = clk;
  108. }
  109. }
  110. void __init hisi_clk_register_mux(struct hisi_mux_clock *clks,
  111. int nums, struct hisi_clock_data *data)
  112. {
  113. struct clk *clk;
  114. void __iomem *base = data->base;
  115. int i;
  116. for (i = 0; i < nums; i++) {
  117. u32 mask = BIT(clks[i].width) - 1;
  118. clk = clk_register_mux_table(NULL, clks[i].name,
  119. clks[i].parent_names,
  120. clks[i].num_parents, clks[i].flags,
  121. base + clks[i].offset, clks[i].shift,
  122. mask, clks[i].mux_flags,
  123. clks[i].table, &hisi_clk_lock);
  124. if (IS_ERR(clk)) {
  125. pr_err("%s: failed to register clock %s\n",
  126. __func__, clks[i].name);
  127. continue;
  128. }
  129. if (clks[i].alias)
  130. clk_register_clkdev(clk, clks[i].alias, NULL);
  131. data->clk_data.clks[clks[i].id] = clk;
  132. }
  133. }
  134. void __init hisi_clk_register_divider(struct hisi_divider_clock *clks,
  135. int nums, struct hisi_clock_data *data)
  136. {
  137. struct clk *clk;
  138. void __iomem *base = data->base;
  139. int i;
  140. for (i = 0; i < nums; i++) {
  141. clk = clk_register_divider_table(NULL, clks[i].name,
  142. clks[i].parent_name,
  143. clks[i].flags,
  144. base + clks[i].offset,
  145. clks[i].shift, clks[i].width,
  146. clks[i].div_flags,
  147. clks[i].table,
  148. &hisi_clk_lock);
  149. if (IS_ERR(clk)) {
  150. pr_err("%s: failed to register clock %s\n",
  151. __func__, clks[i].name);
  152. continue;
  153. }
  154. if (clks[i].alias)
  155. clk_register_clkdev(clk, clks[i].alias, NULL);
  156. data->clk_data.clks[clks[i].id] = clk;
  157. }
  158. }
  159. void __init hisi_clk_register_gate(struct hisi_gate_clock *clks,
  160. int nums, struct hisi_clock_data *data)
  161. {
  162. struct clk *clk;
  163. void __iomem *base = data->base;
  164. int i;
  165. for (i = 0; i < nums; i++) {
  166. clk = clk_register_gate(NULL, clks[i].name,
  167. clks[i].parent_name,
  168. clks[i].flags,
  169. base + clks[i].offset,
  170. clks[i].bit_idx,
  171. clks[i].gate_flags,
  172. &hisi_clk_lock);
  173. if (IS_ERR(clk)) {
  174. pr_err("%s: failed to register clock %s\n",
  175. __func__, clks[i].name);
  176. continue;
  177. }
  178. if (clks[i].alias)
  179. clk_register_clkdev(clk, clks[i].alias, NULL);
  180. data->clk_data.clks[clks[i].id] = clk;
  181. }
  182. }
  183. void __init hisi_clk_register_gate_sep(struct hisi_gate_clock *clks,
  184. int nums, struct hisi_clock_data *data)
  185. {
  186. struct clk *clk;
  187. void __iomem *base = data->base;
  188. int i;
  189. for (i = 0; i < nums; i++) {
  190. clk = hisi_register_clkgate_sep(NULL, clks[i].name,
  191. clks[i].parent_name,
  192. clks[i].flags,
  193. base + clks[i].offset,
  194. clks[i].bit_idx,
  195. clks[i].gate_flags,
  196. &hisi_clk_lock);
  197. if (IS_ERR(clk)) {
  198. pr_err("%s: failed to register clock %s\n",
  199. __func__, clks[i].name);
  200. continue;
  201. }
  202. if (clks[i].alias)
  203. clk_register_clkdev(clk, clks[i].alias, NULL);
  204. data->clk_data.clks[clks[i].id] = clk;
  205. }
  206. }