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/module.h>
1062306a36Sopenharmony_ci#include <linux/platform_device.h>
1162306a36Sopenharmony_ci#include "clk-cpumux.h"
1262306a36Sopenharmony_ci#include "clk-gate.h"
1362306a36Sopenharmony_ci#include "clk-mtk.h"
1462306a36Sopenharmony_ci#include "reset.h"
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_ci#define GATE_ICG(_id, _name, _parent, _shift)			\
1762306a36Sopenharmony_ci		GATE_MTK(_id, _name, _parent, &infra_cg_regs,	\
1862306a36Sopenharmony_ci			 _shift, &mtk_clk_gate_ops_setclr)
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_cistatic struct clk_hw_onecell_data *infra_clk_data;
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_cistatic const struct mtk_gate_regs infra_cg_regs = {
2362306a36Sopenharmony_ci	.set_ofs = 0x0040,
2462306a36Sopenharmony_ci	.clr_ofs = 0x0044,
2562306a36Sopenharmony_ci	.sta_ofs = 0x0048,
2662306a36Sopenharmony_ci};
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_cistatic const char * const ca53_parents[] __initconst = {
2962306a36Sopenharmony_ci	"clk26m",
3062306a36Sopenharmony_ci	"armca7pll",
3162306a36Sopenharmony_ci	"mainpll",
3262306a36Sopenharmony_ci	"univpll"
3362306a36Sopenharmony_ci};
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_cistatic const char * const ca72_parents[] __initconst = {
3662306a36Sopenharmony_ci	"clk26m",
3762306a36Sopenharmony_ci	"armca15pll",
3862306a36Sopenharmony_ci	"mainpll",
3962306a36Sopenharmony_ci	"univpll"
4062306a36Sopenharmony_ci};
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_cistatic const struct mtk_composite cpu_muxes[] = {
4362306a36Sopenharmony_ci	MUX(CLK_INFRA_CA53SEL, "infra_ca53_sel", ca53_parents, 0x0000, 0, 2),
4462306a36Sopenharmony_ci	MUX(CLK_INFRA_CA72SEL, "infra_ca72_sel", ca72_parents, 0x0000, 2, 2),
4562306a36Sopenharmony_ci};
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_cistatic const struct mtk_fixed_factor infra_early_divs[] = {
4862306a36Sopenharmony_ci	FACTOR(CLK_INFRA_CLK_13M, "clk13m", "clk26m", 1, 2),
4962306a36Sopenharmony_ci};
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_cistatic const struct mtk_gate infra_gates[] = {
5262306a36Sopenharmony_ci	GATE_ICG(CLK_INFRA_DBGCLK, "infra_dbgclk", "axi_sel", 0),
5362306a36Sopenharmony_ci	GATE_ICG(CLK_INFRA_SMI, "infra_smi", "mm_sel", 1),
5462306a36Sopenharmony_ci	GATE_ICG(CLK_INFRA_AUDIO, "infra_audio", "aud_intbus_sel", 5),
5562306a36Sopenharmony_ci	GATE_ICG(CLK_INFRA_GCE, "infra_gce", "axi_sel", 6),
5662306a36Sopenharmony_ci	GATE_ICG(CLK_INFRA_L2C_SRAM, "infra_l2c_sram", "axi_sel", 7),
5762306a36Sopenharmony_ci	GATE_ICG(CLK_INFRA_M4U, "infra_m4u", "mem_sel", 8),
5862306a36Sopenharmony_ci	GATE_ICG(CLK_INFRA_CPUM, "infra_cpum", "cpum_ck", 15),
5962306a36Sopenharmony_ci	GATE_ICG(CLK_INFRA_KP, "infra_kp", "axi_sel", 16),
6062306a36Sopenharmony_ci	GATE_ICG(CLK_INFRA_CEC, "infra_cec", "clk26m", 18),
6162306a36Sopenharmony_ci	GATE_ICG(CLK_INFRA_PMICSPI, "infra_pmicspi", "pmicspi_sel", 22),
6262306a36Sopenharmony_ci	GATE_ICG(CLK_INFRA_PMICWRAP, "infra_pmicwrap", "axi_sel", 23),
6362306a36Sopenharmony_ci};
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_cistatic u16 infrasys_rst_ofs[] = { 0x30, 0x34 };
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_cistatic const struct mtk_clk_rst_desc clk_rst_desc = {
6862306a36Sopenharmony_ci	.version = MTK_RST_SIMPLE,
6962306a36Sopenharmony_ci	.rst_bank_ofs = infrasys_rst_ofs,
7062306a36Sopenharmony_ci	.rst_bank_nr = ARRAY_SIZE(infrasys_rst_ofs),
7162306a36Sopenharmony_ci};
7262306a36Sopenharmony_ci
7362306a36Sopenharmony_cistatic const struct of_device_id of_match_clk_mt8173_infracfg[] = {
7462306a36Sopenharmony_ci	{ .compatible = "mediatek,mt8173-infracfg" },
7562306a36Sopenharmony_ci	{ /* sentinel */ }
7662306a36Sopenharmony_ci};
7762306a36Sopenharmony_ciMODULE_DEVICE_TABLE(of, of_match_clk_mt8173_infracfg);
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_cistatic void clk_mt8173_infra_init_early(struct device_node *node)
8062306a36Sopenharmony_ci{
8162306a36Sopenharmony_ci	int i;
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_ci	infra_clk_data = mtk_alloc_clk_data(CLK_INFRA_NR_CLK);
8462306a36Sopenharmony_ci	if (!infra_clk_data)
8562306a36Sopenharmony_ci		return;
8662306a36Sopenharmony_ci
8762306a36Sopenharmony_ci	for (i = 0; i < CLK_INFRA_NR_CLK; i++)
8862306a36Sopenharmony_ci		infra_clk_data->hws[i] = ERR_PTR(-EPROBE_DEFER);
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_ci	mtk_clk_register_factors(infra_early_divs,
9162306a36Sopenharmony_ci				 ARRAY_SIZE(infra_early_divs), infra_clk_data);
9262306a36Sopenharmony_ci
9362306a36Sopenharmony_ci	of_clk_add_hw_provider(node, of_clk_hw_onecell_get, infra_clk_data);
9462306a36Sopenharmony_ci}
9562306a36Sopenharmony_ciCLK_OF_DECLARE_DRIVER(mtk_infrasys, "mediatek,mt8173-infracfg",
9662306a36Sopenharmony_ci		      clk_mt8173_infra_init_early);
9762306a36Sopenharmony_ci
9862306a36Sopenharmony_cistatic int clk_mt8173_infracfg_probe(struct platform_device *pdev)
9962306a36Sopenharmony_ci{
10062306a36Sopenharmony_ci	struct device_node *node = pdev->dev.of_node;
10162306a36Sopenharmony_ci	int r;
10262306a36Sopenharmony_ci
10362306a36Sopenharmony_ci	r = mtk_clk_register_gates(&pdev->dev, node, infra_gates,
10462306a36Sopenharmony_ci				   ARRAY_SIZE(infra_gates), infra_clk_data);
10562306a36Sopenharmony_ci	if (r)
10662306a36Sopenharmony_ci		return r;
10762306a36Sopenharmony_ci
10862306a36Sopenharmony_ci	r = mtk_clk_register_cpumuxes(&pdev->dev, node, cpu_muxes,
10962306a36Sopenharmony_ci				      ARRAY_SIZE(cpu_muxes), infra_clk_data);
11062306a36Sopenharmony_ci	if (r)
11162306a36Sopenharmony_ci		goto unregister_gates;
11262306a36Sopenharmony_ci
11362306a36Sopenharmony_ci	r = of_clk_add_hw_provider(node, of_clk_hw_onecell_get, infra_clk_data);
11462306a36Sopenharmony_ci	if (r)
11562306a36Sopenharmony_ci		goto unregister_cpumuxes;
11662306a36Sopenharmony_ci
11762306a36Sopenharmony_ci	r = mtk_register_reset_controller_with_dev(&pdev->dev, &clk_rst_desc);
11862306a36Sopenharmony_ci	if (r)
11962306a36Sopenharmony_ci		goto unregister_clk_hw;
12062306a36Sopenharmony_ci
12162306a36Sopenharmony_ci	return 0;
12262306a36Sopenharmony_ci
12362306a36Sopenharmony_ciunregister_clk_hw:
12462306a36Sopenharmony_ci	of_clk_del_provider(node);
12562306a36Sopenharmony_ciunregister_cpumuxes:
12662306a36Sopenharmony_ci	mtk_clk_unregister_cpumuxes(cpu_muxes, ARRAY_SIZE(cpu_muxes), infra_clk_data);
12762306a36Sopenharmony_ciunregister_gates:
12862306a36Sopenharmony_ci	mtk_clk_unregister_gates(infra_gates, ARRAY_SIZE(infra_gates), infra_clk_data);
12962306a36Sopenharmony_ci	return r;
13062306a36Sopenharmony_ci}
13162306a36Sopenharmony_ci
13262306a36Sopenharmony_cistatic void clk_mt8173_infracfg_remove(struct platform_device *pdev)
13362306a36Sopenharmony_ci{
13462306a36Sopenharmony_ci	struct device_node *node = pdev->dev.of_node;
13562306a36Sopenharmony_ci	struct clk_hw_onecell_data *clk_data = platform_get_drvdata(pdev);
13662306a36Sopenharmony_ci
13762306a36Sopenharmony_ci	of_clk_del_provider(node);
13862306a36Sopenharmony_ci	mtk_clk_unregister_cpumuxes(cpu_muxes, ARRAY_SIZE(cpu_muxes), clk_data);
13962306a36Sopenharmony_ci	mtk_clk_unregister_gates(infra_gates, ARRAY_SIZE(infra_gates), clk_data);
14062306a36Sopenharmony_ci	mtk_free_clk_data(clk_data);
14162306a36Sopenharmony_ci}
14262306a36Sopenharmony_ci
14362306a36Sopenharmony_cistatic struct platform_driver clk_mt8173_infracfg_drv = {
14462306a36Sopenharmony_ci	.driver = {
14562306a36Sopenharmony_ci		.name = "clk-mt8173-infracfg",
14662306a36Sopenharmony_ci		.of_match_table = of_match_clk_mt8173_infracfg,
14762306a36Sopenharmony_ci	},
14862306a36Sopenharmony_ci	.probe = clk_mt8173_infracfg_probe,
14962306a36Sopenharmony_ci	.remove_new = clk_mt8173_infracfg_remove,
15062306a36Sopenharmony_ci};
15162306a36Sopenharmony_cimodule_platform_driver(clk_mt8173_infracfg_drv);
15262306a36Sopenharmony_ci
15362306a36Sopenharmony_ciMODULE_DESCRIPTION("MediaTek MT8173 infracfg clocks driver");
15462306a36Sopenharmony_ciMODULE_LICENSE("GPL");
155