162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright (c) 2018 MediaTek Inc.
462306a36Sopenharmony_ci *               Weiyi Lu <weiyi.lu@mediatek.com>
562306a36Sopenharmony_ci * Copyright (c) 2023 Collabora, Ltd.
662306a36Sopenharmony_ci *               AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com>
762306a36Sopenharmony_ci */
862306a36Sopenharmony_ci
962306a36Sopenharmony_ci#include <dt-bindings/clock/mt8183-clk.h>
1062306a36Sopenharmony_ci#include <linux/clk.h>
1162306a36Sopenharmony_ci#include <linux/of.h>
1262306a36Sopenharmony_ci#include <linux/platform_device.h>
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_ci#include "clk-gate.h"
1562306a36Sopenharmony_ci#include "clk-mtk.h"
1662306a36Sopenharmony_ci#include "clk-pll.h"
1762306a36Sopenharmony_ci
1862306a36Sopenharmony_cistatic const struct mtk_gate_regs apmixed_cg_regs = {
1962306a36Sopenharmony_ci	.set_ofs = 0x20,
2062306a36Sopenharmony_ci	.clr_ofs = 0x20,
2162306a36Sopenharmony_ci	.sta_ofs = 0x20,
2262306a36Sopenharmony_ci};
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ci#define GATE_APMIXED_FLAGS(_id, _name, _parent, _shift, _flags)		\
2562306a36Sopenharmony_ci	GATE_MTK_FLAGS(_id, _name, _parent, &apmixed_cg_regs,		\
2662306a36Sopenharmony_ci		       _shift, &mtk_clk_gate_ops_no_setclr_inv, _flags)
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ci#define GATE_APMIXED(_id, _name, _parent, _shift)			\
2962306a36Sopenharmony_ci	GATE_APMIXED_FLAGS(_id, _name, _parent, _shift,	0)
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_ci/*
3262306a36Sopenharmony_ci * CRITICAL CLOCK:
3362306a36Sopenharmony_ci * apmixed_appll26m is the toppest clock gate of all PLLs.
3462306a36Sopenharmony_ci */
3562306a36Sopenharmony_cistatic const struct mtk_gate apmixed_clks[] = {
3662306a36Sopenharmony_ci	/* AUDIO0 */
3762306a36Sopenharmony_ci	GATE_APMIXED(CLK_APMIXED_SSUSB_26M, "apmixed_ssusb26m", "f_f26m_ck", 4),
3862306a36Sopenharmony_ci	GATE_APMIXED_FLAGS(CLK_APMIXED_APPLL_26M, "apmixed_appll26m",
3962306a36Sopenharmony_ci			   "f_f26m_ck", 5, CLK_IS_CRITICAL),
4062306a36Sopenharmony_ci	GATE_APMIXED(CLK_APMIXED_MIPIC0_26M, "apmixed_mipic026m", "f_f26m_ck", 6),
4162306a36Sopenharmony_ci	GATE_APMIXED(CLK_APMIXED_MDPLLGP_26M, "apmixed_mdpll26m", "f_f26m_ck", 7),
4262306a36Sopenharmony_ci	GATE_APMIXED(CLK_APMIXED_MMSYS_26M, "apmixed_mmsys26m", "f_f26m_ck", 8),
4362306a36Sopenharmony_ci	GATE_APMIXED(CLK_APMIXED_UFS_26M, "apmixed_ufs26m", "f_f26m_ck", 9),
4462306a36Sopenharmony_ci	GATE_APMIXED(CLK_APMIXED_MIPIC1_26M, "apmixed_mipic126m", "f_f26m_ck", 11),
4562306a36Sopenharmony_ci	GATE_APMIXED(CLK_APMIXED_MEMPLL_26M, "apmixed_mempll26m", "f_f26m_ck", 13),
4662306a36Sopenharmony_ci	GATE_APMIXED(CLK_APMIXED_CLKSQ_LVPLL_26M, "apmixed_lvpll26m", "f_f26m_ck", 14),
4762306a36Sopenharmony_ci	GATE_APMIXED(CLK_APMIXED_MIPID0_26M, "apmixed_mipid026m", "f_f26m_ck", 16),
4862306a36Sopenharmony_ci	GATE_APMIXED(CLK_APMIXED_MIPID1_26M, "apmixed_mipid126m", "f_f26m_ck", 17),
4962306a36Sopenharmony_ci};
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_ci#define MT8183_PLL_FMAX		(3800UL * MHZ)
5262306a36Sopenharmony_ci#define MT8183_PLL_FMIN		(1500UL * MHZ)
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_ci#define PLL_B(_id, _name, _reg, _pwr_reg, _en_mask, _flags,		\
5562306a36Sopenharmony_ci			_rst_bar_mask, _pcwbits, _pcwibits, _pd_reg,	\
5662306a36Sopenharmony_ci			_pd_shift, _tuner_reg,  _tuner_en_reg,		\
5762306a36Sopenharmony_ci			_tuner_en_bit, _pcw_reg, _pcw_shift,		\
5862306a36Sopenharmony_ci			_pcw_chg_reg, _div_table) {			\
5962306a36Sopenharmony_ci		.id = _id,						\
6062306a36Sopenharmony_ci		.name = _name,						\
6162306a36Sopenharmony_ci		.reg = _reg,						\
6262306a36Sopenharmony_ci		.pwr_reg = _pwr_reg,					\
6362306a36Sopenharmony_ci		.en_mask = _en_mask,					\
6462306a36Sopenharmony_ci		.flags = _flags,					\
6562306a36Sopenharmony_ci		.rst_bar_mask = _rst_bar_mask,				\
6662306a36Sopenharmony_ci		.fmax = MT8183_PLL_FMAX,				\
6762306a36Sopenharmony_ci		.fmin = MT8183_PLL_FMIN,				\
6862306a36Sopenharmony_ci		.pcwbits = _pcwbits,					\
6962306a36Sopenharmony_ci		.pcwibits = _pcwibits,					\
7062306a36Sopenharmony_ci		.pd_reg = _pd_reg,					\
7162306a36Sopenharmony_ci		.pd_shift = _pd_shift,					\
7262306a36Sopenharmony_ci		.tuner_reg = _tuner_reg,				\
7362306a36Sopenharmony_ci		.tuner_en_reg = _tuner_en_reg,				\
7462306a36Sopenharmony_ci		.tuner_en_bit = _tuner_en_bit,				\
7562306a36Sopenharmony_ci		.pcw_reg = _pcw_reg,					\
7662306a36Sopenharmony_ci		.pcw_shift = _pcw_shift,				\
7762306a36Sopenharmony_ci		.pcw_chg_reg = _pcw_chg_reg,				\
7862306a36Sopenharmony_ci		.div_table = _div_table,				\
7962306a36Sopenharmony_ci	}
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_ci#define PLL(_id, _name, _reg, _pwr_reg, _en_mask, _flags,		\
8262306a36Sopenharmony_ci			_rst_bar_mask, _pcwbits, _pcwibits, _pd_reg,	\
8362306a36Sopenharmony_ci			_pd_shift, _tuner_reg, _tuner_en_reg,		\
8462306a36Sopenharmony_ci			_tuner_en_bit, _pcw_reg, _pcw_shift,		\
8562306a36Sopenharmony_ci			_pcw_chg_reg)					\
8662306a36Sopenharmony_ci		PLL_B(_id, _name, _reg, _pwr_reg, _en_mask, _flags,	\
8762306a36Sopenharmony_ci			_rst_bar_mask, _pcwbits, _pcwibits, _pd_reg,	\
8862306a36Sopenharmony_ci			_pd_shift, _tuner_reg, _tuner_en_reg,		\
8962306a36Sopenharmony_ci			_tuner_en_bit, _pcw_reg, _pcw_shift,		\
9062306a36Sopenharmony_ci			_pcw_chg_reg, NULL)
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_cistatic const struct mtk_pll_div_table armpll_div_table[] = {
9362306a36Sopenharmony_ci	{ .div = 0, .freq = MT8183_PLL_FMAX },
9462306a36Sopenharmony_ci	{ .div = 1, .freq = 1500 * MHZ },
9562306a36Sopenharmony_ci	{ .div = 2, .freq = 750 * MHZ },
9662306a36Sopenharmony_ci	{ .div = 3, .freq = 375 * MHZ },
9762306a36Sopenharmony_ci	{ .div = 4, .freq = 187500000 },
9862306a36Sopenharmony_ci	{ /* sentinel */ }
9962306a36Sopenharmony_ci};
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_cistatic const struct mtk_pll_div_table mfgpll_div_table[] = {
10262306a36Sopenharmony_ci	{ .div = 0, .freq = MT8183_PLL_FMAX },
10362306a36Sopenharmony_ci	{ .div = 1, .freq = 1600 * MHZ },
10462306a36Sopenharmony_ci	{ .div = 2, .freq = 800 * MHZ },
10562306a36Sopenharmony_ci	{ .div = 3, .freq = 400 * MHZ },
10662306a36Sopenharmony_ci	{ .div = 4, .freq = 200 * MHZ },
10762306a36Sopenharmony_ci	{ /* sentinel */ }
10862306a36Sopenharmony_ci};
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_cistatic const struct mtk_pll_data plls[] = {
11162306a36Sopenharmony_ci	PLL_B(CLK_APMIXED_ARMPLL_LL, "armpll_ll", 0x0200, 0x020C, 0,
11262306a36Sopenharmony_ci	      HAVE_RST_BAR | PLL_AO, BIT(24), 22, 8, 0x0204, 24, 0x0, 0x0, 0,
11362306a36Sopenharmony_ci	      0x0204, 0, 0, armpll_div_table),
11462306a36Sopenharmony_ci	PLL_B(CLK_APMIXED_ARMPLL_L, "armpll_l", 0x0210, 0x021C, 0,
11562306a36Sopenharmony_ci	      HAVE_RST_BAR | PLL_AO, BIT(24), 22, 8, 0x0214, 24, 0x0, 0x0, 0,
11662306a36Sopenharmony_ci	      0x0214, 0, 0, armpll_div_table),
11762306a36Sopenharmony_ci	PLL(CLK_APMIXED_CCIPLL, "ccipll", 0x0290, 0x029C, 0,
11862306a36Sopenharmony_ci	    HAVE_RST_BAR | PLL_AO, BIT(24), 22, 8, 0x0294, 24, 0x0, 0x0, 0,
11962306a36Sopenharmony_ci	    0x0294, 0, 0),
12062306a36Sopenharmony_ci	PLL(CLK_APMIXED_MAINPLL, "mainpll", 0x0220, 0x022C, 0,
12162306a36Sopenharmony_ci	    HAVE_RST_BAR, BIT(24), 22, 8, 0x0224, 24, 0x0, 0x0, 0,
12262306a36Sopenharmony_ci	    0x0224, 0, 0),
12362306a36Sopenharmony_ci	PLL(CLK_APMIXED_UNIV2PLL, "univ2pll", 0x0230, 0x023C, 0,
12462306a36Sopenharmony_ci	    HAVE_RST_BAR, BIT(24), 22, 8, 0x0234, 24, 0x0, 0x0, 0,
12562306a36Sopenharmony_ci	    0x0234, 0, 0),
12662306a36Sopenharmony_ci	PLL_B(CLK_APMIXED_MFGPLL, "mfgpll", 0x0240, 0x024C, 0,
12762306a36Sopenharmony_ci	      0, 0, 22, 8, 0x0244, 24, 0x0, 0x0, 0, 0x0244, 0, 0,
12862306a36Sopenharmony_ci	      mfgpll_div_table),
12962306a36Sopenharmony_ci	PLL(CLK_APMIXED_MSDCPLL, "msdcpll", 0x0250, 0x025C, 0,
13062306a36Sopenharmony_ci	    0, 0, 22, 8, 0x0254, 24, 0x0, 0x0, 0, 0x0254, 0, 0),
13162306a36Sopenharmony_ci	PLL(CLK_APMIXED_TVDPLL, "tvdpll", 0x0260, 0x026C, 0,
13262306a36Sopenharmony_ci	    0, 0, 22, 8, 0x0264, 24, 0x0, 0x0, 0, 0x0264, 0, 0),
13362306a36Sopenharmony_ci	PLL(CLK_APMIXED_MMPLL, "mmpll", 0x0270, 0x027C, 0,
13462306a36Sopenharmony_ci	    HAVE_RST_BAR, BIT(23), 22, 8, 0x0274, 24, 0x0, 0x0, 0,
13562306a36Sopenharmony_ci	    0x0274, 0, 0),
13662306a36Sopenharmony_ci	PLL(CLK_APMIXED_APLL1, "apll1", 0x02A0, 0x02B0, 0,
13762306a36Sopenharmony_ci	    0, 0, 32, 8, 0x02A0, 1, 0x02A8, 0x0014, 0, 0x02A4, 0, 0x02A0),
13862306a36Sopenharmony_ci	PLL(CLK_APMIXED_APLL2, "apll2", 0x02b4, 0x02c4, 0,
13962306a36Sopenharmony_ci	    0, 0, 32, 8, 0x02B4, 1, 0x02BC, 0x0014, 1, 0x02B8, 0, 0x02B4),
14062306a36Sopenharmony_ci};
14162306a36Sopenharmony_ci
14262306a36Sopenharmony_cistatic int clk_mt8183_apmixed_probe(struct platform_device *pdev)
14362306a36Sopenharmony_ci{
14462306a36Sopenharmony_ci	void __iomem *base;
14562306a36Sopenharmony_ci	struct clk_hw_onecell_data *clk_data;
14662306a36Sopenharmony_ci	struct device_node *node = pdev->dev.of_node;
14762306a36Sopenharmony_ci	struct device *dev = &pdev->dev;
14862306a36Sopenharmony_ci	int ret;
14962306a36Sopenharmony_ci
15062306a36Sopenharmony_ci	base = devm_platform_ioremap_resource(pdev, 0);
15162306a36Sopenharmony_ci	if (IS_ERR(base))
15262306a36Sopenharmony_ci		return PTR_ERR(base);
15362306a36Sopenharmony_ci
15462306a36Sopenharmony_ci	clk_data = mtk_devm_alloc_clk_data(dev, CLK_APMIXED_NR_CLK);
15562306a36Sopenharmony_ci	if (!clk_data)
15662306a36Sopenharmony_ci		return -ENOMEM;
15762306a36Sopenharmony_ci
15862306a36Sopenharmony_ci	ret = mtk_clk_register_plls(node, plls, ARRAY_SIZE(plls), clk_data);
15962306a36Sopenharmony_ci	if (ret)
16062306a36Sopenharmony_ci		return ret;
16162306a36Sopenharmony_ci
16262306a36Sopenharmony_ci	ret = mtk_clk_register_gates(&pdev->dev, node, apmixed_clks,
16362306a36Sopenharmony_ci				     ARRAY_SIZE(apmixed_clks), clk_data);
16462306a36Sopenharmony_ci	if (ret)
16562306a36Sopenharmony_ci		goto unregister_plls;
16662306a36Sopenharmony_ci
16762306a36Sopenharmony_ci	ret = of_clk_add_hw_provider(node, of_clk_hw_onecell_get, clk_data);
16862306a36Sopenharmony_ci	if (ret)
16962306a36Sopenharmony_ci		goto unregister_gates;
17062306a36Sopenharmony_ci
17162306a36Sopenharmony_ci	return 0;
17262306a36Sopenharmony_ci
17362306a36Sopenharmony_ciunregister_gates:
17462306a36Sopenharmony_ci	mtk_clk_unregister_gates(apmixed_clks, ARRAY_SIZE(apmixed_clks), clk_data);
17562306a36Sopenharmony_ciunregister_plls:
17662306a36Sopenharmony_ci	mtk_clk_unregister_plls(plls, ARRAY_SIZE(plls), clk_data);
17762306a36Sopenharmony_ci
17862306a36Sopenharmony_ci	return ret;
17962306a36Sopenharmony_ci}
18062306a36Sopenharmony_ci
18162306a36Sopenharmony_cistatic const struct of_device_id of_match_clk_mt8183_apmixed[] = {
18262306a36Sopenharmony_ci	{ .compatible = "mediatek,mt8183-apmixedsys" },
18362306a36Sopenharmony_ci	{ /* sentinel */ }
18462306a36Sopenharmony_ci};
18562306a36Sopenharmony_ciMODULE_DEVICE_TABLE(of, of_match_clk_mt8183_apmixed);
18662306a36Sopenharmony_ci
18762306a36Sopenharmony_cistatic struct platform_driver clk_mt8183_apmixed_drv = {
18862306a36Sopenharmony_ci	.probe = clk_mt8183_apmixed_probe,
18962306a36Sopenharmony_ci	.driver = {
19062306a36Sopenharmony_ci		.name = "clk-mt8183-apmixed",
19162306a36Sopenharmony_ci		.of_match_table = of_match_clk_mt8183_apmixed,
19262306a36Sopenharmony_ci	},
19362306a36Sopenharmony_ci};
19462306a36Sopenharmony_cibuiltin_platform_driver(clk_mt8183_apmixed_drv)
19562306a36Sopenharmony_ciMODULE_LICENSE("GPL");
196