intel_dp.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115
  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. struct intel_dp_priv {
  40. uint32_t output_reg;
  41. uint32_t DP;
  42. uint8_t link_configuration[DP_LINK_CONFIGURATION_SIZE];
  43. uint32_t save_DP;
  44. uint8_t save_link_configuration[DP_LINK_CONFIGURATION_SIZE];
  45. bool has_audio;
  46. int dpms_mode;
  47. uint8_t link_bw;
  48. uint8_t lane_count;
  49. uint8_t dpcd[4];
  50. struct intel_output *intel_output;
  51. struct i2c_adapter adapter;
  52. struct i2c_algo_dp_aux_data algo;
  53. };
  54. static void
  55. intel_dp_link_train(struct intel_output *intel_output, uint32_t DP,
  56. uint8_t link_configuration[DP_LINK_CONFIGURATION_SIZE]);
  57. static void
  58. intel_dp_link_down(struct intel_output *intel_output, uint32_t DP);
  59. static int
  60. intel_dp_max_lane_count(struct intel_output *intel_output)
  61. {
  62. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  63. int max_lane_count = 4;
  64. if (dp_priv->dpcd[0] >= 0x11) {
  65. max_lane_count = dp_priv->dpcd[2] & 0x1f;
  66. switch (max_lane_count) {
  67. case 1: case 2: case 4:
  68. break;
  69. default:
  70. max_lane_count = 4;
  71. }
  72. }
  73. return max_lane_count;
  74. }
  75. static int
  76. intel_dp_max_link_bw(struct intel_output *intel_output)
  77. {
  78. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  79. int max_link_bw = dp_priv->dpcd[1];
  80. switch (max_link_bw) {
  81. case DP_LINK_BW_1_62:
  82. case DP_LINK_BW_2_7:
  83. break;
  84. default:
  85. max_link_bw = DP_LINK_BW_1_62;
  86. break;
  87. }
  88. return max_link_bw;
  89. }
  90. static int
  91. intel_dp_link_clock(uint8_t link_bw)
  92. {
  93. if (link_bw == DP_LINK_BW_2_7)
  94. return 270000;
  95. else
  96. return 162000;
  97. }
  98. /* I think this is a fiction */
  99. static int
  100. intel_dp_link_required(int pixel_clock)
  101. {
  102. return pixel_clock * 3;
  103. }
  104. static int
  105. intel_dp_mode_valid(struct drm_connector *connector,
  106. struct drm_display_mode *mode)
  107. {
  108. struct intel_output *intel_output = to_intel_output(connector);
  109. int max_link_clock = intel_dp_link_clock(intel_dp_max_link_bw(intel_output));
  110. int max_lanes = intel_dp_max_lane_count(intel_output);
  111. if (intel_dp_link_required(mode->clock) > max_link_clock * max_lanes)
  112. return MODE_CLOCK_HIGH;
  113. if (mode->clock < 10000)
  114. return MODE_CLOCK_LOW;
  115. return MODE_OK;
  116. }
  117. static uint32_t
  118. pack_aux(uint8_t *src, int src_bytes)
  119. {
  120. int i;
  121. uint32_t v = 0;
  122. if (src_bytes > 4)
  123. src_bytes = 4;
  124. for (i = 0; i < src_bytes; i++)
  125. v |= ((uint32_t) src[i]) << ((3-i) * 8);
  126. return v;
  127. }
  128. static void
  129. unpack_aux(uint32_t src, uint8_t *dst, int dst_bytes)
  130. {
  131. int i;
  132. if (dst_bytes > 4)
  133. dst_bytes = 4;
  134. for (i = 0; i < dst_bytes; i++)
  135. dst[i] = src >> ((3-i) * 8);
  136. }
  137. static int
  138. intel_dp_aux_ch(struct intel_output *intel_output,
  139. uint8_t *send, int send_bytes,
  140. uint8_t *recv, int recv_size)
  141. {
  142. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  143. uint32_t output_reg = dp_priv->output_reg;
  144. struct drm_device *dev = intel_output->base.dev;
  145. struct drm_i915_private *dev_priv = dev->dev_private;
  146. uint32_t ch_ctl = output_reg + 0x10;
  147. uint32_t ch_data = ch_ctl + 4;
  148. int i;
  149. int recv_bytes;
  150. uint32_t ctl;
  151. uint32_t status;
  152. /* Load the send data into the aux channel data registers */
  153. for (i = 0; i < send_bytes; i += 4) {
  154. uint32_t d = pack_aux(send + i, send_bytes - i);;
  155. I915_WRITE(ch_data + i, d);
  156. }
  157. /* The clock divider is based off the hrawclk,
  158. * and would like to run at 2MHz. The 133 below assumes
  159. * a 266MHz hrawclk; need to figure out how we're supposed
  160. * to know what hrawclk is...
  161. */
  162. ctl = (DP_AUX_CH_CTL_SEND_BUSY |
  163. DP_AUX_CH_CTL_TIME_OUT_1600us |
  164. (send_bytes << DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT) |
  165. (5 << DP_AUX_CH_CTL_PRECHARGE_2US_SHIFT) |
  166. (133 << DP_AUX_CH_CTL_BIT_CLOCK_2X_SHIFT) |
  167. DP_AUX_CH_CTL_TIME_OUT_ERROR |
  168. DP_AUX_CH_CTL_RECEIVE_ERROR);
  169. /* Send the command and wait for it to complete */
  170. I915_WRITE(ch_ctl, ctl);
  171. (void) I915_READ(ch_ctl);
  172. for (;;) {
  173. udelay(100);
  174. status = I915_READ(ch_ctl);
  175. if ((status & DP_AUX_CH_CTL_SEND_BUSY) == 0)
  176. break;
  177. }
  178. /* Clear done status and any errors */
  179. I915_WRITE(ch_ctl, (ctl |
  180. DP_AUX_CH_CTL_DONE |
  181. DP_AUX_CH_CTL_TIME_OUT_ERROR |
  182. DP_AUX_CH_CTL_RECEIVE_ERROR));
  183. (void) I915_READ(ch_ctl);
  184. if ((status & DP_AUX_CH_CTL_DONE) == 0) {
  185. printk(KERN_ERR "dp_aux_ch not done status 0x%08x\n", status);
  186. return -EBUSY;
  187. }
  188. /* Check for timeout or receive error.
  189. * Timeouts occur when the sink is not connected
  190. */
  191. if (status & DP_AUX_CH_CTL_RECEIVE_ERROR) {
  192. printk(KERN_ERR "dp_aux_ch receive error status 0x%08x\n", status);
  193. return -EIO;
  194. }
  195. if (status & DP_AUX_CH_CTL_TIME_OUT_ERROR) {
  196. printk(KERN_ERR "dp_aux_ch timeout status 0x%08x\n", status);
  197. return -ETIMEDOUT;
  198. }
  199. /* Unload any bytes sent back from the other side */
  200. recv_bytes = ((status & DP_AUX_CH_CTL_MESSAGE_SIZE_MASK) >>
  201. DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT);
  202. if (recv_bytes > recv_size)
  203. recv_bytes = recv_size;
  204. for (i = 0; i < recv_bytes; i += 4) {
  205. uint32_t d = I915_READ(ch_data + i);
  206. unpack_aux(d, recv + i, recv_bytes - i);
  207. }
  208. return recv_bytes;
  209. }
  210. /* Write data to the aux channel in native mode */
  211. static int
  212. intel_dp_aux_native_write(struct intel_output *intel_output,
  213. uint16_t address, uint8_t *send, int send_bytes)
  214. {
  215. int ret;
  216. uint8_t msg[20];
  217. int msg_bytes;
  218. uint8_t ack;
  219. if (send_bytes > 16)
  220. return -1;
  221. msg[0] = AUX_NATIVE_WRITE << 4;
  222. msg[1] = address >> 8;
  223. msg[2] = address;
  224. msg[3] = send_bytes - 1;
  225. memcpy(&msg[4], send, send_bytes);
  226. msg_bytes = send_bytes + 4;
  227. for (;;) {
  228. ret = intel_dp_aux_ch(intel_output, msg, msg_bytes, &ack, 1);
  229. if (ret < 0)
  230. return ret;
  231. if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_ACK)
  232. break;
  233. else if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_DEFER)
  234. udelay(100);
  235. else
  236. return -EIO;
  237. }
  238. return send_bytes;
  239. }
  240. /* Write a single byte to the aux channel in native mode */
  241. static int
  242. intel_dp_aux_native_write_1(struct intel_output *intel_output,
  243. uint16_t address, uint8_t byte)
  244. {
  245. return intel_dp_aux_native_write(intel_output, address, &byte, 1);
  246. }
  247. /* read bytes from a native aux channel */
  248. static int
  249. intel_dp_aux_native_read(struct intel_output *intel_output,
  250. uint16_t address, uint8_t *recv, int recv_bytes)
  251. {
  252. uint8_t msg[4];
  253. int msg_bytes;
  254. uint8_t reply[20];
  255. int reply_bytes;
  256. uint8_t ack;
  257. int ret;
  258. msg[0] = AUX_NATIVE_READ << 4;
  259. msg[1] = address >> 8;
  260. msg[2] = address & 0xff;
  261. msg[3] = recv_bytes - 1;
  262. msg_bytes = 4;
  263. reply_bytes = recv_bytes + 1;
  264. for (;;) {
  265. ret = intel_dp_aux_ch(intel_output, msg, msg_bytes,
  266. reply, reply_bytes);
  267. if (ret == 0)
  268. return -EPROTO;
  269. if (ret < 0)
  270. return ret;
  271. ack = reply[0];
  272. if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_ACK) {
  273. memcpy(recv, reply + 1, ret - 1);
  274. return ret - 1;
  275. }
  276. else if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_DEFER)
  277. udelay(100);
  278. else
  279. return -EIO;
  280. }
  281. }
  282. static int
  283. intel_dp_i2c_aux_ch(struct i2c_adapter *adapter,
  284. uint8_t *send, int send_bytes,
  285. uint8_t *recv, int recv_bytes)
  286. {
  287. struct intel_dp_priv *dp_priv = container_of(adapter,
  288. struct intel_dp_priv,
  289. adapter);
  290. struct intel_output *intel_output = dp_priv->intel_output;
  291. return intel_dp_aux_ch(intel_output,
  292. send, send_bytes, recv, recv_bytes);
  293. }
  294. static int
  295. intel_dp_i2c_init(struct intel_output *intel_output, const char *name)
  296. {
  297. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  298. DRM_ERROR("i2c_init %s\n", name);
  299. dp_priv->algo.running = false;
  300. dp_priv->algo.address = 0;
  301. dp_priv->algo.aux_ch = intel_dp_i2c_aux_ch;
  302. memset(&dp_priv->adapter, '\0', sizeof (dp_priv->adapter));
  303. dp_priv->adapter.owner = THIS_MODULE;
  304. dp_priv->adapter.class = I2C_CLASS_DDC;
  305. strncpy (dp_priv->adapter.name, name, sizeof dp_priv->adapter.name - 1);
  306. dp_priv->adapter.name[sizeof dp_priv->adapter.name - 1] = '\0';
  307. dp_priv->adapter.algo_data = &dp_priv->algo;
  308. dp_priv->adapter.dev.parent = &intel_output->base.kdev;
  309. return i2c_dp_aux_add_bus(&dp_priv->adapter);
  310. }
  311. static bool
  312. intel_dp_mode_fixup(struct drm_encoder *encoder, struct drm_display_mode *mode,
  313. struct drm_display_mode *adjusted_mode)
  314. {
  315. struct intel_output *intel_output = enc_to_intel_output(encoder);
  316. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  317. int lane_count, clock;
  318. int max_lane_count = intel_dp_max_lane_count(intel_output);
  319. int max_clock = intel_dp_max_link_bw(intel_output) == DP_LINK_BW_2_7 ? 1 : 0;
  320. static int bws[2] = { DP_LINK_BW_1_62, DP_LINK_BW_2_7 };
  321. for (lane_count = 1; lane_count <= max_lane_count; lane_count <<= 1) {
  322. for (clock = 0; clock <= max_clock; clock++) {
  323. int link_avail = intel_dp_link_clock(bws[clock]) * lane_count;
  324. if (intel_dp_link_required(mode->clock) <= link_avail) {
  325. dp_priv->link_bw = bws[clock];
  326. dp_priv->lane_count = lane_count;
  327. adjusted_mode->clock = intel_dp_link_clock(dp_priv->link_bw);
  328. printk(KERN_ERR "link bw %02x lane count %d clock %d\n",
  329. dp_priv->link_bw, dp_priv->lane_count,
  330. adjusted_mode->clock);
  331. return true;
  332. }
  333. }
  334. }
  335. return false;
  336. }
  337. struct intel_dp_m_n {
  338. uint32_t tu;
  339. uint32_t gmch_m;
  340. uint32_t gmch_n;
  341. uint32_t link_m;
  342. uint32_t link_n;
  343. };
  344. static void
  345. intel_reduce_ratio(uint32_t *num, uint32_t *den)
  346. {
  347. while (*num > 0xffffff || *den > 0xffffff) {
  348. *num >>= 1;
  349. *den >>= 1;
  350. }
  351. }
  352. static void
  353. intel_dp_compute_m_n(int bytes_per_pixel,
  354. int nlanes,
  355. int pixel_clock,
  356. int link_clock,
  357. struct intel_dp_m_n *m_n)
  358. {
  359. m_n->tu = 64;
  360. m_n->gmch_m = pixel_clock * bytes_per_pixel;
  361. m_n->gmch_n = link_clock * nlanes;
  362. intel_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
  363. m_n->link_m = pixel_clock;
  364. m_n->link_n = link_clock;
  365. intel_reduce_ratio(&m_n->link_m, &m_n->link_n);
  366. }
  367. void
  368. intel_dp_set_m_n(struct drm_crtc *crtc, struct drm_display_mode *mode,
  369. struct drm_display_mode *adjusted_mode)
  370. {
  371. struct drm_device *dev = crtc->dev;
  372. struct drm_mode_config *mode_config = &dev->mode_config;
  373. struct drm_connector *connector;
  374. struct drm_i915_private *dev_priv = dev->dev_private;
  375. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  376. int lane_count = 4;
  377. struct intel_dp_m_n m_n;
  378. /*
  379. * Find the lane count in the intel_output private
  380. */
  381. list_for_each_entry(connector, &mode_config->connector_list, head) {
  382. struct intel_output *intel_output = to_intel_output(connector);
  383. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  384. if (!connector->encoder || connector->encoder->crtc != crtc)
  385. continue;
  386. if (intel_output->type == INTEL_OUTPUT_DISPLAYPORT) {
  387. lane_count = dp_priv->lane_count;
  388. break;
  389. }
  390. }
  391. /*
  392. * Compute the GMCH and Link ratios. The '3' here is
  393. * the number of bytes_per_pixel post-LUT, which we always
  394. * set up for 8-bits of R/G/B, or 3 bytes total.
  395. */
  396. intel_dp_compute_m_n(3, lane_count,
  397. mode->clock, adjusted_mode->clock, &m_n);
  398. if (intel_crtc->pipe == 0) {
  399. I915_WRITE(PIPEA_GMCH_DATA_M,
  400. ((m_n.tu - 1) << PIPE_GMCH_DATA_M_TU_SIZE_SHIFT) |
  401. m_n.gmch_m);
  402. I915_WRITE(PIPEA_GMCH_DATA_N,
  403. m_n.gmch_n);
  404. I915_WRITE(PIPEA_DP_LINK_M, m_n.link_m);
  405. I915_WRITE(PIPEA_DP_LINK_N, m_n.link_n);
  406. } else {
  407. I915_WRITE(PIPEB_GMCH_DATA_M,
  408. ((m_n.tu - 1) << PIPE_GMCH_DATA_M_TU_SIZE_SHIFT) |
  409. m_n.gmch_m);
  410. I915_WRITE(PIPEB_GMCH_DATA_N,
  411. m_n.gmch_n);
  412. I915_WRITE(PIPEB_DP_LINK_M, m_n.link_m);
  413. I915_WRITE(PIPEB_DP_LINK_N, m_n.link_n);
  414. }
  415. }
  416. static void
  417. intel_dp_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
  418. struct drm_display_mode *adjusted_mode)
  419. {
  420. struct intel_output *intel_output = enc_to_intel_output(encoder);
  421. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  422. struct drm_crtc *crtc = intel_output->enc.crtc;
  423. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  424. dp_priv->DP = (DP_LINK_TRAIN_OFF |
  425. DP_VOLTAGE_0_4 |
  426. DP_PRE_EMPHASIS_0 |
  427. DP_SYNC_VS_HIGH |
  428. DP_SYNC_HS_HIGH);
  429. switch (dp_priv->lane_count) {
  430. case 1:
  431. dp_priv->DP |= DP_PORT_WIDTH_1;
  432. break;
  433. case 2:
  434. dp_priv->DP |= DP_PORT_WIDTH_2;
  435. break;
  436. case 4:
  437. dp_priv->DP |= DP_PORT_WIDTH_4;
  438. break;
  439. }
  440. if (dp_priv->has_audio)
  441. dp_priv->DP |= DP_AUDIO_OUTPUT_ENABLE;
  442. memset(dp_priv->link_configuration, 0, DP_LINK_CONFIGURATION_SIZE);
  443. dp_priv->link_configuration[0] = dp_priv->link_bw;
  444. dp_priv->link_configuration[1] = dp_priv->lane_count;
  445. /*
  446. * Check for DPCD version > 1.1,
  447. * enable enahanced frame stuff in that case
  448. */
  449. if (dp_priv->dpcd[0] >= 0x11) {
  450. dp_priv->link_configuration[1] |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
  451. dp_priv->DP |= DP_ENHANCED_FRAMING;
  452. }
  453. if (intel_crtc->pipe == 1)
  454. dp_priv->DP |= DP_PIPEB_SELECT;
  455. }
  456. static void
  457. intel_dp_dpms(struct drm_encoder *encoder, int mode)
  458. {
  459. struct intel_output *intel_output = enc_to_intel_output(encoder);
  460. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  461. struct drm_device *dev = intel_output->base.dev;
  462. struct drm_i915_private *dev_priv = dev->dev_private;
  463. uint32_t dp_reg = I915_READ(dp_priv->output_reg);
  464. if (mode != DRM_MODE_DPMS_ON) {
  465. if (dp_reg & DP_PORT_EN)
  466. intel_dp_link_down(intel_output, dp_priv->DP);
  467. } else {
  468. if (!(dp_reg & DP_PORT_EN))
  469. intel_dp_link_train(intel_output, dp_priv->DP, dp_priv->link_configuration);
  470. }
  471. dp_priv->dpms_mode = mode;
  472. }
  473. /*
  474. * Fetch AUX CH registers 0x202 - 0x207 which contain
  475. * link status information
  476. */
  477. static bool
  478. intel_dp_get_link_status(struct intel_output *intel_output,
  479. uint8_t link_status[DP_LINK_STATUS_SIZE])
  480. {
  481. int ret;
  482. ret = intel_dp_aux_native_read(intel_output,
  483. DP_LANE0_1_STATUS,
  484. link_status, DP_LINK_STATUS_SIZE);
  485. if (ret != DP_LINK_STATUS_SIZE)
  486. return false;
  487. return true;
  488. }
  489. static uint8_t
  490. intel_dp_link_status(uint8_t link_status[DP_LINK_STATUS_SIZE],
  491. int r)
  492. {
  493. return link_status[r - DP_LANE0_1_STATUS];
  494. }
  495. static void
  496. intel_dp_save(struct drm_connector *connector)
  497. {
  498. struct intel_output *intel_output = to_intel_output(connector);
  499. struct drm_device *dev = intel_output->base.dev;
  500. struct drm_i915_private *dev_priv = dev->dev_private;
  501. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  502. dp_priv->save_DP = I915_READ(dp_priv->output_reg);
  503. intel_dp_aux_native_read(intel_output, DP_LINK_BW_SET,
  504. dp_priv->save_link_configuration,
  505. sizeof (dp_priv->save_link_configuration));
  506. }
  507. static uint8_t
  508. intel_get_adjust_request_voltage(uint8_t link_status[DP_LINK_STATUS_SIZE],
  509. int lane)
  510. {
  511. int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
  512. int s = ((lane & 1) ?
  513. DP_ADJUST_VOLTAGE_SWING_LANE1_SHIFT :
  514. DP_ADJUST_VOLTAGE_SWING_LANE0_SHIFT);
  515. uint8_t l = intel_dp_link_status(link_status, i);
  516. return ((l >> s) & 3) << DP_TRAIN_VOLTAGE_SWING_SHIFT;
  517. }
  518. static uint8_t
  519. intel_get_adjust_request_pre_emphasis(uint8_t link_status[DP_LINK_STATUS_SIZE],
  520. int lane)
  521. {
  522. int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
  523. int s = ((lane & 1) ?
  524. DP_ADJUST_PRE_EMPHASIS_LANE1_SHIFT :
  525. DP_ADJUST_PRE_EMPHASIS_LANE0_SHIFT);
  526. uint8_t l = intel_dp_link_status(link_status, i);
  527. return ((l >> s) & 3) << DP_TRAIN_PRE_EMPHASIS_SHIFT;
  528. }
  529. #if 0
  530. static char *voltage_names[] = {
  531. "0.4V", "0.6V", "0.8V", "1.2V"
  532. };
  533. static char *pre_emph_names[] = {
  534. "0dB", "3.5dB", "6dB", "9.5dB"
  535. };
  536. static char *link_train_names[] = {
  537. "pattern 1", "pattern 2", "idle", "off"
  538. };
  539. #endif
  540. /*
  541. * These are source-specific values; current Intel hardware supports
  542. * a maximum voltage of 800mV and a maximum pre-emphasis of 6dB
  543. */
  544. #define I830_DP_VOLTAGE_MAX DP_TRAIN_VOLTAGE_SWING_800
  545. static uint8_t
  546. intel_dp_pre_emphasis_max(uint8_t voltage_swing)
  547. {
  548. switch (voltage_swing & DP_TRAIN_VOLTAGE_SWING_MASK) {
  549. case DP_TRAIN_VOLTAGE_SWING_400:
  550. return DP_TRAIN_PRE_EMPHASIS_6;
  551. case DP_TRAIN_VOLTAGE_SWING_600:
  552. return DP_TRAIN_PRE_EMPHASIS_6;
  553. case DP_TRAIN_VOLTAGE_SWING_800:
  554. return DP_TRAIN_PRE_EMPHASIS_3_5;
  555. case DP_TRAIN_VOLTAGE_SWING_1200:
  556. default:
  557. return DP_TRAIN_PRE_EMPHASIS_0;
  558. }
  559. }
  560. static void
  561. intel_get_adjust_train(struct intel_output *intel_output,
  562. uint8_t link_status[DP_LINK_STATUS_SIZE],
  563. int lane_count,
  564. uint8_t train_set[4])
  565. {
  566. uint8_t v = 0;
  567. uint8_t p = 0;
  568. int lane;
  569. for (lane = 0; lane < lane_count; lane++) {
  570. uint8_t this_v = intel_get_adjust_request_voltage(link_status, lane);
  571. uint8_t this_p = intel_get_adjust_request_pre_emphasis(link_status, lane);
  572. if (this_v > v)
  573. v = this_v;
  574. if (this_p > p)
  575. p = this_p;
  576. }
  577. if (v >= I830_DP_VOLTAGE_MAX)
  578. v = I830_DP_VOLTAGE_MAX | DP_TRAIN_MAX_SWING_REACHED;
  579. if (p >= intel_dp_pre_emphasis_max(v))
  580. p = intel_dp_pre_emphasis_max(v) | DP_TRAIN_MAX_PRE_EMPHASIS_REACHED;
  581. for (lane = 0; lane < 4; lane++)
  582. train_set[lane] = v | p;
  583. }
  584. static uint32_t
  585. intel_dp_signal_levels(uint8_t train_set, int lane_count)
  586. {
  587. uint32_t signal_levels = 0;
  588. switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
  589. case DP_TRAIN_VOLTAGE_SWING_400:
  590. default:
  591. signal_levels |= DP_VOLTAGE_0_4;
  592. break;
  593. case DP_TRAIN_VOLTAGE_SWING_600:
  594. signal_levels |= DP_VOLTAGE_0_6;
  595. break;
  596. case DP_TRAIN_VOLTAGE_SWING_800:
  597. signal_levels |= DP_VOLTAGE_0_8;
  598. break;
  599. case DP_TRAIN_VOLTAGE_SWING_1200:
  600. signal_levels |= DP_VOLTAGE_1_2;
  601. break;
  602. }
  603. switch (train_set & DP_TRAIN_PRE_EMPHASIS_MASK) {
  604. case DP_TRAIN_PRE_EMPHASIS_0:
  605. default:
  606. signal_levels |= DP_PRE_EMPHASIS_0;
  607. break;
  608. case DP_TRAIN_PRE_EMPHASIS_3_5:
  609. signal_levels |= DP_PRE_EMPHASIS_3_5;
  610. break;
  611. case DP_TRAIN_PRE_EMPHASIS_6:
  612. signal_levels |= DP_PRE_EMPHASIS_6;
  613. break;
  614. case DP_TRAIN_PRE_EMPHASIS_9_5:
  615. signal_levels |= DP_PRE_EMPHASIS_9_5;
  616. break;
  617. }
  618. return signal_levels;
  619. }
  620. static uint8_t
  621. intel_get_lane_status(uint8_t link_status[DP_LINK_STATUS_SIZE],
  622. int lane)
  623. {
  624. int i = DP_LANE0_1_STATUS + (lane >> 1);
  625. int s = (lane & 1) * 4;
  626. uint8_t l = intel_dp_link_status(link_status, i);
  627. return (l >> s) & 0xf;
  628. }
  629. /* Check for clock recovery is done on all channels */
  630. static bool
  631. intel_clock_recovery_ok(uint8_t link_status[DP_LINK_STATUS_SIZE], int lane_count)
  632. {
  633. int lane;
  634. uint8_t lane_status;
  635. for (lane = 0; lane < lane_count; lane++) {
  636. lane_status = intel_get_lane_status(link_status, lane);
  637. if ((lane_status & DP_LANE_CR_DONE) == 0)
  638. return false;
  639. }
  640. return true;
  641. }
  642. /* Check to see if channel eq is done on all channels */
  643. #define CHANNEL_EQ_BITS (DP_LANE_CR_DONE|\
  644. DP_LANE_CHANNEL_EQ_DONE|\
  645. DP_LANE_SYMBOL_LOCKED)
  646. static bool
  647. intel_channel_eq_ok(uint8_t link_status[DP_LINK_STATUS_SIZE], int lane_count)
  648. {
  649. uint8_t lane_align;
  650. uint8_t lane_status;
  651. int lane;
  652. lane_align = intel_dp_link_status(link_status,
  653. DP_LANE_ALIGN_STATUS_UPDATED);
  654. if ((lane_align & DP_INTERLANE_ALIGN_DONE) == 0)
  655. return false;
  656. for (lane = 0; lane < lane_count; lane++) {
  657. lane_status = intel_get_lane_status(link_status, lane);
  658. if ((lane_status & CHANNEL_EQ_BITS) != CHANNEL_EQ_BITS)
  659. return false;
  660. }
  661. return true;
  662. }
  663. static bool
  664. intel_dp_set_link_train(struct intel_output *intel_output,
  665. uint32_t dp_reg_value,
  666. uint8_t dp_train_pat,
  667. uint8_t train_set[4],
  668. bool first)
  669. {
  670. struct drm_device *dev = intel_output->base.dev;
  671. struct drm_i915_private *dev_priv = dev->dev_private;
  672. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  673. int ret;
  674. I915_WRITE(dp_priv->output_reg, dp_reg_value);
  675. POSTING_READ(dp_priv->output_reg);
  676. if (first)
  677. intel_wait_for_vblank(dev);
  678. intel_dp_aux_native_write_1(intel_output,
  679. DP_TRAINING_PATTERN_SET,
  680. dp_train_pat);
  681. ret = intel_dp_aux_native_write(intel_output,
  682. DP_TRAINING_LANE0_SET, train_set, 4);
  683. if (ret != 4)
  684. return false;
  685. return true;
  686. }
  687. static void
  688. intel_dp_link_train(struct intel_output *intel_output, uint32_t DP,
  689. uint8_t link_configuration[DP_LINK_CONFIGURATION_SIZE])
  690. {
  691. struct drm_device *dev = intel_output->base.dev;
  692. struct drm_i915_private *dev_priv = dev->dev_private;
  693. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  694. uint8_t train_set[4];
  695. uint8_t link_status[DP_LINK_STATUS_SIZE];
  696. int i;
  697. uint8_t voltage;
  698. bool clock_recovery = false;
  699. bool channel_eq = false;
  700. bool first = true;
  701. int tries;
  702. /* Write the link configuration data */
  703. intel_dp_aux_native_write(intel_output, 0x100,
  704. link_configuration, DP_LINK_CONFIGURATION_SIZE);
  705. DP |= DP_PORT_EN;
  706. DP &= ~DP_LINK_TRAIN_MASK;
  707. memset(train_set, 0, 4);
  708. voltage = 0xff;
  709. tries = 0;
  710. clock_recovery = false;
  711. for (;;) {
  712. /* Use train_set[0] to set the voltage and pre emphasis values */
  713. uint32_t signal_levels = intel_dp_signal_levels(train_set[0], dp_priv->lane_count);
  714. DP = (DP & ~(DP_VOLTAGE_MASK|DP_PRE_EMPHASIS_MASK)) | signal_levels;
  715. if (!intel_dp_set_link_train(intel_output, DP | DP_LINK_TRAIN_PAT_1,
  716. DP_TRAINING_PATTERN_1, train_set, first))
  717. break;
  718. first = false;
  719. /* Set training pattern 1 */
  720. udelay(100);
  721. if (!intel_dp_get_link_status(intel_output, link_status))
  722. break;
  723. if (intel_clock_recovery_ok(link_status, dp_priv->lane_count)) {
  724. clock_recovery = true;
  725. break;
  726. }
  727. /* Check to see if we've tried the max voltage */
  728. for (i = 0; i < dp_priv->lane_count; i++)
  729. if ((train_set[i] & DP_TRAIN_MAX_SWING_REACHED) == 0)
  730. break;
  731. if (i == dp_priv->lane_count)
  732. break;
  733. /* Check to see if we've tried the same voltage 5 times */
  734. if ((train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK) == voltage) {
  735. ++tries;
  736. if (tries == 5)
  737. break;
  738. } else
  739. tries = 0;
  740. voltage = train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK;
  741. /* Compute new train_set as requested by target */
  742. intel_get_adjust_train(intel_output, link_status, dp_priv->lane_count, train_set);
  743. }
  744. /* channel equalization */
  745. tries = 0;
  746. channel_eq = false;
  747. for (;;) {
  748. /* Use train_set[0] to set the voltage and pre emphasis values */
  749. uint32_t signal_levels = intel_dp_signal_levels(train_set[0], dp_priv->lane_count);
  750. DP = (DP & ~(DP_VOLTAGE_MASK|DP_PRE_EMPHASIS_MASK)) | signal_levels;
  751. /* channel eq pattern */
  752. if (!intel_dp_set_link_train(intel_output, DP | DP_LINK_TRAIN_PAT_2,
  753. DP_TRAINING_PATTERN_2, train_set,
  754. false))
  755. break;
  756. udelay(400);
  757. if (!intel_dp_get_link_status(intel_output, link_status))
  758. break;
  759. if (intel_channel_eq_ok(link_status, dp_priv->lane_count)) {
  760. channel_eq = true;
  761. break;
  762. }
  763. /* Try 5 times */
  764. if (tries > 5)
  765. break;
  766. /* Compute new train_set as requested by target */
  767. intel_get_adjust_train(intel_output, link_status, dp_priv->lane_count, train_set);
  768. ++tries;
  769. }
  770. I915_WRITE(dp_priv->output_reg, DP | DP_LINK_TRAIN_OFF);
  771. POSTING_READ(dp_priv->output_reg);
  772. intel_dp_aux_native_write_1(intel_output,
  773. DP_TRAINING_PATTERN_SET, DP_TRAINING_PATTERN_DISABLE);
  774. }
  775. static void
  776. intel_dp_link_down(struct intel_output *intel_output, uint32_t DP)
  777. {
  778. struct drm_device *dev = intel_output->base.dev;
  779. struct drm_i915_private *dev_priv = dev->dev_private;
  780. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  781. I915_WRITE(dp_priv->output_reg, DP & ~DP_PORT_EN);
  782. POSTING_READ(dp_priv->output_reg);
  783. }
  784. static void
  785. intel_dp_restore(struct drm_connector *connector)
  786. {
  787. struct intel_output *intel_output = to_intel_output(connector);
  788. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  789. if (dp_priv->save_DP & DP_PORT_EN)
  790. intel_dp_link_train(intel_output, dp_priv->save_DP, dp_priv->save_link_configuration);
  791. else
  792. intel_dp_link_down(intel_output, dp_priv->save_DP);
  793. }
  794. /*
  795. * According to DP spec
  796. * 5.1.2:
  797. * 1. Read DPCD
  798. * 2. Configure link according to Receiver Capabilities
  799. * 3. Use Link Training from 2.5.3.3 and 3.5.1.3
  800. * 4. Check link status on receipt of hot-plug interrupt
  801. */
  802. static void
  803. intel_dp_check_link_status(struct intel_output *intel_output)
  804. {
  805. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  806. uint8_t link_status[DP_LINK_STATUS_SIZE];
  807. if (!intel_output->enc.crtc)
  808. return;
  809. if (!intel_dp_get_link_status(intel_output, link_status)) {
  810. intel_dp_link_down(intel_output, dp_priv->DP);
  811. return;
  812. }
  813. if (!intel_channel_eq_ok(link_status, dp_priv->lane_count))
  814. intel_dp_link_train(intel_output, dp_priv->DP, dp_priv->link_configuration);
  815. }
  816. /**
  817. * Uses CRT_HOTPLUG_EN and CRT_HOTPLUG_STAT to detect DP connection.
  818. *
  819. * \return true if DP port is connected.
  820. * \return false if DP port is disconnected.
  821. */
  822. static enum drm_connector_status
  823. intel_dp_detect(struct drm_connector *connector)
  824. {
  825. struct intel_output *intel_output = to_intel_output(connector);
  826. struct drm_device *dev = intel_output->base.dev;
  827. struct drm_i915_private *dev_priv = dev->dev_private;
  828. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  829. uint32_t temp, bit;
  830. enum drm_connector_status status;
  831. dp_priv->has_audio = false;
  832. temp = I915_READ(PORT_HOTPLUG_EN);
  833. I915_WRITE(PORT_HOTPLUG_EN,
  834. temp |
  835. DPB_HOTPLUG_INT_EN |
  836. DPC_HOTPLUG_INT_EN |
  837. DPD_HOTPLUG_INT_EN);
  838. POSTING_READ(PORT_HOTPLUG_EN);
  839. switch (dp_priv->output_reg) {
  840. case DP_B:
  841. bit = DPB_HOTPLUG_INT_STATUS;
  842. break;
  843. case DP_C:
  844. bit = DPC_HOTPLUG_INT_STATUS;
  845. break;
  846. case DP_D:
  847. bit = DPD_HOTPLUG_INT_STATUS;
  848. break;
  849. default:
  850. return connector_status_unknown;
  851. }
  852. temp = I915_READ(PORT_HOTPLUG_STAT);
  853. if ((temp & bit) == 0)
  854. return connector_status_disconnected;
  855. status = connector_status_disconnected;
  856. if (intel_dp_aux_native_read(intel_output,
  857. 0x000, dp_priv->dpcd,
  858. sizeof (dp_priv->dpcd)) == sizeof (dp_priv->dpcd))
  859. {
  860. if (dp_priv->dpcd[0] != 0)
  861. status = connector_status_connected;
  862. }
  863. return status;
  864. }
  865. static int intel_dp_get_modes(struct drm_connector *connector)
  866. {
  867. struct intel_output *intel_output = to_intel_output(connector);
  868. /* We should parse the EDID data and find out if it has an audio sink
  869. */
  870. return intel_ddc_get_modes(intel_output);
  871. }
  872. static void
  873. intel_dp_destroy (struct drm_connector *connector)
  874. {
  875. struct intel_output *intel_output = to_intel_output(connector);
  876. if (intel_output->i2c_bus)
  877. intel_i2c_destroy(intel_output->i2c_bus);
  878. drm_sysfs_connector_remove(connector);
  879. drm_connector_cleanup(connector);
  880. kfree(intel_output);
  881. }
  882. static const struct drm_encoder_helper_funcs intel_dp_helper_funcs = {
  883. .dpms = intel_dp_dpms,
  884. .mode_fixup = intel_dp_mode_fixup,
  885. .prepare = intel_encoder_prepare,
  886. .mode_set = intel_dp_mode_set,
  887. .commit = intel_encoder_commit,
  888. };
  889. static const struct drm_connector_funcs intel_dp_connector_funcs = {
  890. .dpms = drm_helper_connector_dpms,
  891. .save = intel_dp_save,
  892. .restore = intel_dp_restore,
  893. .detect = intel_dp_detect,
  894. .fill_modes = drm_helper_probe_single_connector_modes,
  895. .destroy = intel_dp_destroy,
  896. };
  897. static const struct drm_connector_helper_funcs intel_dp_connector_helper_funcs = {
  898. .get_modes = intel_dp_get_modes,
  899. .mode_valid = intel_dp_mode_valid,
  900. .best_encoder = intel_best_encoder,
  901. };
  902. static void intel_dp_enc_destroy(struct drm_encoder *encoder)
  903. {
  904. drm_encoder_cleanup(encoder);
  905. }
  906. static const struct drm_encoder_funcs intel_dp_enc_funcs = {
  907. .destroy = intel_dp_enc_destroy,
  908. };
  909. void
  910. intel_dp_hot_plug(struct intel_output *intel_output)
  911. {
  912. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  913. if (dp_priv->dpms_mode == DRM_MODE_DPMS_ON)
  914. intel_dp_check_link_status(intel_output);
  915. }
  916. void
  917. intel_dp_init(struct drm_device *dev, int output_reg)
  918. {
  919. struct drm_i915_private *dev_priv = dev->dev_private;
  920. struct drm_connector *connector;
  921. struct intel_output *intel_output;
  922. struct intel_dp_priv *dp_priv;
  923. intel_output = kcalloc(sizeof(struct intel_output) +
  924. sizeof(struct intel_dp_priv), 1, GFP_KERNEL);
  925. if (!intel_output)
  926. return;
  927. dp_priv = (struct intel_dp_priv *)(intel_output + 1);
  928. connector = &intel_output->base;
  929. drm_connector_init(dev, connector, &intel_dp_connector_funcs,
  930. DRM_MODE_CONNECTOR_DisplayPort);
  931. drm_connector_helper_add(connector, &intel_dp_connector_helper_funcs);
  932. intel_output->type = INTEL_OUTPUT_DISPLAYPORT;
  933. connector->interlace_allowed = true;
  934. connector->doublescan_allowed = 0;
  935. dp_priv->intel_output = intel_output;
  936. dp_priv->output_reg = output_reg;
  937. dp_priv->has_audio = false;
  938. dp_priv->dpms_mode = DRM_MODE_DPMS_ON;
  939. intel_output->dev_priv = dp_priv;
  940. drm_encoder_init(dev, &intel_output->enc, &intel_dp_enc_funcs,
  941. DRM_MODE_ENCODER_TMDS);
  942. drm_encoder_helper_add(&intel_output->enc, &intel_dp_helper_funcs);
  943. drm_mode_connector_attach_encoder(&intel_output->base,
  944. &intel_output->enc);
  945. drm_sysfs_connector_add(connector);
  946. /* Set up the DDC bus. */
  947. intel_dp_i2c_init(intel_output,
  948. (output_reg == DP_B) ? "DPDDC-B" :
  949. (output_reg == DP_C) ? "DPDDC-C" : "DPDDC-D");
  950. intel_output->ddc_bus = &dp_priv->adapter;
  951. intel_output->hot_plug = intel_dp_hot_plug;
  952. /* For G4X desktop chip, PEG_BAND_GAP_DATA 3:0 must first be written
  953. * 0xd. Failure to do so will result in spurious interrupts being
  954. * generated on the port when a cable is not attached.
  955. */
  956. if (IS_G4X(dev) && !IS_GM45(dev)) {
  957. u32 temp = I915_READ(PEG_BAND_GAP_DATA);
  958. I915_WRITE(PEG_BAND_GAP_DATA, (temp & ~0xf) | 0xd);
  959. }
  960. }