ccu-sun8i-de2.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * Copyright (c) 2017 Icenowy Zheng <icenowy@aosc.io>
  3. *
  4. * This software is licensed under the terms of the GNU General Public
  5. * License version 2, as published by the Free Software Foundation, and
  6. * may be copied, distributed, and modified under those terms.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/clk.h>
  14. #include <linux/clk-provider.h>
  15. #include <linux/of_address.h>
  16. #include <linux/of_platform.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/reset.h>
  19. #include "ccu_common.h"
  20. #include "ccu_div.h"
  21. #include "ccu_gate.h"
  22. #include "ccu_reset.h"
  23. #include "ccu-sun8i-de2.h"
  24. static SUNXI_CCU_GATE(bus_mixer0_clk, "bus-mixer0", "bus-de",
  25. 0x04, BIT(0), 0);
  26. static SUNXI_CCU_GATE(bus_mixer1_clk, "bus-mixer1", "bus-de",
  27. 0x04, BIT(1), 0);
  28. static SUNXI_CCU_GATE(bus_wb_clk, "bus-wb", "bus-de",
  29. 0x04, BIT(2), 0);
  30. static SUNXI_CCU_GATE(mixer0_clk, "mixer0", "mixer0-div",
  31. 0x00, BIT(0), CLK_SET_RATE_PARENT);
  32. static SUNXI_CCU_GATE(mixer1_clk, "mixer1", "mixer1-div",
  33. 0x00, BIT(1), CLK_SET_RATE_PARENT);
  34. static SUNXI_CCU_GATE(wb_clk, "wb", "wb-div",
  35. 0x00, BIT(2), CLK_SET_RATE_PARENT);
  36. static SUNXI_CCU_M(mixer0_div_clk, "mixer0-div", "de", 0x0c, 0, 4,
  37. CLK_SET_RATE_PARENT);
  38. static SUNXI_CCU_M(mixer1_div_clk, "mixer1-div", "de", 0x0c, 4, 4,
  39. CLK_SET_RATE_PARENT);
  40. static SUNXI_CCU_M(wb_div_clk, "wb-div", "de", 0x0c, 8, 4,
  41. CLK_SET_RATE_PARENT);
  42. static SUNXI_CCU_M(mixer0_div_a83_clk, "mixer0-div", "pll-de", 0x0c, 0, 4,
  43. CLK_SET_RATE_PARENT);
  44. static SUNXI_CCU_M(mixer1_div_a83_clk, "mixer1-div", "pll-de", 0x0c, 4, 4,
  45. CLK_SET_RATE_PARENT);
  46. static SUNXI_CCU_M(wb_div_a83_clk, "wb-div", "pll-de", 0x0c, 8, 4,
  47. CLK_SET_RATE_PARENT);
  48. static struct ccu_common *sun8i_a83t_de2_clks[] = {
  49. &mixer0_clk.common,
  50. &mixer1_clk.common,
  51. &wb_clk.common,
  52. &bus_mixer0_clk.common,
  53. &bus_mixer1_clk.common,
  54. &bus_wb_clk.common,
  55. &mixer0_div_a83_clk.common,
  56. &mixer1_div_a83_clk.common,
  57. &wb_div_a83_clk.common,
  58. };
  59. static struct ccu_common *sun8i_h3_de2_clks[] = {
  60. &mixer0_clk.common,
  61. &mixer1_clk.common,
  62. &wb_clk.common,
  63. &bus_mixer0_clk.common,
  64. &bus_mixer1_clk.common,
  65. &bus_wb_clk.common,
  66. &mixer0_div_clk.common,
  67. &mixer1_div_clk.common,
  68. &wb_div_clk.common,
  69. };
  70. static struct ccu_common *sun8i_v3s_de2_clks[] = {
  71. &mixer0_clk.common,
  72. &wb_clk.common,
  73. &bus_mixer0_clk.common,
  74. &bus_wb_clk.common,
  75. &mixer0_div_clk.common,
  76. &wb_div_clk.common,
  77. };
  78. static struct clk_hw_onecell_data sun8i_a83t_de2_hw_clks = {
  79. .hws = {
  80. [CLK_MIXER0] = &mixer0_clk.common.hw,
  81. [CLK_MIXER1] = &mixer1_clk.common.hw,
  82. [CLK_WB] = &wb_clk.common.hw,
  83. [CLK_BUS_MIXER0] = &bus_mixer0_clk.common.hw,
  84. [CLK_BUS_MIXER1] = &bus_mixer1_clk.common.hw,
  85. [CLK_BUS_WB] = &bus_wb_clk.common.hw,
  86. [CLK_MIXER0_DIV] = &mixer0_div_a83_clk.common.hw,
  87. [CLK_MIXER1_DIV] = &mixer1_div_a83_clk.common.hw,
  88. [CLK_WB_DIV] = &wb_div_a83_clk.common.hw,
  89. },
  90. .num = CLK_NUMBER,
  91. };
  92. static struct clk_hw_onecell_data sun8i_h3_de2_hw_clks = {
  93. .hws = {
  94. [CLK_MIXER0] = &mixer0_clk.common.hw,
  95. [CLK_MIXER1] = &mixer1_clk.common.hw,
  96. [CLK_WB] = &wb_clk.common.hw,
  97. [CLK_BUS_MIXER0] = &bus_mixer0_clk.common.hw,
  98. [CLK_BUS_MIXER1] = &bus_mixer1_clk.common.hw,
  99. [CLK_BUS_WB] = &bus_wb_clk.common.hw,
  100. [CLK_MIXER0_DIV] = &mixer0_div_clk.common.hw,
  101. [CLK_MIXER1_DIV] = &mixer1_div_clk.common.hw,
  102. [CLK_WB_DIV] = &wb_div_clk.common.hw,
  103. },
  104. .num = CLK_NUMBER,
  105. };
  106. static struct clk_hw_onecell_data sun8i_v3s_de2_hw_clks = {
  107. .hws = {
  108. [CLK_MIXER0] = &mixer0_clk.common.hw,
  109. [CLK_WB] = &wb_clk.common.hw,
  110. [CLK_BUS_MIXER0] = &bus_mixer0_clk.common.hw,
  111. [CLK_BUS_WB] = &bus_wb_clk.common.hw,
  112. [CLK_MIXER0_DIV] = &mixer0_div_clk.common.hw,
  113. [CLK_WB_DIV] = &wb_div_clk.common.hw,
  114. },
  115. .num = CLK_NUMBER,
  116. };
  117. static struct ccu_reset_map sun8i_a83t_de2_resets[] = {
  118. [RST_MIXER0] = { 0x08, BIT(0) },
  119. /*
  120. * For A83T, H3 and R40, mixer1 reset line is shared with wb, so
  121. * only RST_WB is exported here.
  122. * For V3s there's just no mixer1, so it also shares this struct.
  123. */
  124. [RST_WB] = { 0x08, BIT(2) },
  125. };
  126. static struct ccu_reset_map sun50i_a64_de2_resets[] = {
  127. [RST_MIXER0] = { 0x08, BIT(0) },
  128. [RST_MIXER1] = { 0x08, BIT(1) },
  129. [RST_WB] = { 0x08, BIT(2) },
  130. };
  131. static const struct sunxi_ccu_desc sun8i_a83t_de2_clk_desc = {
  132. .ccu_clks = sun8i_a83t_de2_clks,
  133. .num_ccu_clks = ARRAY_SIZE(sun8i_a83t_de2_clks),
  134. .hw_clks = &sun8i_a83t_de2_hw_clks,
  135. .resets = sun8i_a83t_de2_resets,
  136. .num_resets = ARRAY_SIZE(sun8i_a83t_de2_resets),
  137. };
  138. static const struct sunxi_ccu_desc sun8i_h3_de2_clk_desc = {
  139. .ccu_clks = sun8i_h3_de2_clks,
  140. .num_ccu_clks = ARRAY_SIZE(sun8i_h3_de2_clks),
  141. .hw_clks = &sun8i_h3_de2_hw_clks,
  142. .resets = sun8i_a83t_de2_resets,
  143. .num_resets = ARRAY_SIZE(sun8i_a83t_de2_resets),
  144. };
  145. static const struct sunxi_ccu_desc sun50i_a64_de2_clk_desc = {
  146. .ccu_clks = sun8i_h3_de2_clks,
  147. .num_ccu_clks = ARRAY_SIZE(sun8i_h3_de2_clks),
  148. .hw_clks = &sun8i_h3_de2_hw_clks,
  149. .resets = sun50i_a64_de2_resets,
  150. .num_resets = ARRAY_SIZE(sun50i_a64_de2_resets),
  151. };
  152. static const struct sunxi_ccu_desc sun8i_v3s_de2_clk_desc = {
  153. .ccu_clks = sun8i_v3s_de2_clks,
  154. .num_ccu_clks = ARRAY_SIZE(sun8i_v3s_de2_clks),
  155. .hw_clks = &sun8i_v3s_de2_hw_clks,
  156. .resets = sun8i_a83t_de2_resets,
  157. .num_resets = ARRAY_SIZE(sun8i_a83t_de2_resets),
  158. };
  159. static int sunxi_de2_clk_probe(struct platform_device *pdev)
  160. {
  161. struct resource *res;
  162. struct clk *bus_clk, *mod_clk;
  163. struct reset_control *rstc;
  164. void __iomem *reg;
  165. const struct sunxi_ccu_desc *ccu_desc;
  166. int ret;
  167. ccu_desc = of_device_get_match_data(&pdev->dev);
  168. if (!ccu_desc)
  169. return -EINVAL;
  170. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  171. reg = devm_ioremap_resource(&pdev->dev, res);
  172. if (IS_ERR(reg))
  173. return PTR_ERR(reg);
  174. bus_clk = devm_clk_get(&pdev->dev, "bus");
  175. if (IS_ERR(bus_clk)) {
  176. ret = PTR_ERR(bus_clk);
  177. if (ret != -EPROBE_DEFER)
  178. dev_err(&pdev->dev, "Couldn't get bus clk: %d\n", ret);
  179. return ret;
  180. }
  181. mod_clk = devm_clk_get(&pdev->dev, "mod");
  182. if (IS_ERR(mod_clk)) {
  183. ret = PTR_ERR(mod_clk);
  184. if (ret != -EPROBE_DEFER)
  185. dev_err(&pdev->dev, "Couldn't get mod clk: %d\n", ret);
  186. return ret;
  187. }
  188. rstc = devm_reset_control_get_exclusive(&pdev->dev, NULL);
  189. if (IS_ERR(rstc)) {
  190. ret = PTR_ERR(rstc);
  191. if (ret != -EPROBE_DEFER)
  192. dev_err(&pdev->dev,
  193. "Couldn't get reset control: %d\n", ret);
  194. return ret;
  195. }
  196. /* The clocks need to be enabled for us to access the registers */
  197. ret = clk_prepare_enable(bus_clk);
  198. if (ret) {
  199. dev_err(&pdev->dev, "Couldn't enable bus clk: %d\n", ret);
  200. return ret;
  201. }
  202. ret = clk_prepare_enable(mod_clk);
  203. if (ret) {
  204. dev_err(&pdev->dev, "Couldn't enable mod clk: %d\n", ret);
  205. goto err_disable_bus_clk;
  206. }
  207. /* The reset control needs to be asserted for the controls to work */
  208. ret = reset_control_deassert(rstc);
  209. if (ret) {
  210. dev_err(&pdev->dev,
  211. "Couldn't deassert reset control: %d\n", ret);
  212. goto err_disable_mod_clk;
  213. }
  214. ret = sunxi_ccu_probe(pdev->dev.of_node, reg, ccu_desc);
  215. if (ret)
  216. goto err_assert_reset;
  217. return 0;
  218. err_assert_reset:
  219. reset_control_assert(rstc);
  220. err_disable_mod_clk:
  221. clk_disable_unprepare(mod_clk);
  222. err_disable_bus_clk:
  223. clk_disable_unprepare(bus_clk);
  224. return ret;
  225. }
  226. static const struct of_device_id sunxi_de2_clk_ids[] = {
  227. {
  228. .compatible = "allwinner,sun8i-a83t-de2-clk",
  229. .data = &sun8i_a83t_de2_clk_desc,
  230. },
  231. {
  232. .compatible = "allwinner,sun8i-h3-de2-clk",
  233. .data = &sun8i_h3_de2_clk_desc,
  234. },
  235. {
  236. .compatible = "allwinner,sun8i-v3s-de2-clk",
  237. .data = &sun8i_v3s_de2_clk_desc,
  238. },
  239. {
  240. .compatible = "allwinner,sun50i-a64-de2-clk",
  241. .data = &sun50i_a64_de2_clk_desc,
  242. },
  243. {
  244. .compatible = "allwinner,sun50i-h5-de2-clk",
  245. .data = &sun50i_a64_de2_clk_desc,
  246. },
  247. { }
  248. };
  249. static struct platform_driver sunxi_de2_clk_driver = {
  250. .probe = sunxi_de2_clk_probe,
  251. .driver = {
  252. .name = "sunxi-de2-clks",
  253. .of_match_table = sunxi_de2_clk_ids,
  254. },
  255. };
  256. builtin_platform_driver(sunxi_de2_clk_driver);