drm/nouveau/dp: rewrite auxch transaction routines
[pandora-kernel.git] / drivers / gpu / drm / nouveau / nouveau_dp.c
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
25 #include "drmP.h"
26
27 #include "nouveau_drv.h"
28 #include "nouveau_i2c.h"
29 #include "nouveau_connector.h"
30 #include "nouveau_encoder.h"
31
32 /******************************************************************************
33  * aux channel util functions
34  *****************************************************************************/
35 #define AUX_DBG(fmt, args...) do {                                             \
36         if (nouveau_reg_debug & NOUVEAU_REG_DEBUG_AUXCH) {                     \
37                 NV_PRINTK(KERN_DEBUG, dev, "AUXCH(%d): " fmt, ch, ##args);     \
38         }                                                                      \
39 } while (0)
40 #define AUX_ERR(fmt, args...) NV_ERROR(dev, "AUXCH(%d): " fmt, ch, ##args)
41
42 static void
43 auxch_fini(struct drm_device *dev, int ch)
44 {
45         nv_mask(dev, 0x00e4e4 + (ch * 0x50), 0x00310000, 0x00000000);
46 }
47
48 static int
49 auxch_init(struct drm_device *dev, int ch)
50 {
51         const u32 unksel = 1; /* nfi which to use, or if it matters.. */
52         const u32 ureq = unksel ? 0x00100000 : 0x00200000;
53         const u32 urep = unksel ? 0x01000000 : 0x02000000;
54         u32 ctrl, timeout;
55
56         /* wait up to 1ms for any previous transaction to be done... */
57         timeout = 1000;
58         do {
59                 ctrl = nv_rd32(dev, 0x00e4e4 + (ch * 0x50));
60                 udelay(1);
61                 if (!timeout--) {
62                         AUX_ERR("begin idle timeout 0x%08x", ctrl);
63                         return -EBUSY;
64                 }
65         } while (ctrl & 0x03010000);
66
67         /* set some magic, and wait up to 1ms for it to appear */
68         nv_mask(dev, 0x00e4e4 + (ch * 0x50), 0x00300000, ureq);
69         timeout = 1000;
70         do {
71                 ctrl = nv_rd32(dev, 0x00e4e4 + (ch * 0x50));
72                 udelay(1);
73                 if (!timeout--) {
74                         AUX_ERR("magic wait 0x%08x\n", ctrl);
75                         auxch_fini(dev, ch);
76                         return -EBUSY;
77                 }
78         } while ((ctrl & 0x03000000) != urep);
79
80         return 0;
81 }
82
83 static int
84 auxch_tx(struct drm_device *dev, int ch, u8 type, u32 addr, u8 *data, u8 size)
85 {
86         u32 ctrl, stat, timeout, retries;
87         u32 xbuf[4] = {};
88         int ret, i;
89
90         AUX_DBG("%d: 0x%08x %d\n", type, addr, size);
91
92         ret = auxch_init(dev, ch);
93         if (ret)
94                 goto out;
95
96         stat = nv_rd32(dev, 0x00e4e8 + (ch * 0x50));
97         if (!(stat & 0x10000000)) {
98                 AUX_DBG("sink not detected\n");
99                 ret = -ENXIO;
100                 goto out;
101         }
102
103         if (!(type & 1)) {
104                 memcpy(xbuf, data, size);
105                 for (i = 0; i < 16; i += 4) {
106                         AUX_DBG("wr 0x%08x\n", xbuf[i / 4]);
107                         nv_wr32(dev, 0x00e4c0 + (ch * 0x50) + i, xbuf[i / 4]);
108                 }
109         }
110
111         ctrl  = nv_rd32(dev, 0x00e4e4 + (ch * 0x50));
112         ctrl &= ~0x0001f0ff;
113         ctrl |= type << 12;
114         ctrl |= size - 1;
115         nv_wr32(dev, 0x00e4e0 + (ch * 0x50), addr);
116
117         /* retry transaction a number of times on failure... */
118         ret = -EREMOTEIO;
119         for (retries = 0; retries < 32; retries++) {
120                 /* reset, and delay a while if this is a retry */
121                 nv_wr32(dev, 0x00e4e4 + (ch * 0x50), 0x80000000 | ctrl);
122                 nv_wr32(dev, 0x00e4e4 + (ch * 0x50), 0x00000000 | ctrl);
123                 if (retries)
124                         udelay(400);
125
126                 /* transaction request, wait up to 1ms for it to complete */
127                 nv_wr32(dev, 0x00e4e4 + (ch * 0x50), 0x00010000 | ctrl);
128
129                 timeout = 1000;
130                 do {
131                         ctrl = nv_rd32(dev, 0x00e4e4 + (ch * 0x50));
132                         udelay(1);
133                         if (!timeout--) {
134                                 AUX_ERR("tx req timeout 0x%08x\n", ctrl);
135                                 goto out;
136                         }
137                 } while (ctrl & 0x00010000);
138
139                 /* read status, and check if transaction completed ok */
140                 stat = nv_mask(dev, 0x00e4e8 + (ch * 0x50), 0, 0);
141                 if (!(stat & 0x000f0f00)) {
142                         ret = 0;
143                         break;
144                 }
145
146                 AUX_DBG("%02d 0x%08x 0x%08x\n", retries, ctrl, stat);
147         }
148
149         if (type & 1) {
150                 for (i = 0; i < 16; i += 4) {
151                         xbuf[i / 4] = nv_rd32(dev, 0x00e4d0 + (ch * 0x50) + i);
152                         AUX_DBG("rd 0x%08x\n", xbuf[i / 4]);
153                 }
154                 memcpy(data, xbuf, size);
155         }
156
157 out:
158         auxch_fini(dev, ch);
159         return ret;
160 }
161
162 static int
163 auxch_rd(struct drm_encoder *encoder, int address, uint8_t *buf, int size)
164 {
165         struct drm_device *dev = encoder->dev;
166         struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
167         struct nouveau_i2c_chan *auxch;
168         int ret;
169
170         auxch = nouveau_i2c_find(dev, nv_encoder->dcb->i2c_index);
171         if (!auxch)
172                 return -ENODEV;
173
174         ret = nouveau_dp_auxch(auxch, 9, address, buf, size);
175         if (ret)
176                 return ret;
177
178         return 0;
179 }
180
181 static int
182 auxch_wr(struct drm_encoder *encoder, int address, uint8_t *buf, int size)
183 {
184         struct drm_device *dev = encoder->dev;
185         struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
186         struct nouveau_i2c_chan *auxch;
187         int ret;
188
189         auxch = nouveau_i2c_find(dev, nv_encoder->dcb->i2c_index);
190         if (!auxch)
191                 return -ENODEV;
192
193         ret = nouveau_dp_auxch(auxch, 8, address, buf, size);
194         return ret;
195 }
196
197 static int
198 nouveau_dp_lane_count_set(struct drm_encoder *encoder, uint8_t cmd)
199 {
200         struct drm_device *dev = encoder->dev;
201         struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
202         uint32_t tmp;
203         int or = nv_encoder->or, link = !(nv_encoder->dcb->sorconf.link & 1);
204
205         tmp  = nv_rd32(dev, NV50_SOR_DP_CTRL(or, link));
206         tmp &= ~(NV50_SOR_DP_CTRL_ENHANCED_FRAME_ENABLED |
207                  NV50_SOR_DP_CTRL_LANE_MASK);
208         tmp |= ((1 << (cmd & DP_LANE_COUNT_MASK)) - 1) << 16;
209         if (cmd & DP_LANE_COUNT_ENHANCED_FRAME_EN)
210                 tmp |= NV50_SOR_DP_CTRL_ENHANCED_FRAME_ENABLED;
211         nv_wr32(dev, NV50_SOR_DP_CTRL(or, link), tmp);
212
213         return auxch_wr(encoder, DP_LANE_COUNT_SET, &cmd, 1);
214 }
215
216 static int
217 nouveau_dp_link_bw_set(struct drm_encoder *encoder, uint8_t cmd)
218 {
219         struct drm_device *dev = encoder->dev;
220         struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
221         uint32_t tmp;
222         int reg = 0x614300 + (nv_encoder->or * 0x800);
223
224         tmp  = nv_rd32(dev, reg);
225         tmp &= 0xfff3ffff;
226         if (cmd == DP_LINK_BW_2_7)
227                 tmp |= 0x00040000;
228         nv_wr32(dev, reg, tmp);
229
230         return auxch_wr(encoder, DP_LINK_BW_SET, &cmd, 1);
231 }
232
233 static int
234 nouveau_dp_link_train_set(struct drm_encoder *encoder, int pattern)
235 {
236         struct drm_device *dev = encoder->dev;
237         struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
238         uint32_t tmp;
239         uint8_t cmd;
240         int or = nv_encoder->or, link = !(nv_encoder->dcb->sorconf.link & 1);
241         int ret;
242
243         tmp  = nv_rd32(dev, NV50_SOR_DP_CTRL(or, link));
244         tmp &= ~NV50_SOR_DP_CTRL_TRAINING_PATTERN;
245         tmp |= (pattern << 24);
246         nv_wr32(dev, NV50_SOR_DP_CTRL(or, link), tmp);
247
248         ret = auxch_rd(encoder, DP_TRAINING_PATTERN_SET, &cmd, 1);
249         if (ret)
250                 return ret;
251         cmd &= ~DP_TRAINING_PATTERN_MASK;
252         cmd |= (pattern & DP_TRAINING_PATTERN_MASK);
253         return auxch_wr(encoder, DP_TRAINING_PATTERN_SET, &cmd, 1);
254 }
255
256 static int
257 nouveau_dp_max_voltage_swing(struct drm_encoder *encoder)
258 {
259         struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
260         struct drm_device *dev = encoder->dev;
261         struct bit_displayport_encoder_table_entry *dpse;
262         struct bit_displayport_encoder_table *dpe;
263         int i, dpe_headerlen, max_vs = 0;
264
265         dpe = nouveau_bios_dp_table(dev, nv_encoder->dcb, &dpe_headerlen);
266         if (!dpe)
267                 return false;
268         dpse = (void *)((char *)dpe + dpe_headerlen);
269
270         for (i = 0; i < dpe_headerlen; i++, dpse++) {
271                 if (dpse->vs_level > max_vs)
272                         max_vs = dpse->vs_level;
273         }
274
275         return max_vs;
276 }
277
278 static int
279 nouveau_dp_max_pre_emphasis(struct drm_encoder *encoder, int vs)
280 {
281         struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
282         struct drm_device *dev = encoder->dev;
283         struct bit_displayport_encoder_table_entry *dpse;
284         struct bit_displayport_encoder_table *dpe;
285         int i, dpe_headerlen, max_pre = 0;
286
287         dpe = nouveau_bios_dp_table(dev, nv_encoder->dcb, &dpe_headerlen);
288         if (!dpe)
289                 return false;
290         dpse = (void *)((char *)dpe + dpe_headerlen);
291
292         for (i = 0; i < dpe_headerlen; i++, dpse++) {
293                 if (dpse->vs_level != vs)
294                         continue;
295
296                 if (dpse->pre_level > max_pre)
297                         max_pre = dpse->pre_level;
298         }
299
300         return max_pre;
301 }
302
303 static bool
304 nouveau_dp_link_train_adjust(struct drm_encoder *encoder, uint8_t *config)
305 {
306         struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
307         struct drm_device *dev = encoder->dev;
308         struct bit_displayport_encoder_table *dpe;
309         int ret, i, dpe_headerlen, vs = 0, pre = 0;
310         uint8_t request[2];
311
312         dpe = nouveau_bios_dp_table(dev, nv_encoder->dcb, &dpe_headerlen);
313         if (!dpe)
314                 return false;
315
316         ret = auxch_rd(encoder, DP_ADJUST_REQUEST_LANE0_1, request, 2);
317         if (ret)
318                 return false;
319
320         NV_DEBUG_KMS(dev, "\t\tadjust 0x%02x 0x%02x\n", request[0], request[1]);
321
322         /* Keep all lanes at the same level.. */
323         for (i = 0; i < nv_encoder->dp.link_nr; i++) {
324                 int lane_req = (request[i >> 1] >> ((i & 1) << 2)) & 0xf;
325                 int lane_vs = lane_req & 3;
326                 int lane_pre = (lane_req >> 2) & 3;
327
328                 if (lane_vs > vs)
329                         vs = lane_vs;
330                 if (lane_pre > pre)
331                         pre = lane_pre;
332         }
333
334         if (vs >= nouveau_dp_max_voltage_swing(encoder)) {
335                 vs  = nouveau_dp_max_voltage_swing(encoder);
336                 vs |= 4;
337         }
338
339         if (pre >= nouveau_dp_max_pre_emphasis(encoder, vs & 3)) {
340                 pre  = nouveau_dp_max_pre_emphasis(encoder, vs & 3);
341                 pre |= 4;
342         }
343
344         /* Update the configuration for all lanes.. */
345         for (i = 0; i < nv_encoder->dp.link_nr; i++)
346                 config[i] = (pre << 3) | vs;
347
348         return true;
349 }
350
351 static bool
352 nouveau_dp_link_train_commit(struct drm_encoder *encoder, uint8_t *config)
353 {
354         struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
355         struct drm_device *dev = encoder->dev;
356         struct bit_displayport_encoder_table_entry *dpse;
357         struct bit_displayport_encoder_table *dpe;
358         int or = nv_encoder->or, link = !(nv_encoder->dcb->sorconf.link & 1);
359         int dpe_headerlen, ret, i;
360
361         NV_DEBUG_KMS(dev, "\t\tconfig 0x%02x 0x%02x 0x%02x 0x%02x\n",
362                  config[0], config[1], config[2], config[3]);
363
364         dpe = nouveau_bios_dp_table(dev, nv_encoder->dcb, &dpe_headerlen);
365         if (!dpe)
366                 return false;
367         dpse = (void *)((char *)dpe + dpe_headerlen);
368
369         for (i = 0; i < dpe->record_nr; i++, dpse++) {
370                 if (dpse->vs_level == (config[0] & 3) &&
371                     dpse->pre_level == ((config[0] >> 3) & 3))
372                         break;
373         }
374         BUG_ON(i == dpe->record_nr);
375
376         for (i = 0; i < nv_encoder->dp.link_nr; i++) {
377                 const int shift[4] = { 16, 8, 0, 24 };
378                 uint32_t mask = 0xff << shift[i];
379                 uint32_t reg0, reg1, reg2;
380
381                 reg0  = nv_rd32(dev, NV50_SOR_DP_UNK118(or, link)) & ~mask;
382                 reg0 |= (dpse->reg0 << shift[i]);
383                 reg1  = nv_rd32(dev, NV50_SOR_DP_UNK120(or, link)) & ~mask;
384                 reg1 |= (dpse->reg1 << shift[i]);
385                 reg2  = nv_rd32(dev, NV50_SOR_DP_UNK130(or, link)) & 0xffff00ff;
386                 reg2 |= (dpse->reg2 << 8);
387                 nv_wr32(dev, NV50_SOR_DP_UNK118(or, link), reg0);
388                 nv_wr32(dev, NV50_SOR_DP_UNK120(or, link), reg1);
389                 nv_wr32(dev, NV50_SOR_DP_UNK130(or, link), reg2);
390         }
391
392         ret = auxch_wr(encoder, DP_TRAINING_LANE0_SET, config, 4);
393         if (ret)
394                 return false;
395
396         return true;
397 }
398
399 bool
400 nouveau_dp_link_train(struct drm_encoder *encoder)
401 {
402         struct drm_device *dev = encoder->dev;
403         struct drm_nouveau_private *dev_priv = dev->dev_private;
404         struct nouveau_gpio_engine *pgpio = &dev_priv->engine.gpio;
405         struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
406         struct nouveau_connector *nv_connector;
407         struct bit_displayport_encoder_table *dpe;
408         int dpe_headerlen;
409         uint8_t config[4], status[3];
410         bool cr_done, cr_max_vs, eq_done, hpd_state;
411         int ret = 0, i, tries, voltage;
412
413         NV_DEBUG_KMS(dev, "link training!!\n");
414
415         nv_connector = nouveau_encoder_connector_get(nv_encoder);
416         if (!nv_connector)
417                 return false;
418
419         dpe = nouveau_bios_dp_table(dev, nv_encoder->dcb, &dpe_headerlen);
420         if (!dpe) {
421                 NV_ERROR(dev, "SOR-%d: no DP encoder table!\n", nv_encoder->or);
422                 return false;
423         }
424
425         /* disable hotplug detect, this flips around on some panels during
426          * link training.
427          */
428         hpd_state = pgpio->irq_enable(dev, nv_connector->dcb->gpio_tag, false);
429
430         if (dpe->script0) {
431                 NV_DEBUG_KMS(dev, "SOR-%d: running DP script 0\n", nv_encoder->or);
432                 nouveau_bios_run_init_table(dev, le16_to_cpu(dpe->script0),
433                                             nv_encoder->dcb, -1);
434         }
435
436 train:
437         cr_done = eq_done = false;
438
439         /* set link configuration */
440         NV_DEBUG_KMS(dev, "\tbegin train: bw %d, lanes %d\n",
441                  nv_encoder->dp.link_bw, nv_encoder->dp.link_nr);
442
443         ret = nouveau_dp_link_bw_set(encoder, nv_encoder->dp.link_bw);
444         if (ret)
445                 return false;
446
447         config[0] = nv_encoder->dp.link_nr;
448         if (nv_encoder->dp.dpcd_version >= 0x11 &&
449             nv_encoder->dp.enhanced_frame)
450                 config[0] |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
451
452         ret = nouveau_dp_lane_count_set(encoder, config[0]);
453         if (ret)
454                 return false;
455
456         /* clock recovery */
457         NV_DEBUG_KMS(dev, "\tbegin cr\n");
458         ret = nouveau_dp_link_train_set(encoder, DP_TRAINING_PATTERN_1);
459         if (ret)
460                 goto stop;
461
462         tries = 0;
463         voltage = -1;
464         memset(config, 0x00, sizeof(config));
465         for (;;) {
466                 if (!nouveau_dp_link_train_commit(encoder, config))
467                         break;
468
469                 udelay(100);
470
471                 ret = auxch_rd(encoder, DP_LANE0_1_STATUS, status, 2);
472                 if (ret)
473                         break;
474                 NV_DEBUG_KMS(dev, "\t\tstatus: 0x%02x 0x%02x\n",
475                          status[0], status[1]);
476
477                 cr_done = true;
478                 cr_max_vs = false;
479                 for (i = 0; i < nv_encoder->dp.link_nr; i++) {
480                         int lane = (status[i >> 1] >> ((i & 1) * 4)) & 0xf;
481
482                         if (!(lane & DP_LANE_CR_DONE)) {
483                                 cr_done = false;
484                                 if (config[i] & DP_TRAIN_MAX_PRE_EMPHASIS_REACHED)
485                                         cr_max_vs = true;
486                                 break;
487                         }
488                 }
489
490                 if ((config[0] & DP_TRAIN_VOLTAGE_SWING_MASK) != voltage) {
491                         voltage = config[0] & DP_TRAIN_VOLTAGE_SWING_MASK;
492                         tries = 0;
493                 }
494
495                 if (cr_done || cr_max_vs || (++tries == 5))
496                         break;
497
498                 if (!nouveau_dp_link_train_adjust(encoder, config))
499                         break;
500         }
501
502         if (!cr_done)
503                 goto stop;
504
505         /* channel equalisation */
506         NV_DEBUG_KMS(dev, "\tbegin eq\n");
507         ret = nouveau_dp_link_train_set(encoder, DP_TRAINING_PATTERN_2);
508         if (ret)
509                 goto stop;
510
511         for (tries = 0; tries <= 5; tries++) {
512                 udelay(400);
513
514                 ret = auxch_rd(encoder, DP_LANE0_1_STATUS, status, 3);
515                 if (ret)
516                         break;
517                 NV_DEBUG_KMS(dev, "\t\tstatus: 0x%02x 0x%02x\n",
518                          status[0], status[1]);
519
520                 eq_done = true;
521                 if (!(status[2] & DP_INTERLANE_ALIGN_DONE))
522                         eq_done = false;
523
524                 for (i = 0; eq_done && i < nv_encoder->dp.link_nr; i++) {
525                         int lane = (status[i >> 1] >> ((i & 1) * 4)) & 0xf;
526
527                         if (!(lane & DP_LANE_CR_DONE)) {
528                                 cr_done = false;
529                                 break;
530                         }
531
532                         if (!(lane & DP_LANE_CHANNEL_EQ_DONE) ||
533                             !(lane & DP_LANE_SYMBOL_LOCKED)) {
534                                 eq_done = false;
535                                 break;
536                         }
537                 }
538
539                 if (eq_done || !cr_done)
540                         break;
541
542                 if (!nouveau_dp_link_train_adjust(encoder, config) ||
543                     !nouveau_dp_link_train_commit(encoder, config))
544                         break;
545         }
546
547 stop:
548         /* end link training */
549         ret = nouveau_dp_link_train_set(encoder, DP_TRAINING_PATTERN_DISABLE);
550         if (ret)
551                 return false;
552
553         /* retry at a lower setting, if possible */
554         if (!ret && !(eq_done && cr_done)) {
555                 NV_DEBUG_KMS(dev, "\twe failed\n");
556                 if (nv_encoder->dp.link_bw != DP_LINK_BW_1_62) {
557                         NV_DEBUG_KMS(dev, "retry link training at low rate\n");
558                         nv_encoder->dp.link_bw = DP_LINK_BW_1_62;
559                         goto train;
560                 }
561         }
562
563         if (dpe->script1) {
564                 NV_DEBUG_KMS(dev, "SOR-%d: running DP script 1\n", nv_encoder->or);
565                 nouveau_bios_run_init_table(dev, le16_to_cpu(dpe->script1),
566                                             nv_encoder->dcb, -1);
567         }
568
569         /* re-enable hotplug detect */
570         pgpio->irq_enable(dev, nv_connector->dcb->gpio_tag, hpd_state);
571
572         return eq_done;
573 }
574
575 bool
576 nouveau_dp_detect(struct drm_encoder *encoder)
577 {
578         struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
579         struct drm_device *dev = encoder->dev;
580         uint8_t dpcd[4];
581         int ret;
582
583         ret = auxch_rd(encoder, 0x0000, dpcd, 4);
584         if (ret)
585                 return false;
586
587         NV_DEBUG_KMS(dev, "encoder: link_bw %d, link_nr %d\n"
588                       "display: link_bw %d, link_nr %d version 0x%02x\n",
589                  nv_encoder->dcb->dpconf.link_bw,
590                  nv_encoder->dcb->dpconf.link_nr,
591                  dpcd[1], dpcd[2] & 0x0f, dpcd[0]);
592
593         nv_encoder->dp.dpcd_version = dpcd[0];
594
595         nv_encoder->dp.link_bw = dpcd[1];
596         if (nv_encoder->dp.link_bw != DP_LINK_BW_1_62 &&
597             !nv_encoder->dcb->dpconf.link_bw)
598                 nv_encoder->dp.link_bw = DP_LINK_BW_1_62;
599
600         nv_encoder->dp.link_nr = dpcd[2] & DP_MAX_LANE_COUNT_MASK;
601         if (nv_encoder->dp.link_nr > nv_encoder->dcb->dpconf.link_nr)
602                 nv_encoder->dp.link_nr = nv_encoder->dcb->dpconf.link_nr;
603
604         nv_encoder->dp.enhanced_frame = (dpcd[2] & DP_ENHANCED_FRAME_CAP);
605
606         return true;
607 }
608
609 int
610 nouveau_dp_auxch(struct nouveau_i2c_chan *auxch, int cmd, int addr,
611                  uint8_t *data, int data_nr)
612 {
613         return auxch_tx(auxch->dev, auxch->rd, cmd, addr, data, data_nr);
614 }
615
616 static int
617 nouveau_dp_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
618 {
619         struct nouveau_i2c_chan *auxch = (struct nouveau_i2c_chan *)adap;
620         struct drm_device *dev = auxch->dev;
621         struct i2c_msg *msg = msgs;
622         int ret, mcnt = num;
623
624         while (mcnt--) {
625                 u8 remaining = msg->len;
626                 u8 *ptr = msg->buf;
627
628                 while (remaining) {
629                         u8 cnt = (remaining > 16) ? 16 : remaining;
630                         u8 cmd;
631
632                         if (msg->flags & I2C_M_RD)
633                                 cmd = AUX_I2C_READ;
634                         else
635                                 cmd = AUX_I2C_WRITE;
636
637                         if (mcnt || remaining > 16)
638                                 cmd |= AUX_I2C_MOT;
639
640                         ret = nouveau_dp_auxch(auxch, cmd, msg->addr, ptr, cnt);
641                         if (ret < 0)
642                                 return ret;
643
644                         ptr += cnt;
645                         remaining -= cnt;
646                 }
647
648                 msg++;
649         }
650
651         return num;
652 }
653
654 static u32
655 nouveau_dp_i2c_func(struct i2c_adapter *adap)
656 {
657         return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
658 }
659
660 const struct i2c_algorithm nouveau_dp_i2c_algo = {
661         .master_xfer = nouveau_dp_i2c_xfer,
662         .functionality = nouveau_dp_i2c_func
663 };