intel_dp.c 35 KB

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