1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (c) 2021 Fuzhou Rockchip Electronics Co., Ltd 4 * Author: Algea Cao <algea.cao@rock-chips.com> 5 */ 6 7 #include <common.h> 8 #include <malloc.h> 9 #include <syscon.h> 10 #include <asm/arch-rockchip/clock.h> 11 #include <asm/arch/vendor.h> 12 #include <edid.h> 13 #include <dm/device.h> 14 #include <dm/of_access.h> 15 #include <dm/ofnode.h> 16 #include <dm/read.h> 17 #include <linux/hdmi.h> 18 #include <linux/media-bus-format.h> 19 #include <linux/dw_hdmi.h> 20 #include <asm/io.h> 21 #include "rockchip_display.h" 22 #include "rockchip_crtc.h" 23 #include "rockchip_connector.h" 24 #include "dw_hdmi_qp.h" 25 #include "rockchip_phy.h" 26 27 enum frl_mask { 28 FRL_3GBPS_3LANE = 1, 29 FRL_6GBPS_3LANE, 30 FRL_6GBPS_4LANE, 31 FRL_8GBPS_4LANE, 32 FRL_10GBPS_4LANE, 33 FRL_12GBPS_4LANE, 34 }; 35 36 #define DDC_CI_ADDR 0x37 37 #define DDC_SEGMENT_ADDR 0x30 38 39 #define HDMI_EDID_LEN 512 40 41 /* DW-HDMI Controller >= 0x200a are at least compliant with SCDC version 1 */ 42 #define SCDC_MIN_SOURCE_VERSION 0x1 43 44 #define HDMI14_MAX_TMDSCLK 340000000 45 46 struct hdmi_vmode { 47 bool mdataenablepolarity; 48 49 unsigned int mpixelclock; 50 unsigned int mpixelrepetitioninput; 51 unsigned int mpixelrepetitionoutput; 52 unsigned int mtmdsclock; 53 }; 54 55 struct hdmi_data_info { 56 unsigned int enc_in_bus_format; 57 unsigned int enc_out_bus_format; 58 unsigned int enc_in_encoding; 59 unsigned int enc_out_encoding; 60 unsigned int quant_range; 61 unsigned int pix_repet_factor; 62 struct hdmi_vmode video_mode; 63 }; 64 65 struct dw_hdmi_phy_data { 66 enum dw_hdmi_phy_type type; 67 const char *name; 68 unsigned int gen; 69 bool has_svsret; 70 int (*configure)(struct dw_hdmi *hdmi, 71 const struct dw_hdmi_plat_data *pdata, 72 unsigned long mpixelclock); 73 }; 74 75 struct dw_hdmi_i2c { 76 u8 slave_reg; 77 bool is_regaddr; 78 bool is_segment; 79 80 unsigned int scl_high_ns; 81 unsigned int scl_low_ns; 82 }; 83 84 struct dw_hdmi_qp { 85 enum dw_hdmi_devtype dev_type; 86 unsigned int version; 87 struct hdmi_data_info hdmi_data; 88 struct hdmi_edid_data edid_data; 89 const struct dw_hdmi_plat_data *plat_data; 90 struct ddc_adapter adap; 91 92 int vic; 93 int id; 94 95 unsigned long bus_format; 96 bool cable_plugin; 97 bool sink_is_hdmi; 98 bool sink_has_audio; 99 void *regs; 100 void *rk_hdmi; 101 struct dw_hdmi_i2c *i2c; 102 103 struct { 104 const struct dw_hdmi_qp_phy_ops *ops; 105 const char *name; 106 void *data; 107 bool enabled; 108 } phy; 109 110 struct drm_display_mode previous_mode; 111 112 unsigned int sample_rate; 113 unsigned int audio_cts; 114 unsigned int audio_n; 115 bool audio_enable; 116 bool scramble_low_rates; 117 118 void (*write)(struct dw_hdmi_qp *hdmi, u32 val, int offset); 119 u8 (*read)(struct dw_hdmi_qp *hdmi, int offset); 120 121 bool hdcp1x_enable; 122 bool output_bus_format_rgb; 123 }; 124 125 static inline void hdmi_writel(struct dw_hdmi_qp *hdmi, u32 val, int offset) 126 { 127 writel(val, hdmi->regs + offset); 128 } 129 130 static inline u32 hdmi_readl(struct dw_hdmi_qp *hdmi, int offset) 131 { 132 return readl(hdmi->regs + offset); 133 } 134 135 static void 136 hdmi_modb(struct dw_hdmi_qp *hdmi, u32 data, u32 mask, unsigned int reg) 137 { 138 u32 val = hdmi_readl(hdmi, reg) & ~mask; 139 140 val |= data & mask; 141 hdmi_writel(hdmi, val, reg); 142 } 143 144 static bool hdmi_bus_fmt_is_rgb(unsigned int bus_format) 145 { 146 switch (bus_format) { 147 case MEDIA_BUS_FMT_RGB888_1X24: 148 case MEDIA_BUS_FMT_RGB101010_1X30: 149 case MEDIA_BUS_FMT_RGB121212_1X36: 150 case MEDIA_BUS_FMT_RGB161616_1X48: 151 return true; 152 153 default: 154 return false; 155 } 156 } 157 158 static bool hdmi_bus_fmt_is_yuv444(unsigned int bus_format) 159 { 160 switch (bus_format) { 161 case MEDIA_BUS_FMT_YUV8_1X24: 162 case MEDIA_BUS_FMT_YUV10_1X30: 163 case MEDIA_BUS_FMT_YUV12_1X36: 164 case MEDIA_BUS_FMT_YUV16_1X48: 165 return true; 166 167 default: 168 return false; 169 } 170 } 171 172 static bool hdmi_bus_fmt_is_yuv422(unsigned int bus_format) 173 { 174 switch (bus_format) { 175 case MEDIA_BUS_FMT_UYVY8_1X16: 176 case MEDIA_BUS_FMT_UYVY10_1X20: 177 case MEDIA_BUS_FMT_UYVY12_1X24: 178 return true; 179 180 default: 181 return false; 182 } 183 } 184 185 static bool hdmi_bus_fmt_is_yuv420(unsigned int bus_format) 186 { 187 switch (bus_format) { 188 case MEDIA_BUS_FMT_UYYVYY8_0_5X24: 189 case MEDIA_BUS_FMT_UYYVYY10_0_5X30: 190 case MEDIA_BUS_FMT_UYYVYY12_0_5X36: 191 case MEDIA_BUS_FMT_UYYVYY16_0_5X48: 192 return true; 193 194 default: 195 return false; 196 } 197 } 198 199 static int hdmi_bus_fmt_color_depth(unsigned int bus_format) 200 { 201 switch (bus_format) { 202 case MEDIA_BUS_FMT_RGB888_1X24: 203 case MEDIA_BUS_FMT_YUV8_1X24: 204 case MEDIA_BUS_FMT_UYVY8_1X16: 205 case MEDIA_BUS_FMT_UYYVYY8_0_5X24: 206 return 8; 207 208 case MEDIA_BUS_FMT_RGB101010_1X30: 209 case MEDIA_BUS_FMT_YUV10_1X30: 210 case MEDIA_BUS_FMT_UYVY10_1X20: 211 case MEDIA_BUS_FMT_UYYVYY10_0_5X30: 212 return 10; 213 214 case MEDIA_BUS_FMT_RGB121212_1X36: 215 case MEDIA_BUS_FMT_YUV12_1X36: 216 case MEDIA_BUS_FMT_UYVY12_1X24: 217 case MEDIA_BUS_FMT_UYYVYY12_0_5X36: 218 return 12; 219 220 case MEDIA_BUS_FMT_RGB161616_1X48: 221 case MEDIA_BUS_FMT_YUV16_1X48: 222 case MEDIA_BUS_FMT_UYYVYY16_0_5X48: 223 return 16; 224 225 default: 226 return 0; 227 } 228 } 229 230 static bool drm_scdc_set_scrambling(struct ddc_adapter *adapter, bool enable) 231 { 232 u8 config; 233 int ret; 234 235 ret = drm_scdc_readb(adapter, SCDC_TMDS_CONFIG, &config); 236 if (ret < 0) { 237 debug("Failed to read TMDS config: %d\n", ret); 238 return false; 239 } 240 241 if (enable) 242 config |= SCDC_SCRAMBLING_ENABLE; 243 else 244 config &= ~SCDC_SCRAMBLING_ENABLE; 245 246 ret = drm_scdc_writeb(adapter, SCDC_TMDS_CONFIG, config); 247 if (ret < 0) { 248 debug("Failed to enable scrambling: %d\n", ret); 249 return false; 250 } 251 252 return true; 253 } 254 255 static bool 256 drm_scdc_set_high_tmds_clock_ratio(struct ddc_adapter *adapter, bool set) 257 { 258 u8 config; 259 int ret; 260 261 ret = drm_scdc_readb(adapter, SCDC_TMDS_CONFIG, &config); 262 if (ret < 0) { 263 debug("Failed to read TMDS config: %d\n", ret); 264 return false; 265 } 266 267 if (set) 268 config |= SCDC_TMDS_BIT_CLOCK_RATIO_BY_40; 269 else 270 config &= ~SCDC_TMDS_BIT_CLOCK_RATIO_BY_40; 271 272 ret = drm_scdc_writeb(adapter, SCDC_TMDS_CONFIG, config); 273 if (ret < 0) { 274 debug("Failed to set TMDS clock ratio: %d\n", ret); 275 return false; 276 } 277 278 /* 279 * The spec says that a source should wait minimum 1ms and maximum 280 * 100ms after writing the TMDS config for clock ratio. Lets allow a 281 * wait of up to 2ms here. 282 */ 283 udelay(2000); 284 return true; 285 } 286 287 static void dw_hdmi_i2c_init(struct dw_hdmi_qp *hdmi) 288 { 289 /* Software reset */ 290 hdmi_writel(hdmi, 0x01, I2CM_CONTROL0); 291 292 hdmi_writel(hdmi, 0x085c085c, I2CM_FM_SCL_CONFIG0); 293 294 hdmi_modb(hdmi, 0, I2CM_FM_EN, I2CM_INTERFACE_CONTROL0); 295 296 /* Clear DONE and ERROR interrupts */ 297 hdmi_writel(hdmi, I2CM_OP_DONE_CLEAR | I2CM_NACK_RCVD_CLEAR, 298 MAINUNIT_1_INT_CLEAR); 299 } 300 301 static int dw_hdmi_i2c_read(struct dw_hdmi_qp *hdmi, 302 unsigned char *buf, unsigned int length) 303 { 304 struct dw_hdmi_i2c *i2c = hdmi->i2c; 305 int i = 20; 306 u32 intr = 0; 307 308 if (!i2c->is_regaddr) { 309 printf("set read register address to 0\n"); 310 i2c->slave_reg = 0x00; 311 i2c->is_regaddr = true; 312 } 313 314 while (length--) { 315 hdmi_modb(hdmi, i2c->slave_reg++ << 12, I2CM_ADDR, 316 I2CM_INTERFACE_CONTROL0); 317 318 hdmi_modb(hdmi, I2CM_FM_READ, I2CM_WR_MASK, 319 I2CM_INTERFACE_CONTROL0); 320 321 while (i--) { 322 udelay(1000); 323 intr = hdmi_readl(hdmi, MAINUNIT_1_INT_STATUS) & 324 (I2CM_OP_DONE_IRQ | I2CM_READ_REQUEST_IRQ | 325 I2CM_NACK_RCVD_IRQ); 326 if (intr) { 327 hdmi_writel(hdmi, intr, MAINUNIT_1_INT_CLEAR); 328 break; 329 } 330 } 331 332 if (!i) { 333 printf("i2c read time out!\n"); 334 hdmi_writel(hdmi, 0x01, I2CM_CONTROL0); 335 return -EAGAIN; 336 } 337 338 /* Check for error condition on the bus */ 339 if (intr & I2CM_NACK_RCVD_IRQ) { 340 printf("i2c read err!\n"); 341 hdmi_writel(hdmi, 0x01, I2CM_CONTROL0); 342 return -EIO; 343 } 344 345 *buf++ = hdmi_readl(hdmi, I2CM_INTERFACE_RDDATA_0_3) & 0xff; 346 hdmi_modb(hdmi, 0, I2CM_WR_MASK, I2CM_INTERFACE_CONTROL0); 347 i = 20; 348 } 349 i2c->is_segment = false; 350 351 return 0; 352 } 353 354 static int dw_hdmi_i2c_write(struct dw_hdmi_qp *hdmi, 355 unsigned char *buf, unsigned int length) 356 { 357 struct dw_hdmi_i2c *i2c = hdmi->i2c; 358 int i = 20; 359 u32 intr = 0; 360 361 if (!i2c->is_regaddr) { 362 /* Use the first write byte as register address */ 363 i2c->slave_reg = buf[0]; 364 length--; 365 buf++; 366 i2c->is_regaddr = true; 367 } 368 369 while (length--) { 370 hdmi_writel(hdmi, *buf++, I2CM_INTERFACE_WRDATA_0_3); 371 hdmi_modb(hdmi, i2c->slave_reg++ << 12, I2CM_ADDR, 372 I2CM_INTERFACE_CONTROL0); 373 hdmi_modb(hdmi, I2CM_FM_WRITE, I2CM_WR_MASK, 374 I2CM_INTERFACE_CONTROL0); 375 376 while (i--) { 377 udelay(1000); 378 intr = hdmi_readl(hdmi, MAINUNIT_1_INT_STATUS) & 379 (I2CM_OP_DONE_IRQ | I2CM_READ_REQUEST_IRQ | 380 I2CM_NACK_RCVD_IRQ); 381 if (intr) { 382 hdmi_writel(hdmi, intr, MAINUNIT_1_INT_CLEAR); 383 break; 384 } 385 } 386 387 if (!i) { 388 printf("i2c write time out!\n"); 389 hdmi_writel(hdmi, 0x01, I2CM_CONTROL0); 390 return -EAGAIN; 391 } 392 393 /* Check for error condition on the bus */ 394 if (intr & I2CM_NACK_RCVD_IRQ) { 395 printf("i2c write nack!\n"); 396 hdmi_writel(hdmi, 0x01, I2CM_CONTROL0); 397 return -EIO; 398 } 399 hdmi_modb(hdmi, 0, I2CM_WR_MASK, I2CM_INTERFACE_CONTROL0); 400 i = 20; 401 } 402 403 return 0; 404 } 405 406 static int dw_hdmi_i2c_xfer(struct ddc_adapter *adap, 407 struct i2c_msg *msgs, int num) 408 { 409 struct dw_hdmi_qp *hdmi = container_of(adap, struct dw_hdmi_qp, adap); 410 struct dw_hdmi_i2c *i2c = hdmi->i2c; 411 u8 addr = msgs[0].addr; 412 int i, ret = 0; 413 414 debug("i2c xfer: num: %d, addr: %#x\n", num, addr); 415 416 for (i = 0; i < num; i++) { 417 if (msgs[i].len == 0) { 418 printf("unsupported transfer %d/%d, no data\n", 419 i + 1, num); 420 return -EOPNOTSUPP; 421 } 422 } 423 424 /* Unmute DONE and ERROR interrupts */ 425 hdmi_modb(hdmi, I2CM_NACK_RCVD_MASK_N | I2CM_OP_DONE_MASK_N, 426 I2CM_NACK_RCVD_MASK_N | I2CM_OP_DONE_MASK_N, 427 MAINUNIT_1_INT_MASK_N); 428 429 /* Set slave device address taken from the first I2C message */ 430 if (addr == DDC_SEGMENT_ADDR && msgs[0].len == 1) 431 addr = DDC_ADDR; 432 433 hdmi_modb(hdmi, addr << 5, I2CM_SLVADDR, I2CM_INTERFACE_CONTROL0); 434 435 /* Set slave device register address on transfer */ 436 i2c->is_regaddr = false; 437 438 /* Set segment pointer for I2C extended read mode operation */ 439 i2c->is_segment = false; 440 441 for (i = 0; i < num; i++) { 442 debug("xfer: num: %d/%d, len: %d, flags: %#x\n", 443 i + 1, num, msgs[i].len, msgs[i].flags); 444 445 if (msgs[i].addr == DDC_SEGMENT_ADDR && msgs[i].len == 1) { 446 i2c->is_segment = true; 447 hdmi_modb(hdmi, DDC_SEGMENT_ADDR, I2CM_SEG_ADDR, 448 I2CM_INTERFACE_CONTROL1); 449 hdmi_modb(hdmi, *msgs[i].buf, I2CM_SEG_PTR, 450 I2CM_INTERFACE_CONTROL1); 451 } else { 452 if (msgs[i].flags & I2C_M_RD) 453 ret = dw_hdmi_i2c_read(hdmi, msgs[i].buf, 454 msgs[i].len); 455 else 456 ret = dw_hdmi_i2c_write(hdmi, msgs[i].buf, 457 msgs[i].len); 458 } 459 if (ret < 0) 460 break; 461 } 462 463 if (!ret) 464 ret = num; 465 466 /* Mute DONE and ERROR interrupts */ 467 hdmi_modb(hdmi, 0, I2CM_OP_DONE_MASK_N | I2CM_NACK_RCVD_MASK_N, 468 MAINUNIT_1_INT_MASK_N); 469 470 return ret; 471 } 472 473 static int dw_hdmi_detect_phy(struct dw_hdmi_qp *hdmi) 474 { 475 /* Vendor PHYs require support from the glue layer. */ 476 if (!hdmi->plat_data->qp_phy_ops || !hdmi->plat_data->phy_name) { 477 dev_err(hdmi->dev, 478 "Vendor HDMI PHY not supported by glue layer\n"); 479 return -ENODEV; 480 } 481 482 hdmi->phy.ops = hdmi->plat_data->qp_phy_ops; 483 hdmi->phy.data = hdmi->plat_data->phy_data; 484 hdmi->phy.name = hdmi->plat_data->phy_name; 485 486 return 0; 487 } 488 489 static unsigned int 490 hdmi_get_tmdsclock(struct dw_hdmi_qp *hdmi, unsigned long mpixelclock) 491 { 492 unsigned int tmdsclock = mpixelclock; 493 unsigned int depth = 494 hdmi_bus_fmt_color_depth(hdmi->hdmi_data.enc_out_bus_format); 495 496 if (!hdmi_bus_fmt_is_yuv422(hdmi->hdmi_data.enc_out_bus_format)) { 497 switch (depth) { 498 case 16: 499 tmdsclock = mpixelclock * 2; 500 break; 501 case 12: 502 tmdsclock = mpixelclock * 3 / 2; 503 break; 504 case 10: 505 tmdsclock = mpixelclock * 5 / 4; 506 break; 507 default: 508 break; 509 } 510 } 511 512 return tmdsclock; 513 } 514 515 static void hdmi_config_AVI(struct dw_hdmi_qp *hdmi, struct drm_display_mode *mode) 516 { 517 struct hdmi_avi_infoframe frame; 518 u32 val, i, j; 519 u8 buff[17]; 520 bool is_hdmi2 = false; 521 enum hdmi_quantization_range rgb_quant_range = 522 hdmi->hdmi_data.quant_range; 523 524 if (hdmi_bus_fmt_is_yuv420(hdmi->hdmi_data.enc_out_bus_format) || 525 hdmi->edid_data.display_info.hdmi.scdc.supported) 526 is_hdmi2 = true; 527 /* Initialise info frame from DRM mode */ 528 drm_hdmi_avi_infoframe_from_display_mode(&frame, mode, is_hdmi2); 529 530 /* 531 * Ignore monitor selectable quantization, use quantization set 532 * by the user 533 */ 534 drm_hdmi_avi_infoframe_quant_range(&frame, mode, rgb_quant_range, 535 true); 536 if (hdmi_bus_fmt_is_yuv444(hdmi->hdmi_data.enc_out_bus_format)) 537 frame.colorspace = HDMI_COLORSPACE_YUV444; 538 else if (hdmi_bus_fmt_is_yuv422(hdmi->hdmi_data.enc_out_bus_format)) 539 frame.colorspace = HDMI_COLORSPACE_YUV422; 540 else if (hdmi_bus_fmt_is_yuv420(hdmi->hdmi_data.enc_out_bus_format)) 541 frame.colorspace = HDMI_COLORSPACE_YUV420; 542 else 543 frame.colorspace = HDMI_COLORSPACE_RGB; 544 545 /* Set up colorimetry */ 546 if (!hdmi_bus_fmt_is_rgb(hdmi->hdmi_data.enc_out_bus_format)) { 547 switch (hdmi->hdmi_data.enc_out_encoding) { 548 case V4L2_YCBCR_ENC_601: 549 if (hdmi->hdmi_data.enc_in_encoding == V4L2_YCBCR_ENC_XV601) 550 frame.colorimetry = HDMI_COLORIMETRY_EXTENDED; 551 else 552 frame.colorimetry = HDMI_COLORIMETRY_ITU_601; 553 frame.extended_colorimetry = 554 HDMI_EXTENDED_COLORIMETRY_XV_YCC_601; 555 break; 556 case V4L2_YCBCR_ENC_709: 557 if (hdmi->hdmi_data.enc_in_encoding == V4L2_YCBCR_ENC_XV709) 558 frame.colorimetry = HDMI_COLORIMETRY_EXTENDED; 559 else 560 frame.colorimetry = HDMI_COLORIMETRY_ITU_709; 561 frame.extended_colorimetry = 562 HDMI_EXTENDED_COLORIMETRY_XV_YCC_709; 563 break; 564 default: /* Carries no data */ 565 frame.colorimetry = HDMI_COLORIMETRY_ITU_601; 566 frame.extended_colorimetry = 567 HDMI_EXTENDED_COLORIMETRY_XV_YCC_601; 568 break; 569 } 570 } 571 572 frame.scan_mode = HDMI_SCAN_MODE_NONE; 573 frame.video_code = hdmi->vic; 574 575 hdmi_avi_infoframe_pack_only(&frame, buff, 17); 576 577 /* 578 * The Designware IP uses a different byte format from standard 579 * AVI info frames, though generally the bits are in the correct 580 * bytes. 581 */ 582 583 val = (frame.version << 8) | (frame.length << 16); 584 hdmi_writel(hdmi, val, PKT_AVI_CONTENTS0); 585 586 for (i = 0; i < 4; i++) { 587 for (j = 0; j < 4; j++) { 588 if (i * 4 + j >= 14) 589 break; 590 if (!j) 591 val = buff[i * 4 + j + 3]; 592 val |= buff[i * 4 + j + 3] << (8 * j); 593 } 594 595 hdmi_writel(hdmi, val, PKT_AVI_CONTENTS1 + i * 4); 596 } 597 598 hdmi_modb(hdmi, PKTSCHED_AVI_TX_EN | PKTSCHED_GCP_TX_EN, 599 PKTSCHED_AVI_TX_EN | PKTSCHED_GCP_TX_EN, 600 PKTSCHED_PKT_EN); 601 } 602 603 static void hdmi_config_CVTEM(struct dw_hdmi_qp *hdmi, 604 struct dw_hdmi_link_config *link_cfg) 605 { 606 u8 ds_type = 0; 607 u8 sync = 1; 608 u8 vfr = 1; 609 u8 afr = 0; 610 u8 new = 1; 611 u8 end = 0; 612 u8 data_set_length = 136; 613 u8 hb1[6] = { 0x80, 0, 0, 0, 0, 0x40 }; 614 u8 *pps_body; 615 u32 val, i, reg; 616 struct drm_display_mode *mode = &hdmi->previous_mode; 617 int hsync, hfront, hback; 618 619 hdmi_modb(hdmi, 0, PKTSCHED_EMP_CVTEM_TX_EN, PKTSCHED_PKT_EN); 620 621 if (!link_cfg->dsc_mode) { 622 printf("don't use dsc mode\n"); 623 return; 624 } 625 626 pps_body = link_cfg->pps_payload; 627 628 hsync = mode->hsync_end - mode->hsync_start; 629 hback = mode->htotal - mode->hsync_end; 630 hfront = mode->hsync_start - mode->hdisplay; 631 632 for (i = 0; i < 6; i++) { 633 val = i << 16 | hb1[i] << 8; 634 hdmi_writel(hdmi, val, PKT0_EMP_CVTEM_CONTENTS0 + i * 0x20); 635 } 636 637 val = new << 7 | end << 6 | ds_type << 4 | afr << 3 | 638 vfr << 2 | sync << 1; 639 hdmi_writel(hdmi, val, PKT0_EMP_CVTEM_CONTENTS1); 640 641 val = data_set_length << 16 | pps_body[0] << 24; 642 hdmi_writel(hdmi, val, PKT0_EMP_CVTEM_CONTENTS2); 643 644 reg = PKT0_EMP_CVTEM_CONTENTS3; 645 for (i = 1; i < 125; i++) { 646 if (reg == PKT1_EMP_CVTEM_CONTENTS0 || 647 reg == PKT2_EMP_CVTEM_CONTENTS0 || 648 reg == PKT3_EMP_CVTEM_CONTENTS0 || 649 reg == PKT4_EMP_CVTEM_CONTENTS0 || 650 reg == PKT5_EMP_CVTEM_CONTENTS0) { 651 reg += 4; 652 i--; 653 continue; 654 } 655 if (i % 4 == 1) 656 val = pps_body[i]; 657 if (i % 4 == 2) 658 val |= pps_body[i] << 8; 659 if (i % 4 == 3) 660 val |= pps_body[i] << 16; 661 if (!(i % 4)) { 662 val |= pps_body[i] << 24; 663 hdmi_writel(hdmi, val, reg); 664 reg += 4; 665 } 666 } 667 668 val = (hfront & 0xff) << 24 | pps_body[127] << 16 | 669 pps_body[126] << 8 | pps_body[125]; 670 hdmi_writel(hdmi, val, PKT4_EMP_CVTEM_CONTENTS6); 671 672 val = (hback & 0xff) << 24 | ((hsync >> 8) & 0xff) << 16 | 673 (hsync & 0xff) << 8 | ((hfront >> 8) & 0xff); 674 hdmi_writel(hdmi, val, PKT4_EMP_CVTEM_CONTENTS7); 675 676 val = link_cfg->hcactive << 8 | ((hback >> 8) & 0xff); 677 hdmi_writel(hdmi, val, PKT5_EMP_CVTEM_CONTENTS1); 678 679 for (i = PKT5_EMP_CVTEM_CONTENTS2; i <= PKT5_EMP_CVTEM_CONTENTS7; i += 4) 680 hdmi_writel(hdmi, 0, i); 681 682 hdmi_modb(hdmi, PKTSCHED_EMP_CVTEM_TX_EN, PKTSCHED_EMP_CVTEM_TX_EN, 683 PKTSCHED_PKT_EN); 684 } 685 686 static int hdmi_set_frl_mask(int frl_rate) 687 { 688 switch (frl_rate) { 689 case 48: 690 return FRL_12GBPS_4LANE; 691 case 40: 692 return FRL_10GBPS_4LANE; 693 case 32: 694 return FRL_8GBPS_4LANE; 695 case 24: 696 return FRL_6GBPS_4LANE; 697 case 18: 698 return FRL_6GBPS_3LANE; 699 case 9: 700 return FRL_3GBPS_3LANE; 701 } 702 703 return 0; 704 } 705 706 static int hdmi_start_flt(struct dw_hdmi_qp *hdmi, u8 rate) 707 { 708 u8 val; 709 u32 value; 710 u8 ffe_lv = 0; 711 int i = 0; 712 bool ltsp = false; 713 714 hdmi_modb(hdmi, BIT(6), BIT(6), 0x44); 715 /* FLT_READY & FFE_LEVELS read */ 716 for (i = 0; i < 20; i++) { 717 drm_scdc_readb(&hdmi->adap, SCDC_STATUS_FLAGS_0, &val); 718 if (val & BIT(6)) 719 break; 720 mdelay(20); 721 } 722 723 if (i == 20) { 724 dev_err(hdmi->dev, "sink flt isn't ready\n"); 725 return -EINVAL; 726 } 727 728 /* max ffe level 3 */ 729 val = 0 << 4 | hdmi_set_frl_mask(rate); 730 drm_scdc_writeb(&hdmi->adap, 0x31, val); 731 732 /* select FRL_RATE & FFE_LEVELS */ 733 hdmi_writel(hdmi, ffe_lv, FLT_CONFIG0); 734 735 while (1) { 736 mdelay(4); 737 drm_scdc_readb(&hdmi->adap, 0x10, &val); 738 739 if (!(val & 0x30)) 740 continue; 741 742 if (val & BIT(5)) { 743 u8 reg_val, ln0, ln1, ln2, ln3; 744 745 drm_scdc_readb(&hdmi->adap, 0x41, ®_val); 746 ln0 = reg_val & 0xf; 747 ln1 = (reg_val >> 4) & 0xf; 748 749 drm_scdc_readb(&hdmi->adap, 0x42, ®_val); 750 ln2 = reg_val & 0xf; 751 ln3 = (reg_val >> 4) & 0xf; 752 753 if (!ln0 && !ln1 && !ln2 && !ln3) { 754 printf("ltsp!\n"); 755 ltsp = true; 756 hdmi_writel(hdmi, 0, FLT_CONFIG1); 757 } else if ((ln0 == 0xf) | (ln1 == 0xf) | (ln2 == 0xf) | (ln3 == 0xf)) { 758 printf("lts4!\n"); 759 break; 760 } else if ((ln0 == 0xe) | (ln1 == 0xe) | (ln2 == 0xe) | (ln3 == 0xe)) { 761 printf("ffe!\n"); 762 break; 763 } else { 764 value = (ln3 << 16) | (ln2 << 12) | (ln1 << 8) | (ln0 << 4) | 0xf; 765 hdmi_writel(hdmi, value, FLT_CONFIG1); 766 } 767 } 768 769 drm_scdc_writeb(&hdmi->adap, 0x10, val); 770 771 if ((val & BIT(4)) && ltsp) { 772 printf("flt success\n"); 773 break; 774 } 775 } 776 777 hdmi_modb(hdmi, 0, BIT(6), 0x44); 778 return 0; 779 } 780 781 #define HDMI_MODE_FRL_MASK BIT(30) 782 783 static void hdmi_set_op_mode(struct dw_hdmi_qp *hdmi, 784 struct dw_hdmi_link_config *link_cfg, 785 bool scdc_support) 786 { 787 int frl_rate; 788 int i; 789 790 hdmi_writel(hdmi, 0, FLT_CONFIG0); 791 if (scdc_support) 792 drm_scdc_writeb(&hdmi->adap, 0x31, 0); 793 mdelay(200); 794 if (!link_cfg->frl_mode) { 795 printf("dw hdmi qp use tmds mode\n"); 796 hdmi_modb(hdmi, 0, OPMODE_FRL, LINK_CONFIG0); 797 hdmi_modb(hdmi, 0, OPMODE_FRL_4LANES, LINK_CONFIG0); 798 return; 799 } 800 801 if (link_cfg->frl_lanes == 4) 802 hdmi_modb(hdmi, OPMODE_FRL_4LANES, OPMODE_FRL_4LANES, 803 LINK_CONFIG0); 804 else 805 hdmi_modb(hdmi, 0, OPMODE_FRL_4LANES, LINK_CONFIG0); 806 807 hdmi_modb(hdmi, 1, OPMODE_FRL, LINK_CONFIG0); 808 809 frl_rate = link_cfg->frl_lanes * link_cfg->rate_per_lane; 810 hdmi_start_flt(hdmi, frl_rate); 811 812 for (i = 0; i < 50; i++) { 813 hdmi_modb(hdmi, PKTSCHED_NULL_TX_EN, PKTSCHED_NULL_TX_EN, PKTSCHED_PKT_EN); 814 mdelay(1); 815 hdmi_modb(hdmi, 0, PKTSCHED_NULL_TX_EN, PKTSCHED_PKT_EN); 816 } 817 } 818 819 static int dw_hdmi_setup(struct dw_hdmi_qp *hdmi, 820 struct drm_display_mode *mode, 821 struct display_state *state) 822 { 823 int ret; 824 void *data = hdmi->plat_data->phy_data; 825 struct dw_hdmi_link_config *link_cfg; 826 struct drm_hdmi_info *hdmi_info = &hdmi->edid_data.display_info.hdmi; 827 struct hdmi_vmode *vmode = &hdmi->hdmi_data.video_mode; 828 u8 bytes = 0; 829 830 if (!hdmi->vic) 831 printf("Non-CEA mode used in HDMI\n"); 832 else 833 printf("CEA mode used vic=%d\n", hdmi->vic); 834 835 vmode->mpixelclock = mode->clock * 1000; 836 vmode->mtmdsclock = hdmi_get_tmdsclock(hdmi, vmode->mpixelclock); 837 if (hdmi_bus_fmt_is_yuv420(hdmi->hdmi_data.enc_out_bus_format)) 838 vmode->mtmdsclock /= 2; 839 printf("mtmdsclock:%d\n", vmode->mtmdsclock); 840 841 if (hdmi->plat_data->get_enc_out_encoding) 842 hdmi->hdmi_data.enc_out_encoding = 843 hdmi->plat_data->get_enc_out_encoding(data); 844 else if (hdmi->vic == 6 || hdmi->vic == 7 || 845 hdmi->vic == 21 || hdmi->vic == 22 || 846 hdmi->vic == 2 || hdmi->vic == 3 || 847 hdmi->vic == 17 || hdmi->vic == 18) 848 hdmi->hdmi_data.enc_out_encoding = V4L2_YCBCR_ENC_601; 849 else 850 hdmi->hdmi_data.enc_out_encoding = V4L2_YCBCR_ENC_709; 851 852 if (mode->flags & DRM_MODE_FLAG_DBLCLK) { 853 hdmi->hdmi_data.video_mode.mpixelrepetitionoutput = 1; 854 hdmi->hdmi_data.video_mode.mpixelrepetitioninput = 1; 855 } else { 856 hdmi->hdmi_data.video_mode.mpixelrepetitionoutput = 0; 857 hdmi->hdmi_data.video_mode.mpixelrepetitioninput = 0; 858 } 859 860 /* TOFIX: Get input encoding from plat data or fallback to none */ 861 if (hdmi->plat_data->get_enc_in_encoding) 862 hdmi->hdmi_data.enc_in_encoding = 863 hdmi->plat_data->get_enc_in_encoding(data); 864 else if (hdmi->plat_data->input_bus_encoding) 865 hdmi->hdmi_data.enc_in_encoding = 866 hdmi->plat_data->input_bus_encoding; 867 else 868 hdmi->hdmi_data.enc_in_encoding = V4L2_YCBCR_ENC_DEFAULT; 869 870 if (hdmi->plat_data->get_quant_range) 871 hdmi->hdmi_data.quant_range = 872 hdmi->plat_data->get_quant_range(data); 873 else 874 hdmi->hdmi_data.quant_range = HDMI_QUANTIZATION_RANGE_DEFAULT; 875 876 /* 877 * According to the dw-hdmi specification 6.4.2 878 * vp_pr_cd[3:0]: 879 * 0000b: No pixel repetition (pixel sent only once) 880 * 0001b: Pixel sent two times (pixel repeated once) 881 */ 882 hdmi->hdmi_data.pix_repet_factor = 883 (mode->flags & DRM_MODE_FLAG_DBLCLK) ? 1 : 0; 884 hdmi->hdmi_data.video_mode.mdataenablepolarity = true; 885 886 /* HDMI Initialization Step B.2 */ 887 ret = hdmi->phy.ops->init(hdmi->rk_hdmi, state); 888 if (ret) 889 return ret; 890 hdmi->phy.enabled = true; 891 892 rk3588_set_grf_cfg(hdmi->rk_hdmi); 893 link_cfg = dw_hdmi_rockchip_get_link_cfg(hdmi->rk_hdmi); 894 895 /* not for DVI mode */ 896 if (hdmi->sink_is_hdmi) { 897 printf("%s HDMI mode\n", __func__); 898 hdmi_modb(hdmi, 0, OPMODE_DVI, LINK_CONFIG0); 899 hdmi_modb(hdmi, HDCP2_BYPASS, HDCP2_BYPASS, HDCP2LOGIC_CONFIG0); 900 if (!link_cfg->frl_mode) { 901 if (vmode->mtmdsclock > HDMI14_MAX_TMDSCLK) { 902 drm_scdc_readb(&hdmi->adap, SCDC_SINK_VERSION, &bytes); 903 drm_scdc_writeb(&hdmi->adap, SCDC_SOURCE_VERSION, 904 min_t(u8, bytes, SCDC_MIN_SOURCE_VERSION)); 905 drm_scdc_set_high_tmds_clock_ratio(&hdmi->adap, 1); 906 drm_scdc_set_scrambling(&hdmi->adap, 1); 907 hdmi_writel(hdmi, 1, SCRAMB_CONFIG0); 908 } else { 909 if (hdmi_info->scdc.supported) { 910 drm_scdc_set_high_tmds_clock_ratio(&hdmi->adap, 0); 911 drm_scdc_set_scrambling(&hdmi->adap, 0); 912 } 913 hdmi_writel(hdmi, 0, SCRAMB_CONFIG0); 914 } 915 } 916 /* HDMI Initialization Step F - Configure AVI InfoFrame */ 917 hdmi_config_AVI(hdmi, mode); 918 hdmi_config_CVTEM(hdmi, link_cfg); 919 hdmi_set_op_mode(hdmi, link_cfg, hdmi_info->scdc.supported); 920 } else { 921 hdmi_modb(hdmi, OPMODE_DVI, OPMODE_DVI, LINK_CONFIG0); 922 printf("%s DVI mode\n", __func__); 923 } 924 925 return 0; 926 } 927 928 int dw_hdmi_detect_hotplug(struct dw_hdmi_qp *hdmi, 929 struct display_state *state) 930 { 931 struct connector_state *conn_state = &state->conn_state; 932 int ret; 933 934 ret = hdmi->phy.ops->read_hpd(hdmi->rk_hdmi); 935 if (ret || state->force_output) { 936 if (!hdmi->id) 937 conn_state->output_if |= VOP_OUTPUT_IF_HDMI0; 938 else 939 conn_state->output_if |= VOP_OUTPUT_IF_HDMI1; 940 } 941 942 return ret; 943 } 944 945 int rockchip_dw_hdmi_qp_pre_init(struct display_state *state) 946 { 947 struct connector_state *conn_state = &state->conn_state; 948 949 conn_state->type = DRM_MODE_CONNECTOR_HDMIA; 950 951 return 0; 952 } 953 954 int rockchip_dw_hdmi_qp_init(struct display_state *state) 955 { 956 struct connector_state *conn_state = &state->conn_state; 957 const struct rockchip_connector *connector = conn_state->connector; 958 const struct dw_hdmi_plat_data *pdata = connector->data; 959 void *rk_hdmi = dev_get_priv(conn_state->dev); 960 struct dw_hdmi_qp *hdmi; 961 struct drm_display_mode *mode_buf; 962 ofnode hdmi_node = conn_state->node; 963 struct device_node *ddc_node; 964 965 hdmi = malloc(sizeof(struct dw_hdmi_qp)); 966 if (!hdmi) 967 return -ENOMEM; 968 969 memset(hdmi, 0, sizeof(struct dw_hdmi_qp)); 970 mode_buf = malloc(MODE_LEN * sizeof(struct drm_display_mode)); 971 if (!mode_buf) 972 return -ENOMEM; 973 974 hdmi->rk_hdmi = rk_hdmi; 975 hdmi->id = of_alias_get_id(ofnode_to_np(hdmi_node), "hdmi"); 976 if (hdmi->id < 0) 977 hdmi->id = 0; 978 conn_state->disp_info = rockchip_get_disp_info(conn_state->type, hdmi->id); 979 980 memset(mode_buf, 0, MODE_LEN * sizeof(struct drm_display_mode)); 981 982 hdmi->regs = dev_read_addr_ptr(conn_state->dev); 983 984 ddc_node = of_parse_phandle(ofnode_to_np(hdmi_node), "ddc-i2c-bus", 0); 985 if (ddc_node) { 986 uclass_get_device_by_ofnode(UCLASS_I2C, np_to_ofnode(ddc_node), 987 &hdmi->adap.i2c_bus); 988 if (hdmi->adap.i2c_bus) 989 hdmi->adap.ops = i2c_get_ops(hdmi->adap.i2c_bus); 990 } 991 992 hdmi->i2c = malloc(sizeof(struct dw_hdmi_i2c)); 993 if (!hdmi->i2c) 994 return -ENOMEM; 995 hdmi->adap.ddc_xfer = dw_hdmi_i2c_xfer; 996 997 /* 998 * Read high and low time from device tree. If not available use 999 * the default timing scl clock rate is about 99.6KHz. 1000 */ 1001 hdmi->i2c->scl_high_ns = 1002 ofnode_read_s32_default(hdmi_node, 1003 "ddc-i2c-scl-high-time-ns", 4708); 1004 hdmi->i2c->scl_low_ns = 1005 ofnode_read_s32_default(hdmi_node, 1006 "ddc-i2c-scl-low-time-ns", 4916); 1007 1008 dw_hdmi_i2c_init(hdmi); 1009 conn_state->output_mode = ROCKCHIP_OUT_MODE_AAAA; 1010 1011 hdmi->dev_type = pdata->dev_type; 1012 hdmi->plat_data = pdata; 1013 hdmi->edid_data.mode_buf = mode_buf; 1014 1015 conn_state->private = hdmi; 1016 1017 dw_hdmi_detect_phy(hdmi); 1018 hdmi_writel(hdmi, 0, MAINUNIT_0_INT_MASK_N); 1019 hdmi_writel(hdmi, 0, MAINUNIT_1_INT_MASK_N); 1020 hdmi_writel(hdmi, 428571429, TIMER_BASE_CONFIG0); 1021 1022 dw_hdmi_qp_set_iomux(hdmi->rk_hdmi); 1023 1024 return 0; 1025 } 1026 1027 void rockchip_dw_hdmi_qp_deinit(struct display_state *state) 1028 { 1029 struct connector_state *conn_state = &state->conn_state; 1030 struct dw_hdmi_qp *hdmi = conn_state->private; 1031 1032 if (hdmi->i2c) 1033 free(hdmi->i2c); 1034 if (hdmi->edid_data.mode_buf) 1035 free(hdmi->edid_data.mode_buf); 1036 if (hdmi) 1037 free(hdmi); 1038 } 1039 1040 int rockchip_dw_hdmi_qp_prepare(struct display_state *state) 1041 { 1042 return 0; 1043 } 1044 1045 static void dw_hdmi_disable(struct dw_hdmi_qp *hdmi, struct display_state *state) 1046 { 1047 if (hdmi->phy.enabled) { 1048 hdmi->phy.ops->disable(hdmi->rk_hdmi, state); 1049 hdmi->phy.enabled = false; 1050 } 1051 } 1052 1053 int rockchip_dw_hdmi_qp_enable(struct display_state *state) 1054 { 1055 struct connector_state *conn_state = &state->conn_state; 1056 struct drm_display_mode *mode = &conn_state->mode; 1057 struct dw_hdmi_qp *hdmi = conn_state->private; 1058 1059 if (!hdmi) 1060 return -EFAULT; 1061 1062 /* Store the display mode for plugin/DKMS poweron events */ 1063 memcpy(&hdmi->previous_mode, mode, sizeof(hdmi->previous_mode)); 1064 1065 dw_hdmi_setup(hdmi, mode, state); 1066 1067 return 0; 1068 } 1069 1070 int rockchip_dw_hdmi_qp_disable(struct display_state *state) 1071 { 1072 struct connector_state *conn_state = &state->conn_state; 1073 struct dw_hdmi_qp *hdmi = conn_state->private; 1074 1075 dw_hdmi_disable(hdmi, state); 1076 return 0; 1077 } 1078 1079 int rockchip_dw_hdmi_qp_get_timing(struct display_state *state) 1080 { 1081 int ret, i; 1082 struct connector_state *conn_state = &state->conn_state; 1083 struct drm_display_mode *mode = &conn_state->mode; 1084 struct dw_hdmi_qp *hdmi = conn_state->private; 1085 struct edid *edid = (struct edid *)conn_state->edid; 1086 unsigned int bus_format; 1087 unsigned long enc_out_encoding; 1088 struct overscan *overscan = &conn_state->overscan; 1089 const u8 def_modes_vic[6] = {4, 16, 2, 17, 31, 19}; 1090 1091 if (!hdmi) 1092 return -EFAULT; 1093 1094 ret = drm_do_get_edid(&hdmi->adap, conn_state->edid); 1095 if (!ret) { 1096 hdmi->sink_is_hdmi = 1097 drm_detect_hdmi_monitor(edid); 1098 hdmi->sink_has_audio = drm_detect_monitor_audio(edid); 1099 ret = drm_add_edid_modes(&hdmi->edid_data, conn_state->edid); 1100 } 1101 if (ret < 0) { 1102 hdmi->sink_is_hdmi = true; 1103 hdmi->sink_has_audio = true; 1104 do_cea_modes(&hdmi->edid_data, def_modes_vic, 1105 sizeof(def_modes_vic)); 1106 hdmi->edid_data.preferred_mode = &hdmi->edid_data.mode_buf[0]; 1107 printf("failed to get edid\n"); 1108 } 1109 drm_rk_filter_whitelist(&hdmi->edid_data); 1110 if (hdmi->phy.ops->mode_valid) 1111 hdmi->phy.ops->mode_valid(hdmi->rk_hdmi, state); 1112 drm_mode_max_resolution_filter(&hdmi->edid_data, 1113 &state->crtc_state.max_output); 1114 if (!drm_mode_prune_invalid(&hdmi->edid_data)) { 1115 printf("can't find valid hdmi mode\n"); 1116 return -EINVAL; 1117 } 1118 1119 for (i = 0; i < hdmi->edid_data.modes; i++) 1120 hdmi->edid_data.mode_buf[i].vrefresh = 1121 drm_mode_vrefresh(&hdmi->edid_data.mode_buf[i]); 1122 1123 drm_mode_sort(&hdmi->edid_data); 1124 dw_hdmi_qp_selete_output(&hdmi->edid_data, conn_state, &bus_format, 1125 overscan, hdmi->dev_type, 1126 hdmi->output_bus_format_rgb, hdmi->rk_hdmi, 1127 state); 1128 1129 *mode = *hdmi->edid_data.preferred_mode; 1130 hdmi->vic = drm_match_cea_mode(mode); 1131 1132 printf("mode:%dx%d bus_format:0x%x\n", mode->hdisplay, mode->vdisplay, bus_format); 1133 conn_state->bus_format = bus_format; 1134 hdmi->hdmi_data.enc_in_bus_format = bus_format; 1135 hdmi->hdmi_data.enc_out_bus_format = bus_format; 1136 1137 switch (bus_format) { 1138 case MEDIA_BUS_FMT_UYVY10_1X20: 1139 conn_state->bus_format = MEDIA_BUS_FMT_YUV10_1X30; 1140 hdmi->hdmi_data.enc_in_bus_format = 1141 MEDIA_BUS_FMT_YUV10_1X30; 1142 break; 1143 case MEDIA_BUS_FMT_UYVY8_1X16: 1144 conn_state->bus_format = MEDIA_BUS_FMT_YUV8_1X24; 1145 hdmi->hdmi_data.enc_in_bus_format = 1146 MEDIA_BUS_FMT_YUV8_1X24; 1147 break; 1148 case MEDIA_BUS_FMT_UYYVYY8_0_5X24: 1149 case MEDIA_BUS_FMT_UYYVYY10_0_5X30: 1150 conn_state->output_mode = ROCKCHIP_OUT_MODE_YUV420; 1151 break; 1152 } 1153 1154 if (hdmi->vic == 6 || hdmi->vic == 7 || hdmi->vic == 21 || 1155 hdmi->vic == 22 || hdmi->vic == 2 || hdmi->vic == 3 || 1156 hdmi->vic == 17 || hdmi->vic == 18) 1157 enc_out_encoding = V4L2_YCBCR_ENC_601; 1158 else 1159 enc_out_encoding = V4L2_YCBCR_ENC_709; 1160 1161 if (enc_out_encoding == V4L2_YCBCR_ENC_BT2020) 1162 conn_state->color_space = V4L2_COLORSPACE_BT2020; 1163 else if (bus_format == MEDIA_BUS_FMT_RGB888_1X24 || 1164 bus_format == MEDIA_BUS_FMT_RGB101010_1X30) 1165 conn_state->color_space = V4L2_COLORSPACE_DEFAULT; 1166 else if (enc_out_encoding == V4L2_YCBCR_ENC_709) 1167 conn_state->color_space = V4L2_COLORSPACE_REC709; 1168 else 1169 conn_state->color_space = V4L2_COLORSPACE_SMPTE170M; 1170 1171 return 0; 1172 } 1173 1174 int rockchip_dw_hdmi_qp_detect(struct display_state *state) 1175 { 1176 int ret; 1177 struct connector_state *conn_state = &state->conn_state; 1178 struct dw_hdmi_qp *hdmi = conn_state->private; 1179 1180 if (!hdmi) 1181 return -EFAULT; 1182 1183 ret = dw_hdmi_detect_hotplug(hdmi, state); 1184 1185 return ret; 1186 } 1187 1188 int rockchip_dw_hdmi_qp_get_edid(struct display_state *state) 1189 { 1190 int ret; 1191 struct connector_state *conn_state = &state->conn_state; 1192 struct dw_hdmi_qp *hdmi = conn_state->private; 1193 1194 ret = drm_do_get_edid(&hdmi->adap, conn_state->edid); 1195 1196 return ret; 1197 } 1198