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,videocc-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_cienum {
218c2ecf20Sopenharmony_ci	P_BI_TCXO,
228c2ecf20Sopenharmony_ci	P_CHIP_SLEEP_CLK,
238c2ecf20Sopenharmony_ci	P_CORE_BI_PLL_TEST_SE,
248c2ecf20Sopenharmony_ci	P_VIDEO_PLL0_OUT_EVEN,
258c2ecf20Sopenharmony_ci	P_VIDEO_PLL0_OUT_MAIN,
268c2ecf20Sopenharmony_ci	P_VIDEO_PLL0_OUT_ODD,
278c2ecf20Sopenharmony_ci};
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_cistatic const struct pll_vco fabia_vco[] = {
308c2ecf20Sopenharmony_ci	{ 249600000, 2000000000, 0 },
318c2ecf20Sopenharmony_ci};
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_cistatic struct clk_alpha_pll video_pll0 = {
348c2ecf20Sopenharmony_ci	.offset = 0x42c,
358c2ecf20Sopenharmony_ci	.vco_table = fabia_vco,
368c2ecf20Sopenharmony_ci	.num_vco = ARRAY_SIZE(fabia_vco),
378c2ecf20Sopenharmony_ci	.regs = clk_alpha_pll_regs[CLK_ALPHA_PLL_TYPE_FABIA],
388c2ecf20Sopenharmony_ci	.clkr = {
398c2ecf20Sopenharmony_ci		.hw.init = &(struct clk_init_data){
408c2ecf20Sopenharmony_ci			.name = "video_pll0",
418c2ecf20Sopenharmony_ci			.parent_data = &(const struct clk_parent_data){
428c2ecf20Sopenharmony_ci				.fw_name = "bi_tcxo",
438c2ecf20Sopenharmony_ci			},
448c2ecf20Sopenharmony_ci			.num_parents = 1,
458c2ecf20Sopenharmony_ci			.ops = &clk_alpha_pll_fabia_ops,
468c2ecf20Sopenharmony_ci		},
478c2ecf20Sopenharmony_ci	},
488c2ecf20Sopenharmony_ci};
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_cistatic const struct parent_map video_cc_parent_map_1[] = {
518c2ecf20Sopenharmony_ci	{ P_BI_TCXO, 0 },
528c2ecf20Sopenharmony_ci	{ P_VIDEO_PLL0_OUT_MAIN, 1 },
538c2ecf20Sopenharmony_ci};
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_cistatic const struct clk_parent_data video_cc_parent_data_1[] = {
568c2ecf20Sopenharmony_ci	{ .fw_name = "bi_tcxo" },
578c2ecf20Sopenharmony_ci	{ .hw = &video_pll0.clkr.hw },
588c2ecf20Sopenharmony_ci};
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_cistatic const struct freq_tbl ftbl_video_cc_venus_clk_src[] = {
618c2ecf20Sopenharmony_ci	F(19200000, P_BI_TCXO, 1, 0, 0),
628c2ecf20Sopenharmony_ci	F(150000000, P_VIDEO_PLL0_OUT_MAIN, 4, 0, 0),
638c2ecf20Sopenharmony_ci	F(270000000, P_VIDEO_PLL0_OUT_MAIN, 2.5, 0, 0),
648c2ecf20Sopenharmony_ci	F(340000000, P_VIDEO_PLL0_OUT_MAIN, 2, 0, 0),
658c2ecf20Sopenharmony_ci	F(434000000, P_VIDEO_PLL0_OUT_MAIN, 2, 0, 0),
668c2ecf20Sopenharmony_ci	F(500000000, P_VIDEO_PLL0_OUT_MAIN, 2, 0, 0),
678c2ecf20Sopenharmony_ci	{ }
688c2ecf20Sopenharmony_ci};
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_cistatic struct clk_rcg2 video_cc_venus_clk_src = {
718c2ecf20Sopenharmony_ci	.cmd_rcgr = 0x7f0,
728c2ecf20Sopenharmony_ci	.mnd_width = 0,
738c2ecf20Sopenharmony_ci	.hid_width = 5,
748c2ecf20Sopenharmony_ci	.parent_map = video_cc_parent_map_1,
758c2ecf20Sopenharmony_ci	.freq_tbl = ftbl_video_cc_venus_clk_src,
768c2ecf20Sopenharmony_ci	.clkr.hw.init = &(struct clk_init_data){
778c2ecf20Sopenharmony_ci		.name = "video_cc_venus_clk_src",
788c2ecf20Sopenharmony_ci		.parent_data = video_cc_parent_data_1,
798c2ecf20Sopenharmony_ci		.num_parents = ARRAY_SIZE(video_cc_parent_data_1),
808c2ecf20Sopenharmony_ci		.flags = CLK_SET_RATE_PARENT,
818c2ecf20Sopenharmony_ci		.ops = &clk_rcg2_shared_ops,
828c2ecf20Sopenharmony_ci	},
838c2ecf20Sopenharmony_ci};
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_cistatic struct clk_branch video_cc_vcodec0_axi_clk = {
868c2ecf20Sopenharmony_ci	.halt_reg = 0x9ec,
878c2ecf20Sopenharmony_ci	.halt_check = BRANCH_HALT,
888c2ecf20Sopenharmony_ci	.clkr = {
898c2ecf20Sopenharmony_ci		.enable_reg = 0x9ec,
908c2ecf20Sopenharmony_ci		.enable_mask = BIT(0),
918c2ecf20Sopenharmony_ci		.hw.init = &(struct clk_init_data){
928c2ecf20Sopenharmony_ci			.name = "video_cc_vcodec0_axi_clk",
938c2ecf20Sopenharmony_ci			.ops = &clk_branch2_ops,
948c2ecf20Sopenharmony_ci		},
958c2ecf20Sopenharmony_ci	},
968c2ecf20Sopenharmony_ci};
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_cistatic struct clk_branch video_cc_vcodec0_core_clk = {
998c2ecf20Sopenharmony_ci	.halt_reg = 0x890,
1008c2ecf20Sopenharmony_ci	.halt_check = BRANCH_HALT_VOTED,
1018c2ecf20Sopenharmony_ci	.clkr = {
1028c2ecf20Sopenharmony_ci		.enable_reg = 0x890,
1038c2ecf20Sopenharmony_ci		.enable_mask = BIT(0),
1048c2ecf20Sopenharmony_ci		.hw.init = &(struct clk_init_data){
1058c2ecf20Sopenharmony_ci			.name = "video_cc_vcodec0_core_clk",
1068c2ecf20Sopenharmony_ci			.parent_data = &(const struct clk_parent_data){
1078c2ecf20Sopenharmony_ci				.hw = &video_cc_venus_clk_src.clkr.hw,
1088c2ecf20Sopenharmony_ci			},
1098c2ecf20Sopenharmony_ci			.num_parents = 1,
1108c2ecf20Sopenharmony_ci			.flags = CLK_SET_RATE_PARENT,
1118c2ecf20Sopenharmony_ci			.ops = &clk_branch2_ops,
1128c2ecf20Sopenharmony_ci		},
1138c2ecf20Sopenharmony_ci	},
1148c2ecf20Sopenharmony_ci};
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_cistatic struct clk_branch video_cc_venus_ahb_clk = {
1178c2ecf20Sopenharmony_ci	.halt_reg = 0xa4c,
1188c2ecf20Sopenharmony_ci	.halt_check = BRANCH_HALT,
1198c2ecf20Sopenharmony_ci	.clkr = {
1208c2ecf20Sopenharmony_ci		.enable_reg = 0xa4c,
1218c2ecf20Sopenharmony_ci		.enable_mask = BIT(0),
1228c2ecf20Sopenharmony_ci		.hw.init = &(struct clk_init_data){
1238c2ecf20Sopenharmony_ci			.name = "video_cc_venus_ahb_clk",
1248c2ecf20Sopenharmony_ci			.ops = &clk_branch2_ops,
1258c2ecf20Sopenharmony_ci		},
1268c2ecf20Sopenharmony_ci	},
1278c2ecf20Sopenharmony_ci};
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_cistatic struct clk_branch video_cc_venus_ctl_axi_clk = {
1308c2ecf20Sopenharmony_ci	.halt_reg = 0x9cc,
1318c2ecf20Sopenharmony_ci	.halt_check = BRANCH_HALT,
1328c2ecf20Sopenharmony_ci	.clkr = {
1338c2ecf20Sopenharmony_ci		.enable_reg = 0x9cc,
1348c2ecf20Sopenharmony_ci		.enable_mask = BIT(0),
1358c2ecf20Sopenharmony_ci		.hw.init = &(struct clk_init_data){
1368c2ecf20Sopenharmony_ci			.name = "video_cc_venus_ctl_axi_clk",
1378c2ecf20Sopenharmony_ci			.ops = &clk_branch2_ops,
1388c2ecf20Sopenharmony_ci		},
1398c2ecf20Sopenharmony_ci	},
1408c2ecf20Sopenharmony_ci};
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_cistatic struct clk_branch video_cc_venus_ctl_core_clk = {
1438c2ecf20Sopenharmony_ci	.halt_reg = 0x850,
1448c2ecf20Sopenharmony_ci	.halt_check = BRANCH_HALT,
1458c2ecf20Sopenharmony_ci	.clkr = {
1468c2ecf20Sopenharmony_ci		.enable_reg = 0x850,
1478c2ecf20Sopenharmony_ci		.enable_mask = BIT(0),
1488c2ecf20Sopenharmony_ci		.hw.init = &(struct clk_init_data){
1498c2ecf20Sopenharmony_ci			.name = "video_cc_venus_ctl_core_clk",
1508c2ecf20Sopenharmony_ci			.parent_data = &(const struct clk_parent_data){
1518c2ecf20Sopenharmony_ci				.hw = &video_cc_venus_clk_src.clkr.hw,
1528c2ecf20Sopenharmony_ci			},
1538c2ecf20Sopenharmony_ci			.num_parents = 1,
1548c2ecf20Sopenharmony_ci			.flags = CLK_SET_RATE_PARENT,
1558c2ecf20Sopenharmony_ci			.ops = &clk_branch2_ops,
1568c2ecf20Sopenharmony_ci		},
1578c2ecf20Sopenharmony_ci	},
1588c2ecf20Sopenharmony_ci};
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_cistatic struct gdsc venus_gdsc = {
1618c2ecf20Sopenharmony_ci	.gdscr = 0x814,
1628c2ecf20Sopenharmony_ci	.pd = {
1638c2ecf20Sopenharmony_ci		.name = "venus_gdsc",
1648c2ecf20Sopenharmony_ci	},
1658c2ecf20Sopenharmony_ci	.pwrsts = PWRSTS_OFF_ON,
1668c2ecf20Sopenharmony_ci};
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_cistatic struct gdsc vcodec0_gdsc = {
1698c2ecf20Sopenharmony_ci	.gdscr = 0x874,
1708c2ecf20Sopenharmony_ci	.pd = {
1718c2ecf20Sopenharmony_ci		.name = "vcodec0_gdsc",
1728c2ecf20Sopenharmony_ci	},
1738c2ecf20Sopenharmony_ci	.flags = HW_CTRL,
1748c2ecf20Sopenharmony_ci	.pwrsts = PWRSTS_OFF_ON,
1758c2ecf20Sopenharmony_ci};
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_cistatic struct clk_regmap *video_cc_sc7180_clocks[] = {
1788c2ecf20Sopenharmony_ci	[VIDEO_CC_VCODEC0_AXI_CLK] = &video_cc_vcodec0_axi_clk.clkr,
1798c2ecf20Sopenharmony_ci	[VIDEO_CC_VCODEC0_CORE_CLK] = &video_cc_vcodec0_core_clk.clkr,
1808c2ecf20Sopenharmony_ci	[VIDEO_CC_VENUS_AHB_CLK] = &video_cc_venus_ahb_clk.clkr,
1818c2ecf20Sopenharmony_ci	[VIDEO_CC_VENUS_CLK_SRC] = &video_cc_venus_clk_src.clkr,
1828c2ecf20Sopenharmony_ci	[VIDEO_CC_VENUS_CTL_AXI_CLK] = &video_cc_venus_ctl_axi_clk.clkr,
1838c2ecf20Sopenharmony_ci	[VIDEO_CC_VENUS_CTL_CORE_CLK] = &video_cc_venus_ctl_core_clk.clkr,
1848c2ecf20Sopenharmony_ci	[VIDEO_PLL0] = &video_pll0.clkr,
1858c2ecf20Sopenharmony_ci};
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_cistatic struct gdsc *video_cc_sc7180_gdscs[] = {
1888c2ecf20Sopenharmony_ci	[VENUS_GDSC] = &venus_gdsc,
1898c2ecf20Sopenharmony_ci	[VCODEC0_GDSC] = &vcodec0_gdsc,
1908c2ecf20Sopenharmony_ci};
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_cistatic const struct regmap_config video_cc_sc7180_regmap_config = {
1938c2ecf20Sopenharmony_ci	.reg_bits = 32,
1948c2ecf20Sopenharmony_ci	.reg_stride = 4,
1958c2ecf20Sopenharmony_ci	.val_bits = 32,
1968c2ecf20Sopenharmony_ci	.max_register = 0xb94,
1978c2ecf20Sopenharmony_ci	.fast_io = true,
1988c2ecf20Sopenharmony_ci};
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_cistatic const struct qcom_cc_desc video_cc_sc7180_desc = {
2018c2ecf20Sopenharmony_ci	.config = &video_cc_sc7180_regmap_config,
2028c2ecf20Sopenharmony_ci	.clks = video_cc_sc7180_clocks,
2038c2ecf20Sopenharmony_ci	.num_clks = ARRAY_SIZE(video_cc_sc7180_clocks),
2048c2ecf20Sopenharmony_ci	.gdscs = video_cc_sc7180_gdscs,
2058c2ecf20Sopenharmony_ci	.num_gdscs = ARRAY_SIZE(video_cc_sc7180_gdscs),
2068c2ecf20Sopenharmony_ci};
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_cistatic const struct of_device_id video_cc_sc7180_match_table[] = {
2098c2ecf20Sopenharmony_ci	{ .compatible = "qcom,sc7180-videocc" },
2108c2ecf20Sopenharmony_ci	{ }
2118c2ecf20Sopenharmony_ci};
2128c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, video_cc_sc7180_match_table);
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_cistatic int video_cc_sc7180_probe(struct platform_device *pdev)
2158c2ecf20Sopenharmony_ci{
2168c2ecf20Sopenharmony_ci	struct regmap *regmap;
2178c2ecf20Sopenharmony_ci	struct alpha_pll_config video_pll0_config = {};
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci	regmap = qcom_cc_map(pdev, &video_cc_sc7180_desc);
2208c2ecf20Sopenharmony_ci	if (IS_ERR(regmap))
2218c2ecf20Sopenharmony_ci		return PTR_ERR(regmap);
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci	video_pll0_config.l = 0x1f;
2248c2ecf20Sopenharmony_ci	video_pll0_config.alpha = 0x4000;
2258c2ecf20Sopenharmony_ci	video_pll0_config.user_ctl_val = 0x00000001;
2268c2ecf20Sopenharmony_ci	video_pll0_config.user_ctl_hi_val = 0x00004805;
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci	clk_fabia_pll_configure(&video_pll0, regmap, &video_pll0_config);
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci	/* Keep VIDEO_CC_XO_CLK ALWAYS-ON */
2318c2ecf20Sopenharmony_ci	regmap_update_bits(regmap, 0x984, 0x1, 0x1);
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ci	return qcom_cc_really_probe(pdev, &video_cc_sc7180_desc, regmap);
2348c2ecf20Sopenharmony_ci}
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_cistatic struct platform_driver video_cc_sc7180_driver = {
2378c2ecf20Sopenharmony_ci	.probe = video_cc_sc7180_probe,
2388c2ecf20Sopenharmony_ci	.driver = {
2398c2ecf20Sopenharmony_ci		.name = "sc7180-videocc",
2408c2ecf20Sopenharmony_ci		.of_match_table = video_cc_sc7180_match_table,
2418c2ecf20Sopenharmony_ci	},
2428c2ecf20Sopenharmony_ci};
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_cistatic int __init video_cc_sc7180_init(void)
2458c2ecf20Sopenharmony_ci{
2468c2ecf20Sopenharmony_ci	return platform_driver_register(&video_cc_sc7180_driver);
2478c2ecf20Sopenharmony_ci}
2488c2ecf20Sopenharmony_cisubsys_initcall(video_cc_sc7180_init);
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_cistatic void __exit video_cc_sc7180_exit(void)
2518c2ecf20Sopenharmony_ci{
2528c2ecf20Sopenharmony_ci	platform_driver_unregister(&video_cc_sc7180_driver);
2538c2ecf20Sopenharmony_ci}
2548c2ecf20Sopenharmony_cimodule_exit(video_cc_sc7180_exit);
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
2578c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("QTI VIDEOCC SC7180 Driver");
258