2 * Samsung SoC DP (Display Port) interface driver.
4 * Copyright (C) 2012 Samsung Electronics Co., Ltd.
5 * Author: Jingoo Han <jg1.han@samsung.com>
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.
13 #include <linux/module.h>
14 #include <linux/platform_device.h>
15 #include <linux/err.h>
16 #include <linux/clk.h>
18 #include <linux/interrupt.h>
19 #include <linux/delay.h>
21 #include <linux/of_gpio.h>
22 #include <linux/gpio.h>
23 #include <linux/component.h>
24 #include <linux/phy/phy.h>
25 #include <video/of_display_timing.h>
26 #include <video/of_videomode.h>
29 #include <drm/drm_crtc.h>
30 #include <drm/drm_crtc_helper.h>
31 #include <drm/bridge/ptn3460.h>
33 #include "exynos_drm_drv.h"
34 #include "exynos_dp_core.h"
36 #define ctx_from_connector(c) container_of(c, struct exynos_dp_device, \
40 struct i2c_client
*client
;
41 struct device_node
*node
;
44 static int exynos_dp_init_dp(struct exynos_dp_device
*dp
)
48 exynos_dp_swreset(dp
);
50 exynos_dp_init_analog_param(dp
);
51 exynos_dp_init_interrupt(dp
);
53 /* SW defined function Normal operation */
54 exynos_dp_enable_sw_function(dp
);
56 exynos_dp_config_interrupt(dp
);
57 exynos_dp_init_analog_func(dp
);
59 exynos_dp_init_hpd(dp
);
60 exynos_dp_init_aux(dp
);
65 static int exynos_dp_detect_hpd(struct exynos_dp_device
*dp
)
69 while (exynos_dp_get_plug_in_status(dp
) != 0) {
71 if (DP_TIMEOUT_LOOP_COUNT
< timeout_loop
) {
72 dev_err(dp
->dev
, "failed to get hpd plug status\n");
81 static unsigned char exynos_dp_calc_edid_check_sum(unsigned char *edid_data
)
84 unsigned char sum
= 0;
86 for (i
= 0; i
< EDID_BLOCK_LENGTH
; i
++)
87 sum
= sum
+ edid_data
[i
];
92 static int exynos_dp_read_edid(struct exynos_dp_device
*dp
)
94 unsigned char edid
[EDID_BLOCK_LENGTH
* 2];
95 unsigned int extend_block
= 0;
97 unsigned char test_vector
;
101 * EDID device address is 0x50.
102 * However, if necessary, you must have set upper address
103 * into E-EDID in I2C device, 0x30.
106 /* Read Extension Flag, Number of 128-byte EDID extension blocks */
107 retval
= exynos_dp_read_byte_from_i2c(dp
, I2C_EDID_DEVICE_ADDR
,
113 if (extend_block
> 0) {
114 dev_dbg(dp
->dev
, "EDID data includes a single extension!\n");
117 retval
= exynos_dp_read_bytes_from_i2c(dp
, I2C_EDID_DEVICE_ADDR
,
120 &edid
[EDID_HEADER_PATTERN
]);
122 dev_err(dp
->dev
, "EDID Read failed!\n");
125 sum
= exynos_dp_calc_edid_check_sum(edid
);
127 dev_err(dp
->dev
, "EDID bad checksum!\n");
131 /* Read additional EDID data */
132 retval
= exynos_dp_read_bytes_from_i2c(dp
,
133 I2C_EDID_DEVICE_ADDR
,
136 &edid
[EDID_BLOCK_LENGTH
]);
138 dev_err(dp
->dev
, "EDID Read failed!\n");
141 sum
= exynos_dp_calc_edid_check_sum(&edid
[EDID_BLOCK_LENGTH
]);
143 dev_err(dp
->dev
, "EDID bad checksum!\n");
147 exynos_dp_read_byte_from_dpcd(dp
, DP_TEST_REQUEST
,
149 if (test_vector
& DP_TEST_LINK_EDID_READ
) {
150 exynos_dp_write_byte_to_dpcd(dp
,
151 DP_TEST_EDID_CHECKSUM
,
152 edid
[EDID_BLOCK_LENGTH
+ EDID_CHECKSUM
]);
153 exynos_dp_write_byte_to_dpcd(dp
,
155 DP_TEST_EDID_CHECKSUM_WRITE
);
158 dev_info(dp
->dev
, "EDID data does not include any extensions.\n");
161 retval
= exynos_dp_read_bytes_from_i2c(dp
,
162 I2C_EDID_DEVICE_ADDR
,
165 &edid
[EDID_HEADER_PATTERN
]);
167 dev_err(dp
->dev
, "EDID Read failed!\n");
170 sum
= exynos_dp_calc_edid_check_sum(edid
);
172 dev_err(dp
->dev
, "EDID bad checksum!\n");
176 exynos_dp_read_byte_from_dpcd(dp
,
179 if (test_vector
& DP_TEST_LINK_EDID_READ
) {
180 exynos_dp_write_byte_to_dpcd(dp
,
181 DP_TEST_EDID_CHECKSUM
,
182 edid
[EDID_CHECKSUM
]);
183 exynos_dp_write_byte_to_dpcd(dp
,
185 DP_TEST_EDID_CHECKSUM_WRITE
);
189 dev_err(dp
->dev
, "EDID Read success!\n");
193 static int exynos_dp_handle_edid(struct exynos_dp_device
*dp
)
199 /* Read DPCD DP_DPCD_REV~RECEIVE_PORT1_CAP_1 */
200 retval
= exynos_dp_read_bytes_from_dpcd(dp
, DP_DPCD_REV
,
206 for (i
= 0; i
< 3; i
++) {
207 retval
= exynos_dp_read_edid(dp
);
215 static void exynos_dp_enable_rx_to_enhanced_mode(struct exynos_dp_device
*dp
,
220 exynos_dp_read_byte_from_dpcd(dp
, DP_LANE_COUNT_SET
, &data
);
223 exynos_dp_write_byte_to_dpcd(dp
, DP_LANE_COUNT_SET
,
224 DP_LANE_COUNT_ENHANCED_FRAME_EN
|
225 DPCD_LANE_COUNT_SET(data
));
227 exynos_dp_write_byte_to_dpcd(dp
, DP_LANE_COUNT_SET
,
228 DPCD_LANE_COUNT_SET(data
));
231 static int exynos_dp_is_enhanced_mode_available(struct exynos_dp_device
*dp
)
236 exynos_dp_read_byte_from_dpcd(dp
, DP_MAX_LANE_COUNT
, &data
);
237 retval
= DPCD_ENHANCED_FRAME_CAP(data
);
242 static void exynos_dp_set_enhanced_mode(struct exynos_dp_device
*dp
)
246 data
= exynos_dp_is_enhanced_mode_available(dp
);
247 exynos_dp_enable_rx_to_enhanced_mode(dp
, data
);
248 exynos_dp_enable_enhanced_mode(dp
, data
);
251 static void exynos_dp_training_pattern_dis(struct exynos_dp_device
*dp
)
253 exynos_dp_set_training_pattern(dp
, DP_NONE
);
255 exynos_dp_write_byte_to_dpcd(dp
,
256 DP_TRAINING_PATTERN_SET
,
257 DP_TRAINING_PATTERN_DISABLE
);
260 static void exynos_dp_set_lane_lane_pre_emphasis(struct exynos_dp_device
*dp
,
261 int pre_emphasis
, int lane
)
265 exynos_dp_set_lane0_pre_emphasis(dp
, pre_emphasis
);
268 exynos_dp_set_lane1_pre_emphasis(dp
, pre_emphasis
);
272 exynos_dp_set_lane2_pre_emphasis(dp
, pre_emphasis
);
276 exynos_dp_set_lane3_pre_emphasis(dp
, pre_emphasis
);
281 static int exynos_dp_link_start(struct exynos_dp_device
*dp
)
284 int lane
, lane_count
, pll_tries
, retval
;
286 lane_count
= dp
->link_train
.lane_count
;
288 dp
->link_train
.lt_state
= CLOCK_RECOVERY
;
289 dp
->link_train
.eq_loop
= 0;
291 for (lane
= 0; lane
< lane_count
; lane
++)
292 dp
->link_train
.cr_loop
[lane
] = 0;
294 /* Set link rate and count as you want to establish*/
295 exynos_dp_set_link_bandwidth(dp
, dp
->link_train
.link_rate
);
296 exynos_dp_set_lane_count(dp
, dp
->link_train
.lane_count
);
298 /* Setup RX configuration */
299 buf
[0] = dp
->link_train
.link_rate
;
300 buf
[1] = dp
->link_train
.lane_count
;
301 retval
= exynos_dp_write_bytes_to_dpcd(dp
, DP_LINK_BW_SET
,
306 /* Set TX pre-emphasis to minimum */
307 for (lane
= 0; lane
< lane_count
; lane
++)
308 exynos_dp_set_lane_lane_pre_emphasis(dp
,
309 PRE_EMPHASIS_LEVEL_0
, lane
);
311 /* Wait for PLL lock */
313 while (exynos_dp_get_pll_lock_status(dp
) == PLL_UNLOCKED
) {
314 if (pll_tries
== DP_TIMEOUT_LOOP_COUNT
) {
315 dev_err(dp
->dev
, "Wait for PLL lock timed out\n");
320 usleep_range(90, 120);
323 /* Set training pattern 1 */
324 exynos_dp_set_training_pattern(dp
, TRAINING_PTN1
);
326 /* Set RX training pattern */
327 retval
= exynos_dp_write_byte_to_dpcd(dp
,
328 DP_TRAINING_PATTERN_SET
,
329 DP_LINK_SCRAMBLING_DISABLE
| DP_TRAINING_PATTERN_1
);
333 for (lane
= 0; lane
< lane_count
; lane
++)
334 buf
[lane
] = DP_TRAIN_PRE_EMPHASIS_0
|
335 DP_TRAIN_VOLTAGE_SWING_400
;
337 retval
= exynos_dp_write_bytes_to_dpcd(dp
, DP_TRAINING_LANE0_SET
,
343 static unsigned char exynos_dp_get_lane_status(u8 link_status
[2], int lane
)
345 int shift
= (lane
& 1) * 4;
346 u8 link_value
= link_status
[lane
>>1];
348 return (link_value
>> shift
) & 0xf;
351 static int exynos_dp_clock_recovery_ok(u8 link_status
[2], int lane_count
)
356 for (lane
= 0; lane
< lane_count
; lane
++) {
357 lane_status
= exynos_dp_get_lane_status(link_status
, lane
);
358 if ((lane_status
& DP_LANE_CR_DONE
) == 0)
364 static int exynos_dp_channel_eq_ok(u8 link_status
[2], u8 link_align
,
370 if ((link_align
& DP_INTERLANE_ALIGN_DONE
) == 0)
373 for (lane
= 0; lane
< lane_count
; lane
++) {
374 lane_status
= exynos_dp_get_lane_status(link_status
, lane
);
375 lane_status
&= DP_CHANNEL_EQ_BITS
;
376 if (lane_status
!= DP_CHANNEL_EQ_BITS
)
383 static unsigned char exynos_dp_get_adjust_request_voltage(u8 adjust_request
[2],
386 int shift
= (lane
& 1) * 4;
387 u8 link_value
= adjust_request
[lane
>>1];
389 return (link_value
>> shift
) & 0x3;
392 static unsigned char exynos_dp_get_adjust_request_pre_emphasis(
393 u8 adjust_request
[2],
396 int shift
= (lane
& 1) * 4;
397 u8 link_value
= adjust_request
[lane
>>1];
399 return ((link_value
>> shift
) & 0xc) >> 2;
402 static void exynos_dp_set_lane_link_training(struct exynos_dp_device
*dp
,
403 u8 training_lane_set
, int lane
)
407 exynos_dp_set_lane0_link_training(dp
, training_lane_set
);
410 exynos_dp_set_lane1_link_training(dp
, training_lane_set
);
414 exynos_dp_set_lane2_link_training(dp
, training_lane_set
);
418 exynos_dp_set_lane3_link_training(dp
, training_lane_set
);
423 static unsigned int exynos_dp_get_lane_link_training(
424 struct exynos_dp_device
*dp
,
431 reg
= exynos_dp_get_lane0_link_training(dp
);
434 reg
= exynos_dp_get_lane1_link_training(dp
);
437 reg
= exynos_dp_get_lane2_link_training(dp
);
440 reg
= exynos_dp_get_lane3_link_training(dp
);
450 static void exynos_dp_reduce_link_rate(struct exynos_dp_device
*dp
)
452 exynos_dp_training_pattern_dis(dp
);
453 exynos_dp_set_enhanced_mode(dp
);
455 dp
->link_train
.lt_state
= FAILED
;
458 static void exynos_dp_get_adjust_training_lane(struct exynos_dp_device
*dp
,
459 u8 adjust_request
[2])
461 int lane
, lane_count
;
462 u8 voltage_swing
, pre_emphasis
, training_lane
;
464 lane_count
= dp
->link_train
.lane_count
;
465 for (lane
= 0; lane
< lane_count
; lane
++) {
466 voltage_swing
= exynos_dp_get_adjust_request_voltage(
467 adjust_request
, lane
);
468 pre_emphasis
= exynos_dp_get_adjust_request_pre_emphasis(
469 adjust_request
, lane
);
470 training_lane
= DPCD_VOLTAGE_SWING_SET(voltage_swing
) |
471 DPCD_PRE_EMPHASIS_SET(pre_emphasis
);
473 if (voltage_swing
== VOLTAGE_LEVEL_3
)
474 training_lane
|= DP_TRAIN_MAX_SWING_REACHED
;
475 if (pre_emphasis
== PRE_EMPHASIS_LEVEL_3
)
476 training_lane
|= DP_TRAIN_MAX_PRE_EMPHASIS_REACHED
;
478 dp
->link_train
.training_lane
[lane
] = training_lane
;
482 static int exynos_dp_process_clock_recovery(struct exynos_dp_device
*dp
)
484 int lane
, lane_count
, retval
;
485 u8 voltage_swing
, pre_emphasis
, training_lane
;
486 u8 link_status
[2], adjust_request
[2];
488 usleep_range(100, 101);
490 lane_count
= dp
->link_train
.lane_count
;
492 retval
= exynos_dp_read_bytes_from_dpcd(dp
,
493 DP_LANE0_1_STATUS
, 2, link_status
);
497 retval
= exynos_dp_read_bytes_from_dpcd(dp
,
498 DP_ADJUST_REQUEST_LANE0_1
, 2, adjust_request
);
502 if (exynos_dp_clock_recovery_ok(link_status
, lane_count
) == 0) {
503 /* set training pattern 2 for EQ */
504 exynos_dp_set_training_pattern(dp
, TRAINING_PTN2
);
506 retval
= exynos_dp_write_byte_to_dpcd(dp
,
507 DP_TRAINING_PATTERN_SET
,
508 DP_LINK_SCRAMBLING_DISABLE
|
509 DP_TRAINING_PATTERN_2
);
513 dev_info(dp
->dev
, "Link Training Clock Recovery success\n");
514 dp
->link_train
.lt_state
= EQUALIZER_TRAINING
;
516 for (lane
= 0; lane
< lane_count
; lane
++) {
517 training_lane
= exynos_dp_get_lane_link_training(
519 voltage_swing
= exynos_dp_get_adjust_request_voltage(
520 adjust_request
, lane
);
521 pre_emphasis
= exynos_dp_get_adjust_request_pre_emphasis(
522 adjust_request
, lane
);
524 if (DPCD_VOLTAGE_SWING_GET(training_lane
) ==
526 DPCD_PRE_EMPHASIS_GET(training_lane
) ==
528 dp
->link_train
.cr_loop
[lane
]++;
530 if (dp
->link_train
.cr_loop
[lane
] == MAX_CR_LOOP
||
531 voltage_swing
== VOLTAGE_LEVEL_3
||
532 pre_emphasis
== PRE_EMPHASIS_LEVEL_3
) {
533 dev_err(dp
->dev
, "CR Max reached (%d,%d,%d)\n",
534 dp
->link_train
.cr_loop
[lane
],
535 voltage_swing
, pre_emphasis
);
536 exynos_dp_reduce_link_rate(dp
);
542 exynos_dp_get_adjust_training_lane(dp
, adjust_request
);
544 for (lane
= 0; lane
< lane_count
; lane
++)
545 exynos_dp_set_lane_link_training(dp
,
546 dp
->link_train
.training_lane
[lane
], lane
);
548 retval
= exynos_dp_write_bytes_to_dpcd(dp
,
549 DP_TRAINING_LANE0_SET
, lane_count
,
550 dp
->link_train
.training_lane
);
557 static int exynos_dp_process_equalizer_training(struct exynos_dp_device
*dp
)
559 int lane
, lane_count
, retval
;
561 u8 link_align
, link_status
[2], adjust_request
[2];
563 usleep_range(400, 401);
565 lane_count
= dp
->link_train
.lane_count
;
567 retval
= exynos_dp_read_bytes_from_dpcd(dp
,
568 DP_LANE0_1_STATUS
, 2, link_status
);
572 if (exynos_dp_clock_recovery_ok(link_status
, lane_count
)) {
573 exynos_dp_reduce_link_rate(dp
);
577 retval
= exynos_dp_read_bytes_from_dpcd(dp
,
578 DP_ADJUST_REQUEST_LANE0_1
, 2, adjust_request
);
582 retval
= exynos_dp_read_byte_from_dpcd(dp
,
583 DP_LANE_ALIGN_STATUS_UPDATED
, &link_align
);
587 exynos_dp_get_adjust_training_lane(dp
, adjust_request
);
589 if (!exynos_dp_channel_eq_ok(link_status
, link_align
, lane_count
)) {
590 /* traing pattern Set to Normal */
591 exynos_dp_training_pattern_dis(dp
);
593 dev_info(dp
->dev
, "Link Training success!\n");
595 exynos_dp_get_link_bandwidth(dp
, ®
);
596 dp
->link_train
.link_rate
= reg
;
597 dev_dbg(dp
->dev
, "final bandwidth = %.2x\n",
598 dp
->link_train
.link_rate
);
600 exynos_dp_get_lane_count(dp
, ®
);
601 dp
->link_train
.lane_count
= reg
;
602 dev_dbg(dp
->dev
, "final lane count = %.2x\n",
603 dp
->link_train
.lane_count
);
605 /* set enhanced mode if available */
606 exynos_dp_set_enhanced_mode(dp
);
607 dp
->link_train
.lt_state
= FINISHED
;
613 dp
->link_train
.eq_loop
++;
615 if (dp
->link_train
.eq_loop
> MAX_EQ_LOOP
) {
616 dev_err(dp
->dev
, "EQ Max loop\n");
617 exynos_dp_reduce_link_rate(dp
);
621 for (lane
= 0; lane
< lane_count
; lane
++)
622 exynos_dp_set_lane_link_training(dp
,
623 dp
->link_train
.training_lane
[lane
], lane
);
625 retval
= exynos_dp_write_bytes_to_dpcd(dp
, DP_TRAINING_LANE0_SET
,
626 lane_count
, dp
->link_train
.training_lane
);
631 static void exynos_dp_get_max_rx_bandwidth(struct exynos_dp_device
*dp
,
637 * For DP rev.1.1, Maximum link rate of Main Link lanes
638 * 0x06 = 1.62 Gbps, 0x0a = 2.7 Gbps
640 exynos_dp_read_byte_from_dpcd(dp
, DP_MAX_LINK_RATE
, &data
);
644 static void exynos_dp_get_max_rx_lane_count(struct exynos_dp_device
*dp
,
650 * For DP rev.1.1, Maximum number of Main Link lanes
651 * 0x01 = 1 lane, 0x02 = 2 lanes, 0x04 = 4 lanes
653 exynos_dp_read_byte_from_dpcd(dp
, DP_MAX_LANE_COUNT
, &data
);
654 *lane_count
= DPCD_MAX_LANE_COUNT(data
);
657 static void exynos_dp_init_training(struct exynos_dp_device
*dp
,
658 enum link_lane_count_type max_lane
,
659 enum link_rate_type max_rate
)
662 * MACRO_RST must be applied after the PLL_LOCK to avoid
663 * the DP inter pair skew issue for at least 10 us
665 exynos_dp_reset_macro(dp
);
667 /* Initialize by reading RX's DPCD */
668 exynos_dp_get_max_rx_bandwidth(dp
, &dp
->link_train
.link_rate
);
669 exynos_dp_get_max_rx_lane_count(dp
, &dp
->link_train
.lane_count
);
671 if ((dp
->link_train
.link_rate
!= LINK_RATE_1_62GBPS
) &&
672 (dp
->link_train
.link_rate
!= LINK_RATE_2_70GBPS
)) {
673 dev_err(dp
->dev
, "Rx Max Link Rate is abnormal :%x !\n",
674 dp
->link_train
.link_rate
);
675 dp
->link_train
.link_rate
= LINK_RATE_1_62GBPS
;
678 if (dp
->link_train
.lane_count
== 0) {
679 dev_err(dp
->dev
, "Rx Max Lane count is abnormal :%x !\n",
680 dp
->link_train
.lane_count
);
681 dp
->link_train
.lane_count
= (u8
)LANE_COUNT1
;
684 /* Setup TX lane count & rate */
685 if (dp
->link_train
.lane_count
> max_lane
)
686 dp
->link_train
.lane_count
= max_lane
;
687 if (dp
->link_train
.link_rate
> max_rate
)
688 dp
->link_train
.link_rate
= max_rate
;
690 /* All DP analog module power up */
691 exynos_dp_set_analog_power_down(dp
, POWER_ALL
, 0);
694 static int exynos_dp_sw_link_training(struct exynos_dp_device
*dp
)
696 int retval
= 0, training_finished
= 0;
698 dp
->link_train
.lt_state
= START
;
701 while (!retval
&& !training_finished
) {
702 switch (dp
->link_train
.lt_state
) {
704 retval
= exynos_dp_link_start(dp
);
706 dev_err(dp
->dev
, "LT link start failed!\n");
709 retval
= exynos_dp_process_clock_recovery(dp
);
711 dev_err(dp
->dev
, "LT CR failed!\n");
713 case EQUALIZER_TRAINING
:
714 retval
= exynos_dp_process_equalizer_training(dp
);
716 dev_err(dp
->dev
, "LT EQ failed!\n");
719 training_finished
= 1;
726 dev_err(dp
->dev
, "eDP link training failed (%d)\n", retval
);
731 static int exynos_dp_set_link_train(struct exynos_dp_device
*dp
,
738 for (i
= 0; i
< DP_TIMEOUT_LOOP_COUNT
; i
++) {
739 exynos_dp_init_training(dp
, count
, bwtype
);
740 retval
= exynos_dp_sw_link_training(dp
);
744 usleep_range(100, 110);
750 static int exynos_dp_config_video(struct exynos_dp_device
*dp
)
753 int timeout_loop
= 0;
756 exynos_dp_config_video_slave_mode(dp
);
758 exynos_dp_set_video_color_format(dp
);
760 if (exynos_dp_get_pll_lock_status(dp
) == PLL_UNLOCKED
) {
761 dev_err(dp
->dev
, "PLL is not locked yet.\n");
767 if (exynos_dp_is_slave_video_stream_clock_on(dp
) == 0)
769 if (DP_TIMEOUT_LOOP_COUNT
< timeout_loop
) {
770 dev_err(dp
->dev
, "Timeout of video streamclk ok\n");
777 /* Set to use the register calculated M/N video */
778 exynos_dp_set_video_cr_mn(dp
, CALCULATED_M
, 0, 0);
780 /* For video bist, Video timing must be generated by register */
781 exynos_dp_set_video_timing_mode(dp
, VIDEO_TIMING_FROM_CAPTURE
);
783 /* Disable video mute */
784 exynos_dp_enable_video_mute(dp
, 0);
786 /* Configure video slave mode */
787 exynos_dp_enable_video_master(dp
, 0);
790 exynos_dp_start_video(dp
);
796 if (exynos_dp_is_video_stream_on(dp
) == 0) {
800 } else if (done_count
) {
803 if (DP_TIMEOUT_LOOP_COUNT
< timeout_loop
) {
804 dev_err(dp
->dev
, "Timeout of video streamclk ok\n");
808 usleep_range(1000, 1001);
812 dev_err(dp
->dev
, "Video stream is not detected!\n");
817 static void exynos_dp_enable_scramble(struct exynos_dp_device
*dp
, bool enable
)
822 exynos_dp_enable_scrambling(dp
);
824 exynos_dp_read_byte_from_dpcd(dp
,
825 DP_TRAINING_PATTERN_SET
,
827 exynos_dp_write_byte_to_dpcd(dp
,
828 DP_TRAINING_PATTERN_SET
,
829 (u8
)(data
& ~DP_LINK_SCRAMBLING_DISABLE
));
831 exynos_dp_disable_scrambling(dp
);
833 exynos_dp_read_byte_from_dpcd(dp
,
834 DP_TRAINING_PATTERN_SET
,
836 exynos_dp_write_byte_to_dpcd(dp
,
837 DP_TRAINING_PATTERN_SET
,
838 (u8
)(data
| DP_LINK_SCRAMBLING_DISABLE
));
842 static irqreturn_t
exynos_dp_irq_handler(int irq
, void *arg
)
844 struct exynos_dp_device
*dp
= arg
;
846 enum dp_irq_type irq_type
;
848 irq_type
= exynos_dp_get_irq_type(dp
);
850 case DP_IRQ_TYPE_HP_CABLE_IN
:
851 dev_dbg(dp
->dev
, "Received irq - cable in\n");
852 schedule_work(&dp
->hotplug_work
);
853 exynos_dp_clear_hotplug_interrupts(dp
);
855 case DP_IRQ_TYPE_HP_CABLE_OUT
:
856 dev_dbg(dp
->dev
, "Received irq - cable out\n");
857 exynos_dp_clear_hotplug_interrupts(dp
);
859 case DP_IRQ_TYPE_HP_CHANGE
:
861 * We get these change notifications once in a while, but there
862 * is nothing we can do with them. Just ignore it for now and
863 * only handle cable changes.
865 dev_dbg(dp
->dev
, "Received irq - hotplug change; ignoring.\n");
866 exynos_dp_clear_hotplug_interrupts(dp
);
869 dev_err(dp
->dev
, "Received irq - unknown type!\n");
875 static void exynos_dp_hotplug(struct work_struct
*work
)
877 struct exynos_dp_device
*dp
;
880 dp
= container_of(work
, struct exynos_dp_device
, hotplug_work
);
882 ret
= exynos_dp_detect_hpd(dp
);
884 /* Cable has been disconnected, we're done */
888 ret
= exynos_dp_handle_edid(dp
);
890 dev_err(dp
->dev
, "unable to handle edid\n");
894 ret
= exynos_dp_set_link_train(dp
, dp
->video_info
->lane_count
,
895 dp
->video_info
->link_rate
);
897 dev_err(dp
->dev
, "unable to do link train\n");
901 exynos_dp_enable_scramble(dp
, 1);
902 exynos_dp_enable_rx_to_enhanced_mode(dp
, 1);
903 exynos_dp_enable_enhanced_mode(dp
, 1);
905 exynos_dp_set_lane_count(dp
, dp
->video_info
->lane_count
);
906 exynos_dp_set_link_bandwidth(dp
, dp
->video_info
->link_rate
);
908 exynos_dp_init_video(dp
);
909 ret
= exynos_dp_config_video(dp
);
911 dev_err(dp
->dev
, "unable to config video\n");
914 static enum drm_connector_status
exynos_dp_detect(
915 struct drm_connector
*connector
, bool force
)
917 return connector_status_connected
;
920 static void exynos_dp_connector_destroy(struct drm_connector
*connector
)
924 static struct drm_connector_funcs exynos_dp_connector_funcs
= {
925 .dpms
= drm_helper_connector_dpms
,
926 .fill_modes
= drm_helper_probe_single_connector_modes
,
927 .detect
= exynos_dp_detect
,
928 .destroy
= exynos_dp_connector_destroy
,
931 static int exynos_dp_get_modes(struct drm_connector
*connector
)
933 struct exynos_dp_device
*dp
= ctx_from_connector(connector
);
934 struct drm_display_mode
*mode
;
936 mode
= drm_mode_create(connector
->dev
);
938 DRM_ERROR("failed to create a new display mode.\n");
942 drm_display_mode_from_videomode(&dp
->panel
.vm
, mode
);
943 mode
->width_mm
= dp
->panel
.width_mm
;
944 mode
->height_mm
= dp
->panel
.height_mm
;
945 connector
->display_info
.width_mm
= mode
->width_mm
;
946 connector
->display_info
.height_mm
= mode
->height_mm
;
948 mode
->type
= DRM_MODE_TYPE_DRIVER
| DRM_MODE_TYPE_PREFERRED
;
949 drm_mode_set_name(mode
);
950 drm_mode_probed_add(connector
, mode
);
955 static struct drm_encoder
*exynos_dp_best_encoder(
956 struct drm_connector
*connector
)
958 struct exynos_dp_device
*dp
= ctx_from_connector(connector
);
963 static struct drm_connector_helper_funcs exynos_dp_connector_helper_funcs
= {
964 .get_modes
= exynos_dp_get_modes
,
965 .best_encoder
= exynos_dp_best_encoder
,
968 static bool find_bridge(const char *compat
, struct bridge_init
*bridge
)
970 bridge
->client
= NULL
;
971 bridge
->node
= of_find_compatible_node(NULL
, NULL
, compat
);
975 bridge
->client
= of_find_i2c_device_by_node(bridge
->node
);
982 /* returns the number of bridges attached */
983 static int exynos_drm_attach_lcd_bridge(struct drm_device
*dev
,
984 struct drm_encoder
*encoder
)
986 struct bridge_init bridge
;
989 if (find_bridge("nxp,ptn3460", &bridge
)) {
990 ret
= ptn3460_init(dev
, encoder
, bridge
.client
, bridge
.node
);
997 static int exynos_dp_create_connector(struct exynos_drm_display
*display
,
998 struct drm_encoder
*encoder
)
1000 struct exynos_dp_device
*dp
= display
->ctx
;
1001 struct drm_connector
*connector
= &dp
->connector
;
1004 dp
->encoder
= encoder
;
1006 /* Pre-empt DP connector creation if there's a bridge */
1007 ret
= exynos_drm_attach_lcd_bridge(dp
->drm_dev
, encoder
);
1011 connector
->polled
= DRM_CONNECTOR_POLL_HPD
;
1013 ret
= drm_connector_init(dp
->drm_dev
, connector
,
1014 &exynos_dp_connector_funcs
, DRM_MODE_CONNECTOR_eDP
);
1016 DRM_ERROR("Failed to initialize connector with drm\n");
1020 drm_connector_helper_add(connector
, &exynos_dp_connector_helper_funcs
);
1021 drm_connector_register(connector
);
1022 drm_mode_connector_attach_encoder(connector
, encoder
);
1027 static void exynos_dp_phy_init(struct exynos_dp_device
*dp
)
1030 phy_power_on(dp
->phy
);
1031 } else if (dp
->phy_addr
) {
1034 reg
= __raw_readl(dp
->phy_addr
);
1035 reg
|= dp
->enable_mask
;
1036 __raw_writel(reg
, dp
->phy_addr
);
1040 static void exynos_dp_phy_exit(struct exynos_dp_device
*dp
)
1043 phy_power_off(dp
->phy
);
1044 } else if (dp
->phy_addr
) {
1047 reg
= __raw_readl(dp
->phy_addr
);
1048 reg
&= ~(dp
->enable_mask
);
1049 __raw_writel(reg
, dp
->phy_addr
);
1053 static void exynos_dp_poweron(struct exynos_dp_device
*dp
)
1055 if (dp
->dpms_mode
== DRM_MODE_DPMS_ON
)
1058 clk_prepare_enable(dp
->clock
);
1059 exynos_dp_phy_init(dp
);
1060 exynos_dp_init_dp(dp
);
1061 enable_irq(dp
->irq
);
1064 static void exynos_dp_poweroff(struct exynos_dp_device
*dp
)
1066 if (dp
->dpms_mode
!= DRM_MODE_DPMS_ON
)
1069 disable_irq(dp
->irq
);
1070 flush_work(&dp
->hotplug_work
);
1071 exynos_dp_phy_exit(dp
);
1072 clk_disable_unprepare(dp
->clock
);
1075 static void exynos_dp_dpms(struct exynos_drm_display
*display
, int mode
)
1077 struct exynos_dp_device
*dp
= display
->ctx
;
1080 case DRM_MODE_DPMS_ON
:
1081 exynos_dp_poweron(dp
);
1083 case DRM_MODE_DPMS_STANDBY
:
1084 case DRM_MODE_DPMS_SUSPEND
:
1085 case DRM_MODE_DPMS_OFF
:
1086 exynos_dp_poweroff(dp
);
1091 dp
->dpms_mode
= mode
;
1094 static struct exynos_drm_display_ops exynos_dp_display_ops
= {
1095 .create_connector
= exynos_dp_create_connector
,
1096 .dpms
= exynos_dp_dpms
,
1099 static struct exynos_drm_display exynos_dp_display
= {
1100 .type
= EXYNOS_DISPLAY_TYPE_LCD
,
1101 .ops
= &exynos_dp_display_ops
,
1104 static struct video_info
*exynos_dp_dt_parse_pdata(struct device
*dev
)
1106 struct device_node
*dp_node
= dev
->of_node
;
1107 struct video_info
*dp_video_config
;
1109 dp_video_config
= devm_kzalloc(dev
,
1110 sizeof(*dp_video_config
), GFP_KERNEL
);
1111 if (!dp_video_config
)
1112 return ERR_PTR(-ENOMEM
);
1114 dp_video_config
->h_sync_polarity
=
1115 of_property_read_bool(dp_node
, "hsync-active-high");
1117 dp_video_config
->v_sync_polarity
=
1118 of_property_read_bool(dp_node
, "vsync-active-high");
1120 dp_video_config
->interlaced
=
1121 of_property_read_bool(dp_node
, "interlaced");
1123 if (of_property_read_u32(dp_node
, "samsung,color-space",
1124 &dp_video_config
->color_space
)) {
1125 dev_err(dev
, "failed to get color-space\n");
1126 return ERR_PTR(-EINVAL
);
1129 if (of_property_read_u32(dp_node
, "samsung,dynamic-range",
1130 &dp_video_config
->dynamic_range
)) {
1131 dev_err(dev
, "failed to get dynamic-range\n");
1132 return ERR_PTR(-EINVAL
);
1135 if (of_property_read_u32(dp_node
, "samsung,ycbcr-coeff",
1136 &dp_video_config
->ycbcr_coeff
)) {
1137 dev_err(dev
, "failed to get ycbcr-coeff\n");
1138 return ERR_PTR(-EINVAL
);
1141 if (of_property_read_u32(dp_node
, "samsung,color-depth",
1142 &dp_video_config
->color_depth
)) {
1143 dev_err(dev
, "failed to get color-depth\n");
1144 return ERR_PTR(-EINVAL
);
1147 if (of_property_read_u32(dp_node
, "samsung,link-rate",
1148 &dp_video_config
->link_rate
)) {
1149 dev_err(dev
, "failed to get link-rate\n");
1150 return ERR_PTR(-EINVAL
);
1153 if (of_property_read_u32(dp_node
, "samsung,lane-count",
1154 &dp_video_config
->lane_count
)) {
1155 dev_err(dev
, "failed to get lane-count\n");
1156 return ERR_PTR(-EINVAL
);
1159 return dp_video_config
;
1162 static int exynos_dp_dt_parse_phydata(struct exynos_dp_device
*dp
)
1164 struct device_node
*dp_phy_node
= of_node_get(dp
->dev
->of_node
);
1168 dp_phy_node
= of_find_node_by_name(dp_phy_node
, "dptx-phy");
1170 dp
->phy
= devm_phy_get(dp
->dev
, "dp");
1171 return PTR_ERR_OR_ZERO(dp
->phy
);
1174 if (of_property_read_u32(dp_phy_node
, "reg", &phy_base
)) {
1175 dev_err(dp
->dev
, "failed to get reg for dptx-phy\n");
1180 if (of_property_read_u32(dp_phy_node
, "samsung,enable-mask",
1181 &dp
->enable_mask
)) {
1182 dev_err(dp
->dev
, "failed to get enable-mask for dptx-phy\n");
1187 dp
->phy_addr
= ioremap(phy_base
, SZ_4
);
1188 if (!dp
->phy_addr
) {
1189 dev_err(dp
->dev
, "failed to ioremap dp-phy\n");
1195 of_node_put(dp_phy_node
);
1200 static int exynos_dp_dt_parse_panel(struct exynos_dp_device
*dp
)
1204 ret
= of_get_videomode(dp
->dev
->of_node
, &dp
->panel
.vm
,
1205 OF_USE_NATIVE_MODE
);
1207 DRM_ERROR("failed: of_get_videomode() : %d\n", ret
);
1213 static int exynos_dp_bind(struct device
*dev
, struct device
*master
, void *data
)
1215 struct platform_device
*pdev
= to_platform_device(dev
);
1216 struct drm_device
*drm_dev
= data
;
1217 struct resource
*res
;
1218 struct exynos_dp_device
*dp
;
1219 unsigned int irq_flags
;
1223 dp
= devm_kzalloc(&pdev
->dev
, sizeof(struct exynos_dp_device
),
1228 dp
->dev
= &pdev
->dev
;
1229 dp
->dpms_mode
= DRM_MODE_DPMS_OFF
;
1231 dp
->video_info
= exynos_dp_dt_parse_pdata(&pdev
->dev
);
1232 if (IS_ERR(dp
->video_info
))
1233 return PTR_ERR(dp
->video_info
);
1235 ret
= exynos_dp_dt_parse_phydata(dp
);
1239 ret
= exynos_dp_dt_parse_panel(dp
);
1243 dp
->clock
= devm_clk_get(&pdev
->dev
, "dp");
1244 if (IS_ERR(dp
->clock
)) {
1245 dev_err(&pdev
->dev
, "failed to get clock\n");
1246 return PTR_ERR(dp
->clock
);
1249 clk_prepare_enable(dp
->clock
);
1251 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1253 dp
->reg_base
= devm_ioremap_resource(&pdev
->dev
, res
);
1254 if (IS_ERR(dp
->reg_base
))
1255 return PTR_ERR(dp
->reg_base
);
1257 dp
->hpd_gpio
= of_get_named_gpio(dev
->of_node
, "samsung,hpd-gpio", 0);
1259 if (gpio_is_valid(dp
->hpd_gpio
)) {
1261 * Set up the hotplug GPIO from the device tree as an interrupt.
1262 * Simply specifying a different interrupt in the device tree
1263 * doesn't work since we handle hotplug rather differently when
1264 * using a GPIO. We also need the actual GPIO specifier so
1265 * that we can get the current state of the GPIO.
1267 ret
= devm_gpio_request_one(&pdev
->dev
, dp
->hpd_gpio
, GPIOF_IN
,
1270 dev_err(&pdev
->dev
, "failed to get hpd gpio\n");
1273 dp
->irq
= gpio_to_irq(dp
->hpd_gpio
);
1274 irq_flags
= IRQF_TRIGGER_RISING
| IRQF_TRIGGER_FALLING
;
1276 dp
->hpd_gpio
= -ENODEV
;
1277 dp
->irq
= platform_get_irq(pdev
, 0);
1281 if (dp
->irq
== -ENXIO
) {
1282 dev_err(&pdev
->dev
, "failed to get irq\n");
1286 INIT_WORK(&dp
->hotplug_work
, exynos_dp_hotplug
);
1288 exynos_dp_phy_init(dp
);
1290 exynos_dp_init_dp(dp
);
1292 ret
= devm_request_irq(&pdev
->dev
, dp
->irq
, exynos_dp_irq_handler
,
1293 irq_flags
, "exynos-dp", dp
);
1295 dev_err(&pdev
->dev
, "failed to request irq\n");
1298 disable_irq(dp
->irq
);
1300 dp
->drm_dev
= drm_dev
;
1301 exynos_dp_display
.ctx
= dp
;
1303 platform_set_drvdata(pdev
, &exynos_dp_display
);
1305 return exynos_drm_create_enc_conn(drm_dev
, &exynos_dp_display
);
1308 static void exynos_dp_unbind(struct device
*dev
, struct device
*master
,
1311 struct exynos_drm_display
*display
= dev_get_drvdata(dev
);
1312 struct exynos_dp_device
*dp
= display
->ctx
;
1313 struct drm_encoder
*encoder
= dp
->encoder
;
1315 exynos_dp_dpms(display
, DRM_MODE_DPMS_OFF
);
1317 encoder
->funcs
->destroy(encoder
);
1318 drm_connector_cleanup(&dp
->connector
);
1321 static const struct component_ops exynos_dp_ops
= {
1322 .bind
= exynos_dp_bind
,
1323 .unbind
= exynos_dp_unbind
,
1326 static int exynos_dp_probe(struct platform_device
*pdev
)
1330 ret
= exynos_drm_component_add(&pdev
->dev
, EXYNOS_DEVICE_TYPE_CONNECTOR
,
1331 exynos_dp_display
.type
);
1335 ret
= component_add(&pdev
->dev
, &exynos_dp_ops
);
1337 exynos_drm_component_del(&pdev
->dev
,
1338 EXYNOS_DEVICE_TYPE_CONNECTOR
);
1343 static int exynos_dp_remove(struct platform_device
*pdev
)
1345 component_del(&pdev
->dev
, &exynos_dp_ops
);
1346 exynos_drm_component_del(&pdev
->dev
, EXYNOS_DEVICE_TYPE_CONNECTOR
);
1351 #ifdef CONFIG_PM_SLEEP
1352 static int exynos_dp_suspend(struct device
*dev
)
1354 struct platform_device
*pdev
= to_platform_device(dev
);
1355 struct exynos_drm_display
*display
= platform_get_drvdata(pdev
);
1357 exynos_dp_dpms(display
, DRM_MODE_DPMS_OFF
);
1361 static int exynos_dp_resume(struct device
*dev
)
1363 struct platform_device
*pdev
= to_platform_device(dev
);
1364 struct exynos_drm_display
*display
= platform_get_drvdata(pdev
);
1366 exynos_dp_dpms(display
, DRM_MODE_DPMS_ON
);
1371 static const struct dev_pm_ops exynos_dp_pm_ops
= {
1372 SET_SYSTEM_SLEEP_PM_OPS(exynos_dp_suspend
, exynos_dp_resume
)
1375 static const struct of_device_id exynos_dp_match
[] = {
1376 { .compatible
= "samsung,exynos5-dp" },
1380 struct platform_driver dp_driver
= {
1381 .probe
= exynos_dp_probe
,
1382 .remove
= exynos_dp_remove
,
1384 .name
= "exynos-dp",
1385 .owner
= THIS_MODULE
,
1386 .pm
= &exynos_dp_pm_ops
,
1387 .of_match_table
= exynos_dp_match
,
1391 MODULE_AUTHOR("Jingoo Han <jg1.han@samsung.com>");
1392 MODULE_DESCRIPTION("Samsung SoC DP Driver");
1393 MODULE_LICENSE("GPL");