clk-uniphier-core.c 5.5 KB

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