intel_dp.c 29 KB

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