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>
15 #include <drm/display/drm_dp_helper.h>
16 #include <drm/drm_fixed.h>
17 #include <drm/drm_print.h>
23 #define DP_KHZ_TO_HZ 1000
24 #define IDLE_PATTERN_COMPLETION_TIMEOUT_JIFFIES (30 * HZ / 1000) /* 30 ms */
25 #define PSR_OPERATION_COMPLETION_TIMEOUT_JIFFIES (300 * HZ / 1000) /* 300 ms */
26 #define WAIT_FOR_VIDEO_READY_TIMEOUT_JIFFIES (HZ / 2)
28 #define DP_CTRL_INTR_READY_FOR_VIDEO BIT(0)
29 #define DP_CTRL_INTR_IDLE_PATTERN_SENT BIT(3)
31 #define MR_LINK_TRAINING1 0x8
32 #define MR_LINK_SYMBOL_ERM 0x80
33 #define MR_LINK_PRBS7 0x100
34 #define MR_LINK_CUSTOM80 0x200
35 #define MR_LINK_TRAINING4 0x40
43 struct dp_tu_calc_input {
44 u64 lclk; /* 162, 270, 540 and 810 */
45 u64 pclk_khz; /* in KHz */
46 u64 hactive; /* active h-width */
47 u64 hporch; /* bp + fp + pulse */
48 int nlanes; /* no.of.lanes */
50 int pixel_enc; /* 444, 420, 422 */
51 int dsc_en; /* dsc on/off */
52 int async_en; /* async mode */
54 int compress_ratio; /* 2:1 = 200, 3:1 = 300, 3.75:1 = 375 */
55 int num_of_dsc_slices; /* number of slices per line */
58 struct dp_vc_tu_mapping_table {
61 u8 lrate; /* DP_LINK_RATE -> 162(6), 270(10), 540(20), 810 (30) */
63 u8 valid_boundary_link;
65 bool boundary_moderation_en;
66 u8 valid_lower_boundary_link;
67 u8 upper_boundary_count;
68 u8 lower_boundary_count;
72 struct dp_ctrl_private {
73 struct dp_ctrl dp_ctrl;
74 struct drm_device *drm_dev;
76 struct drm_dp_aux *aux;
77 struct dp_panel *panel;
79 struct dp_power *power;
80 struct dp_parser *parser;
81 struct dp_catalog *catalog;
83 struct completion idle_comp;
84 struct completion psr_op_comp;
85 struct completion video_comp;
88 static int dp_aux_link_configure(struct drm_dp_aux *aux,
89 struct dp_link_info *link)
94 values[0] = drm_dp_link_rate_to_bw_code(link->rate);
95 values[1] = link->num_lanes;
97 if (link->capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
98 values[1] |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
100 err = drm_dp_dpcd_write(aux, DP_LINK_BW_SET, values, sizeof(values));
107 void dp_ctrl_push_idle(struct dp_ctrl *dp_ctrl)
109 struct dp_ctrl_private *ctrl;
111 ctrl = container_of(dp_ctrl, struct dp_ctrl_private, dp_ctrl);
113 reinit_completion(&ctrl->idle_comp);
114 dp_catalog_ctrl_state_ctrl(ctrl->catalog, DP_STATE_CTRL_PUSH_IDLE);
116 if (!wait_for_completion_timeout(&ctrl->idle_comp,
117 IDLE_PATTERN_COMPLETION_TIMEOUT_JIFFIES))
118 pr_warn("PUSH_IDLE pattern timedout\n");
120 drm_dbg_dp(ctrl->drm_dev, "mainlink off\n");
123 static void dp_ctrl_config_ctrl(struct dp_ctrl_private *ctrl)
126 const u8 *dpcd = ctrl->panel->dpcd;
128 /* Default-> LSCLK DIV: 1/4 LCLK */
129 config |= (2 << DP_CONFIGURATION_CTRL_LSCLK_DIV_SHIFT);
131 /* Scrambler reset enable */
132 if (drm_dp_alternate_scrambler_reset_cap(dpcd))
133 config |= DP_CONFIGURATION_CTRL_ASSR;
135 tbd = dp_link_get_test_bits_depth(ctrl->link,
136 ctrl->panel->dp_mode.bpp);
138 if (tbd == DP_TEST_BIT_DEPTH_UNKNOWN) {
139 pr_debug("BIT_DEPTH not set. Configure default\n");
140 tbd = DP_TEST_BIT_DEPTH_8;
143 config |= tbd << DP_CONFIGURATION_CTRL_BPC_SHIFT;
146 config |= ((ctrl->link->link_params.num_lanes - 1)
147 << DP_CONFIGURATION_CTRL_NUM_OF_LANES_SHIFT);
149 if (drm_dp_enhanced_frame_cap(dpcd))
150 config |= DP_CONFIGURATION_CTRL_ENHANCED_FRAMING;
152 config |= DP_CONFIGURATION_CTRL_P_INTERLACED; /* progressive video */
154 /* sync clock & static Mvid */
155 config |= DP_CONFIGURATION_CTRL_STATIC_DYNAMIC_CN;
156 config |= DP_CONFIGURATION_CTRL_SYNC_ASYNC_CLK;
158 if (ctrl->panel->psr_cap.version)
159 config |= DP_CONFIGURATION_CTRL_SEND_VSC;
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_ctrl_private *ctrl,
613 struct dp_tu_calc_input *in,
614 struct dp_vc_tu_mapping_table *tu_table)
616 struct tu_algo_data *tu;
617 int compare_result_1, compare_result_2;
619 s64 temp_fp = 0, temp1_fp = 0, temp2_fp = 0;
621 s64 LCLK_FAST_SKEW_fp = drm_fixp_from_fraction(6, 10000); /* 0.0006 */
622 s64 const_p49_fp = drm_fixp_from_fraction(49, 100); /* 0.49 */
623 s64 const_p56_fp = drm_fixp_from_fraction(56, 100); /* 0.56 */
624 s64 RATIO_SCALE_fp = drm_fixp_from_fraction(1001, 1000);
626 u8 DP_BRUTE_FORCE = 1;
627 s64 BRUTE_FORCE_THRESHOLD_fp = drm_fixp_from_fraction(1, 10); /* 0.1 */
628 uint EXTRA_PIXCLK_CYCLE_DELAY = 4;
629 uint HBLANK_MARGIN = 4;
631 tu = kzalloc(sizeof(*tu), GFP_KERNEL);
635 dp_panel_update_tu_timings(in, tu);
637 tu->err_fp = drm_fixp_from_fraction(1000, 1); /* 1000 */
639 temp1_fp = drm_fixp_from_fraction(4, 1);
640 temp2_fp = drm_fixp_mul(temp1_fp, tu->lclk_fp);
641 temp_fp = drm_fixp_div(temp2_fp, tu->pclk_fp);
642 tu->extra_buffer_margin = drm_fixp2int_ceil(temp_fp);
644 temp1_fp = drm_fixp_from_fraction(tu->bpp, 8);
645 temp2_fp = drm_fixp_mul(tu->pclk_fp, temp1_fp);
646 temp1_fp = drm_fixp_from_fraction(tu->nlanes, 1);
647 temp2_fp = drm_fixp_div(temp2_fp, temp1_fp);
648 tu->ratio_fp = drm_fixp_div(temp2_fp, tu->lclk_fp);
650 tu->original_ratio_fp = tu->ratio_fp;
651 tu->boundary_moderation_en = false;
652 tu->upper_boundary_count = 0;
653 tu->lower_boundary_count = 0;
654 tu->i_upper_boundary_count = 0;
655 tu->i_lower_boundary_count = 0;
656 tu->valid_lower_boundary_link = 0;
657 tu->even_distribution_BF = 0;
658 tu->even_distribution_legacy = 0;
659 tu->even_distribution = 0;
660 tu->delay_start_time_fp = 0;
662 tu->err_fp = drm_fixp_from_fraction(1000, 1);
666 tu->ratio = drm_fixp2int(tu->ratio_fp);
667 temp1_fp = drm_fixp_from_fraction(tu->nlanes, 1);
668 div64_u64_rem(tu->lwidth_fp, temp1_fp, &temp2_fp);
670 !tu->ratio && tu->dsc_en == 0) {
671 tu->ratio_fp = drm_fixp_mul(tu->ratio_fp, RATIO_SCALE_fp);
672 tu->ratio = drm_fixp2int(tu->ratio_fp);
674 tu->ratio_fp = drm_fixp_from_fraction(1, 1);
683 compare_result_1 = _tu_param_compare(tu->ratio_fp, const_p49_fp);
684 if (!compare_result_1 || compare_result_1 == 1)
685 compare_result_1 = 1;
687 compare_result_1 = 0;
689 compare_result_2 = _tu_param_compare(tu->ratio_fp, const_p56_fp);
690 if (!compare_result_2 || compare_result_2 == 2)
691 compare_result_2 = 1;
693 compare_result_2 = 0;
695 if (tu->dsc_en && compare_result_1 && compare_result_2) {
697 drm_dbg_dp(ctrl->drm_dev,
698 "increase HBLANK_MARGIN to %d\n", HBLANK_MARGIN);
702 for (tu->tu_size = 32; tu->tu_size <= 64; tu->tu_size++) {
703 temp1_fp = drm_fixp_from_fraction(tu->tu_size, 1);
704 temp2_fp = drm_fixp_mul(tu->ratio_fp, temp1_fp);
705 temp = drm_fixp2int_ceil(temp2_fp);
706 temp1_fp = drm_fixp_from_fraction(temp, 1);
707 tu->n_err_fp = temp1_fp - temp2_fp;
709 if (tu->n_err_fp < tu->err_fp) {
710 tu->err_fp = tu->n_err_fp;
711 tu->tu_size_desired = tu->tu_size;
715 tu->tu_size_minus1 = tu->tu_size_desired - 1;
717 temp1_fp = drm_fixp_from_fraction(tu->tu_size_desired, 1);
718 temp2_fp = drm_fixp_mul(tu->ratio_fp, temp1_fp);
719 tu->valid_boundary_link = drm_fixp2int_ceil(temp2_fp);
721 temp1_fp = drm_fixp_from_fraction(tu->bpp, 8);
722 temp2_fp = tu->lwidth_fp;
723 temp2_fp = drm_fixp_mul(temp2_fp, temp1_fp);
725 temp1_fp = drm_fixp_from_fraction(tu->valid_boundary_link, 1);
726 temp2_fp = drm_fixp_div(temp2_fp, temp1_fp);
727 tu->n_tus = drm_fixp2int(temp2_fp);
728 if ((temp2_fp & 0xFFFFFFFF) > 0xFFFFF000)
731 tu->even_distribution_legacy = tu->n_tus % tu->nlanes == 0 ? 1 : 0;
733 drm_dbg_dp(ctrl->drm_dev,
734 "n_sym = %d, num_of_tus = %d\n",
735 tu->valid_boundary_link, tu->n_tus);
737 temp1_fp = drm_fixp_from_fraction(tu->tu_size_desired, 1);
738 temp2_fp = drm_fixp_mul(tu->original_ratio_fp, temp1_fp);
739 temp1_fp = drm_fixp_from_fraction(tu->valid_boundary_link, 1);
740 temp2_fp = temp1_fp - temp2_fp;
741 temp1_fp = drm_fixp_from_fraction(tu->n_tus + 1, 1);
742 temp2_fp = drm_fixp_mul(temp1_fp, temp2_fp);
744 temp = drm_fixp2int(temp2_fp);
745 if (temp && temp2_fp)
746 tu->extra_bytes = drm_fixp2int_ceil(temp2_fp);
750 temp1_fp = drm_fixp_from_fraction(tu->extra_bytes, 1);
751 temp2_fp = drm_fixp_from_fraction(8, tu->bpp);
752 temp1_fp = drm_fixp_mul(temp1_fp, temp2_fp);
754 if (temp && temp1_fp)
755 tu->extra_pclk_cycles = drm_fixp2int_ceil(temp1_fp);
757 tu->extra_pclk_cycles = drm_fixp2int(temp1_fp);
759 temp1_fp = drm_fixp_div(tu->lclk_fp, tu->pclk_fp);
760 temp2_fp = drm_fixp_from_fraction(tu->extra_pclk_cycles, 1);
761 temp1_fp = drm_fixp_mul(temp2_fp, temp1_fp);
764 tu->extra_pclk_cycles_in_link_clk = drm_fixp2int_ceil(temp1_fp);
766 tu->extra_pclk_cycles_in_link_clk = drm_fixp2int(temp1_fp);
768 tu->filler_size = tu->tu_size_desired - tu->valid_boundary_link;
770 temp1_fp = drm_fixp_from_fraction(tu->tu_size_desired, 1);
771 tu->ratio_by_tu_fp = drm_fixp_mul(tu->ratio_fp, temp1_fp);
773 tu->delay_start_link = tu->extra_pclk_cycles_in_link_clk +
774 tu->filler_size + tu->extra_buffer_margin;
776 tu->resulting_valid_fp =
777 drm_fixp_from_fraction(tu->valid_boundary_link, 1);
779 temp1_fp = drm_fixp_from_fraction(tu->tu_size_desired, 1);
780 temp2_fp = drm_fixp_div(tu->resulting_valid_fp, temp1_fp);
781 tu->TU_ratio_err_fp = temp2_fp - tu->original_ratio_fp;
783 temp1_fp = drm_fixp_from_fraction(HBLANK_MARGIN, 1);
784 temp1_fp = tu->hbp_relative_to_pclk_fp - temp1_fp;
785 tu->hbp_time_fp = drm_fixp_div(temp1_fp, tu->pclk_fp);
787 temp1_fp = drm_fixp_from_fraction(tu->delay_start_link, 1);
788 tu->delay_start_time_fp = drm_fixp_div(temp1_fp, tu->lclk_fp);
790 compare_result_1 = _tu_param_compare(tu->hbp_time_fp,
791 tu->delay_start_time_fp);
792 if (compare_result_1 == 2) /* if (hbp_time_fp < delay_start_time_fp) */
793 tu->min_hblank_violated = 1;
795 tu->hactive_time_fp = drm_fixp_div(tu->lwidth_fp, tu->pclk_fp);
797 compare_result_2 = _tu_param_compare(tu->hactive_time_fp,
798 tu->delay_start_time_fp);
799 if (compare_result_2 == 2)
800 tu->min_hblank_violated = 1;
802 tu->delay_start_time_fp = 0;
806 tu->delay_start_link_extra_pixclk = EXTRA_PIXCLK_CYCLE_DELAY;
807 tu->diff_abs_fp = tu->resulting_valid_fp - tu->ratio_by_tu_fp;
809 temp = drm_fixp2int(tu->diff_abs_fp);
810 if (!temp && tu->diff_abs_fp <= 0xffff)
813 /* if(diff_abs < 0) diff_abs *= -1 */
814 if (tu->diff_abs_fp < 0)
815 tu->diff_abs_fp = drm_fixp_mul(tu->diff_abs_fp, -1);
817 tu->boundary_mod_lower_err = 0;
818 if ((tu->diff_abs_fp != 0 &&
819 ((tu->diff_abs_fp > BRUTE_FORCE_THRESHOLD_fp) ||
820 (tu->even_distribution_legacy == 0) ||
821 (DP_BRUTE_FORCE == 1))) ||
822 (tu->min_hblank_violated == 1)) {
824 tu->err_fp = drm_fixp_from_fraction(1000, 1);
826 temp1_fp = drm_fixp_div(tu->lclk_fp, tu->pclk_fp);
827 temp2_fp = drm_fixp_from_fraction(
828 tu->delay_start_link_extra_pixclk, 1);
829 temp1_fp = drm_fixp_mul(temp2_fp, temp1_fp);
832 tu->extra_buffer_margin =
833 drm_fixp2int_ceil(temp1_fp);
835 tu->extra_buffer_margin = 0;
837 temp1_fp = drm_fixp_from_fraction(tu->bpp, 8);
838 temp1_fp = drm_fixp_mul(tu->lwidth_fp, temp1_fp);
841 tu->n_symbols = drm_fixp2int_ceil(temp1_fp);
845 for (tu->tu_size = 32; tu->tu_size <= 64; tu->tu_size++) {
846 for (tu->i_upper_boundary_count = 1;
847 tu->i_upper_boundary_count <= 15;
848 tu->i_upper_boundary_count++) {
849 for (tu->i_lower_boundary_count = 1;
850 tu->i_lower_boundary_count <= 15;
851 tu->i_lower_boundary_count++) {
852 _tu_valid_boundary_calc(tu);
856 tu->delay_start_link_extra_pixclk--;
857 } while (tu->boundary_moderation_en != true &&
858 tu->boundary_mod_lower_err == 1 &&
859 tu->delay_start_link_extra_pixclk != 0);
861 if (tu->boundary_moderation_en == true) {
862 temp1_fp = drm_fixp_from_fraction(
863 (tu->upper_boundary_count *
864 tu->valid_boundary_link +
865 tu->lower_boundary_count *
866 (tu->valid_boundary_link - 1)), 1);
867 temp2_fp = drm_fixp_from_fraction(
868 (tu->upper_boundary_count +
869 tu->lower_boundary_count), 1);
870 tu->resulting_valid_fp =
871 drm_fixp_div(temp1_fp, temp2_fp);
873 temp1_fp = drm_fixp_from_fraction(
874 tu->tu_size_desired, 1);
876 drm_fixp_mul(tu->original_ratio_fp, temp1_fp);
878 tu->valid_lower_boundary_link =
879 tu->valid_boundary_link - 1;
881 temp1_fp = drm_fixp_from_fraction(tu->bpp, 8);
882 temp1_fp = drm_fixp_mul(tu->lwidth_fp, temp1_fp);
883 temp2_fp = drm_fixp_div(temp1_fp,
884 tu->resulting_valid_fp);
885 tu->n_tus = drm_fixp2int(temp2_fp);
887 tu->tu_size_minus1 = tu->tu_size_desired - 1;
888 tu->even_distribution_BF = 1;
891 drm_fixp_from_fraction(tu->tu_size_desired, 1);
893 drm_fixp_div(tu->resulting_valid_fp, temp1_fp);
894 tu->TU_ratio_err_fp = temp2_fp - tu->original_ratio_fp;
898 temp2_fp = drm_fixp_mul(LCLK_FAST_SKEW_fp, tu->lwidth_fp);
901 temp = drm_fixp2int_ceil(temp2_fp);
905 temp1_fp = drm_fixp_from_fraction(tu->nlanes, 1);
906 temp2_fp = drm_fixp_mul(tu->original_ratio_fp, temp1_fp);
907 temp1_fp = drm_fixp_from_fraction(tu->bpp, 8);
908 temp2_fp = drm_fixp_div(temp1_fp, temp2_fp);
909 temp1_fp = drm_fixp_from_fraction(temp, 1);
910 temp2_fp = drm_fixp_mul(temp1_fp, temp2_fp);
911 temp = drm_fixp2int(temp2_fp);
914 tu->delay_start_link += (int)temp;
916 temp1_fp = drm_fixp_from_fraction(tu->delay_start_link, 1);
917 tu->delay_start_time_fp = drm_fixp_div(temp1_fp, tu->lclk_fp);
920 tu_table->valid_boundary_link = tu->valid_boundary_link;
921 tu_table->delay_start_link = tu->delay_start_link;
922 tu_table->boundary_moderation_en = tu->boundary_moderation_en;
923 tu_table->valid_lower_boundary_link = tu->valid_lower_boundary_link;
924 tu_table->upper_boundary_count = tu->upper_boundary_count;
925 tu_table->lower_boundary_count = tu->lower_boundary_count;
926 tu_table->tu_size_minus1 = tu->tu_size_minus1;
928 drm_dbg_dp(ctrl->drm_dev, "TU: valid_boundary_link: %d\n",
929 tu_table->valid_boundary_link);
930 drm_dbg_dp(ctrl->drm_dev, "TU: delay_start_link: %d\n",
931 tu_table->delay_start_link);
932 drm_dbg_dp(ctrl->drm_dev, "TU: boundary_moderation_en: %d\n",
933 tu_table->boundary_moderation_en);
934 drm_dbg_dp(ctrl->drm_dev, "TU: valid_lower_boundary_link: %d\n",
935 tu_table->valid_lower_boundary_link);
936 drm_dbg_dp(ctrl->drm_dev, "TU: upper_boundary_count: %d\n",
937 tu_table->upper_boundary_count);
938 drm_dbg_dp(ctrl->drm_dev, "TU: lower_boundary_count: %d\n",
939 tu_table->lower_boundary_count);
940 drm_dbg_dp(ctrl->drm_dev, "TU: tu_size_minus1: %d\n",
941 tu_table->tu_size_minus1);
946 static void dp_ctrl_calc_tu_parameters(struct dp_ctrl_private *ctrl,
947 struct dp_vc_tu_mapping_table *tu_table)
949 struct dp_tu_calc_input in;
950 struct drm_display_mode *drm_mode;
952 drm_mode = &ctrl->panel->dp_mode.drm_mode;
954 in.lclk = ctrl->link->link_params.rate / 1000;
955 in.pclk_khz = drm_mode->clock;
956 in.hactive = drm_mode->hdisplay;
957 in.hporch = drm_mode->htotal - drm_mode->hdisplay;
958 in.nlanes = ctrl->link->link_params.num_lanes;
959 in.bpp = ctrl->panel->dp_mode.bpp;
964 in.num_of_dsc_slices = 0;
965 in.compress_ratio = 100;
967 _dp_ctrl_calc_tu(ctrl, &in, tu_table);
970 static void dp_ctrl_setup_tr_unit(struct dp_ctrl_private *ctrl)
973 u32 valid_boundary = 0x0;
974 u32 valid_boundary2 = 0x0;
975 struct dp_vc_tu_mapping_table tu_calc_table;
977 dp_ctrl_calc_tu_parameters(ctrl, &tu_calc_table);
979 dp_tu |= tu_calc_table.tu_size_minus1;
980 valid_boundary |= tu_calc_table.valid_boundary_link;
981 valid_boundary |= (tu_calc_table.delay_start_link << 16);
983 valid_boundary2 |= (tu_calc_table.valid_lower_boundary_link << 1);
984 valid_boundary2 |= (tu_calc_table.upper_boundary_count << 16);
985 valid_boundary2 |= (tu_calc_table.lower_boundary_count << 20);
987 if (tu_calc_table.boundary_moderation_en)
988 valid_boundary2 |= BIT(0);
990 pr_debug("dp_tu=0x%x, valid_boundary=0x%x, valid_boundary2=0x%x\n",
991 dp_tu, valid_boundary, valid_boundary2);
993 dp_catalog_ctrl_update_transfer_unit(ctrl->catalog,
994 dp_tu, valid_boundary, valid_boundary2);
997 static int dp_ctrl_wait4video_ready(struct dp_ctrl_private *ctrl)
1001 if (!wait_for_completion_timeout(&ctrl->video_comp,
1002 WAIT_FOR_VIDEO_READY_TIMEOUT_JIFFIES)) {
1003 DRM_ERROR("wait4video timedout\n");
1009 static int dp_ctrl_update_vx_px(struct dp_ctrl_private *ctrl)
1011 struct dp_link *link = ctrl->link;
1012 int ret = 0, lane, lane_cnt;
1014 u32 max_level_reached = 0;
1015 u32 voltage_swing_level = link->phy_params.v_level;
1016 u32 pre_emphasis_level = link->phy_params.p_level;
1018 drm_dbg_dp(ctrl->drm_dev,
1019 "voltage level: %d emphasis level: %d\n",
1020 voltage_swing_level, pre_emphasis_level);
1021 ret = dp_catalog_ctrl_update_vx_px(ctrl->catalog,
1022 voltage_swing_level, pre_emphasis_level);
1027 if (voltage_swing_level >= DP_TRAIN_VOLTAGE_SWING_MAX) {
1028 drm_dbg_dp(ctrl->drm_dev,
1029 "max. voltage swing level reached %d\n",
1030 voltage_swing_level);
1031 max_level_reached |= DP_TRAIN_MAX_SWING_REACHED;
1034 if (pre_emphasis_level >= DP_TRAIN_PRE_EMPHASIS_MAX) {
1035 drm_dbg_dp(ctrl->drm_dev,
1036 "max. pre-emphasis level reached %d\n",
1037 pre_emphasis_level);
1038 max_level_reached |= DP_TRAIN_MAX_PRE_EMPHASIS_REACHED;
1041 pre_emphasis_level <<= DP_TRAIN_PRE_EMPHASIS_SHIFT;
1043 lane_cnt = ctrl->link->link_params.num_lanes;
1044 for (lane = 0; lane < lane_cnt; lane++)
1045 buf[lane] = voltage_swing_level | pre_emphasis_level
1046 | max_level_reached;
1048 drm_dbg_dp(ctrl->drm_dev, "sink: p|v=0x%x\n",
1049 voltage_swing_level | pre_emphasis_level);
1050 ret = drm_dp_dpcd_write(ctrl->aux, DP_TRAINING_LANE0_SET,
1052 if (ret == lane_cnt)
1058 static bool dp_ctrl_train_pattern_set(struct dp_ctrl_private *ctrl,
1064 drm_dbg_dp(ctrl->drm_dev, "sink: pattern=%x\n", pattern);
1068 if (pattern && pattern != DP_TRAINING_PATTERN_4)
1069 buf |= DP_LINK_SCRAMBLING_DISABLE;
1071 ret = drm_dp_dpcd_writeb(ctrl->aux, DP_TRAINING_PATTERN_SET, buf);
1075 static int dp_ctrl_read_link_status(struct dp_ctrl_private *ctrl,
1080 len = drm_dp_dpcd_read_link_status(ctrl->aux, link_status);
1081 if (len != DP_LINK_STATUS_SIZE) {
1082 DRM_ERROR("DP link status read failed, err: %d\n", len);
1089 static int dp_ctrl_link_train_1(struct dp_ctrl_private *ctrl,
1092 int tries, old_v_level, ret = 0;
1093 u8 link_status[DP_LINK_STATUS_SIZE];
1094 int const maximum_retries = 4;
1096 dp_catalog_ctrl_state_ctrl(ctrl->catalog, 0);
1098 *training_step = DP_TRAINING_1;
1100 ret = dp_catalog_ctrl_set_pattern_state_bit(ctrl->catalog, 1);
1103 dp_ctrl_train_pattern_set(ctrl, DP_TRAINING_PATTERN_1 |
1104 DP_LINK_SCRAMBLING_DISABLE);
1106 ret = dp_ctrl_update_vx_px(ctrl);
1111 old_v_level = ctrl->link->phy_params.v_level;
1112 for (tries = 0; tries < maximum_retries; tries++) {
1113 drm_dp_link_train_clock_recovery_delay(ctrl->aux, ctrl->panel->dpcd);
1115 ret = dp_ctrl_read_link_status(ctrl, link_status);
1119 if (drm_dp_clock_recovery_ok(link_status,
1120 ctrl->link->link_params.num_lanes)) {
1124 if (ctrl->link->phy_params.v_level >=
1125 DP_TRAIN_VOLTAGE_SWING_MAX) {
1126 DRM_ERROR_RATELIMITED("max v_level reached\n");
1130 if (old_v_level != ctrl->link->phy_params.v_level) {
1132 old_v_level = ctrl->link->phy_params.v_level;
1135 dp_link_adjust_levels(ctrl->link, link_status);
1136 ret = dp_ctrl_update_vx_px(ctrl);
1141 DRM_ERROR("max tries reached\n");
1145 static int dp_ctrl_link_rate_down_shift(struct dp_ctrl_private *ctrl)
1149 switch (ctrl->link->link_params.rate) {
1151 ctrl->link->link_params.rate = 540000;
1154 ctrl->link->link_params.rate = 270000;
1157 ctrl->link->link_params.rate = 162000;
1166 drm_dbg_dp(ctrl->drm_dev, "new rate=0x%x\n",
1167 ctrl->link->link_params.rate);
1173 static int dp_ctrl_link_lane_down_shift(struct dp_ctrl_private *ctrl)
1176 if (ctrl->link->link_params.num_lanes == 1)
1179 ctrl->link->link_params.num_lanes /= 2;
1180 ctrl->link->link_params.rate = ctrl->panel->link_info.rate;
1182 ctrl->link->phy_params.p_level = 0;
1183 ctrl->link->phy_params.v_level = 0;
1188 static void dp_ctrl_clear_training_pattern(struct dp_ctrl_private *ctrl)
1190 dp_ctrl_train_pattern_set(ctrl, DP_TRAINING_PATTERN_DISABLE);
1191 drm_dp_link_train_channel_eq_delay(ctrl->aux, ctrl->panel->dpcd);
1194 static int dp_ctrl_link_train_2(struct dp_ctrl_private *ctrl,
1197 int tries = 0, ret = 0;
1200 int const maximum_retries = 5;
1201 u8 link_status[DP_LINK_STATUS_SIZE];
1203 dp_catalog_ctrl_state_ctrl(ctrl->catalog, 0);
1205 *training_step = DP_TRAINING_2;
1207 if (drm_dp_tps4_supported(ctrl->panel->dpcd)) {
1208 pattern = DP_TRAINING_PATTERN_4;
1210 } else if (drm_dp_tps3_supported(ctrl->panel->dpcd)) {
1211 pattern = DP_TRAINING_PATTERN_3;
1214 pattern = DP_TRAINING_PATTERN_2;
1218 ret = dp_catalog_ctrl_set_pattern_state_bit(ctrl->catalog, state_ctrl_bit);
1222 dp_ctrl_train_pattern_set(ctrl, pattern);
1224 for (tries = 0; tries <= maximum_retries; tries++) {
1225 drm_dp_link_train_channel_eq_delay(ctrl->aux, ctrl->panel->dpcd);
1227 ret = dp_ctrl_read_link_status(ctrl, link_status);
1231 if (drm_dp_channel_eq_ok(link_status,
1232 ctrl->link->link_params.num_lanes)) {
1236 dp_link_adjust_levels(ctrl->link, link_status);
1237 ret = dp_ctrl_update_vx_px(ctrl);
1246 static int dp_ctrl_link_train(struct dp_ctrl_private *ctrl,
1250 const u8 *dpcd = ctrl->panel->dpcd;
1251 u8 encoding[] = { 0, DP_SET_ANSI_8B10B };
1253 struct dp_link_info link_info = {0};
1255 dp_ctrl_config_ctrl(ctrl);
1257 link_info.num_lanes = ctrl->link->link_params.num_lanes;
1258 link_info.rate = ctrl->link->link_params.rate;
1259 link_info.capabilities = DP_LINK_CAP_ENHANCED_FRAMING;
1261 dp_aux_link_configure(ctrl->aux, &link_info);
1263 if (drm_dp_max_downspread(dpcd))
1264 encoding[0] |= DP_SPREAD_AMP_0_5;
1266 /* config DOWNSPREAD_CTRL and MAIN_LINK_CHANNEL_CODING_SET */
1267 drm_dp_dpcd_write(ctrl->aux, DP_DOWNSPREAD_CTRL, encoding, 2);
1269 if (drm_dp_alternate_scrambler_reset_cap(dpcd)) {
1270 assr = DP_ALTERNATE_SCRAMBLER_RESET_ENABLE;
1271 drm_dp_dpcd_write(ctrl->aux, DP_EDP_CONFIGURATION_SET,
1275 ret = dp_ctrl_link_train_1(ctrl, training_step);
1277 DRM_ERROR("link training #1 failed. ret=%d\n", ret);
1281 /* print success info as this is a result of user initiated action */
1282 drm_dbg_dp(ctrl->drm_dev, "link training #1 successful\n");
1284 ret = dp_ctrl_link_train_2(ctrl, training_step);
1286 DRM_ERROR("link training #2 failed. ret=%d\n", ret);
1290 /* print success info as this is a result of user initiated action */
1291 drm_dbg_dp(ctrl->drm_dev, "link training #2 successful\n");
1294 dp_catalog_ctrl_state_ctrl(ctrl->catalog, 0);
1299 static int dp_ctrl_setup_main_link(struct dp_ctrl_private *ctrl,
1304 dp_catalog_ctrl_mainlink_ctrl(ctrl->catalog, true);
1306 if (ctrl->link->sink_request & DP_TEST_LINK_PHY_TEST_PATTERN)
1310 * As part of previous calls, DP controller state might have
1311 * transitioned to PUSH_IDLE. In order to start transmitting
1312 * a link training pattern, we have to first do soft reset.
1315 ret = dp_ctrl_link_train(ctrl, training_step);
1320 static void dp_ctrl_set_clock_rate(struct dp_ctrl_private *ctrl,
1321 enum dp_pm_type module, char *name, unsigned long rate)
1323 u32 num = ctrl->parser->mp[module].num_clk;
1324 struct clk_bulk_data *cfg = ctrl->parser->mp[module].clocks;
1326 while (num && strcmp(cfg->id, name)) {
1331 drm_dbg_dp(ctrl->drm_dev, "setting rate=%lu on clk=%s\n",
1335 clk_set_rate(cfg->clk, rate);
1337 DRM_ERROR("%s clock doesn't exit to set rate %lu\n",
1341 static int dp_ctrl_enable_mainlink_clocks(struct dp_ctrl_private *ctrl)
1344 struct dp_io *dp_io = &ctrl->parser->io;
1345 struct phy *phy = dp_io->phy;
1346 struct phy_configure_opts_dp *opts_dp = &dp_io->phy_opts.dp;
1347 const u8 *dpcd = ctrl->panel->dpcd;
1349 opts_dp->lanes = ctrl->link->link_params.num_lanes;
1350 opts_dp->link_rate = ctrl->link->link_params.rate / 100;
1351 opts_dp->ssc = drm_dp_max_downspread(dpcd);
1353 phy_configure(phy, &dp_io->phy_opts);
1356 dev_pm_opp_set_rate(ctrl->dev, ctrl->link->link_params.rate * 1000);
1357 ret = dp_power_clk_enable(ctrl->power, DP_CTRL_PM, true);
1359 DRM_ERROR("Unable to start link clocks. ret=%d\n", ret);
1361 drm_dbg_dp(ctrl->drm_dev, "link rate=%d\n", ctrl->link->link_params.rate);
1366 void dp_ctrl_reset_irq_ctrl(struct dp_ctrl *dp_ctrl, bool enable)
1368 struct dp_ctrl_private *ctrl;
1370 ctrl = container_of(dp_ctrl, struct dp_ctrl_private, dp_ctrl);
1372 dp_catalog_ctrl_reset(ctrl->catalog);
1375 * all dp controller programmable registers will not
1376 * be reset to default value after DP_SW_RESET
1377 * therefore interrupt mask bits have to be updated
1378 * to enable/disable interrupts
1380 dp_catalog_ctrl_enable_irq(ctrl->catalog, enable);
1383 void dp_ctrl_config_psr(struct dp_ctrl *dp_ctrl)
1386 struct dp_ctrl_private *ctrl = container_of(dp_ctrl,
1387 struct dp_ctrl_private, dp_ctrl);
1389 if (!ctrl->panel->psr_cap.version)
1392 dp_catalog_ctrl_config_psr(ctrl->catalog);
1394 cfg = DP_PSR_ENABLE;
1395 drm_dp_dpcd_write(ctrl->aux, DP_PSR_EN_CFG, &cfg, 1);
1398 void dp_ctrl_set_psr(struct dp_ctrl *dp_ctrl, bool enter)
1400 struct dp_ctrl_private *ctrl = container_of(dp_ctrl,
1401 struct dp_ctrl_private, dp_ctrl);
1403 if (!ctrl->panel->psr_cap.version)
1407 * When entering PSR,
1408 * 1. Send PSR enter SDP and wait for the PSR_UPDATE_INT
1410 * 3. Disable the mainlink
1413 * 1. Enable the mainlink
1414 * 2. Send the PSR exit SDP
1417 reinit_completion(&ctrl->psr_op_comp);
1418 dp_catalog_ctrl_set_psr(ctrl->catalog, true);
1420 if (!wait_for_completion_timeout(&ctrl->psr_op_comp,
1421 PSR_OPERATION_COMPLETION_TIMEOUT_JIFFIES)) {
1422 DRM_ERROR("PSR_ENTRY timedout\n");
1423 dp_catalog_ctrl_set_psr(ctrl->catalog, false);
1427 dp_ctrl_push_idle(dp_ctrl);
1428 dp_catalog_ctrl_state_ctrl(ctrl->catalog, 0);
1430 dp_catalog_ctrl_psr_mainlink_enable(ctrl->catalog, false);
1432 dp_catalog_ctrl_psr_mainlink_enable(ctrl->catalog, true);
1434 dp_catalog_ctrl_set_psr(ctrl->catalog, false);
1435 dp_catalog_ctrl_state_ctrl(ctrl->catalog, DP_STATE_CTRL_SEND_VIDEO);
1436 dp_ctrl_wait4video_ready(ctrl);
1437 dp_catalog_ctrl_state_ctrl(ctrl->catalog, 0);
1441 void dp_ctrl_phy_init(struct dp_ctrl *dp_ctrl)
1443 struct dp_ctrl_private *ctrl;
1444 struct dp_io *dp_io;
1447 ctrl = container_of(dp_ctrl, struct dp_ctrl_private, dp_ctrl);
1448 dp_io = &ctrl->parser->io;
1451 dp_catalog_ctrl_phy_reset(ctrl->catalog);
1454 drm_dbg_dp(ctrl->drm_dev, "phy=%p init=%d power_on=%d\n",
1455 phy, phy->init_count, phy->power_count);
1458 void dp_ctrl_phy_exit(struct dp_ctrl *dp_ctrl)
1460 struct dp_ctrl_private *ctrl;
1461 struct dp_io *dp_io;
1464 ctrl = container_of(dp_ctrl, struct dp_ctrl_private, dp_ctrl);
1465 dp_io = &ctrl->parser->io;
1468 dp_catalog_ctrl_phy_reset(ctrl->catalog);
1470 drm_dbg_dp(ctrl->drm_dev, "phy=%p init=%d power_on=%d\n",
1471 phy, phy->init_count, phy->power_count);
1474 static bool dp_ctrl_use_fixed_nvid(struct dp_ctrl_private *ctrl)
1476 const u8 *dpcd = ctrl->panel->dpcd;
1479 * For better interop experience, used a fixed NVID=0x8000
1480 * whenever connected to a VGA dongle downstream.
1482 if (drm_dp_is_branch(dpcd))
1483 return (drm_dp_has_quirk(&ctrl->panel->desc,
1484 DP_DPCD_QUIRK_CONSTANT_N));
1489 static int dp_ctrl_reinitialize_mainlink(struct dp_ctrl_private *ctrl)
1492 struct dp_io *dp_io = &ctrl->parser->io;
1493 struct phy *phy = dp_io->phy;
1494 struct phy_configure_opts_dp *opts_dp = &dp_io->phy_opts.dp;
1496 dp_catalog_ctrl_mainlink_ctrl(ctrl->catalog, false);
1497 opts_dp->lanes = ctrl->link->link_params.num_lanes;
1498 phy_configure(phy, &dp_io->phy_opts);
1500 * Disable and re-enable the mainlink clock since the
1501 * link clock might have been adjusted as part of the
1504 dev_pm_opp_set_rate(ctrl->dev, 0);
1505 ret = dp_power_clk_enable(ctrl->power, DP_CTRL_PM, false);
1507 DRM_ERROR("Failed to disable clocks. ret=%d\n", ret);
1511 /* hw recommended delay before re-enabling clocks */
1514 ret = dp_ctrl_enable_mainlink_clocks(ctrl);
1516 DRM_ERROR("Failed to enable mainlink clks. ret=%d\n", ret);
1523 static int dp_ctrl_deinitialize_mainlink(struct dp_ctrl_private *ctrl)
1525 struct dp_io *dp_io;
1529 dp_io = &ctrl->parser->io;
1532 dp_catalog_ctrl_mainlink_ctrl(ctrl->catalog, false);
1534 dp_catalog_ctrl_reset(ctrl->catalog);
1536 dev_pm_opp_set_rate(ctrl->dev, 0);
1537 ret = dp_power_clk_enable(ctrl->power, DP_CTRL_PM, false);
1539 DRM_ERROR("Failed to disable link clocks. ret=%d\n", ret);
1544 /* aux channel down, reinit phy */
1548 drm_dbg_dp(ctrl->drm_dev, "phy=%p init=%d power_on=%d\n",
1549 phy, phy->init_count, phy->power_count);
1553 static int dp_ctrl_link_maintenance(struct dp_ctrl_private *ctrl)
1556 int training_step = DP_TRAINING_NONE;
1558 dp_ctrl_push_idle(&ctrl->dp_ctrl);
1560 ctrl->link->phy_params.p_level = 0;
1561 ctrl->link->phy_params.v_level = 0;
1563 ret = dp_ctrl_setup_main_link(ctrl, &training_step);
1567 dp_ctrl_clear_training_pattern(ctrl);
1569 dp_catalog_ctrl_state_ctrl(ctrl->catalog, DP_STATE_CTRL_SEND_VIDEO);
1571 ret = dp_ctrl_wait4video_ready(ctrl);
1576 static bool dp_ctrl_send_phy_test_pattern(struct dp_ctrl_private *ctrl)
1578 bool success = false;
1579 u32 pattern_sent = 0x0;
1580 u32 pattern_requested = ctrl->link->phy_params.phy_test_pattern_sel;
1582 drm_dbg_dp(ctrl->drm_dev, "request: 0x%x\n", pattern_requested);
1584 if (dp_catalog_ctrl_update_vx_px(ctrl->catalog,
1585 ctrl->link->phy_params.v_level,
1586 ctrl->link->phy_params.p_level)) {
1587 DRM_ERROR("Failed to set v/p levels\n");
1590 dp_catalog_ctrl_send_phy_pattern(ctrl->catalog, pattern_requested);
1591 dp_ctrl_update_vx_px(ctrl);
1592 dp_link_send_test_response(ctrl->link);
1594 pattern_sent = dp_catalog_ctrl_read_phy_pattern(ctrl->catalog);
1596 switch (pattern_sent) {
1597 case MR_LINK_TRAINING1:
1598 success = (pattern_requested ==
1599 DP_PHY_TEST_PATTERN_D10_2);
1601 case MR_LINK_SYMBOL_ERM:
1602 success = ((pattern_requested ==
1603 DP_PHY_TEST_PATTERN_ERROR_COUNT) ||
1604 (pattern_requested ==
1605 DP_PHY_TEST_PATTERN_CP2520));
1608 success = (pattern_requested ==
1609 DP_PHY_TEST_PATTERN_PRBS7);
1611 case MR_LINK_CUSTOM80:
1612 success = (pattern_requested ==
1613 DP_PHY_TEST_PATTERN_80BIT_CUSTOM);
1615 case MR_LINK_TRAINING4:
1616 success = (pattern_requested ==
1617 DP_PHY_TEST_PATTERN_SEL_MASK);
1623 drm_dbg_dp(ctrl->drm_dev, "%s: test->0x%x\n",
1624 success ? "success" : "failed", pattern_requested);
1628 static int dp_ctrl_process_phy_test_request(struct dp_ctrl_private *ctrl)
1631 unsigned long pixel_rate;
1633 if (!ctrl->link->phy_params.phy_test_pattern_sel) {
1634 drm_dbg_dp(ctrl->drm_dev,
1635 "no test pattern selected by sink\n");
1640 * The global reset will need DP link related clocks to be
1641 * running. Add the global reset just before disabling the
1642 * link clocks and core clocks.
1644 ret = dp_ctrl_off(&ctrl->dp_ctrl);
1646 DRM_ERROR("failed to disable DP controller\n");
1650 ret = dp_ctrl_on_link(&ctrl->dp_ctrl);
1652 DRM_ERROR("failed to enable DP link controller\n");
1656 pixel_rate = ctrl->panel->dp_mode.drm_mode.clock;
1657 dp_ctrl_set_clock_rate(ctrl, DP_STREAM_PM, "stream_pixel", pixel_rate * 1000);
1659 ret = dp_power_clk_enable(ctrl->power, DP_STREAM_PM, true);
1661 DRM_ERROR("Failed to start pixel clocks. ret=%d\n", ret);
1665 dp_ctrl_send_phy_test_pattern(ctrl);
1670 void dp_ctrl_handle_sink_request(struct dp_ctrl *dp_ctrl)
1672 struct dp_ctrl_private *ctrl;
1673 u32 sink_request = 0x0;
1676 DRM_ERROR("invalid input\n");
1680 ctrl = container_of(dp_ctrl, struct dp_ctrl_private, dp_ctrl);
1681 sink_request = ctrl->link->sink_request;
1683 if (sink_request & DP_TEST_LINK_PHY_TEST_PATTERN) {
1684 drm_dbg_dp(ctrl->drm_dev, "PHY_TEST_PATTERN request\n");
1685 if (dp_ctrl_process_phy_test_request(ctrl)) {
1686 DRM_ERROR("process phy_test_req failed\n");
1691 if (sink_request & DP_LINK_STATUS_UPDATED) {
1692 if (dp_ctrl_link_maintenance(ctrl)) {
1693 DRM_ERROR("LM failed: TEST_LINK_TRAINING\n");
1698 if (sink_request & DP_TEST_LINK_TRAINING) {
1699 dp_link_send_test_response(ctrl->link);
1700 if (dp_ctrl_link_maintenance(ctrl)) {
1701 DRM_ERROR("LM failed: TEST_LINK_TRAINING\n");
1707 static bool dp_ctrl_clock_recovery_any_ok(
1708 const u8 link_status[DP_LINK_STATUS_SIZE],
1713 if (lane_count <= 1)
1717 * only interested in the lane number after reduced
1718 * lane_count = 4, then only interested in 2 lanes
1719 * lane_count = 2, then only interested in 1 lane
1721 reduced_cnt = lane_count >> 1;
1723 return drm_dp_clock_recovery_ok(link_status, reduced_cnt);
1726 static bool dp_ctrl_channel_eq_ok(struct dp_ctrl_private *ctrl)
1728 u8 link_status[DP_LINK_STATUS_SIZE];
1729 int num_lanes = ctrl->link->link_params.num_lanes;
1731 dp_ctrl_read_link_status(ctrl, link_status);
1733 return drm_dp_channel_eq_ok(link_status, num_lanes);
1736 int dp_ctrl_on_link(struct dp_ctrl *dp_ctrl)
1739 struct dp_ctrl_private *ctrl;
1741 int link_train_max_retries = 5;
1742 u32 const phy_cts_pixel_clk_khz = 148500;
1743 u8 link_status[DP_LINK_STATUS_SIZE];
1744 unsigned int training_step;
1745 unsigned long pixel_rate;
1750 ctrl = container_of(dp_ctrl, struct dp_ctrl_private, dp_ctrl);
1752 rate = ctrl->panel->link_info.rate;
1753 pixel_rate = ctrl->panel->dp_mode.drm_mode.clock;
1755 dp_power_clk_enable(ctrl->power, DP_CORE_PM, true);
1757 if (ctrl->link->sink_request & DP_TEST_LINK_PHY_TEST_PATTERN) {
1758 drm_dbg_dp(ctrl->drm_dev,
1759 "using phy test link parameters\n");
1761 pixel_rate = phy_cts_pixel_clk_khz;
1763 ctrl->link->link_params.rate = rate;
1764 ctrl->link->link_params.num_lanes =
1765 ctrl->panel->link_info.num_lanes;
1768 drm_dbg_dp(ctrl->drm_dev, "rate=%d, num_lanes=%d, pixel_rate=%lu\n",
1769 ctrl->link->link_params.rate, ctrl->link->link_params.num_lanes,
1772 rc = dp_ctrl_enable_mainlink_clocks(ctrl);
1776 while (--link_train_max_retries) {
1777 rc = dp_ctrl_reinitialize_mainlink(ctrl);
1779 DRM_ERROR("Failed to reinitialize mainlink. rc=%d\n",
1784 training_step = DP_TRAINING_NONE;
1785 rc = dp_ctrl_setup_main_link(ctrl, &training_step);
1787 /* training completed successfully */
1789 } else if (training_step == DP_TRAINING_1) {
1790 /* link train_1 failed */
1791 if (!dp_catalog_link_is_connected(ctrl->catalog))
1794 dp_ctrl_read_link_status(ctrl, link_status);
1796 rc = dp_ctrl_link_rate_down_shift(ctrl);
1797 if (rc < 0) { /* already in RBR = 1.6G */
1798 if (dp_ctrl_clock_recovery_any_ok(link_status,
1799 ctrl->link->link_params.num_lanes)) {
1801 * some lanes are ready,
1802 * reduce lane number
1804 rc = dp_ctrl_link_lane_down_shift(ctrl);
1805 if (rc < 0) { /* lane == 1 already */
1806 /* end with failure */
1810 /* end with failure */
1811 break; /* lane == 1 already */
1814 } else if (training_step == DP_TRAINING_2) {
1815 /* link train_2 failed */
1816 if (!dp_catalog_link_is_connected(ctrl->catalog))
1819 dp_ctrl_read_link_status(ctrl, link_status);
1821 if (!drm_dp_clock_recovery_ok(link_status,
1822 ctrl->link->link_params.num_lanes))
1823 rc = dp_ctrl_link_rate_down_shift(ctrl);
1825 rc = dp_ctrl_link_lane_down_shift(ctrl);
1828 /* end with failure */
1829 break; /* lane == 1 already */
1832 /* stop link training before start re training */
1833 dp_ctrl_clear_training_pattern(ctrl);
1837 if (ctrl->link->sink_request & DP_TEST_LINK_PHY_TEST_PATTERN)
1840 if (rc == 0) { /* link train successfully */
1842 * do not stop train pattern here
1843 * stop link training at on_stream
1844 * to pass compliance test
1848 * link training failed
1849 * end txing train pattern here
1851 dp_ctrl_clear_training_pattern(ctrl);
1853 dp_ctrl_deinitialize_mainlink(ctrl);
1860 static int dp_ctrl_link_retrain(struct dp_ctrl_private *ctrl)
1862 int training_step = DP_TRAINING_NONE;
1864 return dp_ctrl_setup_main_link(ctrl, &training_step);
1867 int dp_ctrl_on_stream(struct dp_ctrl *dp_ctrl, bool force_link_train)
1870 bool mainlink_ready = false;
1871 struct dp_ctrl_private *ctrl;
1872 unsigned long pixel_rate;
1873 unsigned long pixel_rate_orig;
1878 ctrl = container_of(dp_ctrl, struct dp_ctrl_private, dp_ctrl);
1880 pixel_rate = pixel_rate_orig = ctrl->panel->dp_mode.drm_mode.clock;
1882 if (dp_ctrl->wide_bus_en)
1885 drm_dbg_dp(ctrl->drm_dev, "rate=%d, num_lanes=%d, pixel_rate=%lu\n",
1886 ctrl->link->link_params.rate,
1887 ctrl->link->link_params.num_lanes, pixel_rate);
1889 if (!dp_power_clk_status(ctrl->power, DP_CTRL_PM)) { /* link clk is off */
1890 ret = dp_ctrl_enable_mainlink_clocks(ctrl);
1892 DRM_ERROR("Failed to start link clocks. ret=%d\n", ret);
1897 dp_ctrl_set_clock_rate(ctrl, DP_STREAM_PM, "stream_pixel", pixel_rate * 1000);
1899 ret = dp_power_clk_enable(ctrl->power, DP_STREAM_PM, true);
1901 DRM_ERROR("Unable to start pixel clocks. ret=%d\n", ret);
1905 if (force_link_train || !dp_ctrl_channel_eq_ok(ctrl))
1906 dp_ctrl_link_retrain(ctrl);
1908 /* stop txing train pattern to end link training */
1909 dp_ctrl_clear_training_pattern(ctrl);
1912 * Set up transfer unit values and set controller state to send
1915 reinit_completion(&ctrl->video_comp);
1917 dp_ctrl_configure_source_params(ctrl);
1919 dp_catalog_ctrl_config_msa(ctrl->catalog,
1920 ctrl->link->link_params.rate,
1921 pixel_rate_orig, dp_ctrl_use_fixed_nvid(ctrl));
1923 dp_ctrl_setup_tr_unit(ctrl);
1925 dp_catalog_ctrl_state_ctrl(ctrl->catalog, DP_STATE_CTRL_SEND_VIDEO);
1927 ret = dp_ctrl_wait4video_ready(ctrl);
1931 mainlink_ready = dp_catalog_ctrl_mainlink_ready(ctrl->catalog);
1932 drm_dbg_dp(ctrl->drm_dev,
1933 "mainlink %s\n", mainlink_ready ? "READY" : "NOT READY");
1939 int dp_ctrl_off_link_stream(struct dp_ctrl *dp_ctrl)
1941 struct dp_ctrl_private *ctrl;
1942 struct dp_io *dp_io;
1946 ctrl = container_of(dp_ctrl, struct dp_ctrl_private, dp_ctrl);
1947 dp_io = &ctrl->parser->io;
1950 /* set dongle to D3 (power off) mode */
1951 dp_link_psm_config(ctrl->link, &ctrl->panel->link_info, true);
1953 dp_catalog_ctrl_mainlink_ctrl(ctrl->catalog, false);
1955 if (dp_power_clk_status(ctrl->power, DP_STREAM_PM)) {
1956 ret = dp_power_clk_enable(ctrl->power, DP_STREAM_PM, false);
1958 DRM_ERROR("Failed to disable pclk. ret=%d\n", ret);
1963 dev_pm_opp_set_rate(ctrl->dev, 0);
1964 ret = dp_power_clk_enable(ctrl->power, DP_CTRL_PM, false);
1966 DRM_ERROR("Failed to disable link clocks. ret=%d\n", ret);
1972 /* aux channel down, reinit phy */
1976 drm_dbg_dp(ctrl->drm_dev, "phy=%p init=%d power_on=%d\n",
1977 phy, phy->init_count, phy->power_count);
1981 int dp_ctrl_off_link(struct dp_ctrl *dp_ctrl)
1983 struct dp_ctrl_private *ctrl;
1984 struct dp_io *dp_io;
1988 ctrl = container_of(dp_ctrl, struct dp_ctrl_private, dp_ctrl);
1989 dp_io = &ctrl->parser->io;
1992 dp_catalog_ctrl_mainlink_ctrl(ctrl->catalog, false);
1994 ret = dp_power_clk_enable(ctrl->power, DP_CTRL_PM, false);
1996 DRM_ERROR("Failed to disable link clocks. ret=%d\n", ret);
1999 DRM_DEBUG_DP("Before, phy=%p init_count=%d power_on=%d\n",
2000 phy, phy->init_count, phy->power_count);
2004 DRM_DEBUG_DP("After, phy=%p init_count=%d power_on=%d\n",
2005 phy, phy->init_count, phy->power_count);
2010 int dp_ctrl_off(struct dp_ctrl *dp_ctrl)
2012 struct dp_ctrl_private *ctrl;
2013 struct dp_io *dp_io;
2020 ctrl = container_of(dp_ctrl, struct dp_ctrl_private, dp_ctrl);
2021 dp_io = &ctrl->parser->io;
2024 dp_catalog_ctrl_mainlink_ctrl(ctrl->catalog, false);
2026 dp_catalog_ctrl_reset(ctrl->catalog);
2028 ret = dp_power_clk_enable(ctrl->power, DP_STREAM_PM, false);
2030 DRM_ERROR("Failed to disable pixel clocks. ret=%d\n", ret);
2032 dev_pm_opp_set_rate(ctrl->dev, 0);
2033 ret = dp_power_clk_enable(ctrl->power, DP_CTRL_PM, false);
2035 DRM_ERROR("Failed to disable link clocks. ret=%d\n", ret);
2039 drm_dbg_dp(ctrl->drm_dev, "phy=%p init=%d power_on=%d\n",
2040 phy, phy->init_count, phy->power_count);
2045 irqreturn_t dp_ctrl_isr(struct dp_ctrl *dp_ctrl)
2047 struct dp_ctrl_private *ctrl;
2049 irqreturn_t ret = IRQ_NONE;
2054 ctrl = container_of(dp_ctrl, struct dp_ctrl_private, dp_ctrl);
2056 if (ctrl->panel->psr_cap.version) {
2057 isr = dp_catalog_ctrl_read_psr_interrupt_status(ctrl->catalog);
2060 complete(&ctrl->psr_op_comp);
2062 if (isr & PSR_EXIT_INT)
2063 drm_dbg_dp(ctrl->drm_dev, "PSR exit done\n");
2065 if (isr & PSR_UPDATE_INT)
2066 drm_dbg_dp(ctrl->drm_dev, "PSR frame update done\n");
2068 if (isr & PSR_CAPTURE_INT)
2069 drm_dbg_dp(ctrl->drm_dev, "PSR frame capture done\n");
2072 isr = dp_catalog_ctrl_get_interrupt(ctrl->catalog);
2075 if (isr & DP_CTRL_INTR_READY_FOR_VIDEO) {
2076 drm_dbg_dp(ctrl->drm_dev, "dp_video_ready\n");
2077 complete(&ctrl->video_comp);
2081 if (isr & DP_CTRL_INTR_IDLE_PATTERN_SENT) {
2082 drm_dbg_dp(ctrl->drm_dev, "idle_patterns_sent\n");
2083 complete(&ctrl->idle_comp);
2090 struct dp_ctrl *dp_ctrl_get(struct device *dev, struct dp_link *link,
2091 struct dp_panel *panel, struct drm_dp_aux *aux,
2092 struct dp_power *power, struct dp_catalog *catalog,
2093 struct dp_parser *parser)
2095 struct dp_ctrl_private *ctrl;
2098 if (!dev || !panel || !aux ||
2099 !link || !catalog) {
2100 DRM_ERROR("invalid input\n");
2101 return ERR_PTR(-EINVAL);
2104 ctrl = devm_kzalloc(dev, sizeof(*ctrl), GFP_KERNEL);
2106 DRM_ERROR("Mem allocation failure\n");
2107 return ERR_PTR(-ENOMEM);
2110 ret = devm_pm_opp_set_clkname(dev, "ctrl_link");
2112 dev_err(dev, "invalid DP OPP table in device tree\n");
2113 /* caller do PTR_ERR(opp_table) */
2114 return (struct dp_ctrl *)ERR_PTR(ret);
2117 /* OPP table is optional */
2118 ret = devm_pm_opp_of_add_table(dev);
2120 dev_err(dev, "failed to add DP OPP table\n");
2122 init_completion(&ctrl->idle_comp);
2123 init_completion(&ctrl->psr_op_comp);
2124 init_completion(&ctrl->video_comp);
2127 ctrl->parser = parser;
2128 ctrl->panel = panel;
2129 ctrl->power = power;
2132 ctrl->catalog = catalog;
2135 return &ctrl->dp_ctrl;