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