162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright (c) 2014 MediaTek Inc.
462306a36Sopenharmony_ci * Copyright (c) 2022 Collabora Ltd.
562306a36Sopenharmony_ci * Author: AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com>
662306a36Sopenharmony_ci */
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci#include <dt-bindings/clock/mt8173-clk.h>
962306a36Sopenharmony_ci#include <linux/of_address.h>
1062306a36Sopenharmony_ci#include <linux/module.h>
1162306a36Sopenharmony_ci#include <linux/platform_device.h>
1262306a36Sopenharmony_ci#include "clk-fhctl.h"
1362306a36Sopenharmony_ci#include "clk-mtk.h"
1462306a36Sopenharmony_ci#include "clk-pll.h"
1562306a36Sopenharmony_ci#include "clk-pllfh.h"
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ci#define REGOFF_REF2USB		0x8
1862306a36Sopenharmony_ci#define REGOFF_HDMI_REF		0x40
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci#define MT8173_PLL_FMAX		(3000UL * MHZ)
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_ci#define CON0_MT8173_RST_BAR	BIT(24)
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ci#define PLL_B(_id, _name, _reg, _pwr_reg, _en_mask, _flags, _pcwbits,	\
2562306a36Sopenharmony_ci			_pd_reg, _pd_shift, _tuner_reg, _pcw_reg,	\
2662306a36Sopenharmony_ci			_pcw_shift, _div_table) {			\
2762306a36Sopenharmony_ci		.id = _id,						\
2862306a36Sopenharmony_ci		.name = _name,						\
2962306a36Sopenharmony_ci		.reg = _reg,						\
3062306a36Sopenharmony_ci		.pwr_reg = _pwr_reg,					\
3162306a36Sopenharmony_ci		.en_mask = _en_mask,					\
3262306a36Sopenharmony_ci		.flags = _flags,					\
3362306a36Sopenharmony_ci		.rst_bar_mask = CON0_MT8173_RST_BAR,			\
3462306a36Sopenharmony_ci		.fmax = MT8173_PLL_FMAX,				\
3562306a36Sopenharmony_ci		.pcwbits = _pcwbits,					\
3662306a36Sopenharmony_ci		.pd_reg = _pd_reg,					\
3762306a36Sopenharmony_ci		.pd_shift = _pd_shift,					\
3862306a36Sopenharmony_ci		.tuner_reg = _tuner_reg,				\
3962306a36Sopenharmony_ci		.pcw_reg = _pcw_reg,					\
4062306a36Sopenharmony_ci		.pcw_shift = _pcw_shift,				\
4162306a36Sopenharmony_ci		.div_table = _div_table,				\
4262306a36Sopenharmony_ci	}
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_ci#define PLL(_id, _name, _reg, _pwr_reg, _en_mask, _flags, _pcwbits,	\
4562306a36Sopenharmony_ci			_pd_reg, _pd_shift, _tuner_reg, _pcw_reg,	\
4662306a36Sopenharmony_ci			_pcw_shift)					\
4762306a36Sopenharmony_ci		PLL_B(_id, _name, _reg, _pwr_reg, _en_mask, _flags, _pcwbits, \
4862306a36Sopenharmony_ci			_pd_reg, _pd_shift, _tuner_reg, _pcw_reg, _pcw_shift, \
4962306a36Sopenharmony_ci			NULL)
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_cistatic const struct mtk_pll_div_table mmpll_div_table[] = {
5262306a36Sopenharmony_ci	{ .div = 0, .freq = MT8173_PLL_FMAX },
5362306a36Sopenharmony_ci	{ .div = 1, .freq = 1000000000 },
5462306a36Sopenharmony_ci	{ .div = 2, .freq = 702000000 },
5562306a36Sopenharmony_ci	{ .div = 3, .freq = 253500000 },
5662306a36Sopenharmony_ci	{ .div = 4, .freq = 126750000 },
5762306a36Sopenharmony_ci	{ } /* sentinel */
5862306a36Sopenharmony_ci};
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_cistatic const struct mtk_pll_data plls[] = {
6162306a36Sopenharmony_ci	PLL(CLK_APMIXED_ARMCA15PLL, "armca15pll", 0x200, 0x20c, 0, PLL_AO,
6262306a36Sopenharmony_ci	    21, 0x204, 24, 0x0, 0x204, 0),
6362306a36Sopenharmony_ci	PLL(CLK_APMIXED_ARMCA7PLL, "armca7pll", 0x210, 0x21c, 0, PLL_AO,
6462306a36Sopenharmony_ci	    21, 0x214, 24, 0x0, 0x214, 0),
6562306a36Sopenharmony_ci	PLL(CLK_APMIXED_MAINPLL, "mainpll", 0x220, 0x22c, 0xf0000100, HAVE_RST_BAR, 21,
6662306a36Sopenharmony_ci	    0x220, 4, 0x0, 0x224, 0),
6762306a36Sopenharmony_ci	PLL(CLK_APMIXED_UNIVPLL, "univpll", 0x230, 0x23c, 0xfe000000, HAVE_RST_BAR, 7,
6862306a36Sopenharmony_ci	    0x230, 4, 0x0, 0x234, 14),
6962306a36Sopenharmony_ci	PLL_B(CLK_APMIXED_MMPLL, "mmpll", 0x240, 0x24c, 0, 0, 21, 0x244, 24, 0x0,
7062306a36Sopenharmony_ci	      0x244, 0, mmpll_div_table),
7162306a36Sopenharmony_ci	PLL(CLK_APMIXED_MSDCPLL, "msdcpll", 0x250, 0x25c, 0, 0, 21, 0x250, 4, 0x0, 0x254, 0),
7262306a36Sopenharmony_ci	PLL(CLK_APMIXED_VENCPLL, "vencpll", 0x260, 0x26c, 0, 0, 21, 0x260, 4, 0x0, 0x264, 0),
7362306a36Sopenharmony_ci	PLL(CLK_APMIXED_TVDPLL, "tvdpll", 0x270, 0x27c, 0, 0, 21, 0x270, 4, 0x0, 0x274, 0),
7462306a36Sopenharmony_ci	PLL(CLK_APMIXED_MPLL, "mpll", 0x280, 0x28c, 0, 0, 21, 0x280, 4, 0x0, 0x284, 0),
7562306a36Sopenharmony_ci	PLL(CLK_APMIXED_VCODECPLL, "vcodecpll", 0x290, 0x29c, 0, 0, 21, 0x290, 4, 0x0, 0x294, 0),
7662306a36Sopenharmony_ci	PLL(CLK_APMIXED_APLL1, "apll1", 0x2a0, 0x2b0, 0, 0, 31, 0x2a0, 4, 0x2a4, 0x2a4, 0),
7762306a36Sopenharmony_ci	PLL(CLK_APMIXED_APLL2, "apll2", 0x2b4, 0x2c4, 0, 0, 31, 0x2b4, 4, 0x2b8, 0x2b8, 0),
7862306a36Sopenharmony_ci	PLL(CLK_APMIXED_LVDSPLL, "lvdspll", 0x2d0, 0x2dc, 0, 0, 21, 0x2d0, 4, 0x0, 0x2d4, 0),
7962306a36Sopenharmony_ci	PLL(CLK_APMIXED_MSDCPLL2, "msdcpll2", 0x2f0, 0x2fc, 0, 0, 21, 0x2f0, 4, 0x0, 0x2f4, 0),
8062306a36Sopenharmony_ci};
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_cienum fh_pll_id {
8362306a36Sopenharmony_ci	FH_ARMCA7PLL,
8462306a36Sopenharmony_ci	FH_ARMCA15PLL,
8562306a36Sopenharmony_ci	FH_MAINPLL,
8662306a36Sopenharmony_ci	FH_MPLL,
8762306a36Sopenharmony_ci	FH_MSDCPLL,
8862306a36Sopenharmony_ci	FH_MMPLL,
8962306a36Sopenharmony_ci	FH_VENCPLL,
9062306a36Sopenharmony_ci	FH_TVDPLL,
9162306a36Sopenharmony_ci	FH_VCODECPLL,
9262306a36Sopenharmony_ci	FH_LVDSPLL,
9362306a36Sopenharmony_ci	FH_MSDC2PLL,
9462306a36Sopenharmony_ci	FH_NR_FH,
9562306a36Sopenharmony_ci};
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_ci#define FH(_pllid, _fhid, _offset) {					\
9862306a36Sopenharmony_ci		.data = {						\
9962306a36Sopenharmony_ci			.pll_id = _pllid,				\
10062306a36Sopenharmony_ci			.fh_id = _fhid,					\
10162306a36Sopenharmony_ci			.fh_ver = FHCTL_PLLFH_V1,			\
10262306a36Sopenharmony_ci			.fhx_offset = _offset,				\
10362306a36Sopenharmony_ci			.dds_mask = GENMASK(21, 0),			\
10462306a36Sopenharmony_ci			.slope0_value = 0x6003c97,			\
10562306a36Sopenharmony_ci			.slope1_value = 0x6003c97,			\
10662306a36Sopenharmony_ci			.sfstrx_en = BIT(2),				\
10762306a36Sopenharmony_ci			.frddsx_en = BIT(1),				\
10862306a36Sopenharmony_ci			.fhctlx_en = BIT(0),				\
10962306a36Sopenharmony_ci			.tgl_org = BIT(31),				\
11062306a36Sopenharmony_ci			.dvfs_tri = BIT(31),				\
11162306a36Sopenharmony_ci			.pcwchg = BIT(31),				\
11262306a36Sopenharmony_ci			.dt_val = 0x0,					\
11362306a36Sopenharmony_ci			.df_val = 0x9,					\
11462306a36Sopenharmony_ci			.updnlmt_shft = 16,				\
11562306a36Sopenharmony_ci			.msk_frddsx_dys = GENMASK(23, 20),		\
11662306a36Sopenharmony_ci			.msk_frddsx_dts = GENMASK(19, 16),		\
11762306a36Sopenharmony_ci		},							\
11862306a36Sopenharmony_ci	}
11962306a36Sopenharmony_ci
12062306a36Sopenharmony_cistatic struct mtk_pllfh_data pllfhs[] = {
12162306a36Sopenharmony_ci	FH(CLK_APMIXED_ARMCA7PLL, FH_ARMCA7PLL, 0x38),
12262306a36Sopenharmony_ci	FH(CLK_APMIXED_ARMCA15PLL, FH_ARMCA15PLL, 0x4c),
12362306a36Sopenharmony_ci	FH(CLK_APMIXED_MAINPLL, FH_MAINPLL, 0x60),
12462306a36Sopenharmony_ci	FH(CLK_APMIXED_MPLL, FH_MPLL, 0x74),
12562306a36Sopenharmony_ci	FH(CLK_APMIXED_MSDCPLL, FH_MSDCPLL, 0x88),
12662306a36Sopenharmony_ci	FH(CLK_APMIXED_MMPLL, FH_MMPLL, 0x9c),
12762306a36Sopenharmony_ci	FH(CLK_APMIXED_VENCPLL, FH_VENCPLL, 0xb0),
12862306a36Sopenharmony_ci	FH(CLK_APMIXED_TVDPLL, FH_TVDPLL, 0xc4),
12962306a36Sopenharmony_ci	FH(CLK_APMIXED_VCODECPLL, FH_VCODECPLL, 0xd8),
13062306a36Sopenharmony_ci	FH(CLK_APMIXED_LVDSPLL, FH_LVDSPLL, 0xec),
13162306a36Sopenharmony_ci	FH(CLK_APMIXED_MSDCPLL2, FH_MSDC2PLL, 0x100),
13262306a36Sopenharmony_ci};
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_cistatic const struct of_device_id of_match_clk_mt8173_apmixed[] = {
13562306a36Sopenharmony_ci	{ .compatible = "mediatek,mt8173-apmixedsys" },
13662306a36Sopenharmony_ci	{ /* sentinel */ }
13762306a36Sopenharmony_ci};
13862306a36Sopenharmony_ciMODULE_DEVICE_TABLE(of, of_match_clk_mt8173_apmixed);
13962306a36Sopenharmony_ci
14062306a36Sopenharmony_cistatic int clk_mt8173_apmixed_probe(struct platform_device *pdev)
14162306a36Sopenharmony_ci{
14262306a36Sopenharmony_ci	const u8 *fhctl_node = "mediatek,mt8173-fhctl";
14362306a36Sopenharmony_ci	struct device_node *node = pdev->dev.of_node;
14462306a36Sopenharmony_ci	struct clk_hw_onecell_data *clk_data;
14562306a36Sopenharmony_ci	void __iomem *base;
14662306a36Sopenharmony_ci	struct clk_hw *hw;
14762306a36Sopenharmony_ci	int r;
14862306a36Sopenharmony_ci
14962306a36Sopenharmony_ci	base = of_iomap(node, 0);
15062306a36Sopenharmony_ci	if (!base)
15162306a36Sopenharmony_ci		return -ENOMEM;
15262306a36Sopenharmony_ci
15362306a36Sopenharmony_ci	clk_data = mtk_alloc_clk_data(CLK_APMIXED_NR_CLK);
15462306a36Sopenharmony_ci	if (IS_ERR_OR_NULL(clk_data)) {
15562306a36Sopenharmony_ci		iounmap(base);
15662306a36Sopenharmony_ci		return -ENOMEM;
15762306a36Sopenharmony_ci	}
15862306a36Sopenharmony_ci
15962306a36Sopenharmony_ci	fhctl_parse_dt(fhctl_node, pllfhs, ARRAY_SIZE(pllfhs));
16062306a36Sopenharmony_ci	r = mtk_clk_register_pllfhs(node, plls, ARRAY_SIZE(plls),
16162306a36Sopenharmony_ci				    pllfhs, ARRAY_SIZE(pllfhs), clk_data);
16262306a36Sopenharmony_ci	if (r)
16362306a36Sopenharmony_ci		goto free_clk_data;
16462306a36Sopenharmony_ci
16562306a36Sopenharmony_ci	hw = mtk_clk_register_ref2usb_tx("ref2usb_tx", "clk26m", base + REGOFF_REF2USB);
16662306a36Sopenharmony_ci	if (IS_ERR(hw)) {
16762306a36Sopenharmony_ci		r = PTR_ERR(hw);
16862306a36Sopenharmony_ci		dev_err(&pdev->dev, "Failed to register ref2usb_tx: %d\n", r);
16962306a36Sopenharmony_ci		goto unregister_plls;
17062306a36Sopenharmony_ci	}
17162306a36Sopenharmony_ci	clk_data->hws[CLK_APMIXED_REF2USB_TX] = hw;
17262306a36Sopenharmony_ci
17362306a36Sopenharmony_ci	hw = devm_clk_hw_register_divider(&pdev->dev, "hdmi_ref", "tvdpll_594m", 0,
17462306a36Sopenharmony_ci					  base + REGOFF_HDMI_REF, 16, 3,
17562306a36Sopenharmony_ci					  CLK_DIVIDER_POWER_OF_TWO, NULL);
17662306a36Sopenharmony_ci	clk_data->hws[CLK_APMIXED_HDMI_REF] = hw;
17762306a36Sopenharmony_ci
17862306a36Sopenharmony_ci	r = of_clk_add_hw_provider(node, of_clk_hw_onecell_get, clk_data);
17962306a36Sopenharmony_ci	if (r)
18062306a36Sopenharmony_ci		goto unregister_ref2usb;
18162306a36Sopenharmony_ci
18262306a36Sopenharmony_ci	return 0;
18362306a36Sopenharmony_ci
18462306a36Sopenharmony_ciunregister_ref2usb:
18562306a36Sopenharmony_ci	mtk_clk_unregister_ref2usb_tx(clk_data->hws[CLK_APMIXED_REF2USB_TX]);
18662306a36Sopenharmony_ciunregister_plls:
18762306a36Sopenharmony_ci	mtk_clk_unregister_pllfhs(plls, ARRAY_SIZE(plls), pllfhs,
18862306a36Sopenharmony_ci				  ARRAY_SIZE(pllfhs), clk_data);
18962306a36Sopenharmony_cifree_clk_data:
19062306a36Sopenharmony_ci	mtk_free_clk_data(clk_data);
19162306a36Sopenharmony_ci	iounmap(base);
19262306a36Sopenharmony_ci	return r;
19362306a36Sopenharmony_ci}
19462306a36Sopenharmony_ci
19562306a36Sopenharmony_cistatic void clk_mt8173_apmixed_remove(struct platform_device *pdev)
19662306a36Sopenharmony_ci{
19762306a36Sopenharmony_ci	struct device_node *node = pdev->dev.of_node;
19862306a36Sopenharmony_ci	struct clk_hw_onecell_data *clk_data = platform_get_drvdata(pdev);
19962306a36Sopenharmony_ci
20062306a36Sopenharmony_ci	of_clk_del_provider(node);
20162306a36Sopenharmony_ci	mtk_clk_unregister_ref2usb_tx(clk_data->hws[CLK_APMIXED_REF2USB_TX]);
20262306a36Sopenharmony_ci	mtk_clk_unregister_pllfhs(plls, ARRAY_SIZE(plls), pllfhs,
20362306a36Sopenharmony_ci				  ARRAY_SIZE(pllfhs), clk_data);
20462306a36Sopenharmony_ci	mtk_free_clk_data(clk_data);
20562306a36Sopenharmony_ci}
20662306a36Sopenharmony_ci
20762306a36Sopenharmony_cistatic struct platform_driver clk_mt8173_apmixed_drv = {
20862306a36Sopenharmony_ci	.probe = clk_mt8173_apmixed_probe,
20962306a36Sopenharmony_ci	.remove_new = clk_mt8173_apmixed_remove,
21062306a36Sopenharmony_ci	.driver = {
21162306a36Sopenharmony_ci		.name = "clk-mt8173-apmixed",
21262306a36Sopenharmony_ci		.of_match_table = of_match_clk_mt8173_apmixed,
21362306a36Sopenharmony_ci	},
21462306a36Sopenharmony_ci};
21562306a36Sopenharmony_cimodule_platform_driver(clk_mt8173_apmixed_drv);
21662306a36Sopenharmony_ci
21762306a36Sopenharmony_ciMODULE_DESCRIPTION("MediaTek MT8173 apmixed clocks driver");
21862306a36Sopenharmony_ciMODULE_LICENSE("GPL");
219