1 /*
2 * Analogix DP (Display Port) core interface driver.
3 *
4 * Copyright (C) 2012 Samsung Electronics Co., Ltd.
5 * Author: Jingoo Han <jg1.han@samsung.com>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
11 */
12 
13 #include <linux/module.h>
14 #include <linux/platform_device.h>
15 #include <linux/err.h>
16 #include <linux/clk.h>
17 #include <linux/io.h>
18 #include <linux/interrupt.h>
19 #include <linux/of.h>
20 #include <linux/of_gpio.h>
21 #include <linux/gpio.h>
22 #include <linux/component.h>
23 #include <linux/phy/phy.h>
24 
25 #include <drm/drmP.h>
26 #include <drm/drm_atomic_helper.h>
27 #include <drm/drm_crtc.h>
28 #include <drm/drm_crtc_helper.h>
29 #include <drm/drm_panel.h>
30 
31 #include <drm/bridge/analogix_dp.h>
32 
33 #include "analogix_dp_core.h"
34 #include "analogix_dp_reg.h"
35 
36 #define to_dp(nm)	container_of(nm, struct analogix_dp_device, nm)
37 
38 struct bridge_init {
39 	struct i2c_client *client;
40 	struct device_node *node;
41 };
42 
43 static void analogix_dp_init_dp(struct analogix_dp_device *dp)
44 {
45 	analogix_dp_reset(dp);
46 
47 	analogix_dp_swreset(dp);
48 
49 	analogix_dp_init_analog_param(dp);
50 	analogix_dp_init_interrupt(dp);
51 
52 	/* SW defined function Normal operation */
53 	analogix_dp_enable_sw_function(dp);
54 
55 	analogix_dp_config_interrupt(dp);
56 	analogix_dp_init_analog_func(dp);
57 
58 	analogix_dp_init_hpd(dp);
59 	analogix_dp_init_aux(dp);
60 }
61 
62 static int analogix_dp_detect_hpd(struct analogix_dp_device *dp)
63 {
64 	int timeout_loop = 0;
65 
66 	while (timeout_loop < DP_TIMEOUT_LOOP_COUNT) {
67 		if (analogix_dp_get_plug_in_status(dp) == 0)
68 			return 0;
69 
70 		timeout_loop++;
71 		usleep_range(10, 11);
72 	}
73 
74 	/*
75 	 * Some edp screen do not have hpd signal, so we can't just
76 	 * return failed when hpd plug in detect failed, DT property
77 	 * "force-hpd" would indicate whether driver need this.
78 	 */
79 	if (!dp->force_hpd)
80 		return -ETIMEDOUT;
81 
82 	/*
83 	 * The eDP TRM indicate that if HPD_STATUS(RO) is 0, AUX CH
84 	 * will not work, so we need to give a force hpd action to
85 	 * set HPD_STATUS manually.
86 	 */
87 	dev_dbg(dp->dev, "failed to get hpd plug status, try to force hpd\n");
88 
89 	analogix_dp_force_hpd(dp);
90 
91 	if (analogix_dp_get_plug_in_status(dp) != 0) {
92 		dev_err(dp->dev, "failed to get hpd plug in status\n");
93 		return -EINVAL;
94 	}
95 
96 	dev_dbg(dp->dev, "success to get plug in status after force hpd\n");
97 
98 	return 0;
99 }
100 
101 int analogix_dp_psr_supported(struct analogix_dp_device *dp)
102 {
103 
104 	return dp->psr_support;
105 }
106 EXPORT_SYMBOL_GPL(analogix_dp_psr_supported);
107 
108 int analogix_dp_enable_psr(struct analogix_dp_device *dp)
109 {
110 	struct edp_vsc_psr psr_vsc;
111 
112 	if (!dp->psr_support)
113 		return 0;
114 
115 	/* Prepare VSC packet as per EDP 1.4 spec, Table 6.9 */
116 	memset(&psr_vsc, 0, sizeof(psr_vsc));
117 	psr_vsc.sdp_header.HB0 = 0;
118 	psr_vsc.sdp_header.HB1 = 0x7;
119 	psr_vsc.sdp_header.HB2 = 0x2;
120 	psr_vsc.sdp_header.HB3 = 0x8;
121 
122 	psr_vsc.DB0 = 0;
123 	psr_vsc.DB1 = EDP_VSC_PSR_STATE_ACTIVE | EDP_VSC_PSR_CRC_VALUES_VALID;
124 
125 	analogix_dp_send_psr_spd(dp, &psr_vsc);
126 	return 0;
127 }
128 EXPORT_SYMBOL_GPL(analogix_dp_enable_psr);
129 
130 int analogix_dp_disable_psr(struct analogix_dp_device *dp)
131 {
132 	struct edp_vsc_psr psr_vsc;
133 	int ret;
134 
135 	if (!dp->psr_support)
136 		return 0;
137 
138 	/* Prepare VSC packet as per EDP 1.4 spec, Table 6.9 */
139 	memset(&psr_vsc, 0, sizeof(psr_vsc));
140 	psr_vsc.sdp_header.HB0 = 0;
141 	psr_vsc.sdp_header.HB1 = 0x7;
142 	psr_vsc.sdp_header.HB2 = 0x2;
143 	psr_vsc.sdp_header.HB3 = 0x8;
144 
145 	psr_vsc.DB0 = 0;
146 	psr_vsc.DB1 = 0;
147 
148 	ret = drm_dp_dpcd_writeb(&dp->aux, DP_SET_POWER, DP_SET_POWER_D0);
149 	if (ret != 1)
150 		dev_err(dp->dev, "Failed to set DP Power0 %d\n", ret);
151 
152 	analogix_dp_send_psr_spd(dp, &psr_vsc);
153 	return 0;
154 }
155 EXPORT_SYMBOL_GPL(analogix_dp_disable_psr);
156 
157 static bool analogix_dp_detect_sink_psr(struct analogix_dp_device *dp)
158 {
159 	unsigned char psr_version;
160 
161 	drm_dp_dpcd_readb(&dp->aux, DP_PSR_SUPPORT, &psr_version);
162 	dev_dbg(dp->dev, "Panel PSR version : %x\n", psr_version);
163 
164 	return (psr_version & DP_PSR_IS_SUPPORTED) ? true : false;
165 }
166 
167 static void analogix_dp_enable_sink_psr(struct analogix_dp_device *dp)
168 {
169 	unsigned char psr_en;
170 
171 	/* Disable psr function */
172 	drm_dp_dpcd_readb(&dp->aux, DP_PSR_EN_CFG, &psr_en);
173 	psr_en &= ~DP_PSR_ENABLE;
174 	drm_dp_dpcd_writeb(&dp->aux, DP_PSR_EN_CFG, psr_en);
175 
176 	/* Main-Link transmitter remains active during PSR active states */
177 	psr_en = DP_PSR_MAIN_LINK_ACTIVE | DP_PSR_CRC_VERIFICATION;
178 	drm_dp_dpcd_writeb(&dp->aux, DP_PSR_EN_CFG, psr_en);
179 
180 	/* Enable psr function */
181 	psr_en = DP_PSR_ENABLE | DP_PSR_MAIN_LINK_ACTIVE |
182 		 DP_PSR_CRC_VERIFICATION;
183 	drm_dp_dpcd_writeb(&dp->aux, DP_PSR_EN_CFG, psr_en);
184 
185 	analogix_dp_enable_psr_crc(dp);
186 }
187 
188 static void
189 analogix_dp_enable_rx_to_enhanced_mode(struct analogix_dp_device *dp,
190 				       bool enable)
191 {
192 	u8 data;
193 
194 	drm_dp_dpcd_readb(&dp->aux, DP_LANE_COUNT_SET, &data);
195 
196 	if (enable)
197 		drm_dp_dpcd_writeb(&dp->aux, DP_LANE_COUNT_SET,
198 				   DP_LANE_COUNT_ENHANCED_FRAME_EN |
199 					DPCD_LANE_COUNT_SET(data));
200 	else
201 		drm_dp_dpcd_writeb(&dp->aux, DP_LANE_COUNT_SET,
202 				   DPCD_LANE_COUNT_SET(data));
203 }
204 
205 static int analogix_dp_is_enhanced_mode_available(struct analogix_dp_device *dp)
206 {
207 	u8 data;
208 	int retval;
209 
210 	drm_dp_dpcd_readb(&dp->aux, DP_MAX_LANE_COUNT, &data);
211 	retval = DPCD_ENHANCED_FRAME_CAP(data);
212 
213 	return retval;
214 }
215 
216 static void analogix_dp_set_enhanced_mode(struct analogix_dp_device *dp)
217 {
218 	u8 data;
219 
220 	data = analogix_dp_is_enhanced_mode_available(dp);
221 	analogix_dp_enable_rx_to_enhanced_mode(dp, data);
222 	analogix_dp_enable_enhanced_mode(dp, data);
223 }
224 
225 static void analogix_dp_training_pattern_dis(struct analogix_dp_device *dp)
226 {
227 	analogix_dp_set_training_pattern(dp, DP_NONE);
228 
229 	drm_dp_dpcd_writeb(&dp->aux, DP_TRAINING_PATTERN_SET,
230 			   DP_TRAINING_PATTERN_DISABLE);
231 }
232 
233 static void
234 analogix_dp_set_lane_lane_pre_emphasis(struct analogix_dp_device *dp,
235 				       int pre_emphasis, int lane)
236 {
237 	switch (lane) {
238 	case 0:
239 		analogix_dp_set_lane0_pre_emphasis(dp, pre_emphasis);
240 		break;
241 	case 1:
242 		analogix_dp_set_lane1_pre_emphasis(dp, pre_emphasis);
243 		break;
244 
245 	case 2:
246 		analogix_dp_set_lane2_pre_emphasis(dp, pre_emphasis);
247 		break;
248 
249 	case 3:
250 		analogix_dp_set_lane3_pre_emphasis(dp, pre_emphasis);
251 		break;
252 	}
253 }
254 
255 static int analogix_dp_link_start(struct analogix_dp_device *dp)
256 {
257 	u8 buf[4];
258 	int lane, lane_count, pll_tries, retval;
259 
260 	lane_count = dp->link_train.lane_count;
261 
262 	dp->link_train.lt_state = CLOCK_RECOVERY;
263 	dp->link_train.eq_loop = 0;
264 
265 	for (lane = 0; lane < lane_count; lane++)
266 		dp->link_train.cr_loop[lane] = 0;
267 
268 	/* Set link rate and count as you want to establish*/
269 	analogix_dp_set_link_bandwidth(dp, dp->link_train.link_rate);
270 	analogix_dp_set_lane_count(dp, dp->link_train.lane_count);
271 
272 	/* Setup RX configuration */
273 	buf[0] = dp->link_train.link_rate;
274 	buf[1] = dp->link_train.lane_count;
275 	retval = drm_dp_dpcd_write(&dp->aux, DP_LINK_BW_SET, buf, 2);
276 	if (retval < 0)
277 		return retval;
278 
279 	/* Set TX pre-emphasis to minimum */
280 	for (lane = 0; lane < lane_count; lane++)
281 		analogix_dp_set_lane_lane_pre_emphasis(dp,
282 			PRE_EMPHASIS_LEVEL_0, lane);
283 
284 	/* Wait for PLL lock */
285 	pll_tries = 0;
286 	while (analogix_dp_get_pll_lock_status(dp) == PLL_UNLOCKED) {
287 		if (pll_tries == DP_TIMEOUT_LOOP_COUNT) {
288 			dev_err(dp->dev, "Wait for PLL lock timed out\n");
289 			return -ETIMEDOUT;
290 		}
291 
292 		pll_tries++;
293 		usleep_range(90, 120);
294 	}
295 
296 	/* Set training pattern 1 */
297 	analogix_dp_set_training_pattern(dp, TRAINING_PTN1);
298 
299 	/* Set RX training pattern */
300 	retval = drm_dp_dpcd_writeb(&dp->aux, DP_TRAINING_PATTERN_SET,
301 				    DP_LINK_SCRAMBLING_DISABLE |
302 					DP_TRAINING_PATTERN_1);
303 	if (retval < 0)
304 		return retval;
305 
306 	for (lane = 0; lane < lane_count; lane++)
307 		buf[lane] = DP_TRAIN_PRE_EMPH_LEVEL_0 |
308 			    DP_TRAIN_VOLTAGE_SWING_LEVEL_0;
309 
310 	retval = drm_dp_dpcd_write(&dp->aux, DP_TRAINING_LANE0_SET, buf,
311 				   lane_count);
312 	if (retval < 0)
313 		return retval;
314 
315 	return 0;
316 }
317 
318 static unsigned char analogix_dp_get_lane_status(u8 link_status[2], int lane)
319 {
320 	int shift = (lane & 1) * 4;
321 	u8 link_value = link_status[lane >> 1];
322 
323 	return (link_value >> shift) & 0xf;
324 }
325 
326 static int analogix_dp_clock_recovery_ok(u8 link_status[2], int lane_count)
327 {
328 	int lane;
329 	u8 lane_status;
330 
331 	for (lane = 0; lane < lane_count; lane++) {
332 		lane_status = analogix_dp_get_lane_status(link_status, lane);
333 		if ((lane_status & DP_LANE_CR_DONE) == 0)
334 			return -EINVAL;
335 	}
336 	return 0;
337 }
338 
339 static int analogix_dp_channel_eq_ok(u8 link_status[2], u8 link_align,
340 				     int lane_count)
341 {
342 	int lane;
343 	u8 lane_status;
344 
345 	if ((link_align & DP_INTERLANE_ALIGN_DONE) == 0)
346 		return -EINVAL;
347 
348 	for (lane = 0; lane < lane_count; lane++) {
349 		lane_status = analogix_dp_get_lane_status(link_status, lane);
350 		lane_status &= DP_CHANNEL_EQ_BITS;
351 		if (lane_status != DP_CHANNEL_EQ_BITS)
352 			return -EINVAL;
353 	}
354 
355 	return 0;
356 }
357 
358 static unsigned char
359 analogix_dp_get_adjust_request_voltage(u8 adjust_request[2], int lane)
360 {
361 	int shift = (lane & 1) * 4;
362 	u8 link_value = adjust_request[lane >> 1];
363 
364 	return (link_value >> shift) & 0x3;
365 }
366 
367 static unsigned char analogix_dp_get_adjust_request_pre_emphasis(
368 					u8 adjust_request[2],
369 					int lane)
370 {
371 	int shift = (lane & 1) * 4;
372 	u8 link_value = adjust_request[lane >> 1];
373 
374 	return ((link_value >> shift) & 0xc) >> 2;
375 }
376 
377 static void analogix_dp_set_lane_link_training(struct analogix_dp_device *dp,
378 					       u8 training_lane_set, int lane)
379 {
380 	switch (lane) {
381 	case 0:
382 		analogix_dp_set_lane0_link_training(dp, training_lane_set);
383 		break;
384 	case 1:
385 		analogix_dp_set_lane1_link_training(dp, training_lane_set);
386 		break;
387 
388 	case 2:
389 		analogix_dp_set_lane2_link_training(dp, training_lane_set);
390 		break;
391 
392 	case 3:
393 		analogix_dp_set_lane3_link_training(dp, training_lane_set);
394 		break;
395 	}
396 }
397 
398 static unsigned int
399 analogix_dp_get_lane_link_training(struct analogix_dp_device *dp,
400 				   int lane)
401 {
402 	u32 reg;
403 
404 	switch (lane) {
405 	case 0:
406 		reg = analogix_dp_get_lane0_link_training(dp);
407 		break;
408 	case 1:
409 		reg = analogix_dp_get_lane1_link_training(dp);
410 		break;
411 	case 2:
412 		reg = analogix_dp_get_lane2_link_training(dp);
413 		break;
414 	case 3:
415 		reg = analogix_dp_get_lane3_link_training(dp);
416 		break;
417 	default:
418 		WARN_ON(1);
419 		return 0;
420 	}
421 
422 	return reg;
423 }
424 
425 static void analogix_dp_reduce_link_rate(struct analogix_dp_device *dp)
426 {
427 	analogix_dp_training_pattern_dis(dp);
428 	analogix_dp_set_enhanced_mode(dp);
429 
430 	dp->link_train.lt_state = FAILED;
431 }
432 
433 static void analogix_dp_get_adjust_training_lane(struct analogix_dp_device *dp,
434 						 u8 adjust_request[2])
435 {
436 	int lane, lane_count;
437 	u8 voltage_swing, pre_emphasis, training_lane;
438 
439 	lane_count = dp->link_train.lane_count;
440 	for (lane = 0; lane < lane_count; lane++) {
441 		voltage_swing = analogix_dp_get_adjust_request_voltage(
442 						adjust_request, lane);
443 		pre_emphasis = analogix_dp_get_adjust_request_pre_emphasis(
444 						adjust_request, lane);
445 		training_lane = DPCD_VOLTAGE_SWING_SET(voltage_swing) |
446 				DPCD_PRE_EMPHASIS_SET(pre_emphasis);
447 
448 		if (voltage_swing == VOLTAGE_LEVEL_3)
449 			training_lane |= DP_TRAIN_MAX_SWING_REACHED;
450 		if (pre_emphasis == PRE_EMPHASIS_LEVEL_3)
451 			training_lane |= DP_TRAIN_MAX_PRE_EMPHASIS_REACHED;
452 
453 		dp->link_train.training_lane[lane] = training_lane;
454 	}
455 }
456 
457 static int analogix_dp_process_clock_recovery(struct analogix_dp_device *dp)
458 {
459 	int lane, lane_count, retval;
460 	u8 voltage_swing, pre_emphasis, training_lane;
461 	u8 link_status[2], adjust_request[2];
462 
463 	usleep_range(100, 101);
464 
465 	lane_count = dp->link_train.lane_count;
466 
467 	retval = drm_dp_dpcd_read(&dp->aux, DP_LANE0_1_STATUS, link_status, 2);
468 	if (retval < 0)
469 		return retval;
470 
471 	retval = drm_dp_dpcd_read(&dp->aux, DP_ADJUST_REQUEST_LANE0_1,
472 				  adjust_request, 2);
473 	if (retval < 0)
474 		return retval;
475 
476 	if (analogix_dp_clock_recovery_ok(link_status, lane_count) == 0) {
477 		/* set training pattern 2 for EQ */
478 		analogix_dp_set_training_pattern(dp, TRAINING_PTN2);
479 
480 		retval = drm_dp_dpcd_writeb(&dp->aux, DP_TRAINING_PATTERN_SET,
481 					    DP_LINK_SCRAMBLING_DISABLE |
482 						DP_TRAINING_PATTERN_2);
483 		if (retval < 0)
484 			return retval;
485 
486 		dev_info(dp->dev, "Link Training Clock Recovery success\n");
487 		dp->link_train.lt_state = EQUALIZER_TRAINING;
488 	} else {
489 		for (lane = 0; lane < lane_count; lane++) {
490 			training_lane = analogix_dp_get_lane_link_training(
491 							dp, lane);
492 			voltage_swing = analogix_dp_get_adjust_request_voltage(
493 							adjust_request, lane);
494 			pre_emphasis = analogix_dp_get_adjust_request_pre_emphasis(
495 							adjust_request, lane);
496 
497 			if (DPCD_VOLTAGE_SWING_GET(training_lane) ==
498 					voltage_swing &&
499 			    DPCD_PRE_EMPHASIS_GET(training_lane) ==
500 					pre_emphasis)
501 				dp->link_train.cr_loop[lane]++;
502 
503 			if (dp->link_train.cr_loop[lane] == MAX_CR_LOOP ||
504 			    voltage_swing == VOLTAGE_LEVEL_3 ||
505 			    pre_emphasis == PRE_EMPHASIS_LEVEL_3) {
506 				dev_err(dp->dev, "CR Max reached (%d,%d,%d)\n",
507 					dp->link_train.cr_loop[lane],
508 					voltage_swing, pre_emphasis);
509 				analogix_dp_reduce_link_rate(dp);
510 				return -EIO;
511 			}
512 		}
513 	}
514 
515 	analogix_dp_get_adjust_training_lane(dp, adjust_request);
516 
517 	for (lane = 0; lane < lane_count; lane++)
518 		analogix_dp_set_lane_link_training(dp,
519 			dp->link_train.training_lane[lane], lane);
520 
521 	retval = drm_dp_dpcd_write(&dp->aux, DP_TRAINING_LANE0_SET,
522 				   dp->link_train.training_lane, lane_count);
523 	if (retval < 0)
524 		return retval;
525 
526 	return 0;
527 }
528 
529 static int analogix_dp_process_equalizer_training(struct analogix_dp_device *dp)
530 {
531 	int lane, lane_count, retval;
532 	u32 reg;
533 	u8 link_align, link_status[2], adjust_request[2];
534 
535 	usleep_range(400, 401);
536 
537 	lane_count = dp->link_train.lane_count;
538 
539 	retval = drm_dp_dpcd_read(&dp->aux, DP_LANE0_1_STATUS, link_status, 2);
540 	if (retval < 0)
541 		return retval;
542 
543 	if (analogix_dp_clock_recovery_ok(link_status, lane_count)) {
544 		analogix_dp_reduce_link_rate(dp);
545 		return -EIO;
546 	}
547 
548 	retval = drm_dp_dpcd_read(&dp->aux, DP_ADJUST_REQUEST_LANE0_1,
549 				  adjust_request, 2);
550 	if (retval < 0)
551 		return retval;
552 
553 	retval = drm_dp_dpcd_readb(&dp->aux, DP_LANE_ALIGN_STATUS_UPDATED,
554 				   &link_align);
555 	if (retval < 0)
556 		return retval;
557 
558 	analogix_dp_get_adjust_training_lane(dp, adjust_request);
559 
560 	if (!analogix_dp_channel_eq_ok(link_status, link_align, lane_count)) {
561 		/* traing pattern Set to Normal */
562 		analogix_dp_training_pattern_dis(dp);
563 
564 		dev_info(dp->dev, "Link Training success!\n");
565 
566 		analogix_dp_get_link_bandwidth(dp, &reg);
567 		dp->link_train.link_rate = reg;
568 		dev_dbg(dp->dev, "final bandwidth = %.2x\n",
569 			dp->link_train.link_rate);
570 
571 		analogix_dp_get_lane_count(dp, &reg);
572 		dp->link_train.lane_count = reg;
573 		dev_dbg(dp->dev, "final lane count = %.2x\n",
574 			dp->link_train.lane_count);
575 
576 		/* set enhanced mode if available */
577 		analogix_dp_set_enhanced_mode(dp);
578 		dp->link_train.lt_state = FINISHED;
579 
580 		return 0;
581 	}
582 
583 	/* not all locked */
584 	dp->link_train.eq_loop++;
585 
586 	if (dp->link_train.eq_loop > MAX_EQ_LOOP) {
587 		dev_err(dp->dev, "EQ Max loop\n");
588 		analogix_dp_reduce_link_rate(dp);
589 		return -EIO;
590 	}
591 
592 	for (lane = 0; lane < lane_count; lane++)
593 		analogix_dp_set_lane_link_training(dp,
594 			dp->link_train.training_lane[lane], lane);
595 
596 	retval = drm_dp_dpcd_write(&dp->aux, DP_TRAINING_LANE0_SET,
597 				   dp->link_train.training_lane, lane_count);
598 	if (retval < 0)
599 		return retval;
600 
601 	return 0;
602 }
603 
604 static void analogix_dp_get_max_rx_bandwidth(struct analogix_dp_device *dp,
605 					     u8 *bandwidth)
606 {
607 	u8 data;
608 
609 	/*
610 	 * For DP rev.1.1, Maximum link rate of Main Link lanes
611 	 * 0x06 = 1.62 Gbps, 0x0a = 2.7 Gbps
612 	 * For DP rev.1.2, Maximum link rate of Main Link lanes
613 	 * 0x06 = 1.62 Gbps, 0x0a = 2.7 Gbps, 0x14 = 5.4Gbps
614 	 */
615 	drm_dp_dpcd_readb(&dp->aux, DP_MAX_LINK_RATE, &data);
616 	*bandwidth = data;
617 }
618 
619 static void analogix_dp_get_max_rx_lane_count(struct analogix_dp_device *dp,
620 					      u8 *lane_count)
621 {
622 	u8 data;
623 
624 	/*
625 	 * For DP rev.1.1, Maximum number of Main Link lanes
626 	 * 0x01 = 1 lane, 0x02 = 2 lanes, 0x04 = 4 lanes
627 	 */
628 	drm_dp_dpcd_readb(&dp->aux, DP_MAX_LANE_COUNT, &data);
629 	*lane_count = DPCD_MAX_LANE_COUNT(data);
630 }
631 
632 static void analogix_dp_init_training(struct analogix_dp_device *dp,
633 				      enum link_lane_count_type max_lane,
634 				      int max_rate)
635 {
636 	/*
637 	 * MACRO_RST must be applied after the PLL_LOCK to avoid
638 	 * the DP inter pair skew issue for at least 10 us
639 	 */
640 	analogix_dp_reset_macro(dp);
641 
642 	/* Initialize by reading RX's DPCD */
643 	analogix_dp_get_max_rx_bandwidth(dp, &dp->link_train.link_rate);
644 	analogix_dp_get_max_rx_lane_count(dp, &dp->link_train.lane_count);
645 
646 	if ((dp->link_train.link_rate != DP_LINK_BW_1_62) &&
647 	    (dp->link_train.link_rate != DP_LINK_BW_2_7) &&
648 	    (dp->link_train.link_rate != DP_LINK_BW_5_4)) {
649 		dev_err(dp->dev, "Rx Max Link Rate is abnormal :%x !\n",
650 			dp->link_train.link_rate);
651 		dp->link_train.link_rate = DP_LINK_BW_1_62;
652 	}
653 
654 	if (dp->link_train.lane_count == 0) {
655 		dev_err(dp->dev, "Rx Max Lane count is abnormal :%x !\n",
656 			dp->link_train.lane_count);
657 		dp->link_train.lane_count = (u8)LANE_COUNT1;
658 	}
659 
660 	/* Setup TX lane count & rate */
661 	if (dp->link_train.lane_count > max_lane)
662 		dp->link_train.lane_count = max_lane;
663 	if (dp->link_train.link_rate > max_rate)
664 		dp->link_train.link_rate = max_rate;
665 
666 	/* All DP analog module power up */
667 	analogix_dp_set_analog_power_down(dp, POWER_ALL, 0);
668 }
669 
670 static int analogix_dp_sw_link_training(struct analogix_dp_device *dp)
671 {
672 	int retval = 0, training_finished = 0;
673 
674 	dp->link_train.lt_state = START;
675 
676 	/* Process here */
677 	while (!retval && !training_finished) {
678 		switch (dp->link_train.lt_state) {
679 		case START:
680 			retval = analogix_dp_link_start(dp);
681 			if (retval)
682 				dev_err(dp->dev, "LT link start failed!\n");
683 			break;
684 		case CLOCK_RECOVERY:
685 			retval = analogix_dp_process_clock_recovery(dp);
686 			if (retval)
687 				dev_err(dp->dev, "LT CR failed!\n");
688 			break;
689 		case EQUALIZER_TRAINING:
690 			retval = analogix_dp_process_equalizer_training(dp);
691 			if (retval)
692 				dev_err(dp->dev, "LT EQ failed!\n");
693 			break;
694 		case FINISHED:
695 			training_finished = 1;
696 			break;
697 		case FAILED:
698 			return -EREMOTEIO;
699 		}
700 	}
701 	if (retval)
702 		dev_err(dp->dev, "eDP link training failed (%d)\n", retval);
703 
704 	return retval;
705 }
706 
707 static int analogix_dp_set_link_train(struct analogix_dp_device *dp,
708 				      u32 count, u32 bwtype)
709 {
710 	int i;
711 	int retval;
712 
713 	for (i = 0; i < DP_TIMEOUT_LOOP_COUNT; i++) {
714 		analogix_dp_init_training(dp, count, bwtype);
715 		retval = analogix_dp_sw_link_training(dp);
716 		if (retval == 0)
717 			break;
718 
719 		usleep_range(100, 110);
720 	}
721 
722 	return retval;
723 }
724 
725 static int analogix_dp_config_video(struct analogix_dp_device *dp)
726 {
727 	int timeout_loop = 0;
728 	int done_count = 0;
729 
730 	analogix_dp_config_video_slave_mode(dp);
731 
732 	analogix_dp_set_video_color_format(dp);
733 
734 	if (analogix_dp_get_pll_lock_status(dp) == PLL_UNLOCKED) {
735 		dev_err(dp->dev, "PLL is not locked yet.\n");
736 		return -EINVAL;
737 	}
738 
739 	for (;;) {
740 		timeout_loop++;
741 		if (analogix_dp_is_slave_video_stream_clock_on(dp) == 0)
742 			break;
743 		if (timeout_loop > DP_TIMEOUT_LOOP_COUNT) {
744 			dev_err(dp->dev, "Timeout of video streamclk ok\n");
745 			return -ETIMEDOUT;
746 		}
747 
748 		usleep_range(1, 2);
749 	}
750 
751 	/* Set to use the register calculated M/N video */
752 	analogix_dp_set_video_cr_mn(dp, CALCULATED_M, 0, 0);
753 
754 	/* For video bist, Video timing must be generated by register */
755 	analogix_dp_set_video_timing_mode(dp, VIDEO_TIMING_FROM_CAPTURE);
756 
757 	/* Disable video mute */
758 	analogix_dp_enable_video_mute(dp, 0);
759 
760 	/* Configure video slave mode */
761 	analogix_dp_enable_video_master(dp, 0);
762 
763 	timeout_loop = 0;
764 
765 	for (;;) {
766 		timeout_loop++;
767 		if (analogix_dp_is_video_stream_on(dp) == 0) {
768 			done_count++;
769 			if (done_count > 10)
770 				break;
771 		} else if (done_count) {
772 			done_count = 0;
773 		}
774 		if (timeout_loop > DP_TIMEOUT_LOOP_COUNT) {
775 			dev_err(dp->dev, "Timeout of video streamclk ok\n");
776 			return -ETIMEDOUT;
777 		}
778 
779 		usleep_range(1000, 1001);
780 	}
781 
782 	return 0;
783 }
784 
785 static void analogix_dp_enable_scramble(struct analogix_dp_device *dp,
786 					bool enable)
787 {
788 	u8 data;
789 
790 	if (enable) {
791 		analogix_dp_enable_scrambling(dp);
792 
793 		drm_dp_dpcd_readb(&dp->aux, DP_TRAINING_PATTERN_SET, &data);
794 		drm_dp_dpcd_writeb(&dp->aux, DP_TRAINING_PATTERN_SET,
795 				   (u8)(data & ~DP_LINK_SCRAMBLING_DISABLE));
796 	} else {
797 		analogix_dp_disable_scrambling(dp);
798 
799 		drm_dp_dpcd_readb(&dp->aux, DP_TRAINING_PATTERN_SET, &data);
800 		drm_dp_dpcd_writeb(&dp->aux, DP_TRAINING_PATTERN_SET,
801 				   (u8)(data | DP_LINK_SCRAMBLING_DISABLE));
802 	}
803 }
804 
805 static irqreturn_t analogix_dp_hardirq(int irq, void *arg)
806 {
807 	struct analogix_dp_device *dp = arg;
808 	irqreturn_t ret = IRQ_NONE;
809 	enum dp_irq_type irq_type;
810 
811 	irq_type = analogix_dp_get_irq_type(dp);
812 	if (irq_type != DP_IRQ_TYPE_UNKNOWN) {
813 		analogix_dp_mute_hpd_interrupt(dp);
814 		ret = IRQ_WAKE_THREAD;
815 	}
816 
817 	return ret;
818 }
819 
820 static irqreturn_t analogix_dp_irq_thread(int irq, void *arg)
821 {
822 	struct analogix_dp_device *dp = arg;
823 	enum dp_irq_type irq_type;
824 
825 	irq_type = analogix_dp_get_irq_type(dp);
826 	if (irq_type & DP_IRQ_TYPE_HP_CABLE_IN ||
827 	    irq_type & DP_IRQ_TYPE_HP_CABLE_OUT) {
828 		dev_dbg(dp->dev, "Detected cable status changed!\n");
829 		if (dp->drm_dev)
830 			drm_helper_hpd_irq_event(dp->drm_dev);
831 	}
832 
833 	if (irq_type != DP_IRQ_TYPE_UNKNOWN) {
834 		analogix_dp_clear_hotplug_interrupts(dp);
835 		analogix_dp_unmute_hpd_interrupt(dp);
836 	}
837 
838 	return IRQ_HANDLED;
839 }
840 
841 static void analogix_dp_commit(struct analogix_dp_device *dp)
842 {
843 	int ret;
844 
845 	/* Keep the panel disabled while we configure video */
846 	if (dp->plat_data->panel) {
847 		if (drm_panel_disable(dp->plat_data->panel))
848 			DRM_ERROR("failed to disable the panel\n");
849 	}
850 
851 	ret = analogix_dp_set_link_train(dp, dp->video_info.max_lane_count,
852 					 dp->video_info.max_link_rate);
853 	if (ret) {
854 		dev_err(dp->dev, "unable to do link train\n");
855 		return;
856 	}
857 
858 	analogix_dp_enable_scramble(dp, 1);
859 	analogix_dp_enable_rx_to_enhanced_mode(dp, 1);
860 	analogix_dp_enable_enhanced_mode(dp, 1);
861 
862 	analogix_dp_init_video(dp);
863 	ret = analogix_dp_config_video(dp);
864 	if (ret)
865 		dev_err(dp->dev, "unable to config video\n");
866 
867 	/* Safe to enable the panel now */
868 	if (dp->plat_data->panel) {
869 		if (drm_panel_enable(dp->plat_data->panel))
870 			DRM_ERROR("failed to enable the panel\n");
871 	}
872 
873 	/* Enable video */
874 	analogix_dp_start_video(dp);
875 
876 	dp->psr_support = analogix_dp_detect_sink_psr(dp);
877 	if (dp->psr_support)
878 		analogix_dp_enable_sink_psr(dp);
879 }
880 
881 /*
882  * This function is a bit of a catch-all for panel preparation, hopefully
883  * simplifying the logic of functions that need to prepare/unprepare the panel
884  * below.
885  *
886  * If @prepare is true, this function will prepare the panel. Conversely, if it
887  * is false, the panel will be unprepared.
888  *
889  * If @is_modeset_prepare is true, the function will disregard the current state
890  * of the panel and either prepare/unprepare the panel based on @prepare. Once
891  * it finishes, it will update dp->panel_is_modeset to reflect the current state
892  * of the panel.
893  */
894 static int analogix_dp_prepare_panel(struct analogix_dp_device *dp,
895 				     bool prepare, bool is_modeset_prepare)
896 {
897 	int ret = 0;
898 
899 	if (!dp->plat_data->panel)
900 		return 0;
901 
902 	mutex_lock(&dp->panel_lock);
903 
904 	/*
905 	 * Exit early if this is a temporary prepare/unprepare and we're already
906 	 * modeset (since we neither want to prepare twice or unprepare early).
907 	 */
908 	if (dp->panel_is_modeset && !is_modeset_prepare)
909 		goto out;
910 
911 	if (prepare)
912 		ret = drm_panel_prepare(dp->plat_data->panel);
913 	else
914 		ret = drm_panel_unprepare(dp->plat_data->panel);
915 
916 	if (ret)
917 		goto out;
918 
919 	if (is_modeset_prepare)
920 		dp->panel_is_modeset = prepare;
921 
922 out:
923 	mutex_unlock(&dp->panel_lock);
924 	return ret;
925 }
926 
927 static int analogix_dp_get_modes(struct drm_connector *connector)
928 {
929 	struct analogix_dp_device *dp = to_dp(connector);
930 	struct edid *edid;
931 	int ret, num_modes = 0;
932 
933 	if (dp->plat_data->panel) {
934 		num_modes += drm_panel_get_modes(dp->plat_data->panel);
935 	} else {
936 		ret = analogix_dp_prepare_panel(dp, true, false);
937 		if (ret) {
938 			DRM_ERROR("Failed to prepare panel (%d)\n", ret);
939 			return 0;
940 		}
941 
942 		pm_runtime_get_sync(dp->dev);
943 		edid = drm_get_edid(connector, &dp->aux.ddc);
944 		pm_runtime_put(dp->dev);
945 		if (edid) {
946 			drm_mode_connector_update_edid_property(&dp->connector,
947 								edid);
948 			num_modes += drm_add_edid_modes(&dp->connector, edid);
949 			kfree(edid);
950 		}
951 
952 		ret = analogix_dp_prepare_panel(dp, false, false);
953 		if (ret)
954 			DRM_ERROR("Failed to unprepare panel (%d)\n", ret);
955 	}
956 
957 	if (dp->plat_data->get_modes)
958 		num_modes += dp->plat_data->get_modes(dp->plat_data, connector);
959 
960 	return num_modes;
961 }
962 
963 static struct drm_encoder *
964 analogix_dp_best_encoder(struct drm_connector *connector)
965 {
966 	struct analogix_dp_device *dp = to_dp(connector);
967 
968 	return dp->encoder;
969 }
970 
971 static const struct drm_connector_helper_funcs analogix_dp_connector_helper_funcs = {
972 	.get_modes = analogix_dp_get_modes,
973 	.best_encoder = analogix_dp_best_encoder,
974 };
975 
976 static enum drm_connector_status
977 analogix_dp_detect(struct drm_connector *connector, bool force)
978 {
979 	struct analogix_dp_device *dp = to_dp(connector);
980 	enum drm_connector_status status = connector_status_disconnected;
981 	int ret;
982 
983 	if (dp->plat_data->panel)
984 		return connector_status_connected;
985 
986 	ret = analogix_dp_prepare_panel(dp, true, false);
987 	if (ret) {
988 		DRM_ERROR("Failed to prepare panel (%d)\n", ret);
989 		return connector_status_disconnected;
990 	}
991 
992 	if (!analogix_dp_detect_hpd(dp))
993 		status = connector_status_connected;
994 
995 	ret = analogix_dp_prepare_panel(dp, false, false);
996 	if (ret)
997 		DRM_ERROR("Failed to unprepare panel (%d)\n", ret);
998 
999 	return status;
1000 }
1001 
1002 static const struct drm_connector_funcs analogix_dp_connector_funcs = {
1003 	.fill_modes = drm_helper_probe_single_connector_modes,
1004 	.detect = analogix_dp_detect,
1005 	.destroy = drm_connector_cleanup,
1006 	.reset = drm_atomic_helper_connector_reset,
1007 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
1008 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
1009 };
1010 
1011 static int analogix_dp_bridge_attach(struct drm_bridge *bridge)
1012 {
1013 	struct analogix_dp_device *dp = bridge->driver_private;
1014 	struct drm_encoder *encoder = dp->encoder;
1015 	struct drm_connector *connector = NULL;
1016 	int ret = 0;
1017 
1018 	if (!bridge->encoder) {
1019 		DRM_ERROR("Parent encoder object not found");
1020 		return -ENODEV;
1021 	}
1022 
1023 	if (!dp->plat_data->skip_connector) {
1024 		connector = &dp->connector;
1025 		connector->polled = DRM_CONNECTOR_POLL_HPD;
1026 
1027 		ret = drm_connector_init(dp->drm_dev, connector,
1028 					 &analogix_dp_connector_funcs,
1029 					 DRM_MODE_CONNECTOR_eDP);
1030 		if (ret) {
1031 			DRM_ERROR("Failed to initialize connector with drm\n");
1032 			return ret;
1033 		}
1034 
1035 		drm_connector_helper_add(connector,
1036 					 &analogix_dp_connector_helper_funcs);
1037 		drm_mode_connector_attach_encoder(connector, encoder);
1038 	}
1039 
1040 	/*
1041 	 * NOTE: the connector registration is implemented in analogix
1042 	 * platform driver, that to say connector would be exist after
1043 	 * plat_data->attch return, that's why we record the connector
1044 	 * point after plat attached.
1045 	 */
1046 	 if (dp->plat_data->attach) {
1047 		 ret = dp->plat_data->attach(dp->plat_data, bridge, connector);
1048 		 if (ret) {
1049 			 DRM_ERROR("Failed at platform attch func\n");
1050 			 return ret;
1051 		 }
1052 	}
1053 
1054 	if (dp->plat_data->panel) {
1055 		ret = drm_panel_attach(dp->plat_data->panel, &dp->connector);
1056 		if (ret) {
1057 			DRM_ERROR("Failed to attach panel\n");
1058 			return ret;
1059 		}
1060 	}
1061 
1062 	return 0;
1063 }
1064 
1065 static void analogix_dp_bridge_pre_enable(struct drm_bridge *bridge)
1066 {
1067 	struct analogix_dp_device *dp = bridge->driver_private;
1068 	int ret;
1069 
1070 	ret = analogix_dp_prepare_panel(dp, true, true);
1071 	if (ret)
1072 		DRM_ERROR("failed to setup the panel ret = %d\n", ret);
1073 }
1074 
1075 static void analogix_dp_bridge_enable(struct drm_bridge *bridge)
1076 {
1077 	struct analogix_dp_device *dp = bridge->driver_private;
1078 
1079 	if (dp->dpms_mode == DRM_MODE_DPMS_ON)
1080 		return;
1081 
1082 	pm_runtime_get_sync(dp->dev);
1083 
1084 	if (dp->plat_data->power_on)
1085 		dp->plat_data->power_on(dp->plat_data);
1086 
1087 	phy_power_on(dp->phy);
1088 	analogix_dp_init_dp(dp);
1089 	enable_irq(dp->irq);
1090 	analogix_dp_commit(dp);
1091 
1092 	dp->dpms_mode = DRM_MODE_DPMS_ON;
1093 }
1094 
1095 static void analogix_dp_bridge_disable(struct drm_bridge *bridge)
1096 {
1097 	struct analogix_dp_device *dp = bridge->driver_private;
1098 	int ret;
1099 
1100 	if (dp->dpms_mode != DRM_MODE_DPMS_ON)
1101 		return;
1102 
1103 	if (dp->plat_data->panel) {
1104 		if (drm_panel_disable(dp->plat_data->panel)) {
1105 			DRM_ERROR("failed to disable the panel\n");
1106 			return;
1107 		}
1108 	}
1109 
1110 	disable_irq(dp->irq);
1111 	phy_power_off(dp->phy);
1112 
1113 	if (dp->plat_data->power_off)
1114 		dp->plat_data->power_off(dp->plat_data);
1115 
1116 	pm_runtime_put_sync(dp->dev);
1117 
1118 	ret = analogix_dp_prepare_panel(dp, false, true);
1119 	if (ret)
1120 		DRM_ERROR("failed to setup the panel ret = %d\n", ret);
1121 
1122 	dp->dpms_mode = DRM_MODE_DPMS_OFF;
1123 }
1124 
1125 static void analogix_dp_bridge_mode_set(struct drm_bridge *bridge,
1126 					struct drm_display_mode *orig_mode,
1127 					struct drm_display_mode *mode)
1128 {
1129 	struct analogix_dp_device *dp = bridge->driver_private;
1130 	struct drm_display_info *display_info = &dp->connector.display_info;
1131 	struct video_info *video = &dp->video_info;
1132 	struct device_node *dp_node = dp->dev->of_node;
1133 	int vic;
1134 
1135 	/* Input video interlaces & hsync pol & vsync pol */
1136 	video->interlaced = !!(mode->flags & DRM_MODE_FLAG_INTERLACE);
1137 	video->v_sync_polarity = !!(mode->flags & DRM_MODE_FLAG_NVSYNC);
1138 	video->h_sync_polarity = !!(mode->flags & DRM_MODE_FLAG_NHSYNC);
1139 
1140 	/* Input video dynamic_range & colorimetry */
1141 	vic = drm_match_cea_mode(mode);
1142 	if ((vic == 6) || (vic == 7) || (vic == 21) || (vic == 22) ||
1143 	    (vic == 2) || (vic == 3) || (vic == 17) || (vic == 18)) {
1144 		video->dynamic_range = CEA;
1145 		video->ycbcr_coeff = COLOR_YCBCR601;
1146 	} else if (vic) {
1147 		video->dynamic_range = CEA;
1148 		video->ycbcr_coeff = COLOR_YCBCR709;
1149 	} else {
1150 		video->dynamic_range = VESA;
1151 		video->ycbcr_coeff = COLOR_YCBCR709;
1152 	}
1153 
1154 	/* Input vide bpc and color_formats */
1155 	switch (display_info->bpc) {
1156 	case 12:
1157 		video->color_depth = COLOR_12;
1158 		break;
1159 	case 10:
1160 		video->color_depth = COLOR_10;
1161 		break;
1162 	case 8:
1163 		video->color_depth = COLOR_8;
1164 		break;
1165 	case 6:
1166 		video->color_depth = COLOR_6;
1167 		break;
1168 	default:
1169 		video->color_depth = COLOR_8;
1170 		break;
1171 	}
1172 	if (display_info->color_formats & DRM_COLOR_FORMAT_YCRCB444)
1173 		video->color_space = COLOR_YCBCR444;
1174 	else if (display_info->color_formats & DRM_COLOR_FORMAT_YCRCB422)
1175 		video->color_space = COLOR_YCBCR422;
1176 	else if (display_info->color_formats & DRM_COLOR_FORMAT_RGB444)
1177 		video->color_space = COLOR_RGB;
1178 	else
1179 		video->color_space = COLOR_RGB;
1180 
1181 	/*
1182 	 * NOTE: those property parsing code is used for providing backward
1183 	 * compatibility for samsung platform.
1184 	 * Due to we used the "of_property_read_u32" interfaces, when this
1185 	 * property isn't present, the "video_info" can keep the original
1186 	 * values and wouldn't be modified.
1187 	 */
1188 	of_property_read_u32(dp_node, "samsung,color-space",
1189 			     &video->color_space);
1190 	of_property_read_u32(dp_node, "samsung,dynamic-range",
1191 			     &video->dynamic_range);
1192 	of_property_read_u32(dp_node, "samsung,ycbcr-coeff",
1193 			     &video->ycbcr_coeff);
1194 	of_property_read_u32(dp_node, "samsung,color-depth",
1195 			     &video->color_depth);
1196 	if (of_property_read_bool(dp_node, "hsync-active-high"))
1197 		video->h_sync_polarity = true;
1198 	if (of_property_read_bool(dp_node, "vsync-active-high"))
1199 		video->v_sync_polarity = true;
1200 	if (of_property_read_bool(dp_node, "interlaced"))
1201 		video->interlaced = true;
1202 }
1203 
1204 static void analogix_dp_bridge_nop(struct drm_bridge *bridge)
1205 {
1206 	/* do nothing */
1207 }
1208 
1209 static const struct drm_bridge_funcs analogix_dp_bridge_funcs = {
1210 	.pre_enable = analogix_dp_bridge_pre_enable,
1211 	.enable = analogix_dp_bridge_enable,
1212 	.disable = analogix_dp_bridge_disable,
1213 	.post_disable = analogix_dp_bridge_nop,
1214 	.mode_set = analogix_dp_bridge_mode_set,
1215 	.attach = analogix_dp_bridge_attach,
1216 };
1217 
1218 static int analogix_dp_create_bridge(struct drm_device *drm_dev,
1219 				     struct analogix_dp_device *dp)
1220 {
1221 	struct drm_bridge *bridge;
1222 	int ret;
1223 
1224 	bridge = devm_kzalloc(drm_dev->dev, sizeof(*bridge), GFP_KERNEL);
1225 	if (!bridge) {
1226 		DRM_ERROR("failed to allocate for drm bridge\n");
1227 		return -ENOMEM;
1228 	}
1229 
1230 	dp->bridge = bridge;
1231 
1232 	bridge->driver_private = dp;
1233 	bridge->funcs = &analogix_dp_bridge_funcs;
1234 
1235 	ret = drm_bridge_attach(dp->encoder, bridge, NULL);
1236 	if (ret) {
1237 		DRM_ERROR("failed to attach drm bridge\n");
1238 		return -EINVAL;
1239 	}
1240 
1241 	return 0;
1242 }
1243 
1244 static int analogix_dp_dt_parse_pdata(struct analogix_dp_device *dp)
1245 {
1246 	struct device_node *dp_node = dp->dev->of_node;
1247 	struct video_info *video_info = &dp->video_info;
1248 
1249 	switch (dp->plat_data->dev_type) {
1250 	case RK3288_DP:
1251 	case RK3399_EDP:
1252 		/*
1253 		 * Like Rk3288 DisplayPort TRM indicate that "Main link
1254 		 * containing 4 physical lanes of 2.7/1.62 Gbps/lane".
1255 		 */
1256 		video_info->max_link_rate = 0x0A;
1257 		video_info->max_lane_count = 0x04;
1258 		break;
1259 	case EXYNOS_DP:
1260 		/*
1261 		 * NOTE: those property parseing code is used for
1262 		 * providing backward compatibility for samsung platform.
1263 		 */
1264 		of_property_read_u32(dp_node, "samsung,link-rate",
1265 				     &video_info->max_link_rate);
1266 		of_property_read_u32(dp_node, "samsung,lane-count",
1267 				     &video_info->max_lane_count);
1268 		break;
1269 	}
1270 
1271 	return 0;
1272 }
1273 
1274 static ssize_t analogix_dpaux_transfer(struct drm_dp_aux *aux,
1275 				       struct drm_dp_aux_msg *msg)
1276 {
1277 	struct analogix_dp_device *dp = to_dp(aux);
1278 
1279 	return analogix_dp_transfer(dp, msg);
1280 }
1281 
1282 struct analogix_dp_device *
1283 analogix_dp_bind(struct device *dev, struct drm_device *drm_dev,
1284 		 struct analogix_dp_plat_data *plat_data)
1285 {
1286 	struct platform_device *pdev = to_platform_device(dev);
1287 	struct analogix_dp_device *dp;
1288 	struct resource *res;
1289 	unsigned int irq_flags;
1290 	int ret;
1291 
1292 	if (!plat_data) {
1293 		dev_err(dev, "Invalided input plat_data\n");
1294 		return ERR_PTR(-EINVAL);
1295 	}
1296 
1297 	dp = devm_kzalloc(dev, sizeof(struct analogix_dp_device), GFP_KERNEL);
1298 	if (!dp)
1299 		return ERR_PTR(-ENOMEM);
1300 
1301 	dp->dev = &pdev->dev;
1302 	dp->dpms_mode = DRM_MODE_DPMS_OFF;
1303 
1304 	mutex_init(&dp->panel_lock);
1305 	dp->panel_is_modeset = false;
1306 
1307 	/*
1308 	 * platform dp driver need containor_of the plat_data to get
1309 	 * the driver private data, so we need to store the point of
1310 	 * plat_data, not the context of plat_data.
1311 	 */
1312 	dp->plat_data = plat_data;
1313 
1314 	ret = analogix_dp_dt_parse_pdata(dp);
1315 	if (ret)
1316 		return ERR_PTR(ret);
1317 
1318 	dp->phy = devm_phy_get(dp->dev, "dp");
1319 	if (IS_ERR(dp->phy)) {
1320 		dev_err(dp->dev, "no DP phy configured\n");
1321 		ret = PTR_ERR(dp->phy);
1322 		if (ret) {
1323 			/*
1324 			 * phy itself is not enabled, so we can move forward
1325 			 * assigning NULL to phy pointer.
1326 			 */
1327 			if (ret == -ENOSYS || ret == -ENODEV)
1328 				dp->phy = NULL;
1329 			else
1330 				return ERR_PTR(ret);
1331 		}
1332 	}
1333 
1334 	dp->clock = devm_clk_get(&pdev->dev, "dp");
1335 	if (IS_ERR(dp->clock)) {
1336 		dev_err(&pdev->dev, "failed to get clock\n");
1337 		return ERR_CAST(dp->clock);
1338 	}
1339 
1340 	clk_prepare_enable(dp->clock);
1341 
1342 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1343 
1344 	dp->reg_base = devm_ioremap_resource(&pdev->dev, res);
1345 	if (IS_ERR(dp->reg_base))
1346 		return ERR_CAST(dp->reg_base);
1347 
1348 	dp->force_hpd = of_property_read_bool(dev->of_node, "force-hpd");
1349 
1350 	dp->hpd_gpio = of_get_named_gpio(dev->of_node, "hpd-gpios", 0);
1351 	if (!gpio_is_valid(dp->hpd_gpio))
1352 		dp->hpd_gpio = of_get_named_gpio(dev->of_node,
1353 						 "samsung,hpd-gpio", 0);
1354 
1355 	if (gpio_is_valid(dp->hpd_gpio)) {
1356 		/*
1357 		 * Set up the hotplug GPIO from the device tree as an interrupt.
1358 		 * Simply specifying a different interrupt in the device tree
1359 		 * doesn't work since we handle hotplug rather differently when
1360 		 * using a GPIO.  We also need the actual GPIO specifier so
1361 		 * that we can get the current state of the GPIO.
1362 		 */
1363 		ret = devm_gpio_request_one(&pdev->dev, dp->hpd_gpio, GPIOF_IN,
1364 					    "hpd_gpio");
1365 		if (ret) {
1366 			dev_err(&pdev->dev, "failed to get hpd gpio\n");
1367 			return ERR_PTR(ret);
1368 		}
1369 		dp->irq = gpio_to_irq(dp->hpd_gpio);
1370 		irq_flags = IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING;
1371 	} else {
1372 		dp->hpd_gpio = -ENODEV;
1373 		dp->irq = platform_get_irq(pdev, 0);
1374 		irq_flags = 0;
1375 	}
1376 
1377 	if (dp->irq == -ENXIO) {
1378 		dev_err(&pdev->dev, "failed to get irq\n");
1379 		return ERR_PTR(-ENODEV);
1380 	}
1381 
1382 	ret = devm_request_threaded_irq(&pdev->dev, dp->irq,
1383 					analogix_dp_hardirq,
1384 					analogix_dp_irq_thread,
1385 					irq_flags, "analogix-dp", dp);
1386 	if (ret) {
1387 		dev_err(&pdev->dev, "failed to request irq\n");
1388 		goto err_disable_pm_runtime;
1389 	}
1390 	disable_irq(dp->irq);
1391 
1392 	dp->drm_dev = drm_dev;
1393 	dp->encoder = dp->plat_data->encoder;
1394 
1395 	dp->aux.name = "DP-AUX";
1396 	dp->aux.transfer = analogix_dpaux_transfer;
1397 	dp->aux.dev = &pdev->dev;
1398 
1399 	ret = drm_dp_aux_register(&dp->aux);
1400 	if (ret)
1401 		return ERR_PTR(ret);
1402 
1403 	pm_runtime_enable(dev);
1404 
1405 	ret = analogix_dp_create_bridge(drm_dev, dp);
1406 	if (ret) {
1407 		DRM_ERROR("failed to create bridge (%d)\n", ret);
1408 		goto err_disable_pm_runtime;
1409 	}
1410 
1411 	return dp;
1412 
1413 err_disable_pm_runtime:
1414 
1415 	pm_runtime_disable(dev);
1416 
1417 	return ERR_PTR(ret);
1418 }
1419 EXPORT_SYMBOL_GPL(analogix_dp_bind);
1420 
1421 void analogix_dp_unbind(struct analogix_dp_device *dp)
1422 {
1423 	analogix_dp_bridge_disable(dp->bridge);
1424 	dp->connector.funcs->destroy(&dp->connector);
1425 
1426 	if (dp->plat_data->panel) {
1427 		if (drm_panel_unprepare(dp->plat_data->panel))
1428 			DRM_ERROR("failed to turnoff the panel\n");
1429 		if (drm_panel_detach(dp->plat_data->panel))
1430 			DRM_ERROR("failed to detach the panel\n");
1431 	}
1432 
1433 	drm_dp_aux_unregister(&dp->aux);
1434 	pm_runtime_disable(dp->dev);
1435 	clk_disable_unprepare(dp->clock);
1436 }
1437 EXPORT_SYMBOL_GPL(analogix_dp_unbind);
1438 
1439 #ifdef CONFIG_PM
1440 int analogix_dp_suspend(struct analogix_dp_device *dp)
1441 {
1442 	clk_disable_unprepare(dp->clock);
1443 
1444 	if (dp->plat_data->panel) {
1445 		if (drm_panel_unprepare(dp->plat_data->panel))
1446 			DRM_ERROR("failed to turnoff the panel\n");
1447 	}
1448 
1449 	return 0;
1450 }
1451 EXPORT_SYMBOL_GPL(analogix_dp_suspend);
1452 
1453 int analogix_dp_resume(struct analogix_dp_device *dp)
1454 {
1455 	int ret;
1456 
1457 	ret = clk_prepare_enable(dp->clock);
1458 	if (ret < 0) {
1459 		DRM_ERROR("Failed to prepare_enable the clock clk [%d]\n", ret);
1460 		return ret;
1461 	}
1462 
1463 	if (dp->plat_data->panel) {
1464 		if (drm_panel_prepare(dp->plat_data->panel)) {
1465 			DRM_ERROR("failed to setup the panel\n");
1466 			return -EBUSY;
1467 		}
1468 	}
1469 
1470 	return 0;
1471 }
1472 EXPORT_SYMBOL_GPL(analogix_dp_resume);
1473 #endif
1474 
1475 int analogix_dp_start_crc(struct drm_connector *connector)
1476 {
1477 	struct analogix_dp_device *dp = to_dp(connector);
1478 
1479 	if (!connector->state->crtc) {
1480 		DRM_ERROR("Connector %s doesn't currently have a CRTC.\n",
1481 			  connector->name);
1482 		return -EINVAL;
1483 	}
1484 
1485 	return drm_dp_start_crc(&dp->aux, connector->state->crtc);
1486 }
1487 EXPORT_SYMBOL_GPL(analogix_dp_start_crc);
1488 
1489 int analogix_dp_stop_crc(struct drm_connector *connector)
1490 {
1491 	struct analogix_dp_device *dp = to_dp(connector);
1492 
1493 	return drm_dp_stop_crc(&dp->aux);
1494 }
1495 EXPORT_SYMBOL_GPL(analogix_dp_stop_crc);
1496 
1497 MODULE_AUTHOR("Jingoo Han <jg1.han@samsung.com>");
1498 MODULE_DESCRIPTION("Analogix DP Core Driver");
1499 MODULE_LICENSE("GPL v2");
1500