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