162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright (c) 2021 MediaTek Inc.
462306a36Sopenharmony_ci * Author: Sam Shih <sam.shih@mediatek.com>
562306a36Sopenharmony_ci * Author: Wenzhen Yu <wenzhen.yu@mediatek.com>
662306a36Sopenharmony_ci */
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci#include <linux/clk-provider.h>
962306a36Sopenharmony_ci#include <linux/mod_devicetable.h>
1062306a36Sopenharmony_ci#include <linux/platform_device.h>
1162306a36Sopenharmony_ci#include "clk-mtk.h"
1262306a36Sopenharmony_ci#include "clk-gate.h"
1362306a36Sopenharmony_ci#include "clk-mux.h"
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ci#include <dt-bindings/clock/mt7986-clk.h>
1662306a36Sopenharmony_ci#include <linux/clk.h>
1762306a36Sopenharmony_ci
1862306a36Sopenharmony_cistatic DEFINE_SPINLOCK(mt7986_clk_lock);
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_cistatic const struct mtk_fixed_factor infra_divs[] = {
2162306a36Sopenharmony_ci	FACTOR(CLK_INFRA_SYSAXI_D2, "infra_sysaxi_d2", "sysaxi_sel", 1, 2),
2262306a36Sopenharmony_ci};
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_cistatic const char *const infra_uart_parent[] __initconst = { "csw_f26m_sel",
2562306a36Sopenharmony_ci							     "uart_sel" };
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_cistatic const char *const infra_spi_parents[] __initconst = { "i2c_sel",
2862306a36Sopenharmony_ci							     "spi_sel" };
2962306a36Sopenharmony_ci
3062306a36Sopenharmony_cistatic const char *const infra_pwm_bsel_parents[] __initconst = {
3162306a36Sopenharmony_ci	"top_rtc_32p7k", "csw_f26m_sel", "infra_sysaxi_d2", "pwm_sel"
3262306a36Sopenharmony_ci};
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_cistatic const char *const infra_pcie_parents[] __initconst = {
3562306a36Sopenharmony_ci	"top_rtc_32p7k", "csw_f26m_sel", "top_xtal", "pextp_tl_ck_sel"
3662306a36Sopenharmony_ci};
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_cistatic const struct mtk_mux infra_muxes[] = {
3962306a36Sopenharmony_ci	/* MODULE_CLK_SEL_0 */
4062306a36Sopenharmony_ci	MUX_GATE_CLR_SET_UPD(CLK_INFRA_UART0_SEL, "infra_uart0_sel",
4162306a36Sopenharmony_ci			     infra_uart_parent, 0x0018, 0x0010, 0x0014, 0, 1,
4262306a36Sopenharmony_ci			     -1, -1, -1),
4362306a36Sopenharmony_ci	MUX_GATE_CLR_SET_UPD(CLK_INFRA_UART1_SEL, "infra_uart1_sel",
4462306a36Sopenharmony_ci			     infra_uart_parent, 0x0018, 0x0010, 0x0014, 1, 1,
4562306a36Sopenharmony_ci			     -1, -1, -1),
4662306a36Sopenharmony_ci	MUX_GATE_CLR_SET_UPD(CLK_INFRA_UART2_SEL, "infra_uart2_sel",
4762306a36Sopenharmony_ci			     infra_uart_parent, 0x0018, 0x0010, 0x0014, 2, 1,
4862306a36Sopenharmony_ci			     -1, -1, -1),
4962306a36Sopenharmony_ci	MUX_GATE_CLR_SET_UPD(CLK_INFRA_SPI0_SEL, "infra_spi0_sel",
5062306a36Sopenharmony_ci			     infra_spi_parents, 0x0018, 0x0010, 0x0014, 4, 1,
5162306a36Sopenharmony_ci			     -1, -1, -1),
5262306a36Sopenharmony_ci	MUX_GATE_CLR_SET_UPD(CLK_INFRA_SPI1_SEL, "infra_spi1_sel",
5362306a36Sopenharmony_ci			     infra_spi_parents, 0x0018, 0x0010, 0x0014, 5, 1,
5462306a36Sopenharmony_ci			     -1, -1, -1),
5562306a36Sopenharmony_ci	MUX_GATE_CLR_SET_UPD(CLK_INFRA_PWM1_SEL, "infra_pwm1_sel",
5662306a36Sopenharmony_ci			     infra_pwm_bsel_parents, 0x0018, 0x0010, 0x0014, 9,
5762306a36Sopenharmony_ci			     2, -1, -1, -1),
5862306a36Sopenharmony_ci	MUX_GATE_CLR_SET_UPD(CLK_INFRA_PWM2_SEL, "infra_pwm2_sel",
5962306a36Sopenharmony_ci			     infra_pwm_bsel_parents, 0x0018, 0x0010, 0x0014, 11,
6062306a36Sopenharmony_ci			     2, -1, -1, -1),
6162306a36Sopenharmony_ci	MUX_GATE_CLR_SET_UPD(CLK_INFRA_PWM_BSEL, "infra_pwm_bsel",
6262306a36Sopenharmony_ci			     infra_pwm_bsel_parents, 0x0018, 0x0010, 0x0014, 13,
6362306a36Sopenharmony_ci			     2, -1, -1, -1),
6462306a36Sopenharmony_ci	/* MODULE_CLK_SEL_1 */
6562306a36Sopenharmony_ci	MUX_GATE_CLR_SET_UPD(CLK_INFRA_PCIE_SEL, "infra_pcie_sel",
6662306a36Sopenharmony_ci			     infra_pcie_parents, 0x0028, 0x0020, 0x0024, 0, 2,
6762306a36Sopenharmony_ci			     -1, -1, -1),
6862306a36Sopenharmony_ci};
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_cistatic const struct mtk_gate_regs infra0_cg_regs = {
7162306a36Sopenharmony_ci	.set_ofs = 0x40,
7262306a36Sopenharmony_ci	.clr_ofs = 0x44,
7362306a36Sopenharmony_ci	.sta_ofs = 0x48,
7462306a36Sopenharmony_ci};
7562306a36Sopenharmony_ci
7662306a36Sopenharmony_cistatic const struct mtk_gate_regs infra1_cg_regs = {
7762306a36Sopenharmony_ci	.set_ofs = 0x50,
7862306a36Sopenharmony_ci	.clr_ofs = 0x54,
7962306a36Sopenharmony_ci	.sta_ofs = 0x58,
8062306a36Sopenharmony_ci};
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_cistatic const struct mtk_gate_regs infra2_cg_regs = {
8362306a36Sopenharmony_ci	.set_ofs = 0x60,
8462306a36Sopenharmony_ci	.clr_ofs = 0x64,
8562306a36Sopenharmony_ci	.sta_ofs = 0x68,
8662306a36Sopenharmony_ci};
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ci#define GATE_INFRA0(_id, _name, _parent, _shift)			\
8962306a36Sopenharmony_ci	GATE_MTK(_id, _name, _parent, &infra0_cg_regs, _shift, &mtk_clk_gate_ops_setclr)
9062306a36Sopenharmony_ci
9162306a36Sopenharmony_ci#define GATE_INFRA1(_id, _name, _parent, _shift)			\
9262306a36Sopenharmony_ci	GATE_MTK(_id, _name, _parent, &infra1_cg_regs, _shift, &mtk_clk_gate_ops_setclr)
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_ci#define GATE_INFRA2(_id, _name, _parent, _shift)			\
9562306a36Sopenharmony_ci	GATE_MTK(_id, _name, _parent, &infra2_cg_regs, _shift, &mtk_clk_gate_ops_setclr)
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_cistatic const struct mtk_gate infra_clks[] = {
9862306a36Sopenharmony_ci	/* INFRA0 */
9962306a36Sopenharmony_ci	GATE_INFRA0(CLK_INFRA_GPT_STA, "infra_gpt_sta", "infra_sysaxi_d2", 0),
10062306a36Sopenharmony_ci	GATE_INFRA0(CLK_INFRA_PWM_HCK, "infra_pwm_hck", "infra_sysaxi_d2", 1),
10162306a36Sopenharmony_ci	GATE_INFRA0(CLK_INFRA_PWM_STA, "infra_pwm_sta", "infra_pwm_bsel", 2),
10262306a36Sopenharmony_ci	GATE_INFRA0(CLK_INFRA_PWM1_CK, "infra_pwm1", "infra_pwm1_sel", 3),
10362306a36Sopenharmony_ci	GATE_INFRA0(CLK_INFRA_PWM2_CK, "infra_pwm2", "infra_pwm2_sel", 4),
10462306a36Sopenharmony_ci	GATE_INFRA0(CLK_INFRA_CQ_DMA_CK, "infra_cq_dma", "sysaxi_sel", 6),
10562306a36Sopenharmony_ci	GATE_INFRA0(CLK_INFRA_EIP97_CK, "infra_eip97", "eip_b_sel", 7),
10662306a36Sopenharmony_ci	GATE_INFRA0(CLK_INFRA_AUD_BUS_CK, "infra_aud_bus", "sysaxi_sel", 8),
10762306a36Sopenharmony_ci	GATE_INFRA0(CLK_INFRA_AUD_26M_CK, "infra_aud_26m", "csw_f26m_sel", 9),
10862306a36Sopenharmony_ci	GATE_INFRA0(CLK_INFRA_AUD_L_CK, "infra_aud_l", "aud_l_sel", 10),
10962306a36Sopenharmony_ci	GATE_INFRA0(CLK_INFRA_AUD_AUD_CK, "infra_aud_aud", "a1sys_sel", 11),
11062306a36Sopenharmony_ci	GATE_INFRA0(CLK_INFRA_AUD_EG2_CK, "infra_aud_eg2", "a_tuner_sel", 13),
11162306a36Sopenharmony_ci	GATE_INFRA0(CLK_INFRA_DRAMC_26M_CK, "infra_dramc_26m", "csw_f26m_sel",
11262306a36Sopenharmony_ci		    14),
11362306a36Sopenharmony_ci	GATE_INFRA0(CLK_INFRA_DBG_CK, "infra_dbg", "infra_sysaxi_d2", 15),
11462306a36Sopenharmony_ci	GATE_INFRA0(CLK_INFRA_AP_DMA_CK, "infra_ap_dma", "infra_sysaxi_d2", 16),
11562306a36Sopenharmony_ci	GATE_INFRA0(CLK_INFRA_SEJ_CK, "infra_sej", "infra_sysaxi_d2", 24),
11662306a36Sopenharmony_ci	GATE_INFRA0(CLK_INFRA_SEJ_13M_CK, "infra_sej_13m", "csw_f26m_sel", 25),
11762306a36Sopenharmony_ci	GATE_INFRA0(CLK_INFRA_TRNG_CK, "infra_trng", "sysaxi_sel", 26),
11862306a36Sopenharmony_ci	/* INFRA1 */
11962306a36Sopenharmony_ci	GATE_INFRA1(CLK_INFRA_THERM_CK, "infra_therm", "csw_f26m_sel", 0),
12062306a36Sopenharmony_ci	GATE_INFRA1(CLK_INFRA_I2C0_CK, "infra_i2c0", "i2c_sel", 1),
12162306a36Sopenharmony_ci	GATE_INFRA1(CLK_INFRA_UART0_CK, "infra_uart0", "infra_uart0_sel", 2),
12262306a36Sopenharmony_ci	GATE_INFRA1(CLK_INFRA_UART1_CK, "infra_uart1", "infra_uart1_sel", 3),
12362306a36Sopenharmony_ci	GATE_INFRA1(CLK_INFRA_UART2_CK, "infra_uart2", "infra_uart2_sel", 4),
12462306a36Sopenharmony_ci	GATE_INFRA1(CLK_INFRA_NFI1_CK, "infra_nfi1", "nfi1x_sel", 8),
12562306a36Sopenharmony_ci	GATE_INFRA1(CLK_INFRA_SPINFI1_CK, "infra_spinfi1", "spinfi_sel", 9),
12662306a36Sopenharmony_ci	GATE_INFRA1(CLK_INFRA_NFI_HCK_CK, "infra_nfi_hck", "infra_sysaxi_d2",
12762306a36Sopenharmony_ci		    10),
12862306a36Sopenharmony_ci	GATE_INFRA1(CLK_INFRA_SPI0_CK, "infra_spi0", "infra_spi0_sel", 11),
12962306a36Sopenharmony_ci	GATE_INFRA1(CLK_INFRA_SPI1_CK, "infra_spi1", "infra_spi1_sel", 12),
13062306a36Sopenharmony_ci	GATE_INFRA1(CLK_INFRA_SPI0_HCK_CK, "infra_spi0_hck", "infra_sysaxi_d2",
13162306a36Sopenharmony_ci		    13),
13262306a36Sopenharmony_ci	GATE_INFRA1(CLK_INFRA_SPI1_HCK_CK, "infra_spi1_hck", "infra_sysaxi_d2",
13362306a36Sopenharmony_ci		    14),
13462306a36Sopenharmony_ci	GATE_INFRA1(CLK_INFRA_FRTC_CK, "infra_frtc", "top_rtc_32k", 15),
13562306a36Sopenharmony_ci	GATE_INFRA1(CLK_INFRA_MSDC_CK, "infra_msdc", "emmc_416m_sel", 16),
13662306a36Sopenharmony_ci	GATE_INFRA1(CLK_INFRA_MSDC_HCK_CK, "infra_msdc_hck", "emmc_250m_sel",
13762306a36Sopenharmony_ci		    17),
13862306a36Sopenharmony_ci	GATE_INFRA1(CLK_INFRA_MSDC_133M_CK, "infra_msdc_133m", "sysaxi_sel",
13962306a36Sopenharmony_ci		    18),
14062306a36Sopenharmony_ci	GATE_INFRA1(CLK_INFRA_MSDC_66M_CK, "infra_msdc_66m", "infra_sysaxi_d2",
14162306a36Sopenharmony_ci		    19),
14262306a36Sopenharmony_ci	GATE_INFRA1(CLK_INFRA_ADC_26M_CK, "infra_adc_26m", "infra_adc_frc", 20),
14362306a36Sopenharmony_ci	GATE_INFRA1(CLK_INFRA_ADC_FRC_CK, "infra_adc_frc", "csw_f26m_sel", 21),
14462306a36Sopenharmony_ci	GATE_INFRA1(CLK_INFRA_FBIST2FPC_CK, "infra_fbist2fpc", "nfi1x_sel", 23),
14562306a36Sopenharmony_ci	/* INFRA2 */
14662306a36Sopenharmony_ci	GATE_INFRA2(CLK_INFRA_IUSB_133_CK, "infra_iusb_133", "sysaxi_sel", 0),
14762306a36Sopenharmony_ci	GATE_INFRA2(CLK_INFRA_IUSB_66M_CK, "infra_iusb_66m", "infra_sysaxi_d2",
14862306a36Sopenharmony_ci		    1),
14962306a36Sopenharmony_ci	GATE_INFRA2(CLK_INFRA_IUSB_SYS_CK, "infra_iusb_sys", "u2u3_sys_sel", 2),
15062306a36Sopenharmony_ci	GATE_INFRA2(CLK_INFRA_IUSB_CK, "infra_iusb", "u2u3_sel", 3),
15162306a36Sopenharmony_ci	GATE_INFRA2(CLK_INFRA_IPCIE_CK, "infra_ipcie", "pextp_tl_ck_sel", 12),
15262306a36Sopenharmony_ci	GATE_INFRA2(CLK_INFRA_IPCIE_PIPE_CK, "infra_ipcie_pipe", "top_xtal",
15362306a36Sopenharmony_ci		    13),
15462306a36Sopenharmony_ci	GATE_INFRA2(CLK_INFRA_IPCIER_CK, "infra_ipcier", "csw_f26m_sel", 14),
15562306a36Sopenharmony_ci	GATE_INFRA2(CLK_INFRA_IPCIEB_CK, "infra_ipcieb", "sysaxi_sel", 15),
15662306a36Sopenharmony_ci};
15762306a36Sopenharmony_ci
15862306a36Sopenharmony_cistatic const struct mtk_clk_desc infra_desc = {
15962306a36Sopenharmony_ci	.clks = infra_clks,
16062306a36Sopenharmony_ci	.num_clks = ARRAY_SIZE(infra_clks),
16162306a36Sopenharmony_ci	.factor_clks = infra_divs,
16262306a36Sopenharmony_ci	.num_factor_clks = ARRAY_SIZE(infra_divs),
16362306a36Sopenharmony_ci	.mux_clks = infra_muxes,
16462306a36Sopenharmony_ci	.num_mux_clks = ARRAY_SIZE(infra_muxes),
16562306a36Sopenharmony_ci	.clk_lock = &mt7986_clk_lock,
16662306a36Sopenharmony_ci};
16762306a36Sopenharmony_ci
16862306a36Sopenharmony_cistatic const struct of_device_id of_match_clk_mt7986_infracfg[] = {
16962306a36Sopenharmony_ci	{ .compatible = "mediatek,mt7986-infracfg", .data = &infra_desc },
17062306a36Sopenharmony_ci	{ /* sentinel */ }
17162306a36Sopenharmony_ci};
17262306a36Sopenharmony_ciMODULE_DEVICE_TABLE(of, of_match_clk_mt7986_infracfg);
17362306a36Sopenharmony_ci
17462306a36Sopenharmony_cistatic struct platform_driver clk_mt7986_infracfg_drv = {
17562306a36Sopenharmony_ci	.driver = {
17662306a36Sopenharmony_ci		.name = "clk-mt7986-infracfg",
17762306a36Sopenharmony_ci		.of_match_table = of_match_clk_mt7986_infracfg,
17862306a36Sopenharmony_ci	},
17962306a36Sopenharmony_ci	.probe = mtk_clk_simple_probe,
18062306a36Sopenharmony_ci	.remove_new = mtk_clk_simple_remove,
18162306a36Sopenharmony_ci};
18262306a36Sopenharmony_cimodule_platform_driver(clk_mt7986_infracfg_drv);
18362306a36Sopenharmony_ci
18462306a36Sopenharmony_ciMODULE_DESCRIPTION("MediaTek MT7986 infracfg clocks driver");
18562306a36Sopenharmony_ciMODULE_LICENSE("GPL");
186