18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (c) 2019, The Linux Foundation. All rights reserved.
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#include <linux/clk-provider.h>
78c2ecf20Sopenharmony_ci#include <linux/module.h>
88c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
98c2ecf20Sopenharmony_ci#include <linux/regmap.h>
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include <dt-bindings/clock/qcom,gpucc-sc7180.h>
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#include "clk-alpha-pll.h"
148c2ecf20Sopenharmony_ci#include "clk-branch.h"
158c2ecf20Sopenharmony_ci#include "clk-rcg.h"
168c2ecf20Sopenharmony_ci#include "clk-regmap.h"
178c2ecf20Sopenharmony_ci#include "common.h"
188c2ecf20Sopenharmony_ci#include "gdsc.h"
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci#define CX_GMU_CBCR_SLEEP_MASK		0xF
218c2ecf20Sopenharmony_ci#define CX_GMU_CBCR_SLEEP_SHIFT		4
228c2ecf20Sopenharmony_ci#define CX_GMU_CBCR_WAKE_MASK		0xF
238c2ecf20Sopenharmony_ci#define CX_GMU_CBCR_WAKE_SHIFT		8
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_cienum {
268c2ecf20Sopenharmony_ci	P_BI_TCXO,
278c2ecf20Sopenharmony_ci	P_CORE_BI_PLL_TEST_SE,
288c2ecf20Sopenharmony_ci	P_GPLL0_OUT_MAIN,
298c2ecf20Sopenharmony_ci	P_GPLL0_OUT_MAIN_DIV,
308c2ecf20Sopenharmony_ci	P_GPU_CC_PLL1_OUT_EVEN,
318c2ecf20Sopenharmony_ci	P_GPU_CC_PLL1_OUT_MAIN,
328c2ecf20Sopenharmony_ci	P_GPU_CC_PLL1_OUT_ODD,
338c2ecf20Sopenharmony_ci};
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_cistatic const struct pll_vco fabia_vco[] = {
368c2ecf20Sopenharmony_ci	{ 249600000, 2000000000, 0 },
378c2ecf20Sopenharmony_ci};
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_cistatic struct clk_alpha_pll gpu_cc_pll1 = {
408c2ecf20Sopenharmony_ci	.offset = 0x100,
418c2ecf20Sopenharmony_ci	.vco_table = fabia_vco,
428c2ecf20Sopenharmony_ci	.num_vco = ARRAY_SIZE(fabia_vco),
438c2ecf20Sopenharmony_ci	.regs = clk_alpha_pll_regs[CLK_ALPHA_PLL_TYPE_FABIA],
448c2ecf20Sopenharmony_ci	.clkr = {
458c2ecf20Sopenharmony_ci		.hw.init = &(struct clk_init_data){
468c2ecf20Sopenharmony_ci			.name = "gpu_cc_pll1",
478c2ecf20Sopenharmony_ci			.parent_data =  &(const struct clk_parent_data){
488c2ecf20Sopenharmony_ci				.fw_name = "bi_tcxo",
498c2ecf20Sopenharmony_ci			},
508c2ecf20Sopenharmony_ci			.num_parents = 1,
518c2ecf20Sopenharmony_ci			.ops = &clk_alpha_pll_fabia_ops,
528c2ecf20Sopenharmony_ci		},
538c2ecf20Sopenharmony_ci	},
548c2ecf20Sopenharmony_ci};
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_cistatic const struct parent_map gpu_cc_parent_map_0[] = {
578c2ecf20Sopenharmony_ci	{ P_BI_TCXO, 0 },
588c2ecf20Sopenharmony_ci	{ P_GPU_CC_PLL1_OUT_MAIN, 3 },
598c2ecf20Sopenharmony_ci	{ P_GPLL0_OUT_MAIN, 5 },
608c2ecf20Sopenharmony_ci	{ P_GPLL0_OUT_MAIN_DIV, 6 },
618c2ecf20Sopenharmony_ci};
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_cistatic const struct clk_parent_data gpu_cc_parent_data_0[] = {
648c2ecf20Sopenharmony_ci	{ .fw_name = "bi_tcxo" },
658c2ecf20Sopenharmony_ci	{ .hw = &gpu_cc_pll1.clkr.hw },
668c2ecf20Sopenharmony_ci	{ .fw_name = "gcc_gpu_gpll0_clk_src" },
678c2ecf20Sopenharmony_ci	{ .fw_name = "gcc_gpu_gpll0_div_clk_src" },
688c2ecf20Sopenharmony_ci};
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_cistatic const struct freq_tbl ftbl_gpu_cc_gmu_clk_src[] = {
718c2ecf20Sopenharmony_ci	F(19200000, P_BI_TCXO, 1, 0, 0),
728c2ecf20Sopenharmony_ci	F(200000000, P_GPLL0_OUT_MAIN_DIV, 1.5, 0, 0),
738c2ecf20Sopenharmony_ci	{ }
748c2ecf20Sopenharmony_ci};
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_cistatic struct clk_rcg2 gpu_cc_gmu_clk_src = {
778c2ecf20Sopenharmony_ci	.cmd_rcgr = 0x1120,
788c2ecf20Sopenharmony_ci	.mnd_width = 0,
798c2ecf20Sopenharmony_ci	.hid_width = 5,
808c2ecf20Sopenharmony_ci	.parent_map = gpu_cc_parent_map_0,
818c2ecf20Sopenharmony_ci	.freq_tbl = ftbl_gpu_cc_gmu_clk_src,
828c2ecf20Sopenharmony_ci	.clkr.hw.init = &(struct clk_init_data){
838c2ecf20Sopenharmony_ci		.name = "gpu_cc_gmu_clk_src",
848c2ecf20Sopenharmony_ci		.parent_data = gpu_cc_parent_data_0,
858c2ecf20Sopenharmony_ci		.num_parents = ARRAY_SIZE(gpu_cc_parent_data_0),
868c2ecf20Sopenharmony_ci		.flags = CLK_SET_RATE_PARENT,
878c2ecf20Sopenharmony_ci		.ops = &clk_rcg2_shared_ops,
888c2ecf20Sopenharmony_ci	},
898c2ecf20Sopenharmony_ci};
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_cistatic struct clk_branch gpu_cc_crc_ahb_clk = {
928c2ecf20Sopenharmony_ci	.halt_reg = 0x107c,
938c2ecf20Sopenharmony_ci	.halt_check = BRANCH_HALT_DELAY,
948c2ecf20Sopenharmony_ci	.clkr = {
958c2ecf20Sopenharmony_ci		.enable_reg = 0x107c,
968c2ecf20Sopenharmony_ci		.enable_mask = BIT(0),
978c2ecf20Sopenharmony_ci		.hw.init = &(struct clk_init_data){
988c2ecf20Sopenharmony_ci			.name = "gpu_cc_crc_ahb_clk",
998c2ecf20Sopenharmony_ci			.ops = &clk_branch2_ops,
1008c2ecf20Sopenharmony_ci		},
1018c2ecf20Sopenharmony_ci	},
1028c2ecf20Sopenharmony_ci};
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_cistatic struct clk_branch gpu_cc_cx_gmu_clk = {
1058c2ecf20Sopenharmony_ci	.halt_reg = 0x1098,
1068c2ecf20Sopenharmony_ci	.halt_check = BRANCH_HALT,
1078c2ecf20Sopenharmony_ci	.clkr = {
1088c2ecf20Sopenharmony_ci		.enable_reg = 0x1098,
1098c2ecf20Sopenharmony_ci		.enable_mask = BIT(0),
1108c2ecf20Sopenharmony_ci		.hw.init = &(struct clk_init_data){
1118c2ecf20Sopenharmony_ci			.name = "gpu_cc_cx_gmu_clk",
1128c2ecf20Sopenharmony_ci			.parent_data =  &(const struct clk_parent_data){
1138c2ecf20Sopenharmony_ci				.hw = &gpu_cc_gmu_clk_src.clkr.hw,
1148c2ecf20Sopenharmony_ci			},
1158c2ecf20Sopenharmony_ci			.num_parents = 1,
1168c2ecf20Sopenharmony_ci			.flags = CLK_SET_RATE_PARENT,
1178c2ecf20Sopenharmony_ci			.ops = &clk_branch2_ops,
1188c2ecf20Sopenharmony_ci		},
1198c2ecf20Sopenharmony_ci	},
1208c2ecf20Sopenharmony_ci};
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_cistatic struct clk_branch gpu_cc_cx_snoc_dvm_clk = {
1238c2ecf20Sopenharmony_ci	.halt_reg = 0x108c,
1248c2ecf20Sopenharmony_ci	.halt_check = BRANCH_HALT_DELAY,
1258c2ecf20Sopenharmony_ci	.clkr = {
1268c2ecf20Sopenharmony_ci		.enable_reg = 0x108c,
1278c2ecf20Sopenharmony_ci		.enable_mask = BIT(0),
1288c2ecf20Sopenharmony_ci		.hw.init = &(struct clk_init_data){
1298c2ecf20Sopenharmony_ci			.name = "gpu_cc_cx_snoc_dvm_clk",
1308c2ecf20Sopenharmony_ci			.ops = &clk_branch2_ops,
1318c2ecf20Sopenharmony_ci		},
1328c2ecf20Sopenharmony_ci	},
1338c2ecf20Sopenharmony_ci};
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_cistatic struct clk_branch gpu_cc_cxo_aon_clk = {
1368c2ecf20Sopenharmony_ci	.halt_reg = 0x1004,
1378c2ecf20Sopenharmony_ci	.halt_check = BRANCH_HALT_DELAY,
1388c2ecf20Sopenharmony_ci	.clkr = {
1398c2ecf20Sopenharmony_ci		.enable_reg = 0x1004,
1408c2ecf20Sopenharmony_ci		.enable_mask = BIT(0),
1418c2ecf20Sopenharmony_ci		.hw.init = &(struct clk_init_data){
1428c2ecf20Sopenharmony_ci			.name = "gpu_cc_cxo_aon_clk",
1438c2ecf20Sopenharmony_ci			.ops = &clk_branch2_ops,
1448c2ecf20Sopenharmony_ci		},
1458c2ecf20Sopenharmony_ci	},
1468c2ecf20Sopenharmony_ci};
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_cistatic struct clk_branch gpu_cc_cxo_clk = {
1498c2ecf20Sopenharmony_ci	.halt_reg = 0x109c,
1508c2ecf20Sopenharmony_ci	.halt_check = BRANCH_HALT,
1518c2ecf20Sopenharmony_ci	.clkr = {
1528c2ecf20Sopenharmony_ci		.enable_reg = 0x109c,
1538c2ecf20Sopenharmony_ci		.enable_mask = BIT(0),
1548c2ecf20Sopenharmony_ci		.hw.init = &(struct clk_init_data){
1558c2ecf20Sopenharmony_ci			.name = "gpu_cc_cxo_clk",
1568c2ecf20Sopenharmony_ci			.ops = &clk_branch2_ops,
1578c2ecf20Sopenharmony_ci		},
1588c2ecf20Sopenharmony_ci	},
1598c2ecf20Sopenharmony_ci};
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_cistatic struct gdsc cx_gdsc = {
1628c2ecf20Sopenharmony_ci	.gdscr = 0x106c,
1638c2ecf20Sopenharmony_ci	.gds_hw_ctrl = 0x1540,
1648c2ecf20Sopenharmony_ci	.clk_dis_wait_val = 8,
1658c2ecf20Sopenharmony_ci	.pd = {
1668c2ecf20Sopenharmony_ci		.name = "cx_gdsc",
1678c2ecf20Sopenharmony_ci	},
1688c2ecf20Sopenharmony_ci	.pwrsts = PWRSTS_OFF_ON,
1698c2ecf20Sopenharmony_ci	.flags = VOTABLE,
1708c2ecf20Sopenharmony_ci};
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_cistatic struct gdsc gx_gdsc = {
1738c2ecf20Sopenharmony_ci	.gdscr = 0x100c,
1748c2ecf20Sopenharmony_ci	.clamp_io_ctrl = 0x1508,
1758c2ecf20Sopenharmony_ci	.pd = {
1768c2ecf20Sopenharmony_ci		.name = "gx_gdsc",
1778c2ecf20Sopenharmony_ci		.power_on = gdsc_gx_do_nothing_enable,
1788c2ecf20Sopenharmony_ci	},
1798c2ecf20Sopenharmony_ci	.pwrsts = PWRSTS_OFF_ON,
1808c2ecf20Sopenharmony_ci	.flags = CLAMP_IO,
1818c2ecf20Sopenharmony_ci};
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_cistatic struct gdsc *gpu_cc_sc7180_gdscs[] = {
1848c2ecf20Sopenharmony_ci	[CX_GDSC] = &cx_gdsc,
1858c2ecf20Sopenharmony_ci	[GX_GDSC] = &gx_gdsc,
1868c2ecf20Sopenharmony_ci};
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_cistatic struct clk_regmap *gpu_cc_sc7180_clocks[] = {
1898c2ecf20Sopenharmony_ci	[GPU_CC_CXO_CLK] = &gpu_cc_cxo_clk.clkr,
1908c2ecf20Sopenharmony_ci	[GPU_CC_CRC_AHB_CLK] = &gpu_cc_crc_ahb_clk.clkr,
1918c2ecf20Sopenharmony_ci	[GPU_CC_CX_GMU_CLK] = &gpu_cc_cx_gmu_clk.clkr,
1928c2ecf20Sopenharmony_ci	[GPU_CC_CX_SNOC_DVM_CLK] = &gpu_cc_cx_snoc_dvm_clk.clkr,
1938c2ecf20Sopenharmony_ci	[GPU_CC_CXO_AON_CLK] = &gpu_cc_cxo_aon_clk.clkr,
1948c2ecf20Sopenharmony_ci	[GPU_CC_GMU_CLK_SRC] = &gpu_cc_gmu_clk_src.clkr,
1958c2ecf20Sopenharmony_ci	[GPU_CC_PLL1] = &gpu_cc_pll1.clkr,
1968c2ecf20Sopenharmony_ci};
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_cistatic const struct regmap_config gpu_cc_sc7180_regmap_config = {
1998c2ecf20Sopenharmony_ci	.reg_bits =	32,
2008c2ecf20Sopenharmony_ci	.reg_stride =	4,
2018c2ecf20Sopenharmony_ci	.val_bits =	32,
2028c2ecf20Sopenharmony_ci	.max_register =	0x8008,
2038c2ecf20Sopenharmony_ci	.fast_io =	true,
2048c2ecf20Sopenharmony_ci};
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_cistatic const struct qcom_cc_desc gpu_cc_sc7180_desc = {
2078c2ecf20Sopenharmony_ci	.config = &gpu_cc_sc7180_regmap_config,
2088c2ecf20Sopenharmony_ci	.clks = gpu_cc_sc7180_clocks,
2098c2ecf20Sopenharmony_ci	.num_clks = ARRAY_SIZE(gpu_cc_sc7180_clocks),
2108c2ecf20Sopenharmony_ci	.gdscs = gpu_cc_sc7180_gdscs,
2118c2ecf20Sopenharmony_ci	.num_gdscs = ARRAY_SIZE(gpu_cc_sc7180_gdscs),
2128c2ecf20Sopenharmony_ci};
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_cistatic const struct of_device_id gpu_cc_sc7180_match_table[] = {
2158c2ecf20Sopenharmony_ci	{ .compatible = "qcom,sc7180-gpucc" },
2168c2ecf20Sopenharmony_ci	{ }
2178c2ecf20Sopenharmony_ci};
2188c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, gpu_cc_sc7180_match_table);
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_cistatic int gpu_cc_sc7180_probe(struct platform_device *pdev)
2218c2ecf20Sopenharmony_ci{
2228c2ecf20Sopenharmony_ci	struct regmap *regmap;
2238c2ecf20Sopenharmony_ci	struct alpha_pll_config gpu_cc_pll_config = {};
2248c2ecf20Sopenharmony_ci	unsigned int value, mask;
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci	regmap = qcom_cc_map(pdev, &gpu_cc_sc7180_desc);
2278c2ecf20Sopenharmony_ci	if (IS_ERR(regmap))
2288c2ecf20Sopenharmony_ci		return PTR_ERR(regmap);
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci	/* 360MHz Configuration */
2318c2ecf20Sopenharmony_ci	gpu_cc_pll_config.l = 0x12;
2328c2ecf20Sopenharmony_ci	gpu_cc_pll_config.alpha = 0xc000;
2338c2ecf20Sopenharmony_ci	gpu_cc_pll_config.config_ctl_val = 0x20485699;
2348c2ecf20Sopenharmony_ci	gpu_cc_pll_config.config_ctl_hi_val = 0x00002067;
2358c2ecf20Sopenharmony_ci	gpu_cc_pll_config.user_ctl_val = 0x00000001;
2368c2ecf20Sopenharmony_ci	gpu_cc_pll_config.user_ctl_hi_val = 0x00004805;
2378c2ecf20Sopenharmony_ci	gpu_cc_pll_config.test_ctl_hi_val = 0x40000000;
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci	clk_fabia_pll_configure(&gpu_cc_pll1, regmap, &gpu_cc_pll_config);
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci	/* Recommended WAKEUP/SLEEP settings for the gpu_cc_cx_gmu_clk */
2428c2ecf20Sopenharmony_ci	mask = CX_GMU_CBCR_WAKE_MASK << CX_GMU_CBCR_WAKE_SHIFT;
2438c2ecf20Sopenharmony_ci	mask |= CX_GMU_CBCR_SLEEP_MASK << CX_GMU_CBCR_SLEEP_SHIFT;
2448c2ecf20Sopenharmony_ci	value = 0xF << CX_GMU_CBCR_WAKE_SHIFT | 0xF << CX_GMU_CBCR_SLEEP_SHIFT;
2458c2ecf20Sopenharmony_ci	regmap_update_bits(regmap, 0x1098, mask, value);
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci	return qcom_cc_really_probe(pdev, &gpu_cc_sc7180_desc, regmap);
2488c2ecf20Sopenharmony_ci}
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_cistatic struct platform_driver gpu_cc_sc7180_driver = {
2518c2ecf20Sopenharmony_ci	.probe = gpu_cc_sc7180_probe,
2528c2ecf20Sopenharmony_ci	.driver = {
2538c2ecf20Sopenharmony_ci		.name = "sc7180-gpucc",
2548c2ecf20Sopenharmony_ci		.of_match_table = gpu_cc_sc7180_match_table,
2558c2ecf20Sopenharmony_ci	},
2568c2ecf20Sopenharmony_ci};
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_cistatic int __init gpu_cc_sc7180_init(void)
2598c2ecf20Sopenharmony_ci{
2608c2ecf20Sopenharmony_ci	return platform_driver_register(&gpu_cc_sc7180_driver);
2618c2ecf20Sopenharmony_ci}
2628c2ecf20Sopenharmony_cisubsys_initcall(gpu_cc_sc7180_init);
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_cistatic void __exit gpu_cc_sc7180_exit(void)
2658c2ecf20Sopenharmony_ci{
2668c2ecf20Sopenharmony_ci	platform_driver_unregister(&gpu_cc_sc7180_driver);
2678c2ecf20Sopenharmony_ci}
2688c2ecf20Sopenharmony_cimodule_exit(gpu_cc_sc7180_exit);
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("QTI GPU_CC SC7180 Driver");
2718c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
272