blob: 1a825abfca7bc36eeb5d2ed06318c628dc52bf04 [file] [log] [blame]
Yakir Yang68f42be2015-04-29 10:08:12 -05001/*
2 * Copyright (C) Rockchip, Inc.
3 * Copyright (C) Freescale Semiconductor, Inc.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * Designware High-Definition Multimedia Interface (HDMI) driveG
11 */
12
13#include <arch/io.h>
14#include <assert.h>
15#include <console/console.h>
16#include <delay.h>
17#include <edid.h>
Douglas Anderson7760a472015-10-27 16:05:15 -070018#include <gpio.h>
Yakir Yang68f42be2015-04-29 10:08:12 -050019#include <stdlib.h>
20#include <stdint.h>
21#include <string.h>
22#include <soc/addressmap.h>
23#include <soc/hdmi.h>
24#include <soc/grf.h>
25#include <soc/vop.h>
26#include <timer.h>
27
28#include "chip.h"
29
30#define AUDIO_SAMPLERATE_DEFAULT (48*KHz)
31
32#define hdmi_debug(x...) do { if (0) printk(BIOS_DEBUG, x); } while (0)
33
34struct rk3288_hdmi_regs * const hdmi_regs = (void *)HDMI_TX_BASE;
35
36struct tmds_n_cts {
37 u32 tmds;
38 u32 cts;
39 u32 n;
40};
41
42static const struct tmds_n_cts n_cts_table[] = {
43 {
44 .tmds = 25175, .n = 6144, .cts = 25175,
45 }, {
46 .tmds = 25200, .n = 6144, .cts = 25200,
47 }, {
48 .tmds = 27000, .n = 6144, .cts = 27000,
49 }, {
50 .tmds = 27027, .n = 6144, .cts = 27027,
51 }, {
52 .tmds = 40000, .n = 6144, .cts = 40000,
53 }, {
54 .tmds = 54000, .n = 6144, .cts = 54000,
55 }, {
56 .tmds = 54054, .n = 6144, .cts = 54054,
57 }, {
58 .tmds = 65000, .n = 6144, .cts = 65000,
59 }, {
60 .tmds = 74176, .n = 11648, .cts = 140625,
61 }, {
62 .tmds = 74250, .n = 6144, .cts = 74250,
63 }, {
64 .tmds = 83500, .n = 6144, .cts = 83500,
65 }, {
66 .tmds = 106500, .n = 6144, .cts = 106500,
67 }, {
68 .tmds = 108000, .n = 6144, .cts = 108000,
69 }, {
70 .tmds = 148352, .n = 5824, .cts = 140625,
71 }, {
72 .tmds = 148500, .n = 6144, .cts = 148500,
73 }, {
74 .tmds = 297000, .n = 5120, .cts = 247500,
75 }
76};
77
78struct hdmi_mpll_config {
79 u64 mpixelclock;
80 /* Mode of Operation and PLL Dividers Control Register */
81 u32 cpce;
82 /* PLL Gmp Control Register */
83 u32 gmp;
84 /* PLL Current COntrol Register */
85 u32 curr;
86};
87
88struct hdmi_phy_config {
89 u64 mpixelclock;
90 u32 sym_ctr; /* clock symbol and transmitter control */
91 u32 term; /* transmission termination value */
92 u32 vlev_ctr; /* voltage level control */
93};
94
95static const struct hdmi_phy_config rockchip_phy_config[] = {
96 {
97 .mpixelclock = 74250,
98 .sym_ctr = 0x8009, .term = 0x0004, .vlev_ctr = 0x0272,
99 }, {
100 .mpixelclock = 148500,
101 .sym_ctr = 0x802b, .term = 0x0004, .vlev_ctr = 0x028d,
102 }, {
103 .mpixelclock = 297000,
104 .sym_ctr = 0x8039, .term = 0x0005, .vlev_ctr = 0x028d,
105 }, {
106 .mpixelclock = ~0ul,
107 .sym_ctr = 0x0000, .term = 0x0000, .vlev_ctr = 0x0000,
108 }
109};
110
111static const struct hdmi_mpll_config rockchip_mpll_cfg[] = {
112 {
113 .mpixelclock = 40000,
114 .cpce = 0x00b3, .gmp = 0x0000, .curr = 0x0018,
115 }, {
116 .mpixelclock = 65000,
117 .cpce = 0x0072, .gmp = 0x0001, .curr = 0x0028,
118 }, {
119 .mpixelclock = 66000,
120 .cpce = 0x013e, .gmp = 0x0003, .curr = 0x0038,
121 }, {
122 .mpixelclock = 83500,
123 .cpce = 0x0072, .gmp = 0x0001, .curr = 0x0028,
124 }, {
125 .mpixelclock = 146250,
126 .cpce = 0x0051, .gmp = 0x0002, .curr = 0x0038,
127 }, {
128 .mpixelclock = 148500,
129 .cpce = 0x0051, .gmp = 0x0003, .curr = 0x0000,
130 }, {
131 .mpixelclock = ~0ul,
132 .cpce = 0x0051, .gmp = 0x0003, .curr = 0x0000,
133 }
134};
135
136static const u32 csc_coeff_default[3][4] = {
137 { 0x2000, 0x0000, 0x0000, 0x0000 },
138 { 0x0000, 0x2000, 0x0000, 0x0000 },
139 { 0x0000, 0x0000, 0x2000, 0x0000 }
140};
141
142static void hdmi_set_clock_regenerator(u32 n, u32 cts)
143{
144 u8 cts3;
145 u8 n3;
146
147 /* first set ncts_atomic_write (if present) */
148 n3 = HDMI_AUD_N3_NCTS_ATOMIC_WRITE;
149 write32(&hdmi_regs->aud_n3, n3);
150
151 /* set cts_manual (if present) */
152 cts3 = HDMI_AUD_CTS3_CTS_MANUAL;
153
154 cts3 |= HDMI_AUD_CTS3_N_SHIFT_1 << HDMI_AUD_CTS3_N_SHIFT_OFFSET;
155 cts3 |= (cts >> 16) & HDMI_AUD_CTS3_AUDCTS19_16_MASK;
156
157 /* write cts values; cts3 must be written first */
158 write32(&hdmi_regs->aud_cts3, cts3);
159 write32(&hdmi_regs->aud_cts2, (cts >> 8) & 0xff);
160 write32(&hdmi_regs->aud_cts1, cts & 0xff);
161
162 /* write n values; n1 must be written last */
163 n3 |= (n >> 16) & HDMI_AUD_N3_AUDN19_16_MASK;
164 write32(&hdmi_regs->aud_n3, n3);
165 write32(&hdmi_regs->aud_n2, (n >> 8) & 0xff);
166 write32(&hdmi_regs->aud_n1, n & 0xff);
167
168 write32(&hdmi_regs->aud_inputclkfs, HDMI_AUD_INPUTCLKFS_128);
169}
170
171static int hdmi_lookup_n_cts(u32 pixel_clk)
172{
173 int i;
174
175 for (i = 0; i < ARRAY_SIZE(n_cts_table); i++)
176 if (pixel_clk <= n_cts_table[i].tmds)
177 break;
178
179 if (i >= ARRAY_SIZE(n_cts_table))
180 return -1;
181
182 return i;
183}
184
185static void hdmi_audio_set_samplerate(u32 pixel_clk)
186{
187 u32 clk_n, clk_cts;
188 int index;
189
190 index = hdmi_lookup_n_cts(pixel_clk);
191 if (index == -1) {
192 hdmi_debug("audio not supported for pixel clk %d\n", pixel_clk);
193 return;
194 }
195
196 clk_n = n_cts_table[index].n;
197 clk_cts = n_cts_table[index].cts;
198 hdmi_set_clock_regenerator(clk_n, clk_cts);
199}
200
201/*
202 * this submodule is responsible for the video data synchronization.
203 * for example, for rgb 4:4:4 input, the data map is defined as
204 * pin{47~40} <==> r[7:0]
205 * pin{31~24} <==> g[7:0]
206 * pin{15~8} <==> b[7:0]
207 */
208static void hdmi_video_sample(void)
209{
210 u32 color_format = 0x01;
211 u8 val;
212
213 val = HDMI_TX_INVID0_INTERNAL_DE_GENERATOR_DISABLE |
214 ((color_format << HDMI_TX_INVID0_VIDEO_MAPPING_OFFSET) &
215 HDMI_TX_INVID0_VIDEO_MAPPING_MASK);
216
217 write32(&hdmi_regs->tx_invid0, val);
218
219 /* enable tx stuffing: when de is inactive, fix the output data to 0 */
220 val = HDMI_TX_INSTUFFING_BDBDATA_STUFFING_ENABLE |
221 HDMI_TX_INSTUFFING_RCRDATA_STUFFING_ENABLE |
222 HDMI_TX_INSTUFFING_GYDATA_STUFFING_ENABLE;
223 write32(&hdmi_regs->tx_instuffing, val);
224 write32(&hdmi_regs->tx_gydata0, 0x0);
225 write32(&hdmi_regs->tx_gydata1, 0x0);
226 write32(&hdmi_regs->tx_rcrdata0, 0x0);
227 write32(&hdmi_regs->tx_rcrdata1, 0x0);
228 write32(&hdmi_regs->tx_bcbdata0, 0x0);
229 write32(&hdmi_regs->tx_bcbdata1, 0x0);
230}
231
232static void hdmi_update_csc_coeffs(void)
233{
234 u32 i, j;
235 u32 csc_scale = 1;
236
237 /* the csc registers are sequential, alternating msb then lsb */
238 for (i = 0; i < ARRAY_SIZE(csc_coeff_default); i++) {
239 for (j = 0; j < ARRAY_SIZE(csc_coeff_default[0]); j++) {
240 u32 coeff = csc_coeff_default[i][j];
241 write32(&hdmi_regs->csc_coef[i][j].msb, coeff >> 8);
Stefan Reinauera3f1a2e2015-07-08 23:31:46 +0200242 write32(&hdmi_regs->csc_coef[i][j].lsb, coeff & 0xff);
Yakir Yang68f42be2015-04-29 10:08:12 -0500243 }
244 }
245
246 clrsetbits_le32(&hdmi_regs->csc_scale, HDMI_CSC_SCALE_CSCSCALE_MASK,
247 csc_scale);
248}
249
250static void hdmi_video_csc(void)
251{
252 u32 color_depth = HDMI_CSC_SCALE_CSC_COLORDE_PTH_24BPP;
253 u32 interpolation = HDMI_CSC_CFG_INTMODE_DISABLE;
254
255 /* configure the csc registers */
256 write32(&hdmi_regs->csc_cfg, interpolation);
257 clrsetbits_le32(&hdmi_regs->csc_scale,
258 HDMI_CSC_SCALE_CSC_COLORDE_PTH_MASK, color_depth);
259
260 hdmi_update_csc_coeffs();
261}
262
263static void hdmi_video_packetize(void)
264{
265 u32 output_select = HDMI_VP_CONF_OUTPUT_SELECTOR_BYPASS;
266 u32 remap_size = HDMI_VP_REMAP_YCC422_16BIT;
267 u32 color_depth = 0;
268 u8 val, vp_conf;
269
270 /* set the packetizer registers */
271 val = ((color_depth << HDMI_VP_PR_CD_COLOR_DEPTH_OFFSET) &
272 HDMI_VP_PR_CD_COLOR_DEPTH_MASK) |
273 ((0 << HDMI_VP_PR_CD_DESIRED_PR_FACTOR_OFFSET) &
274 HDMI_VP_PR_CD_DESIRED_PR_FACTOR_MASK);
275 write32(&hdmi_regs->vp_pr_cd, val);
276
277 clrsetbits_le32(&hdmi_regs->vp_stuff, HDMI_VP_STUFF_PR_STUFFING_MASK,
278 HDMI_VP_STUFF_PR_STUFFING_STUFFING_MODE);
279
280 /* data from pixel repeater block */
281 vp_conf = HDMI_VP_CONF_PR_EN_DISABLE |
282 HDMI_VP_CONF_BYPASS_SELECT_VID_PACKETIZER;
283
284 clrsetbits_le32(&hdmi_regs->vp_conf, HDMI_VP_CONF_PR_EN_MASK |
285 HDMI_VP_CONF_BYPASS_SELECT_MASK, vp_conf);
286
287 clrsetbits_le32(&hdmi_regs->vp_stuff, HDMI_VP_STUFF_IDEFAULT_PHASE_MASK,
288 1 << HDMI_VP_STUFF_IDEFAULT_PHASE_OFFSET);
289
290 write32(&hdmi_regs->vp_remap, remap_size);
291
292 vp_conf = HDMI_VP_CONF_BYPASS_EN_ENABLE |
293 HDMI_VP_CONF_PP_EN_DISABLE |
294 HDMI_VP_CONF_YCC422_EN_DISABLE;
295
296 clrsetbits_le32(&hdmi_regs->vp_conf, HDMI_VP_CONF_BYPASS_EN_MASK |
297 HDMI_VP_CONF_PP_EN_ENMASK | HDMI_VP_CONF_YCC422_EN_MASK,
298 vp_conf);
299
300 clrsetbits_le32(&hdmi_regs->vp_stuff, HDMI_VP_STUFF_PP_STUFFING_MASK |
301 HDMI_VP_STUFF_YCC422_STUFFING_MASK,
302 HDMI_VP_STUFF_PP_STUFFING_STUFFING_MODE |
303 HDMI_VP_STUFF_YCC422_STUFFING_STUFFING_MODE);
304
305 clrsetbits_le32(&hdmi_regs->vp_conf, HDMI_VP_CONF_OUTPUT_SELECTOR_MASK,
306 output_select);
307}
308
309static inline void hdmi_phy_test_clear(u8 bit)
310{
311 clrsetbits_le32(&hdmi_regs->phy_tst0, HDMI_PHY_TST0_TSTCLR_MASK,
312 bit << HDMI_PHY_TST0_TSTCLR_OFFSET);
313}
314
315static int hdmi_phy_wait_i2c_done(u32 msec)
316{
317 struct stopwatch phyi2c_done;
318 u32 val;
319
320 stopwatch_init_msecs_expire(&phyi2c_done, msec);
321 do {
322 val = read32(&hdmi_regs->ih_i2cmphy_stat0);
323 if (val & 0x3) {
324 write32(&hdmi_regs->ih_i2cmphy_stat0, val);
325 return 0;
326 }
327
328 udelay(100);
329 } while (!stopwatch_expired(&phyi2c_done));
330
331 return 1;
332}
333
334static void hdmi_phy_i2c_write(u16 data, u8 addr)
335{
336 write32(&hdmi_regs->ih_i2cmphy_stat0, 0xff);
337 write32(&hdmi_regs->phy_i2cm_address_addr, addr);
338 write32(&hdmi_regs->phy_i2cm_datao_1_addr, (u8)(data >> 8));
339 write32(&hdmi_regs->phy_i2cm_datao_0_addr, (u8)(data >> 0));
340 write32(&hdmi_regs->phy_i2cm_operation_addr,
341 HDMI_PHY_I2CM_OPERATION_ADDR_WRITE);
342
343 hdmi_phy_wait_i2c_done(1000);
344}
345
346static void hdmi_phy_enable_power(u8 enable)
347{
348 clrsetbits_le32(&hdmi_regs->phy_conf0, HDMI_PHY_CONF0_PDZ_MASK,
349 enable << HDMI_PHY_CONF0_PDZ_OFFSET);
350}
351
352static void hdmi_phy_enable_tmds(u8 enable)
353{
354 clrsetbits_le32(&hdmi_regs->phy_conf0, HDMI_PHY_CONF0_ENTMDS_MASK,
355 enable << HDMI_PHY_CONF0_ENTMDS_OFFSET);
356}
357
358static void hdmi_phy_enable_spare(u8 enable)
359{
360 clrsetbits_le32(&hdmi_regs->phy_conf0, HDMI_PHY_CONF0_SPARECTRL_MASK,
361 enable << HDMI_PHY_CONF0_SPARECTRL_OFFSET);
362}
363
364static void hdmi_phy_gen2_pddq(u8 enable)
365{
366 clrsetbits_le32(&hdmi_regs->phy_conf0, HDMI_PHY_CONF0_GEN2_PDDQ_MASK,
367 enable << HDMI_PHY_CONF0_GEN2_PDDQ_OFFSET);
368}
369
370static void hdmi_phy_gen2_txpwron(u8 enable)
371{
372 clrsetbits_le32(&hdmi_regs->phy_conf0,
373 HDMI_PHY_CONF0_GEN2_TXPWRON_MASK,
374 enable << HDMI_PHY_CONF0_GEN2_TXPWRON_OFFSET);
375}
376
377static void hdmi_phy_sel_data_en_pol(u8 enable)
378{
379 clrsetbits_le32(&hdmi_regs->phy_conf0,
380 HDMI_PHY_CONF0_SELDATAENPOL_MASK,
381 enable << HDMI_PHY_CONF0_SELDATAENPOL_OFFSET);
382}
383
384static void hdmi_phy_sel_interface_control(u8 enable)
385{
386 clrsetbits_le32(&hdmi_regs->phy_conf0, HDMI_PHY_CONF0_SELDIPIF_MASK,
387 enable << HDMI_PHY_CONF0_SELDIPIF_OFFSET);
388}
389
390static int hdmi_phy_configure(u32 mpixelclock)
391{
392 struct stopwatch pll_ready;
393 u8 i, val;
394
395 write32(&hdmi_regs->mc_flowctrl,
396 HDMI_MC_FLOWCTRL_FEED_THROUGH_OFF_CSC_BYPASS);
397
398 /* gen2 tx power off */
399 hdmi_phy_gen2_txpwron(0);
400
401 /* gen2 pddq */
402 hdmi_phy_gen2_pddq(1);
403
404 /* phy reset */
405 write32(&hdmi_regs->mc_phyrstz, HDMI_MC_PHYRSTZ_DEASSERT);
406 write32(&hdmi_regs->mc_phyrstz, HDMI_MC_PHYRSTZ_ASSERT);
407 write32(&hdmi_regs->mc_heacphy_rst, HDMI_MC_HEACPHY_RST_ASSERT);
408
409 hdmi_phy_test_clear(1);
410 write32(&hdmi_regs->phy_i2cm_slave_addr,
411 HDMI_PHY_I2CM_SLAVE_ADDR_PHY_GEN2);
412 hdmi_phy_test_clear(0);
413
414 /* pll/mpll cfg - always match on final entry */
415 for (i = 0; rockchip_mpll_cfg[i].mpixelclock != (~0ul); i++)
416 if (mpixelclock <= rockchip_mpll_cfg[i].mpixelclock)
417 break;
418
419 hdmi_phy_i2c_write(rockchip_mpll_cfg[i].cpce, PHY_OPMODE_PLLCFG);
420 hdmi_phy_i2c_write(rockchip_mpll_cfg[i].gmp, PHY_PLLGMPCTRL);
421 hdmi_phy_i2c_write(rockchip_mpll_cfg[i].curr, PHY_PLLCURRCTRL);
422
423 hdmi_phy_i2c_write(0x0000, PHY_PLLPHBYCTRL);
424 hdmi_phy_i2c_write(0x0006, PHY_PLLCLKBISTPHASE);
425
426 for (i = 0; rockchip_phy_config[i].mpixelclock != (~0ul); i++)
427 if (mpixelclock <= rockchip_phy_config[i].mpixelclock)
428 break;
429
430 /*
431 * resistance term 133ohm cfg
432 * preemp cgf 0.00
433 * tx/ck lvl 10
434 */
435 hdmi_phy_i2c_write(rockchip_phy_config[i].term, PHY_TXTERM);
436 hdmi_phy_i2c_write(rockchip_phy_config[i].sym_ctr, PHY_CKSYMTXCTRL);
437 hdmi_phy_i2c_write(rockchip_phy_config[i].vlev_ctr, PHY_VLEVCTRL);
438
439 /* remove clk term */
440 hdmi_phy_i2c_write(0x8000, PHY_CKCALCTRL);
441
442 hdmi_phy_enable_power(1);
443
444 /* toggle tmds enable */
445 hdmi_phy_enable_tmds(0);
446 hdmi_phy_enable_tmds(1);
447
448 /* gen2 tx power on */
449 hdmi_phy_gen2_txpwron(1);
450 hdmi_phy_gen2_pddq(0);
451
452 hdmi_phy_enable_spare(1);
453
454 /* wait for phy pll lock */
455 stopwatch_init_msecs_expire(&pll_ready, 5);
456 do {
457 val = read32(&hdmi_regs->phy_stat0);
458 if (!(val & HDMI_PHY_TX_PHY_LOCK))
459 return 0;
460
461 udelay(100);
462 } while (!stopwatch_expired(&pll_ready));
463
464 return -1;
465}
466
467static int hdmi_phy_init(u32 mpixelclock)
468{
469 int i, ret;
470
471 /* hdmi phy spec says to do the phy initialization sequence twice */
472 for (i = 0; i < 2; i++) {
473 hdmi_phy_sel_data_en_pol(1);
474 hdmi_phy_sel_interface_control(0);
475 hdmi_phy_enable_tmds(0);
476 hdmi_phy_enable_power(0);
477
478 /* enable csc */
479 ret = hdmi_phy_configure(mpixelclock);
480 if (ret) {
481 hdmi_debug("hdmi phy config failure %d\n", ret);
482 return ret;
483 }
484 }
485
486 return 0;
487}
488
489static void hdmi_av_composer(const struct edid *edid)
490{
491 u8 mdataenablepolarity = 1;
Yakir Yang68f42be2015-04-29 10:08:12 -0500492 u8 inv_val;
493
494 /* set up hdmi_fc_invidconf */
495 inv_val = HDMI_FC_INVIDCONF_HDCP_KEEPOUT_INACTIVE;
496
David Hendricks7dbf9c62015-07-30 18:49:48 -0700497 inv_val |= ((edid->mode.pvsync == '+') ?
Yakir Yang68f42be2015-04-29 10:08:12 -0500498 HDMI_FC_INVIDCONF_VSYNC_IN_POLARITY_ACTIVE_HIGH :
499 HDMI_FC_INVIDCONF_VSYNC_IN_POLARITY_ACTIVE_LOW);
500
David Hendricks7dbf9c62015-07-30 18:49:48 -0700501 inv_val |= ((edid->mode.phsync == '+') ?
Yakir Yang68f42be2015-04-29 10:08:12 -0500502 HDMI_FC_INVIDCONF_HSYNC_IN_POLARITY_ACTIVE_HIGH :
503 HDMI_FC_INVIDCONF_HSYNC_IN_POLARITY_ACTIVE_LOW);
504
505 inv_val |= (mdataenablepolarity ?
506 HDMI_FC_INVIDCONF_DE_IN_POLARITY_ACTIVE_HIGH :
507 HDMI_FC_INVIDCONF_DE_IN_POLARITY_ACTIVE_LOW);
508
Yakir Yange78a1be2015-10-27 16:40:08 +0800509 inv_val |= (edid->hdmi_monitor_detected ?
510 HDMI_FC_INVIDCONF_DVI_MODEZ_HDMI_MODE :
511 HDMI_FC_INVIDCONF_DVI_MODEZ_DVI_MODE);
Yakir Yang68f42be2015-04-29 10:08:12 -0500512
513 inv_val |= HDMI_FC_INVIDCONF_R_V_BLANK_IN_OSC_ACTIVE_LOW;
514
515 inv_val |= HDMI_FC_INVIDCONF_IN_I_P_PROGRESSIVE;
516
517 write32(&hdmi_regs->fc_invidconf, inv_val);
518
519 /* set up horizontal active pixel width */
David Hendricks7dbf9c62015-07-30 18:49:48 -0700520 write32(&hdmi_regs->fc_inhactv1, edid->mode.ha >> 8);
521 write32(&hdmi_regs->fc_inhactv0, edid->mode.ha);
Yakir Yang68f42be2015-04-29 10:08:12 -0500522
523 /* set up vertical active lines */
David Hendricks7dbf9c62015-07-30 18:49:48 -0700524 write32(&hdmi_regs->fc_invactv1, edid->mode.va >> 8);
525 write32(&hdmi_regs->fc_invactv0, edid->mode.va);
Yakir Yang68f42be2015-04-29 10:08:12 -0500526
527 /* set up horizontal blanking pixel region width */
David Hendricks7dbf9c62015-07-30 18:49:48 -0700528 write32(&hdmi_regs->fc_inhblank1, edid->mode.hbl >> 8);
529 write32(&hdmi_regs->fc_inhblank0, edid->mode.hbl);
Yakir Yang68f42be2015-04-29 10:08:12 -0500530
531 /* set up vertical blanking pixel region width */
David Hendricks7dbf9c62015-07-30 18:49:48 -0700532 write32(&hdmi_regs->fc_invblank, edid->mode.vbl);
Yakir Yang68f42be2015-04-29 10:08:12 -0500533
534 /* set up hsync active edge delay width (in pixel clks) */
David Hendricks7dbf9c62015-07-30 18:49:48 -0700535 write32(&hdmi_regs->fc_hsyncindelay1, edid->mode.hso >> 8);
536 write32(&hdmi_regs->fc_hsyncindelay0, edid->mode.hso);
Yakir Yang68f42be2015-04-29 10:08:12 -0500537
538 /* set up vsync active edge delay (in lines) */
David Hendricks7dbf9c62015-07-30 18:49:48 -0700539 write32(&hdmi_regs->fc_vsyncindelay, edid->mode.vso);
Yakir Yang68f42be2015-04-29 10:08:12 -0500540
541 /* set up hsync active pulse width (in pixel clks) */
David Hendricks7dbf9c62015-07-30 18:49:48 -0700542 write32(&hdmi_regs->fc_hsyncinwidth1, edid->mode.hspw >> 8);
543 write32(&hdmi_regs->fc_hsyncinwidth0, edid->mode.hspw);
Yakir Yang68f42be2015-04-29 10:08:12 -0500544
545 /* set up vsync active edge delay (in lines) */
David Hendricks7dbf9c62015-07-30 18:49:48 -0700546 write32(&hdmi_regs->fc_vsyncinwidth, edid->mode.vspw);
Yakir Yang68f42be2015-04-29 10:08:12 -0500547}
548
549/* hdmi initialization step b.4 */
550static void hdmi_enable_video_path(void)
551{
552 u8 clkdis;
553
554 /* control period minimum duration */
555 write32(&hdmi_regs->fc_ctrldur, 12);
556 write32(&hdmi_regs->fc_exctrldur, 32);
557 write32(&hdmi_regs->fc_exctrlspac, 1);
558
559 /* set to fill tmds data channels */
560 write32(&hdmi_regs->fc_ch0pream, 0x0b);
561 write32(&hdmi_regs->fc_ch1pream, 0x16);
562 write32(&hdmi_regs->fc_ch2pream, 0x21);
563
564 /* enable pixel clock and tmds data path */
565 clkdis = 0x7f;
566 clkdis &= ~HDMI_MC_CLKDIS_PIXELCLK_DISABLE;
567 write32(&hdmi_regs->mc_clkdis, clkdis);
568
569 clkdis &= ~HDMI_MC_CLKDIS_TMDSCLK_DISABLE;
570 write32(&hdmi_regs->mc_clkdis, clkdis);
571
572 clkdis &= ~HDMI_MC_CLKDIS_AUDCLK_DISABLE;
573 write32(&hdmi_regs->mc_clkdis, clkdis);
574}
575
576/* workaround to clear the overflow condition */
577static void hdmi_clear_overflow(void)
578{
579 u8 val, count;
580
581 /* tmds software reset */
582 write32(&hdmi_regs->mc_swrstz, (u8)~HDMI_MC_SWRSTZ_TMDSSWRST_REQ);
583
584 val = read32(&hdmi_regs->fc_invidconf);
585
586 for (count = 0; count < 4; count++)
587 write32(&hdmi_regs->fc_invidconf, val);
588}
589
590static void hdmi_audio_set_format(void)
591{
592 write32(&hdmi_regs->aud_conf0,
593 HDMI_AUD_CONF0_I2S_SELECT | HDMI_AUD_CONF0_I2S_IN_EN_0);
594
595 write32(&hdmi_regs->aud_conf1,
596 HDMI_AUD_CONF1_I2S_MODE_STANDARD_MODE |
597 HDMI_AUD_CONF1_I2S_WIDTH_16BIT);
598
599 write32(&hdmi_regs->aud_conf2, 0x00);
600}
601
602static void hdmi_audio_fifo_reset(void)
603{
604 write32(&hdmi_regs->mc_swrstz, (u8)~HDMI_MC_SWRSTZ_II2SSWRST_REQ);
605 write32(&hdmi_regs->aud_conf0, HDMI_AUD_CONF0_SW_AUDIO_FIFO_RST);
606
607 write32(&hdmi_regs->aud_int, 0x00);
608 write32(&hdmi_regs->aud_int1, 0x00);
609}
610
611static int hdmi_setup(const struct edid *edid)
612{
613 int ret;
614
615 hdmi_debug("hdmi, mode info : clock %d hdis %d vdis %d\n",
David Hendricks7dbf9c62015-07-30 18:49:48 -0700616 edid->mode.pixel_clock, edid->mode.ha, edid->mode.va);
Yakir Yang68f42be2015-04-29 10:08:12 -0500617
618 hdmi_av_composer(edid);
619
David Hendricks7dbf9c62015-07-30 18:49:48 -0700620 ret = hdmi_phy_init(edid->mode.pixel_clock);
Yakir Yang68f42be2015-04-29 10:08:12 -0500621 if (ret)
622 return ret;
623
624 hdmi_enable_video_path();
625
626 hdmi_audio_fifo_reset();
627 hdmi_audio_set_format();
David Hendricks7dbf9c62015-07-30 18:49:48 -0700628 hdmi_audio_set_samplerate(edid->mode.pixel_clock);
Yakir Yang68f42be2015-04-29 10:08:12 -0500629
630 hdmi_video_packetize();
631 hdmi_video_csc();
632 hdmi_video_sample();
633
634 hdmi_clear_overflow();
635
636 return 0;
637}
638
639static void hdmi_init_interrupt(void)
640{
641 u8 ih_mute;
642
643 /*
644 * boot up defaults are:
645 * hdmi_ih_mute = 0x03 (disabled)
646 * hdmi_ih_mute_* = 0x00 (enabled)
647 *
648 * disable top level interrupt bits in hdmi block
649 */
650 ih_mute = read32(&hdmi_regs->ih_mute) |
651 HDMI_IH_MUTE_MUTE_WAKEUP_INTERRUPT |
652 HDMI_IH_MUTE_MUTE_ALL_INTERRUPT;
653
654 write32(&hdmi_regs->ih_mute, ih_mute);
655
656 /* enable i2c master done irq */
657 write32(&hdmi_regs->i2cm_int, ~0x04);
658
659 /* enable i2c client nack % arbitration error irq */
660 write32(&hdmi_regs->i2cm_ctlint, ~0x44);
661
662 /* enable phy i2cm done irq */
663 write32(&hdmi_regs->phy_i2cm_int_addr, HDMI_PHY_I2CM_INT_ADDR_DONE_POL);
664
665 /* enable phy i2cm nack & arbitration error irq */
666 write32(&hdmi_regs->phy_i2cm_ctlint_addr,
667 HDMI_PHY_I2CM_CTLINT_ADDR_NAC_POL |
668 HDMI_PHY_I2CM_CTLINT_ADDR_ARBITRATION_POL);
669
670 /* enable cable hot plug irq */
671 write32(&hdmi_regs->phy_mask0, (u8)~HDMI_PHY_HPD);
672
673 /* clear hotplug interrupts */
674 write32(&hdmi_regs->ih_phy_stat0, HDMI_IH_PHY_STAT0_HPD);
675}
676
677static u8 hdmi_get_plug_in_status(void)
678{
679 u8 val = read32(&hdmi_regs->phy_stat0) & HDMI_PHY_HPD;
680
681 return !!(val);
682}
683
684static int hdmi_wait_for_hpd(void)
685{
686 struct stopwatch hpd;
687
688 stopwatch_init_msecs_expire(&hpd, 30000);
689 do {
690 if (hdmi_get_plug_in_status())
691 return 0;
692 udelay(100);
693 } while (!stopwatch_expired(&hpd));
694
695 return -1;
696}
697
698static int hdmi_ddc_wait_i2c_done(int msec)
699{
700 struct stopwatch ddci2c_done;
701 u32 val;
702
703 stopwatch_init_msecs_expire(&ddci2c_done, msec);
704 do {
705 val = read32(&hdmi_regs->ih_i2cm_stat0);
706 if (val & 0x2) {
707 write32(&hdmi_regs->ih_i2cm_stat0, val);
708 return 0;
709 }
710
711 udelay(100);
712 } while (!stopwatch_expired(&ddci2c_done));
713
714 return 1;
715}
716
717static void hdmi_ddc_reset(void)
718{
719 clrsetbits_le32(&hdmi_regs->i2cm_softrstz, HDMI_I2CM_SOFTRSTZ,
720 HDMI_I2CM_SOFTRSTZ);
721}
722
723static int hdmi_read_edid(int block, u8 *buff)
724{
725 int shift = (block % 2) * 0x80;
726 int edid_read_err = 0;
727 u32 trytime = 5;
728 u32 n, j, val;
729
730 /* set ddc i2c clk which devided from ddc_clk to 100khz */
731 write32(&hdmi_regs->i2cm_ss_scl_hcnt_0_addr, 0x7a);
732 write32(&hdmi_regs->i2cm_ss_scl_lcnt_0_addr, 0x8d);
733 clrsetbits_le32(&hdmi_regs->i2cm_div, HDMI_I2CM_DIV_FAST_STD_MODE,
734 HDMI_I2CM_DIV_STD_MODE);
735
736 write32(&hdmi_regs->i2cm_slave, HDMI_I2CM_SLAVE_DDC_ADDR);
737 write32(&hdmi_regs->i2cm_segaddr, HDMI_I2CM_SEGADDR_DDC);
738 write32(&hdmi_regs->i2cm_segptr, block >> 1);
739
740 while (trytime--) {
Douglas Anderson6db10452015-10-27 16:27:29 -0700741 edid_read_err = 0;
742
Yakir Yang68f42be2015-04-29 10:08:12 -0500743 for (n = 0; n < HDMI_EDID_BLOCK_SIZE/8; n++) {
744 write32(&hdmi_regs->i2cmess, shift + 8 * n);
745
746 if (block == 0)
747 clrsetbits_le32(&hdmi_regs->i2cm_operation,
748 HDMI_I2CM_OPT_RD8,
749 HDMI_I2CM_OPT_RD8);
750 else
751 clrsetbits_le32(&hdmi_regs->i2cm_operation,
752 HDMI_I2CM_OPT_RD8_EXT,
753 HDMI_I2CM_OPT_RD8_EXT);
754
755 if (hdmi_ddc_wait_i2c_done(10)) {
756 hdmi_ddc_reset();
757 edid_read_err = 1;
758 break;
759 }
760
761 for (j = 0; j < 8; j++) {
762 val = read32(&hdmi_regs->i2cm_buf0 + j);
763 buff[8 * n + j] = val;
764 }
765 }
766
767 if (!edid_read_err)
768 break;
Yakir Yang68f42be2015-04-29 10:08:12 -0500769 }
770
771 return edid_read_err;
772}
773
774int rk_hdmi_get_edid(struct edid *edid)
775{
776 u8 edid_buf[HDMI_EDID_BLOCK_SIZE * 2];
777 u32 edid_size = HDMI_EDID_BLOCK_SIZE;
Douglas Anderson7760a472015-10-27 16:05:15 -0700778 gpio_t hdmi_i2c_sda = GPIO(7, C, 3);
779 gpio_t hdmi_i2c_scl = GPIO(7, C, 4);
Yakir Yang68f42be2015-04-29 10:08:12 -0500780 int ret;
781
Douglas Anderson7760a472015-10-27 16:05:15 -0700782 /* If SDA is low, try to clock once to fix it */
783 gpio_input_pullup(hdmi_i2c_sda);
784 if (gpio_get(hdmi_i2c_sda) == 0) {
785 gpio_output(hdmi_i2c_scl, 0);
786 udelay(1000);
787 gpio_input_pullup(hdmi_i2c_scl);
788 udelay(1000);
789 }
790
791 /* HDMI I2C */
792 write32(&rk3288_grf->iomux_i2c5sda, IOMUX_HDMI_EDP_I2C_SDA);
793 write32(&rk3288_grf->iomux_i2c5scl, IOMUX_HDMI_EDP_I2C_SCL);
794
Yakir Yang68f42be2015-04-29 10:08:12 -0500795 ret = hdmi_read_edid(0, edid_buf);
796 if (ret) {
797 hdmi_debug("failed to read edid.\n");
798 return -1;
799 }
800
801 if (edid_buf[0x7e] != 0) {
802 hdmi_read_edid(1, edid_buf + HDMI_EDID_BLOCK_SIZE);
803 edid_size += HDMI_EDID_BLOCK_SIZE;
804 }
805
David Hendricks04002a92015-08-13 15:43:55 -0700806 /* Assume usage of HDMI implies an external display in which case
807 * we should be lenient about errors that the EDID decoder finds. */
808 if (decode_edid(edid_buf, edid_size, edid))
Yakir Yang68f42be2015-04-29 10:08:12 -0500809 hdmi_debug("failed to decode edid.\n");
Yakir Yang68f42be2015-04-29 10:08:12 -0500810
Yakir Yang9a640592015-07-29 08:54:14 -0500811 /* Try 480p for best compatibility. */
812 if (set_display_mode(edid, EDID_MODE_640x480_60Hz))
813 hdmi_debug("failed to set mode to 640x480@60Hz\n");
814
Yakir Yang68f42be2015-04-29 10:08:12 -0500815 return 0;
816}
817
818int rk_hdmi_enable(const struct edid *edid)
819{
820 hdmi_setup(edid);
821
822 return 0;
823}
824
825int rk_hdmi_init(u32 vop_id)
826{
827 int ret;
828 u32 val;
829
830 /* hdmi source select hdmi controller */
831 write32(&rk3288_grf->soc_con6, RK_SETBITS(1 << 15));
832
833 /* hdmi data from vop id */
834 val = (vop_id == 1) ? RK_SETBITS(1 << 4) : RK_CLRBITS(1 << 4);
835 write32(&rk3288_grf->soc_con6, val);
836
837 ret = hdmi_wait_for_hpd();
838 if (ret < 0) {
839 hdmi_debug("hdmi can not get hpd signal\n");
840 return -1;
841 }
842
843 hdmi_init_interrupt();
844
845 hdmi_debug("hdmi init success\n");
846
847 return 0;
848}