1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (c) 2012-2020, The Linux Foundation. All rights reserved. 4 */ 5 6 #define pr_fmt(fmt) "[drm-dp] %s: " fmt, __func__ 7 8 #include <linux/types.h> 9 #include <linux/completion.h> 10 #include <linux/delay.h> 11 #include <linux/phy/phy.h> 12 #include <linux/phy/phy-dp.h> 13 #include <linux/pm_opp.h> 14 15 #include <drm/display/drm_dp_helper.h> 16 #include <drm/drm_fixed.h> 17 #include <drm/drm_print.h> 18 19 #include "dp_reg.h" 20 #include "dp_ctrl.h" 21 #include "dp_link.h" 22 23 #define DP_KHZ_TO_HZ 1000 24 #define IDLE_PATTERN_COMPLETION_TIMEOUT_JIFFIES (30 * HZ / 1000) /* 30 ms */ 25 #define PSR_OPERATION_COMPLETION_TIMEOUT_JIFFIES (300 * HZ / 1000) /* 300 ms */ 26 #define WAIT_FOR_VIDEO_READY_TIMEOUT_JIFFIES (HZ / 2) 27 28 #define DP_CTRL_INTR_READY_FOR_VIDEO BIT(0) 29 #define DP_CTRL_INTR_IDLE_PATTERN_SENT BIT(3) 30 31 #define MR_LINK_TRAINING1 0x8 32 #define MR_LINK_SYMBOL_ERM 0x80 33 #define MR_LINK_PRBS7 0x100 34 #define MR_LINK_CUSTOM80 0x200 35 #define MR_LINK_TRAINING4 0x40 36 37 enum { 38 DP_TRAINING_NONE, 39 DP_TRAINING_1, 40 DP_TRAINING_2, 41 }; 42 43 struct msm_dp_tu_calc_input { 44 u64 lclk; /* 162, 270, 540 and 810 */ 45 u64 pclk_khz; /* in KHz */ 46 u64 hactive; /* active h-width */ 47 u64 hporch; /* bp + fp + pulse */ 48 int nlanes; /* no.of.lanes */ 49 int bpp; /* bits */ 50 int pixel_enc; /* 444, 420, 422 */ 51 int dsc_en; /* dsc on/off */ 52 int async_en; /* async mode */ 53 int fec_en; /* fec */ 54 int compress_ratio; /* 2:1 = 200, 3:1 = 300, 3.75:1 = 375 */ 55 int num_of_dsc_slices; /* number of slices per line */ 56 }; 57 58 struct msm_dp_vc_tu_mapping_table { 59 u32 vic; 60 u8 lanes; 61 u8 lrate; /* DP_LINK_RATE -> 162(6), 270(10), 540(20), 810 (30) */ 62 u8 bpp; 63 u8 valid_boundary_link; 64 u16 delay_start_link; 65 bool boundary_moderation_en; 66 u8 valid_lower_boundary_link; 67 u8 upper_boundary_count; 68 u8 lower_boundary_count; 69 u8 tu_size_minus1; 70 }; 71 72 struct msm_dp_ctrl_private { 73 struct msm_dp_ctrl msm_dp_ctrl; 74 struct drm_device *drm_dev; 75 struct device *dev; 76 struct drm_dp_aux *aux; 77 struct msm_dp_panel *panel; 78 struct msm_dp_link *link; 79 struct msm_dp_catalog *catalog; 80 81 struct phy *phy; 82 83 unsigned int num_core_clks; 84 struct clk_bulk_data *core_clks; 85 86 unsigned int num_link_clks; 87 struct clk_bulk_data *link_clks; 88 89 struct clk *pixel_clk; 90 91 union phy_configure_opts phy_opts; 92 93 struct completion idle_comp; 94 struct completion psr_op_comp; 95 struct completion video_comp; 96 97 bool core_clks_on; 98 bool link_clks_on; 99 bool stream_clks_on; 100 }; 101 102 static int msm_dp_aux_link_configure(struct drm_dp_aux *aux, 103 struct msm_dp_link_info *link) 104 { 105 u8 values[2]; 106 int err; 107 108 values[0] = drm_dp_link_rate_to_bw_code(link->rate); 109 values[1] = link->num_lanes; 110 111 if (link->capabilities & DP_LINK_CAP_ENHANCED_FRAMING) 112 values[1] |= DP_LANE_COUNT_ENHANCED_FRAME_EN; 113 114 err = drm_dp_dpcd_write(aux, DP_LINK_BW_SET, values, sizeof(values)); 115 if (err < 0) 116 return err; 117 118 return 0; 119 } 120 121 void msm_dp_ctrl_push_idle(struct msm_dp_ctrl *msm_dp_ctrl) 122 { 123 struct msm_dp_ctrl_private *ctrl; 124 125 ctrl = container_of(msm_dp_ctrl, struct msm_dp_ctrl_private, msm_dp_ctrl); 126 127 reinit_completion(&ctrl->idle_comp); 128 msm_dp_catalog_ctrl_state_ctrl(ctrl->catalog, DP_STATE_CTRL_PUSH_IDLE); 129 130 if (!wait_for_completion_timeout(&ctrl->idle_comp, 131 IDLE_PATTERN_COMPLETION_TIMEOUT_JIFFIES)) 132 pr_warn("PUSH_IDLE pattern timedout\n"); 133 134 drm_dbg_dp(ctrl->drm_dev, "mainlink off\n"); 135 } 136 137 static void msm_dp_ctrl_config_ctrl(struct msm_dp_ctrl_private *ctrl) 138 { 139 u32 config = 0, tbd; 140 const u8 *dpcd = ctrl->panel->dpcd; 141 142 /* Default-> LSCLK DIV: 1/4 LCLK */ 143 config |= (2 << DP_CONFIGURATION_CTRL_LSCLK_DIV_SHIFT); 144 145 if (ctrl->panel->msm_dp_mode.out_fmt_is_yuv_420) 146 config |= DP_CONFIGURATION_CTRL_RGB_YUV; /* YUV420 */ 147 148 /* Scrambler reset enable */ 149 if (drm_dp_alternate_scrambler_reset_cap(dpcd)) 150 config |= DP_CONFIGURATION_CTRL_ASSR; 151 152 tbd = msm_dp_link_get_test_bits_depth(ctrl->link, 153 ctrl->panel->msm_dp_mode.bpp); 154 155 config |= tbd << DP_CONFIGURATION_CTRL_BPC_SHIFT; 156 157 /* Num of Lanes */ 158 config |= ((ctrl->link->link_params.num_lanes - 1) 159 << DP_CONFIGURATION_CTRL_NUM_OF_LANES_SHIFT); 160 161 if (drm_dp_enhanced_frame_cap(dpcd)) 162 config |= DP_CONFIGURATION_CTRL_ENHANCED_FRAMING; 163 164 config |= DP_CONFIGURATION_CTRL_P_INTERLACED; /* progressive video */ 165 166 /* sync clock & static Mvid */ 167 config |= DP_CONFIGURATION_CTRL_STATIC_DYNAMIC_CN; 168 config |= DP_CONFIGURATION_CTRL_SYNC_ASYNC_CLK; 169 170 if (ctrl->panel->psr_cap.version) 171 config |= DP_CONFIGURATION_CTRL_SEND_VSC; 172 173 msm_dp_catalog_ctrl_config_ctrl(ctrl->catalog, config); 174 } 175 176 static void msm_dp_ctrl_configure_source_params(struct msm_dp_ctrl_private *ctrl) 177 { 178 u32 cc, tb; 179 180 msm_dp_catalog_ctrl_lane_mapping(ctrl->catalog); 181 msm_dp_catalog_setup_peripheral_flush(ctrl->catalog); 182 183 msm_dp_ctrl_config_ctrl(ctrl); 184 185 tb = msm_dp_link_get_test_bits_depth(ctrl->link, 186 ctrl->panel->msm_dp_mode.bpp); 187 cc = msm_dp_link_get_colorimetry_config(ctrl->link); 188 msm_dp_catalog_ctrl_config_misc(ctrl->catalog, cc, tb); 189 msm_dp_panel_timing_cfg(ctrl->panel); 190 } 191 192 /* 193 * The structure and few functions present below are IP/Hardware 194 * specific implementation. Most of the implementation will not 195 * have coding comments 196 */ 197 struct tu_algo_data { 198 s64 lclk_fp; 199 s64 pclk_fp; 200 s64 lwidth; 201 s64 lwidth_fp; 202 s64 hbp_relative_to_pclk; 203 s64 hbp_relative_to_pclk_fp; 204 int nlanes; 205 int bpp; 206 int pixelEnc; 207 int dsc_en; 208 int async_en; 209 int bpc; 210 211 uint delay_start_link_extra_pixclk; 212 int extra_buffer_margin; 213 s64 ratio_fp; 214 s64 original_ratio_fp; 215 216 s64 err_fp; 217 s64 n_err_fp; 218 s64 n_n_err_fp; 219 int tu_size; 220 int tu_size_desired; 221 int tu_size_minus1; 222 223 int valid_boundary_link; 224 s64 resulting_valid_fp; 225 s64 total_valid_fp; 226 s64 effective_valid_fp; 227 s64 effective_valid_recorded_fp; 228 int n_tus; 229 int n_tus_per_lane; 230 int paired_tus; 231 int remainder_tus; 232 int remainder_tus_upper; 233 int remainder_tus_lower; 234 int extra_bytes; 235 int filler_size; 236 int delay_start_link; 237 238 int extra_pclk_cycles; 239 int extra_pclk_cycles_in_link_clk; 240 s64 ratio_by_tu_fp; 241 s64 average_valid2_fp; 242 int new_valid_boundary_link; 243 int remainder_symbols_exist; 244 int n_symbols; 245 s64 n_remainder_symbols_per_lane_fp; 246 s64 last_partial_tu_fp; 247 s64 TU_ratio_err_fp; 248 249 int n_tus_incl_last_incomplete_tu; 250 int extra_pclk_cycles_tmp; 251 int extra_pclk_cycles_in_link_clk_tmp; 252 int extra_required_bytes_new_tmp; 253 int filler_size_tmp; 254 int lower_filler_size_tmp; 255 int delay_start_link_tmp; 256 257 bool boundary_moderation_en; 258 int boundary_mod_lower_err; 259 int upper_boundary_count; 260 int lower_boundary_count; 261 int i_upper_boundary_count; 262 int i_lower_boundary_count; 263 int valid_lower_boundary_link; 264 int even_distribution_BF; 265 int even_distribution_legacy; 266 int even_distribution; 267 int min_hblank_violated; 268 s64 delay_start_time_fp; 269 s64 hbp_time_fp; 270 s64 hactive_time_fp; 271 s64 diff_abs_fp; 272 273 s64 ratio; 274 }; 275 276 static int _tu_param_compare(s64 a, s64 b) 277 { 278 u32 a_sign; 279 u32 b_sign; 280 s64 a_temp, b_temp, minus_1; 281 282 if (a == b) 283 return 0; 284 285 minus_1 = drm_fixp_from_fraction(-1, 1); 286 287 a_sign = (a >> 32) & 0x80000000 ? 1 : 0; 288 289 b_sign = (b >> 32) & 0x80000000 ? 1 : 0; 290 291 if (a_sign > b_sign) 292 return 2; 293 else if (b_sign > a_sign) 294 return 1; 295 296 if (!a_sign && !b_sign) { /* positive */ 297 if (a > b) 298 return 1; 299 else 300 return 2; 301 } else { /* negative */ 302 a_temp = drm_fixp_mul(a, minus_1); 303 b_temp = drm_fixp_mul(b, minus_1); 304 305 if (a_temp > b_temp) 306 return 2; 307 else 308 return 1; 309 } 310 } 311 312 static void msm_dp_panel_update_tu_timings(struct msm_dp_tu_calc_input *in, 313 struct tu_algo_data *tu) 314 { 315 int nlanes = in->nlanes; 316 int dsc_num_slices = in->num_of_dsc_slices; 317 int dsc_num_bytes = 0; 318 int numerator; 319 s64 pclk_dsc_fp; 320 s64 dwidth_dsc_fp; 321 s64 hbp_dsc_fp; 322 323 int tot_num_eoc_symbols = 0; 324 int tot_num_hor_bytes = 0; 325 int tot_num_dummy_bytes = 0; 326 int dwidth_dsc_bytes = 0; 327 int eoc_bytes = 0; 328 329 s64 temp1_fp, temp2_fp, temp3_fp; 330 331 tu->lclk_fp = drm_fixp_from_fraction(in->lclk, 1); 332 tu->pclk_fp = drm_fixp_from_fraction(in->pclk_khz, 1000); 333 tu->lwidth = in->hactive; 334 tu->hbp_relative_to_pclk = in->hporch; 335 tu->nlanes = in->nlanes; 336 tu->bpp = in->bpp; 337 tu->pixelEnc = in->pixel_enc; 338 tu->dsc_en = in->dsc_en; 339 tu->async_en = in->async_en; 340 tu->lwidth_fp = drm_fixp_from_fraction(in->hactive, 1); 341 tu->hbp_relative_to_pclk_fp = drm_fixp_from_fraction(in->hporch, 1); 342 343 if (tu->pixelEnc == 420) { 344 temp1_fp = drm_fixp_from_fraction(2, 1); 345 tu->pclk_fp = drm_fixp_div(tu->pclk_fp, temp1_fp); 346 tu->lwidth_fp = drm_fixp_div(tu->lwidth_fp, temp1_fp); 347 tu->hbp_relative_to_pclk_fp = 348 drm_fixp_div(tu->hbp_relative_to_pclk_fp, 2); 349 } 350 351 if (tu->pixelEnc == 422) { 352 switch (tu->bpp) { 353 case 24: 354 tu->bpp = 16; 355 tu->bpc = 8; 356 break; 357 case 30: 358 tu->bpp = 20; 359 tu->bpc = 10; 360 break; 361 default: 362 tu->bpp = 16; 363 tu->bpc = 8; 364 break; 365 } 366 } else { 367 tu->bpc = tu->bpp/3; 368 } 369 370 if (!in->dsc_en) 371 goto fec_check; 372 373 temp1_fp = drm_fixp_from_fraction(in->compress_ratio, 100); 374 temp2_fp = drm_fixp_from_fraction(in->bpp, 1); 375 temp3_fp = drm_fixp_div(temp2_fp, temp1_fp); 376 temp2_fp = drm_fixp_mul(tu->lwidth_fp, temp3_fp); 377 378 temp1_fp = drm_fixp_from_fraction(8, 1); 379 temp3_fp = drm_fixp_div(temp2_fp, temp1_fp); 380 381 numerator = drm_fixp2int(temp3_fp); 382 383 dsc_num_bytes = numerator / dsc_num_slices; 384 eoc_bytes = dsc_num_bytes % nlanes; 385 tot_num_eoc_symbols = nlanes * dsc_num_slices; 386 tot_num_hor_bytes = dsc_num_bytes * dsc_num_slices; 387 tot_num_dummy_bytes = (nlanes - eoc_bytes) * dsc_num_slices; 388 389 if (dsc_num_bytes == 0) 390 pr_info("incorrect no of bytes per slice=%d\n", dsc_num_bytes); 391 392 dwidth_dsc_bytes = (tot_num_hor_bytes + 393 tot_num_eoc_symbols + 394 (eoc_bytes == 0 ? 0 : tot_num_dummy_bytes)); 395 396 dwidth_dsc_fp = drm_fixp_from_fraction(dwidth_dsc_bytes, 3); 397 398 temp2_fp = drm_fixp_mul(tu->pclk_fp, dwidth_dsc_fp); 399 temp1_fp = drm_fixp_div(temp2_fp, tu->lwidth_fp); 400 pclk_dsc_fp = temp1_fp; 401 402 temp1_fp = drm_fixp_div(pclk_dsc_fp, tu->pclk_fp); 403 temp2_fp = drm_fixp_mul(tu->hbp_relative_to_pclk_fp, temp1_fp); 404 hbp_dsc_fp = temp2_fp; 405 406 /* output */ 407 tu->pclk_fp = pclk_dsc_fp; 408 tu->lwidth_fp = dwidth_dsc_fp; 409 tu->hbp_relative_to_pclk_fp = hbp_dsc_fp; 410 411 fec_check: 412 if (in->fec_en) { 413 temp1_fp = drm_fixp_from_fraction(976, 1000); /* 0.976 */ 414 tu->lclk_fp = drm_fixp_mul(tu->lclk_fp, temp1_fp); 415 } 416 } 417 418 static void _tu_valid_boundary_calc(struct tu_algo_data *tu) 419 { 420 s64 temp1_fp, temp2_fp, temp, temp1, temp2; 421 int compare_result_1, compare_result_2, compare_result_3; 422 423 temp1_fp = drm_fixp_from_fraction(tu->tu_size, 1); 424 temp2_fp = drm_fixp_mul(tu->ratio_fp, temp1_fp); 425 426 tu->new_valid_boundary_link = drm_fixp2int_ceil(temp2_fp); 427 428 temp = (tu->i_upper_boundary_count * 429 tu->new_valid_boundary_link + 430 tu->i_lower_boundary_count * 431 (tu->new_valid_boundary_link-1)); 432 tu->average_valid2_fp = drm_fixp_from_fraction(temp, 433 (tu->i_upper_boundary_count + 434 tu->i_lower_boundary_count)); 435 436 temp1_fp = drm_fixp_from_fraction(tu->bpp, 8); 437 temp2_fp = tu->lwidth_fp; 438 temp1_fp = drm_fixp_mul(temp2_fp, temp1_fp); 439 temp2_fp = drm_fixp_div(temp1_fp, tu->average_valid2_fp); 440 tu->n_tus = drm_fixp2int(temp2_fp); 441 if ((temp2_fp & 0xFFFFFFFF) > 0xFFFFF000) 442 tu->n_tus += 1; 443 444 temp1_fp = drm_fixp_from_fraction(tu->n_tus, 1); 445 temp2_fp = drm_fixp_mul(temp1_fp, tu->average_valid2_fp); 446 temp1_fp = drm_fixp_from_fraction(tu->n_symbols, 1); 447 temp2_fp = temp1_fp - temp2_fp; 448 temp1_fp = drm_fixp_from_fraction(tu->nlanes, 1); 449 temp2_fp = drm_fixp_div(temp2_fp, temp1_fp); 450 tu->n_remainder_symbols_per_lane_fp = temp2_fp; 451 452 temp1_fp = drm_fixp_from_fraction(tu->tu_size, 1); 453 tu->last_partial_tu_fp = 454 drm_fixp_div(tu->n_remainder_symbols_per_lane_fp, 455 temp1_fp); 456 457 if (tu->n_remainder_symbols_per_lane_fp != 0) 458 tu->remainder_symbols_exist = 1; 459 else 460 tu->remainder_symbols_exist = 0; 461 462 temp1_fp = drm_fixp_from_fraction(tu->n_tus, tu->nlanes); 463 tu->n_tus_per_lane = drm_fixp2int(temp1_fp); 464 465 tu->paired_tus = (int)((tu->n_tus_per_lane) / 466 (tu->i_upper_boundary_count + 467 tu->i_lower_boundary_count)); 468 469 tu->remainder_tus = tu->n_tus_per_lane - tu->paired_tus * 470 (tu->i_upper_boundary_count + 471 tu->i_lower_boundary_count); 472 473 if ((tu->remainder_tus - tu->i_upper_boundary_count) > 0) { 474 tu->remainder_tus_upper = tu->i_upper_boundary_count; 475 tu->remainder_tus_lower = tu->remainder_tus - 476 tu->i_upper_boundary_count; 477 } else { 478 tu->remainder_tus_upper = tu->remainder_tus; 479 tu->remainder_tus_lower = 0; 480 } 481 482 temp = tu->paired_tus * (tu->i_upper_boundary_count * 483 tu->new_valid_boundary_link + 484 tu->i_lower_boundary_count * 485 (tu->new_valid_boundary_link - 1)) + 486 (tu->remainder_tus_upper * 487 tu->new_valid_boundary_link) + 488 (tu->remainder_tus_lower * 489 (tu->new_valid_boundary_link - 1)); 490 tu->total_valid_fp = drm_fixp_from_fraction(temp, 1); 491 492 if (tu->remainder_symbols_exist) { 493 temp1_fp = tu->total_valid_fp + 494 tu->n_remainder_symbols_per_lane_fp; 495 temp2_fp = drm_fixp_from_fraction(tu->n_tus_per_lane, 1); 496 temp2_fp = temp2_fp + tu->last_partial_tu_fp; 497 temp1_fp = drm_fixp_div(temp1_fp, temp2_fp); 498 } else { 499 temp2_fp = drm_fixp_from_fraction(tu->n_tus_per_lane, 1); 500 temp1_fp = drm_fixp_div(tu->total_valid_fp, temp2_fp); 501 } 502 tu->effective_valid_fp = temp1_fp; 503 504 temp1_fp = drm_fixp_from_fraction(tu->tu_size, 1); 505 temp2_fp = drm_fixp_mul(tu->ratio_fp, temp1_fp); 506 tu->n_n_err_fp = tu->effective_valid_fp - temp2_fp; 507 508 temp1_fp = drm_fixp_from_fraction(tu->tu_size, 1); 509 temp2_fp = drm_fixp_mul(tu->ratio_fp, temp1_fp); 510 tu->n_err_fp = tu->average_valid2_fp - temp2_fp; 511 512 tu->even_distribution = tu->n_tus % tu->nlanes == 0 ? 1 : 0; 513 514 temp1_fp = drm_fixp_from_fraction(tu->bpp, 8); 515 temp2_fp = tu->lwidth_fp; 516 temp1_fp = drm_fixp_mul(temp2_fp, temp1_fp); 517 temp2_fp = drm_fixp_div(temp1_fp, tu->average_valid2_fp); 518 519 if (temp2_fp) 520 tu->n_tus_incl_last_incomplete_tu = drm_fixp2int_ceil(temp2_fp); 521 else 522 tu->n_tus_incl_last_incomplete_tu = 0; 523 524 temp1 = 0; 525 temp1_fp = drm_fixp_from_fraction(tu->tu_size, 1); 526 temp2_fp = drm_fixp_mul(tu->original_ratio_fp, temp1_fp); 527 temp1_fp = tu->average_valid2_fp - temp2_fp; 528 temp2_fp = drm_fixp_from_fraction(tu->n_tus_incl_last_incomplete_tu, 1); 529 temp1_fp = drm_fixp_mul(temp2_fp, temp1_fp); 530 531 if (temp1_fp) 532 temp1 = drm_fixp2int_ceil(temp1_fp); 533 534 temp = tu->i_upper_boundary_count * tu->nlanes; 535 temp1_fp = drm_fixp_from_fraction(tu->tu_size, 1); 536 temp2_fp = drm_fixp_mul(tu->original_ratio_fp, temp1_fp); 537 temp1_fp = drm_fixp_from_fraction(tu->new_valid_boundary_link, 1); 538 temp2_fp = temp1_fp - temp2_fp; 539 temp1_fp = drm_fixp_from_fraction(temp, 1); 540 temp2_fp = drm_fixp_mul(temp1_fp, temp2_fp); 541 542 if (temp2_fp) 543 temp2 = drm_fixp2int_ceil(temp2_fp); 544 else 545 temp2 = 0; 546 tu->extra_required_bytes_new_tmp = (int)(temp1 + temp2); 547 548 temp1_fp = drm_fixp_from_fraction(8, tu->bpp); 549 temp2_fp = drm_fixp_from_fraction( 550 tu->extra_required_bytes_new_tmp, 1); 551 temp1_fp = drm_fixp_mul(temp2_fp, temp1_fp); 552 553 if (temp1_fp) 554 tu->extra_pclk_cycles_tmp = drm_fixp2int_ceil(temp1_fp); 555 else 556 tu->extra_pclk_cycles_tmp = 0; 557 558 temp1_fp = drm_fixp_from_fraction(tu->extra_pclk_cycles_tmp, 1); 559 temp2_fp = drm_fixp_div(tu->lclk_fp, tu->pclk_fp); 560 temp1_fp = drm_fixp_mul(temp1_fp, temp2_fp); 561 562 if (temp1_fp) 563 tu->extra_pclk_cycles_in_link_clk_tmp = 564 drm_fixp2int_ceil(temp1_fp); 565 else 566 tu->extra_pclk_cycles_in_link_clk_tmp = 0; 567 568 tu->filler_size_tmp = tu->tu_size - tu->new_valid_boundary_link; 569 570 tu->lower_filler_size_tmp = tu->filler_size_tmp + 1; 571 572 tu->delay_start_link_tmp = tu->extra_pclk_cycles_in_link_clk_tmp + 573 tu->lower_filler_size_tmp + 574 tu->extra_buffer_margin; 575 576 temp1_fp = drm_fixp_from_fraction(tu->delay_start_link_tmp, 1); 577 tu->delay_start_time_fp = drm_fixp_div(temp1_fp, tu->lclk_fp); 578 579 compare_result_1 = _tu_param_compare(tu->n_n_err_fp, tu->diff_abs_fp); 580 if (compare_result_1 == 2) 581 compare_result_1 = 1; 582 else 583 compare_result_1 = 0; 584 585 compare_result_2 = _tu_param_compare(tu->n_n_err_fp, tu->err_fp); 586 if (compare_result_2 == 2) 587 compare_result_2 = 1; 588 else 589 compare_result_2 = 0; 590 591 compare_result_3 = _tu_param_compare(tu->hbp_time_fp, 592 tu->delay_start_time_fp); 593 if (compare_result_3 == 2) 594 compare_result_3 = 0; 595 else 596 compare_result_3 = 1; 597 598 if (((tu->even_distribution == 1) || 599 ((tu->even_distribution_BF == 0) && 600 (tu->even_distribution_legacy == 0))) && 601 tu->n_err_fp >= 0 && tu->n_n_err_fp >= 0 && 602 compare_result_2 && 603 (compare_result_1 || (tu->min_hblank_violated == 1)) && 604 (tu->new_valid_boundary_link - 1) > 0 && 605 compare_result_3 && 606 (tu->delay_start_link_tmp <= 1023)) { 607 tu->upper_boundary_count = tu->i_upper_boundary_count; 608 tu->lower_boundary_count = tu->i_lower_boundary_count; 609 tu->err_fp = tu->n_n_err_fp; 610 tu->boundary_moderation_en = true; 611 tu->tu_size_desired = tu->tu_size; 612 tu->valid_boundary_link = tu->new_valid_boundary_link; 613 tu->effective_valid_recorded_fp = tu->effective_valid_fp; 614 tu->even_distribution_BF = 1; 615 tu->delay_start_link = tu->delay_start_link_tmp; 616 } else if (tu->boundary_mod_lower_err == 0) { 617 compare_result_1 = _tu_param_compare(tu->n_n_err_fp, 618 tu->diff_abs_fp); 619 if (compare_result_1 == 2) 620 tu->boundary_mod_lower_err = 1; 621 } 622 } 623 624 static void _dp_ctrl_calc_tu(struct msm_dp_ctrl_private *ctrl, 625 struct msm_dp_tu_calc_input *in, 626 struct msm_dp_vc_tu_mapping_table *tu_table) 627 { 628 struct tu_algo_data *tu; 629 int compare_result_1, compare_result_2; 630 u64 temp = 0; 631 s64 temp_fp = 0, temp1_fp = 0, temp2_fp = 0; 632 633 s64 LCLK_FAST_SKEW_fp = drm_fixp_from_fraction(6, 10000); /* 0.0006 */ 634 s64 const_p49_fp = drm_fixp_from_fraction(49, 100); /* 0.49 */ 635 s64 const_p56_fp = drm_fixp_from_fraction(56, 100); /* 0.56 */ 636 s64 RATIO_SCALE_fp = drm_fixp_from_fraction(1001, 1000); 637 638 u8 DP_BRUTE_FORCE = 1; 639 s64 BRUTE_FORCE_THRESHOLD_fp = drm_fixp_from_fraction(1, 10); /* 0.1 */ 640 uint EXTRA_PIXCLK_CYCLE_DELAY = 4; 641 uint HBLANK_MARGIN = 4; 642 643 tu = kzalloc(sizeof(*tu), GFP_KERNEL); 644 if (!tu) 645 return; 646 647 msm_dp_panel_update_tu_timings(in, tu); 648 649 tu->err_fp = drm_fixp_from_fraction(1000, 1); /* 1000 */ 650 651 temp1_fp = drm_fixp_from_fraction(4, 1); 652 temp2_fp = drm_fixp_mul(temp1_fp, tu->lclk_fp); 653 temp_fp = drm_fixp_div(temp2_fp, tu->pclk_fp); 654 tu->extra_buffer_margin = drm_fixp2int_ceil(temp_fp); 655 656 temp1_fp = drm_fixp_from_fraction(tu->bpp, 8); 657 temp2_fp = drm_fixp_mul(tu->pclk_fp, temp1_fp); 658 temp1_fp = drm_fixp_from_fraction(tu->nlanes, 1); 659 temp2_fp = drm_fixp_div(temp2_fp, temp1_fp); 660 tu->ratio_fp = drm_fixp_div(temp2_fp, tu->lclk_fp); 661 662 tu->original_ratio_fp = tu->ratio_fp; 663 tu->boundary_moderation_en = false; 664 tu->upper_boundary_count = 0; 665 tu->lower_boundary_count = 0; 666 tu->i_upper_boundary_count = 0; 667 tu->i_lower_boundary_count = 0; 668 tu->valid_lower_boundary_link = 0; 669 tu->even_distribution_BF = 0; 670 tu->even_distribution_legacy = 0; 671 tu->even_distribution = 0; 672 tu->delay_start_time_fp = 0; 673 674 tu->err_fp = drm_fixp_from_fraction(1000, 1); 675 tu->n_err_fp = 0; 676 tu->n_n_err_fp = 0; 677 678 tu->ratio = drm_fixp2int(tu->ratio_fp); 679 temp1_fp = drm_fixp_from_fraction(tu->nlanes, 1); 680 div64_u64_rem(tu->lwidth_fp, temp1_fp, &temp2_fp); 681 if (temp2_fp != 0 && 682 !tu->ratio && tu->dsc_en == 0) { 683 tu->ratio_fp = drm_fixp_mul(tu->ratio_fp, RATIO_SCALE_fp); 684 tu->ratio = drm_fixp2int(tu->ratio_fp); 685 if (tu->ratio) 686 tu->ratio_fp = drm_fixp_from_fraction(1, 1); 687 } 688 689 if (tu->ratio > 1) 690 tu->ratio = 1; 691 692 if (tu->ratio == 1) 693 goto tu_size_calc; 694 695 compare_result_1 = _tu_param_compare(tu->ratio_fp, const_p49_fp); 696 if (!compare_result_1 || compare_result_1 == 1) 697 compare_result_1 = 1; 698 else 699 compare_result_1 = 0; 700 701 compare_result_2 = _tu_param_compare(tu->ratio_fp, const_p56_fp); 702 if (!compare_result_2 || compare_result_2 == 2) 703 compare_result_2 = 1; 704 else 705 compare_result_2 = 0; 706 707 if (tu->dsc_en && compare_result_1 && compare_result_2) { 708 HBLANK_MARGIN += 4; 709 drm_dbg_dp(ctrl->drm_dev, 710 "increase HBLANK_MARGIN to %d\n", HBLANK_MARGIN); 711 } 712 713 tu_size_calc: 714 for (tu->tu_size = 32; tu->tu_size <= 64; tu->tu_size++) { 715 temp1_fp = drm_fixp_from_fraction(tu->tu_size, 1); 716 temp2_fp = drm_fixp_mul(tu->ratio_fp, temp1_fp); 717 temp = drm_fixp2int_ceil(temp2_fp); 718 temp1_fp = drm_fixp_from_fraction(temp, 1); 719 tu->n_err_fp = temp1_fp - temp2_fp; 720 721 if (tu->n_err_fp < tu->err_fp) { 722 tu->err_fp = tu->n_err_fp; 723 tu->tu_size_desired = tu->tu_size; 724 } 725 } 726 727 tu->tu_size_minus1 = tu->tu_size_desired - 1; 728 729 temp1_fp = drm_fixp_from_fraction(tu->tu_size_desired, 1); 730 temp2_fp = drm_fixp_mul(tu->ratio_fp, temp1_fp); 731 tu->valid_boundary_link = drm_fixp2int_ceil(temp2_fp); 732 733 temp1_fp = drm_fixp_from_fraction(tu->bpp, 8); 734 temp2_fp = tu->lwidth_fp; 735 temp2_fp = drm_fixp_mul(temp2_fp, temp1_fp); 736 737 temp1_fp = drm_fixp_from_fraction(tu->valid_boundary_link, 1); 738 temp2_fp = drm_fixp_div(temp2_fp, temp1_fp); 739 tu->n_tus = drm_fixp2int(temp2_fp); 740 if ((temp2_fp & 0xFFFFFFFF) > 0xFFFFF000) 741 tu->n_tus += 1; 742 743 tu->even_distribution_legacy = tu->n_tus % tu->nlanes == 0 ? 1 : 0; 744 745 drm_dbg_dp(ctrl->drm_dev, 746 "n_sym = %d, num_of_tus = %d\n", 747 tu->valid_boundary_link, tu->n_tus); 748 749 temp1_fp = drm_fixp_from_fraction(tu->tu_size_desired, 1); 750 temp2_fp = drm_fixp_mul(tu->original_ratio_fp, temp1_fp); 751 temp1_fp = drm_fixp_from_fraction(tu->valid_boundary_link, 1); 752 temp2_fp = temp1_fp - temp2_fp; 753 temp1_fp = drm_fixp_from_fraction(tu->n_tus + 1, 1); 754 temp2_fp = drm_fixp_mul(temp1_fp, temp2_fp); 755 756 temp = drm_fixp2int(temp2_fp); 757 if (temp && temp2_fp) 758 tu->extra_bytes = drm_fixp2int_ceil(temp2_fp); 759 else 760 tu->extra_bytes = 0; 761 762 temp1_fp = drm_fixp_from_fraction(tu->extra_bytes, 1); 763 temp2_fp = drm_fixp_from_fraction(8, tu->bpp); 764 temp1_fp = drm_fixp_mul(temp1_fp, temp2_fp); 765 766 if (temp && temp1_fp) 767 tu->extra_pclk_cycles = drm_fixp2int_ceil(temp1_fp); 768 else 769 tu->extra_pclk_cycles = drm_fixp2int(temp1_fp); 770 771 temp1_fp = drm_fixp_div(tu->lclk_fp, tu->pclk_fp); 772 temp2_fp = drm_fixp_from_fraction(tu->extra_pclk_cycles, 1); 773 temp1_fp = drm_fixp_mul(temp2_fp, temp1_fp); 774 775 if (temp1_fp) 776 tu->extra_pclk_cycles_in_link_clk = drm_fixp2int_ceil(temp1_fp); 777 else 778 tu->extra_pclk_cycles_in_link_clk = drm_fixp2int(temp1_fp); 779 780 tu->filler_size = tu->tu_size_desired - tu->valid_boundary_link; 781 782 temp1_fp = drm_fixp_from_fraction(tu->tu_size_desired, 1); 783 tu->ratio_by_tu_fp = drm_fixp_mul(tu->ratio_fp, temp1_fp); 784 785 tu->delay_start_link = tu->extra_pclk_cycles_in_link_clk + 786 tu->filler_size + tu->extra_buffer_margin; 787 788 tu->resulting_valid_fp = 789 drm_fixp_from_fraction(tu->valid_boundary_link, 1); 790 791 temp1_fp = drm_fixp_from_fraction(tu->tu_size_desired, 1); 792 temp2_fp = drm_fixp_div(tu->resulting_valid_fp, temp1_fp); 793 tu->TU_ratio_err_fp = temp2_fp - tu->original_ratio_fp; 794 795 temp1_fp = drm_fixp_from_fraction(HBLANK_MARGIN, 1); 796 temp1_fp = tu->hbp_relative_to_pclk_fp - temp1_fp; 797 tu->hbp_time_fp = drm_fixp_div(temp1_fp, tu->pclk_fp); 798 799 temp1_fp = drm_fixp_from_fraction(tu->delay_start_link, 1); 800 tu->delay_start_time_fp = drm_fixp_div(temp1_fp, tu->lclk_fp); 801 802 compare_result_1 = _tu_param_compare(tu->hbp_time_fp, 803 tu->delay_start_time_fp); 804 if (compare_result_1 == 2) /* if (hbp_time_fp < delay_start_time_fp) */ 805 tu->min_hblank_violated = 1; 806 807 tu->hactive_time_fp = drm_fixp_div(tu->lwidth_fp, tu->pclk_fp); 808 809 compare_result_2 = _tu_param_compare(tu->hactive_time_fp, 810 tu->delay_start_time_fp); 811 if (compare_result_2 == 2) 812 tu->min_hblank_violated = 1; 813 814 tu->delay_start_time_fp = 0; 815 816 /* brute force */ 817 818 tu->delay_start_link_extra_pixclk = EXTRA_PIXCLK_CYCLE_DELAY; 819 tu->diff_abs_fp = tu->resulting_valid_fp - tu->ratio_by_tu_fp; 820 821 temp = drm_fixp2int(tu->diff_abs_fp); 822 if (!temp && tu->diff_abs_fp <= 0xffff) 823 tu->diff_abs_fp = 0; 824 825 /* if(diff_abs < 0) diff_abs *= -1 */ 826 if (tu->diff_abs_fp < 0) 827 tu->diff_abs_fp = drm_fixp_mul(tu->diff_abs_fp, -1); 828 829 tu->boundary_mod_lower_err = 0; 830 if ((tu->diff_abs_fp != 0 && 831 ((tu->diff_abs_fp > BRUTE_FORCE_THRESHOLD_fp) || 832 (tu->even_distribution_legacy == 0) || 833 (DP_BRUTE_FORCE == 1))) || 834 (tu->min_hblank_violated == 1)) { 835 do { 836 tu->err_fp = drm_fixp_from_fraction(1000, 1); 837 838 temp1_fp = drm_fixp_div(tu->lclk_fp, tu->pclk_fp); 839 temp2_fp = drm_fixp_from_fraction( 840 tu->delay_start_link_extra_pixclk, 1); 841 temp1_fp = drm_fixp_mul(temp2_fp, temp1_fp); 842 843 if (temp1_fp) 844 tu->extra_buffer_margin = 845 drm_fixp2int_ceil(temp1_fp); 846 else 847 tu->extra_buffer_margin = 0; 848 849 temp1_fp = drm_fixp_from_fraction(tu->bpp, 8); 850 temp1_fp = drm_fixp_mul(tu->lwidth_fp, temp1_fp); 851 852 if (temp1_fp) 853 tu->n_symbols = drm_fixp2int_ceil(temp1_fp); 854 else 855 tu->n_symbols = 0; 856 857 for (tu->tu_size = 32; tu->tu_size <= 64; tu->tu_size++) { 858 for (tu->i_upper_boundary_count = 1; 859 tu->i_upper_boundary_count <= 15; 860 tu->i_upper_boundary_count++) { 861 for (tu->i_lower_boundary_count = 1; 862 tu->i_lower_boundary_count <= 15; 863 tu->i_lower_boundary_count++) { 864 _tu_valid_boundary_calc(tu); 865 } 866 } 867 } 868 tu->delay_start_link_extra_pixclk--; 869 } while (tu->boundary_moderation_en != true && 870 tu->boundary_mod_lower_err == 1 && 871 tu->delay_start_link_extra_pixclk != 0); 872 873 if (tu->boundary_moderation_en == true) { 874 temp1_fp = drm_fixp_from_fraction( 875 (tu->upper_boundary_count * 876 tu->valid_boundary_link + 877 tu->lower_boundary_count * 878 (tu->valid_boundary_link - 1)), 1); 879 temp2_fp = drm_fixp_from_fraction( 880 (tu->upper_boundary_count + 881 tu->lower_boundary_count), 1); 882 tu->resulting_valid_fp = 883 drm_fixp_div(temp1_fp, temp2_fp); 884 885 temp1_fp = drm_fixp_from_fraction( 886 tu->tu_size_desired, 1); 887 tu->ratio_by_tu_fp = 888 drm_fixp_mul(tu->original_ratio_fp, temp1_fp); 889 890 tu->valid_lower_boundary_link = 891 tu->valid_boundary_link - 1; 892 893 temp1_fp = drm_fixp_from_fraction(tu->bpp, 8); 894 temp1_fp = drm_fixp_mul(tu->lwidth_fp, temp1_fp); 895 temp2_fp = drm_fixp_div(temp1_fp, 896 tu->resulting_valid_fp); 897 tu->n_tus = drm_fixp2int(temp2_fp); 898 899 tu->tu_size_minus1 = tu->tu_size_desired - 1; 900 tu->even_distribution_BF = 1; 901 902 temp1_fp = 903 drm_fixp_from_fraction(tu->tu_size_desired, 1); 904 temp2_fp = 905 drm_fixp_div(tu->resulting_valid_fp, temp1_fp); 906 tu->TU_ratio_err_fp = temp2_fp - tu->original_ratio_fp; 907 } 908 } 909 910 temp2_fp = drm_fixp_mul(LCLK_FAST_SKEW_fp, tu->lwidth_fp); 911 912 if (temp2_fp) 913 temp = drm_fixp2int_ceil(temp2_fp); 914 else 915 temp = 0; 916 917 temp1_fp = drm_fixp_from_fraction(tu->nlanes, 1); 918 temp2_fp = drm_fixp_mul(tu->original_ratio_fp, temp1_fp); 919 temp1_fp = drm_fixp_from_fraction(tu->bpp, 8); 920 temp2_fp = drm_fixp_div(temp1_fp, temp2_fp); 921 temp1_fp = drm_fixp_from_fraction(temp, 1); 922 temp2_fp = drm_fixp_mul(temp1_fp, temp2_fp); 923 temp = drm_fixp2int(temp2_fp); 924 925 if (tu->async_en) 926 tu->delay_start_link += (int)temp; 927 928 temp1_fp = drm_fixp_from_fraction(tu->delay_start_link, 1); 929 tu->delay_start_time_fp = drm_fixp_div(temp1_fp, tu->lclk_fp); 930 931 /* OUTPUTS */ 932 tu_table->valid_boundary_link = tu->valid_boundary_link; 933 tu_table->delay_start_link = tu->delay_start_link; 934 tu_table->boundary_moderation_en = tu->boundary_moderation_en; 935 tu_table->valid_lower_boundary_link = tu->valid_lower_boundary_link; 936 tu_table->upper_boundary_count = tu->upper_boundary_count; 937 tu_table->lower_boundary_count = tu->lower_boundary_count; 938 tu_table->tu_size_minus1 = tu->tu_size_minus1; 939 940 drm_dbg_dp(ctrl->drm_dev, "TU: valid_boundary_link: %d\n", 941 tu_table->valid_boundary_link); 942 drm_dbg_dp(ctrl->drm_dev, "TU: delay_start_link: %d\n", 943 tu_table->delay_start_link); 944 drm_dbg_dp(ctrl->drm_dev, "TU: boundary_moderation_en: %d\n", 945 tu_table->boundary_moderation_en); 946 drm_dbg_dp(ctrl->drm_dev, "TU: valid_lower_boundary_link: %d\n", 947 tu_table->valid_lower_boundary_link); 948 drm_dbg_dp(ctrl->drm_dev, "TU: upper_boundary_count: %d\n", 949 tu_table->upper_boundary_count); 950 drm_dbg_dp(ctrl->drm_dev, "TU: lower_boundary_count: %d\n", 951 tu_table->lower_boundary_count); 952 drm_dbg_dp(ctrl->drm_dev, "TU: tu_size_minus1: %d\n", 953 tu_table->tu_size_minus1); 954 955 kfree(tu); 956 } 957 958 static void msm_dp_ctrl_calc_tu_parameters(struct msm_dp_ctrl_private *ctrl, 959 struct msm_dp_vc_tu_mapping_table *tu_table) 960 { 961 struct msm_dp_tu_calc_input in; 962 struct drm_display_mode *drm_mode; 963 964 drm_mode = &ctrl->panel->msm_dp_mode.drm_mode; 965 966 in.lclk = ctrl->link->link_params.rate / 1000; 967 in.pclk_khz = drm_mode->clock; 968 in.hactive = drm_mode->hdisplay; 969 in.hporch = drm_mode->htotal - drm_mode->hdisplay; 970 in.nlanes = ctrl->link->link_params.num_lanes; 971 in.bpp = ctrl->panel->msm_dp_mode.bpp; 972 in.pixel_enc = ctrl->panel->msm_dp_mode.out_fmt_is_yuv_420 ? 420 : 444; 973 in.dsc_en = 0; 974 in.async_en = 0; 975 in.fec_en = 0; 976 in.num_of_dsc_slices = 0; 977 in.compress_ratio = 100; 978 979 _dp_ctrl_calc_tu(ctrl, &in, tu_table); 980 } 981 982 static void msm_dp_ctrl_setup_tr_unit(struct msm_dp_ctrl_private *ctrl) 983 { 984 u32 msm_dp_tu = 0x0; 985 u32 valid_boundary = 0x0; 986 u32 valid_boundary2 = 0x0; 987 struct msm_dp_vc_tu_mapping_table tu_calc_table; 988 989 msm_dp_ctrl_calc_tu_parameters(ctrl, &tu_calc_table); 990 991 msm_dp_tu |= tu_calc_table.tu_size_minus1; 992 valid_boundary |= tu_calc_table.valid_boundary_link; 993 valid_boundary |= (tu_calc_table.delay_start_link << 16); 994 995 valid_boundary2 |= (tu_calc_table.valid_lower_boundary_link << 1); 996 valid_boundary2 |= (tu_calc_table.upper_boundary_count << 16); 997 valid_boundary2 |= (tu_calc_table.lower_boundary_count << 20); 998 999 if (tu_calc_table.boundary_moderation_en) 1000 valid_boundary2 |= BIT(0); 1001 1002 pr_debug("dp_tu=0x%x, valid_boundary=0x%x, valid_boundary2=0x%x\n", 1003 msm_dp_tu, valid_boundary, valid_boundary2); 1004 1005 msm_dp_catalog_ctrl_update_transfer_unit(ctrl->catalog, 1006 msm_dp_tu, valid_boundary, valid_boundary2); 1007 } 1008 1009 static int msm_dp_ctrl_wait4video_ready(struct msm_dp_ctrl_private *ctrl) 1010 { 1011 int ret = 0; 1012 1013 if (!wait_for_completion_timeout(&ctrl->video_comp, 1014 WAIT_FOR_VIDEO_READY_TIMEOUT_JIFFIES)) { 1015 DRM_ERROR("wait4video timedout\n"); 1016 ret = -ETIMEDOUT; 1017 } 1018 return ret; 1019 } 1020 1021 static int msm_dp_ctrl_set_vx_px(struct msm_dp_ctrl_private *ctrl, 1022 u8 v_level, u8 p_level) 1023 { 1024 union phy_configure_opts *phy_opts = &ctrl->phy_opts; 1025 1026 /* TODO: Update for all lanes instead of just first one */ 1027 phy_opts->dp.voltage[0] = v_level; 1028 phy_opts->dp.pre[0] = p_level; 1029 phy_opts->dp.set_voltages = 1; 1030 phy_configure(ctrl->phy, phy_opts); 1031 phy_opts->dp.set_voltages = 0; 1032 1033 return 0; 1034 } 1035 1036 static int msm_dp_ctrl_update_vx_px(struct msm_dp_ctrl_private *ctrl) 1037 { 1038 struct msm_dp_link *link = ctrl->link; 1039 int ret = 0, lane, lane_cnt; 1040 u8 buf[4]; 1041 u32 max_level_reached = 0; 1042 u32 voltage_swing_level = link->phy_params.v_level; 1043 u32 pre_emphasis_level = link->phy_params.p_level; 1044 1045 drm_dbg_dp(ctrl->drm_dev, 1046 "voltage level: %d emphasis level: %d\n", 1047 voltage_swing_level, pre_emphasis_level); 1048 ret = msm_dp_ctrl_set_vx_px(ctrl, 1049 voltage_swing_level, pre_emphasis_level); 1050 1051 if (ret) 1052 return ret; 1053 1054 if (voltage_swing_level >= DP_TRAIN_LEVEL_MAX) { 1055 drm_dbg_dp(ctrl->drm_dev, 1056 "max. voltage swing level reached %d\n", 1057 voltage_swing_level); 1058 max_level_reached |= DP_TRAIN_MAX_SWING_REACHED; 1059 } 1060 1061 if (pre_emphasis_level >= DP_TRAIN_LEVEL_MAX) { 1062 drm_dbg_dp(ctrl->drm_dev, 1063 "max. pre-emphasis level reached %d\n", 1064 pre_emphasis_level); 1065 max_level_reached |= DP_TRAIN_MAX_PRE_EMPHASIS_REACHED; 1066 } 1067 1068 pre_emphasis_level <<= DP_TRAIN_PRE_EMPHASIS_SHIFT; 1069 1070 lane_cnt = ctrl->link->link_params.num_lanes; 1071 for (lane = 0; lane < lane_cnt; lane++) 1072 buf[lane] = voltage_swing_level | pre_emphasis_level 1073 | max_level_reached; 1074 1075 drm_dbg_dp(ctrl->drm_dev, "sink: p|v=0x%x\n", 1076 voltage_swing_level | pre_emphasis_level); 1077 ret = drm_dp_dpcd_write(ctrl->aux, DP_TRAINING_LANE0_SET, 1078 buf, lane_cnt); 1079 if (ret == lane_cnt) 1080 ret = 0; 1081 1082 return ret; 1083 } 1084 1085 static bool msm_dp_ctrl_train_pattern_set(struct msm_dp_ctrl_private *ctrl, 1086 u8 pattern) 1087 { 1088 u8 buf; 1089 int ret = 0; 1090 1091 drm_dbg_dp(ctrl->drm_dev, "sink: pattern=%x\n", pattern); 1092 1093 buf = pattern; 1094 1095 if (pattern && pattern != DP_TRAINING_PATTERN_4) 1096 buf |= DP_LINK_SCRAMBLING_DISABLE; 1097 1098 ret = drm_dp_dpcd_writeb(ctrl->aux, DP_TRAINING_PATTERN_SET, buf); 1099 return ret == 1; 1100 } 1101 1102 static int msm_dp_ctrl_link_train_1(struct msm_dp_ctrl_private *ctrl, 1103 int *training_step) 1104 { 1105 int tries, old_v_level, ret = 0; 1106 u8 link_status[DP_LINK_STATUS_SIZE]; 1107 int const maximum_retries = 4; 1108 1109 msm_dp_catalog_ctrl_state_ctrl(ctrl->catalog, 0); 1110 1111 *training_step = DP_TRAINING_1; 1112 1113 ret = msm_dp_catalog_ctrl_set_pattern_state_bit(ctrl->catalog, 1); 1114 if (ret) 1115 return ret; 1116 msm_dp_ctrl_train_pattern_set(ctrl, DP_TRAINING_PATTERN_1 | 1117 DP_LINK_SCRAMBLING_DISABLE); 1118 1119 ret = msm_dp_ctrl_update_vx_px(ctrl); 1120 if (ret) 1121 return ret; 1122 1123 tries = 0; 1124 old_v_level = ctrl->link->phy_params.v_level; 1125 for (tries = 0; tries < maximum_retries; tries++) { 1126 drm_dp_link_train_clock_recovery_delay(ctrl->aux, ctrl->panel->dpcd); 1127 1128 ret = drm_dp_dpcd_read_link_status(ctrl->aux, link_status); 1129 if (ret) 1130 return ret; 1131 1132 if (drm_dp_clock_recovery_ok(link_status, 1133 ctrl->link->link_params.num_lanes)) { 1134 return 0; 1135 } 1136 1137 if (ctrl->link->phy_params.v_level >= 1138 DP_TRAIN_LEVEL_MAX) { 1139 DRM_ERROR_RATELIMITED("max v_level reached\n"); 1140 return -EAGAIN; 1141 } 1142 1143 if (old_v_level != ctrl->link->phy_params.v_level) { 1144 tries = 0; 1145 old_v_level = ctrl->link->phy_params.v_level; 1146 } 1147 1148 msm_dp_link_adjust_levels(ctrl->link, link_status); 1149 ret = msm_dp_ctrl_update_vx_px(ctrl); 1150 if (ret) 1151 return ret; 1152 } 1153 1154 DRM_ERROR("max tries reached\n"); 1155 return -ETIMEDOUT; 1156 } 1157 1158 static int msm_dp_ctrl_link_rate_down_shift(struct msm_dp_ctrl_private *ctrl) 1159 { 1160 int ret = 0; 1161 1162 switch (ctrl->link->link_params.rate) { 1163 case 810000: 1164 ctrl->link->link_params.rate = 540000; 1165 break; 1166 case 540000: 1167 ctrl->link->link_params.rate = 270000; 1168 break; 1169 case 270000: 1170 ctrl->link->link_params.rate = 162000; 1171 break; 1172 case 162000: 1173 default: 1174 ret = -EINVAL; 1175 break; 1176 } 1177 1178 if (!ret) { 1179 drm_dbg_dp(ctrl->drm_dev, "new rate=0x%x\n", 1180 ctrl->link->link_params.rate); 1181 } 1182 1183 return ret; 1184 } 1185 1186 static int msm_dp_ctrl_link_lane_down_shift(struct msm_dp_ctrl_private *ctrl) 1187 { 1188 1189 if (ctrl->link->link_params.num_lanes == 1) 1190 return -1; 1191 1192 ctrl->link->link_params.num_lanes /= 2; 1193 ctrl->link->link_params.rate = ctrl->panel->link_info.rate; 1194 1195 ctrl->link->phy_params.p_level = 0; 1196 ctrl->link->phy_params.v_level = 0; 1197 1198 return 0; 1199 } 1200 1201 static void msm_dp_ctrl_clear_training_pattern(struct msm_dp_ctrl_private *ctrl) 1202 { 1203 msm_dp_ctrl_train_pattern_set(ctrl, DP_TRAINING_PATTERN_DISABLE); 1204 drm_dp_link_train_channel_eq_delay(ctrl->aux, ctrl->panel->dpcd); 1205 } 1206 1207 static int msm_dp_ctrl_link_train_2(struct msm_dp_ctrl_private *ctrl, 1208 int *training_step) 1209 { 1210 int tries = 0, ret = 0; 1211 u8 pattern; 1212 u32 state_ctrl_bit; 1213 int const maximum_retries = 5; 1214 u8 link_status[DP_LINK_STATUS_SIZE]; 1215 1216 msm_dp_catalog_ctrl_state_ctrl(ctrl->catalog, 0); 1217 1218 *training_step = DP_TRAINING_2; 1219 1220 if (drm_dp_tps4_supported(ctrl->panel->dpcd)) { 1221 pattern = DP_TRAINING_PATTERN_4; 1222 state_ctrl_bit = 4; 1223 } else if (drm_dp_tps3_supported(ctrl->panel->dpcd)) { 1224 pattern = DP_TRAINING_PATTERN_3; 1225 state_ctrl_bit = 3; 1226 } else { 1227 pattern = DP_TRAINING_PATTERN_2; 1228 state_ctrl_bit = 2; 1229 } 1230 1231 ret = msm_dp_catalog_ctrl_set_pattern_state_bit(ctrl->catalog, state_ctrl_bit); 1232 if (ret) 1233 return ret; 1234 1235 msm_dp_ctrl_train_pattern_set(ctrl, pattern); 1236 1237 for (tries = 0; tries <= maximum_retries; tries++) { 1238 drm_dp_link_train_channel_eq_delay(ctrl->aux, ctrl->panel->dpcd); 1239 1240 ret = drm_dp_dpcd_read_link_status(ctrl->aux, link_status); 1241 if (ret) 1242 return ret; 1243 1244 if (drm_dp_channel_eq_ok(link_status, 1245 ctrl->link->link_params.num_lanes)) { 1246 return 0; 1247 } 1248 1249 msm_dp_link_adjust_levels(ctrl->link, link_status); 1250 ret = msm_dp_ctrl_update_vx_px(ctrl); 1251 if (ret) 1252 return ret; 1253 1254 } 1255 1256 return -ETIMEDOUT; 1257 } 1258 1259 static int msm_dp_ctrl_link_train(struct msm_dp_ctrl_private *ctrl, 1260 int *training_step) 1261 { 1262 int ret = 0; 1263 const u8 *dpcd = ctrl->panel->dpcd; 1264 u8 encoding[] = { 0, DP_SET_ANSI_8B10B }; 1265 u8 assr; 1266 struct msm_dp_link_info link_info = {0}; 1267 1268 msm_dp_ctrl_config_ctrl(ctrl); 1269 1270 link_info.num_lanes = ctrl->link->link_params.num_lanes; 1271 link_info.rate = ctrl->link->link_params.rate; 1272 link_info.capabilities = DP_LINK_CAP_ENHANCED_FRAMING; 1273 1274 msm_dp_link_reset_phy_params_vx_px(ctrl->link); 1275 1276 msm_dp_aux_link_configure(ctrl->aux, &link_info); 1277 1278 if (drm_dp_max_downspread(dpcd)) 1279 encoding[0] |= DP_SPREAD_AMP_0_5; 1280 1281 /* config DOWNSPREAD_CTRL and MAIN_LINK_CHANNEL_CODING_SET */ 1282 drm_dp_dpcd_write(ctrl->aux, DP_DOWNSPREAD_CTRL, encoding, 2); 1283 1284 if (drm_dp_alternate_scrambler_reset_cap(dpcd)) { 1285 assr = DP_ALTERNATE_SCRAMBLER_RESET_ENABLE; 1286 drm_dp_dpcd_write(ctrl->aux, DP_EDP_CONFIGURATION_SET, 1287 &assr, 1); 1288 } 1289 1290 ret = msm_dp_ctrl_link_train_1(ctrl, training_step); 1291 if (ret) { 1292 DRM_ERROR("link training #1 failed. ret=%d\n", ret); 1293 goto end; 1294 } 1295 1296 /* print success info as this is a result of user initiated action */ 1297 drm_dbg_dp(ctrl->drm_dev, "link training #1 successful\n"); 1298 1299 ret = msm_dp_ctrl_link_train_2(ctrl, training_step); 1300 if (ret) { 1301 DRM_ERROR("link training #2 failed. ret=%d\n", ret); 1302 goto end; 1303 } 1304 1305 /* print success info as this is a result of user initiated action */ 1306 drm_dbg_dp(ctrl->drm_dev, "link training #2 successful\n"); 1307 1308 end: 1309 msm_dp_catalog_ctrl_state_ctrl(ctrl->catalog, 0); 1310 1311 return ret; 1312 } 1313 1314 static int msm_dp_ctrl_setup_main_link(struct msm_dp_ctrl_private *ctrl, 1315 int *training_step) 1316 { 1317 int ret = 0; 1318 1319 msm_dp_catalog_ctrl_mainlink_ctrl(ctrl->catalog, true); 1320 1321 if (ctrl->link->sink_request & DP_TEST_LINK_PHY_TEST_PATTERN) 1322 return ret; 1323 1324 /* 1325 * As part of previous calls, DP controller state might have 1326 * transitioned to PUSH_IDLE. In order to start transmitting 1327 * a link training pattern, we have to first do soft reset. 1328 */ 1329 1330 ret = msm_dp_ctrl_link_train(ctrl, training_step); 1331 1332 return ret; 1333 } 1334 1335 int msm_dp_ctrl_core_clk_enable(struct msm_dp_ctrl *msm_dp_ctrl) 1336 { 1337 struct msm_dp_ctrl_private *ctrl; 1338 int ret = 0; 1339 1340 ctrl = container_of(msm_dp_ctrl, struct msm_dp_ctrl_private, msm_dp_ctrl); 1341 1342 if (ctrl->core_clks_on) { 1343 drm_dbg_dp(ctrl->drm_dev, "core clks already enabled\n"); 1344 return 0; 1345 } 1346 1347 ret = clk_bulk_prepare_enable(ctrl->num_core_clks, ctrl->core_clks); 1348 if (ret) 1349 return ret; 1350 1351 ctrl->core_clks_on = true; 1352 1353 drm_dbg_dp(ctrl->drm_dev, "enable core clocks \n"); 1354 drm_dbg_dp(ctrl->drm_dev, "stream_clks:%s link_clks:%s core_clks:%s\n", 1355 ctrl->stream_clks_on ? "on" : "off", 1356 ctrl->link_clks_on ? "on" : "off", 1357 ctrl->core_clks_on ? "on" : "off"); 1358 1359 return 0; 1360 } 1361 1362 void msm_dp_ctrl_core_clk_disable(struct msm_dp_ctrl *msm_dp_ctrl) 1363 { 1364 struct msm_dp_ctrl_private *ctrl; 1365 1366 ctrl = container_of(msm_dp_ctrl, struct msm_dp_ctrl_private, msm_dp_ctrl); 1367 1368 clk_bulk_disable_unprepare(ctrl->num_core_clks, ctrl->core_clks); 1369 1370 ctrl->core_clks_on = false; 1371 1372 drm_dbg_dp(ctrl->drm_dev, "disable core clocks \n"); 1373 drm_dbg_dp(ctrl->drm_dev, "stream_clks:%s link_clks:%s core_clks:%s\n", 1374 ctrl->stream_clks_on ? "on" : "off", 1375 ctrl->link_clks_on ? "on" : "off", 1376 ctrl->core_clks_on ? "on" : "off"); 1377 } 1378 1379 static int msm_dp_ctrl_link_clk_enable(struct msm_dp_ctrl *msm_dp_ctrl) 1380 { 1381 struct msm_dp_ctrl_private *ctrl; 1382 int ret = 0; 1383 1384 ctrl = container_of(msm_dp_ctrl, struct msm_dp_ctrl_private, msm_dp_ctrl); 1385 1386 if (ctrl->link_clks_on) { 1387 drm_dbg_dp(ctrl->drm_dev, "links clks already enabled\n"); 1388 return 0; 1389 } 1390 1391 if (!ctrl->core_clks_on) { 1392 drm_dbg_dp(ctrl->drm_dev, "Enable core clks before link clks\n"); 1393 1394 msm_dp_ctrl_core_clk_enable(msm_dp_ctrl); 1395 } 1396 1397 ret = clk_bulk_prepare_enable(ctrl->num_link_clks, ctrl->link_clks); 1398 if (ret) 1399 return ret; 1400 1401 ctrl->link_clks_on = true; 1402 1403 drm_dbg_dp(ctrl->drm_dev, "enable link clocks\n"); 1404 drm_dbg_dp(ctrl->drm_dev, "stream_clks:%s link_clks:%s core_clks:%s\n", 1405 ctrl->stream_clks_on ? "on" : "off", 1406 ctrl->link_clks_on ? "on" : "off", 1407 ctrl->core_clks_on ? "on" : "off"); 1408 1409 return 0; 1410 } 1411 1412 static void msm_dp_ctrl_link_clk_disable(struct msm_dp_ctrl *msm_dp_ctrl) 1413 { 1414 struct msm_dp_ctrl_private *ctrl; 1415 1416 ctrl = container_of(msm_dp_ctrl, struct msm_dp_ctrl_private, msm_dp_ctrl); 1417 1418 clk_bulk_disable_unprepare(ctrl->num_link_clks, ctrl->link_clks); 1419 1420 ctrl->link_clks_on = false; 1421 1422 drm_dbg_dp(ctrl->drm_dev, "disabled link clocks\n"); 1423 drm_dbg_dp(ctrl->drm_dev, "stream_clks:%s link_clks:%s core_clks:%s\n", 1424 ctrl->stream_clks_on ? "on" : "off", 1425 ctrl->link_clks_on ? "on" : "off", 1426 ctrl->core_clks_on ? "on" : "off"); 1427 } 1428 1429 static int msm_dp_ctrl_enable_mainlink_clocks(struct msm_dp_ctrl_private *ctrl) 1430 { 1431 int ret = 0; 1432 struct phy *phy = ctrl->phy; 1433 const u8 *dpcd = ctrl->panel->dpcd; 1434 1435 ctrl->phy_opts.dp.lanes = ctrl->link->link_params.num_lanes; 1436 ctrl->phy_opts.dp.link_rate = ctrl->link->link_params.rate / 100; 1437 ctrl->phy_opts.dp.ssc = drm_dp_max_downspread(dpcd); 1438 1439 phy_configure(phy, &ctrl->phy_opts); 1440 phy_power_on(phy); 1441 1442 dev_pm_opp_set_rate(ctrl->dev, ctrl->link->link_params.rate * 1000); 1443 ret = msm_dp_ctrl_link_clk_enable(&ctrl->msm_dp_ctrl); 1444 if (ret) 1445 DRM_ERROR("Unable to start link clocks. ret=%d\n", ret); 1446 1447 drm_dbg_dp(ctrl->drm_dev, "link rate=%d\n", ctrl->link->link_params.rate); 1448 1449 return ret; 1450 } 1451 1452 void msm_dp_ctrl_reset_irq_ctrl(struct msm_dp_ctrl *msm_dp_ctrl, bool enable) 1453 { 1454 struct msm_dp_ctrl_private *ctrl; 1455 1456 ctrl = container_of(msm_dp_ctrl, struct msm_dp_ctrl_private, msm_dp_ctrl); 1457 1458 msm_dp_catalog_ctrl_reset(ctrl->catalog); 1459 1460 /* 1461 * all dp controller programmable registers will not 1462 * be reset to default value after DP_SW_RESET 1463 * therefore interrupt mask bits have to be updated 1464 * to enable/disable interrupts 1465 */ 1466 msm_dp_catalog_ctrl_enable_irq(ctrl->catalog, enable); 1467 } 1468 1469 void msm_dp_ctrl_config_psr(struct msm_dp_ctrl *msm_dp_ctrl) 1470 { 1471 u8 cfg; 1472 struct msm_dp_ctrl_private *ctrl = container_of(msm_dp_ctrl, 1473 struct msm_dp_ctrl_private, msm_dp_ctrl); 1474 1475 if (!ctrl->panel->psr_cap.version) 1476 return; 1477 1478 msm_dp_catalog_ctrl_config_psr(ctrl->catalog); 1479 1480 cfg = DP_PSR_ENABLE; 1481 drm_dp_dpcd_write(ctrl->aux, DP_PSR_EN_CFG, &cfg, 1); 1482 } 1483 1484 void msm_dp_ctrl_set_psr(struct msm_dp_ctrl *msm_dp_ctrl, bool enter) 1485 { 1486 struct msm_dp_ctrl_private *ctrl = container_of(msm_dp_ctrl, 1487 struct msm_dp_ctrl_private, msm_dp_ctrl); 1488 1489 if (!ctrl->panel->psr_cap.version) 1490 return; 1491 1492 /* 1493 * When entering PSR, 1494 * 1. Send PSR enter SDP and wait for the PSR_UPDATE_INT 1495 * 2. Turn off video 1496 * 3. Disable the mainlink 1497 * 1498 * When exiting PSR, 1499 * 1. Enable the mainlink 1500 * 2. Send the PSR exit SDP 1501 */ 1502 if (enter) { 1503 reinit_completion(&ctrl->psr_op_comp); 1504 msm_dp_catalog_ctrl_set_psr(ctrl->catalog, true); 1505 1506 if (!wait_for_completion_timeout(&ctrl->psr_op_comp, 1507 PSR_OPERATION_COMPLETION_TIMEOUT_JIFFIES)) { 1508 DRM_ERROR("PSR_ENTRY timedout\n"); 1509 msm_dp_catalog_ctrl_set_psr(ctrl->catalog, false); 1510 return; 1511 } 1512 1513 msm_dp_ctrl_push_idle(msm_dp_ctrl); 1514 msm_dp_catalog_ctrl_state_ctrl(ctrl->catalog, 0); 1515 1516 msm_dp_catalog_ctrl_psr_mainlink_enable(ctrl->catalog, false); 1517 } else { 1518 msm_dp_catalog_ctrl_psr_mainlink_enable(ctrl->catalog, true); 1519 1520 msm_dp_catalog_ctrl_set_psr(ctrl->catalog, false); 1521 msm_dp_catalog_ctrl_state_ctrl(ctrl->catalog, DP_STATE_CTRL_SEND_VIDEO); 1522 msm_dp_ctrl_wait4video_ready(ctrl); 1523 msm_dp_catalog_ctrl_state_ctrl(ctrl->catalog, 0); 1524 } 1525 } 1526 1527 void msm_dp_ctrl_phy_init(struct msm_dp_ctrl *msm_dp_ctrl) 1528 { 1529 struct msm_dp_ctrl_private *ctrl; 1530 struct phy *phy; 1531 1532 ctrl = container_of(msm_dp_ctrl, struct msm_dp_ctrl_private, msm_dp_ctrl); 1533 phy = ctrl->phy; 1534 1535 msm_dp_catalog_ctrl_phy_reset(ctrl->catalog); 1536 phy_init(phy); 1537 1538 drm_dbg_dp(ctrl->drm_dev, "phy=%p init=%d power_on=%d\n", 1539 phy, phy->init_count, phy->power_count); 1540 } 1541 1542 void msm_dp_ctrl_phy_exit(struct msm_dp_ctrl *msm_dp_ctrl) 1543 { 1544 struct msm_dp_ctrl_private *ctrl; 1545 struct phy *phy; 1546 1547 ctrl = container_of(msm_dp_ctrl, struct msm_dp_ctrl_private, msm_dp_ctrl); 1548 phy = ctrl->phy; 1549 1550 msm_dp_catalog_ctrl_phy_reset(ctrl->catalog); 1551 phy_exit(phy); 1552 drm_dbg_dp(ctrl->drm_dev, "phy=%p init=%d power_on=%d\n", 1553 phy, phy->init_count, phy->power_count); 1554 } 1555 1556 static int msm_dp_ctrl_reinitialize_mainlink(struct msm_dp_ctrl_private *ctrl) 1557 { 1558 struct phy *phy = ctrl->phy; 1559 int ret = 0; 1560 1561 msm_dp_catalog_ctrl_mainlink_ctrl(ctrl->catalog, false); 1562 ctrl->phy_opts.dp.lanes = ctrl->link->link_params.num_lanes; 1563 phy_configure(phy, &ctrl->phy_opts); 1564 /* 1565 * Disable and re-enable the mainlink clock since the 1566 * link clock might have been adjusted as part of the 1567 * link maintenance. 1568 */ 1569 dev_pm_opp_set_rate(ctrl->dev, 0); 1570 1571 msm_dp_ctrl_link_clk_disable(&ctrl->msm_dp_ctrl); 1572 1573 phy_power_off(phy); 1574 /* hw recommended delay before re-enabling clocks */ 1575 msleep(20); 1576 1577 ret = msm_dp_ctrl_enable_mainlink_clocks(ctrl); 1578 if (ret) { 1579 DRM_ERROR("Failed to enable mainlink clks. ret=%d\n", ret); 1580 return ret; 1581 } 1582 1583 return ret; 1584 } 1585 1586 static int msm_dp_ctrl_deinitialize_mainlink(struct msm_dp_ctrl_private *ctrl) 1587 { 1588 struct phy *phy; 1589 1590 phy = ctrl->phy; 1591 1592 msm_dp_catalog_ctrl_mainlink_ctrl(ctrl->catalog, false); 1593 1594 msm_dp_catalog_ctrl_reset(ctrl->catalog); 1595 1596 dev_pm_opp_set_rate(ctrl->dev, 0); 1597 msm_dp_ctrl_link_clk_disable(&ctrl->msm_dp_ctrl); 1598 1599 phy_power_off(phy); 1600 1601 /* aux channel down, reinit phy */ 1602 phy_exit(phy); 1603 phy_init(phy); 1604 1605 drm_dbg_dp(ctrl->drm_dev, "phy=%p init=%d power_on=%d\n", 1606 phy, phy->init_count, phy->power_count); 1607 return 0; 1608 } 1609 1610 static int msm_dp_ctrl_link_maintenance(struct msm_dp_ctrl_private *ctrl) 1611 { 1612 int ret = 0; 1613 int training_step = DP_TRAINING_NONE; 1614 1615 msm_dp_ctrl_push_idle(&ctrl->msm_dp_ctrl); 1616 1617 ctrl->link->phy_params.p_level = 0; 1618 ctrl->link->phy_params.v_level = 0; 1619 1620 ret = msm_dp_ctrl_setup_main_link(ctrl, &training_step); 1621 if (ret) 1622 goto end; 1623 1624 msm_dp_ctrl_clear_training_pattern(ctrl); 1625 1626 msm_dp_catalog_ctrl_state_ctrl(ctrl->catalog, DP_STATE_CTRL_SEND_VIDEO); 1627 1628 ret = msm_dp_ctrl_wait4video_ready(ctrl); 1629 end: 1630 return ret; 1631 } 1632 1633 static bool msm_dp_ctrl_send_phy_test_pattern(struct msm_dp_ctrl_private *ctrl) 1634 { 1635 bool success = false; 1636 u32 pattern_sent = 0x0; 1637 u32 pattern_requested = ctrl->link->phy_params.phy_test_pattern_sel; 1638 1639 drm_dbg_dp(ctrl->drm_dev, "request: 0x%x\n", pattern_requested); 1640 1641 if (msm_dp_ctrl_set_vx_px(ctrl, 1642 ctrl->link->phy_params.v_level, 1643 ctrl->link->phy_params.p_level)) { 1644 DRM_ERROR("Failed to set v/p levels\n"); 1645 return false; 1646 } 1647 msm_dp_catalog_ctrl_send_phy_pattern(ctrl->catalog, pattern_requested); 1648 msm_dp_ctrl_update_vx_px(ctrl); 1649 msm_dp_link_send_test_response(ctrl->link); 1650 1651 pattern_sent = msm_dp_catalog_ctrl_read_phy_pattern(ctrl->catalog); 1652 1653 switch (pattern_sent) { 1654 case MR_LINK_TRAINING1: 1655 success = (pattern_requested == 1656 DP_PHY_TEST_PATTERN_D10_2); 1657 break; 1658 case MR_LINK_SYMBOL_ERM: 1659 success = ((pattern_requested == 1660 DP_PHY_TEST_PATTERN_ERROR_COUNT) || 1661 (pattern_requested == 1662 DP_PHY_TEST_PATTERN_CP2520)); 1663 break; 1664 case MR_LINK_PRBS7: 1665 success = (pattern_requested == 1666 DP_PHY_TEST_PATTERN_PRBS7); 1667 break; 1668 case MR_LINK_CUSTOM80: 1669 success = (pattern_requested == 1670 DP_PHY_TEST_PATTERN_80BIT_CUSTOM); 1671 break; 1672 case MR_LINK_TRAINING4: 1673 success = (pattern_requested == 1674 DP_PHY_TEST_PATTERN_SEL_MASK); 1675 break; 1676 default: 1677 success = false; 1678 } 1679 1680 drm_dbg_dp(ctrl->drm_dev, "%s: test->0x%x\n", 1681 success ? "success" : "failed", pattern_requested); 1682 return success; 1683 } 1684 1685 static int msm_dp_ctrl_process_phy_test_request(struct msm_dp_ctrl_private *ctrl) 1686 { 1687 int ret; 1688 unsigned long pixel_rate; 1689 1690 if (!ctrl->link->phy_params.phy_test_pattern_sel) { 1691 drm_dbg_dp(ctrl->drm_dev, 1692 "no test pattern selected by sink\n"); 1693 return 0; 1694 } 1695 1696 /* 1697 * The global reset will need DP link related clocks to be 1698 * running. Add the global reset just before disabling the 1699 * link clocks and core clocks. 1700 */ 1701 msm_dp_ctrl_off(&ctrl->msm_dp_ctrl); 1702 1703 ret = msm_dp_ctrl_on_link(&ctrl->msm_dp_ctrl); 1704 if (ret) { 1705 DRM_ERROR("failed to enable DP link controller\n"); 1706 return ret; 1707 } 1708 1709 pixel_rate = ctrl->panel->msm_dp_mode.drm_mode.clock; 1710 ret = clk_set_rate(ctrl->pixel_clk, pixel_rate * 1000); 1711 if (ret) { 1712 DRM_ERROR("Failed to set pixel clock rate. ret=%d\n", ret); 1713 return ret; 1714 } 1715 1716 if (ctrl->stream_clks_on) { 1717 drm_dbg_dp(ctrl->drm_dev, "pixel clks already enabled\n"); 1718 } else { 1719 ret = clk_prepare_enable(ctrl->pixel_clk); 1720 if (ret) { 1721 DRM_ERROR("Failed to start pixel clocks. ret=%d\n", ret); 1722 return ret; 1723 } 1724 ctrl->stream_clks_on = true; 1725 } 1726 1727 msm_dp_ctrl_send_phy_test_pattern(ctrl); 1728 1729 return 0; 1730 } 1731 1732 void msm_dp_ctrl_handle_sink_request(struct msm_dp_ctrl *msm_dp_ctrl) 1733 { 1734 struct msm_dp_ctrl_private *ctrl; 1735 u32 sink_request = 0x0; 1736 1737 if (!msm_dp_ctrl) { 1738 DRM_ERROR("invalid input\n"); 1739 return; 1740 } 1741 1742 ctrl = container_of(msm_dp_ctrl, struct msm_dp_ctrl_private, msm_dp_ctrl); 1743 sink_request = ctrl->link->sink_request; 1744 1745 if (sink_request & DP_TEST_LINK_PHY_TEST_PATTERN) { 1746 drm_dbg_dp(ctrl->drm_dev, "PHY_TEST_PATTERN request\n"); 1747 if (msm_dp_ctrl_process_phy_test_request(ctrl)) { 1748 DRM_ERROR("process phy_test_req failed\n"); 1749 return; 1750 } 1751 } 1752 1753 if (sink_request & DP_LINK_STATUS_UPDATED) { 1754 if (msm_dp_ctrl_link_maintenance(ctrl)) { 1755 DRM_ERROR("LM failed: TEST_LINK_TRAINING\n"); 1756 return; 1757 } 1758 } 1759 1760 if (sink_request & DP_TEST_LINK_TRAINING) { 1761 msm_dp_link_send_test_response(ctrl->link); 1762 if (msm_dp_ctrl_link_maintenance(ctrl)) { 1763 DRM_ERROR("LM failed: TEST_LINK_TRAINING\n"); 1764 return; 1765 } 1766 } 1767 } 1768 1769 static bool msm_dp_ctrl_clock_recovery_any_ok( 1770 const u8 link_status[DP_LINK_STATUS_SIZE], 1771 int lane_count) 1772 { 1773 int reduced_cnt; 1774 1775 if (lane_count <= 1) 1776 return false; 1777 1778 /* 1779 * only interested in the lane number after reduced 1780 * lane_count = 4, then only interested in 2 lanes 1781 * lane_count = 2, then only interested in 1 lane 1782 */ 1783 reduced_cnt = lane_count >> 1; 1784 1785 return drm_dp_clock_recovery_ok(link_status, reduced_cnt); 1786 } 1787 1788 static bool msm_dp_ctrl_channel_eq_ok(struct msm_dp_ctrl_private *ctrl) 1789 { 1790 u8 link_status[DP_LINK_STATUS_SIZE]; 1791 int num_lanes = ctrl->link->link_params.num_lanes; 1792 1793 drm_dp_dpcd_read_link_status(ctrl->aux, link_status); 1794 1795 return drm_dp_channel_eq_ok(link_status, num_lanes); 1796 } 1797 1798 int msm_dp_ctrl_on_link(struct msm_dp_ctrl *msm_dp_ctrl) 1799 { 1800 int rc = 0; 1801 struct msm_dp_ctrl_private *ctrl; 1802 u32 rate; 1803 int link_train_max_retries = 5; 1804 u32 const phy_cts_pixel_clk_khz = 148500; 1805 u8 link_status[DP_LINK_STATUS_SIZE]; 1806 unsigned int training_step; 1807 unsigned long pixel_rate; 1808 1809 if (!msm_dp_ctrl) 1810 return -EINVAL; 1811 1812 ctrl = container_of(msm_dp_ctrl, struct msm_dp_ctrl_private, msm_dp_ctrl); 1813 1814 rate = ctrl->panel->link_info.rate; 1815 pixel_rate = ctrl->panel->msm_dp_mode.drm_mode.clock; 1816 1817 msm_dp_ctrl_core_clk_enable(&ctrl->msm_dp_ctrl); 1818 1819 if (ctrl->link->sink_request & DP_TEST_LINK_PHY_TEST_PATTERN) { 1820 drm_dbg_dp(ctrl->drm_dev, 1821 "using phy test link parameters\n"); 1822 if (!pixel_rate) 1823 pixel_rate = phy_cts_pixel_clk_khz; 1824 } else { 1825 ctrl->link->link_params.rate = rate; 1826 ctrl->link->link_params.num_lanes = 1827 ctrl->panel->link_info.num_lanes; 1828 if (ctrl->panel->msm_dp_mode.out_fmt_is_yuv_420) 1829 pixel_rate >>= 1; 1830 } 1831 1832 drm_dbg_dp(ctrl->drm_dev, "rate=%d, num_lanes=%d, pixel_rate=%lu\n", 1833 ctrl->link->link_params.rate, ctrl->link->link_params.num_lanes, 1834 pixel_rate); 1835 1836 rc = msm_dp_ctrl_enable_mainlink_clocks(ctrl); 1837 if (rc) 1838 return rc; 1839 1840 while (--link_train_max_retries) { 1841 training_step = DP_TRAINING_NONE; 1842 rc = msm_dp_ctrl_setup_main_link(ctrl, &training_step); 1843 if (rc == 0) { 1844 /* training completed successfully */ 1845 break; 1846 } else if (training_step == DP_TRAINING_1) { 1847 /* link train_1 failed */ 1848 if (!msm_dp_catalog_link_is_connected(ctrl->catalog)) 1849 break; 1850 1851 drm_dp_dpcd_read_link_status(ctrl->aux, link_status); 1852 1853 rc = msm_dp_ctrl_link_rate_down_shift(ctrl); 1854 if (rc < 0) { /* already in RBR = 1.6G */ 1855 if (msm_dp_ctrl_clock_recovery_any_ok(link_status, 1856 ctrl->link->link_params.num_lanes)) { 1857 /* 1858 * some lanes are ready, 1859 * reduce lane number 1860 */ 1861 rc = msm_dp_ctrl_link_lane_down_shift(ctrl); 1862 if (rc < 0) { /* lane == 1 already */ 1863 /* end with failure */ 1864 break; 1865 } 1866 } else { 1867 /* end with failure */ 1868 break; /* lane == 1 already */ 1869 } 1870 } 1871 } else if (training_step == DP_TRAINING_2) { 1872 /* link train_2 failed */ 1873 if (!msm_dp_catalog_link_is_connected(ctrl->catalog)) 1874 break; 1875 1876 drm_dp_dpcd_read_link_status(ctrl->aux, link_status); 1877 1878 if (!drm_dp_clock_recovery_ok(link_status, 1879 ctrl->link->link_params.num_lanes)) 1880 rc = msm_dp_ctrl_link_rate_down_shift(ctrl); 1881 else 1882 rc = msm_dp_ctrl_link_lane_down_shift(ctrl); 1883 1884 if (rc < 0) { 1885 /* end with failure */ 1886 break; /* lane == 1 already */ 1887 } 1888 1889 /* stop link training before start re training */ 1890 msm_dp_ctrl_clear_training_pattern(ctrl); 1891 } 1892 1893 rc = msm_dp_ctrl_reinitialize_mainlink(ctrl); 1894 if (rc) { 1895 DRM_ERROR("Failed to reinitialize mainlink. rc=%d\n", rc); 1896 break; 1897 } 1898 } 1899 1900 if (ctrl->link->sink_request & DP_TEST_LINK_PHY_TEST_PATTERN) 1901 return rc; 1902 1903 if (rc == 0) { /* link train successfully */ 1904 /* 1905 * do not stop train pattern here 1906 * stop link training at on_stream 1907 * to pass compliance test 1908 */ 1909 } else { 1910 /* 1911 * link training failed 1912 * end txing train pattern here 1913 */ 1914 msm_dp_ctrl_clear_training_pattern(ctrl); 1915 1916 msm_dp_ctrl_deinitialize_mainlink(ctrl); 1917 rc = -ECONNRESET; 1918 } 1919 1920 return rc; 1921 } 1922 1923 static int msm_dp_ctrl_link_retrain(struct msm_dp_ctrl_private *ctrl) 1924 { 1925 int training_step = DP_TRAINING_NONE; 1926 1927 return msm_dp_ctrl_setup_main_link(ctrl, &training_step); 1928 } 1929 1930 int msm_dp_ctrl_on_stream(struct msm_dp_ctrl *msm_dp_ctrl, bool force_link_train) 1931 { 1932 int ret = 0; 1933 bool mainlink_ready = false; 1934 struct msm_dp_ctrl_private *ctrl; 1935 unsigned long pixel_rate; 1936 unsigned long pixel_rate_orig; 1937 1938 if (!msm_dp_ctrl) 1939 return -EINVAL; 1940 1941 ctrl = container_of(msm_dp_ctrl, struct msm_dp_ctrl_private, msm_dp_ctrl); 1942 1943 pixel_rate = pixel_rate_orig = ctrl->panel->msm_dp_mode.drm_mode.clock; 1944 1945 if (msm_dp_ctrl->wide_bus_en || ctrl->panel->msm_dp_mode.out_fmt_is_yuv_420) 1946 pixel_rate >>= 1; 1947 1948 drm_dbg_dp(ctrl->drm_dev, "rate=%d, num_lanes=%d, pixel_rate=%lu\n", 1949 ctrl->link->link_params.rate, 1950 ctrl->link->link_params.num_lanes, pixel_rate); 1951 1952 drm_dbg_dp(ctrl->drm_dev, 1953 "core_clk_on=%d link_clk_on=%d stream_clk_on=%d\n", 1954 ctrl->core_clks_on, ctrl->link_clks_on, ctrl->stream_clks_on); 1955 1956 if (!ctrl->link_clks_on) { /* link clk is off */ 1957 ret = msm_dp_ctrl_enable_mainlink_clocks(ctrl); 1958 if (ret) { 1959 DRM_ERROR("Failed to start link clocks. ret=%d\n", ret); 1960 goto end; 1961 } 1962 } 1963 1964 ret = clk_set_rate(ctrl->pixel_clk, pixel_rate * 1000); 1965 if (ret) { 1966 DRM_ERROR("Failed to set pixel clock rate. ret=%d\n", ret); 1967 goto end; 1968 } 1969 1970 if (ctrl->stream_clks_on) { 1971 drm_dbg_dp(ctrl->drm_dev, "pixel clks already enabled\n"); 1972 } else { 1973 ret = clk_prepare_enable(ctrl->pixel_clk); 1974 if (ret) { 1975 DRM_ERROR("Failed to start pixel clocks. ret=%d\n", ret); 1976 goto end; 1977 } 1978 ctrl->stream_clks_on = true; 1979 } 1980 1981 if (force_link_train || !msm_dp_ctrl_channel_eq_ok(ctrl)) 1982 msm_dp_ctrl_link_retrain(ctrl); 1983 1984 /* stop txing train pattern to end link training */ 1985 msm_dp_ctrl_clear_training_pattern(ctrl); 1986 1987 /* 1988 * Set up transfer unit values and set controller state to send 1989 * video. 1990 */ 1991 reinit_completion(&ctrl->video_comp); 1992 1993 msm_dp_ctrl_configure_source_params(ctrl); 1994 1995 msm_dp_catalog_ctrl_config_msa(ctrl->catalog, 1996 ctrl->link->link_params.rate, 1997 pixel_rate_orig, 1998 ctrl->panel->msm_dp_mode.out_fmt_is_yuv_420); 1999 2000 msm_dp_ctrl_setup_tr_unit(ctrl); 2001 2002 msm_dp_catalog_ctrl_state_ctrl(ctrl->catalog, DP_STATE_CTRL_SEND_VIDEO); 2003 2004 ret = msm_dp_ctrl_wait4video_ready(ctrl); 2005 if (ret) 2006 return ret; 2007 2008 mainlink_ready = msm_dp_catalog_ctrl_mainlink_ready(ctrl->catalog); 2009 drm_dbg_dp(ctrl->drm_dev, 2010 "mainlink %s\n", mainlink_ready ? "READY" : "NOT READY"); 2011 2012 end: 2013 return ret; 2014 } 2015 2016 void msm_dp_ctrl_off_link_stream(struct msm_dp_ctrl *msm_dp_ctrl) 2017 { 2018 struct msm_dp_ctrl_private *ctrl; 2019 struct phy *phy; 2020 2021 ctrl = container_of(msm_dp_ctrl, struct msm_dp_ctrl_private, msm_dp_ctrl); 2022 phy = ctrl->phy; 2023 2024 msm_dp_catalog_panel_disable_vsc_sdp(ctrl->catalog); 2025 2026 /* set dongle to D3 (power off) mode */ 2027 msm_dp_link_psm_config(ctrl->link, &ctrl->panel->link_info, true); 2028 2029 msm_dp_catalog_ctrl_mainlink_ctrl(ctrl->catalog, false); 2030 2031 if (ctrl->stream_clks_on) { 2032 clk_disable_unprepare(ctrl->pixel_clk); 2033 ctrl->stream_clks_on = false; 2034 } 2035 2036 dev_pm_opp_set_rate(ctrl->dev, 0); 2037 msm_dp_ctrl_link_clk_disable(&ctrl->msm_dp_ctrl); 2038 2039 phy_power_off(phy); 2040 2041 /* aux channel down, reinit phy */ 2042 phy_exit(phy); 2043 phy_init(phy); 2044 2045 drm_dbg_dp(ctrl->drm_dev, "phy=%p init=%d power_on=%d\n", 2046 phy, phy->init_count, phy->power_count); 2047 } 2048 2049 void msm_dp_ctrl_off_link(struct msm_dp_ctrl *msm_dp_ctrl) 2050 { 2051 struct msm_dp_ctrl_private *ctrl; 2052 struct phy *phy; 2053 2054 ctrl = container_of(msm_dp_ctrl, struct msm_dp_ctrl_private, msm_dp_ctrl); 2055 phy = ctrl->phy; 2056 2057 msm_dp_catalog_ctrl_mainlink_ctrl(ctrl->catalog, false); 2058 2059 dev_pm_opp_set_rate(ctrl->dev, 0); 2060 msm_dp_ctrl_link_clk_disable(&ctrl->msm_dp_ctrl); 2061 2062 DRM_DEBUG_DP("Before, phy=%p init_count=%d power_on=%d\n", 2063 phy, phy->init_count, phy->power_count); 2064 2065 phy_power_off(phy); 2066 2067 DRM_DEBUG_DP("After, phy=%p init_count=%d power_on=%d\n", 2068 phy, phy->init_count, phy->power_count); 2069 } 2070 2071 void msm_dp_ctrl_off(struct msm_dp_ctrl *msm_dp_ctrl) 2072 { 2073 struct msm_dp_ctrl_private *ctrl; 2074 struct phy *phy; 2075 2076 ctrl = container_of(msm_dp_ctrl, struct msm_dp_ctrl_private, msm_dp_ctrl); 2077 phy = ctrl->phy; 2078 2079 msm_dp_catalog_panel_disable_vsc_sdp(ctrl->catalog); 2080 2081 msm_dp_catalog_ctrl_mainlink_ctrl(ctrl->catalog, false); 2082 2083 msm_dp_catalog_ctrl_reset(ctrl->catalog); 2084 2085 if (ctrl->stream_clks_on) { 2086 clk_disable_unprepare(ctrl->pixel_clk); 2087 ctrl->stream_clks_on = false; 2088 } 2089 2090 dev_pm_opp_set_rate(ctrl->dev, 0); 2091 msm_dp_ctrl_link_clk_disable(&ctrl->msm_dp_ctrl); 2092 2093 phy_power_off(phy); 2094 drm_dbg_dp(ctrl->drm_dev, "phy=%p init=%d power_on=%d\n", 2095 phy, phy->init_count, phy->power_count); 2096 } 2097 2098 irqreturn_t msm_dp_ctrl_isr(struct msm_dp_ctrl *msm_dp_ctrl) 2099 { 2100 struct msm_dp_ctrl_private *ctrl; 2101 u32 isr; 2102 irqreturn_t ret = IRQ_NONE; 2103 2104 if (!msm_dp_ctrl) 2105 return IRQ_NONE; 2106 2107 ctrl = container_of(msm_dp_ctrl, struct msm_dp_ctrl_private, msm_dp_ctrl); 2108 2109 if (ctrl->panel->psr_cap.version) { 2110 isr = msm_dp_catalog_ctrl_read_psr_interrupt_status(ctrl->catalog); 2111 2112 if (isr) 2113 complete(&ctrl->psr_op_comp); 2114 2115 if (isr & PSR_EXIT_INT) 2116 drm_dbg_dp(ctrl->drm_dev, "PSR exit done\n"); 2117 2118 if (isr & PSR_UPDATE_INT) 2119 drm_dbg_dp(ctrl->drm_dev, "PSR frame update done\n"); 2120 2121 if (isr & PSR_CAPTURE_INT) 2122 drm_dbg_dp(ctrl->drm_dev, "PSR frame capture done\n"); 2123 } 2124 2125 isr = msm_dp_catalog_ctrl_get_interrupt(ctrl->catalog); 2126 2127 2128 if (isr & DP_CTRL_INTR_READY_FOR_VIDEO) { 2129 drm_dbg_dp(ctrl->drm_dev, "dp_video_ready\n"); 2130 complete(&ctrl->video_comp); 2131 ret = IRQ_HANDLED; 2132 } 2133 2134 if (isr & DP_CTRL_INTR_IDLE_PATTERN_SENT) { 2135 drm_dbg_dp(ctrl->drm_dev, "idle_patterns_sent\n"); 2136 complete(&ctrl->idle_comp); 2137 ret = IRQ_HANDLED; 2138 } 2139 2140 return ret; 2141 } 2142 2143 static const char *core_clks[] = { 2144 "core_iface", 2145 "core_aux", 2146 }; 2147 2148 static const char *ctrl_clks[] = { 2149 "ctrl_link", 2150 "ctrl_link_iface", 2151 }; 2152 2153 static int msm_dp_ctrl_clk_init(struct msm_dp_ctrl *msm_dp_ctrl) 2154 { 2155 struct msm_dp_ctrl_private *ctrl; 2156 struct device *dev; 2157 int i, rc; 2158 2159 ctrl = container_of(msm_dp_ctrl, struct msm_dp_ctrl_private, msm_dp_ctrl); 2160 dev = ctrl->dev; 2161 2162 ctrl->num_core_clks = ARRAY_SIZE(core_clks); 2163 ctrl->core_clks = devm_kcalloc(dev, ctrl->num_core_clks, sizeof(*ctrl->core_clks), GFP_KERNEL); 2164 if (!ctrl->core_clks) 2165 return -ENOMEM; 2166 2167 for (i = 0; i < ctrl->num_core_clks; i++) 2168 ctrl->core_clks[i].id = core_clks[i]; 2169 2170 rc = devm_clk_bulk_get(dev, ctrl->num_core_clks, ctrl->core_clks); 2171 if (rc) 2172 return rc; 2173 2174 ctrl->num_link_clks = ARRAY_SIZE(ctrl_clks); 2175 ctrl->link_clks = devm_kcalloc(dev, ctrl->num_link_clks, sizeof(*ctrl->link_clks), GFP_KERNEL); 2176 if (!ctrl->link_clks) 2177 return -ENOMEM; 2178 2179 for (i = 0; i < ctrl->num_link_clks; i++) 2180 ctrl->link_clks[i].id = ctrl_clks[i]; 2181 2182 rc = devm_clk_bulk_get(dev, ctrl->num_link_clks, ctrl->link_clks); 2183 if (rc) 2184 return rc; 2185 2186 ctrl->pixel_clk = devm_clk_get(dev, "stream_pixel"); 2187 if (IS_ERR(ctrl->pixel_clk)) 2188 return PTR_ERR(ctrl->pixel_clk); 2189 2190 return 0; 2191 } 2192 2193 struct msm_dp_ctrl *msm_dp_ctrl_get(struct device *dev, struct msm_dp_link *link, 2194 struct msm_dp_panel *panel, struct drm_dp_aux *aux, 2195 struct msm_dp_catalog *catalog, 2196 struct phy *phy) 2197 { 2198 struct msm_dp_ctrl_private *ctrl; 2199 int ret; 2200 2201 if (!dev || !panel || !aux || 2202 !link || !catalog) { 2203 DRM_ERROR("invalid input\n"); 2204 return ERR_PTR(-EINVAL); 2205 } 2206 2207 ctrl = devm_kzalloc(dev, sizeof(*ctrl), GFP_KERNEL); 2208 if (!ctrl) { 2209 DRM_ERROR("Mem allocation failure\n"); 2210 return ERR_PTR(-ENOMEM); 2211 } 2212 2213 ret = devm_pm_opp_set_clkname(dev, "ctrl_link"); 2214 if (ret) { 2215 dev_err(dev, "invalid DP OPP table in device tree\n"); 2216 /* caller do PTR_ERR(opp_table) */ 2217 return (struct msm_dp_ctrl *)ERR_PTR(ret); 2218 } 2219 2220 /* OPP table is optional */ 2221 ret = devm_pm_opp_of_add_table(dev); 2222 if (ret) 2223 dev_err(dev, "failed to add DP OPP table\n"); 2224 2225 init_completion(&ctrl->idle_comp); 2226 init_completion(&ctrl->psr_op_comp); 2227 init_completion(&ctrl->video_comp); 2228 2229 /* in parameters */ 2230 ctrl->panel = panel; 2231 ctrl->aux = aux; 2232 ctrl->link = link; 2233 ctrl->catalog = catalog; 2234 ctrl->dev = dev; 2235 ctrl->phy = phy; 2236 2237 ret = msm_dp_ctrl_clk_init(&ctrl->msm_dp_ctrl); 2238 if (ret) { 2239 dev_err(dev, "failed to init clocks\n"); 2240 return ERR_PTR(ret); 2241 } 2242 2243 return &ctrl->msm_dp_ctrl; 2244 } 2245