clk-uniphier-core.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2016 Socionext Inc.
  4. * Author: Masahiro Yamada <yamada.masahiro@socionext.com>
  5. */
  6. #include <linux/clk-provider.h>
  7. #include <linux/init.h>
  8. #include <linux/mfd/syscon.h>
  9. #include <linux/of.h>
  10. #include <linux/platform_device.h>
  11. #include "clk-uniphier.h"
  12. static struct clk_hw *uniphier_clk_register(struct device *dev,
  13. struct regmap *regmap,
  14. const struct uniphier_clk_data *data)
  15. {
  16. switch (data->type) {
  17. case UNIPHIER_CLK_TYPE_CPUGEAR:
  18. return uniphier_clk_register_cpugear(dev, regmap, data->name,
  19. &data->data.cpugear);
  20. case UNIPHIER_CLK_TYPE_FIXED_FACTOR:
  21. return uniphier_clk_register_fixed_factor(dev, data->name,
  22. &data->data.factor);
  23. case UNIPHIER_CLK_TYPE_FIXED_RATE:
  24. return uniphier_clk_register_fixed_rate(dev, data->name,
  25. &data->data.rate);
  26. case UNIPHIER_CLK_TYPE_GATE:
  27. return uniphier_clk_register_gate(dev, regmap, data->name,
  28. &data->data.gate);
  29. case UNIPHIER_CLK_TYPE_MUX:
  30. return uniphier_clk_register_mux(dev, regmap, data->name,
  31. &data->data.mux);
  32. default:
  33. dev_err(dev, "unsupported clock type\n");
  34. return ERR_PTR(-EINVAL);
  35. }
  36. }
  37. static int uniphier_clk_probe(struct platform_device *pdev)
  38. {
  39. struct device *dev = &pdev->dev;
  40. struct clk_hw_onecell_data *hw_data;
  41. const struct uniphier_clk_data *p, *data;
  42. struct regmap *regmap;
  43. struct device_node *parent;
  44. int clk_num = 0;
  45. data = of_device_get_match_data(dev);
  46. if (WARN_ON(!data))
  47. return -EINVAL;
  48. parent = of_get_parent(dev->of_node); /* parent should be syscon node */
  49. regmap = syscon_node_to_regmap(parent);
  50. of_node_put(parent);
  51. if (IS_ERR(regmap)) {
  52. dev_err(dev, "failed to get regmap (error %ld)\n",
  53. PTR_ERR(regmap));
  54. return PTR_ERR(regmap);
  55. }
  56. for (p = data; p->name; p++)
  57. clk_num = max(clk_num, p->idx + 1);
  58. hw_data = devm_kzalloc(dev, struct_size(hw_data, hws, clk_num),
  59. GFP_KERNEL);
  60. if (!hw_data)
  61. return -ENOMEM;
  62. hw_data->num = clk_num;
  63. /* avoid returning NULL for unused idx */
  64. while (--clk_num >= 0)
  65. hw_data->hws[clk_num] = ERR_PTR(-EINVAL);
  66. for (p = data; p->name; p++) {
  67. struct clk_hw *hw;
  68. dev_dbg(dev, "register %s (index=%d)\n", p->name, p->idx);
  69. hw = uniphier_clk_register(dev, regmap, p);
  70. if (WARN(IS_ERR(hw), "failed to register %s", p->name))
  71. continue;
  72. if (p->idx >= 0)
  73. hw_data->hws[p->idx] = hw;
  74. }
  75. return devm_of_clk_add_hw_provider(dev, of_clk_hw_onecell_get,
  76. hw_data);
  77. }
  78. static const struct of_device_id uniphier_clk_match[] = {
  79. /* System clock */
  80. {
  81. .compatible = "socionext,uniphier-ld4-clock",
  82. .data = uniphier_ld4_sys_clk_data,
  83. },
  84. {
  85. .compatible = "socionext,uniphier-pro4-clock",
  86. .data = uniphier_pro4_sys_clk_data,
  87. },
  88. {
  89. .compatible = "socionext,uniphier-sld8-clock",
  90. .data = uniphier_sld8_sys_clk_data,
  91. },
  92. {
  93. .compatible = "socionext,uniphier-pro5-clock",
  94. .data = uniphier_pro5_sys_clk_data,
  95. },
  96. {
  97. .compatible = "socionext,uniphier-pxs2-clock",
  98. .data = uniphier_pxs2_sys_clk_data,
  99. },
  100. {
  101. .compatible = "socionext,uniphier-ld11-clock",
  102. .data = uniphier_ld11_sys_clk_data,
  103. },
  104. {
  105. .compatible = "socionext,uniphier-ld20-clock",
  106. .data = uniphier_ld20_sys_clk_data,
  107. },
  108. {
  109. .compatible = "socionext,uniphier-pxs3-clock",
  110. .data = uniphier_pxs3_sys_clk_data,
  111. },
  112. {
  113. .compatible = "socionext,uniphier-nx1-clock",
  114. .data = uniphier_nx1_sys_clk_data,
  115. },
  116. /* Media I/O clock, SD clock */
  117. {
  118. .compatible = "socionext,uniphier-ld4-mio-clock",
  119. .data = uniphier_ld4_mio_clk_data,
  120. },
  121. {
  122. .compatible = "socionext,uniphier-pro4-mio-clock",
  123. .data = uniphier_ld4_mio_clk_data,
  124. },
  125. {
  126. .compatible = "socionext,uniphier-sld8-mio-clock",
  127. .data = uniphier_ld4_mio_clk_data,
  128. },
  129. {
  130. .compatible = "socionext,uniphier-pro5-sd-clock",
  131. .data = uniphier_pro5_sd_clk_data,
  132. },
  133. {
  134. .compatible = "socionext,uniphier-pxs2-sd-clock",
  135. .data = uniphier_pro5_sd_clk_data,
  136. },
  137. {
  138. .compatible = "socionext,uniphier-ld11-mio-clock",
  139. .data = uniphier_ld4_mio_clk_data,
  140. },
  141. {
  142. .compatible = "socionext,uniphier-ld20-sd-clock",
  143. .data = uniphier_pro5_sd_clk_data,
  144. },
  145. {
  146. .compatible = "socionext,uniphier-pxs3-sd-clock",
  147. .data = uniphier_pro5_sd_clk_data,
  148. },
  149. {
  150. .compatible = "socionext,uniphier-nx1-sd-clock",
  151. .data = uniphier_pro5_sd_clk_data,
  152. },
  153. /* Peripheral clock */
  154. {
  155. .compatible = "socionext,uniphier-ld4-peri-clock",
  156. .data = uniphier_ld4_peri_clk_data,
  157. },
  158. {
  159. .compatible = "socionext,uniphier-pro4-peri-clock",
  160. .data = uniphier_pro4_peri_clk_data,
  161. },
  162. {
  163. .compatible = "socionext,uniphier-sld8-peri-clock",
  164. .data = uniphier_ld4_peri_clk_data,
  165. },
  166. {
  167. .compatible = "socionext,uniphier-pro5-peri-clock",
  168. .data = uniphier_pro4_peri_clk_data,
  169. },
  170. {
  171. .compatible = "socionext,uniphier-pxs2-peri-clock",
  172. .data = uniphier_pro4_peri_clk_data,
  173. },
  174. {
  175. .compatible = "socionext,uniphier-ld11-peri-clock",
  176. .data = uniphier_pro4_peri_clk_data,
  177. },
  178. {
  179. .compatible = "socionext,uniphier-ld20-peri-clock",
  180. .data = uniphier_pro4_peri_clk_data,
  181. },
  182. {
  183. .compatible = "socionext,uniphier-pxs3-peri-clock",
  184. .data = uniphier_pro4_peri_clk_data,
  185. },
  186. {
  187. .compatible = "socionext,uniphier-nx1-peri-clock",
  188. .data = uniphier_pro4_peri_clk_data,
  189. },
  190. /* SoC-glue clock */
  191. {
  192. .compatible = "socionext,uniphier-pro4-sg-clock",
  193. .data = uniphier_pro4_sg_clk_data,
  194. },
  195. { /* sentinel */ }
  196. };
  197. static struct platform_driver uniphier_clk_driver = {
  198. .probe = uniphier_clk_probe,
  199. .driver = {
  200. .name = "uniphier-clk",
  201. .of_match_table = uniphier_clk_match,
  202. },
  203. };
  204. builtin_platform_driver(uniphier_clk_driver);