1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2015 Google, Inc
4 * Copyright 2014 Rockchip Inc.
5 * Copyright 2017 Jernej Skrabec <jernej.skrabec@siol.net>
11 #include <media_bus_format.h>
20 static const struct tmds_n_cts n_cts_table[] = {
22 .tmds = 25175000, .n = 6144, .cts = 25175,
24 .tmds = 25200000, .n = 6144, .cts = 25200,
26 .tmds = 27000000, .n = 6144, .cts = 27000,
28 .tmds = 27027000, .n = 6144, .cts = 27027,
30 .tmds = 40000000, .n = 6144, .cts = 40000,
32 .tmds = 54000000, .n = 6144, .cts = 54000,
34 .tmds = 54054000, .n = 6144, .cts = 54054,
36 .tmds = 65000000, .n = 6144, .cts = 65000,
38 .tmds = 74176000, .n = 11648, .cts = 140625,
40 .tmds = 74250000, .n = 6144, .cts = 74250,
42 .tmds = 83500000, .n = 6144, .cts = 83500,
44 .tmds = 106500000, .n = 6144, .cts = 106500,
46 .tmds = 108000000, .n = 6144, .cts = 108000,
48 .tmds = 148352000, .n = 5824, .cts = 140625,
50 .tmds = 148500000, .n = 6144, .cts = 148500,
52 .tmds = 297000000, .n = 5120, .cts = 247500,
56 static const u16 csc_coeff_default[3][4] = {
57 { 0x2000, 0x0000, 0x0000, 0x0000 },
58 { 0x0000, 0x2000, 0x0000, 0x0000 },
59 { 0x0000, 0x0000, 0x2000, 0x0000 }
62 static const u16 csc_coeff_rgb_in_eitu601[3][4] = {
63 { 0x2591, 0x1322, 0x074b, 0x0000 },
64 { 0x6535, 0x2000, 0x7acc, 0x0200 },
65 { 0x6acd, 0x7534, 0x2000, 0x0200 }
68 static const u16 csc_coeff_rgb_out_eitu601[3][4] = {
69 { 0x2000, 0x6926, 0x74fd, 0x010e },
70 { 0x2000, 0x2cdd, 0x0000, 0x7e9a },
71 { 0x2000, 0x0000, 0x38b4, 0x7e3b }
74 static void dw_hdmi_write(struct dw_hdmi *hdmi, u8 val, int offset)
76 switch (hdmi->reg_io_width) {
78 writeb(val, hdmi->ioaddr + offset);
81 writel(val, hdmi->ioaddr + (offset << 2));
84 debug("reg_io_width has unsupported width!\n");
89 static u8 dw_hdmi_read(struct dw_hdmi *hdmi, int offset)
91 switch (hdmi->reg_io_width) {
93 return readb(hdmi->ioaddr + offset);
95 return readl(hdmi->ioaddr + (offset << 2));
97 debug("reg_io_width has unsupported width!\n");
104 static u8 (*hdmi_read)(struct dw_hdmi *hdmi, int offset) = dw_hdmi_read;
105 static void (*hdmi_write)(struct dw_hdmi *hdmi, u8 val, int offset) =
108 static void hdmi_mod(struct dw_hdmi *hdmi, unsigned reg, u8 mask, u8 data)
110 u8 val = hdmi_read(hdmi, reg) & ~mask;
113 hdmi_write(hdmi, val, reg);
116 static void hdmi_set_clock_regenerator(struct dw_hdmi *hdmi, u32 n, u32 cts)
121 /* first set ncts_atomic_write (if present) */
122 n3 = HDMI_AUD_N3_NCTS_ATOMIC_WRITE;
123 hdmi_write(hdmi, n3, HDMI_AUD_N3);
125 /* set cts_manual (if present) */
126 cts3 = HDMI_AUD_CTS3_CTS_MANUAL;
128 cts3 |= HDMI_AUD_CTS3_N_SHIFT_1 << HDMI_AUD_CTS3_N_SHIFT_OFFSET;
129 cts3 |= (cts >> 16) & HDMI_AUD_CTS3_AUDCTS19_16_MASK;
131 /* write cts values; cts3 must be written first */
132 hdmi_write(hdmi, cts3, HDMI_AUD_CTS3);
133 hdmi_write(hdmi, (cts >> 8) & 0xff, HDMI_AUD_CTS2);
134 hdmi_write(hdmi, cts & 0xff, HDMI_AUD_CTS1);
136 /* write n values; n1 must be written last */
137 n3 |= (n >> 16) & HDMI_AUD_N3_AUDN19_16_MASK;
138 hdmi_write(hdmi, n3, HDMI_AUD_N3);
139 hdmi_write(hdmi, (n >> 8) & 0xff, HDMI_AUD_N2);
140 hdmi_write(hdmi, n & 0xff, HDMI_AUD_N3);
142 hdmi_write(hdmi, HDMI_AUD_INPUTCLKFS_128, HDMI_AUD_INPUTCLKFS);
145 static int hdmi_lookup_n_cts(u32 pixel_clk)
149 for (i = 0; i < ARRAY_SIZE(n_cts_table); i++)
150 if (pixel_clk <= n_cts_table[i].tmds)
153 if (i >= ARRAY_SIZE(n_cts_table))
159 static void hdmi_audio_set_samplerate(struct dw_hdmi *hdmi, u32 pixel_clk)
164 index = hdmi_lookup_n_cts(pixel_clk);
166 debug("audio not supported for pixel clk %d\n", pixel_clk);
170 clk_n = n_cts_table[index].n;
171 clk_cts = n_cts_table[index].cts;
172 hdmi_set_clock_regenerator(hdmi, clk_n, clk_cts);
176 * this submodule is responsible for the video data synchronization.
177 * for example, for rgb 4:4:4 input, the data map is defined as
178 * pin{47~40} <==> r[7:0]
179 * pin{31~24} <==> g[7:0]
180 * pin{15~8} <==> b[7:0]
182 static void hdmi_video_sample(struct dw_hdmi *hdmi)
187 switch (hdmi->hdmi_data.enc_in_bus_format) {
188 case MEDIA_BUS_FMT_RGB888_1X24:
191 case MEDIA_BUS_FMT_RGB101010_1X30:
194 case MEDIA_BUS_FMT_RGB121212_1X36:
197 case MEDIA_BUS_FMT_RGB161616_1X48:
200 case MEDIA_BUS_FMT_YUV8_1X24:
201 case MEDIA_BUS_FMT_UYYVYY8_0_5X24:
204 case MEDIA_BUS_FMT_YUV10_1X30:
205 case MEDIA_BUS_FMT_UYYVYY10_0_5X30:
208 case MEDIA_BUS_FMT_YUV12_1X36:
209 case MEDIA_BUS_FMT_UYYVYY12_0_5X36:
212 case MEDIA_BUS_FMT_YUV16_1X48:
213 case MEDIA_BUS_FMT_UYYVYY16_0_5X48:
216 case MEDIA_BUS_FMT_UYVY8_1X16:
219 case MEDIA_BUS_FMT_UYVY10_1X20:
222 case MEDIA_BUS_FMT_UYVY12_1X24:
230 val = HDMI_TX_INVID0_INTERNAL_DE_GENERATOR_DISABLE |
231 ((color_format << HDMI_TX_INVID0_VIDEO_MAPPING_OFFSET) &
232 HDMI_TX_INVID0_VIDEO_MAPPING_MASK);
234 hdmi_write(hdmi, val, HDMI_TX_INVID0);
236 /* enable tx stuffing: when de is inactive, fix the output data to 0 */
237 val = HDMI_TX_INSTUFFING_BDBDATA_STUFFING_ENABLE |
238 HDMI_TX_INSTUFFING_RCRDATA_STUFFING_ENABLE |
239 HDMI_TX_INSTUFFING_GYDATA_STUFFING_ENABLE;
240 hdmi_write(hdmi, val, HDMI_TX_INSTUFFING);
241 hdmi_write(hdmi, 0x0, HDMI_TX_GYDATA0);
242 hdmi_write(hdmi, 0x0, HDMI_TX_GYDATA1);
243 hdmi_write(hdmi, 0x0, HDMI_TX_RCRDATA0);
244 hdmi_write(hdmi, 0x0, HDMI_TX_RCRDATA1);
245 hdmi_write(hdmi, 0x0, HDMI_TX_BCBDATA0);
246 hdmi_write(hdmi, 0x0, HDMI_TX_BCBDATA1);
249 static void hdmi_video_packetize(struct dw_hdmi *hdmi)
251 u32 output_select = HDMI_VP_CONF_OUTPUT_SELECTOR_BYPASS;
252 u32 remap_size = HDMI_VP_REMAP_YCC422_16BIT;
256 /* set the packetizer registers */
257 val = ((color_depth << HDMI_VP_PR_CD_COLOR_DEPTH_OFFSET) &
258 HDMI_VP_PR_CD_COLOR_DEPTH_MASK) |
259 ((0 << HDMI_VP_PR_CD_DESIRED_PR_FACTOR_OFFSET) &
260 HDMI_VP_PR_CD_DESIRED_PR_FACTOR_MASK);
261 hdmi_write(hdmi, val, HDMI_VP_PR_CD);
263 hdmi_mod(hdmi, HDMI_VP_STUFF, HDMI_VP_STUFF_PR_STUFFING_MASK,
264 HDMI_VP_STUFF_PR_STUFFING_STUFFING_MODE);
266 /* data from pixel repeater block */
267 vp_conf = HDMI_VP_CONF_PR_EN_DISABLE |
268 HDMI_VP_CONF_BYPASS_SELECT_VID_PACKETIZER;
270 hdmi_mod(hdmi, HDMI_VP_CONF, HDMI_VP_CONF_PR_EN_MASK |
271 HDMI_VP_CONF_BYPASS_SELECT_MASK, vp_conf);
273 hdmi_mod(hdmi, HDMI_VP_STUFF, HDMI_VP_STUFF_IDEFAULT_PHASE_MASK,
274 1 << HDMI_VP_STUFF_IDEFAULT_PHASE_OFFSET);
276 hdmi_write(hdmi, remap_size, HDMI_VP_REMAP);
278 vp_conf = HDMI_VP_CONF_BYPASS_EN_ENABLE |
279 HDMI_VP_CONF_PP_EN_DISABLE |
280 HDMI_VP_CONF_YCC422_EN_DISABLE;
282 hdmi_mod(hdmi, HDMI_VP_CONF, HDMI_VP_CONF_BYPASS_EN_MASK |
283 HDMI_VP_CONF_PP_EN_ENMASK | HDMI_VP_CONF_YCC422_EN_MASK,
286 hdmi_mod(hdmi, HDMI_VP_STUFF, HDMI_VP_STUFF_PP_STUFFING_MASK |
287 HDMI_VP_STUFF_YCC422_STUFFING_MASK,
288 HDMI_VP_STUFF_PP_STUFFING_STUFFING_MODE |
289 HDMI_VP_STUFF_YCC422_STUFFING_STUFFING_MODE);
291 hdmi_mod(hdmi, HDMI_VP_CONF, HDMI_VP_CONF_OUTPUT_SELECTOR_MASK,
295 static inline void hdmi_phy_test_clear(struct dw_hdmi *hdmi, uint bit)
297 hdmi_mod(hdmi, HDMI_PHY_TST0, HDMI_PHY_TST0_TSTCLR_MASK,
298 bit << HDMI_PHY_TST0_TSTCLR_OFFSET);
301 static int hdmi_phy_wait_i2c_done(struct dw_hdmi *hdmi, u32 msec)
306 start = get_timer(0);
308 val = hdmi_read(hdmi, HDMI_IH_I2CMPHY_STAT0);
310 hdmi_write(hdmi, val, HDMI_IH_I2CMPHY_STAT0);
315 } while (get_timer(start) < msec);
320 static void hdmi_phy_i2c_write(struct dw_hdmi *hdmi, uint data, uint addr)
322 hdmi_write(hdmi, 0xff, HDMI_IH_I2CMPHY_STAT0);
323 hdmi_write(hdmi, addr, HDMI_PHY_I2CM_ADDRESS_ADDR);
324 hdmi_write(hdmi, (u8)(data >> 8), HDMI_PHY_I2CM_DATAO_1_ADDR);
325 hdmi_write(hdmi, (u8)(data >> 0), HDMI_PHY_I2CM_DATAO_0_ADDR);
326 hdmi_write(hdmi, HDMI_PHY_I2CM_OPERATION_ADDR_WRITE,
327 HDMI_PHY_I2CM_OPERATION_ADDR);
329 hdmi_phy_wait_i2c_done(hdmi, 1000);
332 static void hdmi_phy_enable_power(struct dw_hdmi *hdmi, uint enable)
334 hdmi_mod(hdmi, HDMI_PHY_CONF0, HDMI_PHY_CONF0_PDZ_MASK,
335 enable << HDMI_PHY_CONF0_PDZ_OFFSET);
338 static void hdmi_phy_enable_tmds(struct dw_hdmi *hdmi, uint enable)
340 hdmi_mod(hdmi, HDMI_PHY_CONF0, HDMI_PHY_CONF0_ENTMDS_MASK,
341 enable << HDMI_PHY_CONF0_ENTMDS_OFFSET);
344 static void hdmi_phy_enable_spare(struct dw_hdmi *hdmi, uint enable)
346 hdmi_mod(hdmi, HDMI_PHY_CONF0, HDMI_PHY_CONF0_SPARECTRL_MASK,
347 enable << HDMI_PHY_CONF0_SPARECTRL_OFFSET);
350 static void hdmi_phy_gen2_pddq(struct dw_hdmi *hdmi, uint enable)
352 hdmi_mod(hdmi, HDMI_PHY_CONF0, HDMI_PHY_CONF0_GEN2_PDDQ_MASK,
353 enable << HDMI_PHY_CONF0_GEN2_PDDQ_OFFSET);
356 static void hdmi_phy_gen2_txpwron(struct dw_hdmi *hdmi, uint enable)
358 hdmi_mod(hdmi, HDMI_PHY_CONF0,
359 HDMI_PHY_CONF0_GEN2_TXPWRON_MASK,
360 enable << HDMI_PHY_CONF0_GEN2_TXPWRON_OFFSET);
363 static void hdmi_phy_sel_data_en_pol(struct dw_hdmi *hdmi, uint enable)
365 hdmi_mod(hdmi, HDMI_PHY_CONF0,
366 HDMI_PHY_CONF0_SELDATAENPOL_MASK,
367 enable << HDMI_PHY_CONF0_SELDATAENPOL_OFFSET);
370 static void hdmi_phy_sel_interface_control(struct dw_hdmi *hdmi,
373 hdmi_mod(hdmi, HDMI_PHY_CONF0, HDMI_PHY_CONF0_SELDIPIF_MASK,
374 enable << HDMI_PHY_CONF0_SELDIPIF_OFFSET);
377 static int hdmi_phy_configure(struct dw_hdmi *hdmi, u32 mpixelclock)
382 if (!hdmi->mpll_cfg || !hdmi->phy_cfg)
385 /* gen2 tx power off */
386 hdmi_phy_gen2_txpwron(hdmi, 0);
389 hdmi_phy_gen2_pddq(hdmi, 1);
392 hdmi_write(hdmi, HDMI_MC_PHYRSTZ_DEASSERT, HDMI_MC_PHYRSTZ);
393 hdmi_write(hdmi, HDMI_MC_PHYRSTZ_ASSERT, HDMI_MC_PHYRSTZ);
394 hdmi_write(hdmi, HDMI_MC_HEACPHY_RST_ASSERT, HDMI_MC_HEACPHY_RST);
396 hdmi_phy_test_clear(hdmi, 1);
397 hdmi_write(hdmi, HDMI_PHY_I2CM_SLAVE_ADDR_PHY_GEN2,
398 HDMI_PHY_I2CM_SLAVE_ADDR);
399 hdmi_phy_test_clear(hdmi, 0);
401 /* pll/mpll cfg - always match on final entry */
402 for (i = 0; hdmi->mpll_cfg[i].mpixelclock != (~0ul); i++)
403 if (mpixelclock <= hdmi->mpll_cfg[i].mpixelclock)
406 hdmi_phy_i2c_write(hdmi, hdmi->mpll_cfg[i].cpce, PHY_OPMODE_PLLCFG);
407 hdmi_phy_i2c_write(hdmi, hdmi->mpll_cfg[i].gmp, PHY_PLLGMPCTRL);
408 hdmi_phy_i2c_write(hdmi, hdmi->mpll_cfg[i].curr, PHY_PLLCURRCTRL);
410 hdmi_phy_i2c_write(hdmi, 0x0000, PHY_PLLPHBYCTRL);
411 hdmi_phy_i2c_write(hdmi, 0x0006, PHY_PLLCLKBISTPHASE);
413 for (i = 0; hdmi->phy_cfg[i].mpixelclock != (~0ul); i++)
414 if (mpixelclock <= hdmi->phy_cfg[i].mpixelclock)
418 * resistance term 133ohm cfg
422 hdmi_phy_i2c_write(hdmi, hdmi->phy_cfg[i].term, PHY_TXTERM);
423 hdmi_phy_i2c_write(hdmi, hdmi->phy_cfg[i].sym_ctr, PHY_CKSYMTXCTRL);
424 hdmi_phy_i2c_write(hdmi, hdmi->phy_cfg[i].vlev_ctr, PHY_VLEVCTRL);
426 /* remove clk term */
427 hdmi_phy_i2c_write(hdmi, 0x8000, PHY_CKCALCTRL);
429 hdmi_phy_enable_power(hdmi, 1);
431 /* toggle tmds enable */
432 hdmi_phy_enable_tmds(hdmi, 0);
433 hdmi_phy_enable_tmds(hdmi, 1);
435 /* gen2 tx power on */
436 hdmi_phy_gen2_txpwron(hdmi, 1);
437 hdmi_phy_gen2_pddq(hdmi, 0);
439 hdmi_phy_enable_spare(hdmi, 1);
441 /* wait for phy pll lock */
442 start = get_timer(0);
444 val = hdmi_read(hdmi, HDMI_PHY_STAT0);
445 if (!(val & HDMI_PHY_TX_PHY_LOCK))
449 } while (get_timer(start) < 5);
454 static void hdmi_av_composer(struct dw_hdmi *hdmi,
455 const struct display_timing *edid)
457 bool mdataenablepolarity = true;
462 hbl = edid->hback_porch.typ + edid->hfront_porch.typ +
464 vbl = edid->vback_porch.typ + edid->vfront_porch.typ +
467 /* set up hdmi_fc_invidconf */
468 inv_val = HDMI_FC_INVIDCONF_HDCP_KEEPOUT_INACTIVE;
470 inv_val |= (edid->flags & DISPLAY_FLAGS_VSYNC_HIGH ?
471 HDMI_FC_INVIDCONF_VSYNC_IN_POLARITY_ACTIVE_HIGH :
472 HDMI_FC_INVIDCONF_VSYNC_IN_POLARITY_ACTIVE_LOW);
474 inv_val |= (edid->flags & DISPLAY_FLAGS_HSYNC_HIGH ?
475 HDMI_FC_INVIDCONF_HSYNC_IN_POLARITY_ACTIVE_HIGH :
476 HDMI_FC_INVIDCONF_HSYNC_IN_POLARITY_ACTIVE_LOW);
478 inv_val |= (mdataenablepolarity ?
479 HDMI_FC_INVIDCONF_DE_IN_POLARITY_ACTIVE_HIGH :
480 HDMI_FC_INVIDCONF_DE_IN_POLARITY_ACTIVE_LOW);
482 inv_val |= (edid->hdmi_monitor ?
483 HDMI_FC_INVIDCONF_DVI_MODEZ_HDMI_MODE :
484 HDMI_FC_INVIDCONF_DVI_MODEZ_DVI_MODE);
486 inv_val |= HDMI_FC_INVIDCONF_R_V_BLANK_IN_OSC_ACTIVE_LOW;
488 inv_val |= HDMI_FC_INVIDCONF_IN_I_P_PROGRESSIVE;
490 hdmi_write(hdmi, inv_val, HDMI_FC_INVIDCONF);
492 /* set up horizontal active pixel width */
493 hdmi_write(hdmi, edid->hactive.typ >> 8, HDMI_FC_INHACTV1);
494 hdmi_write(hdmi, edid->hactive.typ, HDMI_FC_INHACTV0);
496 /* set up vertical active lines */
497 hdmi_write(hdmi, edid->vactive.typ >> 8, HDMI_FC_INVACTV1);
498 hdmi_write(hdmi, edid->vactive.typ, HDMI_FC_INVACTV0);
500 /* set up horizontal blanking pixel region width */
501 hdmi_write(hdmi, hbl >> 8, HDMI_FC_INHBLANK1);
502 hdmi_write(hdmi, hbl, HDMI_FC_INHBLANK0);
504 /* set up vertical blanking pixel region width */
505 hdmi_write(hdmi, vbl, HDMI_FC_INVBLANK);
507 /* set up hsync active edge delay width (in pixel clks) */
508 hdmi_write(hdmi, edid->hfront_porch.typ >> 8, HDMI_FC_HSYNCINDELAY1);
509 hdmi_write(hdmi, edid->hfront_porch.typ, HDMI_FC_HSYNCINDELAY0);
511 /* set up vsync active edge delay (in lines) */
512 hdmi_write(hdmi, edid->vfront_porch.typ, HDMI_FC_VSYNCINDELAY);
514 /* set up hsync active pulse width (in pixel clks) */
515 hdmi_write(hdmi, edid->hsync_len.typ >> 8, HDMI_FC_HSYNCINWIDTH1);
516 hdmi_write(hdmi, edid->hsync_len.typ, HDMI_FC_HSYNCINWIDTH0);
518 /* set up vsync active edge delay (in lines) */
519 hdmi_write(hdmi, edid->vsync_len.typ, HDMI_FC_VSYNCINWIDTH);
522 static bool hdmi_bus_fmt_is_rgb(unsigned int bus_format)
524 switch (bus_format) {
525 case MEDIA_BUS_FMT_RGB888_1X24:
526 case MEDIA_BUS_FMT_RGB101010_1X30:
527 case MEDIA_BUS_FMT_RGB121212_1X36:
528 case MEDIA_BUS_FMT_RGB161616_1X48:
536 static bool hdmi_bus_fmt_is_yuv444(unsigned int bus_format)
538 switch (bus_format) {
539 case MEDIA_BUS_FMT_YUV8_1X24:
540 case MEDIA_BUS_FMT_YUV10_1X30:
541 case MEDIA_BUS_FMT_YUV12_1X36:
542 case MEDIA_BUS_FMT_YUV16_1X48:
550 static bool hdmi_bus_fmt_is_yuv422(unsigned int bus_format)
552 switch (bus_format) {
553 case MEDIA_BUS_FMT_UYVY8_1X16:
554 case MEDIA_BUS_FMT_UYVY10_1X20:
555 case MEDIA_BUS_FMT_UYVY12_1X24:
563 static int is_color_space_interpolation(struct dw_hdmi *hdmi)
565 if (!hdmi_bus_fmt_is_yuv422(hdmi->hdmi_data.enc_in_bus_format))
568 if (hdmi_bus_fmt_is_rgb(hdmi->hdmi_data.enc_out_bus_format) ||
569 hdmi_bus_fmt_is_yuv444(hdmi->hdmi_data.enc_out_bus_format))
575 static int is_color_space_decimation(struct dw_hdmi *hdmi)
577 if (!hdmi_bus_fmt_is_yuv422(hdmi->hdmi_data.enc_out_bus_format))
580 if (hdmi_bus_fmt_is_rgb(hdmi->hdmi_data.enc_in_bus_format) ||
581 hdmi_bus_fmt_is_yuv444(hdmi->hdmi_data.enc_in_bus_format))
587 static int hdmi_bus_fmt_color_depth(unsigned int bus_format)
589 switch (bus_format) {
590 case MEDIA_BUS_FMT_RGB888_1X24:
591 case MEDIA_BUS_FMT_YUV8_1X24:
592 case MEDIA_BUS_FMT_UYVY8_1X16:
593 case MEDIA_BUS_FMT_UYYVYY8_0_5X24:
596 case MEDIA_BUS_FMT_RGB101010_1X30:
597 case MEDIA_BUS_FMT_YUV10_1X30:
598 case MEDIA_BUS_FMT_UYVY10_1X20:
599 case MEDIA_BUS_FMT_UYYVYY10_0_5X30:
602 case MEDIA_BUS_FMT_RGB121212_1X36:
603 case MEDIA_BUS_FMT_YUV12_1X36:
604 case MEDIA_BUS_FMT_UYVY12_1X24:
605 case MEDIA_BUS_FMT_UYYVYY12_0_5X36:
608 case MEDIA_BUS_FMT_RGB161616_1X48:
609 case MEDIA_BUS_FMT_YUV16_1X48:
610 case MEDIA_BUS_FMT_UYYVYY16_0_5X48:
618 static int is_color_space_conversion(struct dw_hdmi *hdmi)
620 return hdmi->hdmi_data.enc_in_bus_format !=
621 hdmi->hdmi_data.enc_out_bus_format;
624 static void dw_hdmi_update_csc_coeffs(struct dw_hdmi *hdmi)
626 const u16 (*csc_coeff)[3][4] = &csc_coeff_default;
630 if (is_color_space_conversion(hdmi)) {
631 if (hdmi_bus_fmt_is_rgb(hdmi->hdmi_data.enc_out_bus_format)) {
632 csc_coeff = &csc_coeff_rgb_out_eitu601;
633 } else if (hdmi_bus_fmt_is_rgb(
634 hdmi->hdmi_data.enc_in_bus_format)) {
635 csc_coeff = &csc_coeff_rgb_in_eitu601;
640 /* The CSC registers are sequential, alternating MSB then LSB */
641 for (i = 0; i < ARRAY_SIZE(csc_coeff_default[0]); i++) {
642 u16 coeff_a = (*csc_coeff)[0][i];
643 u16 coeff_b = (*csc_coeff)[1][i];
644 u16 coeff_c = (*csc_coeff)[2][i];
646 hdmi_write(hdmi, coeff_a & 0xff, HDMI_CSC_COEF_A1_LSB + i * 2);
647 hdmi_write(hdmi, coeff_a >> 8, HDMI_CSC_COEF_A1_MSB + i * 2);
648 hdmi_write(hdmi, coeff_b & 0xff, HDMI_CSC_COEF_B1_LSB + i * 2);
649 hdmi_write(hdmi, coeff_b >> 8, HDMI_CSC_COEF_B1_MSB + i * 2);
650 hdmi_write(hdmi, coeff_c & 0xff, HDMI_CSC_COEF_C1_LSB + i * 2);
651 hdmi_write(hdmi, coeff_c >> 8, HDMI_CSC_COEF_C1_MSB + i * 2);
654 hdmi_mod(hdmi, HDMI_CSC_SCALE, HDMI_CSC_SCALE_CSCSCALE_MASK, csc_scale);
657 static void hdmi_video_csc(struct dw_hdmi *hdmi)
660 int interpolation = HDMI_CSC_CFG_INTMODE_DISABLE;
663 /* YCC422 interpolation to 444 mode */
664 if (is_color_space_interpolation(hdmi))
665 interpolation = HDMI_CSC_CFG_INTMODE_CHROMA_INT_FORMULA1;
666 else if (is_color_space_decimation(hdmi))
667 decimation = HDMI_CSC_CFG_DECMODE_CHROMA_INT_FORMULA3;
669 switch (hdmi_bus_fmt_color_depth(hdmi->hdmi_data.enc_out_bus_format)) {
671 color_depth = HDMI_CSC_SCALE_CSC_COLORDE_PTH_24BPP;
674 color_depth = HDMI_CSC_SCALE_CSC_COLORDE_PTH_30BPP;
677 color_depth = HDMI_CSC_SCALE_CSC_COLORDE_PTH_36BPP;
680 color_depth = HDMI_CSC_SCALE_CSC_COLORDE_PTH_48BPP;
687 /* Configure the CSC registers */
688 hdmi_write(hdmi, interpolation | decimation, HDMI_CSC_CFG);
690 hdmi_mod(hdmi, HDMI_CSC_SCALE, HDMI_CSC_SCALE_CSC_COLORDE_PTH_MASK,
693 dw_hdmi_update_csc_coeffs(hdmi);
696 /* hdmi initialization step b.4 */
697 static void hdmi_enable_video_path(struct dw_hdmi *hdmi, bool audio)
701 /* control period minimum duration */
702 hdmi_write(hdmi, 12, HDMI_FC_CTRLDUR);
703 hdmi_write(hdmi, 32, HDMI_FC_EXCTRLDUR);
704 hdmi_write(hdmi, 1, HDMI_FC_EXCTRLSPAC);
706 /* set to fill tmds data channels */
707 hdmi_write(hdmi, 0x0b, HDMI_FC_CH0PREAM);
708 hdmi_write(hdmi, 0x16, HDMI_FC_CH1PREAM);
709 hdmi_write(hdmi, 0x21, HDMI_FC_CH2PREAM);
711 hdmi_write(hdmi, HDMI_MC_FLOWCTRL_FEED_THROUGH_OFF_CSC_BYPASS,
714 /* enable pixel clock and tmds data path */
716 clkdis &= ~HDMI_MC_CLKDIS_PIXELCLK_DISABLE;
717 hdmi_write(hdmi, clkdis, HDMI_MC_CLKDIS);
719 clkdis &= ~HDMI_MC_CLKDIS_TMDSCLK_DISABLE;
720 hdmi_write(hdmi, clkdis, HDMI_MC_CLKDIS);
722 /* Enable csc path */
723 if (is_color_space_conversion(hdmi)) {
724 clkdis &= ~HDMI_MC_CLKDIS_CSCCLK_DISABLE;
725 hdmi_write(hdmi, clkdis, HDMI_MC_CLKDIS);
728 /* Enable color space conversion if needed */
729 if (is_color_space_conversion(hdmi))
730 hdmi_write(hdmi, HDMI_MC_FLOWCTRL_FEED_THROUGH_OFF_CSC_IN_PATH,
733 hdmi_write(hdmi, HDMI_MC_FLOWCTRL_FEED_THROUGH_OFF_CSC_BYPASS,
737 clkdis &= ~HDMI_MC_CLKDIS_AUDCLK_DISABLE;
738 hdmi_write(hdmi, clkdis, HDMI_MC_CLKDIS);
742 /* workaround to clear the overflow condition */
743 static void hdmi_clear_overflow(struct dw_hdmi *hdmi)
747 /* tmds software reset */
748 hdmi_write(hdmi, (u8)~HDMI_MC_SWRSTZ_TMDSSWRST_REQ, HDMI_MC_SWRSTZ);
750 val = hdmi_read(hdmi, HDMI_FC_INVIDCONF);
752 for (count = 0; count < 4; count++)
753 hdmi_write(hdmi, val, HDMI_FC_INVIDCONF);
756 static void hdmi_audio_set_format(struct dw_hdmi *hdmi)
758 hdmi_write(hdmi, HDMI_AUD_CONF0_I2S_SELECT | HDMI_AUD_CONF0_I2S_IN_EN_0,
762 hdmi_write(hdmi, HDMI_AUD_CONF1_I2S_MODE_STANDARD_MODE |
763 HDMI_AUD_CONF1_I2S_WIDTH_16BIT, HDMI_AUD_CONF1);
765 hdmi_write(hdmi, 0x00, HDMI_AUD_CONF2);
768 static void hdmi_audio_fifo_reset(struct dw_hdmi *hdmi)
770 hdmi_write(hdmi, (u8)~HDMI_MC_SWRSTZ_II2SSWRST_REQ, HDMI_MC_SWRSTZ);
771 hdmi_write(hdmi, HDMI_AUD_CONF0_SW_AUDIO_FIFO_RST, HDMI_AUD_CONF0);
773 hdmi_write(hdmi, 0x00, HDMI_AUD_INT);
774 hdmi_write(hdmi, 0x00, HDMI_AUD_INT1);
777 static int hdmi_get_plug_in_status(struct dw_hdmi *hdmi)
779 uint val = hdmi_read(hdmi, HDMI_PHY_STAT0) & HDMI_PHY_HPD;
784 static int hdmi_ddc_wait_i2c_done(struct dw_hdmi *hdmi, int msec)
789 start = get_timer(0);
791 val = hdmi_read(hdmi, HDMI_IH_I2CM_STAT0);
793 hdmi_write(hdmi, val, HDMI_IH_I2CM_STAT0);
798 } while (get_timer(start) < msec);
803 static void hdmi_ddc_reset(struct dw_hdmi *hdmi)
805 hdmi_mod(hdmi, HDMI_I2CM_SOFTRSTZ, HDMI_I2CM_SOFTRSTZ_MASK, 0);
808 static int hdmi_read_edid(struct dw_hdmi *hdmi, int block, u8 *buff)
810 int shift = (block % 2) * 0x80;
811 int edid_read_err = 0;
815 /* set ddc i2c clk which devided from ddc_clk to 100khz */
816 hdmi_write(hdmi, hdmi->i2c_clk_high, HDMI_I2CM_SS_SCL_HCNT_0_ADDR);
817 hdmi_write(hdmi, hdmi->i2c_clk_low, HDMI_I2CM_SS_SCL_LCNT_0_ADDR);
818 hdmi_mod(hdmi, HDMI_I2CM_DIV, HDMI_I2CM_DIV_FAST_STD_MODE,
819 HDMI_I2CM_DIV_STD_MODE);
821 hdmi_write(hdmi, HDMI_I2CM_SLAVE_DDC_ADDR, HDMI_I2CM_SLAVE);
822 hdmi_write(hdmi, HDMI_I2CM_SEGADDR_DDC, HDMI_I2CM_SEGADDR);
823 hdmi_write(hdmi, block >> 1, HDMI_I2CM_SEGPTR);
828 for (n = 0; n < HDMI_EDID_BLOCK_SIZE; n++) {
829 hdmi_write(hdmi, shift + n, HDMI_I2CM_ADDRESS);
832 hdmi_write(hdmi, HDMI_I2CM_OP_RD8,
833 HDMI_I2CM_OPERATION);
835 hdmi_write(hdmi, HDMI_I2CM_OP_RD8_EXT,
836 HDMI_I2CM_OPERATION);
838 if (hdmi_ddc_wait_i2c_done(hdmi, 10)) {
839 hdmi_ddc_reset(hdmi);
844 buff[n] = hdmi_read(hdmi, HDMI_I2CM_DATAI);
851 return edid_read_err;
854 static const u8 pre_buf[] = {
855 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00,
856 0x04, 0x69, 0xfa, 0x23, 0xc8, 0x28, 0x01, 0x00,
857 0x10, 0x17, 0x01, 0x03, 0x80, 0x33, 0x1d, 0x78,
858 0x2a, 0xd9, 0x45, 0xa2, 0x55, 0x4d, 0xa0, 0x27,
859 0x12, 0x50, 0x54, 0xb7, 0xef, 0x00, 0x71, 0x4f,
860 0x81, 0x40, 0x81, 0x80, 0x95, 0x00, 0xb3, 0x00,
861 0xd1, 0xc0, 0x81, 0xc0, 0x81, 0x00, 0x02, 0x3a,
862 0x80, 0x18, 0x71, 0x38, 0x2d, 0x40, 0x58, 0x2c,
863 0x45, 0x00, 0xfd, 0x1e, 0x11, 0x00, 0x00, 0x1e,
864 0x00, 0x00, 0x00, 0xff, 0x00, 0x44, 0x34, 0x4c,
865 0x4d, 0x54, 0x46, 0x30, 0x37, 0x35, 0x39, 0x37,
866 0x36, 0x0a, 0x00, 0x00, 0x00, 0xfd, 0x00, 0x32,
867 0x4b, 0x18, 0x53, 0x11, 0x00, 0x0a, 0x20, 0x20,
868 0x20, 0x20, 0x20, 0x20, 0x00, 0x00, 0x00, 0xfc,
869 0x00, 0x41, 0x53, 0x55, 0x53, 0x20, 0x56, 0x53,
870 0x32, 0x33, 0x38, 0x0a, 0x20, 0x20, 0x01, 0xb0,
871 0x02, 0x03, 0x22, 0x71, 0x4f, 0x01, 0x02, 0x03,
872 0x11, 0x12, 0x13, 0x04, 0x14, 0x05, 0x0e, 0x0f,
873 0x1d, 0x1e, 0x1f, 0x10, 0x23, 0x09, 0x17, 0x07,
874 0x83, 0x01, 0x00, 0x00, 0x65, 0x03, 0x0c, 0x00,
875 0x10, 0x00, 0x8c, 0x0a, 0xd0, 0x8a, 0x20, 0xe0,
876 0x2d, 0x10, 0x10, 0x3e, 0x96, 0x00, 0xfd, 0x1e,
877 0x11, 0x00, 0x00, 0x18, 0x01, 0x1d, 0x00, 0x72,
878 0x51, 0xd0, 0x1e, 0x20, 0x6e, 0x28, 0x55, 0x00,
879 0xfd, 0x1e, 0x11, 0x00, 0x00, 0x1e, 0x01, 0x1d,
880 0x00, 0xbc, 0x52, 0xd0, 0x1e, 0x20, 0xb8, 0x28,
881 0x55, 0x40, 0xfd, 0x1e, 0x11, 0x00, 0x00, 0x1e,
882 0x8c, 0x0a, 0xd0, 0x90, 0x20, 0x40, 0x31, 0x20,
883 0x0c, 0x40, 0x55, 0x00, 0xfd, 0x1e, 0x11, 0x00,
884 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
885 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
886 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe9,
889 int dw_hdmi_phy_cfg(struct dw_hdmi *hdmi, uint mpixelclock)
893 /* hdmi phy spec says to do the phy initialization sequence twice */
894 for (i = 0; i < 2; i++) {
895 hdmi_phy_sel_data_en_pol(hdmi, 1);
896 hdmi_phy_sel_interface_control(hdmi, 0);
897 hdmi_phy_enable_tmds(hdmi, 0);
898 hdmi_phy_enable_power(hdmi, 0);
900 ret = hdmi_phy_configure(hdmi, mpixelclock);
902 debug("hdmi phy config failure %d\n", ret);
910 int dw_hdmi_phy_wait_for_hpd(struct dw_hdmi *hdmi)
914 start = get_timer(0);
916 if (hdmi_get_plug_in_status(hdmi))
919 } while (get_timer(start) < 300);
924 void dw_hdmi_phy_init(struct dw_hdmi *hdmi)
926 /* enable phy i2cm done irq */
927 hdmi_write(hdmi, HDMI_PHY_I2CM_INT_ADDR_DONE_POL,
928 HDMI_PHY_I2CM_INT_ADDR);
930 /* enable phy i2cm nack & arbitration error irq */
931 hdmi_write(hdmi, HDMI_PHY_I2CM_CTLINT_ADDR_NAC_POL |
932 HDMI_PHY_I2CM_CTLINT_ADDR_ARBITRATION_POL,
933 HDMI_PHY_I2CM_CTLINT_ADDR);
935 /* enable cable hot plug irq */
936 hdmi_write(hdmi, (u8)~HDMI_PHY_HPD, HDMI_PHY_MASK0);
938 /* clear hotplug interrupts */
939 hdmi_write(hdmi, HDMI_IH_PHY_STAT0_HPD, HDMI_IH_PHY_STAT0);
942 int dw_hdmi_read_edid(struct dw_hdmi *hdmi, u8 *buf, int buf_size)
944 u32 edid_size = HDMI_EDID_BLOCK_SIZE;
948 edid_size = sizeof(pre_buf);
949 memcpy(buf, pre_buf, edid_size);
951 ret = hdmi_read_edid(hdmi, 0, buf);
953 debug("failed to read edid.\n");
957 if (buf[0x7e] != 0) {
958 hdmi_read_edid(hdmi, 1, buf + HDMI_EDID_BLOCK_SIZE);
959 edid_size += HDMI_EDID_BLOCK_SIZE;
966 int dw_hdmi_enable(struct dw_hdmi *hdmi, const struct display_timing *edid)
970 debug("%s, mode info : clock %d hdis %d vdis %d\n",
971 edid->hdmi_monitor ? "hdmi" : "dvi",
972 edid->pixelclock.typ, edid->hactive.typ, edid->vactive.typ);
974 hdmi_av_composer(hdmi, edid);
976 ret = hdmi->phy_set(hdmi, edid->pixelclock.typ);
980 hdmi_enable_video_path(hdmi, edid->hdmi_monitor);
982 if (edid->hdmi_monitor) {
983 hdmi_audio_fifo_reset(hdmi);
984 hdmi_audio_set_format(hdmi);
985 hdmi_audio_set_samplerate(hdmi, edid->pixelclock.typ);
988 hdmi_video_packetize(hdmi);
989 hdmi_video_csc(hdmi);
990 hdmi_video_sample(hdmi);
992 hdmi_clear_overflow(hdmi);
997 void dw_hdmi_init(struct dw_hdmi *hdmi)
1002 * boot up defaults are:
1003 * hdmi_ih_mute = 0x03 (disabled)
1004 * hdmi_ih_mute_* = 0x00 (enabled)
1006 * disable top level interrupt bits in hdmi block
1008 ih_mute = /*hdmi_read(hdmi, HDMI_IH_MUTE) |*/
1009 HDMI_IH_MUTE_MUTE_WAKEUP_INTERRUPT |
1010 HDMI_IH_MUTE_MUTE_ALL_INTERRUPT;
1012 if (hdmi->write_reg)
1013 hdmi_write = hdmi->write_reg;
1016 hdmi_read = hdmi->read_reg;
1018 hdmi_write(hdmi, ih_mute, HDMI_IH_MUTE);
1020 /* enable i2c master done irq */
1021 hdmi_write(hdmi, ~0x04, HDMI_I2CM_INT);
1023 /* enable i2c client nack % arbitration error irq */
1024 hdmi_write(hdmi, ~0x44, HDMI_I2CM_CTLINT);