exynos_dp_core.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417
  1. /*
  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. #include <linux/module.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/err.h>
  15. #include <linux/clk.h>
  16. #include <linux/io.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/of.h>
  19. #include <linux/of_gpio.h>
  20. #include <linux/of_graph.h>
  21. #include <linux/gpio.h>
  22. #include <linux/component.h>
  23. #include <linux/phy/phy.h>
  24. #include <video/of_display_timing.h>
  25. #include <video/of_videomode.h>
  26. #include <drm/drmP.h>
  27. #include <drm/drm_crtc.h>
  28. #include <drm/drm_crtc_helper.h>
  29. #include <drm/drm_atomic_helper.h>
  30. #include <drm/drm_panel.h>
  31. #include "exynos_dp_core.h"
  32. #define ctx_from_connector(c) container_of(c, struct exynos_dp_device, \
  33. connector)
  34. static inline struct exynos_drm_crtc *dp_to_crtc(struct exynos_dp_device *dp)
  35. {
  36. return to_exynos_crtc(dp->encoder->crtc);
  37. }
  38. static inline struct exynos_dp_device *
  39. display_to_dp(struct exynos_drm_display *d)
  40. {
  41. return container_of(d, struct exynos_dp_device, display);
  42. }
  43. struct bridge_init {
  44. struct i2c_client *client;
  45. struct device_node *node;
  46. };
  47. static void exynos_dp_init_dp(struct exynos_dp_device *dp)
  48. {
  49. exynos_dp_reset(dp);
  50. exynos_dp_swreset(dp);
  51. exynos_dp_init_analog_param(dp);
  52. exynos_dp_init_interrupt(dp);
  53. /* SW defined function Normal operation */
  54. exynos_dp_enable_sw_function(dp);
  55. exynos_dp_config_interrupt(dp);
  56. exynos_dp_init_analog_func(dp);
  57. exynos_dp_init_hpd(dp);
  58. exynos_dp_init_aux(dp);
  59. }
  60. static int exynos_dp_detect_hpd(struct exynos_dp_device *dp)
  61. {
  62. int timeout_loop = 0;
  63. while (exynos_dp_get_plug_in_status(dp) != 0) {
  64. timeout_loop++;
  65. if (DP_TIMEOUT_LOOP_COUNT < timeout_loop) {
  66. dev_err(dp->dev, "failed to get hpd plug status\n");
  67. return -ETIMEDOUT;
  68. }
  69. usleep_range(10, 11);
  70. }
  71. return 0;
  72. }
  73. static unsigned char exynos_dp_calc_edid_check_sum(unsigned char *edid_data)
  74. {
  75. int i;
  76. unsigned char sum = 0;
  77. for (i = 0; i < EDID_BLOCK_LENGTH; i++)
  78. sum = sum + edid_data[i];
  79. return sum;
  80. }
  81. static int exynos_dp_read_edid(struct exynos_dp_device *dp)
  82. {
  83. unsigned char edid[EDID_BLOCK_LENGTH * 2];
  84. unsigned int extend_block = 0;
  85. unsigned char sum;
  86. unsigned char test_vector;
  87. int retval;
  88. /*
  89. * EDID device address is 0x50.
  90. * However, if necessary, you must have set upper address
  91. * into E-EDID in I2C device, 0x30.
  92. */
  93. /* Read Extension Flag, Number of 128-byte EDID extension blocks */
  94. retval = exynos_dp_read_byte_from_i2c(dp, I2C_EDID_DEVICE_ADDR,
  95. EDID_EXTENSION_FLAG,
  96. &extend_block);
  97. if (retval)
  98. return retval;
  99. if (extend_block > 0) {
  100. dev_dbg(dp->dev, "EDID data includes a single extension!\n");
  101. /* Read EDID data */
  102. retval = exynos_dp_read_bytes_from_i2c(dp, I2C_EDID_DEVICE_ADDR,
  103. EDID_HEADER_PATTERN,
  104. EDID_BLOCK_LENGTH,
  105. &edid[EDID_HEADER_PATTERN]);
  106. if (retval != 0) {
  107. dev_err(dp->dev, "EDID Read failed!\n");
  108. return -EIO;
  109. }
  110. sum = exynos_dp_calc_edid_check_sum(edid);
  111. if (sum != 0) {
  112. dev_err(dp->dev, "EDID bad checksum!\n");
  113. return -EIO;
  114. }
  115. /* Read additional EDID data */
  116. retval = exynos_dp_read_bytes_from_i2c(dp,
  117. I2C_EDID_DEVICE_ADDR,
  118. EDID_BLOCK_LENGTH,
  119. EDID_BLOCK_LENGTH,
  120. &edid[EDID_BLOCK_LENGTH]);
  121. if (retval != 0) {
  122. dev_err(dp->dev, "EDID Read failed!\n");
  123. return -EIO;
  124. }
  125. sum = exynos_dp_calc_edid_check_sum(&edid[EDID_BLOCK_LENGTH]);
  126. if (sum != 0) {
  127. dev_err(dp->dev, "EDID bad checksum!\n");
  128. return -EIO;
  129. }
  130. exynos_dp_read_byte_from_dpcd(dp, DP_TEST_REQUEST,
  131. &test_vector);
  132. if (test_vector & DP_TEST_LINK_EDID_READ) {
  133. exynos_dp_write_byte_to_dpcd(dp,
  134. DP_TEST_EDID_CHECKSUM,
  135. edid[EDID_BLOCK_LENGTH + EDID_CHECKSUM]);
  136. exynos_dp_write_byte_to_dpcd(dp,
  137. DP_TEST_RESPONSE,
  138. DP_TEST_EDID_CHECKSUM_WRITE);
  139. }
  140. } else {
  141. dev_info(dp->dev, "EDID data does not include any extensions.\n");
  142. /* Read EDID data */
  143. retval = exynos_dp_read_bytes_from_i2c(dp,
  144. I2C_EDID_DEVICE_ADDR,
  145. EDID_HEADER_PATTERN,
  146. EDID_BLOCK_LENGTH,
  147. &edid[EDID_HEADER_PATTERN]);
  148. if (retval != 0) {
  149. dev_err(dp->dev, "EDID Read failed!\n");
  150. return -EIO;
  151. }
  152. sum = exynos_dp_calc_edid_check_sum(edid);
  153. if (sum != 0) {
  154. dev_err(dp->dev, "EDID bad checksum!\n");
  155. return -EIO;
  156. }
  157. exynos_dp_read_byte_from_dpcd(dp,
  158. DP_TEST_REQUEST,
  159. &test_vector);
  160. if (test_vector & DP_TEST_LINK_EDID_READ) {
  161. exynos_dp_write_byte_to_dpcd(dp,
  162. DP_TEST_EDID_CHECKSUM,
  163. edid[EDID_CHECKSUM]);
  164. exynos_dp_write_byte_to_dpcd(dp,
  165. DP_TEST_RESPONSE,
  166. DP_TEST_EDID_CHECKSUM_WRITE);
  167. }
  168. }
  169. dev_dbg(dp->dev, "EDID Read success!\n");
  170. return 0;
  171. }
  172. static int exynos_dp_handle_edid(struct exynos_dp_device *dp)
  173. {
  174. u8 buf[12];
  175. int i;
  176. int retval;
  177. /* Read DPCD DP_DPCD_REV~RECEIVE_PORT1_CAP_1 */
  178. retval = exynos_dp_read_bytes_from_dpcd(dp, DP_DPCD_REV,
  179. 12, buf);
  180. if (retval)
  181. return retval;
  182. /* Read EDID */
  183. for (i = 0; i < 3; i++) {
  184. retval = exynos_dp_read_edid(dp);
  185. if (!retval)
  186. break;
  187. }
  188. return retval;
  189. }
  190. static void exynos_dp_enable_rx_to_enhanced_mode(struct exynos_dp_device *dp,
  191. bool enable)
  192. {
  193. u8 data;
  194. exynos_dp_read_byte_from_dpcd(dp, DP_LANE_COUNT_SET, &data);
  195. if (enable)
  196. exynos_dp_write_byte_to_dpcd(dp, DP_LANE_COUNT_SET,
  197. DP_LANE_COUNT_ENHANCED_FRAME_EN |
  198. DPCD_LANE_COUNT_SET(data));
  199. else
  200. exynos_dp_write_byte_to_dpcd(dp, DP_LANE_COUNT_SET,
  201. DPCD_LANE_COUNT_SET(data));
  202. }
  203. static int exynos_dp_is_enhanced_mode_available(struct exynos_dp_device *dp)
  204. {
  205. u8 data;
  206. int retval;
  207. exynos_dp_read_byte_from_dpcd(dp, DP_MAX_LANE_COUNT, &data);
  208. retval = DPCD_ENHANCED_FRAME_CAP(data);
  209. return retval;
  210. }
  211. static void exynos_dp_set_enhanced_mode(struct exynos_dp_device *dp)
  212. {
  213. u8 data;
  214. data = exynos_dp_is_enhanced_mode_available(dp);
  215. exynos_dp_enable_rx_to_enhanced_mode(dp, data);
  216. exynos_dp_enable_enhanced_mode(dp, data);
  217. }
  218. static void exynos_dp_training_pattern_dis(struct exynos_dp_device *dp)
  219. {
  220. exynos_dp_set_training_pattern(dp, DP_NONE);
  221. exynos_dp_write_byte_to_dpcd(dp,
  222. DP_TRAINING_PATTERN_SET,
  223. DP_TRAINING_PATTERN_DISABLE);
  224. }
  225. static void exynos_dp_set_lane_lane_pre_emphasis(struct exynos_dp_device *dp,
  226. int pre_emphasis, int lane)
  227. {
  228. switch (lane) {
  229. case 0:
  230. exynos_dp_set_lane0_pre_emphasis(dp, pre_emphasis);
  231. break;
  232. case 1:
  233. exynos_dp_set_lane1_pre_emphasis(dp, pre_emphasis);
  234. break;
  235. case 2:
  236. exynos_dp_set_lane2_pre_emphasis(dp, pre_emphasis);
  237. break;
  238. case 3:
  239. exynos_dp_set_lane3_pre_emphasis(dp, pre_emphasis);
  240. break;
  241. }
  242. }
  243. static int exynos_dp_link_start(struct exynos_dp_device *dp)
  244. {
  245. u8 buf[4];
  246. int lane, lane_count, pll_tries, retval;
  247. lane_count = dp->link_train.lane_count;
  248. dp->link_train.lt_state = CLOCK_RECOVERY;
  249. dp->link_train.eq_loop = 0;
  250. for (lane = 0; lane < lane_count; lane++)
  251. dp->link_train.cr_loop[lane] = 0;
  252. /* Set link rate and count as you want to establish*/
  253. exynos_dp_set_link_bandwidth(dp, dp->link_train.link_rate);
  254. exynos_dp_set_lane_count(dp, dp->link_train.lane_count);
  255. /* Setup RX configuration */
  256. buf[0] = dp->link_train.link_rate;
  257. buf[1] = dp->link_train.lane_count;
  258. retval = exynos_dp_write_bytes_to_dpcd(dp, DP_LINK_BW_SET,
  259. 2, buf);
  260. if (retval)
  261. return retval;
  262. /* Set TX pre-emphasis to minimum */
  263. for (lane = 0; lane < lane_count; lane++)
  264. exynos_dp_set_lane_lane_pre_emphasis(dp,
  265. PRE_EMPHASIS_LEVEL_0, lane);
  266. /* Wait for PLL lock */
  267. pll_tries = 0;
  268. while (exynos_dp_get_pll_lock_status(dp) == PLL_UNLOCKED) {
  269. if (pll_tries == DP_TIMEOUT_LOOP_COUNT) {
  270. dev_err(dp->dev, "Wait for PLL lock timed out\n");
  271. return -ETIMEDOUT;
  272. }
  273. pll_tries++;
  274. usleep_range(90, 120);
  275. }
  276. /* Set training pattern 1 */
  277. exynos_dp_set_training_pattern(dp, TRAINING_PTN1);
  278. /* Set RX training pattern */
  279. retval = exynos_dp_write_byte_to_dpcd(dp,
  280. DP_TRAINING_PATTERN_SET,
  281. DP_LINK_SCRAMBLING_DISABLE | DP_TRAINING_PATTERN_1);
  282. if (retval)
  283. return retval;
  284. for (lane = 0; lane < lane_count; lane++)
  285. buf[lane] = DP_TRAIN_PRE_EMPH_LEVEL_0 |
  286. DP_TRAIN_VOLTAGE_SWING_LEVEL_0;
  287. retval = exynos_dp_write_bytes_to_dpcd(dp, DP_TRAINING_LANE0_SET,
  288. lane_count, buf);
  289. return retval;
  290. }
  291. static unsigned char exynos_dp_get_lane_status(u8 link_status[2], int lane)
  292. {
  293. int shift = (lane & 1) * 4;
  294. u8 link_value = link_status[lane>>1];
  295. return (link_value >> shift) & 0xf;
  296. }
  297. static int exynos_dp_clock_recovery_ok(u8 link_status[2], int lane_count)
  298. {
  299. int lane;
  300. u8 lane_status;
  301. for (lane = 0; lane < lane_count; lane++) {
  302. lane_status = exynos_dp_get_lane_status(link_status, lane);
  303. if ((lane_status & DP_LANE_CR_DONE) == 0)
  304. return -EINVAL;
  305. }
  306. return 0;
  307. }
  308. static int exynos_dp_channel_eq_ok(u8 link_status[2], u8 link_align,
  309. int lane_count)
  310. {
  311. int lane;
  312. u8 lane_status;
  313. if ((link_align & DP_INTERLANE_ALIGN_DONE) == 0)
  314. return -EINVAL;
  315. for (lane = 0; lane < lane_count; lane++) {
  316. lane_status = exynos_dp_get_lane_status(link_status, lane);
  317. lane_status &= DP_CHANNEL_EQ_BITS;
  318. if (lane_status != DP_CHANNEL_EQ_BITS)
  319. return -EINVAL;
  320. }
  321. return 0;
  322. }
  323. static unsigned char exynos_dp_get_adjust_request_voltage(u8 adjust_request[2],
  324. int lane)
  325. {
  326. int shift = (lane & 1) * 4;
  327. u8 link_value = adjust_request[lane>>1];
  328. return (link_value >> shift) & 0x3;
  329. }
  330. static unsigned char exynos_dp_get_adjust_request_pre_emphasis(
  331. u8 adjust_request[2],
  332. int lane)
  333. {
  334. int shift = (lane & 1) * 4;
  335. u8 link_value = adjust_request[lane>>1];
  336. return ((link_value >> shift) & 0xc) >> 2;
  337. }
  338. static void exynos_dp_set_lane_link_training(struct exynos_dp_device *dp,
  339. u8 training_lane_set, int lane)
  340. {
  341. switch (lane) {
  342. case 0:
  343. exynos_dp_set_lane0_link_training(dp, training_lane_set);
  344. break;
  345. case 1:
  346. exynos_dp_set_lane1_link_training(dp, training_lane_set);
  347. break;
  348. case 2:
  349. exynos_dp_set_lane2_link_training(dp, training_lane_set);
  350. break;
  351. case 3:
  352. exynos_dp_set_lane3_link_training(dp, training_lane_set);
  353. break;
  354. }
  355. }
  356. static unsigned int exynos_dp_get_lane_link_training(
  357. struct exynos_dp_device *dp,
  358. int lane)
  359. {
  360. u32 reg;
  361. switch (lane) {
  362. case 0:
  363. reg = exynos_dp_get_lane0_link_training(dp);
  364. break;
  365. case 1:
  366. reg = exynos_dp_get_lane1_link_training(dp);
  367. break;
  368. case 2:
  369. reg = exynos_dp_get_lane2_link_training(dp);
  370. break;
  371. case 3:
  372. reg = exynos_dp_get_lane3_link_training(dp);
  373. break;
  374. default:
  375. WARN_ON(1);
  376. return 0;
  377. }
  378. return reg;
  379. }
  380. static void exynos_dp_reduce_link_rate(struct exynos_dp_device *dp)
  381. {
  382. exynos_dp_training_pattern_dis(dp);
  383. exynos_dp_set_enhanced_mode(dp);
  384. dp->link_train.lt_state = FAILED;
  385. }
  386. static void exynos_dp_get_adjust_training_lane(struct exynos_dp_device *dp,
  387. u8 adjust_request[2])
  388. {
  389. int lane, lane_count;
  390. u8 voltage_swing, pre_emphasis, training_lane;
  391. lane_count = dp->link_train.lane_count;
  392. for (lane = 0; lane < lane_count; lane++) {
  393. voltage_swing = exynos_dp_get_adjust_request_voltage(
  394. adjust_request, lane);
  395. pre_emphasis = exynos_dp_get_adjust_request_pre_emphasis(
  396. adjust_request, lane);
  397. training_lane = DPCD_VOLTAGE_SWING_SET(voltage_swing) |
  398. DPCD_PRE_EMPHASIS_SET(pre_emphasis);
  399. if (voltage_swing == VOLTAGE_LEVEL_3)
  400. training_lane |= DP_TRAIN_MAX_SWING_REACHED;
  401. if (pre_emphasis == PRE_EMPHASIS_LEVEL_3)
  402. training_lane |= DP_TRAIN_MAX_PRE_EMPHASIS_REACHED;
  403. dp->link_train.training_lane[lane] = training_lane;
  404. }
  405. }
  406. static int exynos_dp_process_clock_recovery(struct exynos_dp_device *dp)
  407. {
  408. int lane, lane_count, retval;
  409. u8 voltage_swing, pre_emphasis, training_lane;
  410. u8 link_status[2], adjust_request[2];
  411. usleep_range(100, 101);
  412. lane_count = dp->link_train.lane_count;
  413. retval = exynos_dp_read_bytes_from_dpcd(dp,
  414. DP_LANE0_1_STATUS, 2, link_status);
  415. if (retval)
  416. return retval;
  417. retval = exynos_dp_read_bytes_from_dpcd(dp,
  418. DP_ADJUST_REQUEST_LANE0_1, 2, adjust_request);
  419. if (retval)
  420. return retval;
  421. if (exynos_dp_clock_recovery_ok(link_status, lane_count) == 0) {
  422. /* set training pattern 2 for EQ */
  423. exynos_dp_set_training_pattern(dp, TRAINING_PTN2);
  424. retval = exynos_dp_write_byte_to_dpcd(dp,
  425. DP_TRAINING_PATTERN_SET,
  426. DP_LINK_SCRAMBLING_DISABLE |
  427. DP_TRAINING_PATTERN_2);
  428. if (retval)
  429. return retval;
  430. dev_info(dp->dev, "Link Training Clock Recovery success\n");
  431. dp->link_train.lt_state = EQUALIZER_TRAINING;
  432. } else {
  433. for (lane = 0; lane < lane_count; lane++) {
  434. training_lane = exynos_dp_get_lane_link_training(
  435. dp, lane);
  436. voltage_swing = exynos_dp_get_adjust_request_voltage(
  437. adjust_request, lane);
  438. pre_emphasis = exynos_dp_get_adjust_request_pre_emphasis(
  439. adjust_request, lane);
  440. if (DPCD_VOLTAGE_SWING_GET(training_lane) ==
  441. voltage_swing &&
  442. DPCD_PRE_EMPHASIS_GET(training_lane) ==
  443. pre_emphasis)
  444. dp->link_train.cr_loop[lane]++;
  445. if (dp->link_train.cr_loop[lane] == MAX_CR_LOOP ||
  446. voltage_swing == VOLTAGE_LEVEL_3 ||
  447. pre_emphasis == PRE_EMPHASIS_LEVEL_3) {
  448. dev_err(dp->dev, "CR Max reached (%d,%d,%d)\n",
  449. dp->link_train.cr_loop[lane],
  450. voltage_swing, pre_emphasis);
  451. exynos_dp_reduce_link_rate(dp);
  452. return -EIO;
  453. }
  454. }
  455. }
  456. exynos_dp_get_adjust_training_lane(dp, adjust_request);
  457. for (lane = 0; lane < lane_count; lane++)
  458. exynos_dp_set_lane_link_training(dp,
  459. dp->link_train.training_lane[lane], lane);
  460. retval = exynos_dp_write_bytes_to_dpcd(dp,
  461. DP_TRAINING_LANE0_SET, lane_count,
  462. dp->link_train.training_lane);
  463. if (retval)
  464. return retval;
  465. return retval;
  466. }
  467. static int exynos_dp_process_equalizer_training(struct exynos_dp_device *dp)
  468. {
  469. int lane, lane_count, retval;
  470. u32 reg;
  471. u8 link_align, link_status[2], adjust_request[2];
  472. usleep_range(400, 401);
  473. lane_count = dp->link_train.lane_count;
  474. retval = exynos_dp_read_bytes_from_dpcd(dp,
  475. DP_LANE0_1_STATUS, 2, link_status);
  476. if (retval)
  477. return retval;
  478. if (exynos_dp_clock_recovery_ok(link_status, lane_count)) {
  479. exynos_dp_reduce_link_rate(dp);
  480. return -EIO;
  481. }
  482. retval = exynos_dp_read_bytes_from_dpcd(dp,
  483. DP_ADJUST_REQUEST_LANE0_1, 2, adjust_request);
  484. if (retval)
  485. return retval;
  486. retval = exynos_dp_read_byte_from_dpcd(dp,
  487. DP_LANE_ALIGN_STATUS_UPDATED, &link_align);
  488. if (retval)
  489. return retval;
  490. exynos_dp_get_adjust_training_lane(dp, adjust_request);
  491. if (!exynos_dp_channel_eq_ok(link_status, link_align, lane_count)) {
  492. /* traing pattern Set to Normal */
  493. exynos_dp_training_pattern_dis(dp);
  494. dev_info(dp->dev, "Link Training success!\n");
  495. exynos_dp_get_link_bandwidth(dp, &reg);
  496. dp->link_train.link_rate = reg;
  497. dev_dbg(dp->dev, "final bandwidth = %.2x\n",
  498. dp->link_train.link_rate);
  499. exynos_dp_get_lane_count(dp, &reg);
  500. dp->link_train.lane_count = reg;
  501. dev_dbg(dp->dev, "final lane count = %.2x\n",
  502. dp->link_train.lane_count);
  503. /* set enhanced mode if available */
  504. exynos_dp_set_enhanced_mode(dp);
  505. dp->link_train.lt_state = FINISHED;
  506. return 0;
  507. }
  508. /* not all locked */
  509. dp->link_train.eq_loop++;
  510. if (dp->link_train.eq_loop > MAX_EQ_LOOP) {
  511. dev_err(dp->dev, "EQ Max loop\n");
  512. exynos_dp_reduce_link_rate(dp);
  513. return -EIO;
  514. }
  515. for (lane = 0; lane < lane_count; lane++)
  516. exynos_dp_set_lane_link_training(dp,
  517. dp->link_train.training_lane[lane], lane);
  518. retval = exynos_dp_write_bytes_to_dpcd(dp, DP_TRAINING_LANE0_SET,
  519. lane_count, dp->link_train.training_lane);
  520. return retval;
  521. }
  522. static void exynos_dp_get_max_rx_bandwidth(struct exynos_dp_device *dp,
  523. u8 *bandwidth)
  524. {
  525. u8 data;
  526. /*
  527. * For DP rev.1.1, Maximum link rate of Main Link lanes
  528. * 0x06 = 1.62 Gbps, 0x0a = 2.7 Gbps
  529. */
  530. exynos_dp_read_byte_from_dpcd(dp, DP_MAX_LINK_RATE, &data);
  531. *bandwidth = data;
  532. }
  533. static void exynos_dp_get_max_rx_lane_count(struct exynos_dp_device *dp,
  534. u8 *lane_count)
  535. {
  536. u8 data;
  537. /*
  538. * For DP rev.1.1, Maximum number of Main Link lanes
  539. * 0x01 = 1 lane, 0x02 = 2 lanes, 0x04 = 4 lanes
  540. */
  541. exynos_dp_read_byte_from_dpcd(dp, DP_MAX_LANE_COUNT, &data);
  542. *lane_count = DPCD_MAX_LANE_COUNT(data);
  543. }
  544. static void exynos_dp_init_training(struct exynos_dp_device *dp,
  545. enum link_lane_count_type max_lane,
  546. enum link_rate_type max_rate)
  547. {
  548. /*
  549. * MACRO_RST must be applied after the PLL_LOCK to avoid
  550. * the DP inter pair skew issue for at least 10 us
  551. */
  552. exynos_dp_reset_macro(dp);
  553. /* Initialize by reading RX's DPCD */
  554. exynos_dp_get_max_rx_bandwidth(dp, &dp->link_train.link_rate);
  555. exynos_dp_get_max_rx_lane_count(dp, &dp->link_train.lane_count);
  556. if ((dp->link_train.link_rate != LINK_RATE_1_62GBPS) &&
  557. (dp->link_train.link_rate != LINK_RATE_2_70GBPS)) {
  558. dev_err(dp->dev, "Rx Max Link Rate is abnormal :%x !\n",
  559. dp->link_train.link_rate);
  560. dp->link_train.link_rate = LINK_RATE_1_62GBPS;
  561. }
  562. if (dp->link_train.lane_count == 0) {
  563. dev_err(dp->dev, "Rx Max Lane count is abnormal :%x !\n",
  564. dp->link_train.lane_count);
  565. dp->link_train.lane_count = (u8)LANE_COUNT1;
  566. }
  567. /* Setup TX lane count & rate */
  568. if (dp->link_train.lane_count > max_lane)
  569. dp->link_train.lane_count = max_lane;
  570. if (dp->link_train.link_rate > max_rate)
  571. dp->link_train.link_rate = max_rate;
  572. /* All DP analog module power up */
  573. exynos_dp_set_analog_power_down(dp, POWER_ALL, 0);
  574. }
  575. static int exynos_dp_sw_link_training(struct exynos_dp_device *dp)
  576. {
  577. int retval = 0, training_finished = 0;
  578. dp->link_train.lt_state = START;
  579. /* Process here */
  580. while (!retval && !training_finished) {
  581. switch (dp->link_train.lt_state) {
  582. case START:
  583. retval = exynos_dp_link_start(dp);
  584. if (retval)
  585. dev_err(dp->dev, "LT link start failed!\n");
  586. break;
  587. case CLOCK_RECOVERY:
  588. retval = exynos_dp_process_clock_recovery(dp);
  589. if (retval)
  590. dev_err(dp->dev, "LT CR failed!\n");
  591. break;
  592. case EQUALIZER_TRAINING:
  593. retval = exynos_dp_process_equalizer_training(dp);
  594. if (retval)
  595. dev_err(dp->dev, "LT EQ failed!\n");
  596. break;
  597. case FINISHED:
  598. training_finished = 1;
  599. break;
  600. case FAILED:
  601. return -EREMOTEIO;
  602. }
  603. }
  604. if (retval)
  605. dev_err(dp->dev, "eDP link training failed (%d)\n", retval);
  606. return retval;
  607. }
  608. static int exynos_dp_set_link_train(struct exynos_dp_device *dp,
  609. u32 count,
  610. u32 bwtype)
  611. {
  612. int i;
  613. int retval;
  614. for (i = 0; i < DP_TIMEOUT_LOOP_COUNT; i++) {
  615. exynos_dp_init_training(dp, count, bwtype);
  616. retval = exynos_dp_sw_link_training(dp);
  617. if (retval == 0)
  618. break;
  619. usleep_range(100, 110);
  620. }
  621. return retval;
  622. }
  623. static int exynos_dp_config_video(struct exynos_dp_device *dp)
  624. {
  625. int retval = 0;
  626. int timeout_loop = 0;
  627. int done_count = 0;
  628. exynos_dp_config_video_slave_mode(dp);
  629. exynos_dp_set_video_color_format(dp);
  630. if (exynos_dp_get_pll_lock_status(dp) == PLL_UNLOCKED) {
  631. dev_err(dp->dev, "PLL is not locked yet.\n");
  632. return -EINVAL;
  633. }
  634. for (;;) {
  635. timeout_loop++;
  636. if (exynos_dp_is_slave_video_stream_clock_on(dp) == 0)
  637. break;
  638. if (DP_TIMEOUT_LOOP_COUNT < timeout_loop) {
  639. dev_err(dp->dev, "Timeout of video streamclk ok\n");
  640. return -ETIMEDOUT;
  641. }
  642. usleep_range(1, 2);
  643. }
  644. /* Set to use the register calculated M/N video */
  645. exynos_dp_set_video_cr_mn(dp, CALCULATED_M, 0, 0);
  646. /* For video bist, Video timing must be generated by register */
  647. exynos_dp_set_video_timing_mode(dp, VIDEO_TIMING_FROM_CAPTURE);
  648. /* Disable video mute */
  649. exynos_dp_enable_video_mute(dp, 0);
  650. /* Configure video slave mode */
  651. exynos_dp_enable_video_master(dp, 0);
  652. /* Enable video */
  653. exynos_dp_start_video(dp);
  654. timeout_loop = 0;
  655. for (;;) {
  656. timeout_loop++;
  657. if (exynos_dp_is_video_stream_on(dp) == 0) {
  658. done_count++;
  659. if (done_count > 10)
  660. break;
  661. } else if (done_count) {
  662. done_count = 0;
  663. }
  664. if (DP_TIMEOUT_LOOP_COUNT < timeout_loop) {
  665. dev_err(dp->dev, "Timeout of video streamclk ok\n");
  666. return -ETIMEDOUT;
  667. }
  668. usleep_range(1000, 1001);
  669. }
  670. if (retval != 0)
  671. dev_err(dp->dev, "Video stream is not detected!\n");
  672. return retval;
  673. }
  674. static void exynos_dp_enable_scramble(struct exynos_dp_device *dp, bool enable)
  675. {
  676. u8 data;
  677. if (enable) {
  678. exynos_dp_enable_scrambling(dp);
  679. exynos_dp_read_byte_from_dpcd(dp,
  680. DP_TRAINING_PATTERN_SET,
  681. &data);
  682. exynos_dp_write_byte_to_dpcd(dp,
  683. DP_TRAINING_PATTERN_SET,
  684. (u8)(data & ~DP_LINK_SCRAMBLING_DISABLE));
  685. } else {
  686. exynos_dp_disable_scrambling(dp);
  687. exynos_dp_read_byte_from_dpcd(dp,
  688. DP_TRAINING_PATTERN_SET,
  689. &data);
  690. exynos_dp_write_byte_to_dpcd(dp,
  691. DP_TRAINING_PATTERN_SET,
  692. (u8)(data | DP_LINK_SCRAMBLING_DISABLE));
  693. }
  694. }
  695. static irqreturn_t exynos_dp_irq_handler(int irq, void *arg)
  696. {
  697. struct exynos_dp_device *dp = arg;
  698. enum dp_irq_type irq_type;
  699. irq_type = exynos_dp_get_irq_type(dp);
  700. switch (irq_type) {
  701. case DP_IRQ_TYPE_HP_CABLE_IN:
  702. dev_dbg(dp->dev, "Received irq - cable in\n");
  703. schedule_work(&dp->hotplug_work);
  704. exynos_dp_clear_hotplug_interrupts(dp);
  705. break;
  706. case DP_IRQ_TYPE_HP_CABLE_OUT:
  707. dev_dbg(dp->dev, "Received irq - cable out\n");
  708. exynos_dp_clear_hotplug_interrupts(dp);
  709. break;
  710. case DP_IRQ_TYPE_HP_CHANGE:
  711. /*
  712. * We get these change notifications once in a while, but there
  713. * is nothing we can do with them. Just ignore it for now and
  714. * only handle cable changes.
  715. */
  716. dev_dbg(dp->dev, "Received irq - hotplug change; ignoring.\n");
  717. exynos_dp_clear_hotplug_interrupts(dp);
  718. break;
  719. default:
  720. dev_err(dp->dev, "Received irq - unknown type!\n");
  721. break;
  722. }
  723. return IRQ_HANDLED;
  724. }
  725. static void exynos_dp_hotplug(struct work_struct *work)
  726. {
  727. struct exynos_dp_device *dp;
  728. dp = container_of(work, struct exynos_dp_device, hotplug_work);
  729. if (dp->drm_dev)
  730. drm_helper_hpd_irq_event(dp->drm_dev);
  731. }
  732. static void exynos_dp_commit(struct exynos_drm_display *display)
  733. {
  734. struct exynos_dp_device *dp = display_to_dp(display);
  735. int ret;
  736. /* Keep the panel disabled while we configure video */
  737. if (dp->panel) {
  738. if (drm_panel_disable(dp->panel))
  739. DRM_ERROR("failed to disable the panel\n");
  740. }
  741. ret = exynos_dp_detect_hpd(dp);
  742. if (ret) {
  743. /* Cable has been disconnected, we're done */
  744. return;
  745. }
  746. ret = exynos_dp_handle_edid(dp);
  747. if (ret) {
  748. dev_err(dp->dev, "unable to handle edid\n");
  749. return;
  750. }
  751. ret = exynos_dp_set_link_train(dp, dp->video_info->lane_count,
  752. dp->video_info->link_rate);
  753. if (ret) {
  754. dev_err(dp->dev, "unable to do link train\n");
  755. return;
  756. }
  757. exynos_dp_enable_scramble(dp, 1);
  758. exynos_dp_enable_rx_to_enhanced_mode(dp, 1);
  759. exynos_dp_enable_enhanced_mode(dp, 1);
  760. exynos_dp_set_lane_count(dp, dp->video_info->lane_count);
  761. exynos_dp_set_link_bandwidth(dp, dp->video_info->link_rate);
  762. exynos_dp_init_video(dp);
  763. ret = exynos_dp_config_video(dp);
  764. if (ret)
  765. dev_err(dp->dev, "unable to config video\n");
  766. /* Safe to enable the panel now */
  767. if (dp->panel) {
  768. if (drm_panel_enable(dp->panel))
  769. DRM_ERROR("failed to enable the panel\n");
  770. }
  771. }
  772. static enum drm_connector_status exynos_dp_detect(
  773. struct drm_connector *connector, bool force)
  774. {
  775. return connector_status_connected;
  776. }
  777. static void exynos_dp_connector_destroy(struct drm_connector *connector)
  778. {
  779. drm_connector_unregister(connector);
  780. drm_connector_cleanup(connector);
  781. }
  782. static struct drm_connector_funcs exynos_dp_connector_funcs = {
  783. .dpms = drm_atomic_helper_connector_dpms,
  784. .fill_modes = drm_helper_probe_single_connector_modes,
  785. .detect = exynos_dp_detect,
  786. .destroy = exynos_dp_connector_destroy,
  787. .reset = drm_atomic_helper_connector_reset,
  788. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  789. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  790. };
  791. static int exynos_dp_get_modes(struct drm_connector *connector)
  792. {
  793. struct exynos_dp_device *dp = ctx_from_connector(connector);
  794. struct drm_display_mode *mode;
  795. if (dp->panel)
  796. return drm_panel_get_modes(dp->panel);
  797. mode = drm_mode_create(connector->dev);
  798. if (!mode) {
  799. DRM_ERROR("failed to create a new display mode.\n");
  800. return 0;
  801. }
  802. drm_display_mode_from_videomode(&dp->priv.vm, mode);
  803. mode->width_mm = dp->priv.width_mm;
  804. mode->height_mm = dp->priv.height_mm;
  805. connector->display_info.width_mm = mode->width_mm;
  806. connector->display_info.height_mm = mode->height_mm;
  807. mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
  808. drm_mode_set_name(mode);
  809. drm_mode_probed_add(connector, mode);
  810. return 1;
  811. }
  812. static struct drm_encoder *exynos_dp_best_encoder(
  813. struct drm_connector *connector)
  814. {
  815. struct exynos_dp_device *dp = ctx_from_connector(connector);
  816. return dp->encoder;
  817. }
  818. static struct drm_connector_helper_funcs exynos_dp_connector_helper_funcs = {
  819. .get_modes = exynos_dp_get_modes,
  820. .best_encoder = exynos_dp_best_encoder,
  821. };
  822. /* returns the number of bridges attached */
  823. static int exynos_drm_attach_lcd_bridge(struct exynos_dp_device *dp,
  824. struct drm_encoder *encoder)
  825. {
  826. int ret;
  827. encoder->bridge = dp->bridge;
  828. dp->bridge->encoder = encoder;
  829. ret = drm_bridge_attach(encoder->dev, dp->bridge);
  830. if (ret) {
  831. DRM_ERROR("Failed to attach bridge to drm\n");
  832. return ret;
  833. }
  834. return 0;
  835. }
  836. static int exynos_dp_create_connector(struct exynos_drm_display *display,
  837. struct drm_encoder *encoder)
  838. {
  839. struct exynos_dp_device *dp = display_to_dp(display);
  840. struct drm_connector *connector = &dp->connector;
  841. int ret;
  842. dp->encoder = encoder;
  843. /* Pre-empt DP connector creation if there's a bridge */
  844. if (dp->bridge) {
  845. ret = exynos_drm_attach_lcd_bridge(dp, encoder);
  846. if (!ret)
  847. return 0;
  848. }
  849. connector->polled = DRM_CONNECTOR_POLL_HPD;
  850. ret = drm_connector_init(dp->drm_dev, connector,
  851. &exynos_dp_connector_funcs, DRM_MODE_CONNECTOR_eDP);
  852. if (ret) {
  853. DRM_ERROR("Failed to initialize connector with drm\n");
  854. return ret;
  855. }
  856. drm_connector_helper_add(connector, &exynos_dp_connector_helper_funcs);
  857. drm_connector_register(connector);
  858. drm_mode_connector_attach_encoder(connector, encoder);
  859. if (dp->panel)
  860. ret = drm_panel_attach(dp->panel, &dp->connector);
  861. return ret;
  862. }
  863. static void exynos_dp_phy_init(struct exynos_dp_device *dp)
  864. {
  865. if (dp->phy)
  866. phy_power_on(dp->phy);
  867. }
  868. static void exynos_dp_phy_exit(struct exynos_dp_device *dp)
  869. {
  870. if (dp->phy)
  871. phy_power_off(dp->phy);
  872. }
  873. static void exynos_dp_poweron(struct exynos_dp_device *dp)
  874. {
  875. struct exynos_drm_crtc *crtc = dp_to_crtc(dp);
  876. if (dp->dpms_mode == DRM_MODE_DPMS_ON)
  877. return;
  878. if (dp->panel) {
  879. if (drm_panel_prepare(dp->panel)) {
  880. DRM_ERROR("failed to setup the panel\n");
  881. return;
  882. }
  883. }
  884. if (crtc->ops->clock_enable)
  885. crtc->ops->clock_enable(dp_to_crtc(dp), true);
  886. clk_prepare_enable(dp->clock);
  887. exynos_dp_phy_init(dp);
  888. exynos_dp_init_dp(dp);
  889. enable_irq(dp->irq);
  890. exynos_dp_commit(&dp->display);
  891. }
  892. static void exynos_dp_poweroff(struct exynos_dp_device *dp)
  893. {
  894. struct exynos_drm_crtc *crtc = dp_to_crtc(dp);
  895. if (dp->dpms_mode != DRM_MODE_DPMS_ON)
  896. return;
  897. if (dp->panel) {
  898. if (drm_panel_disable(dp->panel)) {
  899. DRM_ERROR("failed to disable the panel\n");
  900. return;
  901. }
  902. }
  903. disable_irq(dp->irq);
  904. flush_work(&dp->hotplug_work);
  905. exynos_dp_phy_exit(dp);
  906. clk_disable_unprepare(dp->clock);
  907. if (crtc->ops->clock_enable)
  908. crtc->ops->clock_enable(dp_to_crtc(dp), false);
  909. if (dp->panel) {
  910. if (drm_panel_unprepare(dp->panel))
  911. DRM_ERROR("failed to turnoff the panel\n");
  912. }
  913. }
  914. static void exynos_dp_dpms(struct exynos_drm_display *display, int mode)
  915. {
  916. struct exynos_dp_device *dp = display_to_dp(display);
  917. switch (mode) {
  918. case DRM_MODE_DPMS_ON:
  919. exynos_dp_poweron(dp);
  920. break;
  921. case DRM_MODE_DPMS_STANDBY:
  922. case DRM_MODE_DPMS_SUSPEND:
  923. case DRM_MODE_DPMS_OFF:
  924. exynos_dp_poweroff(dp);
  925. break;
  926. default:
  927. break;
  928. }
  929. dp->dpms_mode = mode;
  930. }
  931. static struct exynos_drm_display_ops exynos_dp_display_ops = {
  932. .create_connector = exynos_dp_create_connector,
  933. .dpms = exynos_dp_dpms,
  934. .commit = exynos_dp_commit,
  935. };
  936. static struct video_info *exynos_dp_dt_parse_pdata(struct device *dev)
  937. {
  938. struct device_node *dp_node = dev->of_node;
  939. struct video_info *dp_video_config;
  940. dp_video_config = devm_kzalloc(dev,
  941. sizeof(*dp_video_config), GFP_KERNEL);
  942. if (!dp_video_config)
  943. return ERR_PTR(-ENOMEM);
  944. dp_video_config->h_sync_polarity =
  945. of_property_read_bool(dp_node, "hsync-active-high");
  946. dp_video_config->v_sync_polarity =
  947. of_property_read_bool(dp_node, "vsync-active-high");
  948. dp_video_config->interlaced =
  949. of_property_read_bool(dp_node, "interlaced");
  950. if (of_property_read_u32(dp_node, "samsung,color-space",
  951. &dp_video_config->color_space)) {
  952. dev_err(dev, "failed to get color-space\n");
  953. return ERR_PTR(-EINVAL);
  954. }
  955. if (of_property_read_u32(dp_node, "samsung,dynamic-range",
  956. &dp_video_config->dynamic_range)) {
  957. dev_err(dev, "failed to get dynamic-range\n");
  958. return ERR_PTR(-EINVAL);
  959. }
  960. if (of_property_read_u32(dp_node, "samsung,ycbcr-coeff",
  961. &dp_video_config->ycbcr_coeff)) {
  962. dev_err(dev, "failed to get ycbcr-coeff\n");
  963. return ERR_PTR(-EINVAL);
  964. }
  965. if (of_property_read_u32(dp_node, "samsung,color-depth",
  966. &dp_video_config->color_depth)) {
  967. dev_err(dev, "failed to get color-depth\n");
  968. return ERR_PTR(-EINVAL);
  969. }
  970. if (of_property_read_u32(dp_node, "samsung,link-rate",
  971. &dp_video_config->link_rate)) {
  972. dev_err(dev, "failed to get link-rate\n");
  973. return ERR_PTR(-EINVAL);
  974. }
  975. if (of_property_read_u32(dp_node, "samsung,lane-count",
  976. &dp_video_config->lane_count)) {
  977. dev_err(dev, "failed to get lane-count\n");
  978. return ERR_PTR(-EINVAL);
  979. }
  980. return dp_video_config;
  981. }
  982. static int exynos_dp_dt_parse_panel(struct exynos_dp_device *dp)
  983. {
  984. int ret;
  985. ret = of_get_videomode(dp->dev->of_node, &dp->priv.vm,
  986. OF_USE_NATIVE_MODE);
  987. if (ret) {
  988. DRM_ERROR("failed: of_get_videomode() : %d\n", ret);
  989. return ret;
  990. }
  991. return 0;
  992. }
  993. static int exynos_dp_bind(struct device *dev, struct device *master, void *data)
  994. {
  995. struct exynos_dp_device *dp = dev_get_drvdata(dev);
  996. struct platform_device *pdev = to_platform_device(dev);
  997. struct drm_device *drm_dev = data;
  998. struct resource *res;
  999. unsigned int irq_flags;
  1000. int ret = 0;
  1001. dp->dev = &pdev->dev;
  1002. dp->dpms_mode = DRM_MODE_DPMS_OFF;
  1003. dp->video_info = exynos_dp_dt_parse_pdata(&pdev->dev);
  1004. if (IS_ERR(dp->video_info))
  1005. return PTR_ERR(dp->video_info);
  1006. dp->phy = devm_phy_get(dp->dev, "dp");
  1007. if (IS_ERR(dp->phy)) {
  1008. dev_err(dp->dev, "no DP phy configured\n");
  1009. ret = PTR_ERR(dp->phy);
  1010. if (ret) {
  1011. /*
  1012. * phy itself is not enabled, so we can move forward
  1013. * assigning NULL to phy pointer.
  1014. */
  1015. if (ret == -ENOSYS || ret == -ENODEV)
  1016. dp->phy = NULL;
  1017. else
  1018. return ret;
  1019. }
  1020. }
  1021. if (!dp->panel && !dp->bridge) {
  1022. ret = exynos_dp_dt_parse_panel(dp);
  1023. if (ret)
  1024. return ret;
  1025. }
  1026. dp->clock = devm_clk_get(&pdev->dev, "dp");
  1027. if (IS_ERR(dp->clock)) {
  1028. dev_err(&pdev->dev, "failed to get clock\n");
  1029. return PTR_ERR(dp->clock);
  1030. }
  1031. clk_prepare_enable(dp->clock);
  1032. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1033. dp->reg_base = devm_ioremap_resource(&pdev->dev, res);
  1034. if (IS_ERR(dp->reg_base))
  1035. return PTR_ERR(dp->reg_base);
  1036. dp->hpd_gpio = of_get_named_gpio(dev->of_node, "samsung,hpd-gpio", 0);
  1037. if (gpio_is_valid(dp->hpd_gpio)) {
  1038. /*
  1039. * Set up the hotplug GPIO from the device tree as an interrupt.
  1040. * Simply specifying a different interrupt in the device tree
  1041. * doesn't work since we handle hotplug rather differently when
  1042. * using a GPIO. We also need the actual GPIO specifier so
  1043. * that we can get the current state of the GPIO.
  1044. */
  1045. ret = devm_gpio_request_one(&pdev->dev, dp->hpd_gpio, GPIOF_IN,
  1046. "hpd_gpio");
  1047. if (ret) {
  1048. dev_err(&pdev->dev, "failed to get hpd gpio\n");
  1049. return ret;
  1050. }
  1051. dp->irq = gpio_to_irq(dp->hpd_gpio);
  1052. irq_flags = IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING;
  1053. } else {
  1054. dp->hpd_gpio = -ENODEV;
  1055. dp->irq = platform_get_irq(pdev, 0);
  1056. irq_flags = 0;
  1057. }
  1058. if (dp->irq == -ENXIO) {
  1059. dev_err(&pdev->dev, "failed to get irq\n");
  1060. return -ENODEV;
  1061. }
  1062. INIT_WORK(&dp->hotplug_work, exynos_dp_hotplug);
  1063. exynos_dp_phy_init(dp);
  1064. exynos_dp_init_dp(dp);
  1065. ret = devm_request_irq(&pdev->dev, dp->irq, exynos_dp_irq_handler,
  1066. irq_flags, "exynos-dp", dp);
  1067. if (ret) {
  1068. dev_err(&pdev->dev, "failed to request irq\n");
  1069. return ret;
  1070. }
  1071. disable_irq(dp->irq);
  1072. dp->drm_dev = drm_dev;
  1073. return exynos_drm_create_enc_conn(drm_dev, &dp->display);
  1074. }
  1075. static void exynos_dp_unbind(struct device *dev, struct device *master,
  1076. void *data)
  1077. {
  1078. struct exynos_dp_device *dp = dev_get_drvdata(dev);
  1079. exynos_dp_dpms(&dp->display, DRM_MODE_DPMS_OFF);
  1080. }
  1081. static const struct component_ops exynos_dp_ops = {
  1082. .bind = exynos_dp_bind,
  1083. .unbind = exynos_dp_unbind,
  1084. };
  1085. static int exynos_dp_probe(struct platform_device *pdev)
  1086. {
  1087. struct device *dev = &pdev->dev;
  1088. struct device_node *panel_node, *bridge_node, *endpoint;
  1089. struct exynos_dp_device *dp;
  1090. dp = devm_kzalloc(&pdev->dev, sizeof(struct exynos_dp_device),
  1091. GFP_KERNEL);
  1092. if (!dp)
  1093. return -ENOMEM;
  1094. dp->display.type = EXYNOS_DISPLAY_TYPE_LCD;
  1095. dp->display.ops = &exynos_dp_display_ops;
  1096. platform_set_drvdata(pdev, dp);
  1097. panel_node = of_parse_phandle(dev->of_node, "panel", 0);
  1098. if (panel_node) {
  1099. dp->panel = of_drm_find_panel(panel_node);
  1100. of_node_put(panel_node);
  1101. if (!dp->panel)
  1102. return -EPROBE_DEFER;
  1103. }
  1104. endpoint = of_graph_get_next_endpoint(dev->of_node, NULL);
  1105. if (endpoint) {
  1106. bridge_node = of_graph_get_remote_port_parent(endpoint);
  1107. if (bridge_node) {
  1108. dp->bridge = of_drm_find_bridge(bridge_node);
  1109. of_node_put(bridge_node);
  1110. if (!dp->bridge)
  1111. return -EPROBE_DEFER;
  1112. } else
  1113. return -EPROBE_DEFER;
  1114. }
  1115. return component_add(&pdev->dev, &exynos_dp_ops);
  1116. }
  1117. static int exynos_dp_remove(struct platform_device *pdev)
  1118. {
  1119. component_del(&pdev->dev, &exynos_dp_ops);
  1120. return 0;
  1121. }
  1122. #ifdef CONFIG_PM_SLEEP
  1123. static int exynos_dp_suspend(struct device *dev)
  1124. {
  1125. struct exynos_dp_device *dp = dev_get_drvdata(dev);
  1126. exynos_dp_dpms(&dp->display, DRM_MODE_DPMS_OFF);
  1127. return 0;
  1128. }
  1129. static int exynos_dp_resume(struct device *dev)
  1130. {
  1131. struct exynos_dp_device *dp = dev_get_drvdata(dev);
  1132. exynos_dp_dpms(&dp->display, DRM_MODE_DPMS_ON);
  1133. return 0;
  1134. }
  1135. #endif
  1136. static const struct dev_pm_ops exynos_dp_pm_ops = {
  1137. SET_SYSTEM_SLEEP_PM_OPS(exynos_dp_suspend, exynos_dp_resume)
  1138. };
  1139. static const struct of_device_id exynos_dp_match[] = {
  1140. { .compatible = "samsung,exynos5-dp" },
  1141. {},
  1142. };
  1143. MODULE_DEVICE_TABLE(of, exynos_dp_match);
  1144. struct platform_driver dp_driver = {
  1145. .probe = exynos_dp_probe,
  1146. .remove = exynos_dp_remove,
  1147. .driver = {
  1148. .name = "exynos-dp",
  1149. .owner = THIS_MODULE,
  1150. .pm = &exynos_dp_pm_ops,
  1151. .of_match_table = exynos_dp_match,
  1152. },
  1153. };
  1154. MODULE_AUTHOR("Jingoo Han <jg1.han@samsung.com>");
  1155. MODULE_DESCRIPTION("Samsung SoC DP Driver");
  1156. MODULE_LICENSE("GPL v2");