1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2014 Texas Instruments Incorporated - http://www.ti.com/
6 #define DSS_SUBSYS_NAME "PLL"
8 #include <linux/delay.h>
11 #include <linux/kernel.h>
12 #include <linux/regulator/consumer.h>
13 #include <linux/sched.h>
18 #define PLL_CONTROL 0x0000
19 #define PLL_STATUS 0x0004
21 #define PLL_CONFIGURATION1 0x000C
22 #define PLL_CONFIGURATION2 0x0010
23 #define PLL_CONFIGURATION3 0x0014
24 #define PLL_SSC_CONFIGURATION1 0x0018
25 #define PLL_SSC_CONFIGURATION2 0x001C
26 #define PLL_CONFIGURATION4 0x0020
28 int dss_pll_register(struct dss_device
*dss
, struct dss_pll
*pll
)
32 for (i
= 0; i
< ARRAY_SIZE(dss
->plls
); ++i
) {
43 void dss_pll_unregister(struct dss_pll
*pll
)
45 struct dss_device
*dss
= pll
->dss
;
48 for (i
= 0; i
< ARRAY_SIZE(dss
->plls
); ++i
) {
49 if (dss
->plls
[i
] == pll
) {
57 struct dss_pll
*dss_pll_find(struct dss_device
*dss
, const char *name
)
61 for (i
= 0; i
< ARRAY_SIZE(dss
->plls
); ++i
) {
62 if (dss
->plls
[i
] && strcmp(dss
->plls
[i
]->name
, name
) == 0)
69 struct dss_pll
*dss_pll_find_by_src(struct dss_device
*dss
,
70 enum dss_clk_source src
)
79 case DSS_CLK_SRC_HDMI_PLL
:
80 return dss_pll_find(dss
, "hdmi");
82 case DSS_CLK_SRC_PLL1_1
:
83 case DSS_CLK_SRC_PLL1_2
:
84 case DSS_CLK_SRC_PLL1_3
:
85 pll
= dss_pll_find(dss
, "dsi0");
87 pll
= dss_pll_find(dss
, "video0");
90 case DSS_CLK_SRC_PLL2_1
:
91 case DSS_CLK_SRC_PLL2_2
:
92 case DSS_CLK_SRC_PLL2_3
:
93 pll
= dss_pll_find(dss
, "dsi1");
95 pll
= dss_pll_find(dss
, "video1");
100 unsigned int dss_pll_get_clkout_idx_for_src(enum dss_clk_source src
)
103 case DSS_CLK_SRC_HDMI_PLL
:
106 case DSS_CLK_SRC_PLL1_1
:
107 case DSS_CLK_SRC_PLL2_1
:
110 case DSS_CLK_SRC_PLL1_2
:
111 case DSS_CLK_SRC_PLL2_2
:
114 case DSS_CLK_SRC_PLL1_3
:
115 case DSS_CLK_SRC_PLL2_3
:
123 int dss_pll_enable(struct dss_pll
*pll
)
127 r
= clk_prepare_enable(pll
->clkin
);
131 if (pll
->regulator
) {
132 r
= regulator_enable(pll
->regulator
);
137 r
= pll
->ops
->enable(pll
);
145 regulator_disable(pll
->regulator
);
147 clk_disable_unprepare(pll
->clkin
);
151 void dss_pll_disable(struct dss_pll
*pll
)
153 pll
->ops
->disable(pll
);
156 regulator_disable(pll
->regulator
);
158 clk_disable_unprepare(pll
->clkin
);
160 memset(&pll
->cinfo
, 0, sizeof(pll
->cinfo
));
163 int dss_pll_set_config(struct dss_pll
*pll
, const struct dss_pll_clock_info
*cinfo
)
167 r
= pll
->ops
->set_config(pll
, cinfo
);
176 bool dss_pll_hsdiv_calc_a(const struct dss_pll
*pll
, unsigned long clkdco
,
177 unsigned long out_min
, unsigned long out_max
,
178 dss_hsdiv_calc_func func
, void *data
)
180 const struct dss_pll_hw
*hw
= pll
->hw
;
181 int m
, m_start
, m_stop
;
184 out_min
= out_min
? out_min
: 1;
185 out_max
= out_max
? out_max
: ULONG_MAX
;
187 m_start
= max(DIV_ROUND_UP(clkdco
, out_max
), 1ul);
189 m_stop
= min((unsigned)(clkdco
/ out_min
), hw
->mX_max
);
191 for (m
= m_start
; m
<= m_stop
; ++m
) {
194 if (func(m
, out
, data
))
202 * clkdco = clkin / n * m * 2
203 * clkoutX = clkdco / mX
205 bool dss_pll_calc_a(const struct dss_pll
*pll
, unsigned long clkin
,
206 unsigned long pll_min
, unsigned long pll_max
,
207 dss_pll_calc_func func
, void *data
)
209 const struct dss_pll_hw
*hw
= pll
->hw
;
210 int n
, n_start
, n_stop
, n_inc
;
211 int m
, m_start
, m_stop
, m_inc
;
212 unsigned long fint
, clkdco
;
213 unsigned long pll_hw_max
;
214 unsigned long fint_hw_min
, fint_hw_max
;
216 pll_hw_max
= hw
->clkdco_max
;
218 fint_hw_min
= hw
->fint_min
;
219 fint_hw_max
= hw
->fint_max
;
221 n_start
= max(DIV_ROUND_UP(clkin
, fint_hw_max
), 1ul);
222 n_stop
= min((unsigned)(clkin
/ fint_hw_min
), hw
->n_max
);
225 if (hw
->errata_i886
) {
226 swap(n_start
, n_stop
);
230 pll_max
= pll_max
? pll_max
: ULONG_MAX
;
232 for (n
= n_start
; n
!= n_stop
; n
+= n_inc
) {
235 m_start
= max(DIV_ROUND_UP(DIV_ROUND_UP(pll_min
, fint
), 2),
237 m_stop
= min3((unsigned)(pll_max
/ fint
/ 2),
238 (unsigned)(pll_hw_max
/ fint
/ 2),
242 if (hw
->errata_i886
) {
243 swap(m_start
, m_stop
);
247 for (m
= m_start
; m
!= m_stop
; m
+= m_inc
) {
248 clkdco
= 2 * m
* fint
;
250 if (func(n
, m
, fint
, clkdco
, data
))
259 * This calculates a PLL config that will provide the target_clkout rate
260 * for clkout. Additionally clkdco rate will be the same as clkout rate
261 * when clkout rate is >= min_clkdco.
263 * clkdco = clkin / n * m + clkin / n * mf / 262144
264 * clkout = clkdco / m2
266 bool dss_pll_calc_b(const struct dss_pll
*pll
, unsigned long clkin
,
267 unsigned long target_clkout
, struct dss_pll_clock_info
*cinfo
)
269 unsigned long fint
, clkdco
, clkout
;
270 unsigned long target_clkdco
;
271 unsigned long min_dco
;
272 unsigned int n
, m
, mf
, m2
, sd
;
273 const struct dss_pll_hw
*hw
= pll
->hw
;
275 DSSDBG("clkin %lu, target clkout %lu\n", clkin
, target_clkout
);
278 n
= DIV_ROUND_UP(clkin
, hw
->fint_max
);
281 /* adjust m2 so that the clkdco will be high enough */
282 min_dco
= roundup(hw
->clkdco_min
, fint
);
283 m2
= DIV_ROUND_UP(min_dco
, target_clkout
);
287 target_clkdco
= target_clkout
* m2
;
288 m
= target_clkdco
/ fint
;
292 /* adjust clkdco with fractional mf */
293 if (WARN_ON(target_clkdco
- clkdco
> fint
))
296 mf
= (u32
)div_u64(262144ull * (target_clkdco
- clkdco
), fint
);
299 clkdco
+= (u32
)div_u64((u64
)mf
* fint
, 262144);
301 clkout
= clkdco
/ m2
;
304 sd
= DIV_ROUND_UP(fint
* m
, 250000000);
306 DSSDBG("N = %u, M = %u, M.f = %u, M2 = %u, SD = %u\n",
308 DSSDBG("Fint %lu, clkdco %lu, clkout %lu\n", fint
, clkdco
, clkout
);
317 cinfo
->clkdco
= clkdco
;
318 cinfo
->clkout
[0] = clkout
;
323 static int wait_for_bit_change(void __iomem
*reg
, int bitnum
, int value
)
325 unsigned long timeout
;
329 /* first busyloop to see if the bit changes right away */
332 if (FLD_GET(readl_relaxed(reg
), bitnum
, bitnum
) == value
)
336 /* then loop for 500ms, sleeping for 1ms in between */
337 timeout
= jiffies
+ msecs_to_jiffies(500);
338 while (time_before(jiffies
, timeout
)) {
339 if (FLD_GET(readl_relaxed(reg
), bitnum
, bitnum
) == value
)
342 wait
= ns_to_ktime(1000 * 1000);
343 set_current_state(TASK_UNINTERRUPTIBLE
);
344 schedule_hrtimeout(&wait
, HRTIMER_MODE_REL
);
350 int dss_pll_wait_reset_done(struct dss_pll
*pll
)
352 void __iomem
*base
= pll
->base
;
354 if (wait_for_bit_change(base
+ PLL_STATUS
, 0, 1) != 1)
360 static int dss_wait_hsdiv_ack(struct dss_pll
*pll
, u32 hsdiv_ack_mask
)
365 u32 v
= readl_relaxed(pll
->base
+ PLL_STATUS
);
367 if (v
== hsdiv_ack_mask
)
374 static bool pll_is_locked(u32 stat
)
377 * Required value for each bitfield listed below
379 * PLL_STATUS[6] = 0 PLL_BYPASS
380 * PLL_STATUS[5] = 0 PLL_HIGHJITTER
382 * PLL_STATUS[3] = 0 PLL_LOSSREF
383 * PLL_STATUS[2] = 0 PLL_RECAL
384 * PLL_STATUS[1] = 1 PLL_LOCK
385 * PLL_STATUS[0] = 1 PLL_CTRL_RESET_DONE
387 return ((stat
& 0x6f) == 0x3);
390 int dss_pll_write_config_type_a(struct dss_pll
*pll
,
391 const struct dss_pll_clock_info
*cinfo
)
393 const struct dss_pll_hw
*hw
= pll
->hw
;
394 void __iomem
*base
= pll
->base
;
399 if (hw
->has_stopmode
)
400 l
= FLD_MOD(l
, 1, 0, 0); /* PLL_STOPMODE */
401 l
= FLD_MOD(l
, cinfo
->n
- 1, hw
->n_msb
, hw
->n_lsb
); /* PLL_REGN */
402 l
= FLD_MOD(l
, cinfo
->m
, hw
->m_msb
, hw
->m_lsb
); /* PLL_REGM */
404 l
= FLD_MOD(l
, cinfo
->mX
[0] ? cinfo
->mX
[0] - 1 : 0,
405 hw
->mX_msb
[0], hw
->mX_lsb
[0]);
407 l
= FLD_MOD(l
, cinfo
->mX
[1] ? cinfo
->mX
[1] - 1 : 0,
408 hw
->mX_msb
[1], hw
->mX_lsb
[1]);
409 writel_relaxed(l
, base
+ PLL_CONFIGURATION1
);
413 l
= FLD_MOD(l
, cinfo
->mX
[2] ? cinfo
->mX
[2] - 1 : 0,
414 hw
->mX_msb
[2], hw
->mX_lsb
[2]);
416 l
= FLD_MOD(l
, cinfo
->mX
[3] ? cinfo
->mX
[3] - 1 : 0,
417 hw
->mX_msb
[3], hw
->mX_lsb
[3]);
418 writel_relaxed(l
, base
+ PLL_CONFIGURATION3
);
420 l
= readl_relaxed(base
+ PLL_CONFIGURATION2
);
421 if (hw
->has_freqsel
) {
422 u32 f
= cinfo
->fint
< 1000000 ? 0x3 :
423 cinfo
->fint
< 1250000 ? 0x4 :
424 cinfo
->fint
< 1500000 ? 0x5 :
425 cinfo
->fint
< 1750000 ? 0x6 :
428 l
= FLD_MOD(l
, f
, 4, 1); /* PLL_FREQSEL */
429 } else if (hw
->has_selfreqdco
) {
430 u32 f
= cinfo
->clkdco
< hw
->clkdco_low
? 0x2 : 0x4;
432 l
= FLD_MOD(l
, f
, 3, 1); /* PLL_SELFREQDCO */
434 l
= FLD_MOD(l
, 1, 13, 13); /* PLL_REFEN */
435 l
= FLD_MOD(l
, 0, 14, 14); /* PHY_CLKINEN */
436 l
= FLD_MOD(l
, 0, 16, 16); /* M4_CLOCK_EN */
437 l
= FLD_MOD(l
, 0, 18, 18); /* M5_CLOCK_EN */
438 l
= FLD_MOD(l
, 1, 20, 20); /* HSDIVBYPASS */
440 l
= FLD_MOD(l
, 3, 22, 21); /* REFSEL = sysclk */
441 l
= FLD_MOD(l
, 0, 23, 23); /* M6_CLOCK_EN */
442 l
= FLD_MOD(l
, 0, 25, 25); /* M7_CLOCK_EN */
443 writel_relaxed(l
, base
+ PLL_CONFIGURATION2
);
445 if (hw
->errata_i932
) {
448 const u32 max_lock_retries
= 20;
451 * Calculate wait time for PLL LOCK
452 * 1000 REFCLK cycles in us.
454 sleep_time
= DIV_ROUND_UP(1000*1000*1000, cinfo
->fint
);
456 for (cnt
= 0; cnt
< max_lock_retries
; cnt
++) {
457 writel_relaxed(1, base
+ PLL_GO
); /* PLL_GO */
460 * read the register back to ensure the write is
463 readl_relaxed(base
+ PLL_GO
);
465 usleep_range(sleep_time
, sleep_time
+ 5);
466 l
= readl_relaxed(base
+ PLL_STATUS
);
468 if (pll_is_locked(l
) &&
469 !(readl_relaxed(base
+ PLL_GO
) & 0x1))
474 if (cnt
== max_lock_retries
) {
475 DSSERR("cannot lock PLL\n");
480 writel_relaxed(1, base
+ PLL_GO
); /* PLL_GO */
482 if (wait_for_bit_change(base
+ PLL_GO
, 0, 0) != 0) {
483 DSSERR("DSS DPLL GO bit not going down.\n");
488 if (wait_for_bit_change(base
+ PLL_STATUS
, 1, 1) != 1) {
489 DSSERR("cannot lock DSS DPLL\n");
495 l
= readl_relaxed(base
+ PLL_CONFIGURATION2
);
496 l
= FLD_MOD(l
, 1, 14, 14); /* PHY_CLKINEN */
497 l
= FLD_MOD(l
, cinfo
->mX
[0] ? 1 : 0, 16, 16); /* M4_CLOCK_EN */
498 l
= FLD_MOD(l
, cinfo
->mX
[1] ? 1 : 0, 18, 18); /* M5_CLOCK_EN */
499 l
= FLD_MOD(l
, 0, 20, 20); /* HSDIVBYPASS */
500 l
= FLD_MOD(l
, cinfo
->mX
[2] ? 1 : 0, 23, 23); /* M6_CLOCK_EN */
501 l
= FLD_MOD(l
, cinfo
->mX
[3] ? 1 : 0, 25, 25); /* M7_CLOCK_EN */
502 writel_relaxed(l
, base
+ PLL_CONFIGURATION2
);
504 r
= dss_wait_hsdiv_ack(pll
,
505 (cinfo
->mX
[0] ? BIT(7) : 0) |
506 (cinfo
->mX
[1] ? BIT(8) : 0) |
507 (cinfo
->mX
[2] ? BIT(10) : 0) |
508 (cinfo
->mX
[3] ? BIT(11) : 0));
510 DSSERR("failed to enable HSDIV clocks\n");
518 int dss_pll_write_config_type_b(struct dss_pll
*pll
,
519 const struct dss_pll_clock_info
*cinfo
)
521 const struct dss_pll_hw
*hw
= pll
->hw
;
522 void __iomem
*base
= pll
->base
;
526 l
= FLD_MOD(l
, cinfo
->m
, 20, 9); /* PLL_REGM */
527 l
= FLD_MOD(l
, cinfo
->n
- 1, 8, 1); /* PLL_REGN */
528 writel_relaxed(l
, base
+ PLL_CONFIGURATION1
);
530 l
= readl_relaxed(base
+ PLL_CONFIGURATION2
);
531 l
= FLD_MOD(l
, 0x0, 12, 12); /* PLL_HIGHFREQ divide by 2 */
532 l
= FLD_MOD(l
, 0x1, 13, 13); /* PLL_REFEN */
533 l
= FLD_MOD(l
, 0x0, 14, 14); /* PHY_CLKINEN */
535 l
= FLD_MOD(l
, 0x3, 22, 21); /* REFSEL = SYSCLK */
538 if (cinfo
->clkdco
> hw
->clkdco_low
)
539 l
= FLD_MOD(l
, 0x4, 3, 1);
541 l
= FLD_MOD(l
, 0x2, 3, 1);
542 writel_relaxed(l
, base
+ PLL_CONFIGURATION2
);
544 l
= readl_relaxed(base
+ PLL_CONFIGURATION3
);
545 l
= FLD_MOD(l
, cinfo
->sd
, 17, 10); /* PLL_REGSD */
546 writel_relaxed(l
, base
+ PLL_CONFIGURATION3
);
548 l
= readl_relaxed(base
+ PLL_CONFIGURATION4
);
549 l
= FLD_MOD(l
, cinfo
->mX
[0], 24, 18); /* PLL_REGM2 */
550 l
= FLD_MOD(l
, cinfo
->mf
, 17, 0); /* PLL_REGM_F */
551 writel_relaxed(l
, base
+ PLL_CONFIGURATION4
);
553 writel_relaxed(1, base
+ PLL_GO
); /* PLL_GO */
555 if (wait_for_bit_change(base
+ PLL_GO
, 0, 0) != 0) {
556 DSSERR("DSS DPLL GO bit not going down.\n");
560 if (wait_for_bit_change(base
+ PLL_STATUS
, 1, 1) != 1) {
561 DSSERR("cannot lock DSS DPLL\n");