Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[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 <linux/pm_runtime.h>
30 #include <linux/vga_switcheroo.h>
31
32 #include <drm/drmP.h>
33 #include <drm/drm_atomic_helper.h>
34 #include <drm/drm_edid.h>
35 #include <drm/drm_crtc_helper.h>
36 #include <drm/drm_probe_helper.h>
37 #include <drm/drm_atomic.h>
38
39 #include "nouveau_reg.h"
40 #include "nouveau_drv.h"
41 #include "dispnv04/hw.h"
42 #include "nouveau_acpi.h"
43
44 #include "nouveau_display.h"
45 #include "nouveau_connector.h"
46 #include "nouveau_encoder.h"
47 #include "nouveau_crtc.h"
48
49 #include <nvif/class.h>
50 #include <nvif/cl0046.h>
51 #include <nvif/event.h>
52
53 struct drm_display_mode *
54 nouveau_conn_native_mode(struct drm_connector *connector)
55 {
56         const struct drm_connector_helper_funcs *helper = connector->helper_private;
57         struct nouveau_drm *drm = nouveau_drm(connector->dev);
58         struct drm_device *dev = connector->dev;
59         struct drm_display_mode *mode, *largest = NULL;
60         int high_w = 0, high_h = 0, high_v = 0;
61
62         list_for_each_entry(mode, &connector->probed_modes, head) {
63                 mode->vrefresh = drm_mode_vrefresh(mode);
64                 if (helper->mode_valid(connector, mode) != MODE_OK ||
65                     (mode->flags & DRM_MODE_FLAG_INTERLACE))
66                         continue;
67
68                 /* Use preferred mode if there is one.. */
69                 if (mode->type & DRM_MODE_TYPE_PREFERRED) {
70                         NV_DEBUG(drm, "native mode from preferred\n");
71                         return drm_mode_duplicate(dev, mode);
72                 }
73
74                 /* Otherwise, take the resolution with the largest width, then
75                  * height, then vertical refresh
76                  */
77                 if (mode->hdisplay < high_w)
78                         continue;
79
80                 if (mode->hdisplay == high_w && mode->vdisplay < high_h)
81                         continue;
82
83                 if (mode->hdisplay == high_w && mode->vdisplay == high_h &&
84                     mode->vrefresh < high_v)
85                         continue;
86
87                 high_w = mode->hdisplay;
88                 high_h = mode->vdisplay;
89                 high_v = mode->vrefresh;
90                 largest = mode;
91         }
92
93         NV_DEBUG(drm, "native mode from largest: %dx%d@%d\n",
94                       high_w, high_h, high_v);
95         return largest ? drm_mode_duplicate(dev, largest) : NULL;
96 }
97
98 int
99 nouveau_conn_atomic_get_property(struct drm_connector *connector,
100                                  const struct drm_connector_state *state,
101                                  struct drm_property *property, u64 *val)
102 {
103         struct nouveau_conn_atom *asyc = nouveau_conn_atom(state);
104         struct nouveau_display *disp = nouveau_display(connector->dev);
105         struct drm_device *dev = connector->dev;
106
107         if (property == dev->mode_config.scaling_mode_property)
108                 *val = asyc->scaler.mode;
109         else if (property == disp->underscan_property)
110                 *val = asyc->scaler.underscan.mode;
111         else if (property == disp->underscan_hborder_property)
112                 *val = asyc->scaler.underscan.hborder;
113         else if (property == disp->underscan_vborder_property)
114                 *val = asyc->scaler.underscan.vborder;
115         else if (property == disp->dithering_mode)
116                 *val = asyc->dither.mode;
117         else if (property == disp->dithering_depth)
118                 *val = asyc->dither.depth;
119         else if (property == disp->vibrant_hue_property)
120                 *val = asyc->procamp.vibrant_hue;
121         else if (property == disp->color_vibrance_property)
122                 *val = asyc->procamp.color_vibrance;
123         else
124                 return -EINVAL;
125
126         return 0;
127 }
128
129 int
130 nouveau_conn_atomic_set_property(struct drm_connector *connector,
131                                  struct drm_connector_state *state,
132                                  struct drm_property *property, u64 val)
133 {
134         struct drm_device *dev = connector->dev;
135         struct nouveau_conn_atom *asyc = nouveau_conn_atom(state);
136         struct nouveau_display *disp = nouveau_display(dev);
137
138         if (property == dev->mode_config.scaling_mode_property) {
139                 switch (val) {
140                 case DRM_MODE_SCALE_NONE:
141                         /* We allow 'None' for EDID modes, even on a fixed
142                          * panel (some exist with support for lower refresh
143                          * rates, which people might want to use for power-
144                          * saving purposes).
145                          *
146                          * Non-EDID modes will force the use of GPU scaling
147                          * to the native mode regardless of this setting.
148                          */
149                         switch (connector->connector_type) {
150                         case DRM_MODE_CONNECTOR_LVDS:
151                         case DRM_MODE_CONNECTOR_eDP:
152                                 /* ... except prior to G80, where the code
153                                  * doesn't support such things.
154                                  */
155                                 if (disp->disp.object.oclass < NV50_DISP)
156                                         return -EINVAL;
157                                 break;
158                         default:
159                                 break;
160                         }
161                 case DRM_MODE_SCALE_FULLSCREEN:
162                 case DRM_MODE_SCALE_CENTER:
163                 case DRM_MODE_SCALE_ASPECT:
164                         break;
165                 default:
166                         return -EINVAL;
167                 }
168
169                 if (asyc->scaler.mode != val) {
170                         asyc->scaler.mode = val;
171                         asyc->set.scaler = true;
172                 }
173         } else
174         if (property == disp->underscan_property) {
175                 if (asyc->scaler.underscan.mode != val) {
176                         asyc->scaler.underscan.mode = val;
177                         asyc->set.scaler = true;
178                 }
179         } else
180         if (property == disp->underscan_hborder_property) {
181                 if (asyc->scaler.underscan.hborder != val) {
182                         asyc->scaler.underscan.hborder = val;
183                         asyc->set.scaler = true;
184                 }
185         } else
186         if (property == disp->underscan_vborder_property) {
187                 if (asyc->scaler.underscan.vborder != val) {
188                         asyc->scaler.underscan.vborder = val;
189                         asyc->set.scaler = true;
190                 }
191         } else
192         if (property == disp->dithering_mode) {
193                 if (asyc->dither.mode != val) {
194                         asyc->dither.mode = val;
195                         asyc->set.dither = true;
196                 }
197         } else
198         if (property == disp->dithering_depth) {
199                 if (asyc->dither.mode != val) {
200                         asyc->dither.depth = val;
201                         asyc->set.dither = true;
202                 }
203         } else
204         if (property == disp->vibrant_hue_property) {
205                 if (asyc->procamp.vibrant_hue != val) {
206                         asyc->procamp.vibrant_hue = val;
207                         asyc->set.procamp = true;
208                 }
209         } else
210         if (property == disp->color_vibrance_property) {
211                 if (asyc->procamp.color_vibrance != val) {
212                         asyc->procamp.color_vibrance = val;
213                         asyc->set.procamp = true;
214                 }
215         } else {
216                 return -EINVAL;
217         }
218
219         return 0;
220 }
221
222 void
223 nouveau_conn_atomic_destroy_state(struct drm_connector *connector,
224                                   struct drm_connector_state *state)
225 {
226         struct nouveau_conn_atom *asyc = nouveau_conn_atom(state);
227         __drm_atomic_helper_connector_destroy_state(&asyc->state);
228         kfree(asyc);
229 }
230
231 struct drm_connector_state *
232 nouveau_conn_atomic_duplicate_state(struct drm_connector *connector)
233 {
234         struct nouveau_conn_atom *armc = nouveau_conn_atom(connector->state);
235         struct nouveau_conn_atom *asyc;
236         if (!(asyc = kmalloc(sizeof(*asyc), GFP_KERNEL)))
237                 return NULL;
238         __drm_atomic_helper_connector_duplicate_state(connector, &asyc->state);
239         asyc->dither = armc->dither;
240         asyc->scaler = armc->scaler;
241         asyc->procamp = armc->procamp;
242         asyc->set.mask = 0;
243         return &asyc->state;
244 }
245
246 void
247 nouveau_conn_reset(struct drm_connector *connector)
248 {
249         struct nouveau_conn_atom *asyc;
250
251         if (WARN_ON(!(asyc = kzalloc(sizeof(*asyc), GFP_KERNEL))))
252                 return;
253
254         if (connector->state)
255                 __drm_atomic_helper_connector_destroy_state(connector->state);
256         __drm_atomic_helper_connector_reset(connector, &asyc->state);
257         asyc->dither.mode = DITHERING_MODE_AUTO;
258         asyc->dither.depth = DITHERING_DEPTH_AUTO;
259         asyc->scaler.mode = DRM_MODE_SCALE_NONE;
260         asyc->scaler.underscan.mode = UNDERSCAN_OFF;
261         asyc->procamp.color_vibrance = 150;
262         asyc->procamp.vibrant_hue = 90;
263
264         if (nouveau_display(connector->dev)->disp.object.oclass < NV50_DISP) {
265                 switch (connector->connector_type) {
266                 case DRM_MODE_CONNECTOR_LVDS:
267                         /* See note in nouveau_conn_atomic_set_property(). */
268                         asyc->scaler.mode = DRM_MODE_SCALE_FULLSCREEN;
269                         break;
270                 default:
271                         break;
272                 }
273         }
274 }
275
276 void
277 nouveau_conn_attach_properties(struct drm_connector *connector)
278 {
279         struct drm_device *dev = connector->dev;
280         struct nouveau_conn_atom *armc = nouveau_conn_atom(connector->state);
281         struct nouveau_display *disp = nouveau_display(dev);
282
283         /* Init DVI-I specific properties. */
284         if (connector->connector_type == DRM_MODE_CONNECTOR_DVII)
285                 drm_object_attach_property(&connector->base, dev->mode_config.
286                                            dvi_i_subconnector_property, 0);
287
288         /* Add overscan compensation options to digital outputs. */
289         if (disp->underscan_property &&
290             (connector->connector_type == DRM_MODE_CONNECTOR_DVID ||
291              connector->connector_type == DRM_MODE_CONNECTOR_DVII ||
292              connector->connector_type == DRM_MODE_CONNECTOR_HDMIA ||
293              connector->connector_type == DRM_MODE_CONNECTOR_DisplayPort)) {
294                 drm_object_attach_property(&connector->base,
295                                            disp->underscan_property,
296                                            UNDERSCAN_OFF);
297                 drm_object_attach_property(&connector->base,
298                                            disp->underscan_hborder_property, 0);
299                 drm_object_attach_property(&connector->base,
300                                            disp->underscan_vborder_property, 0);
301         }
302
303         /* Add hue and saturation options. */
304         if (disp->vibrant_hue_property)
305                 drm_object_attach_property(&connector->base,
306                                            disp->vibrant_hue_property,
307                                            armc->procamp.vibrant_hue);
308         if (disp->color_vibrance_property)
309                 drm_object_attach_property(&connector->base,
310                                            disp->color_vibrance_property,
311                                            armc->procamp.color_vibrance);
312
313         /* Scaling mode property. */
314         switch (connector->connector_type) {
315         case DRM_MODE_CONNECTOR_TV:
316                 break;
317         case DRM_MODE_CONNECTOR_VGA:
318                 if (disp->disp.object.oclass < NV50_DISP)
319                         break; /* Can only scale on DFPs. */
320                 /* Fall-through. */
321         default:
322                 drm_object_attach_property(&connector->base, dev->mode_config.
323                                            scaling_mode_property,
324                                            armc->scaler.mode);
325                 break;
326         }
327
328         /* Dithering properties. */
329         switch (connector->connector_type) {
330         case DRM_MODE_CONNECTOR_TV:
331         case DRM_MODE_CONNECTOR_VGA:
332                 break;
333         default:
334                 if (disp->dithering_mode) {
335                         drm_object_attach_property(&connector->base,
336                                                    disp->dithering_mode,
337                                                    armc->dither.mode);
338                 }
339                 if (disp->dithering_depth) {
340                         drm_object_attach_property(&connector->base,
341                                                    disp->dithering_depth,
342                                                    armc->dither.depth);
343                 }
344                 break;
345         }
346 }
347
348 MODULE_PARM_DESC(tv_disable, "Disable TV-out detection");
349 int nouveau_tv_disable = 0;
350 module_param_named(tv_disable, nouveau_tv_disable, int, 0400);
351
352 MODULE_PARM_DESC(ignorelid, "Ignore ACPI lid status");
353 int nouveau_ignorelid = 0;
354 module_param_named(ignorelid, nouveau_ignorelid, int, 0400);
355
356 MODULE_PARM_DESC(duallink, "Allow dual-link TMDS (default: enabled)");
357 int nouveau_duallink = 1;
358 module_param_named(duallink, nouveau_duallink, int, 0400);
359
360 MODULE_PARM_DESC(hdmimhz, "Force a maximum HDMI pixel clock (in MHz)");
361 int nouveau_hdmimhz = 0;
362 module_param_named(hdmimhz, nouveau_hdmimhz, int, 0400);
363
364 struct nouveau_encoder *
365 find_encoder(struct drm_connector *connector, int type)
366 {
367         struct nouveau_encoder *nv_encoder;
368         struct drm_encoder *enc;
369         int i;
370
371         drm_connector_for_each_possible_encoder(connector, enc, i) {
372                 nv_encoder = nouveau_encoder(enc);
373
374                 if (type == DCB_OUTPUT_ANY ||
375                     (nv_encoder->dcb && nv_encoder->dcb->type == type))
376                         return nv_encoder;
377         }
378
379         return NULL;
380 }
381
382 struct nouveau_connector *
383 nouveau_encoder_connector_get(struct nouveau_encoder *encoder)
384 {
385         struct drm_device *dev = to_drm_encoder(encoder)->dev;
386         struct drm_connector *drm_connector;
387
388         list_for_each_entry(drm_connector, &dev->mode_config.connector_list, head) {
389                 if (drm_connector->encoder == to_drm_encoder(encoder))
390                         return nouveau_connector(drm_connector);
391         }
392
393         return NULL;
394 }
395
396 static void
397 nouveau_connector_destroy(struct drm_connector *connector)
398 {
399         struct nouveau_connector *nv_connector = nouveau_connector(connector);
400         nvif_notify_fini(&nv_connector->hpd);
401         kfree(nv_connector->edid);
402         drm_connector_unregister(connector);
403         drm_connector_cleanup(connector);
404         if (nv_connector->aux.transfer) {
405                 drm_dp_cec_unregister_connector(&nv_connector->aux);
406                 drm_dp_aux_unregister(&nv_connector->aux);
407                 kfree(nv_connector->aux.name);
408         }
409         kfree(connector);
410 }
411
412 static struct nouveau_encoder *
413 nouveau_connector_ddc_detect(struct drm_connector *connector)
414 {
415         struct drm_device *dev = connector->dev;
416         struct nouveau_encoder *nv_encoder = NULL, *found = NULL;
417         struct drm_encoder *encoder;
418         int i, ret;
419         bool switcheroo_ddc = false;
420
421         drm_connector_for_each_possible_encoder(connector, encoder, i) {
422                 nv_encoder = nouveau_encoder(encoder);
423
424                 switch (nv_encoder->dcb->type) {
425                 case DCB_OUTPUT_DP:
426                         ret = nouveau_dp_detect(nv_encoder);
427                         if (ret == NOUVEAU_DP_MST)
428                                 return NULL;
429                         else if (ret == NOUVEAU_DP_SST)
430                                 found = nv_encoder;
431
432                         break;
433                 case DCB_OUTPUT_LVDS:
434                         switcheroo_ddc = !!(vga_switcheroo_handler_flags() &
435                                             VGA_SWITCHEROO_CAN_SWITCH_DDC);
436                 /* fall-through */
437                 default:
438                         if (!nv_encoder->i2c)
439                                 break;
440
441                         if (switcheroo_ddc)
442                                 vga_switcheroo_lock_ddc(dev->pdev);
443                         if (nvkm_probe_i2c(nv_encoder->i2c, 0x50))
444                                 found = nv_encoder;
445                         if (switcheroo_ddc)
446                                 vga_switcheroo_unlock_ddc(dev->pdev);
447
448                         break;
449                 }
450                 if (found)
451                         break;
452         }
453
454         return found;
455 }
456
457 static struct nouveau_encoder *
458 nouveau_connector_of_detect(struct drm_connector *connector)
459 {
460 #ifdef __powerpc__
461         struct drm_device *dev = connector->dev;
462         struct nouveau_connector *nv_connector = nouveau_connector(connector);
463         struct nouveau_encoder *nv_encoder;
464         struct device_node *cn, *dn = pci_device_to_OF_node(dev->pdev);
465
466         if (!dn ||
467             !((nv_encoder = find_encoder(connector, DCB_OUTPUT_TMDS)) ||
468               (nv_encoder = find_encoder(connector, DCB_OUTPUT_ANALOG))))
469                 return NULL;
470
471         for_each_child_of_node(dn, cn) {
472                 const char *name = of_get_property(cn, "name", NULL);
473                 const void *edid = of_get_property(cn, "EDID", NULL);
474                 int idx = name ? name[strlen(name) - 1] - 'A' : 0;
475
476                 if (nv_encoder->dcb->i2c_index == idx && edid) {
477                         nv_connector->edid =
478                                 kmemdup(edid, EDID_LENGTH, GFP_KERNEL);
479                         of_node_put(cn);
480                         return nv_encoder;
481                 }
482         }
483 #endif
484         return NULL;
485 }
486
487 static void
488 nouveau_connector_set_encoder(struct drm_connector *connector,
489                               struct nouveau_encoder *nv_encoder)
490 {
491         struct nouveau_connector *nv_connector = nouveau_connector(connector);
492         struct nouveau_drm *drm = nouveau_drm(connector->dev);
493         struct drm_device *dev = connector->dev;
494
495         if (nv_connector->detected_encoder == nv_encoder)
496                 return;
497         nv_connector->detected_encoder = nv_encoder;
498
499         if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_TESLA) {
500                 connector->interlace_allowed = true;
501                 connector->doublescan_allowed = true;
502         } else
503         if (nv_encoder->dcb->type == DCB_OUTPUT_LVDS ||
504             nv_encoder->dcb->type == DCB_OUTPUT_TMDS) {
505                 connector->doublescan_allowed = false;
506                 connector->interlace_allowed = false;
507         } else {
508                 connector->doublescan_allowed = true;
509                 if (drm->client.device.info.family == NV_DEVICE_INFO_V0_KELVIN ||
510                     (drm->client.device.info.family == NV_DEVICE_INFO_V0_CELSIUS &&
511                      (dev->pdev->device & 0x0ff0) != 0x0100 &&
512                      (dev->pdev->device & 0x0ff0) != 0x0150))
513                         /* HW is broken */
514                         connector->interlace_allowed = false;
515                 else
516                         connector->interlace_allowed = true;
517         }
518
519         if (nv_connector->type == DCB_CONNECTOR_DVI_I) {
520                 drm_object_property_set_value(&connector->base,
521                         dev->mode_config.dvi_i_subconnector_property,
522                         nv_encoder->dcb->type == DCB_OUTPUT_TMDS ?
523                         DRM_MODE_SUBCONNECTOR_DVID :
524                         DRM_MODE_SUBCONNECTOR_DVIA);
525         }
526 }
527
528 static enum drm_connector_status
529 nouveau_connector_detect(struct drm_connector *connector, bool force)
530 {
531         struct drm_device *dev = connector->dev;
532         struct nouveau_drm *drm = nouveau_drm(dev);
533         struct nouveau_connector *nv_connector = nouveau_connector(connector);
534         struct nouveau_encoder *nv_encoder = NULL;
535         struct nouveau_encoder *nv_partner;
536         struct i2c_adapter *i2c;
537         int type;
538         int ret;
539         enum drm_connector_status conn_status = connector_status_disconnected;
540
541         /* Cleanup the previous EDID block. */
542         if (nv_connector->edid) {
543                 drm_connector_update_edid_property(connector, NULL);
544                 kfree(nv_connector->edid);
545                 nv_connector->edid = NULL;
546         }
547
548         /* Outputs are only polled while runtime active, so resuming the
549          * device here is unnecessary (and would deadlock upon runtime suspend
550          * because it waits for polling to finish). We do however, want to
551          * prevent the autosuspend timer from elapsing during this operation
552          * if possible.
553          */
554         if (drm_kms_helper_is_poll_worker()) {
555                 pm_runtime_get_noresume(dev->dev);
556         } else {
557                 ret = pm_runtime_get_sync(dev->dev);
558                 if (ret < 0 && ret != -EACCES)
559                         return conn_status;
560         }
561
562         nv_encoder = nouveau_connector_ddc_detect(connector);
563         if (nv_encoder && (i2c = nv_encoder->i2c) != NULL) {
564                 if ((vga_switcheroo_handler_flags() &
565                      VGA_SWITCHEROO_CAN_SWITCH_DDC) &&
566                     nv_connector->type == DCB_CONNECTOR_LVDS)
567                         nv_connector->edid = drm_get_edid_switcheroo(connector,
568                                                                      i2c);
569                 else
570                         nv_connector->edid = drm_get_edid(connector, i2c);
571
572                 drm_connector_update_edid_property(connector,
573                                                         nv_connector->edid);
574                 if (!nv_connector->edid) {
575                         NV_ERROR(drm, "DDC responded, but no EDID for %s\n",
576                                  connector->name);
577                         goto detect_analog;
578                 }
579
580                 /* Override encoder type for DVI-I based on whether EDID
581                  * says the display is digital or analog, both use the
582                  * same i2c channel so the value returned from ddc_detect
583                  * isn't necessarily correct.
584                  */
585                 nv_partner = NULL;
586                 if (nv_encoder->dcb->type == DCB_OUTPUT_TMDS)
587                         nv_partner = find_encoder(connector, DCB_OUTPUT_ANALOG);
588                 if (nv_encoder->dcb->type == DCB_OUTPUT_ANALOG)
589                         nv_partner = find_encoder(connector, DCB_OUTPUT_TMDS);
590
591                 if (nv_partner && ((nv_encoder->dcb->type == DCB_OUTPUT_ANALOG &&
592                                     nv_partner->dcb->type == DCB_OUTPUT_TMDS) ||
593                                    (nv_encoder->dcb->type == DCB_OUTPUT_TMDS &&
594                                     nv_partner->dcb->type == DCB_OUTPUT_ANALOG))) {
595                         if (nv_connector->edid->input & DRM_EDID_INPUT_DIGITAL)
596                                 type = DCB_OUTPUT_TMDS;
597                         else
598                                 type = DCB_OUTPUT_ANALOG;
599
600                         nv_encoder = find_encoder(connector, type);
601                 }
602
603                 nouveau_connector_set_encoder(connector, nv_encoder);
604                 conn_status = connector_status_connected;
605                 drm_dp_cec_set_edid(&nv_connector->aux, nv_connector->edid);
606                 goto out;
607         }
608
609         nv_encoder = nouveau_connector_of_detect(connector);
610         if (nv_encoder) {
611                 nouveau_connector_set_encoder(connector, nv_encoder);
612                 conn_status = connector_status_connected;
613                 goto out;
614         }
615
616 detect_analog:
617         nv_encoder = find_encoder(connector, DCB_OUTPUT_ANALOG);
618         if (!nv_encoder && !nouveau_tv_disable)
619                 nv_encoder = find_encoder(connector, DCB_OUTPUT_TV);
620         if (nv_encoder && force) {
621                 struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
622                 const struct drm_encoder_helper_funcs *helper =
623                                                 encoder->helper_private;
624
625                 if (helper->detect(encoder, connector) ==
626                                                 connector_status_connected) {
627                         nouveau_connector_set_encoder(connector, nv_encoder);
628                         conn_status = connector_status_connected;
629                         goto out;
630                 }
631
632         }
633
634  out:
635
636         pm_runtime_mark_last_busy(dev->dev);
637         pm_runtime_put_autosuspend(dev->dev);
638
639         return conn_status;
640 }
641
642 static enum drm_connector_status
643 nouveau_connector_detect_lvds(struct drm_connector *connector, bool force)
644 {
645         struct drm_device *dev = connector->dev;
646         struct nouveau_drm *drm = nouveau_drm(dev);
647         struct nouveau_connector *nv_connector = nouveau_connector(connector);
648         struct nouveau_encoder *nv_encoder = NULL;
649         enum drm_connector_status status = connector_status_disconnected;
650
651         /* Cleanup the previous EDID block. */
652         if (nv_connector->edid) {
653                 drm_connector_update_edid_property(connector, NULL);
654                 kfree(nv_connector->edid);
655                 nv_connector->edid = NULL;
656         }
657
658         nv_encoder = find_encoder(connector, DCB_OUTPUT_LVDS);
659         if (!nv_encoder)
660                 return connector_status_disconnected;
661
662         /* Try retrieving EDID via DDC */
663         if (!drm->vbios.fp_no_ddc) {
664                 status = nouveau_connector_detect(connector, force);
665                 if (status == connector_status_connected)
666                         goto out;
667         }
668
669         /* On some laptops (Sony, i'm looking at you) there appears to
670          * be no direct way of accessing the panel's EDID.  The only
671          * option available to us appears to be to ask ACPI for help..
672          *
673          * It's important this check's before trying straps, one of the
674          * said manufacturer's laptops are configured in such a way
675          * the nouveau decides an entry in the VBIOS FP mode table is
676          * valid - it's not (rh#613284)
677          */
678         if (nv_encoder->dcb->lvdsconf.use_acpi_for_edid) {
679                 if ((nv_connector->edid = nouveau_acpi_edid(dev, connector))) {
680                         status = connector_status_connected;
681                         goto out;
682                 }
683         }
684
685         /* If no EDID found above, and the VBIOS indicates a hardcoded
686          * modeline is avalilable for the panel, set it as the panel's
687          * native mode and exit.
688          */
689         if (nouveau_bios_fp_mode(dev, NULL) && (drm->vbios.fp_no_ddc ||
690             nv_encoder->dcb->lvdsconf.use_straps_for_mode)) {
691                 status = connector_status_connected;
692                 goto out;
693         }
694
695         /* Still nothing, some VBIOS images have a hardcoded EDID block
696          * stored for the panel stored in them.
697          */
698         if (!drm->vbios.fp_no_ddc) {
699                 struct edid *edid =
700                         (struct edid *)nouveau_bios_embedded_edid(dev);
701                 if (edid) {
702                         nv_connector->edid =
703                                         kmemdup(edid, EDID_LENGTH, GFP_KERNEL);
704                         if (nv_connector->edid)
705                                 status = connector_status_connected;
706                 }
707         }
708
709 out:
710 #if defined(CONFIG_ACPI_BUTTON) || \
711         (defined(CONFIG_ACPI_BUTTON_MODULE) && defined(MODULE))
712         if (status == connector_status_connected &&
713             !nouveau_ignorelid && !acpi_lid_open())
714                 status = connector_status_unknown;
715 #endif
716
717         drm_connector_update_edid_property(connector, nv_connector->edid);
718         nouveau_connector_set_encoder(connector, nv_encoder);
719         return status;
720 }
721
722 static void
723 nouveau_connector_force(struct drm_connector *connector)
724 {
725         struct nouveau_drm *drm = nouveau_drm(connector->dev);
726         struct nouveau_connector *nv_connector = nouveau_connector(connector);
727         struct nouveau_encoder *nv_encoder;
728         int type;
729
730         if (nv_connector->type == DCB_CONNECTOR_DVI_I) {
731                 if (connector->force == DRM_FORCE_ON_DIGITAL)
732                         type = DCB_OUTPUT_TMDS;
733                 else
734                         type = DCB_OUTPUT_ANALOG;
735         } else
736                 type = DCB_OUTPUT_ANY;
737
738         nv_encoder = find_encoder(connector, type);
739         if (!nv_encoder) {
740                 NV_ERROR(drm, "can't find encoder to force %s on!\n",
741                          connector->name);
742                 connector->status = connector_status_disconnected;
743                 return;
744         }
745
746         nouveau_connector_set_encoder(connector, nv_encoder);
747 }
748
749 static int
750 nouveau_connector_set_property(struct drm_connector *connector,
751                                struct drm_property *property, uint64_t value)
752 {
753         struct nouveau_conn_atom *asyc = nouveau_conn_atom(connector->state);
754         struct nouveau_connector *nv_connector = nouveau_connector(connector);
755         struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
756         struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
757         int ret;
758
759         ret = connector->funcs->atomic_set_property(&nv_connector->base,
760                                                     &asyc->state,
761                                                     property, value);
762         if (ret) {
763                 if (nv_encoder && nv_encoder->dcb->type == DCB_OUTPUT_TV)
764                         return get_slave_funcs(encoder)->set_property(
765                                 encoder, connector, property, value);
766                 return ret;
767         }
768
769         nv_connector->scaling_mode = asyc->scaler.mode;
770         nv_connector->dithering_mode = asyc->dither.mode;
771
772         if (connector->encoder && connector->encoder->crtc) {
773                 ret = drm_crtc_helper_set_mode(connector->encoder->crtc,
774                                               &connector->encoder->crtc->mode,
775                                                connector->encoder->crtc->x,
776                                                connector->encoder->crtc->y,
777                                                NULL);
778                 if (!ret)
779                         return -EINVAL;
780         }
781
782         return 0;
783 }
784
785 struct moderec {
786         int hdisplay;
787         int vdisplay;
788 };
789
790 static struct moderec scaler_modes[] = {
791         { 1920, 1200 },
792         { 1920, 1080 },
793         { 1680, 1050 },
794         { 1600, 1200 },
795         { 1400, 1050 },
796         { 1280, 1024 },
797         { 1280, 960 },
798         { 1152, 864 },
799         { 1024, 768 },
800         { 800, 600 },
801         { 720, 400 },
802         { 640, 480 },
803         { 640, 400 },
804         { 640, 350 },
805         {}
806 };
807
808 static int
809 nouveau_connector_scaler_modes_add(struct drm_connector *connector)
810 {
811         struct nouveau_connector *nv_connector = nouveau_connector(connector);
812         struct drm_display_mode *native = nv_connector->native_mode, *m;
813         struct drm_device *dev = connector->dev;
814         struct moderec *mode = &scaler_modes[0];
815         int modes = 0;
816
817         if (!native)
818                 return 0;
819
820         while (mode->hdisplay) {
821                 if (mode->hdisplay <= native->hdisplay &&
822                     mode->vdisplay <= native->vdisplay &&
823                     (mode->hdisplay != native->hdisplay ||
824                      mode->vdisplay != native->vdisplay)) {
825                         m = drm_cvt_mode(dev, mode->hdisplay, mode->vdisplay,
826                                          drm_mode_vrefresh(native), false,
827                                          false, false);
828                         if (!m)
829                                 continue;
830
831                         drm_mode_probed_add(connector, m);
832                         modes++;
833                 }
834
835                 mode++;
836         }
837
838         return modes;
839 }
840
841 static void
842 nouveau_connector_detect_depth(struct drm_connector *connector)
843 {
844         struct nouveau_drm *drm = nouveau_drm(connector->dev);
845         struct nouveau_connector *nv_connector = nouveau_connector(connector);
846         struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
847         struct nvbios *bios = &drm->vbios;
848         struct drm_display_mode *mode = nv_connector->native_mode;
849         bool duallink;
850
851         /* if the edid is feeling nice enough to provide this info, use it */
852         if (nv_connector->edid && connector->display_info.bpc)
853                 return;
854
855         /* EDID 1.4 is *supposed* to be supported on eDP, but, Apple... */
856         if (nv_connector->type == DCB_CONNECTOR_eDP) {
857                 connector->display_info.bpc = 6;
858                 return;
859         }
860
861         /* we're out of options unless we're LVDS, default to 8bpc */
862         if (nv_encoder->dcb->type != DCB_OUTPUT_LVDS) {
863                 connector->display_info.bpc = 8;
864                 return;
865         }
866
867         connector->display_info.bpc = 6;
868
869         /* LVDS: panel straps */
870         if (bios->fp_no_ddc) {
871                 if (bios->fp.if_is_24bit)
872                         connector->display_info.bpc = 8;
873                 return;
874         }
875
876         /* LVDS: DDC panel, need to first determine the number of links to
877          * know which if_is_24bit flag to check...
878          */
879         if (nv_connector->edid &&
880             nv_connector->type == DCB_CONNECTOR_LVDS_SPWG)
881                 duallink = ((u8 *)nv_connector->edid)[121] == 2;
882         else
883                 duallink = mode->clock >= bios->fp.duallink_transition_clk;
884
885         if ((!duallink && (bios->fp.strapless_is_24bit & 1)) ||
886             ( duallink && (bios->fp.strapless_is_24bit & 2)))
887                 connector->display_info.bpc = 8;
888 }
889
890 static int
891 nouveau_connector_late_register(struct drm_connector *connector)
892 {
893         int ret;
894
895         ret = nouveau_backlight_init(connector);
896
897         return ret;
898 }
899
900 static void
901 nouveau_connector_early_unregister(struct drm_connector *connector)
902 {
903         nouveau_backlight_fini(connector);
904 }
905
906 static int
907 nouveau_connector_get_modes(struct drm_connector *connector)
908 {
909         struct drm_device *dev = connector->dev;
910         struct nouveau_drm *drm = nouveau_drm(dev);
911         struct nouveau_connector *nv_connector = nouveau_connector(connector);
912         struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
913         struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
914         int ret = 0;
915
916         /* destroy the native mode, the attached monitor could have changed.
917          */
918         if (nv_connector->native_mode) {
919                 drm_mode_destroy(dev, nv_connector->native_mode);
920                 nv_connector->native_mode = NULL;
921         }
922
923         if (nv_connector->edid)
924                 ret = drm_add_edid_modes(connector, nv_connector->edid);
925         else
926         if (nv_encoder->dcb->type == DCB_OUTPUT_LVDS &&
927             (nv_encoder->dcb->lvdsconf.use_straps_for_mode ||
928              drm->vbios.fp_no_ddc) && nouveau_bios_fp_mode(dev, NULL)) {
929                 struct drm_display_mode mode;
930
931                 nouveau_bios_fp_mode(dev, &mode);
932                 nv_connector->native_mode = drm_mode_duplicate(dev, &mode);
933         }
934
935         /* Determine display colour depth for everything except LVDS now,
936          * DP requires this before mode_valid() is called.
937          */
938         if (connector->connector_type != DRM_MODE_CONNECTOR_LVDS)
939                 nouveau_connector_detect_depth(connector);
940
941         /* Find the native mode if this is a digital panel, if we didn't
942          * find any modes through DDC previously add the native mode to
943          * the list of modes.
944          */
945         if (!nv_connector->native_mode)
946                 nv_connector->native_mode = nouveau_conn_native_mode(connector);
947         if (ret == 0 && nv_connector->native_mode) {
948                 struct drm_display_mode *mode;
949
950                 mode = drm_mode_duplicate(dev, nv_connector->native_mode);
951                 drm_mode_probed_add(connector, mode);
952                 ret = 1;
953         }
954
955         /* Determine LVDS colour depth, must happen after determining
956          * "native" mode as some VBIOS tables require us to use the
957          * pixel clock as part of the lookup...
958          */
959         if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS)
960                 nouveau_connector_detect_depth(connector);
961
962         if (nv_encoder->dcb->type == DCB_OUTPUT_TV)
963                 ret = get_slave_funcs(encoder)->get_modes(encoder, connector);
964
965         if (nv_connector->type == DCB_CONNECTOR_LVDS ||
966             nv_connector->type == DCB_CONNECTOR_LVDS_SPWG ||
967             nv_connector->type == DCB_CONNECTOR_eDP)
968                 ret += nouveau_connector_scaler_modes_add(connector);
969
970         return ret;
971 }
972
973 static unsigned
974 get_tmds_link_bandwidth(struct drm_connector *connector)
975 {
976         struct nouveau_connector *nv_connector = nouveau_connector(connector);
977         struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
978         struct nouveau_drm *drm = nouveau_drm(connector->dev);
979         struct dcb_output *dcb = nv_connector->detected_encoder->dcb;
980         struct drm_display_info *info = NULL;
981         const unsigned duallink_scale =
982                 nouveau_duallink && nv_encoder->dcb->duallink_possible ? 2 : 1;
983
984         if (drm_detect_hdmi_monitor(nv_connector->edid))
985                 info = &nv_connector->base.display_info;
986
987         if (info) {
988                 if (nouveau_hdmimhz > 0)
989                         return nouveau_hdmimhz * 1000;
990                 /* Note: these limits are conservative, some Fermi's
991                  * can do 297 MHz. Unclear how this can be determined.
992                  */
993                 if (drm->client.device.info.chipset >= 0x120) {
994                         const int max_tmds_clock =
995                                 info->hdmi.scdc.scrambling.supported ?
996                                 594000 : 340000;
997                         return info->max_tmds_clock ?
998                                 min(info->max_tmds_clock, max_tmds_clock) :
999                                 max_tmds_clock;
1000                 }
1001                 if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_KEPLER)
1002                         return 297000;
1003                 if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_FERMI)
1004                         return 225000;
1005         }
1006         if (dcb->location != DCB_LOC_ON_CHIP ||
1007             drm->client.device.info.chipset >= 0x46)
1008                 return 165000 * duallink_scale;
1009         else if (drm->client.device.info.chipset >= 0x40)
1010                 return 155000 * duallink_scale;
1011         else if (drm->client.device.info.chipset >= 0x18)
1012                 return 135000 * duallink_scale;
1013         else
1014                 return 112000 * duallink_scale;
1015 }
1016
1017 static enum drm_mode_status
1018 nouveau_connector_mode_valid(struct drm_connector *connector,
1019                              struct drm_display_mode *mode)
1020 {
1021         struct nouveau_connector *nv_connector = nouveau_connector(connector);
1022         struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
1023         struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
1024         unsigned min_clock = 25000, max_clock = min_clock;
1025         unsigned clock = mode->clock;
1026
1027         switch (nv_encoder->dcb->type) {
1028         case DCB_OUTPUT_LVDS:
1029                 if (nv_connector->native_mode &&
1030                     (mode->hdisplay > nv_connector->native_mode->hdisplay ||
1031                      mode->vdisplay > nv_connector->native_mode->vdisplay))
1032                         return MODE_PANEL;
1033
1034                 min_clock = 0;
1035                 max_clock = 400000;
1036                 break;
1037         case DCB_OUTPUT_TMDS:
1038                 max_clock = get_tmds_link_bandwidth(connector);
1039                 break;
1040         case DCB_OUTPUT_ANALOG:
1041                 max_clock = nv_encoder->dcb->crtconf.maxfreq;
1042                 if (!max_clock)
1043                         max_clock = 350000;
1044                 break;
1045         case DCB_OUTPUT_TV:
1046                 return get_slave_funcs(encoder)->mode_valid(encoder, mode);
1047         case DCB_OUTPUT_DP:
1048                 max_clock  = nv_encoder->dp.link_nr;
1049                 max_clock *= nv_encoder->dp.link_bw;
1050                 clock = clock * (connector->display_info.bpc * 3) / 10;
1051                 break;
1052         default:
1053                 BUG();
1054                 return MODE_BAD;
1055         }
1056
1057         if ((mode->flags & DRM_MODE_FLAG_3D_MASK) == DRM_MODE_FLAG_3D_FRAME_PACKING)
1058                 clock *= 2;
1059
1060         if (clock < min_clock)
1061                 return MODE_CLOCK_LOW;
1062
1063         if (clock > max_clock)
1064                 return MODE_CLOCK_HIGH;
1065
1066         return MODE_OK;
1067 }
1068
1069 static struct drm_encoder *
1070 nouveau_connector_best_encoder(struct drm_connector *connector)
1071 {
1072         struct nouveau_connector *nv_connector = nouveau_connector(connector);
1073
1074         if (nv_connector->detected_encoder)
1075                 return to_drm_encoder(nv_connector->detected_encoder);
1076
1077         return NULL;
1078 }
1079
1080 static const struct drm_connector_helper_funcs
1081 nouveau_connector_helper_funcs = {
1082         .get_modes = nouveau_connector_get_modes,
1083         .mode_valid = nouveau_connector_mode_valid,
1084         .best_encoder = nouveau_connector_best_encoder,
1085 };
1086
1087 static const struct drm_connector_funcs
1088 nouveau_connector_funcs = {
1089         .dpms = drm_helper_connector_dpms,
1090         .reset = nouveau_conn_reset,
1091         .detect = nouveau_connector_detect,
1092         .force = nouveau_connector_force,
1093         .fill_modes = drm_helper_probe_single_connector_modes,
1094         .set_property = nouveau_connector_set_property,
1095         .destroy = nouveau_connector_destroy,
1096         .atomic_duplicate_state = nouveau_conn_atomic_duplicate_state,
1097         .atomic_destroy_state = nouveau_conn_atomic_destroy_state,
1098         .atomic_set_property = nouveau_conn_atomic_set_property,
1099         .atomic_get_property = nouveau_conn_atomic_get_property,
1100         .late_register = nouveau_connector_late_register,
1101         .early_unregister = nouveau_connector_early_unregister,
1102 };
1103
1104 static const struct drm_connector_funcs
1105 nouveau_connector_funcs_lvds = {
1106         .dpms = drm_helper_connector_dpms,
1107         .reset = nouveau_conn_reset,
1108         .detect = nouveau_connector_detect_lvds,
1109         .force = nouveau_connector_force,
1110         .fill_modes = drm_helper_probe_single_connector_modes,
1111         .set_property = nouveau_connector_set_property,
1112         .destroy = nouveau_connector_destroy,
1113         .atomic_duplicate_state = nouveau_conn_atomic_duplicate_state,
1114         .atomic_destroy_state = nouveau_conn_atomic_destroy_state,
1115         .atomic_set_property = nouveau_conn_atomic_set_property,
1116         .atomic_get_property = nouveau_conn_atomic_get_property,
1117         .late_register = nouveau_connector_late_register,
1118         .early_unregister = nouveau_connector_early_unregister,
1119 };
1120
1121 static int
1122 nouveau_connector_hotplug(struct nvif_notify *notify)
1123 {
1124         struct nouveau_connector *nv_connector =
1125                 container_of(notify, typeof(*nv_connector), hpd);
1126         struct drm_connector *connector = &nv_connector->base;
1127         struct nouveau_drm *drm = nouveau_drm(connector->dev);
1128         const struct nvif_notify_conn_rep_v0 *rep = notify->data;
1129         const char *name = connector->name;
1130         struct nouveau_encoder *nv_encoder;
1131         int ret;
1132
1133         ret = pm_runtime_get(drm->dev->dev);
1134         if (ret == 0) {
1135                 /* We can't block here if there's a pending PM request
1136                  * running, as we'll deadlock nouveau_display_fini() when it
1137                  * calls nvif_put() on our nvif_notify struct. So, simply
1138                  * defer the hotplug event until the device finishes resuming
1139                  */
1140                 NV_DEBUG(drm, "Deferring HPD on %s until runtime resume\n",
1141                          name);
1142                 schedule_work(&drm->hpd_work);
1143
1144                 pm_runtime_put_noidle(drm->dev->dev);
1145                 return NVIF_NOTIFY_KEEP;
1146         } else if (ret != 1 && ret != -EACCES) {
1147                 NV_WARN(drm, "HPD on %s dropped due to RPM failure: %d\n",
1148                         name, ret);
1149                 return NVIF_NOTIFY_DROP;
1150         }
1151
1152         if (rep->mask & NVIF_NOTIFY_CONN_V0_IRQ) {
1153                 NV_DEBUG(drm, "service %s\n", name);
1154                 drm_dp_cec_irq(&nv_connector->aux);
1155                 if ((nv_encoder = find_encoder(connector, DCB_OUTPUT_DP)))
1156                         nv50_mstm_service(nv_encoder->dp.mstm);
1157         } else {
1158                 bool plugged = (rep->mask != NVIF_NOTIFY_CONN_V0_UNPLUG);
1159
1160                 if (!plugged)
1161                         drm_dp_cec_unset_edid(&nv_connector->aux);
1162                 NV_DEBUG(drm, "%splugged %s\n", plugged ? "" : "un", name);
1163                 if ((nv_encoder = find_encoder(connector, DCB_OUTPUT_DP))) {
1164                         if (!plugged)
1165                                 nv50_mstm_remove(nv_encoder->dp.mstm);
1166                 }
1167
1168                 drm_helper_hpd_irq_event(connector->dev);
1169         }
1170
1171         pm_runtime_mark_last_busy(drm->dev->dev);
1172         pm_runtime_put_autosuspend(drm->dev->dev);
1173         return NVIF_NOTIFY_KEEP;
1174 }
1175
1176 static ssize_t
1177 nouveau_connector_aux_xfer(struct drm_dp_aux *obj, struct drm_dp_aux_msg *msg)
1178 {
1179         struct nouveau_connector *nv_connector =
1180                 container_of(obj, typeof(*nv_connector), aux);
1181         struct nouveau_encoder *nv_encoder;
1182         struct nvkm_i2c_aux *aux;
1183         u8 size = msg->size;
1184         int ret;
1185
1186         nv_encoder = find_encoder(&nv_connector->base, DCB_OUTPUT_DP);
1187         if (!nv_encoder || !(aux = nv_encoder->aux))
1188                 return -ENODEV;
1189         if (WARN_ON(msg->size > 16))
1190                 return -E2BIG;
1191
1192         ret = nvkm_i2c_aux_acquire(aux);
1193         if (ret)
1194                 return ret;
1195
1196         ret = nvkm_i2c_aux_xfer(aux, false, msg->request, msg->address,
1197                                 msg->buffer, &size);
1198         nvkm_i2c_aux_release(aux);
1199         if (ret >= 0) {
1200                 msg->reply = ret;
1201                 return size;
1202         }
1203
1204         return ret;
1205 }
1206
1207 static int
1208 drm_conntype_from_dcb(enum dcb_connector_type dcb)
1209 {
1210         switch (dcb) {
1211         case DCB_CONNECTOR_VGA      : return DRM_MODE_CONNECTOR_VGA;
1212         case DCB_CONNECTOR_TV_0     :
1213         case DCB_CONNECTOR_TV_1     :
1214         case DCB_CONNECTOR_TV_3     : return DRM_MODE_CONNECTOR_TV;
1215         case DCB_CONNECTOR_DMS59_0  :
1216         case DCB_CONNECTOR_DMS59_1  :
1217         case DCB_CONNECTOR_DVI_I    : return DRM_MODE_CONNECTOR_DVII;
1218         case DCB_CONNECTOR_DVI_D    : return DRM_MODE_CONNECTOR_DVID;
1219         case DCB_CONNECTOR_LVDS     :
1220         case DCB_CONNECTOR_LVDS_SPWG: return DRM_MODE_CONNECTOR_LVDS;
1221         case DCB_CONNECTOR_DMS59_DP0:
1222         case DCB_CONNECTOR_DMS59_DP1:
1223         case DCB_CONNECTOR_DP       :
1224         case DCB_CONNECTOR_USB_C    : return DRM_MODE_CONNECTOR_DisplayPort;
1225         case DCB_CONNECTOR_eDP      : return DRM_MODE_CONNECTOR_eDP;
1226         case DCB_CONNECTOR_HDMI_0   :
1227         case DCB_CONNECTOR_HDMI_1   :
1228         case DCB_CONNECTOR_HDMI_C   : return DRM_MODE_CONNECTOR_HDMIA;
1229         case DCB_CONNECTOR_WFD      : return DRM_MODE_CONNECTOR_VIRTUAL;
1230         default:
1231                 break;
1232         }
1233
1234         return DRM_MODE_CONNECTOR_Unknown;
1235 }
1236
1237 struct drm_connector *
1238 nouveau_connector_create(struct drm_device *dev,
1239                          const struct dcb_output *dcbe)
1240 {
1241         const struct drm_connector_funcs *funcs = &nouveau_connector_funcs;
1242         struct nouveau_drm *drm = nouveau_drm(dev);
1243         struct nouveau_display *disp = nouveau_display(dev);
1244         struct nouveau_connector *nv_connector = NULL;
1245         struct drm_connector *connector;
1246         struct drm_connector_list_iter conn_iter;
1247         char aux_name[48] = {0};
1248         int index = dcbe->connector;
1249         int type, ret = 0;
1250         bool dummy;
1251
1252         drm_connector_list_iter_begin(dev, &conn_iter);
1253         nouveau_for_each_non_mst_connector_iter(connector, &conn_iter) {
1254                 nv_connector = nouveau_connector(connector);
1255                 if (nv_connector->index == index) {
1256                         drm_connector_list_iter_end(&conn_iter);
1257                         return connector;
1258                 }
1259         }
1260         drm_connector_list_iter_end(&conn_iter);
1261
1262         nv_connector = kzalloc(sizeof(*nv_connector), GFP_KERNEL);
1263         if (!nv_connector)
1264                 return ERR_PTR(-ENOMEM);
1265
1266         connector = &nv_connector->base;
1267         nv_connector->index = index;
1268
1269         /* attempt to parse vbios connector type and hotplug gpio */
1270         nv_connector->dcb = olddcb_conn(dev, index);
1271         if (nv_connector->dcb) {
1272                 u32 entry = ROM16(nv_connector->dcb[0]);
1273                 if (olddcb_conntab(dev)[3] >= 4)
1274                         entry |= (u32)ROM16(nv_connector->dcb[2]) << 16;
1275
1276                 nv_connector->type = nv_connector->dcb[0];
1277                 if (drm_conntype_from_dcb(nv_connector->type) ==
1278                                           DRM_MODE_CONNECTOR_Unknown) {
1279                         NV_WARN(drm, "unknown connector type %02x\n",
1280                                 nv_connector->type);
1281                         nv_connector->type = DCB_CONNECTOR_NONE;
1282                 }
1283
1284                 /* Gigabyte NX85T */
1285                 if (nv_match_device(dev, 0x0421, 0x1458, 0x344c)) {
1286                         if (nv_connector->type == DCB_CONNECTOR_HDMI_1)
1287                                 nv_connector->type = DCB_CONNECTOR_DVI_I;
1288                 }
1289
1290                 /* Gigabyte GV-NX86T512H */
1291                 if (nv_match_device(dev, 0x0402, 0x1458, 0x3455)) {
1292                         if (nv_connector->type == DCB_CONNECTOR_HDMI_1)
1293                                 nv_connector->type = DCB_CONNECTOR_DVI_I;
1294                 }
1295         } else {
1296                 nv_connector->type = DCB_CONNECTOR_NONE;
1297         }
1298
1299         /* no vbios data, or an unknown dcb connector type - attempt to
1300          * figure out something suitable ourselves
1301          */
1302         if (nv_connector->type == DCB_CONNECTOR_NONE) {
1303                 struct nouveau_drm *drm = nouveau_drm(dev);
1304                 struct dcb_table *dcbt = &drm->vbios.dcb;
1305                 u32 encoders = 0;
1306                 int i;
1307
1308                 for (i = 0; i < dcbt->entries; i++) {
1309                         if (dcbt->entry[i].connector == nv_connector->index)
1310                                 encoders |= (1 << dcbt->entry[i].type);
1311                 }
1312
1313                 if (encoders & (1 << DCB_OUTPUT_DP)) {
1314                         if (encoders & (1 << DCB_OUTPUT_TMDS))
1315                                 nv_connector->type = DCB_CONNECTOR_DP;
1316                         else
1317                                 nv_connector->type = DCB_CONNECTOR_eDP;
1318                 } else
1319                 if (encoders & (1 << DCB_OUTPUT_TMDS)) {
1320                         if (encoders & (1 << DCB_OUTPUT_ANALOG))
1321                                 nv_connector->type = DCB_CONNECTOR_DVI_I;
1322                         else
1323                                 nv_connector->type = DCB_CONNECTOR_DVI_D;
1324                 } else
1325                 if (encoders & (1 << DCB_OUTPUT_ANALOG)) {
1326                         nv_connector->type = DCB_CONNECTOR_VGA;
1327                 } else
1328                 if (encoders & (1 << DCB_OUTPUT_LVDS)) {
1329                         nv_connector->type = DCB_CONNECTOR_LVDS;
1330                 } else
1331                 if (encoders & (1 << DCB_OUTPUT_TV)) {
1332                         nv_connector->type = DCB_CONNECTOR_TV_0;
1333                 }
1334         }
1335
1336         switch ((type = drm_conntype_from_dcb(nv_connector->type))) {
1337         case DRM_MODE_CONNECTOR_LVDS:
1338                 ret = nouveau_bios_parse_lvds_table(dev, 0, &dummy, &dummy);
1339                 if (ret) {
1340                         NV_ERROR(drm, "Error parsing LVDS table, disabling\n");
1341                         kfree(nv_connector);
1342                         return ERR_PTR(ret);
1343                 }
1344
1345                 funcs = &nouveau_connector_funcs_lvds;
1346                 break;
1347         case DRM_MODE_CONNECTOR_DisplayPort:
1348         case DRM_MODE_CONNECTOR_eDP:
1349                 nv_connector->aux.dev = dev->dev;
1350                 nv_connector->aux.transfer = nouveau_connector_aux_xfer;
1351                 snprintf(aux_name, sizeof(aux_name), "sor-%04x-%04x",
1352                          dcbe->hasht, dcbe->hashm);
1353                 nv_connector->aux.name = kstrdup(aux_name, GFP_KERNEL);
1354                 ret = drm_dp_aux_register(&nv_connector->aux);
1355                 if (ret) {
1356                         NV_ERROR(drm, "failed to register aux channel\n");
1357                         kfree(nv_connector);
1358                         return ERR_PTR(ret);
1359                 }
1360                 funcs = &nouveau_connector_funcs;
1361                 break;
1362         default:
1363                 funcs = &nouveau_connector_funcs;
1364                 break;
1365         }
1366
1367         /* HDMI 3D support */
1368         if ((disp->disp.object.oclass >= G82_DISP)
1369             && ((type == DRM_MODE_CONNECTOR_DisplayPort)
1370                 || (type == DRM_MODE_CONNECTOR_eDP)
1371                 || (type == DRM_MODE_CONNECTOR_HDMIA)))
1372                 connector->stereo_allowed = true;
1373
1374         /* defaults, will get overridden in detect() */
1375         connector->interlace_allowed = false;
1376         connector->doublescan_allowed = false;
1377
1378         drm_connector_init(dev, connector, funcs, type);
1379         drm_connector_helper_add(connector, &nouveau_connector_helper_funcs);
1380
1381         connector->funcs->reset(connector);
1382         nouveau_conn_attach_properties(connector);
1383
1384         /* Default scaling mode */
1385         switch (nv_connector->type) {
1386         case DCB_CONNECTOR_LVDS:
1387         case DCB_CONNECTOR_LVDS_SPWG:
1388         case DCB_CONNECTOR_eDP:
1389                 /* see note in nouveau_connector_set_property() */
1390                 if (disp->disp.object.oclass < NV50_DISP) {
1391                         nv_connector->scaling_mode = DRM_MODE_SCALE_FULLSCREEN;
1392                         break;
1393                 }
1394                 nv_connector->scaling_mode = DRM_MODE_SCALE_NONE;
1395                 break;
1396         default:
1397                 nv_connector->scaling_mode = DRM_MODE_SCALE_NONE;
1398                 break;
1399         }
1400
1401         /* dithering properties */
1402         switch (nv_connector->type) {
1403         case DCB_CONNECTOR_TV_0:
1404         case DCB_CONNECTOR_TV_1:
1405         case DCB_CONNECTOR_TV_3:
1406         case DCB_CONNECTOR_VGA:
1407                 break;
1408         default:
1409                 nv_connector->dithering_mode = DITHERING_MODE_AUTO;
1410                 break;
1411         }
1412
1413         switch (type) {
1414         case DRM_MODE_CONNECTOR_DisplayPort:
1415         case DRM_MODE_CONNECTOR_eDP:
1416                 drm_dp_cec_register_connector(&nv_connector->aux,
1417                                               connector->name, dev->dev);
1418                 break;
1419         }
1420
1421         ret = nvif_notify_init(&disp->disp.object, nouveau_connector_hotplug,
1422                                true, NV04_DISP_NTFY_CONN,
1423                                &(struct nvif_notify_conn_req_v0) {
1424                                 .mask = NVIF_NOTIFY_CONN_V0_ANY,
1425                                 .conn = index,
1426                                },
1427                                sizeof(struct nvif_notify_conn_req_v0),
1428                                sizeof(struct nvif_notify_conn_rep_v0),
1429                                &nv_connector->hpd);
1430         if (ret)
1431                 connector->polled = DRM_CONNECTOR_POLL_CONNECT;
1432         else
1433                 connector->polled = DRM_CONNECTOR_POLL_HPD;
1434
1435         drm_connector_register(connector);
1436         return connector;
1437 }