2 * Copyright (C) 2016 BayLibre, SAS
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 #define FREQ_1000_1001(_freq) DIV_ROUND_CLOSEST(_freq * 1000, 1001)
122 /* VID PLL Dividers */
141 void meson_vid_pll_set(struct meson_drm *priv, unsigned int div)
143 unsigned int shift_val = 0;
144 unsigned int shift_sel = 0;
146 /* Disable vid_pll output clock */
147 regmap_update_bits(priv->hhi, HHI_VID_PLL_CLK_DIV, VID_PLL_EN, 0);
148 regmap_update_bits(priv->hhi, HHI_VID_PLL_CLK_DIV, VID_PLL_PRESET, 0);
155 case VID_PLL_DIV_2p5:
163 case VID_PLL_DIV_3p5:
167 case VID_PLL_DIV_3p75:
183 case VID_PLL_DIV_6p25:
191 case VID_PLL_DIV_7p5:
209 if (div == VID_PLL_DIV_1)
210 /* Enable vid_pll bypass to HDMI pll */
211 regmap_update_bits(priv->hhi, HHI_VID_PLL_CLK_DIV,
212 VID_PLL_BYPASS, VID_PLL_BYPASS);
215 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,
222 regmap_update_bits(priv->hhi, HHI_VID_PLL_CLK_DIV,
225 /* Setup sel and val */
226 regmap_update_bits(priv->hhi, HHI_VID_PLL_CLK_DIV,
227 3 << 16, shift_sel << 16);
228 regmap_update_bits(priv->hhi, HHI_VID_PLL_CLK_DIV,
229 VID_PLL_PRESET, VID_PLL_PRESET);
230 regmap_update_bits(priv->hhi, HHI_VID_PLL_CLK_DIV,
233 regmap_update_bits(priv->hhi, HHI_VID_PLL_CLK_DIV,
237 /* Enable the vid_pll output clock */
238 regmap_update_bits(priv->hhi, HHI_VID_PLL_CLK_DIV,
239 VID_PLL_EN, VID_PLL_EN);
243 * Setup VCLK2 for 27MHz, and enable clocks for ENCI and VDAC
245 * TOFIX: Refactor into table to also handle HDMI frequency and paths
247 static void meson_venci_cvbs_clock_config(struct meson_drm *priv)
251 /* Setup PLL to output 1.485GHz */
252 if (meson_vpu_is_compatible(priv, "amlogic,meson-gxbb-vpu")) {
253 regmap_write(priv->hhi, HHI_HDMI_PLL_CNTL, 0x5800023d);
254 regmap_write(priv->hhi, HHI_HDMI_PLL_CNTL2, 0x00404e00);
255 regmap_write(priv->hhi, HHI_HDMI_PLL_CNTL3, 0x0d5c5091);
256 regmap_write(priv->hhi, HHI_HDMI_PLL_CNTL4, 0x801da72c);
257 regmap_write(priv->hhi, HHI_HDMI_PLL_CNTL5, 0x71486980);
258 regmap_write(priv->hhi, HHI_HDMI_PLL_CNTL6, 0x00000e55);
259 regmap_write(priv->hhi, HHI_HDMI_PLL_CNTL, 0x4800023d);
260 } else if (meson_vpu_is_compatible(priv, "amlogic,meson-gxm-vpu") ||
261 meson_vpu_is_compatible(priv, "amlogic,meson-gxl-vpu")) {
262 regmap_write(priv->hhi, HHI_HDMI_PLL_CNTL, 0x4000027b);
263 regmap_write(priv->hhi, HHI_HDMI_PLL_CNTL2, 0x800cb300);
264 regmap_write(priv->hhi, HHI_HDMI_PLL_CNTL3, 0xa6212844);
265 regmap_write(priv->hhi, HHI_HDMI_PLL_CNTL4, 0x0c4d000c);
266 regmap_write(priv->hhi, HHI_HDMI_PLL_CNTL5, 0x001fa729);
267 regmap_write(priv->hhi, HHI_HDMI_PLL_CNTL6, 0x01a31500);
270 regmap_update_bits(priv->hhi, HHI_HDMI_PLL_CNTL,
271 HDMI_PLL_RESET, HDMI_PLL_RESET);
272 regmap_update_bits(priv->hhi, HHI_HDMI_PLL_CNTL,
276 /* Poll for lock bit */
277 regmap_read_poll_timeout(priv->hhi, HHI_HDMI_PLL_CNTL, val,
278 (val & HDMI_PLL_LOCK), 10, 0);
281 regmap_update_bits(priv->hhi, HHI_VIID_CLK_CNTL, VCLK2_EN, 0);
283 /* Setup vid_pll to /1 */
284 meson_vid_pll_set(priv, VID_PLL_DIV_1);
286 /* Setup the VCLK2 divider value to achieve 27MHz */
287 regmap_update_bits(priv->hhi, HHI_VIID_CLK_DIV,
288 VCLK2_DIV_MASK, (55 - 1));
290 /* select vid_pll for vclk2 */
291 regmap_update_bits(priv->hhi, HHI_VIID_CLK_CNTL,
292 VCLK2_SEL_MASK, (4 << VCLK2_SEL_SHIFT));
293 /* enable vclk2 gate */
294 regmap_update_bits(priv->hhi, HHI_VIID_CLK_CNTL, VCLK2_EN, VCLK2_EN);
296 /* select vclk_div1 for enci */
297 regmap_update_bits(priv->hhi, HHI_VID_CLK_DIV,
298 CTS_ENCI_SEL_MASK, (8 << CTS_ENCI_SEL_SHIFT));
299 /* select vclk_div1 for vdac */
300 regmap_update_bits(priv->hhi, HHI_VIID_CLK_DIV,
301 CTS_VDAC_SEL_MASK, (8 << CTS_VDAC_SEL_SHIFT));
303 /* release vclk2_div_reset and enable vclk2_div */
304 regmap_update_bits(priv->hhi, HHI_VIID_CLK_DIV,
305 VCLK2_DIV_EN | VCLK2_DIV_RESET, VCLK2_DIV_EN);
307 /* enable vclk2_div1 gate */
308 regmap_update_bits(priv->hhi, HHI_VIID_CLK_CNTL,
309 VCLK2_DIV1_EN, VCLK2_DIV1_EN);
312 regmap_update_bits(priv->hhi, HHI_VIID_CLK_CNTL,
313 VCLK2_SOFT_RESET, VCLK2_SOFT_RESET);
314 regmap_update_bits(priv->hhi, HHI_VIID_CLK_CNTL,
315 VCLK2_SOFT_RESET, 0);
317 /* enable enci_clk */
318 regmap_update_bits(priv->hhi, HHI_VID_CLK_CNTL2,
319 CTS_ENCI_EN, CTS_ENCI_EN);
320 /* enable vdac_clk */
321 regmap_update_bits(priv->hhi, HHI_VID_CLK_CNTL2,
322 CTS_VDAC_EN, CTS_VDAC_EN);
326 /* PLL O1 O2 O3 VP DV EN TX */
327 /* 4320 /4 /4 /1 /5 /1 => /2 /2 */
328 MESON_VCLK_HDMI_ENCI_54000 = 0,
329 /* 4320 /4 /4 /1 /5 /1 => /1 /2 */
330 MESON_VCLK_HDMI_DDR_54000,
331 /* 2970 /4 /1 /1 /5 /1 => /1 /2 */
332 MESON_VCLK_HDMI_DDR_148500,
333 /* 2970 /2 /2 /2 /5 /1 => /1 /1 */
334 MESON_VCLK_HDMI_74250,
335 /* 2970 /1 /2 /2 /5 /1 => /1 /1 */
336 MESON_VCLK_HDMI_148500,
337 /* 2970 /1 /1 /1 /5 /2 => /1 /1 */
338 MESON_VCLK_HDMI_297000,
339 /* 5940 /1 /1 /2 /5 /1 => /1 /1 */
340 MESON_VCLK_HDMI_594000
343 struct meson_vclk_params {
344 unsigned int pixel_freq;
345 unsigned int pll_base_freq;
346 unsigned int pll_od1;
347 unsigned int pll_od2;
348 unsigned int pll_od3;
349 unsigned int vid_pll_div;
350 unsigned int vclk_div;
352 [MESON_VCLK_HDMI_ENCI_54000] = {
354 .pll_base_freq = 4320000,
358 .vid_pll_div = VID_PLL_DIV_5,
361 [MESON_VCLK_HDMI_DDR_54000] = {
363 .pll_base_freq = 4320000,
367 .vid_pll_div = VID_PLL_DIV_5,
370 [MESON_VCLK_HDMI_DDR_148500] = {
371 .pixel_freq = 148500,
372 .pll_base_freq = 2970000,
376 .vid_pll_div = VID_PLL_DIV_5,
379 [MESON_VCLK_HDMI_74250] = {
381 .pll_base_freq = 2970000,
385 .vid_pll_div = VID_PLL_DIV_5,
388 [MESON_VCLK_HDMI_148500] = {
389 .pixel_freq = 148500,
390 .pll_base_freq = 2970000,
394 .vid_pll_div = VID_PLL_DIV_5,
397 [MESON_VCLK_HDMI_297000] = {
398 .pixel_freq = 297000,
399 .pll_base_freq = 2970000,
403 .vid_pll_div = VID_PLL_DIV_5,
406 [MESON_VCLK_HDMI_594000] = {
407 .pixel_freq = 594000,
408 .pll_base_freq = 5940000,
412 .vid_pll_div = VID_PLL_DIV_5,
418 static inline unsigned int pll_od_to_reg(unsigned int od)
435 void meson_hdmi_pll_set_params(struct meson_drm *priv, unsigned int m,
436 unsigned int frac, unsigned int od1,
437 unsigned int od2, unsigned int od3)
441 if (meson_vpu_is_compatible(priv, "amlogic,meson-gxbb-vpu")) {
442 regmap_write(priv->hhi, HHI_HDMI_PLL_CNTL, 0x58000200 | m);
444 regmap_write(priv->hhi, HHI_HDMI_PLL_CNTL2,
447 regmap_write(priv->hhi, HHI_HDMI_PLL_CNTL2,
449 regmap_write(priv->hhi, HHI_HDMI_PLL_CNTL3, 0x0d5c5091);
450 regmap_write(priv->hhi, HHI_HDMI_PLL_CNTL4, 0x801da72c);
451 regmap_write(priv->hhi, HHI_HDMI_PLL_CNTL5, 0x71486980);
452 regmap_write(priv->hhi, HHI_HDMI_PLL_CNTL6, 0x00000e55);
454 /* Enable and unreset */
455 regmap_update_bits(priv->hhi, HHI_HDMI_PLL_CNTL,
456 0x7 << 28, 0x4 << 28);
458 /* Poll for lock bit */
459 regmap_read_poll_timeout(priv->hhi, HHI_HDMI_PLL_CNTL,
460 val, (val & HDMI_PLL_LOCK), 10, 0);
461 } else if (meson_vpu_is_compatible(priv, "amlogic,meson-gxm-vpu") ||
462 meson_vpu_is_compatible(priv, "amlogic,meson-gxl-vpu")) {
463 regmap_write(priv->hhi, HHI_HDMI_PLL_CNTL, 0x40000200 | m);
464 regmap_write(priv->hhi, HHI_HDMI_PLL_CNTL2, 0x800cb000 | frac);
465 regmap_write(priv->hhi, HHI_HDMI_PLL_CNTL3, 0x860f30c4);
466 regmap_write(priv->hhi, HHI_HDMI_PLL_CNTL4, 0x0c8e0000);
467 regmap_write(priv->hhi, HHI_HDMI_PLL_CNTL5, 0x001fa729);
468 regmap_write(priv->hhi, HHI_HDMI_PLL_CNTL6, 0x01a31500);
471 regmap_update_bits(priv->hhi, HHI_HDMI_PLL_CNTL,
472 HDMI_PLL_RESET, HDMI_PLL_RESET);
473 regmap_update_bits(priv->hhi, HHI_HDMI_PLL_CNTL,
476 /* Poll for lock bit */
477 regmap_read_poll_timeout(priv->hhi, HHI_HDMI_PLL_CNTL, val,
478 (val & HDMI_PLL_LOCK), 10, 0);
481 if (meson_vpu_is_compatible(priv, "amlogic,meson-gxbb-vpu"))
482 regmap_update_bits(priv->hhi, HHI_HDMI_PLL_CNTL2,
483 3 << 16, pll_od_to_reg(od1) << 16);
484 else if (meson_vpu_is_compatible(priv, "amlogic,meson-gxm-vpu") ||
485 meson_vpu_is_compatible(priv, "amlogic,meson-gxl-vpu"))
486 regmap_update_bits(priv->hhi, HHI_HDMI_PLL_CNTL3,
487 3 << 21, pll_od_to_reg(od1) << 21);
489 if (meson_vpu_is_compatible(priv, "amlogic,meson-gxbb-vpu"))
490 regmap_update_bits(priv->hhi, HHI_HDMI_PLL_CNTL2,
491 3 << 22, pll_od_to_reg(od2) << 22);
492 else if (meson_vpu_is_compatible(priv, "amlogic,meson-gxm-vpu") ||
493 meson_vpu_is_compatible(priv, "amlogic,meson-gxl-vpu"))
494 regmap_update_bits(priv->hhi, HHI_HDMI_PLL_CNTL3,
495 3 << 23, pll_od_to_reg(od2) << 23);
497 if (meson_vpu_is_compatible(priv, "amlogic,meson-gxbb-vpu"))
498 regmap_update_bits(priv->hhi, HHI_HDMI_PLL_CNTL2,
499 3 << 18, pll_od_to_reg(od3) << 18);
500 else if (meson_vpu_is_compatible(priv, "amlogic,meson-gxm-vpu") ||
501 meson_vpu_is_compatible(priv, "amlogic,meson-gxl-vpu"))
502 regmap_update_bits(priv->hhi, HHI_HDMI_PLL_CNTL3,
503 3 << 19, pll_od_to_reg(od3) << 19);
507 #define XTAL_FREQ 24000
509 static unsigned int meson_hdmi_pll_get_m(struct meson_drm *priv,
510 unsigned int pll_freq)
512 /* The GXBB PLL has a /2 pre-multiplier */
513 if (meson_vpu_is_compatible(priv, "amlogic,meson-gxbb-vpu"))
516 return pll_freq / XTAL_FREQ;
519 #define HDMI_FRAC_MAX_GXBB 4096
520 #define HDMI_FRAC_MAX_GXL 1024
522 static unsigned int meson_hdmi_pll_get_frac(struct meson_drm *priv,
524 unsigned int pll_freq)
526 unsigned int parent_freq = XTAL_FREQ;
527 unsigned int frac_max = HDMI_FRAC_MAX_GXL;
531 /* The GXBB PLL has a /2 pre-multiplier and a larger FRAC width */
532 if (meson_vpu_is_compatible(priv, "amlogic,meson-gxbb-vpu")) {
533 frac_max = HDMI_FRAC_MAX_GXBB;
537 /* We can have a perfect match !*/
538 if (pll_freq / m == parent_freq &&
542 frac = div_u64((u64)pll_freq * (u64)frac_max, parent_freq);
543 frac_m = m * frac_max;
548 return min((u16)frac, (u16)(frac_max - 1));
551 static bool meson_hdmi_pll_validate_params(struct meson_drm *priv,
555 if (meson_vpu_is_compatible(priv, "amlogic,meson-gxbb-vpu")) {
556 /* Empiric supported min/max dividers */
557 if (m < 53 || m > 123)
559 if (frac >= HDMI_FRAC_MAX_GXBB)
561 } else if (meson_vpu_is_compatible(priv, "amlogic,meson-gxm-vpu") ||
562 meson_vpu_is_compatible(priv, "amlogic,meson-gxl-vpu")) {
563 /* Empiric supported min/max dividers */
564 if (m < 106 || m > 247)
566 if (frac >= HDMI_FRAC_MAX_GXL)
573 static bool meson_hdmi_pll_find_params(struct meson_drm *priv,
579 /* Cycle from /16 to /2 */
580 for (*od = 16 ; *od > 1 ; *od >>= 1) {
581 *m = meson_hdmi_pll_get_m(priv, freq * *od);
584 *frac = meson_hdmi_pll_get_frac(priv, *m, freq * *od);
586 DRM_DEBUG_DRIVER("PLL params for %dkHz: m=%x frac=%x od=%d\n",
587 freq, *m, *frac, *od);
589 if (meson_hdmi_pll_validate_params(priv, *m, *frac))
596 /* pll_freq is the frequency after the OD dividers */
598 meson_vclk_dmt_supported_freq(struct meson_drm *priv, unsigned int freq)
600 unsigned int od, m, frac;
602 /* In DMT mode, path after PLL is always /10 */
605 if (meson_hdmi_pll_find_params(priv, freq, &m, &frac, &od))
608 return MODE_CLOCK_RANGE;
610 EXPORT_SYMBOL_GPL(meson_vclk_dmt_supported_freq);
612 /* pll_freq is the frequency after the OD dividers */
613 static void meson_hdmi_pll_generic_set(struct meson_drm *priv,
614 unsigned int pll_freq)
616 unsigned int od, m, frac, od1, od2, od3;
618 if (meson_hdmi_pll_find_params(priv, pll_freq, &m, &frac, &od)) {
628 DRM_DEBUG_DRIVER("PLL params for %dkHz: m=%x frac=%x od=%d/%d/%d\n",
629 pll_freq, m, frac, od1, od2, od3);
631 meson_hdmi_pll_set_params(priv, m, frac, od1, od2, od3);
636 DRM_ERROR("Fatal, unable to find parameters for PLL freq %d\n",
641 meson_vclk_vic_supported_freq(unsigned int freq)
645 DRM_DEBUG_DRIVER("freq = %d\n", freq);
647 for (i = 0 ; params[i].pixel_freq ; ++i) {
648 DRM_DEBUG_DRIVER("i = %d pixel_freq = %d alt = %d\n",
649 i, params[i].pixel_freq,
650 FREQ_1000_1001(params[i].pixel_freq));
651 /* Match strict frequency */
652 if (freq == params[i].pixel_freq)
654 /* Match 1000/1001 variant */
655 if (freq == FREQ_1000_1001(params[i].pixel_freq))
659 return MODE_CLOCK_RANGE;
661 EXPORT_SYMBOL_GPL(meson_vclk_vic_supported_freq);
663 static void meson_vclk_set(struct meson_drm *priv, unsigned int pll_base_freq,
664 unsigned int od1, unsigned int od2, unsigned int od3,
665 unsigned int vid_pll_div, unsigned int vclk_div,
666 unsigned int hdmi_tx_div, unsigned int venc_div,
667 bool hdmi_use_enci, bool vic_alternate_clock)
669 unsigned int m = 0, frac = 0;
671 /* Set HDMI-TX sys clock */
672 regmap_update_bits(priv->hhi, HHI_HDMI_CLK_CNTL,
673 CTS_HDMI_SYS_SEL_MASK, 0);
674 regmap_update_bits(priv->hhi, HHI_HDMI_CLK_CNTL,
675 CTS_HDMI_SYS_DIV_MASK, 0);
676 regmap_update_bits(priv->hhi, HHI_HDMI_CLK_CNTL,
677 CTS_HDMI_SYS_EN, CTS_HDMI_SYS_EN);
679 /* Set HDMI PLL rate */
680 if (!od1 && !od2 && !od3) {
681 meson_hdmi_pll_generic_set(priv, pll_base_freq);
682 } else if (meson_vpu_is_compatible(priv, "amlogic,meson-gxbb-vpu")) {
683 switch (pll_base_freq) {
686 frac = vic_alternate_clock ? 0xd02 : 0xe00;
689 m = vic_alternate_clock ? 0x59 : 0x5a;
690 frac = vic_alternate_clock ? 0xe8f : 0;
694 frac = vic_alternate_clock ? 0xa05 : 0xc00;
698 meson_hdmi_pll_set_params(priv, m, frac, od1, od2, od3);
699 } else if (meson_vpu_is_compatible(priv, "amlogic,meson-gxm-vpu") ||
700 meson_vpu_is_compatible(priv, "amlogic,meson-gxl-vpu")) {
701 switch (pll_base_freq) {
704 frac = vic_alternate_clock ? 0x281 : 0x300;
707 m = vic_alternate_clock ? 0xb3 : 0xb4;
708 frac = vic_alternate_clock ? 0x347 : 0;
712 frac = vic_alternate_clock ? 0x102 : 0x200;
716 meson_hdmi_pll_set_params(priv, m, frac, od1, od2, od3);
719 /* Setup vid_pll divider */
720 meson_vid_pll_set(priv, vid_pll_div);
723 regmap_update_bits(priv->hhi, HHI_VID_CLK_CNTL,
725 regmap_update_bits(priv->hhi, HHI_VID_CLK_DIV,
726 VCLK_DIV_MASK, vclk_div - 1);
728 /* Set HDMI-TX source */
729 switch (hdmi_tx_div) {
731 /* enable vclk_div1 gate */
732 regmap_update_bits(priv->hhi, HHI_VID_CLK_CNTL,
733 VCLK_DIV1_EN, VCLK_DIV1_EN);
735 /* select vclk_div1 for HDMI-TX */
736 regmap_update_bits(priv->hhi, HHI_HDMI_CLK_CNTL,
737 HDMI_TX_PIXEL_SEL_MASK, 0);
740 /* enable vclk_div2 gate */
741 regmap_update_bits(priv->hhi, HHI_VID_CLK_CNTL,
742 VCLK_DIV2_EN, VCLK_DIV2_EN);
744 /* select vclk_div2 for HDMI-TX */
745 regmap_update_bits(priv->hhi, HHI_HDMI_CLK_CNTL,
746 HDMI_TX_PIXEL_SEL_MASK, 1 << HDMI_TX_PIXEL_SEL_SHIFT);
749 /* enable vclk_div4 gate */
750 regmap_update_bits(priv->hhi, HHI_VID_CLK_CNTL,
751 VCLK_DIV4_EN, VCLK_DIV4_EN);
753 /* select vclk_div4 for HDMI-TX */
754 regmap_update_bits(priv->hhi, HHI_HDMI_CLK_CNTL,
755 HDMI_TX_PIXEL_SEL_MASK, 2 << HDMI_TX_PIXEL_SEL_SHIFT);
758 /* enable vclk_div6 gate */
759 regmap_update_bits(priv->hhi, HHI_VID_CLK_CNTL,
760 VCLK_DIV6_EN, VCLK_DIV6_EN);
762 /* select vclk_div6 for HDMI-TX */
763 regmap_update_bits(priv->hhi, HHI_HDMI_CLK_CNTL,
764 HDMI_TX_PIXEL_SEL_MASK, 3 << HDMI_TX_PIXEL_SEL_SHIFT);
767 /* enable vclk_div12 gate */
768 regmap_update_bits(priv->hhi, HHI_VID_CLK_CNTL,
769 VCLK_DIV12_EN, VCLK_DIV12_EN);
771 /* select vclk_div12 for HDMI-TX */
772 regmap_update_bits(priv->hhi, HHI_HDMI_CLK_CNTL,
773 HDMI_TX_PIXEL_SEL_MASK, 4 << HDMI_TX_PIXEL_SEL_SHIFT);
776 regmap_update_bits(priv->hhi, HHI_VID_CLK_CNTL2,
777 HDMI_TX_PIXEL_EN, HDMI_TX_PIXEL_EN);
779 /* Set ENCI/ENCP Source */
782 /* enable vclk_div1 gate */
783 regmap_update_bits(priv->hhi, HHI_VID_CLK_CNTL,
784 VCLK_DIV1_EN, VCLK_DIV1_EN);
787 /* select vclk_div1 for enci */
788 regmap_update_bits(priv->hhi, HHI_VID_CLK_DIV,
789 CTS_ENCI_SEL_MASK, 0);
791 /* select vclk_div1 for encp */
792 regmap_update_bits(priv->hhi, HHI_VID_CLK_DIV,
793 CTS_ENCP_SEL_MASK, 0);
796 /* enable vclk_div2 gate */
797 regmap_update_bits(priv->hhi, HHI_VID_CLK_CNTL,
798 VCLK_DIV2_EN, VCLK_DIV2_EN);
801 /* select vclk_div2 for enci */
802 regmap_update_bits(priv->hhi, HHI_VID_CLK_DIV,
803 CTS_ENCI_SEL_MASK, 1 << CTS_ENCI_SEL_SHIFT);
805 /* select vclk_div2 for encp */
806 regmap_update_bits(priv->hhi, HHI_VID_CLK_DIV,
807 CTS_ENCP_SEL_MASK, 1 << CTS_ENCP_SEL_SHIFT);
810 /* enable vclk_div4 gate */
811 regmap_update_bits(priv->hhi, HHI_VID_CLK_CNTL,
812 VCLK_DIV4_EN, VCLK_DIV4_EN);
815 /* select vclk_div4 for enci */
816 regmap_update_bits(priv->hhi, HHI_VID_CLK_DIV,
817 CTS_ENCI_SEL_MASK, 2 << CTS_ENCI_SEL_SHIFT);
819 /* select vclk_div4 for encp */
820 regmap_update_bits(priv->hhi, HHI_VID_CLK_DIV,
821 CTS_ENCP_SEL_MASK, 2 << CTS_ENCP_SEL_SHIFT);
824 /* enable vclk_div6 gate */
825 regmap_update_bits(priv->hhi, HHI_VID_CLK_CNTL,
826 VCLK_DIV6_EN, VCLK_DIV6_EN);
829 /* select vclk_div6 for enci */
830 regmap_update_bits(priv->hhi, HHI_VID_CLK_DIV,
831 CTS_ENCI_SEL_MASK, 3 << CTS_ENCI_SEL_SHIFT);
833 /* select vclk_div6 for encp */
834 regmap_update_bits(priv->hhi, HHI_VID_CLK_DIV,
835 CTS_ENCP_SEL_MASK, 3 << CTS_ENCP_SEL_SHIFT);
838 /* enable vclk_div12 gate */
839 regmap_update_bits(priv->hhi, HHI_VID_CLK_CNTL,
840 VCLK_DIV12_EN, VCLK_DIV12_EN);
843 /* select vclk_div12 for enci */
844 regmap_update_bits(priv->hhi, HHI_VID_CLK_DIV,
845 CTS_ENCI_SEL_MASK, 4 << CTS_ENCI_SEL_SHIFT);
847 /* select vclk_div12 for encp */
848 regmap_update_bits(priv->hhi, HHI_VID_CLK_DIV,
849 CTS_ENCP_SEL_MASK, 4 << CTS_ENCP_SEL_SHIFT);
854 /* Enable ENCI clock gate */
855 regmap_update_bits(priv->hhi, HHI_VID_CLK_CNTL2,
856 CTS_ENCI_EN, CTS_ENCI_EN);
858 /* Enable ENCP clock gate */
859 regmap_update_bits(priv->hhi, HHI_VID_CLK_CNTL2,
860 CTS_ENCP_EN, CTS_ENCP_EN);
862 regmap_update_bits(priv->hhi, HHI_VID_CLK_CNTL, VCLK_EN, VCLK_EN);
865 void meson_vclk_setup(struct meson_drm *priv, unsigned int target,
866 unsigned int vclk_freq, unsigned int venc_freq,
867 unsigned int dac_freq, bool hdmi_use_enci)
869 bool vic_alternate_clock = false;
871 unsigned int hdmi_tx_div;
872 unsigned int venc_div;
874 if (target == MESON_VCLK_TARGET_CVBS) {
875 meson_venci_cvbs_clock_config(priv);
877 } else if (target == MESON_VCLK_TARGET_DMT) {
878 /* The DMT clock path is fixed after the PLL:
879 * - automatic PLL freq + OD management
880 * - vid_pll_div = VID_PLL_DIV_5
886 meson_vclk_set(priv, vclk_freq * 10, 0, 0, 0,
887 VID_PLL_DIV_5, 2, 1, 1, false, false);
891 hdmi_tx_div = vclk_freq / dac_freq;
893 if (hdmi_tx_div == 0) {
894 pr_err("Fatal Error, invalid HDMI-TX freq %d\n",
899 venc_div = vclk_freq / venc_freq;
902 pr_err("Fatal Error, invalid HDMI venc freq %d\n",
907 for (freq = 0 ; params[freq].pixel_freq ; ++freq) {
908 if (vclk_freq == params[freq].pixel_freq ||
909 vclk_freq == FREQ_1000_1001(params[freq].pixel_freq)) {
910 if (vclk_freq != params[freq].pixel_freq)
911 vic_alternate_clock = true;
913 vic_alternate_clock = false;
915 if (freq == MESON_VCLK_HDMI_ENCI_54000 &&
919 if (freq == MESON_VCLK_HDMI_DDR_54000 &&
923 if (freq == MESON_VCLK_HDMI_DDR_148500 &&
924 dac_freq == vclk_freq)
927 if (freq == MESON_VCLK_HDMI_148500 &&
928 dac_freq != vclk_freq)
934 if (!params[freq].pixel_freq) {
935 pr_err("Fatal Error, invalid HDMI vclk freq %d\n", vclk_freq);
939 meson_vclk_set(priv, params[freq].pll_base_freq,
940 params[freq].pll_od1, params[freq].pll_od2,
941 params[freq].pll_od3, params[freq].vid_pll_div,
942 params[freq].vclk_div, hdmi_tx_div, venc_div,
943 hdmi_use_enci, vic_alternate_clock);
945 EXPORT_SYMBOL_GPL(meson_vclk_setup);