blob: ed818b9a615a3bc00e8ee33cdfcdd3f16478505b [file] [log] [blame]
Jingoo Hane9474be2012-02-03 18:01:55 +09001/*
2 * Samsung SoC DP (Display Port) 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>
Jingoo Hane9474be2012-02-03 18:01:55 +090015#include <linux/err.h>
16#include <linux/clk.h>
17#include <linux/io.h>
18#include <linux/interrupt.h>
Ajay Kumarc4e235c2012-10-13 05:48:00 +090019#include <linux/of.h>
Andrew Brestickerb8b52472014-04-22 04:09:10 +053020#include <linux/of_gpio.h>
21#include <linux/gpio.h>
Inki Daef37cd5e2014-05-09 14:25:20 +090022#include <linux/component.h>
Jingoo Han8114fab2013-10-16 21:58:16 +053023#include <linux/phy/phy.h>
Sean Paul1417f102014-01-30 16:19:23 -050024#include <video/of_display_timing.h>
25#include <video/of_videomode.h>
Jingoo Hane9474be2012-02-03 18:01:55 +090026
Sean Paul1417f102014-01-30 16:19:23 -050027#include <drm/drmP.h>
Sean Paulcaa5d1e2014-01-30 16:19:30 -050028#include <drm/drm_crtc.h>
29#include <drm/drm_crtc_helper.h>
Ajay Kumar5f1dcd82014-07-31 23:12:14 +053030#include <drm/drm_panel.h>
Sean Paul1634ba22014-02-24 19:20:15 +090031#include <drm/bridge/ptn3460.h>
Sean Paul1417f102014-01-30 16:19:23 -050032
33#include "exynos_drm_drv.h"
Jingoo Hane9474be2012-02-03 18:01:55 +090034#include "exynos_dp_core.h"
35
Sean Paulcaa5d1e2014-01-30 16:19:30 -050036#define ctx_from_connector(c) container_of(c, struct exynos_dp_device, \
37 connector)
38
Sean Paul1634ba22014-02-24 19:20:15 +090039struct bridge_init {
40 struct i2c_client *client;
41 struct device_node *node;
42};
43
Ajay Kumar5f1dcd82014-07-31 23:12:14 +053044static void exynos_dp_init_dp(struct exynos_dp_device *dp)
Jingoo Hane9474be2012-02-03 18:01:55 +090045{
46 exynos_dp_reset(dp);
47
Jingoo Han24db03a2012-05-25 16:21:08 +090048 exynos_dp_swreset(dp);
49
Jingoo Han75435c72012-08-23 19:55:13 +090050 exynos_dp_init_analog_param(dp);
51 exynos_dp_init_interrupt(dp);
52
Jingoo Hane9474be2012-02-03 18:01:55 +090053 /* SW defined function Normal operation */
54 exynos_dp_enable_sw_function(dp);
55
56 exynos_dp_config_interrupt(dp);
57 exynos_dp_init_analog_func(dp);
58
59 exynos_dp_init_hpd(dp);
60 exynos_dp_init_aux(dp);
Jingoo Hane9474be2012-02-03 18:01:55 +090061}
62
63static int exynos_dp_detect_hpd(struct exynos_dp_device *dp)
64{
65 int timeout_loop = 0;
66
Jingoo Hane9474be2012-02-03 18:01:55 +090067 while (exynos_dp_get_plug_in_status(dp) != 0) {
68 timeout_loop++;
69 if (DP_TIMEOUT_LOOP_COUNT < timeout_loop) {
70 dev_err(dp->dev, "failed to get hpd plug status\n");
71 return -ETIMEDOUT;
72 }
Jingoo Hana2c81bc2012-07-18 18:50:59 +090073 usleep_range(10, 11);
Jingoo Hane9474be2012-02-03 18:01:55 +090074 }
75
76 return 0;
77}
78
79static unsigned char exynos_dp_calc_edid_check_sum(unsigned char *edid_data)
80{
81 int i;
82 unsigned char sum = 0;
83
84 for (i = 0; i < EDID_BLOCK_LENGTH; i++)
85 sum = sum + edid_data[i];
86
87 return sum;
88}
89
90static int exynos_dp_read_edid(struct exynos_dp_device *dp)
91{
92 unsigned char edid[EDID_BLOCK_LENGTH * 2];
93 unsigned int extend_block = 0;
94 unsigned char sum;
95 unsigned char test_vector;
96 int retval;
97
98 /*
99 * EDID device address is 0x50.
100 * However, if necessary, you must have set upper address
101 * into E-EDID in I2C device, 0x30.
102 */
103
104 /* Read Extension Flag, Number of 128-byte EDID extension blocks */
Sean Paul99f54152012-11-01 02:13:00 +0000105 retval = exynos_dp_read_byte_from_i2c(dp, I2C_EDID_DEVICE_ADDR,
Jingoo Hane9474be2012-02-03 18:01:55 +0900106 EDID_EXTENSION_FLAG,
107 &extend_block);
Sean Paul99f54152012-11-01 02:13:00 +0000108 if (retval)
109 return retval;
Jingoo Hane9474be2012-02-03 18:01:55 +0900110
111 if (extend_block > 0) {
112 dev_dbg(dp->dev, "EDID data includes a single extension!\n");
113
114 /* Read EDID data */
115 retval = exynos_dp_read_bytes_from_i2c(dp, I2C_EDID_DEVICE_ADDR,
116 EDID_HEADER_PATTERN,
117 EDID_BLOCK_LENGTH,
118 &edid[EDID_HEADER_PATTERN]);
119 if (retval != 0) {
120 dev_err(dp->dev, "EDID Read failed!\n");
121 return -EIO;
122 }
123 sum = exynos_dp_calc_edid_check_sum(edid);
124 if (sum != 0) {
125 dev_err(dp->dev, "EDID bad checksum!\n");
126 return -EIO;
127 }
128
129 /* Read additional EDID data */
130 retval = exynos_dp_read_bytes_from_i2c(dp,
131 I2C_EDID_DEVICE_ADDR,
132 EDID_BLOCK_LENGTH,
133 EDID_BLOCK_LENGTH,
134 &edid[EDID_BLOCK_LENGTH]);
135 if (retval != 0) {
136 dev_err(dp->dev, "EDID Read failed!\n");
137 return -EIO;
138 }
139 sum = exynos_dp_calc_edid_check_sum(&edid[EDID_BLOCK_LENGTH]);
140 if (sum != 0) {
141 dev_err(dp->dev, "EDID bad checksum!\n");
142 return -EIO;
143 }
144
Jingoo Han073ea2a2014-05-07 20:44:51 +0900145 exynos_dp_read_byte_from_dpcd(dp, DP_TEST_REQUEST,
Jingoo Hane9474be2012-02-03 18:01:55 +0900146 &test_vector);
Jingoo Han073ea2a2014-05-07 20:44:51 +0900147 if (test_vector & DP_TEST_LINK_EDID_READ) {
Jingoo Hane9474be2012-02-03 18:01:55 +0900148 exynos_dp_write_byte_to_dpcd(dp,
Jingoo Han073ea2a2014-05-07 20:44:51 +0900149 DP_TEST_EDID_CHECKSUM,
Jingoo Hane9474be2012-02-03 18:01:55 +0900150 edid[EDID_BLOCK_LENGTH + EDID_CHECKSUM]);
151 exynos_dp_write_byte_to_dpcd(dp,
Jingoo Han073ea2a2014-05-07 20:44:51 +0900152 DP_TEST_RESPONSE,
153 DP_TEST_EDID_CHECKSUM_WRITE);
Jingoo Hane9474be2012-02-03 18:01:55 +0900154 }
155 } else {
156 dev_info(dp->dev, "EDID data does not include any extensions.\n");
157
158 /* Read EDID data */
159 retval = exynos_dp_read_bytes_from_i2c(dp,
160 I2C_EDID_DEVICE_ADDR,
161 EDID_HEADER_PATTERN,
162 EDID_BLOCK_LENGTH,
163 &edid[EDID_HEADER_PATTERN]);
164 if (retval != 0) {
165 dev_err(dp->dev, "EDID Read failed!\n");
166 return -EIO;
167 }
168 sum = exynos_dp_calc_edid_check_sum(edid);
169 if (sum != 0) {
170 dev_err(dp->dev, "EDID bad checksum!\n");
171 return -EIO;
172 }
173
174 exynos_dp_read_byte_from_dpcd(dp,
Jingoo Han073ea2a2014-05-07 20:44:51 +0900175 DP_TEST_REQUEST,
Jingoo Hane9474be2012-02-03 18:01:55 +0900176 &test_vector);
Jingoo Han073ea2a2014-05-07 20:44:51 +0900177 if (test_vector & DP_TEST_LINK_EDID_READ) {
Jingoo Hane9474be2012-02-03 18:01:55 +0900178 exynos_dp_write_byte_to_dpcd(dp,
Jingoo Han073ea2a2014-05-07 20:44:51 +0900179 DP_TEST_EDID_CHECKSUM,
Jingoo Hane9474be2012-02-03 18:01:55 +0900180 edid[EDID_CHECKSUM]);
181 exynos_dp_write_byte_to_dpcd(dp,
Jingoo Han073ea2a2014-05-07 20:44:51 +0900182 DP_TEST_RESPONSE,
183 DP_TEST_EDID_CHECKSUM_WRITE);
Jingoo Hane9474be2012-02-03 18:01:55 +0900184 }
185 }
186
187 dev_err(dp->dev, "EDID Read success!\n");
188 return 0;
189}
190
191static int exynos_dp_handle_edid(struct exynos_dp_device *dp)
192{
193 u8 buf[12];
194 int i;
195 int retval;
196
Jingoo Han073ea2a2014-05-07 20:44:51 +0900197 /* Read DPCD DP_DPCD_REV~RECEIVE_PORT1_CAP_1 */
198 retval = exynos_dp_read_bytes_from_dpcd(dp, DP_DPCD_REV,
Sean Paul99f54152012-11-01 02:13:00 +0000199 12, buf);
200 if (retval)
201 return retval;
Jingoo Hane9474be2012-02-03 18:01:55 +0900202
203 /* Read EDID */
204 for (i = 0; i < 3; i++) {
205 retval = exynos_dp_read_edid(dp);
Sean Paul99f54152012-11-01 02:13:00 +0000206 if (!retval)
Jingoo Hane9474be2012-02-03 18:01:55 +0900207 break;
208 }
209
210 return retval;
211}
212
213static void exynos_dp_enable_rx_to_enhanced_mode(struct exynos_dp_device *dp,
214 bool enable)
215{
216 u8 data;
217
Jingoo Han073ea2a2014-05-07 20:44:51 +0900218 exynos_dp_read_byte_from_dpcd(dp, DP_LANE_COUNT_SET, &data);
Jingoo Hane9474be2012-02-03 18:01:55 +0900219
220 if (enable)
Jingoo Han073ea2a2014-05-07 20:44:51 +0900221 exynos_dp_write_byte_to_dpcd(dp, DP_LANE_COUNT_SET,
222 DP_LANE_COUNT_ENHANCED_FRAME_EN |
Jingoo Hane9474be2012-02-03 18:01:55 +0900223 DPCD_LANE_COUNT_SET(data));
224 else
Jingoo Han073ea2a2014-05-07 20:44:51 +0900225 exynos_dp_write_byte_to_dpcd(dp, DP_LANE_COUNT_SET,
Jingoo Hane9474be2012-02-03 18:01:55 +0900226 DPCD_LANE_COUNT_SET(data));
227}
228
229static int exynos_dp_is_enhanced_mode_available(struct exynos_dp_device *dp)
230{
231 u8 data;
232 int retval;
233
Jingoo Han073ea2a2014-05-07 20:44:51 +0900234 exynos_dp_read_byte_from_dpcd(dp, DP_MAX_LANE_COUNT, &data);
Jingoo Hane9474be2012-02-03 18:01:55 +0900235 retval = DPCD_ENHANCED_FRAME_CAP(data);
236
237 return retval;
238}
239
240static void exynos_dp_set_enhanced_mode(struct exynos_dp_device *dp)
241{
242 u8 data;
243
244 data = exynos_dp_is_enhanced_mode_available(dp);
245 exynos_dp_enable_rx_to_enhanced_mode(dp, data);
246 exynos_dp_enable_enhanced_mode(dp, data);
247}
248
249static void exynos_dp_training_pattern_dis(struct exynos_dp_device *dp)
250{
251 exynos_dp_set_training_pattern(dp, DP_NONE);
252
253 exynos_dp_write_byte_to_dpcd(dp,
Jingoo Han073ea2a2014-05-07 20:44:51 +0900254 DP_TRAINING_PATTERN_SET,
255 DP_TRAINING_PATTERN_DISABLE);
Jingoo Hane9474be2012-02-03 18:01:55 +0900256}
257
258static void exynos_dp_set_lane_lane_pre_emphasis(struct exynos_dp_device *dp,
259 int pre_emphasis, int lane)
260{
261 switch (lane) {
262 case 0:
263 exynos_dp_set_lane0_pre_emphasis(dp, pre_emphasis);
264 break;
265 case 1:
266 exynos_dp_set_lane1_pre_emphasis(dp, pre_emphasis);
267 break;
268
269 case 2:
270 exynos_dp_set_lane2_pre_emphasis(dp, pre_emphasis);
271 break;
272
273 case 3:
274 exynos_dp_set_lane3_pre_emphasis(dp, pre_emphasis);
275 break;
276 }
277}
278
Sean Paulace2d7f2012-10-31 23:21:00 +0000279static int exynos_dp_link_start(struct exynos_dp_device *dp)
Jingoo Hane9474be2012-02-03 18:01:55 +0900280{
Jingoo Hand5c0eed2012-07-19 13:52:59 +0900281 u8 buf[4];
Sean Paul49ce41f2012-10-31 23:21:00 +0000282 int lane, lane_count, pll_tries, retval;
Jingoo Hane9474be2012-02-03 18:01:55 +0900283
284 lane_count = dp->link_train.lane_count;
285
286 dp->link_train.lt_state = CLOCK_RECOVERY;
287 dp->link_train.eq_loop = 0;
288
289 for (lane = 0; lane < lane_count; lane++)
290 dp->link_train.cr_loop[lane] = 0;
291
Jingoo Hane9474be2012-02-03 18:01:55 +0900292 /* Set link rate and count as you want to establish*/
293 exynos_dp_set_link_bandwidth(dp, dp->link_train.link_rate);
294 exynos_dp_set_lane_count(dp, dp->link_train.lane_count);
295
296 /* Setup RX configuration */
297 buf[0] = dp->link_train.link_rate;
298 buf[1] = dp->link_train.lane_count;
Jingoo Han073ea2a2014-05-07 20:44:51 +0900299 retval = exynos_dp_write_bytes_to_dpcd(dp, DP_LINK_BW_SET,
Jingoo Hane9474be2012-02-03 18:01:55 +0900300 2, buf);
Sean Paulace2d7f2012-10-31 23:21:00 +0000301 if (retval)
302 return retval;
Jingoo Hane9474be2012-02-03 18:01:55 +0900303
304 /* Set TX pre-emphasis to minimum */
305 for (lane = 0; lane < lane_count; lane++)
306 exynos_dp_set_lane_lane_pre_emphasis(dp,
307 PRE_EMPHASIS_LEVEL_0, lane);
308
Sean Paul49ce41f2012-10-31 23:21:00 +0000309 /* Wait for PLL lock */
310 pll_tries = 0;
311 while (exynos_dp_get_pll_lock_status(dp) == PLL_UNLOCKED) {
312 if (pll_tries == DP_TIMEOUT_LOOP_COUNT) {
313 dev_err(dp->dev, "Wait for PLL lock timed out\n");
314 return -ETIMEDOUT;
315 }
316
317 pll_tries++;
318 usleep_range(90, 120);
319 }
320
Jingoo Hane9474be2012-02-03 18:01:55 +0900321 /* Set training pattern 1 */
322 exynos_dp_set_training_pattern(dp, TRAINING_PTN1);
323
324 /* Set RX training pattern */
Sean Paulfadec4b2012-10-31 23:21:00 +0000325 retval = exynos_dp_write_byte_to_dpcd(dp,
Jingoo Han073ea2a2014-05-07 20:44:51 +0900326 DP_TRAINING_PATTERN_SET,
327 DP_LINK_SCRAMBLING_DISABLE | DP_TRAINING_PATTERN_1);
Sean Paulfadec4b2012-10-31 23:21:00 +0000328 if (retval)
329 return retval;
Jingoo Hane9474be2012-02-03 18:01:55 +0900330
331 for (lane = 0; lane < lane_count; lane++)
Sonika Jindal0ded9252014-08-08 16:23:42 +0530332 buf[lane] = DP_TRAIN_PRE_EMPH_LEVEL_0 |
333 DP_TRAIN_VOLTAGE_SWING_LEVEL_0;
Sean Paulfadec4b2012-10-31 23:21:00 +0000334
Jingoo Han073ea2a2014-05-07 20:44:51 +0900335 retval = exynos_dp_write_bytes_to_dpcd(dp, DP_TRAINING_LANE0_SET,
Sean Paulfadec4b2012-10-31 23:21:00 +0000336 lane_count, buf);
Sean Paulace2d7f2012-10-31 23:21:00 +0000337
338 return retval;
Jingoo Hane9474be2012-02-03 18:01:55 +0900339}
340
Jingoo Hand5c0eed2012-07-19 13:52:59 +0900341static unsigned char exynos_dp_get_lane_status(u8 link_status[2], int lane)
Jingoo Hane9474be2012-02-03 18:01:55 +0900342{
343 int shift = (lane & 1) * 4;
344 u8 link_value = link_status[lane>>1];
345
346 return (link_value >> shift) & 0xf;
347}
348
Jingoo Hand5c0eed2012-07-19 13:52:59 +0900349static int exynos_dp_clock_recovery_ok(u8 link_status[2], int lane_count)
Jingoo Hane9474be2012-02-03 18:01:55 +0900350{
351 int lane;
352 u8 lane_status;
353
354 for (lane = 0; lane < lane_count; lane++) {
355 lane_status = exynos_dp_get_lane_status(link_status, lane);
Jingoo Han073ea2a2014-05-07 20:44:51 +0900356 if ((lane_status & DP_LANE_CR_DONE) == 0)
Jingoo Hane9474be2012-02-03 18:01:55 +0900357 return -EINVAL;
358 }
359 return 0;
360}
361
Sean Paulfadec4b2012-10-31 23:21:00 +0000362static int exynos_dp_channel_eq_ok(u8 link_status[2], u8 link_align,
363 int lane_count)
Jingoo Hane9474be2012-02-03 18:01:55 +0900364{
365 int lane;
Jingoo Hane9474be2012-02-03 18:01:55 +0900366 u8 lane_status;
367
Jingoo Han073ea2a2014-05-07 20:44:51 +0900368 if ((link_align & DP_INTERLANE_ALIGN_DONE) == 0)
Jingoo Hane9474be2012-02-03 18:01:55 +0900369 return -EINVAL;
370
371 for (lane = 0; lane < lane_count; lane++) {
Sean Paulfadec4b2012-10-31 23:21:00 +0000372 lane_status = exynos_dp_get_lane_status(link_status, lane);
Jingoo Han073ea2a2014-05-07 20:44:51 +0900373 lane_status &= DP_CHANNEL_EQ_BITS;
374 if (lane_status != DP_CHANNEL_EQ_BITS)
Jingoo Hane9474be2012-02-03 18:01:55 +0900375 return -EINVAL;
376 }
Jingoo Hand5c0eed2012-07-19 13:52:59 +0900377
Jingoo Hane9474be2012-02-03 18:01:55 +0900378 return 0;
379}
380
381static unsigned char exynos_dp_get_adjust_request_voltage(u8 adjust_request[2],
382 int lane)
383{
384 int shift = (lane & 1) * 4;
385 u8 link_value = adjust_request[lane>>1];
386
387 return (link_value >> shift) & 0x3;
388}
389
390static unsigned char exynos_dp_get_adjust_request_pre_emphasis(
391 u8 adjust_request[2],
392 int lane)
393{
394 int shift = (lane & 1) * 4;
395 u8 link_value = adjust_request[lane>>1];
396
397 return ((link_value >> shift) & 0xc) >> 2;
398}
399
400static void exynos_dp_set_lane_link_training(struct exynos_dp_device *dp,
401 u8 training_lane_set, int lane)
402{
403 switch (lane) {
404 case 0:
405 exynos_dp_set_lane0_link_training(dp, training_lane_set);
406 break;
407 case 1:
408 exynos_dp_set_lane1_link_training(dp, training_lane_set);
409 break;
410
411 case 2:
412 exynos_dp_set_lane2_link_training(dp, training_lane_set);
413 break;
414
415 case 3:
416 exynos_dp_set_lane3_link_training(dp, training_lane_set);
417 break;
418 }
419}
420
421static unsigned int exynos_dp_get_lane_link_training(
422 struct exynos_dp_device *dp,
423 int lane)
424{
425 u32 reg;
426
427 switch (lane) {
428 case 0:
429 reg = exynos_dp_get_lane0_link_training(dp);
430 break;
431 case 1:
432 reg = exynos_dp_get_lane1_link_training(dp);
433 break;
434 case 2:
435 reg = exynos_dp_get_lane2_link_training(dp);
436 break;
437 case 3:
438 reg = exynos_dp_get_lane3_link_training(dp);
439 break;
Jingoo Han64c43df2012-06-20 10:25:48 +0900440 default:
441 WARN_ON(1);
442 return 0;
Jingoo Hane9474be2012-02-03 18:01:55 +0900443 }
444
445 return reg;
446}
447
448static void exynos_dp_reduce_link_rate(struct exynos_dp_device *dp)
449{
Jingoo Hand5c0eed2012-07-19 13:52:59 +0900450 exynos_dp_training_pattern_dis(dp);
451 exynos_dp_set_enhanced_mode(dp);
Jingoo Hane9474be2012-02-03 18:01:55 +0900452
Jingoo Hand5c0eed2012-07-19 13:52:59 +0900453 dp->link_train.lt_state = FAILED;
Jingoo Hane9474be2012-02-03 18:01:55 +0900454}
455
Sean Paulfadec4b2012-10-31 23:21:00 +0000456static void exynos_dp_get_adjust_training_lane(struct exynos_dp_device *dp,
457 u8 adjust_request[2])
458{
459 int lane, lane_count;
460 u8 voltage_swing, pre_emphasis, training_lane;
461
462 lane_count = dp->link_train.lane_count;
463 for (lane = 0; lane < lane_count; lane++) {
464 voltage_swing = exynos_dp_get_adjust_request_voltage(
465 adjust_request, lane);
466 pre_emphasis = exynos_dp_get_adjust_request_pre_emphasis(
467 adjust_request, lane);
468 training_lane = DPCD_VOLTAGE_SWING_SET(voltage_swing) |
469 DPCD_PRE_EMPHASIS_SET(pre_emphasis);
470
471 if (voltage_swing == VOLTAGE_LEVEL_3)
Jingoo Han073ea2a2014-05-07 20:44:51 +0900472 training_lane |= DP_TRAIN_MAX_SWING_REACHED;
Sean Paulfadec4b2012-10-31 23:21:00 +0000473 if (pre_emphasis == PRE_EMPHASIS_LEVEL_3)
Jingoo Han073ea2a2014-05-07 20:44:51 +0900474 training_lane |= DP_TRAIN_MAX_PRE_EMPHASIS_REACHED;
Sean Paulfadec4b2012-10-31 23:21:00 +0000475
476 dp->link_train.training_lane[lane] = training_lane;
477 }
478}
479
Jingoo Hane9474be2012-02-03 18:01:55 +0900480static int exynos_dp_process_clock_recovery(struct exynos_dp_device *dp)
481{
Sean Paulace2d7f2012-10-31 23:21:00 +0000482 int lane, lane_count, retval;
Sean Paulfadec4b2012-10-31 23:21:00 +0000483 u8 voltage_swing, pre_emphasis, training_lane;
484 u8 link_status[2], adjust_request[2];
Jingoo Hane9474be2012-02-03 18:01:55 +0900485
Jingoo Hana2c81bc2012-07-18 18:50:59 +0900486 usleep_range(100, 101);
Jingoo Hane9474be2012-02-03 18:01:55 +0900487
Jingoo Hane9474be2012-02-03 18:01:55 +0900488 lane_count = dp->link_train.lane_count;
489
Sean Paulfadec4b2012-10-31 23:21:00 +0000490 retval = exynos_dp_read_bytes_from_dpcd(dp,
Jingoo Han073ea2a2014-05-07 20:44:51 +0900491 DP_LANE0_1_STATUS, 2, link_status);
Sean Paulfadec4b2012-10-31 23:21:00 +0000492 if (retval)
493 return retval;
494
495 retval = exynos_dp_read_bytes_from_dpcd(dp,
Jingoo Han073ea2a2014-05-07 20:44:51 +0900496 DP_ADJUST_REQUEST_LANE0_1, 2, adjust_request);
Sean Paulace2d7f2012-10-31 23:21:00 +0000497 if (retval)
498 return retval;
Jingoo Hand5c0eed2012-07-19 13:52:59 +0900499
Jingoo Hane9474be2012-02-03 18:01:55 +0900500 if (exynos_dp_clock_recovery_ok(link_status, lane_count) == 0) {
501 /* set training pattern 2 for EQ */
502 exynos_dp_set_training_pattern(dp, TRAINING_PTN2);
503
Sean Paulace2d7f2012-10-31 23:21:00 +0000504 retval = exynos_dp_write_byte_to_dpcd(dp,
Jingoo Han073ea2a2014-05-07 20:44:51 +0900505 DP_TRAINING_PATTERN_SET,
506 DP_LINK_SCRAMBLING_DISABLE |
507 DP_TRAINING_PATTERN_2);
Sean Paulace2d7f2012-10-31 23:21:00 +0000508 if (retval)
509 return retval;
Jingoo Hand5c0eed2012-07-19 13:52:59 +0900510
511 dev_info(dp->dev, "Link Training Clock Recovery success\n");
512 dp->link_train.lt_state = EQUALIZER_TRAINING;
513 } else {
514 for (lane = 0; lane < lane_count; lane++) {
515 training_lane = exynos_dp_get_lane_link_training(
516 dp, lane);
Jingoo Hand5c0eed2012-07-19 13:52:59 +0900517 voltage_swing = exynos_dp_get_adjust_request_voltage(
518 adjust_request, lane);
519 pre_emphasis = exynos_dp_get_adjust_request_pre_emphasis(
520 adjust_request, lane);
521
Sean Paulfadec4b2012-10-31 23:21:00 +0000522 if (DPCD_VOLTAGE_SWING_GET(training_lane) ==
523 voltage_swing &&
524 DPCD_PRE_EMPHASIS_GET(training_lane) ==
525 pre_emphasis)
Jingoo Hand5c0eed2012-07-19 13:52:59 +0900526 dp->link_train.cr_loop[lane]++;
Sean Paulfadec4b2012-10-31 23:21:00 +0000527
528 if (dp->link_train.cr_loop[lane] == MAX_CR_LOOP ||
529 voltage_swing == VOLTAGE_LEVEL_3 ||
530 pre_emphasis == PRE_EMPHASIS_LEVEL_3) {
531 dev_err(dp->dev, "CR Max reached (%d,%d,%d)\n",
532 dp->link_train.cr_loop[lane],
533 voltage_swing, pre_emphasis);
534 exynos_dp_reduce_link_rate(dp);
535 return -EIO;
Jingoo Hand5c0eed2012-07-19 13:52:59 +0900536 }
Jingoo Hane9474be2012-02-03 18:01:55 +0900537 }
538 }
539
Sean Paulfadec4b2012-10-31 23:21:00 +0000540 exynos_dp_get_adjust_training_lane(dp, adjust_request);
Jingoo Hand5c0eed2012-07-19 13:52:59 +0900541
Sean Paulfadec4b2012-10-31 23:21:00 +0000542 for (lane = 0; lane < lane_count; lane++)
543 exynos_dp_set_lane_link_training(dp,
544 dp->link_train.training_lane[lane], lane);
545
546 retval = exynos_dp_write_bytes_to_dpcd(dp,
Jingoo Han073ea2a2014-05-07 20:44:51 +0900547 DP_TRAINING_LANE0_SET, lane_count,
Sean Paulfadec4b2012-10-31 23:21:00 +0000548 dp->link_train.training_lane);
549 if (retval)
550 return retval;
551
552 return retval;
Jingoo Hane9474be2012-02-03 18:01:55 +0900553}
554
555static int exynos_dp_process_equalizer_training(struct exynos_dp_device *dp)
556{
Sean Paulace2d7f2012-10-31 23:21:00 +0000557 int lane, lane_count, retval;
Jingoo Hane9474be2012-02-03 18:01:55 +0900558 u32 reg;
Sean Paulfadec4b2012-10-31 23:21:00 +0000559 u8 link_align, link_status[2], adjust_request[2];
Jingoo Hane9474be2012-02-03 18:01:55 +0900560
Jingoo Hana2c81bc2012-07-18 18:50:59 +0900561 usleep_range(400, 401);
Jingoo Hane9474be2012-02-03 18:01:55 +0900562
Jingoo Hane9474be2012-02-03 18:01:55 +0900563 lane_count = dp->link_train.lane_count;
564
Sean Paulfadec4b2012-10-31 23:21:00 +0000565 retval = exynos_dp_read_bytes_from_dpcd(dp,
Jingoo Han073ea2a2014-05-07 20:44:51 +0900566 DP_LANE0_1_STATUS, 2, link_status);
Sean Paulace2d7f2012-10-31 23:21:00 +0000567 if (retval)
568 return retval;
Jingoo Hand5c0eed2012-07-19 13:52:59 +0900569
Sean Paulfadec4b2012-10-31 23:21:00 +0000570 if (exynos_dp_clock_recovery_ok(link_status, lane_count)) {
571 exynos_dp_reduce_link_rate(dp);
572 return -EIO;
Jingoo Hane9474be2012-02-03 18:01:55 +0900573 }
574
Sean Paulfadec4b2012-10-31 23:21:00 +0000575 retval = exynos_dp_read_bytes_from_dpcd(dp,
Jingoo Han073ea2a2014-05-07 20:44:51 +0900576 DP_ADJUST_REQUEST_LANE0_1, 2, adjust_request);
Sean Paulfadec4b2012-10-31 23:21:00 +0000577 if (retval)
578 return retval;
Jingoo Hand5c0eed2012-07-19 13:52:59 +0900579
Sean Paulfadec4b2012-10-31 23:21:00 +0000580 retval = exynos_dp_read_byte_from_dpcd(dp,
Jingoo Han073ea2a2014-05-07 20:44:51 +0900581 DP_LANE_ALIGN_STATUS_UPDATED, &link_align);
Sean Paulfadec4b2012-10-31 23:21:00 +0000582 if (retval)
583 return retval;
584
585 exynos_dp_get_adjust_training_lane(dp, adjust_request);
586
587 if (!exynos_dp_channel_eq_ok(link_status, link_align, lane_count)) {
588 /* traing pattern Set to Normal */
589 exynos_dp_training_pattern_dis(dp);
590
591 dev_info(dp->dev, "Link Training success!\n");
592
593 exynos_dp_get_link_bandwidth(dp, &reg);
594 dp->link_train.link_rate = reg;
595 dev_dbg(dp->dev, "final bandwidth = %.2x\n",
596 dp->link_train.link_rate);
597
598 exynos_dp_get_lane_count(dp, &reg);
599 dp->link_train.lane_count = reg;
600 dev_dbg(dp->dev, "final lane count = %.2x\n",
601 dp->link_train.lane_count);
602
603 /* set enhanced mode if available */
604 exynos_dp_set_enhanced_mode(dp);
605 dp->link_train.lt_state = FINISHED;
606
607 return 0;
608 }
609
610 /* not all locked */
611 dp->link_train.eq_loop++;
612
613 if (dp->link_train.eq_loop > MAX_EQ_LOOP) {
614 dev_err(dp->dev, "EQ Max loop\n");
615 exynos_dp_reduce_link_rate(dp);
616 return -EIO;
617 }
618
619 for (lane = 0; lane < lane_count; lane++)
620 exynos_dp_set_lane_link_training(dp,
621 dp->link_train.training_lane[lane], lane);
622
Jingoo Han073ea2a2014-05-07 20:44:51 +0900623 retval = exynos_dp_write_bytes_to_dpcd(dp, DP_TRAINING_LANE0_SET,
Sean Paulfadec4b2012-10-31 23:21:00 +0000624 lane_count, dp->link_train.training_lane);
625
626 return retval;
Jingoo Hane9474be2012-02-03 18:01:55 +0900627}
628
629static void exynos_dp_get_max_rx_bandwidth(struct exynos_dp_device *dp,
Jingoo Hand5c0eed2012-07-19 13:52:59 +0900630 u8 *bandwidth)
Jingoo Hane9474be2012-02-03 18:01:55 +0900631{
632 u8 data;
633
634 /*
635 * For DP rev.1.1, Maximum link rate of Main Link lanes
636 * 0x06 = 1.62 Gbps, 0x0a = 2.7 Gbps
637 */
Jingoo Han073ea2a2014-05-07 20:44:51 +0900638 exynos_dp_read_byte_from_dpcd(dp, DP_MAX_LINK_RATE, &data);
Jingoo Hane9474be2012-02-03 18:01:55 +0900639 *bandwidth = data;
640}
641
642static void exynos_dp_get_max_rx_lane_count(struct exynos_dp_device *dp,
Jingoo Hand5c0eed2012-07-19 13:52:59 +0900643 u8 *lane_count)
Jingoo Hane9474be2012-02-03 18:01:55 +0900644{
645 u8 data;
646
647 /*
648 * For DP rev.1.1, Maximum number of Main Link lanes
649 * 0x01 = 1 lane, 0x02 = 2 lanes, 0x04 = 4 lanes
650 */
Jingoo Han073ea2a2014-05-07 20:44:51 +0900651 exynos_dp_read_byte_from_dpcd(dp, DP_MAX_LANE_COUNT, &data);
Jingoo Hane9474be2012-02-03 18:01:55 +0900652 *lane_count = DPCD_MAX_LANE_COUNT(data);
653}
654
655static void exynos_dp_init_training(struct exynos_dp_device *dp,
656 enum link_lane_count_type max_lane,
657 enum link_rate_type max_rate)
658{
659 /*
660 * MACRO_RST must be applied after the PLL_LOCK to avoid
661 * the DP inter pair skew issue for at least 10 us
662 */
663 exynos_dp_reset_macro(dp);
664
665 /* Initialize by reading RX's DPCD */
666 exynos_dp_get_max_rx_bandwidth(dp, &dp->link_train.link_rate);
667 exynos_dp_get_max_rx_lane_count(dp, &dp->link_train.lane_count);
668
669 if ((dp->link_train.link_rate != LINK_RATE_1_62GBPS) &&
670 (dp->link_train.link_rate != LINK_RATE_2_70GBPS)) {
671 dev_err(dp->dev, "Rx Max Link Rate is abnormal :%x !\n",
672 dp->link_train.link_rate);
673 dp->link_train.link_rate = LINK_RATE_1_62GBPS;
674 }
675
676 if (dp->link_train.lane_count == 0) {
677 dev_err(dp->dev, "Rx Max Lane count is abnormal :%x !\n",
678 dp->link_train.lane_count);
679 dp->link_train.lane_count = (u8)LANE_COUNT1;
680 }
681
682 /* Setup TX lane count & rate */
683 if (dp->link_train.lane_count > max_lane)
684 dp->link_train.lane_count = max_lane;
685 if (dp->link_train.link_rate > max_rate)
686 dp->link_train.link_rate = max_rate;
687
688 /* All DP analog module power up */
689 exynos_dp_set_analog_power_down(dp, POWER_ALL, 0);
690}
691
692static int exynos_dp_sw_link_training(struct exynos_dp_device *dp)
693{
Sean Paulace2d7f2012-10-31 23:21:00 +0000694 int retval = 0, training_finished = 0;
Jingoo Hane9474be2012-02-03 18:01:55 +0900695
696 dp->link_train.lt_state = START;
697
698 /* Process here */
Sean Paulace2d7f2012-10-31 23:21:00 +0000699 while (!retval && !training_finished) {
Jingoo Hane9474be2012-02-03 18:01:55 +0900700 switch (dp->link_train.lt_state) {
701 case START:
Sean Paulace2d7f2012-10-31 23:21:00 +0000702 retval = exynos_dp_link_start(dp);
703 if (retval)
704 dev_err(dp->dev, "LT link start failed!\n");
Jingoo Hane9474be2012-02-03 18:01:55 +0900705 break;
706 case CLOCK_RECOVERY:
Jingoo Hand5c0eed2012-07-19 13:52:59 +0900707 retval = exynos_dp_process_clock_recovery(dp);
708 if (retval)
709 dev_err(dp->dev, "LT CR failed!\n");
Jingoo Hane9474be2012-02-03 18:01:55 +0900710 break;
711 case EQUALIZER_TRAINING:
Jingoo Hand5c0eed2012-07-19 13:52:59 +0900712 retval = exynos_dp_process_equalizer_training(dp);
713 if (retval)
714 dev_err(dp->dev, "LT EQ failed!\n");
Jingoo Hane9474be2012-02-03 18:01:55 +0900715 break;
716 case FINISHED:
717 training_finished = 1;
718 break;
719 case FAILED:
720 return -EREMOTEIO;
721 }
722 }
Sean Paulace2d7f2012-10-31 23:21:00 +0000723 if (retval)
724 dev_err(dp->dev, "eDP link training failed (%d)\n", retval);
Jingoo Hane9474be2012-02-03 18:01:55 +0900725
726 return retval;
727}
728
729static int exynos_dp_set_link_train(struct exynos_dp_device *dp,
730 u32 count,
731 u32 bwtype)
732{
733 int i;
734 int retval;
735
736 for (i = 0; i < DP_TIMEOUT_LOOP_COUNT; i++) {
737 exynos_dp_init_training(dp, count, bwtype);
738 retval = exynos_dp_sw_link_training(dp);
739 if (retval == 0)
740 break;
741
Jingoo Hana2c81bc2012-07-18 18:50:59 +0900742 usleep_range(100, 110);
Jingoo Hane9474be2012-02-03 18:01:55 +0900743 }
744
745 return retval;
746}
747
Ajay Kumar3fcb6eb2012-11-09 14:05:06 +0900748static int exynos_dp_config_video(struct exynos_dp_device *dp)
Jingoo Hane9474be2012-02-03 18:01:55 +0900749{
750 int retval = 0;
751 int timeout_loop = 0;
752 int done_count = 0;
753
Ajay Kumar3fcb6eb2012-11-09 14:05:06 +0900754 exynos_dp_config_video_slave_mode(dp);
Jingoo Hane9474be2012-02-03 18:01:55 +0900755
Ajay Kumar3fcb6eb2012-11-09 14:05:06 +0900756 exynos_dp_set_video_color_format(dp);
Jingoo Hane9474be2012-02-03 18:01:55 +0900757
758 if (exynos_dp_get_pll_lock_status(dp) == PLL_UNLOCKED) {
759 dev_err(dp->dev, "PLL is not locked yet.\n");
760 return -EINVAL;
761 }
762
763 for (;;) {
764 timeout_loop++;
765 if (exynos_dp_is_slave_video_stream_clock_on(dp) == 0)
766 break;
767 if (DP_TIMEOUT_LOOP_COUNT < timeout_loop) {
768 dev_err(dp->dev, "Timeout of video streamclk ok\n");
769 return -ETIMEDOUT;
770 }
771
Jingoo Hana2c81bc2012-07-18 18:50:59 +0900772 usleep_range(1, 2);
Jingoo Hane9474be2012-02-03 18:01:55 +0900773 }
774
775 /* Set to use the register calculated M/N video */
776 exynos_dp_set_video_cr_mn(dp, CALCULATED_M, 0, 0);
777
778 /* For video bist, Video timing must be generated by register */
779 exynos_dp_set_video_timing_mode(dp, VIDEO_TIMING_FROM_CAPTURE);
780
781 /* Disable video mute */
782 exynos_dp_enable_video_mute(dp, 0);
783
784 /* Configure video slave mode */
785 exynos_dp_enable_video_master(dp, 0);
786
787 /* Enable video */
788 exynos_dp_start_video(dp);
789
790 timeout_loop = 0;
791
792 for (;;) {
793 timeout_loop++;
794 if (exynos_dp_is_video_stream_on(dp) == 0) {
795 done_count++;
796 if (done_count > 10)
797 break;
798 } else if (done_count) {
799 done_count = 0;
800 }
801 if (DP_TIMEOUT_LOOP_COUNT < timeout_loop) {
802 dev_err(dp->dev, "Timeout of video streamclk ok\n");
803 return -ETIMEDOUT;
804 }
805
Jingoo Hana2c81bc2012-07-18 18:50:59 +0900806 usleep_range(1000, 1001);
Jingoo Hane9474be2012-02-03 18:01:55 +0900807 }
808
809 if (retval != 0)
810 dev_err(dp->dev, "Video stream is not detected!\n");
811
812 return retval;
813}
814
815static void exynos_dp_enable_scramble(struct exynos_dp_device *dp, bool enable)
816{
817 u8 data;
818
819 if (enable) {
820 exynos_dp_enable_scrambling(dp);
821
822 exynos_dp_read_byte_from_dpcd(dp,
Jingoo Han073ea2a2014-05-07 20:44:51 +0900823 DP_TRAINING_PATTERN_SET,
Jingoo Hane9474be2012-02-03 18:01:55 +0900824 &data);
825 exynos_dp_write_byte_to_dpcd(dp,
Jingoo Han073ea2a2014-05-07 20:44:51 +0900826 DP_TRAINING_PATTERN_SET,
827 (u8)(data & ~DP_LINK_SCRAMBLING_DISABLE));
Jingoo Hane9474be2012-02-03 18:01:55 +0900828 } else {
829 exynos_dp_disable_scrambling(dp);
830
831 exynos_dp_read_byte_from_dpcd(dp,
Jingoo Han073ea2a2014-05-07 20:44:51 +0900832 DP_TRAINING_PATTERN_SET,
Jingoo Hane9474be2012-02-03 18:01:55 +0900833 &data);
834 exynos_dp_write_byte_to_dpcd(dp,
Jingoo Han073ea2a2014-05-07 20:44:51 +0900835 DP_TRAINING_PATTERN_SET,
836 (u8)(data | DP_LINK_SCRAMBLING_DISABLE));
Jingoo Hane9474be2012-02-03 18:01:55 +0900837 }
838}
839
840static irqreturn_t exynos_dp_irq_handler(int irq, void *arg)
841{
842 struct exynos_dp_device *dp = arg;
843
Sean Paulc30ffb92012-11-01 19:13:46 +0900844 enum dp_irq_type irq_type;
845
846 irq_type = exynos_dp_get_irq_type(dp);
847 switch (irq_type) {
848 case DP_IRQ_TYPE_HP_CABLE_IN:
849 dev_dbg(dp->dev, "Received irq - cable in\n");
850 schedule_work(&dp->hotplug_work);
851 exynos_dp_clear_hotplug_interrupts(dp);
852 break;
853 case DP_IRQ_TYPE_HP_CABLE_OUT:
854 dev_dbg(dp->dev, "Received irq - cable out\n");
855 exynos_dp_clear_hotplug_interrupts(dp);
856 break;
857 case DP_IRQ_TYPE_HP_CHANGE:
858 /*
859 * We get these change notifications once in a while, but there
860 * is nothing we can do with them. Just ignore it for now and
861 * only handle cable changes.
862 */
863 dev_dbg(dp->dev, "Received irq - hotplug change; ignoring.\n");
864 exynos_dp_clear_hotplug_interrupts(dp);
865 break;
866 default:
867 dev_err(dp->dev, "Received irq - unknown type!\n");
868 break;
869 }
Jingoo Hane9474be2012-02-03 18:01:55 +0900870 return IRQ_HANDLED;
871}
872
Sean Paul784fa9a2012-11-09 13:55:08 +0900873static void exynos_dp_hotplug(struct work_struct *work)
874{
875 struct exynos_dp_device *dp;
Sean Paul784fa9a2012-11-09 13:55:08 +0900876
877 dp = container_of(work, struct exynos_dp_device, hotplug_work);
878
Ajay Kumar4deabfa2014-07-31 23:12:13 +0530879 if (dp->drm_dev)
880 drm_helper_hpd_irq_event(dp->drm_dev);
881}
882
883static void exynos_dp_commit(struct exynos_drm_display *display)
884{
885 struct exynos_dp_device *dp = display->ctx;
886 int ret;
887
Ajay Kumar5f1dcd82014-07-31 23:12:14 +0530888 /* Keep the panel disabled while we configure video */
889 if (dp->panel) {
890 if (drm_panel_disable(dp->panel))
891 DRM_ERROR("failed to disable the panel\n");
892 }
893
Sean Paul784fa9a2012-11-09 13:55:08 +0900894 ret = exynos_dp_detect_hpd(dp);
895 if (ret) {
Sean Paulc30ffb92012-11-01 19:13:46 +0900896 /* Cable has been disconnected, we're done */
Sean Paul784fa9a2012-11-09 13:55:08 +0900897 return;
898 }
899
900 ret = exynos_dp_handle_edid(dp);
901 if (ret) {
902 dev_err(dp->dev, "unable to handle edid\n");
903 return;
904 }
905
906 ret = exynos_dp_set_link_train(dp, dp->video_info->lane_count,
907 dp->video_info->link_rate);
908 if (ret) {
909 dev_err(dp->dev, "unable to do link train\n");
910 return;
911 }
912
913 exynos_dp_enable_scramble(dp, 1);
914 exynos_dp_enable_rx_to_enhanced_mode(dp, 1);
915 exynos_dp_enable_enhanced_mode(dp, 1);
916
917 exynos_dp_set_lane_count(dp, dp->video_info->lane_count);
918 exynos_dp_set_link_bandwidth(dp, dp->video_info->link_rate);
919
920 exynos_dp_init_video(dp);
Ajay Kumar3fcb6eb2012-11-09 14:05:06 +0900921 ret = exynos_dp_config_video(dp);
Sean Paul784fa9a2012-11-09 13:55:08 +0900922 if (ret)
923 dev_err(dp->dev, "unable to config video\n");
Ajay Kumar5f1dcd82014-07-31 23:12:14 +0530924
925 /* Safe to enable the panel now */
926 if (dp->panel) {
927 if (drm_panel_enable(dp->panel))
928 DRM_ERROR("failed to enable the panel\n");
929 }
Sean Paul784fa9a2012-11-09 13:55:08 +0900930}
931
Sean Paulcaa5d1e2014-01-30 16:19:30 -0500932static enum drm_connector_status exynos_dp_detect(
933 struct drm_connector *connector, bool force)
Sean Paul1417f102014-01-30 16:19:23 -0500934{
Sean Paulcaa5d1e2014-01-30 16:19:30 -0500935 return connector_status_connected;
Sean Paul1417f102014-01-30 16:19:23 -0500936}
937
Sean Paulcaa5d1e2014-01-30 16:19:30 -0500938static void exynos_dp_connector_destroy(struct drm_connector *connector)
939{
Andrzej Hajda7c61b1e2014-09-09 15:16:12 +0200940 drm_connector_unregister(connector);
941 drm_connector_cleanup(connector);
Sean Paulcaa5d1e2014-01-30 16:19:30 -0500942}
943
944static struct drm_connector_funcs exynos_dp_connector_funcs = {
945 .dpms = drm_helper_connector_dpms,
946 .fill_modes = drm_helper_probe_single_connector_modes,
947 .detect = exynos_dp_detect,
948 .destroy = exynos_dp_connector_destroy,
949};
950
951static int exynos_dp_get_modes(struct drm_connector *connector)
952{
953 struct exynos_dp_device *dp = ctx_from_connector(connector);
954 struct drm_display_mode *mode;
955
Ajay Kumar5f1dcd82014-07-31 23:12:14 +0530956 if (dp->panel)
957 return drm_panel_get_modes(dp->panel);
958
Sean Paulcaa5d1e2014-01-30 16:19:30 -0500959 mode = drm_mode_create(connector->dev);
960 if (!mode) {
961 DRM_ERROR("failed to create a new display mode.\n");
962 return 0;
963 }
964
Ajay Kumar5f1dcd82014-07-31 23:12:14 +0530965 drm_display_mode_from_videomode(&dp->priv.vm, mode);
966 mode->width_mm = dp->priv.width_mm;
967 mode->height_mm = dp->priv.height_mm;
Sean Paulcaa5d1e2014-01-30 16:19:30 -0500968 connector->display_info.width_mm = mode->width_mm;
969 connector->display_info.height_mm = mode->height_mm;
970
971 mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
972 drm_mode_set_name(mode);
973 drm_mode_probed_add(connector, mode);
974
975 return 1;
976}
977
Sean Paulcaa5d1e2014-01-30 16:19:30 -0500978static struct drm_encoder *exynos_dp_best_encoder(
979 struct drm_connector *connector)
980{
981 struct exynos_dp_device *dp = ctx_from_connector(connector);
982
983 return dp->encoder;
984}
985
986static struct drm_connector_helper_funcs exynos_dp_connector_helper_funcs = {
987 .get_modes = exynos_dp_get_modes,
Sean Paulcaa5d1e2014-01-30 16:19:30 -0500988 .best_encoder = exynos_dp_best_encoder,
989};
990
Sean Paul1634ba22014-02-24 19:20:15 +0900991static bool find_bridge(const char *compat, struct bridge_init *bridge)
992{
993 bridge->client = NULL;
994 bridge->node = of_find_compatible_node(NULL, NULL, compat);
995 if (!bridge->node)
996 return false;
997
998 bridge->client = of_find_i2c_device_by_node(bridge->node);
999 if (!bridge->client)
1000 return false;
1001
1002 return true;
1003}
1004
1005/* returns the number of bridges attached */
1006static int exynos_drm_attach_lcd_bridge(struct drm_device *dev,
1007 struct drm_encoder *encoder)
1008{
1009 struct bridge_init bridge;
1010 int ret;
1011
1012 if (find_bridge("nxp,ptn3460", &bridge)) {
1013 ret = ptn3460_init(dev, encoder, bridge.client, bridge.node);
1014 if (!ret)
1015 return 1;
1016 }
1017 return 0;
1018}
1019
Sean Paulcaa5d1e2014-01-30 16:19:30 -05001020static int exynos_dp_create_connector(struct exynos_drm_display *display,
1021 struct drm_encoder *encoder)
Sean Paul1417f102014-01-30 16:19:23 -05001022{
Sean Paulcaa5d1e2014-01-30 16:19:30 -05001023 struct exynos_dp_device *dp = display->ctx;
1024 struct drm_connector *connector = &dp->connector;
1025 int ret;
1026
1027 dp->encoder = encoder;
Sean Paul1634ba22014-02-24 19:20:15 +09001028
1029 /* Pre-empt DP connector creation if there's a bridge */
1030 ret = exynos_drm_attach_lcd_bridge(dp->drm_dev, encoder);
1031 if (ret)
1032 return 0;
1033
Sean Paulcaa5d1e2014-01-30 16:19:30 -05001034 connector->polled = DRM_CONNECTOR_POLL_HPD;
1035
1036 ret = drm_connector_init(dp->drm_dev, connector,
1037 &exynos_dp_connector_funcs, DRM_MODE_CONNECTOR_eDP);
1038 if (ret) {
1039 DRM_ERROR("Failed to initialize connector with drm\n");
1040 return ret;
1041 }
1042
1043 drm_connector_helper_add(connector, &exynos_dp_connector_helper_funcs);
Thomas Wood34ea3d32014-05-29 16:57:41 +01001044 drm_connector_register(connector);
Sean Paulcaa5d1e2014-01-30 16:19:30 -05001045 drm_mode_connector_attach_encoder(connector, encoder);
1046
Ajay Kumar5f1dcd82014-07-31 23:12:14 +05301047 if (dp->panel)
1048 ret = drm_panel_attach(dp->panel, &dp->connector);
1049
1050 return ret;
Sean Paul1417f102014-01-30 16:19:23 -05001051}
1052
Sean Paul12f5ad62014-01-30 16:19:25 -05001053static void exynos_dp_phy_init(struct exynos_dp_device *dp)
1054{
Vivek Gautamb128aef2014-11-12 15:12:10 +05301055 if (dp->phy)
Sean Paul12f5ad62014-01-30 16:19:25 -05001056 phy_power_on(dp->phy);
Sean Paul12f5ad62014-01-30 16:19:25 -05001057}
1058
1059static void exynos_dp_phy_exit(struct exynos_dp_device *dp)
1060{
Vivek Gautamb128aef2014-11-12 15:12:10 +05301061 if (dp->phy)
Sean Paul12f5ad62014-01-30 16:19:25 -05001062 phy_power_off(dp->phy);
Sean Paul12f5ad62014-01-30 16:19:25 -05001063}
1064
Ajay Kumar4deabfa2014-07-31 23:12:13 +05301065static void exynos_dp_poweron(struct exynos_drm_display *display)
Sean Paul12f5ad62014-01-30 16:19:25 -05001066{
Ajay Kumar4deabfa2014-07-31 23:12:13 +05301067 struct exynos_dp_device *dp = display->ctx;
1068
Sean Paul12f5ad62014-01-30 16:19:25 -05001069 if (dp->dpms_mode == DRM_MODE_DPMS_ON)
1070 return;
1071
Ajay Kumar5f1dcd82014-07-31 23:12:14 +05301072 if (dp->panel) {
1073 if (drm_panel_prepare(dp->panel)) {
1074 DRM_ERROR("failed to setup the panel\n");
1075 return;
1076 }
1077 }
1078
Sean Paul12f5ad62014-01-30 16:19:25 -05001079 clk_prepare_enable(dp->clock);
1080 exynos_dp_phy_init(dp);
1081 exynos_dp_init_dp(dp);
1082 enable_irq(dp->irq);
Ajay Kumar4deabfa2014-07-31 23:12:13 +05301083 exynos_dp_commit(display);
Sean Paul12f5ad62014-01-30 16:19:25 -05001084}
1085
Ajay Kumar4deabfa2014-07-31 23:12:13 +05301086static void exynos_dp_poweroff(struct exynos_drm_display *display)
Sean Paul12f5ad62014-01-30 16:19:25 -05001087{
Ajay Kumar4deabfa2014-07-31 23:12:13 +05301088 struct exynos_dp_device *dp = display->ctx;
1089
Sean Paul12f5ad62014-01-30 16:19:25 -05001090 if (dp->dpms_mode != DRM_MODE_DPMS_ON)
1091 return;
1092
Ajay Kumar5f1dcd82014-07-31 23:12:14 +05301093 if (dp->panel) {
1094 if (drm_panel_disable(dp->panel)) {
1095 DRM_ERROR("failed to disable the panel\n");
1096 return;
1097 }
1098 }
1099
Sean Paul12f5ad62014-01-30 16:19:25 -05001100 disable_irq(dp->irq);
1101 flush_work(&dp->hotplug_work);
1102 exynos_dp_phy_exit(dp);
1103 clk_disable_unprepare(dp->clock);
Ajay Kumar5f1dcd82014-07-31 23:12:14 +05301104
1105 if (dp->panel) {
1106 if (drm_panel_unprepare(dp->panel))
1107 DRM_ERROR("failed to turnoff the panel\n");
1108 }
Sean Paul12f5ad62014-01-30 16:19:25 -05001109}
1110
1111static void exynos_dp_dpms(struct exynos_drm_display *display, int mode)
1112{
1113 struct exynos_dp_device *dp = display->ctx;
1114
1115 switch (mode) {
1116 case DRM_MODE_DPMS_ON:
Ajay Kumar4deabfa2014-07-31 23:12:13 +05301117 exynos_dp_poweron(display);
Sean Paul12f5ad62014-01-30 16:19:25 -05001118 break;
1119 case DRM_MODE_DPMS_STANDBY:
1120 case DRM_MODE_DPMS_SUSPEND:
1121 case DRM_MODE_DPMS_OFF:
Ajay Kumar4deabfa2014-07-31 23:12:13 +05301122 exynos_dp_poweroff(display);
Sean Paul12f5ad62014-01-30 16:19:25 -05001123 break;
1124 default:
1125 break;
Damien Lespiau10d9b4e2014-06-09 14:21:08 +01001126 }
Sean Paul12f5ad62014-01-30 16:19:25 -05001127 dp->dpms_mode = mode;
1128}
1129
Sean Paul1417f102014-01-30 16:19:23 -05001130static struct exynos_drm_display_ops exynos_dp_display_ops = {
Sean Paulcaa5d1e2014-01-30 16:19:30 -05001131 .create_connector = exynos_dp_create_connector,
Sean Paul12f5ad62014-01-30 16:19:25 -05001132 .dpms = exynos_dp_dpms,
Ajay Kumar4deabfa2014-07-31 23:12:13 +05301133 .commit = exynos_dp_commit,
Sean Paul1417f102014-01-30 16:19:23 -05001134};
1135
1136static struct exynos_drm_display exynos_dp_display = {
1137 .type = EXYNOS_DISPLAY_TYPE_LCD,
1138 .ops = &exynos_dp_display_ops,
1139};
1140
Jingoo Hanf9b1e012013-10-16 21:58:15 +05301141static struct video_info *exynos_dp_dt_parse_pdata(struct device *dev)
Ajay Kumarc4e235c2012-10-13 05:48:00 +09001142{
1143 struct device_node *dp_node = dev->of_node;
Ajay Kumarc4e235c2012-10-13 05:48:00 +09001144 struct video_info *dp_video_config;
1145
Ajay Kumarc4e235c2012-10-13 05:48:00 +09001146 dp_video_config = devm_kzalloc(dev,
1147 sizeof(*dp_video_config), GFP_KERNEL);
Jingoo Han7a5b68272014-04-17 19:08:14 +09001148 if (!dp_video_config)
Ajay Kumarc4e235c2012-10-13 05:48:00 +09001149 return ERR_PTR(-ENOMEM);
Ajay Kumarc4e235c2012-10-13 05:48:00 +09001150
1151 dp_video_config->h_sync_polarity =
1152 of_property_read_bool(dp_node, "hsync-active-high");
1153
1154 dp_video_config->v_sync_polarity =
1155 of_property_read_bool(dp_node, "vsync-active-high");
1156
1157 dp_video_config->interlaced =
1158 of_property_read_bool(dp_node, "interlaced");
1159
1160 if (of_property_read_u32(dp_node, "samsung,color-space",
1161 &dp_video_config->color_space)) {
1162 dev_err(dev, "failed to get color-space\n");
1163 return ERR_PTR(-EINVAL);
1164 }
1165
1166 if (of_property_read_u32(dp_node, "samsung,dynamic-range",
1167 &dp_video_config->dynamic_range)) {
1168 dev_err(dev, "failed to get dynamic-range\n");
1169 return ERR_PTR(-EINVAL);
1170 }
1171
1172 if (of_property_read_u32(dp_node, "samsung,ycbcr-coeff",
1173 &dp_video_config->ycbcr_coeff)) {
1174 dev_err(dev, "failed to get ycbcr-coeff\n");
1175 return ERR_PTR(-EINVAL);
1176 }
1177
1178 if (of_property_read_u32(dp_node, "samsung,color-depth",
1179 &dp_video_config->color_depth)) {
1180 dev_err(dev, "failed to get color-depth\n");
1181 return ERR_PTR(-EINVAL);
1182 }
1183
1184 if (of_property_read_u32(dp_node, "samsung,link-rate",
1185 &dp_video_config->link_rate)) {
1186 dev_err(dev, "failed to get link-rate\n");
1187 return ERR_PTR(-EINVAL);
1188 }
1189
1190 if (of_property_read_u32(dp_node, "samsung,lane-count",
1191 &dp_video_config->lane_count)) {
1192 dev_err(dev, "failed to get lane-count\n");
1193 return ERR_PTR(-EINVAL);
1194 }
1195
Jingoo Hanf9b1e012013-10-16 21:58:15 +05301196 return dp_video_config;
Ajay Kumarc4e235c2012-10-13 05:48:00 +09001197}
1198
Sean Paul1417f102014-01-30 16:19:23 -05001199static int exynos_dp_dt_parse_panel(struct exynos_dp_device *dp)
1200{
1201 int ret;
1202
Ajay Kumar5f1dcd82014-07-31 23:12:14 +05301203 ret = of_get_videomode(dp->dev->of_node, &dp->priv.vm,
Sean Paul1417f102014-01-30 16:19:23 -05001204 OF_USE_NATIVE_MODE);
1205 if (ret) {
1206 DRM_ERROR("failed: of_get_videomode() : %d\n", ret);
1207 return ret;
1208 }
1209 return 0;
1210}
1211
Inki Daef37cd5e2014-05-09 14:25:20 +09001212static int exynos_dp_bind(struct device *dev, struct device *master, void *data)
Jingoo Hane9474be2012-02-03 18:01:55 +09001213{
Inki Daef37cd5e2014-05-09 14:25:20 +09001214 struct platform_device *pdev = to_platform_device(dev);
1215 struct drm_device *drm_dev = data;
Jingoo Hane9474be2012-02-03 18:01:55 +09001216 struct resource *res;
Ajay Kumar5f1dcd82014-07-31 23:12:14 +05301217 struct exynos_dp_device *dp = exynos_dp_display.ctx;
Andrew Brestickerb8b52472014-04-22 04:09:10 +05301218 unsigned int irq_flags;
Jingoo Hane9474be2012-02-03 18:01:55 +09001219 int ret = 0;
1220
Jingoo Hane9474be2012-02-03 18:01:55 +09001221 dp->dev = &pdev->dev;
Sean Paul12f5ad62014-01-30 16:19:25 -05001222 dp->dpms_mode = DRM_MODE_DPMS_OFF;
Jingoo Hane9474be2012-02-03 18:01:55 +09001223
Jingoo Hanf9b1e012013-10-16 21:58:15 +05301224 dp->video_info = exynos_dp_dt_parse_pdata(&pdev->dev);
1225 if (IS_ERR(dp->video_info))
1226 return PTR_ERR(dp->video_info);
Ajay Kumarc4e235c2012-10-13 05:48:00 +09001227
Vivek Gautamb128aef2014-11-12 15:12:10 +05301228 dp->phy = devm_phy_get(dp->dev, "dp");
1229 if (IS_ERR(dp->phy)) {
1230 dev_err(dp->dev, "no DP phy configured\n");
1231 ret = PTR_ERR(dp->phy);
1232 if (ret) {
1233 /*
1234 * phy itself is not enabled, so we can move forward
1235 * assigning NULL to phy pointer.
1236 */
1237 if (ret == -ENOSYS || ret == -ENODEV)
1238 dp->phy = NULL;
1239 else
1240 return ret;
1241 }
1242 }
Ajay Kumarc4e235c2012-10-13 05:48:00 +09001243
Ajay Kumar5f1dcd82014-07-31 23:12:14 +05301244 if (!dp->panel) {
1245 ret = exynos_dp_dt_parse_panel(dp);
1246 if (ret)
1247 return ret;
1248 }
Sean Paul1417f102014-01-30 16:19:23 -05001249
Damien Cassoud913f362012-08-01 18:20:39 +02001250 dp->clock = devm_clk_get(&pdev->dev, "dp");
Jingoo Hane9474be2012-02-03 18:01:55 +09001251 if (IS_ERR(dp->clock)) {
1252 dev_err(&pdev->dev, "failed to get clock\n");
Jingoo Han4d10ecf82012-05-25 16:20:45 +09001253 return PTR_ERR(dp->clock);
Jingoo Hane9474be2012-02-03 18:01:55 +09001254 }
1255
Jingoo Han37414fb2012-10-04 15:45:14 +09001256 clk_prepare_enable(dp->clock);
Jingoo Hane9474be2012-02-03 18:01:55 +09001257
1258 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Jingoo Hane9474be2012-02-03 18:01:55 +09001259
Thierry Redingbc3bad12013-01-21 11:09:23 +01001260 dp->reg_base = devm_ioremap_resource(&pdev->dev, res);
1261 if (IS_ERR(dp->reg_base))
1262 return PTR_ERR(dp->reg_base);
Jingoo Hane9474be2012-02-03 18:01:55 +09001263
Andrew Brestickerb8b52472014-04-22 04:09:10 +05301264 dp->hpd_gpio = of_get_named_gpio(dev->of_node, "samsung,hpd-gpio", 0);
1265
1266 if (gpio_is_valid(dp->hpd_gpio)) {
1267 /*
1268 * Set up the hotplug GPIO from the device tree as an interrupt.
1269 * Simply specifying a different interrupt in the device tree
1270 * doesn't work since we handle hotplug rather differently when
1271 * using a GPIO. We also need the actual GPIO specifier so
1272 * that we can get the current state of the GPIO.
1273 */
1274 ret = devm_gpio_request_one(&pdev->dev, dp->hpd_gpio, GPIOF_IN,
1275 "hpd_gpio");
1276 if (ret) {
1277 dev_err(&pdev->dev, "failed to get hpd gpio\n");
1278 return ret;
1279 }
1280 dp->irq = gpio_to_irq(dp->hpd_gpio);
1281 irq_flags = IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING;
1282 } else {
1283 dp->hpd_gpio = -ENODEV;
1284 dp->irq = platform_get_irq(pdev, 0);
1285 irq_flags = 0;
1286 }
1287
Sean Paul1cefc1d2012-10-31 23:21:00 +00001288 if (dp->irq == -ENXIO) {
Jingoo Hane9474be2012-02-03 18:01:55 +09001289 dev_err(&pdev->dev, "failed to get irq\n");
Damien Cassoud913f362012-08-01 18:20:39 +02001290 return -ENODEV;
Jingoo Hane9474be2012-02-03 18:01:55 +09001291 }
1292
Sean Paul784fa9a2012-11-09 13:55:08 +09001293 INIT_WORK(&dp->hotplug_work, exynos_dp_hotplug);
1294
Jingoo Hanf9b1e012013-10-16 21:58:15 +05301295 exynos_dp_phy_init(dp);
Jingoo Hane9474be2012-02-03 18:01:55 +09001296
1297 exynos_dp_init_dp(dp);
1298
Andrew Brestickerb8b52472014-04-22 04:09:10 +05301299 ret = devm_request_irq(&pdev->dev, dp->irq, exynos_dp_irq_handler,
1300 irq_flags, "exynos-dp", dp);
Ajay Kumar22ce19c2012-11-09 13:59:09 +09001301 if (ret) {
1302 dev_err(&pdev->dev, "failed to request irq\n");
1303 return ret;
1304 }
Sean Paul12f5ad62014-01-30 16:19:25 -05001305 disable_irq(dp->irq);
Jingoo Hane9474be2012-02-03 18:01:55 +09001306
Inki Daef37cd5e2014-05-09 14:25:20 +09001307 dp->drm_dev = drm_dev;
Sean Paul12f5ad62014-01-30 16:19:25 -05001308
1309 platform_set_drvdata(pdev, &exynos_dp_display);
Sean Paul1417f102014-01-30 16:19:23 -05001310
Inki Daef37cd5e2014-05-09 14:25:20 +09001311 return exynos_drm_create_enc_conn(drm_dev, &exynos_dp_display);
1312}
1313
1314static void exynos_dp_unbind(struct device *dev, struct device *master,
1315 void *data)
1316{
1317 struct exynos_drm_display *display = dev_get_drvdata(dev);
Inki Daef37cd5e2014-05-09 14:25:20 +09001318
1319 exynos_dp_dpms(display, DRM_MODE_DPMS_OFF);
Inki Daef37cd5e2014-05-09 14:25:20 +09001320}
1321
1322static const struct component_ops exynos_dp_ops = {
1323 .bind = exynos_dp_bind,
1324 .unbind = exynos_dp_unbind,
1325};
1326
1327static int exynos_dp_probe(struct platform_device *pdev)
1328{
Ajay Kumar5f1dcd82014-07-31 23:12:14 +05301329 struct device *dev = &pdev->dev;
1330 struct device_node *panel_node;
1331 struct exynos_dp_device *dp;
Inki Daedf5225b2014-05-29 18:28:02 +09001332 int ret;
1333
1334 ret = exynos_drm_component_add(&pdev->dev, EXYNOS_DEVICE_TYPE_CONNECTOR,
1335 exynos_dp_display.type);
1336 if (ret)
1337 return ret;
1338
Ajay Kumar5f1dcd82014-07-31 23:12:14 +05301339 dp = devm_kzalloc(&pdev->dev, sizeof(struct exynos_dp_device),
1340 GFP_KERNEL);
1341 if (!dp)
1342 return -ENOMEM;
1343
1344 panel_node = of_parse_phandle(dev->of_node, "panel", 0);
1345 if (panel_node) {
1346 dp->panel = of_drm_find_panel(panel_node);
1347 of_node_put(panel_node);
1348 if (!dp->panel)
1349 return -EPROBE_DEFER;
1350 }
1351
1352 exynos_dp_display.ctx = dp;
1353
Inki Daedf5225b2014-05-29 18:28:02 +09001354 ret = component_add(&pdev->dev, &exynos_dp_ops);
1355 if (ret)
1356 exynos_drm_component_del(&pdev->dev,
1357 EXYNOS_DEVICE_TYPE_CONNECTOR);
1358
1359 return ret;
Jingoo Hane9474be2012-02-03 18:01:55 +09001360}
1361
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -08001362static int exynos_dp_remove(struct platform_device *pdev)
Jingoo Hane9474be2012-02-03 18:01:55 +09001363{
Inki Daedf5225b2014-05-29 18:28:02 +09001364 component_del(&pdev->dev, &exynos_dp_ops);
1365 exynos_drm_component_del(&pdev->dev, EXYNOS_DEVICE_TYPE_CONNECTOR);
1366
Jingoo Hane9474be2012-02-03 18:01:55 +09001367 return 0;
1368}
1369
1370#ifdef CONFIG_PM_SLEEP
1371static int exynos_dp_suspend(struct device *dev)
1372{
Sean Paul12f5ad62014-01-30 16:19:25 -05001373 struct platform_device *pdev = to_platform_device(dev);
1374 struct exynos_drm_display *display = platform_get_drvdata(pdev);
Jingoo Hane9474be2012-02-03 18:01:55 +09001375
Sean Paul12f5ad62014-01-30 16:19:25 -05001376 exynos_dp_dpms(display, DRM_MODE_DPMS_OFF);
Jingoo Hane9474be2012-02-03 18:01:55 +09001377 return 0;
1378}
1379
1380static int exynos_dp_resume(struct device *dev)
1381{
Sean Paul12f5ad62014-01-30 16:19:25 -05001382 struct platform_device *pdev = to_platform_device(dev);
1383 struct exynos_drm_display *display = platform_get_drvdata(pdev);
Jingoo Hane9474be2012-02-03 18:01:55 +09001384
Sean Paul12f5ad62014-01-30 16:19:25 -05001385 exynos_dp_dpms(display, DRM_MODE_DPMS_ON);
Jingoo Hane9474be2012-02-03 18:01:55 +09001386 return 0;
1387}
1388#endif
1389
1390static const struct dev_pm_ops exynos_dp_pm_ops = {
1391 SET_SYSTEM_SLEEP_PM_OPS(exynos_dp_suspend, exynos_dp_resume)
1392};
1393
Ajay Kumarc4e235c2012-10-13 05:48:00 +09001394static const struct of_device_id exynos_dp_match[] = {
1395 { .compatible = "samsung,exynos5-dp" },
1396 {},
1397};
Sjoerd Simonsbd024b82014-07-30 11:29:41 +09001398MODULE_DEVICE_TABLE(of, exynos_dp_match);
Ajay Kumarc4e235c2012-10-13 05:48:00 +09001399
Sean Paul1417f102014-01-30 16:19:23 -05001400struct platform_driver dp_driver = {
Jingoo Hane9474be2012-02-03 18:01:55 +09001401 .probe = exynos_dp_probe,
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -08001402 .remove = exynos_dp_remove,
Jingoo Hane9474be2012-02-03 18:01:55 +09001403 .driver = {
1404 .name = "exynos-dp",
1405 .owner = THIS_MODULE,
1406 .pm = &exynos_dp_pm_ops,
Jingoo Hanf9b1e012013-10-16 21:58:15 +05301407 .of_match_table = exynos_dp_match,
Jingoo Hane9474be2012-02-03 18:01:55 +09001408 },
1409};
1410
Jingoo Hane9474be2012-02-03 18:01:55 +09001411MODULE_AUTHOR("Jingoo Han <jg1.han@samsung.com>");
1412MODULE_DESCRIPTION("Samsung SoC DP Driver");
Jingoo Han8f589bb2014-06-03 21:46:11 +09001413MODULE_LICENSE("GPL v2");