1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2012-2015, The Linux Foundation. All rights reserved.
6 #include <linux/clk-provider.h>
12 * DSI PLL 28nm (8960/A family) - clock diagram (eg: DSI1):
16 * dsi1vco_clk ----o-----| DIV1 |---dsi1pllbit (not exposed as clock)
17 * F * byte_clk | +------+
18 * | bit clock divider (F / 8)
21 * o-----| DIV2 |---dsi0pllbyte---o---> To byte RCG
22 * | +------+ | (sets parent rate)
23 * | byte clock divider (F) |
26 * | (doesn't set parent rate)
29 * o-----| DIV3 |----dsi0pll------o---> To dsi RCG
30 * +------+ | (sets parent rate)
31 * dsi clock divider (F * magic) |
34 * (doesn't set parent rate)
37 #define POLL_MAX_READS 8000
38 #define POLL_TIMEOUT_US 1
40 #define NUM_PROVIDED_CLKS 2
42 #define VCO_REF_CLK_RATE 27000000
43 #define VCO_MIN_RATE 600000000
44 #define VCO_MAX_RATE 1200000000
46 #define DSI_BYTE_PLL_CLK 0
47 #define DSI_PIXEL_PLL_CLK 1
49 #define VCO_PREF_DIV_RATIO 27
51 struct pll_28nm_cached_state
{
52 unsigned long vco_rate
;
64 struct msm_dsi_pll base
;
67 struct platform_device
*pdev
;
70 /* custom byte clock divider */
71 struct clk_bytediv
*bytediv
;
74 struct clk
*clks
[NUM_DSI_CLOCKS_MAX
];
78 struct clk
*provided_clks
[NUM_PROVIDED_CLKS
];
79 struct clk_onecell_data clk_data
;
81 struct pll_28nm_cached_state cached_state
;
84 #define to_pll_28nm(x) container_of(x, struct dsi_pll_28nm, base)
86 static bool pll_28nm_poll_for_ready(struct dsi_pll_28nm
*pll_28nm
,
87 int nb_tries
, int timeout_us
)
89 bool pll_locked
= false;
93 val
= pll_read(pll_28nm
->mmio
+ REG_DSI_28nm_8960_PHY_PLL_RDY
);
94 pll_locked
= !!(val
& DSI_28nm_8960_PHY_PLL_RDY_PLL_RDY
);
101 DBG("DSI PLL is %slocked", pll_locked
? "" : "*not* ");
109 static int dsi_pll_28nm_clk_set_rate(struct clk_hw
*hw
, unsigned long rate
,
110 unsigned long parent_rate
)
112 struct msm_dsi_pll
*pll
= hw_clk_to_pll(hw
);
113 struct dsi_pll_28nm
*pll_28nm
= to_pll_28nm(pll
);
114 void __iomem
*base
= pll_28nm
->mmio
;
115 u32 val
, temp
, fb_divider
;
117 DBG("rate=%lu, parent's=%lu", rate
, parent_rate
);
120 val
= VCO_REF_CLK_RATE
/ 10;
121 fb_divider
= (temp
* VCO_PREF_DIV_RATIO
) / val
;
122 fb_divider
= fb_divider
/ 2 - 1;
123 pll_write(base
+ REG_DSI_28nm_8960_PHY_PLL_CTRL_1
,
126 val
= pll_read(base
+ REG_DSI_28nm_8960_PHY_PLL_CTRL_2
);
128 val
|= (fb_divider
>> 8) & 0x07;
130 pll_write(base
+ REG_DSI_28nm_8960_PHY_PLL_CTRL_2
,
133 val
= pll_read(base
+ REG_DSI_28nm_8960_PHY_PLL_CTRL_3
);
135 val
|= (VCO_PREF_DIV_RATIO
- 1) & 0x3f;
137 pll_write(base
+ REG_DSI_28nm_8960_PHY_PLL_CTRL_3
,
140 pll_write(base
+ REG_DSI_28nm_8960_PHY_PLL_CTRL_6
,
143 val
= pll_read(base
+ REG_DSI_28nm_8960_PHY_PLL_CTRL_8
);
145 pll_write(base
+ REG_DSI_28nm_8960_PHY_PLL_CTRL_8
,
151 static int dsi_pll_28nm_clk_is_enabled(struct clk_hw
*hw
)
153 struct msm_dsi_pll
*pll
= hw_clk_to_pll(hw
);
154 struct dsi_pll_28nm
*pll_28nm
= to_pll_28nm(pll
);
156 return pll_28nm_poll_for_ready(pll_28nm
, POLL_MAX_READS
,
160 static unsigned long dsi_pll_28nm_clk_recalc_rate(struct clk_hw
*hw
,
161 unsigned long parent_rate
)
163 struct msm_dsi_pll
*pll
= hw_clk_to_pll(hw
);
164 struct dsi_pll_28nm
*pll_28nm
= to_pll_28nm(pll
);
165 void __iomem
*base
= pll_28nm
->mmio
;
166 unsigned long vco_rate
;
167 u32 status
, fb_divider
, temp
, ref_divider
;
169 VERB("parent_rate=%lu", parent_rate
);
171 status
= pll_read(base
+ REG_DSI_28nm_8960_PHY_PLL_CTRL_0
);
173 if (status
& DSI_28nm_8960_PHY_PLL_CTRL_0_ENABLE
) {
174 fb_divider
= pll_read(base
+ REG_DSI_28nm_8960_PHY_PLL_CTRL_1
);
176 temp
= pll_read(base
+ REG_DSI_28nm_8960_PHY_PLL_CTRL_2
) & 0x07;
177 fb_divider
= (temp
<< 8) | fb_divider
;
180 ref_divider
= pll_read(base
+ REG_DSI_28nm_8960_PHY_PLL_CTRL_3
);
185 vco_rate
= (parent_rate
/ ref_divider
) * fb_divider
* 2;
190 DBG("returning vco rate = %lu", vco_rate
);
195 static const struct clk_ops clk_ops_dsi_pll_28nm_vco
= {
196 .round_rate
= msm_dsi_pll_helper_clk_round_rate
,
197 .set_rate
= dsi_pll_28nm_clk_set_rate
,
198 .recalc_rate
= dsi_pll_28nm_clk_recalc_rate
,
199 .prepare
= msm_dsi_pll_helper_clk_prepare
,
200 .unprepare
= msm_dsi_pll_helper_clk_unprepare
,
201 .is_enabled
= dsi_pll_28nm_clk_is_enabled
,
205 * Custom byte clock divier clk_ops
207 * This clock is the entry point to configuring the PLL. The user (dsi host)
208 * will set this clock's rate to the desired byte clock rate. The VCO lock
209 * frequency is a multiple of the byte clock rate. The multiplication factor
210 * (shown as F in the diagram above) is a function of the byte clock rate.
212 * This custom divider clock ensures that its parent (VCO) is set to the
213 * desired rate, and that the byte clock postdivider (POSTDIV2) is configured
216 #define to_clk_bytediv(_hw) container_of(_hw, struct clk_bytediv, hw)
218 static unsigned long clk_bytediv_recalc_rate(struct clk_hw
*hw
,
219 unsigned long parent_rate
)
221 struct clk_bytediv
*bytediv
= to_clk_bytediv(hw
);
224 div
= pll_read(bytediv
->reg
) & 0xff;
226 return parent_rate
/ (div
+ 1);
229 /* find multiplication factor(wrt byte clock) at which the VCO should be set */
230 static unsigned int get_vco_mul_factor(unsigned long byte_clk_rate
)
232 unsigned long bit_mhz
;
234 /* convert to bit clock in Mhz */
235 bit_mhz
= (byte_clk_rate
* 8) / 1000000;
239 else if (bit_mhz
< 250)
241 else if (bit_mhz
< 600)
247 static long clk_bytediv_round_rate(struct clk_hw
*hw
, unsigned long rate
,
248 unsigned long *prate
)
250 unsigned long best_parent
;
253 factor
= get_vco_mul_factor(rate
);
255 best_parent
= rate
* factor
;
256 *prate
= clk_hw_round_rate(clk_hw_get_parent(hw
), best_parent
);
258 return *prate
/ factor
;
261 static int clk_bytediv_set_rate(struct clk_hw
*hw
, unsigned long rate
,
262 unsigned long parent_rate
)
264 struct clk_bytediv
*bytediv
= to_clk_bytediv(hw
);
268 factor
= get_vco_mul_factor(rate
);
270 val
= pll_read(bytediv
->reg
);
271 val
|= (factor
- 1) & 0xff;
272 pll_write(bytediv
->reg
, val
);
277 /* Our special byte clock divider ops */
278 static const struct clk_ops clk_bytediv_ops
= {
279 .round_rate
= clk_bytediv_round_rate
,
280 .set_rate
= clk_bytediv_set_rate
,
281 .recalc_rate
= clk_bytediv_recalc_rate
,
287 static int dsi_pll_28nm_enable_seq(struct msm_dsi_pll
*pll
)
289 struct dsi_pll_28nm
*pll_28nm
= to_pll_28nm(pll
);
290 struct device
*dev
= &pll_28nm
->pdev
->dev
;
291 void __iomem
*base
= pll_28nm
->mmio
;
293 unsigned int bit_div
, byte_div
;
294 int max_reads
= 1000, timeout_us
= 100;
297 DBG("id=%d", pll_28nm
->id
);
300 * before enabling the PLL, configure the bit clock divider since we
301 * don't expose it as a clock to the outside world
302 * 1: read back the byte clock divider that should already be set
303 * 2: divide by 8 to get bit clock divider
304 * 3: write it to POSTDIV1
306 val
= pll_read(base
+ REG_DSI_28nm_8960_PHY_PLL_CTRL_9
);
308 bit_div
= byte_div
/ 8;
310 val
= pll_read(base
+ REG_DSI_28nm_8960_PHY_PLL_CTRL_8
);
312 val
|= (bit_div
- 1);
313 pll_write(base
+ REG_DSI_28nm_8960_PHY_PLL_CTRL_8
, val
);
316 pll_write(base
+ REG_DSI_28nm_8960_PHY_PLL_CTRL_0
,
317 DSI_28nm_8960_PHY_PLL_CTRL_0_ENABLE
);
319 locked
= pll_28nm_poll_for_ready(pll_28nm
, max_reads
, timeout_us
);
321 if (unlikely(!locked
))
322 DRM_DEV_ERROR(dev
, "DSI PLL lock failed\n");
324 DBG("DSI PLL lock success");
326 return locked
? 0 : -EINVAL
;
329 static void dsi_pll_28nm_disable_seq(struct msm_dsi_pll
*pll
)
331 struct dsi_pll_28nm
*pll_28nm
= to_pll_28nm(pll
);
333 DBG("id=%d", pll_28nm
->id
);
334 pll_write(pll_28nm
->mmio
+ REG_DSI_28nm_8960_PHY_PLL_CTRL_0
, 0x00);
337 static void dsi_pll_28nm_save_state(struct msm_dsi_pll
*pll
)
339 struct dsi_pll_28nm
*pll_28nm
= to_pll_28nm(pll
);
340 struct pll_28nm_cached_state
*cached_state
= &pll_28nm
->cached_state
;
341 void __iomem
*base
= pll_28nm
->mmio
;
343 cached_state
->postdiv3
=
344 pll_read(base
+ REG_DSI_28nm_8960_PHY_PLL_CTRL_10
);
345 cached_state
->postdiv2
=
346 pll_read(base
+ REG_DSI_28nm_8960_PHY_PLL_CTRL_9
);
347 cached_state
->postdiv1
=
348 pll_read(base
+ REG_DSI_28nm_8960_PHY_PLL_CTRL_8
);
350 cached_state
->vco_rate
= clk_hw_get_rate(&pll
->clk_hw
);
353 static int dsi_pll_28nm_restore_state(struct msm_dsi_pll
*pll
)
355 struct dsi_pll_28nm
*pll_28nm
= to_pll_28nm(pll
);
356 struct pll_28nm_cached_state
*cached_state
= &pll_28nm
->cached_state
;
357 void __iomem
*base
= pll_28nm
->mmio
;
360 ret
= dsi_pll_28nm_clk_set_rate(&pll
->clk_hw
,
361 cached_state
->vco_rate
, 0);
363 DRM_DEV_ERROR(&pll_28nm
->pdev
->dev
,
364 "restore vco rate failed. ret=%d\n", ret
);
368 pll_write(base
+ REG_DSI_28nm_8960_PHY_PLL_CTRL_10
,
369 cached_state
->postdiv3
);
370 pll_write(base
+ REG_DSI_28nm_8960_PHY_PLL_CTRL_9
,
371 cached_state
->postdiv2
);
372 pll_write(base
+ REG_DSI_28nm_8960_PHY_PLL_CTRL_8
,
373 cached_state
->postdiv1
);
378 static int dsi_pll_28nm_get_provider(struct msm_dsi_pll
*pll
,
379 struct clk
**byte_clk_provider
,
380 struct clk
**pixel_clk_provider
)
382 struct dsi_pll_28nm
*pll_28nm
= to_pll_28nm(pll
);
384 if (byte_clk_provider
)
385 *byte_clk_provider
= pll_28nm
->provided_clks
[DSI_BYTE_PLL_CLK
];
386 if (pixel_clk_provider
)
387 *pixel_clk_provider
=
388 pll_28nm
->provided_clks
[DSI_PIXEL_PLL_CLK
];
393 static void dsi_pll_28nm_destroy(struct msm_dsi_pll
*pll
)
395 struct dsi_pll_28nm
*pll_28nm
= to_pll_28nm(pll
);
397 msm_dsi_pll_helper_unregister_clks(pll_28nm
->pdev
,
398 pll_28nm
->clks
, pll_28nm
->num_clks
);
401 static int pll_28nm_register(struct dsi_pll_28nm
*pll_28nm
)
403 char *clk_name
, *parent_name
, *vco_name
;
404 struct clk_init_data vco_init
= {
405 .parent_names
= (const char *[]){ "pxo" },
407 .flags
= CLK_IGNORE_UNUSED
,
408 .ops
= &clk_ops_dsi_pll_28nm_vco
,
410 struct device
*dev
= &pll_28nm
->pdev
->dev
;
411 struct clk
**clks
= pll_28nm
->clks
;
412 struct clk
**provided_clks
= pll_28nm
->provided_clks
;
413 struct clk_bytediv
*bytediv
;
414 struct clk_init_data bytediv_init
= { };
417 DBG("%d", pll_28nm
->id
);
419 bytediv
= devm_kzalloc(dev
, sizeof(*bytediv
), GFP_KERNEL
);
423 vco_name
= devm_kzalloc(dev
, 32, GFP_KERNEL
);
427 parent_name
= devm_kzalloc(dev
, 32, GFP_KERNEL
);
431 clk_name
= devm_kzalloc(dev
, 32, GFP_KERNEL
);
435 pll_28nm
->bytediv
= bytediv
;
437 snprintf(vco_name
, 32, "dsi%dvco_clk", pll_28nm
->id
);
438 vco_init
.name
= vco_name
;
440 pll_28nm
->base
.clk_hw
.init
= &vco_init
;
442 clks
[num
++] = clk_register(dev
, &pll_28nm
->base
.clk_hw
);
444 /* prepare and register bytediv */
445 bytediv
->hw
.init
= &bytediv_init
;
446 bytediv
->reg
= pll_28nm
->mmio
+ REG_DSI_28nm_8960_PHY_PLL_CTRL_9
;
448 snprintf(parent_name
, 32, "dsi%dvco_clk", pll_28nm
->id
);
449 snprintf(clk_name
, 32, "dsi%dpllbyte", pll_28nm
->id
);
451 bytediv_init
.name
= clk_name
;
452 bytediv_init
.ops
= &clk_bytediv_ops
;
453 bytediv_init
.flags
= CLK_SET_RATE_PARENT
;
454 bytediv_init
.parent_names
= (const char * const *) &parent_name
;
455 bytediv_init
.num_parents
= 1;
458 clks
[num
++] = provided_clks
[DSI_BYTE_PLL_CLK
] =
459 clk_register(dev
, &bytediv
->hw
);
461 snprintf(clk_name
, 32, "dsi%dpll", pll_28nm
->id
);
463 clks
[num
++] = provided_clks
[DSI_PIXEL_PLL_CLK
] =
464 clk_register_divider(dev
, clk_name
,
465 parent_name
, 0, pll_28nm
->mmio
+
466 REG_DSI_28nm_8960_PHY_PLL_CTRL_10
,
469 pll_28nm
->num_clks
= num
;
471 pll_28nm
->clk_data
.clk_num
= NUM_PROVIDED_CLKS
;
472 pll_28nm
->clk_data
.clks
= provided_clks
;
474 ret
= of_clk_add_provider(dev
->of_node
,
475 of_clk_src_onecell_get
, &pll_28nm
->clk_data
);
477 DRM_DEV_ERROR(dev
, "failed to register clk provider: %d\n", ret
);
484 struct msm_dsi_pll
*msm_dsi_pll_28nm_8960_init(struct platform_device
*pdev
,
487 struct dsi_pll_28nm
*pll_28nm
;
488 struct msm_dsi_pll
*pll
;
492 return ERR_PTR(-ENODEV
);
494 pll_28nm
= devm_kzalloc(&pdev
->dev
, sizeof(*pll_28nm
), GFP_KERNEL
);
496 return ERR_PTR(-ENOMEM
);
498 pll_28nm
->pdev
= pdev
;
499 pll_28nm
->id
= id
+ 1;
501 pll_28nm
->mmio
= msm_ioremap(pdev
, "dsi_pll", "DSI_PLL");
502 if (IS_ERR_OR_NULL(pll_28nm
->mmio
)) {
503 DRM_DEV_ERROR(&pdev
->dev
, "%s: failed to map pll base\n", __func__
);
504 return ERR_PTR(-ENOMEM
);
507 pll
= &pll_28nm
->base
;
508 pll
->min_rate
= VCO_MIN_RATE
;
509 pll
->max_rate
= VCO_MAX_RATE
;
510 pll
->get_provider
= dsi_pll_28nm_get_provider
;
511 pll
->destroy
= dsi_pll_28nm_destroy
;
512 pll
->disable_seq
= dsi_pll_28nm_disable_seq
;
513 pll
->save_state
= dsi_pll_28nm_save_state
;
514 pll
->restore_state
= dsi_pll_28nm_restore_state
;
517 pll
->enable_seqs
[0] = dsi_pll_28nm_enable_seq
;
519 ret
= pll_28nm_register(pll_28nm
);
521 DRM_DEV_ERROR(&pdev
->dev
, "failed to register PLL: %d\n", ret
);