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