1 // SPDX-License-Identifier: GPL-2.0
3 * Amlogic Meson Video Processing Unit driver
5 * Copyright (c) 2018 BayLibre, SAS.
12 #include "meson_vpu.h"
13 #include <linux/iopoll.h>
14 #include <linux/math64.h>
16 #define writel_bits(mask, val, addr) \
17 writel((readl(addr) & ~(mask)) | (val), addr)
20 MESON_VCLK_TARGET_CVBS = 0,
21 MESON_VCLK_TARGET_HDMI = 1,
22 MESON_VCLK_TARGET_DMT = 2,
26 #define HHI_VID_PLL_CLK_DIV 0x1a0 /* 0x68 offset in data sheet */
27 #define VID_PLL_EN BIT(19)
28 #define VID_PLL_BYPASS BIT(18)
29 #define VID_PLL_PRESET BIT(15)
30 #define HHI_VIID_CLK_DIV 0x128 /* 0x4a offset in data sheet */
31 #define VCLK2_DIV_MASK 0xff
32 #define VCLK2_DIV_EN BIT(16)
33 #define VCLK2_DIV_RESET BIT(17)
34 #define CTS_VDAC_SEL_MASK (0xf << 28)
35 #define CTS_VDAC_SEL_SHIFT 28
36 #define HHI_VIID_CLK_CNTL 0x12c /* 0x4b offset in data sheet */
37 #define VCLK2_EN BIT(19)
38 #define VCLK2_SEL_MASK (0x7 << 16)
39 #define VCLK2_SEL_SHIFT 16
40 #define VCLK2_SOFT_RESET BIT(15)
41 #define VCLK2_DIV1_EN BIT(0)
42 #define HHI_VID_CLK_DIV 0x164 /* 0x59 offset in data sheet */
43 #define VCLK_DIV_MASK 0xff
44 #define VCLK_DIV_EN BIT(16)
45 #define VCLK_DIV_RESET BIT(17)
46 #define CTS_ENCP_SEL_MASK (0xf << 24)
47 #define CTS_ENCP_SEL_SHIFT 24
48 #define CTS_ENCI_SEL_MASK (0xf << 28)
49 #define CTS_ENCI_SEL_SHIFT 28
50 #define HHI_VID_CLK_CNTL 0x17c /* 0x5f offset in data sheet */
51 #define VCLK_EN BIT(19)
52 #define VCLK_SEL_MASK (0x7 << 16)
53 #define VCLK_SEL_SHIFT 16
54 #define VCLK_SOFT_RESET BIT(15)
55 #define VCLK_DIV1_EN BIT(0)
56 #define VCLK_DIV2_EN BIT(1)
57 #define VCLK_DIV4_EN BIT(2)
58 #define VCLK_DIV6_EN BIT(3)
59 #define VCLK_DIV12_EN BIT(4)
60 #define HHI_VID_CLK_CNTL2 0x194 /* 0x65 offset in data sheet */
61 #define CTS_ENCI_EN BIT(0)
62 #define CTS_ENCP_EN BIT(2)
63 #define CTS_VDAC_EN BIT(4)
64 #define HDMI_TX_PIXEL_EN BIT(5)
65 #define HHI_HDMI_CLK_CNTL 0x1cc /* 0x73 offset in data sheet */
66 #define HDMI_TX_PIXEL_SEL_MASK (0xf << 16)
67 #define HDMI_TX_PIXEL_SEL_SHIFT 16
68 #define CTS_HDMI_SYS_SEL_MASK (0x7 << 9)
69 #define CTS_HDMI_SYS_DIV_MASK (0x7f)
70 #define CTS_HDMI_SYS_EN BIT(8)
72 #define HHI_HDMI_PLL_CNTL 0x320 /* 0xc8 offset in data sheet */
73 #define HHI_HDMI_PLL_CNTL_EN BIT(30)
74 #define HHI_HDMI_PLL_CNTL2 0x324 /* 0xc9 offset in data sheet */
75 #define HHI_HDMI_PLL_CNTL3 0x328 /* 0xca offset in data sheet */
76 #define HHI_HDMI_PLL_CNTL4 0x32C /* 0xcb offset in data sheet */
77 #define HHI_HDMI_PLL_CNTL5 0x330 /* 0xcc offset in data sheet */
78 #define HHI_HDMI_PLL_CNTL6 0x334 /* 0xcd offset in data sheet */
79 #define HHI_HDMI_PLL_CNTL7 0x338 /* 0xce offset in data sheet */
81 #define HDMI_PLL_RESET BIT(28)
82 #define HDMI_PLL_RESET_G12A BIT(29)
83 #define HDMI_PLL_LOCK BIT(31)
84 #define HDMI_PLL_LOCK_G12A (3 << 30)
86 #define FREQ_1000_1001(_freq) DIV_ROUND_CLOSEST(_freq * 1000, 1001)
88 /* VID PLL Dividers */
107 void meson_vid_pll_set(struct meson_vpu_priv *priv, unsigned int div)
109 unsigned int shift_val = 0;
110 unsigned int shift_sel = 0;
112 /* Disable vid_pll output clock */
113 hhi_update_bits(HHI_VID_PLL_CLK_DIV, VID_PLL_EN, 0);
114 hhi_update_bits(HHI_VID_PLL_CLK_DIV, VID_PLL_PRESET, 0);
121 case VID_PLL_DIV_2p5:
129 case VID_PLL_DIV_3p5:
133 case VID_PLL_DIV_3p75:
149 case VID_PLL_DIV_6p25:
157 case VID_PLL_DIV_7p5:
175 if (div == VID_PLL_DIV_1) {
176 /* Enable vid_pll bypass to HDMI pll */
177 hhi_update_bits(HHI_VID_PLL_CLK_DIV,
178 VID_PLL_BYPASS, VID_PLL_BYPASS);
181 hhi_update_bits(HHI_VID_PLL_CLK_DIV,
184 hhi_update_bits(HHI_VID_PLL_CLK_DIV,
186 hhi_update_bits(HHI_VID_PLL_CLK_DIV,
188 hhi_update_bits(HHI_VID_PLL_CLK_DIV,
191 /* Setup sel and val */
192 hhi_update_bits(HHI_VID_PLL_CLK_DIV,
193 3 << 16, shift_sel << 16);
194 hhi_update_bits(HHI_VID_PLL_CLK_DIV,
195 VID_PLL_PRESET, VID_PLL_PRESET);
196 hhi_update_bits(HHI_VID_PLL_CLK_DIV,
199 hhi_update_bits(HHI_VID_PLL_CLK_DIV,
203 /* Enable the vid_pll output clock */
204 hhi_update_bits(HHI_VID_PLL_CLK_DIV,
205 VID_PLL_EN, VID_PLL_EN);
209 * Setup VCLK2 for 27MHz, and enable clocks for ENCI and VDAC
211 * TOFIX: Refactor into table to also handle HDMI frequency and paths
213 static void meson_venci_cvbs_clock_config(struct meson_vpu_priv *priv)
217 /* Setup PLL to output 1.485GHz */
218 if (meson_vpu_is_compatible(priv, VPU_COMPATIBLE_GXBB)) {
219 hhi_write(HHI_HDMI_PLL_CNTL, 0x5800023d);
220 hhi_write(HHI_HDMI_PLL_CNTL2, 0x00404e00);
221 hhi_write(HHI_HDMI_PLL_CNTL3, 0x0d5c5091);
222 hhi_write(HHI_HDMI_PLL_CNTL4, 0x801da72c);
223 hhi_write(HHI_HDMI_PLL_CNTL5, 0x71486980);
224 hhi_write(HHI_HDMI_PLL_CNTL6, 0x00000e55);
225 hhi_write(HHI_HDMI_PLL_CNTL, 0x4800023d);
227 /* Poll for lock bit */
228 readl_poll_timeout(priv->hhi_base + HHI_HDMI_PLL_CNTL, val,
229 (val & HDMI_PLL_LOCK), 10);
230 } else if (meson_vpu_is_compatible(priv, VPU_COMPATIBLE_GXM) ||
231 meson_vpu_is_compatible(priv, VPU_COMPATIBLE_GXL)) {
232 hhi_write(HHI_HDMI_PLL_CNTL, 0x4000027b);
233 hhi_write(HHI_HDMI_PLL_CNTL2, 0x800cb300);
234 hhi_write(HHI_HDMI_PLL_CNTL3, 0xa6212844);
235 hhi_write(HHI_HDMI_PLL_CNTL4, 0x0c4d000c);
236 hhi_write(HHI_HDMI_PLL_CNTL5, 0x001fa729);
237 hhi_write(HHI_HDMI_PLL_CNTL6, 0x01a31500);
240 hhi_update_bits(HHI_HDMI_PLL_CNTL,
241 HDMI_PLL_RESET, HDMI_PLL_RESET);
242 hhi_update_bits(HHI_HDMI_PLL_CNTL,
245 /* Poll for lock bit */
246 readl_poll_timeout(priv->hhi_base + HHI_HDMI_PLL_CNTL, val,
247 (val & HDMI_PLL_LOCK), 10);
248 } else if (meson_vpu_is_compatible(priv, VPU_COMPATIBLE_G12A)) {
249 hhi_write(HHI_HDMI_PLL_CNTL, 0x1a0504f7);
250 hhi_write(HHI_HDMI_PLL_CNTL2, 0x00010000);
251 hhi_write(HHI_HDMI_PLL_CNTL3, 0x00000000);
252 hhi_write(HHI_HDMI_PLL_CNTL4, 0x6a28dc00);
253 hhi_write(HHI_HDMI_PLL_CNTL5, 0x65771290);
254 hhi_write(HHI_HDMI_PLL_CNTL6, 0x39272000);
255 hhi_write(HHI_HDMI_PLL_CNTL7, 0x56540000);
256 hhi_write(HHI_HDMI_PLL_CNTL, 0x3a0504f7);
257 hhi_write(HHI_HDMI_PLL_CNTL, 0x1a0504f7);
259 /* Poll for lock bit */
260 readl_poll_timeout(priv->hhi_base + HHI_HDMI_PLL_CNTL, val,
261 ((val & HDMI_PLL_LOCK_G12A) == HDMI_PLL_LOCK_G12A),
266 hhi_update_bits(HHI_VIID_CLK_CNTL, VCLK2_EN, 0);
268 /* Setup vid_pll to /1 */
269 meson_vid_pll_set(priv, VID_PLL_DIV_1);
271 /* Setup the VCLK2 divider value to achieve 27MHz */
272 hhi_update_bits(HHI_VIID_CLK_DIV,
273 VCLK2_DIV_MASK, (55 - 1));
275 /* select vid_pll for vclk2 */
276 if (meson_vpu_is_compatible(priv, VPU_COMPATIBLE_G12A))
277 hhi_update_bits(HHI_VIID_CLK_CNTL,
278 VCLK2_SEL_MASK, (0 << VCLK2_SEL_SHIFT));
280 hhi_update_bits(HHI_VIID_CLK_CNTL,
281 VCLK2_SEL_MASK, (4 << VCLK2_SEL_SHIFT));
283 /* enable vclk2 gate */
284 hhi_update_bits(HHI_VIID_CLK_CNTL, VCLK2_EN, VCLK2_EN);
286 /* select vclk_div1 for enci */
287 hhi_update_bits(HHI_VID_CLK_DIV,
288 CTS_ENCI_SEL_MASK, (8 << CTS_ENCI_SEL_SHIFT));
289 /* select vclk_div1 for vdac */
290 hhi_update_bits(HHI_VIID_CLK_DIV,
291 CTS_VDAC_SEL_MASK, (8 << CTS_VDAC_SEL_SHIFT));
293 /* release vclk2_div_reset and enable vclk2_div */
294 hhi_update_bits(HHI_VIID_CLK_DIV,
295 VCLK2_DIV_EN | VCLK2_DIV_RESET, VCLK2_DIV_EN);
297 /* enable vclk2_div1 gate */
298 hhi_update_bits(HHI_VIID_CLK_CNTL,
299 VCLK2_DIV1_EN, VCLK2_DIV1_EN);
302 hhi_update_bits(HHI_VIID_CLK_CNTL,
303 VCLK2_SOFT_RESET, VCLK2_SOFT_RESET);
304 hhi_update_bits(HHI_VIID_CLK_CNTL,
305 VCLK2_SOFT_RESET, 0);
307 /* enable enci_clk */
308 hhi_update_bits(HHI_VID_CLK_CNTL2,
309 CTS_ENCI_EN, CTS_ENCI_EN);
310 /* enable vdac_clk */
311 hhi_update_bits(HHI_VID_CLK_CNTL2,
312 CTS_VDAC_EN, CTS_VDAC_EN);
316 /* PLL O1 O2 O3 VP DV EN TX */
317 /* 4320 /4 /4 /1 /5 /1 => /2 /2 */
318 MESON_VCLK_HDMI_ENCI_54000 = 0,
319 /* 4320 /4 /4 /1 /5 /1 => /1 /2 */
320 MESON_VCLK_HDMI_DDR_54000,
321 /* 2970 /4 /1 /1 /5 /1 => /1 /2 */
322 MESON_VCLK_HDMI_DDR_148500,
323 /* 2970 /2 /2 /2 /5 /1 => /1 /1 */
324 MESON_VCLK_HDMI_74250,
325 /* 2970 /1 /2 /2 /5 /1 => /1 /1 */
326 MESON_VCLK_HDMI_148500,
327 /* 2970 /1 /1 /1 /5 /2 => /1 /1 */
328 MESON_VCLK_HDMI_297000,
329 /* 5940 /1 /1 /2 /5 /1 => /1 /1 */
330 MESON_VCLK_HDMI_594000
333 struct meson_vclk_params {
334 unsigned int pixel_freq;
335 unsigned int pll_base_freq;
336 unsigned int pll_od1;
337 unsigned int pll_od2;
338 unsigned int pll_od3;
339 unsigned int vid_pll_div;
340 unsigned int vclk_div;
342 [MESON_VCLK_HDMI_ENCI_54000] = {
344 .pll_base_freq = 4320000,
348 .vid_pll_div = VID_PLL_DIV_5,
351 [MESON_VCLK_HDMI_DDR_54000] = {
353 .pll_base_freq = 4320000,
357 .vid_pll_div = VID_PLL_DIV_5,
360 [MESON_VCLK_HDMI_DDR_148500] = {
361 .pixel_freq = 148500,
362 .pll_base_freq = 2970000,
366 .vid_pll_div = VID_PLL_DIV_5,
369 [MESON_VCLK_HDMI_74250] = {
371 .pll_base_freq = 2970000,
375 .vid_pll_div = VID_PLL_DIV_5,
378 [MESON_VCLK_HDMI_148500] = {
379 .pixel_freq = 148500,
380 .pll_base_freq = 2970000,
384 .vid_pll_div = VID_PLL_DIV_5,
387 [MESON_VCLK_HDMI_297000] = {
388 .pixel_freq = 297000,
389 .pll_base_freq = 5940000,
393 .vid_pll_div = VID_PLL_DIV_5,
396 [MESON_VCLK_HDMI_594000] = {
397 .pixel_freq = 594000,
398 .pll_base_freq = 5940000,
402 .vid_pll_div = VID_PLL_DIV_5,
408 static inline unsigned int pll_od_to_reg(unsigned int od)
425 void meson_hdmi_pll_set_params(struct meson_vpu_priv *priv, unsigned int m,
426 unsigned int frac, unsigned int od1,
427 unsigned int od2, unsigned int od3)
431 if (meson_vpu_is_compatible(priv, VPU_COMPATIBLE_GXBB)) {
432 hhi_write(HHI_HDMI_PLL_CNTL, 0x58000200 | m);
434 hhi_write(HHI_HDMI_PLL_CNTL2,
437 hhi_write(HHI_HDMI_PLL_CNTL2,
439 hhi_write(HHI_HDMI_PLL_CNTL3, 0x0d5c5091);
440 hhi_write(HHI_HDMI_PLL_CNTL4, 0x801da72c);
441 hhi_write(HHI_HDMI_PLL_CNTL5, 0x71486980);
442 hhi_write(HHI_HDMI_PLL_CNTL6, 0x00000e55);
444 /* Enable and unreset */
445 hhi_update_bits(HHI_HDMI_PLL_CNTL,
446 0x7 << 28, 0x4 << 28);
448 /* Poll for lock bit */
449 readl_poll_timeout(priv->hhi_base + HHI_HDMI_PLL_CNTL, val,
450 (val & HDMI_PLL_LOCK), 10);
451 } else if (meson_vpu_is_compatible(priv, VPU_COMPATIBLE_GXM) ||
452 meson_vpu_is_compatible(priv, VPU_COMPATIBLE_GXL)) {
453 hhi_write(HHI_HDMI_PLL_CNTL, 0x40000200 | m);
454 hhi_write(HHI_HDMI_PLL_CNTL2, 0x800cb000 | frac);
455 hhi_write(HHI_HDMI_PLL_CNTL3, 0x860f30c4);
456 hhi_write(HHI_HDMI_PLL_CNTL4, 0x0c8e0000);
457 hhi_write(HHI_HDMI_PLL_CNTL5, 0x001fa729);
458 hhi_write(HHI_HDMI_PLL_CNTL6, 0x01a31500);
461 hhi_update_bits(HHI_HDMI_PLL_CNTL,
462 HDMI_PLL_RESET, HDMI_PLL_RESET);
463 hhi_update_bits(HHI_HDMI_PLL_CNTL,
466 /* Poll for lock bit */
467 readl_poll_timeout(priv->hhi_base + HHI_HDMI_PLL_CNTL, val,
468 (val & HDMI_PLL_LOCK), 10);
469 } else if (meson_vpu_is_compatible(priv, VPU_COMPATIBLE_G12A)) {
470 hhi_write(HHI_HDMI_PLL_CNTL, 0x0b3a0400 | m);
472 /* Enable and reset */
473 hhi_update_bits(HHI_HDMI_PLL_CNTL, 0x3 << 28, 0x3 << 28);
475 hhi_write(HHI_HDMI_PLL_CNTL2, frac);
476 hhi_write(HHI_HDMI_PLL_CNTL3, 0x00000000);
478 /* G12A HDMI PLL Needs specific parameters for 5.4GHz */
480 if (frac < 0x10000) {
481 hhi_write(HHI_HDMI_PLL_CNTL4, 0x6a685c00);
482 hhi_write(HHI_HDMI_PLL_CNTL5, 0x11551293);
484 hhi_write(HHI_HDMI_PLL_CNTL4, 0xea68dc00);
485 hhi_write(HHI_HDMI_PLL_CNTL5, 0x65771290);
487 hhi_write(HHI_HDMI_PLL_CNTL6, 0x39272000);
488 hhi_write(HHI_HDMI_PLL_CNTL7, 0x55540000);
490 hhi_write(HHI_HDMI_PLL_CNTL4, 0x0a691c00);
491 hhi_write(HHI_HDMI_PLL_CNTL5, 0x33771290);
492 hhi_write(HHI_HDMI_PLL_CNTL6, 0x39270000);
493 hhi_write(HHI_HDMI_PLL_CNTL7, 0x50540000);
498 hhi_update_bits(HHI_HDMI_PLL_CNTL,
500 HDMI_PLL_RESET_G12A);
503 hhi_update_bits(HHI_HDMI_PLL_CNTL,
504 HDMI_PLL_RESET_G12A, 0);
506 /* Poll for lock bits */
507 if (!readl_poll_timeout(
508 priv->hhi_base + HHI_HDMI_PLL_CNTL, val,
509 ((val & HDMI_PLL_LOCK_G12A)
510 == HDMI_PLL_LOCK_G12A), 100))
515 if (meson_vpu_is_compatible(priv, VPU_COMPATIBLE_GXBB))
516 hhi_update_bits(HHI_HDMI_PLL_CNTL2,
517 3 << 16, pll_od_to_reg(od1) << 16);
518 else if (meson_vpu_is_compatible(priv, VPU_COMPATIBLE_GXM) ||
519 meson_vpu_is_compatible(priv, VPU_COMPATIBLE_GXL))
520 hhi_update_bits(HHI_HDMI_PLL_CNTL3,
521 3 << 21, pll_od_to_reg(od1) << 21);
522 else if (meson_vpu_is_compatible(priv, VPU_COMPATIBLE_G12A))
523 hhi_update_bits(HHI_HDMI_PLL_CNTL,
524 3 << 16, pll_od_to_reg(od1) << 16);
526 if (meson_vpu_is_compatible(priv, VPU_COMPATIBLE_GXBB))
527 hhi_update_bits(HHI_HDMI_PLL_CNTL2,
528 3 << 22, pll_od_to_reg(od2) << 22);
529 else if (meson_vpu_is_compatible(priv, VPU_COMPATIBLE_GXM) ||
530 meson_vpu_is_compatible(priv, VPU_COMPATIBLE_GXL))
531 hhi_update_bits(HHI_HDMI_PLL_CNTL3,
532 3 << 23, pll_od_to_reg(od2) << 23);
533 else if (meson_vpu_is_compatible(priv, VPU_COMPATIBLE_G12A))
534 hhi_update_bits(HHI_HDMI_PLL_CNTL,
535 3 << 18, pll_od_to_reg(od2) << 18);
537 if (meson_vpu_is_compatible(priv, VPU_COMPATIBLE_GXBB))
538 hhi_update_bits(HHI_HDMI_PLL_CNTL2,
539 3 << 18, pll_od_to_reg(od3) << 18);
540 else if (meson_vpu_is_compatible(priv, VPU_COMPATIBLE_GXM) ||
541 meson_vpu_is_compatible(priv, VPU_COMPATIBLE_GXL))
542 hhi_update_bits(HHI_HDMI_PLL_CNTL3,
543 3 << 19, pll_od_to_reg(od3) << 19);
544 else if (meson_vpu_is_compatible(priv, VPU_COMPATIBLE_G12A))
545 hhi_update_bits(HHI_HDMI_PLL_CNTL,
546 3 << 20, pll_od_to_reg(od3) << 20);
549 #define XTAL_FREQ 24000
551 static unsigned int meson_hdmi_pll_get_m(struct meson_vpu_priv *priv,
552 unsigned int pll_freq)
554 /* The GXBB PLL has a /2 pre-multiplier */
555 if (meson_vpu_is_compatible(priv, VPU_COMPATIBLE_GXBB))
558 return pll_freq / XTAL_FREQ;
561 #define HDMI_FRAC_MAX_GXBB 4096
562 #define HDMI_FRAC_MAX_GXL 1024
563 #define HDMI_FRAC_MAX_G12A 131072
565 static unsigned int meson_hdmi_pll_get_frac(struct meson_vpu_priv *priv,
567 unsigned int pll_freq)
569 unsigned int parent_freq = XTAL_FREQ;
570 unsigned int frac_max = HDMI_FRAC_MAX_GXL;
574 /* The GXBB PLL has a /2 pre-multiplier and a larger FRAC width */
575 if (meson_vpu_is_compatible(priv, VPU_COMPATIBLE_GXBB)) {
576 frac_max = HDMI_FRAC_MAX_GXBB;
580 if (meson_vpu_is_compatible(priv, VPU_COMPATIBLE_G12A))
581 frac_max = HDMI_FRAC_MAX_G12A;
583 /* We can have a perfect match !*/
584 if (pll_freq / m == parent_freq &&
588 frac = div_u64((u64)pll_freq * (u64)frac_max, parent_freq);
589 frac_m = m * frac_max;
594 return min((u16)frac, (u16)(frac_max - 1));
597 static bool meson_hdmi_pll_validate_params(struct meson_vpu_priv *priv,
601 if (meson_vpu_is_compatible(priv, VPU_COMPATIBLE_GXBB)) {
602 /* Empiric supported min/max dividers */
603 if (m < 53 || m > 123)
605 if (frac >= HDMI_FRAC_MAX_GXBB)
607 } else if (meson_vpu_is_compatible(priv, VPU_COMPATIBLE_GXM) ||
608 meson_vpu_is_compatible(priv, VPU_COMPATIBLE_GXL)) {
609 /* Empiric supported min/max dividers */
610 if (m < 106 || m > 247)
612 if (frac >= HDMI_FRAC_MAX_GXL)
614 } else if (meson_vpu_is_compatible(priv, VPU_COMPATIBLE_G12A)) {
615 /* Empiric supported min/max dividers */
616 if (m < 106 || m > 247)
618 if (frac >= HDMI_FRAC_MAX_G12A)
625 static bool meson_hdmi_pll_find_params(struct meson_vpu_priv *priv,
631 /* Cycle from /16 to /2 */
632 for (*od = 16 ; *od > 1 ; *od >>= 1) {
633 *m = meson_hdmi_pll_get_m(priv, freq * *od);
636 *frac = meson_hdmi_pll_get_frac(priv, *m, freq * *od);
638 debug("PLL params for %dkHz: m=%x frac=%x od=%d\n",
639 freq, *m, *frac, *od);
641 if (meson_hdmi_pll_validate_params(priv, *m, *frac))
648 /* pll_freq is the frequency after the OD dividers */
649 bool meson_vclk_dmt_supported_freq(struct meson_vpu_priv *priv,
652 unsigned int od, m, frac;
654 /* In DMT mode, path after PLL is always /10 */
657 if (meson_hdmi_pll_find_params(priv, freq, &m, &frac, &od))
663 /* pll_freq is the frequency after the OD dividers */
664 static void meson_hdmi_pll_generic_set(struct meson_vpu_priv *priv,
665 unsigned int pll_freq)
667 unsigned int od, m, frac, od1, od2, od3;
669 if (meson_hdmi_pll_find_params(priv, pll_freq, &m, &frac, &od)) {
679 debug("PLL params for %dkHz: m=%x frac=%x od=%d/%d/%d\n",
680 pll_freq, m, frac, od1, od2, od3);
682 meson_hdmi_pll_set_params(priv, m, frac, od1, od2, od3);
687 printf("Fatal, unable to find parameters for PLL freq %d\n",
692 meson_vclk_set(struct meson_vpu_priv *priv, unsigned int pll_base_freq,
693 unsigned int od1, unsigned int od2, unsigned int od3,
694 unsigned int vid_pll_div, unsigned int vclk_div,
695 unsigned int hdmi_tx_div, unsigned int venc_div,
696 bool hdmi_use_enci, bool vic_alternate_clock)
698 unsigned int m = 0, frac = 0;
700 /* Set HDMI-TX sys clock */
701 hhi_update_bits(HHI_HDMI_CLK_CNTL,
702 CTS_HDMI_SYS_SEL_MASK, 0);
703 hhi_update_bits(HHI_HDMI_CLK_CNTL,
704 CTS_HDMI_SYS_DIV_MASK, 0);
705 hhi_update_bits(HHI_HDMI_CLK_CNTL,
706 CTS_HDMI_SYS_EN, CTS_HDMI_SYS_EN);
708 /* Set HDMI PLL rate */
709 if (!od1 && !od2 && !od3) {
710 meson_hdmi_pll_generic_set(priv, pll_base_freq);
711 } else if (meson_vpu_is_compatible(priv, VPU_COMPATIBLE_GXBB)) {
712 switch (pll_base_freq) {
715 frac = vic_alternate_clock ? 0xd02 : 0xe00;
718 m = vic_alternate_clock ? 0x59 : 0x5a;
719 frac = vic_alternate_clock ? 0xe8f : 0;
723 frac = vic_alternate_clock ? 0xa05 : 0xc00;
727 meson_hdmi_pll_set_params(priv, m, frac, od1, od2, od3);
728 } else if (meson_vpu_is_compatible(priv, VPU_COMPATIBLE_GXM) ||
729 meson_vpu_is_compatible(priv, VPU_COMPATIBLE_GXL)) {
730 switch (pll_base_freq) {
733 frac = vic_alternate_clock ? 0x281 : 0x300;
736 m = vic_alternate_clock ? 0xb3 : 0xb4;
737 frac = vic_alternate_clock ? 0x347 : 0;
741 frac = vic_alternate_clock ? 0x102 : 0x200;
745 meson_hdmi_pll_set_params(priv, m, frac, od1, od2, od3);
746 } else if (meson_vpu_is_compatible(priv, VPU_COMPATIBLE_G12A)) {
747 switch (pll_base_freq) {
750 frac = vic_alternate_clock ? 0x140b4 : 0x18000;
753 m = vic_alternate_clock ? 0xb3 : 0xb4;
754 frac = vic_alternate_clock ? 0x1a3ee : 0;
758 frac = vic_alternate_clock ? 0x8148 : 0x10000;
762 meson_hdmi_pll_set_params(priv, m, frac, od1, od2, od3);
765 /* Setup vid_pll divider */
766 meson_vid_pll_set(priv, vid_pll_div);
769 hhi_update_bits(HHI_VID_CLK_CNTL,
771 hhi_update_bits(HHI_VID_CLK_DIV,
772 VCLK_DIV_MASK, vclk_div - 1);
774 /* Set HDMI-TX source */
775 switch (hdmi_tx_div) {
777 /* enable vclk_div1 gate */
778 hhi_update_bits(HHI_VID_CLK_CNTL,
779 VCLK_DIV1_EN, VCLK_DIV1_EN);
781 /* select vclk_div1 for HDMI-TX */
782 hhi_update_bits(HHI_HDMI_CLK_CNTL,
783 HDMI_TX_PIXEL_SEL_MASK, 0);
786 /* enable vclk_div2 gate */
787 hhi_update_bits(HHI_VID_CLK_CNTL,
788 VCLK_DIV2_EN, VCLK_DIV2_EN);
790 /* select vclk_div2 for HDMI-TX */
791 hhi_update_bits(HHI_HDMI_CLK_CNTL,
792 HDMI_TX_PIXEL_SEL_MASK,
793 1 << HDMI_TX_PIXEL_SEL_SHIFT);
796 /* enable vclk_div4 gate */
797 hhi_update_bits(HHI_VID_CLK_CNTL,
798 VCLK_DIV4_EN, VCLK_DIV4_EN);
800 /* select vclk_div4 for HDMI-TX */
801 hhi_update_bits(HHI_HDMI_CLK_CNTL,
802 HDMI_TX_PIXEL_SEL_MASK,
803 2 << HDMI_TX_PIXEL_SEL_SHIFT);
806 /* enable vclk_div6 gate */
807 hhi_update_bits(HHI_VID_CLK_CNTL,
808 VCLK_DIV6_EN, VCLK_DIV6_EN);
810 /* select vclk_div6 for HDMI-TX */
811 hhi_update_bits(HHI_HDMI_CLK_CNTL,
812 HDMI_TX_PIXEL_SEL_MASK,
813 3 << HDMI_TX_PIXEL_SEL_SHIFT);
816 /* enable vclk_div12 gate */
817 hhi_update_bits(HHI_VID_CLK_CNTL,
818 VCLK_DIV12_EN, VCLK_DIV12_EN);
820 /* select vclk_div12 for HDMI-TX */
821 hhi_update_bits(HHI_HDMI_CLK_CNTL,
822 HDMI_TX_PIXEL_SEL_MASK,
823 4 << HDMI_TX_PIXEL_SEL_SHIFT);
826 hhi_update_bits(HHI_VID_CLK_CNTL2,
827 HDMI_TX_PIXEL_EN, HDMI_TX_PIXEL_EN);
829 /* Set ENCI/ENCP Source */
832 /* enable vclk_div1 gate */
833 hhi_update_bits(HHI_VID_CLK_CNTL,
834 VCLK_DIV1_EN, VCLK_DIV1_EN);
837 /* select vclk_div1 for enci */
838 hhi_update_bits(HHI_VID_CLK_DIV,
839 CTS_ENCI_SEL_MASK, 0);
841 /* select vclk_div1 for encp */
842 hhi_update_bits(HHI_VID_CLK_DIV,
843 CTS_ENCP_SEL_MASK, 0);
846 /* enable vclk_div2 gate */
847 hhi_update_bits(HHI_VID_CLK_CNTL,
848 VCLK_DIV2_EN, VCLK_DIV2_EN);
851 /* select vclk_div2 for enci */
852 hhi_update_bits(HHI_VID_CLK_DIV,
854 1 << CTS_ENCI_SEL_SHIFT);
856 /* select vclk_div2 for encp */
857 hhi_update_bits(HHI_VID_CLK_DIV,
859 1 << CTS_ENCP_SEL_SHIFT);
862 /* enable vclk_div4 gate */
863 hhi_update_bits(HHI_VID_CLK_CNTL,
864 VCLK_DIV4_EN, VCLK_DIV4_EN);
867 /* select vclk_div4 for enci */
868 hhi_update_bits(HHI_VID_CLK_DIV,
870 2 << CTS_ENCI_SEL_SHIFT);
872 /* select vclk_div4 for encp */
873 hhi_update_bits(HHI_VID_CLK_DIV,
875 2 << CTS_ENCP_SEL_SHIFT);
878 /* enable vclk_div6 gate */
879 hhi_update_bits(HHI_VID_CLK_CNTL,
880 VCLK_DIV6_EN, VCLK_DIV6_EN);
883 /* select vclk_div6 for enci */
884 hhi_update_bits(HHI_VID_CLK_DIV,
886 3 << CTS_ENCI_SEL_SHIFT);
888 /* select vclk_div6 for encp */
889 hhi_update_bits(HHI_VID_CLK_DIV,
891 3 << CTS_ENCP_SEL_SHIFT);
894 /* enable vclk_div12 gate */
895 hhi_update_bits(HHI_VID_CLK_CNTL,
896 VCLK_DIV12_EN, VCLK_DIV12_EN);
899 /* select vclk_div12 for enci */
900 hhi_update_bits(HHI_VID_CLK_DIV,
902 4 << CTS_ENCI_SEL_SHIFT);
904 /* select vclk_div12 for encp */
905 hhi_update_bits(HHI_VID_CLK_DIV,
907 4 << CTS_ENCP_SEL_SHIFT);
912 /* Enable ENCI clock gate */
913 hhi_update_bits(HHI_VID_CLK_CNTL2,
914 CTS_ENCI_EN, CTS_ENCI_EN);
916 /* Enable ENCP clock gate */
917 hhi_update_bits(HHI_VID_CLK_CNTL2,
918 CTS_ENCP_EN, CTS_ENCP_EN);
920 hhi_update_bits(HHI_VID_CLK_CNTL, VCLK_EN, VCLK_EN);
923 static void meson_vclk_setup(struct meson_vpu_priv *priv, unsigned int target,
924 unsigned int vclk_freq, unsigned int venc_freq,
925 unsigned int dac_freq, bool hdmi_use_enci)
927 bool vic_alternate_clock = false;
929 unsigned int hdmi_tx_div;
930 unsigned int venc_div;
932 if (target == MESON_VCLK_TARGET_CVBS) {
933 meson_venci_cvbs_clock_config(priv);
935 } else if (target == MESON_VCLK_TARGET_DMT) {
936 /* The DMT clock path is fixed after the PLL:
937 * - automatic PLL freq + OD management
938 * - vid_pll_div = VID_PLL_DIV_5
944 meson_vclk_set(priv, vclk_freq * 10, 0, 0, 0,
945 VID_PLL_DIV_5, 2, 1, 1, false, false);
949 hdmi_tx_div = vclk_freq / dac_freq;
951 if (hdmi_tx_div == 0) {
952 printf("Fatal Error, invalid HDMI-TX freq %d\n",
957 venc_div = vclk_freq / venc_freq;
960 printf("Fatal Error, invalid HDMI venc freq %d\n",
965 for (freq = 0 ; params[freq].pixel_freq ; ++freq) {
966 if (vclk_freq == params[freq].pixel_freq ||
967 vclk_freq == FREQ_1000_1001(params[freq].pixel_freq)) {
968 if (vclk_freq != params[freq].pixel_freq)
969 vic_alternate_clock = true;
971 vic_alternate_clock = false;
973 if (freq == MESON_VCLK_HDMI_ENCI_54000 &&
977 if (freq == MESON_VCLK_HDMI_DDR_54000 &&
981 if (freq == MESON_VCLK_HDMI_DDR_148500 &&
982 dac_freq == vclk_freq)
985 if (freq == MESON_VCLK_HDMI_148500 &&
986 dac_freq != vclk_freq)
992 if (!params[freq].pixel_freq) {
993 pr_err("Fatal Error, invalid HDMI vclk freq %d\n", vclk_freq);
997 meson_vclk_set(priv, params[freq].pll_base_freq,
998 params[freq].pll_od1, params[freq].pll_od2,
999 params[freq].pll_od3, params[freq].vid_pll_div,
1000 params[freq].vclk_div, hdmi_tx_div, venc_div,
1001 hdmi_use_enci, vic_alternate_clock);
1004 void meson_vpu_setup_vclk(struct udevice *dev,
1005 const struct display_timing *mode, bool is_cvbs)
1007 struct meson_vpu_priv *priv = dev_get_priv(dev);
1008 unsigned int vclk_freq;
1011 return meson_vclk_setup(priv, MESON_VCLK_TARGET_CVBS,
1014 vclk_freq = mode->pixelclock.typ / 1000;
1016 return meson_vclk_setup(priv, MESON_VCLK_TARGET_DMT,
1017 vclk_freq, vclk_freq, vclk_freq, false);