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