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