intel_dp.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390
  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(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, int mode,
  339. uint8_t write_byte, uint8_t *read_byte)
  340. {
  341. struct i2c_algo_dp_aux_data *algo_data = adapter->algo_data;
  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. uint16_t address = algo_data->address;
  347. uint8_t msg[5];
  348. uint8_t reply[2];
  349. int msg_bytes;
  350. int reply_bytes;
  351. int ret;
  352. /* Set up the command byte */
  353. if (mode & MODE_I2C_READ)
  354. msg[0] = AUX_I2C_READ << 4;
  355. else
  356. msg[0] = AUX_I2C_WRITE << 4;
  357. if (!(mode & MODE_I2C_STOP))
  358. msg[0] |= AUX_I2C_MOT << 4;
  359. msg[1] = address >> 8;
  360. msg[2] = address;
  361. switch (mode) {
  362. case MODE_I2C_WRITE:
  363. msg[3] = 0;
  364. msg[4] = write_byte;
  365. msg_bytes = 5;
  366. reply_bytes = 1;
  367. break;
  368. case MODE_I2C_READ:
  369. msg[3] = 0;
  370. msg_bytes = 4;
  371. reply_bytes = 2;
  372. break;
  373. default:
  374. msg_bytes = 3;
  375. reply_bytes = 1;
  376. break;
  377. }
  378. for (;;) {
  379. ret = intel_dp_aux_ch(intel_output,
  380. msg, msg_bytes,
  381. reply, reply_bytes);
  382. if (ret < 0) {
  383. DRM_DEBUG("aux_ch failed %d\n", ret);
  384. return ret;
  385. }
  386. switch (reply[0] & AUX_I2C_REPLY_MASK) {
  387. case AUX_I2C_REPLY_ACK:
  388. if (mode == MODE_I2C_READ) {
  389. *read_byte = reply[1];
  390. }
  391. return reply_bytes - 1;
  392. case AUX_I2C_REPLY_NACK:
  393. DRM_DEBUG("aux_ch nack\n");
  394. return -EREMOTEIO;
  395. case AUX_I2C_REPLY_DEFER:
  396. DRM_DEBUG("aux_ch defer\n");
  397. udelay(100);
  398. break;
  399. default:
  400. DRM_ERROR("aux_ch invalid reply 0x%02x\n", reply[0]);
  401. return -EREMOTEIO;
  402. }
  403. }
  404. }
  405. static int
  406. intel_dp_i2c_init(struct intel_output *intel_output, const char *name)
  407. {
  408. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  409. DRM_DEBUG_KMS("i2c_init %s\n", name);
  410. dp_priv->algo.running = false;
  411. dp_priv->algo.address = 0;
  412. dp_priv->algo.aux_ch = intel_dp_i2c_aux_ch;
  413. memset(&dp_priv->adapter, '\0', sizeof (dp_priv->adapter));
  414. dp_priv->adapter.owner = THIS_MODULE;
  415. dp_priv->adapter.class = I2C_CLASS_DDC;
  416. strncpy (dp_priv->adapter.name, name, sizeof(dp_priv->adapter.name) - 1);
  417. dp_priv->adapter.name[sizeof(dp_priv->adapter.name) - 1] = '\0';
  418. dp_priv->adapter.algo_data = &dp_priv->algo;
  419. dp_priv->adapter.dev.parent = &intel_output->base.kdev;
  420. return i2c_dp_aux_add_bus(&dp_priv->adapter);
  421. }
  422. static bool
  423. intel_dp_mode_fixup(struct drm_encoder *encoder, struct drm_display_mode *mode,
  424. struct drm_display_mode *adjusted_mode)
  425. {
  426. struct intel_output *intel_output = enc_to_intel_output(encoder);
  427. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  428. int lane_count, clock;
  429. int max_lane_count = intel_dp_max_lane_count(intel_output);
  430. int max_clock = intel_dp_max_link_bw(intel_output) == DP_LINK_BW_2_7 ? 1 : 0;
  431. static int bws[2] = { DP_LINK_BW_1_62, DP_LINK_BW_2_7 };
  432. for (lane_count = 1; lane_count <= max_lane_count; lane_count <<= 1) {
  433. for (clock = 0; clock <= max_clock; clock++) {
  434. int link_avail = intel_dp_link_clock(bws[clock]) * lane_count;
  435. if (intel_dp_link_required(mode->clock) <= link_avail) {
  436. dp_priv->link_bw = bws[clock];
  437. dp_priv->lane_count = lane_count;
  438. adjusted_mode->clock = intel_dp_link_clock(dp_priv->link_bw);
  439. DRM_DEBUG("Display port link bw %02x lane count %d clock %d\n",
  440. dp_priv->link_bw, dp_priv->lane_count,
  441. adjusted_mode->clock);
  442. return true;
  443. }
  444. }
  445. }
  446. return false;
  447. }
  448. struct intel_dp_m_n {
  449. uint32_t tu;
  450. uint32_t gmch_m;
  451. uint32_t gmch_n;
  452. uint32_t link_m;
  453. uint32_t link_n;
  454. };
  455. static void
  456. intel_reduce_ratio(uint32_t *num, uint32_t *den)
  457. {
  458. while (*num > 0xffffff || *den > 0xffffff) {
  459. *num >>= 1;
  460. *den >>= 1;
  461. }
  462. }
  463. static void
  464. intel_dp_compute_m_n(int bytes_per_pixel,
  465. int nlanes,
  466. int pixel_clock,
  467. int link_clock,
  468. struct intel_dp_m_n *m_n)
  469. {
  470. m_n->tu = 64;
  471. m_n->gmch_m = pixel_clock * bytes_per_pixel;
  472. m_n->gmch_n = link_clock * nlanes;
  473. intel_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
  474. m_n->link_m = pixel_clock;
  475. m_n->link_n = link_clock;
  476. intel_reduce_ratio(&m_n->link_m, &m_n->link_n);
  477. }
  478. void
  479. intel_dp_set_m_n(struct drm_crtc *crtc, struct drm_display_mode *mode,
  480. struct drm_display_mode *adjusted_mode)
  481. {
  482. struct drm_device *dev = crtc->dev;
  483. struct drm_mode_config *mode_config = &dev->mode_config;
  484. struct drm_connector *connector;
  485. struct drm_i915_private *dev_priv = dev->dev_private;
  486. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  487. int lane_count = 4;
  488. struct intel_dp_m_n m_n;
  489. /*
  490. * Find the lane count in the intel_output private
  491. */
  492. list_for_each_entry(connector, &mode_config->connector_list, head) {
  493. struct intel_output *intel_output = to_intel_output(connector);
  494. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  495. if (!connector->encoder || connector->encoder->crtc != crtc)
  496. continue;
  497. if (intel_output->type == INTEL_OUTPUT_DISPLAYPORT) {
  498. lane_count = dp_priv->lane_count;
  499. break;
  500. }
  501. }
  502. /*
  503. * Compute the GMCH and Link ratios. The '3' here is
  504. * the number of bytes_per_pixel post-LUT, which we always
  505. * set up for 8-bits of R/G/B, or 3 bytes total.
  506. */
  507. intel_dp_compute_m_n(3, lane_count,
  508. mode->clock, adjusted_mode->clock, &m_n);
  509. if (IS_IGDNG(dev)) {
  510. if (intel_crtc->pipe == 0) {
  511. I915_WRITE(TRANSA_DATA_M1,
  512. ((m_n.tu - 1) << PIPE_GMCH_DATA_M_TU_SIZE_SHIFT) |
  513. m_n.gmch_m);
  514. I915_WRITE(TRANSA_DATA_N1, m_n.gmch_n);
  515. I915_WRITE(TRANSA_DP_LINK_M1, m_n.link_m);
  516. I915_WRITE(TRANSA_DP_LINK_N1, m_n.link_n);
  517. } else {
  518. I915_WRITE(TRANSB_DATA_M1,
  519. ((m_n.tu - 1) << PIPE_GMCH_DATA_M_TU_SIZE_SHIFT) |
  520. m_n.gmch_m);
  521. I915_WRITE(TRANSB_DATA_N1, m_n.gmch_n);
  522. I915_WRITE(TRANSB_DP_LINK_M1, m_n.link_m);
  523. I915_WRITE(TRANSB_DP_LINK_N1, m_n.link_n);
  524. }
  525. } else {
  526. if (intel_crtc->pipe == 0) {
  527. I915_WRITE(PIPEA_GMCH_DATA_M,
  528. ((m_n.tu - 1) << PIPE_GMCH_DATA_M_TU_SIZE_SHIFT) |
  529. m_n.gmch_m);
  530. I915_WRITE(PIPEA_GMCH_DATA_N,
  531. m_n.gmch_n);
  532. I915_WRITE(PIPEA_DP_LINK_M, m_n.link_m);
  533. I915_WRITE(PIPEA_DP_LINK_N, m_n.link_n);
  534. } else {
  535. I915_WRITE(PIPEB_GMCH_DATA_M,
  536. ((m_n.tu - 1) << PIPE_GMCH_DATA_M_TU_SIZE_SHIFT) |
  537. m_n.gmch_m);
  538. I915_WRITE(PIPEB_GMCH_DATA_N,
  539. m_n.gmch_n);
  540. I915_WRITE(PIPEB_DP_LINK_M, m_n.link_m);
  541. I915_WRITE(PIPEB_DP_LINK_N, m_n.link_n);
  542. }
  543. }
  544. }
  545. static void
  546. intel_dp_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
  547. struct drm_display_mode *adjusted_mode)
  548. {
  549. struct intel_output *intel_output = enc_to_intel_output(encoder);
  550. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  551. struct drm_crtc *crtc = intel_output->enc.crtc;
  552. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  553. dp_priv->DP = (DP_LINK_TRAIN_OFF |
  554. DP_VOLTAGE_0_4 |
  555. DP_PRE_EMPHASIS_0 |
  556. DP_SYNC_VS_HIGH |
  557. DP_SYNC_HS_HIGH);
  558. switch (dp_priv->lane_count) {
  559. case 1:
  560. dp_priv->DP |= DP_PORT_WIDTH_1;
  561. break;
  562. case 2:
  563. dp_priv->DP |= DP_PORT_WIDTH_2;
  564. break;
  565. case 4:
  566. dp_priv->DP |= DP_PORT_WIDTH_4;
  567. break;
  568. }
  569. if (dp_priv->has_audio)
  570. dp_priv->DP |= DP_AUDIO_OUTPUT_ENABLE;
  571. memset(dp_priv->link_configuration, 0, DP_LINK_CONFIGURATION_SIZE);
  572. dp_priv->link_configuration[0] = dp_priv->link_bw;
  573. dp_priv->link_configuration[1] = dp_priv->lane_count;
  574. /*
  575. * Check for DPCD version > 1.1,
  576. * enable enahanced frame stuff in that case
  577. */
  578. if (dp_priv->dpcd[0] >= 0x11) {
  579. dp_priv->link_configuration[1] |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
  580. dp_priv->DP |= DP_ENHANCED_FRAMING;
  581. }
  582. if (intel_crtc->pipe == 1)
  583. dp_priv->DP |= DP_PIPEB_SELECT;
  584. if (IS_eDP(intel_output)) {
  585. /* don't miss out required setting for eDP */
  586. dp_priv->DP |= DP_PLL_ENABLE;
  587. if (adjusted_mode->clock < 200000)
  588. dp_priv->DP |= DP_PLL_FREQ_160MHZ;
  589. else
  590. dp_priv->DP |= DP_PLL_FREQ_270MHZ;
  591. }
  592. }
  593. static void igdng_edp_backlight_on (struct drm_device *dev)
  594. {
  595. struct drm_i915_private *dev_priv = dev->dev_private;
  596. u32 pp;
  597. DRM_DEBUG("\n");
  598. pp = I915_READ(PCH_PP_CONTROL);
  599. pp |= EDP_BLC_ENABLE;
  600. I915_WRITE(PCH_PP_CONTROL, pp);
  601. }
  602. static void igdng_edp_backlight_off (struct drm_device *dev)
  603. {
  604. struct drm_i915_private *dev_priv = dev->dev_private;
  605. u32 pp;
  606. DRM_DEBUG("\n");
  607. pp = I915_READ(PCH_PP_CONTROL);
  608. pp &= ~EDP_BLC_ENABLE;
  609. I915_WRITE(PCH_PP_CONTROL, pp);
  610. }
  611. static void
  612. intel_dp_dpms(struct drm_encoder *encoder, int mode)
  613. {
  614. struct intel_output *intel_output = enc_to_intel_output(encoder);
  615. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  616. struct drm_device *dev = intel_output->base.dev;
  617. struct drm_i915_private *dev_priv = dev->dev_private;
  618. uint32_t dp_reg = I915_READ(dp_priv->output_reg);
  619. if (mode != DRM_MODE_DPMS_ON) {
  620. if (dp_reg & DP_PORT_EN) {
  621. intel_dp_link_down(intel_output, dp_priv->DP);
  622. if (IS_eDP(intel_output))
  623. igdng_edp_backlight_off(dev);
  624. }
  625. } else {
  626. if (!(dp_reg & DP_PORT_EN)) {
  627. intel_dp_link_train(intel_output, dp_priv->DP, dp_priv->link_configuration);
  628. if (IS_eDP(intel_output))
  629. igdng_edp_backlight_on(dev);
  630. }
  631. }
  632. dp_priv->dpms_mode = mode;
  633. }
  634. /*
  635. * Fetch AUX CH registers 0x202 - 0x207 which contain
  636. * link status information
  637. */
  638. static bool
  639. intel_dp_get_link_status(struct intel_output *intel_output,
  640. uint8_t link_status[DP_LINK_STATUS_SIZE])
  641. {
  642. int ret;
  643. ret = intel_dp_aux_native_read(intel_output,
  644. DP_LANE0_1_STATUS,
  645. link_status, DP_LINK_STATUS_SIZE);
  646. if (ret != DP_LINK_STATUS_SIZE)
  647. return false;
  648. return true;
  649. }
  650. static uint8_t
  651. intel_dp_link_status(uint8_t link_status[DP_LINK_STATUS_SIZE],
  652. int r)
  653. {
  654. return link_status[r - DP_LANE0_1_STATUS];
  655. }
  656. static void
  657. intel_dp_save(struct drm_connector *connector)
  658. {
  659. struct intel_output *intel_output = to_intel_output(connector);
  660. struct drm_device *dev = intel_output->base.dev;
  661. struct drm_i915_private *dev_priv = dev->dev_private;
  662. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  663. dp_priv->save_DP = I915_READ(dp_priv->output_reg);
  664. intel_dp_aux_native_read(intel_output, DP_LINK_BW_SET,
  665. dp_priv->save_link_configuration,
  666. sizeof (dp_priv->save_link_configuration));
  667. }
  668. static uint8_t
  669. intel_get_adjust_request_voltage(uint8_t link_status[DP_LINK_STATUS_SIZE],
  670. int lane)
  671. {
  672. int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
  673. int s = ((lane & 1) ?
  674. DP_ADJUST_VOLTAGE_SWING_LANE1_SHIFT :
  675. DP_ADJUST_VOLTAGE_SWING_LANE0_SHIFT);
  676. uint8_t l = intel_dp_link_status(link_status, i);
  677. return ((l >> s) & 3) << DP_TRAIN_VOLTAGE_SWING_SHIFT;
  678. }
  679. static uint8_t
  680. intel_get_adjust_request_pre_emphasis(uint8_t link_status[DP_LINK_STATUS_SIZE],
  681. int lane)
  682. {
  683. int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
  684. int s = ((lane & 1) ?
  685. DP_ADJUST_PRE_EMPHASIS_LANE1_SHIFT :
  686. DP_ADJUST_PRE_EMPHASIS_LANE0_SHIFT);
  687. uint8_t l = intel_dp_link_status(link_status, i);
  688. return ((l >> s) & 3) << DP_TRAIN_PRE_EMPHASIS_SHIFT;
  689. }
  690. #if 0
  691. static char *voltage_names[] = {
  692. "0.4V", "0.6V", "0.8V", "1.2V"
  693. };
  694. static char *pre_emph_names[] = {
  695. "0dB", "3.5dB", "6dB", "9.5dB"
  696. };
  697. static char *link_train_names[] = {
  698. "pattern 1", "pattern 2", "idle", "off"
  699. };
  700. #endif
  701. /*
  702. * These are source-specific values; current Intel hardware supports
  703. * a maximum voltage of 800mV and a maximum pre-emphasis of 6dB
  704. */
  705. #define I830_DP_VOLTAGE_MAX DP_TRAIN_VOLTAGE_SWING_800
  706. static uint8_t
  707. intel_dp_pre_emphasis_max(uint8_t voltage_swing)
  708. {
  709. switch (voltage_swing & DP_TRAIN_VOLTAGE_SWING_MASK) {
  710. case DP_TRAIN_VOLTAGE_SWING_400:
  711. return DP_TRAIN_PRE_EMPHASIS_6;
  712. case DP_TRAIN_VOLTAGE_SWING_600:
  713. return DP_TRAIN_PRE_EMPHASIS_6;
  714. case DP_TRAIN_VOLTAGE_SWING_800:
  715. return DP_TRAIN_PRE_EMPHASIS_3_5;
  716. case DP_TRAIN_VOLTAGE_SWING_1200:
  717. default:
  718. return DP_TRAIN_PRE_EMPHASIS_0;
  719. }
  720. }
  721. static void
  722. intel_get_adjust_train(struct intel_output *intel_output,
  723. uint8_t link_status[DP_LINK_STATUS_SIZE],
  724. int lane_count,
  725. uint8_t train_set[4])
  726. {
  727. uint8_t v = 0;
  728. uint8_t p = 0;
  729. int lane;
  730. for (lane = 0; lane < lane_count; lane++) {
  731. uint8_t this_v = intel_get_adjust_request_voltage(link_status, lane);
  732. uint8_t this_p = intel_get_adjust_request_pre_emphasis(link_status, lane);
  733. if (this_v > v)
  734. v = this_v;
  735. if (this_p > p)
  736. p = this_p;
  737. }
  738. if (v >= I830_DP_VOLTAGE_MAX)
  739. v = I830_DP_VOLTAGE_MAX | DP_TRAIN_MAX_SWING_REACHED;
  740. if (p >= intel_dp_pre_emphasis_max(v))
  741. p = intel_dp_pre_emphasis_max(v) | DP_TRAIN_MAX_PRE_EMPHASIS_REACHED;
  742. for (lane = 0; lane < 4; lane++)
  743. train_set[lane] = v | p;
  744. }
  745. static uint32_t
  746. intel_dp_signal_levels(uint8_t train_set, int lane_count)
  747. {
  748. uint32_t signal_levels = 0;
  749. switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
  750. case DP_TRAIN_VOLTAGE_SWING_400:
  751. default:
  752. signal_levels |= DP_VOLTAGE_0_4;
  753. break;
  754. case DP_TRAIN_VOLTAGE_SWING_600:
  755. signal_levels |= DP_VOLTAGE_0_6;
  756. break;
  757. case DP_TRAIN_VOLTAGE_SWING_800:
  758. signal_levels |= DP_VOLTAGE_0_8;
  759. break;
  760. case DP_TRAIN_VOLTAGE_SWING_1200:
  761. signal_levels |= DP_VOLTAGE_1_2;
  762. break;
  763. }
  764. switch (train_set & DP_TRAIN_PRE_EMPHASIS_MASK) {
  765. case DP_TRAIN_PRE_EMPHASIS_0:
  766. default:
  767. signal_levels |= DP_PRE_EMPHASIS_0;
  768. break;
  769. case DP_TRAIN_PRE_EMPHASIS_3_5:
  770. signal_levels |= DP_PRE_EMPHASIS_3_5;
  771. break;
  772. case DP_TRAIN_PRE_EMPHASIS_6:
  773. signal_levels |= DP_PRE_EMPHASIS_6;
  774. break;
  775. case DP_TRAIN_PRE_EMPHASIS_9_5:
  776. signal_levels |= DP_PRE_EMPHASIS_9_5;
  777. break;
  778. }
  779. return signal_levels;
  780. }
  781. static uint8_t
  782. intel_get_lane_status(uint8_t link_status[DP_LINK_STATUS_SIZE],
  783. int lane)
  784. {
  785. int i = DP_LANE0_1_STATUS + (lane >> 1);
  786. int s = (lane & 1) * 4;
  787. uint8_t l = intel_dp_link_status(link_status, i);
  788. return (l >> s) & 0xf;
  789. }
  790. /* Check for clock recovery is done on all channels */
  791. static bool
  792. intel_clock_recovery_ok(uint8_t link_status[DP_LINK_STATUS_SIZE], int lane_count)
  793. {
  794. int lane;
  795. uint8_t lane_status;
  796. for (lane = 0; lane < lane_count; lane++) {
  797. lane_status = intel_get_lane_status(link_status, lane);
  798. if ((lane_status & DP_LANE_CR_DONE) == 0)
  799. return false;
  800. }
  801. return true;
  802. }
  803. /* Check to see if channel eq is done on all channels */
  804. #define CHANNEL_EQ_BITS (DP_LANE_CR_DONE|\
  805. DP_LANE_CHANNEL_EQ_DONE|\
  806. DP_LANE_SYMBOL_LOCKED)
  807. static bool
  808. intel_channel_eq_ok(uint8_t link_status[DP_LINK_STATUS_SIZE], int lane_count)
  809. {
  810. uint8_t lane_align;
  811. uint8_t lane_status;
  812. int lane;
  813. lane_align = intel_dp_link_status(link_status,
  814. DP_LANE_ALIGN_STATUS_UPDATED);
  815. if ((lane_align & DP_INTERLANE_ALIGN_DONE) == 0)
  816. return false;
  817. for (lane = 0; lane < lane_count; lane++) {
  818. lane_status = intel_get_lane_status(link_status, lane);
  819. if ((lane_status & CHANNEL_EQ_BITS) != CHANNEL_EQ_BITS)
  820. return false;
  821. }
  822. return true;
  823. }
  824. static bool
  825. intel_dp_set_link_train(struct intel_output *intel_output,
  826. uint32_t dp_reg_value,
  827. uint8_t dp_train_pat,
  828. uint8_t train_set[4],
  829. bool first)
  830. {
  831. struct drm_device *dev = intel_output->base.dev;
  832. struct drm_i915_private *dev_priv = dev->dev_private;
  833. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  834. int ret;
  835. I915_WRITE(dp_priv->output_reg, dp_reg_value);
  836. POSTING_READ(dp_priv->output_reg);
  837. if (first)
  838. intel_wait_for_vblank(dev);
  839. intel_dp_aux_native_write_1(intel_output,
  840. DP_TRAINING_PATTERN_SET,
  841. dp_train_pat);
  842. ret = intel_dp_aux_native_write(intel_output,
  843. DP_TRAINING_LANE0_SET, train_set, 4);
  844. if (ret != 4)
  845. return false;
  846. return true;
  847. }
  848. static void
  849. intel_dp_link_train(struct intel_output *intel_output, uint32_t DP,
  850. uint8_t link_configuration[DP_LINK_CONFIGURATION_SIZE])
  851. {
  852. struct drm_device *dev = intel_output->base.dev;
  853. struct drm_i915_private *dev_priv = dev->dev_private;
  854. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  855. uint8_t train_set[4];
  856. uint8_t link_status[DP_LINK_STATUS_SIZE];
  857. int i;
  858. uint8_t voltage;
  859. bool clock_recovery = false;
  860. bool channel_eq = false;
  861. bool first = true;
  862. int tries;
  863. /* Write the link configuration data */
  864. intel_dp_aux_native_write(intel_output, 0x100,
  865. link_configuration, DP_LINK_CONFIGURATION_SIZE);
  866. DP |= DP_PORT_EN;
  867. DP &= ~DP_LINK_TRAIN_MASK;
  868. memset(train_set, 0, 4);
  869. voltage = 0xff;
  870. tries = 0;
  871. clock_recovery = false;
  872. for (;;) {
  873. /* Use train_set[0] to set the voltage and pre emphasis values */
  874. uint32_t signal_levels = intel_dp_signal_levels(train_set[0], dp_priv->lane_count);
  875. DP = (DP & ~(DP_VOLTAGE_MASK|DP_PRE_EMPHASIS_MASK)) | signal_levels;
  876. if (!intel_dp_set_link_train(intel_output, DP | DP_LINK_TRAIN_PAT_1,
  877. DP_TRAINING_PATTERN_1, train_set, first))
  878. break;
  879. first = false;
  880. /* Set training pattern 1 */
  881. udelay(100);
  882. if (!intel_dp_get_link_status(intel_output, link_status))
  883. break;
  884. if (intel_clock_recovery_ok(link_status, dp_priv->lane_count)) {
  885. clock_recovery = true;
  886. break;
  887. }
  888. /* Check to see if we've tried the max voltage */
  889. for (i = 0; i < dp_priv->lane_count; i++)
  890. if ((train_set[i] & DP_TRAIN_MAX_SWING_REACHED) == 0)
  891. break;
  892. if (i == dp_priv->lane_count)
  893. break;
  894. /* Check to see if we've tried the same voltage 5 times */
  895. if ((train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK) == voltage) {
  896. ++tries;
  897. if (tries == 5)
  898. break;
  899. } else
  900. tries = 0;
  901. voltage = train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK;
  902. /* Compute new train_set as requested by target */
  903. intel_get_adjust_train(intel_output, link_status, dp_priv->lane_count, train_set);
  904. }
  905. /* channel equalization */
  906. tries = 0;
  907. channel_eq = false;
  908. for (;;) {
  909. /* Use train_set[0] to set the voltage and pre emphasis values */
  910. uint32_t signal_levels = intel_dp_signal_levels(train_set[0], dp_priv->lane_count);
  911. DP = (DP & ~(DP_VOLTAGE_MASK|DP_PRE_EMPHASIS_MASK)) | signal_levels;
  912. /* channel eq pattern */
  913. if (!intel_dp_set_link_train(intel_output, DP | DP_LINK_TRAIN_PAT_2,
  914. DP_TRAINING_PATTERN_2, train_set,
  915. false))
  916. break;
  917. udelay(400);
  918. if (!intel_dp_get_link_status(intel_output, link_status))
  919. break;
  920. if (intel_channel_eq_ok(link_status, dp_priv->lane_count)) {
  921. channel_eq = true;
  922. break;
  923. }
  924. /* Try 5 times */
  925. if (tries > 5)
  926. break;
  927. /* Compute new train_set as requested by target */
  928. intel_get_adjust_train(intel_output, link_status, dp_priv->lane_count, train_set);
  929. ++tries;
  930. }
  931. I915_WRITE(dp_priv->output_reg, DP | DP_LINK_TRAIN_OFF);
  932. POSTING_READ(dp_priv->output_reg);
  933. intel_dp_aux_native_write_1(intel_output,
  934. DP_TRAINING_PATTERN_SET, DP_TRAINING_PATTERN_DISABLE);
  935. }
  936. static void
  937. intel_dp_link_down(struct intel_output *intel_output, uint32_t DP)
  938. {
  939. struct drm_device *dev = intel_output->base.dev;
  940. struct drm_i915_private *dev_priv = dev->dev_private;
  941. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  942. DRM_DEBUG("\n");
  943. if (IS_eDP(intel_output)) {
  944. DP &= ~DP_PLL_ENABLE;
  945. I915_WRITE(dp_priv->output_reg, DP);
  946. POSTING_READ(dp_priv->output_reg);
  947. udelay(100);
  948. }
  949. DP &= ~DP_LINK_TRAIN_MASK;
  950. I915_WRITE(dp_priv->output_reg, DP | DP_LINK_TRAIN_PAT_IDLE);
  951. POSTING_READ(dp_priv->output_reg);
  952. udelay(17000);
  953. if (IS_eDP(intel_output))
  954. DP |= DP_LINK_TRAIN_OFF;
  955. I915_WRITE(dp_priv->output_reg, DP & ~DP_PORT_EN);
  956. POSTING_READ(dp_priv->output_reg);
  957. }
  958. static void
  959. intel_dp_restore(struct drm_connector *connector)
  960. {
  961. struct intel_output *intel_output = to_intel_output(connector);
  962. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  963. if (dp_priv->save_DP & DP_PORT_EN)
  964. intel_dp_link_train(intel_output, dp_priv->save_DP, dp_priv->save_link_configuration);
  965. else
  966. intel_dp_link_down(intel_output, dp_priv->save_DP);
  967. }
  968. /*
  969. * According to DP spec
  970. * 5.1.2:
  971. * 1. Read DPCD
  972. * 2. Configure link according to Receiver Capabilities
  973. * 3. Use Link Training from 2.5.3.3 and 3.5.1.3
  974. * 4. Check link status on receipt of hot-plug interrupt
  975. */
  976. static void
  977. intel_dp_check_link_status(struct intel_output *intel_output)
  978. {
  979. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  980. uint8_t link_status[DP_LINK_STATUS_SIZE];
  981. if (!intel_output->enc.crtc)
  982. return;
  983. if (!intel_dp_get_link_status(intel_output, link_status)) {
  984. intel_dp_link_down(intel_output, dp_priv->DP);
  985. return;
  986. }
  987. if (!intel_channel_eq_ok(link_status, dp_priv->lane_count))
  988. intel_dp_link_train(intel_output, dp_priv->DP, dp_priv->link_configuration);
  989. }
  990. static enum drm_connector_status
  991. igdng_dp_detect(struct drm_connector *connector)
  992. {
  993. struct intel_output *intel_output = to_intel_output(connector);
  994. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  995. enum drm_connector_status status;
  996. status = connector_status_disconnected;
  997. if (intel_dp_aux_native_read(intel_output,
  998. 0x000, dp_priv->dpcd,
  999. sizeof (dp_priv->dpcd)) == sizeof (dp_priv->dpcd))
  1000. {
  1001. if (dp_priv->dpcd[0] != 0)
  1002. status = connector_status_connected;
  1003. }
  1004. return status;
  1005. }
  1006. /**
  1007. * Uses CRT_HOTPLUG_EN and CRT_HOTPLUG_STAT to detect DP connection.
  1008. *
  1009. * \return true if DP port is connected.
  1010. * \return false if DP port is disconnected.
  1011. */
  1012. static enum drm_connector_status
  1013. intel_dp_detect(struct drm_connector *connector)
  1014. {
  1015. struct intel_output *intel_output = to_intel_output(connector);
  1016. struct drm_device *dev = intel_output->base.dev;
  1017. struct drm_i915_private *dev_priv = dev->dev_private;
  1018. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  1019. uint32_t temp, bit;
  1020. enum drm_connector_status status;
  1021. dp_priv->has_audio = false;
  1022. if (IS_IGDNG(dev))
  1023. return igdng_dp_detect(connector);
  1024. temp = I915_READ(PORT_HOTPLUG_EN);
  1025. I915_WRITE(PORT_HOTPLUG_EN,
  1026. temp |
  1027. DPB_HOTPLUG_INT_EN |
  1028. DPC_HOTPLUG_INT_EN |
  1029. DPD_HOTPLUG_INT_EN);
  1030. POSTING_READ(PORT_HOTPLUG_EN);
  1031. switch (dp_priv->output_reg) {
  1032. case DP_B:
  1033. bit = DPB_HOTPLUG_INT_STATUS;
  1034. break;
  1035. case DP_C:
  1036. bit = DPC_HOTPLUG_INT_STATUS;
  1037. break;
  1038. case DP_D:
  1039. bit = DPD_HOTPLUG_INT_STATUS;
  1040. break;
  1041. default:
  1042. return connector_status_unknown;
  1043. }
  1044. temp = I915_READ(PORT_HOTPLUG_STAT);
  1045. if ((temp & bit) == 0)
  1046. return connector_status_disconnected;
  1047. status = connector_status_disconnected;
  1048. if (intel_dp_aux_native_read(intel_output,
  1049. 0x000, dp_priv->dpcd,
  1050. sizeof (dp_priv->dpcd)) == sizeof (dp_priv->dpcd))
  1051. {
  1052. if (dp_priv->dpcd[0] != 0)
  1053. status = connector_status_connected;
  1054. }
  1055. return status;
  1056. }
  1057. static int intel_dp_get_modes(struct drm_connector *connector)
  1058. {
  1059. struct intel_output *intel_output = to_intel_output(connector);
  1060. struct drm_device *dev = intel_output->base.dev;
  1061. struct drm_i915_private *dev_priv = dev->dev_private;
  1062. int ret;
  1063. /* We should parse the EDID data and find out if it has an audio sink
  1064. */
  1065. ret = intel_ddc_get_modes(intel_output);
  1066. if (ret)
  1067. return ret;
  1068. /* if eDP has no EDID, try to use fixed panel mode from VBT */
  1069. if (IS_eDP(intel_output)) {
  1070. if (dev_priv->panel_fixed_mode != NULL) {
  1071. struct drm_display_mode *mode;
  1072. mode = drm_mode_duplicate(dev, dev_priv->panel_fixed_mode);
  1073. drm_mode_probed_add(connector, mode);
  1074. return 1;
  1075. }
  1076. }
  1077. return 0;
  1078. }
  1079. static void
  1080. intel_dp_destroy (struct drm_connector *connector)
  1081. {
  1082. struct intel_output *intel_output = to_intel_output(connector);
  1083. if (intel_output->i2c_bus)
  1084. intel_i2c_destroy(intel_output->i2c_bus);
  1085. drm_sysfs_connector_remove(connector);
  1086. drm_connector_cleanup(connector);
  1087. kfree(intel_output);
  1088. }
  1089. static const struct drm_encoder_helper_funcs intel_dp_helper_funcs = {
  1090. .dpms = intel_dp_dpms,
  1091. .mode_fixup = intel_dp_mode_fixup,
  1092. .prepare = intel_encoder_prepare,
  1093. .mode_set = intel_dp_mode_set,
  1094. .commit = intel_encoder_commit,
  1095. };
  1096. static const struct drm_connector_funcs intel_dp_connector_funcs = {
  1097. .dpms = drm_helper_connector_dpms,
  1098. .save = intel_dp_save,
  1099. .restore = intel_dp_restore,
  1100. .detect = intel_dp_detect,
  1101. .fill_modes = drm_helper_probe_single_connector_modes,
  1102. .destroy = intel_dp_destroy,
  1103. };
  1104. static const struct drm_connector_helper_funcs intel_dp_connector_helper_funcs = {
  1105. .get_modes = intel_dp_get_modes,
  1106. .mode_valid = intel_dp_mode_valid,
  1107. .best_encoder = intel_best_encoder,
  1108. };
  1109. static void intel_dp_enc_destroy(struct drm_encoder *encoder)
  1110. {
  1111. drm_encoder_cleanup(encoder);
  1112. }
  1113. static const struct drm_encoder_funcs intel_dp_enc_funcs = {
  1114. .destroy = intel_dp_enc_destroy,
  1115. };
  1116. void
  1117. intel_dp_hot_plug(struct intel_output *intel_output)
  1118. {
  1119. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  1120. if (dp_priv->dpms_mode == DRM_MODE_DPMS_ON)
  1121. intel_dp_check_link_status(intel_output);
  1122. }
  1123. void
  1124. intel_dp_init(struct drm_device *dev, int output_reg)
  1125. {
  1126. struct drm_i915_private *dev_priv = dev->dev_private;
  1127. struct drm_connector *connector;
  1128. struct intel_output *intel_output;
  1129. struct intel_dp_priv *dp_priv;
  1130. const char *name = NULL;
  1131. intel_output = kcalloc(sizeof(struct intel_output) +
  1132. sizeof(struct intel_dp_priv), 1, GFP_KERNEL);
  1133. if (!intel_output)
  1134. return;
  1135. dp_priv = (struct intel_dp_priv *)(intel_output + 1);
  1136. connector = &intel_output->base;
  1137. drm_connector_init(dev, connector, &intel_dp_connector_funcs,
  1138. DRM_MODE_CONNECTOR_DisplayPort);
  1139. drm_connector_helper_add(connector, &intel_dp_connector_helper_funcs);
  1140. if (output_reg == DP_A)
  1141. intel_output->type = INTEL_OUTPUT_EDP;
  1142. else
  1143. intel_output->type = INTEL_OUTPUT_DISPLAYPORT;
  1144. if (output_reg == DP_B)
  1145. intel_output->clone_mask = (1 << INTEL_DP_B_CLONE_BIT);
  1146. else if (output_reg == DP_C)
  1147. intel_output->clone_mask = (1 << INTEL_DP_C_CLONE_BIT);
  1148. else if (output_reg == DP_D)
  1149. intel_output->clone_mask = (1 << INTEL_DP_D_CLONE_BIT);
  1150. if (IS_eDP(intel_output)) {
  1151. intel_output->crtc_mask = (1 << 1);
  1152. intel_output->clone_mask = (1 << INTEL_EDP_CLONE_BIT);
  1153. } else
  1154. intel_output->crtc_mask = (1 << 0) | (1 << 1);
  1155. connector->interlace_allowed = true;
  1156. connector->doublescan_allowed = 0;
  1157. dp_priv->intel_output = intel_output;
  1158. dp_priv->output_reg = output_reg;
  1159. dp_priv->has_audio = false;
  1160. dp_priv->dpms_mode = DRM_MODE_DPMS_ON;
  1161. intel_output->dev_priv = dp_priv;
  1162. drm_encoder_init(dev, &intel_output->enc, &intel_dp_enc_funcs,
  1163. DRM_MODE_ENCODER_TMDS);
  1164. drm_encoder_helper_add(&intel_output->enc, &intel_dp_helper_funcs);
  1165. drm_mode_connector_attach_encoder(&intel_output->base,
  1166. &intel_output->enc);
  1167. drm_sysfs_connector_add(connector);
  1168. /* Set up the DDC bus. */
  1169. switch (output_reg) {
  1170. case DP_A:
  1171. name = "DPDDC-A";
  1172. break;
  1173. case DP_B:
  1174. case PCH_DP_B:
  1175. name = "DPDDC-B";
  1176. break;
  1177. case DP_C:
  1178. case PCH_DP_C:
  1179. name = "DPDDC-C";
  1180. break;
  1181. case DP_D:
  1182. case PCH_DP_D:
  1183. name = "DPDDC-D";
  1184. break;
  1185. }
  1186. intel_dp_i2c_init(intel_output, name);
  1187. intel_output->ddc_bus = &dp_priv->adapter;
  1188. intel_output->hot_plug = intel_dp_hot_plug;
  1189. if (output_reg == DP_A) {
  1190. /* initialize panel mode from VBT if available for eDP */
  1191. if (dev_priv->lfp_lvds_vbt_mode) {
  1192. dev_priv->panel_fixed_mode =
  1193. drm_mode_duplicate(dev, dev_priv->lfp_lvds_vbt_mode);
  1194. if (dev_priv->panel_fixed_mode) {
  1195. dev_priv->panel_fixed_mode->type |=
  1196. DRM_MODE_TYPE_PREFERRED;
  1197. }
  1198. }
  1199. }
  1200. /* For G4X desktop chip, PEG_BAND_GAP_DATA 3:0 must first be written
  1201. * 0xd. Failure to do so will result in spurious interrupts being
  1202. * generated on the port when a cable is not attached.
  1203. */
  1204. if (IS_G4X(dev) && !IS_GM45(dev)) {
  1205. u32 temp = I915_READ(PEG_BAND_GAP_DATA);
  1206. I915_WRITE(PEG_BAND_GAP_DATA, (temp & ~0xf) | 0xd);
  1207. }
  1208. }