nouveau_dp.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. /*
  2. * Copyright 2009 Red Hat Inc.
  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 shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * Authors: Ben Skeggs
  23. */
  24. #include "drmP.h"
  25. #include "nouveau_drv.h"
  26. #include "nouveau_i2c.h"
  27. #include "nouveau_connector.h"
  28. #include "nouveau_encoder.h"
  29. #include "nouveau_crtc.h"
  30. /******************************************************************************
  31. * aux channel util functions
  32. *****************************************************************************/
  33. #define AUX_DBG(fmt, args...) do { \
  34. if (nouveau_reg_debug & NOUVEAU_REG_DEBUG_AUXCH) { \
  35. NV_PRINTK(KERN_DEBUG, dev, "AUXCH(%d): " fmt, ch, ##args); \
  36. } \
  37. } while (0)
  38. #define AUX_ERR(fmt, args...) NV_ERROR(dev, "AUXCH(%d): " fmt, ch, ##args)
  39. static void
  40. auxch_fini(struct drm_device *dev, int ch)
  41. {
  42. nv_mask(dev, 0x00e4e4 + (ch * 0x50), 0x00310000, 0x00000000);
  43. }
  44. static int
  45. auxch_init(struct drm_device *dev, int ch)
  46. {
  47. const u32 unksel = 1; /* nfi which to use, or if it matters.. */
  48. const u32 ureq = unksel ? 0x00100000 : 0x00200000;
  49. const u32 urep = unksel ? 0x01000000 : 0x02000000;
  50. u32 ctrl, timeout;
  51. /* wait up to 1ms for any previous transaction to be done... */
  52. timeout = 1000;
  53. do {
  54. ctrl = nv_rd32(dev, 0x00e4e4 + (ch * 0x50));
  55. udelay(1);
  56. if (!timeout--) {
  57. AUX_ERR("begin idle timeout 0x%08x", ctrl);
  58. return -EBUSY;
  59. }
  60. } while (ctrl & 0x03010000);
  61. /* set some magic, and wait up to 1ms for it to appear */
  62. nv_mask(dev, 0x00e4e4 + (ch * 0x50), 0x00300000, ureq);
  63. timeout = 1000;
  64. do {
  65. ctrl = nv_rd32(dev, 0x00e4e4 + (ch * 0x50));
  66. udelay(1);
  67. if (!timeout--) {
  68. AUX_ERR("magic wait 0x%08x\n", ctrl);
  69. auxch_fini(dev, ch);
  70. return -EBUSY;
  71. }
  72. } while ((ctrl & 0x03000000) != urep);
  73. return 0;
  74. }
  75. static int
  76. auxch_tx(struct drm_device *dev, int ch, u8 type, u32 addr, u8 *data, u8 size)
  77. {
  78. u32 ctrl, stat, timeout, retries;
  79. u32 xbuf[4] = {};
  80. int ret, i;
  81. AUX_DBG("%d: 0x%08x %d\n", type, addr, size);
  82. ret = auxch_init(dev, ch);
  83. if (ret)
  84. goto out;
  85. stat = nv_rd32(dev, 0x00e4e8 + (ch * 0x50));
  86. if (!(stat & 0x10000000)) {
  87. AUX_DBG("sink not detected\n");
  88. ret = -ENXIO;
  89. goto out;
  90. }
  91. if (!(type & 1)) {
  92. memcpy(xbuf, data, size);
  93. for (i = 0; i < 16; i += 4) {
  94. AUX_DBG("wr 0x%08x\n", xbuf[i / 4]);
  95. nv_wr32(dev, 0x00e4c0 + (ch * 0x50) + i, xbuf[i / 4]);
  96. }
  97. }
  98. ctrl = nv_rd32(dev, 0x00e4e4 + (ch * 0x50));
  99. ctrl &= ~0x0001f0ff;
  100. ctrl |= type << 12;
  101. ctrl |= size - 1;
  102. nv_wr32(dev, 0x00e4e0 + (ch * 0x50), addr);
  103. /* retry transaction a number of times on failure... */
  104. ret = -EREMOTEIO;
  105. for (retries = 0; retries < 32; retries++) {
  106. /* reset, and delay a while if this is a retry */
  107. nv_wr32(dev, 0x00e4e4 + (ch * 0x50), 0x80000000 | ctrl);
  108. nv_wr32(dev, 0x00e4e4 + (ch * 0x50), 0x00000000 | ctrl);
  109. if (retries)
  110. udelay(400);
  111. /* transaction request, wait up to 1ms for it to complete */
  112. nv_wr32(dev, 0x00e4e4 + (ch * 0x50), 0x00010000 | ctrl);
  113. timeout = 1000;
  114. do {
  115. ctrl = nv_rd32(dev, 0x00e4e4 + (ch * 0x50));
  116. udelay(1);
  117. if (!timeout--) {
  118. AUX_ERR("tx req timeout 0x%08x\n", ctrl);
  119. goto out;
  120. }
  121. } while (ctrl & 0x00010000);
  122. /* read status, and check if transaction completed ok */
  123. stat = nv_mask(dev, 0x00e4e8 + (ch * 0x50), 0, 0);
  124. if (!(stat & 0x000f0f00)) {
  125. ret = 0;
  126. break;
  127. }
  128. AUX_DBG("%02d 0x%08x 0x%08x\n", retries, ctrl, stat);
  129. }
  130. if (type & 1) {
  131. for (i = 0; i < 16; i += 4) {
  132. xbuf[i / 4] = nv_rd32(dev, 0x00e4d0 + (ch * 0x50) + i);
  133. AUX_DBG("rd 0x%08x\n", xbuf[i / 4]);
  134. }
  135. memcpy(data, xbuf, size);
  136. }
  137. out:
  138. auxch_fini(dev, ch);
  139. return ret;
  140. }
  141. static u32
  142. dp_link_bw_get(struct drm_device *dev, int or, int link)
  143. {
  144. u32 ctrl = nv_rd32(dev, 0x614300 + (or * 0x800));
  145. if (!(ctrl & 0x000c0000))
  146. return 162000;
  147. return 270000;
  148. }
  149. static int
  150. dp_lane_count_get(struct drm_device *dev, int or, int link)
  151. {
  152. u32 ctrl = nv_rd32(dev, NV50_SOR_DP_CTRL(or, link));
  153. switch (ctrl & 0x000f0000) {
  154. case 0x00010000: return 1;
  155. case 0x00030000: return 2;
  156. default:
  157. return 4;
  158. }
  159. }
  160. void
  161. nouveau_dp_tu_update(struct drm_device *dev, int or, int link, u32 clk, u32 bpp)
  162. {
  163. const u32 symbol = 100000;
  164. int bestTU = 0, bestVTUi = 0, bestVTUf = 0, bestVTUa = 0;
  165. int TU, VTUi, VTUf, VTUa;
  166. u64 link_data_rate, link_ratio, unk;
  167. u32 best_diff = 64 * symbol;
  168. u32 link_nr, link_bw, r;
  169. /* calculate packed data rate for each lane */
  170. link_nr = dp_lane_count_get(dev, or, link);
  171. link_data_rate = (clk * bpp / 8) / link_nr;
  172. /* calculate ratio of packed data rate to link symbol rate */
  173. link_bw = dp_link_bw_get(dev, or, link);
  174. link_ratio = link_data_rate * symbol;
  175. r = do_div(link_ratio, link_bw);
  176. for (TU = 64; TU >= 32; TU--) {
  177. /* calculate average number of valid symbols in each TU */
  178. u32 tu_valid = link_ratio * TU;
  179. u32 calc, diff;
  180. /* find a hw representation for the fraction.. */
  181. VTUi = tu_valid / symbol;
  182. calc = VTUi * symbol;
  183. diff = tu_valid - calc;
  184. if (diff) {
  185. if (diff >= (symbol / 2)) {
  186. VTUf = symbol / (symbol - diff);
  187. if (symbol - (VTUf * diff))
  188. VTUf++;
  189. if (VTUf <= 15) {
  190. VTUa = 1;
  191. calc += symbol - (symbol / VTUf);
  192. } else {
  193. VTUa = 0;
  194. VTUf = 1;
  195. calc += symbol;
  196. }
  197. } else {
  198. VTUa = 0;
  199. VTUf = min((int)(symbol / diff), 15);
  200. calc += symbol / VTUf;
  201. }
  202. diff = calc - tu_valid;
  203. } else {
  204. /* no remainder, but the hw doesn't like the fractional
  205. * part to be zero. decrement the integer part and
  206. * have the fraction add a whole symbol back
  207. */
  208. VTUa = 0;
  209. VTUf = 1;
  210. VTUi--;
  211. }
  212. if (diff < best_diff) {
  213. best_diff = diff;
  214. bestTU = TU;
  215. bestVTUa = VTUa;
  216. bestVTUf = VTUf;
  217. bestVTUi = VTUi;
  218. if (diff == 0)
  219. break;
  220. }
  221. }
  222. if (!bestTU) {
  223. NV_ERROR(dev, "DP: unable to find suitable config\n");
  224. return;
  225. }
  226. /* XXX close to vbios numbers, but not right */
  227. unk = (symbol - link_ratio) * bestTU;
  228. unk *= link_ratio;
  229. r = do_div(unk, symbol);
  230. r = do_div(unk, symbol);
  231. unk += 6;
  232. nv_mask(dev, NV50_SOR_DP_CTRL(or, link), 0x000001fc, bestTU << 2);
  233. nv_mask(dev, NV50_SOR_DP_SCFG(or, link), 0x010f7f3f, bestVTUa << 24 |
  234. bestVTUf << 16 |
  235. bestVTUi << 8 |
  236. unk);
  237. }
  238. /******************************************************************************
  239. * link training
  240. *****************************************************************************/
  241. struct dp_state {
  242. struct dcb_entry *dcb;
  243. int auxch;
  244. int crtc;
  245. int or;
  246. int link;
  247. u8 *dpcd;
  248. int link_nr;
  249. u32 link_bw;
  250. u8 stat[6];
  251. u8 conf[4];
  252. };
  253. static void
  254. dp_set_link_config(struct drm_device *dev, struct dp_state *dp)
  255. {
  256. struct drm_nouveau_private *dev_priv = dev->dev_private;
  257. int or = dp->or, link = dp->link;
  258. u8 *bios, headerlen, sink[2];
  259. u32 dp_ctrl;
  260. NV_DEBUG_KMS(dev, "%d lanes at %d KB/s\n", dp->link_nr, dp->link_bw);
  261. /* set selected link rate on source */
  262. switch (dp->link_bw) {
  263. case 270000:
  264. nv_mask(dev, 0x614300 + (or * 0x800), 0x000c0000, 0x00040000);
  265. sink[0] = DP_LINK_BW_2_7;
  266. break;
  267. default:
  268. nv_mask(dev, 0x614300 + (or * 0x800), 0x000c0000, 0x00000000);
  269. sink[0] = DP_LINK_BW_1_62;
  270. break;
  271. }
  272. /* offset +0x0a of each dp encoder table entry is a pointer to another
  273. * table, that has (among other things) pointers to more scripts that
  274. * need to be executed, this time depending on link speed.
  275. */
  276. bios = nouveau_bios_dp_table(dev, dp->dcb, &headerlen);
  277. if (bios && (bios = ROMPTR(&dev_priv->vbios, bios[10]))) {
  278. while (dp->link_bw < (ROM16(bios[0]) * 10))
  279. bios += 4;
  280. nouveau_bios_run_init_table(dev, ROM16(bios[2]), dp->dcb, dp->crtc);
  281. }
  282. /* configure lane count on the source */
  283. dp_ctrl = ((1 << dp->link_nr) - 1) << 16;
  284. sink[1] = dp->link_nr;
  285. if (dp->dpcd[2] & DP_ENHANCED_FRAME_CAP) {
  286. dp_ctrl |= 0x00004000;
  287. sink[1] |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
  288. }
  289. nv_mask(dev, NV50_SOR_DP_CTRL(or, link), 0x001f4000, dp_ctrl);
  290. /* inform the sink of the new configuration */
  291. auxch_tx(dev, dp->auxch, 8, DP_LINK_BW_SET, sink, 2);
  292. }
  293. static void
  294. dp_set_training_pattern(struct drm_device *dev, struct dp_state *dp, u8 tp)
  295. {
  296. NV_DEBUG_KMS(dev, "training pattern %d\n", tp);
  297. nv_mask(dev, NV50_SOR_DP_CTRL(dp->or, dp->link), 0x0f000000, tp << 24);
  298. auxch_tx(dev, dp->auxch, 8, DP_TRAINING_PATTERN_SET, &tp, 1);
  299. }
  300. static const u8 nv50_lane_map[] = { 16, 8, 0, 24 };
  301. static const u8 nvaf_lane_map[] = { 24, 16, 8, 0 };
  302. static int
  303. dp_link_train_commit(struct drm_device *dev, struct dp_state *dp)
  304. {
  305. struct drm_nouveau_private *dev_priv = dev->dev_private;
  306. u32 mask = 0, drv = 0, pre = 0, unk = 0;
  307. u8 *bios, *last, headerlen;
  308. const u8 *shifts;
  309. int link = dp->link;
  310. int or = dp->or;
  311. int i;
  312. if (dev_priv->chipset != 0xaf)
  313. shifts = nv50_lane_map;
  314. else
  315. shifts = nvaf_lane_map;
  316. bios = nouveau_bios_dp_table(dev, dp->dcb, &headerlen);
  317. last = bios + headerlen + (bios[4] * 5);
  318. for (i = 0; i < dp->link_nr; i++) {
  319. u8 lane = (dp->stat[4 + (i >> 1)] >> ((i & 1) * 4)) & 0xf;
  320. u8 *conf = bios + headerlen;
  321. while (conf < last) {
  322. if ((lane & 3) == conf[0] &&
  323. (lane >> 2) == conf[1])
  324. break;
  325. conf += 5;
  326. }
  327. if (conf == last)
  328. return -EINVAL;
  329. dp->conf[i] = (conf[1] << 3) | conf[0];
  330. if (conf[0] == DP_TRAIN_VOLTAGE_SWING_1200)
  331. dp->conf[i] |= DP_TRAIN_MAX_SWING_REACHED;
  332. if (conf[1] == DP_TRAIN_PRE_EMPHASIS_9_5)
  333. dp->conf[i] |= DP_TRAIN_MAX_PRE_EMPHASIS_REACHED;
  334. NV_DEBUG_KMS(dev, "config lane %d %02x\n", i, dp->conf[i]);
  335. mask |= 0xff << shifts[i];
  336. drv |= conf[2] << shifts[i];
  337. pre |= conf[3] << shifts[i];
  338. unk = (unk & ~0x0000ff00) | (conf[4] << 8);
  339. unk |= 1 << (shifts[i] >> 3);
  340. }
  341. nv_mask(dev, NV50_SOR_DP_UNK118(or, link), mask, drv);
  342. nv_mask(dev, NV50_SOR_DP_UNK120(or, link), mask, pre);
  343. nv_mask(dev, NV50_SOR_DP_UNK130(or, link), 0x0000ff0f, unk);
  344. return auxch_tx(dev, dp->auxch, 8, DP_TRAINING_LANE0_SET, dp->conf, 4);
  345. }
  346. static int
  347. dp_link_train_update(struct drm_device *dev, struct dp_state *dp, u32 delay)
  348. {
  349. int ret;
  350. udelay(delay);
  351. ret = auxch_tx(dev, dp->auxch, 9, DP_LANE0_1_STATUS, dp->stat, 6);
  352. if (ret)
  353. return ret;
  354. NV_DEBUG_KMS(dev, "status %02x %02x %02x %02x %02x %02x\n",
  355. dp->stat[0], dp->stat[1], dp->stat[2], dp->stat[3],
  356. dp->stat[4], dp->stat[5]);
  357. return 0;
  358. }
  359. static int
  360. dp_link_train_cr(struct drm_device *dev, struct dp_state *dp)
  361. {
  362. bool cr_done = false, abort = false;
  363. int voltage = dp->conf[0] & DP_TRAIN_VOLTAGE_SWING_MASK;
  364. int tries = 0, i;
  365. dp_set_training_pattern(dev, dp, DP_TRAINING_PATTERN_1);
  366. do {
  367. if (dp_link_train_commit(dev, dp) ||
  368. dp_link_train_update(dev, dp, 100))
  369. break;
  370. cr_done = true;
  371. for (i = 0; i < dp->link_nr; i++) {
  372. u8 lane = (dp->stat[i >> 1] >> ((i & 1) * 4)) & 0xf;
  373. if (!(lane & DP_LANE_CR_DONE)) {
  374. cr_done = false;
  375. if (dp->conf[i] & DP_TRAIN_MAX_SWING_REACHED)
  376. abort = true;
  377. break;
  378. }
  379. }
  380. if ((dp->conf[0] & DP_TRAIN_VOLTAGE_SWING_MASK) != voltage) {
  381. voltage = dp->conf[0] & DP_TRAIN_VOLTAGE_SWING_MASK;
  382. tries = 0;
  383. }
  384. } while (!cr_done && !abort && ++tries < 5);
  385. return cr_done ? 0 : -1;
  386. }
  387. static int
  388. dp_link_train_eq(struct drm_device *dev, struct dp_state *dp)
  389. {
  390. bool eq_done, cr_done = true;
  391. int tries = 0, i;
  392. dp_set_training_pattern(dev, dp, DP_TRAINING_PATTERN_2);
  393. do {
  394. if (dp_link_train_update(dev, dp, 400))
  395. break;
  396. eq_done = !!(dp->stat[2] & DP_INTERLANE_ALIGN_DONE);
  397. for (i = 0; i < dp->link_nr && eq_done; i++) {
  398. u8 lane = (dp->stat[i >> 1] >> ((i & 1) * 4)) & 0xf;
  399. if (!(lane & DP_LANE_CR_DONE))
  400. cr_done = false;
  401. if (!(lane & DP_LANE_CHANNEL_EQ_DONE) ||
  402. !(lane & DP_LANE_SYMBOL_LOCKED))
  403. eq_done = false;
  404. }
  405. if (dp_link_train_commit(dev, dp))
  406. break;
  407. } while (!eq_done && cr_done && ++tries <= 5);
  408. return eq_done ? 0 : -1;
  409. }
  410. bool
  411. nouveau_dp_link_train(struct drm_encoder *encoder, u32 datarate)
  412. {
  413. struct drm_nouveau_private *dev_priv = encoder->dev->dev_private;
  414. struct nouveau_gpio_engine *pgpio = &dev_priv->engine.gpio;
  415. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  416. struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
  417. struct nouveau_connector *nv_connector =
  418. nouveau_encoder_connector_get(nv_encoder);
  419. struct drm_device *dev = encoder->dev;
  420. struct nouveau_i2c_chan *auxch;
  421. const u32 bw_list[] = { 270000, 162000, 0 };
  422. const u32 *link_bw = bw_list;
  423. struct dp_state dp;
  424. u8 *bios, headerlen;
  425. auxch = nouveau_i2c_find(dev, nv_encoder->dcb->i2c_index);
  426. if (!auxch)
  427. return false;
  428. bios = nouveau_bios_dp_table(dev, nv_encoder->dcb, &headerlen);
  429. if (!bios)
  430. return -EINVAL;
  431. dp.dcb = nv_encoder->dcb;
  432. dp.crtc = nv_crtc->index;
  433. dp.auxch = auxch->rd;
  434. dp.or = nv_encoder->or;
  435. dp.link = !(nv_encoder->dcb->sorconf.link & 1);
  436. dp.dpcd = nv_encoder->dp.dpcd;
  437. /* some sinks toggle hotplug in response to some of the actions
  438. * we take during link training (DP_SET_POWER is one), we need
  439. * to ignore them for the moment to avoid races.
  440. */
  441. pgpio->irq_enable(dev, nv_connector->dcb->gpio_tag, false);
  442. /* enable down-spreading, if possible */
  443. if (headerlen >= 16) {
  444. u16 script = ROM16(bios[14]);
  445. if (nv_encoder->dp.dpcd[3] & 1)
  446. script = ROM16(bios[12]);
  447. nouveau_bios_run_init_table(dev, script, dp.dcb, dp.crtc);
  448. }
  449. /* execute pre-train script from vbios */
  450. nouveau_bios_run_init_table(dev, ROM16(bios[6]), dp.dcb, dp.crtc);
  451. /* start off at highest link rate supported by encoder and display */
  452. while (*link_bw > nv_encoder->dp.link_bw)
  453. link_bw++;
  454. while (link_bw[0]) {
  455. /* find minimum required lane count at this link rate */
  456. dp.link_nr = nv_encoder->dp.link_nr;
  457. while ((dp.link_nr >> 1) * link_bw[0] > datarate)
  458. dp.link_nr >>= 1;
  459. /* drop link rate to minimum with this lane count */
  460. while ((link_bw[1] * dp.link_nr) > datarate)
  461. link_bw++;
  462. dp.link_bw = link_bw[0];
  463. /* program selected link configuration */
  464. dp_set_link_config(dev, &dp);
  465. /* attempt to train the link at this configuration */
  466. memset(dp.stat, 0x00, sizeof(dp.stat));
  467. if (!dp_link_train_cr(dev, &dp) &&
  468. !dp_link_train_eq(dev, &dp))
  469. break;
  470. /* retry at lower rate */
  471. link_bw++;
  472. }
  473. /* finish link training */
  474. dp_set_training_pattern(dev, &dp, DP_TRAINING_PATTERN_DISABLE);
  475. /* execute post-train script from vbios */
  476. nouveau_bios_run_init_table(dev, ROM16(bios[8]), dp.dcb, dp.crtc);
  477. /* re-enable hotplug detect */
  478. pgpio->irq_enable(dev, nv_connector->dcb->gpio_tag, true);
  479. return true;
  480. }
  481. bool
  482. nouveau_dp_detect(struct drm_encoder *encoder)
  483. {
  484. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  485. struct drm_device *dev = encoder->dev;
  486. struct nouveau_i2c_chan *auxch;
  487. u8 *dpcd = nv_encoder->dp.dpcd;
  488. int ret;
  489. auxch = nouveau_i2c_find(dev, nv_encoder->dcb->i2c_index);
  490. if (!auxch)
  491. return false;
  492. ret = auxch_tx(dev, auxch->rd, 9, DP_DPCD_REV, dpcd, 8);
  493. if (ret)
  494. return false;
  495. nv_encoder->dp.link_bw = 27000 * dpcd[1];
  496. nv_encoder->dp.link_nr = dpcd[2] & DP_MAX_LANE_COUNT_MASK;
  497. NV_DEBUG_KMS(dev, "display: %dx%d dpcd 0x%02x\n",
  498. nv_encoder->dp.link_nr, nv_encoder->dp.link_bw, dpcd[0]);
  499. NV_DEBUG_KMS(dev, "encoder: %dx%d\n",
  500. nv_encoder->dcb->dpconf.link_nr,
  501. nv_encoder->dcb->dpconf.link_bw);
  502. if (nv_encoder->dcb->dpconf.link_nr < nv_encoder->dp.link_nr)
  503. nv_encoder->dp.link_nr = nv_encoder->dcb->dpconf.link_nr;
  504. if (nv_encoder->dcb->dpconf.link_bw < nv_encoder->dp.link_bw)
  505. nv_encoder->dp.link_bw = nv_encoder->dcb->dpconf.link_bw;
  506. NV_DEBUG_KMS(dev, "maximum: %dx%d\n",
  507. nv_encoder->dp.link_nr, nv_encoder->dp.link_bw);
  508. return true;
  509. }
  510. int
  511. nouveau_dp_auxch(struct nouveau_i2c_chan *auxch, int cmd, int addr,
  512. uint8_t *data, int data_nr)
  513. {
  514. return auxch_tx(auxch->dev, auxch->rd, cmd, addr, data, data_nr);
  515. }
  516. static int
  517. nouveau_dp_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
  518. {
  519. struct nouveau_i2c_chan *auxch = (struct nouveau_i2c_chan *)adap;
  520. struct i2c_msg *msg = msgs;
  521. int ret, mcnt = num;
  522. while (mcnt--) {
  523. u8 remaining = msg->len;
  524. u8 *ptr = msg->buf;
  525. while (remaining) {
  526. u8 cnt = (remaining > 16) ? 16 : remaining;
  527. u8 cmd;
  528. if (msg->flags & I2C_M_RD)
  529. cmd = AUX_I2C_READ;
  530. else
  531. cmd = AUX_I2C_WRITE;
  532. if (mcnt || remaining > 16)
  533. cmd |= AUX_I2C_MOT;
  534. ret = nouveau_dp_auxch(auxch, cmd, msg->addr, ptr, cnt);
  535. if (ret < 0)
  536. return ret;
  537. ptr += cnt;
  538. remaining -= cnt;
  539. }
  540. msg++;
  541. }
  542. return num;
  543. }
  544. static u32
  545. nouveau_dp_i2c_func(struct i2c_adapter *adap)
  546. {
  547. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  548. }
  549. const struct i2c_algorithm nouveau_dp_i2c_algo = {
  550. .master_xfer = nouveau_dp_i2c_xfer,
  551. .functionality = nouveau_dp_i2c_func
  552. };