clk-hi3519.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Hi3519 Clock Driver
  4. *
  5. * Copyright (c) 2015-2016 HiSilicon Technologies Co., Ltd.
  6. */
  7. #include <dt-bindings/clock/hi3519-clock.h>
  8. #include <linux/clk-provider.h>
  9. #include <linux/module.h>
  10. #include <linux/platform_device.h>
  11. #include "clk.h"
  12. #include "reset.h"
  13. #define HI3519_INNER_CLK_OFFSET 64
  14. #define HI3519_FIXED_24M 65
  15. #define HI3519_FIXED_50M 66
  16. #define HI3519_FIXED_75M 67
  17. #define HI3519_FIXED_125M 68
  18. #define HI3519_FIXED_150M 69
  19. #define HI3519_FIXED_200M 70
  20. #define HI3519_FIXED_250M 71
  21. #define HI3519_FIXED_300M 72
  22. #define HI3519_FIXED_400M 73
  23. #define HI3519_FMC_MUX 74
  24. #define HI3519_NR_CLKS 128
  25. struct hi3519_crg_data {
  26. struct hisi_clock_data *clk_data;
  27. struct hisi_reset_controller *rstc;
  28. };
  29. static const struct hisi_fixed_rate_clock hi3519_fixed_rate_clks[] = {
  30. { HI3519_FIXED_24M, "24m", NULL, 0, 24000000, },
  31. { HI3519_FIXED_50M, "50m", NULL, 0, 50000000, },
  32. { HI3519_FIXED_75M, "75m", NULL, 0, 75000000, },
  33. { HI3519_FIXED_125M, "125m", NULL, 0, 125000000, },
  34. { HI3519_FIXED_150M, "150m", NULL, 0, 150000000, },
  35. { HI3519_FIXED_200M, "200m", NULL, 0, 200000000, },
  36. { HI3519_FIXED_250M, "250m", NULL, 0, 250000000, },
  37. { HI3519_FIXED_300M, "300m", NULL, 0, 300000000, },
  38. { HI3519_FIXED_400M, "400m", NULL, 0, 400000000, },
  39. };
  40. static const char *const fmc_mux_p[] = {
  41. "24m", "75m", "125m", "150m", "200m", "250m", "300m", "400m", };
  42. static u32 fmc_mux_table[] = {0, 1, 2, 3, 4, 5, 6, 7};
  43. static const struct hisi_mux_clock hi3519_mux_clks[] = {
  44. { HI3519_FMC_MUX, "fmc_mux", fmc_mux_p, ARRAY_SIZE(fmc_mux_p),
  45. CLK_SET_RATE_PARENT, 0xc0, 2, 3, 0, fmc_mux_table, },
  46. };
  47. static const struct hisi_gate_clock hi3519_gate_clks[] = {
  48. { HI3519_FMC_CLK, "clk_fmc", "fmc_mux",
  49. CLK_SET_RATE_PARENT, 0xc0, 1, 0, },
  50. { HI3519_UART0_CLK, "clk_uart0", "24m",
  51. CLK_SET_RATE_PARENT, 0xe4, 20, 0, },
  52. { HI3519_UART1_CLK, "clk_uart1", "24m",
  53. CLK_SET_RATE_PARENT, 0xe4, 21, 0, },
  54. { HI3519_UART2_CLK, "clk_uart2", "24m",
  55. CLK_SET_RATE_PARENT, 0xe4, 22, 0, },
  56. { HI3519_UART3_CLK, "clk_uart3", "24m",
  57. CLK_SET_RATE_PARENT, 0xe4, 23, 0, },
  58. { HI3519_UART4_CLK, "clk_uart4", "24m",
  59. CLK_SET_RATE_PARENT, 0xe4, 24, 0, },
  60. { HI3519_SPI0_CLK, "clk_spi0", "50m",
  61. CLK_SET_RATE_PARENT, 0xe4, 16, 0, },
  62. { HI3519_SPI1_CLK, "clk_spi1", "50m",
  63. CLK_SET_RATE_PARENT, 0xe4, 17, 0, },
  64. { HI3519_SPI2_CLK, "clk_spi2", "50m",
  65. CLK_SET_RATE_PARENT, 0xe4, 18, 0, },
  66. };
  67. static struct hisi_clock_data *hi3519_clk_register(struct platform_device *pdev)
  68. {
  69. struct hisi_clock_data *clk_data;
  70. int ret;
  71. clk_data = hisi_clk_alloc(pdev, HI3519_NR_CLKS);
  72. if (!clk_data)
  73. return ERR_PTR(-ENOMEM);
  74. ret = hisi_clk_register_fixed_rate(hi3519_fixed_rate_clks,
  75. ARRAY_SIZE(hi3519_fixed_rate_clks),
  76. clk_data);
  77. if (ret)
  78. return ERR_PTR(ret);
  79. ret = hisi_clk_register_mux(hi3519_mux_clks,
  80. ARRAY_SIZE(hi3519_mux_clks),
  81. clk_data);
  82. if (ret)
  83. goto unregister_fixed_rate;
  84. ret = hisi_clk_register_gate(hi3519_gate_clks,
  85. ARRAY_SIZE(hi3519_gate_clks),
  86. clk_data);
  87. if (ret)
  88. goto unregister_mux;
  89. ret = of_clk_add_provider(pdev->dev.of_node,
  90. of_clk_src_onecell_get, &clk_data->clk_data);
  91. if (ret)
  92. goto unregister_gate;
  93. return clk_data;
  94. unregister_fixed_rate:
  95. hisi_clk_unregister_fixed_rate(hi3519_fixed_rate_clks,
  96. ARRAY_SIZE(hi3519_fixed_rate_clks),
  97. clk_data);
  98. unregister_mux:
  99. hisi_clk_unregister_mux(hi3519_mux_clks,
  100. ARRAY_SIZE(hi3519_mux_clks),
  101. clk_data);
  102. unregister_gate:
  103. hisi_clk_unregister_gate(hi3519_gate_clks,
  104. ARRAY_SIZE(hi3519_gate_clks),
  105. clk_data);
  106. return ERR_PTR(ret);
  107. }
  108. static void hi3519_clk_unregister(struct platform_device *pdev)
  109. {
  110. struct hi3519_crg_data *crg = platform_get_drvdata(pdev);
  111. of_clk_del_provider(pdev->dev.of_node);
  112. hisi_clk_unregister_gate(hi3519_gate_clks,
  113. ARRAY_SIZE(hi3519_mux_clks),
  114. crg->clk_data);
  115. hisi_clk_unregister_mux(hi3519_mux_clks,
  116. ARRAY_SIZE(hi3519_mux_clks),
  117. crg->clk_data);
  118. hisi_clk_unregister_fixed_rate(hi3519_fixed_rate_clks,
  119. ARRAY_SIZE(hi3519_fixed_rate_clks),
  120. crg->clk_data);
  121. }
  122. static int hi3519_clk_probe(struct platform_device *pdev)
  123. {
  124. struct hi3519_crg_data *crg;
  125. crg = devm_kmalloc(&pdev->dev, sizeof(*crg), GFP_KERNEL);
  126. if (!crg)
  127. return -ENOMEM;
  128. crg->rstc = hisi_reset_init(pdev);
  129. if (!crg->rstc)
  130. return -ENOMEM;
  131. crg->clk_data = hi3519_clk_register(pdev);
  132. if (IS_ERR(crg->clk_data)) {
  133. hisi_reset_exit(crg->rstc);
  134. return PTR_ERR(crg->clk_data);
  135. }
  136. platform_set_drvdata(pdev, crg);
  137. return 0;
  138. }
  139. static int hi3519_clk_remove(struct platform_device *pdev)
  140. {
  141. struct hi3519_crg_data *crg = platform_get_drvdata(pdev);
  142. hisi_reset_exit(crg->rstc);
  143. hi3519_clk_unregister(pdev);
  144. return 0;
  145. }
  146. static const struct of_device_id hi3519_clk_match_table[] = {
  147. { .compatible = "hisilicon,hi3519-crg" },
  148. { }
  149. };
  150. MODULE_DEVICE_TABLE(of, hi3519_clk_match_table);
  151. static struct platform_driver hi3519_clk_driver = {
  152. .probe = hi3519_clk_probe,
  153. .remove = hi3519_clk_remove,
  154. .driver = {
  155. .name = "hi3519-clk",
  156. .of_match_table = hi3519_clk_match_table,
  157. },
  158. };
  159. static int __init hi3519_clk_init(void)
  160. {
  161. return platform_driver_register(&hi3519_clk_driver);
  162. }
  163. core_initcall(hi3519_clk_init);
  164. static void __exit hi3519_clk_exit(void)
  165. {
  166. platform_driver_unregister(&hi3519_clk_driver);
  167. }
  168. module_exit(hi3519_clk_exit);
  169. MODULE_LICENSE("GPL v2");
  170. MODULE_DESCRIPTION("HiSilicon Hi3519 Clock Driver");