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 * Author: Jianhui Zhao <zhaojh329@gmail.com>
762306a36Sopenharmony_ci * Author: Daniel Golle <daniel@makrotopia.org>
862306a36Sopenharmony_ci */
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ci#include <linux/clk-provider.h>
1162306a36Sopenharmony_ci#include <linux/mod_devicetable.h>
1262306a36Sopenharmony_ci#include <linux/platform_device.h>
1362306a36Sopenharmony_ci#include "clk-mtk.h"
1462306a36Sopenharmony_ci#include "clk-gate.h"
1562306a36Sopenharmony_ci#include "clk-mux.h"
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ci#include <dt-bindings/clock/mediatek,mt7981-clk.h>
1862306a36Sopenharmony_ci#include <linux/clk.h>
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_cistatic DEFINE_SPINLOCK(mt7981_clk_lock);
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_cistatic const struct mtk_fixed_factor infra_divs[] = {
2362306a36Sopenharmony_ci	FACTOR(CLK_INFRA_66M_MCK, "infra_66m_mck", "sysaxi_sel", 1, 2),
2462306a36Sopenharmony_ci};
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_cistatic const char *const infra_uart_parent[] __initconst = { "csw_f26m_sel",
2762306a36Sopenharmony_ci								"uart_sel" };
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_cistatic const char *const infra_spi0_parents[] __initconst = { "i2c_sel",
3062306a36Sopenharmony_ci							      "spi_sel" };
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_cistatic const char *const infra_spi1_parents[] __initconst = { "i2c_sel",
3362306a36Sopenharmony_ci							      "spim_mst_sel" };
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_cistatic const char *const infra_pwm1_parents[] __initconst = { "pwm_sel" };
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_cistatic const char *const infra_pwm_bsel_parents[] __initconst = {
3862306a36Sopenharmony_ci	"cb_rtc_32p7k", "csw_f26m_sel", "infra_66m_mck", "pwm_sel"
3962306a36Sopenharmony_ci};
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_cistatic const char *const infra_pcie_parents[] __initconst = {
4262306a36Sopenharmony_ci	"cb_rtc_32p7k", "csw_f26m_sel", "cb_cksq_40m", "pextp_tl_ck_sel"
4362306a36Sopenharmony_ci};
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_cistatic const struct mtk_mux infra_muxes[] = {
4662306a36Sopenharmony_ci	/* MODULE_CLK_SEL_0 */
4762306a36Sopenharmony_ci	MUX_GATE_CLR_SET_UPD(CLK_INFRA_UART0_SEL, "infra_uart0_sel",
4862306a36Sopenharmony_ci			     infra_uart_parent, 0x0018, 0x0010, 0x0014, 0, 1,
4962306a36Sopenharmony_ci			     -1, -1, -1),
5062306a36Sopenharmony_ci	MUX_GATE_CLR_SET_UPD(CLK_INFRA_UART1_SEL, "infra_uart1_sel",
5162306a36Sopenharmony_ci			     infra_uart_parent, 0x0018, 0x0010, 0x0014, 1, 1,
5262306a36Sopenharmony_ci			     -1, -1, -1),
5362306a36Sopenharmony_ci	MUX_GATE_CLR_SET_UPD(CLK_INFRA_UART2_SEL, "infra_uart2_sel",
5462306a36Sopenharmony_ci			     infra_uart_parent, 0x0018, 0x0010, 0x0014, 2, 1,
5562306a36Sopenharmony_ci			     -1, -1, -1),
5662306a36Sopenharmony_ci	MUX_GATE_CLR_SET_UPD(CLK_INFRA_SPI0_SEL, "infra_spi0_sel",
5762306a36Sopenharmony_ci			     infra_spi0_parents, 0x0018, 0x0010, 0x0014, 4, 1,
5862306a36Sopenharmony_ci			     -1, -1, -1),
5962306a36Sopenharmony_ci	MUX_GATE_CLR_SET_UPD(CLK_INFRA_SPI1_SEL, "infra_spi1_sel",
6062306a36Sopenharmony_ci			     infra_spi1_parents, 0x0018, 0x0010, 0x0014, 5, 1,
6162306a36Sopenharmony_ci			     -1, -1, -1),
6262306a36Sopenharmony_ci	MUX_GATE_CLR_SET_UPD(CLK_INFRA_SPI2_SEL, "infra_spi2_sel",
6362306a36Sopenharmony_ci			     infra_spi0_parents, 0x0018, 0x0010, 0x0014, 6, 1,
6462306a36Sopenharmony_ci			     -1, -1, -1),
6562306a36Sopenharmony_ci	MUX_GATE_CLR_SET_UPD(CLK_INFRA_PWM1_SEL, "infra_pwm1_sel",
6662306a36Sopenharmony_ci			     infra_pwm1_parents, 0x0018, 0x0010, 0x0014, 9, 1,
6762306a36Sopenharmony_ci			     -1, -1, -1),
6862306a36Sopenharmony_ci	MUX_GATE_CLR_SET_UPD(CLK_INFRA_PWM2_SEL, "infra_pwm2_sel",
6962306a36Sopenharmony_ci			     infra_pwm1_parents, 0x0018, 0x0010, 0x0014, 11, 1,
7062306a36Sopenharmony_ci			     -1, -1, -1),
7162306a36Sopenharmony_ci	MUX_GATE_CLR_SET_UPD(CLK_INFRA_PWM3_SEL, "infra_pwm3_sel",
7262306a36Sopenharmony_ci			     infra_pwm1_parents, 0x0018, 0x0010, 0x0014, 15, 1,
7362306a36Sopenharmony_ci			     -1, -1, -1),
7462306a36Sopenharmony_ci	MUX_GATE_CLR_SET_UPD(CLK_INFRA_PWM_BSEL, "infra_pwm_bsel",
7562306a36Sopenharmony_ci			     infra_pwm_bsel_parents, 0x0018, 0x0010, 0x0014, 13,
7662306a36Sopenharmony_ci			     2, -1, -1, -1),
7762306a36Sopenharmony_ci	/* MODULE_CLK_SEL_1 */
7862306a36Sopenharmony_ci	MUX_GATE_CLR_SET_UPD(CLK_INFRA_PCIE_SEL, "infra_pcie_sel",
7962306a36Sopenharmony_ci			     infra_pcie_parents, 0x0028, 0x0020, 0x0024, 0, 2,
8062306a36Sopenharmony_ci			     -1, -1, -1),
8162306a36Sopenharmony_ci};
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_cistatic const struct mtk_gate_regs infra0_cg_regs = {
8462306a36Sopenharmony_ci	.set_ofs = 0x40,
8562306a36Sopenharmony_ci	.clr_ofs = 0x44,
8662306a36Sopenharmony_ci	.sta_ofs = 0x48,
8762306a36Sopenharmony_ci};
8862306a36Sopenharmony_ci
8962306a36Sopenharmony_cistatic const struct mtk_gate_regs infra1_cg_regs = {
9062306a36Sopenharmony_ci	.set_ofs = 0x50,
9162306a36Sopenharmony_ci	.clr_ofs = 0x54,
9262306a36Sopenharmony_ci	.sta_ofs = 0x58,
9362306a36Sopenharmony_ci};
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_cistatic const struct mtk_gate_regs infra2_cg_regs = {
9662306a36Sopenharmony_ci	.set_ofs = 0x60,
9762306a36Sopenharmony_ci	.clr_ofs = 0x64,
9862306a36Sopenharmony_ci	.sta_ofs = 0x68,
9962306a36Sopenharmony_ci};
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_ci#define GATE_INFRA0(_id, _name, _parent, _shift)                               \
10262306a36Sopenharmony_ci	{                                                                      \
10362306a36Sopenharmony_ci		.id = _id, .name = _name, .parent_name = _parent,              \
10462306a36Sopenharmony_ci		.regs = &infra0_cg_regs, .shift = _shift,                      \
10562306a36Sopenharmony_ci		.ops = &mtk_clk_gate_ops_setclr,                               \
10662306a36Sopenharmony_ci	}
10762306a36Sopenharmony_ci
10862306a36Sopenharmony_ci#define GATE_INFRA1(_id, _name, _parent, _shift)                               \
10962306a36Sopenharmony_ci	{                                                                      \
11062306a36Sopenharmony_ci		.id = _id, .name = _name, .parent_name = _parent,              \
11162306a36Sopenharmony_ci		.regs = &infra1_cg_regs, .shift = _shift,                      \
11262306a36Sopenharmony_ci		.ops = &mtk_clk_gate_ops_setclr,                               \
11362306a36Sopenharmony_ci	}
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_ci#define GATE_INFRA2(_id, _name, _parent, _shift)                               \
11662306a36Sopenharmony_ci	{                                                                      \
11762306a36Sopenharmony_ci		.id = _id, .name = _name, .parent_name = _parent,              \
11862306a36Sopenharmony_ci		.regs = &infra2_cg_regs, .shift = _shift,                      \
11962306a36Sopenharmony_ci		.ops = &mtk_clk_gate_ops_setclr,                               \
12062306a36Sopenharmony_ci	}
12162306a36Sopenharmony_ci
12262306a36Sopenharmony_cistatic const struct mtk_gate infra_clks[] = {
12362306a36Sopenharmony_ci	/* INFRA0 */
12462306a36Sopenharmony_ci	GATE_INFRA0(CLK_INFRA_GPT_STA, "infra_gpt_sta", "infra_66m_mck", 0),
12562306a36Sopenharmony_ci	GATE_INFRA0(CLK_INFRA_PWM_HCK, "infra_pwm_hck", "infra_66m_mck", 1),
12662306a36Sopenharmony_ci	GATE_INFRA0(CLK_INFRA_PWM_STA, "infra_pwm_sta", "infra_pwm_bsel", 2),
12762306a36Sopenharmony_ci	GATE_INFRA0(CLK_INFRA_PWM1_CK, "infra_pwm1", "infra_pwm1_sel", 3),
12862306a36Sopenharmony_ci	GATE_INFRA0(CLK_INFRA_PWM2_CK, "infra_pwm2", "infra_pwm2_sel", 4),
12962306a36Sopenharmony_ci	GATE_INFRA0(CLK_INFRA_CQ_DMA_CK, "infra_cq_dma", "sysaxi", 6),
13062306a36Sopenharmony_ci
13162306a36Sopenharmony_ci	GATE_INFRA0(CLK_INFRA_AUD_BUS_CK, "infra_aud_bus", "sysaxi", 8),
13262306a36Sopenharmony_ci	GATE_INFRA0(CLK_INFRA_AUD_26M_CK, "infra_aud_26m", "csw_f26m_sel", 9),
13362306a36Sopenharmony_ci	GATE_INFRA0(CLK_INFRA_AUD_L_CK, "infra_aud_l", "aud_l", 10),
13462306a36Sopenharmony_ci	GATE_INFRA0(CLK_INFRA_AUD_AUD_CK, "infra_aud_aud", "a1sys", 11),
13562306a36Sopenharmony_ci	GATE_INFRA0(CLK_INFRA_AUD_EG2_CK, "infra_aud_eg2", "a_tuner", 13),
13662306a36Sopenharmony_ci	GATE_INFRA0(CLK_INFRA_DRAMC_26M_CK, "infra_dramc_26m", "csw_f26m_sel",
13762306a36Sopenharmony_ci		    14),
13862306a36Sopenharmony_ci	GATE_INFRA0(CLK_INFRA_DBG_CK, "infra_dbg", "infra_66m_mck", 15),
13962306a36Sopenharmony_ci	GATE_INFRA0(CLK_INFRA_AP_DMA_CK, "infra_ap_dma", "infra_66m_mck", 16),
14062306a36Sopenharmony_ci	GATE_INFRA0(CLK_INFRA_SEJ_CK, "infra_sej", "infra_66m_mck", 24),
14162306a36Sopenharmony_ci	GATE_INFRA0(CLK_INFRA_SEJ_13M_CK, "infra_sej_13m", "csw_f26m_sel", 25),
14262306a36Sopenharmony_ci	GATE_INFRA0(CLK_INFRA_PWM3_CK, "infra_pwm3", "infra_pwm3_sel", 27),
14362306a36Sopenharmony_ci	/* INFRA1 */
14462306a36Sopenharmony_ci	GATE_INFRA1(CLK_INFRA_THERM_CK, "infra_therm", "csw_f26m_sel", 0),
14562306a36Sopenharmony_ci	GATE_INFRA1(CLK_INFRA_I2C0_CK, "infra_i2c0", "i2c_bck", 1),
14662306a36Sopenharmony_ci	GATE_INFRA1(CLK_INFRA_UART0_CK, "infra_uart0", "infra_uart0_sel", 2),
14762306a36Sopenharmony_ci	GATE_INFRA1(CLK_INFRA_UART1_CK, "infra_uart1", "infra_uart1_sel", 3),
14862306a36Sopenharmony_ci	GATE_INFRA1(CLK_INFRA_UART2_CK, "infra_uart2", "infra_uart2_sel", 4),
14962306a36Sopenharmony_ci	GATE_INFRA1(CLK_INFRA_SPI2_CK, "infra_spi2", "infra_spi2_sel", 6),
15062306a36Sopenharmony_ci	GATE_INFRA1(CLK_INFRA_SPI2_HCK_CK, "infra_spi2_hck", "infra_66m_mck", 7),
15162306a36Sopenharmony_ci	GATE_INFRA1(CLK_INFRA_NFI1_CK, "infra_nfi1", "nfi1x", 8),
15262306a36Sopenharmony_ci	GATE_INFRA1(CLK_INFRA_SPINFI1_CK, "infra_spinfi1", "spinfi_bck", 9),
15362306a36Sopenharmony_ci	GATE_INFRA1(CLK_INFRA_NFI_HCK_CK, "infra_nfi_hck", "infra_66m_mck", 10),
15462306a36Sopenharmony_ci	GATE_INFRA1(CLK_INFRA_SPI0_CK, "infra_spi0", "infra_spi0_sel", 11),
15562306a36Sopenharmony_ci	GATE_INFRA1(CLK_INFRA_SPI1_CK, "infra_spi1", "infra_spi1_sel", 12),
15662306a36Sopenharmony_ci	GATE_INFRA1(CLK_INFRA_SPI0_HCK_CK, "infra_spi0_hck", "infra_66m_mck",
15762306a36Sopenharmony_ci		    13),
15862306a36Sopenharmony_ci	GATE_INFRA1(CLK_INFRA_SPI1_HCK_CK, "infra_spi1_hck", "infra_66m_mck",
15962306a36Sopenharmony_ci		    14),
16062306a36Sopenharmony_ci	GATE_INFRA1(CLK_INFRA_FRTC_CK, "infra_frtc", "cb_rtc_32k", 15),
16162306a36Sopenharmony_ci	GATE_INFRA1(CLK_INFRA_MSDC_CK, "infra_msdc", "emmc_400m", 16),
16262306a36Sopenharmony_ci	GATE_INFRA1(CLK_INFRA_MSDC_HCK_CK, "infra_msdc_hck", "emmc_208m", 17),
16362306a36Sopenharmony_ci	GATE_INFRA1(CLK_INFRA_MSDC_133M_CK, "infra_msdc_133m", "sysaxi", 18),
16462306a36Sopenharmony_ci	GATE_INFRA1(CLK_INFRA_MSDC_66M_CK, "infra_msdc_66m", "sysaxi", 19),
16562306a36Sopenharmony_ci	GATE_INFRA1(CLK_INFRA_ADC_26M_CK, "infra_adc_26m", "infra_adc_frc", 20),
16662306a36Sopenharmony_ci	GATE_INFRA1(CLK_INFRA_ADC_FRC_CK, "infra_adc_frc", "csw_f26m", 21),
16762306a36Sopenharmony_ci	GATE_INFRA1(CLK_INFRA_FBIST2FPC_CK, "infra_fbist2fpc", "nfi1x", 23),
16862306a36Sopenharmony_ci	GATE_INFRA1(CLK_INFRA_I2C_MCK_CK, "infra_i2c_mck", "sysaxi", 25),
16962306a36Sopenharmony_ci	GATE_INFRA1(CLK_INFRA_I2C_PCK_CK, "infra_i2c_pck", "infra_66m_mck", 26),
17062306a36Sopenharmony_ci	/* INFRA2 */
17162306a36Sopenharmony_ci	GATE_INFRA2(CLK_INFRA_IUSB_133_CK, "infra_iusb_133", "sysaxi", 0),
17262306a36Sopenharmony_ci	GATE_INFRA2(CLK_INFRA_IUSB_66M_CK, "infra_iusb_66m", "sysaxi", 1),
17362306a36Sopenharmony_ci	GATE_INFRA2(CLK_INFRA_IUSB_SYS_CK, "infra_iusb_sys", "u2u3_sys", 2),
17462306a36Sopenharmony_ci	GATE_INFRA2(CLK_INFRA_IUSB_CK, "infra_iusb", "u2u3_ref", 3),
17562306a36Sopenharmony_ci	GATE_INFRA2(CLK_INFRA_IPCIE_CK, "infra_ipcie", "pextp_tl", 12),
17662306a36Sopenharmony_ci	GATE_INFRA2(CLK_INFRA_IPCIE_PIPE_CK, "infra_ipcie_pipe", "cb_cksq_40m",
17762306a36Sopenharmony_ci		    13),
17862306a36Sopenharmony_ci	GATE_INFRA2(CLK_INFRA_IPCIER_CK, "infra_ipcier", "csw_f26m", 14),
17962306a36Sopenharmony_ci	GATE_INFRA2(CLK_INFRA_IPCIEB_CK, "infra_ipcieb", "sysaxi", 15),
18062306a36Sopenharmony_ci};
18162306a36Sopenharmony_ci
18262306a36Sopenharmony_cistatic const struct mtk_clk_desc infracfg_desc = {
18362306a36Sopenharmony_ci	.factor_clks = infra_divs,
18462306a36Sopenharmony_ci	.num_factor_clks = ARRAY_SIZE(infra_divs),
18562306a36Sopenharmony_ci	.mux_clks = infra_muxes,
18662306a36Sopenharmony_ci	.num_mux_clks = ARRAY_SIZE(infra_muxes),
18762306a36Sopenharmony_ci	.clks = infra_clks,
18862306a36Sopenharmony_ci	.num_clks = ARRAY_SIZE(infra_clks),
18962306a36Sopenharmony_ci	.clk_lock = &mt7981_clk_lock,
19062306a36Sopenharmony_ci};
19162306a36Sopenharmony_ci
19262306a36Sopenharmony_cistatic const struct of_device_id of_match_clk_mt7981_infracfg[] = {
19362306a36Sopenharmony_ci	{ .compatible = "mediatek,mt7981-infracfg", .data = &infracfg_desc },
19462306a36Sopenharmony_ci	{ /* sentinel */ }
19562306a36Sopenharmony_ci};
19662306a36Sopenharmony_ciMODULE_DEVICE_TABLE(of, of_match_clk_mt7981_infracfg);
19762306a36Sopenharmony_ci
19862306a36Sopenharmony_cistatic struct platform_driver clk_mt7981_infracfg_drv = {
19962306a36Sopenharmony_ci	.probe = mtk_clk_simple_probe,
20062306a36Sopenharmony_ci	.remove_new = mtk_clk_simple_remove,
20162306a36Sopenharmony_ci	.driver = {
20262306a36Sopenharmony_ci		.name = "clk-mt7981-infracfg",
20362306a36Sopenharmony_ci		.of_match_table = of_match_clk_mt7981_infracfg,
20462306a36Sopenharmony_ci	},
20562306a36Sopenharmony_ci};
20662306a36Sopenharmony_cimodule_platform_driver(clk_mt7981_infracfg_drv);
20762306a36Sopenharmony_ciMODULE_LICENSE("GPL");
208