1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2012-2020, The Linux Foundation. All rights reserved.
6 #define pr_fmt(fmt) "[drm-dp] %s: " fmt, __func__
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 <drm/drm_fixed.h>
14 #include <drm/drm_dp_helper.h>
15 #include <drm/drm_print.h>
21 #define DP_KHZ_TO_HZ 1000
22 #define IDLE_PATTERN_COMPLETION_TIMEOUT_JIFFIES (30 * HZ / 1000) /* 30 ms */
23 #define WAIT_FOR_VIDEO_READY_TIMEOUT_JIFFIES (HZ / 2)
25 #define DP_CTRL_INTR_READY_FOR_VIDEO BIT(0)
26 #define DP_CTRL_INTR_IDLE_PATTERN_SENT BIT(3)
28 #define MR_LINK_TRAINING1 0x8
29 #define MR_LINK_SYMBOL_ERM 0x80
30 #define MR_LINK_PRBS7 0x100
31 #define MR_LINK_CUSTOM80 0x200
32 #define MR_LINK_TRAINING4 0x40
40 struct dp_tu_calc_input {
41 u64 lclk; /* 162, 270, 540 and 810 */
42 u64 pclk_khz; /* in KHz */
43 u64 hactive; /* active h-width */
44 u64 hporch; /* bp + fp + pulse */
45 int nlanes; /* no.of.lanes */
47 int pixel_enc; /* 444, 420, 422 */
48 int dsc_en; /* dsc on/off */
49 int async_en; /* async mode */
51 int compress_ratio; /* 2:1 = 200, 3:1 = 300, 3.75:1 = 375 */
52 int num_of_dsc_slices; /* number of slices per line */
55 struct dp_vc_tu_mapping_table {
58 u8 lrate; /* DP_LINK_RATE -> 162(6), 270(10), 540(20), 810 (30) */
60 u8 valid_boundary_link;
62 bool boundary_moderation_en;
63 u8 valid_lower_boundary_link;
64 u8 upper_boundary_count;
65 u8 lower_boundary_count;
69 struct dp_ctrl_private {
70 struct dp_ctrl dp_ctrl;
72 struct drm_dp_aux *aux;
73 struct dp_panel *panel;
75 struct dp_power *power;
76 struct dp_parser *parser;
77 struct dp_catalog *catalog;
79 struct completion idle_comp;
80 struct completion video_comp;
88 #define DP_LANE0_1_CR_DONE 0x11
90 static int dp_aux_link_configure(struct drm_dp_aux *aux,
91 struct dp_link_info *link)
96 values[0] = drm_dp_link_rate_to_bw_code(link->rate);
97 values[1] = link->num_lanes;
99 if (link->capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
100 values[1] |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
102 err = drm_dp_dpcd_write(aux, DP_LINK_BW_SET, values, sizeof(values));
109 void dp_ctrl_push_idle(struct dp_ctrl *dp_ctrl)
111 struct dp_ctrl_private *ctrl;
113 ctrl = container_of(dp_ctrl, struct dp_ctrl_private, dp_ctrl);
115 reinit_completion(&ctrl->idle_comp);
116 dp_catalog_ctrl_state_ctrl(ctrl->catalog, DP_STATE_CTRL_PUSH_IDLE);
118 if (!wait_for_completion_timeout(&ctrl->idle_comp,
119 IDLE_PATTERN_COMPLETION_TIMEOUT_JIFFIES))
120 pr_warn("PUSH_IDLE pattern timedout\n");
122 pr_debug("mainlink off done\n");
125 static void dp_ctrl_config_ctrl(struct dp_ctrl_private *ctrl)
128 u8 *dpcd = ctrl->panel->dpcd;
130 /* Default-> LSCLK DIV: 1/4 LCLK */
131 config |= (2 << DP_CONFIGURATION_CTRL_LSCLK_DIV_SHIFT);
133 /* Scrambler reset enable */
134 if (dpcd[DP_EDP_CONFIGURATION_CAP] & DP_ALTERNATE_SCRAMBLER_RESET_CAP)
135 config |= DP_CONFIGURATION_CTRL_ASSR;
137 tbd = dp_link_get_test_bits_depth(ctrl->link,
138 ctrl->panel->dp_mode.bpp);
140 if (tbd == DP_TEST_BIT_DEPTH_UNKNOWN) {
141 pr_debug("BIT_DEPTH not set. Configure default\n");
142 tbd = DP_TEST_BIT_DEPTH_8;
145 config |= tbd << DP_CONFIGURATION_CTRL_BPC_SHIFT;
148 config |= ((ctrl->link->link_params.num_lanes - 1)
149 << DP_CONFIGURATION_CTRL_NUM_OF_LANES_SHIFT);
151 if (drm_dp_enhanced_frame_cap(dpcd))
152 config |= DP_CONFIGURATION_CTRL_ENHANCED_FRAMING;
154 config |= DP_CONFIGURATION_CTRL_P_INTERLACED; /* progressive video */
156 /* sync clock & static Mvid */
157 config |= DP_CONFIGURATION_CTRL_STATIC_DYNAMIC_CN;
158 config |= DP_CONFIGURATION_CTRL_SYNC_ASYNC_CLK;
160 dp_catalog_ctrl_config_ctrl(ctrl->catalog, config);
163 static void dp_ctrl_configure_source_params(struct dp_ctrl_private *ctrl)
167 dp_catalog_ctrl_lane_mapping(ctrl->catalog);
168 dp_catalog_ctrl_mainlink_ctrl(ctrl->catalog, true);
170 dp_ctrl_config_ctrl(ctrl);
172 tb = dp_link_get_test_bits_depth(ctrl->link,
173 ctrl->panel->dp_mode.bpp);
174 cc = dp_link_get_colorimetry_config(ctrl->link);
175 dp_catalog_ctrl_config_misc(ctrl->catalog, cc, tb);
176 dp_panel_timing_cfg(ctrl->panel);
180 * The structure and few functions present below are IP/Hardware
181 * specific implementation. Most of the implementation will not
182 * have coding comments
184 struct tu_algo_data {
189 s64 hbp_relative_to_pclk;
190 s64 hbp_relative_to_pclk_fp;
198 uint delay_start_link_extra_pixclk;
199 int extra_buffer_margin;
201 s64 original_ratio_fp;
210 int valid_boundary_link;
211 s64 resulting_valid_fp;
213 s64 effective_valid_fp;
214 s64 effective_valid_recorded_fp;
219 int remainder_tus_upper;
220 int remainder_tus_lower;
223 int delay_start_link;
225 int extra_pclk_cycles;
226 int extra_pclk_cycles_in_link_clk;
228 s64 average_valid2_fp;
229 int new_valid_boundary_link;
230 int remainder_symbols_exist;
232 s64 n_remainder_symbols_per_lane_fp;
233 s64 last_partial_tu_fp;
236 int n_tus_incl_last_incomplete_tu;
237 int extra_pclk_cycles_tmp;
238 int extra_pclk_cycles_in_link_clk_tmp;
239 int extra_required_bytes_new_tmp;
241 int lower_filler_size_tmp;
242 int delay_start_link_tmp;
244 bool boundary_moderation_en;
245 int boundary_mod_lower_err;
246 int upper_boundary_count;
247 int lower_boundary_count;
248 int i_upper_boundary_count;
249 int i_lower_boundary_count;
250 int valid_lower_boundary_link;
251 int even_distribution_BF;
252 int even_distribution_legacy;
253 int even_distribution;
254 int min_hblank_violated;
255 s64 delay_start_time_fp;
263 static int _tu_param_compare(s64 a, s64 b)
267 s64 a_temp, b_temp, minus_1;
272 minus_1 = drm_fixp_from_fraction(-1, 1);
274 a_sign = (a >> 32) & 0x80000000 ? 1 : 0;
276 b_sign = (b >> 32) & 0x80000000 ? 1 : 0;
280 else if (b_sign > a_sign)
283 if (!a_sign && !b_sign) { /* positive */
288 } else { /* negative */
289 a_temp = drm_fixp_mul(a, minus_1);
290 b_temp = drm_fixp_mul(b, minus_1);
299 static void dp_panel_update_tu_timings(struct dp_tu_calc_input *in,
300 struct tu_algo_data *tu)
302 int nlanes = in->nlanes;
303 int dsc_num_slices = in->num_of_dsc_slices;
304 int dsc_num_bytes = 0;
310 int tot_num_eoc_symbols = 0;
311 int tot_num_hor_bytes = 0;
312 int tot_num_dummy_bytes = 0;
313 int dwidth_dsc_bytes = 0;
316 s64 temp1_fp, temp2_fp, temp3_fp;
318 tu->lclk_fp = drm_fixp_from_fraction(in->lclk, 1);
319 tu->pclk_fp = drm_fixp_from_fraction(in->pclk_khz, 1000);
320 tu->lwidth = in->hactive;
321 tu->hbp_relative_to_pclk = in->hporch;
322 tu->nlanes = in->nlanes;
324 tu->pixelEnc = in->pixel_enc;
325 tu->dsc_en = in->dsc_en;
326 tu->async_en = in->async_en;
327 tu->lwidth_fp = drm_fixp_from_fraction(in->hactive, 1);
328 tu->hbp_relative_to_pclk_fp = drm_fixp_from_fraction(in->hporch, 1);
330 if (tu->pixelEnc == 420) {
331 temp1_fp = drm_fixp_from_fraction(2, 1);
332 tu->pclk_fp = drm_fixp_div(tu->pclk_fp, temp1_fp);
333 tu->lwidth_fp = drm_fixp_div(tu->lwidth_fp, temp1_fp);
334 tu->hbp_relative_to_pclk_fp =
335 drm_fixp_div(tu->hbp_relative_to_pclk_fp, 2);
338 if (tu->pixelEnc == 422) {
360 temp1_fp = drm_fixp_from_fraction(in->compress_ratio, 100);
361 temp2_fp = drm_fixp_from_fraction(in->bpp, 1);
362 temp3_fp = drm_fixp_div(temp2_fp, temp1_fp);
363 temp2_fp = drm_fixp_mul(tu->lwidth_fp, temp3_fp);
365 temp1_fp = drm_fixp_from_fraction(8, 1);
366 temp3_fp = drm_fixp_div(temp2_fp, temp1_fp);
368 numerator = drm_fixp2int(temp3_fp);
370 dsc_num_bytes = numerator / dsc_num_slices;
371 eoc_bytes = dsc_num_bytes % nlanes;
372 tot_num_eoc_symbols = nlanes * dsc_num_slices;
373 tot_num_hor_bytes = dsc_num_bytes * dsc_num_slices;
374 tot_num_dummy_bytes = (nlanes - eoc_bytes) * dsc_num_slices;
376 if (dsc_num_bytes == 0)
377 pr_info("incorrect no of bytes per slice=%d\n", dsc_num_bytes);
379 dwidth_dsc_bytes = (tot_num_hor_bytes +
380 tot_num_eoc_symbols +
381 (eoc_bytes == 0 ? 0 : tot_num_dummy_bytes));
383 dwidth_dsc_fp = drm_fixp_from_fraction(dwidth_dsc_bytes, 3);
385 temp2_fp = drm_fixp_mul(tu->pclk_fp, dwidth_dsc_fp);
386 temp1_fp = drm_fixp_div(temp2_fp, tu->lwidth_fp);
387 pclk_dsc_fp = temp1_fp;
389 temp1_fp = drm_fixp_div(pclk_dsc_fp, tu->pclk_fp);
390 temp2_fp = drm_fixp_mul(tu->hbp_relative_to_pclk_fp, temp1_fp);
391 hbp_dsc_fp = temp2_fp;
394 tu->pclk_fp = pclk_dsc_fp;
395 tu->lwidth_fp = dwidth_dsc_fp;
396 tu->hbp_relative_to_pclk_fp = hbp_dsc_fp;
400 temp1_fp = drm_fixp_from_fraction(976, 1000); /* 0.976 */
401 tu->lclk_fp = drm_fixp_mul(tu->lclk_fp, temp1_fp);
405 static void _tu_valid_boundary_calc(struct tu_algo_data *tu)
407 s64 temp1_fp, temp2_fp, temp, temp1, temp2;
408 int compare_result_1, compare_result_2, compare_result_3;
410 temp1_fp = drm_fixp_from_fraction(tu->tu_size, 1);
411 temp2_fp = drm_fixp_mul(tu->ratio_fp, temp1_fp);
413 tu->new_valid_boundary_link = drm_fixp2int_ceil(temp2_fp);
415 temp = (tu->i_upper_boundary_count *
416 tu->new_valid_boundary_link +
417 tu->i_lower_boundary_count *
418 (tu->new_valid_boundary_link-1));
419 tu->average_valid2_fp = drm_fixp_from_fraction(temp,
420 (tu->i_upper_boundary_count +
421 tu->i_lower_boundary_count));
423 temp1_fp = drm_fixp_from_fraction(tu->bpp, 8);
424 temp2_fp = tu->lwidth_fp;
425 temp1_fp = drm_fixp_mul(temp2_fp, temp1_fp);
426 temp2_fp = drm_fixp_div(temp1_fp, tu->average_valid2_fp);
427 tu->n_tus = drm_fixp2int(temp2_fp);
428 if ((temp2_fp & 0xFFFFFFFF) > 0xFFFFF000)
431 temp1_fp = drm_fixp_from_fraction(tu->n_tus, 1);
432 temp2_fp = drm_fixp_mul(temp1_fp, tu->average_valid2_fp);
433 temp1_fp = drm_fixp_from_fraction(tu->n_symbols, 1);
434 temp2_fp = temp1_fp - temp2_fp;
435 temp1_fp = drm_fixp_from_fraction(tu->nlanes, 1);
436 temp2_fp = drm_fixp_div(temp2_fp, temp1_fp);
437 tu->n_remainder_symbols_per_lane_fp = temp2_fp;
439 temp1_fp = drm_fixp_from_fraction(tu->tu_size, 1);
440 tu->last_partial_tu_fp =
441 drm_fixp_div(tu->n_remainder_symbols_per_lane_fp,
444 if (tu->n_remainder_symbols_per_lane_fp != 0)
445 tu->remainder_symbols_exist = 1;
447 tu->remainder_symbols_exist = 0;
449 temp1_fp = drm_fixp_from_fraction(tu->n_tus, tu->nlanes);
450 tu->n_tus_per_lane = drm_fixp2int(temp1_fp);
452 tu->paired_tus = (int)((tu->n_tus_per_lane) /
453 (tu->i_upper_boundary_count +
454 tu->i_lower_boundary_count));
456 tu->remainder_tus = tu->n_tus_per_lane - tu->paired_tus *
457 (tu->i_upper_boundary_count +
458 tu->i_lower_boundary_count);
460 if ((tu->remainder_tus - tu->i_upper_boundary_count) > 0) {
461 tu->remainder_tus_upper = tu->i_upper_boundary_count;
462 tu->remainder_tus_lower = tu->remainder_tus -
463 tu->i_upper_boundary_count;
465 tu->remainder_tus_upper = tu->remainder_tus;
466 tu->remainder_tus_lower = 0;
469 temp = tu->paired_tus * (tu->i_upper_boundary_count *
470 tu->new_valid_boundary_link +
471 tu->i_lower_boundary_count *
472 (tu->new_valid_boundary_link - 1)) +
473 (tu->remainder_tus_upper *
474 tu->new_valid_boundary_link) +
475 (tu->remainder_tus_lower *
476 (tu->new_valid_boundary_link - 1));
477 tu->total_valid_fp = drm_fixp_from_fraction(temp, 1);
479 if (tu->remainder_symbols_exist) {
480 temp1_fp = tu->total_valid_fp +
481 tu->n_remainder_symbols_per_lane_fp;
482 temp2_fp = drm_fixp_from_fraction(tu->n_tus_per_lane, 1);
483 temp2_fp = temp2_fp + tu->last_partial_tu_fp;
484 temp1_fp = drm_fixp_div(temp1_fp, temp2_fp);
486 temp2_fp = drm_fixp_from_fraction(tu->n_tus_per_lane, 1);
487 temp1_fp = drm_fixp_div(tu->total_valid_fp, temp2_fp);
489 tu->effective_valid_fp = temp1_fp;
491 temp1_fp = drm_fixp_from_fraction(tu->tu_size, 1);
492 temp2_fp = drm_fixp_mul(tu->ratio_fp, temp1_fp);
493 tu->n_n_err_fp = tu->effective_valid_fp - temp2_fp;
495 temp1_fp = drm_fixp_from_fraction(tu->tu_size, 1);
496 temp2_fp = drm_fixp_mul(tu->ratio_fp, temp1_fp);
497 tu->n_err_fp = tu->average_valid2_fp - temp2_fp;
499 tu->even_distribution = tu->n_tus % tu->nlanes == 0 ? 1 : 0;
501 temp1_fp = drm_fixp_from_fraction(tu->bpp, 8);
502 temp2_fp = tu->lwidth_fp;
503 temp1_fp = drm_fixp_mul(temp2_fp, temp1_fp);
504 temp2_fp = drm_fixp_div(temp1_fp, tu->average_valid2_fp);
507 tu->n_tus_incl_last_incomplete_tu = drm_fixp2int_ceil(temp2_fp);
509 tu->n_tus_incl_last_incomplete_tu = 0;
512 temp1_fp = drm_fixp_from_fraction(tu->tu_size, 1);
513 temp2_fp = drm_fixp_mul(tu->original_ratio_fp, temp1_fp);
514 temp1_fp = tu->average_valid2_fp - temp2_fp;
515 temp2_fp = drm_fixp_from_fraction(tu->n_tus_incl_last_incomplete_tu, 1);
516 temp1_fp = drm_fixp_mul(temp2_fp, temp1_fp);
519 temp1 = drm_fixp2int_ceil(temp1_fp);
521 temp = tu->i_upper_boundary_count * tu->nlanes;
522 temp1_fp = drm_fixp_from_fraction(tu->tu_size, 1);
523 temp2_fp = drm_fixp_mul(tu->original_ratio_fp, temp1_fp);
524 temp1_fp = drm_fixp_from_fraction(tu->new_valid_boundary_link, 1);
525 temp2_fp = temp1_fp - temp2_fp;
526 temp1_fp = drm_fixp_from_fraction(temp, 1);
527 temp2_fp = drm_fixp_mul(temp1_fp, temp2_fp);
530 temp2 = drm_fixp2int_ceil(temp2_fp);
533 tu->extra_required_bytes_new_tmp = (int)(temp1 + temp2);
535 temp1_fp = drm_fixp_from_fraction(8, tu->bpp);
536 temp2_fp = drm_fixp_from_fraction(
537 tu->extra_required_bytes_new_tmp, 1);
538 temp1_fp = drm_fixp_mul(temp2_fp, temp1_fp);
541 tu->extra_pclk_cycles_tmp = drm_fixp2int_ceil(temp1_fp);
543 tu->extra_pclk_cycles_tmp = 0;
545 temp1_fp = drm_fixp_from_fraction(tu->extra_pclk_cycles_tmp, 1);
546 temp2_fp = drm_fixp_div(tu->lclk_fp, tu->pclk_fp);
547 temp1_fp = drm_fixp_mul(temp1_fp, temp2_fp);
550 tu->extra_pclk_cycles_in_link_clk_tmp =
551 drm_fixp2int_ceil(temp1_fp);
553 tu->extra_pclk_cycles_in_link_clk_tmp = 0;
555 tu->filler_size_tmp = tu->tu_size - tu->new_valid_boundary_link;
557 tu->lower_filler_size_tmp = tu->filler_size_tmp + 1;
559 tu->delay_start_link_tmp = tu->extra_pclk_cycles_in_link_clk_tmp +
560 tu->lower_filler_size_tmp +
561 tu->extra_buffer_margin;
563 temp1_fp = drm_fixp_from_fraction(tu->delay_start_link_tmp, 1);
564 tu->delay_start_time_fp = drm_fixp_div(temp1_fp, tu->lclk_fp);
566 compare_result_1 = _tu_param_compare(tu->n_n_err_fp, tu->diff_abs_fp);
567 if (compare_result_1 == 2)
568 compare_result_1 = 1;
570 compare_result_1 = 0;
572 compare_result_2 = _tu_param_compare(tu->n_n_err_fp, tu->err_fp);
573 if (compare_result_2 == 2)
574 compare_result_2 = 1;
576 compare_result_2 = 0;
578 compare_result_3 = _tu_param_compare(tu->hbp_time_fp,
579 tu->delay_start_time_fp);
580 if (compare_result_3 == 2)
581 compare_result_3 = 0;
583 compare_result_3 = 1;
585 if (((tu->even_distribution == 1) ||
586 ((tu->even_distribution_BF == 0) &&
587 (tu->even_distribution_legacy == 0))) &&
588 tu->n_err_fp >= 0 && tu->n_n_err_fp >= 0 &&
590 (compare_result_1 || (tu->min_hblank_violated == 1)) &&
591 (tu->new_valid_boundary_link - 1) > 0 &&
593 (tu->delay_start_link_tmp <= 1023)) {
594 tu->upper_boundary_count = tu->i_upper_boundary_count;
595 tu->lower_boundary_count = tu->i_lower_boundary_count;
596 tu->err_fp = tu->n_n_err_fp;
597 tu->boundary_moderation_en = true;
598 tu->tu_size_desired = tu->tu_size;
599 tu->valid_boundary_link = tu->new_valid_boundary_link;
600 tu->effective_valid_recorded_fp = tu->effective_valid_fp;
601 tu->even_distribution_BF = 1;
602 tu->delay_start_link = tu->delay_start_link_tmp;
603 } else if (tu->boundary_mod_lower_err == 0) {
604 compare_result_1 = _tu_param_compare(tu->n_n_err_fp,
606 if (compare_result_1 == 2)
607 tu->boundary_mod_lower_err = 1;
611 static void _dp_ctrl_calc_tu(struct dp_tu_calc_input *in,
612 struct dp_vc_tu_mapping_table *tu_table)
614 struct tu_algo_data tu;
615 int compare_result_1, compare_result_2;
617 s64 temp_fp = 0, temp1_fp = 0, temp2_fp = 0;
619 s64 LCLK_FAST_SKEW_fp = drm_fixp_from_fraction(6, 10000); /* 0.0006 */
620 s64 const_p49_fp = drm_fixp_from_fraction(49, 100); /* 0.49 */
621 s64 const_p56_fp = drm_fixp_from_fraction(56, 100); /* 0.56 */
622 s64 RATIO_SCALE_fp = drm_fixp_from_fraction(1001, 1000);
624 u8 DP_BRUTE_FORCE = 1;
625 s64 BRUTE_FORCE_THRESHOLD_fp = drm_fixp_from_fraction(1, 10); /* 0.1 */
626 uint EXTRA_PIXCLK_CYCLE_DELAY = 4;
627 uint HBLANK_MARGIN = 4;
629 memset(&tu, 0, sizeof(tu));
631 dp_panel_update_tu_timings(in, &tu);
633 tu.err_fp = drm_fixp_from_fraction(1000, 1); /* 1000 */
635 temp1_fp = drm_fixp_from_fraction(4, 1);
636 temp2_fp = drm_fixp_mul(temp1_fp, tu.lclk_fp);
637 temp_fp = drm_fixp_div(temp2_fp, tu.pclk_fp);
638 tu.extra_buffer_margin = drm_fixp2int_ceil(temp_fp);
640 temp1_fp = drm_fixp_from_fraction(tu.bpp, 8);
641 temp2_fp = drm_fixp_mul(tu.pclk_fp, temp1_fp);
642 temp1_fp = drm_fixp_from_fraction(tu.nlanes, 1);
643 temp2_fp = drm_fixp_div(temp2_fp, temp1_fp);
644 tu.ratio_fp = drm_fixp_div(temp2_fp, tu.lclk_fp);
646 tu.original_ratio_fp = tu.ratio_fp;
647 tu.boundary_moderation_en = false;
648 tu.upper_boundary_count = 0;
649 tu.lower_boundary_count = 0;
650 tu.i_upper_boundary_count = 0;
651 tu.i_lower_boundary_count = 0;
652 tu.valid_lower_boundary_link = 0;
653 tu.even_distribution_BF = 0;
654 tu.even_distribution_legacy = 0;
655 tu.even_distribution = 0;
656 tu.delay_start_time_fp = 0;
658 tu.err_fp = drm_fixp_from_fraction(1000, 1);
662 tu.ratio = drm_fixp2int(tu.ratio_fp);
663 temp1_fp = drm_fixp_from_fraction(tu.nlanes, 1);
664 div64_u64_rem(tu.lwidth_fp, temp1_fp, &temp2_fp);
666 !tu.ratio && tu.dsc_en == 0) {
667 tu.ratio_fp = drm_fixp_mul(tu.ratio_fp, RATIO_SCALE_fp);
668 tu.ratio = drm_fixp2int(tu.ratio_fp);
670 tu.ratio_fp = drm_fixp_from_fraction(1, 1);
679 compare_result_1 = _tu_param_compare(tu.ratio_fp, const_p49_fp);
680 if (!compare_result_1 || compare_result_1 == 1)
681 compare_result_1 = 1;
683 compare_result_1 = 0;
685 compare_result_2 = _tu_param_compare(tu.ratio_fp, const_p56_fp);
686 if (!compare_result_2 || compare_result_2 == 2)
687 compare_result_2 = 1;
689 compare_result_2 = 0;
691 if (tu.dsc_en && compare_result_1 && compare_result_2) {
693 DRM_DEBUG_DP("Info: increase HBLANK_MARGIN to %d\n",
698 for (tu.tu_size = 32; tu.tu_size <= 64; tu.tu_size++) {
699 temp1_fp = drm_fixp_from_fraction(tu.tu_size, 1);
700 temp2_fp = drm_fixp_mul(tu.ratio_fp, temp1_fp);
701 temp = drm_fixp2int_ceil(temp2_fp);
702 temp1_fp = drm_fixp_from_fraction(temp, 1);
703 tu.n_err_fp = temp1_fp - temp2_fp;
705 if (tu.n_err_fp < tu.err_fp) {
706 tu.err_fp = tu.n_err_fp;
707 tu.tu_size_desired = tu.tu_size;
711 tu.tu_size_minus1 = tu.tu_size_desired - 1;
713 temp1_fp = drm_fixp_from_fraction(tu.tu_size_desired, 1);
714 temp2_fp = drm_fixp_mul(tu.ratio_fp, temp1_fp);
715 tu.valid_boundary_link = drm_fixp2int_ceil(temp2_fp);
717 temp1_fp = drm_fixp_from_fraction(tu.bpp, 8);
718 temp2_fp = tu.lwidth_fp;
719 temp2_fp = drm_fixp_mul(temp2_fp, temp1_fp);
721 temp1_fp = drm_fixp_from_fraction(tu.valid_boundary_link, 1);
722 temp2_fp = drm_fixp_div(temp2_fp, temp1_fp);
723 tu.n_tus = drm_fixp2int(temp2_fp);
724 if ((temp2_fp & 0xFFFFFFFF) > 0xFFFFF000)
727 tu.even_distribution_legacy = tu.n_tus % tu.nlanes == 0 ? 1 : 0;
728 DRM_DEBUG_DP("Info: n_sym = %d, num_of_tus = %d\n",
729 tu.valid_boundary_link, tu.n_tus);
731 temp1_fp = drm_fixp_from_fraction(tu.tu_size_desired, 1);
732 temp2_fp = drm_fixp_mul(tu.original_ratio_fp, temp1_fp);
733 temp1_fp = drm_fixp_from_fraction(tu.valid_boundary_link, 1);
734 temp2_fp = temp1_fp - temp2_fp;
735 temp1_fp = drm_fixp_from_fraction(tu.n_tus + 1, 1);
736 temp2_fp = drm_fixp_mul(temp1_fp, temp2_fp);
738 temp = drm_fixp2int(temp2_fp);
739 if (temp && temp2_fp)
740 tu.extra_bytes = drm_fixp2int_ceil(temp2_fp);
744 temp1_fp = drm_fixp_from_fraction(tu.extra_bytes, 1);
745 temp2_fp = drm_fixp_from_fraction(8, tu.bpp);
746 temp1_fp = drm_fixp_mul(temp1_fp, temp2_fp);
748 if (temp && temp1_fp)
749 tu.extra_pclk_cycles = drm_fixp2int_ceil(temp1_fp);
751 tu.extra_pclk_cycles = drm_fixp2int(temp1_fp);
753 temp1_fp = drm_fixp_div(tu.lclk_fp, tu.pclk_fp);
754 temp2_fp = drm_fixp_from_fraction(tu.extra_pclk_cycles, 1);
755 temp1_fp = drm_fixp_mul(temp2_fp, temp1_fp);
758 tu.extra_pclk_cycles_in_link_clk = drm_fixp2int_ceil(temp1_fp);
760 tu.extra_pclk_cycles_in_link_clk = drm_fixp2int(temp1_fp);
762 tu.filler_size = tu.tu_size_desired - tu.valid_boundary_link;
764 temp1_fp = drm_fixp_from_fraction(tu.tu_size_desired, 1);
765 tu.ratio_by_tu_fp = drm_fixp_mul(tu.ratio_fp, temp1_fp);
767 tu.delay_start_link = tu.extra_pclk_cycles_in_link_clk +
768 tu.filler_size + tu.extra_buffer_margin;
770 tu.resulting_valid_fp =
771 drm_fixp_from_fraction(tu.valid_boundary_link, 1);
773 temp1_fp = drm_fixp_from_fraction(tu.tu_size_desired, 1);
774 temp2_fp = drm_fixp_div(tu.resulting_valid_fp, temp1_fp);
775 tu.TU_ratio_err_fp = temp2_fp - tu.original_ratio_fp;
777 temp1_fp = drm_fixp_from_fraction(HBLANK_MARGIN, 1);
778 temp1_fp = tu.hbp_relative_to_pclk_fp - temp1_fp;
779 tu.hbp_time_fp = drm_fixp_div(temp1_fp, tu.pclk_fp);
781 temp1_fp = drm_fixp_from_fraction(tu.delay_start_link, 1);
782 tu.delay_start_time_fp = drm_fixp_div(temp1_fp, tu.lclk_fp);
784 compare_result_1 = _tu_param_compare(tu.hbp_time_fp,
785 tu.delay_start_time_fp);
786 if (compare_result_1 == 2) /* if (hbp_time_fp < delay_start_time_fp) */
787 tu.min_hblank_violated = 1;
789 tu.hactive_time_fp = drm_fixp_div(tu.lwidth_fp, tu.pclk_fp);
791 compare_result_2 = _tu_param_compare(tu.hactive_time_fp,
792 tu.delay_start_time_fp);
793 if (compare_result_2 == 2)
794 tu.min_hblank_violated = 1;
796 tu.delay_start_time_fp = 0;
800 tu.delay_start_link_extra_pixclk = EXTRA_PIXCLK_CYCLE_DELAY;
801 tu.diff_abs_fp = tu.resulting_valid_fp - tu.ratio_by_tu_fp;
803 temp = drm_fixp2int(tu.diff_abs_fp);
804 if (!temp && tu.diff_abs_fp <= 0xffff)
807 /* if(diff_abs < 0) diff_abs *= -1 */
808 if (tu.diff_abs_fp < 0)
809 tu.diff_abs_fp = drm_fixp_mul(tu.diff_abs_fp, -1);
811 tu.boundary_mod_lower_err = 0;
812 if ((tu.diff_abs_fp != 0 &&
813 ((tu.diff_abs_fp > BRUTE_FORCE_THRESHOLD_fp) ||
814 (tu.even_distribution_legacy == 0) ||
815 (DP_BRUTE_FORCE == 1))) ||
816 (tu.min_hblank_violated == 1)) {
818 tu.err_fp = drm_fixp_from_fraction(1000, 1);
820 temp1_fp = drm_fixp_div(tu.lclk_fp, tu.pclk_fp);
821 temp2_fp = drm_fixp_from_fraction(
822 tu.delay_start_link_extra_pixclk, 1);
823 temp1_fp = drm_fixp_mul(temp2_fp, temp1_fp);
826 tu.extra_buffer_margin =
827 drm_fixp2int_ceil(temp1_fp);
829 tu.extra_buffer_margin = 0;
831 temp1_fp = drm_fixp_from_fraction(tu.bpp, 8);
832 temp1_fp = drm_fixp_mul(tu.lwidth_fp, temp1_fp);
835 tu.n_symbols = drm_fixp2int_ceil(temp1_fp);
839 for (tu.tu_size = 32; tu.tu_size <= 64; tu.tu_size++) {
840 for (tu.i_upper_boundary_count = 1;
841 tu.i_upper_boundary_count <= 15;
842 tu.i_upper_boundary_count++) {
843 for (tu.i_lower_boundary_count = 1;
844 tu.i_lower_boundary_count <= 15;
845 tu.i_lower_boundary_count++) {
846 _tu_valid_boundary_calc(&tu);
850 tu.delay_start_link_extra_pixclk--;
851 } while (tu.boundary_moderation_en != true &&
852 tu.boundary_mod_lower_err == 1 &&
853 tu.delay_start_link_extra_pixclk != 0);
855 if (tu.boundary_moderation_en == true) {
856 temp1_fp = drm_fixp_from_fraction(
857 (tu.upper_boundary_count *
858 tu.valid_boundary_link +
859 tu.lower_boundary_count *
860 (tu.valid_boundary_link - 1)), 1);
861 temp2_fp = drm_fixp_from_fraction(
862 (tu.upper_boundary_count +
863 tu.lower_boundary_count), 1);
864 tu.resulting_valid_fp =
865 drm_fixp_div(temp1_fp, temp2_fp);
867 temp1_fp = drm_fixp_from_fraction(
868 tu.tu_size_desired, 1);
870 drm_fixp_mul(tu.original_ratio_fp, temp1_fp);
872 tu.valid_lower_boundary_link =
873 tu.valid_boundary_link - 1;
875 temp1_fp = drm_fixp_from_fraction(tu.bpp, 8);
876 temp1_fp = drm_fixp_mul(tu.lwidth_fp, temp1_fp);
877 temp2_fp = drm_fixp_div(temp1_fp,
878 tu.resulting_valid_fp);
879 tu.n_tus = drm_fixp2int(temp2_fp);
881 tu.tu_size_minus1 = tu.tu_size_desired - 1;
882 tu.even_distribution_BF = 1;
885 drm_fixp_from_fraction(tu.tu_size_desired, 1);
887 drm_fixp_div(tu.resulting_valid_fp, temp1_fp);
888 tu.TU_ratio_err_fp = temp2_fp - tu.original_ratio_fp;
892 temp2_fp = drm_fixp_mul(LCLK_FAST_SKEW_fp, tu.lwidth_fp);
895 temp = drm_fixp2int_ceil(temp2_fp);
899 temp1_fp = drm_fixp_from_fraction(tu.nlanes, 1);
900 temp2_fp = drm_fixp_mul(tu.original_ratio_fp, temp1_fp);
901 temp1_fp = drm_fixp_from_fraction(tu.bpp, 8);
902 temp2_fp = drm_fixp_div(temp1_fp, temp2_fp);
903 temp1_fp = drm_fixp_from_fraction(temp, 1);
904 temp2_fp = drm_fixp_mul(temp1_fp, temp2_fp);
905 temp = drm_fixp2int(temp2_fp);
908 tu.delay_start_link += (int)temp;
910 temp1_fp = drm_fixp_from_fraction(tu.delay_start_link, 1);
911 tu.delay_start_time_fp = drm_fixp_div(temp1_fp, tu.lclk_fp);
914 tu_table->valid_boundary_link = tu.valid_boundary_link;
915 tu_table->delay_start_link = tu.delay_start_link;
916 tu_table->boundary_moderation_en = tu.boundary_moderation_en;
917 tu_table->valid_lower_boundary_link = tu.valid_lower_boundary_link;
918 tu_table->upper_boundary_count = tu.upper_boundary_count;
919 tu_table->lower_boundary_count = tu.lower_boundary_count;
920 tu_table->tu_size_minus1 = tu.tu_size_minus1;
922 DRM_DEBUG_DP("TU: valid_boundary_link: %d\n",
923 tu_table->valid_boundary_link);
924 DRM_DEBUG_DP("TU: delay_start_link: %d\n",
925 tu_table->delay_start_link);
926 DRM_DEBUG_DP("TU: boundary_moderation_en: %d\n",
927 tu_table->boundary_moderation_en);
928 DRM_DEBUG_DP("TU: valid_lower_boundary_link: %d\n",
929 tu_table->valid_lower_boundary_link);
930 DRM_DEBUG_DP("TU: upper_boundary_count: %d\n",
931 tu_table->upper_boundary_count);
932 DRM_DEBUG_DP("TU: lower_boundary_count: %d\n",
933 tu_table->lower_boundary_count);
934 DRM_DEBUG_DP("TU: tu_size_minus1: %d\n", tu_table->tu_size_minus1);
937 static void dp_ctrl_calc_tu_parameters(struct dp_ctrl_private *ctrl,
938 struct dp_vc_tu_mapping_table *tu_table)
940 struct dp_tu_calc_input in;
941 struct drm_display_mode *drm_mode;
943 drm_mode = &ctrl->panel->dp_mode.drm_mode;
945 in.lclk = ctrl->link->link_params.rate / 1000;
946 in.pclk_khz = drm_mode->clock;
947 in.hactive = drm_mode->hdisplay;
948 in.hporch = drm_mode->htotal - drm_mode->hdisplay;
949 in.nlanes = ctrl->link->link_params.num_lanes;
950 in.bpp = ctrl->panel->dp_mode.bpp;
955 in.num_of_dsc_slices = 0;
956 in.compress_ratio = 100;
958 _dp_ctrl_calc_tu(&in, tu_table);
961 static void dp_ctrl_setup_tr_unit(struct dp_ctrl_private *ctrl)
964 u32 valid_boundary = 0x0;
965 u32 valid_boundary2 = 0x0;
966 struct dp_vc_tu_mapping_table tu_calc_table;
968 dp_ctrl_calc_tu_parameters(ctrl, &tu_calc_table);
970 dp_tu |= tu_calc_table.tu_size_minus1;
971 valid_boundary |= tu_calc_table.valid_boundary_link;
972 valid_boundary |= (tu_calc_table.delay_start_link << 16);
974 valid_boundary2 |= (tu_calc_table.valid_lower_boundary_link << 1);
975 valid_boundary2 |= (tu_calc_table.upper_boundary_count << 16);
976 valid_boundary2 |= (tu_calc_table.lower_boundary_count << 20);
978 if (tu_calc_table.boundary_moderation_en)
979 valid_boundary2 |= BIT(0);
981 pr_debug("dp_tu=0x%x, valid_boundary=0x%x, valid_boundary2=0x%x\n",
982 dp_tu, valid_boundary, valid_boundary2);
984 dp_catalog_ctrl_update_transfer_unit(ctrl->catalog,
985 dp_tu, valid_boundary, valid_boundary2);
988 static int dp_ctrl_wait4video_ready(struct dp_ctrl_private *ctrl)
992 if (!wait_for_completion_timeout(&ctrl->video_comp,
993 WAIT_FOR_VIDEO_READY_TIMEOUT_JIFFIES)) {
994 DRM_ERROR("wait4video timedout\n");
1000 static int dp_ctrl_update_vx_px(struct dp_ctrl_private *ctrl)
1002 struct dp_link *link = ctrl->link;
1003 int ret = 0, lane, lane_cnt;
1005 u32 max_level_reached = 0;
1006 u32 voltage_swing_level = link->phy_params.v_level;
1007 u32 pre_emphasis_level = link->phy_params.p_level;
1009 ret = dp_catalog_ctrl_update_vx_px(ctrl->catalog,
1010 voltage_swing_level, pre_emphasis_level);
1015 if (voltage_swing_level >= DP_TRAIN_VOLTAGE_SWING_MAX) {
1016 DRM_DEBUG_DP("max. voltage swing level reached %d\n",
1017 voltage_swing_level);
1018 max_level_reached |= DP_TRAIN_MAX_SWING_REACHED;
1021 if (pre_emphasis_level >= DP_TRAIN_PRE_EMPHASIS_MAX) {
1022 DRM_DEBUG_DP("max. pre-emphasis level reached %d\n",
1023 pre_emphasis_level);
1024 max_level_reached |= DP_TRAIN_MAX_PRE_EMPHASIS_REACHED;
1027 pre_emphasis_level <<= DP_TRAIN_PRE_EMPHASIS_SHIFT;
1029 lane_cnt = ctrl->link->link_params.num_lanes;
1030 for (lane = 0; lane < lane_cnt; lane++)
1031 buf[lane] = voltage_swing_level | pre_emphasis_level
1032 | max_level_reached;
1034 DRM_DEBUG_DP("sink: p|v=0x%x\n", voltage_swing_level
1035 | pre_emphasis_level);
1036 ret = drm_dp_dpcd_write(ctrl->aux, DP_TRAINING_LANE0_SET,
1038 if (ret == lane_cnt)
1044 static bool dp_ctrl_train_pattern_set(struct dp_ctrl_private *ctrl,
1050 DRM_DEBUG_DP("sink: pattern=%x\n", pattern);
1054 if (pattern && pattern != DP_TRAINING_PATTERN_4)
1055 buf |= DP_LINK_SCRAMBLING_DISABLE;
1057 ret = drm_dp_dpcd_writeb(ctrl->aux, DP_TRAINING_PATTERN_SET, buf);
1061 static int dp_ctrl_read_link_status(struct dp_ctrl_private *ctrl,
1065 u32 const offset = DP_LANE_ALIGN_STATUS_UPDATED - DP_LANE0_1_STATUS;
1066 u32 link_status_read_max_retries = 100;
1068 while (--link_status_read_max_retries) {
1069 len = drm_dp_dpcd_read_link_status(ctrl->aux,
1071 if (len != DP_LINK_STATUS_SIZE) {
1072 DRM_ERROR("DP link status read failed, err: %d\n", len);
1076 if (!(link_status[offset] & DP_LINK_STATUS_UPDATED))
1083 static int dp_ctrl_link_train_1(struct dp_ctrl_private *ctrl,
1084 struct dp_cr_status *cr, int *training_step)
1086 int tries, old_v_level, ret = 0;
1087 u8 link_status[DP_LINK_STATUS_SIZE];
1088 int const maximum_retries = 4;
1090 dp_catalog_ctrl_state_ctrl(ctrl->catalog, 0);
1092 *training_step = DP_TRAINING_1;
1094 ret = dp_catalog_ctrl_set_pattern(ctrl->catalog, DP_TRAINING_PATTERN_1);
1097 dp_ctrl_train_pattern_set(ctrl, DP_TRAINING_PATTERN_1 |
1098 DP_LINK_SCRAMBLING_DISABLE);
1100 ret = dp_ctrl_update_vx_px(ctrl);
1105 old_v_level = ctrl->link->phy_params.v_level;
1106 for (tries = 0; tries < maximum_retries; tries++) {
1107 drm_dp_link_train_clock_recovery_delay(ctrl->panel->dpcd);
1109 ret = dp_ctrl_read_link_status(ctrl, link_status);
1113 cr->lane_0_1 = link_status[0];
1114 cr->lane_2_3 = link_status[1];
1116 if (drm_dp_clock_recovery_ok(link_status,
1117 ctrl->link->link_params.num_lanes)) {
1121 if (ctrl->link->phy_params.v_level >=
1122 DP_TRAIN_VOLTAGE_SWING_MAX) {
1123 DRM_ERROR_RATELIMITED("max v_level reached\n");
1127 if (old_v_level != ctrl->link->phy_params.v_level) {
1129 old_v_level = ctrl->link->phy_params.v_level;
1132 DRM_DEBUG_DP("clock recovery not done, adjusting vx px\n");
1134 dp_link_adjust_levels(ctrl->link, link_status);
1135 ret = dp_ctrl_update_vx_px(ctrl);
1140 DRM_ERROR("max tries reached\n");
1144 static int dp_ctrl_link_rate_down_shift(struct dp_ctrl_private *ctrl)
1148 switch (ctrl->link->link_params.rate) {
1150 ctrl->link->link_params.rate = 540000;
1153 ctrl->link->link_params.rate = 270000;
1156 ctrl->link->link_params.rate = 162000;
1165 DRM_DEBUG_DP("new rate=0x%x\n", ctrl->link->link_params.rate);
1170 static int dp_ctrl_link_lane_down_shift(struct dp_ctrl_private *ctrl)
1173 if (ctrl->link->link_params.num_lanes == 1)
1176 ctrl->link->link_params.num_lanes /= 2;
1177 ctrl->link->link_params.rate = ctrl->panel->link_info.rate;
1179 ctrl->link->phy_params.p_level = 0;
1180 ctrl->link->phy_params.v_level = 0;
1185 static void dp_ctrl_clear_training_pattern(struct dp_ctrl_private *ctrl)
1187 dp_ctrl_train_pattern_set(ctrl, DP_TRAINING_PATTERN_DISABLE);
1188 drm_dp_link_train_channel_eq_delay(ctrl->panel->dpcd);
1191 static int dp_ctrl_link_train_2(struct dp_ctrl_private *ctrl,
1192 struct dp_cr_status *cr, int *training_step)
1194 int tries = 0, ret = 0;
1196 int const maximum_retries = 5;
1197 u8 link_status[DP_LINK_STATUS_SIZE];
1199 dp_catalog_ctrl_state_ctrl(ctrl->catalog, 0);
1201 *training_step = DP_TRAINING_2;
1203 if (drm_dp_tps3_supported(ctrl->panel->dpcd))
1204 pattern = DP_TRAINING_PATTERN_3;
1206 pattern = DP_TRAINING_PATTERN_2;
1208 ret = dp_ctrl_update_vx_px(ctrl);
1212 ret = dp_catalog_ctrl_set_pattern(ctrl->catalog, pattern);
1216 dp_ctrl_train_pattern_set(ctrl, pattern | DP_RECOVERED_CLOCK_OUT_EN);
1218 for (tries = 0; tries <= maximum_retries; tries++) {
1219 drm_dp_link_train_channel_eq_delay(ctrl->panel->dpcd);
1221 ret = dp_ctrl_read_link_status(ctrl, link_status);
1224 cr->lane_0_1 = link_status[0];
1225 cr->lane_2_3 = link_status[1];
1227 if (drm_dp_channel_eq_ok(link_status,
1228 ctrl->link->link_params.num_lanes)) {
1232 dp_link_adjust_levels(ctrl->link, link_status);
1233 ret = dp_ctrl_update_vx_px(ctrl);
1242 static int dp_ctrl_reinitialize_mainlink(struct dp_ctrl_private *ctrl);
1244 static int dp_ctrl_link_train(struct dp_ctrl_private *ctrl,
1245 struct dp_cr_status *cr, int *training_step)
1248 u8 encoding = DP_SET_ANSI_8B10B;
1249 struct dp_link_info link_info = {0};
1251 dp_ctrl_config_ctrl(ctrl);
1253 link_info.num_lanes = ctrl->link->link_params.num_lanes;
1254 link_info.rate = ctrl->link->link_params.rate;
1255 link_info.capabilities = DP_LINK_CAP_ENHANCED_FRAMING;
1257 dp_aux_link_configure(ctrl->aux, &link_info);
1258 drm_dp_dpcd_write(ctrl->aux, DP_MAIN_LINK_CHANNEL_CODING_SET,
1261 ret = dp_ctrl_link_train_1(ctrl, cr, training_step);
1263 DRM_ERROR("link training #1 failed. ret=%d\n", ret);
1267 /* print success info as this is a result of user initiated action */
1268 DRM_DEBUG_DP("link training #1 successful\n");
1270 ret = dp_ctrl_link_train_2(ctrl, cr, training_step);
1272 DRM_ERROR("link training #2 failed. ret=%d\n", ret);
1276 /* print success info as this is a result of user initiated action */
1277 DRM_DEBUG_DP("link training #2 successful\n");
1280 dp_catalog_ctrl_state_ctrl(ctrl->catalog, 0);
1285 static int dp_ctrl_setup_main_link(struct dp_ctrl_private *ctrl,
1286 struct dp_cr_status *cr, int *training_step)
1290 dp_catalog_ctrl_mainlink_ctrl(ctrl->catalog, true);
1292 if (ctrl->link->sink_request & DP_TEST_LINK_PHY_TEST_PATTERN)
1296 * As part of previous calls, DP controller state might have
1297 * transitioned to PUSH_IDLE. In order to start transmitting
1298 * a link training pattern, we have to first do soft reset.
1300 dp_catalog_ctrl_reset(ctrl->catalog);
1302 ret = dp_ctrl_link_train(ctrl, cr, training_step);
1307 static void dp_ctrl_set_clock_rate(struct dp_ctrl_private *ctrl,
1308 enum dp_pm_type module, char *name, unsigned long rate)
1310 u32 num = ctrl->parser->mp[module].num_clk;
1311 struct dss_clk *cfg = ctrl->parser->mp[module].clk_config;
1313 while (num && strcmp(cfg->clk_name, name)) {
1318 DRM_DEBUG_DP("setting rate=%lu on clk=%s\n", rate, name);
1323 DRM_ERROR("%s clock doesn't exit to set rate %lu\n",
1327 static int dp_ctrl_enable_mainlink_clocks(struct dp_ctrl_private *ctrl)
1330 struct dp_io *dp_io = &ctrl->parser->io;
1331 struct phy *phy = dp_io->phy;
1332 struct phy_configure_opts_dp *opts_dp = &dp_io->phy_opts.dp;
1334 opts_dp->lanes = ctrl->link->link_params.num_lanes;
1335 opts_dp->link_rate = ctrl->link->link_params.rate / 100;
1336 dp_ctrl_set_clock_rate(ctrl, DP_CTRL_PM, "ctrl_link",
1337 ctrl->link->link_params.rate * 1000);
1339 phy_configure(phy, &dp_io->phy_opts);
1342 ret = dp_power_clk_enable(ctrl->power, DP_CTRL_PM, true);
1344 DRM_ERROR("Unable to start link clocks. ret=%d\n", ret);
1346 DRM_DEBUG_DP("link rate=%d pixel_clk=%d\n",
1347 ctrl->link->link_params.rate, ctrl->dp_ctrl.pixel_rate);
1352 static int dp_ctrl_enable_stream_clocks(struct dp_ctrl_private *ctrl)
1356 dp_ctrl_set_clock_rate(ctrl, DP_STREAM_PM, "stream_pixel",
1357 ctrl->dp_ctrl.pixel_rate * 1000);
1359 ret = dp_power_clk_enable(ctrl->power, DP_STREAM_PM, true);
1361 DRM_ERROR("Unabled to start pixel clocks. ret=%d\n", ret);
1363 DRM_DEBUG_DP("link rate=%d pixel_clk=%d\n",
1364 ctrl->link->link_params.rate, ctrl->dp_ctrl.pixel_rate);
1369 int dp_ctrl_host_init(struct dp_ctrl *dp_ctrl, bool flip)
1371 struct dp_ctrl_private *ctrl;
1372 struct dp_io *dp_io;
1376 DRM_ERROR("Invalid input data\n");
1380 ctrl = container_of(dp_ctrl, struct dp_ctrl_private, dp_ctrl);
1381 dp_io = &ctrl->parser->io;
1384 ctrl->dp_ctrl.orientation = flip;
1386 dp_catalog_ctrl_phy_reset(ctrl->catalog);
1388 dp_catalog_ctrl_enable_irq(ctrl->catalog, true);
1394 * dp_ctrl_host_deinit() - Uninitialize DP controller
1395 * @dp_ctrl: Display Port Driver data
1397 * Perform required steps to uninitialize DP controller
1398 * and its resources.
1400 void dp_ctrl_host_deinit(struct dp_ctrl *dp_ctrl)
1402 struct dp_ctrl_private *ctrl;
1405 DRM_ERROR("Invalid input data\n");
1409 ctrl = container_of(dp_ctrl, struct dp_ctrl_private, dp_ctrl);
1411 dp_catalog_ctrl_enable_irq(ctrl->catalog, false);
1413 DRM_DEBUG_DP("Host deinitialized successfully\n");
1416 static bool dp_ctrl_use_fixed_nvid(struct dp_ctrl_private *ctrl)
1418 u8 *dpcd = ctrl->panel->dpcd;
1419 u32 edid_quirks = 0;
1421 edid_quirks = drm_dp_get_edid_quirks(ctrl->panel->edid);
1423 * For better interop experience, used a fixed NVID=0x8000
1424 * whenever connected to a VGA dongle downstream.
1426 if (drm_dp_is_branch(dpcd))
1427 return (drm_dp_has_quirk(&ctrl->panel->desc, edid_quirks,
1428 DP_DPCD_QUIRK_CONSTANT_N));
1433 static int dp_ctrl_reinitialize_mainlink(struct dp_ctrl_private *ctrl)
1436 struct dp_io *dp_io = &ctrl->parser->io;
1437 struct phy *phy = dp_io->phy;
1438 struct phy_configure_opts_dp *opts_dp = &dp_io->phy_opts.dp;
1440 dp_catalog_ctrl_mainlink_ctrl(ctrl->catalog, false);
1441 opts_dp->lanes = ctrl->link->link_params.num_lanes;
1442 phy_configure(phy, &dp_io->phy_opts);
1444 * Disable and re-enable the mainlink clock since the
1445 * link clock might have been adjusted as part of the
1448 ret = dp_power_clk_enable(ctrl->power, DP_CTRL_PM, false);
1450 DRM_ERROR("Failed to disable clocks. ret=%d\n", ret);
1454 /* hw recommended delay before re-enabling clocks */
1457 ret = dp_ctrl_enable_mainlink_clocks(ctrl);
1459 DRM_ERROR("Failed to enable mainlink clks. ret=%d\n", ret);
1466 static int dp_ctrl_link_maintenance(struct dp_ctrl_private *ctrl)
1469 struct dp_cr_status cr;
1470 int training_step = DP_TRAINING_NONE;
1472 dp_ctrl_push_idle(&ctrl->dp_ctrl);
1473 dp_catalog_ctrl_reset(ctrl->catalog);
1475 ctrl->dp_ctrl.pixel_rate = ctrl->panel->dp_mode.drm_mode.clock;
1477 ret = dp_ctrl_setup_main_link(ctrl, &cr, &training_step);
1481 dp_ctrl_clear_training_pattern(ctrl);
1483 dp_catalog_ctrl_state_ctrl(ctrl->catalog, DP_STATE_CTRL_SEND_VIDEO);
1485 ret = dp_ctrl_wait4video_ready(ctrl);
1490 static int dp_ctrl_process_phy_test_request(struct dp_ctrl_private *ctrl)
1494 if (!ctrl->link->phy_params.phy_test_pattern_sel) {
1495 DRM_DEBUG_DP("no test pattern selected by sink\n");
1500 * The global reset will need DP link related clocks to be
1501 * running. Add the global reset just before disabling the
1502 * link clocks and core clocks.
1504 ret = dp_ctrl_off(&ctrl->dp_ctrl);
1506 DRM_ERROR("failed to disable DP controller\n");
1510 ret = dp_ctrl_on_link(&ctrl->dp_ctrl);
1512 ret = dp_ctrl_on_stream(&ctrl->dp_ctrl);
1514 DRM_ERROR("failed to enable DP link controller\n");
1519 static bool dp_ctrl_send_phy_test_pattern(struct dp_ctrl_private *ctrl)
1521 bool success = false;
1522 u32 pattern_sent = 0x0;
1523 u32 pattern_requested = ctrl->link->phy_params.phy_test_pattern_sel;
1525 DRM_DEBUG_DP("request: 0x%x\n", pattern_requested);
1527 if (dp_catalog_ctrl_update_vx_px(ctrl->catalog,
1528 ctrl->link->phy_params.v_level,
1529 ctrl->link->phy_params.p_level)) {
1530 DRM_ERROR("Failed to set v/p levels\n");
1533 dp_catalog_ctrl_send_phy_pattern(ctrl->catalog, pattern_requested);
1534 dp_ctrl_update_vx_px(ctrl);
1535 dp_link_send_test_response(ctrl->link);
1537 pattern_sent = dp_catalog_ctrl_read_phy_pattern(ctrl->catalog);
1539 switch (pattern_sent) {
1540 case MR_LINK_TRAINING1:
1541 success = (pattern_requested ==
1542 DP_PHY_TEST_PATTERN_D10_2);
1544 case MR_LINK_SYMBOL_ERM:
1545 success = ((pattern_requested ==
1546 DP_PHY_TEST_PATTERN_ERROR_COUNT) ||
1547 (pattern_requested ==
1548 DP_PHY_TEST_PATTERN_CP2520));
1551 success = (pattern_requested ==
1552 DP_PHY_TEST_PATTERN_PRBS7);
1554 case MR_LINK_CUSTOM80:
1555 success = (pattern_requested ==
1556 DP_PHY_TEST_PATTERN_80BIT_CUSTOM);
1558 case MR_LINK_TRAINING4:
1559 success = (pattern_requested ==
1560 DP_PHY_TEST_PATTERN_SEL_MASK);
1566 DRM_DEBUG_DP("%s: test->0x%x\n", success ? "success" : "failed",
1571 void dp_ctrl_handle_sink_request(struct dp_ctrl *dp_ctrl)
1573 struct dp_ctrl_private *ctrl;
1574 u32 sink_request = 0x0;
1577 DRM_ERROR("invalid input\n");
1581 ctrl = container_of(dp_ctrl, struct dp_ctrl_private, dp_ctrl);
1582 sink_request = ctrl->link->sink_request;
1584 if (sink_request & DP_TEST_LINK_PHY_TEST_PATTERN) {
1585 DRM_DEBUG_DP("PHY_TEST_PATTERN request\n");
1586 if (dp_ctrl_process_phy_test_request(ctrl)) {
1587 DRM_ERROR("process phy_test_req failed\n");
1592 if (sink_request & DP_LINK_STATUS_UPDATED) {
1593 if (dp_ctrl_link_maintenance(ctrl)) {
1594 DRM_ERROR("LM failed: TEST_LINK_TRAINING\n");
1599 if (sink_request & DP_TEST_LINK_TRAINING) {
1600 dp_link_send_test_response(ctrl->link);
1601 if (dp_ctrl_link_maintenance(ctrl)) {
1602 DRM_ERROR("LM failed: TEST_LINK_TRAINING\n");
1608 int dp_ctrl_on_link(struct dp_ctrl *dp_ctrl)
1611 struct dp_ctrl_private *ctrl;
1613 int link_train_max_retries = 5;
1614 u32 const phy_cts_pixel_clk_khz = 148500;
1615 struct dp_cr_status cr;
1616 unsigned int training_step;
1621 ctrl = container_of(dp_ctrl, struct dp_ctrl_private, dp_ctrl);
1623 rate = ctrl->panel->link_info.rate;
1625 dp_power_clk_enable(ctrl->power, DP_CORE_PM, true);
1627 if (ctrl->link->sink_request & DP_TEST_LINK_PHY_TEST_PATTERN) {
1628 DRM_DEBUG_DP("using phy test link parameters\n");
1629 if (!ctrl->panel->dp_mode.drm_mode.clock)
1630 ctrl->dp_ctrl.pixel_rate = phy_cts_pixel_clk_khz;
1632 ctrl->link->link_params.rate = rate;
1633 ctrl->link->link_params.num_lanes =
1634 ctrl->panel->link_info.num_lanes;
1635 ctrl->dp_ctrl.pixel_rate = ctrl->panel->dp_mode.drm_mode.clock;
1638 DRM_DEBUG_DP("rate=%d, num_lanes=%d, pixel_rate=%d\n",
1639 ctrl->link->link_params.rate,
1640 ctrl->link->link_params.num_lanes, ctrl->dp_ctrl.pixel_rate);
1642 rc = dp_ctrl_enable_mainlink_clocks(ctrl);
1646 ctrl->link->phy_params.p_level = 0;
1647 ctrl->link->phy_params.v_level = 0;
1649 while (--link_train_max_retries &&
1650 !atomic_read(&ctrl->dp_ctrl.aborted)) {
1651 rc = dp_ctrl_reinitialize_mainlink(ctrl);
1653 DRM_ERROR("Failed to reinitialize mainlink. rc=%d\n",
1658 training_step = DP_TRAINING_NONE;
1659 rc = dp_ctrl_setup_main_link(ctrl, &cr, &training_step);
1661 /* training completed successfully */
1663 } else if (training_step == DP_TRAINING_1) {
1664 /* link train_1 failed */
1665 rc = dp_ctrl_link_rate_down_shift(ctrl);
1666 if (rc < 0) { /* already in RBR = 1.6G */
1667 if (cr.lane_0_1 & DP_LANE0_1_CR_DONE) {
1669 * some lanes are ready,
1670 * reduce lane number
1672 rc = dp_ctrl_link_lane_down_shift(ctrl);
1673 if (rc < 0) { /* lane == 1 already */
1674 /* end with failure */
1678 /* end with failure */
1679 break; /* lane == 1 already */
1682 } else if (training_step == DP_TRAINING_2) {
1683 /* link train_2 failed, lower lane rate */
1684 rc = dp_ctrl_link_lane_down_shift(ctrl);
1686 /* end with failure */
1687 break; /* lane == 1 already */
1692 if (ctrl->link->sink_request & DP_TEST_LINK_PHY_TEST_PATTERN)
1695 /* stop txing train pattern */
1696 dp_ctrl_clear_training_pattern(ctrl);
1699 * keep transmitting idle pattern until video ready
1700 * to avoid main link from loss of sync
1702 if (rc == 0) /* link train successfully */
1703 dp_ctrl_push_idle(dp_ctrl);
1708 int dp_ctrl_on_stream(struct dp_ctrl *dp_ctrl)
1712 bool mainlink_ready = false;
1713 struct dp_ctrl_private *ctrl;
1718 ctrl = container_of(dp_ctrl, struct dp_ctrl_private, dp_ctrl);
1720 rate = ctrl->panel->link_info.rate;
1722 ctrl->link->link_params.rate = rate;
1723 ctrl->link->link_params.num_lanes = ctrl->panel->link_info.num_lanes;
1724 ctrl->dp_ctrl.pixel_rate = ctrl->panel->dp_mode.drm_mode.clock;
1726 DRM_DEBUG_DP("rate=%d, num_lanes=%d, pixel_rate=%d\n",
1727 ctrl->link->link_params.rate,
1728 ctrl->link->link_params.num_lanes, ctrl->dp_ctrl.pixel_rate);
1730 if (!dp_power_clk_status(ctrl->power, DP_CTRL_PM)) { /* link clk is off */
1731 ret = dp_ctrl_enable_mainlink_clocks(ctrl);
1733 DRM_ERROR("Failed to start link clocks. ret=%d\n", ret);
1738 ret = dp_ctrl_enable_stream_clocks(ctrl);
1740 DRM_ERROR("Failed to start pixel clocks. ret=%d\n", ret);
1744 if (ctrl->link->sink_request & DP_TEST_LINK_PHY_TEST_PATTERN) {
1745 dp_ctrl_send_phy_test_pattern(ctrl);
1750 * Set up transfer unit values and set controller state to send
1753 dp_ctrl_configure_source_params(ctrl);
1755 dp_catalog_ctrl_config_msa(ctrl->catalog,
1756 ctrl->link->link_params.rate,
1757 ctrl->dp_ctrl.pixel_rate, dp_ctrl_use_fixed_nvid(ctrl));
1759 reinit_completion(&ctrl->video_comp);
1761 dp_ctrl_setup_tr_unit(ctrl);
1763 dp_catalog_ctrl_state_ctrl(ctrl->catalog, DP_STATE_CTRL_SEND_VIDEO);
1765 ret = dp_ctrl_wait4video_ready(ctrl);
1769 mainlink_ready = dp_catalog_ctrl_mainlink_ready(ctrl->catalog);
1770 DRM_DEBUG_DP("mainlink %s\n", mainlink_ready ? "READY" : "NOT READY");
1776 int dp_ctrl_off(struct dp_ctrl *dp_ctrl)
1778 struct dp_ctrl_private *ctrl;
1779 struct dp_io *dp_io;
1786 ctrl = container_of(dp_ctrl, struct dp_ctrl_private, dp_ctrl);
1787 dp_io = &ctrl->parser->io;
1790 dp_catalog_ctrl_mainlink_ctrl(ctrl->catalog, false);
1792 dp_catalog_ctrl_reset(ctrl->catalog);
1794 ret = dp_power_clk_enable(ctrl->power, DP_STREAM_PM, false);
1796 DRM_ERROR("Failed to disable pixel clocks. ret=%d\n", ret);
1798 ret = dp_power_clk_enable(ctrl->power, DP_CTRL_PM, false);
1800 DRM_ERROR("Failed to disable link clocks. ret=%d\n", ret);
1806 DRM_DEBUG_DP("DP off done\n");
1810 void dp_ctrl_isr(struct dp_ctrl *dp_ctrl)
1812 struct dp_ctrl_private *ctrl;
1818 ctrl = container_of(dp_ctrl, struct dp_ctrl_private, dp_ctrl);
1820 isr = dp_catalog_ctrl_get_interrupt(ctrl->catalog);
1822 if (isr & DP_CTRL_INTR_READY_FOR_VIDEO) {
1823 DRM_DEBUG_DP("dp_video_ready\n");
1824 complete(&ctrl->video_comp);
1827 if (isr & DP_CTRL_INTR_IDLE_PATTERN_SENT) {
1828 DRM_DEBUG_DP("idle_patterns_sent\n");
1829 complete(&ctrl->idle_comp);
1833 struct dp_ctrl *dp_ctrl_get(struct device *dev, struct dp_link *link,
1834 struct dp_panel *panel, struct drm_dp_aux *aux,
1835 struct dp_power *power, struct dp_catalog *catalog,
1836 struct dp_parser *parser)
1838 struct dp_ctrl_private *ctrl;
1840 if (!dev || !panel || !aux ||
1841 !link || !catalog) {
1842 DRM_ERROR("invalid input\n");
1843 return ERR_PTR(-EINVAL);
1846 ctrl = devm_kzalloc(dev, sizeof(*ctrl), GFP_KERNEL);
1848 DRM_ERROR("Mem allocation failure\n");
1849 return ERR_PTR(-ENOMEM);
1852 init_completion(&ctrl->idle_comp);
1853 init_completion(&ctrl->video_comp);
1856 ctrl->parser = parser;
1857 ctrl->panel = panel;
1858 ctrl->power = power;
1861 ctrl->catalog = catalog;
1864 return &ctrl->dp_ctrl;
1867 void dp_ctrl_put(struct dp_ctrl *dp_ctrl)