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