intel_dp.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405
  1. /*
  2. * Copyright © 2008 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Keith Packard <keithp@keithp.com>
  25. *
  26. */
  27. #include <linux/i2c.h>
  28. #include "drmP.h"
  29. #include "drm.h"
  30. #include "drm_crtc.h"
  31. #include "drm_crtc_helper.h"
  32. #include "intel_drv.h"
  33. #include "i915_drm.h"
  34. #include "i915_drv.h"
  35. #include "drm_dp_helper.h"
  36. #define DP_LINK_STATUS_SIZE 6
  37. #define DP_LINK_CHECK_TIMEOUT (10 * 1000)
  38. #define DP_LINK_CONFIGURATION_SIZE 9
  39. #define IS_eDP(i) ((i)->type == INTEL_OUTPUT_EDP)
  40. struct intel_dp_priv {
  41. uint32_t output_reg;
  42. uint32_t DP;
  43. uint8_t link_configuration[DP_LINK_CONFIGURATION_SIZE];
  44. uint32_t save_DP;
  45. uint8_t save_link_configuration[DP_LINK_CONFIGURATION_SIZE];
  46. bool has_audio;
  47. int dpms_mode;
  48. uint8_t link_bw;
  49. uint8_t lane_count;
  50. uint8_t dpcd[4];
  51. struct intel_output *intel_output;
  52. struct i2c_adapter adapter;
  53. struct i2c_algo_dp_aux_data algo;
  54. };
  55. static void
  56. intel_dp_link_train(struct intel_output *intel_output, uint32_t DP,
  57. uint8_t link_configuration[DP_LINK_CONFIGURATION_SIZE]);
  58. static void
  59. intel_dp_link_down(struct intel_output *intel_output, uint32_t DP);
  60. void
  61. intel_edp_link_config (struct intel_output *intel_output,
  62. int *lane_num, int *link_bw)
  63. {
  64. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  65. *lane_num = dp_priv->lane_count;
  66. if (dp_priv->link_bw == DP_LINK_BW_1_62)
  67. *link_bw = 162000;
  68. else if (dp_priv->link_bw == DP_LINK_BW_2_7)
  69. *link_bw = 270000;
  70. }
  71. static int
  72. intel_dp_max_lane_count(struct intel_output *intel_output)
  73. {
  74. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  75. int max_lane_count = 4;
  76. if (dp_priv->dpcd[0] >= 0x11) {
  77. max_lane_count = dp_priv->dpcd[2] & 0x1f;
  78. switch (max_lane_count) {
  79. case 1: case 2: case 4:
  80. break;
  81. default:
  82. max_lane_count = 4;
  83. }
  84. }
  85. return max_lane_count;
  86. }
  87. static int
  88. intel_dp_max_link_bw(struct intel_output *intel_output)
  89. {
  90. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  91. int max_link_bw = dp_priv->dpcd[1];
  92. switch (max_link_bw) {
  93. case DP_LINK_BW_1_62:
  94. case DP_LINK_BW_2_7:
  95. break;
  96. default:
  97. max_link_bw = DP_LINK_BW_1_62;
  98. break;
  99. }
  100. return max_link_bw;
  101. }
  102. static int
  103. intel_dp_link_clock(uint8_t link_bw)
  104. {
  105. if (link_bw == DP_LINK_BW_2_7)
  106. return 270000;
  107. else
  108. return 162000;
  109. }
  110. /* I think this is a fiction */
  111. static int
  112. intel_dp_link_required(struct drm_device *dev,
  113. struct intel_output *intel_output, int pixel_clock)
  114. {
  115. struct drm_i915_private *dev_priv = dev->dev_private;
  116. if (IS_eDP(intel_output))
  117. return (pixel_clock * dev_priv->edp_bpp) / 8;
  118. else
  119. return pixel_clock * 3;
  120. }
  121. static int
  122. intel_dp_mode_valid(struct drm_connector *connector,
  123. struct drm_display_mode *mode)
  124. {
  125. struct intel_output *intel_output = to_intel_output(connector);
  126. int max_link_clock = intel_dp_link_clock(intel_dp_max_link_bw(intel_output));
  127. int max_lanes = intel_dp_max_lane_count(intel_output);
  128. if (intel_dp_link_required(connector->dev, intel_output, mode->clock)
  129. > max_link_clock * max_lanes)
  130. return MODE_CLOCK_HIGH;
  131. if (mode->clock < 10000)
  132. return MODE_CLOCK_LOW;
  133. return MODE_OK;
  134. }
  135. static uint32_t
  136. pack_aux(uint8_t *src, int src_bytes)
  137. {
  138. int i;
  139. uint32_t v = 0;
  140. if (src_bytes > 4)
  141. src_bytes = 4;
  142. for (i = 0; i < src_bytes; i++)
  143. v |= ((uint32_t) src[i]) << ((3-i) * 8);
  144. return v;
  145. }
  146. static void
  147. unpack_aux(uint32_t src, uint8_t *dst, int dst_bytes)
  148. {
  149. int i;
  150. if (dst_bytes > 4)
  151. dst_bytes = 4;
  152. for (i = 0; i < dst_bytes; i++)
  153. dst[i] = src >> ((3-i) * 8);
  154. }
  155. /* hrawclock is 1/4 the FSB frequency */
  156. static int
  157. intel_hrawclk(struct drm_device *dev)
  158. {
  159. struct drm_i915_private *dev_priv = dev->dev_private;
  160. uint32_t clkcfg;
  161. clkcfg = I915_READ(CLKCFG);
  162. switch (clkcfg & CLKCFG_FSB_MASK) {
  163. case CLKCFG_FSB_400:
  164. return 100;
  165. case CLKCFG_FSB_533:
  166. return 133;
  167. case CLKCFG_FSB_667:
  168. return 166;
  169. case CLKCFG_FSB_800:
  170. return 200;
  171. case CLKCFG_FSB_1067:
  172. return 266;
  173. case CLKCFG_FSB_1333:
  174. return 333;
  175. /* these two are just a guess; one of them might be right */
  176. case CLKCFG_FSB_1600:
  177. case CLKCFG_FSB_1600_ALT:
  178. return 400;
  179. default:
  180. return 133;
  181. }
  182. }
  183. static int
  184. intel_dp_aux_ch(struct intel_output *intel_output,
  185. uint8_t *send, int send_bytes,
  186. uint8_t *recv, int recv_size)
  187. {
  188. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  189. uint32_t output_reg = dp_priv->output_reg;
  190. struct drm_device *dev = intel_output->base.dev;
  191. struct drm_i915_private *dev_priv = dev->dev_private;
  192. uint32_t ch_ctl = output_reg + 0x10;
  193. uint32_t ch_data = ch_ctl + 4;
  194. int i;
  195. int recv_bytes;
  196. uint32_t ctl;
  197. uint32_t status;
  198. uint32_t aux_clock_divider;
  199. int try;
  200. /* The clock divider is based off the hrawclk,
  201. * and would like to run at 2MHz. So, take the
  202. * hrawclk value and divide by 2 and use that
  203. */
  204. if (IS_eDP(intel_output))
  205. aux_clock_divider = 225; /* eDP input clock at 450Mhz */
  206. else if (HAS_PCH_SPLIT(dev))
  207. aux_clock_divider = 62; /* IRL input clock fixed at 125Mhz */
  208. else
  209. aux_clock_divider = intel_hrawclk(dev) / 2;
  210. /* Must try at least 3 times according to DP spec */
  211. for (try = 0; try < 5; try++) {
  212. /* Load the send data into the aux channel data registers */
  213. for (i = 0; i < send_bytes; i += 4) {
  214. uint32_t d = pack_aux(send + i, send_bytes - i);
  215. I915_WRITE(ch_data + i, d);
  216. }
  217. ctl = (DP_AUX_CH_CTL_SEND_BUSY |
  218. DP_AUX_CH_CTL_TIME_OUT_400us |
  219. (send_bytes << DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT) |
  220. (5 << DP_AUX_CH_CTL_PRECHARGE_2US_SHIFT) |
  221. (aux_clock_divider << DP_AUX_CH_CTL_BIT_CLOCK_2X_SHIFT) |
  222. DP_AUX_CH_CTL_DONE |
  223. DP_AUX_CH_CTL_TIME_OUT_ERROR |
  224. DP_AUX_CH_CTL_RECEIVE_ERROR);
  225. /* Send the command and wait for it to complete */
  226. I915_WRITE(ch_ctl, ctl);
  227. (void) I915_READ(ch_ctl);
  228. for (;;) {
  229. udelay(100);
  230. status = I915_READ(ch_ctl);
  231. if ((status & DP_AUX_CH_CTL_SEND_BUSY) == 0)
  232. break;
  233. }
  234. /* Clear done status and any errors */
  235. I915_WRITE(ch_ctl, (status |
  236. DP_AUX_CH_CTL_DONE |
  237. DP_AUX_CH_CTL_TIME_OUT_ERROR |
  238. DP_AUX_CH_CTL_RECEIVE_ERROR));
  239. (void) I915_READ(ch_ctl);
  240. if ((status & DP_AUX_CH_CTL_TIME_OUT_ERROR) == 0)
  241. break;
  242. }
  243. if ((status & DP_AUX_CH_CTL_DONE) == 0) {
  244. DRM_ERROR("dp_aux_ch not done status 0x%08x\n", status);
  245. return -EBUSY;
  246. }
  247. /* Check for timeout or receive error.
  248. * Timeouts occur when the sink is not connected
  249. */
  250. if (status & DP_AUX_CH_CTL_RECEIVE_ERROR) {
  251. DRM_ERROR("dp_aux_ch receive error status 0x%08x\n", status);
  252. return -EIO;
  253. }
  254. /* Timeouts occur when the device isn't connected, so they're
  255. * "normal" -- don't fill the kernel log with these */
  256. if (status & DP_AUX_CH_CTL_TIME_OUT_ERROR) {
  257. DRM_DEBUG_KMS("dp_aux_ch timeout status 0x%08x\n", status);
  258. return -ETIMEDOUT;
  259. }
  260. /* Unload any bytes sent back from the other side */
  261. recv_bytes = ((status & DP_AUX_CH_CTL_MESSAGE_SIZE_MASK) >>
  262. DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT);
  263. if (recv_bytes > recv_size)
  264. recv_bytes = recv_size;
  265. for (i = 0; i < recv_bytes; i += 4) {
  266. uint32_t d = I915_READ(ch_data + i);
  267. unpack_aux(d, recv + i, recv_bytes - i);
  268. }
  269. return recv_bytes;
  270. }
  271. /* Write data to the aux channel in native mode */
  272. static int
  273. intel_dp_aux_native_write(struct intel_output *intel_output,
  274. uint16_t address, uint8_t *send, int send_bytes)
  275. {
  276. int ret;
  277. uint8_t msg[20];
  278. int msg_bytes;
  279. uint8_t ack;
  280. if (send_bytes > 16)
  281. return -1;
  282. msg[0] = AUX_NATIVE_WRITE << 4;
  283. msg[1] = address >> 8;
  284. msg[2] = address & 0xff;
  285. msg[3] = send_bytes - 1;
  286. memcpy(&msg[4], send, send_bytes);
  287. msg_bytes = send_bytes + 4;
  288. for (;;) {
  289. ret = intel_dp_aux_ch(intel_output, msg, msg_bytes, &ack, 1);
  290. if (ret < 0)
  291. return ret;
  292. if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_ACK)
  293. break;
  294. else if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_DEFER)
  295. udelay(100);
  296. else
  297. return -EIO;
  298. }
  299. return send_bytes;
  300. }
  301. /* Write a single byte to the aux channel in native mode */
  302. static int
  303. intel_dp_aux_native_write_1(struct intel_output *intel_output,
  304. uint16_t address, uint8_t byte)
  305. {
  306. return intel_dp_aux_native_write(intel_output, address, &byte, 1);
  307. }
  308. /* read bytes from a native aux channel */
  309. static int
  310. intel_dp_aux_native_read(struct intel_output *intel_output,
  311. uint16_t address, uint8_t *recv, int recv_bytes)
  312. {
  313. uint8_t msg[4];
  314. int msg_bytes;
  315. uint8_t reply[20];
  316. int reply_bytes;
  317. uint8_t ack;
  318. int ret;
  319. msg[0] = AUX_NATIVE_READ << 4;
  320. msg[1] = address >> 8;
  321. msg[2] = address & 0xff;
  322. msg[3] = recv_bytes - 1;
  323. msg_bytes = 4;
  324. reply_bytes = recv_bytes + 1;
  325. for (;;) {
  326. ret = intel_dp_aux_ch(intel_output, msg, msg_bytes,
  327. reply, reply_bytes);
  328. if (ret == 0)
  329. return -EPROTO;
  330. if (ret < 0)
  331. return ret;
  332. ack = reply[0];
  333. if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_ACK) {
  334. memcpy(recv, reply + 1, ret - 1);
  335. return ret - 1;
  336. }
  337. else if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_DEFER)
  338. udelay(100);
  339. else
  340. return -EIO;
  341. }
  342. }
  343. static int
  344. intel_dp_i2c_aux_ch(struct i2c_adapter *adapter, int mode,
  345. uint8_t write_byte, uint8_t *read_byte)
  346. {
  347. struct i2c_algo_dp_aux_data *algo_data = adapter->algo_data;
  348. struct intel_dp_priv *dp_priv = container_of(adapter,
  349. struct intel_dp_priv,
  350. adapter);
  351. struct intel_output *intel_output = dp_priv->intel_output;
  352. uint16_t address = algo_data->address;
  353. uint8_t msg[5];
  354. uint8_t reply[2];
  355. int msg_bytes;
  356. int reply_bytes;
  357. int ret;
  358. /* Set up the command byte */
  359. if (mode & MODE_I2C_READ)
  360. msg[0] = AUX_I2C_READ << 4;
  361. else
  362. msg[0] = AUX_I2C_WRITE << 4;
  363. if (!(mode & MODE_I2C_STOP))
  364. msg[0] |= AUX_I2C_MOT << 4;
  365. msg[1] = address >> 8;
  366. msg[2] = address;
  367. switch (mode) {
  368. case MODE_I2C_WRITE:
  369. msg[3] = 0;
  370. msg[4] = write_byte;
  371. msg_bytes = 5;
  372. reply_bytes = 1;
  373. break;
  374. case MODE_I2C_READ:
  375. msg[3] = 0;
  376. msg_bytes = 4;
  377. reply_bytes = 2;
  378. break;
  379. default:
  380. msg_bytes = 3;
  381. reply_bytes = 1;
  382. break;
  383. }
  384. for (;;) {
  385. ret = intel_dp_aux_ch(intel_output,
  386. msg, msg_bytes,
  387. reply, reply_bytes);
  388. if (ret < 0) {
  389. DRM_DEBUG_KMS("aux_ch failed %d\n", ret);
  390. return ret;
  391. }
  392. switch (reply[0] & AUX_I2C_REPLY_MASK) {
  393. case AUX_I2C_REPLY_ACK:
  394. if (mode == MODE_I2C_READ) {
  395. *read_byte = reply[1];
  396. }
  397. return reply_bytes - 1;
  398. case AUX_I2C_REPLY_NACK:
  399. DRM_DEBUG_KMS("aux_ch nack\n");
  400. return -EREMOTEIO;
  401. case AUX_I2C_REPLY_DEFER:
  402. DRM_DEBUG_KMS("aux_ch defer\n");
  403. udelay(100);
  404. break;
  405. default:
  406. DRM_ERROR("aux_ch invalid reply 0x%02x\n", reply[0]);
  407. return -EREMOTEIO;
  408. }
  409. }
  410. }
  411. static int
  412. intel_dp_i2c_init(struct intel_output *intel_output, const char *name)
  413. {
  414. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  415. DRM_DEBUG_KMS("i2c_init %s\n", name);
  416. dp_priv->algo.running = false;
  417. dp_priv->algo.address = 0;
  418. dp_priv->algo.aux_ch = intel_dp_i2c_aux_ch;
  419. memset(&dp_priv->adapter, '\0', sizeof (dp_priv->adapter));
  420. dp_priv->adapter.owner = THIS_MODULE;
  421. dp_priv->adapter.class = I2C_CLASS_DDC;
  422. strncpy (dp_priv->adapter.name, name, sizeof(dp_priv->adapter.name) - 1);
  423. dp_priv->adapter.name[sizeof(dp_priv->adapter.name) - 1] = '\0';
  424. dp_priv->adapter.algo_data = &dp_priv->algo;
  425. dp_priv->adapter.dev.parent = &intel_output->base.kdev;
  426. return i2c_dp_aux_add_bus(&dp_priv->adapter);
  427. }
  428. static bool
  429. intel_dp_mode_fixup(struct drm_encoder *encoder, struct drm_display_mode *mode,
  430. struct drm_display_mode *adjusted_mode)
  431. {
  432. struct intel_output *intel_output = enc_to_intel_output(encoder);
  433. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  434. int lane_count, clock;
  435. int max_lane_count = intel_dp_max_lane_count(intel_output);
  436. int max_clock = intel_dp_max_link_bw(intel_output) == DP_LINK_BW_2_7 ? 1 : 0;
  437. static int bws[2] = { DP_LINK_BW_1_62, DP_LINK_BW_2_7 };
  438. for (lane_count = 1; lane_count <= max_lane_count; lane_count <<= 1) {
  439. for (clock = 0; clock <= max_clock; clock++) {
  440. int link_avail = intel_dp_link_clock(bws[clock]) * lane_count;
  441. if (intel_dp_link_required(encoder->dev, intel_output, mode->clock)
  442. <= link_avail) {
  443. dp_priv->link_bw = bws[clock];
  444. dp_priv->lane_count = lane_count;
  445. adjusted_mode->clock = intel_dp_link_clock(dp_priv->link_bw);
  446. DRM_DEBUG_KMS("Display port link bw %02x lane "
  447. "count %d clock %d\n",
  448. dp_priv->link_bw, dp_priv->lane_count,
  449. adjusted_mode->clock);
  450. return true;
  451. }
  452. }
  453. }
  454. return false;
  455. }
  456. struct intel_dp_m_n {
  457. uint32_t tu;
  458. uint32_t gmch_m;
  459. uint32_t gmch_n;
  460. uint32_t link_m;
  461. uint32_t link_n;
  462. };
  463. static void
  464. intel_reduce_ratio(uint32_t *num, uint32_t *den)
  465. {
  466. while (*num > 0xffffff || *den > 0xffffff) {
  467. *num >>= 1;
  468. *den >>= 1;
  469. }
  470. }
  471. static void
  472. intel_dp_compute_m_n(int bytes_per_pixel,
  473. int nlanes,
  474. int pixel_clock,
  475. int link_clock,
  476. struct intel_dp_m_n *m_n)
  477. {
  478. m_n->tu = 64;
  479. m_n->gmch_m = pixel_clock * bytes_per_pixel;
  480. m_n->gmch_n = link_clock * nlanes;
  481. intel_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
  482. m_n->link_m = pixel_clock;
  483. m_n->link_n = link_clock;
  484. intel_reduce_ratio(&m_n->link_m, &m_n->link_n);
  485. }
  486. void
  487. intel_dp_set_m_n(struct drm_crtc *crtc, struct drm_display_mode *mode,
  488. struct drm_display_mode *adjusted_mode)
  489. {
  490. struct drm_device *dev = crtc->dev;
  491. struct drm_mode_config *mode_config = &dev->mode_config;
  492. struct drm_connector *connector;
  493. struct drm_i915_private *dev_priv = dev->dev_private;
  494. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  495. int lane_count = 4;
  496. struct intel_dp_m_n m_n;
  497. /*
  498. * Find the lane count in the intel_output private
  499. */
  500. list_for_each_entry(connector, &mode_config->connector_list, head) {
  501. struct intel_output *intel_output = to_intel_output(connector);
  502. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  503. if (!connector->encoder || connector->encoder->crtc != crtc)
  504. continue;
  505. if (intel_output->type == INTEL_OUTPUT_DISPLAYPORT) {
  506. lane_count = dp_priv->lane_count;
  507. break;
  508. }
  509. }
  510. /*
  511. * Compute the GMCH and Link ratios. The '3' here is
  512. * the number of bytes_per_pixel post-LUT, which we always
  513. * set up for 8-bits of R/G/B, or 3 bytes total.
  514. */
  515. intel_dp_compute_m_n(3, lane_count,
  516. mode->clock, adjusted_mode->clock, &m_n);
  517. if (HAS_PCH_SPLIT(dev)) {
  518. if (intel_crtc->pipe == 0) {
  519. I915_WRITE(TRANSA_DATA_M1,
  520. ((m_n.tu - 1) << PIPE_GMCH_DATA_M_TU_SIZE_SHIFT) |
  521. m_n.gmch_m);
  522. I915_WRITE(TRANSA_DATA_N1, m_n.gmch_n);
  523. I915_WRITE(TRANSA_DP_LINK_M1, m_n.link_m);
  524. I915_WRITE(TRANSA_DP_LINK_N1, m_n.link_n);
  525. } else {
  526. I915_WRITE(TRANSB_DATA_M1,
  527. ((m_n.tu - 1) << PIPE_GMCH_DATA_M_TU_SIZE_SHIFT) |
  528. m_n.gmch_m);
  529. I915_WRITE(TRANSB_DATA_N1, m_n.gmch_n);
  530. I915_WRITE(TRANSB_DP_LINK_M1, m_n.link_m);
  531. I915_WRITE(TRANSB_DP_LINK_N1, m_n.link_n);
  532. }
  533. } else {
  534. if (intel_crtc->pipe == 0) {
  535. I915_WRITE(PIPEA_GMCH_DATA_M,
  536. ((m_n.tu - 1) << PIPE_GMCH_DATA_M_TU_SIZE_SHIFT) |
  537. m_n.gmch_m);
  538. I915_WRITE(PIPEA_GMCH_DATA_N,
  539. m_n.gmch_n);
  540. I915_WRITE(PIPEA_DP_LINK_M, m_n.link_m);
  541. I915_WRITE(PIPEA_DP_LINK_N, m_n.link_n);
  542. } else {
  543. I915_WRITE(PIPEB_GMCH_DATA_M,
  544. ((m_n.tu - 1) << PIPE_GMCH_DATA_M_TU_SIZE_SHIFT) |
  545. m_n.gmch_m);
  546. I915_WRITE(PIPEB_GMCH_DATA_N,
  547. m_n.gmch_n);
  548. I915_WRITE(PIPEB_DP_LINK_M, m_n.link_m);
  549. I915_WRITE(PIPEB_DP_LINK_N, m_n.link_n);
  550. }
  551. }
  552. }
  553. static void
  554. intel_dp_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
  555. struct drm_display_mode *adjusted_mode)
  556. {
  557. struct intel_output *intel_output = enc_to_intel_output(encoder);
  558. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  559. struct drm_crtc *crtc = intel_output->enc.crtc;
  560. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  561. dp_priv->DP = (DP_LINK_TRAIN_OFF |
  562. DP_VOLTAGE_0_4 |
  563. DP_PRE_EMPHASIS_0 |
  564. DP_SYNC_VS_HIGH |
  565. DP_SYNC_HS_HIGH);
  566. switch (dp_priv->lane_count) {
  567. case 1:
  568. dp_priv->DP |= DP_PORT_WIDTH_1;
  569. break;
  570. case 2:
  571. dp_priv->DP |= DP_PORT_WIDTH_2;
  572. break;
  573. case 4:
  574. dp_priv->DP |= DP_PORT_WIDTH_4;
  575. break;
  576. }
  577. if (dp_priv->has_audio)
  578. dp_priv->DP |= DP_AUDIO_OUTPUT_ENABLE;
  579. memset(dp_priv->link_configuration, 0, DP_LINK_CONFIGURATION_SIZE);
  580. dp_priv->link_configuration[0] = dp_priv->link_bw;
  581. dp_priv->link_configuration[1] = dp_priv->lane_count;
  582. /*
  583. * Check for DPCD version > 1.1,
  584. * enable enahanced frame stuff in that case
  585. */
  586. if (dp_priv->dpcd[0] >= 0x11) {
  587. dp_priv->link_configuration[1] |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
  588. dp_priv->DP |= DP_ENHANCED_FRAMING;
  589. }
  590. if (intel_crtc->pipe == 1)
  591. dp_priv->DP |= DP_PIPEB_SELECT;
  592. if (IS_eDP(intel_output)) {
  593. /* don't miss out required setting for eDP */
  594. dp_priv->DP |= DP_PLL_ENABLE;
  595. if (adjusted_mode->clock < 200000)
  596. dp_priv->DP |= DP_PLL_FREQ_160MHZ;
  597. else
  598. dp_priv->DP |= DP_PLL_FREQ_270MHZ;
  599. }
  600. }
  601. static void ironlake_edp_backlight_on (struct drm_device *dev)
  602. {
  603. struct drm_i915_private *dev_priv = dev->dev_private;
  604. u32 pp;
  605. DRM_DEBUG_KMS("\n");
  606. pp = I915_READ(PCH_PP_CONTROL);
  607. pp |= EDP_BLC_ENABLE;
  608. I915_WRITE(PCH_PP_CONTROL, pp);
  609. }
  610. static void ironlake_edp_backlight_off (struct drm_device *dev)
  611. {
  612. struct drm_i915_private *dev_priv = dev->dev_private;
  613. u32 pp;
  614. DRM_DEBUG_KMS("\n");
  615. pp = I915_READ(PCH_PP_CONTROL);
  616. pp &= ~EDP_BLC_ENABLE;
  617. I915_WRITE(PCH_PP_CONTROL, pp);
  618. }
  619. static void
  620. intel_dp_dpms(struct drm_encoder *encoder, int mode)
  621. {
  622. struct intel_output *intel_output = enc_to_intel_output(encoder);
  623. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  624. struct drm_device *dev = intel_output->base.dev;
  625. struct drm_i915_private *dev_priv = dev->dev_private;
  626. uint32_t dp_reg = I915_READ(dp_priv->output_reg);
  627. if (mode != DRM_MODE_DPMS_ON) {
  628. if (dp_reg & DP_PORT_EN) {
  629. intel_dp_link_down(intel_output, dp_priv->DP);
  630. if (IS_eDP(intel_output))
  631. ironlake_edp_backlight_off(dev);
  632. }
  633. } else {
  634. if (!(dp_reg & DP_PORT_EN)) {
  635. intel_dp_link_train(intel_output, dp_priv->DP, dp_priv->link_configuration);
  636. if (IS_eDP(intel_output))
  637. ironlake_edp_backlight_on(dev);
  638. }
  639. }
  640. dp_priv->dpms_mode = mode;
  641. }
  642. /*
  643. * Fetch AUX CH registers 0x202 - 0x207 which contain
  644. * link status information
  645. */
  646. static bool
  647. intel_dp_get_link_status(struct intel_output *intel_output,
  648. uint8_t link_status[DP_LINK_STATUS_SIZE])
  649. {
  650. int ret;
  651. ret = intel_dp_aux_native_read(intel_output,
  652. DP_LANE0_1_STATUS,
  653. link_status, DP_LINK_STATUS_SIZE);
  654. if (ret != DP_LINK_STATUS_SIZE)
  655. return false;
  656. return true;
  657. }
  658. static uint8_t
  659. intel_dp_link_status(uint8_t link_status[DP_LINK_STATUS_SIZE],
  660. int r)
  661. {
  662. return link_status[r - DP_LANE0_1_STATUS];
  663. }
  664. static void
  665. intel_dp_save(struct drm_connector *connector)
  666. {
  667. struct intel_output *intel_output = to_intel_output(connector);
  668. struct drm_device *dev = intel_output->base.dev;
  669. struct drm_i915_private *dev_priv = dev->dev_private;
  670. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  671. dp_priv->save_DP = I915_READ(dp_priv->output_reg);
  672. intel_dp_aux_native_read(intel_output, DP_LINK_BW_SET,
  673. dp_priv->save_link_configuration,
  674. sizeof (dp_priv->save_link_configuration));
  675. }
  676. static uint8_t
  677. intel_get_adjust_request_voltage(uint8_t link_status[DP_LINK_STATUS_SIZE],
  678. int lane)
  679. {
  680. int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
  681. int s = ((lane & 1) ?
  682. DP_ADJUST_VOLTAGE_SWING_LANE1_SHIFT :
  683. DP_ADJUST_VOLTAGE_SWING_LANE0_SHIFT);
  684. uint8_t l = intel_dp_link_status(link_status, i);
  685. return ((l >> s) & 3) << DP_TRAIN_VOLTAGE_SWING_SHIFT;
  686. }
  687. static uint8_t
  688. intel_get_adjust_request_pre_emphasis(uint8_t link_status[DP_LINK_STATUS_SIZE],
  689. int lane)
  690. {
  691. int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
  692. int s = ((lane & 1) ?
  693. DP_ADJUST_PRE_EMPHASIS_LANE1_SHIFT :
  694. DP_ADJUST_PRE_EMPHASIS_LANE0_SHIFT);
  695. uint8_t l = intel_dp_link_status(link_status, i);
  696. return ((l >> s) & 3) << DP_TRAIN_PRE_EMPHASIS_SHIFT;
  697. }
  698. #if 0
  699. static char *voltage_names[] = {
  700. "0.4V", "0.6V", "0.8V", "1.2V"
  701. };
  702. static char *pre_emph_names[] = {
  703. "0dB", "3.5dB", "6dB", "9.5dB"
  704. };
  705. static char *link_train_names[] = {
  706. "pattern 1", "pattern 2", "idle", "off"
  707. };
  708. #endif
  709. /*
  710. * These are source-specific values; current Intel hardware supports
  711. * a maximum voltage of 800mV and a maximum pre-emphasis of 6dB
  712. */
  713. #define I830_DP_VOLTAGE_MAX DP_TRAIN_VOLTAGE_SWING_800
  714. static uint8_t
  715. intel_dp_pre_emphasis_max(uint8_t voltage_swing)
  716. {
  717. switch (voltage_swing & DP_TRAIN_VOLTAGE_SWING_MASK) {
  718. case DP_TRAIN_VOLTAGE_SWING_400:
  719. return DP_TRAIN_PRE_EMPHASIS_6;
  720. case DP_TRAIN_VOLTAGE_SWING_600:
  721. return DP_TRAIN_PRE_EMPHASIS_6;
  722. case DP_TRAIN_VOLTAGE_SWING_800:
  723. return DP_TRAIN_PRE_EMPHASIS_3_5;
  724. case DP_TRAIN_VOLTAGE_SWING_1200:
  725. default:
  726. return DP_TRAIN_PRE_EMPHASIS_0;
  727. }
  728. }
  729. static void
  730. intel_get_adjust_train(struct intel_output *intel_output,
  731. uint8_t link_status[DP_LINK_STATUS_SIZE],
  732. int lane_count,
  733. uint8_t train_set[4])
  734. {
  735. uint8_t v = 0;
  736. uint8_t p = 0;
  737. int lane;
  738. for (lane = 0; lane < lane_count; lane++) {
  739. uint8_t this_v = intel_get_adjust_request_voltage(link_status, lane);
  740. uint8_t this_p = intel_get_adjust_request_pre_emphasis(link_status, lane);
  741. if (this_v > v)
  742. v = this_v;
  743. if (this_p > p)
  744. p = this_p;
  745. }
  746. if (v >= I830_DP_VOLTAGE_MAX)
  747. v = I830_DP_VOLTAGE_MAX | DP_TRAIN_MAX_SWING_REACHED;
  748. if (p >= intel_dp_pre_emphasis_max(v))
  749. p = intel_dp_pre_emphasis_max(v) | DP_TRAIN_MAX_PRE_EMPHASIS_REACHED;
  750. for (lane = 0; lane < 4; lane++)
  751. train_set[lane] = v | p;
  752. }
  753. static uint32_t
  754. intel_dp_signal_levels(uint8_t train_set, int lane_count)
  755. {
  756. uint32_t signal_levels = 0;
  757. switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
  758. case DP_TRAIN_VOLTAGE_SWING_400:
  759. default:
  760. signal_levels |= DP_VOLTAGE_0_4;
  761. break;
  762. case DP_TRAIN_VOLTAGE_SWING_600:
  763. signal_levels |= DP_VOLTAGE_0_6;
  764. break;
  765. case DP_TRAIN_VOLTAGE_SWING_800:
  766. signal_levels |= DP_VOLTAGE_0_8;
  767. break;
  768. case DP_TRAIN_VOLTAGE_SWING_1200:
  769. signal_levels |= DP_VOLTAGE_1_2;
  770. break;
  771. }
  772. switch (train_set & DP_TRAIN_PRE_EMPHASIS_MASK) {
  773. case DP_TRAIN_PRE_EMPHASIS_0:
  774. default:
  775. signal_levels |= DP_PRE_EMPHASIS_0;
  776. break;
  777. case DP_TRAIN_PRE_EMPHASIS_3_5:
  778. signal_levels |= DP_PRE_EMPHASIS_3_5;
  779. break;
  780. case DP_TRAIN_PRE_EMPHASIS_6:
  781. signal_levels |= DP_PRE_EMPHASIS_6;
  782. break;
  783. case DP_TRAIN_PRE_EMPHASIS_9_5:
  784. signal_levels |= DP_PRE_EMPHASIS_9_5;
  785. break;
  786. }
  787. return signal_levels;
  788. }
  789. static uint8_t
  790. intel_get_lane_status(uint8_t link_status[DP_LINK_STATUS_SIZE],
  791. int lane)
  792. {
  793. int i = DP_LANE0_1_STATUS + (lane >> 1);
  794. int s = (lane & 1) * 4;
  795. uint8_t l = intel_dp_link_status(link_status, i);
  796. return (l >> s) & 0xf;
  797. }
  798. /* Check for clock recovery is done on all channels */
  799. static bool
  800. intel_clock_recovery_ok(uint8_t link_status[DP_LINK_STATUS_SIZE], int lane_count)
  801. {
  802. int lane;
  803. uint8_t lane_status;
  804. for (lane = 0; lane < lane_count; lane++) {
  805. lane_status = intel_get_lane_status(link_status, lane);
  806. if ((lane_status & DP_LANE_CR_DONE) == 0)
  807. return false;
  808. }
  809. return true;
  810. }
  811. /* Check to see if channel eq is done on all channels */
  812. #define CHANNEL_EQ_BITS (DP_LANE_CR_DONE|\
  813. DP_LANE_CHANNEL_EQ_DONE|\
  814. DP_LANE_SYMBOL_LOCKED)
  815. static bool
  816. intel_channel_eq_ok(uint8_t link_status[DP_LINK_STATUS_SIZE], int lane_count)
  817. {
  818. uint8_t lane_align;
  819. uint8_t lane_status;
  820. int lane;
  821. lane_align = intel_dp_link_status(link_status,
  822. DP_LANE_ALIGN_STATUS_UPDATED);
  823. if ((lane_align & DP_INTERLANE_ALIGN_DONE) == 0)
  824. return false;
  825. for (lane = 0; lane < lane_count; lane++) {
  826. lane_status = intel_get_lane_status(link_status, lane);
  827. if ((lane_status & CHANNEL_EQ_BITS) != CHANNEL_EQ_BITS)
  828. return false;
  829. }
  830. return true;
  831. }
  832. static bool
  833. intel_dp_set_link_train(struct intel_output *intel_output,
  834. uint32_t dp_reg_value,
  835. uint8_t dp_train_pat,
  836. uint8_t train_set[4],
  837. bool first)
  838. {
  839. struct drm_device *dev = intel_output->base.dev;
  840. struct drm_i915_private *dev_priv = dev->dev_private;
  841. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  842. int ret;
  843. I915_WRITE(dp_priv->output_reg, dp_reg_value);
  844. POSTING_READ(dp_priv->output_reg);
  845. if (first)
  846. intel_wait_for_vblank(dev);
  847. intel_dp_aux_native_write_1(intel_output,
  848. DP_TRAINING_PATTERN_SET,
  849. dp_train_pat);
  850. ret = intel_dp_aux_native_write(intel_output,
  851. DP_TRAINING_LANE0_SET, train_set, 4);
  852. if (ret != 4)
  853. return false;
  854. return true;
  855. }
  856. static void
  857. intel_dp_link_train(struct intel_output *intel_output, uint32_t DP,
  858. uint8_t link_configuration[DP_LINK_CONFIGURATION_SIZE])
  859. {
  860. struct drm_device *dev = intel_output->base.dev;
  861. struct drm_i915_private *dev_priv = dev->dev_private;
  862. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  863. uint8_t train_set[4];
  864. uint8_t link_status[DP_LINK_STATUS_SIZE];
  865. int i;
  866. uint8_t voltage;
  867. bool clock_recovery = false;
  868. bool channel_eq = false;
  869. bool first = true;
  870. int tries;
  871. /* Write the link configuration data */
  872. intel_dp_aux_native_write(intel_output, 0x100,
  873. link_configuration, DP_LINK_CONFIGURATION_SIZE);
  874. DP |= DP_PORT_EN;
  875. DP &= ~DP_LINK_TRAIN_MASK;
  876. memset(train_set, 0, 4);
  877. voltage = 0xff;
  878. tries = 0;
  879. clock_recovery = false;
  880. for (;;) {
  881. /* Use train_set[0] to set the voltage and pre emphasis values */
  882. uint32_t signal_levels = intel_dp_signal_levels(train_set[0], dp_priv->lane_count);
  883. DP = (DP & ~(DP_VOLTAGE_MASK|DP_PRE_EMPHASIS_MASK)) | signal_levels;
  884. if (!intel_dp_set_link_train(intel_output, DP | DP_LINK_TRAIN_PAT_1,
  885. DP_TRAINING_PATTERN_1, train_set, first))
  886. break;
  887. first = false;
  888. /* Set training pattern 1 */
  889. udelay(100);
  890. if (!intel_dp_get_link_status(intel_output, link_status))
  891. break;
  892. if (intel_clock_recovery_ok(link_status, dp_priv->lane_count)) {
  893. clock_recovery = true;
  894. break;
  895. }
  896. /* Check to see if we've tried the max voltage */
  897. for (i = 0; i < dp_priv->lane_count; i++)
  898. if ((train_set[i] & DP_TRAIN_MAX_SWING_REACHED) == 0)
  899. break;
  900. if (i == dp_priv->lane_count)
  901. break;
  902. /* Check to see if we've tried the same voltage 5 times */
  903. if ((train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK) == voltage) {
  904. ++tries;
  905. if (tries == 5)
  906. break;
  907. } else
  908. tries = 0;
  909. voltage = train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK;
  910. /* Compute new train_set as requested by target */
  911. intel_get_adjust_train(intel_output, link_status, dp_priv->lane_count, train_set);
  912. }
  913. /* channel equalization */
  914. tries = 0;
  915. channel_eq = false;
  916. for (;;) {
  917. /* Use train_set[0] to set the voltage and pre emphasis values */
  918. uint32_t signal_levels = intel_dp_signal_levels(train_set[0], dp_priv->lane_count);
  919. DP = (DP & ~(DP_VOLTAGE_MASK|DP_PRE_EMPHASIS_MASK)) | signal_levels;
  920. /* channel eq pattern */
  921. if (!intel_dp_set_link_train(intel_output, DP | DP_LINK_TRAIN_PAT_2,
  922. DP_TRAINING_PATTERN_2, train_set,
  923. false))
  924. break;
  925. udelay(400);
  926. if (!intel_dp_get_link_status(intel_output, link_status))
  927. break;
  928. if (intel_channel_eq_ok(link_status, dp_priv->lane_count)) {
  929. channel_eq = true;
  930. break;
  931. }
  932. /* Try 5 times */
  933. if (tries > 5)
  934. break;
  935. /* Compute new train_set as requested by target */
  936. intel_get_adjust_train(intel_output, link_status, dp_priv->lane_count, train_set);
  937. ++tries;
  938. }
  939. I915_WRITE(dp_priv->output_reg, DP | DP_LINK_TRAIN_OFF);
  940. POSTING_READ(dp_priv->output_reg);
  941. intel_dp_aux_native_write_1(intel_output,
  942. DP_TRAINING_PATTERN_SET, DP_TRAINING_PATTERN_DISABLE);
  943. }
  944. static void
  945. intel_dp_link_down(struct intel_output *intel_output, uint32_t DP)
  946. {
  947. struct drm_device *dev = intel_output->base.dev;
  948. struct drm_i915_private *dev_priv = dev->dev_private;
  949. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  950. DRM_DEBUG_KMS("\n");
  951. if (IS_eDP(intel_output)) {
  952. DP &= ~DP_PLL_ENABLE;
  953. I915_WRITE(dp_priv->output_reg, DP);
  954. POSTING_READ(dp_priv->output_reg);
  955. udelay(100);
  956. }
  957. DP &= ~DP_LINK_TRAIN_MASK;
  958. I915_WRITE(dp_priv->output_reg, DP | DP_LINK_TRAIN_PAT_IDLE);
  959. POSTING_READ(dp_priv->output_reg);
  960. udelay(17000);
  961. if (IS_eDP(intel_output))
  962. DP |= DP_LINK_TRAIN_OFF;
  963. I915_WRITE(dp_priv->output_reg, DP & ~DP_PORT_EN);
  964. POSTING_READ(dp_priv->output_reg);
  965. }
  966. static void
  967. intel_dp_restore(struct drm_connector *connector)
  968. {
  969. struct intel_output *intel_output = to_intel_output(connector);
  970. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  971. if (dp_priv->save_DP & DP_PORT_EN)
  972. intel_dp_link_train(intel_output, dp_priv->save_DP, dp_priv->save_link_configuration);
  973. else
  974. intel_dp_link_down(intel_output, dp_priv->save_DP);
  975. }
  976. /*
  977. * According to DP spec
  978. * 5.1.2:
  979. * 1. Read DPCD
  980. * 2. Configure link according to Receiver Capabilities
  981. * 3. Use Link Training from 2.5.3.3 and 3.5.1.3
  982. * 4. Check link status on receipt of hot-plug interrupt
  983. */
  984. static void
  985. intel_dp_check_link_status(struct intel_output *intel_output)
  986. {
  987. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  988. uint8_t link_status[DP_LINK_STATUS_SIZE];
  989. if (!intel_output->enc.crtc)
  990. return;
  991. if (!intel_dp_get_link_status(intel_output, link_status)) {
  992. intel_dp_link_down(intel_output, dp_priv->DP);
  993. return;
  994. }
  995. if (!intel_channel_eq_ok(link_status, dp_priv->lane_count))
  996. intel_dp_link_train(intel_output, dp_priv->DP, dp_priv->link_configuration);
  997. }
  998. static enum drm_connector_status
  999. ironlake_dp_detect(struct drm_connector *connector)
  1000. {
  1001. struct intel_output *intel_output = to_intel_output(connector);
  1002. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  1003. enum drm_connector_status status;
  1004. status = connector_status_disconnected;
  1005. if (intel_dp_aux_native_read(intel_output,
  1006. 0x000, dp_priv->dpcd,
  1007. sizeof (dp_priv->dpcd)) == sizeof (dp_priv->dpcd))
  1008. {
  1009. if (dp_priv->dpcd[0] != 0)
  1010. status = connector_status_connected;
  1011. }
  1012. return status;
  1013. }
  1014. /**
  1015. * Uses CRT_HOTPLUG_EN and CRT_HOTPLUG_STAT to detect DP connection.
  1016. *
  1017. * \return true if DP port is connected.
  1018. * \return false if DP port is disconnected.
  1019. */
  1020. static enum drm_connector_status
  1021. intel_dp_detect(struct drm_connector *connector)
  1022. {
  1023. struct intel_output *intel_output = to_intel_output(connector);
  1024. struct drm_device *dev = intel_output->base.dev;
  1025. struct drm_i915_private *dev_priv = dev->dev_private;
  1026. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  1027. uint32_t temp, bit;
  1028. enum drm_connector_status status;
  1029. dp_priv->has_audio = false;
  1030. if (HAS_PCH_SPLIT(dev))
  1031. return ironlake_dp_detect(connector);
  1032. temp = I915_READ(PORT_HOTPLUG_EN);
  1033. I915_WRITE(PORT_HOTPLUG_EN,
  1034. temp |
  1035. DPB_HOTPLUG_INT_EN |
  1036. DPC_HOTPLUG_INT_EN |
  1037. DPD_HOTPLUG_INT_EN);
  1038. POSTING_READ(PORT_HOTPLUG_EN);
  1039. switch (dp_priv->output_reg) {
  1040. case DP_B:
  1041. bit = DPB_HOTPLUG_INT_STATUS;
  1042. break;
  1043. case DP_C:
  1044. bit = DPC_HOTPLUG_INT_STATUS;
  1045. break;
  1046. case DP_D:
  1047. bit = DPD_HOTPLUG_INT_STATUS;
  1048. break;
  1049. default:
  1050. return connector_status_unknown;
  1051. }
  1052. temp = I915_READ(PORT_HOTPLUG_STAT);
  1053. if ((temp & bit) == 0)
  1054. return connector_status_disconnected;
  1055. status = connector_status_disconnected;
  1056. if (intel_dp_aux_native_read(intel_output,
  1057. 0x000, dp_priv->dpcd,
  1058. sizeof (dp_priv->dpcd)) == sizeof (dp_priv->dpcd))
  1059. {
  1060. if (dp_priv->dpcd[0] != 0)
  1061. status = connector_status_connected;
  1062. }
  1063. return status;
  1064. }
  1065. static int intel_dp_get_modes(struct drm_connector *connector)
  1066. {
  1067. struct intel_output *intel_output = to_intel_output(connector);
  1068. struct drm_device *dev = intel_output->base.dev;
  1069. struct drm_i915_private *dev_priv = dev->dev_private;
  1070. int ret;
  1071. /* We should parse the EDID data and find out if it has an audio sink
  1072. */
  1073. ret = intel_ddc_get_modes(intel_output);
  1074. if (ret)
  1075. return ret;
  1076. /* if eDP has no EDID, try to use fixed panel mode from VBT */
  1077. if (IS_eDP(intel_output)) {
  1078. if (dev_priv->panel_fixed_mode != NULL) {
  1079. struct drm_display_mode *mode;
  1080. mode = drm_mode_duplicate(dev, dev_priv->panel_fixed_mode);
  1081. drm_mode_probed_add(connector, mode);
  1082. return 1;
  1083. }
  1084. }
  1085. return 0;
  1086. }
  1087. static void
  1088. intel_dp_destroy (struct drm_connector *connector)
  1089. {
  1090. struct intel_output *intel_output = to_intel_output(connector);
  1091. if (intel_output->i2c_bus)
  1092. intel_i2c_destroy(intel_output->i2c_bus);
  1093. drm_sysfs_connector_remove(connector);
  1094. drm_connector_cleanup(connector);
  1095. kfree(intel_output);
  1096. }
  1097. static const struct drm_encoder_helper_funcs intel_dp_helper_funcs = {
  1098. .dpms = intel_dp_dpms,
  1099. .mode_fixup = intel_dp_mode_fixup,
  1100. .prepare = intel_encoder_prepare,
  1101. .mode_set = intel_dp_mode_set,
  1102. .commit = intel_encoder_commit,
  1103. };
  1104. static const struct drm_connector_funcs intel_dp_connector_funcs = {
  1105. .dpms = drm_helper_connector_dpms,
  1106. .save = intel_dp_save,
  1107. .restore = intel_dp_restore,
  1108. .detect = intel_dp_detect,
  1109. .fill_modes = drm_helper_probe_single_connector_modes,
  1110. .destroy = intel_dp_destroy,
  1111. };
  1112. static const struct drm_connector_helper_funcs intel_dp_connector_helper_funcs = {
  1113. .get_modes = intel_dp_get_modes,
  1114. .mode_valid = intel_dp_mode_valid,
  1115. .best_encoder = intel_best_encoder,
  1116. };
  1117. static void intel_dp_enc_destroy(struct drm_encoder *encoder)
  1118. {
  1119. drm_encoder_cleanup(encoder);
  1120. }
  1121. static const struct drm_encoder_funcs intel_dp_enc_funcs = {
  1122. .destroy = intel_dp_enc_destroy,
  1123. };
  1124. void
  1125. intel_dp_hot_plug(struct intel_output *intel_output)
  1126. {
  1127. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  1128. if (dp_priv->dpms_mode == DRM_MODE_DPMS_ON)
  1129. intel_dp_check_link_status(intel_output);
  1130. }
  1131. void
  1132. intel_dp_init(struct drm_device *dev, int output_reg)
  1133. {
  1134. struct drm_i915_private *dev_priv = dev->dev_private;
  1135. struct drm_connector *connector;
  1136. struct intel_output *intel_output;
  1137. struct intel_dp_priv *dp_priv;
  1138. const char *name = NULL;
  1139. intel_output = kcalloc(sizeof(struct intel_output) +
  1140. sizeof(struct intel_dp_priv), 1, GFP_KERNEL);
  1141. if (!intel_output)
  1142. return;
  1143. dp_priv = (struct intel_dp_priv *)(intel_output + 1);
  1144. connector = &intel_output->base;
  1145. drm_connector_init(dev, connector, &intel_dp_connector_funcs,
  1146. DRM_MODE_CONNECTOR_DisplayPort);
  1147. drm_connector_helper_add(connector, &intel_dp_connector_helper_funcs);
  1148. if (output_reg == DP_A)
  1149. intel_output->type = INTEL_OUTPUT_EDP;
  1150. else
  1151. intel_output->type = INTEL_OUTPUT_DISPLAYPORT;
  1152. if (output_reg == DP_B || output_reg == PCH_DP_B)
  1153. intel_output->clone_mask = (1 << INTEL_DP_B_CLONE_BIT);
  1154. else if (output_reg == DP_C || output_reg == PCH_DP_C)
  1155. intel_output->clone_mask = (1 << INTEL_DP_C_CLONE_BIT);
  1156. else if (output_reg == DP_D || output_reg == PCH_DP_D)
  1157. intel_output->clone_mask = (1 << INTEL_DP_D_CLONE_BIT);
  1158. if (IS_eDP(intel_output))
  1159. intel_output->clone_mask = (1 << INTEL_EDP_CLONE_BIT);
  1160. intel_output->crtc_mask = (1 << 0) | (1 << 1);
  1161. connector->interlace_allowed = true;
  1162. connector->doublescan_allowed = 0;
  1163. dp_priv->intel_output = intel_output;
  1164. dp_priv->output_reg = output_reg;
  1165. dp_priv->has_audio = false;
  1166. dp_priv->dpms_mode = DRM_MODE_DPMS_ON;
  1167. intel_output->dev_priv = dp_priv;
  1168. drm_encoder_init(dev, &intel_output->enc, &intel_dp_enc_funcs,
  1169. DRM_MODE_ENCODER_TMDS);
  1170. drm_encoder_helper_add(&intel_output->enc, &intel_dp_helper_funcs);
  1171. drm_mode_connector_attach_encoder(&intel_output->base,
  1172. &intel_output->enc);
  1173. drm_sysfs_connector_add(connector);
  1174. /* Set up the DDC bus. */
  1175. switch (output_reg) {
  1176. case DP_A:
  1177. name = "DPDDC-A";
  1178. break;
  1179. case DP_B:
  1180. case PCH_DP_B:
  1181. dev_priv->hotplug_supported_mask |=
  1182. HDMIB_HOTPLUG_INT_STATUS;
  1183. name = "DPDDC-B";
  1184. break;
  1185. case DP_C:
  1186. case PCH_DP_C:
  1187. dev_priv->hotplug_supported_mask |=
  1188. HDMIC_HOTPLUG_INT_STATUS;
  1189. name = "DPDDC-C";
  1190. break;
  1191. case DP_D:
  1192. case PCH_DP_D:
  1193. dev_priv->hotplug_supported_mask |=
  1194. HDMID_HOTPLUG_INT_STATUS;
  1195. name = "DPDDC-D";
  1196. break;
  1197. }
  1198. intel_dp_i2c_init(intel_output, name);
  1199. intel_output->ddc_bus = &dp_priv->adapter;
  1200. intel_output->hot_plug = intel_dp_hot_plug;
  1201. if (output_reg == DP_A) {
  1202. /* initialize panel mode from VBT if available for eDP */
  1203. if (dev_priv->lfp_lvds_vbt_mode) {
  1204. dev_priv->panel_fixed_mode =
  1205. drm_mode_duplicate(dev, dev_priv->lfp_lvds_vbt_mode);
  1206. if (dev_priv->panel_fixed_mode) {
  1207. dev_priv->panel_fixed_mode->type |=
  1208. DRM_MODE_TYPE_PREFERRED;
  1209. }
  1210. }
  1211. }
  1212. /* For G4X desktop chip, PEG_BAND_GAP_DATA 3:0 must first be written
  1213. * 0xd. Failure to do so will result in spurious interrupts being
  1214. * generated on the port when a cable is not attached.
  1215. */
  1216. if (IS_G4X(dev) && !IS_GM45(dev)) {
  1217. u32 temp = I915_READ(PEG_BAND_GAP_DATA);
  1218. I915_WRITE(PEG_BAND_GAP_DATA, (temp & ~0xf) | 0xd);
  1219. }
  1220. }