exynos_dp_core.c 34 KB

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