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