clk-mt7986-infracfg.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2021 MediaTek Inc.
  4. * Author: Sam Shih <[email protected]>
  5. * Author: Wenzhen Yu <[email protected]>
  6. */
  7. #include <linux/clk-provider.h>
  8. #include <linux/of.h>
  9. #include <linux/of_address.h>
  10. #include <linux/of_device.h>
  11. #include <linux/platform_device.h>
  12. #include "clk-mtk.h"
  13. #include "clk-gate.h"
  14. #include "clk-mux.h"
  15. #include <dt-bindings/clock/mt7986-clk.h>
  16. #include <linux/clk.h>
  17. static DEFINE_SPINLOCK(mt7986_clk_lock);
  18. static const struct mtk_fixed_factor infra_divs[] = {
  19. FACTOR(CLK_INFRA_SYSAXI_D2, "infra_sysaxi_d2", "sysaxi_sel", 1, 2),
  20. };
  21. static const char *const infra_uart_parent[] __initconst = { "csw_f26m_sel",
  22. "uart_sel" };
  23. static const char *const infra_spi_parents[] __initconst = { "i2c_sel",
  24. "spi_sel" };
  25. static const char *const infra_pwm_bsel_parents[] __initconst = {
  26. "top_rtc_32p7k", "csw_f26m_sel", "infra_sysaxi_d2", "pwm_sel"
  27. };
  28. static const char *const infra_pcie_parents[] __initconst = {
  29. "top_rtc_32p7k", "csw_f26m_sel", "top_xtal", "pextp_tl_ck_sel"
  30. };
  31. static const struct mtk_mux infra_muxes[] = {
  32. /* MODULE_CLK_SEL_0 */
  33. MUX_GATE_CLR_SET_UPD(CLK_INFRA_UART0_SEL, "infra_uart0_sel",
  34. infra_uart_parent, 0x0018, 0x0010, 0x0014, 0, 1,
  35. -1, -1, -1),
  36. MUX_GATE_CLR_SET_UPD(CLK_INFRA_UART1_SEL, "infra_uart1_sel",
  37. infra_uart_parent, 0x0018, 0x0010, 0x0014, 1, 1,
  38. -1, -1, -1),
  39. MUX_GATE_CLR_SET_UPD(CLK_INFRA_UART2_SEL, "infra_uart2_sel",
  40. infra_uart_parent, 0x0018, 0x0010, 0x0014, 2, 1,
  41. -1, -1, -1),
  42. MUX_GATE_CLR_SET_UPD(CLK_INFRA_SPI0_SEL, "infra_spi0_sel",
  43. infra_spi_parents, 0x0018, 0x0010, 0x0014, 4, 1,
  44. -1, -1, -1),
  45. MUX_GATE_CLR_SET_UPD(CLK_INFRA_SPI1_SEL, "infra_spi1_sel",
  46. infra_spi_parents, 0x0018, 0x0010, 0x0014, 5, 1,
  47. -1, -1, -1),
  48. MUX_GATE_CLR_SET_UPD(CLK_INFRA_PWM1_SEL, "infra_pwm1_sel",
  49. infra_pwm_bsel_parents, 0x0018, 0x0010, 0x0014, 9,
  50. 2, -1, -1, -1),
  51. MUX_GATE_CLR_SET_UPD(CLK_INFRA_PWM2_SEL, "infra_pwm2_sel",
  52. infra_pwm_bsel_parents, 0x0018, 0x0010, 0x0014, 11,
  53. 2, -1, -1, -1),
  54. MUX_GATE_CLR_SET_UPD(CLK_INFRA_PWM_BSEL, "infra_pwm_bsel",
  55. infra_pwm_bsel_parents, 0x0018, 0x0010, 0x0014, 13,
  56. 2, -1, -1, -1),
  57. /* MODULE_CLK_SEL_1 */
  58. MUX_GATE_CLR_SET_UPD(CLK_INFRA_PCIE_SEL, "infra_pcie_sel",
  59. infra_pcie_parents, 0x0028, 0x0020, 0x0024, 0, 2,
  60. -1, -1, -1),
  61. };
  62. static const struct mtk_gate_regs infra0_cg_regs = {
  63. .set_ofs = 0x40,
  64. .clr_ofs = 0x44,
  65. .sta_ofs = 0x48,
  66. };
  67. static const struct mtk_gate_regs infra1_cg_regs = {
  68. .set_ofs = 0x50,
  69. .clr_ofs = 0x54,
  70. .sta_ofs = 0x58,
  71. };
  72. static const struct mtk_gate_regs infra2_cg_regs = {
  73. .set_ofs = 0x60,
  74. .clr_ofs = 0x64,
  75. .sta_ofs = 0x68,
  76. };
  77. #define GATE_INFRA0(_id, _name, _parent, _shift) \
  78. GATE_MTK(_id, _name, _parent, &infra0_cg_regs, _shift, &mtk_clk_gate_ops_setclr)
  79. #define GATE_INFRA1(_id, _name, _parent, _shift) \
  80. GATE_MTK(_id, _name, _parent, &infra1_cg_regs, _shift, &mtk_clk_gate_ops_setclr)
  81. #define GATE_INFRA2(_id, _name, _parent, _shift) \
  82. GATE_MTK(_id, _name, _parent, &infra2_cg_regs, _shift, &mtk_clk_gate_ops_setclr)
  83. static const struct mtk_gate infra_clks[] = {
  84. /* INFRA0 */
  85. GATE_INFRA0(CLK_INFRA_GPT_STA, "infra_gpt_sta", "infra_sysaxi_d2", 0),
  86. GATE_INFRA0(CLK_INFRA_PWM_HCK, "infra_pwm_hck", "infra_sysaxi_d2", 1),
  87. GATE_INFRA0(CLK_INFRA_PWM_STA, "infra_pwm_sta", "infra_pwm_bsel", 2),
  88. GATE_INFRA0(CLK_INFRA_PWM1_CK, "infra_pwm1", "infra_pwm1_sel", 3),
  89. GATE_INFRA0(CLK_INFRA_PWM2_CK, "infra_pwm2", "infra_pwm2_sel", 4),
  90. GATE_INFRA0(CLK_INFRA_CQ_DMA_CK, "infra_cq_dma", "sysaxi_sel", 6),
  91. GATE_INFRA0(CLK_INFRA_EIP97_CK, "infra_eip97", "eip_b_sel", 7),
  92. GATE_INFRA0(CLK_INFRA_AUD_BUS_CK, "infra_aud_bus", "sysaxi_sel", 8),
  93. GATE_INFRA0(CLK_INFRA_AUD_26M_CK, "infra_aud_26m", "csw_f26m_sel", 9),
  94. GATE_INFRA0(CLK_INFRA_AUD_L_CK, "infra_aud_l", "aud_l_sel", 10),
  95. GATE_INFRA0(CLK_INFRA_AUD_AUD_CK, "infra_aud_aud", "a1sys_sel", 11),
  96. GATE_INFRA0(CLK_INFRA_AUD_EG2_CK, "infra_aud_eg2", "a_tuner_sel", 13),
  97. GATE_INFRA0(CLK_INFRA_DRAMC_26M_CK, "infra_dramc_26m", "csw_f26m_sel",
  98. 14),
  99. GATE_INFRA0(CLK_INFRA_DBG_CK, "infra_dbg", "infra_sysaxi_d2", 15),
  100. GATE_INFRA0(CLK_INFRA_AP_DMA_CK, "infra_ap_dma", "infra_sysaxi_d2", 16),
  101. GATE_INFRA0(CLK_INFRA_SEJ_CK, "infra_sej", "infra_sysaxi_d2", 24),
  102. GATE_INFRA0(CLK_INFRA_SEJ_13M_CK, "infra_sej_13m", "csw_f26m_sel", 25),
  103. GATE_INFRA0(CLK_INFRA_TRNG_CK, "infra_trng", "sysaxi_sel", 26),
  104. /* INFRA1 */
  105. GATE_INFRA1(CLK_INFRA_THERM_CK, "infra_therm", "csw_f26m_sel", 0),
  106. GATE_INFRA1(CLK_INFRA_I2C0_CK, "infra_i2c0", "i2c_sel", 1),
  107. GATE_INFRA1(CLK_INFRA_UART0_CK, "infra_uart0", "infra_uart0_sel", 2),
  108. GATE_INFRA1(CLK_INFRA_UART1_CK, "infra_uart1", "infra_uart1_sel", 3),
  109. GATE_INFRA1(CLK_INFRA_UART2_CK, "infra_uart2", "infra_uart2_sel", 4),
  110. GATE_INFRA1(CLK_INFRA_NFI1_CK, "infra_nfi1", "nfi1x_sel", 8),
  111. GATE_INFRA1(CLK_INFRA_SPINFI1_CK, "infra_spinfi1", "spinfi_sel", 9),
  112. GATE_INFRA1(CLK_INFRA_NFI_HCK_CK, "infra_nfi_hck", "infra_sysaxi_d2",
  113. 10),
  114. GATE_INFRA1(CLK_INFRA_SPI0_CK, "infra_spi0", "infra_spi0_sel", 11),
  115. GATE_INFRA1(CLK_INFRA_SPI1_CK, "infra_spi1", "infra_spi1_sel", 12),
  116. GATE_INFRA1(CLK_INFRA_SPI0_HCK_CK, "infra_spi0_hck", "infra_sysaxi_d2",
  117. 13),
  118. GATE_INFRA1(CLK_INFRA_SPI1_HCK_CK, "infra_spi1_hck", "infra_sysaxi_d2",
  119. 14),
  120. GATE_INFRA1(CLK_INFRA_FRTC_CK, "infra_frtc", "top_rtc_32k", 15),
  121. GATE_INFRA1(CLK_INFRA_MSDC_CK, "infra_msdc", "emmc_416m_sel", 16),
  122. GATE_INFRA1(CLK_INFRA_MSDC_HCK_CK, "infra_msdc_hck", "emmc_250m_sel",
  123. 17),
  124. GATE_INFRA1(CLK_INFRA_MSDC_133M_CK, "infra_msdc_133m", "sysaxi_sel",
  125. 18),
  126. GATE_INFRA1(CLK_INFRA_MSDC_66M_CK, "infra_msdc_66m", "infra_sysaxi_d2",
  127. 19),
  128. GATE_INFRA1(CLK_INFRA_ADC_26M_CK, "infra_adc_26m", "infra_adc_frc", 20),
  129. GATE_INFRA1(CLK_INFRA_ADC_FRC_CK, "infra_adc_frc", "csw_f26m_sel", 21),
  130. GATE_INFRA1(CLK_INFRA_FBIST2FPC_CK, "infra_fbist2fpc", "nfi1x_sel", 23),
  131. /* INFRA2 */
  132. GATE_INFRA2(CLK_INFRA_IUSB_133_CK, "infra_iusb_133", "sysaxi_sel", 0),
  133. GATE_INFRA2(CLK_INFRA_IUSB_66M_CK, "infra_iusb_66m", "infra_sysaxi_d2",
  134. 1),
  135. GATE_INFRA2(CLK_INFRA_IUSB_SYS_CK, "infra_iusb_sys", "u2u3_sys_sel", 2),
  136. GATE_INFRA2(CLK_INFRA_IUSB_CK, "infra_iusb", "u2u3_sel", 3),
  137. GATE_INFRA2(CLK_INFRA_IPCIE_CK, "infra_ipcie", "pextp_tl_ck_sel", 12),
  138. GATE_INFRA2(CLK_INFRA_IPCIE_PIPE_CK, "infra_ipcie_pipe", "top_xtal",
  139. 13),
  140. GATE_INFRA2(CLK_INFRA_IPCIER_CK, "infra_ipcier", "csw_f26m_sel", 14),
  141. GATE_INFRA2(CLK_INFRA_IPCIEB_CK, "infra_ipcieb", "sysaxi_sel", 15),
  142. };
  143. static int clk_mt7986_infracfg_probe(struct platform_device *pdev)
  144. {
  145. struct clk_hw_onecell_data *clk_data;
  146. struct device_node *node = pdev->dev.of_node;
  147. int r;
  148. void __iomem *base;
  149. int nr = ARRAY_SIZE(infra_divs) + ARRAY_SIZE(infra_muxes) +
  150. ARRAY_SIZE(infra_clks);
  151. base = of_iomap(node, 0);
  152. if (!base) {
  153. pr_err("%s(): ioremap failed\n", __func__);
  154. return -ENOMEM;
  155. }
  156. clk_data = mtk_alloc_clk_data(nr);
  157. if (!clk_data)
  158. return -ENOMEM;
  159. mtk_clk_register_factors(infra_divs, ARRAY_SIZE(infra_divs), clk_data);
  160. mtk_clk_register_muxes(infra_muxes, ARRAY_SIZE(infra_muxes), node,
  161. &mt7986_clk_lock, clk_data);
  162. mtk_clk_register_gates(node, infra_clks, ARRAY_SIZE(infra_clks),
  163. clk_data);
  164. r = of_clk_add_hw_provider(node, of_clk_hw_onecell_get, clk_data);
  165. if (r) {
  166. pr_err("%s(): could not register clock provider: %d\n",
  167. __func__, r);
  168. goto free_infracfg_data;
  169. }
  170. return r;
  171. free_infracfg_data:
  172. mtk_free_clk_data(clk_data);
  173. return r;
  174. }
  175. static const struct of_device_id of_match_clk_mt7986_infracfg[] = {
  176. { .compatible = "mediatek,mt7986-infracfg", },
  177. {}
  178. };
  179. static struct platform_driver clk_mt7986_infracfg_drv = {
  180. .probe = clk_mt7986_infracfg_probe,
  181. .driver = {
  182. .name = "clk-mt7986-infracfg",
  183. .of_match_table = of_match_clk_mt7986_infracfg,
  184. },
  185. };
  186. builtin_platform_driver(clk_mt7986_infracfg_drv);