intel_dp.c 37 KB

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