clk-hi3519.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Hi3519 Clock Driver
  3. *
  4. * Copyright (c) 2015-2016 HiSilicon Technologies Co., Ltd.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <dt-bindings/clock/hi3519-clock.h>
  20. #include <linux/clk-provider.h>
  21. #include <linux/module.h>
  22. #include <linux/platform_device.h>
  23. #include "clk.h"
  24. #include "reset.h"
  25. #define HI3519_INNER_CLK_OFFSET 64
  26. #define HI3519_FIXED_24M 65
  27. #define HI3519_FIXED_50M 66
  28. #define HI3519_FIXED_75M 67
  29. #define HI3519_FIXED_125M 68
  30. #define HI3519_FIXED_150M 69
  31. #define HI3519_FIXED_200M 70
  32. #define HI3519_FIXED_250M 71
  33. #define HI3519_FIXED_300M 72
  34. #define HI3519_FIXED_400M 73
  35. #define HI3519_FMC_MUX 74
  36. #define HI3519_NR_CLKS 128
  37. struct hi3519_crg_data {
  38. struct hisi_clock_data *clk_data;
  39. struct hisi_reset_controller *rstc;
  40. };
  41. static const struct hisi_fixed_rate_clock hi3519_fixed_rate_clks[] = {
  42. { HI3519_FIXED_24M, "24m", NULL, 0, 24000000, },
  43. { HI3519_FIXED_50M, "50m", NULL, 0, 50000000, },
  44. { HI3519_FIXED_75M, "75m", NULL, 0, 75000000, },
  45. { HI3519_FIXED_125M, "125m", NULL, 0, 125000000, },
  46. { HI3519_FIXED_150M, "150m", NULL, 0, 150000000, },
  47. { HI3519_FIXED_200M, "200m", NULL, 0, 200000000, },
  48. { HI3519_FIXED_250M, "250m", NULL, 0, 250000000, },
  49. { HI3519_FIXED_300M, "300m", NULL, 0, 300000000, },
  50. { HI3519_FIXED_400M, "400m", NULL, 0, 400000000, },
  51. };
  52. static const char *const fmc_mux_p[] = {
  53. "24m", "75m", "125m", "150m", "200m", "250m", "300m", "400m", };
  54. static u32 fmc_mux_table[] = {0, 1, 2, 3, 4, 5, 6, 7};
  55. static const struct hisi_mux_clock hi3519_mux_clks[] = {
  56. { HI3519_FMC_MUX, "fmc_mux", fmc_mux_p, ARRAY_SIZE(fmc_mux_p),
  57. CLK_SET_RATE_PARENT, 0xc0, 2, 3, 0, fmc_mux_table, },
  58. };
  59. static const struct hisi_gate_clock hi3519_gate_clks[] = {
  60. { HI3519_FMC_CLK, "clk_fmc", "fmc_mux",
  61. CLK_SET_RATE_PARENT, 0xc0, 1, 0, },
  62. { HI3519_UART0_CLK, "clk_uart0", "24m",
  63. CLK_SET_RATE_PARENT, 0xe4, 20, 0, },
  64. { HI3519_UART1_CLK, "clk_uart1", "24m",
  65. CLK_SET_RATE_PARENT, 0xe4, 21, 0, },
  66. { HI3519_UART2_CLK, "clk_uart2", "24m",
  67. CLK_SET_RATE_PARENT, 0xe4, 22, 0, },
  68. { HI3519_UART3_CLK, "clk_uart3", "24m",
  69. CLK_SET_RATE_PARENT, 0xe4, 23, 0, },
  70. { HI3519_UART4_CLK, "clk_uart4", "24m",
  71. CLK_SET_RATE_PARENT, 0xe4, 24, 0, },
  72. { HI3519_SPI0_CLK, "clk_spi0", "50m",
  73. CLK_SET_RATE_PARENT, 0xe4, 16, 0, },
  74. { HI3519_SPI1_CLK, "clk_spi1", "50m",
  75. CLK_SET_RATE_PARENT, 0xe4, 17, 0, },
  76. { HI3519_SPI2_CLK, "clk_spi2", "50m",
  77. CLK_SET_RATE_PARENT, 0xe4, 18, 0, },
  78. };
  79. static struct hisi_clock_data *hi3519_clk_register(struct platform_device *pdev)
  80. {
  81. struct hisi_clock_data *clk_data;
  82. int ret;
  83. clk_data = hisi_clk_alloc(pdev, HI3519_NR_CLKS);
  84. if (!clk_data)
  85. return ERR_PTR(-ENOMEM);
  86. ret = hisi_clk_register_fixed_rate(hi3519_fixed_rate_clks,
  87. ARRAY_SIZE(hi3519_fixed_rate_clks),
  88. clk_data);
  89. if (ret)
  90. return ERR_PTR(ret);
  91. ret = hisi_clk_register_mux(hi3519_mux_clks,
  92. ARRAY_SIZE(hi3519_mux_clks),
  93. clk_data);
  94. if (ret)
  95. goto unregister_fixed_rate;
  96. ret = hisi_clk_register_gate(hi3519_gate_clks,
  97. ARRAY_SIZE(hi3519_gate_clks),
  98. clk_data);
  99. if (ret)
  100. goto unregister_mux;
  101. ret = of_clk_add_provider(pdev->dev.of_node,
  102. of_clk_src_onecell_get, &clk_data->clk_data);
  103. if (ret)
  104. goto unregister_gate;
  105. return clk_data;
  106. unregister_fixed_rate:
  107. hisi_clk_unregister_fixed_rate(hi3519_fixed_rate_clks,
  108. ARRAY_SIZE(hi3519_fixed_rate_clks),
  109. clk_data);
  110. unregister_mux:
  111. hisi_clk_unregister_mux(hi3519_mux_clks,
  112. ARRAY_SIZE(hi3519_mux_clks),
  113. clk_data);
  114. unregister_gate:
  115. hisi_clk_unregister_gate(hi3519_gate_clks,
  116. ARRAY_SIZE(hi3519_gate_clks),
  117. clk_data);
  118. return ERR_PTR(ret);
  119. }
  120. static void hi3519_clk_unregister(struct platform_device *pdev)
  121. {
  122. struct hi3519_crg_data *crg = platform_get_drvdata(pdev);
  123. of_clk_del_provider(pdev->dev.of_node);
  124. hisi_clk_unregister_gate(hi3519_gate_clks,
  125. ARRAY_SIZE(hi3519_mux_clks),
  126. crg->clk_data);
  127. hisi_clk_unregister_mux(hi3519_mux_clks,
  128. ARRAY_SIZE(hi3519_mux_clks),
  129. crg->clk_data);
  130. hisi_clk_unregister_fixed_rate(hi3519_fixed_rate_clks,
  131. ARRAY_SIZE(hi3519_fixed_rate_clks),
  132. crg->clk_data);
  133. }
  134. static int hi3519_clk_probe(struct platform_device *pdev)
  135. {
  136. struct hi3519_crg_data *crg;
  137. crg = devm_kmalloc(&pdev->dev, sizeof(*crg), GFP_KERNEL);
  138. if (!crg)
  139. return -ENOMEM;
  140. crg->rstc = hisi_reset_init(pdev);
  141. if (!crg->rstc)
  142. return -ENOMEM;
  143. crg->clk_data = hi3519_clk_register(pdev);
  144. if (IS_ERR(crg->clk_data)) {
  145. hisi_reset_exit(crg->rstc);
  146. return PTR_ERR(crg->clk_data);
  147. }
  148. platform_set_drvdata(pdev, crg);
  149. return 0;
  150. }
  151. static int hi3519_clk_remove(struct platform_device *pdev)
  152. {
  153. struct hi3519_crg_data *crg = platform_get_drvdata(pdev);
  154. hisi_reset_exit(crg->rstc);
  155. hi3519_clk_unregister(pdev);
  156. return 0;
  157. }
  158. static const struct of_device_id hi3519_clk_match_table[] = {
  159. { .compatible = "hisilicon,hi3519-crg" },
  160. { }
  161. };
  162. MODULE_DEVICE_TABLE(of, hi3519_clk_match_table);
  163. static struct platform_driver hi3519_clk_driver = {
  164. .probe = hi3519_clk_probe,
  165. .remove = hi3519_clk_remove,
  166. .driver = {
  167. .name = "hi3519-clk",
  168. .of_match_table = hi3519_clk_match_table,
  169. },
  170. };
  171. static int __init hi3519_clk_init(void)
  172. {
  173. return platform_driver_register(&hi3519_clk_driver);
  174. }
  175. core_initcall(hi3519_clk_init);
  176. static void __exit hi3519_clk_exit(void)
  177. {
  178. platform_driver_unregister(&hi3519_clk_driver);
  179. }
  180. module_exit(hi3519_clk_exit);
  181. MODULE_LICENSE("GPL v2");
  182. MODULE_DESCRIPTION("HiSilicon Hi3519 Clock Driver");