2 * Copyright (c) 2012-2015, The Linux Foundation. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 and
6 * only version 2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
14 #include <linux/clk-provider.h>
20 * DSI PLL 28nm (8960/A family) - clock diagram (eg: DSI1):
24 * dsi1vco_clk ----o-----| DIV1 |---dsi1pllbit (not exposed as clock)
25 * F * byte_clk | +------+
26 * | bit clock divider (F / 8)
29 * o-----| DIV2 |---dsi0pllbyte---o---> To byte RCG
30 * | +------+ | (sets parent rate)
31 * | byte clock divider (F) |
34 * | (doesn't set parent rate)
37 * o-----| DIV3 |----dsi0pll------o---> To dsi RCG
38 * +------+ | (sets parent rate)
39 * dsi clock divider (F * magic) |
42 * (doesn't set parent rate)
45 #define POLL_MAX_READS 8000
46 #define POLL_TIMEOUT_US 1
48 #define NUM_PROVIDED_CLKS 2
50 #define VCO_REF_CLK_RATE 27000000
51 #define VCO_MIN_RATE 600000000
52 #define VCO_MAX_RATE 1200000000
54 #define DSI_BYTE_PLL_CLK 0
55 #define DSI_PIXEL_PLL_CLK 1
57 #define VCO_PREF_DIV_RATIO 27
59 struct pll_28nm_cached_state
{
60 unsigned long vco_rate
;
72 struct msm_dsi_pll base
;
75 struct platform_device
*pdev
;
78 /* custom byte clock divider */
79 struct clk_bytediv
*bytediv
;
82 struct clk
*clks
[NUM_DSI_CLOCKS_MAX
];
86 struct clk
*provided_clks
[NUM_PROVIDED_CLKS
];
87 struct clk_onecell_data clk_data
;
89 struct pll_28nm_cached_state cached_state
;
92 #define to_pll_28nm(x) container_of(x, struct dsi_pll_28nm, base)
94 static bool pll_28nm_poll_for_ready(struct dsi_pll_28nm
*pll_28nm
,
95 int nb_tries
, int timeout_us
)
97 bool pll_locked
= false;
101 val
= pll_read(pll_28nm
->mmio
+ REG_DSI_28nm_8960_PHY_PLL_RDY
);
102 pll_locked
= !!(val
& DSI_28nm_8960_PHY_PLL_RDY_PLL_RDY
);
109 DBG("DSI PLL is %slocked", pll_locked
? "" : "*not* ");
117 static int dsi_pll_28nm_clk_set_rate(struct clk_hw
*hw
, unsigned long rate
,
118 unsigned long parent_rate
)
120 struct msm_dsi_pll
*pll
= hw_clk_to_pll(hw
);
121 struct dsi_pll_28nm
*pll_28nm
= to_pll_28nm(pll
);
122 void __iomem
*base
= pll_28nm
->mmio
;
123 u32 val
, temp
, fb_divider
;
125 DBG("rate=%lu, parent's=%lu", rate
, parent_rate
);
128 val
= VCO_REF_CLK_RATE
/ 10;
129 fb_divider
= (temp
* VCO_PREF_DIV_RATIO
) / val
;
130 fb_divider
= fb_divider
/ 2 - 1;
131 pll_write(base
+ REG_DSI_28nm_8960_PHY_PLL_CTRL_1
,
134 val
= pll_read(base
+ REG_DSI_28nm_8960_PHY_PLL_CTRL_2
);
136 val
|= (fb_divider
>> 8) & 0x07;
138 pll_write(base
+ REG_DSI_28nm_8960_PHY_PLL_CTRL_2
,
141 val
= pll_read(base
+ REG_DSI_28nm_8960_PHY_PLL_CTRL_3
);
143 val
|= (VCO_PREF_DIV_RATIO
- 1) & 0x3f;
145 pll_write(base
+ REG_DSI_28nm_8960_PHY_PLL_CTRL_3
,
148 pll_write(base
+ REG_DSI_28nm_8960_PHY_PLL_CTRL_6
,
151 val
= pll_read(base
+ REG_DSI_28nm_8960_PHY_PLL_CTRL_8
);
153 pll_write(base
+ REG_DSI_28nm_8960_PHY_PLL_CTRL_8
,
159 static int dsi_pll_28nm_clk_is_enabled(struct clk_hw
*hw
)
161 struct msm_dsi_pll
*pll
= hw_clk_to_pll(hw
);
162 struct dsi_pll_28nm
*pll_28nm
= to_pll_28nm(pll
);
164 return pll_28nm_poll_for_ready(pll_28nm
, POLL_MAX_READS
,
168 static unsigned long dsi_pll_28nm_clk_recalc_rate(struct clk_hw
*hw
,
169 unsigned long parent_rate
)
171 struct msm_dsi_pll
*pll
= hw_clk_to_pll(hw
);
172 struct dsi_pll_28nm
*pll_28nm
= to_pll_28nm(pll
);
173 void __iomem
*base
= pll_28nm
->mmio
;
174 unsigned long vco_rate
;
175 u32 status
, fb_divider
, temp
, ref_divider
;
177 VERB("parent_rate=%lu", parent_rate
);
179 status
= pll_read(base
+ REG_DSI_28nm_8960_PHY_PLL_CTRL_0
);
181 if (status
& DSI_28nm_8960_PHY_PLL_CTRL_0_ENABLE
) {
182 fb_divider
= pll_read(base
+ REG_DSI_28nm_8960_PHY_PLL_CTRL_1
);
184 temp
= pll_read(base
+ REG_DSI_28nm_8960_PHY_PLL_CTRL_2
) & 0x07;
185 fb_divider
= (temp
<< 8) | fb_divider
;
188 ref_divider
= pll_read(base
+ REG_DSI_28nm_8960_PHY_PLL_CTRL_3
);
193 vco_rate
= (parent_rate
/ ref_divider
) * fb_divider
* 2;
198 DBG("returning vco rate = %lu", vco_rate
);
203 static const struct clk_ops clk_ops_dsi_pll_28nm_vco
= {
204 .round_rate
= msm_dsi_pll_helper_clk_round_rate
,
205 .set_rate
= dsi_pll_28nm_clk_set_rate
,
206 .recalc_rate
= dsi_pll_28nm_clk_recalc_rate
,
207 .prepare
= msm_dsi_pll_helper_clk_prepare
,
208 .unprepare
= msm_dsi_pll_helper_clk_unprepare
,
209 .is_enabled
= dsi_pll_28nm_clk_is_enabled
,
213 * Custom byte clock divier clk_ops
215 * This clock is the entry point to configuring the PLL. The user (dsi host)
216 * will set this clock's rate to the desired byte clock rate. The VCO lock
217 * frequency is a multiple of the byte clock rate. The multiplication factor
218 * (shown as F in the diagram above) is a function of the byte clock rate.
220 * This custom divider clock ensures that its parent (VCO) is set to the
221 * desired rate, and that the byte clock postdivider (POSTDIV2) is configured
224 #define to_clk_bytediv(_hw) container_of(_hw, struct clk_bytediv, hw)
226 static unsigned long clk_bytediv_recalc_rate(struct clk_hw
*hw
,
227 unsigned long parent_rate
)
229 struct clk_bytediv
*bytediv
= to_clk_bytediv(hw
);
232 div
= pll_read(bytediv
->reg
) & 0xff;
234 return parent_rate
/ (div
+ 1);
237 /* find multiplication factor(wrt byte clock) at which the VCO should be set */
238 static unsigned int get_vco_mul_factor(unsigned long byte_clk_rate
)
240 unsigned long bit_mhz
;
242 /* convert to bit clock in Mhz */
243 bit_mhz
= (byte_clk_rate
* 8) / 1000000;
247 else if (bit_mhz
< 250)
249 else if (bit_mhz
< 600)
255 static long clk_bytediv_round_rate(struct clk_hw
*hw
, unsigned long rate
,
256 unsigned long *prate
)
258 unsigned long best_parent
;
261 factor
= get_vco_mul_factor(rate
);
263 best_parent
= rate
* factor
;
264 *prate
= clk_hw_round_rate(clk_hw_get_parent(hw
), best_parent
);
266 return *prate
/ factor
;
269 static int clk_bytediv_set_rate(struct clk_hw
*hw
, unsigned long rate
,
270 unsigned long parent_rate
)
272 struct clk_bytediv
*bytediv
= to_clk_bytediv(hw
);
276 factor
= get_vco_mul_factor(rate
);
278 val
= pll_read(bytediv
->reg
);
279 val
|= (factor
- 1) & 0xff;
280 pll_write(bytediv
->reg
, val
);
285 /* Our special byte clock divider ops */
286 static const struct clk_ops clk_bytediv_ops
= {
287 .round_rate
= clk_bytediv_round_rate
,
288 .set_rate
= clk_bytediv_set_rate
,
289 .recalc_rate
= clk_bytediv_recalc_rate
,
295 static int dsi_pll_28nm_enable_seq(struct msm_dsi_pll
*pll
)
297 struct dsi_pll_28nm
*pll_28nm
= to_pll_28nm(pll
);
298 struct device
*dev
= &pll_28nm
->pdev
->dev
;
299 void __iomem
*base
= pll_28nm
->mmio
;
301 unsigned int bit_div
, byte_div
;
302 int max_reads
= 1000, timeout_us
= 100;
305 DBG("id=%d", pll_28nm
->id
);
308 * before enabling the PLL, configure the bit clock divider since we
309 * don't expose it as a clock to the outside world
310 * 1: read back the byte clock divider that should already be set
311 * 2: divide by 8 to get bit clock divider
312 * 3: write it to POSTDIV1
314 val
= pll_read(base
+ REG_DSI_28nm_8960_PHY_PLL_CTRL_9
);
316 bit_div
= byte_div
/ 8;
318 val
= pll_read(base
+ REG_DSI_28nm_8960_PHY_PLL_CTRL_8
);
320 val
|= (bit_div
- 1);
321 pll_write(base
+ REG_DSI_28nm_8960_PHY_PLL_CTRL_8
, val
);
324 pll_write(base
+ REG_DSI_28nm_8960_PHY_PLL_CTRL_0
,
325 DSI_28nm_8960_PHY_PLL_CTRL_0_ENABLE
);
327 locked
= pll_28nm_poll_for_ready(pll_28nm
, max_reads
, timeout_us
);
329 if (unlikely(!locked
))
330 DRM_DEV_ERROR(dev
, "DSI PLL lock failed\n");
332 DBG("DSI PLL lock success");
334 return locked
? 0 : -EINVAL
;
337 static void dsi_pll_28nm_disable_seq(struct msm_dsi_pll
*pll
)
339 struct dsi_pll_28nm
*pll_28nm
= to_pll_28nm(pll
);
341 DBG("id=%d", pll_28nm
->id
);
342 pll_write(pll_28nm
->mmio
+ REG_DSI_28nm_8960_PHY_PLL_CTRL_0
, 0x00);
345 static void dsi_pll_28nm_save_state(struct msm_dsi_pll
*pll
)
347 struct dsi_pll_28nm
*pll_28nm
= to_pll_28nm(pll
);
348 struct pll_28nm_cached_state
*cached_state
= &pll_28nm
->cached_state
;
349 void __iomem
*base
= pll_28nm
->mmio
;
351 cached_state
->postdiv3
=
352 pll_read(base
+ REG_DSI_28nm_8960_PHY_PLL_CTRL_10
);
353 cached_state
->postdiv2
=
354 pll_read(base
+ REG_DSI_28nm_8960_PHY_PLL_CTRL_9
);
355 cached_state
->postdiv1
=
356 pll_read(base
+ REG_DSI_28nm_8960_PHY_PLL_CTRL_8
);
358 cached_state
->vco_rate
= clk_hw_get_rate(&pll
->clk_hw
);
361 static int dsi_pll_28nm_restore_state(struct msm_dsi_pll
*pll
)
363 struct dsi_pll_28nm
*pll_28nm
= to_pll_28nm(pll
);
364 struct pll_28nm_cached_state
*cached_state
= &pll_28nm
->cached_state
;
365 void __iomem
*base
= pll_28nm
->mmio
;
368 ret
= dsi_pll_28nm_clk_set_rate(&pll
->clk_hw
,
369 cached_state
->vco_rate
, 0);
371 DRM_DEV_ERROR(&pll_28nm
->pdev
->dev
,
372 "restore vco rate failed. ret=%d\n", ret
);
376 pll_write(base
+ REG_DSI_28nm_8960_PHY_PLL_CTRL_10
,
377 cached_state
->postdiv3
);
378 pll_write(base
+ REG_DSI_28nm_8960_PHY_PLL_CTRL_9
,
379 cached_state
->postdiv2
);
380 pll_write(base
+ REG_DSI_28nm_8960_PHY_PLL_CTRL_8
,
381 cached_state
->postdiv1
);
386 static int dsi_pll_28nm_get_provider(struct msm_dsi_pll
*pll
,
387 struct clk
**byte_clk_provider
,
388 struct clk
**pixel_clk_provider
)
390 struct dsi_pll_28nm
*pll_28nm
= to_pll_28nm(pll
);
392 if (byte_clk_provider
)
393 *byte_clk_provider
= pll_28nm
->provided_clks
[DSI_BYTE_PLL_CLK
];
394 if (pixel_clk_provider
)
395 *pixel_clk_provider
=
396 pll_28nm
->provided_clks
[DSI_PIXEL_PLL_CLK
];
401 static void dsi_pll_28nm_destroy(struct msm_dsi_pll
*pll
)
403 struct dsi_pll_28nm
*pll_28nm
= to_pll_28nm(pll
);
405 msm_dsi_pll_helper_unregister_clks(pll_28nm
->pdev
,
406 pll_28nm
->clks
, pll_28nm
->num_clks
);
409 static int pll_28nm_register(struct dsi_pll_28nm
*pll_28nm
)
411 char *clk_name
, *parent_name
, *vco_name
;
412 struct clk_init_data vco_init
= {
413 .parent_names
= (const char *[]){ "pxo" },
415 .flags
= CLK_IGNORE_UNUSED
,
416 .ops
= &clk_ops_dsi_pll_28nm_vco
,
418 struct device
*dev
= &pll_28nm
->pdev
->dev
;
419 struct clk
**clks
= pll_28nm
->clks
;
420 struct clk
**provided_clks
= pll_28nm
->provided_clks
;
421 struct clk_bytediv
*bytediv
;
422 struct clk_init_data bytediv_init
= { };
425 DBG("%d", pll_28nm
->id
);
427 bytediv
= devm_kzalloc(dev
, sizeof(*bytediv
), GFP_KERNEL
);
431 vco_name
= devm_kzalloc(dev
, 32, GFP_KERNEL
);
435 parent_name
= devm_kzalloc(dev
, 32, GFP_KERNEL
);
439 clk_name
= devm_kzalloc(dev
, 32, GFP_KERNEL
);
443 pll_28nm
->bytediv
= bytediv
;
445 snprintf(vco_name
, 32, "dsi%dvco_clk", pll_28nm
->id
);
446 vco_init
.name
= vco_name
;
448 pll_28nm
->base
.clk_hw
.init
= &vco_init
;
450 clks
[num
++] = clk_register(dev
, &pll_28nm
->base
.clk_hw
);
452 /* prepare and register bytediv */
453 bytediv
->hw
.init
= &bytediv_init
;
454 bytediv
->reg
= pll_28nm
->mmio
+ REG_DSI_28nm_8960_PHY_PLL_CTRL_9
;
456 snprintf(parent_name
, 32, "dsi%dvco_clk", pll_28nm
->id
);
457 snprintf(clk_name
, 32, "dsi%dpllbyte", pll_28nm
->id
);
459 bytediv_init
.name
= clk_name
;
460 bytediv_init
.ops
= &clk_bytediv_ops
;
461 bytediv_init
.flags
= CLK_SET_RATE_PARENT
;
462 bytediv_init
.parent_names
= (const char * const *) &parent_name
;
463 bytediv_init
.num_parents
= 1;
466 clks
[num
++] = provided_clks
[DSI_BYTE_PLL_CLK
] =
467 clk_register(dev
, &bytediv
->hw
);
469 snprintf(clk_name
, 32, "dsi%dpll", pll_28nm
->id
);
471 clks
[num
++] = provided_clks
[DSI_PIXEL_PLL_CLK
] =
472 clk_register_divider(dev
, clk_name
,
473 parent_name
, 0, pll_28nm
->mmio
+
474 REG_DSI_28nm_8960_PHY_PLL_CTRL_10
,
477 pll_28nm
->num_clks
= num
;
479 pll_28nm
->clk_data
.clk_num
= NUM_PROVIDED_CLKS
;
480 pll_28nm
->clk_data
.clks
= provided_clks
;
482 ret
= of_clk_add_provider(dev
->of_node
,
483 of_clk_src_onecell_get
, &pll_28nm
->clk_data
);
485 DRM_DEV_ERROR(dev
, "failed to register clk provider: %d\n", ret
);
492 struct msm_dsi_pll
*msm_dsi_pll_28nm_8960_init(struct platform_device
*pdev
,
495 struct dsi_pll_28nm
*pll_28nm
;
496 struct msm_dsi_pll
*pll
;
500 return ERR_PTR(-ENODEV
);
502 pll_28nm
= devm_kzalloc(&pdev
->dev
, sizeof(*pll_28nm
), GFP_KERNEL
);
504 return ERR_PTR(-ENOMEM
);
506 pll_28nm
->pdev
= pdev
;
507 pll_28nm
->id
= id
+ 1;
509 pll_28nm
->mmio
= msm_ioremap(pdev
, "dsi_pll", "DSI_PLL");
510 if (IS_ERR_OR_NULL(pll_28nm
->mmio
)) {
511 DRM_DEV_ERROR(&pdev
->dev
, "%s: failed to map pll base\n", __func__
);
512 return ERR_PTR(-ENOMEM
);
515 pll
= &pll_28nm
->base
;
516 pll
->min_rate
= VCO_MIN_RATE
;
517 pll
->max_rate
= VCO_MAX_RATE
;
518 pll
->get_provider
= dsi_pll_28nm_get_provider
;
519 pll
->destroy
= dsi_pll_28nm_destroy
;
520 pll
->disable_seq
= dsi_pll_28nm_disable_seq
;
521 pll
->save_state
= dsi_pll_28nm_save_state
;
522 pll
->restore_state
= dsi_pll_28nm_restore_state
;
525 pll
->enable_seqs
[0] = dsi_pll_28nm_enable_seq
;
527 ret
= pll_28nm_register(pll_28nm
);
529 DRM_DEV_ERROR(&pdev
->dev
, "failed to register PLL: %d\n", ret
);