]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/gpu/drm/nouveau/nouveau_connector.c
5dbf2e45993d722edc4a1248b6b371abc4f8f956
[karo-tx-linux.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 #include "nouveau_acpi.h"
40
41 #include <subdev/bios/gpio.h>
42
43 static void nouveau_connector_hotplug(void *, int);
44
45 struct nouveau_encoder *
46 find_encoder(struct drm_connector *connector, int type)
47 {
48         struct drm_device *dev = connector->dev;
49         struct nouveau_encoder *nv_encoder;
50         struct drm_mode_object *obj;
51         int i, id;
52
53         for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
54                 id = connector->encoder_ids[i];
55                 if (!id)
56                         break;
57
58                 obj = drm_mode_object_find(dev, id, DRM_MODE_OBJECT_ENCODER);
59                 if (!obj)
60                         continue;
61                 nv_encoder = nouveau_encoder(obj_to_encoder(obj));
62
63                 if (type == DCB_OUTPUT_ANY || nv_encoder->dcb->type == type)
64                         return nv_encoder;
65         }
66
67         return NULL;
68 }
69
70 struct nouveau_connector *
71 nouveau_encoder_connector_get(struct nouveau_encoder *encoder)
72 {
73         struct drm_device *dev = to_drm_encoder(encoder)->dev;
74         struct drm_connector *drm_connector;
75
76         list_for_each_entry(drm_connector, &dev->mode_config.connector_list, head) {
77                 if (drm_connector->encoder == to_drm_encoder(encoder))
78                         return nouveau_connector(drm_connector);
79         }
80
81         return NULL;
82 }
83
84 static void
85 nouveau_connector_destroy(struct drm_connector *connector)
86 {
87         struct nouveau_connector *nv_connector = nouveau_connector(connector);
88         struct drm_nouveau_private *dev_priv;
89         struct drm_device *dev;
90
91         if (!nv_connector)
92                 return;
93
94         dev = nv_connector->base.dev;
95         dev_priv = dev->dev_private;
96         NV_DEBUG_KMS(dev, "\n");
97
98         if (nv_connector->hpd != DCB_GPIO_UNUSED) {
99                 nouveau_gpio_isr_del(dev, 0, nv_connector->hpd, 0xff,
100                                      nouveau_connector_hotplug, connector);
101         }
102
103         kfree(nv_connector->edid);
104         drm_sysfs_connector_remove(connector);
105         drm_connector_cleanup(connector);
106         kfree(connector);
107 }
108
109 static struct nouveau_i2c_port *
110 nouveau_connector_ddc_detect(struct drm_connector *connector,
111                              struct nouveau_encoder **pnv_encoder)
112 {
113         struct drm_device *dev = connector->dev;
114         int i;
115
116         for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
117                 struct nouveau_i2c_port *i2c = NULL;
118                 struct nouveau_encoder *nv_encoder;
119                 struct drm_mode_object *obj;
120                 int id;
121
122                 id = connector->encoder_ids[i];
123                 if (!id)
124                         break;
125
126                 obj = drm_mode_object_find(dev, id, DRM_MODE_OBJECT_ENCODER);
127                 if (!obj)
128                         continue;
129                 nv_encoder = nouveau_encoder(obj_to_encoder(obj));
130
131                 if (nv_encoder->dcb->i2c_index < 0xf)
132                         i2c = nouveau_i2c_find(dev, nv_encoder->dcb->i2c_index);
133
134                 if (i2c && nouveau_probe_i2c_addr(i2c, 0x50)) {
135                         *pnv_encoder = nv_encoder;
136                         return i2c;
137                 }
138         }
139
140         return NULL;
141 }
142
143 static struct nouveau_encoder *
144 nouveau_connector_of_detect(struct drm_connector *connector)
145 {
146 #ifdef __powerpc__
147         struct drm_device *dev = connector->dev;
148         struct nouveau_connector *nv_connector = nouveau_connector(connector);
149         struct nouveau_encoder *nv_encoder;
150         struct device_node *cn, *dn = pci_device_to_OF_node(dev->pdev);
151
152         if (!dn ||
153             !((nv_encoder = find_encoder(connector, DCB_OUTPUT_TMDS)) ||
154               (nv_encoder = find_encoder(connector, DCB_OUTPUT_ANALOG))))
155                 return NULL;
156
157         for_each_child_of_node(dn, cn) {
158                 const char *name = of_get_property(cn, "name", NULL);
159                 const void *edid = of_get_property(cn, "EDID", NULL);
160                 int idx = name ? name[strlen(name) - 1] - 'A' : 0;
161
162                 if (nv_encoder->dcb->i2c_index == idx && edid) {
163                         nv_connector->edid =
164                                 kmemdup(edid, EDID_LENGTH, GFP_KERNEL);
165                         of_node_put(cn);
166                         return nv_encoder;
167                 }
168         }
169 #endif
170         return NULL;
171 }
172
173 static void
174 nouveau_connector_set_encoder(struct drm_connector *connector,
175                               struct nouveau_encoder *nv_encoder)
176 {
177         struct nouveau_connector *nv_connector = nouveau_connector(connector);
178         struct drm_nouveau_private *dev_priv = connector->dev->dev_private;
179         struct drm_device *dev = connector->dev;
180
181         if (nv_connector->detected_encoder == nv_encoder)
182                 return;
183         nv_connector->detected_encoder = nv_encoder;
184
185         if (dev_priv->card_type >= NV_50) {
186                 connector->interlace_allowed = true;
187                 connector->doublescan_allowed = true;
188         } else
189         if (nv_encoder->dcb->type == DCB_OUTPUT_LVDS ||
190             nv_encoder->dcb->type == DCB_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->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 == DCB_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, bool force)
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_encoder *nv_partner;
221         struct nouveau_i2c_port *i2c;
222         int type;
223
224         /* Cleanup the previous EDID block. */
225         if (nv_connector->edid) {
226                 drm_mode_connector_update_edid_property(connector, NULL);
227                 kfree(nv_connector->edid);
228                 nv_connector->edid = NULL;
229         }
230
231         i2c = nouveau_connector_ddc_detect(connector, &nv_encoder);
232         if (i2c) {
233                 nv_connector->edid = drm_get_edid(connector, nouveau_i2c_adapter(i2c));
234                 drm_mode_connector_update_edid_property(connector,
235                                                         nv_connector->edid);
236                 if (!nv_connector->edid) {
237                         NV_ERROR(dev, "DDC responded, but no EDID for %s\n",
238                                  drm_get_connector_name(connector));
239                         goto detect_analog;
240                 }
241
242                 if (nv_encoder->dcb->type == DCB_OUTPUT_DP &&
243                     !nouveau_dp_detect(to_drm_encoder(nv_encoder))) {
244                         NV_ERROR(dev, "Detected %s, but failed init\n",
245                                  drm_get_connector_name(connector));
246                         return connector_status_disconnected;
247                 }
248
249                 /* Override encoder type for DVI-I based on whether EDID
250                  * says the display is digital or analog, both use the
251                  * same i2c channel so the value returned from ddc_detect
252                  * isn't necessarily correct.
253                  */
254                 nv_partner = NULL;
255                 if (nv_encoder->dcb->type == DCB_OUTPUT_TMDS)
256                         nv_partner = find_encoder(connector, DCB_OUTPUT_ANALOG);
257                 if (nv_encoder->dcb->type == DCB_OUTPUT_ANALOG)
258                         nv_partner = find_encoder(connector, DCB_OUTPUT_TMDS);
259
260                 if (nv_partner && ((nv_encoder->dcb->type == DCB_OUTPUT_ANALOG &&
261                                     nv_partner->dcb->type == DCB_OUTPUT_TMDS) ||
262                                    (nv_encoder->dcb->type == DCB_OUTPUT_TMDS &&
263                                     nv_partner->dcb->type == DCB_OUTPUT_ANALOG))) {
264                         if (nv_connector->edid->input & DRM_EDID_INPUT_DIGITAL)
265                                 type = DCB_OUTPUT_TMDS;
266                         else
267                                 type = DCB_OUTPUT_ANALOG;
268
269                         nv_encoder = find_encoder(connector, type);
270                 }
271
272                 nouveau_connector_set_encoder(connector, nv_encoder);
273                 return connector_status_connected;
274         }
275
276         nv_encoder = nouveau_connector_of_detect(connector);
277         if (nv_encoder) {
278                 nouveau_connector_set_encoder(connector, nv_encoder);
279                 return connector_status_connected;
280         }
281
282 detect_analog:
283         nv_encoder = find_encoder(connector, DCB_OUTPUT_ANALOG);
284         if (!nv_encoder && !nouveau_tv_disable)
285                 nv_encoder = find_encoder(connector, DCB_OUTPUT_TV);
286         if (nv_encoder && force) {
287                 struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
288                 struct drm_encoder_helper_funcs *helper =
289                                                 encoder->helper_private;
290
291                 if (helper->detect(encoder, connector) ==
292                                                 connector_status_connected) {
293                         nouveau_connector_set_encoder(connector, nv_encoder);
294                         return connector_status_connected;
295                 }
296
297         }
298
299         return connector_status_disconnected;
300 }
301
302 static enum drm_connector_status
303 nouveau_connector_detect_lvds(struct drm_connector *connector, bool force)
304 {
305         struct drm_device *dev = connector->dev;
306         struct drm_nouveau_private *dev_priv = dev->dev_private;
307         struct nouveau_connector *nv_connector = nouveau_connector(connector);
308         struct nouveau_encoder *nv_encoder = NULL;
309         enum drm_connector_status status = connector_status_disconnected;
310
311         /* Cleanup the previous EDID block. */
312         if (nv_connector->edid) {
313                 drm_mode_connector_update_edid_property(connector, NULL);
314                 kfree(nv_connector->edid);
315                 nv_connector->edid = NULL;
316         }
317
318         nv_encoder = find_encoder(connector, DCB_OUTPUT_LVDS);
319         if (!nv_encoder)
320                 return connector_status_disconnected;
321
322         /* Try retrieving EDID via DDC */
323         if (!dev_priv->vbios.fp_no_ddc) {
324                 status = nouveau_connector_detect(connector, force);
325                 if (status == connector_status_connected)
326                         goto out;
327         }
328
329         /* On some laptops (Sony, i'm looking at you) there appears to
330          * be no direct way of accessing the panel's EDID.  The only
331          * option available to us appears to be to ask ACPI for help..
332          *
333          * It's important this check's before trying straps, one of the
334          * said manufacturer's laptops are configured in such a way
335          * the nouveau decides an entry in the VBIOS FP mode table is
336          * valid - it's not (rh#613284)
337          */
338         if (nv_encoder->dcb->lvdsconf.use_acpi_for_edid) {
339                 if (!(nv_connector->edid = nouveau_acpi_edid(dev, connector))) {
340                         status = connector_status_connected;
341                         goto out;
342                 }
343         }
344
345         /* If no EDID found above, and the VBIOS indicates a hardcoded
346          * modeline is avalilable for the panel, set it as the panel's
347          * native mode and exit.
348          */
349         if (nouveau_bios_fp_mode(dev, NULL) && (dev_priv->vbios.fp_no_ddc ||
350             nv_encoder->dcb->lvdsconf.use_straps_for_mode)) {
351                 status = connector_status_connected;
352                 goto out;
353         }
354
355         /* Still nothing, some VBIOS images have a hardcoded EDID block
356          * stored for the panel stored in them.
357          */
358         if (!dev_priv->vbios.fp_no_ddc) {
359                 struct edid *edid =
360                         (struct edid *)nouveau_bios_embedded_edid(dev);
361                 if (edid) {
362                         nv_connector->edid = kmalloc(EDID_LENGTH, GFP_KERNEL);
363                         *(nv_connector->edid) = *edid;
364                         status = connector_status_connected;
365                 }
366         }
367
368 out:
369 #if defined(CONFIG_ACPI_BUTTON) || \
370         (defined(CONFIG_ACPI_BUTTON_MODULE) && defined(MODULE))
371         if (status == connector_status_connected &&
372             !nouveau_ignorelid && !acpi_lid_open())
373                 status = connector_status_unknown;
374 #endif
375
376         drm_mode_connector_update_edid_property(connector, nv_connector->edid);
377         nouveau_connector_set_encoder(connector, nv_encoder);
378         return status;
379 }
380
381 static void
382 nouveau_connector_force(struct drm_connector *connector)
383 {
384         struct nouveau_connector *nv_connector = nouveau_connector(connector);
385         struct nouveau_encoder *nv_encoder;
386         int type;
387
388         if (nv_connector->type == DCB_CONNECTOR_DVI_I) {
389                 if (connector->force == DRM_FORCE_ON_DIGITAL)
390                         type = DCB_OUTPUT_TMDS;
391                 else
392                         type = DCB_OUTPUT_ANALOG;
393         } else
394                 type = DCB_OUTPUT_ANY;
395
396         nv_encoder = find_encoder(connector, type);
397         if (!nv_encoder) {
398                 NV_ERROR(connector->dev, "can't find encoder to force %s on!\n",
399                          drm_get_connector_name(connector));
400                 connector->status = connector_status_disconnected;
401                 return;
402         }
403
404         nouveau_connector_set_encoder(connector, nv_encoder);
405 }
406
407 static int
408 nouveau_connector_set_property(struct drm_connector *connector,
409                                struct drm_property *property, uint64_t value)
410 {
411         struct drm_nouveau_private *dev_priv = connector->dev->dev_private;
412         struct nouveau_display_engine *disp = &dev_priv->engine.display;
413         struct nouveau_connector *nv_connector = nouveau_connector(connector);
414         struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
415         struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
416         struct drm_device *dev = connector->dev;
417         struct nouveau_crtc *nv_crtc;
418         int ret;
419
420         nv_crtc = NULL;
421         if (connector->encoder && connector->encoder->crtc)
422                 nv_crtc = nouveau_crtc(connector->encoder->crtc);
423
424         /* Scaling mode */
425         if (property == dev->mode_config.scaling_mode_property) {
426                 bool modeset = false;
427
428                 switch (value) {
429                 case DRM_MODE_SCALE_NONE:
430                 case DRM_MODE_SCALE_FULLSCREEN:
431                 case DRM_MODE_SCALE_CENTER:
432                 case DRM_MODE_SCALE_ASPECT:
433                         break;
434                 default:
435                         return -EINVAL;
436                 }
437
438                 /* LVDS always needs gpu scaling */
439                 if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS &&
440                     value == DRM_MODE_SCALE_NONE)
441                         return -EINVAL;
442
443                 /* Changing between GPU and panel scaling requires a full
444                  * modeset
445                  */
446                 if ((nv_connector->scaling_mode == DRM_MODE_SCALE_NONE) ||
447                     (value == DRM_MODE_SCALE_NONE))
448                         modeset = true;
449                 nv_connector->scaling_mode = value;
450
451                 if (!nv_crtc)
452                         return 0;
453
454                 if (modeset || !nv_crtc->set_scale) {
455                         ret = drm_crtc_helper_set_mode(&nv_crtc->base,
456                                                         &nv_crtc->base.mode,
457                                                         nv_crtc->base.x,
458                                                         nv_crtc->base.y, NULL);
459                         if (!ret)
460                                 return -EINVAL;
461                 } else {
462                         ret = nv_crtc->set_scale(nv_crtc, true);
463                         if (ret)
464                                 return ret;
465                 }
466
467                 return 0;
468         }
469
470         /* Underscan */
471         if (property == disp->underscan_property) {
472                 if (nv_connector->underscan != value) {
473                         nv_connector->underscan = value;
474                         if (!nv_crtc || !nv_crtc->set_scale)
475                                 return 0;
476
477                         return nv_crtc->set_scale(nv_crtc, true);
478                 }
479
480                 return 0;
481         }
482
483         if (property == disp->underscan_hborder_property) {
484                 if (nv_connector->underscan_hborder != value) {
485                         nv_connector->underscan_hborder = value;
486                         if (!nv_crtc || !nv_crtc->set_scale)
487                                 return 0;
488
489                         return nv_crtc->set_scale(nv_crtc, true);
490                 }
491
492                 return 0;
493         }
494
495         if (property == disp->underscan_vborder_property) {
496                 if (nv_connector->underscan_vborder != value) {
497                         nv_connector->underscan_vborder = value;
498                         if (!nv_crtc || !nv_crtc->set_scale)
499                                 return 0;
500
501                         return nv_crtc->set_scale(nv_crtc, true);
502                 }
503
504                 return 0;
505         }
506
507         /* Dithering */
508         if (property == disp->dithering_mode) {
509                 nv_connector->dithering_mode = value;
510                 if (!nv_crtc || !nv_crtc->set_dither)
511                         return 0;
512
513                 return nv_crtc->set_dither(nv_crtc, true);
514         }
515
516         if (property == disp->dithering_depth) {
517                 nv_connector->dithering_depth = value;
518                 if (!nv_crtc || !nv_crtc->set_dither)
519                         return 0;
520
521                 return nv_crtc->set_dither(nv_crtc, true);
522         }
523
524         if (nv_crtc && nv_crtc->set_color_vibrance) {
525                 /* Hue */
526                 if (property == disp->vibrant_hue_property) {
527                         nv_crtc->vibrant_hue = value - 90;
528                         return nv_crtc->set_color_vibrance(nv_crtc, true);
529                 }
530                 /* Saturation */
531                 if (property == disp->color_vibrance_property) {
532                         nv_crtc->color_vibrance = value - 100;
533                         return nv_crtc->set_color_vibrance(nv_crtc, true);
534                 }
535         }
536
537         if (nv_encoder && nv_encoder->dcb->type == DCB_OUTPUT_TV)
538                 return get_slave_funcs(encoder)->set_property(
539                         encoder, connector, property, value);
540
541         return -EINVAL;
542 }
543
544 static struct drm_display_mode *
545 nouveau_connector_native_mode(struct drm_connector *connector)
546 {
547         struct drm_connector_helper_funcs *helper = connector->helper_private;
548         struct nouveau_connector *nv_connector = nouveau_connector(connector);
549         struct drm_device *dev = connector->dev;
550         struct drm_display_mode *mode, *largest = NULL;
551         int high_w = 0, high_h = 0, high_v = 0;
552
553         list_for_each_entry(mode, &nv_connector->base.probed_modes, head) {
554                 mode->vrefresh = drm_mode_vrefresh(mode);
555                 if (helper->mode_valid(connector, mode) != MODE_OK ||
556                     (mode->flags & DRM_MODE_FLAG_INTERLACE))
557                         continue;
558
559                 /* Use preferred mode if there is one.. */
560                 if (mode->type & DRM_MODE_TYPE_PREFERRED) {
561                         NV_DEBUG_KMS(dev, "native mode from preferred\n");
562                         return drm_mode_duplicate(dev, mode);
563                 }
564
565                 /* Otherwise, take the resolution with the largest width, then
566                  * height, then vertical refresh
567                  */
568                 if (mode->hdisplay < high_w)
569                         continue;
570
571                 if (mode->hdisplay == high_w && mode->vdisplay < high_h)
572                         continue;
573
574                 if (mode->hdisplay == high_w && mode->vdisplay == high_h &&
575                     mode->vrefresh < high_v)
576                         continue;
577
578                 high_w = mode->hdisplay;
579                 high_h = mode->vdisplay;
580                 high_v = mode->vrefresh;
581                 largest = mode;
582         }
583
584         NV_DEBUG_KMS(dev, "native mode from largest: %dx%d@%d\n",
585                       high_w, high_h, high_v);
586         return largest ? drm_mode_duplicate(dev, largest) : NULL;
587 }
588
589 struct moderec {
590         int hdisplay;
591         int vdisplay;
592 };
593
594 static struct moderec scaler_modes[] = {
595         { 1920, 1200 },
596         { 1920, 1080 },
597         { 1680, 1050 },
598         { 1600, 1200 },
599         { 1400, 1050 },
600         { 1280, 1024 },
601         { 1280, 960 },
602         { 1152, 864 },
603         { 1024, 768 },
604         { 800, 600 },
605         { 720, 400 },
606         { 640, 480 },
607         { 640, 400 },
608         { 640, 350 },
609         {}
610 };
611
612 static int
613 nouveau_connector_scaler_modes_add(struct drm_connector *connector)
614 {
615         struct nouveau_connector *nv_connector = nouveau_connector(connector);
616         struct drm_display_mode *native = nv_connector->native_mode, *m;
617         struct drm_device *dev = connector->dev;
618         struct moderec *mode = &scaler_modes[0];
619         int modes = 0;
620
621         if (!native)
622                 return 0;
623
624         while (mode->hdisplay) {
625                 if (mode->hdisplay <= native->hdisplay &&
626                     mode->vdisplay <= native->vdisplay) {
627                         m = drm_cvt_mode(dev, mode->hdisplay, mode->vdisplay,
628                                          drm_mode_vrefresh(native), false,
629                                          false, false);
630                         if (!m)
631                                 continue;
632
633                         m->type |= DRM_MODE_TYPE_DRIVER;
634
635                         drm_mode_probed_add(connector, m);
636                         modes++;
637                 }
638
639                 mode++;
640         }
641
642         return modes;
643 }
644
645 static void
646 nouveau_connector_detect_depth(struct drm_connector *connector)
647 {
648         struct drm_nouveau_private *dev_priv = connector->dev->dev_private;
649         struct nouveau_connector *nv_connector = nouveau_connector(connector);
650         struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
651         struct nvbios *bios = &dev_priv->vbios;
652         struct drm_display_mode *mode = nv_connector->native_mode;
653         bool duallink;
654
655         /* if the edid is feeling nice enough to provide this info, use it */
656         if (nv_connector->edid && connector->display_info.bpc)
657                 return;
658
659         /* EDID 1.4 is *supposed* to be supported on eDP, but, Apple... */
660         if (nv_connector->type == DCB_CONNECTOR_eDP) {
661                 connector->display_info.bpc = 6;
662                 return;
663         }
664
665         /* we're out of options unless we're LVDS, default to 8bpc */
666         if (nv_encoder->dcb->type != DCB_OUTPUT_LVDS) {
667                 connector->display_info.bpc = 8;
668                 return;
669         }
670
671         connector->display_info.bpc = 6;
672
673         /* LVDS: panel straps */
674         if (bios->fp_no_ddc) {
675                 if (bios->fp.if_is_24bit)
676                         connector->display_info.bpc = 8;
677                 return;
678         }
679
680         /* LVDS: DDC panel, need to first determine the number of links to
681          * know which if_is_24bit flag to check...
682          */
683         if (nv_connector->edid &&
684             nv_connector->type == DCB_CONNECTOR_LVDS_SPWG)
685                 duallink = ((u8 *)nv_connector->edid)[121] == 2;
686         else
687                 duallink = mode->clock >= bios->fp.duallink_transition_clk;
688
689         if ((!duallink && (bios->fp.strapless_is_24bit & 1)) ||
690             ( duallink && (bios->fp.strapless_is_24bit & 2)))
691                 connector->display_info.bpc = 8;
692 }
693
694 static int
695 nouveau_connector_get_modes(struct drm_connector *connector)
696 {
697         struct drm_device *dev = connector->dev;
698         struct drm_nouveau_private *dev_priv = dev->dev_private;
699         struct nouveau_connector *nv_connector = nouveau_connector(connector);
700         struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
701         struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
702         int ret = 0;
703
704         /* destroy the native mode, the attached monitor could have changed.
705          */
706         if (nv_connector->native_mode) {
707                 drm_mode_destroy(dev, nv_connector->native_mode);
708                 nv_connector->native_mode = NULL;
709         }
710
711         if (nv_connector->edid)
712                 ret = drm_add_edid_modes(connector, nv_connector->edid);
713         else
714         if (nv_encoder->dcb->type == DCB_OUTPUT_LVDS &&
715             (nv_encoder->dcb->lvdsconf.use_straps_for_mode ||
716              dev_priv->vbios.fp_no_ddc) && nouveau_bios_fp_mode(dev, NULL)) {
717                 struct drm_display_mode mode;
718
719                 nouveau_bios_fp_mode(dev, &mode);
720                 nv_connector->native_mode = drm_mode_duplicate(dev, &mode);
721         }
722
723         /* Determine display colour depth for everything except LVDS now,
724          * DP requires this before mode_valid() is called.
725          */
726         if (connector->connector_type != DRM_MODE_CONNECTOR_LVDS)
727                 nouveau_connector_detect_depth(connector);
728
729         /* Find the native mode if this is a digital panel, if we didn't
730          * find any modes through DDC previously add the native mode to
731          * the list of modes.
732          */
733         if (!nv_connector->native_mode)
734                 nv_connector->native_mode =
735                         nouveau_connector_native_mode(connector);
736         if (ret == 0 && nv_connector->native_mode) {
737                 struct drm_display_mode *mode;
738
739                 mode = drm_mode_duplicate(dev, nv_connector->native_mode);
740                 drm_mode_probed_add(connector, mode);
741                 ret = 1;
742         }
743
744         /* Determine LVDS colour depth, must happen after determining
745          * "native" mode as some VBIOS tables require us to use the
746          * pixel clock as part of the lookup...
747          */
748         if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS)
749                 nouveau_connector_detect_depth(connector);
750
751         if (nv_encoder->dcb->type == DCB_OUTPUT_TV)
752                 ret = get_slave_funcs(encoder)->get_modes(encoder, connector);
753
754         if (nv_connector->type == DCB_CONNECTOR_LVDS ||
755             nv_connector->type == DCB_CONNECTOR_LVDS_SPWG ||
756             nv_connector->type == DCB_CONNECTOR_eDP)
757                 ret += nouveau_connector_scaler_modes_add(connector);
758
759         return ret;
760 }
761
762 static unsigned
763 get_tmds_link_bandwidth(struct drm_connector *connector)
764 {
765         struct nouveau_connector *nv_connector = nouveau_connector(connector);
766         struct drm_nouveau_private *dev_priv = connector->dev->dev_private;
767         struct dcb_output *dcb = nv_connector->detected_encoder->dcb;
768
769         if (dcb->location != DCB_LOC_ON_CHIP ||
770             dev_priv->chipset >= 0x46)
771                 return 165000;
772         else if (dev_priv->chipset >= 0x40)
773                 return 155000;
774         else if (dev_priv->chipset >= 0x18)
775                 return 135000;
776         else
777                 return 112000;
778 }
779
780 static int
781 nouveau_connector_mode_valid(struct drm_connector *connector,
782                              struct drm_display_mode *mode)
783 {
784         struct nouveau_connector *nv_connector = nouveau_connector(connector);
785         struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
786         struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
787         unsigned min_clock = 25000, max_clock = min_clock;
788         unsigned clock = mode->clock;
789
790         switch (nv_encoder->dcb->type) {
791         case DCB_OUTPUT_LVDS:
792                 if (nv_connector->native_mode &&
793                     (mode->hdisplay > nv_connector->native_mode->hdisplay ||
794                      mode->vdisplay > nv_connector->native_mode->vdisplay))
795                         return MODE_PANEL;
796
797                 min_clock = 0;
798                 max_clock = 400000;
799                 break;
800         case DCB_OUTPUT_TMDS:
801                 max_clock = get_tmds_link_bandwidth(connector);
802                 if (nouveau_duallink && nv_encoder->dcb->duallink_possible)
803                         max_clock *= 2;
804                 break;
805         case DCB_OUTPUT_ANALOG:
806                 max_clock = nv_encoder->dcb->crtconf.maxfreq;
807                 if (!max_clock)
808                         max_clock = 350000;
809                 break;
810         case DCB_OUTPUT_TV:
811                 return get_slave_funcs(encoder)->mode_valid(encoder, mode);
812         case DCB_OUTPUT_DP:
813                 max_clock  = nv_encoder->dp.link_nr;
814                 max_clock *= nv_encoder->dp.link_bw;
815                 clock = clock * (connector->display_info.bpc * 3) / 10;
816                 break;
817         default:
818                 BUG_ON(1);
819                 return MODE_BAD;
820         }
821
822         if (clock < min_clock)
823                 return MODE_CLOCK_LOW;
824
825         if (clock > max_clock)
826                 return MODE_CLOCK_HIGH;
827
828         return MODE_OK;
829 }
830
831 static struct drm_encoder *
832 nouveau_connector_best_encoder(struct drm_connector *connector)
833 {
834         struct nouveau_connector *nv_connector = nouveau_connector(connector);
835
836         if (nv_connector->detected_encoder)
837                 return to_drm_encoder(nv_connector->detected_encoder);
838
839         return NULL;
840 }
841
842 static const struct drm_connector_helper_funcs
843 nouveau_connector_helper_funcs = {
844         .get_modes = nouveau_connector_get_modes,
845         .mode_valid = nouveau_connector_mode_valid,
846         .best_encoder = nouveau_connector_best_encoder,
847 };
848
849 static const struct drm_connector_funcs
850 nouveau_connector_funcs = {
851         .dpms = drm_helper_connector_dpms,
852         .save = NULL,
853         .restore = NULL,
854         .detect = nouveau_connector_detect,
855         .destroy = nouveau_connector_destroy,
856         .fill_modes = drm_helper_probe_single_connector_modes,
857         .set_property = nouveau_connector_set_property,
858         .force = nouveau_connector_force
859 };
860
861 static const struct drm_connector_funcs
862 nouveau_connector_funcs_lvds = {
863         .dpms = drm_helper_connector_dpms,
864         .save = NULL,
865         .restore = NULL,
866         .detect = nouveau_connector_detect_lvds,
867         .destroy = nouveau_connector_destroy,
868         .fill_modes = drm_helper_probe_single_connector_modes,
869         .set_property = nouveau_connector_set_property,
870         .force = nouveau_connector_force
871 };
872
873 static int
874 drm_conntype_from_dcb(enum dcb_connector_type dcb)
875 {
876         switch (dcb) {
877         case DCB_CONNECTOR_VGA      : return DRM_MODE_CONNECTOR_VGA;
878         case DCB_CONNECTOR_TV_0     :
879         case DCB_CONNECTOR_TV_1     :
880         case DCB_CONNECTOR_TV_3     : return DRM_MODE_CONNECTOR_TV;
881         case DCB_CONNECTOR_DMS59_0  :
882         case DCB_CONNECTOR_DMS59_1  :
883         case DCB_CONNECTOR_DVI_I    : return DRM_MODE_CONNECTOR_DVII;
884         case DCB_CONNECTOR_DVI_D    : return DRM_MODE_CONNECTOR_DVID;
885         case DCB_CONNECTOR_LVDS     :
886         case DCB_CONNECTOR_LVDS_SPWG: return DRM_MODE_CONNECTOR_LVDS;
887         case DCB_CONNECTOR_DMS59_DP0:
888         case DCB_CONNECTOR_DMS59_DP1:
889         case DCB_CONNECTOR_DP       : return DRM_MODE_CONNECTOR_DisplayPort;
890         case DCB_CONNECTOR_eDP      : return DRM_MODE_CONNECTOR_eDP;
891         case DCB_CONNECTOR_HDMI_0   :
892         case DCB_CONNECTOR_HDMI_1   : return DRM_MODE_CONNECTOR_HDMIA;
893         default:
894                 break;
895         }
896
897         return DRM_MODE_CONNECTOR_Unknown;
898 }
899
900 struct drm_connector *
901 nouveau_connector_create(struct drm_device *dev, int index)
902 {
903         const struct drm_connector_funcs *funcs = &nouveau_connector_funcs;
904         struct drm_nouveau_private *dev_priv = dev->dev_private;
905         struct nouveau_display_engine *disp = &dev_priv->engine.display;
906         struct nouveau_connector *nv_connector = NULL;
907         struct drm_connector *connector;
908         int type, ret = 0;
909         bool dummy;
910
911         NV_DEBUG_KMS(dev, "\n");
912
913         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
914                 nv_connector = nouveau_connector(connector);
915                 if (nv_connector->index == index)
916                         return connector;
917         }
918
919         nv_connector = kzalloc(sizeof(*nv_connector), GFP_KERNEL);
920         if (!nv_connector)
921                 return ERR_PTR(-ENOMEM);
922
923         connector = &nv_connector->base;
924         nv_connector->index = index;
925
926         /* attempt to parse vbios connector type and hotplug gpio */
927         nv_connector->dcb = olddcb_conn(dev, index);
928         if (nv_connector->dcb) {
929                 static const u8 hpd[16] = {
930                         0xff, 0x07, 0x08, 0xff, 0xff, 0x51, 0x52, 0xff,
931                         0xff, 0xff, 0xff, 0xff, 0xff, 0x5e, 0x5f, 0x60,
932                 };
933
934                 u32 entry = ROM16(nv_connector->dcb[0]);
935                 if (olddcb_conntab(dev)[3] >= 4)
936                         entry |= (u32)ROM16(nv_connector->dcb[2]) << 16;
937
938                 nv_connector->hpd = ffs((entry & 0x07033000) >> 12);
939                 nv_connector->hpd = hpd[nv_connector->hpd];
940
941                 nv_connector->type = nv_connector->dcb[0];
942                 if (drm_conntype_from_dcb(nv_connector->type) ==
943                                           DRM_MODE_CONNECTOR_Unknown) {
944                         NV_WARN(dev, "unknown connector type %02x\n",
945                                 nv_connector->type);
946                         nv_connector->type = DCB_CONNECTOR_NONE;
947                 }
948
949                 /* Gigabyte NX85T */
950                 if (nv_match_device(dev, 0x0421, 0x1458, 0x344c)) {
951                         if (nv_connector->type == DCB_CONNECTOR_HDMI_1)
952                                 nv_connector->type = DCB_CONNECTOR_DVI_I;
953                 }
954
955                 /* Gigabyte GV-NX86T512H */
956                 if (nv_match_device(dev, 0x0402, 0x1458, 0x3455)) {
957                         if (nv_connector->type == DCB_CONNECTOR_HDMI_1)
958                                 nv_connector->type = DCB_CONNECTOR_DVI_I;
959                 }
960         } else {
961                 nv_connector->type = DCB_CONNECTOR_NONE;
962                 nv_connector->hpd = DCB_GPIO_UNUSED;
963         }
964
965         /* no vbios data, or an unknown dcb connector type - attempt to
966          * figure out something suitable ourselves
967          */
968         if (nv_connector->type == DCB_CONNECTOR_NONE) {
969                 struct drm_nouveau_private *dev_priv = dev->dev_private;
970                 struct dcb_table *dcbt = &dev_priv->vbios.dcb;
971                 u32 encoders = 0;
972                 int i;
973
974                 for (i = 0; i < dcbt->entries; i++) {
975                         if (dcbt->entry[i].connector == nv_connector->index)
976                                 encoders |= (1 << dcbt->entry[i].type);
977                 }
978
979                 if (encoders & (1 << DCB_OUTPUT_DP)) {
980                         if (encoders & (1 << DCB_OUTPUT_TMDS))
981                                 nv_connector->type = DCB_CONNECTOR_DP;
982                         else
983                                 nv_connector->type = DCB_CONNECTOR_eDP;
984                 } else
985                 if (encoders & (1 << DCB_OUTPUT_TMDS)) {
986                         if (encoders & (1 << DCB_OUTPUT_ANALOG))
987                                 nv_connector->type = DCB_CONNECTOR_DVI_I;
988                         else
989                                 nv_connector->type = DCB_CONNECTOR_DVI_D;
990                 } else
991                 if (encoders & (1 << DCB_OUTPUT_ANALOG)) {
992                         nv_connector->type = DCB_CONNECTOR_VGA;
993                 } else
994                 if (encoders & (1 << DCB_OUTPUT_LVDS)) {
995                         nv_connector->type = DCB_CONNECTOR_LVDS;
996                 } else
997                 if (encoders & (1 << DCB_OUTPUT_TV)) {
998                         nv_connector->type = DCB_CONNECTOR_TV_0;
999                 }
1000         }
1001
1002         type = drm_conntype_from_dcb(nv_connector->type);
1003         if (type == DRM_MODE_CONNECTOR_LVDS) {
1004                 ret = nouveau_bios_parse_lvds_table(dev, 0, &dummy, &dummy);
1005                 if (ret) {
1006                         NV_ERROR(dev, "Error parsing LVDS table, disabling\n");
1007                         kfree(nv_connector);
1008                         return ERR_PTR(ret);
1009                 }
1010
1011                 funcs = &nouveau_connector_funcs_lvds;
1012         } else {
1013                 funcs = &nouveau_connector_funcs;
1014         }
1015
1016         /* defaults, will get overridden in detect() */
1017         connector->interlace_allowed = false;
1018         connector->doublescan_allowed = false;
1019
1020         drm_connector_init(dev, connector, funcs, type);
1021         drm_connector_helper_add(connector, &nouveau_connector_helper_funcs);
1022
1023         /* Init DVI-I specific properties */
1024         if (nv_connector->type == DCB_CONNECTOR_DVI_I)
1025                 drm_connector_attach_property(connector, dev->mode_config.dvi_i_subconnector_property, 0);
1026
1027         /* Add overscan compensation options to digital outputs */
1028         if (disp->underscan_property &&
1029             (type == DRM_MODE_CONNECTOR_DVID ||
1030              type == DRM_MODE_CONNECTOR_DVII ||
1031              type == DRM_MODE_CONNECTOR_HDMIA ||
1032              type == DRM_MODE_CONNECTOR_DisplayPort)) {
1033                 drm_connector_attach_property(connector,
1034                                               disp->underscan_property,
1035                                               UNDERSCAN_OFF);
1036                 drm_connector_attach_property(connector,
1037                                               disp->underscan_hborder_property,
1038                                               0);
1039                 drm_connector_attach_property(connector,
1040                                               disp->underscan_vborder_property,
1041                                               0);
1042         }
1043
1044         /* Add hue and saturation options */
1045         if (disp->vibrant_hue_property)
1046                 drm_connector_attach_property(connector,
1047                                               disp->vibrant_hue_property,
1048                                               90);
1049         if (disp->color_vibrance_property)
1050                 drm_connector_attach_property(connector,
1051                                               disp->color_vibrance_property,
1052                                               150);
1053
1054         switch (nv_connector->type) {
1055         case DCB_CONNECTOR_VGA:
1056                 if (dev_priv->card_type >= NV_50) {
1057                         drm_connector_attach_property(connector,
1058                                         dev->mode_config.scaling_mode_property,
1059                                         nv_connector->scaling_mode);
1060                 }
1061                 /* fall-through */
1062         case DCB_CONNECTOR_TV_0:
1063         case DCB_CONNECTOR_TV_1:
1064         case DCB_CONNECTOR_TV_3:
1065                 nv_connector->scaling_mode = DRM_MODE_SCALE_NONE;
1066                 break;
1067         default:
1068                 nv_connector->scaling_mode = DRM_MODE_SCALE_FULLSCREEN;
1069
1070                 drm_connector_attach_property(connector,
1071                                 dev->mode_config.scaling_mode_property,
1072                                 nv_connector->scaling_mode);
1073                 if (disp->dithering_mode) {
1074                         nv_connector->dithering_mode = DITHERING_MODE_AUTO;
1075                         drm_connector_attach_property(connector,
1076                                                 disp->dithering_mode,
1077                                                 nv_connector->dithering_mode);
1078                 }
1079                 if (disp->dithering_depth) {
1080                         nv_connector->dithering_depth = DITHERING_DEPTH_AUTO;
1081                         drm_connector_attach_property(connector,
1082                                                 disp->dithering_depth,
1083                                                 nv_connector->dithering_depth);
1084                 }
1085                 break;
1086         }
1087
1088         connector->polled = DRM_CONNECTOR_POLL_CONNECT;
1089         if (nv_connector->hpd != DCB_GPIO_UNUSED) {
1090                 ret = nouveau_gpio_isr_add(dev, 0, nv_connector->hpd, 0xff,
1091                                            nouveau_connector_hotplug,
1092                                            connector);
1093                 if (ret == 0)
1094                         connector->polled = DRM_CONNECTOR_POLL_HPD;
1095         }
1096
1097         drm_sysfs_connector_add(connector);
1098         return connector;
1099 }
1100
1101 static void
1102 nouveau_connector_hotplug(void *data, int plugged)
1103 {
1104         struct drm_connector *connector = data;
1105         struct drm_device *dev = connector->dev;
1106
1107         NV_DEBUG(dev, "%splugged %s\n", plugged ? "" : "un",
1108                  drm_get_connector_name(connector));
1109
1110         if (plugged)
1111                 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
1112         else
1113                 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
1114
1115         drm_helper_hpd_irq_event(dev);
1116 }