sysctl: range checking in do_proc_dointvec_ms_jiffies_conv
[linux/fpc-iii.git] / drivers / video / exynos / exynos_dp_core.c
blob12bbede3b091e68ae0600b7fb55c765052d2f66d
1 /*
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/slab.h>
16 #include <linux/err.h>
17 #include <linux/clk.h>
18 #include <linux/io.h>
19 #include <linux/interrupt.h>
20 #include <linux/delay.h>
21 #include <linux/of.h>
23 #include <video/exynos_dp.h>
25 #include "exynos_dp_core.h"
27 static int exynos_dp_init_dp(struct exynos_dp_device *dp)
29 exynos_dp_reset(dp);
31 exynos_dp_swreset(dp);
33 exynos_dp_init_analog_param(dp);
34 exynos_dp_init_interrupt(dp);
36 /* SW defined function Normal operation */
37 exynos_dp_enable_sw_function(dp);
39 exynos_dp_config_interrupt(dp);
40 exynos_dp_init_analog_func(dp);
42 exynos_dp_init_hpd(dp);
43 exynos_dp_init_aux(dp);
45 return 0;
48 static int exynos_dp_detect_hpd(struct exynos_dp_device *dp)
50 int timeout_loop = 0;
52 while (exynos_dp_get_plug_in_status(dp) != 0) {
53 timeout_loop++;
54 if (DP_TIMEOUT_LOOP_COUNT < timeout_loop) {
55 dev_err(dp->dev, "failed to get hpd plug status\n");
56 return -ETIMEDOUT;
58 usleep_range(10, 11);
61 return 0;
64 static unsigned char exynos_dp_calc_edid_check_sum(unsigned char *edid_data)
66 int i;
67 unsigned char sum = 0;
69 for (i = 0; i < EDID_BLOCK_LENGTH; i++)
70 sum = sum + edid_data[i];
72 return sum;
75 static int exynos_dp_read_edid(struct exynos_dp_device *dp)
77 unsigned char edid[EDID_BLOCK_LENGTH * 2];
78 unsigned int extend_block = 0;
79 unsigned char sum;
80 unsigned char test_vector;
81 int retval;
84 * EDID device address is 0x50.
85 * However, if necessary, you must have set upper address
86 * into E-EDID in I2C device, 0x30.
89 /* Read Extension Flag, Number of 128-byte EDID extension blocks */
90 retval = exynos_dp_read_byte_from_i2c(dp, I2C_EDID_DEVICE_ADDR,
91 EDID_EXTENSION_FLAG,
92 &extend_block);
93 if (retval)
94 return retval;
96 if (extend_block > 0) {
97 dev_dbg(dp->dev, "EDID data includes a single extension!\n");
99 /* Read EDID data */
100 retval = exynos_dp_read_bytes_from_i2c(dp, I2C_EDID_DEVICE_ADDR,
101 EDID_HEADER_PATTERN,
102 EDID_BLOCK_LENGTH,
103 &edid[EDID_HEADER_PATTERN]);
104 if (retval != 0) {
105 dev_err(dp->dev, "EDID Read failed!\n");
106 return -EIO;
108 sum = exynos_dp_calc_edid_check_sum(edid);
109 if (sum != 0) {
110 dev_err(dp->dev, "EDID bad checksum!\n");
111 return -EIO;
114 /* Read additional EDID data */
115 retval = exynos_dp_read_bytes_from_i2c(dp,
116 I2C_EDID_DEVICE_ADDR,
117 EDID_BLOCK_LENGTH,
118 EDID_BLOCK_LENGTH,
119 &edid[EDID_BLOCK_LENGTH]);
120 if (retval != 0) {
121 dev_err(dp->dev, "EDID Read failed!\n");
122 return -EIO;
124 sum = exynos_dp_calc_edid_check_sum(&edid[EDID_BLOCK_LENGTH]);
125 if (sum != 0) {
126 dev_err(dp->dev, "EDID bad checksum!\n");
127 return -EIO;
130 exynos_dp_read_byte_from_dpcd(dp, DPCD_ADDR_TEST_REQUEST,
131 &test_vector);
132 if (test_vector & DPCD_TEST_EDID_READ) {
133 exynos_dp_write_byte_to_dpcd(dp,
134 DPCD_ADDR_TEST_EDID_CHECKSUM,
135 edid[EDID_BLOCK_LENGTH + EDID_CHECKSUM]);
136 exynos_dp_write_byte_to_dpcd(dp,
137 DPCD_ADDR_TEST_RESPONSE,
138 DPCD_TEST_EDID_CHECKSUM_WRITE);
140 } else {
141 dev_info(dp->dev, "EDID data does not include any extensions.\n");
143 /* Read EDID data */
144 retval = exynos_dp_read_bytes_from_i2c(dp,
145 I2C_EDID_DEVICE_ADDR,
146 EDID_HEADER_PATTERN,
147 EDID_BLOCK_LENGTH,
148 &edid[EDID_HEADER_PATTERN]);
149 if (retval != 0) {
150 dev_err(dp->dev, "EDID Read failed!\n");
151 return -EIO;
153 sum = exynos_dp_calc_edid_check_sum(edid);
154 if (sum != 0) {
155 dev_err(dp->dev, "EDID bad checksum!\n");
156 return -EIO;
159 exynos_dp_read_byte_from_dpcd(dp,
160 DPCD_ADDR_TEST_REQUEST,
161 &test_vector);
162 if (test_vector & DPCD_TEST_EDID_READ) {
163 exynos_dp_write_byte_to_dpcd(dp,
164 DPCD_ADDR_TEST_EDID_CHECKSUM,
165 edid[EDID_CHECKSUM]);
166 exynos_dp_write_byte_to_dpcd(dp,
167 DPCD_ADDR_TEST_RESPONSE,
168 DPCD_TEST_EDID_CHECKSUM_WRITE);
172 dev_err(dp->dev, "EDID Read success!\n");
173 return 0;
176 static int exynos_dp_handle_edid(struct exynos_dp_device *dp)
178 u8 buf[12];
179 int i;
180 int retval;
182 /* Read DPCD DPCD_ADDR_DPCD_REV~RECEIVE_PORT1_CAP_1 */
183 retval = exynos_dp_read_bytes_from_dpcd(dp, DPCD_ADDR_DPCD_REV,
184 12, buf);
185 if (retval)
186 return retval;
188 /* Read EDID */
189 for (i = 0; i < 3; i++) {
190 retval = exynos_dp_read_edid(dp);
191 if (!retval)
192 break;
195 return retval;
198 static void exynos_dp_enable_rx_to_enhanced_mode(struct exynos_dp_device *dp,
199 bool enable)
201 u8 data;
203 exynos_dp_read_byte_from_dpcd(dp, DPCD_ADDR_LANE_COUNT_SET, &data);
205 if (enable)
206 exynos_dp_write_byte_to_dpcd(dp, DPCD_ADDR_LANE_COUNT_SET,
207 DPCD_ENHANCED_FRAME_EN |
208 DPCD_LANE_COUNT_SET(data));
209 else
210 exynos_dp_write_byte_to_dpcd(dp, DPCD_ADDR_LANE_COUNT_SET,
211 DPCD_LANE_COUNT_SET(data));
214 static int exynos_dp_is_enhanced_mode_available(struct exynos_dp_device *dp)
216 u8 data;
217 int retval;
219 exynos_dp_read_byte_from_dpcd(dp, DPCD_ADDR_MAX_LANE_COUNT, &data);
220 retval = DPCD_ENHANCED_FRAME_CAP(data);
222 return retval;
225 static void exynos_dp_set_enhanced_mode(struct exynos_dp_device *dp)
227 u8 data;
229 data = exynos_dp_is_enhanced_mode_available(dp);
230 exynos_dp_enable_rx_to_enhanced_mode(dp, data);
231 exynos_dp_enable_enhanced_mode(dp, data);
234 static void exynos_dp_training_pattern_dis(struct exynos_dp_device *dp)
236 exynos_dp_set_training_pattern(dp, DP_NONE);
238 exynos_dp_write_byte_to_dpcd(dp,
239 DPCD_ADDR_TRAINING_PATTERN_SET,
240 DPCD_TRAINING_PATTERN_DISABLED);
243 static void exynos_dp_set_lane_lane_pre_emphasis(struct exynos_dp_device *dp,
244 int pre_emphasis, int lane)
246 switch (lane) {
247 case 0:
248 exynos_dp_set_lane0_pre_emphasis(dp, pre_emphasis);
249 break;
250 case 1:
251 exynos_dp_set_lane1_pre_emphasis(dp, pre_emphasis);
252 break;
254 case 2:
255 exynos_dp_set_lane2_pre_emphasis(dp, pre_emphasis);
256 break;
258 case 3:
259 exynos_dp_set_lane3_pre_emphasis(dp, pre_emphasis);
260 break;
264 static int exynos_dp_link_start(struct exynos_dp_device *dp)
266 u8 buf[4];
267 int lane, lane_count, pll_tries, retval;
269 lane_count = dp->link_train.lane_count;
271 dp->link_train.lt_state = CLOCK_RECOVERY;
272 dp->link_train.eq_loop = 0;
274 for (lane = 0; lane < lane_count; lane++)
275 dp->link_train.cr_loop[lane] = 0;
277 /* Set link rate and count as you want to establish*/
278 exynos_dp_set_link_bandwidth(dp, dp->link_train.link_rate);
279 exynos_dp_set_lane_count(dp, dp->link_train.lane_count);
281 /* Setup RX configuration */
282 buf[0] = dp->link_train.link_rate;
283 buf[1] = dp->link_train.lane_count;
284 retval = exynos_dp_write_bytes_to_dpcd(dp, DPCD_ADDR_LINK_BW_SET,
285 2, buf);
286 if (retval)
287 return retval;
289 /* Set TX pre-emphasis to minimum */
290 for (lane = 0; lane < lane_count; lane++)
291 exynos_dp_set_lane_lane_pre_emphasis(dp,
292 PRE_EMPHASIS_LEVEL_0, lane);
294 /* Wait for PLL lock */
295 pll_tries = 0;
296 while (exynos_dp_get_pll_lock_status(dp) == PLL_UNLOCKED) {
297 if (pll_tries == DP_TIMEOUT_LOOP_COUNT) {
298 dev_err(dp->dev, "Wait for PLL lock timed out\n");
299 return -ETIMEDOUT;
302 pll_tries++;
303 usleep_range(90, 120);
306 /* Set training pattern 1 */
307 exynos_dp_set_training_pattern(dp, TRAINING_PTN1);
309 /* Set RX training pattern */
310 retval = exynos_dp_write_byte_to_dpcd(dp,
311 DPCD_ADDR_TRAINING_PATTERN_SET,
312 DPCD_SCRAMBLING_DISABLED | DPCD_TRAINING_PATTERN_1);
313 if (retval)
314 return retval;
316 for (lane = 0; lane < lane_count; lane++)
317 buf[lane] = DPCD_PRE_EMPHASIS_PATTERN2_LEVEL0 |
318 DPCD_VOLTAGE_SWING_PATTERN1_LEVEL0;
320 retval = exynos_dp_write_bytes_to_dpcd(dp, DPCD_ADDR_TRAINING_LANE0_SET,
321 lane_count, buf);
323 return retval;
326 static unsigned char exynos_dp_get_lane_status(u8 link_status[2], int lane)
328 int shift = (lane & 1) * 4;
329 u8 link_value = link_status[lane>>1];
331 return (link_value >> shift) & 0xf;
334 static int exynos_dp_clock_recovery_ok(u8 link_status[2], int lane_count)
336 int lane;
337 u8 lane_status;
339 for (lane = 0; lane < lane_count; lane++) {
340 lane_status = exynos_dp_get_lane_status(link_status, lane);
341 if ((lane_status & DPCD_LANE_CR_DONE) == 0)
342 return -EINVAL;
344 return 0;
347 static int exynos_dp_channel_eq_ok(u8 link_status[2], u8 link_align,
348 int lane_count)
350 int lane;
351 u8 lane_status;
353 if ((link_align & DPCD_INTERLANE_ALIGN_DONE) == 0)
354 return -EINVAL;
356 for (lane = 0; lane < lane_count; lane++) {
357 lane_status = exynos_dp_get_lane_status(link_status, lane);
358 lane_status &= DPCD_CHANNEL_EQ_BITS;
359 if (lane_status != DPCD_CHANNEL_EQ_BITS)
360 return -EINVAL;
363 return 0;
366 static unsigned char exynos_dp_get_adjust_request_voltage(u8 adjust_request[2],
367 int lane)
369 int shift = (lane & 1) * 4;
370 u8 link_value = adjust_request[lane>>1];
372 return (link_value >> shift) & 0x3;
375 static unsigned char exynos_dp_get_adjust_request_pre_emphasis(
376 u8 adjust_request[2],
377 int lane)
379 int shift = (lane & 1) * 4;
380 u8 link_value = adjust_request[lane>>1];
382 return ((link_value >> shift) & 0xc) >> 2;
385 static void exynos_dp_set_lane_link_training(struct exynos_dp_device *dp,
386 u8 training_lane_set, int lane)
388 switch (lane) {
389 case 0:
390 exynos_dp_set_lane0_link_training(dp, training_lane_set);
391 break;
392 case 1:
393 exynos_dp_set_lane1_link_training(dp, training_lane_set);
394 break;
396 case 2:
397 exynos_dp_set_lane2_link_training(dp, training_lane_set);
398 break;
400 case 3:
401 exynos_dp_set_lane3_link_training(dp, training_lane_set);
402 break;
406 static unsigned int exynos_dp_get_lane_link_training(
407 struct exynos_dp_device *dp,
408 int lane)
410 u32 reg;
412 switch (lane) {
413 case 0:
414 reg = exynos_dp_get_lane0_link_training(dp);
415 break;
416 case 1:
417 reg = exynos_dp_get_lane1_link_training(dp);
418 break;
419 case 2:
420 reg = exynos_dp_get_lane2_link_training(dp);
421 break;
422 case 3:
423 reg = exynos_dp_get_lane3_link_training(dp);
424 break;
425 default:
426 WARN_ON(1);
427 return 0;
430 return reg;
433 static void exynos_dp_reduce_link_rate(struct exynos_dp_device *dp)
435 exynos_dp_training_pattern_dis(dp);
436 exynos_dp_set_enhanced_mode(dp);
438 dp->link_train.lt_state = FAILED;
441 static void exynos_dp_get_adjust_training_lane(struct exynos_dp_device *dp,
442 u8 adjust_request[2])
444 int lane, lane_count;
445 u8 voltage_swing, pre_emphasis, training_lane;
447 lane_count = dp->link_train.lane_count;
448 for (lane = 0; lane < lane_count; lane++) {
449 voltage_swing = exynos_dp_get_adjust_request_voltage(
450 adjust_request, lane);
451 pre_emphasis = exynos_dp_get_adjust_request_pre_emphasis(
452 adjust_request, lane);
453 training_lane = DPCD_VOLTAGE_SWING_SET(voltage_swing) |
454 DPCD_PRE_EMPHASIS_SET(pre_emphasis);
456 if (voltage_swing == VOLTAGE_LEVEL_3)
457 training_lane |= DPCD_MAX_SWING_REACHED;
458 if (pre_emphasis == PRE_EMPHASIS_LEVEL_3)
459 training_lane |= DPCD_MAX_PRE_EMPHASIS_REACHED;
461 dp->link_train.training_lane[lane] = training_lane;
465 static int exynos_dp_process_clock_recovery(struct exynos_dp_device *dp)
467 int lane, lane_count, retval;
468 u8 voltage_swing, pre_emphasis, training_lane;
469 u8 link_status[2], adjust_request[2];
471 usleep_range(100, 101);
473 lane_count = dp->link_train.lane_count;
475 retval = exynos_dp_read_bytes_from_dpcd(dp,
476 DPCD_ADDR_LANE0_1_STATUS, 2, link_status);
477 if (retval)
478 return retval;
480 retval = exynos_dp_read_bytes_from_dpcd(dp,
481 DPCD_ADDR_ADJUST_REQUEST_LANE0_1, 2, adjust_request);
482 if (retval)
483 return retval;
485 if (exynos_dp_clock_recovery_ok(link_status, lane_count) == 0) {
486 /* set training pattern 2 for EQ */
487 exynos_dp_set_training_pattern(dp, TRAINING_PTN2);
489 retval = exynos_dp_write_byte_to_dpcd(dp,
490 DPCD_ADDR_TRAINING_PATTERN_SET,
491 DPCD_SCRAMBLING_DISABLED |
492 DPCD_TRAINING_PATTERN_2);
493 if (retval)
494 return retval;
496 dev_info(dp->dev, "Link Training Clock Recovery success\n");
497 dp->link_train.lt_state = EQUALIZER_TRAINING;
498 } else {
499 for (lane = 0; lane < lane_count; lane++) {
500 training_lane = exynos_dp_get_lane_link_training(
501 dp, lane);
502 voltage_swing = exynos_dp_get_adjust_request_voltage(
503 adjust_request, lane);
504 pre_emphasis = exynos_dp_get_adjust_request_pre_emphasis(
505 adjust_request, lane);
507 if (DPCD_VOLTAGE_SWING_GET(training_lane) ==
508 voltage_swing &&
509 DPCD_PRE_EMPHASIS_GET(training_lane) ==
510 pre_emphasis)
511 dp->link_train.cr_loop[lane]++;
513 if (dp->link_train.cr_loop[lane] == MAX_CR_LOOP ||
514 voltage_swing == VOLTAGE_LEVEL_3 ||
515 pre_emphasis == PRE_EMPHASIS_LEVEL_3) {
516 dev_err(dp->dev, "CR Max reached (%d,%d,%d)\n",
517 dp->link_train.cr_loop[lane],
518 voltage_swing, pre_emphasis);
519 exynos_dp_reduce_link_rate(dp);
520 return -EIO;
525 exynos_dp_get_adjust_training_lane(dp, adjust_request);
527 for (lane = 0; lane < lane_count; lane++)
528 exynos_dp_set_lane_link_training(dp,
529 dp->link_train.training_lane[lane], lane);
531 retval = exynos_dp_write_bytes_to_dpcd(dp,
532 DPCD_ADDR_TRAINING_LANE0_SET, lane_count,
533 dp->link_train.training_lane);
534 if (retval)
535 return retval;
537 return retval;
540 static int exynos_dp_process_equalizer_training(struct exynos_dp_device *dp)
542 int lane, lane_count, retval;
543 u32 reg;
544 u8 link_align, link_status[2], adjust_request[2];
546 usleep_range(400, 401);
548 lane_count = dp->link_train.lane_count;
550 retval = exynos_dp_read_bytes_from_dpcd(dp,
551 DPCD_ADDR_LANE0_1_STATUS, 2, link_status);
552 if (retval)
553 return retval;
555 if (exynos_dp_clock_recovery_ok(link_status, lane_count)) {
556 exynos_dp_reduce_link_rate(dp);
557 return -EIO;
560 retval = exynos_dp_read_bytes_from_dpcd(dp,
561 DPCD_ADDR_ADJUST_REQUEST_LANE0_1, 2, adjust_request);
562 if (retval)
563 return retval;
565 retval = exynos_dp_read_byte_from_dpcd(dp,
566 DPCD_ADDR_LANE_ALIGN_STATUS_UPDATED, &link_align);
567 if (retval)
568 return retval;
570 exynos_dp_get_adjust_training_lane(dp, adjust_request);
572 if (!exynos_dp_channel_eq_ok(link_status, link_align, lane_count)) {
573 /* traing pattern Set to Normal */
574 exynos_dp_training_pattern_dis(dp);
576 dev_info(dp->dev, "Link Training success!\n");
578 exynos_dp_get_link_bandwidth(dp, &reg);
579 dp->link_train.link_rate = reg;
580 dev_dbg(dp->dev, "final bandwidth = %.2x\n",
581 dp->link_train.link_rate);
583 exynos_dp_get_lane_count(dp, &reg);
584 dp->link_train.lane_count = reg;
585 dev_dbg(dp->dev, "final lane count = %.2x\n",
586 dp->link_train.lane_count);
588 /* set enhanced mode if available */
589 exynos_dp_set_enhanced_mode(dp);
590 dp->link_train.lt_state = FINISHED;
592 return 0;
595 /* not all locked */
596 dp->link_train.eq_loop++;
598 if (dp->link_train.eq_loop > MAX_EQ_LOOP) {
599 dev_err(dp->dev, "EQ Max loop\n");
600 exynos_dp_reduce_link_rate(dp);
601 return -EIO;
604 for (lane = 0; lane < lane_count; lane++)
605 exynos_dp_set_lane_link_training(dp,
606 dp->link_train.training_lane[lane], lane);
608 retval = exynos_dp_write_bytes_to_dpcd(dp, DPCD_ADDR_TRAINING_LANE0_SET,
609 lane_count, dp->link_train.training_lane);
611 return retval;
614 static void exynos_dp_get_max_rx_bandwidth(struct exynos_dp_device *dp,
615 u8 *bandwidth)
617 u8 data;
620 * For DP rev.1.1, Maximum link rate of Main Link lanes
621 * 0x06 = 1.62 Gbps, 0x0a = 2.7 Gbps
623 exynos_dp_read_byte_from_dpcd(dp, DPCD_ADDR_MAX_LINK_RATE, &data);
624 *bandwidth = data;
627 static void exynos_dp_get_max_rx_lane_count(struct exynos_dp_device *dp,
628 u8 *lane_count)
630 u8 data;
633 * For DP rev.1.1, Maximum number of Main Link lanes
634 * 0x01 = 1 lane, 0x02 = 2 lanes, 0x04 = 4 lanes
636 exynos_dp_read_byte_from_dpcd(dp, DPCD_ADDR_MAX_LANE_COUNT, &data);
637 *lane_count = DPCD_MAX_LANE_COUNT(data);
640 static void exynos_dp_init_training(struct exynos_dp_device *dp,
641 enum link_lane_count_type max_lane,
642 enum link_rate_type max_rate)
645 * MACRO_RST must be applied after the PLL_LOCK to avoid
646 * the DP inter pair skew issue for at least 10 us
648 exynos_dp_reset_macro(dp);
650 /* Initialize by reading RX's DPCD */
651 exynos_dp_get_max_rx_bandwidth(dp, &dp->link_train.link_rate);
652 exynos_dp_get_max_rx_lane_count(dp, &dp->link_train.lane_count);
654 if ((dp->link_train.link_rate != LINK_RATE_1_62GBPS) &&
655 (dp->link_train.link_rate != LINK_RATE_2_70GBPS)) {
656 dev_err(dp->dev, "Rx Max Link Rate is abnormal :%x !\n",
657 dp->link_train.link_rate);
658 dp->link_train.link_rate = LINK_RATE_1_62GBPS;
661 if (dp->link_train.lane_count == 0) {
662 dev_err(dp->dev, "Rx Max Lane count is abnormal :%x !\n",
663 dp->link_train.lane_count);
664 dp->link_train.lane_count = (u8)LANE_COUNT1;
667 /* Setup TX lane count & rate */
668 if (dp->link_train.lane_count > max_lane)
669 dp->link_train.lane_count = max_lane;
670 if (dp->link_train.link_rate > max_rate)
671 dp->link_train.link_rate = max_rate;
673 /* All DP analog module power up */
674 exynos_dp_set_analog_power_down(dp, POWER_ALL, 0);
677 static int exynos_dp_sw_link_training(struct exynos_dp_device *dp)
679 int retval = 0, training_finished = 0;
681 dp->link_train.lt_state = START;
683 /* Process here */
684 while (!retval && !training_finished) {
685 switch (dp->link_train.lt_state) {
686 case START:
687 retval = exynos_dp_link_start(dp);
688 if (retval)
689 dev_err(dp->dev, "LT link start failed!\n");
690 break;
691 case CLOCK_RECOVERY:
692 retval = exynos_dp_process_clock_recovery(dp);
693 if (retval)
694 dev_err(dp->dev, "LT CR failed!\n");
695 break;
696 case EQUALIZER_TRAINING:
697 retval = exynos_dp_process_equalizer_training(dp);
698 if (retval)
699 dev_err(dp->dev, "LT EQ failed!\n");
700 break;
701 case FINISHED:
702 training_finished = 1;
703 break;
704 case FAILED:
705 return -EREMOTEIO;
708 if (retval)
709 dev_err(dp->dev, "eDP link training failed (%d)\n", retval);
711 return retval;
714 static int exynos_dp_set_link_train(struct exynos_dp_device *dp,
715 u32 count,
716 u32 bwtype)
718 int i;
719 int retval;
721 for (i = 0; i < DP_TIMEOUT_LOOP_COUNT; i++) {
722 exynos_dp_init_training(dp, count, bwtype);
723 retval = exynos_dp_sw_link_training(dp);
724 if (retval == 0)
725 break;
727 usleep_range(100, 110);
730 return retval;
733 static int exynos_dp_config_video(struct exynos_dp_device *dp)
735 int retval = 0;
736 int timeout_loop = 0;
737 int done_count = 0;
739 exynos_dp_config_video_slave_mode(dp);
741 exynos_dp_set_video_color_format(dp);
743 if (exynos_dp_get_pll_lock_status(dp) == PLL_UNLOCKED) {
744 dev_err(dp->dev, "PLL is not locked yet.\n");
745 return -EINVAL;
748 for (;;) {
749 timeout_loop++;
750 if (exynos_dp_is_slave_video_stream_clock_on(dp) == 0)
751 break;
752 if (DP_TIMEOUT_LOOP_COUNT < timeout_loop) {
753 dev_err(dp->dev, "Timeout of video streamclk ok\n");
754 return -ETIMEDOUT;
757 usleep_range(1, 2);
760 /* Set to use the register calculated M/N video */
761 exynos_dp_set_video_cr_mn(dp, CALCULATED_M, 0, 0);
763 /* For video bist, Video timing must be generated by register */
764 exynos_dp_set_video_timing_mode(dp, VIDEO_TIMING_FROM_CAPTURE);
766 /* Disable video mute */
767 exynos_dp_enable_video_mute(dp, 0);
769 /* Configure video slave mode */
770 exynos_dp_enable_video_master(dp, 0);
772 /* Enable video */
773 exynos_dp_start_video(dp);
775 timeout_loop = 0;
777 for (;;) {
778 timeout_loop++;
779 if (exynos_dp_is_video_stream_on(dp) == 0) {
780 done_count++;
781 if (done_count > 10)
782 break;
783 } else if (done_count) {
784 done_count = 0;
786 if (DP_TIMEOUT_LOOP_COUNT < timeout_loop) {
787 dev_err(dp->dev, "Timeout of video streamclk ok\n");
788 return -ETIMEDOUT;
791 usleep_range(1000, 1001);
794 if (retval != 0)
795 dev_err(dp->dev, "Video stream is not detected!\n");
797 return retval;
800 static void exynos_dp_enable_scramble(struct exynos_dp_device *dp, bool enable)
802 u8 data;
804 if (enable) {
805 exynos_dp_enable_scrambling(dp);
807 exynos_dp_read_byte_from_dpcd(dp,
808 DPCD_ADDR_TRAINING_PATTERN_SET,
809 &data);
810 exynos_dp_write_byte_to_dpcd(dp,
811 DPCD_ADDR_TRAINING_PATTERN_SET,
812 (u8)(data & ~DPCD_SCRAMBLING_DISABLED));
813 } else {
814 exynos_dp_disable_scrambling(dp);
816 exynos_dp_read_byte_from_dpcd(dp,
817 DPCD_ADDR_TRAINING_PATTERN_SET,
818 &data);
819 exynos_dp_write_byte_to_dpcd(dp,
820 DPCD_ADDR_TRAINING_PATTERN_SET,
821 (u8)(data | DPCD_SCRAMBLING_DISABLED));
825 static irqreturn_t exynos_dp_irq_handler(int irq, void *arg)
827 struct exynos_dp_device *dp = arg;
829 enum dp_irq_type irq_type;
831 irq_type = exynos_dp_get_irq_type(dp);
832 switch (irq_type) {
833 case DP_IRQ_TYPE_HP_CABLE_IN:
834 dev_dbg(dp->dev, "Received irq - cable in\n");
835 schedule_work(&dp->hotplug_work);
836 exynos_dp_clear_hotplug_interrupts(dp);
837 break;
838 case DP_IRQ_TYPE_HP_CABLE_OUT:
839 dev_dbg(dp->dev, "Received irq - cable out\n");
840 exynos_dp_clear_hotplug_interrupts(dp);
841 break;
842 case DP_IRQ_TYPE_HP_CHANGE:
844 * We get these change notifications once in a while, but there
845 * is nothing we can do with them. Just ignore it for now and
846 * only handle cable changes.
848 dev_dbg(dp->dev, "Received irq - hotplug change; ignoring.\n");
849 exynos_dp_clear_hotplug_interrupts(dp);
850 break;
851 default:
852 dev_err(dp->dev, "Received irq - unknown type!\n");
853 break;
855 return IRQ_HANDLED;
858 static void exynos_dp_hotplug(struct work_struct *work)
860 struct exynos_dp_device *dp;
861 int ret;
863 dp = container_of(work, struct exynos_dp_device, hotplug_work);
865 ret = exynos_dp_detect_hpd(dp);
866 if (ret) {
867 /* Cable has been disconnected, we're done */
868 return;
871 ret = exynos_dp_handle_edid(dp);
872 if (ret) {
873 dev_err(dp->dev, "unable to handle edid\n");
874 return;
877 ret = exynos_dp_set_link_train(dp, dp->video_info->lane_count,
878 dp->video_info->link_rate);
879 if (ret) {
880 dev_err(dp->dev, "unable to do link train\n");
881 return;
884 exynos_dp_enable_scramble(dp, 1);
885 exynos_dp_enable_rx_to_enhanced_mode(dp, 1);
886 exynos_dp_enable_enhanced_mode(dp, 1);
888 exynos_dp_set_lane_count(dp, dp->video_info->lane_count);
889 exynos_dp_set_link_bandwidth(dp, dp->video_info->link_rate);
891 exynos_dp_init_video(dp);
892 ret = exynos_dp_config_video(dp);
893 if (ret)
894 dev_err(dp->dev, "unable to config video\n");
897 #ifdef CONFIG_OF
898 static struct exynos_dp_platdata *exynos_dp_dt_parse_pdata(struct device *dev)
900 struct device_node *dp_node = dev->of_node;
901 struct exynos_dp_platdata *pd;
902 struct video_info *dp_video_config;
904 pd = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL);
905 if (!pd) {
906 dev_err(dev, "memory allocation for pdata failed\n");
907 return ERR_PTR(-ENOMEM);
909 dp_video_config = devm_kzalloc(dev,
910 sizeof(*dp_video_config), GFP_KERNEL);
912 if (!dp_video_config) {
913 dev_err(dev, "memory allocation for video config failed\n");
914 return ERR_PTR(-ENOMEM);
916 pd->video_info = dp_video_config;
918 dp_video_config->h_sync_polarity =
919 of_property_read_bool(dp_node, "hsync-active-high");
921 dp_video_config->v_sync_polarity =
922 of_property_read_bool(dp_node, "vsync-active-high");
924 dp_video_config->interlaced =
925 of_property_read_bool(dp_node, "interlaced");
927 if (of_property_read_u32(dp_node, "samsung,color-space",
928 &dp_video_config->color_space)) {
929 dev_err(dev, "failed to get color-space\n");
930 return ERR_PTR(-EINVAL);
933 if (of_property_read_u32(dp_node, "samsung,dynamic-range",
934 &dp_video_config->dynamic_range)) {
935 dev_err(dev, "failed to get dynamic-range\n");
936 return ERR_PTR(-EINVAL);
939 if (of_property_read_u32(dp_node, "samsung,ycbcr-coeff",
940 &dp_video_config->ycbcr_coeff)) {
941 dev_err(dev, "failed to get ycbcr-coeff\n");
942 return ERR_PTR(-EINVAL);
945 if (of_property_read_u32(dp_node, "samsung,color-depth",
946 &dp_video_config->color_depth)) {
947 dev_err(dev, "failed to get color-depth\n");
948 return ERR_PTR(-EINVAL);
951 if (of_property_read_u32(dp_node, "samsung,link-rate",
952 &dp_video_config->link_rate)) {
953 dev_err(dev, "failed to get link-rate\n");
954 return ERR_PTR(-EINVAL);
957 if (of_property_read_u32(dp_node, "samsung,lane-count",
958 &dp_video_config->lane_count)) {
959 dev_err(dev, "failed to get lane-count\n");
960 return ERR_PTR(-EINVAL);
963 return pd;
966 static int exynos_dp_dt_parse_phydata(struct exynos_dp_device *dp)
968 struct device_node *dp_phy_node = of_node_get(dp->dev->of_node);
969 u32 phy_base;
970 int ret = 0;
972 dp_phy_node = of_find_node_by_name(dp_phy_node, "dptx-phy");
973 if (!dp_phy_node) {
974 dev_err(dp->dev, "could not find dptx-phy node\n");
975 return -ENODEV;
978 if (of_property_read_u32(dp_phy_node, "reg", &phy_base)) {
979 dev_err(dp->dev, "failed to get reg for dptx-phy\n");
980 ret = -EINVAL;
981 goto err;
984 if (of_property_read_u32(dp_phy_node, "samsung,enable-mask",
985 &dp->enable_mask)) {
986 dev_err(dp->dev, "failed to get enable-mask for dptx-phy\n");
987 ret = -EINVAL;
988 goto err;
991 dp->phy_addr = ioremap(phy_base, SZ_4);
992 if (!dp->phy_addr) {
993 dev_err(dp->dev, "failed to ioremap dp-phy\n");
994 ret = -ENOMEM;
995 goto err;
998 err:
999 of_node_put(dp_phy_node);
1001 return ret;
1004 static void exynos_dp_phy_init(struct exynos_dp_device *dp)
1006 u32 reg;
1008 reg = __raw_readl(dp->phy_addr);
1009 reg |= dp->enable_mask;
1010 __raw_writel(reg, dp->phy_addr);
1013 static void exynos_dp_phy_exit(struct exynos_dp_device *dp)
1015 u32 reg;
1017 reg = __raw_readl(dp->phy_addr);
1018 reg &= ~(dp->enable_mask);
1019 __raw_writel(reg, dp->phy_addr);
1021 #else
1022 static struct exynos_dp_platdata *exynos_dp_dt_parse_pdata(struct device *dev)
1024 return NULL;
1027 static int exynos_dp_dt_parse_phydata(struct exynos_dp_device *dp)
1029 return -EINVAL;
1032 static void exynos_dp_phy_init(struct exynos_dp_device *dp)
1034 return;
1037 static void exynos_dp_phy_exit(struct exynos_dp_device *dp)
1039 return;
1041 #endif /* CONFIG_OF */
1043 static int exynos_dp_probe(struct platform_device *pdev)
1045 struct resource *res;
1046 struct exynos_dp_device *dp;
1047 struct exynos_dp_platdata *pdata;
1049 int ret = 0;
1051 dp = devm_kzalloc(&pdev->dev, sizeof(struct exynos_dp_device),
1052 GFP_KERNEL);
1053 if (!dp) {
1054 dev_err(&pdev->dev, "no memory for device data\n");
1055 return -ENOMEM;
1058 dp->dev = &pdev->dev;
1060 if (pdev->dev.of_node) {
1061 pdata = exynos_dp_dt_parse_pdata(&pdev->dev);
1062 if (IS_ERR(pdata))
1063 return PTR_ERR(pdata);
1065 ret = exynos_dp_dt_parse_phydata(dp);
1066 if (ret)
1067 return ret;
1068 } else {
1069 pdata = pdev->dev.platform_data;
1070 if (!pdata) {
1071 dev_err(&pdev->dev, "no platform data\n");
1072 return -EINVAL;
1076 dp->clock = devm_clk_get(&pdev->dev, "dp");
1077 if (IS_ERR(dp->clock)) {
1078 dev_err(&pdev->dev, "failed to get clock\n");
1079 return PTR_ERR(dp->clock);
1082 clk_prepare_enable(dp->clock);
1084 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1086 dp->reg_base = devm_ioremap_resource(&pdev->dev, res);
1087 if (IS_ERR(dp->reg_base))
1088 return PTR_ERR(dp->reg_base);
1090 dp->irq = platform_get_irq(pdev, 0);
1091 if (dp->irq == -ENXIO) {
1092 dev_err(&pdev->dev, "failed to get irq\n");
1093 return -ENODEV;
1096 INIT_WORK(&dp->hotplug_work, exynos_dp_hotplug);
1098 dp->video_info = pdata->video_info;
1100 if (pdev->dev.of_node) {
1101 if (dp->phy_addr)
1102 exynos_dp_phy_init(dp);
1103 } else {
1104 if (pdata->phy_init)
1105 pdata->phy_init();
1108 exynos_dp_init_dp(dp);
1110 ret = devm_request_irq(&pdev->dev, dp->irq, exynos_dp_irq_handler, 0,
1111 "exynos-dp", dp);
1112 if (ret) {
1113 dev_err(&pdev->dev, "failed to request irq\n");
1114 return ret;
1117 platform_set_drvdata(pdev, dp);
1119 return 0;
1122 static int exynos_dp_remove(struct platform_device *pdev)
1124 struct exynos_dp_platdata *pdata = pdev->dev.platform_data;
1125 struct exynos_dp_device *dp = platform_get_drvdata(pdev);
1127 flush_work(&dp->hotplug_work);
1129 if (pdev->dev.of_node) {
1130 if (dp->phy_addr)
1131 exynos_dp_phy_exit(dp);
1132 } else {
1133 if (pdata->phy_exit)
1134 pdata->phy_exit();
1137 clk_disable_unprepare(dp->clock);
1140 return 0;
1143 #ifdef CONFIG_PM_SLEEP
1144 static int exynos_dp_suspend(struct device *dev)
1146 struct exynos_dp_platdata *pdata = dev->platform_data;
1147 struct exynos_dp_device *dp = dev_get_drvdata(dev);
1149 disable_irq(dp->irq);
1151 flush_work(&dp->hotplug_work);
1153 if (dev->of_node) {
1154 if (dp->phy_addr)
1155 exynos_dp_phy_exit(dp);
1156 } else {
1157 if (pdata->phy_exit)
1158 pdata->phy_exit();
1161 clk_disable_unprepare(dp->clock);
1163 return 0;
1166 static int exynos_dp_resume(struct device *dev)
1168 struct exynos_dp_platdata *pdata = dev->platform_data;
1169 struct exynos_dp_device *dp = dev_get_drvdata(dev);
1171 if (dev->of_node) {
1172 if (dp->phy_addr)
1173 exynos_dp_phy_init(dp);
1174 } else {
1175 if (pdata->phy_init)
1176 pdata->phy_init();
1179 clk_prepare_enable(dp->clock);
1181 exynos_dp_init_dp(dp);
1183 enable_irq(dp->irq);
1185 return 0;
1187 #endif
1189 static const struct dev_pm_ops exynos_dp_pm_ops = {
1190 SET_SYSTEM_SLEEP_PM_OPS(exynos_dp_suspend, exynos_dp_resume)
1193 static const struct of_device_id exynos_dp_match[] = {
1194 { .compatible = "samsung,exynos5-dp" },
1197 MODULE_DEVICE_TABLE(of, exynos_dp_match);
1199 static struct platform_driver exynos_dp_driver = {
1200 .probe = exynos_dp_probe,
1201 .remove = exynos_dp_remove,
1202 .driver = {
1203 .name = "exynos-dp",
1204 .owner = THIS_MODULE,
1205 .pm = &exynos_dp_pm_ops,
1206 .of_match_table = of_match_ptr(exynos_dp_match),
1210 module_platform_driver(exynos_dp_driver);
1212 MODULE_AUTHOR("Jingoo Han <jg1.han@samsung.com>");
1213 MODULE_DESCRIPTION("Samsung SoC DP Driver");
1214 MODULE_LICENSE("GPL");