Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright (C) 2008 Maarten Maathuis.
0003  * All Rights Reserved.
0004  *
0005  * Permission is hereby granted, free of charge, to any person obtaining
0006  * a copy of this software and associated documentation files (the
0007  * "Software"), to deal in the Software without restriction, including
0008  * without limitation the rights to use, copy, modify, merge, publish,
0009  * distribute, sublicense, and/or sell copies of the Software, and to
0010  * permit persons to whom the Software is furnished to do so, subject to
0011  * the following conditions:
0012  *
0013  * The above copyright notice and this permission notice (including the
0014  * next paragraph) shall be included in all copies or substantial
0015  * portions of the Software.
0016  *
0017  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
0018  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
0019  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
0020  * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
0021  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
0022  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
0023  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
0024  *
0025  */
0026 
0027 #include <acpi/button.h>
0028 
0029 #include <linux/pm_runtime.h>
0030 #include <linux/vga_switcheroo.h>
0031 
0032 #include <drm/drm_atomic_helper.h>
0033 #include <drm/drm_edid.h>
0034 #include <drm/drm_crtc_helper.h>
0035 #include <drm/drm_probe_helper.h>
0036 #include <drm/drm_atomic.h>
0037 
0038 #include "nouveau_reg.h"
0039 #include "nouveau_drv.h"
0040 #include "dispnv04/hw.h"
0041 #include "dispnv50/disp.h"
0042 #include "nouveau_acpi.h"
0043 
0044 #include "nouveau_display.h"
0045 #include "nouveau_connector.h"
0046 #include "nouveau_encoder.h"
0047 #include "nouveau_crtc.h"
0048 
0049 #include <nvif/class.h>
0050 #include <nvif/cl0046.h>
0051 #include <nvif/event.h>
0052 
0053 struct drm_display_mode *
0054 nouveau_conn_native_mode(struct drm_connector *connector)
0055 {
0056     const struct drm_connector_helper_funcs *helper = connector->helper_private;
0057     struct nouveau_drm *drm = nouveau_drm(connector->dev);
0058     struct drm_device *dev = connector->dev;
0059     struct drm_display_mode *mode, *largest = NULL;
0060     int high_w = 0, high_h = 0, high_v = 0;
0061 
0062     list_for_each_entry(mode, &connector->probed_modes, head) {
0063         if (helper->mode_valid(connector, mode) != MODE_OK ||
0064             (mode->flags & DRM_MODE_FLAG_INTERLACE))
0065             continue;
0066 
0067         /* Use preferred mode if there is one.. */
0068         if (mode->type & DRM_MODE_TYPE_PREFERRED) {
0069             NV_DEBUG(drm, "native mode from preferred\n");
0070             return drm_mode_duplicate(dev, mode);
0071         }
0072 
0073         /* Otherwise, take the resolution with the largest width, then
0074          * height, then vertical refresh
0075          */
0076         if (mode->hdisplay < high_w)
0077             continue;
0078 
0079         if (mode->hdisplay == high_w && mode->vdisplay < high_h)
0080             continue;
0081 
0082         if (mode->hdisplay == high_w && mode->vdisplay == high_h &&
0083             drm_mode_vrefresh(mode) < high_v)
0084             continue;
0085 
0086         high_w = mode->hdisplay;
0087         high_h = mode->vdisplay;
0088         high_v = drm_mode_vrefresh(mode);
0089         largest = mode;
0090     }
0091 
0092     NV_DEBUG(drm, "native mode from largest: %dx%d@%d\n",
0093               high_w, high_h, high_v);
0094     return largest ? drm_mode_duplicate(dev, largest) : NULL;
0095 }
0096 
0097 int
0098 nouveau_conn_atomic_get_property(struct drm_connector *connector,
0099                  const struct drm_connector_state *state,
0100                  struct drm_property *property, u64 *val)
0101 {
0102     struct nouveau_conn_atom *asyc = nouveau_conn_atom(state);
0103     struct nouveau_display *disp = nouveau_display(connector->dev);
0104     struct drm_device *dev = connector->dev;
0105 
0106     if (property == dev->mode_config.scaling_mode_property)
0107         *val = asyc->scaler.mode;
0108     else if (property == disp->underscan_property)
0109         *val = asyc->scaler.underscan.mode;
0110     else if (property == disp->underscan_hborder_property)
0111         *val = asyc->scaler.underscan.hborder;
0112     else if (property == disp->underscan_vborder_property)
0113         *val = asyc->scaler.underscan.vborder;
0114     else if (property == disp->dithering_mode)
0115         *val = asyc->dither.mode;
0116     else if (property == disp->dithering_depth)
0117         *val = asyc->dither.depth;
0118     else if (property == disp->vibrant_hue_property)
0119         *val = asyc->procamp.vibrant_hue;
0120     else if (property == disp->color_vibrance_property)
0121         *val = asyc->procamp.color_vibrance;
0122     else
0123         return -EINVAL;
0124 
0125     return 0;
0126 }
0127 
0128 int
0129 nouveau_conn_atomic_set_property(struct drm_connector *connector,
0130                  struct drm_connector_state *state,
0131                  struct drm_property *property, u64 val)
0132 {
0133     struct drm_device *dev = connector->dev;
0134     struct nouveau_conn_atom *asyc = nouveau_conn_atom(state);
0135     struct nouveau_display *disp = nouveau_display(dev);
0136 
0137     if (property == dev->mode_config.scaling_mode_property) {
0138         switch (val) {
0139         case DRM_MODE_SCALE_NONE:
0140             /* We allow 'None' for EDID modes, even on a fixed
0141              * panel (some exist with support for lower refresh
0142              * rates, which people might want to use for power-
0143              * saving purposes).
0144              *
0145              * Non-EDID modes will force the use of GPU scaling
0146              * to the native mode regardless of this setting.
0147              */
0148             switch (connector->connector_type) {
0149             case DRM_MODE_CONNECTOR_LVDS:
0150             case DRM_MODE_CONNECTOR_eDP:
0151                 /* ... except prior to G80, where the code
0152                  * doesn't support such things.
0153                  */
0154                 if (disp->disp.object.oclass < NV50_DISP)
0155                     return -EINVAL;
0156                 break;
0157             default:
0158                 break;
0159             }
0160             break;
0161         case DRM_MODE_SCALE_FULLSCREEN:
0162         case DRM_MODE_SCALE_CENTER:
0163         case DRM_MODE_SCALE_ASPECT:
0164             break;
0165         default:
0166             return -EINVAL;
0167         }
0168 
0169         if (asyc->scaler.mode != val) {
0170             asyc->scaler.mode = val;
0171             asyc->set.scaler = true;
0172         }
0173     } else
0174     if (property == disp->underscan_property) {
0175         if (asyc->scaler.underscan.mode != val) {
0176             asyc->scaler.underscan.mode = val;
0177             asyc->set.scaler = true;
0178         }
0179     } else
0180     if (property == disp->underscan_hborder_property) {
0181         if (asyc->scaler.underscan.hborder != val) {
0182             asyc->scaler.underscan.hborder = val;
0183             asyc->set.scaler = true;
0184         }
0185     } else
0186     if (property == disp->underscan_vborder_property) {
0187         if (asyc->scaler.underscan.vborder != val) {
0188             asyc->scaler.underscan.vborder = val;
0189             asyc->set.scaler = true;
0190         }
0191     } else
0192     if (property == disp->dithering_mode) {
0193         if (asyc->dither.mode != val) {
0194             asyc->dither.mode = val;
0195             asyc->set.dither = true;
0196         }
0197     } else
0198     if (property == disp->dithering_depth) {
0199         if (asyc->dither.mode != val) {
0200             asyc->dither.depth = val;
0201             asyc->set.dither = true;
0202         }
0203     } else
0204     if (property == disp->vibrant_hue_property) {
0205         if (asyc->procamp.vibrant_hue != val) {
0206             asyc->procamp.vibrant_hue = val;
0207             asyc->set.procamp = true;
0208         }
0209     } else
0210     if (property == disp->color_vibrance_property) {
0211         if (asyc->procamp.color_vibrance != val) {
0212             asyc->procamp.color_vibrance = val;
0213             asyc->set.procamp = true;
0214         }
0215     } else {
0216         return -EINVAL;
0217     }
0218 
0219     return 0;
0220 }
0221 
0222 void
0223 nouveau_conn_atomic_destroy_state(struct drm_connector *connector,
0224                   struct drm_connector_state *state)
0225 {
0226     struct nouveau_conn_atom *asyc = nouveau_conn_atom(state);
0227     __drm_atomic_helper_connector_destroy_state(&asyc->state);
0228     kfree(asyc);
0229 }
0230 
0231 struct drm_connector_state *
0232 nouveau_conn_atomic_duplicate_state(struct drm_connector *connector)
0233 {
0234     struct nouveau_conn_atom *armc = nouveau_conn_atom(connector->state);
0235     struct nouveau_conn_atom *asyc;
0236     if (!(asyc = kmalloc(sizeof(*asyc), GFP_KERNEL)))
0237         return NULL;
0238     __drm_atomic_helper_connector_duplicate_state(connector, &asyc->state);
0239     asyc->dither = armc->dither;
0240     asyc->scaler = armc->scaler;
0241     asyc->procamp = armc->procamp;
0242     asyc->set.mask = 0;
0243     return &asyc->state;
0244 }
0245 
0246 void
0247 nouveau_conn_reset(struct drm_connector *connector)
0248 {
0249     struct nouveau_connector *nv_connector = nouveau_connector(connector);
0250     struct nouveau_conn_atom *asyc;
0251 
0252     if (drm_drv_uses_atomic_modeset(connector->dev)) {
0253         if (WARN_ON(!(asyc = kzalloc(sizeof(*asyc), GFP_KERNEL))))
0254             return;
0255 
0256         if (connector->state)
0257             nouveau_conn_atomic_destroy_state(connector,
0258                               connector->state);
0259 
0260         __drm_atomic_helper_connector_reset(connector, &asyc->state);
0261     } else {
0262         asyc = &nv_connector->properties_state;
0263     }
0264 
0265     asyc->dither.mode = DITHERING_MODE_AUTO;
0266     asyc->dither.depth = DITHERING_DEPTH_AUTO;
0267     asyc->scaler.mode = DRM_MODE_SCALE_NONE;
0268     asyc->scaler.underscan.mode = UNDERSCAN_OFF;
0269     asyc->procamp.color_vibrance = 150;
0270     asyc->procamp.vibrant_hue = 90;
0271 
0272     if (nouveau_display(connector->dev)->disp.object.oclass < NV50_DISP) {
0273         switch (connector->connector_type) {
0274         case DRM_MODE_CONNECTOR_LVDS:
0275             /* See note in nouveau_conn_atomic_set_property(). */
0276             asyc->scaler.mode = DRM_MODE_SCALE_FULLSCREEN;
0277             break;
0278         default:
0279             break;
0280         }
0281     }
0282 }
0283 
0284 void
0285 nouveau_conn_attach_properties(struct drm_connector *connector)
0286 {
0287     struct drm_device *dev = connector->dev;
0288     struct nouveau_display *disp = nouveau_display(dev);
0289     struct nouveau_connector *nv_connector = nouveau_connector(connector);
0290     struct nouveau_conn_atom *armc;
0291 
0292     if (drm_drv_uses_atomic_modeset(connector->dev))
0293         armc = nouveau_conn_atom(connector->state);
0294     else
0295         armc = &nv_connector->properties_state;
0296 
0297     /* Init DVI-I specific properties. */
0298     if (connector->connector_type == DRM_MODE_CONNECTOR_DVII)
0299         drm_object_attach_property(&connector->base, dev->mode_config.
0300                        dvi_i_subconnector_property, 0);
0301 
0302     /* Add overscan compensation options to digital outputs. */
0303     if (disp->underscan_property &&
0304         (connector->connector_type == DRM_MODE_CONNECTOR_DVID ||
0305          connector->connector_type == DRM_MODE_CONNECTOR_DVII ||
0306          connector->connector_type == DRM_MODE_CONNECTOR_HDMIA ||
0307          connector->connector_type == DRM_MODE_CONNECTOR_DisplayPort)) {
0308         drm_object_attach_property(&connector->base,
0309                        disp->underscan_property,
0310                        UNDERSCAN_OFF);
0311         drm_object_attach_property(&connector->base,
0312                        disp->underscan_hborder_property, 0);
0313         drm_object_attach_property(&connector->base,
0314                        disp->underscan_vborder_property, 0);
0315     }
0316 
0317     /* Add hue and saturation options. */
0318     if (disp->vibrant_hue_property)
0319         drm_object_attach_property(&connector->base,
0320                        disp->vibrant_hue_property,
0321                        armc->procamp.vibrant_hue);
0322     if (disp->color_vibrance_property)
0323         drm_object_attach_property(&connector->base,
0324                        disp->color_vibrance_property,
0325                        armc->procamp.color_vibrance);
0326 
0327     /* Scaling mode property. */
0328     switch (connector->connector_type) {
0329     case DRM_MODE_CONNECTOR_TV:
0330         break;
0331     case DRM_MODE_CONNECTOR_VGA:
0332         if (disp->disp.object.oclass < NV50_DISP)
0333             break; /* Can only scale on DFPs. */
0334         fallthrough;
0335     default:
0336         drm_object_attach_property(&connector->base, dev->mode_config.
0337                        scaling_mode_property,
0338                        armc->scaler.mode);
0339         break;
0340     }
0341 
0342     /* Dithering properties. */
0343     switch (connector->connector_type) {
0344     case DRM_MODE_CONNECTOR_TV:
0345     case DRM_MODE_CONNECTOR_VGA:
0346         break;
0347     default:
0348         if (disp->dithering_mode) {
0349             drm_object_attach_property(&connector->base,
0350                            disp->dithering_mode,
0351                            armc->dither.mode);
0352         }
0353         if (disp->dithering_depth) {
0354             drm_object_attach_property(&connector->base,
0355                            disp->dithering_depth,
0356                            armc->dither.depth);
0357         }
0358         break;
0359     }
0360 }
0361 
0362 MODULE_PARM_DESC(tv_disable, "Disable TV-out detection");
0363 int nouveau_tv_disable = 0;
0364 module_param_named(tv_disable, nouveau_tv_disable, int, 0400);
0365 
0366 MODULE_PARM_DESC(ignorelid, "Ignore ACPI lid status");
0367 int nouveau_ignorelid = 0;
0368 module_param_named(ignorelid, nouveau_ignorelid, int, 0400);
0369 
0370 MODULE_PARM_DESC(duallink, "Allow dual-link TMDS (default: enabled)");
0371 int nouveau_duallink = 1;
0372 module_param_named(duallink, nouveau_duallink, int, 0400);
0373 
0374 MODULE_PARM_DESC(hdmimhz, "Force a maximum HDMI pixel clock (in MHz)");
0375 int nouveau_hdmimhz = 0;
0376 module_param_named(hdmimhz, nouveau_hdmimhz, int, 0400);
0377 
0378 struct nouveau_encoder *
0379 find_encoder(struct drm_connector *connector, int type)
0380 {
0381     struct nouveau_encoder *nv_encoder;
0382     struct drm_encoder *enc;
0383 
0384     drm_connector_for_each_possible_encoder(connector, enc) {
0385         nv_encoder = nouveau_encoder(enc);
0386 
0387         if (type == DCB_OUTPUT_ANY ||
0388             (nv_encoder->dcb && nv_encoder->dcb->type == type))
0389             return nv_encoder;
0390     }
0391 
0392     return NULL;
0393 }
0394 
0395 static void
0396 nouveau_connector_destroy(struct drm_connector *connector)
0397 {
0398     struct nouveau_connector *nv_connector = nouveau_connector(connector);
0399     nvif_notify_dtor(&nv_connector->hpd);
0400     kfree(nv_connector->edid);
0401     drm_connector_unregister(connector);
0402     drm_connector_cleanup(connector);
0403     if (nv_connector->aux.transfer) {
0404         drm_dp_cec_unregister_connector(&nv_connector->aux);
0405         kfree(nv_connector->aux.name);
0406     }
0407     nvif_conn_dtor(&nv_connector->conn);
0408     kfree(connector);
0409 }
0410 
0411 static struct nouveau_encoder *
0412 nouveau_connector_ddc_detect(struct drm_connector *connector)
0413 {
0414     struct drm_device *dev = connector->dev;
0415     struct pci_dev *pdev = to_pci_dev(dev->dev);
0416     struct nouveau_encoder *nv_encoder = NULL, *found = NULL;
0417     struct drm_encoder *encoder;
0418     int ret;
0419     bool switcheroo_ddc = false;
0420 
0421     drm_connector_for_each_possible_encoder(connector, encoder) {
0422         nv_encoder = nouveau_encoder(encoder);
0423 
0424         switch (nv_encoder->dcb->type) {
0425         case DCB_OUTPUT_DP:
0426             ret = nouveau_dp_detect(nouveau_connector(connector),
0427                         nv_encoder);
0428             if (ret == NOUVEAU_DP_MST)
0429                 return NULL;
0430             else if (ret == NOUVEAU_DP_SST)
0431                 found = nv_encoder;
0432 
0433             break;
0434         case DCB_OUTPUT_LVDS:
0435             switcheroo_ddc = !!(vga_switcheroo_handler_flags() &
0436                         VGA_SWITCHEROO_CAN_SWITCH_DDC);
0437             fallthrough;
0438         default:
0439             if (!nv_encoder->i2c)
0440                 break;
0441 
0442             if (switcheroo_ddc)
0443                 vga_switcheroo_lock_ddc(pdev);
0444             if (nvkm_probe_i2c(nv_encoder->i2c, 0x50))
0445                 found = nv_encoder;
0446             if (switcheroo_ddc)
0447                 vga_switcheroo_unlock_ddc(pdev);
0448 
0449             break;
0450         }
0451         if (found)
0452             break;
0453     }
0454 
0455     return found;
0456 }
0457 
0458 static struct nouveau_encoder *
0459 nouveau_connector_of_detect(struct drm_connector *connector)
0460 {
0461 #ifdef __powerpc__
0462     struct drm_device *dev = connector->dev;
0463     struct nouveau_connector *nv_connector = nouveau_connector(connector);
0464     struct nouveau_encoder *nv_encoder;
0465     struct pci_dev *pdev = to_pci_dev(dev->dev);
0466     struct device_node *cn, *dn = pci_device_to_OF_node(pdev);
0467 
0468     if (!dn ||
0469         !((nv_encoder = find_encoder(connector, DCB_OUTPUT_TMDS)) ||
0470           (nv_encoder = find_encoder(connector, DCB_OUTPUT_ANALOG))))
0471         return NULL;
0472 
0473     for_each_child_of_node(dn, cn) {
0474         const char *name = of_get_property(cn, "name", NULL);
0475         const void *edid = of_get_property(cn, "EDID", NULL);
0476         int idx = name ? name[strlen(name) - 1] - 'A' : 0;
0477 
0478         if (nv_encoder->dcb->i2c_index == idx && edid) {
0479             nv_connector->edid =
0480                 kmemdup(edid, EDID_LENGTH, GFP_KERNEL);
0481             of_node_put(cn);
0482             return nv_encoder;
0483         }
0484     }
0485 #endif
0486     return NULL;
0487 }
0488 
0489 static void
0490 nouveau_connector_set_encoder(struct drm_connector *connector,
0491                   struct nouveau_encoder *nv_encoder)
0492 {
0493     struct nouveau_connector *nv_connector = nouveau_connector(connector);
0494     struct nouveau_drm *drm = nouveau_drm(connector->dev);
0495     struct drm_device *dev = connector->dev;
0496     struct pci_dev *pdev = to_pci_dev(dev->dev);
0497 
0498     if (nv_connector->detected_encoder == nv_encoder)
0499         return;
0500     nv_connector->detected_encoder = nv_encoder;
0501 
0502     if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_TESLA) {
0503         if (nv_encoder->dcb->type == DCB_OUTPUT_DP)
0504             connector->interlace_allowed =
0505                 nv_encoder->caps.dp_interlace;
0506         else
0507             connector->interlace_allowed = true;
0508         connector->doublescan_allowed = true;
0509     } else
0510     if (nv_encoder->dcb->type == DCB_OUTPUT_LVDS ||
0511         nv_encoder->dcb->type == DCB_OUTPUT_TMDS) {
0512         connector->doublescan_allowed = false;
0513         connector->interlace_allowed = false;
0514     } else {
0515         connector->doublescan_allowed = true;
0516         if (drm->client.device.info.family == NV_DEVICE_INFO_V0_KELVIN ||
0517             (drm->client.device.info.family == NV_DEVICE_INFO_V0_CELSIUS &&
0518              (pdev->device & 0x0ff0) != 0x0100 &&
0519              (pdev->device & 0x0ff0) != 0x0150))
0520             /* HW is broken */
0521             connector->interlace_allowed = false;
0522         else
0523             connector->interlace_allowed = true;
0524     }
0525 
0526     if (nv_connector->type == DCB_CONNECTOR_DVI_I) {
0527         drm_object_property_set_value(&connector->base,
0528             dev->mode_config.dvi_i_subconnector_property,
0529             nv_encoder->dcb->type == DCB_OUTPUT_TMDS ?
0530             DRM_MODE_SUBCONNECTOR_DVID :
0531             DRM_MODE_SUBCONNECTOR_DVIA);
0532     }
0533 }
0534 
0535 static void
0536 nouveau_connector_set_edid(struct nouveau_connector *nv_connector,
0537                struct edid *edid)
0538 {
0539     if (nv_connector->edid != edid) {
0540         struct edid *old_edid = nv_connector->edid;
0541 
0542         drm_connector_update_edid_property(&nv_connector->base, edid);
0543         kfree(old_edid);
0544         nv_connector->edid = edid;
0545     }
0546 }
0547 
0548 static enum drm_connector_status
0549 nouveau_connector_detect(struct drm_connector *connector, bool force)
0550 {
0551     struct drm_device *dev = connector->dev;
0552     struct nouveau_drm *drm = nouveau_drm(dev);
0553     struct nouveau_connector *nv_connector = nouveau_connector(connector);
0554     struct nouveau_encoder *nv_encoder = NULL;
0555     struct nouveau_encoder *nv_partner;
0556     struct i2c_adapter *i2c;
0557     int type;
0558     int ret;
0559     enum drm_connector_status conn_status = connector_status_disconnected;
0560 
0561     /* Outputs are only polled while runtime active, so resuming the
0562      * device here is unnecessary (and would deadlock upon runtime suspend
0563      * because it waits for polling to finish). We do however, want to
0564      * prevent the autosuspend timer from elapsing during this operation
0565      * if possible.
0566      */
0567     if (drm_kms_helper_is_poll_worker()) {
0568         pm_runtime_get_noresume(dev->dev);
0569     } else {
0570         ret = pm_runtime_get_sync(dev->dev);
0571         if (ret < 0 && ret != -EACCES) {
0572             pm_runtime_put_autosuspend(dev->dev);
0573             nouveau_connector_set_edid(nv_connector, NULL);
0574             return conn_status;
0575         }
0576     }
0577 
0578     nv_encoder = nouveau_connector_ddc_detect(connector);
0579     if (nv_encoder && (i2c = nv_encoder->i2c) != NULL) {
0580         struct edid *new_edid;
0581 
0582         if ((vga_switcheroo_handler_flags() &
0583              VGA_SWITCHEROO_CAN_SWITCH_DDC) &&
0584             nv_connector->type == DCB_CONNECTOR_LVDS)
0585             new_edid = drm_get_edid_switcheroo(connector, i2c);
0586         else
0587             new_edid = drm_get_edid(connector, i2c);
0588 
0589         nouveau_connector_set_edid(nv_connector, new_edid);
0590         if (!nv_connector->edid) {
0591             NV_ERROR(drm, "DDC responded, but no EDID for %s\n",
0592                  connector->name);
0593             goto detect_analog;
0594         }
0595 
0596         /* Override encoder type for DVI-I based on whether EDID
0597          * says the display is digital or analog, both use the
0598          * same i2c channel so the value returned from ddc_detect
0599          * isn't necessarily correct.
0600          */
0601         nv_partner = NULL;
0602         if (nv_encoder->dcb->type == DCB_OUTPUT_TMDS)
0603             nv_partner = find_encoder(connector, DCB_OUTPUT_ANALOG);
0604         if (nv_encoder->dcb->type == DCB_OUTPUT_ANALOG)
0605             nv_partner = find_encoder(connector, DCB_OUTPUT_TMDS);
0606 
0607         if (nv_partner && ((nv_encoder->dcb->type == DCB_OUTPUT_ANALOG &&
0608                     nv_partner->dcb->type == DCB_OUTPUT_TMDS) ||
0609                    (nv_encoder->dcb->type == DCB_OUTPUT_TMDS &&
0610                     nv_partner->dcb->type == DCB_OUTPUT_ANALOG))) {
0611             if (nv_connector->edid->input & DRM_EDID_INPUT_DIGITAL)
0612                 type = DCB_OUTPUT_TMDS;
0613             else
0614                 type = DCB_OUTPUT_ANALOG;
0615 
0616             nv_encoder = find_encoder(connector, type);
0617         }
0618 
0619         nouveau_connector_set_encoder(connector, nv_encoder);
0620         conn_status = connector_status_connected;
0621         drm_dp_cec_set_edid(&nv_connector->aux, nv_connector->edid);
0622         goto out;
0623     } else {
0624         nouveau_connector_set_edid(nv_connector, NULL);
0625     }
0626 
0627     nv_encoder = nouveau_connector_of_detect(connector);
0628     if (nv_encoder) {
0629         nouveau_connector_set_encoder(connector, nv_encoder);
0630         conn_status = connector_status_connected;
0631         goto out;
0632     }
0633 
0634 detect_analog:
0635     nv_encoder = find_encoder(connector, DCB_OUTPUT_ANALOG);
0636     if (!nv_encoder && !nouveau_tv_disable)
0637         nv_encoder = find_encoder(connector, DCB_OUTPUT_TV);
0638     if (nv_encoder && force) {
0639         struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
0640         const struct drm_encoder_helper_funcs *helper =
0641                         encoder->helper_private;
0642 
0643         if (helper->detect(encoder, connector) ==
0644                         connector_status_connected) {
0645             nouveau_connector_set_encoder(connector, nv_encoder);
0646             conn_status = connector_status_connected;
0647             goto out;
0648         }
0649     }
0650 
0651  out:
0652     if (!nv_connector->edid)
0653         drm_dp_cec_unset_edid(&nv_connector->aux);
0654 
0655     pm_runtime_mark_last_busy(dev->dev);
0656     pm_runtime_put_autosuspend(dev->dev);
0657 
0658     return conn_status;
0659 }
0660 
0661 static enum drm_connector_status
0662 nouveau_connector_detect_lvds(struct drm_connector *connector, bool force)
0663 {
0664     struct drm_device *dev = connector->dev;
0665     struct nouveau_drm *drm = nouveau_drm(dev);
0666     struct nouveau_connector *nv_connector = nouveau_connector(connector);
0667     struct nouveau_encoder *nv_encoder = NULL;
0668     struct edid *edid = NULL;
0669     enum drm_connector_status status = connector_status_disconnected;
0670 
0671     nv_encoder = find_encoder(connector, DCB_OUTPUT_LVDS);
0672     if (!nv_encoder)
0673         goto out;
0674 
0675     /* Try retrieving EDID via DDC */
0676     if (!drm->vbios.fp_no_ddc) {
0677         status = nouveau_connector_detect(connector, force);
0678         if (status == connector_status_connected) {
0679             edid = nv_connector->edid;
0680             goto out;
0681         }
0682     }
0683 
0684     /* On some laptops (Sony, i'm looking at you) there appears to
0685      * be no direct way of accessing the panel's EDID.  The only
0686      * option available to us appears to be to ask ACPI for help..
0687      *
0688      * It's important this check's before trying straps, one of the
0689      * said manufacturer's laptops are configured in such a way
0690      * the nouveau decides an entry in the VBIOS FP mode table is
0691      * valid - it's not (rh#613284)
0692      */
0693     if (nv_encoder->dcb->lvdsconf.use_acpi_for_edid) {
0694         edid = nouveau_acpi_edid(dev, connector);
0695         if (edid) {
0696             status = connector_status_connected;
0697             goto out;
0698         }
0699     }
0700 
0701     /* If no EDID found above, and the VBIOS indicates a hardcoded
0702      * modeline is avalilable for the panel, set it as the panel's
0703      * native mode and exit.
0704      */
0705     if (nouveau_bios_fp_mode(dev, NULL) && (drm->vbios.fp_no_ddc ||
0706         nv_encoder->dcb->lvdsconf.use_straps_for_mode)) {
0707         status = connector_status_connected;
0708         goto out;
0709     }
0710 
0711     /* Still nothing, some VBIOS images have a hardcoded EDID block
0712      * stored for the panel stored in them.
0713      */
0714     if (!drm->vbios.fp_no_ddc) {
0715         edid = (struct edid *)nouveau_bios_embedded_edid(dev);
0716         if (edid) {
0717             edid = kmemdup(edid, EDID_LENGTH, GFP_KERNEL);
0718             if (edid)
0719                 status = connector_status_connected;
0720         }
0721     }
0722 
0723 out:
0724 #if defined(CONFIG_ACPI_BUTTON) || \
0725     (defined(CONFIG_ACPI_BUTTON_MODULE) && defined(MODULE))
0726     if (status == connector_status_connected &&
0727         !nouveau_ignorelid && !acpi_lid_open())
0728         status = connector_status_unknown;
0729 #endif
0730 
0731     nouveau_connector_set_edid(nv_connector, edid);
0732     nouveau_connector_set_encoder(connector, nv_encoder);
0733     return status;
0734 }
0735 
0736 static void
0737 nouveau_connector_force(struct drm_connector *connector)
0738 {
0739     struct nouveau_drm *drm = nouveau_drm(connector->dev);
0740     struct nouveau_connector *nv_connector = nouveau_connector(connector);
0741     struct nouveau_encoder *nv_encoder;
0742     int type;
0743 
0744     if (nv_connector->type == DCB_CONNECTOR_DVI_I) {
0745         if (connector->force == DRM_FORCE_ON_DIGITAL)
0746             type = DCB_OUTPUT_TMDS;
0747         else
0748             type = DCB_OUTPUT_ANALOG;
0749     } else
0750         type = DCB_OUTPUT_ANY;
0751 
0752     nv_encoder = find_encoder(connector, type);
0753     if (!nv_encoder) {
0754         NV_ERROR(drm, "can't find encoder to force %s on!\n",
0755              connector->name);
0756         connector->status = connector_status_disconnected;
0757         return;
0758     }
0759 
0760     nouveau_connector_set_encoder(connector, nv_encoder);
0761 }
0762 
0763 static int
0764 nouveau_connector_set_property(struct drm_connector *connector,
0765                    struct drm_property *property, uint64_t value)
0766 {
0767     struct nouveau_connector *nv_connector = nouveau_connector(connector);
0768     struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
0769     struct nouveau_conn_atom *asyc = &nv_connector->properties_state;
0770     struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
0771     int ret;
0772 
0773     ret = connector->funcs->atomic_set_property(&nv_connector->base,
0774                             &asyc->state,
0775                             property, value);
0776     if (ret) {
0777         if (nv_encoder && nv_encoder->dcb->type == DCB_OUTPUT_TV)
0778             return get_slave_funcs(encoder)->set_property(
0779                 encoder, connector, property, value);
0780         return ret;
0781     }
0782 
0783     nv_connector->scaling_mode = asyc->scaler.mode;
0784     nv_connector->dithering_mode = asyc->dither.mode;
0785 
0786     if (connector->encoder && connector->encoder->crtc) {
0787         ret = drm_crtc_helper_set_mode(connector->encoder->crtc,
0788                           &connector->encoder->crtc->mode,
0789                            connector->encoder->crtc->x,
0790                            connector->encoder->crtc->y,
0791                            NULL);
0792         if (!ret)
0793             return -EINVAL;
0794     }
0795 
0796     return 0;
0797 }
0798 
0799 struct moderec {
0800     int hdisplay;
0801     int vdisplay;
0802 };
0803 
0804 static struct moderec scaler_modes[] = {
0805     { 1920, 1200 },
0806     { 1920, 1080 },
0807     { 1680, 1050 },
0808     { 1600, 1200 },
0809     { 1400, 1050 },
0810     { 1280, 1024 },
0811     { 1280, 960 },
0812     { 1152, 864 },
0813     { 1024, 768 },
0814     { 800, 600 },
0815     { 720, 400 },
0816     { 640, 480 },
0817     { 640, 400 },
0818     { 640, 350 },
0819     {}
0820 };
0821 
0822 static int
0823 nouveau_connector_scaler_modes_add(struct drm_connector *connector)
0824 {
0825     struct nouveau_connector *nv_connector = nouveau_connector(connector);
0826     struct drm_display_mode *native = nv_connector->native_mode, *m;
0827     struct drm_device *dev = connector->dev;
0828     struct moderec *mode = &scaler_modes[0];
0829     int modes = 0;
0830 
0831     if (!native)
0832         return 0;
0833 
0834     while (mode->hdisplay) {
0835         if (mode->hdisplay <= native->hdisplay &&
0836             mode->vdisplay <= native->vdisplay &&
0837             (mode->hdisplay != native->hdisplay ||
0838              mode->vdisplay != native->vdisplay)) {
0839             m = drm_cvt_mode(dev, mode->hdisplay, mode->vdisplay,
0840                      drm_mode_vrefresh(native), false,
0841                      false, false);
0842             if (!m)
0843                 continue;
0844 
0845             drm_mode_probed_add(connector, m);
0846             modes++;
0847         }
0848 
0849         mode++;
0850     }
0851 
0852     return modes;
0853 }
0854 
0855 static void
0856 nouveau_connector_detect_depth(struct drm_connector *connector)
0857 {
0858     struct nouveau_drm *drm = nouveau_drm(connector->dev);
0859     struct nouveau_connector *nv_connector = nouveau_connector(connector);
0860     struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
0861     struct nvbios *bios = &drm->vbios;
0862     struct drm_display_mode *mode = nv_connector->native_mode;
0863     bool duallink;
0864 
0865     /* if the edid is feeling nice enough to provide this info, use it */
0866     if (nv_connector->edid && connector->display_info.bpc)
0867         return;
0868 
0869     /* EDID 1.4 is *supposed* to be supported on eDP, but, Apple... */
0870     if (nv_connector->type == DCB_CONNECTOR_eDP) {
0871         connector->display_info.bpc = 6;
0872         return;
0873     }
0874 
0875     /* we're out of options unless we're LVDS, default to 8bpc */
0876     if (nv_encoder->dcb->type != DCB_OUTPUT_LVDS) {
0877         connector->display_info.bpc = 8;
0878         return;
0879     }
0880 
0881     connector->display_info.bpc = 6;
0882 
0883     /* LVDS: panel straps */
0884     if (bios->fp_no_ddc) {
0885         if (bios->fp.if_is_24bit)
0886             connector->display_info.bpc = 8;
0887         return;
0888     }
0889 
0890     /* LVDS: DDC panel, need to first determine the number of links to
0891      * know which if_is_24bit flag to check...
0892      */
0893     if (nv_connector->edid &&
0894         nv_connector->type == DCB_CONNECTOR_LVDS_SPWG)
0895         duallink = ((u8 *)nv_connector->edid)[121] == 2;
0896     else
0897         duallink = mode->clock >= bios->fp.duallink_transition_clk;
0898 
0899     if ((!duallink && (bios->fp.strapless_is_24bit & 1)) ||
0900         ( duallink && (bios->fp.strapless_is_24bit & 2)))
0901         connector->display_info.bpc = 8;
0902 }
0903 
0904 static int
0905 nouveau_connector_late_register(struct drm_connector *connector)
0906 {
0907     int ret;
0908 
0909     ret = nouveau_backlight_init(connector);
0910     if (ret)
0911         return ret;
0912 
0913     if (connector->connector_type == DRM_MODE_CONNECTOR_eDP ||
0914         connector->connector_type == DRM_MODE_CONNECTOR_DisplayPort) {
0915         ret = drm_dp_aux_register(&nouveau_connector(connector)->aux);
0916         if (ret)
0917             goto backlight_fini;
0918     }
0919 
0920     return 0;
0921 backlight_fini:
0922     nouveau_backlight_fini(connector);
0923     return ret;
0924 }
0925 
0926 static void
0927 nouveau_connector_early_unregister(struct drm_connector *connector)
0928 {
0929     if (connector->connector_type == DRM_MODE_CONNECTOR_eDP ||
0930         connector->connector_type == DRM_MODE_CONNECTOR_DisplayPort)
0931         drm_dp_aux_unregister(&nouveau_connector(connector)->aux);
0932 
0933     nouveau_backlight_fini(connector);
0934 }
0935 
0936 static int
0937 nouveau_connector_get_modes(struct drm_connector *connector)
0938 {
0939     struct drm_device *dev = connector->dev;
0940     struct nouveau_drm *drm = nouveau_drm(dev);
0941     struct nouveau_connector *nv_connector = nouveau_connector(connector);
0942     struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
0943     struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
0944     int ret = 0;
0945 
0946     /* destroy the native mode, the attached monitor could have changed.
0947      */
0948     if (nv_connector->native_mode) {
0949         drm_mode_destroy(dev, nv_connector->native_mode);
0950         nv_connector->native_mode = NULL;
0951     }
0952 
0953     if (nv_connector->edid)
0954         ret = drm_add_edid_modes(connector, nv_connector->edid);
0955     else
0956     if (nv_encoder->dcb->type == DCB_OUTPUT_LVDS &&
0957         (nv_encoder->dcb->lvdsconf.use_straps_for_mode ||
0958          drm->vbios.fp_no_ddc) && nouveau_bios_fp_mode(dev, NULL)) {
0959         struct drm_display_mode mode;
0960 
0961         nouveau_bios_fp_mode(dev, &mode);
0962         nv_connector->native_mode = drm_mode_duplicate(dev, &mode);
0963     }
0964 
0965     /* Determine display colour depth for everything except LVDS now,
0966      * DP requires this before mode_valid() is called.
0967      */
0968     if (connector->connector_type != DRM_MODE_CONNECTOR_LVDS)
0969         nouveau_connector_detect_depth(connector);
0970 
0971     /* Find the native mode if this is a digital panel, if we didn't
0972      * find any modes through DDC previously add the native mode to
0973      * the list of modes.
0974      */
0975     if (!nv_connector->native_mode)
0976         nv_connector->native_mode = nouveau_conn_native_mode(connector);
0977     if (ret == 0 && nv_connector->native_mode) {
0978         struct drm_display_mode *mode;
0979 
0980         mode = drm_mode_duplicate(dev, nv_connector->native_mode);
0981         drm_mode_probed_add(connector, mode);
0982         ret = 1;
0983     }
0984 
0985     /* Determine LVDS colour depth, must happen after determining
0986      * "native" mode as some VBIOS tables require us to use the
0987      * pixel clock as part of the lookup...
0988      */
0989     if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS)
0990         nouveau_connector_detect_depth(connector);
0991 
0992     if (nv_encoder->dcb->type == DCB_OUTPUT_TV)
0993         ret = get_slave_funcs(encoder)->get_modes(encoder, connector);
0994 
0995     if (nv_connector->type == DCB_CONNECTOR_LVDS ||
0996         nv_connector->type == DCB_CONNECTOR_LVDS_SPWG ||
0997         nv_connector->type == DCB_CONNECTOR_eDP)
0998         ret += nouveau_connector_scaler_modes_add(connector);
0999 
1000     return ret;
1001 }
1002 
1003 static unsigned
1004 get_tmds_link_bandwidth(struct drm_connector *connector)
1005 {
1006     struct nouveau_connector *nv_connector = nouveau_connector(connector);
1007     struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
1008     struct nouveau_drm *drm = nouveau_drm(connector->dev);
1009     struct dcb_output *dcb = nv_connector->detected_encoder->dcb;
1010     struct drm_display_info *info = NULL;
1011     unsigned duallink_scale =
1012         nouveau_duallink && nv_encoder->dcb->duallink_possible ? 2 : 1;
1013 
1014     if (drm_detect_hdmi_monitor(nv_connector->edid)) {
1015         info = &nv_connector->base.display_info;
1016         duallink_scale = 1;
1017     }
1018 
1019     if (info) {
1020         if (nouveau_hdmimhz > 0)
1021             return nouveau_hdmimhz * 1000;
1022         /* Note: these limits are conservative, some Fermi's
1023          * can do 297 MHz. Unclear how this can be determined.
1024          */
1025         if (drm->client.device.info.chipset >= 0x120) {
1026             const int max_tmds_clock =
1027                 info->hdmi.scdc.scrambling.supported ?
1028                 594000 : 340000;
1029             return info->max_tmds_clock ?
1030                 min(info->max_tmds_clock, max_tmds_clock) :
1031                 max_tmds_clock;
1032         }
1033         if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_KEPLER)
1034             return 297000;
1035         if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_FERMI)
1036             return 225000;
1037     }
1038 
1039     if (dcb->location != DCB_LOC_ON_CHIP ||
1040         drm->client.device.info.chipset >= 0x46)
1041         return 165000 * duallink_scale;
1042     else if (drm->client.device.info.chipset >= 0x40)
1043         return 155000 * duallink_scale;
1044     else if (drm->client.device.info.chipset >= 0x18)
1045         return 135000 * duallink_scale;
1046     else
1047         return 112000 * duallink_scale;
1048 }
1049 
1050 static enum drm_mode_status
1051 nouveau_connector_mode_valid(struct drm_connector *connector,
1052                  struct drm_display_mode *mode)
1053 {
1054     struct nouveau_connector *nv_connector = nouveau_connector(connector);
1055     struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
1056     struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
1057     unsigned int min_clock = 25000, max_clock = min_clock, clock = mode->clock;
1058 
1059     switch (nv_encoder->dcb->type) {
1060     case DCB_OUTPUT_LVDS:
1061         if (nv_connector->native_mode &&
1062             (mode->hdisplay > nv_connector->native_mode->hdisplay ||
1063              mode->vdisplay > nv_connector->native_mode->vdisplay))
1064             return MODE_PANEL;
1065 
1066         min_clock = 0;
1067         max_clock = 400000;
1068         break;
1069     case DCB_OUTPUT_TMDS:
1070         max_clock = get_tmds_link_bandwidth(connector);
1071         break;
1072     case DCB_OUTPUT_ANALOG:
1073         max_clock = nv_encoder->dcb->crtconf.maxfreq;
1074         if (!max_clock)
1075             max_clock = 350000;
1076         break;
1077     case DCB_OUTPUT_TV:
1078         return get_slave_funcs(encoder)->mode_valid(encoder, mode);
1079     case DCB_OUTPUT_DP:
1080         return nv50_dp_mode_valid(connector, nv_encoder, mode, NULL);
1081     default:
1082         BUG();
1083         return MODE_BAD;
1084     }
1085 
1086     if ((mode->flags & DRM_MODE_FLAG_3D_MASK) == DRM_MODE_FLAG_3D_FRAME_PACKING)
1087         clock *= 2;
1088 
1089     if (clock < min_clock)
1090         return MODE_CLOCK_LOW;
1091     if (clock > max_clock)
1092         return MODE_CLOCK_HIGH;
1093 
1094     return MODE_OK;
1095 }
1096 
1097 static struct drm_encoder *
1098 nouveau_connector_best_encoder(struct drm_connector *connector)
1099 {
1100     struct nouveau_connector *nv_connector = nouveau_connector(connector);
1101 
1102     if (nv_connector->detected_encoder)
1103         return to_drm_encoder(nv_connector->detected_encoder);
1104 
1105     return NULL;
1106 }
1107 
1108 static const struct drm_connector_helper_funcs
1109 nouveau_connector_helper_funcs = {
1110     .get_modes = nouveau_connector_get_modes,
1111     .mode_valid = nouveau_connector_mode_valid,
1112     .best_encoder = nouveau_connector_best_encoder,
1113 };
1114 
1115 static const struct drm_connector_funcs
1116 nouveau_connector_funcs = {
1117     .dpms = drm_helper_connector_dpms,
1118     .reset = nouveau_conn_reset,
1119     .detect = nouveau_connector_detect,
1120     .force = nouveau_connector_force,
1121     .fill_modes = drm_helper_probe_single_connector_modes,
1122     .set_property = nouveau_connector_set_property,
1123     .destroy = nouveau_connector_destroy,
1124     .atomic_duplicate_state = nouveau_conn_atomic_duplicate_state,
1125     .atomic_destroy_state = nouveau_conn_atomic_destroy_state,
1126     .atomic_set_property = nouveau_conn_atomic_set_property,
1127     .atomic_get_property = nouveau_conn_atomic_get_property,
1128     .late_register = nouveau_connector_late_register,
1129     .early_unregister = nouveau_connector_early_unregister,
1130 };
1131 
1132 static const struct drm_connector_funcs
1133 nouveau_connector_funcs_lvds = {
1134     .dpms = drm_helper_connector_dpms,
1135     .reset = nouveau_conn_reset,
1136     .detect = nouveau_connector_detect_lvds,
1137     .force = nouveau_connector_force,
1138     .fill_modes = drm_helper_probe_single_connector_modes,
1139     .set_property = nouveau_connector_set_property,
1140     .destroy = nouveau_connector_destroy,
1141     .atomic_duplicate_state = nouveau_conn_atomic_duplicate_state,
1142     .atomic_destroy_state = nouveau_conn_atomic_destroy_state,
1143     .atomic_set_property = nouveau_conn_atomic_set_property,
1144     .atomic_get_property = nouveau_conn_atomic_get_property,
1145     .late_register = nouveau_connector_late_register,
1146     .early_unregister = nouveau_connector_early_unregister,
1147 };
1148 
1149 void
1150 nouveau_connector_hpd(struct drm_connector *connector)
1151 {
1152     struct nouveau_drm *drm = nouveau_drm(connector->dev);
1153     u32 mask = drm_connector_mask(connector);
1154 
1155     mutex_lock(&drm->hpd_lock);
1156     if (!(drm->hpd_pending & mask)) {
1157         drm->hpd_pending |= mask;
1158         schedule_work(&drm->hpd_work);
1159     }
1160     mutex_unlock(&drm->hpd_lock);
1161 }
1162 
1163 static int
1164 nouveau_connector_hotplug(struct nvif_notify *notify)
1165 {
1166     struct nouveau_connector *nv_connector =
1167         container_of(notify, typeof(*nv_connector), hpd);
1168     struct drm_connector *connector = &nv_connector->base;
1169     struct drm_device *dev = connector->dev;
1170     struct nouveau_drm *drm = nouveau_drm(dev);
1171     const struct nvif_notify_conn_rep_v0 *rep = notify->data;
1172     bool plugged = (rep->mask != NVIF_NOTIFY_CONN_V0_UNPLUG);
1173 
1174     if (rep->mask & NVIF_NOTIFY_CONN_V0_IRQ) {
1175         nouveau_dp_irq(drm, nv_connector);
1176         return NVIF_NOTIFY_KEEP;
1177     }
1178 
1179     NV_DEBUG(drm, "%splugged %s\n", plugged ? "" : "un", connector->name);
1180     nouveau_connector_hpd(connector);
1181 
1182     return NVIF_NOTIFY_KEEP;
1183 }
1184 
1185 static ssize_t
1186 nouveau_connector_aux_xfer(struct drm_dp_aux *obj, struct drm_dp_aux_msg *msg)
1187 {
1188     struct nouveau_connector *nv_connector =
1189         container_of(obj, typeof(*nv_connector), aux);
1190     struct nouveau_encoder *nv_encoder;
1191     struct nvkm_i2c_aux *aux;
1192     u8 size = msg->size;
1193     int ret;
1194 
1195     nv_encoder = find_encoder(&nv_connector->base, DCB_OUTPUT_DP);
1196     if (!nv_encoder || !(aux = nv_encoder->aux))
1197         return -ENODEV;
1198     if (WARN_ON(msg->size > 16))
1199         return -E2BIG;
1200 
1201     ret = nvkm_i2c_aux_acquire(aux);
1202     if (ret)
1203         return ret;
1204 
1205     ret = nvkm_i2c_aux_xfer(aux, false, msg->request, msg->address,
1206                 msg->buffer, &size);
1207     nvkm_i2c_aux_release(aux);
1208     if (ret >= 0) {
1209         msg->reply = ret;
1210         return size;
1211     }
1212 
1213     return ret;
1214 }
1215 
1216 static int
1217 drm_conntype_from_dcb(enum dcb_connector_type dcb)
1218 {
1219     switch (dcb) {
1220     case DCB_CONNECTOR_VGA      : return DRM_MODE_CONNECTOR_VGA;
1221     case DCB_CONNECTOR_TV_0     :
1222     case DCB_CONNECTOR_TV_1     :
1223     case DCB_CONNECTOR_TV_3     : return DRM_MODE_CONNECTOR_TV;
1224     case DCB_CONNECTOR_DMS59_0  :
1225     case DCB_CONNECTOR_DMS59_1  :
1226     case DCB_CONNECTOR_DVI_I    : return DRM_MODE_CONNECTOR_DVII;
1227     case DCB_CONNECTOR_DVI_D    : return DRM_MODE_CONNECTOR_DVID;
1228     case DCB_CONNECTOR_LVDS     :
1229     case DCB_CONNECTOR_LVDS_SPWG: return DRM_MODE_CONNECTOR_LVDS;
1230     case DCB_CONNECTOR_DMS59_DP0:
1231     case DCB_CONNECTOR_DMS59_DP1:
1232     case DCB_CONNECTOR_DP       :
1233     case DCB_CONNECTOR_mDP      :
1234     case DCB_CONNECTOR_USB_C    : return DRM_MODE_CONNECTOR_DisplayPort;
1235     case DCB_CONNECTOR_eDP      : return DRM_MODE_CONNECTOR_eDP;
1236     case DCB_CONNECTOR_HDMI_0   :
1237     case DCB_CONNECTOR_HDMI_1   :
1238     case DCB_CONNECTOR_HDMI_C   : return DRM_MODE_CONNECTOR_HDMIA;
1239     case DCB_CONNECTOR_WFD      : return DRM_MODE_CONNECTOR_VIRTUAL;
1240     default:
1241         break;
1242     }
1243 
1244     return DRM_MODE_CONNECTOR_Unknown;
1245 }
1246 
1247 struct drm_connector *
1248 nouveau_connector_create(struct drm_device *dev,
1249              const struct dcb_output *dcbe)
1250 {
1251     const struct drm_connector_funcs *funcs = &nouveau_connector_funcs;
1252     struct nouveau_drm *drm = nouveau_drm(dev);
1253     struct nouveau_display *disp = nouveau_display(dev);
1254     struct nouveau_connector *nv_connector = NULL;
1255     struct drm_connector *connector;
1256     struct drm_connector_list_iter conn_iter;
1257     char aux_name[48] = {0};
1258     int index = dcbe->connector;
1259     int type, ret = 0;
1260     bool dummy;
1261 
1262     drm_connector_list_iter_begin(dev, &conn_iter);
1263     nouveau_for_each_non_mst_connector_iter(connector, &conn_iter) {
1264         nv_connector = nouveau_connector(connector);
1265         if (nv_connector->index == index) {
1266             drm_connector_list_iter_end(&conn_iter);
1267             return connector;
1268         }
1269     }
1270     drm_connector_list_iter_end(&conn_iter);
1271 
1272     nv_connector = kzalloc(sizeof(*nv_connector), GFP_KERNEL);
1273     if (!nv_connector)
1274         return ERR_PTR(-ENOMEM);
1275 
1276     connector = &nv_connector->base;
1277     nv_connector->index = index;
1278 
1279     /* attempt to parse vbios connector type and hotplug gpio */
1280     nv_connector->dcb = olddcb_conn(dev, index);
1281     if (nv_connector->dcb) {
1282         u32 entry = ROM16(nv_connector->dcb[0]);
1283         if (olddcb_conntab(dev)[3] >= 4)
1284             entry |= (u32)ROM16(nv_connector->dcb[2]) << 16;
1285 
1286         nv_connector->type = nv_connector->dcb[0];
1287         if (drm_conntype_from_dcb(nv_connector->type) ==
1288                       DRM_MODE_CONNECTOR_Unknown) {
1289             NV_WARN(drm, "unknown connector type %02x\n",
1290                 nv_connector->type);
1291             nv_connector->type = DCB_CONNECTOR_NONE;
1292         }
1293 
1294         /* Gigabyte NX85T */
1295         if (nv_match_device(dev, 0x0421, 0x1458, 0x344c)) {
1296             if (nv_connector->type == DCB_CONNECTOR_HDMI_1)
1297                 nv_connector->type = DCB_CONNECTOR_DVI_I;
1298         }
1299 
1300         /* Gigabyte GV-NX86T512H */
1301         if (nv_match_device(dev, 0x0402, 0x1458, 0x3455)) {
1302             if (nv_connector->type == DCB_CONNECTOR_HDMI_1)
1303                 nv_connector->type = DCB_CONNECTOR_DVI_I;
1304         }
1305     } else {
1306         nv_connector->type = DCB_CONNECTOR_NONE;
1307     }
1308 
1309     /* no vbios data, or an unknown dcb connector type - attempt to
1310      * figure out something suitable ourselves
1311      */
1312     if (nv_connector->type == DCB_CONNECTOR_NONE) {
1313         struct nouveau_drm *drm = nouveau_drm(dev);
1314         struct dcb_table *dcbt = &drm->vbios.dcb;
1315         u32 encoders = 0;
1316         int i;
1317 
1318         for (i = 0; i < dcbt->entries; i++) {
1319             if (dcbt->entry[i].connector == nv_connector->index)
1320                 encoders |= (1 << dcbt->entry[i].type);
1321         }
1322 
1323         if (encoders & (1 << DCB_OUTPUT_DP)) {
1324             if (encoders & (1 << DCB_OUTPUT_TMDS))
1325                 nv_connector->type = DCB_CONNECTOR_DP;
1326             else
1327                 nv_connector->type = DCB_CONNECTOR_eDP;
1328         } else
1329         if (encoders & (1 << DCB_OUTPUT_TMDS)) {
1330             if (encoders & (1 << DCB_OUTPUT_ANALOG))
1331                 nv_connector->type = DCB_CONNECTOR_DVI_I;
1332             else
1333                 nv_connector->type = DCB_CONNECTOR_DVI_D;
1334         } else
1335         if (encoders & (1 << DCB_OUTPUT_ANALOG)) {
1336             nv_connector->type = DCB_CONNECTOR_VGA;
1337         } else
1338         if (encoders & (1 << DCB_OUTPUT_LVDS)) {
1339             nv_connector->type = DCB_CONNECTOR_LVDS;
1340         } else
1341         if (encoders & (1 << DCB_OUTPUT_TV)) {
1342             nv_connector->type = DCB_CONNECTOR_TV_0;
1343         }
1344     }
1345 
1346     switch ((type = drm_conntype_from_dcb(nv_connector->type))) {
1347     case DRM_MODE_CONNECTOR_LVDS:
1348         ret = nouveau_bios_parse_lvds_table(dev, 0, &dummy, &dummy);
1349         if (ret) {
1350             NV_ERROR(drm, "Error parsing LVDS table, disabling\n");
1351             kfree(nv_connector);
1352             return ERR_PTR(ret);
1353         }
1354 
1355         funcs = &nouveau_connector_funcs_lvds;
1356         break;
1357     case DRM_MODE_CONNECTOR_DisplayPort:
1358     case DRM_MODE_CONNECTOR_eDP:
1359         nv_connector->aux.dev = connector->kdev;
1360         nv_connector->aux.drm_dev = dev;
1361         nv_connector->aux.transfer = nouveau_connector_aux_xfer;
1362         snprintf(aux_name, sizeof(aux_name), "sor-%04x-%04x",
1363              dcbe->hasht, dcbe->hashm);
1364         nv_connector->aux.name = kstrdup(aux_name, GFP_KERNEL);
1365         if (!nv_connector->aux.name) {
1366             kfree(nv_connector);
1367             return ERR_PTR(-ENOMEM);
1368         }
1369         drm_dp_aux_init(&nv_connector->aux);
1370         fallthrough;
1371     default:
1372         funcs = &nouveau_connector_funcs;
1373         break;
1374     }
1375 
1376     /* HDMI 3D support */
1377     if ((disp->disp.object.oclass >= G82_DISP)
1378         && ((type == DRM_MODE_CONNECTOR_DisplayPort)
1379         || (type == DRM_MODE_CONNECTOR_eDP)
1380         || (type == DRM_MODE_CONNECTOR_HDMIA)))
1381         connector->stereo_allowed = true;
1382 
1383     /* defaults, will get overridden in detect() */
1384     connector->interlace_allowed = false;
1385     connector->doublescan_allowed = false;
1386 
1387     drm_connector_init(dev, connector, funcs, type);
1388     drm_connector_helper_add(connector, &nouveau_connector_helper_funcs);
1389 
1390     if (nv_connector->dcb && (disp->disp.conn_mask & BIT(nv_connector->index))) {
1391         ret = nvif_conn_ctor(&disp->disp, nv_connector->base.name, nv_connector->index,
1392                      &nv_connector->conn);
1393         if (ret) {
1394             kfree(nv_connector);
1395             return ERR_PTR(ret);
1396         }
1397     }
1398 
1399     connector->funcs->reset(connector);
1400     nouveau_conn_attach_properties(connector);
1401 
1402     /* Default scaling mode */
1403     switch (nv_connector->type) {
1404     case DCB_CONNECTOR_LVDS:
1405     case DCB_CONNECTOR_LVDS_SPWG:
1406     case DCB_CONNECTOR_eDP:
1407         /* see note in nouveau_connector_set_property() */
1408         if (disp->disp.object.oclass < NV50_DISP) {
1409             nv_connector->scaling_mode = DRM_MODE_SCALE_FULLSCREEN;
1410             break;
1411         }
1412         nv_connector->scaling_mode = DRM_MODE_SCALE_NONE;
1413         break;
1414     default:
1415         nv_connector->scaling_mode = DRM_MODE_SCALE_NONE;
1416         break;
1417     }
1418 
1419     /* dithering properties */
1420     switch (nv_connector->type) {
1421     case DCB_CONNECTOR_TV_0:
1422     case DCB_CONNECTOR_TV_1:
1423     case DCB_CONNECTOR_TV_3:
1424     case DCB_CONNECTOR_VGA:
1425         break;
1426     default:
1427         nv_connector->dithering_mode = DITHERING_MODE_AUTO;
1428         break;
1429     }
1430 
1431     switch (type) {
1432     case DRM_MODE_CONNECTOR_DisplayPort:
1433     case DRM_MODE_CONNECTOR_eDP:
1434         drm_dp_cec_register_connector(&nv_connector->aux, connector);
1435         break;
1436     }
1437 
1438     ret = nvif_notify_ctor(&disp->disp.object, "kmsHotplug",
1439                    nouveau_connector_hotplug,
1440                    true, NV04_DISP_NTFY_CONN,
1441                    &(struct nvif_notify_conn_req_v0) {
1442                 .mask = NVIF_NOTIFY_CONN_V0_ANY,
1443                 .conn = index,
1444                    },
1445                    sizeof(struct nvif_notify_conn_req_v0),
1446                    sizeof(struct nvif_notify_conn_rep_v0),
1447                    &nv_connector->hpd);
1448     if (ret)
1449         connector->polled = DRM_CONNECTOR_POLL_CONNECT;
1450     else
1451         connector->polled = DRM_CONNECTOR_POLL_HPD;
1452 
1453     drm_connector_register(connector);
1454     return connector;
1455 }