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