intel_dp.c 36 KB

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