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 <linux/pm_opp.h>
14 #include <drm/drm_fixed.h>
15 #include <drm/drm_dp_helper.h>
16 #include <drm/drm_print.h>
22 #define DP_KHZ_TO_HZ 1000
23 #define IDLE_PATTERN_COMPLETION_TIMEOUT_JIFFIES (30 * HZ / 1000) /* 30 ms */
24 #define WAIT_FOR_VIDEO_READY_TIMEOUT_JIFFIES (HZ / 2)
26 #define DP_CTRL_INTR_READY_FOR_VIDEO BIT(0)
27 #define DP_CTRL_INTR_IDLE_PATTERN_SENT BIT(3)
29 #define MR_LINK_TRAINING1 0x8
30 #define MR_LINK_SYMBOL_ERM 0x80
31 #define MR_LINK_PRBS7 0x100
32 #define MR_LINK_CUSTOM80 0x200
33 #define MR_LINK_TRAINING4 0x40
41 struct dp_tu_calc_input {
42 u64 lclk; /* 162, 270, 540 and 810 */
43 u64 pclk_khz; /* in KHz */
44 u64 hactive; /* active h-width */
45 u64 hporch; /* bp + fp + pulse */
46 int nlanes; /* no.of.lanes */
48 int pixel_enc; /* 444, 420, 422 */
49 int dsc_en; /* dsc on/off */
50 int async_en; /* async mode */
52 int compress_ratio; /* 2:1 = 200, 3:1 = 300, 3.75:1 = 375 */
53 int num_of_dsc_slices; /* number of slices per line */
56 struct dp_vc_tu_mapping_table {
59 u8 lrate; /* DP_LINK_RATE -> 162(6), 270(10), 540(20), 810 (30) */
61 u8 valid_boundary_link;
63 bool boundary_moderation_en;
64 u8 valid_lower_boundary_link;
65 u8 upper_boundary_count;
66 u8 lower_boundary_count;
70 struct dp_ctrl_private {
71 struct dp_ctrl dp_ctrl;
73 struct drm_dp_aux *aux;
74 struct dp_panel *panel;
76 struct dp_power *power;
77 struct dp_parser *parser;
78 struct dp_catalog *catalog;
80 struct completion idle_comp;
81 struct completion video_comp;
89 #define DP_LANE0_1_CR_DONE 0x11
91 static int dp_aux_link_configure(struct drm_dp_aux *aux,
92 struct dp_link_info *link)
97 values[0] = drm_dp_link_rate_to_bw_code(link->rate);
98 values[1] = link->num_lanes;
100 if (link->capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
101 values[1] |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
103 err = drm_dp_dpcd_write(aux, DP_LINK_BW_SET, values, sizeof(values));
110 void dp_ctrl_push_idle(struct dp_ctrl *dp_ctrl)
112 struct dp_ctrl_private *ctrl;
114 ctrl = container_of(dp_ctrl, struct dp_ctrl_private, dp_ctrl);
116 reinit_completion(&ctrl->idle_comp);
117 dp_catalog_ctrl_state_ctrl(ctrl->catalog, DP_STATE_CTRL_PUSH_IDLE);
119 if (!wait_for_completion_timeout(&ctrl->idle_comp,
120 IDLE_PATTERN_COMPLETION_TIMEOUT_JIFFIES))
121 pr_warn("PUSH_IDLE pattern timedout\n");
123 pr_debug("mainlink off done\n");
126 static void dp_ctrl_config_ctrl(struct dp_ctrl_private *ctrl)
129 u8 *dpcd = ctrl->panel->dpcd;
131 /* Default-> LSCLK DIV: 1/4 LCLK */
132 config |= (2 << DP_CONFIGURATION_CTRL_LSCLK_DIV_SHIFT);
134 /* Scrambler reset enable */
135 if (dpcd[DP_EDP_CONFIGURATION_CAP] & DP_ALTERNATE_SCRAMBLER_RESET_CAP)
136 config |= DP_CONFIGURATION_CTRL_ASSR;
138 tbd = dp_link_get_test_bits_depth(ctrl->link,
139 ctrl->panel->dp_mode.bpp);
141 if (tbd == DP_TEST_BIT_DEPTH_UNKNOWN) {
142 pr_debug("BIT_DEPTH not set. Configure default\n");
143 tbd = DP_TEST_BIT_DEPTH_8;
146 config |= tbd << DP_CONFIGURATION_CTRL_BPC_SHIFT;
149 config |= ((ctrl->link->link_params.num_lanes - 1)
150 << DP_CONFIGURATION_CTRL_NUM_OF_LANES_SHIFT);
152 if (drm_dp_enhanced_frame_cap(dpcd))
153 config |= DP_CONFIGURATION_CTRL_ENHANCED_FRAMING;
155 config |= DP_CONFIGURATION_CTRL_P_INTERLACED; /* progressive video */
157 /* sync clock & static Mvid */
158 config |= DP_CONFIGURATION_CTRL_STATIC_DYNAMIC_CN;
159 config |= DP_CONFIGURATION_CTRL_SYNC_ASYNC_CLK;
161 dp_catalog_ctrl_config_ctrl(ctrl->catalog, config);
164 static void dp_ctrl_configure_source_params(struct dp_ctrl_private *ctrl)
168 dp_catalog_ctrl_lane_mapping(ctrl->catalog);
169 dp_catalog_ctrl_mainlink_ctrl(ctrl->catalog, true);
171 dp_ctrl_config_ctrl(ctrl);
173 tb = dp_link_get_test_bits_depth(ctrl->link,
174 ctrl->panel->dp_mode.bpp);
175 cc = dp_link_get_colorimetry_config(ctrl->link);
176 dp_catalog_ctrl_config_misc(ctrl->catalog, cc, tb);
177 dp_panel_timing_cfg(ctrl->panel);
181 * The structure and few functions present below are IP/Hardware
182 * specific implementation. Most of the implementation will not
183 * have coding comments
185 struct tu_algo_data {
190 s64 hbp_relative_to_pclk;
191 s64 hbp_relative_to_pclk_fp;
199 uint delay_start_link_extra_pixclk;
200 int extra_buffer_margin;
202 s64 original_ratio_fp;
211 int valid_boundary_link;
212 s64 resulting_valid_fp;
214 s64 effective_valid_fp;
215 s64 effective_valid_recorded_fp;
220 int remainder_tus_upper;
221 int remainder_tus_lower;
224 int delay_start_link;
226 int extra_pclk_cycles;
227 int extra_pclk_cycles_in_link_clk;
229 s64 average_valid2_fp;
230 int new_valid_boundary_link;
231 int remainder_symbols_exist;
233 s64 n_remainder_symbols_per_lane_fp;
234 s64 last_partial_tu_fp;
237 int n_tus_incl_last_incomplete_tu;
238 int extra_pclk_cycles_tmp;
239 int extra_pclk_cycles_in_link_clk_tmp;
240 int extra_required_bytes_new_tmp;
242 int lower_filler_size_tmp;
243 int delay_start_link_tmp;
245 bool boundary_moderation_en;
246 int boundary_mod_lower_err;
247 int upper_boundary_count;
248 int lower_boundary_count;
249 int i_upper_boundary_count;
250 int i_lower_boundary_count;
251 int valid_lower_boundary_link;
252 int even_distribution_BF;
253 int even_distribution_legacy;
254 int even_distribution;
255 int min_hblank_violated;
256 s64 delay_start_time_fp;
264 static int _tu_param_compare(s64 a, s64 b)
268 s64 a_temp, b_temp, minus_1;
273 minus_1 = drm_fixp_from_fraction(-1, 1);
275 a_sign = (a >> 32) & 0x80000000 ? 1 : 0;
277 b_sign = (b >> 32) & 0x80000000 ? 1 : 0;
281 else if (b_sign > a_sign)
284 if (!a_sign && !b_sign) { /* positive */
289 } else { /* negative */
290 a_temp = drm_fixp_mul(a, minus_1);
291 b_temp = drm_fixp_mul(b, minus_1);
300 static void dp_panel_update_tu_timings(struct dp_tu_calc_input *in,
301 struct tu_algo_data *tu)
303 int nlanes = in->nlanes;
304 int dsc_num_slices = in->num_of_dsc_slices;
305 int dsc_num_bytes = 0;
311 int tot_num_eoc_symbols = 0;
312 int tot_num_hor_bytes = 0;
313 int tot_num_dummy_bytes = 0;
314 int dwidth_dsc_bytes = 0;
317 s64 temp1_fp, temp2_fp, temp3_fp;
319 tu->lclk_fp = drm_fixp_from_fraction(in->lclk, 1);
320 tu->pclk_fp = drm_fixp_from_fraction(in->pclk_khz, 1000);
321 tu->lwidth = in->hactive;
322 tu->hbp_relative_to_pclk = in->hporch;
323 tu->nlanes = in->nlanes;
325 tu->pixelEnc = in->pixel_enc;
326 tu->dsc_en = in->dsc_en;
327 tu->async_en = in->async_en;
328 tu->lwidth_fp = drm_fixp_from_fraction(in->hactive, 1);
329 tu->hbp_relative_to_pclk_fp = drm_fixp_from_fraction(in->hporch, 1);
331 if (tu->pixelEnc == 420) {
332 temp1_fp = drm_fixp_from_fraction(2, 1);
333 tu->pclk_fp = drm_fixp_div(tu->pclk_fp, temp1_fp);
334 tu->lwidth_fp = drm_fixp_div(tu->lwidth_fp, temp1_fp);
335 tu->hbp_relative_to_pclk_fp =
336 drm_fixp_div(tu->hbp_relative_to_pclk_fp, 2);
339 if (tu->pixelEnc == 422) {
361 temp1_fp = drm_fixp_from_fraction(in->compress_ratio, 100);
362 temp2_fp = drm_fixp_from_fraction(in->bpp, 1);
363 temp3_fp = drm_fixp_div(temp2_fp, temp1_fp);
364 temp2_fp = drm_fixp_mul(tu->lwidth_fp, temp3_fp);
366 temp1_fp = drm_fixp_from_fraction(8, 1);
367 temp3_fp = drm_fixp_div(temp2_fp, temp1_fp);
369 numerator = drm_fixp2int(temp3_fp);
371 dsc_num_bytes = numerator / dsc_num_slices;
372 eoc_bytes = dsc_num_bytes % nlanes;
373 tot_num_eoc_symbols = nlanes * dsc_num_slices;
374 tot_num_hor_bytes = dsc_num_bytes * dsc_num_slices;
375 tot_num_dummy_bytes = (nlanes - eoc_bytes) * dsc_num_slices;
377 if (dsc_num_bytes == 0)
378 pr_info("incorrect no of bytes per slice=%d\n", dsc_num_bytes);
380 dwidth_dsc_bytes = (tot_num_hor_bytes +
381 tot_num_eoc_symbols +
382 (eoc_bytes == 0 ? 0 : tot_num_dummy_bytes));
384 dwidth_dsc_fp = drm_fixp_from_fraction(dwidth_dsc_bytes, 3);
386 temp2_fp = drm_fixp_mul(tu->pclk_fp, dwidth_dsc_fp);
387 temp1_fp = drm_fixp_div(temp2_fp, tu->lwidth_fp);
388 pclk_dsc_fp = temp1_fp;
390 temp1_fp = drm_fixp_div(pclk_dsc_fp, tu->pclk_fp);
391 temp2_fp = drm_fixp_mul(tu->hbp_relative_to_pclk_fp, temp1_fp);
392 hbp_dsc_fp = temp2_fp;
395 tu->pclk_fp = pclk_dsc_fp;
396 tu->lwidth_fp = dwidth_dsc_fp;
397 tu->hbp_relative_to_pclk_fp = hbp_dsc_fp;
401 temp1_fp = drm_fixp_from_fraction(976, 1000); /* 0.976 */
402 tu->lclk_fp = drm_fixp_mul(tu->lclk_fp, temp1_fp);
406 static void _tu_valid_boundary_calc(struct tu_algo_data *tu)
408 s64 temp1_fp, temp2_fp, temp, temp1, temp2;
409 int compare_result_1, compare_result_2, compare_result_3;
411 temp1_fp = drm_fixp_from_fraction(tu->tu_size, 1);
412 temp2_fp = drm_fixp_mul(tu->ratio_fp, temp1_fp);
414 tu->new_valid_boundary_link = drm_fixp2int_ceil(temp2_fp);
416 temp = (tu->i_upper_boundary_count *
417 tu->new_valid_boundary_link +
418 tu->i_lower_boundary_count *
419 (tu->new_valid_boundary_link-1));
420 tu->average_valid2_fp = drm_fixp_from_fraction(temp,
421 (tu->i_upper_boundary_count +
422 tu->i_lower_boundary_count));
424 temp1_fp = drm_fixp_from_fraction(tu->bpp, 8);
425 temp2_fp = tu->lwidth_fp;
426 temp1_fp = drm_fixp_mul(temp2_fp, temp1_fp);
427 temp2_fp = drm_fixp_div(temp1_fp, tu->average_valid2_fp);
428 tu->n_tus = drm_fixp2int(temp2_fp);
429 if ((temp2_fp & 0xFFFFFFFF) > 0xFFFFF000)
432 temp1_fp = drm_fixp_from_fraction(tu->n_tus, 1);
433 temp2_fp = drm_fixp_mul(temp1_fp, tu->average_valid2_fp);
434 temp1_fp = drm_fixp_from_fraction(tu->n_symbols, 1);
435 temp2_fp = temp1_fp - temp2_fp;
436 temp1_fp = drm_fixp_from_fraction(tu->nlanes, 1);
437 temp2_fp = drm_fixp_div(temp2_fp, temp1_fp);
438 tu->n_remainder_symbols_per_lane_fp = temp2_fp;
440 temp1_fp = drm_fixp_from_fraction(tu->tu_size, 1);
441 tu->last_partial_tu_fp =
442 drm_fixp_div(tu->n_remainder_symbols_per_lane_fp,
445 if (tu->n_remainder_symbols_per_lane_fp != 0)
446 tu->remainder_symbols_exist = 1;
448 tu->remainder_symbols_exist = 0;
450 temp1_fp = drm_fixp_from_fraction(tu->n_tus, tu->nlanes);
451 tu->n_tus_per_lane = drm_fixp2int(temp1_fp);
453 tu->paired_tus = (int)((tu->n_tus_per_lane) /
454 (tu->i_upper_boundary_count +
455 tu->i_lower_boundary_count));
457 tu->remainder_tus = tu->n_tus_per_lane - tu->paired_tus *
458 (tu->i_upper_boundary_count +
459 tu->i_lower_boundary_count);
461 if ((tu->remainder_tus - tu->i_upper_boundary_count) > 0) {
462 tu->remainder_tus_upper = tu->i_upper_boundary_count;
463 tu->remainder_tus_lower = tu->remainder_tus -
464 tu->i_upper_boundary_count;
466 tu->remainder_tus_upper = tu->remainder_tus;
467 tu->remainder_tus_lower = 0;
470 temp = tu->paired_tus * (tu->i_upper_boundary_count *
471 tu->new_valid_boundary_link +
472 tu->i_lower_boundary_count *
473 (tu->new_valid_boundary_link - 1)) +
474 (tu->remainder_tus_upper *
475 tu->new_valid_boundary_link) +
476 (tu->remainder_tus_lower *
477 (tu->new_valid_boundary_link - 1));
478 tu->total_valid_fp = drm_fixp_from_fraction(temp, 1);
480 if (tu->remainder_symbols_exist) {
481 temp1_fp = tu->total_valid_fp +
482 tu->n_remainder_symbols_per_lane_fp;
483 temp2_fp = drm_fixp_from_fraction(tu->n_tus_per_lane, 1);
484 temp2_fp = temp2_fp + tu->last_partial_tu_fp;
485 temp1_fp = drm_fixp_div(temp1_fp, temp2_fp);
487 temp2_fp = drm_fixp_from_fraction(tu->n_tus_per_lane, 1);
488 temp1_fp = drm_fixp_div(tu->total_valid_fp, temp2_fp);
490 tu->effective_valid_fp = temp1_fp;
492 temp1_fp = drm_fixp_from_fraction(tu->tu_size, 1);
493 temp2_fp = drm_fixp_mul(tu->ratio_fp, temp1_fp);
494 tu->n_n_err_fp = tu->effective_valid_fp - temp2_fp;
496 temp1_fp = drm_fixp_from_fraction(tu->tu_size, 1);
497 temp2_fp = drm_fixp_mul(tu->ratio_fp, temp1_fp);
498 tu->n_err_fp = tu->average_valid2_fp - temp2_fp;
500 tu->even_distribution = tu->n_tus % tu->nlanes == 0 ? 1 : 0;
502 temp1_fp = drm_fixp_from_fraction(tu->bpp, 8);
503 temp2_fp = tu->lwidth_fp;
504 temp1_fp = drm_fixp_mul(temp2_fp, temp1_fp);
505 temp2_fp = drm_fixp_div(temp1_fp, tu->average_valid2_fp);
508 tu->n_tus_incl_last_incomplete_tu = drm_fixp2int_ceil(temp2_fp);
510 tu->n_tus_incl_last_incomplete_tu = 0;
513 temp1_fp = drm_fixp_from_fraction(tu->tu_size, 1);
514 temp2_fp = drm_fixp_mul(tu->original_ratio_fp, temp1_fp);
515 temp1_fp = tu->average_valid2_fp - temp2_fp;
516 temp2_fp = drm_fixp_from_fraction(tu->n_tus_incl_last_incomplete_tu, 1);
517 temp1_fp = drm_fixp_mul(temp2_fp, temp1_fp);
520 temp1 = drm_fixp2int_ceil(temp1_fp);
522 temp = tu->i_upper_boundary_count * tu->nlanes;
523 temp1_fp = drm_fixp_from_fraction(tu->tu_size, 1);
524 temp2_fp = drm_fixp_mul(tu->original_ratio_fp, temp1_fp);
525 temp1_fp = drm_fixp_from_fraction(tu->new_valid_boundary_link, 1);
526 temp2_fp = temp1_fp - temp2_fp;
527 temp1_fp = drm_fixp_from_fraction(temp, 1);
528 temp2_fp = drm_fixp_mul(temp1_fp, temp2_fp);
531 temp2 = drm_fixp2int_ceil(temp2_fp);
534 tu->extra_required_bytes_new_tmp = (int)(temp1 + temp2);
536 temp1_fp = drm_fixp_from_fraction(8, tu->bpp);
537 temp2_fp = drm_fixp_from_fraction(
538 tu->extra_required_bytes_new_tmp, 1);
539 temp1_fp = drm_fixp_mul(temp2_fp, temp1_fp);
542 tu->extra_pclk_cycles_tmp = drm_fixp2int_ceil(temp1_fp);
544 tu->extra_pclk_cycles_tmp = 0;
546 temp1_fp = drm_fixp_from_fraction(tu->extra_pclk_cycles_tmp, 1);
547 temp2_fp = drm_fixp_div(tu->lclk_fp, tu->pclk_fp);
548 temp1_fp = drm_fixp_mul(temp1_fp, temp2_fp);
551 tu->extra_pclk_cycles_in_link_clk_tmp =
552 drm_fixp2int_ceil(temp1_fp);
554 tu->extra_pclk_cycles_in_link_clk_tmp = 0;
556 tu->filler_size_tmp = tu->tu_size - tu->new_valid_boundary_link;
558 tu->lower_filler_size_tmp = tu->filler_size_tmp + 1;
560 tu->delay_start_link_tmp = tu->extra_pclk_cycles_in_link_clk_tmp +
561 tu->lower_filler_size_tmp +
562 tu->extra_buffer_margin;
564 temp1_fp = drm_fixp_from_fraction(tu->delay_start_link_tmp, 1);
565 tu->delay_start_time_fp = drm_fixp_div(temp1_fp, tu->lclk_fp);
567 compare_result_1 = _tu_param_compare(tu->n_n_err_fp, tu->diff_abs_fp);
568 if (compare_result_1 == 2)
569 compare_result_1 = 1;
571 compare_result_1 = 0;
573 compare_result_2 = _tu_param_compare(tu->n_n_err_fp, tu->err_fp);
574 if (compare_result_2 == 2)
575 compare_result_2 = 1;
577 compare_result_2 = 0;
579 compare_result_3 = _tu_param_compare(tu->hbp_time_fp,
580 tu->delay_start_time_fp);
581 if (compare_result_3 == 2)
582 compare_result_3 = 0;
584 compare_result_3 = 1;
586 if (((tu->even_distribution == 1) ||
587 ((tu->even_distribution_BF == 0) &&
588 (tu->even_distribution_legacy == 0))) &&
589 tu->n_err_fp >= 0 && tu->n_n_err_fp >= 0 &&
591 (compare_result_1 || (tu->min_hblank_violated == 1)) &&
592 (tu->new_valid_boundary_link - 1) > 0 &&
594 (tu->delay_start_link_tmp <= 1023)) {
595 tu->upper_boundary_count = tu->i_upper_boundary_count;
596 tu->lower_boundary_count = tu->i_lower_boundary_count;
597 tu->err_fp = tu->n_n_err_fp;
598 tu->boundary_moderation_en = true;
599 tu->tu_size_desired = tu->tu_size;
600 tu->valid_boundary_link = tu->new_valid_boundary_link;
601 tu->effective_valid_recorded_fp = tu->effective_valid_fp;
602 tu->even_distribution_BF = 1;
603 tu->delay_start_link = tu->delay_start_link_tmp;
604 } else if (tu->boundary_mod_lower_err == 0) {
605 compare_result_1 = _tu_param_compare(tu->n_n_err_fp,
607 if (compare_result_1 == 2)
608 tu->boundary_mod_lower_err = 1;
612 static void _dp_ctrl_calc_tu(struct dp_tu_calc_input *in,
613 struct dp_vc_tu_mapping_table *tu_table)
615 struct tu_algo_data *tu;
616 int compare_result_1, compare_result_2;
618 s64 temp_fp = 0, temp1_fp = 0, temp2_fp = 0;
620 s64 LCLK_FAST_SKEW_fp = drm_fixp_from_fraction(6, 10000); /* 0.0006 */
621 s64 const_p49_fp = drm_fixp_from_fraction(49, 100); /* 0.49 */
622 s64 const_p56_fp = drm_fixp_from_fraction(56, 100); /* 0.56 */
623 s64 RATIO_SCALE_fp = drm_fixp_from_fraction(1001, 1000);
625 u8 DP_BRUTE_FORCE = 1;
626 s64 BRUTE_FORCE_THRESHOLD_fp = drm_fixp_from_fraction(1, 10); /* 0.1 */
627 uint EXTRA_PIXCLK_CYCLE_DELAY = 4;
628 uint HBLANK_MARGIN = 4;
630 tu = kzalloc(sizeof(*tu), GFP_KERNEL);
634 dp_panel_update_tu_timings(in, tu);
636 tu->err_fp = drm_fixp_from_fraction(1000, 1); /* 1000 */
638 temp1_fp = drm_fixp_from_fraction(4, 1);
639 temp2_fp = drm_fixp_mul(temp1_fp, tu->lclk_fp);
640 temp_fp = drm_fixp_div(temp2_fp, tu->pclk_fp);
641 tu->extra_buffer_margin = drm_fixp2int_ceil(temp_fp);
643 temp1_fp = drm_fixp_from_fraction(tu->bpp, 8);
644 temp2_fp = drm_fixp_mul(tu->pclk_fp, temp1_fp);
645 temp1_fp = drm_fixp_from_fraction(tu->nlanes, 1);
646 temp2_fp = drm_fixp_div(temp2_fp, temp1_fp);
647 tu->ratio_fp = drm_fixp_div(temp2_fp, tu->lclk_fp);
649 tu->original_ratio_fp = tu->ratio_fp;
650 tu->boundary_moderation_en = false;
651 tu->upper_boundary_count = 0;
652 tu->lower_boundary_count = 0;
653 tu->i_upper_boundary_count = 0;
654 tu->i_lower_boundary_count = 0;
655 tu->valid_lower_boundary_link = 0;
656 tu->even_distribution_BF = 0;
657 tu->even_distribution_legacy = 0;
658 tu->even_distribution = 0;
659 tu->delay_start_time_fp = 0;
661 tu->err_fp = drm_fixp_from_fraction(1000, 1);
665 tu->ratio = drm_fixp2int(tu->ratio_fp);
666 temp1_fp = drm_fixp_from_fraction(tu->nlanes, 1);
667 div64_u64_rem(tu->lwidth_fp, temp1_fp, &temp2_fp);
669 !tu->ratio && tu->dsc_en == 0) {
670 tu->ratio_fp = drm_fixp_mul(tu->ratio_fp, RATIO_SCALE_fp);
671 tu->ratio = drm_fixp2int(tu->ratio_fp);
673 tu->ratio_fp = drm_fixp_from_fraction(1, 1);
682 compare_result_1 = _tu_param_compare(tu->ratio_fp, const_p49_fp);
683 if (!compare_result_1 || compare_result_1 == 1)
684 compare_result_1 = 1;
686 compare_result_1 = 0;
688 compare_result_2 = _tu_param_compare(tu->ratio_fp, const_p56_fp);
689 if (!compare_result_2 || compare_result_2 == 2)
690 compare_result_2 = 1;
692 compare_result_2 = 0;
694 if (tu->dsc_en && compare_result_1 && compare_result_2) {
696 DRM_DEBUG_DP("Info: increase HBLANK_MARGIN to %d\n",
701 for (tu->tu_size = 32; tu->tu_size <= 64; tu->tu_size++) {
702 temp1_fp = drm_fixp_from_fraction(tu->tu_size, 1);
703 temp2_fp = drm_fixp_mul(tu->ratio_fp, temp1_fp);
704 temp = drm_fixp2int_ceil(temp2_fp);
705 temp1_fp = drm_fixp_from_fraction(temp, 1);
706 tu->n_err_fp = temp1_fp - temp2_fp;
708 if (tu->n_err_fp < tu->err_fp) {
709 tu->err_fp = tu->n_err_fp;
710 tu->tu_size_desired = tu->tu_size;
714 tu->tu_size_minus1 = tu->tu_size_desired - 1;
716 temp1_fp = drm_fixp_from_fraction(tu->tu_size_desired, 1);
717 temp2_fp = drm_fixp_mul(tu->ratio_fp, temp1_fp);
718 tu->valid_boundary_link = drm_fixp2int_ceil(temp2_fp);
720 temp1_fp = drm_fixp_from_fraction(tu->bpp, 8);
721 temp2_fp = tu->lwidth_fp;
722 temp2_fp = drm_fixp_mul(temp2_fp, temp1_fp);
724 temp1_fp = drm_fixp_from_fraction(tu->valid_boundary_link, 1);
725 temp2_fp = drm_fixp_div(temp2_fp, temp1_fp);
726 tu->n_tus = drm_fixp2int(temp2_fp);
727 if ((temp2_fp & 0xFFFFFFFF) > 0xFFFFF000)
730 tu->even_distribution_legacy = tu->n_tus % tu->nlanes == 0 ? 1 : 0;
731 DRM_DEBUG_DP("Info: n_sym = %d, num_of_tus = %d\n",
732 tu->valid_boundary_link, tu->n_tus);
734 temp1_fp = drm_fixp_from_fraction(tu->tu_size_desired, 1);
735 temp2_fp = drm_fixp_mul(tu->original_ratio_fp, temp1_fp);
736 temp1_fp = drm_fixp_from_fraction(tu->valid_boundary_link, 1);
737 temp2_fp = temp1_fp - temp2_fp;
738 temp1_fp = drm_fixp_from_fraction(tu->n_tus + 1, 1);
739 temp2_fp = drm_fixp_mul(temp1_fp, temp2_fp);
741 temp = drm_fixp2int(temp2_fp);
742 if (temp && temp2_fp)
743 tu->extra_bytes = drm_fixp2int_ceil(temp2_fp);
747 temp1_fp = drm_fixp_from_fraction(tu->extra_bytes, 1);
748 temp2_fp = drm_fixp_from_fraction(8, tu->bpp);
749 temp1_fp = drm_fixp_mul(temp1_fp, temp2_fp);
751 if (temp && temp1_fp)
752 tu->extra_pclk_cycles = drm_fixp2int_ceil(temp1_fp);
754 tu->extra_pclk_cycles = drm_fixp2int(temp1_fp);
756 temp1_fp = drm_fixp_div(tu->lclk_fp, tu->pclk_fp);
757 temp2_fp = drm_fixp_from_fraction(tu->extra_pclk_cycles, 1);
758 temp1_fp = drm_fixp_mul(temp2_fp, temp1_fp);
761 tu->extra_pclk_cycles_in_link_clk = drm_fixp2int_ceil(temp1_fp);
763 tu->extra_pclk_cycles_in_link_clk = drm_fixp2int(temp1_fp);
765 tu->filler_size = tu->tu_size_desired - tu->valid_boundary_link;
767 temp1_fp = drm_fixp_from_fraction(tu->tu_size_desired, 1);
768 tu->ratio_by_tu_fp = drm_fixp_mul(tu->ratio_fp, temp1_fp);
770 tu->delay_start_link = tu->extra_pclk_cycles_in_link_clk +
771 tu->filler_size + tu->extra_buffer_margin;
773 tu->resulting_valid_fp =
774 drm_fixp_from_fraction(tu->valid_boundary_link, 1);
776 temp1_fp = drm_fixp_from_fraction(tu->tu_size_desired, 1);
777 temp2_fp = drm_fixp_div(tu->resulting_valid_fp, temp1_fp);
778 tu->TU_ratio_err_fp = temp2_fp - tu->original_ratio_fp;
780 temp1_fp = drm_fixp_from_fraction(HBLANK_MARGIN, 1);
781 temp1_fp = tu->hbp_relative_to_pclk_fp - temp1_fp;
782 tu->hbp_time_fp = drm_fixp_div(temp1_fp, tu->pclk_fp);
784 temp1_fp = drm_fixp_from_fraction(tu->delay_start_link, 1);
785 tu->delay_start_time_fp = drm_fixp_div(temp1_fp, tu->lclk_fp);
787 compare_result_1 = _tu_param_compare(tu->hbp_time_fp,
788 tu->delay_start_time_fp);
789 if (compare_result_1 == 2) /* if (hbp_time_fp < delay_start_time_fp) */
790 tu->min_hblank_violated = 1;
792 tu->hactive_time_fp = drm_fixp_div(tu->lwidth_fp, tu->pclk_fp);
794 compare_result_2 = _tu_param_compare(tu->hactive_time_fp,
795 tu->delay_start_time_fp);
796 if (compare_result_2 == 2)
797 tu->min_hblank_violated = 1;
799 tu->delay_start_time_fp = 0;
803 tu->delay_start_link_extra_pixclk = EXTRA_PIXCLK_CYCLE_DELAY;
804 tu->diff_abs_fp = tu->resulting_valid_fp - tu->ratio_by_tu_fp;
806 temp = drm_fixp2int(tu->diff_abs_fp);
807 if (!temp && tu->diff_abs_fp <= 0xffff)
810 /* if(diff_abs < 0) diff_abs *= -1 */
811 if (tu->diff_abs_fp < 0)
812 tu->diff_abs_fp = drm_fixp_mul(tu->diff_abs_fp, -1);
814 tu->boundary_mod_lower_err = 0;
815 if ((tu->diff_abs_fp != 0 &&
816 ((tu->diff_abs_fp > BRUTE_FORCE_THRESHOLD_fp) ||
817 (tu->even_distribution_legacy == 0) ||
818 (DP_BRUTE_FORCE == 1))) ||
819 (tu->min_hblank_violated == 1)) {
821 tu->err_fp = drm_fixp_from_fraction(1000, 1);
823 temp1_fp = drm_fixp_div(tu->lclk_fp, tu->pclk_fp);
824 temp2_fp = drm_fixp_from_fraction(
825 tu->delay_start_link_extra_pixclk, 1);
826 temp1_fp = drm_fixp_mul(temp2_fp, temp1_fp);
829 tu->extra_buffer_margin =
830 drm_fixp2int_ceil(temp1_fp);
832 tu->extra_buffer_margin = 0;
834 temp1_fp = drm_fixp_from_fraction(tu->bpp, 8);
835 temp1_fp = drm_fixp_mul(tu->lwidth_fp, temp1_fp);
838 tu->n_symbols = drm_fixp2int_ceil(temp1_fp);
842 for (tu->tu_size = 32; tu->tu_size <= 64; tu->tu_size++) {
843 for (tu->i_upper_boundary_count = 1;
844 tu->i_upper_boundary_count <= 15;
845 tu->i_upper_boundary_count++) {
846 for (tu->i_lower_boundary_count = 1;
847 tu->i_lower_boundary_count <= 15;
848 tu->i_lower_boundary_count++) {
849 _tu_valid_boundary_calc(tu);
853 tu->delay_start_link_extra_pixclk--;
854 } while (tu->boundary_moderation_en != true &&
855 tu->boundary_mod_lower_err == 1 &&
856 tu->delay_start_link_extra_pixclk != 0);
858 if (tu->boundary_moderation_en == true) {
859 temp1_fp = drm_fixp_from_fraction(
860 (tu->upper_boundary_count *
861 tu->valid_boundary_link +
862 tu->lower_boundary_count *
863 (tu->valid_boundary_link - 1)), 1);
864 temp2_fp = drm_fixp_from_fraction(
865 (tu->upper_boundary_count +
866 tu->lower_boundary_count), 1);
867 tu->resulting_valid_fp =
868 drm_fixp_div(temp1_fp, temp2_fp);
870 temp1_fp = drm_fixp_from_fraction(
871 tu->tu_size_desired, 1);
873 drm_fixp_mul(tu->original_ratio_fp, temp1_fp);
875 tu->valid_lower_boundary_link =
876 tu->valid_boundary_link - 1;
878 temp1_fp = drm_fixp_from_fraction(tu->bpp, 8);
879 temp1_fp = drm_fixp_mul(tu->lwidth_fp, temp1_fp);
880 temp2_fp = drm_fixp_div(temp1_fp,
881 tu->resulting_valid_fp);
882 tu->n_tus = drm_fixp2int(temp2_fp);
884 tu->tu_size_minus1 = tu->tu_size_desired - 1;
885 tu->even_distribution_BF = 1;
888 drm_fixp_from_fraction(tu->tu_size_desired, 1);
890 drm_fixp_div(tu->resulting_valid_fp, temp1_fp);
891 tu->TU_ratio_err_fp = temp2_fp - tu->original_ratio_fp;
895 temp2_fp = drm_fixp_mul(LCLK_FAST_SKEW_fp, tu->lwidth_fp);
898 temp = drm_fixp2int_ceil(temp2_fp);
902 temp1_fp = drm_fixp_from_fraction(tu->nlanes, 1);
903 temp2_fp = drm_fixp_mul(tu->original_ratio_fp, temp1_fp);
904 temp1_fp = drm_fixp_from_fraction(tu->bpp, 8);
905 temp2_fp = drm_fixp_div(temp1_fp, temp2_fp);
906 temp1_fp = drm_fixp_from_fraction(temp, 1);
907 temp2_fp = drm_fixp_mul(temp1_fp, temp2_fp);
908 temp = drm_fixp2int(temp2_fp);
911 tu->delay_start_link += (int)temp;
913 temp1_fp = drm_fixp_from_fraction(tu->delay_start_link, 1);
914 tu->delay_start_time_fp = drm_fixp_div(temp1_fp, tu->lclk_fp);
917 tu_table->valid_boundary_link = tu->valid_boundary_link;
918 tu_table->delay_start_link = tu->delay_start_link;
919 tu_table->boundary_moderation_en = tu->boundary_moderation_en;
920 tu_table->valid_lower_boundary_link = tu->valid_lower_boundary_link;
921 tu_table->upper_boundary_count = tu->upper_boundary_count;
922 tu_table->lower_boundary_count = tu->lower_boundary_count;
923 tu_table->tu_size_minus1 = tu->tu_size_minus1;
925 DRM_DEBUG_DP("TU: valid_boundary_link: %d\n",
926 tu_table->valid_boundary_link);
927 DRM_DEBUG_DP("TU: delay_start_link: %d\n",
928 tu_table->delay_start_link);
929 DRM_DEBUG_DP("TU: boundary_moderation_en: %d\n",
930 tu_table->boundary_moderation_en);
931 DRM_DEBUG_DP("TU: valid_lower_boundary_link: %d\n",
932 tu_table->valid_lower_boundary_link);
933 DRM_DEBUG_DP("TU: upper_boundary_count: %d\n",
934 tu_table->upper_boundary_count);
935 DRM_DEBUG_DP("TU: lower_boundary_count: %d\n",
936 tu_table->lower_boundary_count);
937 DRM_DEBUG_DP("TU: tu_size_minus1: %d\n", tu_table->tu_size_minus1);
942 static void dp_ctrl_calc_tu_parameters(struct dp_ctrl_private *ctrl,
943 struct dp_vc_tu_mapping_table *tu_table)
945 struct dp_tu_calc_input in;
946 struct drm_display_mode *drm_mode;
948 drm_mode = &ctrl->panel->dp_mode.drm_mode;
950 in.lclk = ctrl->link->link_params.rate / 1000;
951 in.pclk_khz = drm_mode->clock;
952 in.hactive = drm_mode->hdisplay;
953 in.hporch = drm_mode->htotal - drm_mode->hdisplay;
954 in.nlanes = ctrl->link->link_params.num_lanes;
955 in.bpp = ctrl->panel->dp_mode.bpp;
960 in.num_of_dsc_slices = 0;
961 in.compress_ratio = 100;
963 _dp_ctrl_calc_tu(&in, tu_table);
966 static void dp_ctrl_setup_tr_unit(struct dp_ctrl_private *ctrl)
969 u32 valid_boundary = 0x0;
970 u32 valid_boundary2 = 0x0;
971 struct dp_vc_tu_mapping_table tu_calc_table;
973 dp_ctrl_calc_tu_parameters(ctrl, &tu_calc_table);
975 dp_tu |= tu_calc_table.tu_size_minus1;
976 valid_boundary |= tu_calc_table.valid_boundary_link;
977 valid_boundary |= (tu_calc_table.delay_start_link << 16);
979 valid_boundary2 |= (tu_calc_table.valid_lower_boundary_link << 1);
980 valid_boundary2 |= (tu_calc_table.upper_boundary_count << 16);
981 valid_boundary2 |= (tu_calc_table.lower_boundary_count << 20);
983 if (tu_calc_table.boundary_moderation_en)
984 valid_boundary2 |= BIT(0);
986 pr_debug("dp_tu=0x%x, valid_boundary=0x%x, valid_boundary2=0x%x\n",
987 dp_tu, valid_boundary, valid_boundary2);
989 dp_catalog_ctrl_update_transfer_unit(ctrl->catalog,
990 dp_tu, valid_boundary, valid_boundary2);
993 static int dp_ctrl_wait4video_ready(struct dp_ctrl_private *ctrl)
997 if (!wait_for_completion_timeout(&ctrl->video_comp,
998 WAIT_FOR_VIDEO_READY_TIMEOUT_JIFFIES)) {
999 DRM_ERROR("wait4video timedout\n");
1005 static int dp_ctrl_update_vx_px(struct dp_ctrl_private *ctrl)
1007 struct dp_link *link = ctrl->link;
1008 int ret = 0, lane, lane_cnt;
1010 u32 max_level_reached = 0;
1011 u32 voltage_swing_level = link->phy_params.v_level;
1012 u32 pre_emphasis_level = link->phy_params.p_level;
1014 ret = dp_catalog_ctrl_update_vx_px(ctrl->catalog,
1015 voltage_swing_level, pre_emphasis_level);
1020 if (voltage_swing_level >= DP_TRAIN_VOLTAGE_SWING_MAX) {
1021 DRM_DEBUG_DP("max. voltage swing level reached %d\n",
1022 voltage_swing_level);
1023 max_level_reached |= DP_TRAIN_MAX_SWING_REACHED;
1026 if (pre_emphasis_level >= DP_TRAIN_PRE_EMPHASIS_MAX) {
1027 DRM_DEBUG_DP("max. pre-emphasis level reached %d\n",
1028 pre_emphasis_level);
1029 max_level_reached |= DP_TRAIN_MAX_PRE_EMPHASIS_REACHED;
1032 pre_emphasis_level <<= DP_TRAIN_PRE_EMPHASIS_SHIFT;
1034 lane_cnt = ctrl->link->link_params.num_lanes;
1035 for (lane = 0; lane < lane_cnt; lane++)
1036 buf[lane] = voltage_swing_level | pre_emphasis_level
1037 | max_level_reached;
1039 DRM_DEBUG_DP("sink: p|v=0x%x\n", voltage_swing_level
1040 | pre_emphasis_level);
1041 ret = drm_dp_dpcd_write(ctrl->aux, DP_TRAINING_LANE0_SET,
1043 if (ret == lane_cnt)
1049 static bool dp_ctrl_train_pattern_set(struct dp_ctrl_private *ctrl,
1055 DRM_DEBUG_DP("sink: pattern=%x\n", pattern);
1059 if (pattern && pattern != DP_TRAINING_PATTERN_4)
1060 buf |= DP_LINK_SCRAMBLING_DISABLE;
1062 ret = drm_dp_dpcd_writeb(ctrl->aux, DP_TRAINING_PATTERN_SET, buf);
1066 static int dp_ctrl_read_link_status(struct dp_ctrl_private *ctrl,
1071 len = drm_dp_dpcd_read_link_status(ctrl->aux, link_status);
1072 if (len != DP_LINK_STATUS_SIZE) {
1073 DRM_ERROR("DP link status read failed, err: %d\n", len);
1080 static int dp_ctrl_link_train_1(struct dp_ctrl_private *ctrl,
1081 struct dp_cr_status *cr, int *training_step)
1083 int tries, old_v_level, ret = 0;
1084 u8 link_status[DP_LINK_STATUS_SIZE];
1085 int const maximum_retries = 4;
1087 dp_catalog_ctrl_state_ctrl(ctrl->catalog, 0);
1089 *training_step = DP_TRAINING_1;
1091 ret = dp_catalog_ctrl_set_pattern(ctrl->catalog, DP_TRAINING_PATTERN_1);
1094 dp_ctrl_train_pattern_set(ctrl, DP_TRAINING_PATTERN_1 |
1095 DP_LINK_SCRAMBLING_DISABLE);
1097 ret = dp_ctrl_update_vx_px(ctrl);
1102 old_v_level = ctrl->link->phy_params.v_level;
1103 for (tries = 0; tries < maximum_retries; tries++) {
1104 drm_dp_link_train_clock_recovery_delay(ctrl->aux, ctrl->panel->dpcd);
1106 ret = dp_ctrl_read_link_status(ctrl, link_status);
1110 cr->lane_0_1 = link_status[0];
1111 cr->lane_2_3 = link_status[1];
1113 if (drm_dp_clock_recovery_ok(link_status,
1114 ctrl->link->link_params.num_lanes)) {
1118 if (ctrl->link->phy_params.v_level >=
1119 DP_TRAIN_VOLTAGE_SWING_MAX) {
1120 DRM_ERROR_RATELIMITED("max v_level reached\n");
1124 if (old_v_level != ctrl->link->phy_params.v_level) {
1126 old_v_level = ctrl->link->phy_params.v_level;
1129 DRM_DEBUG_DP("clock recovery not done, adjusting vx px\n");
1131 dp_link_adjust_levels(ctrl->link, link_status);
1132 ret = dp_ctrl_update_vx_px(ctrl);
1137 DRM_ERROR("max tries reached\n");
1141 static int dp_ctrl_link_rate_down_shift(struct dp_ctrl_private *ctrl)
1145 switch (ctrl->link->link_params.rate) {
1147 ctrl->link->link_params.rate = 540000;
1150 ctrl->link->link_params.rate = 270000;
1153 ctrl->link->link_params.rate = 162000;
1162 DRM_DEBUG_DP("new rate=0x%x\n", ctrl->link->link_params.rate);
1167 static int dp_ctrl_link_lane_down_shift(struct dp_ctrl_private *ctrl)
1170 if (ctrl->link->link_params.num_lanes == 1)
1173 ctrl->link->link_params.num_lanes /= 2;
1174 ctrl->link->link_params.rate = ctrl->panel->link_info.rate;
1176 ctrl->link->phy_params.p_level = 0;
1177 ctrl->link->phy_params.v_level = 0;
1182 static void dp_ctrl_clear_training_pattern(struct dp_ctrl_private *ctrl)
1184 dp_ctrl_train_pattern_set(ctrl, DP_TRAINING_PATTERN_DISABLE);
1185 drm_dp_link_train_channel_eq_delay(ctrl->aux, ctrl->panel->dpcd);
1188 static int dp_ctrl_link_train_2(struct dp_ctrl_private *ctrl,
1189 struct dp_cr_status *cr, int *training_step)
1191 int tries = 0, ret = 0;
1193 int const maximum_retries = 5;
1194 u8 link_status[DP_LINK_STATUS_SIZE];
1196 dp_catalog_ctrl_state_ctrl(ctrl->catalog, 0);
1198 *training_step = DP_TRAINING_2;
1200 if (drm_dp_tps3_supported(ctrl->panel->dpcd))
1201 pattern = DP_TRAINING_PATTERN_3;
1203 pattern = DP_TRAINING_PATTERN_2;
1205 ret = dp_ctrl_update_vx_px(ctrl);
1209 ret = dp_catalog_ctrl_set_pattern(ctrl->catalog, pattern);
1213 dp_ctrl_train_pattern_set(ctrl, pattern | DP_RECOVERED_CLOCK_OUT_EN);
1215 for (tries = 0; tries <= maximum_retries; tries++) {
1216 drm_dp_link_train_channel_eq_delay(ctrl->aux, ctrl->panel->dpcd);
1218 ret = dp_ctrl_read_link_status(ctrl, link_status);
1221 cr->lane_0_1 = link_status[0];
1222 cr->lane_2_3 = link_status[1];
1224 if (drm_dp_channel_eq_ok(link_status,
1225 ctrl->link->link_params.num_lanes)) {
1229 dp_link_adjust_levels(ctrl->link, link_status);
1230 ret = dp_ctrl_update_vx_px(ctrl);
1239 static int dp_ctrl_reinitialize_mainlink(struct dp_ctrl_private *ctrl);
1241 static int dp_ctrl_link_train(struct dp_ctrl_private *ctrl,
1242 struct dp_cr_status *cr, int *training_step)
1245 u8 encoding = DP_SET_ANSI_8B10B;
1246 struct dp_link_info link_info = {0};
1248 dp_ctrl_config_ctrl(ctrl);
1250 link_info.num_lanes = ctrl->link->link_params.num_lanes;
1251 link_info.rate = ctrl->link->link_params.rate;
1252 link_info.capabilities = DP_LINK_CAP_ENHANCED_FRAMING;
1254 dp_aux_link_configure(ctrl->aux, &link_info);
1255 drm_dp_dpcd_write(ctrl->aux, DP_MAIN_LINK_CHANNEL_CODING_SET,
1258 ret = dp_ctrl_link_train_1(ctrl, cr, training_step);
1260 DRM_ERROR("link training #1 failed. ret=%d\n", ret);
1264 /* print success info as this is a result of user initiated action */
1265 DRM_DEBUG_DP("link training #1 successful\n");
1267 ret = dp_ctrl_link_train_2(ctrl, cr, training_step);
1269 DRM_ERROR("link training #2 failed. ret=%d\n", ret);
1273 /* print success info as this is a result of user initiated action */
1274 DRM_DEBUG_DP("link training #2 successful\n");
1277 dp_catalog_ctrl_state_ctrl(ctrl->catalog, 0);
1282 static int dp_ctrl_setup_main_link(struct dp_ctrl_private *ctrl,
1283 struct dp_cr_status *cr, int *training_step)
1287 dp_catalog_ctrl_mainlink_ctrl(ctrl->catalog, true);
1289 if (ctrl->link->sink_request & DP_TEST_LINK_PHY_TEST_PATTERN)
1293 * As part of previous calls, DP controller state might have
1294 * transitioned to PUSH_IDLE. In order to start transmitting
1295 * a link training pattern, we have to first do soft reset.
1298 ret = dp_ctrl_link_train(ctrl, cr, training_step);
1303 static void dp_ctrl_set_clock_rate(struct dp_ctrl_private *ctrl,
1304 enum dp_pm_type module, char *name, unsigned long rate)
1306 u32 num = ctrl->parser->mp[module].num_clk;
1307 struct dss_clk *cfg = ctrl->parser->mp[module].clk_config;
1309 while (num && strcmp(cfg->clk_name, name)) {
1314 DRM_DEBUG_DP("setting rate=%lu on clk=%s\n", rate, name);
1319 DRM_ERROR("%s clock doesn't exit to set rate %lu\n",
1323 static int dp_ctrl_enable_mainlink_clocks(struct dp_ctrl_private *ctrl)
1326 struct dp_io *dp_io = &ctrl->parser->io;
1327 struct phy *phy = dp_io->phy;
1328 struct phy_configure_opts_dp *opts_dp = &dp_io->phy_opts.dp;
1330 opts_dp->lanes = ctrl->link->link_params.num_lanes;
1331 opts_dp->link_rate = ctrl->link->link_params.rate / 100;
1332 dp_ctrl_set_clock_rate(ctrl, DP_CTRL_PM, "ctrl_link",
1333 ctrl->link->link_params.rate * 1000);
1335 phy_configure(phy, &dp_io->phy_opts);
1338 ret = dp_power_clk_enable(ctrl->power, DP_CTRL_PM, true);
1340 DRM_ERROR("Unable to start link clocks. ret=%d\n", ret);
1342 DRM_DEBUG_DP("link rate=%d pixel_clk=%d\n",
1343 ctrl->link->link_params.rate, ctrl->dp_ctrl.pixel_rate);
1348 static int dp_ctrl_enable_stream_clocks(struct dp_ctrl_private *ctrl)
1352 dp_ctrl_set_clock_rate(ctrl, DP_STREAM_PM, "stream_pixel",
1353 ctrl->dp_ctrl.pixel_rate * 1000);
1355 ret = dp_power_clk_enable(ctrl->power, DP_STREAM_PM, true);
1357 DRM_ERROR("Unabled to start pixel clocks. ret=%d\n", ret);
1359 DRM_DEBUG_DP("link rate=%d pixel_clk=%d\n",
1360 ctrl->link->link_params.rate, ctrl->dp_ctrl.pixel_rate);
1365 int dp_ctrl_host_init(struct dp_ctrl *dp_ctrl, bool flip, bool reset)
1367 struct dp_ctrl_private *ctrl;
1368 struct dp_io *dp_io;
1372 DRM_ERROR("Invalid input data\n");
1376 ctrl = container_of(dp_ctrl, struct dp_ctrl_private, dp_ctrl);
1377 dp_io = &ctrl->parser->io;
1380 ctrl->dp_ctrl.orientation = flip;
1383 dp_catalog_ctrl_reset(ctrl->catalog);
1385 dp_catalog_ctrl_phy_reset(ctrl->catalog);
1387 dp_catalog_ctrl_enable_irq(ctrl->catalog, true);
1393 * dp_ctrl_host_deinit() - Uninitialize DP controller
1394 * @dp_ctrl: Display Port Driver data
1396 * Perform required steps to uninitialize DP controller
1397 * and its resources.
1399 void dp_ctrl_host_deinit(struct dp_ctrl *dp_ctrl)
1401 struct dp_ctrl_private *ctrl;
1402 struct dp_io *dp_io;
1406 DRM_ERROR("Invalid input data\n");
1410 ctrl = container_of(dp_ctrl, struct dp_ctrl_private, dp_ctrl);
1411 dp_io = &ctrl->parser->io;
1414 dp_catalog_ctrl_enable_irq(ctrl->catalog, false);
1417 DRM_DEBUG_DP("Host deinitialized successfully\n");
1420 static bool dp_ctrl_use_fixed_nvid(struct dp_ctrl_private *ctrl)
1422 u8 *dpcd = ctrl->panel->dpcd;
1425 * For better interop experience, used a fixed NVID=0x8000
1426 * whenever connected to a VGA dongle downstream.
1428 if (drm_dp_is_branch(dpcd))
1429 return (drm_dp_has_quirk(&ctrl->panel->desc,
1430 DP_DPCD_QUIRK_CONSTANT_N));
1435 static int dp_ctrl_reinitialize_mainlink(struct dp_ctrl_private *ctrl)
1438 struct dp_io *dp_io = &ctrl->parser->io;
1439 struct phy *phy = dp_io->phy;
1440 struct phy_configure_opts_dp *opts_dp = &dp_io->phy_opts.dp;
1442 dp_catalog_ctrl_mainlink_ctrl(ctrl->catalog, false);
1443 opts_dp->lanes = ctrl->link->link_params.num_lanes;
1444 phy_configure(phy, &dp_io->phy_opts);
1446 * Disable and re-enable the mainlink clock since the
1447 * link clock might have been adjusted as part of the
1450 ret = dp_power_clk_enable(ctrl->power, DP_CTRL_PM, false);
1452 DRM_ERROR("Failed to disable clocks. ret=%d\n", ret);
1456 /* hw recommended delay before re-enabling clocks */
1459 ret = dp_ctrl_enable_mainlink_clocks(ctrl);
1461 DRM_ERROR("Failed to enable mainlink clks. ret=%d\n", ret);
1468 static int dp_ctrl_deinitialize_mainlink(struct dp_ctrl_private *ctrl)
1470 struct dp_io *dp_io;
1474 dp_io = &ctrl->parser->io;
1477 dp_catalog_ctrl_mainlink_ctrl(ctrl->catalog, false);
1479 dp_catalog_ctrl_reset(ctrl->catalog);
1481 ret = dp_power_clk_enable(ctrl->power, DP_CTRL_PM, false);
1483 DRM_ERROR("Failed to disable link clocks. ret=%d\n", ret);
1492 static int dp_ctrl_link_maintenance(struct dp_ctrl_private *ctrl)
1495 struct dp_cr_status cr;
1496 int training_step = DP_TRAINING_NONE;
1498 dp_ctrl_push_idle(&ctrl->dp_ctrl);
1500 ctrl->dp_ctrl.pixel_rate = ctrl->panel->dp_mode.drm_mode.clock;
1502 ret = dp_ctrl_setup_main_link(ctrl, &cr, &training_step);
1506 dp_ctrl_clear_training_pattern(ctrl);
1508 dp_catalog_ctrl_state_ctrl(ctrl->catalog, DP_STATE_CTRL_SEND_VIDEO);
1510 ret = dp_ctrl_wait4video_ready(ctrl);
1515 static int dp_ctrl_process_phy_test_request(struct dp_ctrl_private *ctrl)
1519 if (!ctrl->link->phy_params.phy_test_pattern_sel) {
1520 DRM_DEBUG_DP("no test pattern selected by sink\n");
1525 * The global reset will need DP link related clocks to be
1526 * running. Add the global reset just before disabling the
1527 * link clocks and core clocks.
1529 ret = dp_ctrl_off(&ctrl->dp_ctrl);
1531 DRM_ERROR("failed to disable DP controller\n");
1535 ret = dp_ctrl_on_link(&ctrl->dp_ctrl);
1537 ret = dp_ctrl_on_stream(&ctrl->dp_ctrl);
1539 DRM_ERROR("failed to enable DP link controller\n");
1544 static bool dp_ctrl_send_phy_test_pattern(struct dp_ctrl_private *ctrl)
1546 bool success = false;
1547 u32 pattern_sent = 0x0;
1548 u32 pattern_requested = ctrl->link->phy_params.phy_test_pattern_sel;
1550 DRM_DEBUG_DP("request: 0x%x\n", pattern_requested);
1552 if (dp_catalog_ctrl_update_vx_px(ctrl->catalog,
1553 ctrl->link->phy_params.v_level,
1554 ctrl->link->phy_params.p_level)) {
1555 DRM_ERROR("Failed to set v/p levels\n");
1558 dp_catalog_ctrl_send_phy_pattern(ctrl->catalog, pattern_requested);
1559 dp_ctrl_update_vx_px(ctrl);
1560 dp_link_send_test_response(ctrl->link);
1562 pattern_sent = dp_catalog_ctrl_read_phy_pattern(ctrl->catalog);
1564 switch (pattern_sent) {
1565 case MR_LINK_TRAINING1:
1566 success = (pattern_requested ==
1567 DP_PHY_TEST_PATTERN_D10_2);
1569 case MR_LINK_SYMBOL_ERM:
1570 success = ((pattern_requested ==
1571 DP_PHY_TEST_PATTERN_ERROR_COUNT) ||
1572 (pattern_requested ==
1573 DP_PHY_TEST_PATTERN_CP2520));
1576 success = (pattern_requested ==
1577 DP_PHY_TEST_PATTERN_PRBS7);
1579 case MR_LINK_CUSTOM80:
1580 success = (pattern_requested ==
1581 DP_PHY_TEST_PATTERN_80BIT_CUSTOM);
1583 case MR_LINK_TRAINING4:
1584 success = (pattern_requested ==
1585 DP_PHY_TEST_PATTERN_SEL_MASK);
1591 DRM_DEBUG_DP("%s: test->0x%x\n", success ? "success" : "failed",
1596 void dp_ctrl_handle_sink_request(struct dp_ctrl *dp_ctrl)
1598 struct dp_ctrl_private *ctrl;
1599 u32 sink_request = 0x0;
1602 DRM_ERROR("invalid input\n");
1606 ctrl = container_of(dp_ctrl, struct dp_ctrl_private, dp_ctrl);
1607 sink_request = ctrl->link->sink_request;
1609 if (sink_request & DP_TEST_LINK_PHY_TEST_PATTERN) {
1610 DRM_DEBUG_DP("PHY_TEST_PATTERN request\n");
1611 if (dp_ctrl_process_phy_test_request(ctrl)) {
1612 DRM_ERROR("process phy_test_req failed\n");
1617 if (sink_request & DP_LINK_STATUS_UPDATED) {
1618 if (dp_ctrl_link_maintenance(ctrl)) {
1619 DRM_ERROR("LM failed: TEST_LINK_TRAINING\n");
1624 if (sink_request & DP_TEST_LINK_TRAINING) {
1625 dp_link_send_test_response(ctrl->link);
1626 if (dp_ctrl_link_maintenance(ctrl)) {
1627 DRM_ERROR("LM failed: TEST_LINK_TRAINING\n");
1633 int dp_ctrl_on_link(struct dp_ctrl *dp_ctrl)
1636 struct dp_ctrl_private *ctrl;
1638 int link_train_max_retries = 5;
1639 u32 const phy_cts_pixel_clk_khz = 148500;
1640 struct dp_cr_status cr;
1641 unsigned int training_step;
1646 ctrl = container_of(dp_ctrl, struct dp_ctrl_private, dp_ctrl);
1648 rate = ctrl->panel->link_info.rate;
1650 dp_power_clk_enable(ctrl->power, DP_CORE_PM, true);
1652 if (ctrl->link->sink_request & DP_TEST_LINK_PHY_TEST_PATTERN) {
1653 DRM_DEBUG_DP("using phy test link parameters\n");
1654 if (!ctrl->panel->dp_mode.drm_mode.clock)
1655 ctrl->dp_ctrl.pixel_rate = phy_cts_pixel_clk_khz;
1657 ctrl->link->link_params.rate = rate;
1658 ctrl->link->link_params.num_lanes =
1659 ctrl->panel->link_info.num_lanes;
1660 ctrl->dp_ctrl.pixel_rate = ctrl->panel->dp_mode.drm_mode.clock;
1663 DRM_DEBUG_DP("rate=%d, num_lanes=%d, pixel_rate=%d\n",
1664 ctrl->link->link_params.rate,
1665 ctrl->link->link_params.num_lanes, ctrl->dp_ctrl.pixel_rate);
1667 rc = dp_ctrl_enable_mainlink_clocks(ctrl);
1671 while (--link_train_max_retries) {
1672 rc = dp_ctrl_reinitialize_mainlink(ctrl);
1674 DRM_ERROR("Failed to reinitialize mainlink. rc=%d\n",
1679 training_step = DP_TRAINING_NONE;
1680 rc = dp_ctrl_setup_main_link(ctrl, &cr, &training_step);
1682 /* training completed successfully */
1684 } else if (training_step == DP_TRAINING_1) {
1685 /* link train_1 failed */
1686 if (!dp_catalog_link_is_connected(ctrl->catalog)) {
1690 rc = dp_ctrl_link_rate_down_shift(ctrl);
1691 if (rc < 0) { /* already in RBR = 1.6G */
1692 if (cr.lane_0_1 & DP_LANE0_1_CR_DONE) {
1694 * some lanes are ready,
1695 * reduce lane number
1697 rc = dp_ctrl_link_lane_down_shift(ctrl);
1698 if (rc < 0) { /* lane == 1 already */
1699 /* end with failure */
1703 /* end with failure */
1704 break; /* lane == 1 already */
1707 } else if (training_step == DP_TRAINING_2) {
1708 /* link train_2 failed, lower lane rate */
1709 if (!dp_catalog_link_is_connected(ctrl->catalog)) {
1713 rc = dp_ctrl_link_lane_down_shift(ctrl);
1715 /* end with failure */
1716 break; /* lane == 1 already */
1721 if (ctrl->link->sink_request & DP_TEST_LINK_PHY_TEST_PATTERN)
1724 /* stop txing train pattern */
1725 dp_ctrl_clear_training_pattern(ctrl);
1728 * keep transmitting idle pattern until video ready
1729 * to avoid main link from loss of sync
1731 if (rc == 0) /* link train successfully */
1732 dp_ctrl_push_idle(dp_ctrl);
1734 /* link training failed */
1735 dp_ctrl_deinitialize_mainlink(ctrl);
1742 int dp_ctrl_on_stream(struct dp_ctrl *dp_ctrl)
1746 bool mainlink_ready = false;
1747 struct dp_ctrl_private *ctrl;
1752 ctrl = container_of(dp_ctrl, struct dp_ctrl_private, dp_ctrl);
1754 rate = ctrl->panel->link_info.rate;
1756 ctrl->link->link_params.rate = rate;
1757 ctrl->link->link_params.num_lanes = ctrl->panel->link_info.num_lanes;
1758 ctrl->dp_ctrl.pixel_rate = ctrl->panel->dp_mode.drm_mode.clock;
1760 DRM_DEBUG_DP("rate=%d, num_lanes=%d, pixel_rate=%d\n",
1761 ctrl->link->link_params.rate,
1762 ctrl->link->link_params.num_lanes, ctrl->dp_ctrl.pixel_rate);
1764 if (!dp_power_clk_status(ctrl->power, DP_CTRL_PM)) { /* link clk is off */
1765 ret = dp_ctrl_enable_mainlink_clocks(ctrl);
1767 DRM_ERROR("Failed to start link clocks. ret=%d\n", ret);
1772 ret = dp_ctrl_enable_stream_clocks(ctrl);
1774 DRM_ERROR("Failed to start pixel clocks. ret=%d\n", ret);
1778 if (ctrl->link->sink_request & DP_TEST_LINK_PHY_TEST_PATTERN) {
1779 dp_ctrl_send_phy_test_pattern(ctrl);
1784 * Set up transfer unit values and set controller state to send
1787 reinit_completion(&ctrl->video_comp);
1789 dp_ctrl_configure_source_params(ctrl);
1791 dp_catalog_ctrl_config_msa(ctrl->catalog,
1792 ctrl->link->link_params.rate,
1793 ctrl->dp_ctrl.pixel_rate, dp_ctrl_use_fixed_nvid(ctrl));
1795 dp_ctrl_setup_tr_unit(ctrl);
1797 dp_catalog_ctrl_state_ctrl(ctrl->catalog, DP_STATE_CTRL_SEND_VIDEO);
1799 ret = dp_ctrl_wait4video_ready(ctrl);
1803 mainlink_ready = dp_catalog_ctrl_mainlink_ready(ctrl->catalog);
1804 DRM_DEBUG_DP("mainlink %s\n", mainlink_ready ? "READY" : "NOT READY");
1810 int dp_ctrl_off_link_stream(struct dp_ctrl *dp_ctrl)
1812 struct dp_ctrl_private *ctrl;
1813 struct dp_io *dp_io;
1817 ctrl = container_of(dp_ctrl, struct dp_ctrl_private, dp_ctrl);
1818 dp_io = &ctrl->parser->io;
1821 /* set dongle to D3 (power off) mode */
1822 dp_link_psm_config(ctrl->link, &ctrl->panel->link_info, true);
1824 dp_catalog_ctrl_mainlink_ctrl(ctrl->catalog, false);
1826 if (dp_power_clk_status(ctrl->power, DP_STREAM_PM)) {
1827 ret = dp_power_clk_enable(ctrl->power, DP_STREAM_PM, false);
1829 DRM_ERROR("Failed to disable pclk. ret=%d\n", ret);
1834 ret = dp_power_clk_enable(ctrl->power, DP_CTRL_PM, false);
1836 DRM_ERROR("Failed to disable link clocks. ret=%d\n", ret);
1842 /* aux channel down, reinit phy */
1846 DRM_DEBUG_DP("DP off link/stream done\n");
1850 void dp_ctrl_off_phy(struct dp_ctrl *dp_ctrl)
1852 struct dp_ctrl_private *ctrl;
1853 struct dp_io *dp_io;
1856 ctrl = container_of(dp_ctrl, struct dp_ctrl_private, dp_ctrl);
1857 dp_io = &ctrl->parser->io;
1860 dp_catalog_ctrl_reset(ctrl->catalog);
1864 DRM_DEBUG_DP("DP off phy done\n");
1867 int dp_ctrl_off(struct dp_ctrl *dp_ctrl)
1869 struct dp_ctrl_private *ctrl;
1870 struct dp_io *dp_io;
1877 ctrl = container_of(dp_ctrl, struct dp_ctrl_private, dp_ctrl);
1878 dp_io = &ctrl->parser->io;
1881 dp_catalog_ctrl_mainlink_ctrl(ctrl->catalog, false);
1883 dp_catalog_ctrl_reset(ctrl->catalog);
1885 ret = dp_power_clk_enable(ctrl->power, DP_STREAM_PM, false);
1887 DRM_ERROR("Failed to disable pixel clocks. ret=%d\n", ret);
1889 ret = dp_power_clk_enable(ctrl->power, DP_CTRL_PM, false);
1891 DRM_ERROR("Failed to disable link clocks. ret=%d\n", ret);
1897 DRM_DEBUG_DP("DP off done\n");
1901 void dp_ctrl_isr(struct dp_ctrl *dp_ctrl)
1903 struct dp_ctrl_private *ctrl;
1909 ctrl = container_of(dp_ctrl, struct dp_ctrl_private, dp_ctrl);
1911 isr = dp_catalog_ctrl_get_interrupt(ctrl->catalog);
1913 if (isr & DP_CTRL_INTR_READY_FOR_VIDEO) {
1914 DRM_DEBUG_DP("dp_video_ready\n");
1915 complete(&ctrl->video_comp);
1918 if (isr & DP_CTRL_INTR_IDLE_PATTERN_SENT) {
1919 DRM_DEBUG_DP("idle_patterns_sent\n");
1920 complete(&ctrl->idle_comp);
1924 struct dp_ctrl *dp_ctrl_get(struct device *dev, struct dp_link *link,
1925 struct dp_panel *panel, struct drm_dp_aux *aux,
1926 struct dp_power *power, struct dp_catalog *catalog,
1927 struct dp_parser *parser)
1929 struct dp_ctrl_private *ctrl;
1932 if (!dev || !panel || !aux ||
1933 !link || !catalog) {
1934 DRM_ERROR("invalid input\n");
1935 return ERR_PTR(-EINVAL);
1938 ctrl = devm_kzalloc(dev, sizeof(*ctrl), GFP_KERNEL);
1940 DRM_ERROR("Mem allocation failure\n");
1941 return ERR_PTR(-ENOMEM);
1944 ret = devm_pm_opp_set_clkname(dev, "ctrl_link");
1946 dev_err(dev, "invalid DP OPP table in device tree\n");
1947 /* caller do PTR_ERR(opp_table) */
1948 return (struct dp_ctrl *)ERR_PTR(ret);
1951 /* OPP table is optional */
1952 ret = devm_pm_opp_of_add_table(dev);
1954 dev_err(dev, "failed to add DP OPP table\n");
1956 init_completion(&ctrl->idle_comp);
1957 init_completion(&ctrl->video_comp);
1960 ctrl->parser = parser;
1961 ctrl->panel = panel;
1962 ctrl->power = power;
1965 ctrl->catalog = catalog;
1968 return &ctrl->dp_ctrl;