1 /* 2 * (C) Copyright 2008-2017 Fuzhou Rockchip Electronics Co., Ltd 3 * 4 * SPDX-License-Identifier: GPL-2.0+ 5 */ 6 7 #include <config.h> 8 #include <common.h> 9 #include <errno.h> 10 #include <malloc.h> 11 #include <asm/unaligned.h> 12 #include <asm/io.h> 13 #include <dm/device.h> 14 #include <dm/of_access.h> 15 #include <dm/read.h> 16 #include <linux/bitfield.h> 17 #include <linux/list.h> 18 #include <syscon.h> 19 #include <asm/arch-rockchip/clock.h> 20 #include <asm/gpio.h> 21 22 #include "rockchip_display.h" 23 #include "rockchip_crtc.h" 24 #include "rockchip_connector.h" 25 #include "analogix_dp.h" 26 27 #define RK3588_GRF_VO1_CON0 0x0000 28 #define EDP_MODE BIT(0) 29 #define RK3588_GRF_VO1_CON1 0x0004 30 31 /** 32 * struct rockchip_dp_chip_data - splite the grf setting of kind of chips 33 * @lcdsel_grf_reg: grf register offset of lcdc select 34 * @lcdsel_big: reg value of selecting vop big for eDP 35 * @lcdsel_lit: reg value of selecting vop little for eDP 36 * @chip_type: specific chip type 37 * @ssc: check if SSC is supported by source 38 */ 39 struct rockchip_dp_chip_data { 40 u32 lcdsel_grf_reg; 41 u32 lcdsel_big; 42 u32 lcdsel_lit; 43 u32 chip_type; 44 bool ssc; 45 46 u32 max_link_rate; 47 u32 max_lane_count; 48 }; 49 50 static void 51 analogix_dp_enable_rx_to_enhanced_mode(struct analogix_dp_device *dp, 52 bool enable) 53 { 54 u8 data; 55 56 analogix_dp_read_byte_from_dpcd(dp, DP_LANE_COUNT_SET, &data); 57 58 if (enable) 59 analogix_dp_write_byte_to_dpcd(dp, DP_LANE_COUNT_SET, 60 DP_LANE_COUNT_ENHANCED_FRAME_EN | 61 DPCD_LANE_COUNT_SET(data)); 62 else 63 analogix_dp_write_byte_to_dpcd(dp, DP_LANE_COUNT_SET, 64 DPCD_LANE_COUNT_SET(data)); 65 } 66 67 static int analogix_dp_is_enhanced_mode_available(struct analogix_dp_device *dp) 68 { 69 u8 data; 70 int retval; 71 72 analogix_dp_read_byte_from_dpcd(dp, DP_MAX_LANE_COUNT, &data); 73 retval = DPCD_ENHANCED_FRAME_CAP(data); 74 75 return retval; 76 } 77 78 static void analogix_dp_set_enhanced_mode(struct analogix_dp_device *dp) 79 { 80 u8 data; 81 82 data = analogix_dp_is_enhanced_mode_available(dp); 83 analogix_dp_enable_rx_to_enhanced_mode(dp, data); 84 analogix_dp_enable_enhanced_mode(dp, data); 85 } 86 87 static void analogix_dp_training_pattern_dis(struct analogix_dp_device *dp) 88 { 89 analogix_dp_set_training_pattern(dp, DP_NONE); 90 91 analogix_dp_write_byte_to_dpcd(dp, DP_TRAINING_PATTERN_SET, 92 DP_TRAINING_PATTERN_DISABLE); 93 } 94 95 static int analogix_dp_link_start(struct analogix_dp_device *dp) 96 { 97 u8 buf[4]; 98 int lane, lane_count, retval; 99 100 lane_count = dp->link_train.lane_count; 101 102 dp->link_train.lt_state = CLOCK_RECOVERY; 103 dp->link_train.eq_loop = 0; 104 105 for (lane = 0; lane < lane_count; lane++) 106 dp->link_train.cr_loop[lane] = 0; 107 108 /* Set link rate and count as you want to establish*/ 109 analogix_dp_set_link_bandwidth(dp, dp->link_train.link_rate); 110 analogix_dp_set_lane_count(dp, dp->link_train.lane_count); 111 112 /* Setup RX configuration */ 113 buf[0] = dp->link_train.link_rate; 114 buf[1] = dp->link_train.lane_count; 115 retval = analogix_dp_write_bytes_to_dpcd(dp, DP_LINK_BW_SET, 2, buf); 116 if (retval) 117 return retval; 118 119 /* Spread AMP if required, enable 8b/10b coding */ 120 buf[0] = analogix_dp_ssc_supported(dp) ? DP_SPREAD_AMP_0_5 : 0; 121 buf[1] = DP_SET_ANSI_8B10B; 122 retval = analogix_dp_write_bytes_to_dpcd(dp, DP_DOWNSPREAD_CTRL, 123 2, buf); 124 if (retval < 0) 125 return retval; 126 127 /* Set TX voltage-swing and pre-emphasis to minimum */ 128 for (lane = 0; lane < lane_count; lane++) 129 dp->link_train.training_lane[lane] = 130 DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | 131 DP_TRAIN_PRE_EMPH_LEVEL_0; 132 analogix_dp_set_lane_link_training(dp); 133 134 /* Set training pattern 1 */ 135 analogix_dp_set_training_pattern(dp, TRAINING_PTN1); 136 137 /* Set RX training pattern */ 138 retval = analogix_dp_write_byte_to_dpcd(dp, 139 DP_TRAINING_PATTERN_SET, 140 DP_LINK_SCRAMBLING_DISABLE | DP_TRAINING_PATTERN_1); 141 if (retval) 142 return retval; 143 144 for (lane = 0; lane < lane_count; lane++) 145 buf[lane] = DP_TRAIN_PRE_EMPH_LEVEL_0 | 146 DP_TRAIN_VOLTAGE_SWING_LEVEL_0; 147 148 retval = analogix_dp_write_bytes_to_dpcd(dp, DP_TRAINING_LANE0_SET, 149 lane_count, buf); 150 151 return retval; 152 } 153 154 static unsigned char analogix_dp_get_lane_status(u8 link_status[2], int lane) 155 { 156 int shift = (lane & 1) * 4; 157 u8 link_value = link_status[lane >> 1]; 158 159 return (link_value >> shift) & 0xf; 160 } 161 162 static int analogix_dp_clock_recovery_ok(u8 link_status[2], int lane_count) 163 { 164 int lane; 165 u8 lane_status; 166 167 for (lane = 0; lane < lane_count; lane++) { 168 lane_status = analogix_dp_get_lane_status(link_status, lane); 169 if ((lane_status & DP_LANE_CR_DONE) == 0) 170 return -EINVAL; 171 } 172 return 0; 173 } 174 175 static int analogix_dp_channel_eq_ok(u8 link_status[2], u8 link_align, 176 int lane_count) 177 { 178 int lane; 179 u8 lane_status; 180 181 if ((link_align & DP_INTERLANE_ALIGN_DONE) == 0) 182 return -EINVAL; 183 184 for (lane = 0; lane < lane_count; lane++) { 185 lane_status = analogix_dp_get_lane_status(link_status, lane); 186 lane_status &= DP_CHANNEL_EQ_BITS; 187 if (lane_status != DP_CHANNEL_EQ_BITS) 188 return -EINVAL; 189 } 190 191 return 0; 192 } 193 194 static unsigned char 195 analogix_dp_get_adjust_request_voltage(u8 adjust_request[2], int lane) 196 { 197 int shift = (lane & 1) * 4; 198 u8 link_value = adjust_request[lane >> 1]; 199 200 return (link_value >> shift) & 0x3; 201 } 202 203 static unsigned char analogix_dp_get_adjust_request_pre_emphasis( 204 u8 adjust_request[2], 205 int lane) 206 { 207 int shift = (lane & 1) * 4; 208 u8 link_value = adjust_request[lane >> 1]; 209 210 return ((link_value >> shift) & 0xc) >> 2; 211 } 212 213 static void analogix_dp_reduce_link_rate(struct analogix_dp_device *dp) 214 { 215 analogix_dp_training_pattern_dis(dp); 216 analogix_dp_set_enhanced_mode(dp); 217 218 dp->link_train.lt_state = FAILED; 219 } 220 221 static void analogix_dp_get_adjust_training_lane(struct analogix_dp_device *dp, 222 u8 adjust_request[2]) 223 { 224 int lane, lane_count; 225 u8 voltage_swing, pre_emphasis, training_lane; 226 227 lane_count = dp->link_train.lane_count; 228 for (lane = 0; lane < lane_count; lane++) { 229 voltage_swing = analogix_dp_get_adjust_request_voltage( 230 adjust_request, lane); 231 pre_emphasis = analogix_dp_get_adjust_request_pre_emphasis( 232 adjust_request, lane); 233 training_lane = DPCD_VOLTAGE_SWING_SET(voltage_swing) | 234 DPCD_PRE_EMPHASIS_SET(pre_emphasis); 235 236 if (voltage_swing == VOLTAGE_LEVEL_3) 237 training_lane |= DP_TRAIN_MAX_SWING_REACHED; 238 if (pre_emphasis == PRE_EMPHASIS_LEVEL_3) 239 training_lane |= DP_TRAIN_MAX_PRE_EMPHASIS_REACHED; 240 241 dp->link_train.training_lane[lane] = training_lane; 242 } 243 } 244 245 static bool analogix_dp_tps3_supported(struct analogix_dp_device *dp) 246 { 247 bool source_tps3_supported, sink_tps3_supported; 248 u8 dpcd = 0; 249 250 source_tps3_supported = 251 dp->video_info.max_link_rate == DP_LINK_BW_5_4; 252 analogix_dp_read_byte_from_dpcd(dp, DP_MAX_LANE_COUNT, &dpcd); 253 sink_tps3_supported = dpcd & DP_TPS3_SUPPORTED; 254 255 return source_tps3_supported && sink_tps3_supported; 256 } 257 258 static int analogix_dp_process_clock_recovery(struct analogix_dp_device *dp) 259 { 260 int lane, lane_count, retval; 261 u8 voltage_swing, pre_emphasis, training_lane; 262 u8 link_status[2], adjust_request[2]; 263 u8 training_pattern = TRAINING_PTN2; 264 265 drm_dp_link_train_clock_recovery_delay(dp->dpcd); 266 267 lane_count = dp->link_train.lane_count; 268 269 retval = analogix_dp_read_bytes_from_dpcd(dp, 270 DP_LANE0_1_STATUS, 2, link_status); 271 if (retval) 272 return retval; 273 274 if (analogix_dp_clock_recovery_ok(link_status, lane_count) == 0) { 275 if (analogix_dp_tps3_supported(dp)) 276 training_pattern = TRAINING_PTN3; 277 278 /* set training pattern for EQ */ 279 analogix_dp_set_training_pattern(dp, training_pattern); 280 281 retval = analogix_dp_write_byte_to_dpcd(dp, 282 DP_TRAINING_PATTERN_SET, 283 (training_pattern == TRAINING_PTN3 ? 284 DP_TRAINING_PATTERN_3 : DP_TRAINING_PATTERN_2)); 285 if (retval) 286 return retval; 287 288 dev_info(dp->dev, "Link Training Clock Recovery success\n"); 289 dp->link_train.lt_state = EQUALIZER_TRAINING; 290 291 return 0; 292 } else { 293 retval = analogix_dp_read_bytes_from_dpcd(dp, 294 DP_ADJUST_REQUEST_LANE0_1, 2, adjust_request); 295 if (retval) 296 return retval; 297 298 for (lane = 0; lane < lane_count; lane++) { 299 training_lane = analogix_dp_get_lane_link_training( 300 dp, lane); 301 voltage_swing = analogix_dp_get_adjust_request_voltage( 302 adjust_request, lane); 303 pre_emphasis = analogix_dp_get_adjust_request_pre_emphasis( 304 adjust_request, lane); 305 306 if (DPCD_VOLTAGE_SWING_GET(training_lane) == 307 voltage_swing && 308 DPCD_PRE_EMPHASIS_GET(training_lane) == 309 pre_emphasis) 310 dp->link_train.cr_loop[lane]++; 311 312 if (dp->link_train.cr_loop[lane] == MAX_CR_LOOP || 313 voltage_swing == VOLTAGE_LEVEL_3 || 314 pre_emphasis == PRE_EMPHASIS_LEVEL_3) { 315 dev_err(dp->dev, "CR Max reached (%d,%d,%d)\n", 316 dp->link_train.cr_loop[lane], 317 voltage_swing, pre_emphasis); 318 analogix_dp_reduce_link_rate(dp); 319 return -EIO; 320 } 321 } 322 } 323 324 analogix_dp_get_adjust_training_lane(dp, adjust_request); 325 analogix_dp_set_lane_link_training(dp); 326 327 retval = analogix_dp_write_bytes_to_dpcd(dp, 328 DP_TRAINING_LANE0_SET, lane_count, 329 dp->link_train.training_lane); 330 if (retval) 331 return retval; 332 333 return retval; 334 } 335 336 static int analogix_dp_process_equalizer_training(struct analogix_dp_device *dp) 337 { 338 int lane_count, retval; 339 u32 reg; 340 u8 link_align, link_status[2], adjust_request[2]; 341 342 drm_dp_link_train_channel_eq_delay(dp->dpcd); 343 344 lane_count = dp->link_train.lane_count; 345 346 retval = analogix_dp_read_bytes_from_dpcd(dp, 347 DP_LANE0_1_STATUS, 2, link_status); 348 if (retval) 349 return retval; 350 351 if (analogix_dp_clock_recovery_ok(link_status, lane_count)) { 352 analogix_dp_reduce_link_rate(dp); 353 return -EIO; 354 } 355 356 retval = analogix_dp_read_byte_from_dpcd(dp, 357 DP_LANE_ALIGN_STATUS_UPDATED, &link_align); 358 if (retval) 359 return retval; 360 361 if (!analogix_dp_channel_eq_ok(link_status, link_align, lane_count)) { 362 /* traing pattern Set to Normal */ 363 analogix_dp_training_pattern_dis(dp); 364 365 printf("Link Training success!\n"); 366 367 analogix_dp_get_link_bandwidth(dp, ®); 368 dp->link_train.link_rate = reg; 369 analogix_dp_get_lane_count(dp, ®); 370 dp->link_train.lane_count = reg; 371 372 printf("final link rate = 0x%.2x, lane count = 0x%.2x\n", 373 dp->link_train.link_rate, dp->link_train.lane_count); 374 375 /* set enhanced mode if available */ 376 analogix_dp_set_enhanced_mode(dp); 377 dp->link_train.lt_state = FINISHED; 378 379 return 0; 380 } 381 382 /* not all locked */ 383 dp->link_train.eq_loop++; 384 385 if (dp->link_train.eq_loop > MAX_EQ_LOOP) { 386 dev_dbg(dp->dev, "EQ Max loop\n"); 387 analogix_dp_reduce_link_rate(dp); 388 return -EIO; 389 } 390 391 retval = analogix_dp_read_bytes_from_dpcd(dp, 392 DP_ADJUST_REQUEST_LANE0_1, 2, adjust_request); 393 if (retval) 394 return retval; 395 396 analogix_dp_get_adjust_training_lane(dp, adjust_request); 397 analogix_dp_set_lane_link_training(dp); 398 399 retval = analogix_dp_write_bytes_to_dpcd(dp, DP_TRAINING_LANE0_SET, 400 lane_count, dp->link_train.training_lane); 401 402 return retval; 403 } 404 405 static void analogix_dp_get_max_rx_bandwidth(struct analogix_dp_device *dp, 406 u8 *bandwidth) 407 { 408 u8 data; 409 410 /* 411 * For DP rev.1.1, Maximum link rate of Main Link lanes 412 * 0x06 = 1.62 Gbps, 0x0a = 2.7 Gbps 413 * For DP rev.1.2, Maximum link rate of Main Link lanes 414 * 0x06 = 1.62 Gbps, 0x0a = 2.7 Gbps, 0x14 = 5.4Gbps 415 */ 416 analogix_dp_read_byte_from_dpcd(dp, DP_MAX_LINK_RATE, &data); 417 *bandwidth = data; 418 } 419 420 static void analogix_dp_get_max_rx_lane_count(struct analogix_dp_device *dp, 421 u8 *lane_count) 422 { 423 u8 data; 424 425 /* 426 * For DP rev.1.1, Maximum number of Main Link lanes 427 * 0x01 = 1 lane, 0x02 = 2 lanes, 0x04 = 4 lanes 428 */ 429 analogix_dp_read_byte_from_dpcd(dp, DP_MAX_LANE_COUNT, &data); 430 *lane_count = DPCD_MAX_LANE_COUNT(data); 431 } 432 433 static int analogix_dp_init_training(struct analogix_dp_device *dp, 434 enum link_lane_count_type max_lane, 435 int max_rate) 436 { 437 u8 dpcd; 438 439 /* 440 * MACRO_RST must be applied after the PLL_LOCK to avoid 441 * the DP inter pair skew issue for at least 10 us 442 */ 443 analogix_dp_reset_macro(dp); 444 445 /* Initialize by reading RX's DPCD */ 446 analogix_dp_get_max_rx_bandwidth(dp, &dp->link_train.link_rate); 447 analogix_dp_get_max_rx_lane_count(dp, &dp->link_train.lane_count); 448 449 /* Setup TX lane count & rate */ 450 dp->link_train.lane_count = min_t(u8, dp->link_train.lane_count, 451 max_lane); 452 dp->link_train.link_rate = min_t(u32, dp->link_train.link_rate, 453 max_rate); 454 455 analogix_dp_read_byte_from_dpcd(dp, DP_MAX_DOWNSPREAD, &dpcd); 456 dp->link_train.ssc = !!(dpcd & DP_MAX_DOWNSPREAD_0_5); 457 458 /* All DP analog module power up */ 459 analogix_dp_set_analog_power_down(dp, POWER_ALL, 0); 460 461 return 0; 462 } 463 464 static int analogix_dp_sw_link_training(struct analogix_dp_device *dp) 465 { 466 int retval = 0, training_finished = 0; 467 468 dp->link_train.lt_state = START; 469 470 /* Process here */ 471 while (!retval && !training_finished) { 472 switch (dp->link_train.lt_state) { 473 case START: 474 retval = analogix_dp_link_start(dp); 475 if (retval) 476 dev_err(dp->dev, "LT link start failed!\n"); 477 break; 478 case CLOCK_RECOVERY: 479 retval = analogix_dp_process_clock_recovery(dp); 480 if (retval) 481 dev_err(dp->dev, "LT CR failed!\n"); 482 break; 483 case EQUALIZER_TRAINING: 484 retval = analogix_dp_process_equalizer_training(dp); 485 if (retval) 486 dev_err(dp->dev, "LT EQ failed!\n"); 487 break; 488 case FINISHED: 489 training_finished = 1; 490 break; 491 case FAILED: 492 return -EREMOTEIO; 493 } 494 } 495 496 return retval; 497 } 498 499 static int analogix_dp_set_link_train(struct analogix_dp_device *dp, 500 u32 count, u32 bwtype) 501 { 502 int i, ret; 503 504 for (i = 0; i < 5; i++) { 505 ret = analogix_dp_init_training(dp, count, bwtype); 506 if (ret < 0) { 507 dev_err(dp->dev, "failed to init training\n"); 508 return ret; 509 } 510 511 ret = analogix_dp_sw_link_training(dp); 512 if (!ret) 513 break; 514 } 515 516 return ret; 517 } 518 519 static int analogix_dp_config_video(struct analogix_dp_device *dp) 520 { 521 int timeout_loop = 0; 522 int done_count = 0; 523 524 analogix_dp_config_video_slave_mode(dp); 525 526 analogix_dp_set_video_color_format(dp); 527 528 if (analogix_dp_get_pll_lock_status(dp) == PLL_UNLOCKED) { 529 dev_err(dp->dev, "PLL is not locked yet.\n"); 530 return -EINVAL; 531 } 532 533 for (;;) { 534 timeout_loop++; 535 if (analogix_dp_is_slave_video_stream_clock_on(dp) == 0) 536 break; 537 if (timeout_loop > DP_TIMEOUT_LOOP_COUNT) { 538 dev_err(dp->dev, "Timeout of video streamclk ok\n"); 539 return -ETIMEDOUT; 540 } 541 542 udelay(2); 543 } 544 545 /* Set to use the register calculated M/N video */ 546 analogix_dp_set_video_cr_mn(dp, CALCULATED_M, 0, 0); 547 548 /* For video bist, Video timing must be generated by register */ 549 analogix_dp_set_video_timing_mode(dp, VIDEO_TIMING_FROM_REGISTER); 550 551 /* Disable video mute */ 552 analogix_dp_enable_video_mute(dp, 0); 553 554 /* Configure video slave mode */ 555 analogix_dp_enable_video_master(dp, 0); 556 557 /* Enable video input */ 558 analogix_dp_start_video(dp); 559 560 timeout_loop = 0; 561 562 for (;;) { 563 timeout_loop++; 564 if (analogix_dp_is_video_stream_on(dp) == 0) { 565 done_count++; 566 if (done_count > 10) 567 break; 568 } else if (done_count) { 569 done_count = 0; 570 } 571 if (timeout_loop > DP_TIMEOUT_LOOP_COUNT) { 572 dev_err(dp->dev, "Timeout of video streamclk ok\n"); 573 return -ETIMEDOUT; 574 } 575 576 udelay(1001); 577 } 578 579 return 0; 580 } 581 582 static void analogix_dp_enable_scramble(struct analogix_dp_device *dp, 583 bool enable) 584 { 585 u8 data; 586 587 if (enable) { 588 analogix_dp_enable_scrambling(dp); 589 590 analogix_dp_read_byte_from_dpcd(dp, DP_TRAINING_PATTERN_SET, 591 &data); 592 analogix_dp_write_byte_to_dpcd(dp, 593 DP_TRAINING_PATTERN_SET, 594 (u8)(data & ~DP_LINK_SCRAMBLING_DISABLE)); 595 } else { 596 analogix_dp_disable_scrambling(dp); 597 598 analogix_dp_read_byte_from_dpcd(dp, DP_TRAINING_PATTERN_SET, 599 &data); 600 analogix_dp_write_byte_to_dpcd(dp, 601 DP_TRAINING_PATTERN_SET, 602 (u8)(data | DP_LINK_SCRAMBLING_DISABLE)); 603 } 604 } 605 606 static void analogix_dp_init_dp(struct analogix_dp_device *dp) 607 { 608 analogix_dp_reset(dp); 609 610 analogix_dp_swreset(dp); 611 612 analogix_dp_init_analog_param(dp); 613 analogix_dp_init_interrupt(dp); 614 615 /* SW defined function Normal operation */ 616 analogix_dp_enable_sw_function(dp); 617 618 analogix_dp_config_interrupt(dp); 619 analogix_dp_init_analog_func(dp); 620 621 analogix_dp_init_hpd(dp); 622 analogix_dp_init_aux(dp); 623 } 624 625 static unsigned char analogix_dp_calc_edid_check_sum(unsigned char *edid_data) 626 { 627 int i; 628 unsigned char sum = 0; 629 630 for (i = 0; i < EDID_BLOCK_LENGTH; i++) 631 sum = sum + edid_data[i]; 632 633 return sum; 634 } 635 636 static int analogix_dp_read_edid(struct analogix_dp_device *dp) 637 { 638 unsigned char *edid = dp->edid; 639 unsigned int extend_block = 0; 640 unsigned char test_vector; 641 int retval; 642 643 /* 644 * EDID device address is 0x50. 645 * However, if necessary, you must have set upper address 646 * into E-EDID in I2C device, 0x30. 647 */ 648 649 /* Read Extension Flag, Number of 128-byte EDID extension blocks */ 650 retval = analogix_dp_read_byte_from_i2c(dp, I2C_EDID_DEVICE_ADDR, 651 EDID_EXTENSION_FLAG, 652 &extend_block); 653 if (retval) 654 return retval; 655 656 if (extend_block > 0) { 657 debug("EDID data includes a single extension!\n"); 658 659 /* Read EDID data */ 660 retval = analogix_dp_read_bytes_from_i2c(dp, 661 I2C_EDID_DEVICE_ADDR, 662 EDID_HEADER_PATTERN, 663 EDID_BLOCK_LENGTH, 664 &edid[EDID_HEADER_PATTERN]); 665 if (retval < 0) 666 return retval; 667 668 if (analogix_dp_calc_edid_check_sum(edid)) 669 return -EINVAL; 670 671 /* Read additional EDID data */ 672 retval = analogix_dp_read_bytes_from_i2c(dp, 673 I2C_EDID_DEVICE_ADDR, 674 EDID_BLOCK_LENGTH, 675 EDID_BLOCK_LENGTH, 676 &edid[EDID_BLOCK_LENGTH]); 677 if (retval < 0) 678 return retval; 679 680 if (analogix_dp_calc_edid_check_sum(&edid[EDID_BLOCK_LENGTH])) 681 return -EINVAL; 682 683 analogix_dp_read_byte_from_dpcd(dp, DP_TEST_REQUEST, 684 &test_vector); 685 if (test_vector & DP_TEST_LINK_EDID_READ) { 686 analogix_dp_write_byte_to_dpcd(dp, 687 DP_TEST_EDID_CHECKSUM, 688 edid[EDID_BLOCK_LENGTH + EDID_CHECKSUM]); 689 analogix_dp_write_byte_to_dpcd(dp, 690 DP_TEST_RESPONSE, 691 DP_TEST_EDID_CHECKSUM_WRITE); 692 } 693 } else { 694 dev_info(dp->dev, 695 "EDID data does not include any extensions.\n"); 696 697 /* Read EDID data */ 698 retval = analogix_dp_read_bytes_from_i2c(dp, 699 I2C_EDID_DEVICE_ADDR, EDID_HEADER_PATTERN, 700 EDID_BLOCK_LENGTH, &edid[EDID_HEADER_PATTERN]); 701 if (retval < 0) 702 return retval; 703 704 if (analogix_dp_calc_edid_check_sum(edid)) 705 return -EINVAL; 706 707 analogix_dp_read_byte_from_dpcd(dp, DP_TEST_REQUEST, 708 &test_vector); 709 if (test_vector & DP_TEST_LINK_EDID_READ) { 710 analogix_dp_write_byte_to_dpcd(dp, 711 DP_TEST_EDID_CHECKSUM, edid[EDID_CHECKSUM]); 712 analogix_dp_write_byte_to_dpcd(dp, 713 DP_TEST_RESPONSE, DP_TEST_EDID_CHECKSUM_WRITE); 714 } 715 } 716 717 return 0; 718 } 719 720 static int analogix_dp_handle_edid(struct analogix_dp_device *dp) 721 { 722 u8 buf[12]; 723 int i, try = 5; 724 int retval; 725 726 retry: 727 /* Read DPCD DP_DPCD_REV~RECEIVE_PORT1_CAP_1 */ 728 retval = analogix_dp_read_bytes_from_dpcd(dp, DP_DPCD_REV, 12, buf); 729 730 if (retval && try--) { 731 mdelay(10); 732 goto retry; 733 } 734 735 if (retval) 736 return retval; 737 738 /* Read EDID */ 739 for (i = 0; i < 3; i++) { 740 retval = analogix_dp_read_edid(dp); 741 if (!retval) 742 break; 743 } 744 745 return retval; 746 } 747 748 static int analogix_dp_connector_init(struct rockchip_connector *conn, struct display_state *state) 749 { 750 struct connector_state *conn_state = &state->conn_state; 751 struct analogix_dp_device *dp = dev_get_priv(conn->dev); 752 753 conn_state->output_if |= dp->id ? VOP_OUTPUT_IF_eDP1 : VOP_OUTPUT_IF_eDP0; 754 conn_state->output_mode = ROCKCHIP_OUT_MODE_AAAA; 755 conn_state->color_space = V4L2_COLORSPACE_DEFAULT; 756 757 reset_assert_bulk(&dp->resets); 758 udelay(1); 759 reset_deassert_bulk(&dp->resets); 760 761 conn_state->disp_info = rockchip_get_disp_info(conn_state->type, dp->id); 762 generic_phy_set_mode(&dp->phy, PHY_MODE_DP); 763 generic_phy_power_on(&dp->phy); 764 analogix_dp_init_dp(dp); 765 766 return 0; 767 } 768 769 static int analogix_dp_connector_get_edid(struct rockchip_connector *conn, 770 struct display_state *state) 771 { 772 struct connector_state *conn_state = &state->conn_state; 773 struct analogix_dp_device *dp = dev_get_priv(conn->dev); 774 int ret; 775 776 ret = analogix_dp_handle_edid(dp); 777 if (ret) { 778 dev_err(dp->dev, "failed to get edid\n"); 779 return ret; 780 } 781 782 memcpy(&conn_state->edid, &dp->edid, sizeof(dp->edid)); 783 784 return 0; 785 } 786 787 static int analogix_dp_link_power_up(struct analogix_dp_device *dp) 788 { 789 u8 value; 790 int ret; 791 792 if (dp->dpcd[DP_DPCD_REV] < 0x11) 793 return 0; 794 795 ret = analogix_dp_read_byte_from_dpcd(dp, DP_SET_POWER, &value); 796 if (ret < 0) 797 return ret; 798 799 value &= ~DP_SET_POWER_MASK; 800 value |= DP_SET_POWER_D0; 801 802 ret = analogix_dp_write_byte_to_dpcd(dp, DP_SET_POWER, value); 803 if (ret < 0) 804 return ret; 805 806 mdelay(1); 807 808 return 0; 809 } 810 811 static int analogix_dp_link_power_down(struct analogix_dp_device *dp) 812 { 813 u8 value; 814 int ret; 815 816 if (dp->dpcd[DP_DPCD_REV] < 0x11) 817 return 0; 818 819 ret = analogix_dp_read_byte_from_dpcd(dp, DP_SET_POWER, &value); 820 if (ret < 0) 821 return ret; 822 823 value &= ~DP_SET_POWER_MASK; 824 value |= DP_SET_POWER_D3; 825 826 ret = analogix_dp_write_byte_to_dpcd(dp, DP_SET_POWER, value); 827 if (ret < 0) 828 return ret; 829 830 return 0; 831 } 832 833 static int analogix_dp_connector_enable(struct rockchip_connector *conn, 834 struct display_state *state) 835 { 836 struct connector_state *conn_state = &state->conn_state; 837 struct crtc_state *crtc_state = &state->crtc_state; 838 const struct rockchip_dp_chip_data *pdata = 839 (const struct rockchip_dp_chip_data *)dev_get_driver_data(conn->dev); 840 struct analogix_dp_device *dp = dev_get_priv(conn->dev); 841 struct video_info *video = &dp->video_info; 842 u32 val; 843 int ret; 844 845 if (pdata->lcdsel_grf_reg) { 846 if (crtc_state->crtc_id) 847 val = pdata->lcdsel_lit; 848 else 849 val = pdata->lcdsel_big; 850 851 regmap_write(dp->grf, pdata->lcdsel_grf_reg, val); 852 } 853 854 if (pdata->chip_type == RK3588_EDP) 855 regmap_write(dp->grf, dp->id ? RK3588_GRF_VO1_CON1 : RK3588_GRF_VO1_CON0, 856 EDP_MODE << 16 | FIELD_PREP(EDP_MODE, 1)); 857 858 switch (conn_state->bpc) { 859 case 12: 860 video->color_depth = COLOR_12; 861 break; 862 case 10: 863 video->color_depth = COLOR_10; 864 break; 865 case 6: 866 video->color_depth = COLOR_6; 867 break; 868 case 8: 869 default: 870 video->color_depth = COLOR_8; 871 break; 872 } 873 874 ret = analogix_dp_read_bytes_from_dpcd(dp, DP_DPCD_REV, 875 DP_RECEIVER_CAP_SIZE, dp->dpcd); 876 if (ret) { 877 dev_err(dp->dev, "failed to read dpcd caps: %d\n", ret); 878 return ret; 879 } 880 881 ret = analogix_dp_link_power_up(dp); 882 if (ret) { 883 dev_err(dp->dev, "failed to power up link: %d\n", ret); 884 return ret; 885 } 886 887 ret = analogix_dp_set_link_train(dp, dp->video_info.max_lane_count, 888 dp->video_info.max_link_rate); 889 if (ret) { 890 dev_err(dp->dev, "unable to do link train\n"); 891 return ret; 892 } 893 894 analogix_dp_enable_scramble(dp, 1); 895 analogix_dp_enable_rx_to_enhanced_mode(dp, 1); 896 analogix_dp_enable_enhanced_mode(dp, 1); 897 898 analogix_dp_init_video(dp); 899 analogix_dp_set_video_format(dp, &conn_state->mode); 900 901 if (dp->video_bist_enable) 902 analogix_dp_video_bist_enable(dp); 903 904 ret = analogix_dp_config_video(dp); 905 if (ret) { 906 dev_err(dp->dev, "unable to config video\n"); 907 return ret; 908 } 909 910 return 0; 911 } 912 913 static int analogix_dp_connector_disable(struct rockchip_connector *conn, 914 struct display_state *state) 915 { 916 const struct rockchip_dp_chip_data *pdata = 917 (const struct rockchip_dp_chip_data *)dev_get_driver_data(conn->dev); 918 struct analogix_dp_device *dp = dev_get_priv(conn->dev); 919 920 if (!analogix_dp_get_plug_in_status(dp)) 921 analogix_dp_link_power_down(dp); 922 923 if (pdata->chip_type == RK3588_EDP) 924 regmap_write(dp->grf, dp->id ? RK3588_GRF_VO1_CON1 : RK3588_GRF_VO1_CON0, 925 EDP_MODE << 16 | FIELD_PREP(EDP_MODE, 0)); 926 927 return 0; 928 } 929 930 static int analogix_dp_connector_detect(struct rockchip_connector *conn, 931 struct display_state *state) 932 { 933 struct analogix_dp_device *dp = dev_get_priv(conn->dev); 934 935 return analogix_dp_detect(dp); 936 } 937 938 static const struct rockchip_connector_funcs analogix_dp_connector_funcs = { 939 .init = analogix_dp_connector_init, 940 .get_edid = analogix_dp_connector_get_edid, 941 .enable = analogix_dp_connector_enable, 942 .disable = analogix_dp_connector_disable, 943 .detect = analogix_dp_connector_detect, 944 }; 945 946 static u32 analogix_dp_parse_link_frequencies(struct analogix_dp_device *dp) 947 { 948 struct udevice *dev = dp->dev; 949 const struct device_node *endpoint; 950 u64 frequency = 0; 951 952 endpoint = rockchip_of_graph_get_endpoint_by_regs(dev->node, 1, 0); 953 if (!endpoint) 954 return 0; 955 956 if (of_property_read_u64(endpoint, "link-frequencies", &frequency) < 0) 957 return 0; 958 959 if (!frequency) 960 return 0; 961 962 do_div(frequency, 10 * 1000); /* symbol rate kbytes */ 963 964 switch (frequency) { 965 case 162000: 966 case 270000: 967 case 540000: 968 break; 969 default: 970 dev_err(dev, "invalid link frequency value: %llu\n", frequency); 971 return 0; 972 } 973 974 return frequency; 975 } 976 977 static int analogix_dp_parse_dt(struct analogix_dp_device *dp) 978 { 979 struct udevice *dev = dp->dev; 980 int len; 981 u32 num_lanes; 982 u32 max_link_rate; 983 int ret; 984 985 dp->force_hpd = dev_read_bool(dev, "force-hpd"); 986 dp->video_bist_enable = dev_read_bool(dev, "analogix,video-bist-enable"); 987 dp->video_info.force_stream_valid = 988 dev_read_bool(dev, "analogix,force-stream-valid"); 989 990 max_link_rate = analogix_dp_parse_link_frequencies(dp); 991 if (max_link_rate && max_link_rate < drm_dp_bw_code_to_link_rate(dp->video_info.max_link_rate)) 992 dp->video_info.max_link_rate = drm_dp_link_rate_to_bw_code(max_link_rate); 993 994 if (dev_read_prop(dev, "data-lanes", &len)) { 995 num_lanes = len / sizeof(u32); 996 if (num_lanes < 1 || num_lanes > 4 || num_lanes == 3) { 997 dev_err(dev, "bad number of data lanes\n"); 998 return -EINVAL; 999 } 1000 1001 ret = dev_read_u32_array(dev, "data-lanes", dp->lane_map, 1002 num_lanes); 1003 if (ret) 1004 return ret; 1005 1006 dp->video_info.max_lane_count = num_lanes; 1007 } else { 1008 dp->lane_map[0] = 0; 1009 dp->lane_map[1] = 1; 1010 dp->lane_map[2] = 2; 1011 dp->lane_map[3] = 3; 1012 } 1013 1014 return 0; 1015 } 1016 1017 static int analogix_dp_probe(struct udevice *dev) 1018 { 1019 struct analogix_dp_device *dp = dev_get_priv(dev); 1020 const struct rockchip_dp_chip_data *pdata = 1021 (const struct rockchip_dp_chip_data *)dev_get_driver_data(dev); 1022 struct udevice *syscon; 1023 int ret; 1024 1025 dp->reg_base = dev_read_addr_ptr(dev); 1026 1027 dp->id = of_alias_get_id(ofnode_to_np(dev->node), "edp"); 1028 if (dp->id < 0) 1029 dp->id = 0; 1030 1031 ret = uclass_get_device_by_phandle(UCLASS_SYSCON, dev, "rockchip,grf", 1032 &syscon); 1033 if (!ret) { 1034 dp->grf = syscon_get_regmap(syscon); 1035 if (!dp->grf) 1036 return -ENODEV; 1037 } 1038 1039 ret = reset_get_bulk(dev, &dp->resets); 1040 if (ret) { 1041 dev_err(dev, "failed to get reset control: %d\n", ret); 1042 return ret; 1043 } 1044 1045 ret = gpio_request_by_name(dev, "hpd-gpios", 0, &dp->hpd_gpio, 1046 GPIOD_IS_IN); 1047 if (ret && ret != -ENOENT) { 1048 dev_err(dev, "failed to get hpd GPIO: %d\n", ret); 1049 return ret; 1050 } 1051 1052 generic_phy_get_by_name(dev, "dp", &dp->phy); 1053 1054 dp->plat_data.dev_type = ROCKCHIP_DP; 1055 dp->plat_data.subdev_type = pdata->chip_type; 1056 dp->plat_data.ssc = pdata->ssc; 1057 1058 dp->video_info.max_link_rate = pdata->max_link_rate; 1059 dp->video_info.max_lane_count = pdata->max_lane_count; 1060 1061 dp->dev = dev; 1062 1063 ret = analogix_dp_parse_dt(dp); 1064 if (ret) { 1065 dev_err(dev, "failed to parse DT: %d\n", ret); 1066 return ret; 1067 } 1068 1069 rockchip_connector_bind(&dp->connector, dev, dp->id, &analogix_dp_connector_funcs, 1070 NULL, DRM_MODE_CONNECTOR_eDP); 1071 1072 return 0; 1073 } 1074 1075 static const struct rockchip_dp_chip_data rk3288_edp_platform_data = { 1076 .lcdsel_grf_reg = 0x025c, 1077 .lcdsel_big = 0 | BIT(21), 1078 .lcdsel_lit = BIT(5) | BIT(21), 1079 .chip_type = RK3288_DP, 1080 1081 .max_link_rate = DP_LINK_BW_2_7, 1082 .max_lane_count = 4, 1083 }; 1084 1085 static const struct rockchip_dp_chip_data rk3368_edp_platform_data = { 1086 .chip_type = RK3368_EDP, 1087 1088 .max_link_rate = DP_LINK_BW_2_7, 1089 .max_lane_count = 4, 1090 }; 1091 1092 static const struct rockchip_dp_chip_data rk3399_edp_platform_data = { 1093 .lcdsel_grf_reg = 0x6250, 1094 .lcdsel_big = 0 | BIT(21), 1095 .lcdsel_lit = BIT(5) | BIT(21), 1096 .chip_type = RK3399_EDP, 1097 .ssc = true, 1098 1099 .max_link_rate = DP_LINK_BW_5_4, 1100 .max_lane_count = 4, 1101 }; 1102 1103 static const struct rockchip_dp_chip_data rk3568_edp_platform_data = { 1104 .chip_type = RK3568_EDP, 1105 .ssc = true, 1106 1107 .max_link_rate = DP_LINK_BW_2_7, 1108 .max_lane_count = 4, 1109 }; 1110 1111 static const struct rockchip_dp_chip_data rk3588_edp_platform_data = { 1112 .chip_type = RK3588_EDP, 1113 .ssc = true, 1114 1115 .max_link_rate = DP_LINK_BW_5_4, 1116 .max_lane_count = 4, 1117 }; 1118 1119 static const struct udevice_id analogix_dp_ids[] = { 1120 { 1121 .compatible = "rockchip,rk3288-dp", 1122 .data = (ulong)&rk3288_edp_platform_data, 1123 }, { 1124 .compatible = "rockchip,rk3368-edp", 1125 .data = (ulong)&rk3368_edp_platform_data, 1126 }, { 1127 .compatible = "rockchip,rk3399-edp", 1128 .data = (ulong)&rk3399_edp_platform_data, 1129 }, { 1130 .compatible = "rockchip,rk3568-edp", 1131 .data = (ulong)&rk3568_edp_platform_data, 1132 }, { 1133 .compatible = "rockchip,rk3588-edp", 1134 .data = (ulong)&rk3588_edp_platform_data, 1135 }, 1136 {} 1137 }; 1138 1139 U_BOOT_DRIVER(analogix_dp) = { 1140 .name = "analogix_dp", 1141 .id = UCLASS_DISPLAY, 1142 .of_match = analogix_dp_ids, 1143 .probe = analogix_dp_probe, 1144 .priv_auto_alloc_size = sizeof(struct analogix_dp_device), 1145 }; 1146