intel_dp.c 29 KB

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