Merge remote branch 'korg/drm-core-next' into drm-next-stage
[linux-2.6.git] / drivers / gpu / drm / nouveau / nouveau_connector.c
1 /*
2  * Copyright (C) 2008 Maarten Maathuis.
3  * All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the
14  * next paragraph) shall be included in all copies or substantial
15  * portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20  * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
21  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  *
25  */
26
27 #include <acpi/button.h>
28
29 #include "drmP.h"
30 #include "drm_edid.h"
31 #include "drm_crtc_helper.h"
32
33 #include "nouveau_reg.h"
34 #include "nouveau_drv.h"
35 #include "nouveau_encoder.h"
36 #include "nouveau_crtc.h"
37 #include "nouveau_connector.h"
38 #include "nouveau_hw.h"
39
40 static inline struct drm_encoder_slave_funcs *
41 get_slave_funcs(struct nouveau_encoder *enc)
42 {
43         return to_encoder_slave(to_drm_encoder(enc))->slave_funcs;
44 }
45
46 static struct nouveau_encoder *
47 find_encoder_by_type(struct drm_connector *connector, int type)
48 {
49         struct drm_device *dev = connector->dev;
50         struct nouveau_encoder *nv_encoder;
51         struct drm_mode_object *obj;
52         int i, id;
53
54         for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
55                 id = connector->encoder_ids[i];
56                 if (!id)
57                         break;
58
59                 obj = drm_mode_object_find(dev, id, DRM_MODE_OBJECT_ENCODER);
60                 if (!obj)
61                         continue;
62                 nv_encoder = nouveau_encoder(obj_to_encoder(obj));
63
64                 if (type == OUTPUT_ANY || nv_encoder->dcb->type == type)
65                         return nv_encoder;
66         }
67
68         return NULL;
69 }
70
71 struct nouveau_connector *
72 nouveau_encoder_connector_get(struct nouveau_encoder *encoder)
73 {
74         struct drm_device *dev = to_drm_encoder(encoder)->dev;
75         struct drm_connector *drm_connector;
76
77         list_for_each_entry(drm_connector, &dev->mode_config.connector_list, head) {
78                 if (drm_connector->encoder == to_drm_encoder(encoder))
79                         return nouveau_connector(drm_connector);
80         }
81
82         return NULL;
83 }
84
85
86 static void
87 nouveau_connector_destroy(struct drm_connector *drm_connector)
88 {
89         struct nouveau_connector *nv_connector =
90                 nouveau_connector(drm_connector);
91         struct drm_device *dev;
92
93         if (!nv_connector)
94                 return;
95
96         dev = nv_connector->base.dev;
97         NV_DEBUG_KMS(dev, "\n");
98
99         kfree(nv_connector->edid);
100         drm_sysfs_connector_remove(drm_connector);
101         drm_connector_cleanup(drm_connector);
102         kfree(drm_connector);
103 }
104
105 static void
106 nouveau_connector_ddc_prepare(struct drm_connector *connector, int *flags)
107 {
108         struct drm_nouveau_private *dev_priv = connector->dev->dev_private;
109
110         if (dev_priv->card_type >= NV_50)
111                 return;
112
113         *flags = 0;
114         if (NVLockVgaCrtcs(dev_priv->dev, false))
115                 *flags |= 1;
116         if (nv_heads_tied(dev_priv->dev))
117                 *flags |= 2;
118
119         if (*flags & 2)
120                 NVSetOwner(dev_priv->dev, 0); /* necessary? */
121 }
122
123 static void
124 nouveau_connector_ddc_finish(struct drm_connector *connector, int flags)
125 {
126         struct drm_nouveau_private *dev_priv = connector->dev->dev_private;
127
128         if (dev_priv->card_type >= NV_50)
129                 return;
130
131         if (flags & 2)
132                 NVSetOwner(dev_priv->dev, 4);
133         if (flags & 1)
134                 NVLockVgaCrtcs(dev_priv->dev, true);
135 }
136
137 static struct nouveau_i2c_chan *
138 nouveau_connector_ddc_detect(struct drm_connector *connector,
139                              struct nouveau_encoder **pnv_encoder)
140 {
141         struct drm_device *dev = connector->dev;
142         uint8_t out_buf[] = { 0x0, 0x0}, buf[2];
143         int ret, flags, i;
144
145         struct i2c_msg msgs[] = {
146                 {
147                         .addr = 0x50,
148                         .flags = 0,
149                         .len = 1,
150                         .buf = out_buf,
151                 },
152                 {
153                         .addr = 0x50,
154                         .flags = I2C_M_RD,
155                         .len = 1,
156                         .buf = buf,
157                 }
158         };
159
160         for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
161                 struct nouveau_i2c_chan *i2c = NULL;
162                 struct nouveau_encoder *nv_encoder;
163                 struct drm_mode_object *obj;
164                 int id;
165
166                 id = connector->encoder_ids[i];
167                 if (!id)
168                         break;
169
170                 obj = drm_mode_object_find(dev, id, DRM_MODE_OBJECT_ENCODER);
171                 if (!obj)
172                         continue;
173                 nv_encoder = nouveau_encoder(obj_to_encoder(obj));
174
175                 if (nv_encoder->dcb->i2c_index < 0xf)
176                         i2c = nouveau_i2c_find(dev, nv_encoder->dcb->i2c_index);
177                 if (!i2c)
178                         continue;
179
180                 nouveau_connector_ddc_prepare(connector, &flags);
181                 ret = i2c_transfer(&i2c->adapter, msgs, 2);
182                 nouveau_connector_ddc_finish(connector, flags);
183
184                 if (ret == 2) {
185                         *pnv_encoder = nv_encoder;
186                         return i2c;
187                 }
188         }
189
190         return NULL;
191 }
192
193 static void
194 nouveau_connector_set_encoder(struct drm_connector *connector,
195                               struct nouveau_encoder *nv_encoder)
196 {
197         struct nouveau_connector *nv_connector = nouveau_connector(connector);
198         struct drm_nouveau_private *dev_priv = connector->dev->dev_private;
199         struct drm_device *dev = connector->dev;
200
201         if (nv_connector->detected_encoder == nv_encoder)
202                 return;
203         nv_connector->detected_encoder = nv_encoder;
204
205         if (nv_encoder->dcb->type == OUTPUT_LVDS ||
206             nv_encoder->dcb->type == OUTPUT_TMDS) {
207                 connector->doublescan_allowed = false;
208                 connector->interlace_allowed = false;
209         } else {
210                 connector->doublescan_allowed = true;
211                 if (dev_priv->card_type == NV_20 ||
212                    (dev_priv->card_type == NV_10 &&
213                     (dev->pci_device & 0x0ff0) != 0x0100 &&
214                     (dev->pci_device & 0x0ff0) != 0x0150))
215                         /* HW is broken */
216                         connector->interlace_allowed = false;
217                 else
218                         connector->interlace_allowed = true;
219         }
220
221         if (connector->connector_type == DRM_MODE_CONNECTOR_DVII) {
222                 drm_connector_property_set_value(connector,
223                         dev->mode_config.dvi_i_subconnector_property,
224                         nv_encoder->dcb->type == OUTPUT_TMDS ?
225                         DRM_MODE_SUBCONNECTOR_DVID :
226                         DRM_MODE_SUBCONNECTOR_DVIA);
227         }
228 }
229
230 static enum drm_connector_status
231 nouveau_connector_detect(struct drm_connector *connector)
232 {
233         struct drm_device *dev = connector->dev;
234         struct nouveau_connector *nv_connector = nouveau_connector(connector);
235         struct nouveau_encoder *nv_encoder = NULL;
236         struct nouveau_i2c_chan *i2c;
237         int type, flags;
238
239         if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS)
240                 nv_encoder = find_encoder_by_type(connector, OUTPUT_LVDS);
241         if (nv_encoder && nv_connector->native_mode) {
242 #ifdef CONFIG_ACPI
243                 if (!nouveau_ignorelid && !acpi_lid_open())
244                         return connector_status_disconnected;
245 #endif
246                 nouveau_connector_set_encoder(connector, nv_encoder);
247                 return connector_status_connected;
248         }
249
250         /* Cleanup the previous EDID block. */
251         if (nv_connector->edid) {
252                 drm_mode_connector_update_edid_property(connector, NULL);
253                 kfree(nv_connector->edid);
254                 nv_connector->edid = NULL;
255         }
256
257         i2c = nouveau_connector_ddc_detect(connector, &nv_encoder);
258         if (i2c) {
259                 nouveau_connector_ddc_prepare(connector, &flags);
260                 nv_connector->edid = drm_get_edid(connector, &i2c->adapter);
261                 nouveau_connector_ddc_finish(connector, flags);
262                 drm_mode_connector_update_edid_property(connector,
263                                                         nv_connector->edid);
264                 if (!nv_connector->edid) {
265                         NV_ERROR(dev, "DDC responded, but no EDID for %s\n",
266                                  drm_get_connector_name(connector));
267                         goto detect_analog;
268                 }
269
270                 if (nv_encoder->dcb->type == OUTPUT_DP &&
271                     !nouveau_dp_detect(to_drm_encoder(nv_encoder))) {
272                         NV_ERROR(dev, "Detected %s, but failed init\n",
273                                  drm_get_connector_name(connector));
274                         return connector_status_disconnected;
275                 }
276
277                 /* Override encoder type for DVI-I based on whether EDID
278                  * says the display is digital or analog, both use the
279                  * same i2c channel so the value returned from ddc_detect
280                  * isn't necessarily correct.
281                  */
282                 if (connector->connector_type == DRM_MODE_CONNECTOR_DVII) {
283                         if (nv_connector->edid->input & DRM_EDID_INPUT_DIGITAL)
284                                 type = OUTPUT_TMDS;
285                         else
286                                 type = OUTPUT_ANALOG;
287
288                         nv_encoder = find_encoder_by_type(connector, type);
289                         if (!nv_encoder) {
290                                 NV_ERROR(dev, "Detected %d encoder on %s, "
291                                               "but no object!\n", type,
292                                          drm_get_connector_name(connector));
293                                 return connector_status_disconnected;
294                         }
295                 }
296
297                 nouveau_connector_set_encoder(connector, nv_encoder);
298                 return connector_status_connected;
299         }
300
301 detect_analog:
302         nv_encoder = find_encoder_by_type(connector, OUTPUT_ANALOG);
303         if (!nv_encoder)
304                 nv_encoder = find_encoder_by_type(connector, OUTPUT_TV);
305         if (nv_encoder) {
306                 struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
307                 struct drm_encoder_helper_funcs *helper =
308                                                 encoder->helper_private;
309
310                 if (helper->detect(encoder, connector) ==
311                                                 connector_status_connected) {
312                         nouveau_connector_set_encoder(connector, nv_encoder);
313                         return connector_status_connected;
314                 }
315
316         }
317
318         return connector_status_disconnected;
319 }
320
321 static void
322 nouveau_connector_force(struct drm_connector *connector)
323 {
324         struct drm_device *dev = connector->dev;
325         struct nouveau_encoder *nv_encoder;
326         int type;
327
328         if (connector->connector_type == DRM_MODE_CONNECTOR_DVII) {
329                 if (connector->force == DRM_FORCE_ON_DIGITAL)
330                         type = OUTPUT_TMDS;
331                 else
332                         type = OUTPUT_ANALOG;
333         } else
334                 type = OUTPUT_ANY;
335
336         nv_encoder = find_encoder_by_type(connector, type);
337         if (!nv_encoder) {
338                 NV_ERROR(dev, "can't find encoder to force %s on!\n",
339                          drm_get_connector_name(connector));
340                 connector->status = connector_status_disconnected;
341                 return;
342         }
343
344         nouveau_connector_set_encoder(connector, nv_encoder);
345 }
346
347 static int
348 nouveau_connector_set_property(struct drm_connector *connector,
349                                struct drm_property *property, uint64_t value)
350 {
351         struct nouveau_connector *nv_connector = nouveau_connector(connector);
352         struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
353         struct drm_device *dev = connector->dev;
354         int ret;
355
356         /* Scaling mode */
357         if (property == dev->mode_config.scaling_mode_property) {
358                 struct nouveau_crtc *nv_crtc = NULL;
359                 bool modeset = false;
360
361                 switch (value) {
362                 case DRM_MODE_SCALE_NONE:
363                 case DRM_MODE_SCALE_FULLSCREEN:
364                 case DRM_MODE_SCALE_CENTER:
365                 case DRM_MODE_SCALE_ASPECT:
366                         break;
367                 default:
368                         return -EINVAL;
369                 }
370
371                 /* LVDS always needs gpu scaling */
372                 if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS &&
373                     value == DRM_MODE_SCALE_NONE)
374                         return -EINVAL;
375
376                 /* Changing between GPU and panel scaling requires a full
377                  * modeset
378                  */
379                 if ((nv_connector->scaling_mode == DRM_MODE_SCALE_NONE) ||
380                     (value == DRM_MODE_SCALE_NONE))
381                         modeset = true;
382                 nv_connector->scaling_mode = value;
383
384                 if (connector->encoder && connector->encoder->crtc)
385                         nv_crtc = nouveau_crtc(connector->encoder->crtc);
386                 if (!nv_crtc)
387                         return 0;
388
389                 if (modeset || !nv_crtc->set_scale) {
390                         ret = drm_crtc_helper_set_mode(&nv_crtc->base,
391                                                         &nv_crtc->base.mode,
392                                                         nv_crtc->base.x,
393                                                         nv_crtc->base.y, NULL);
394                         if (!ret)
395                                 return -EINVAL;
396                 } else {
397                         ret = nv_crtc->set_scale(nv_crtc, value, true);
398                         if (ret)
399                                 return ret;
400                 }
401
402                 return 0;
403         }
404
405         /* Dithering */
406         if (property == dev->mode_config.dithering_mode_property) {
407                 struct nouveau_crtc *nv_crtc = NULL;
408
409                 if (value == DRM_MODE_DITHERING_ON)
410                         nv_connector->use_dithering = true;
411                 else
412                         nv_connector->use_dithering = false;
413
414                 if (connector->encoder && connector->encoder->crtc)
415                         nv_crtc = nouveau_crtc(connector->encoder->crtc);
416
417                 if (!nv_crtc || !nv_crtc->set_dither)
418                         return 0;
419
420                 return nv_crtc->set_dither(nv_crtc, nv_connector->use_dithering,
421                                            true);
422         }
423
424         if (nv_encoder && nv_encoder->dcb->type == OUTPUT_TV)
425                 return get_slave_funcs(nv_encoder)->
426                         set_property(to_drm_encoder(nv_encoder), connector, property, value);
427
428         return -EINVAL;
429 }
430
431 static struct drm_display_mode *
432 nouveau_connector_native_mode(struct nouveau_connector *connector)
433 {
434         struct drm_device *dev = connector->base.dev;
435         struct drm_display_mode *mode, *largest = NULL;
436         int high_w = 0, high_h = 0, high_v = 0;
437
438         /* Use preferred mode if there is one.. */
439         list_for_each_entry(mode, &connector->base.probed_modes, head) {
440                 if (mode->type & DRM_MODE_TYPE_PREFERRED) {
441                         NV_DEBUG_KMS(dev, "native mode from preferred\n");
442                         return drm_mode_duplicate(dev, mode);
443                 }
444         }
445
446         /* Otherwise, take the resolution with the largest width, then height,
447          * then vertical refresh
448          */
449         list_for_each_entry(mode, &connector->base.probed_modes, head) {
450                 if (mode->hdisplay < high_w)
451                         continue;
452
453                 if (mode->hdisplay == high_w && mode->vdisplay < high_h)
454                         continue;
455
456                 if (mode->hdisplay == high_w && mode->vdisplay == high_h &&
457                     mode->vrefresh < high_v)
458                         continue;
459
460                 high_w = mode->hdisplay;
461                 high_h = mode->vdisplay;
462                 high_v = mode->vrefresh;
463                 largest = mode;
464         }
465
466         NV_DEBUG_KMS(dev, "native mode from largest: %dx%d@%d\n",
467                       high_w, high_h, high_v);
468         return largest ? drm_mode_duplicate(dev, largest) : NULL;
469 }
470
471 struct moderec {
472         int hdisplay;
473         int vdisplay;
474 };
475
476 static struct moderec scaler_modes[] = {
477         { 1920, 1200 },
478         { 1920, 1080 },
479         { 1680, 1050 },
480         { 1600, 1200 },
481         { 1400, 1050 },
482         { 1280, 1024 },
483         { 1280, 960 },
484         { 1152, 864 },
485         { 1024, 768 },
486         { 800, 600 },
487         { 720, 400 },
488         { 640, 480 },
489         { 640, 400 },
490         { 640, 350 },
491         {}
492 };
493
494 static int
495 nouveau_connector_scaler_modes_add(struct drm_connector *connector)
496 {
497         struct nouveau_connector *nv_connector = nouveau_connector(connector);
498         struct drm_display_mode *native = nv_connector->native_mode, *m;
499         struct drm_device *dev = connector->dev;
500         struct moderec *mode = &scaler_modes[0];
501         int modes = 0;
502
503         if (!native)
504                 return 0;
505
506         while (mode->hdisplay) {
507                 if (mode->hdisplay <= native->hdisplay &&
508                     mode->vdisplay <= native->vdisplay) {
509                         m = drm_cvt_mode(dev, mode->hdisplay, mode->vdisplay,
510                                          drm_mode_vrefresh(native), false,
511                                          false, false);
512                         if (!m)
513                                 continue;
514
515                         m->type |= DRM_MODE_TYPE_DRIVER;
516
517                         drm_mode_probed_add(connector, m);
518                         modes++;
519                 }
520
521                 mode++;
522         }
523
524         return modes;
525 }
526
527 static int
528 nouveau_connector_get_modes(struct drm_connector *connector)
529 {
530         struct drm_device *dev = connector->dev;
531         struct nouveau_connector *nv_connector = nouveau_connector(connector);
532         struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
533         int ret = 0;
534
535         /* If we're not LVDS, destroy the previous native mode, the attached
536          * monitor could have changed.
537          */
538         if (connector->connector_type != DRM_MODE_CONNECTOR_LVDS &&
539             nv_connector->native_mode) {
540                 drm_mode_destroy(dev, nv_connector->native_mode);
541                 nv_connector->native_mode = NULL;
542         }
543
544         if (nv_connector->edid)
545                 ret = drm_add_edid_modes(connector, nv_connector->edid);
546
547         /* Find the native mode if this is a digital panel, if we didn't
548          * find any modes through DDC previously add the native mode to
549          * the list of modes.
550          */
551         if (!nv_connector->native_mode)
552                 nv_connector->native_mode =
553                         nouveau_connector_native_mode(nv_connector);
554         if (ret == 0 && nv_connector->native_mode) {
555                 struct drm_display_mode *mode;
556
557                 mode = drm_mode_duplicate(dev, nv_connector->native_mode);
558                 drm_mode_probed_add(connector, mode);
559                 ret = 1;
560         }
561
562         if (nv_encoder->dcb->type == OUTPUT_TV)
563                 ret = get_slave_funcs(nv_encoder)->
564                         get_modes(to_drm_encoder(nv_encoder), connector);
565
566         if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS)
567                 ret += nouveau_connector_scaler_modes_add(connector);
568
569         return ret;
570 }
571
572 static int
573 nouveau_connector_mode_valid(struct drm_connector *connector,
574                              struct drm_display_mode *mode)
575 {
576         struct drm_nouveau_private *dev_priv = connector->dev->dev_private;
577         struct nouveau_connector *nv_connector = nouveau_connector(connector);
578         struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
579         unsigned min_clock = 25000, max_clock = min_clock;
580         unsigned clock = mode->clock;
581
582         switch (nv_encoder->dcb->type) {
583         case OUTPUT_LVDS:
584                 BUG_ON(!nv_connector->native_mode);
585                 if (mode->hdisplay > nv_connector->native_mode->hdisplay ||
586                     mode->vdisplay > nv_connector->native_mode->vdisplay)
587                         return MODE_PANEL;
588
589                 min_clock = 0;
590                 max_clock = 400000;
591                 break;
592         case OUTPUT_TMDS:
593                 if ((dev_priv->card_type >= NV_50 && !nouveau_duallink) ||
594                     (dev_priv->card_type < NV_50 &&
595                      !nv_encoder->dcb->duallink_possible))
596                         max_clock = 165000;
597                 else
598                         max_clock = 330000;
599                 break;
600         case OUTPUT_ANALOG:
601                 max_clock = nv_encoder->dcb->crtconf.maxfreq;
602                 if (!max_clock)
603                         max_clock = 350000;
604                 break;
605         case OUTPUT_TV:
606                 return get_slave_funcs(nv_encoder)->
607                         mode_valid(to_drm_encoder(nv_encoder), mode);
608         case OUTPUT_DP:
609                 if (nv_encoder->dp.link_bw == DP_LINK_BW_2_7)
610                         max_clock = nv_encoder->dp.link_nr * 270000;
611                 else
612                         max_clock = nv_encoder->dp.link_nr * 162000;
613
614                 clock *= 3;
615                 break;
616         }
617
618         if (clock < min_clock)
619                 return MODE_CLOCK_LOW;
620
621         if (clock > max_clock)
622                 return MODE_CLOCK_HIGH;
623
624         return MODE_OK;
625 }
626
627 static struct drm_encoder *
628 nouveau_connector_best_encoder(struct drm_connector *connector)
629 {
630         struct nouveau_connector *nv_connector = nouveau_connector(connector);
631
632         if (nv_connector->detected_encoder)
633                 return to_drm_encoder(nv_connector->detected_encoder);
634
635         return NULL;
636 }
637
638 static const struct drm_connector_helper_funcs
639 nouveau_connector_helper_funcs = {
640         .get_modes = nouveau_connector_get_modes,
641         .mode_valid = nouveau_connector_mode_valid,
642         .best_encoder = nouveau_connector_best_encoder,
643 };
644
645 static const struct drm_connector_funcs
646 nouveau_connector_funcs = {
647         .dpms = drm_helper_connector_dpms,
648         .save = NULL,
649         .restore = NULL,
650         .detect = nouveau_connector_detect,
651         .destroy = nouveau_connector_destroy,
652         .fill_modes = drm_helper_probe_single_connector_modes,
653         .set_property = nouveau_connector_set_property,
654         .force = nouveau_connector_force
655 };
656
657 static int
658 nouveau_connector_create_lvds(struct drm_device *dev,
659                               struct drm_connector *connector)
660 {
661         struct nouveau_connector *nv_connector = nouveau_connector(connector);
662         struct drm_nouveau_private *dev_priv = dev->dev_private;
663         struct nouveau_i2c_chan *i2c = NULL;
664         struct nouveau_encoder *nv_encoder;
665         struct drm_display_mode native, *mode, *temp;
666         bool dummy, if_is_24bit = false;
667         int ret, flags;
668
669         nv_encoder = find_encoder_by_type(connector, OUTPUT_LVDS);
670         if (!nv_encoder)
671                 return -ENODEV;
672
673         ret = nouveau_bios_parse_lvds_table(dev, 0, &dummy, &if_is_24bit);
674         if (ret) {
675                 NV_ERROR(dev, "Error parsing LVDS table, disabling LVDS\n");
676                 return ret;
677         }
678         nv_connector->use_dithering = !if_is_24bit;
679
680         /* Firstly try getting EDID over DDC, if allowed and I2C channel
681          * is available.
682          */
683         if (!dev_priv->VBIOS.pub.fp_no_ddc && nv_encoder->dcb->i2c_index < 0xf)
684                 i2c = nouveau_i2c_find(dev, nv_encoder->dcb->i2c_index);
685
686         if (i2c) {
687                 nouveau_connector_ddc_prepare(connector, &flags);
688                 nv_connector->edid = drm_get_edid(connector, &i2c->adapter);
689                 nouveau_connector_ddc_finish(connector, flags);
690         }
691
692         /* If no EDID found above, and the VBIOS indicates a hardcoded
693          * modeline is avalilable for the panel, set it as the panel's
694          * native mode and exit.
695          */
696         if (!nv_connector->edid && nouveau_bios_fp_mode(dev, &native) &&
697              (nv_encoder->dcb->lvdsconf.use_straps_for_mode ||
698               dev_priv->VBIOS.pub.fp_no_ddc)) {
699                 nv_connector->native_mode = drm_mode_duplicate(dev, &native);
700                 goto out;
701         }
702
703         /* Still nothing, some VBIOS images have a hardcoded EDID block
704          * stored for the panel stored in them.
705          */
706         if (!nv_connector->edid && !nv_connector->native_mode &&
707             !dev_priv->VBIOS.pub.fp_no_ddc) {
708                 struct edid *edid =
709                         (struct edid *)nouveau_bios_embedded_edid(dev);
710                 if (edid) {
711                         nv_connector->edid = kmalloc(EDID_LENGTH, GFP_KERNEL);
712                         *(nv_connector->edid) = *edid;
713                 }
714         }
715
716         if (!nv_connector->edid)
717                 goto out;
718
719         /* We didn't find/use a panel mode from the VBIOS, so parse the EDID
720          * block and look for the preferred mode there.
721          */
722         ret = drm_add_edid_modes(connector, nv_connector->edid);
723         if (ret == 0)
724                 goto out;
725         nv_connector->detected_encoder = nv_encoder;
726         nv_connector->native_mode = nouveau_connector_native_mode(nv_connector);
727         list_for_each_entry_safe(mode, temp, &connector->probed_modes, head)
728                 drm_mode_remove(connector, mode);
729
730 out:
731         if (!nv_connector->native_mode) {
732                 NV_ERROR(dev, "LVDS present in DCB table, but couldn't "
733                               "determine its native mode.  Disabling.\n");
734                 return -ENODEV;
735         }
736
737         drm_mode_connector_update_edid_property(connector, nv_connector->edid);
738         return 0;
739 }
740
741 int
742 nouveau_connector_create(struct drm_device *dev, int index, int type)
743 {
744         struct drm_nouveau_private *dev_priv = dev->dev_private;
745         struct nouveau_connector *nv_connector = NULL;
746         struct drm_connector *connector;
747         struct drm_encoder *encoder;
748         int ret;
749
750         NV_DEBUG_KMS(dev, "\n");
751
752         nv_connector = kzalloc(sizeof(*nv_connector), GFP_KERNEL);
753         if (!nv_connector)
754                 return -ENOMEM;
755         nv_connector->dcb = nouveau_bios_connector_entry(dev, index);
756         connector = &nv_connector->base;
757
758         switch (type) {
759         case DRM_MODE_CONNECTOR_VGA:
760                 NV_INFO(dev, "Detected a VGA connector\n");
761                 break;
762         case DRM_MODE_CONNECTOR_DVID:
763                 NV_INFO(dev, "Detected a DVI-D connector\n");
764                 break;
765         case DRM_MODE_CONNECTOR_DVII:
766                 NV_INFO(dev, "Detected a DVI-I connector\n");
767                 break;
768         case DRM_MODE_CONNECTOR_LVDS:
769                 NV_INFO(dev, "Detected a LVDS connector\n");
770                 break;
771         case DRM_MODE_CONNECTOR_TV:
772                 NV_INFO(dev, "Detected a TV connector\n");
773                 break;
774         case DRM_MODE_CONNECTOR_DisplayPort:
775                 NV_INFO(dev, "Detected a DisplayPort connector\n");
776                 break;
777         default:
778                 NV_ERROR(dev, "Unknown connector, this is not good.\n");
779                 break;
780         }
781
782         /* defaults, will get overridden in detect() */
783         connector->interlace_allowed = false;
784         connector->doublescan_allowed = false;
785
786         drm_connector_init(dev, connector, &nouveau_connector_funcs, type);
787         drm_connector_helper_add(connector, &nouveau_connector_helper_funcs);
788
789         /* Init DVI-I specific properties */
790         if (type == DRM_MODE_CONNECTOR_DVII) {
791                 drm_mode_create_dvi_i_properties(dev);
792                 drm_connector_attach_property(connector, dev->mode_config.dvi_i_subconnector_property, 0);
793                 drm_connector_attach_property(connector, dev->mode_config.dvi_i_select_subconnector_property, 0);
794         }
795
796         if (type != DRM_MODE_CONNECTOR_LVDS)
797                 nv_connector->use_dithering = false;
798
799         if (type == DRM_MODE_CONNECTOR_DVID ||
800             type == DRM_MODE_CONNECTOR_DVII ||
801             type == DRM_MODE_CONNECTOR_LVDS ||
802             type == DRM_MODE_CONNECTOR_DisplayPort) {
803                 nv_connector->scaling_mode = DRM_MODE_SCALE_FULLSCREEN;
804
805                 drm_connector_attach_property(connector, dev->mode_config.scaling_mode_property,
806                                               nv_connector->scaling_mode);
807                 drm_connector_attach_property(connector, dev->mode_config.dithering_mode_property,
808                                               nv_connector->use_dithering ? DRM_MODE_DITHERING_ON
809                                               : DRM_MODE_DITHERING_OFF);
810
811         } else {
812                 nv_connector->scaling_mode = DRM_MODE_SCALE_NONE;
813
814                 if (type == DRM_MODE_CONNECTOR_VGA  &&
815                                 dev_priv->card_type >= NV_50) {
816                         drm_connector_attach_property(connector,
817                                         dev->mode_config.scaling_mode_property,
818                                         nv_connector->scaling_mode);
819                 }
820         }
821
822         /* attach encoders */
823         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
824                 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
825
826                 if (nv_encoder->dcb->connector != index)
827                         continue;
828
829                 if (get_slave_funcs(nv_encoder))
830                         get_slave_funcs(nv_encoder)->create_resources(encoder, connector);
831
832                 drm_mode_connector_attach_encoder(connector, encoder);
833         }
834
835         drm_sysfs_connector_add(connector);
836
837         if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS) {
838                 ret = nouveau_connector_create_lvds(dev, connector);
839                 if (ret) {
840                         connector->funcs->destroy(connector);
841                         return ret;
842                 }
843         }
844
845         return 0;
846 }