Merge branch 'timers-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[pandora-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;
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 (nv_connector->dcb->type == DCB_CONNECTOR_DVI_I) {
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 (nv_connector->dcb->type == DCB_CONNECTOR_LVDS)
240                 nv_encoder = find_encoder_by_type(connector, OUTPUT_LVDS);
241         if (nv_encoder && nv_connector->native_mode) {
242                 unsigned status = connector_status_connected;
243
244 #if defined(CONFIG_ACPI_BUTTON) || \
245         (defined(CONFIG_ACPI_BUTTON_MODULE) && defined(MODULE))
246                 if (!nouveau_ignorelid && !acpi_lid_open())
247                         status = connector_status_unknown;
248 #endif
249                 nouveau_connector_set_encoder(connector, nv_encoder);
250                 return status;
251         }
252
253         /* Cleanup the previous EDID block. */
254         if (nv_connector->edid) {
255                 drm_mode_connector_update_edid_property(connector, NULL);
256                 kfree(nv_connector->edid);
257                 nv_connector->edid = NULL;
258         }
259
260         i2c = nouveau_connector_ddc_detect(connector, &nv_encoder);
261         if (i2c) {
262                 nouveau_connector_ddc_prepare(connector, &flags);
263                 nv_connector->edid = drm_get_edid(connector, &i2c->adapter);
264                 nouveau_connector_ddc_finish(connector, flags);
265                 drm_mode_connector_update_edid_property(connector,
266                                                         nv_connector->edid);
267                 if (!nv_connector->edid) {
268                         NV_ERROR(dev, "DDC responded, but no EDID for %s\n",
269                                  drm_get_connector_name(connector));
270                         goto detect_analog;
271                 }
272
273                 if (nv_encoder->dcb->type == OUTPUT_DP &&
274                     !nouveau_dp_detect(to_drm_encoder(nv_encoder))) {
275                         NV_ERROR(dev, "Detected %s, but failed init\n",
276                                  drm_get_connector_name(connector));
277                         return connector_status_disconnected;
278                 }
279
280                 /* Override encoder type for DVI-I based on whether EDID
281                  * says the display is digital or analog, both use the
282                  * same i2c channel so the value returned from ddc_detect
283                  * isn't necessarily correct.
284                  */
285                 if (nv_connector->dcb->type == DCB_CONNECTOR_DVI_I) {
286                         if (nv_connector->edid->input & DRM_EDID_INPUT_DIGITAL)
287                                 type = OUTPUT_TMDS;
288                         else
289                                 type = OUTPUT_ANALOG;
290
291                         nv_encoder = find_encoder_by_type(connector, type);
292                         if (!nv_encoder) {
293                                 NV_ERROR(dev, "Detected %d encoder on %s, "
294                                               "but no object!\n", type,
295                                          drm_get_connector_name(connector));
296                                 return connector_status_disconnected;
297                         }
298                 }
299
300                 nouveau_connector_set_encoder(connector, nv_encoder);
301                 return connector_status_connected;
302         }
303
304 detect_analog:
305         nv_encoder = find_encoder_by_type(connector, OUTPUT_ANALOG);
306         if (!nv_encoder && !nouveau_tv_disable)
307                 nv_encoder = find_encoder_by_type(connector, OUTPUT_TV);
308         if (nv_encoder) {
309                 struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
310                 struct drm_encoder_helper_funcs *helper =
311                                                 encoder->helper_private;
312
313                 if (helper->detect(encoder, connector) ==
314                                                 connector_status_connected) {
315                         nouveau_connector_set_encoder(connector, nv_encoder);
316                         return connector_status_connected;
317                 }
318
319         }
320
321         return connector_status_disconnected;
322 }
323
324 static void
325 nouveau_connector_force(struct drm_connector *connector)
326 {
327         struct nouveau_connector *nv_connector = nouveau_connector(connector);
328         struct nouveau_encoder *nv_encoder;
329         int type;
330
331         if (nv_connector->dcb->type == DCB_CONNECTOR_DVI_I) {
332                 if (connector->force == DRM_FORCE_ON_DIGITAL)
333                         type = OUTPUT_TMDS;
334                 else
335                         type = OUTPUT_ANALOG;
336         } else
337                 type = OUTPUT_ANY;
338
339         nv_encoder = find_encoder_by_type(connector, type);
340         if (!nv_encoder) {
341                 NV_ERROR(connector->dev, "can't find encoder to force %s on!\n",
342                          drm_get_connector_name(connector));
343                 connector->status = connector_status_disconnected;
344                 return;
345         }
346
347         nouveau_connector_set_encoder(connector, nv_encoder);
348 }
349
350 static int
351 nouveau_connector_set_property(struct drm_connector *connector,
352                                struct drm_property *property, uint64_t value)
353 {
354         struct nouveau_connector *nv_connector = nouveau_connector(connector);
355         struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
356         struct drm_device *dev = connector->dev;
357         int ret;
358
359         /* Scaling mode */
360         if (property == dev->mode_config.scaling_mode_property) {
361                 struct nouveau_crtc *nv_crtc = NULL;
362                 bool modeset = false;
363
364                 switch (value) {
365                 case DRM_MODE_SCALE_NONE:
366                 case DRM_MODE_SCALE_FULLSCREEN:
367                 case DRM_MODE_SCALE_CENTER:
368                 case DRM_MODE_SCALE_ASPECT:
369                         break;
370                 default:
371                         return -EINVAL;
372                 }
373
374                 /* LVDS always needs gpu scaling */
375                 if (nv_connector->dcb->type == DCB_CONNECTOR_LVDS &&
376                     value == DRM_MODE_SCALE_NONE)
377                         return -EINVAL;
378
379                 /* Changing between GPU and panel scaling requires a full
380                  * modeset
381                  */
382                 if ((nv_connector->scaling_mode == DRM_MODE_SCALE_NONE) ||
383                     (value == DRM_MODE_SCALE_NONE))
384                         modeset = true;
385                 nv_connector->scaling_mode = value;
386
387                 if (connector->encoder && connector->encoder->crtc)
388                         nv_crtc = nouveau_crtc(connector->encoder->crtc);
389                 if (!nv_crtc)
390                         return 0;
391
392                 if (modeset || !nv_crtc->set_scale) {
393                         ret = drm_crtc_helper_set_mode(&nv_crtc->base,
394                                                         &nv_crtc->base.mode,
395                                                         nv_crtc->base.x,
396                                                         nv_crtc->base.y, NULL);
397                         if (!ret)
398                                 return -EINVAL;
399                 } else {
400                         ret = nv_crtc->set_scale(nv_crtc, value, true);
401                         if (ret)
402                                 return ret;
403                 }
404
405                 return 0;
406         }
407
408         /* Dithering */
409         if (property == dev->mode_config.dithering_mode_property) {
410                 struct nouveau_crtc *nv_crtc = NULL;
411
412                 if (value == DRM_MODE_DITHERING_ON)
413                         nv_connector->use_dithering = true;
414                 else
415                         nv_connector->use_dithering = false;
416
417                 if (connector->encoder && connector->encoder->crtc)
418                         nv_crtc = nouveau_crtc(connector->encoder->crtc);
419
420                 if (!nv_crtc || !nv_crtc->set_dither)
421                         return 0;
422
423                 return nv_crtc->set_dither(nv_crtc, nv_connector->use_dithering,
424                                            true);
425         }
426
427         if (nv_encoder && nv_encoder->dcb->type == OUTPUT_TV)
428                 return get_slave_funcs(nv_encoder)->
429                         set_property(to_drm_encoder(nv_encoder), connector, property, value);
430
431         return -EINVAL;
432 }
433
434 static struct drm_display_mode *
435 nouveau_connector_native_mode(struct nouveau_connector *connector)
436 {
437         struct drm_device *dev = connector->base.dev;
438         struct drm_display_mode *mode, *largest = NULL;
439         int high_w = 0, high_h = 0, high_v = 0;
440
441         /* Use preferred mode if there is one.. */
442         list_for_each_entry(mode, &connector->base.probed_modes, head) {
443                 if (mode->type & DRM_MODE_TYPE_PREFERRED) {
444                         NV_DEBUG_KMS(dev, "native mode from preferred\n");
445                         return drm_mode_duplicate(dev, mode);
446                 }
447         }
448
449         /* Otherwise, take the resolution with the largest width, then height,
450          * then vertical refresh
451          */
452         list_for_each_entry(mode, &connector->base.probed_modes, head) {
453                 if (mode->hdisplay < high_w)
454                         continue;
455
456                 if (mode->hdisplay == high_w && mode->vdisplay < high_h)
457                         continue;
458
459                 if (mode->hdisplay == high_w && mode->vdisplay == high_h &&
460                     mode->vrefresh < high_v)
461                         continue;
462
463                 high_w = mode->hdisplay;
464                 high_h = mode->vdisplay;
465                 high_v = mode->vrefresh;
466                 largest = mode;
467         }
468
469         NV_DEBUG_KMS(dev, "native mode from largest: %dx%d@%d\n",
470                       high_w, high_h, high_v);
471         return largest ? drm_mode_duplicate(dev, largest) : NULL;
472 }
473
474 struct moderec {
475         int hdisplay;
476         int vdisplay;
477 };
478
479 static struct moderec scaler_modes[] = {
480         { 1920, 1200 },
481         { 1920, 1080 },
482         { 1680, 1050 },
483         { 1600, 1200 },
484         { 1400, 1050 },
485         { 1280, 1024 },
486         { 1280, 960 },
487         { 1152, 864 },
488         { 1024, 768 },
489         { 800, 600 },
490         { 720, 400 },
491         { 640, 480 },
492         { 640, 400 },
493         { 640, 350 },
494         {}
495 };
496
497 static int
498 nouveau_connector_scaler_modes_add(struct drm_connector *connector)
499 {
500         struct nouveau_connector *nv_connector = nouveau_connector(connector);
501         struct drm_display_mode *native = nv_connector->native_mode, *m;
502         struct drm_device *dev = connector->dev;
503         struct moderec *mode = &scaler_modes[0];
504         int modes = 0;
505
506         if (!native)
507                 return 0;
508
509         while (mode->hdisplay) {
510                 if (mode->hdisplay <= native->hdisplay &&
511                     mode->vdisplay <= native->vdisplay) {
512                         m = drm_cvt_mode(dev, mode->hdisplay, mode->vdisplay,
513                                          drm_mode_vrefresh(native), false,
514                                          false, false);
515                         if (!m)
516                                 continue;
517
518                         m->type |= DRM_MODE_TYPE_DRIVER;
519
520                         drm_mode_probed_add(connector, m);
521                         modes++;
522                 }
523
524                 mode++;
525         }
526
527         return modes;
528 }
529
530 static int
531 nouveau_connector_get_modes(struct drm_connector *connector)
532 {
533         struct drm_device *dev = connector->dev;
534         struct nouveau_connector *nv_connector = nouveau_connector(connector);
535         struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
536         int ret = 0;
537
538         /* If we're not LVDS, destroy the previous native mode, the attached
539          * monitor could have changed.
540          */
541         if (nv_connector->dcb->type != DCB_CONNECTOR_LVDS &&
542             nv_connector->native_mode) {
543                 drm_mode_destroy(dev, nv_connector->native_mode);
544                 nv_connector->native_mode = NULL;
545         }
546
547         if (nv_connector->edid)
548                 ret = drm_add_edid_modes(connector, nv_connector->edid);
549
550         /* Find the native mode if this is a digital panel, if we didn't
551          * find any modes through DDC previously add the native mode to
552          * the list of modes.
553          */
554         if (!nv_connector->native_mode)
555                 nv_connector->native_mode =
556                         nouveau_connector_native_mode(nv_connector);
557         if (ret == 0 && nv_connector->native_mode) {
558                 struct drm_display_mode *mode;
559
560                 mode = drm_mode_duplicate(dev, nv_connector->native_mode);
561                 drm_mode_probed_add(connector, mode);
562                 ret = 1;
563         }
564
565         if (nv_encoder->dcb->type == OUTPUT_TV)
566                 ret = get_slave_funcs(nv_encoder)->
567                         get_modes(to_drm_encoder(nv_encoder), connector);
568
569         if (nv_encoder->dcb->type == OUTPUT_LVDS)
570                 ret += nouveau_connector_scaler_modes_add(connector);
571
572         return ret;
573 }
574
575 static int
576 nouveau_connector_mode_valid(struct drm_connector *connector,
577                              struct drm_display_mode *mode)
578 {
579         struct drm_nouveau_private *dev_priv = connector->dev->dev_private;
580         struct nouveau_connector *nv_connector = nouveau_connector(connector);
581         struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
582         unsigned min_clock = 25000, max_clock = min_clock;
583         unsigned clock = mode->clock;
584
585         switch (nv_encoder->dcb->type) {
586         case OUTPUT_LVDS:
587                 BUG_ON(!nv_connector->native_mode);
588                 if (mode->hdisplay > nv_connector->native_mode->hdisplay ||
589                     mode->vdisplay > nv_connector->native_mode->vdisplay)
590                         return MODE_PANEL;
591
592                 min_clock = 0;
593                 max_clock = 400000;
594                 break;
595         case OUTPUT_TMDS:
596                 if ((dev_priv->card_type >= NV_50 && !nouveau_duallink) ||
597                     (dev_priv->card_type < NV_50 &&
598                      !nv_encoder->dcb->duallink_possible))
599                         max_clock = 165000;
600                 else
601                         max_clock = 330000;
602                 break;
603         case OUTPUT_ANALOG:
604                 max_clock = nv_encoder->dcb->crtconf.maxfreq;
605                 if (!max_clock)
606                         max_clock = 350000;
607                 break;
608         case OUTPUT_TV:
609                 return get_slave_funcs(nv_encoder)->
610                         mode_valid(to_drm_encoder(nv_encoder), mode);
611         case OUTPUT_DP:
612                 if (nv_encoder->dp.link_bw == DP_LINK_BW_2_7)
613                         max_clock = nv_encoder->dp.link_nr * 270000;
614                 else
615                         max_clock = nv_encoder->dp.link_nr * 162000;
616
617                 clock *= 3;
618                 break;
619         default:
620                 BUG_ON(1);
621                 return MODE_BAD;
622         }
623
624         if (clock < min_clock)
625                 return MODE_CLOCK_LOW;
626
627         if (clock > max_clock)
628                 return MODE_CLOCK_HIGH;
629
630         return MODE_OK;
631 }
632
633 static struct drm_encoder *
634 nouveau_connector_best_encoder(struct drm_connector *connector)
635 {
636         struct nouveau_connector *nv_connector = nouveau_connector(connector);
637
638         if (nv_connector->detected_encoder)
639                 return to_drm_encoder(nv_connector->detected_encoder);
640
641         return NULL;
642 }
643
644 static const struct drm_connector_helper_funcs
645 nouveau_connector_helper_funcs = {
646         .get_modes = nouveau_connector_get_modes,
647         .mode_valid = nouveau_connector_mode_valid,
648         .best_encoder = nouveau_connector_best_encoder,
649 };
650
651 static const struct drm_connector_funcs
652 nouveau_connector_funcs = {
653         .dpms = drm_helper_connector_dpms,
654         .save = NULL,
655         .restore = NULL,
656         .detect = nouveau_connector_detect,
657         .destroy = nouveau_connector_destroy,
658         .fill_modes = drm_helper_probe_single_connector_modes,
659         .set_property = nouveau_connector_set_property,
660         .force = nouveau_connector_force
661 };
662
663 static int
664 nouveau_connector_create_lvds(struct drm_device *dev,
665                               struct drm_connector *connector)
666 {
667         struct nouveau_connector *nv_connector = nouveau_connector(connector);
668         struct drm_nouveau_private *dev_priv = dev->dev_private;
669         struct nouveau_i2c_chan *i2c = NULL;
670         struct nouveau_encoder *nv_encoder;
671         struct drm_display_mode native, *mode, *temp;
672         bool dummy, if_is_24bit = false;
673         int ret, flags;
674
675         nv_encoder = find_encoder_by_type(connector, OUTPUT_LVDS);
676         if (!nv_encoder)
677                 return -ENODEV;
678
679         ret = nouveau_bios_parse_lvds_table(dev, 0, &dummy, &if_is_24bit);
680         if (ret) {
681                 NV_ERROR(dev, "Error parsing LVDS table, disabling LVDS\n");
682                 return ret;
683         }
684         nv_connector->use_dithering = !if_is_24bit;
685
686         /* Firstly try getting EDID over DDC, if allowed and I2C channel
687          * is available.
688          */
689         if (!dev_priv->vbios.fp_no_ddc && nv_encoder->dcb->i2c_index < 0xf)
690                 i2c = nouveau_i2c_find(dev, nv_encoder->dcb->i2c_index);
691
692         if (i2c) {
693                 nouveau_connector_ddc_prepare(connector, &flags);
694                 nv_connector->edid = drm_get_edid(connector, &i2c->adapter);
695                 nouveau_connector_ddc_finish(connector, flags);
696         }
697
698         /* If no EDID found above, and the VBIOS indicates a hardcoded
699          * modeline is avalilable for the panel, set it as the panel's
700          * native mode and exit.
701          */
702         if (!nv_connector->edid && nouveau_bios_fp_mode(dev, &native) &&
703              (nv_encoder->dcb->lvdsconf.use_straps_for_mode ||
704               dev_priv->vbios.fp_no_ddc)) {
705                 nv_connector->native_mode = drm_mode_duplicate(dev, &native);
706                 goto out;
707         }
708
709         /* Still nothing, some VBIOS images have a hardcoded EDID block
710          * stored for the panel stored in them.
711          */
712         if (!nv_connector->edid && !nv_connector->native_mode &&
713             !dev_priv->vbios.fp_no_ddc) {
714                 struct edid *edid =
715                         (struct edid *)nouveau_bios_embedded_edid(dev);
716                 if (edid) {
717                         nv_connector->edid = kmalloc(EDID_LENGTH, GFP_KERNEL);
718                         *(nv_connector->edid) = *edid;
719                 }
720         }
721
722         if (!nv_connector->edid)
723                 goto out;
724
725         /* We didn't find/use a panel mode from the VBIOS, so parse the EDID
726          * block and look for the preferred mode there.
727          */
728         ret = drm_add_edid_modes(connector, nv_connector->edid);
729         if (ret == 0)
730                 goto out;
731         nv_connector->detected_encoder = nv_encoder;
732         nv_connector->native_mode = nouveau_connector_native_mode(nv_connector);
733         list_for_each_entry_safe(mode, temp, &connector->probed_modes, head)
734                 drm_mode_remove(connector, mode);
735
736 out:
737         if (!nv_connector->native_mode) {
738                 NV_ERROR(dev, "LVDS present in DCB table, but couldn't "
739                               "determine its native mode.  Disabling.\n");
740                 return -ENODEV;
741         }
742
743         drm_mode_connector_update_edid_property(connector, nv_connector->edid);
744         return 0;
745 }
746
747 int
748 nouveau_connector_create(struct drm_device *dev,
749                          struct dcb_connector_table_entry *dcb)
750 {
751         struct drm_nouveau_private *dev_priv = dev->dev_private;
752         struct nouveau_connector *nv_connector = NULL;
753         struct drm_connector *connector;
754         struct drm_encoder *encoder;
755         int ret, type;
756
757         NV_DEBUG_KMS(dev, "\n");
758
759         switch (dcb->type) {
760         case DCB_CONNECTOR_NONE:
761                 return 0;
762         case DCB_CONNECTOR_VGA:
763                 NV_INFO(dev, "Detected a VGA connector\n");
764                 type = DRM_MODE_CONNECTOR_VGA;
765                 break;
766         case DCB_CONNECTOR_TV_0:
767         case DCB_CONNECTOR_TV_1:
768         case DCB_CONNECTOR_TV_3:
769                 NV_INFO(dev, "Detected a TV connector\n");
770                 type = DRM_MODE_CONNECTOR_TV;
771                 break;
772         case DCB_CONNECTOR_DVI_I:
773                 NV_INFO(dev, "Detected a DVI-I connector\n");
774                 type = DRM_MODE_CONNECTOR_DVII;
775                 break;
776         case DCB_CONNECTOR_DVI_D:
777                 NV_INFO(dev, "Detected a DVI-D connector\n");
778                 type = DRM_MODE_CONNECTOR_DVID;
779                 break;
780         case DCB_CONNECTOR_HDMI_0:
781         case DCB_CONNECTOR_HDMI_1:
782                 NV_INFO(dev, "Detected a HDMI connector\n");
783                 type = DRM_MODE_CONNECTOR_HDMIA;
784                 break;
785         case DCB_CONNECTOR_LVDS:
786                 NV_INFO(dev, "Detected a LVDS connector\n");
787                 type = DRM_MODE_CONNECTOR_LVDS;
788                 break;
789         case DCB_CONNECTOR_DP:
790                 NV_INFO(dev, "Detected a DisplayPort connector\n");
791                 type = DRM_MODE_CONNECTOR_DisplayPort;
792                 break;
793         case DCB_CONNECTOR_eDP:
794                 NV_INFO(dev, "Detected an eDP connector\n");
795                 type = DRM_MODE_CONNECTOR_eDP;
796                 break;
797         default:
798                 NV_ERROR(dev, "unknown connector type: 0x%02x!!\n", dcb->type);
799                 return -EINVAL;
800         }
801
802         nv_connector = kzalloc(sizeof(*nv_connector), GFP_KERNEL);
803         if (!nv_connector)
804                 return -ENOMEM;
805         nv_connector->dcb = dcb;
806         connector = &nv_connector->base;
807
808         /* defaults, will get overridden in detect() */
809         connector->interlace_allowed = false;
810         connector->doublescan_allowed = false;
811
812         drm_connector_init(dev, connector, &nouveau_connector_funcs, type);
813         drm_connector_helper_add(connector, &nouveau_connector_helper_funcs);
814
815         /* attach encoders */
816         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
817                 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
818
819                 if (nv_encoder->dcb->connector != dcb->index)
820                         continue;
821
822                 if (get_slave_funcs(nv_encoder))
823                         get_slave_funcs(nv_encoder)->create_resources(encoder, connector);
824
825                 drm_mode_connector_attach_encoder(connector, encoder);
826         }
827
828         if (!connector->encoder_ids[0]) {
829                 NV_WARN(dev, "  no encoders, ignoring\n");
830                 drm_connector_cleanup(connector);
831                 kfree(connector);
832                 return 0;
833         }
834
835         /* Init DVI-I specific properties */
836         if (dcb->type == DCB_CONNECTOR_DVI_I) {
837                 drm_mode_create_dvi_i_properties(dev);
838                 drm_connector_attach_property(connector, dev->mode_config.dvi_i_subconnector_property, 0);
839                 drm_connector_attach_property(connector, dev->mode_config.dvi_i_select_subconnector_property, 0);
840         }
841
842         if (dcb->type != DCB_CONNECTOR_LVDS)
843                 nv_connector->use_dithering = false;
844
845         switch (dcb->type) {
846         case DCB_CONNECTOR_VGA:
847                 connector->polled = DRM_CONNECTOR_POLL_CONNECT;
848                 if (dev_priv->card_type >= NV_50) {
849                         drm_connector_attach_property(connector,
850                                         dev->mode_config.scaling_mode_property,
851                                         nv_connector->scaling_mode);
852                 }
853                 /* fall-through */
854         case DCB_CONNECTOR_TV_0:
855         case DCB_CONNECTOR_TV_1:
856         case DCB_CONNECTOR_TV_3:
857                 nv_connector->scaling_mode = DRM_MODE_SCALE_NONE;
858                 break;
859         case DCB_CONNECTOR_DP:
860         case DCB_CONNECTOR_eDP:
861         case DCB_CONNECTOR_HDMI_0:
862         case DCB_CONNECTOR_HDMI_1:
863         case DCB_CONNECTOR_DVI_I:
864         case DCB_CONNECTOR_DVI_D:
865                 if (dev_priv->card_type >= NV_50)
866                         connector->polled = DRM_CONNECTOR_POLL_HPD;
867                 else
868                         connector->polled = DRM_CONNECTOR_POLL_CONNECT;
869                 /* fall-through */
870         default:
871                 nv_connector->scaling_mode = DRM_MODE_SCALE_FULLSCREEN;
872
873                 drm_connector_attach_property(connector,
874                                 dev->mode_config.scaling_mode_property,
875                                 nv_connector->scaling_mode);
876                 drm_connector_attach_property(connector,
877                                 dev->mode_config.dithering_mode_property,
878                                 nv_connector->use_dithering ?
879                                 DRM_MODE_DITHERING_ON : DRM_MODE_DITHERING_OFF);
880                 break;
881         }
882
883         drm_sysfs_connector_add(connector);
884
885         if (dcb->type == DCB_CONNECTOR_LVDS) {
886                 ret = nouveau_connector_create_lvds(dev, connector);
887                 if (ret) {
888                         connector->funcs->destroy(connector);
889                         return ret;
890                 }
891         }
892
893         return 0;
894 }