intel_dp.c 37 KB

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