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_catalog *catalog;
83 unsigned int num_core_clks;
84 struct clk_bulk_data *core_clks;
86 unsigned int num_link_clks;
87 struct clk_bulk_data *link_clks;
89 struct clk *pixel_clk;
91 union phy_configure_opts phy_opts;
93 struct completion idle_comp;
94 struct completion psr_op_comp;
95 struct completion video_comp;
102 static int dp_aux_link_configure(struct drm_dp_aux *aux,
103 struct dp_link_info *link)
108 values[0] = drm_dp_link_rate_to_bw_code(link->rate);
109 values[1] = link->num_lanes;
111 if (link->capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
112 values[1] |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
114 err = drm_dp_dpcd_write(aux, DP_LINK_BW_SET, values, sizeof(values));
121 void dp_ctrl_push_idle(struct dp_ctrl *dp_ctrl)
123 struct dp_ctrl_private *ctrl;
125 ctrl = container_of(dp_ctrl, struct dp_ctrl_private, dp_ctrl);
127 reinit_completion(&ctrl->idle_comp);
128 dp_catalog_ctrl_state_ctrl(ctrl->catalog, DP_STATE_CTRL_PUSH_IDLE);
130 if (!wait_for_completion_timeout(&ctrl->idle_comp,
131 IDLE_PATTERN_COMPLETION_TIMEOUT_JIFFIES))
132 pr_warn("PUSH_IDLE pattern timedout\n");
134 drm_dbg_dp(ctrl->drm_dev, "mainlink off\n");
137 static void dp_ctrl_config_ctrl(struct dp_ctrl_private *ctrl)
140 const u8 *dpcd = ctrl->panel->dpcd;
142 /* Default-> LSCLK DIV: 1/4 LCLK */
143 config |= (2 << DP_CONFIGURATION_CTRL_LSCLK_DIV_SHIFT);
145 if (ctrl->panel->dp_mode.out_fmt_is_yuv_420)
146 config |= DP_CONFIGURATION_CTRL_RGB_YUV; /* YUV420 */
148 /* Scrambler reset enable */
149 if (drm_dp_alternate_scrambler_reset_cap(dpcd))
150 config |= DP_CONFIGURATION_CTRL_ASSR;
152 tbd = dp_link_get_test_bits_depth(ctrl->link,
153 ctrl->panel->dp_mode.bpp);
155 config |= tbd << DP_CONFIGURATION_CTRL_BPC_SHIFT;
158 config |= ((ctrl->link->link_params.num_lanes - 1)
159 << DP_CONFIGURATION_CTRL_NUM_OF_LANES_SHIFT);
161 if (drm_dp_enhanced_frame_cap(dpcd))
162 config |= DP_CONFIGURATION_CTRL_ENHANCED_FRAMING;
164 config |= DP_CONFIGURATION_CTRL_P_INTERLACED; /* progressive video */
166 /* sync clock & static Mvid */
167 config |= DP_CONFIGURATION_CTRL_STATIC_DYNAMIC_CN;
168 config |= DP_CONFIGURATION_CTRL_SYNC_ASYNC_CLK;
170 if (ctrl->panel->psr_cap.version)
171 config |= DP_CONFIGURATION_CTRL_SEND_VSC;
173 dp_catalog_ctrl_config_ctrl(ctrl->catalog, config);
176 static void dp_ctrl_configure_source_params(struct dp_ctrl_private *ctrl)
180 dp_catalog_ctrl_lane_mapping(ctrl->catalog);
181 dp_catalog_ctrl_mainlink_ctrl(ctrl->catalog, true);
182 dp_catalog_setup_peripheral_flush(ctrl->catalog);
184 dp_ctrl_config_ctrl(ctrl);
186 tb = dp_link_get_test_bits_depth(ctrl->link,
187 ctrl->panel->dp_mode.bpp);
188 cc = dp_link_get_colorimetry_config(ctrl->link);
189 dp_catalog_ctrl_config_misc(ctrl->catalog, cc, tb);
190 dp_panel_timing_cfg(ctrl->panel);
194 * The structure and few functions present below are IP/Hardware
195 * specific implementation. Most of the implementation will not
196 * have coding comments
198 struct tu_algo_data {
203 s64 hbp_relative_to_pclk;
204 s64 hbp_relative_to_pclk_fp;
212 uint delay_start_link_extra_pixclk;
213 int extra_buffer_margin;
215 s64 original_ratio_fp;
224 int valid_boundary_link;
225 s64 resulting_valid_fp;
227 s64 effective_valid_fp;
228 s64 effective_valid_recorded_fp;
233 int remainder_tus_upper;
234 int remainder_tus_lower;
237 int delay_start_link;
239 int extra_pclk_cycles;
240 int extra_pclk_cycles_in_link_clk;
242 s64 average_valid2_fp;
243 int new_valid_boundary_link;
244 int remainder_symbols_exist;
246 s64 n_remainder_symbols_per_lane_fp;
247 s64 last_partial_tu_fp;
250 int n_tus_incl_last_incomplete_tu;
251 int extra_pclk_cycles_tmp;
252 int extra_pclk_cycles_in_link_clk_tmp;
253 int extra_required_bytes_new_tmp;
255 int lower_filler_size_tmp;
256 int delay_start_link_tmp;
258 bool boundary_moderation_en;
259 int boundary_mod_lower_err;
260 int upper_boundary_count;
261 int lower_boundary_count;
262 int i_upper_boundary_count;
263 int i_lower_boundary_count;
264 int valid_lower_boundary_link;
265 int even_distribution_BF;
266 int even_distribution_legacy;
267 int even_distribution;
268 int min_hblank_violated;
269 s64 delay_start_time_fp;
277 static int _tu_param_compare(s64 a, s64 b)
281 s64 a_temp, b_temp, minus_1;
286 minus_1 = drm_fixp_from_fraction(-1, 1);
288 a_sign = (a >> 32) & 0x80000000 ? 1 : 0;
290 b_sign = (b >> 32) & 0x80000000 ? 1 : 0;
294 else if (b_sign > a_sign)
297 if (!a_sign && !b_sign) { /* positive */
302 } else { /* negative */
303 a_temp = drm_fixp_mul(a, minus_1);
304 b_temp = drm_fixp_mul(b, minus_1);
313 static void dp_panel_update_tu_timings(struct dp_tu_calc_input *in,
314 struct tu_algo_data *tu)
316 int nlanes = in->nlanes;
317 int dsc_num_slices = in->num_of_dsc_slices;
318 int dsc_num_bytes = 0;
324 int tot_num_eoc_symbols = 0;
325 int tot_num_hor_bytes = 0;
326 int tot_num_dummy_bytes = 0;
327 int dwidth_dsc_bytes = 0;
330 s64 temp1_fp, temp2_fp, temp3_fp;
332 tu->lclk_fp = drm_fixp_from_fraction(in->lclk, 1);
333 tu->pclk_fp = drm_fixp_from_fraction(in->pclk_khz, 1000);
334 tu->lwidth = in->hactive;
335 tu->hbp_relative_to_pclk = in->hporch;
336 tu->nlanes = in->nlanes;
338 tu->pixelEnc = in->pixel_enc;
339 tu->dsc_en = in->dsc_en;
340 tu->async_en = in->async_en;
341 tu->lwidth_fp = drm_fixp_from_fraction(in->hactive, 1);
342 tu->hbp_relative_to_pclk_fp = drm_fixp_from_fraction(in->hporch, 1);
344 if (tu->pixelEnc == 420) {
345 temp1_fp = drm_fixp_from_fraction(2, 1);
346 tu->pclk_fp = drm_fixp_div(tu->pclk_fp, temp1_fp);
347 tu->lwidth_fp = drm_fixp_div(tu->lwidth_fp, temp1_fp);
348 tu->hbp_relative_to_pclk_fp =
349 drm_fixp_div(tu->hbp_relative_to_pclk_fp, 2);
352 if (tu->pixelEnc == 422) {
374 temp1_fp = drm_fixp_from_fraction(in->compress_ratio, 100);
375 temp2_fp = drm_fixp_from_fraction(in->bpp, 1);
376 temp3_fp = drm_fixp_div(temp2_fp, temp1_fp);
377 temp2_fp = drm_fixp_mul(tu->lwidth_fp, temp3_fp);
379 temp1_fp = drm_fixp_from_fraction(8, 1);
380 temp3_fp = drm_fixp_div(temp2_fp, temp1_fp);
382 numerator = drm_fixp2int(temp3_fp);
384 dsc_num_bytes = numerator / dsc_num_slices;
385 eoc_bytes = dsc_num_bytes % nlanes;
386 tot_num_eoc_symbols = nlanes * dsc_num_slices;
387 tot_num_hor_bytes = dsc_num_bytes * dsc_num_slices;
388 tot_num_dummy_bytes = (nlanes - eoc_bytes) * dsc_num_slices;
390 if (dsc_num_bytes == 0)
391 pr_info("incorrect no of bytes per slice=%d\n", dsc_num_bytes);
393 dwidth_dsc_bytes = (tot_num_hor_bytes +
394 tot_num_eoc_symbols +
395 (eoc_bytes == 0 ? 0 : tot_num_dummy_bytes));
397 dwidth_dsc_fp = drm_fixp_from_fraction(dwidth_dsc_bytes, 3);
399 temp2_fp = drm_fixp_mul(tu->pclk_fp, dwidth_dsc_fp);
400 temp1_fp = drm_fixp_div(temp2_fp, tu->lwidth_fp);
401 pclk_dsc_fp = temp1_fp;
403 temp1_fp = drm_fixp_div(pclk_dsc_fp, tu->pclk_fp);
404 temp2_fp = drm_fixp_mul(tu->hbp_relative_to_pclk_fp, temp1_fp);
405 hbp_dsc_fp = temp2_fp;
408 tu->pclk_fp = pclk_dsc_fp;
409 tu->lwidth_fp = dwidth_dsc_fp;
410 tu->hbp_relative_to_pclk_fp = hbp_dsc_fp;
414 temp1_fp = drm_fixp_from_fraction(976, 1000); /* 0.976 */
415 tu->lclk_fp = drm_fixp_mul(tu->lclk_fp, temp1_fp);
419 static void _tu_valid_boundary_calc(struct tu_algo_data *tu)
421 s64 temp1_fp, temp2_fp, temp, temp1, temp2;
422 int compare_result_1, compare_result_2, compare_result_3;
424 temp1_fp = drm_fixp_from_fraction(tu->tu_size, 1);
425 temp2_fp = drm_fixp_mul(tu->ratio_fp, temp1_fp);
427 tu->new_valid_boundary_link = drm_fixp2int_ceil(temp2_fp);
429 temp = (tu->i_upper_boundary_count *
430 tu->new_valid_boundary_link +
431 tu->i_lower_boundary_count *
432 (tu->new_valid_boundary_link-1));
433 tu->average_valid2_fp = drm_fixp_from_fraction(temp,
434 (tu->i_upper_boundary_count +
435 tu->i_lower_boundary_count));
437 temp1_fp = drm_fixp_from_fraction(tu->bpp, 8);
438 temp2_fp = tu->lwidth_fp;
439 temp1_fp = drm_fixp_mul(temp2_fp, temp1_fp);
440 temp2_fp = drm_fixp_div(temp1_fp, tu->average_valid2_fp);
441 tu->n_tus = drm_fixp2int(temp2_fp);
442 if ((temp2_fp & 0xFFFFFFFF) > 0xFFFFF000)
445 temp1_fp = drm_fixp_from_fraction(tu->n_tus, 1);
446 temp2_fp = drm_fixp_mul(temp1_fp, tu->average_valid2_fp);
447 temp1_fp = drm_fixp_from_fraction(tu->n_symbols, 1);
448 temp2_fp = temp1_fp - temp2_fp;
449 temp1_fp = drm_fixp_from_fraction(tu->nlanes, 1);
450 temp2_fp = drm_fixp_div(temp2_fp, temp1_fp);
451 tu->n_remainder_symbols_per_lane_fp = temp2_fp;
453 temp1_fp = drm_fixp_from_fraction(tu->tu_size, 1);
454 tu->last_partial_tu_fp =
455 drm_fixp_div(tu->n_remainder_symbols_per_lane_fp,
458 if (tu->n_remainder_symbols_per_lane_fp != 0)
459 tu->remainder_symbols_exist = 1;
461 tu->remainder_symbols_exist = 0;
463 temp1_fp = drm_fixp_from_fraction(tu->n_tus, tu->nlanes);
464 tu->n_tus_per_lane = drm_fixp2int(temp1_fp);
466 tu->paired_tus = (int)((tu->n_tus_per_lane) /
467 (tu->i_upper_boundary_count +
468 tu->i_lower_boundary_count));
470 tu->remainder_tus = tu->n_tus_per_lane - tu->paired_tus *
471 (tu->i_upper_boundary_count +
472 tu->i_lower_boundary_count);
474 if ((tu->remainder_tus - tu->i_upper_boundary_count) > 0) {
475 tu->remainder_tus_upper = tu->i_upper_boundary_count;
476 tu->remainder_tus_lower = tu->remainder_tus -
477 tu->i_upper_boundary_count;
479 tu->remainder_tus_upper = tu->remainder_tus;
480 tu->remainder_tus_lower = 0;
483 temp = tu->paired_tus * (tu->i_upper_boundary_count *
484 tu->new_valid_boundary_link +
485 tu->i_lower_boundary_count *
486 (tu->new_valid_boundary_link - 1)) +
487 (tu->remainder_tus_upper *
488 tu->new_valid_boundary_link) +
489 (tu->remainder_tus_lower *
490 (tu->new_valid_boundary_link - 1));
491 tu->total_valid_fp = drm_fixp_from_fraction(temp, 1);
493 if (tu->remainder_symbols_exist) {
494 temp1_fp = tu->total_valid_fp +
495 tu->n_remainder_symbols_per_lane_fp;
496 temp2_fp = drm_fixp_from_fraction(tu->n_tus_per_lane, 1);
497 temp2_fp = temp2_fp + tu->last_partial_tu_fp;
498 temp1_fp = drm_fixp_div(temp1_fp, temp2_fp);
500 temp2_fp = drm_fixp_from_fraction(tu->n_tus_per_lane, 1);
501 temp1_fp = drm_fixp_div(tu->total_valid_fp, temp2_fp);
503 tu->effective_valid_fp = temp1_fp;
505 temp1_fp = drm_fixp_from_fraction(tu->tu_size, 1);
506 temp2_fp = drm_fixp_mul(tu->ratio_fp, temp1_fp);
507 tu->n_n_err_fp = tu->effective_valid_fp - temp2_fp;
509 temp1_fp = drm_fixp_from_fraction(tu->tu_size, 1);
510 temp2_fp = drm_fixp_mul(tu->ratio_fp, temp1_fp);
511 tu->n_err_fp = tu->average_valid2_fp - temp2_fp;
513 tu->even_distribution = tu->n_tus % tu->nlanes == 0 ? 1 : 0;
515 temp1_fp = drm_fixp_from_fraction(tu->bpp, 8);
516 temp2_fp = tu->lwidth_fp;
517 temp1_fp = drm_fixp_mul(temp2_fp, temp1_fp);
518 temp2_fp = drm_fixp_div(temp1_fp, tu->average_valid2_fp);
521 tu->n_tus_incl_last_incomplete_tu = drm_fixp2int_ceil(temp2_fp);
523 tu->n_tus_incl_last_incomplete_tu = 0;
526 temp1_fp = drm_fixp_from_fraction(tu->tu_size, 1);
527 temp2_fp = drm_fixp_mul(tu->original_ratio_fp, temp1_fp);
528 temp1_fp = tu->average_valid2_fp - temp2_fp;
529 temp2_fp = drm_fixp_from_fraction(tu->n_tus_incl_last_incomplete_tu, 1);
530 temp1_fp = drm_fixp_mul(temp2_fp, temp1_fp);
533 temp1 = drm_fixp2int_ceil(temp1_fp);
535 temp = tu->i_upper_boundary_count * tu->nlanes;
536 temp1_fp = drm_fixp_from_fraction(tu->tu_size, 1);
537 temp2_fp = drm_fixp_mul(tu->original_ratio_fp, temp1_fp);
538 temp1_fp = drm_fixp_from_fraction(tu->new_valid_boundary_link, 1);
539 temp2_fp = temp1_fp - temp2_fp;
540 temp1_fp = drm_fixp_from_fraction(temp, 1);
541 temp2_fp = drm_fixp_mul(temp1_fp, temp2_fp);
544 temp2 = drm_fixp2int_ceil(temp2_fp);
547 tu->extra_required_bytes_new_tmp = (int)(temp1 + temp2);
549 temp1_fp = drm_fixp_from_fraction(8, tu->bpp);
550 temp2_fp = drm_fixp_from_fraction(
551 tu->extra_required_bytes_new_tmp, 1);
552 temp1_fp = drm_fixp_mul(temp2_fp, temp1_fp);
555 tu->extra_pclk_cycles_tmp = drm_fixp2int_ceil(temp1_fp);
557 tu->extra_pclk_cycles_tmp = 0;
559 temp1_fp = drm_fixp_from_fraction(tu->extra_pclk_cycles_tmp, 1);
560 temp2_fp = drm_fixp_div(tu->lclk_fp, tu->pclk_fp);
561 temp1_fp = drm_fixp_mul(temp1_fp, temp2_fp);
564 tu->extra_pclk_cycles_in_link_clk_tmp =
565 drm_fixp2int_ceil(temp1_fp);
567 tu->extra_pclk_cycles_in_link_clk_tmp = 0;
569 tu->filler_size_tmp = tu->tu_size - tu->new_valid_boundary_link;
571 tu->lower_filler_size_tmp = tu->filler_size_tmp + 1;
573 tu->delay_start_link_tmp = tu->extra_pclk_cycles_in_link_clk_tmp +
574 tu->lower_filler_size_tmp +
575 tu->extra_buffer_margin;
577 temp1_fp = drm_fixp_from_fraction(tu->delay_start_link_tmp, 1);
578 tu->delay_start_time_fp = drm_fixp_div(temp1_fp, tu->lclk_fp);
580 compare_result_1 = _tu_param_compare(tu->n_n_err_fp, tu->diff_abs_fp);
581 if (compare_result_1 == 2)
582 compare_result_1 = 1;
584 compare_result_1 = 0;
586 compare_result_2 = _tu_param_compare(tu->n_n_err_fp, tu->err_fp);
587 if (compare_result_2 == 2)
588 compare_result_2 = 1;
590 compare_result_2 = 0;
592 compare_result_3 = _tu_param_compare(tu->hbp_time_fp,
593 tu->delay_start_time_fp);
594 if (compare_result_3 == 2)
595 compare_result_3 = 0;
597 compare_result_3 = 1;
599 if (((tu->even_distribution == 1) ||
600 ((tu->even_distribution_BF == 0) &&
601 (tu->even_distribution_legacy == 0))) &&
602 tu->n_err_fp >= 0 && tu->n_n_err_fp >= 0 &&
604 (compare_result_1 || (tu->min_hblank_violated == 1)) &&
605 (tu->new_valid_boundary_link - 1) > 0 &&
607 (tu->delay_start_link_tmp <= 1023)) {
608 tu->upper_boundary_count = tu->i_upper_boundary_count;
609 tu->lower_boundary_count = tu->i_lower_boundary_count;
610 tu->err_fp = tu->n_n_err_fp;
611 tu->boundary_moderation_en = true;
612 tu->tu_size_desired = tu->tu_size;
613 tu->valid_boundary_link = tu->new_valid_boundary_link;
614 tu->effective_valid_recorded_fp = tu->effective_valid_fp;
615 tu->even_distribution_BF = 1;
616 tu->delay_start_link = tu->delay_start_link_tmp;
617 } else if (tu->boundary_mod_lower_err == 0) {
618 compare_result_1 = _tu_param_compare(tu->n_n_err_fp,
620 if (compare_result_1 == 2)
621 tu->boundary_mod_lower_err = 1;
625 static void _dp_ctrl_calc_tu(struct dp_ctrl_private *ctrl,
626 struct dp_tu_calc_input *in,
627 struct dp_vc_tu_mapping_table *tu_table)
629 struct tu_algo_data *tu;
630 int compare_result_1, compare_result_2;
632 s64 temp_fp = 0, temp1_fp = 0, temp2_fp = 0;
634 s64 LCLK_FAST_SKEW_fp = drm_fixp_from_fraction(6, 10000); /* 0.0006 */
635 s64 const_p49_fp = drm_fixp_from_fraction(49, 100); /* 0.49 */
636 s64 const_p56_fp = drm_fixp_from_fraction(56, 100); /* 0.56 */
637 s64 RATIO_SCALE_fp = drm_fixp_from_fraction(1001, 1000);
639 u8 DP_BRUTE_FORCE = 1;
640 s64 BRUTE_FORCE_THRESHOLD_fp = drm_fixp_from_fraction(1, 10); /* 0.1 */
641 uint EXTRA_PIXCLK_CYCLE_DELAY = 4;
642 uint HBLANK_MARGIN = 4;
644 tu = kzalloc(sizeof(*tu), GFP_KERNEL);
648 dp_panel_update_tu_timings(in, tu);
650 tu->err_fp = drm_fixp_from_fraction(1000, 1); /* 1000 */
652 temp1_fp = drm_fixp_from_fraction(4, 1);
653 temp2_fp = drm_fixp_mul(temp1_fp, tu->lclk_fp);
654 temp_fp = drm_fixp_div(temp2_fp, tu->pclk_fp);
655 tu->extra_buffer_margin = drm_fixp2int_ceil(temp_fp);
657 temp1_fp = drm_fixp_from_fraction(tu->bpp, 8);
658 temp2_fp = drm_fixp_mul(tu->pclk_fp, temp1_fp);
659 temp1_fp = drm_fixp_from_fraction(tu->nlanes, 1);
660 temp2_fp = drm_fixp_div(temp2_fp, temp1_fp);
661 tu->ratio_fp = drm_fixp_div(temp2_fp, tu->lclk_fp);
663 tu->original_ratio_fp = tu->ratio_fp;
664 tu->boundary_moderation_en = false;
665 tu->upper_boundary_count = 0;
666 tu->lower_boundary_count = 0;
667 tu->i_upper_boundary_count = 0;
668 tu->i_lower_boundary_count = 0;
669 tu->valid_lower_boundary_link = 0;
670 tu->even_distribution_BF = 0;
671 tu->even_distribution_legacy = 0;
672 tu->even_distribution = 0;
673 tu->delay_start_time_fp = 0;
675 tu->err_fp = drm_fixp_from_fraction(1000, 1);
679 tu->ratio = drm_fixp2int(tu->ratio_fp);
680 temp1_fp = drm_fixp_from_fraction(tu->nlanes, 1);
681 div64_u64_rem(tu->lwidth_fp, temp1_fp, &temp2_fp);
683 !tu->ratio && tu->dsc_en == 0) {
684 tu->ratio_fp = drm_fixp_mul(tu->ratio_fp, RATIO_SCALE_fp);
685 tu->ratio = drm_fixp2int(tu->ratio_fp);
687 tu->ratio_fp = drm_fixp_from_fraction(1, 1);
696 compare_result_1 = _tu_param_compare(tu->ratio_fp, const_p49_fp);
697 if (!compare_result_1 || compare_result_1 == 1)
698 compare_result_1 = 1;
700 compare_result_1 = 0;
702 compare_result_2 = _tu_param_compare(tu->ratio_fp, const_p56_fp);
703 if (!compare_result_2 || compare_result_2 == 2)
704 compare_result_2 = 1;
706 compare_result_2 = 0;
708 if (tu->dsc_en && compare_result_1 && compare_result_2) {
710 drm_dbg_dp(ctrl->drm_dev,
711 "increase HBLANK_MARGIN to %d\n", HBLANK_MARGIN);
715 for (tu->tu_size = 32; tu->tu_size <= 64; tu->tu_size++) {
716 temp1_fp = drm_fixp_from_fraction(tu->tu_size, 1);
717 temp2_fp = drm_fixp_mul(tu->ratio_fp, temp1_fp);
718 temp = drm_fixp2int_ceil(temp2_fp);
719 temp1_fp = drm_fixp_from_fraction(temp, 1);
720 tu->n_err_fp = temp1_fp - temp2_fp;
722 if (tu->n_err_fp < tu->err_fp) {
723 tu->err_fp = tu->n_err_fp;
724 tu->tu_size_desired = tu->tu_size;
728 tu->tu_size_minus1 = tu->tu_size_desired - 1;
730 temp1_fp = drm_fixp_from_fraction(tu->tu_size_desired, 1);
731 temp2_fp = drm_fixp_mul(tu->ratio_fp, temp1_fp);
732 tu->valid_boundary_link = drm_fixp2int_ceil(temp2_fp);
734 temp1_fp = drm_fixp_from_fraction(tu->bpp, 8);
735 temp2_fp = tu->lwidth_fp;
736 temp2_fp = drm_fixp_mul(temp2_fp, temp1_fp);
738 temp1_fp = drm_fixp_from_fraction(tu->valid_boundary_link, 1);
739 temp2_fp = drm_fixp_div(temp2_fp, temp1_fp);
740 tu->n_tus = drm_fixp2int(temp2_fp);
741 if ((temp2_fp & 0xFFFFFFFF) > 0xFFFFF000)
744 tu->even_distribution_legacy = tu->n_tus % tu->nlanes == 0 ? 1 : 0;
746 drm_dbg_dp(ctrl->drm_dev,
747 "n_sym = %d, num_of_tus = %d\n",
748 tu->valid_boundary_link, tu->n_tus);
750 temp1_fp = drm_fixp_from_fraction(tu->tu_size_desired, 1);
751 temp2_fp = drm_fixp_mul(tu->original_ratio_fp, temp1_fp);
752 temp1_fp = drm_fixp_from_fraction(tu->valid_boundary_link, 1);
753 temp2_fp = temp1_fp - temp2_fp;
754 temp1_fp = drm_fixp_from_fraction(tu->n_tus + 1, 1);
755 temp2_fp = drm_fixp_mul(temp1_fp, temp2_fp);
757 temp = drm_fixp2int(temp2_fp);
758 if (temp && temp2_fp)
759 tu->extra_bytes = drm_fixp2int_ceil(temp2_fp);
763 temp1_fp = drm_fixp_from_fraction(tu->extra_bytes, 1);
764 temp2_fp = drm_fixp_from_fraction(8, tu->bpp);
765 temp1_fp = drm_fixp_mul(temp1_fp, temp2_fp);
767 if (temp && temp1_fp)
768 tu->extra_pclk_cycles = drm_fixp2int_ceil(temp1_fp);
770 tu->extra_pclk_cycles = drm_fixp2int(temp1_fp);
772 temp1_fp = drm_fixp_div(tu->lclk_fp, tu->pclk_fp);
773 temp2_fp = drm_fixp_from_fraction(tu->extra_pclk_cycles, 1);
774 temp1_fp = drm_fixp_mul(temp2_fp, temp1_fp);
777 tu->extra_pclk_cycles_in_link_clk = drm_fixp2int_ceil(temp1_fp);
779 tu->extra_pclk_cycles_in_link_clk = drm_fixp2int(temp1_fp);
781 tu->filler_size = tu->tu_size_desired - tu->valid_boundary_link;
783 temp1_fp = drm_fixp_from_fraction(tu->tu_size_desired, 1);
784 tu->ratio_by_tu_fp = drm_fixp_mul(tu->ratio_fp, temp1_fp);
786 tu->delay_start_link = tu->extra_pclk_cycles_in_link_clk +
787 tu->filler_size + tu->extra_buffer_margin;
789 tu->resulting_valid_fp =
790 drm_fixp_from_fraction(tu->valid_boundary_link, 1);
792 temp1_fp = drm_fixp_from_fraction(tu->tu_size_desired, 1);
793 temp2_fp = drm_fixp_div(tu->resulting_valid_fp, temp1_fp);
794 tu->TU_ratio_err_fp = temp2_fp - tu->original_ratio_fp;
796 temp1_fp = drm_fixp_from_fraction(HBLANK_MARGIN, 1);
797 temp1_fp = tu->hbp_relative_to_pclk_fp - temp1_fp;
798 tu->hbp_time_fp = drm_fixp_div(temp1_fp, tu->pclk_fp);
800 temp1_fp = drm_fixp_from_fraction(tu->delay_start_link, 1);
801 tu->delay_start_time_fp = drm_fixp_div(temp1_fp, tu->lclk_fp);
803 compare_result_1 = _tu_param_compare(tu->hbp_time_fp,
804 tu->delay_start_time_fp);
805 if (compare_result_1 == 2) /* if (hbp_time_fp < delay_start_time_fp) */
806 tu->min_hblank_violated = 1;
808 tu->hactive_time_fp = drm_fixp_div(tu->lwidth_fp, tu->pclk_fp);
810 compare_result_2 = _tu_param_compare(tu->hactive_time_fp,
811 tu->delay_start_time_fp);
812 if (compare_result_2 == 2)
813 tu->min_hblank_violated = 1;
815 tu->delay_start_time_fp = 0;
819 tu->delay_start_link_extra_pixclk = EXTRA_PIXCLK_CYCLE_DELAY;
820 tu->diff_abs_fp = tu->resulting_valid_fp - tu->ratio_by_tu_fp;
822 temp = drm_fixp2int(tu->diff_abs_fp);
823 if (!temp && tu->diff_abs_fp <= 0xffff)
826 /* if(diff_abs < 0) diff_abs *= -1 */
827 if (tu->diff_abs_fp < 0)
828 tu->diff_abs_fp = drm_fixp_mul(tu->diff_abs_fp, -1);
830 tu->boundary_mod_lower_err = 0;
831 if ((tu->diff_abs_fp != 0 &&
832 ((tu->diff_abs_fp > BRUTE_FORCE_THRESHOLD_fp) ||
833 (tu->even_distribution_legacy == 0) ||
834 (DP_BRUTE_FORCE == 1))) ||
835 (tu->min_hblank_violated == 1)) {
837 tu->err_fp = drm_fixp_from_fraction(1000, 1);
839 temp1_fp = drm_fixp_div(tu->lclk_fp, tu->pclk_fp);
840 temp2_fp = drm_fixp_from_fraction(
841 tu->delay_start_link_extra_pixclk, 1);
842 temp1_fp = drm_fixp_mul(temp2_fp, temp1_fp);
845 tu->extra_buffer_margin =
846 drm_fixp2int_ceil(temp1_fp);
848 tu->extra_buffer_margin = 0;
850 temp1_fp = drm_fixp_from_fraction(tu->bpp, 8);
851 temp1_fp = drm_fixp_mul(tu->lwidth_fp, temp1_fp);
854 tu->n_symbols = drm_fixp2int_ceil(temp1_fp);
858 for (tu->tu_size = 32; tu->tu_size <= 64; tu->tu_size++) {
859 for (tu->i_upper_boundary_count = 1;
860 tu->i_upper_boundary_count <= 15;
861 tu->i_upper_boundary_count++) {
862 for (tu->i_lower_boundary_count = 1;
863 tu->i_lower_boundary_count <= 15;
864 tu->i_lower_boundary_count++) {
865 _tu_valid_boundary_calc(tu);
869 tu->delay_start_link_extra_pixclk--;
870 } while (tu->boundary_moderation_en != true &&
871 tu->boundary_mod_lower_err == 1 &&
872 tu->delay_start_link_extra_pixclk != 0);
874 if (tu->boundary_moderation_en == true) {
875 temp1_fp = drm_fixp_from_fraction(
876 (tu->upper_boundary_count *
877 tu->valid_boundary_link +
878 tu->lower_boundary_count *
879 (tu->valid_boundary_link - 1)), 1);
880 temp2_fp = drm_fixp_from_fraction(
881 (tu->upper_boundary_count +
882 tu->lower_boundary_count), 1);
883 tu->resulting_valid_fp =
884 drm_fixp_div(temp1_fp, temp2_fp);
886 temp1_fp = drm_fixp_from_fraction(
887 tu->tu_size_desired, 1);
889 drm_fixp_mul(tu->original_ratio_fp, temp1_fp);
891 tu->valid_lower_boundary_link =
892 tu->valid_boundary_link - 1;
894 temp1_fp = drm_fixp_from_fraction(tu->bpp, 8);
895 temp1_fp = drm_fixp_mul(tu->lwidth_fp, temp1_fp);
896 temp2_fp = drm_fixp_div(temp1_fp,
897 tu->resulting_valid_fp);
898 tu->n_tus = drm_fixp2int(temp2_fp);
900 tu->tu_size_minus1 = tu->tu_size_desired - 1;
901 tu->even_distribution_BF = 1;
904 drm_fixp_from_fraction(tu->tu_size_desired, 1);
906 drm_fixp_div(tu->resulting_valid_fp, temp1_fp);
907 tu->TU_ratio_err_fp = temp2_fp - tu->original_ratio_fp;
911 temp2_fp = drm_fixp_mul(LCLK_FAST_SKEW_fp, tu->lwidth_fp);
914 temp = drm_fixp2int_ceil(temp2_fp);
918 temp1_fp = drm_fixp_from_fraction(tu->nlanes, 1);
919 temp2_fp = drm_fixp_mul(tu->original_ratio_fp, temp1_fp);
920 temp1_fp = drm_fixp_from_fraction(tu->bpp, 8);
921 temp2_fp = drm_fixp_div(temp1_fp, temp2_fp);
922 temp1_fp = drm_fixp_from_fraction(temp, 1);
923 temp2_fp = drm_fixp_mul(temp1_fp, temp2_fp);
924 temp = drm_fixp2int(temp2_fp);
927 tu->delay_start_link += (int)temp;
929 temp1_fp = drm_fixp_from_fraction(tu->delay_start_link, 1);
930 tu->delay_start_time_fp = drm_fixp_div(temp1_fp, tu->lclk_fp);
933 tu_table->valid_boundary_link = tu->valid_boundary_link;
934 tu_table->delay_start_link = tu->delay_start_link;
935 tu_table->boundary_moderation_en = tu->boundary_moderation_en;
936 tu_table->valid_lower_boundary_link = tu->valid_lower_boundary_link;
937 tu_table->upper_boundary_count = tu->upper_boundary_count;
938 tu_table->lower_boundary_count = tu->lower_boundary_count;
939 tu_table->tu_size_minus1 = tu->tu_size_minus1;
941 drm_dbg_dp(ctrl->drm_dev, "TU: valid_boundary_link: %d\n",
942 tu_table->valid_boundary_link);
943 drm_dbg_dp(ctrl->drm_dev, "TU: delay_start_link: %d\n",
944 tu_table->delay_start_link);
945 drm_dbg_dp(ctrl->drm_dev, "TU: boundary_moderation_en: %d\n",
946 tu_table->boundary_moderation_en);
947 drm_dbg_dp(ctrl->drm_dev, "TU: valid_lower_boundary_link: %d\n",
948 tu_table->valid_lower_boundary_link);
949 drm_dbg_dp(ctrl->drm_dev, "TU: upper_boundary_count: %d\n",
950 tu_table->upper_boundary_count);
951 drm_dbg_dp(ctrl->drm_dev, "TU: lower_boundary_count: %d\n",
952 tu_table->lower_boundary_count);
953 drm_dbg_dp(ctrl->drm_dev, "TU: tu_size_minus1: %d\n",
954 tu_table->tu_size_minus1);
959 static void dp_ctrl_calc_tu_parameters(struct dp_ctrl_private *ctrl,
960 struct dp_vc_tu_mapping_table *tu_table)
962 struct dp_tu_calc_input in;
963 struct drm_display_mode *drm_mode;
965 drm_mode = &ctrl->panel->dp_mode.drm_mode;
967 in.lclk = ctrl->link->link_params.rate / 1000;
968 in.pclk_khz = drm_mode->clock;
969 in.hactive = drm_mode->hdisplay;
970 in.hporch = drm_mode->htotal - drm_mode->hdisplay;
971 in.nlanes = ctrl->link->link_params.num_lanes;
972 in.bpp = ctrl->panel->dp_mode.bpp;
973 in.pixel_enc = ctrl->panel->dp_mode.out_fmt_is_yuv_420 ? 420 : 444;
977 in.num_of_dsc_slices = 0;
978 in.compress_ratio = 100;
980 _dp_ctrl_calc_tu(ctrl, &in, tu_table);
983 static void dp_ctrl_setup_tr_unit(struct dp_ctrl_private *ctrl)
986 u32 valid_boundary = 0x0;
987 u32 valid_boundary2 = 0x0;
988 struct dp_vc_tu_mapping_table tu_calc_table;
990 dp_ctrl_calc_tu_parameters(ctrl, &tu_calc_table);
992 dp_tu |= tu_calc_table.tu_size_minus1;
993 valid_boundary |= tu_calc_table.valid_boundary_link;
994 valid_boundary |= (tu_calc_table.delay_start_link << 16);
996 valid_boundary2 |= (tu_calc_table.valid_lower_boundary_link << 1);
997 valid_boundary2 |= (tu_calc_table.upper_boundary_count << 16);
998 valid_boundary2 |= (tu_calc_table.lower_boundary_count << 20);
1000 if (tu_calc_table.boundary_moderation_en)
1001 valid_boundary2 |= BIT(0);
1003 pr_debug("dp_tu=0x%x, valid_boundary=0x%x, valid_boundary2=0x%x\n",
1004 dp_tu, valid_boundary, valid_boundary2);
1006 dp_catalog_ctrl_update_transfer_unit(ctrl->catalog,
1007 dp_tu, valid_boundary, valid_boundary2);
1010 static int dp_ctrl_wait4video_ready(struct dp_ctrl_private *ctrl)
1014 if (!wait_for_completion_timeout(&ctrl->video_comp,
1015 WAIT_FOR_VIDEO_READY_TIMEOUT_JIFFIES)) {
1016 DRM_ERROR("wait4video timedout\n");
1022 static int dp_ctrl_set_vx_px(struct dp_ctrl_private *ctrl,
1023 u8 v_level, u8 p_level)
1025 union phy_configure_opts *phy_opts = &ctrl->phy_opts;
1027 /* TODO: Update for all lanes instead of just first one */
1028 phy_opts->dp.voltage[0] = v_level;
1029 phy_opts->dp.pre[0] = p_level;
1030 phy_opts->dp.set_voltages = 1;
1031 phy_configure(ctrl->phy, phy_opts);
1032 phy_opts->dp.set_voltages = 0;
1037 static int dp_ctrl_update_vx_px(struct dp_ctrl_private *ctrl)
1039 struct dp_link *link = ctrl->link;
1040 int ret = 0, lane, lane_cnt;
1042 u32 max_level_reached = 0;
1043 u32 voltage_swing_level = link->phy_params.v_level;
1044 u32 pre_emphasis_level = link->phy_params.p_level;
1046 drm_dbg_dp(ctrl->drm_dev,
1047 "voltage level: %d emphasis level: %d\n",
1048 voltage_swing_level, pre_emphasis_level);
1049 ret = dp_ctrl_set_vx_px(ctrl,
1050 voltage_swing_level, pre_emphasis_level);
1055 if (voltage_swing_level >= DP_TRAIN_LEVEL_MAX) {
1056 drm_dbg_dp(ctrl->drm_dev,
1057 "max. voltage swing level reached %d\n",
1058 voltage_swing_level);
1059 max_level_reached |= DP_TRAIN_MAX_SWING_REACHED;
1062 if (pre_emphasis_level >= DP_TRAIN_LEVEL_MAX) {
1063 drm_dbg_dp(ctrl->drm_dev,
1064 "max. pre-emphasis level reached %d\n",
1065 pre_emphasis_level);
1066 max_level_reached |= DP_TRAIN_MAX_PRE_EMPHASIS_REACHED;
1069 pre_emphasis_level <<= DP_TRAIN_PRE_EMPHASIS_SHIFT;
1071 lane_cnt = ctrl->link->link_params.num_lanes;
1072 for (lane = 0; lane < lane_cnt; lane++)
1073 buf[lane] = voltage_swing_level | pre_emphasis_level
1074 | max_level_reached;
1076 drm_dbg_dp(ctrl->drm_dev, "sink: p|v=0x%x\n",
1077 voltage_swing_level | pre_emphasis_level);
1078 ret = drm_dp_dpcd_write(ctrl->aux, DP_TRAINING_LANE0_SET,
1080 if (ret == lane_cnt)
1086 static bool dp_ctrl_train_pattern_set(struct dp_ctrl_private *ctrl,
1092 drm_dbg_dp(ctrl->drm_dev, "sink: pattern=%x\n", pattern);
1096 if (pattern && pattern != DP_TRAINING_PATTERN_4)
1097 buf |= DP_LINK_SCRAMBLING_DISABLE;
1099 ret = drm_dp_dpcd_writeb(ctrl->aux, DP_TRAINING_PATTERN_SET, buf);
1103 static int dp_ctrl_read_link_status(struct dp_ctrl_private *ctrl,
1108 len = drm_dp_dpcd_read_link_status(ctrl->aux, link_status);
1109 if (len != DP_LINK_STATUS_SIZE) {
1110 DRM_ERROR("DP link status read failed, err: %d\n", len);
1117 static int dp_ctrl_link_train_1(struct dp_ctrl_private *ctrl,
1120 int tries, old_v_level, ret = 0;
1121 u8 link_status[DP_LINK_STATUS_SIZE];
1122 int const maximum_retries = 4;
1124 dp_catalog_ctrl_state_ctrl(ctrl->catalog, 0);
1126 *training_step = DP_TRAINING_1;
1128 ret = dp_catalog_ctrl_set_pattern_state_bit(ctrl->catalog, 1);
1131 dp_ctrl_train_pattern_set(ctrl, DP_TRAINING_PATTERN_1 |
1132 DP_LINK_SCRAMBLING_DISABLE);
1134 ret = dp_ctrl_update_vx_px(ctrl);
1139 old_v_level = ctrl->link->phy_params.v_level;
1140 for (tries = 0; tries < maximum_retries; tries++) {
1141 drm_dp_link_train_clock_recovery_delay(ctrl->aux, ctrl->panel->dpcd);
1143 ret = dp_ctrl_read_link_status(ctrl, link_status);
1147 if (drm_dp_clock_recovery_ok(link_status,
1148 ctrl->link->link_params.num_lanes)) {
1152 if (ctrl->link->phy_params.v_level >=
1153 DP_TRAIN_LEVEL_MAX) {
1154 DRM_ERROR_RATELIMITED("max v_level reached\n");
1158 if (old_v_level != ctrl->link->phy_params.v_level) {
1160 old_v_level = ctrl->link->phy_params.v_level;
1163 dp_link_adjust_levels(ctrl->link, link_status);
1164 ret = dp_ctrl_update_vx_px(ctrl);
1169 DRM_ERROR("max tries reached\n");
1173 static int dp_ctrl_link_rate_down_shift(struct dp_ctrl_private *ctrl)
1177 switch (ctrl->link->link_params.rate) {
1179 ctrl->link->link_params.rate = 540000;
1182 ctrl->link->link_params.rate = 270000;
1185 ctrl->link->link_params.rate = 162000;
1194 drm_dbg_dp(ctrl->drm_dev, "new rate=0x%x\n",
1195 ctrl->link->link_params.rate);
1201 static int dp_ctrl_link_lane_down_shift(struct dp_ctrl_private *ctrl)
1204 if (ctrl->link->link_params.num_lanes == 1)
1207 ctrl->link->link_params.num_lanes /= 2;
1208 ctrl->link->link_params.rate = ctrl->panel->link_info.rate;
1210 ctrl->link->phy_params.p_level = 0;
1211 ctrl->link->phy_params.v_level = 0;
1216 static void dp_ctrl_clear_training_pattern(struct dp_ctrl_private *ctrl)
1218 dp_ctrl_train_pattern_set(ctrl, DP_TRAINING_PATTERN_DISABLE);
1219 drm_dp_link_train_channel_eq_delay(ctrl->aux, ctrl->panel->dpcd);
1222 static int dp_ctrl_link_train_2(struct dp_ctrl_private *ctrl,
1225 int tries = 0, ret = 0;
1228 int const maximum_retries = 5;
1229 u8 link_status[DP_LINK_STATUS_SIZE];
1231 dp_catalog_ctrl_state_ctrl(ctrl->catalog, 0);
1233 *training_step = DP_TRAINING_2;
1235 if (drm_dp_tps4_supported(ctrl->panel->dpcd)) {
1236 pattern = DP_TRAINING_PATTERN_4;
1238 } else if (drm_dp_tps3_supported(ctrl->panel->dpcd)) {
1239 pattern = DP_TRAINING_PATTERN_3;
1242 pattern = DP_TRAINING_PATTERN_2;
1246 ret = dp_catalog_ctrl_set_pattern_state_bit(ctrl->catalog, state_ctrl_bit);
1250 dp_ctrl_train_pattern_set(ctrl, pattern);
1252 for (tries = 0; tries <= maximum_retries; tries++) {
1253 drm_dp_link_train_channel_eq_delay(ctrl->aux, ctrl->panel->dpcd);
1255 ret = dp_ctrl_read_link_status(ctrl, link_status);
1259 if (drm_dp_channel_eq_ok(link_status,
1260 ctrl->link->link_params.num_lanes)) {
1264 dp_link_adjust_levels(ctrl->link, link_status);
1265 ret = dp_ctrl_update_vx_px(ctrl);
1274 static int dp_ctrl_link_train(struct dp_ctrl_private *ctrl,
1278 const u8 *dpcd = ctrl->panel->dpcd;
1279 u8 encoding[] = { 0, DP_SET_ANSI_8B10B };
1281 struct dp_link_info link_info = {0};
1283 dp_ctrl_config_ctrl(ctrl);
1285 link_info.num_lanes = ctrl->link->link_params.num_lanes;
1286 link_info.rate = ctrl->link->link_params.rate;
1287 link_info.capabilities = DP_LINK_CAP_ENHANCED_FRAMING;
1289 dp_aux_link_configure(ctrl->aux, &link_info);
1291 if (drm_dp_max_downspread(dpcd))
1292 encoding[0] |= DP_SPREAD_AMP_0_5;
1294 /* config DOWNSPREAD_CTRL and MAIN_LINK_CHANNEL_CODING_SET */
1295 drm_dp_dpcd_write(ctrl->aux, DP_DOWNSPREAD_CTRL, encoding, 2);
1297 if (drm_dp_alternate_scrambler_reset_cap(dpcd)) {
1298 assr = DP_ALTERNATE_SCRAMBLER_RESET_ENABLE;
1299 drm_dp_dpcd_write(ctrl->aux, DP_EDP_CONFIGURATION_SET,
1303 ret = dp_ctrl_link_train_1(ctrl, training_step);
1305 DRM_ERROR("link training #1 failed. ret=%d\n", ret);
1309 /* print success info as this is a result of user initiated action */
1310 drm_dbg_dp(ctrl->drm_dev, "link training #1 successful\n");
1312 ret = dp_ctrl_link_train_2(ctrl, training_step);
1314 DRM_ERROR("link training #2 failed. ret=%d\n", ret);
1318 /* print success info as this is a result of user initiated action */
1319 drm_dbg_dp(ctrl->drm_dev, "link training #2 successful\n");
1322 dp_catalog_ctrl_state_ctrl(ctrl->catalog, 0);
1327 static int dp_ctrl_setup_main_link(struct dp_ctrl_private *ctrl,
1332 dp_catalog_ctrl_mainlink_ctrl(ctrl->catalog, true);
1334 if (ctrl->link->sink_request & DP_TEST_LINK_PHY_TEST_PATTERN)
1338 * As part of previous calls, DP controller state might have
1339 * transitioned to PUSH_IDLE. In order to start transmitting
1340 * a link training pattern, we have to first do soft reset.
1343 ret = dp_ctrl_link_train(ctrl, training_step);
1348 int dp_ctrl_core_clk_enable(struct dp_ctrl *dp_ctrl)
1350 struct dp_ctrl_private *ctrl;
1353 ctrl = container_of(dp_ctrl, struct dp_ctrl_private, dp_ctrl);
1355 if (ctrl->core_clks_on) {
1356 drm_dbg_dp(ctrl->drm_dev, "core clks already enabled\n");
1360 ret = clk_bulk_prepare_enable(ctrl->num_core_clks, ctrl->core_clks);
1364 ctrl->core_clks_on = true;
1366 drm_dbg_dp(ctrl->drm_dev, "enable core clocks \n");
1367 drm_dbg_dp(ctrl->drm_dev, "stream_clks:%s link_clks:%s core_clks:%s\n",
1368 ctrl->stream_clks_on ? "on" : "off",
1369 ctrl->link_clks_on ? "on" : "off",
1370 ctrl->core_clks_on ? "on" : "off");
1375 void dp_ctrl_core_clk_disable(struct dp_ctrl *dp_ctrl)
1377 struct dp_ctrl_private *ctrl;
1379 ctrl = container_of(dp_ctrl, struct dp_ctrl_private, dp_ctrl);
1381 clk_bulk_disable_unprepare(ctrl->num_core_clks, ctrl->core_clks);
1383 ctrl->core_clks_on = false;
1385 drm_dbg_dp(ctrl->drm_dev, "disable core clocks \n");
1386 drm_dbg_dp(ctrl->drm_dev, "stream_clks:%s link_clks:%s core_clks:%s\n",
1387 ctrl->stream_clks_on ? "on" : "off",
1388 ctrl->link_clks_on ? "on" : "off",
1389 ctrl->core_clks_on ? "on" : "off");
1392 static int dp_ctrl_link_clk_enable(struct dp_ctrl *dp_ctrl)
1394 struct dp_ctrl_private *ctrl;
1397 ctrl = container_of(dp_ctrl, struct dp_ctrl_private, dp_ctrl);
1399 if (ctrl->link_clks_on) {
1400 drm_dbg_dp(ctrl->drm_dev, "links clks already enabled\n");
1404 if (!ctrl->core_clks_on) {
1405 drm_dbg_dp(ctrl->drm_dev, "Enable core clks before link clks\n");
1407 dp_ctrl_core_clk_enable(dp_ctrl);
1410 ret = clk_bulk_prepare_enable(ctrl->num_link_clks, ctrl->link_clks);
1414 ctrl->link_clks_on = true;
1416 drm_dbg_dp(ctrl->drm_dev, "enable link clocks\n");
1417 drm_dbg_dp(ctrl->drm_dev, "stream_clks:%s link_clks:%s core_clks:%s\n",
1418 ctrl->stream_clks_on ? "on" : "off",
1419 ctrl->link_clks_on ? "on" : "off",
1420 ctrl->core_clks_on ? "on" : "off");
1425 static void dp_ctrl_link_clk_disable(struct dp_ctrl *dp_ctrl)
1427 struct dp_ctrl_private *ctrl;
1429 ctrl = container_of(dp_ctrl, struct dp_ctrl_private, dp_ctrl);
1431 clk_bulk_disable_unprepare(ctrl->num_link_clks, ctrl->link_clks);
1433 ctrl->link_clks_on = false;
1435 drm_dbg_dp(ctrl->drm_dev, "disabled link clocks\n");
1436 drm_dbg_dp(ctrl->drm_dev, "stream_clks:%s link_clks:%s core_clks:%s\n",
1437 ctrl->stream_clks_on ? "on" : "off",
1438 ctrl->link_clks_on ? "on" : "off",
1439 ctrl->core_clks_on ? "on" : "off");
1442 static int dp_ctrl_enable_mainlink_clocks(struct dp_ctrl_private *ctrl)
1445 struct phy *phy = ctrl->phy;
1446 const u8 *dpcd = ctrl->panel->dpcd;
1448 ctrl->phy_opts.dp.lanes = ctrl->link->link_params.num_lanes;
1449 ctrl->phy_opts.dp.link_rate = ctrl->link->link_params.rate / 100;
1450 ctrl->phy_opts.dp.ssc = drm_dp_max_downspread(dpcd);
1452 phy_configure(phy, &ctrl->phy_opts);
1455 dev_pm_opp_set_rate(ctrl->dev, ctrl->link->link_params.rate * 1000);
1456 ret = dp_ctrl_link_clk_enable(&ctrl->dp_ctrl);
1458 DRM_ERROR("Unable to start link clocks. ret=%d\n", ret);
1460 drm_dbg_dp(ctrl->drm_dev, "link rate=%d\n", ctrl->link->link_params.rate);
1465 void dp_ctrl_reset_irq_ctrl(struct dp_ctrl *dp_ctrl, bool enable)
1467 struct dp_ctrl_private *ctrl;
1469 ctrl = container_of(dp_ctrl, struct dp_ctrl_private, dp_ctrl);
1471 dp_catalog_ctrl_reset(ctrl->catalog);
1474 * all dp controller programmable registers will not
1475 * be reset to default value after DP_SW_RESET
1476 * therefore interrupt mask bits have to be updated
1477 * to enable/disable interrupts
1479 dp_catalog_ctrl_enable_irq(ctrl->catalog, enable);
1482 void dp_ctrl_config_psr(struct dp_ctrl *dp_ctrl)
1485 struct dp_ctrl_private *ctrl = container_of(dp_ctrl,
1486 struct dp_ctrl_private, dp_ctrl);
1488 if (!ctrl->panel->psr_cap.version)
1491 dp_catalog_ctrl_config_psr(ctrl->catalog);
1493 cfg = DP_PSR_ENABLE;
1494 drm_dp_dpcd_write(ctrl->aux, DP_PSR_EN_CFG, &cfg, 1);
1497 void dp_ctrl_set_psr(struct dp_ctrl *dp_ctrl, bool enter)
1499 struct dp_ctrl_private *ctrl = container_of(dp_ctrl,
1500 struct dp_ctrl_private, dp_ctrl);
1502 if (!ctrl->panel->psr_cap.version)
1506 * When entering PSR,
1507 * 1. Send PSR enter SDP and wait for the PSR_UPDATE_INT
1509 * 3. Disable the mainlink
1512 * 1. Enable the mainlink
1513 * 2. Send the PSR exit SDP
1516 reinit_completion(&ctrl->psr_op_comp);
1517 dp_catalog_ctrl_set_psr(ctrl->catalog, true);
1519 if (!wait_for_completion_timeout(&ctrl->psr_op_comp,
1520 PSR_OPERATION_COMPLETION_TIMEOUT_JIFFIES)) {
1521 DRM_ERROR("PSR_ENTRY timedout\n");
1522 dp_catalog_ctrl_set_psr(ctrl->catalog, false);
1526 dp_ctrl_push_idle(dp_ctrl);
1527 dp_catalog_ctrl_state_ctrl(ctrl->catalog, 0);
1529 dp_catalog_ctrl_psr_mainlink_enable(ctrl->catalog, false);
1531 dp_catalog_ctrl_psr_mainlink_enable(ctrl->catalog, true);
1533 dp_catalog_ctrl_set_psr(ctrl->catalog, false);
1534 dp_catalog_ctrl_state_ctrl(ctrl->catalog, DP_STATE_CTRL_SEND_VIDEO);
1535 dp_ctrl_wait4video_ready(ctrl);
1536 dp_catalog_ctrl_state_ctrl(ctrl->catalog, 0);
1540 void dp_ctrl_phy_init(struct dp_ctrl *dp_ctrl)
1542 struct dp_ctrl_private *ctrl;
1545 ctrl = container_of(dp_ctrl, struct dp_ctrl_private, dp_ctrl);
1548 dp_catalog_ctrl_phy_reset(ctrl->catalog);
1551 drm_dbg_dp(ctrl->drm_dev, "phy=%p init=%d power_on=%d\n",
1552 phy, phy->init_count, phy->power_count);
1555 void dp_ctrl_phy_exit(struct dp_ctrl *dp_ctrl)
1557 struct dp_ctrl_private *ctrl;
1560 ctrl = container_of(dp_ctrl, struct dp_ctrl_private, dp_ctrl);
1563 dp_catalog_ctrl_phy_reset(ctrl->catalog);
1565 drm_dbg_dp(ctrl->drm_dev, "phy=%p init=%d power_on=%d\n",
1566 phy, phy->init_count, phy->power_count);
1569 static int dp_ctrl_reinitialize_mainlink(struct dp_ctrl_private *ctrl)
1571 struct phy *phy = ctrl->phy;
1574 dp_catalog_ctrl_mainlink_ctrl(ctrl->catalog, false);
1575 ctrl->phy_opts.dp.lanes = ctrl->link->link_params.num_lanes;
1576 phy_configure(phy, &ctrl->phy_opts);
1578 * Disable and re-enable the mainlink clock since the
1579 * link clock might have been adjusted as part of the
1582 dev_pm_opp_set_rate(ctrl->dev, 0);
1584 dp_ctrl_link_clk_disable(&ctrl->dp_ctrl);
1587 /* hw recommended delay before re-enabling clocks */
1590 ret = dp_ctrl_enable_mainlink_clocks(ctrl);
1592 DRM_ERROR("Failed to enable mainlink clks. ret=%d\n", ret);
1599 static int dp_ctrl_deinitialize_mainlink(struct dp_ctrl_private *ctrl)
1605 dp_catalog_ctrl_mainlink_ctrl(ctrl->catalog, false);
1607 dp_catalog_ctrl_reset(ctrl->catalog);
1609 dev_pm_opp_set_rate(ctrl->dev, 0);
1610 dp_ctrl_link_clk_disable(&ctrl->dp_ctrl);
1614 /* aux channel down, reinit phy */
1618 drm_dbg_dp(ctrl->drm_dev, "phy=%p init=%d power_on=%d\n",
1619 phy, phy->init_count, phy->power_count);
1623 static int dp_ctrl_link_maintenance(struct dp_ctrl_private *ctrl)
1626 int training_step = DP_TRAINING_NONE;
1628 dp_ctrl_push_idle(&ctrl->dp_ctrl);
1630 ctrl->link->phy_params.p_level = 0;
1631 ctrl->link->phy_params.v_level = 0;
1633 ret = dp_ctrl_setup_main_link(ctrl, &training_step);
1637 dp_ctrl_clear_training_pattern(ctrl);
1639 dp_catalog_ctrl_state_ctrl(ctrl->catalog, DP_STATE_CTRL_SEND_VIDEO);
1641 ret = dp_ctrl_wait4video_ready(ctrl);
1646 static bool dp_ctrl_send_phy_test_pattern(struct dp_ctrl_private *ctrl)
1648 bool success = false;
1649 u32 pattern_sent = 0x0;
1650 u32 pattern_requested = ctrl->link->phy_params.phy_test_pattern_sel;
1652 drm_dbg_dp(ctrl->drm_dev, "request: 0x%x\n", pattern_requested);
1654 if (dp_ctrl_set_vx_px(ctrl,
1655 ctrl->link->phy_params.v_level,
1656 ctrl->link->phy_params.p_level)) {
1657 DRM_ERROR("Failed to set v/p levels\n");
1660 dp_catalog_ctrl_send_phy_pattern(ctrl->catalog, pattern_requested);
1661 dp_ctrl_update_vx_px(ctrl);
1662 dp_link_send_test_response(ctrl->link);
1664 pattern_sent = dp_catalog_ctrl_read_phy_pattern(ctrl->catalog);
1666 switch (pattern_sent) {
1667 case MR_LINK_TRAINING1:
1668 success = (pattern_requested ==
1669 DP_PHY_TEST_PATTERN_D10_2);
1671 case MR_LINK_SYMBOL_ERM:
1672 success = ((pattern_requested ==
1673 DP_PHY_TEST_PATTERN_ERROR_COUNT) ||
1674 (pattern_requested ==
1675 DP_PHY_TEST_PATTERN_CP2520));
1678 success = (pattern_requested ==
1679 DP_PHY_TEST_PATTERN_PRBS7);
1681 case MR_LINK_CUSTOM80:
1682 success = (pattern_requested ==
1683 DP_PHY_TEST_PATTERN_80BIT_CUSTOM);
1685 case MR_LINK_TRAINING4:
1686 success = (pattern_requested ==
1687 DP_PHY_TEST_PATTERN_SEL_MASK);
1693 drm_dbg_dp(ctrl->drm_dev, "%s: test->0x%x\n",
1694 success ? "success" : "failed", pattern_requested);
1698 static int dp_ctrl_process_phy_test_request(struct dp_ctrl_private *ctrl)
1701 unsigned long pixel_rate;
1703 if (!ctrl->link->phy_params.phy_test_pattern_sel) {
1704 drm_dbg_dp(ctrl->drm_dev,
1705 "no test pattern selected by sink\n");
1710 * The global reset will need DP link related clocks to be
1711 * running. Add the global reset just before disabling the
1712 * link clocks and core clocks.
1714 dp_ctrl_off(&ctrl->dp_ctrl);
1716 ret = dp_ctrl_on_link(&ctrl->dp_ctrl);
1718 DRM_ERROR("failed to enable DP link controller\n");
1722 pixel_rate = ctrl->panel->dp_mode.drm_mode.clock;
1723 ret = clk_set_rate(ctrl->pixel_clk, pixel_rate * 1000);
1725 DRM_ERROR("Failed to set pixel clock rate. ret=%d\n", ret);
1729 if (ctrl->stream_clks_on) {
1730 drm_dbg_dp(ctrl->drm_dev, "pixel clks already enabled\n");
1732 ret = clk_prepare_enable(ctrl->pixel_clk);
1734 DRM_ERROR("Failed to start pixel clocks. ret=%d\n", ret);
1737 ctrl->stream_clks_on = true;
1740 dp_ctrl_send_phy_test_pattern(ctrl);
1745 void dp_ctrl_handle_sink_request(struct dp_ctrl *dp_ctrl)
1747 struct dp_ctrl_private *ctrl;
1748 u32 sink_request = 0x0;
1751 DRM_ERROR("invalid input\n");
1755 ctrl = container_of(dp_ctrl, struct dp_ctrl_private, dp_ctrl);
1756 sink_request = ctrl->link->sink_request;
1758 if (sink_request & DP_TEST_LINK_PHY_TEST_PATTERN) {
1759 drm_dbg_dp(ctrl->drm_dev, "PHY_TEST_PATTERN request\n");
1760 if (dp_ctrl_process_phy_test_request(ctrl)) {
1761 DRM_ERROR("process phy_test_req failed\n");
1766 if (sink_request & DP_LINK_STATUS_UPDATED) {
1767 if (dp_ctrl_link_maintenance(ctrl)) {
1768 DRM_ERROR("LM failed: TEST_LINK_TRAINING\n");
1773 if (sink_request & DP_TEST_LINK_TRAINING) {
1774 dp_link_send_test_response(ctrl->link);
1775 if (dp_ctrl_link_maintenance(ctrl)) {
1776 DRM_ERROR("LM failed: TEST_LINK_TRAINING\n");
1782 static bool dp_ctrl_clock_recovery_any_ok(
1783 const u8 link_status[DP_LINK_STATUS_SIZE],
1788 if (lane_count <= 1)
1792 * only interested in the lane number after reduced
1793 * lane_count = 4, then only interested in 2 lanes
1794 * lane_count = 2, then only interested in 1 lane
1796 reduced_cnt = lane_count >> 1;
1798 return drm_dp_clock_recovery_ok(link_status, reduced_cnt);
1801 static bool dp_ctrl_channel_eq_ok(struct dp_ctrl_private *ctrl)
1803 u8 link_status[DP_LINK_STATUS_SIZE];
1804 int num_lanes = ctrl->link->link_params.num_lanes;
1806 dp_ctrl_read_link_status(ctrl, link_status);
1808 return drm_dp_channel_eq_ok(link_status, num_lanes);
1811 int dp_ctrl_on_link(struct dp_ctrl *dp_ctrl)
1814 struct dp_ctrl_private *ctrl;
1816 int link_train_max_retries = 5;
1817 u32 const phy_cts_pixel_clk_khz = 148500;
1818 u8 link_status[DP_LINK_STATUS_SIZE];
1819 unsigned int training_step;
1820 unsigned long pixel_rate;
1825 ctrl = container_of(dp_ctrl, struct dp_ctrl_private, dp_ctrl);
1827 rate = ctrl->panel->link_info.rate;
1828 pixel_rate = ctrl->panel->dp_mode.drm_mode.clock;
1830 dp_ctrl_core_clk_enable(&ctrl->dp_ctrl);
1832 if (ctrl->link->sink_request & DP_TEST_LINK_PHY_TEST_PATTERN) {
1833 drm_dbg_dp(ctrl->drm_dev,
1834 "using phy test link parameters\n");
1836 pixel_rate = phy_cts_pixel_clk_khz;
1838 ctrl->link->link_params.rate = rate;
1839 ctrl->link->link_params.num_lanes =
1840 ctrl->panel->link_info.num_lanes;
1841 if (ctrl->panel->dp_mode.out_fmt_is_yuv_420)
1845 drm_dbg_dp(ctrl->drm_dev, "rate=%d, num_lanes=%d, pixel_rate=%lu\n",
1846 ctrl->link->link_params.rate, ctrl->link->link_params.num_lanes,
1849 rc = dp_ctrl_enable_mainlink_clocks(ctrl);
1853 while (--link_train_max_retries) {
1854 training_step = DP_TRAINING_NONE;
1855 rc = dp_ctrl_setup_main_link(ctrl, &training_step);
1857 /* training completed successfully */
1859 } else if (training_step == DP_TRAINING_1) {
1860 /* link train_1 failed */
1861 if (!dp_catalog_link_is_connected(ctrl->catalog))
1864 dp_ctrl_read_link_status(ctrl, link_status);
1866 rc = dp_ctrl_link_rate_down_shift(ctrl);
1867 if (rc < 0) { /* already in RBR = 1.6G */
1868 if (dp_ctrl_clock_recovery_any_ok(link_status,
1869 ctrl->link->link_params.num_lanes)) {
1871 * some lanes are ready,
1872 * reduce lane number
1874 rc = dp_ctrl_link_lane_down_shift(ctrl);
1875 if (rc < 0) { /* lane == 1 already */
1876 /* end with failure */
1880 /* end with failure */
1881 break; /* lane == 1 already */
1884 } else if (training_step == DP_TRAINING_2) {
1885 /* link train_2 failed */
1886 if (!dp_catalog_link_is_connected(ctrl->catalog))
1889 dp_ctrl_read_link_status(ctrl, link_status);
1891 if (!drm_dp_clock_recovery_ok(link_status,
1892 ctrl->link->link_params.num_lanes))
1893 rc = dp_ctrl_link_rate_down_shift(ctrl);
1895 rc = dp_ctrl_link_lane_down_shift(ctrl);
1898 /* end with failure */
1899 break; /* lane == 1 already */
1902 /* stop link training before start re training */
1903 dp_ctrl_clear_training_pattern(ctrl);
1906 rc = dp_ctrl_reinitialize_mainlink(ctrl);
1908 DRM_ERROR("Failed to reinitialize mainlink. rc=%d\n", rc);
1913 if (ctrl->link->sink_request & DP_TEST_LINK_PHY_TEST_PATTERN)
1916 if (rc == 0) { /* link train successfully */
1918 * do not stop train pattern here
1919 * stop link training at on_stream
1920 * to pass compliance test
1924 * link training failed
1925 * end txing train pattern here
1927 dp_ctrl_clear_training_pattern(ctrl);
1929 dp_ctrl_deinitialize_mainlink(ctrl);
1936 static int dp_ctrl_link_retrain(struct dp_ctrl_private *ctrl)
1938 int training_step = DP_TRAINING_NONE;
1940 return dp_ctrl_setup_main_link(ctrl, &training_step);
1943 int dp_ctrl_on_stream(struct dp_ctrl *dp_ctrl, bool force_link_train)
1946 bool mainlink_ready = false;
1947 struct dp_ctrl_private *ctrl;
1948 unsigned long pixel_rate;
1949 unsigned long pixel_rate_orig;
1954 ctrl = container_of(dp_ctrl, struct dp_ctrl_private, dp_ctrl);
1956 pixel_rate = pixel_rate_orig = ctrl->panel->dp_mode.drm_mode.clock;
1958 if (dp_ctrl->wide_bus_en || ctrl->panel->dp_mode.out_fmt_is_yuv_420)
1961 drm_dbg_dp(ctrl->drm_dev, "rate=%d, num_lanes=%d, pixel_rate=%lu\n",
1962 ctrl->link->link_params.rate,
1963 ctrl->link->link_params.num_lanes, pixel_rate);
1965 drm_dbg_dp(ctrl->drm_dev,
1966 "core_clk_on=%d link_clk_on=%d stream_clk_on=%d\n",
1967 ctrl->core_clks_on, ctrl->link_clks_on, ctrl->stream_clks_on);
1969 if (!ctrl->link_clks_on) { /* link clk is off */
1970 ret = dp_ctrl_enable_mainlink_clocks(ctrl);
1972 DRM_ERROR("Failed to start link clocks. ret=%d\n", ret);
1977 ret = clk_set_rate(ctrl->pixel_clk, pixel_rate * 1000);
1979 DRM_ERROR("Failed to set pixel clock rate. ret=%d\n", ret);
1983 if (ctrl->stream_clks_on) {
1984 drm_dbg_dp(ctrl->drm_dev, "pixel clks already enabled\n");
1986 ret = clk_prepare_enable(ctrl->pixel_clk);
1988 DRM_ERROR("Failed to start pixel clocks. ret=%d\n", ret);
1991 ctrl->stream_clks_on = true;
1994 if (force_link_train || !dp_ctrl_channel_eq_ok(ctrl))
1995 dp_ctrl_link_retrain(ctrl);
1997 /* stop txing train pattern to end link training */
1998 dp_ctrl_clear_training_pattern(ctrl);
2001 * Set up transfer unit values and set controller state to send
2004 reinit_completion(&ctrl->video_comp);
2006 dp_ctrl_configure_source_params(ctrl);
2008 dp_catalog_ctrl_config_msa(ctrl->catalog,
2009 ctrl->link->link_params.rate,
2011 ctrl->panel->dp_mode.out_fmt_is_yuv_420);
2013 dp_ctrl_setup_tr_unit(ctrl);
2015 dp_catalog_ctrl_state_ctrl(ctrl->catalog, DP_STATE_CTRL_SEND_VIDEO);
2017 ret = dp_ctrl_wait4video_ready(ctrl);
2021 mainlink_ready = dp_catalog_ctrl_mainlink_ready(ctrl->catalog);
2022 drm_dbg_dp(ctrl->drm_dev,
2023 "mainlink %s\n", mainlink_ready ? "READY" : "NOT READY");
2029 void dp_ctrl_off_link_stream(struct dp_ctrl *dp_ctrl)
2031 struct dp_ctrl_private *ctrl;
2034 ctrl = container_of(dp_ctrl, struct dp_ctrl_private, dp_ctrl);
2037 dp_catalog_panel_disable_vsc_sdp(ctrl->catalog);
2039 /* set dongle to D3 (power off) mode */
2040 dp_link_psm_config(ctrl->link, &ctrl->panel->link_info, true);
2042 dp_catalog_ctrl_mainlink_ctrl(ctrl->catalog, false);
2044 if (ctrl->stream_clks_on) {
2045 clk_disable_unprepare(ctrl->pixel_clk);
2046 ctrl->stream_clks_on = false;
2049 dev_pm_opp_set_rate(ctrl->dev, 0);
2050 dp_ctrl_link_clk_disable(&ctrl->dp_ctrl);
2054 /* aux channel down, reinit phy */
2058 drm_dbg_dp(ctrl->drm_dev, "phy=%p init=%d power_on=%d\n",
2059 phy, phy->init_count, phy->power_count);
2062 void dp_ctrl_off_link(struct dp_ctrl *dp_ctrl)
2064 struct dp_ctrl_private *ctrl;
2067 ctrl = container_of(dp_ctrl, struct dp_ctrl_private, dp_ctrl);
2070 dp_catalog_ctrl_mainlink_ctrl(ctrl->catalog, false);
2072 dp_ctrl_link_clk_disable(&ctrl->dp_ctrl);
2074 DRM_DEBUG_DP("Before, phy=%p init_count=%d power_on=%d\n",
2075 phy, phy->init_count, phy->power_count);
2079 DRM_DEBUG_DP("After, phy=%p init_count=%d power_on=%d\n",
2080 phy, phy->init_count, phy->power_count);
2083 void dp_ctrl_off(struct dp_ctrl *dp_ctrl)
2085 struct dp_ctrl_private *ctrl;
2088 ctrl = container_of(dp_ctrl, struct dp_ctrl_private, dp_ctrl);
2091 dp_catalog_panel_disable_vsc_sdp(ctrl->catalog);
2093 dp_catalog_ctrl_mainlink_ctrl(ctrl->catalog, false);
2095 dp_catalog_ctrl_reset(ctrl->catalog);
2097 if (ctrl->stream_clks_on) {
2098 clk_disable_unprepare(ctrl->pixel_clk);
2099 ctrl->stream_clks_on = false;
2102 dev_pm_opp_set_rate(ctrl->dev, 0);
2103 dp_ctrl_link_clk_disable(&ctrl->dp_ctrl);
2106 drm_dbg_dp(ctrl->drm_dev, "phy=%p init=%d power_on=%d\n",
2107 phy, phy->init_count, phy->power_count);
2110 irqreturn_t dp_ctrl_isr(struct dp_ctrl *dp_ctrl)
2112 struct dp_ctrl_private *ctrl;
2114 irqreturn_t ret = IRQ_NONE;
2119 ctrl = container_of(dp_ctrl, struct dp_ctrl_private, dp_ctrl);
2121 if (ctrl->panel->psr_cap.version) {
2122 isr = dp_catalog_ctrl_read_psr_interrupt_status(ctrl->catalog);
2125 complete(&ctrl->psr_op_comp);
2127 if (isr & PSR_EXIT_INT)
2128 drm_dbg_dp(ctrl->drm_dev, "PSR exit done\n");
2130 if (isr & PSR_UPDATE_INT)
2131 drm_dbg_dp(ctrl->drm_dev, "PSR frame update done\n");
2133 if (isr & PSR_CAPTURE_INT)
2134 drm_dbg_dp(ctrl->drm_dev, "PSR frame capture done\n");
2137 isr = dp_catalog_ctrl_get_interrupt(ctrl->catalog);
2140 if (isr & DP_CTRL_INTR_READY_FOR_VIDEO) {
2141 drm_dbg_dp(ctrl->drm_dev, "dp_video_ready\n");
2142 complete(&ctrl->video_comp);
2146 if (isr & DP_CTRL_INTR_IDLE_PATTERN_SENT) {
2147 drm_dbg_dp(ctrl->drm_dev, "idle_patterns_sent\n");
2148 complete(&ctrl->idle_comp);
2155 static const char *core_clks[] = {
2160 static const char *ctrl_clks[] = {
2165 static int dp_ctrl_clk_init(struct dp_ctrl *dp_ctrl)
2167 struct dp_ctrl_private *ctrl;
2171 ctrl = container_of(dp_ctrl, struct dp_ctrl_private, dp_ctrl);
2174 ctrl->num_core_clks = ARRAY_SIZE(core_clks);
2175 ctrl->core_clks = devm_kcalloc(dev, ctrl->num_core_clks, sizeof(*ctrl->core_clks), GFP_KERNEL);
2176 if (!ctrl->core_clks)
2179 for (i = 0; i < ctrl->num_core_clks; i++)
2180 ctrl->core_clks[i].id = core_clks[i];
2182 rc = devm_clk_bulk_get(dev, ctrl->num_core_clks, ctrl->core_clks);
2186 ctrl->num_link_clks = ARRAY_SIZE(ctrl_clks);
2187 ctrl->link_clks = devm_kcalloc(dev, ctrl->num_link_clks, sizeof(*ctrl->link_clks), GFP_KERNEL);
2188 if (!ctrl->link_clks)
2191 for (i = 0; i < ctrl->num_link_clks; i++)
2192 ctrl->link_clks[i].id = ctrl_clks[i];
2194 rc = devm_clk_bulk_get(dev, ctrl->num_link_clks, ctrl->link_clks);
2198 ctrl->pixel_clk = devm_clk_get(dev, "stream_pixel");
2199 if (IS_ERR(ctrl->pixel_clk))
2200 return PTR_ERR(ctrl->pixel_clk);
2205 struct dp_ctrl *dp_ctrl_get(struct device *dev, struct dp_link *link,
2206 struct dp_panel *panel, struct drm_dp_aux *aux,
2207 struct dp_catalog *catalog,
2210 struct dp_ctrl_private *ctrl;
2213 if (!dev || !panel || !aux ||
2214 !link || !catalog) {
2215 DRM_ERROR("invalid input\n");
2216 return ERR_PTR(-EINVAL);
2219 ctrl = devm_kzalloc(dev, sizeof(*ctrl), GFP_KERNEL);
2221 DRM_ERROR("Mem allocation failure\n");
2222 return ERR_PTR(-ENOMEM);
2225 ret = devm_pm_opp_set_clkname(dev, "ctrl_link");
2227 dev_err(dev, "invalid DP OPP table in device tree\n");
2228 /* caller do PTR_ERR(opp_table) */
2229 return (struct dp_ctrl *)ERR_PTR(ret);
2232 /* OPP table is optional */
2233 ret = devm_pm_opp_of_add_table(dev);
2235 dev_err(dev, "failed to add DP OPP table\n");
2237 init_completion(&ctrl->idle_comp);
2238 init_completion(&ctrl->psr_op_comp);
2239 init_completion(&ctrl->video_comp);
2242 ctrl->panel = panel;
2245 ctrl->catalog = catalog;
2249 ret = dp_ctrl_clk_init(&ctrl->dp_ctrl);
2251 dev_err(dev, "failed to init clocks\n");
2252 return ERR_PTR(ret);
2255 return &ctrl->dp_ctrl;