1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2018, The Linux Foundation. All rights reserved.
4 */
5
6 #include <linux/clk-provider.h>
7 #include <linux/module.h>
8 #include <linux/platform_device.h>
9 #include <linux/regmap.h>
10
11 #include <dt-bindings/clock/qcom,gpucc-sdm845.h>
12
13 #include "common.h"
14 #include "clk-alpha-pll.h"
15 #include "clk-branch.h"
16 #include "clk-pll.h"
17 #include "clk-rcg.h"
18 #include "clk-regmap.h"
19 #include "gdsc.h"
20
21 #define CX_GMU_CBCR_SLEEP_MASK 0xf
22 #define CX_GMU_CBCR_SLEEP_SHIFT 4
23 #define CX_GMU_CBCR_WAKE_MASK 0xf
24 #define CX_GMU_CBCR_WAKE_SHIFT 8
25
26 enum {
27 P_BI_TCXO,
28 P_CORE_BI_PLL_TEST_SE,
29 P_GPLL0_OUT_MAIN,
30 P_GPLL0_OUT_MAIN_DIV,
31 P_GPU_CC_PLL1_OUT_EVEN,
32 P_GPU_CC_PLL1_OUT_MAIN,
33 P_GPU_CC_PLL1_OUT_ODD,
34 };
35
36 static const struct parent_map gpu_cc_parent_map_0[] = {
37 { P_BI_TCXO, 0 },
38 { P_GPU_CC_PLL1_OUT_MAIN, 3 },
39 { P_GPLL0_OUT_MAIN, 5 },
40 { P_GPLL0_OUT_MAIN_DIV, 6 },
41 { P_CORE_BI_PLL_TEST_SE, 7 },
42 };
43
44 static const char * const gpu_cc_parent_names_0[] = {
45 "bi_tcxo",
46 "gpu_cc_pll1",
47 "gcc_gpu_gpll0_clk_src",
48 "gcc_gpu_gpll0_div_clk_src",
49 "core_bi_pll_test_se",
50 };
51
52 static const struct alpha_pll_config gpu_cc_pll1_config = {
53 .l = 0x1a,
54 .alpha = 0xaab,
55 };
56
57 static struct clk_alpha_pll gpu_cc_pll1 = {
58 .offset = 0x100,
59 .regs = clk_alpha_pll_regs[CLK_ALPHA_PLL_TYPE_FABIA],
60 .clkr = {
61 .hw.init = &(struct clk_init_data){
62 .name = "gpu_cc_pll1",
63 .parent_names = (const char *[]){ "bi_tcxo" },
64 .num_parents = 1,
65 .ops = &clk_alpha_pll_fabia_ops,
66 },
67 },
68 };
69
70 static const struct freq_tbl ftbl_gpu_cc_gmu_clk_src[] = {
71 F(19200000, P_BI_TCXO, 1, 0, 0),
72 F(200000000, P_GPLL0_OUT_MAIN_DIV, 1.5, 0, 0),
73 F(500000000, P_GPU_CC_PLL1_OUT_MAIN, 1, 0, 0),
74 { }
75 };
76
77 static struct clk_rcg2 gpu_cc_gmu_clk_src = {
78 .cmd_rcgr = 0x1120,
79 .mnd_width = 0,
80 .hid_width = 5,
81 .parent_map = gpu_cc_parent_map_0,
82 .freq_tbl = ftbl_gpu_cc_gmu_clk_src,
83 .clkr.hw.init = &(struct clk_init_data){
84 .name = "gpu_cc_gmu_clk_src",
85 .parent_names = gpu_cc_parent_names_0,
86 .num_parents = 5,
87 .ops = &clk_rcg2_shared_ops,
88 },
89 };
90
91 static struct clk_branch gpu_cc_cx_gmu_clk = {
92 .halt_reg = 0x1098,
93 .halt_check = BRANCH_HALT,
94 .clkr = {
95 .enable_reg = 0x1098,
96 .enable_mask = BIT(0),
97 .hw.init = &(struct clk_init_data){
98 .name = "gpu_cc_cx_gmu_clk",
99 .parent_names = (const char *[]){
100 "gpu_cc_gmu_clk_src",
101 },
102 .num_parents = 1,
103 .flags = CLK_SET_RATE_PARENT,
104 .ops = &clk_branch2_ops,
105 },
106 },
107 };
108
109 static struct clk_branch gpu_cc_cxo_clk = {
110 .halt_reg = 0x109c,
111 .halt_check = BRANCH_HALT,
112 .clkr = {
113 .enable_reg = 0x109c,
114 .enable_mask = BIT(0),
115 .hw.init = &(struct clk_init_data){
116 .name = "gpu_cc_cxo_clk",
117 .ops = &clk_branch2_ops,
118 },
119 },
120 };
121
122 static struct gdsc gpu_cx_gdsc = {
123 .gdscr = 0x106c,
124 .gds_hw_ctrl = 0x1540,
125 .clk_dis_wait_val = 0x8,
126 .pd = {
127 .name = "gpu_cx_gdsc",
128 },
129 .pwrsts = PWRSTS_OFF_ON,
130 .flags = VOTABLE,
131 };
132
133 /*
134 * On SDM845 the GPU GX domain is *almost* entirely controlled by the GMU
135 * running in the CX domain so the CPU doesn't need to know anything about the
136 * GX domain EXCEPT....
137 *
138 * Hardware constraints dictate that the GX be powered down before the CX. If
139 * the GMU crashes it could leave the GX on. In order to successfully bring back
140 * the device the CPU needs to disable the GX headswitch. There being no sane
141 * way to reach in and touch that register from deep inside the GPU driver we
142 * need to set up the infrastructure to be able to ensure that the GPU can
143 * ensure that the GX is off during this super special case. We do this by
144 * defining a GX gdsc with a dummy enable function and a "default" disable
145 * function.
146 *
147 * This allows us to attach with genpd_dev_pm_attach_by_name() in the GPU
148 * driver. During power up, nothing will happen from the CPU (and the GMU will
149 * power up normally but during power down this will ensure that the GX domain
150 * is *really* off - this gives us a semi standard way of doing what we need.
151 */
gx_gdsc_enable(struct generic_pm_domain * domain)152 static int gx_gdsc_enable(struct generic_pm_domain *domain)
153 {
154 /* Do nothing but give genpd the impression that we were successful */
155 return 0;
156 }
157
158 static struct gdsc gpu_gx_gdsc = {
159 .gdscr = 0x100c,
160 .clamp_io_ctrl = 0x1508,
161 .pd = {
162 .name = "gpu_gx_gdsc",
163 .power_on = gx_gdsc_enable,
164 },
165 .pwrsts = PWRSTS_OFF_ON,
166 .flags = CLAMP_IO | AON_RESET | POLL_CFG_GDSCR,
167 };
168
169 static struct clk_regmap *gpu_cc_sdm845_clocks[] = {
170 [GPU_CC_CXO_CLK] = &gpu_cc_cxo_clk.clkr,
171 [GPU_CC_CX_GMU_CLK] = &gpu_cc_cx_gmu_clk.clkr,
172 [GPU_CC_GMU_CLK_SRC] = &gpu_cc_gmu_clk_src.clkr,
173 [GPU_CC_PLL1] = &gpu_cc_pll1.clkr,
174 };
175
176 static struct gdsc *gpu_cc_sdm845_gdscs[] = {
177 [GPU_CX_GDSC] = &gpu_cx_gdsc,
178 [GPU_GX_GDSC] = &gpu_gx_gdsc,
179 };
180
181 static const struct regmap_config gpu_cc_sdm845_regmap_config = {
182 .reg_bits = 32,
183 .reg_stride = 4,
184 .val_bits = 32,
185 .max_register = 0x8008,
186 .fast_io = true,
187 };
188
189 static const struct qcom_cc_desc gpu_cc_sdm845_desc = {
190 .config = &gpu_cc_sdm845_regmap_config,
191 .clks = gpu_cc_sdm845_clocks,
192 .num_clks = ARRAY_SIZE(gpu_cc_sdm845_clocks),
193 .gdscs = gpu_cc_sdm845_gdscs,
194 .num_gdscs = ARRAY_SIZE(gpu_cc_sdm845_gdscs),
195 };
196
197 static const struct of_device_id gpu_cc_sdm845_match_table[] = {
198 { .compatible = "qcom,sdm845-gpucc" },
199 { }
200 };
201 MODULE_DEVICE_TABLE(of, gpu_cc_sdm845_match_table);
202
gpu_cc_sdm845_probe(struct platform_device * pdev)203 static int gpu_cc_sdm845_probe(struct platform_device *pdev)
204 {
205 struct regmap *regmap;
206 unsigned int value, mask;
207
208 regmap = qcom_cc_map(pdev, &gpu_cc_sdm845_desc);
209 if (IS_ERR(regmap))
210 return PTR_ERR(regmap);
211
212 clk_fabia_pll_configure(&gpu_cc_pll1, regmap, &gpu_cc_pll1_config);
213
214 /*
215 * Configure gpu_cc_cx_gmu_clk with recommended
216 * wakeup/sleep settings
217 */
218 mask = CX_GMU_CBCR_WAKE_MASK << CX_GMU_CBCR_WAKE_SHIFT;
219 mask |= CX_GMU_CBCR_SLEEP_MASK << CX_GMU_CBCR_SLEEP_SHIFT;
220 value = 0xf << CX_GMU_CBCR_WAKE_SHIFT | 0xf << CX_GMU_CBCR_SLEEP_SHIFT;
221 regmap_update_bits(regmap, 0x1098, mask, value);
222
223 return qcom_cc_really_probe(pdev, &gpu_cc_sdm845_desc, regmap);
224 }
225
226 static struct platform_driver gpu_cc_sdm845_driver = {
227 .probe = gpu_cc_sdm845_probe,
228 .driver = {
229 .name = "sdm845-gpucc",
230 .of_match_table = gpu_cc_sdm845_match_table,
231 .sync_state = clk_sync_state,
232 },
233 };
234
gpu_cc_sdm845_init(void)235 static int __init gpu_cc_sdm845_init(void)
236 {
237 return platform_driver_register(&gpu_cc_sdm845_driver);
238 }
239 subsys_initcall(gpu_cc_sdm845_init);
240
gpu_cc_sdm845_exit(void)241 static void __exit gpu_cc_sdm845_exit(void)
242 {
243 platform_driver_unregister(&gpu_cc_sdm845_driver);
244 }
245 module_exit(gpu_cc_sdm845_exit);
246
247 MODULE_DESCRIPTION("QTI GPUCC SDM845 Driver");
248 MODULE_LICENSE("GPL v2");
249