2 * Copyright (C) 2016 BayLibre, SAS
3 * Author: Neil Armstrong <narmstrong@baylibre.com>
4 * Copyright (C) 2015 Amlogic, Inc. All rights reserved.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include <linux/kernel.h>
21 #include <linux/module.h>
23 #include "meson_drv.h"
24 #include "meson_vclk.h"
29 * VCLK is the "Pixel Clock" frequency generator from a dedicated PLL.
30 * We handle the following encodings :
32 * - CVBS 27MHz generator via the VCLK2 to the VENCI and VDAC blocks
33 * - HDMI Pixel Clocks generation
37 * - Genenate Pixel clocks for 2K/4K 10bit formats
39 * Clock generator scheme :
43 * __________ _________ _____
45 * | HDMI PLL |-| PLL_DIV |--- VCLK--| |--ENCL
46 * |__________| |_________| \ | MUX |--ENCP
50 * Final clocks can take input for either VCLK or VCLK2, but
51 * VCLK is the preferred path for HDMI clocking and VCLK2 is the
52 * preferred path for CVBS VDAC clocking.
54 * VCLK and VCLK2 have fixed divided clocks paths for /1, /2, /4, /6 or /12.
56 * The PLL_DIV can achieve an additional fractional dividing like
57 * 1.5, 3.5, 3.75... to generate special 2K and 4K 10bit clocks.
61 #define HHI_VID_PLL_CLK_DIV 0x1a0 /* 0x68 offset in data sheet */
62 #define VID_PLL_EN BIT(19)
63 #define VID_PLL_BYPASS BIT(18)
64 #define VID_PLL_PRESET BIT(15)
65 #define HHI_VIID_CLK_DIV 0x128 /* 0x4a offset in data sheet */
66 #define VCLK2_DIV_MASK 0xff
67 #define VCLK2_DIV_EN BIT(16)
68 #define VCLK2_DIV_RESET BIT(17)
69 #define CTS_VDAC_SEL_MASK (0xf << 28)
70 #define CTS_VDAC_SEL_SHIFT 28
71 #define HHI_VIID_CLK_CNTL 0x12c /* 0x4b offset in data sheet */
72 #define VCLK2_EN BIT(19)
73 #define VCLK2_SEL_MASK (0x7 << 16)
74 #define VCLK2_SEL_SHIFT 16
75 #define VCLK2_SOFT_RESET BIT(15)
76 #define VCLK2_DIV1_EN BIT(0)
77 #define HHI_VID_CLK_DIV 0x164 /* 0x59 offset in data sheet */
78 #define VCLK_DIV_MASK 0xff
79 #define VCLK_DIV_EN BIT(16)
80 #define VCLK_DIV_RESET BIT(17)
81 #define CTS_ENCP_SEL_MASK (0xf << 24)
82 #define CTS_ENCP_SEL_SHIFT 24
83 #define CTS_ENCI_SEL_MASK (0xf << 28)
84 #define CTS_ENCI_SEL_SHIFT 28
85 #define HHI_VID_CLK_CNTL 0x17c /* 0x5f offset in data sheet */
86 #define VCLK_EN BIT(19)
87 #define VCLK_SEL_MASK (0x7 << 16)
88 #define VCLK_SEL_SHIFT 16
89 #define VCLK_SOFT_RESET BIT(15)
90 #define VCLK_DIV1_EN BIT(0)
91 #define VCLK_DIV2_EN BIT(1)
92 #define VCLK_DIV4_EN BIT(2)
93 #define VCLK_DIV6_EN BIT(3)
94 #define VCLK_DIV12_EN BIT(4)
95 #define HHI_VID_CLK_CNTL2 0x194 /* 0x65 offset in data sheet */
96 #define CTS_ENCI_EN BIT(0)
97 #define CTS_ENCP_EN BIT(2)
98 #define CTS_VDAC_EN BIT(4)
99 #define HDMI_TX_PIXEL_EN BIT(5)
100 #define HHI_HDMI_CLK_CNTL 0x1cc /* 0x73 offset in data sheet */
101 #define HDMI_TX_PIXEL_SEL_MASK (0xf << 16)
102 #define HDMI_TX_PIXEL_SEL_SHIFT 16
103 #define CTS_HDMI_SYS_SEL_MASK (0x7 << 9)
104 #define CTS_HDMI_SYS_DIV_MASK (0x7f)
105 #define CTS_HDMI_SYS_EN BIT(8)
107 #define HHI_VDAC_CNTL0 0x2F4 /* 0xbd offset in data sheet */
108 #define HHI_VDAC_CNTL1 0x2F8 /* 0xbe offset in data sheet */
110 #define HHI_HDMI_PLL_CNTL 0x320 /* 0xc8 offset in data sheet */
111 #define HHI_HDMI_PLL_CNTL2 0x324 /* 0xc9 offset in data sheet */
112 #define HHI_HDMI_PLL_CNTL3 0x328 /* 0xca offset in data sheet */
113 #define HHI_HDMI_PLL_CNTL4 0x32C /* 0xcb offset in data sheet */
114 #define HHI_HDMI_PLL_CNTL5 0x330 /* 0xcc offset in data sheet */
115 #define HHI_HDMI_PLL_CNTL6 0x334 /* 0xcd offset in data sheet */
117 #define HDMI_PLL_RESET BIT(28)
118 #define HDMI_PLL_LOCK BIT(31)
120 /* VID PLL Dividers */
139 void meson_vid_pll_set(struct meson_drm
*priv
, unsigned int div
)
141 unsigned int shift_val
= 0;
142 unsigned int shift_sel
= 0;
144 /* Disable vid_pll output clock */
145 regmap_update_bits(priv
->hhi
, HHI_VID_PLL_CLK_DIV
, VID_PLL_EN
, 0);
146 regmap_update_bits(priv
->hhi
, HHI_VID_PLL_CLK_DIV
, VID_PLL_PRESET
, 0);
153 case VID_PLL_DIV_2p5
:
161 case VID_PLL_DIV_3p5
:
165 case VID_PLL_DIV_3p75
:
181 case VID_PLL_DIV_6p25
:
189 case VID_PLL_DIV_7p5
:
207 if (div
== VID_PLL_DIV_1
)
208 /* Enable vid_pll bypass to HDMI pll */
209 regmap_update_bits(priv
->hhi
, HHI_VID_PLL_CLK_DIV
,
210 VID_PLL_BYPASS
, VID_PLL_BYPASS
);
213 regmap_update_bits(priv
->hhi
, HHI_VID_PLL_CLK_DIV
,
216 regmap_update_bits(priv
->hhi
, HHI_VID_PLL_CLK_DIV
,
218 regmap_update_bits(priv
->hhi
, HHI_VID_PLL_CLK_DIV
,
220 regmap_update_bits(priv
->hhi
, HHI_VID_PLL_CLK_DIV
,
223 /* Setup sel and val */
224 regmap_update_bits(priv
->hhi
, HHI_VID_PLL_CLK_DIV
,
225 3 << 16, shift_sel
<< 16);
226 regmap_update_bits(priv
->hhi
, HHI_VID_PLL_CLK_DIV
,
227 VID_PLL_PRESET
, VID_PLL_PRESET
);
228 regmap_update_bits(priv
->hhi
, HHI_VID_PLL_CLK_DIV
,
231 regmap_update_bits(priv
->hhi
, HHI_VID_PLL_CLK_DIV
,
235 /* Enable the vid_pll output clock */
236 regmap_update_bits(priv
->hhi
, HHI_VID_PLL_CLK_DIV
,
237 VID_PLL_EN
, VID_PLL_EN
);
241 * Setup VCLK2 for 27MHz, and enable clocks for ENCI and VDAC
243 * TOFIX: Refactor into table to also handle HDMI frequency and paths
245 static void meson_venci_cvbs_clock_config(struct meson_drm
*priv
)
249 /* Setup PLL to output 1.485GHz */
250 if (meson_vpu_is_compatible(priv
, "amlogic,meson-gxbb-vpu")) {
251 regmap_write(priv
->hhi
, HHI_HDMI_PLL_CNTL
, 0x5800023d);
252 regmap_write(priv
->hhi
, HHI_HDMI_PLL_CNTL2
, 0x00404e00);
253 regmap_write(priv
->hhi
, HHI_HDMI_PLL_CNTL3
, 0x0d5c5091);
254 regmap_write(priv
->hhi
, HHI_HDMI_PLL_CNTL4
, 0x801da72c);
255 regmap_write(priv
->hhi
, HHI_HDMI_PLL_CNTL5
, 0x71486980);
256 regmap_write(priv
->hhi
, HHI_HDMI_PLL_CNTL6
, 0x00000e55);
257 regmap_write(priv
->hhi
, HHI_HDMI_PLL_CNTL
, 0x4800023d);
258 } else if (meson_vpu_is_compatible(priv
, "amlogic,meson-gxm-vpu") ||
259 meson_vpu_is_compatible(priv
, "amlogic,meson-gxl-vpu")) {
260 regmap_write(priv
->hhi
, HHI_HDMI_PLL_CNTL
, 0x4000027b);
261 regmap_write(priv
->hhi
, HHI_HDMI_PLL_CNTL2
, 0x800cb300);
262 regmap_write(priv
->hhi
, HHI_HDMI_PLL_CNTL3
, 0xa6212844);
263 regmap_write(priv
->hhi
, HHI_HDMI_PLL_CNTL4
, 0x0c4d000c);
264 regmap_write(priv
->hhi
, HHI_HDMI_PLL_CNTL5
, 0x001fa729);
265 regmap_write(priv
->hhi
, HHI_HDMI_PLL_CNTL6
, 0x01a31500);
268 regmap_update_bits(priv
->hhi
, HHI_HDMI_PLL_CNTL
,
269 HDMI_PLL_RESET
, HDMI_PLL_RESET
);
270 regmap_update_bits(priv
->hhi
, HHI_HDMI_PLL_CNTL
,
274 /* Poll for lock bit */
275 regmap_read_poll_timeout(priv
->hhi
, HHI_HDMI_PLL_CNTL
, val
,
276 (val
& HDMI_PLL_LOCK
), 10, 0);
279 regmap_update_bits(priv
->hhi
, HHI_VIID_CLK_CNTL
, VCLK2_EN
, 0);
281 /* Setup vid_pll to /1 */
282 meson_vid_pll_set(priv
, VID_PLL_DIV_1
);
284 /* Setup the VCLK2 divider value to achieve 27MHz */
285 regmap_update_bits(priv
->hhi
, HHI_VIID_CLK_DIV
,
286 VCLK2_DIV_MASK
, (55 - 1));
288 /* select vid_pll for vclk2 */
289 regmap_update_bits(priv
->hhi
, HHI_VIID_CLK_CNTL
,
290 VCLK2_SEL_MASK
, (4 << VCLK2_SEL_SHIFT
));
291 /* enable vclk2 gate */
292 regmap_update_bits(priv
->hhi
, HHI_VIID_CLK_CNTL
, VCLK2_EN
, VCLK2_EN
);
294 /* select vclk_div1 for enci */
295 regmap_update_bits(priv
->hhi
, HHI_VID_CLK_DIV
,
296 CTS_ENCI_SEL_MASK
, (8 << CTS_ENCI_SEL_SHIFT
));
297 /* select vclk_div1 for vdac */
298 regmap_update_bits(priv
->hhi
, HHI_VIID_CLK_DIV
,
299 CTS_VDAC_SEL_MASK
, (8 << CTS_VDAC_SEL_SHIFT
));
301 /* release vclk2_div_reset and enable vclk2_div */
302 regmap_update_bits(priv
->hhi
, HHI_VIID_CLK_DIV
,
303 VCLK2_DIV_EN
| VCLK2_DIV_RESET
, VCLK2_DIV_EN
);
305 /* enable vclk2_div1 gate */
306 regmap_update_bits(priv
->hhi
, HHI_VIID_CLK_CNTL
,
307 VCLK2_DIV1_EN
, VCLK2_DIV1_EN
);
310 regmap_update_bits(priv
->hhi
, HHI_VIID_CLK_CNTL
,
311 VCLK2_SOFT_RESET
, VCLK2_SOFT_RESET
);
312 regmap_update_bits(priv
->hhi
, HHI_VIID_CLK_CNTL
,
313 VCLK2_SOFT_RESET
, 0);
315 /* enable enci_clk */
316 regmap_update_bits(priv
->hhi
, HHI_VID_CLK_CNTL2
,
317 CTS_ENCI_EN
, CTS_ENCI_EN
);
318 /* enable vdac_clk */
319 regmap_update_bits(priv
->hhi
, HHI_VID_CLK_CNTL2
,
320 CTS_VDAC_EN
, CTS_VDAC_EN
);
324 /* PLL O1 O2 O3 VP DV EN TX */
325 /* 4320 /4 /4 /1 /5 /1 => /2 /2 */
326 MESON_VCLK_HDMI_ENCI_54000
= 1,
327 /* 4320 /4 /4 /1 /5 /1 => /1 /2 */
328 MESON_VCLK_HDMI_DDR_54000
,
329 /* 2970 /4 /1 /1 /5 /1 => /1 /2 */
330 MESON_VCLK_HDMI_DDR_148500
,
331 /* 2970 /2 /2 /2 /5 /1 => /1 /1 */
332 MESON_VCLK_HDMI_74250
,
333 /* 2970 /1 /2 /2 /5 /1 => /1 /1 */
334 MESON_VCLK_HDMI_148500
,
335 /* 2970 /1 /1 /1 /5 /2 => /1 /1 */
336 MESON_VCLK_HDMI_297000
,
337 /* 5940 /1 /1 /2 /5 /1 => /1 /1 */
338 MESON_VCLK_HDMI_594000
341 struct meson_vclk_params
{
342 unsigned int pll_base_freq
;
343 unsigned int pll_od1
;
344 unsigned int pll_od2
;
345 unsigned int pll_od3
;
346 unsigned int vid_pll_div
;
347 unsigned int vclk_div
;
349 [MESON_VCLK_HDMI_ENCI_54000
] = {
350 .pll_base_freq
= 4320000,
354 .vid_pll_div
= VID_PLL_DIV_5
,
357 [MESON_VCLK_HDMI_DDR_54000
] = {
358 .pll_base_freq
= 4320000,
362 .vid_pll_div
= VID_PLL_DIV_5
,
365 [MESON_VCLK_HDMI_DDR_148500
] = {
366 .pll_base_freq
= 2970000,
370 .vid_pll_div
= VID_PLL_DIV_5
,
373 [MESON_VCLK_HDMI_74250
] = {
374 .pll_base_freq
= 2970000,
378 .vid_pll_div
= VID_PLL_DIV_5
,
381 [MESON_VCLK_HDMI_148500
] = {
382 .pll_base_freq
= 2970000,
386 .vid_pll_div
= VID_PLL_DIV_5
,
389 [MESON_VCLK_HDMI_297000
] = {
390 .pll_base_freq
= 2970000,
394 .vid_pll_div
= VID_PLL_DIV_5
,
397 [MESON_VCLK_HDMI_594000
] = {
398 .pll_base_freq
= 5940000,
402 .vid_pll_div
= VID_PLL_DIV_5
,
407 static inline unsigned int pll_od_to_reg(unsigned int od
)
424 void meson_hdmi_pll_set_params(struct meson_drm
*priv
, unsigned int m
,
425 unsigned int frac
, unsigned int od1
,
426 unsigned int od2
, unsigned int od3
)
430 if (meson_vpu_is_compatible(priv
, "amlogic,meson-gxbb-vpu")) {
431 regmap_write(priv
->hhi
, HHI_HDMI_PLL_CNTL
, 0x58000200 | m
);
433 regmap_write(priv
->hhi
, HHI_HDMI_PLL_CNTL2
,
436 regmap_write(priv
->hhi
, HHI_HDMI_PLL_CNTL2
,
438 regmap_write(priv
->hhi
, HHI_HDMI_PLL_CNTL3
, 0x0d5c5091);
439 regmap_write(priv
->hhi
, HHI_HDMI_PLL_CNTL4
, 0x801da72c);
440 regmap_write(priv
->hhi
, HHI_HDMI_PLL_CNTL5
, 0x71486980);
441 regmap_write(priv
->hhi
, HHI_HDMI_PLL_CNTL6
, 0x00000e55);
443 /* Enable and unreset */
444 regmap_update_bits(priv
->hhi
, HHI_HDMI_PLL_CNTL
,
445 0x7 << 28, 0x4 << 28);
447 /* Poll for lock bit */
448 regmap_read_poll_timeout(priv
->hhi
, HHI_HDMI_PLL_CNTL
,
449 val
, (val
& HDMI_PLL_LOCK
), 10, 0);
450 } else if (meson_vpu_is_compatible(priv
, "amlogic,meson-gxm-vpu") ||
451 meson_vpu_is_compatible(priv
, "amlogic,meson-gxl-vpu")) {
452 regmap_write(priv
->hhi
, HHI_HDMI_PLL_CNTL
, 0x40000200 | m
);
453 regmap_write(priv
->hhi
, HHI_HDMI_PLL_CNTL2
, 0x800cb000 | frac
);
454 regmap_write(priv
->hhi
, HHI_HDMI_PLL_CNTL3
, 0x860f30c4);
455 regmap_write(priv
->hhi
, HHI_HDMI_PLL_CNTL4
, 0x0c8e0000);
456 regmap_write(priv
->hhi
, HHI_HDMI_PLL_CNTL5
, 0x001fa729);
457 regmap_write(priv
->hhi
, HHI_HDMI_PLL_CNTL6
, 0x01a31500);
460 regmap_update_bits(priv
->hhi
, HHI_HDMI_PLL_CNTL
,
461 HDMI_PLL_RESET
, HDMI_PLL_RESET
);
462 regmap_update_bits(priv
->hhi
, HHI_HDMI_PLL_CNTL
,
465 /* Poll for lock bit */
466 regmap_read_poll_timeout(priv
->hhi
, HHI_HDMI_PLL_CNTL
, val
,
467 (val
& HDMI_PLL_LOCK
), 10, 0);
470 if (meson_vpu_is_compatible(priv
, "amlogic,meson-gxbb-vpu"))
471 regmap_update_bits(priv
->hhi
, HHI_HDMI_PLL_CNTL2
,
472 3 << 16, pll_od_to_reg(od1
) << 16);
473 else if (meson_vpu_is_compatible(priv
, "amlogic,meson-gxm-vpu") ||
474 meson_vpu_is_compatible(priv
, "amlogic,meson-gxl-vpu"))
475 regmap_update_bits(priv
->hhi
, HHI_HDMI_PLL_CNTL3
,
476 3 << 21, pll_od_to_reg(od1
) << 21);
478 if (meson_vpu_is_compatible(priv
, "amlogic,meson-gxbb-vpu"))
479 regmap_update_bits(priv
->hhi
, HHI_HDMI_PLL_CNTL2
,
480 3 << 22, pll_od_to_reg(od2
) << 22);
481 else if (meson_vpu_is_compatible(priv
, "amlogic,meson-gxm-vpu") ||
482 meson_vpu_is_compatible(priv
, "amlogic,meson-gxl-vpu"))
483 regmap_update_bits(priv
->hhi
, HHI_HDMI_PLL_CNTL3
,
484 3 << 23, pll_od_to_reg(od2
) << 23);
486 if (meson_vpu_is_compatible(priv
, "amlogic,meson-gxbb-vpu"))
487 regmap_update_bits(priv
->hhi
, HHI_HDMI_PLL_CNTL2
,
488 3 << 18, pll_od_to_reg(od3
) << 18);
489 else if (meson_vpu_is_compatible(priv
, "amlogic,meson-gxm-vpu") ||
490 meson_vpu_is_compatible(priv
, "amlogic,meson-gxl-vpu"))
491 regmap_update_bits(priv
->hhi
, HHI_HDMI_PLL_CNTL3
,
492 3 << 19, pll_od_to_reg(od3
) << 19);
496 #define XTAL_FREQ 24000
498 static unsigned int meson_hdmi_pll_get_m(struct meson_drm
*priv
,
499 unsigned int pll_freq
)
501 /* The GXBB PLL has a /2 pre-multiplier */
502 if (meson_vpu_is_compatible(priv
, "amlogic,meson-gxbb-vpu"))
505 return pll_freq
/ XTAL_FREQ
;
508 #define HDMI_FRAC_MAX_GXBB 4096
509 #define HDMI_FRAC_MAX_GXL 1024
511 static unsigned int meson_hdmi_pll_get_frac(struct meson_drm
*priv
,
513 unsigned int pll_freq
)
515 unsigned int parent_freq
= XTAL_FREQ
;
516 unsigned int frac_max
= HDMI_FRAC_MAX_GXL
;
520 /* The GXBB PLL has a /2 pre-multiplier and a larger FRAC width */
521 if (meson_vpu_is_compatible(priv
, "amlogic,meson-gxbb-vpu")) {
522 frac_max
= HDMI_FRAC_MAX_GXBB
;
526 /* We can have a perfect match !*/
527 if (pll_freq
/ m
== parent_freq
&&
531 frac
= div_u64((u64
)pll_freq
* (u64
)frac_max
, parent_freq
);
532 frac_m
= m
* frac_max
;
537 return min((u16
)frac
, (u16
)(frac_max
- 1));
540 static bool meson_hdmi_pll_validate_params(struct meson_drm
*priv
,
544 if (meson_vpu_is_compatible(priv
, "amlogic,meson-gxbb-vpu")) {
545 /* Empiric supported min/max dividers */
546 if (m
< 53 || m
> 123)
548 if (frac
>= HDMI_FRAC_MAX_GXBB
)
550 } else if (meson_vpu_is_compatible(priv
, "amlogic,meson-gxm-vpu") ||
551 meson_vpu_is_compatible(priv
, "amlogic,meson-gxl-vpu")) {
552 /* Empiric supported min/max dividers */
553 if (m
< 106 || m
> 247)
555 if (frac
>= HDMI_FRAC_MAX_GXL
)
562 static bool meson_hdmi_pll_find_params(struct meson_drm
*priv
,
568 /* Cycle from /16 to /2 */
569 for (*od
= 16 ; *od
> 1 ; *od
>>= 1) {
570 *m
= meson_hdmi_pll_get_m(priv
, freq
* *od
);
573 *frac
= meson_hdmi_pll_get_frac(priv
, *m
, freq
* *od
);
575 DRM_DEBUG_DRIVER("PLL params for %dkHz: m=%x frac=%x od=%d\n",
576 freq
, *m
, *frac
, *od
);
578 if (meson_hdmi_pll_validate_params(priv
, *m
, *frac
))
585 /* pll_freq is the frequency after the OD dividers */
587 meson_vclk_dmt_supported_freq(struct meson_drm
*priv
, unsigned int freq
)
589 unsigned int od
, m
, frac
;
591 /* In DMT mode, path after PLL is always /10 */
594 if (meson_hdmi_pll_find_params(priv
, freq
, &m
, &frac
, &od
))
597 return MODE_CLOCK_RANGE
;
599 EXPORT_SYMBOL_GPL(meson_vclk_dmt_supported_freq
);
601 /* pll_freq is the frequency after the OD dividers */
602 static void meson_hdmi_pll_generic_set(struct meson_drm
*priv
,
603 unsigned int pll_freq
)
605 unsigned int od
, m
, frac
, od1
, od2
, od3
;
607 if (meson_hdmi_pll_find_params(priv
, pll_freq
, &m
, &frac
, &od
)) {
617 DRM_DEBUG_DRIVER("PLL params for %dkHz: m=%x frac=%x od=%d/%d/%d\n",
618 pll_freq
, m
, frac
, od1
, od2
, od3
);
620 meson_hdmi_pll_set_params(priv
, m
, frac
, od1
, od2
, od3
);
625 DRM_ERROR("Fatal, unable to find parameters for PLL freq %d\n",
629 static void meson_vclk_set(struct meson_drm
*priv
, unsigned int pll_base_freq
,
630 unsigned int od1
, unsigned int od2
, unsigned int od3
,
631 unsigned int vid_pll_div
, unsigned int vclk_div
,
632 unsigned int hdmi_tx_div
, unsigned int venc_div
,
635 /* Set HDMI-TX sys clock */
636 regmap_update_bits(priv
->hhi
, HHI_HDMI_CLK_CNTL
,
637 CTS_HDMI_SYS_SEL_MASK
, 0);
638 regmap_update_bits(priv
->hhi
, HHI_HDMI_CLK_CNTL
,
639 CTS_HDMI_SYS_DIV_MASK
, 0);
640 regmap_update_bits(priv
->hhi
, HHI_HDMI_CLK_CNTL
,
641 CTS_HDMI_SYS_EN
, CTS_HDMI_SYS_EN
);
643 /* Set HDMI PLL rate */
644 if (!od1
&& !od2
&& !od3
) {
645 meson_hdmi_pll_generic_set(priv
, pll_base_freq
);
646 } else if (meson_vpu_is_compatible(priv
, "amlogic,meson-gxbb-vpu")) {
647 switch (pll_base_freq
) {
649 meson_hdmi_pll_set_params(priv
, 0x3d, 0xe00,
653 meson_hdmi_pll_set_params(priv
, 0x5a, 0,
657 meson_hdmi_pll_set_params(priv
, 0x7b, 0xc00,
661 } else if (meson_vpu_is_compatible(priv
, "amlogic,meson-gxm-vpu") ||
662 meson_vpu_is_compatible(priv
, "amlogic,meson-gxl-vpu")) {
663 switch (pll_base_freq
) {
665 meson_hdmi_pll_set_params(priv
, 0x7b, 0x300,
669 meson_hdmi_pll_set_params(priv
, 0xb4, 0,
673 meson_hdmi_pll_set_params(priv
, 0xf7, 0x200,
679 /* Setup vid_pll divider */
680 meson_vid_pll_set(priv
, vid_pll_div
);
683 regmap_update_bits(priv
->hhi
, HHI_VID_CLK_CNTL
,
685 regmap_update_bits(priv
->hhi
, HHI_VID_CLK_DIV
,
686 VCLK_DIV_MASK
, vclk_div
- 1);
688 /* Set HDMI-TX source */
689 switch (hdmi_tx_div
) {
691 /* enable vclk_div1 gate */
692 regmap_update_bits(priv
->hhi
, HHI_VID_CLK_CNTL
,
693 VCLK_DIV1_EN
, VCLK_DIV1_EN
);
695 /* select vclk_div1 for HDMI-TX */
696 regmap_update_bits(priv
->hhi
, HHI_HDMI_CLK_CNTL
,
697 HDMI_TX_PIXEL_SEL_MASK
, 0);
700 /* enable vclk_div2 gate */
701 regmap_update_bits(priv
->hhi
, HHI_VID_CLK_CNTL
,
702 VCLK_DIV2_EN
, VCLK_DIV2_EN
);
704 /* select vclk_div2 for HDMI-TX */
705 regmap_update_bits(priv
->hhi
, HHI_HDMI_CLK_CNTL
,
706 HDMI_TX_PIXEL_SEL_MASK
, 1 << HDMI_TX_PIXEL_SEL_SHIFT
);
709 /* enable vclk_div4 gate */
710 regmap_update_bits(priv
->hhi
, HHI_VID_CLK_CNTL
,
711 VCLK_DIV4_EN
, VCLK_DIV4_EN
);
713 /* select vclk_div4 for HDMI-TX */
714 regmap_update_bits(priv
->hhi
, HHI_HDMI_CLK_CNTL
,
715 HDMI_TX_PIXEL_SEL_MASK
, 2 << HDMI_TX_PIXEL_SEL_SHIFT
);
718 /* enable vclk_div6 gate */
719 regmap_update_bits(priv
->hhi
, HHI_VID_CLK_CNTL
,
720 VCLK_DIV6_EN
, VCLK_DIV6_EN
);
722 /* select vclk_div6 for HDMI-TX */
723 regmap_update_bits(priv
->hhi
, HHI_HDMI_CLK_CNTL
,
724 HDMI_TX_PIXEL_SEL_MASK
, 3 << HDMI_TX_PIXEL_SEL_SHIFT
);
727 /* enable vclk_div12 gate */
728 regmap_update_bits(priv
->hhi
, HHI_VID_CLK_CNTL
,
729 VCLK_DIV12_EN
, VCLK_DIV12_EN
);
731 /* select vclk_div12 for HDMI-TX */
732 regmap_update_bits(priv
->hhi
, HHI_HDMI_CLK_CNTL
,
733 HDMI_TX_PIXEL_SEL_MASK
, 4 << HDMI_TX_PIXEL_SEL_SHIFT
);
736 regmap_update_bits(priv
->hhi
, HHI_VID_CLK_CNTL2
,
737 HDMI_TX_PIXEL_EN
, HDMI_TX_PIXEL_EN
);
739 /* Set ENCI/ENCP Source */
742 /* enable vclk_div1 gate */
743 regmap_update_bits(priv
->hhi
, HHI_VID_CLK_CNTL
,
744 VCLK_DIV1_EN
, VCLK_DIV1_EN
);
747 /* select vclk_div1 for enci */
748 regmap_update_bits(priv
->hhi
, HHI_VID_CLK_DIV
,
749 CTS_ENCI_SEL_MASK
, 0);
751 /* select vclk_div1 for encp */
752 regmap_update_bits(priv
->hhi
, HHI_VID_CLK_DIV
,
753 CTS_ENCP_SEL_MASK
, 0);
756 /* enable vclk_div2 gate */
757 regmap_update_bits(priv
->hhi
, HHI_VID_CLK_CNTL
,
758 VCLK_DIV2_EN
, VCLK_DIV2_EN
);
761 /* select vclk_div2 for enci */
762 regmap_update_bits(priv
->hhi
, HHI_VID_CLK_DIV
,
763 CTS_ENCI_SEL_MASK
, 1 << CTS_ENCI_SEL_SHIFT
);
765 /* select vclk_div2 for encp */
766 regmap_update_bits(priv
->hhi
, HHI_VID_CLK_DIV
,
767 CTS_ENCP_SEL_MASK
, 1 << CTS_ENCP_SEL_SHIFT
);
770 /* enable vclk_div4 gate */
771 regmap_update_bits(priv
->hhi
, HHI_VID_CLK_CNTL
,
772 VCLK_DIV4_EN
, VCLK_DIV4_EN
);
775 /* select vclk_div4 for enci */
776 regmap_update_bits(priv
->hhi
, HHI_VID_CLK_DIV
,
777 CTS_ENCI_SEL_MASK
, 2 << CTS_ENCI_SEL_SHIFT
);
779 /* select vclk_div4 for encp */
780 regmap_update_bits(priv
->hhi
, HHI_VID_CLK_DIV
,
781 CTS_ENCP_SEL_MASK
, 2 << CTS_ENCP_SEL_SHIFT
);
784 /* enable vclk_div6 gate */
785 regmap_update_bits(priv
->hhi
, HHI_VID_CLK_CNTL
,
786 VCLK_DIV6_EN
, VCLK_DIV6_EN
);
789 /* select vclk_div6 for enci */
790 regmap_update_bits(priv
->hhi
, HHI_VID_CLK_DIV
,
791 CTS_ENCI_SEL_MASK
, 3 << CTS_ENCI_SEL_SHIFT
);
793 /* select vclk_div6 for encp */
794 regmap_update_bits(priv
->hhi
, HHI_VID_CLK_DIV
,
795 CTS_ENCP_SEL_MASK
, 3 << CTS_ENCP_SEL_SHIFT
);
798 /* enable vclk_div12 gate */
799 regmap_update_bits(priv
->hhi
, HHI_VID_CLK_CNTL
,
800 VCLK_DIV12_EN
, VCLK_DIV12_EN
);
803 /* select vclk_div12 for enci */
804 regmap_update_bits(priv
->hhi
, HHI_VID_CLK_DIV
,
805 CTS_ENCI_SEL_MASK
, 4 << CTS_ENCI_SEL_SHIFT
);
807 /* select vclk_div12 for encp */
808 regmap_update_bits(priv
->hhi
, HHI_VID_CLK_DIV
,
809 CTS_ENCP_SEL_MASK
, 4 << CTS_ENCP_SEL_SHIFT
);
814 /* Enable ENCI clock gate */
815 regmap_update_bits(priv
->hhi
, HHI_VID_CLK_CNTL2
,
816 CTS_ENCI_EN
, CTS_ENCI_EN
);
818 /* Enable ENCP clock gate */
819 regmap_update_bits(priv
->hhi
, HHI_VID_CLK_CNTL2
,
820 CTS_ENCP_EN
, CTS_ENCP_EN
);
822 regmap_update_bits(priv
->hhi
, HHI_VID_CLK_CNTL
, VCLK_EN
, VCLK_EN
);
825 void meson_vclk_setup(struct meson_drm
*priv
, unsigned int target
,
826 unsigned int vclk_freq
, unsigned int venc_freq
,
827 unsigned int dac_freq
, bool hdmi_use_enci
)
830 unsigned int hdmi_tx_div
;
831 unsigned int venc_div
;
833 if (target
== MESON_VCLK_TARGET_CVBS
) {
834 meson_venci_cvbs_clock_config(priv
);
836 } else if (target
== MESON_VCLK_TARGET_DMT
) {
837 /* The DMT clock path is fixed after the PLL:
838 * - automatic PLL freq + OD management
839 * - vid_pll_div = VID_PLL_DIV_5
845 meson_vclk_set(priv
, vclk_freq
* 10, 0, 0, 0,
846 VID_PLL_DIV_5
, 2, 1, 1, false);
850 hdmi_tx_div
= vclk_freq
/ dac_freq
;
852 if (hdmi_tx_div
== 0) {
853 pr_err("Fatal Error, invalid HDMI-TX freq %d\n",
858 venc_div
= vclk_freq
/ venc_freq
;
861 pr_err("Fatal Error, invalid HDMI venc freq %d\n",
869 freq
= MESON_VCLK_HDMI_ENCI_54000
;
871 freq
= MESON_VCLK_HDMI_DDR_54000
;
874 freq
= MESON_VCLK_HDMI_74250
;
877 if (dac_freq
!= 148500)
878 freq
= MESON_VCLK_HDMI_DDR_148500
;
880 freq
= MESON_VCLK_HDMI_148500
;
883 freq
= MESON_VCLK_HDMI_297000
;
886 freq
= MESON_VCLK_HDMI_594000
;
889 pr_err("Fatal Error, invalid HDMI vclk freq %d\n",
894 meson_vclk_set(priv
, params
[freq
].pll_base_freq
,
895 params
[freq
].pll_od1
, params
[freq
].pll_od2
,
896 params
[freq
].pll_od3
, params
[freq
].vid_pll_div
,
897 params
[freq
].vclk_div
, hdmi_tx_div
, venc_div
,
900 EXPORT_SYMBOL_GPL(meson_vclk_setup
);