Home | History | Annotate | Line # | Download | only in nouveau
nouveau_connector.c revision 1.1.1.3
      1 /*	$NetBSD: nouveau_connector.c,v 1.1.1.3 2018/08/27 01:34:55 riastradh Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 2008 Maarten Maathuis.
      5  * All Rights Reserved.
      6  *
      7  * Permission is hereby granted, free of charge, to any person obtaining
      8  * a copy of this software and associated documentation files (the
      9  * "Software"), to deal in the Software without restriction, including
     10  * without limitation the rights to use, copy, modify, merge, publish,
     11  * distribute, sublicense, and/or sell copies of the Software, and to
     12  * permit persons to whom the Software is furnished to do so, subject to
     13  * the following conditions:
     14  *
     15  * The above copyright notice and this permission notice (including the
     16  * next paragraph) shall be included in all copies or substantial
     17  * portions of the Software.
     18  *
     19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
     20  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
     21  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
     22  * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
     23  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
     24  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
     25  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     26  *
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: nouveau_connector.c,v 1.1.1.3 2018/08/27 01:34:55 riastradh Exp $");
     31 
     32 #include <acpi/button.h>
     33 
     34 #include <linux/pm_runtime.h>
     35 
     36 #include <drm/drmP.h>
     37 #include <drm/drm_edid.h>
     38 #include <drm/drm_crtc_helper.h>
     39 
     40 #include "nouveau_reg.h"
     41 #include "nouveau_drm.h"
     42 #include "dispnv04/hw.h"
     43 #include "nouveau_acpi.h"
     44 
     45 #include "nouveau_display.h"
     46 #include "nouveau_connector.h"
     47 #include "nouveau_encoder.h"
     48 #include "nouveau_crtc.h"
     49 
     50 #include <nvif/event.h>
     51 
     52 MODULE_PARM_DESC(tv_disable, "Disable TV-out detection");
     53 int nouveau_tv_disable = 0;
     54 module_param_named(tv_disable, nouveau_tv_disable, int, 0400);
     55 
     56 MODULE_PARM_DESC(ignorelid, "Ignore ACPI lid status");
     57 int nouveau_ignorelid = 0;
     58 module_param_named(ignorelid, nouveau_ignorelid, int, 0400);
     59 
     60 MODULE_PARM_DESC(duallink, "Allow dual-link TMDS (default: enabled)");
     61 int nouveau_duallink = 1;
     62 module_param_named(duallink, nouveau_duallink, int, 0400);
     63 
     64 struct nouveau_encoder *
     65 find_encoder(struct drm_connector *connector, int type)
     66 {
     67 	struct drm_device *dev = connector->dev;
     68 	struct nouveau_encoder *nv_encoder;
     69 	struct drm_encoder *enc;
     70 	int i, id;
     71 
     72 	for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
     73 		id = connector->encoder_ids[i];
     74 		if (!id)
     75 			break;
     76 
     77 		enc = drm_encoder_find(dev, id);
     78 		if (!enc)
     79 			continue;
     80 		nv_encoder = nouveau_encoder(enc);
     81 
     82 		if (type == DCB_OUTPUT_ANY ||
     83 		    (nv_encoder->dcb && nv_encoder->dcb->type == type))
     84 			return nv_encoder;
     85 	}
     86 
     87 	return NULL;
     88 }
     89 
     90 struct nouveau_connector *
     91 nouveau_encoder_connector_get(struct nouveau_encoder *encoder)
     92 {
     93 	struct drm_device *dev = to_drm_encoder(encoder)->dev;
     94 	struct drm_connector *drm_connector;
     95 
     96 	list_for_each_entry(drm_connector, &dev->mode_config.connector_list, head) {
     97 		if (drm_connector->encoder == to_drm_encoder(encoder))
     98 			return nouveau_connector(drm_connector);
     99 	}
    100 
    101 	return NULL;
    102 }
    103 
    104 static void
    105 nouveau_connector_destroy(struct drm_connector *connector)
    106 {
    107 	struct nouveau_connector *nv_connector = nouveau_connector(connector);
    108 	nvif_notify_fini(&nv_connector->hpd);
    109 	kfree(nv_connector->edid);
    110 	drm_connector_unregister(connector);
    111 	drm_connector_cleanup(connector);
    112 	if (nv_connector->aux.transfer)
    113 		drm_dp_aux_unregister(&nv_connector->aux);
    114 	kfree(connector);
    115 }
    116 
    117 static struct nouveau_encoder *
    118 nouveau_connector_ddc_detect(struct drm_connector *connector)
    119 {
    120 	struct drm_device *dev = connector->dev;
    121 	struct nouveau_connector *nv_connector = nouveau_connector(connector);
    122 	struct nouveau_drm *drm = nouveau_drm(dev);
    123 	struct nvkm_gpio *gpio = nvxx_gpio(&drm->device);
    124 	struct nouveau_encoder *nv_encoder;
    125 	struct drm_encoder *encoder;
    126 	int i, panel = -ENODEV;
    127 
    128 	/* eDP panels need powering on by us (if the VBIOS doesn't default it
    129 	 * to on) before doing any AUX channel transactions.  LVDS panel power
    130 	 * is handled by the SOR itself, and not required for LVDS DDC.
    131 	 */
    132 	if (nv_connector->type == DCB_CONNECTOR_eDP) {
    133 		panel = nvkm_gpio_get(gpio, 0, DCB_GPIO_PANEL_POWER, 0xff);
    134 		if (panel == 0) {
    135 			nvkm_gpio_set(gpio, 0, DCB_GPIO_PANEL_POWER, 0xff, 1);
    136 			msleep(300);
    137 		}
    138 	}
    139 
    140 	for (i = 0; nv_encoder = NULL, i < DRM_CONNECTOR_MAX_ENCODER; i++) {
    141 		int id = connector->encoder_ids[i];
    142 		if (id == 0)
    143 			break;
    144 
    145 		encoder = drm_encoder_find(dev, id);
    146 		if (!encoder)
    147 			continue;
    148 		nv_encoder = nouveau_encoder(encoder);
    149 
    150 		if (nv_encoder->dcb->type == DCB_OUTPUT_DP) {
    151 			int ret = nouveau_dp_detect(nv_encoder);
    152 			if (ret == 0)
    153 				break;
    154 		} else
    155 		if (nv_encoder->i2c) {
    156 			if (nvkm_probe_i2c(nv_encoder->i2c, 0x50))
    157 				break;
    158 		}
    159 	}
    160 
    161 	/* eDP panel not detected, restore panel power GPIO to previous
    162 	 * state to avoid confusing the SOR for other output types.
    163 	 */
    164 	if (!nv_encoder && panel == 0)
    165 		nvkm_gpio_set(gpio, 0, DCB_GPIO_PANEL_POWER, 0xff, panel);
    166 
    167 	return nv_encoder;
    168 }
    169 
    170 static struct nouveau_encoder *
    171 nouveau_connector_of_detect(struct drm_connector *connector)
    172 {
    173 #ifdef __powerpc__
    174 	struct drm_device *dev = connector->dev;
    175 	struct nouveau_connector *nv_connector = nouveau_connector(connector);
    176 	struct nouveau_encoder *nv_encoder;
    177 	struct device_node *cn, *dn = pci_device_to_OF_node(dev->pdev);
    178 
    179 	if (!dn ||
    180 	    !((nv_encoder = find_encoder(connector, DCB_OUTPUT_TMDS)) ||
    181 	      (nv_encoder = find_encoder(connector, DCB_OUTPUT_ANALOG))))
    182 		return NULL;
    183 
    184 	for_each_child_of_node(dn, cn) {
    185 		const char *name = of_get_property(cn, "name", NULL);
    186 		const void *edid = of_get_property(cn, "EDID", NULL);
    187 		int idx = name ? name[strlen(name) - 1] - 'A' : 0;
    188 
    189 		if (nv_encoder->dcb->i2c_index == idx && edid) {
    190 			nv_connector->edid =
    191 				kmemdup(edid, EDID_LENGTH, GFP_KERNEL);
    192 			of_node_put(cn);
    193 			return nv_encoder;
    194 		}
    195 	}
    196 #endif
    197 	return NULL;
    198 }
    199 
    200 static void
    201 nouveau_connector_set_encoder(struct drm_connector *connector,
    202 			      struct nouveau_encoder *nv_encoder)
    203 {
    204 	struct nouveau_connector *nv_connector = nouveau_connector(connector);
    205 	struct nouveau_drm *drm = nouveau_drm(connector->dev);
    206 	struct drm_device *dev = connector->dev;
    207 
    208 	if (nv_connector->detected_encoder == nv_encoder)
    209 		return;
    210 	nv_connector->detected_encoder = nv_encoder;
    211 
    212 	if (drm->device.info.family >= NV_DEVICE_INFO_V0_TESLA) {
    213 		connector->interlace_allowed = true;
    214 		connector->doublescan_allowed = true;
    215 	} else
    216 	if (nv_encoder->dcb->type == DCB_OUTPUT_LVDS ||
    217 	    nv_encoder->dcb->type == DCB_OUTPUT_TMDS) {
    218 		connector->doublescan_allowed = false;
    219 		connector->interlace_allowed = false;
    220 	} else {
    221 		connector->doublescan_allowed = true;
    222 		if (drm->device.info.family == NV_DEVICE_INFO_V0_KELVIN ||
    223 		    (drm->device.info.family == NV_DEVICE_INFO_V0_CELSIUS &&
    224 		     (dev->pdev->device & 0x0ff0) != 0x0100 &&
    225 		     (dev->pdev->device & 0x0ff0) != 0x0150))
    226 			/* HW is broken */
    227 			connector->interlace_allowed = false;
    228 		else
    229 			connector->interlace_allowed = true;
    230 	}
    231 
    232 	if (nv_connector->type == DCB_CONNECTOR_DVI_I) {
    233 		drm_object_property_set_value(&connector->base,
    234 			dev->mode_config.dvi_i_subconnector_property,
    235 			nv_encoder->dcb->type == DCB_OUTPUT_TMDS ?
    236 			DRM_MODE_SUBCONNECTOR_DVID :
    237 			DRM_MODE_SUBCONNECTOR_DVIA);
    238 	}
    239 }
    240 
    241 static enum drm_connector_status
    242 nouveau_connector_detect(struct drm_connector *connector, bool force)
    243 {
    244 	struct drm_device *dev = connector->dev;
    245 	struct nouveau_drm *drm = nouveau_drm(dev);
    246 	struct nouveau_connector *nv_connector = nouveau_connector(connector);
    247 	struct nouveau_encoder *nv_encoder = NULL;
    248 	struct nouveau_encoder *nv_partner;
    249 	struct i2c_adapter *i2c;
    250 	int type;
    251 	int ret;
    252 	enum drm_connector_status conn_status = connector_status_disconnected;
    253 
    254 	/* Cleanup the previous EDID block. */
    255 	if (nv_connector->edid) {
    256 		drm_mode_connector_update_edid_property(connector, NULL);
    257 		kfree(nv_connector->edid);
    258 		nv_connector->edid = NULL;
    259 	}
    260 
    261 	/* Outputs are only polled while runtime active, so acquiring a
    262 	 * runtime PM ref here is unnecessary (and would deadlock upon
    263 	 * runtime suspend because it waits for polling to finish).
    264 	 */
    265 	if (!drm_kms_helper_is_poll_worker()) {
    266 		ret = pm_runtime_get_sync(connector->dev->dev);
    267 		if (ret < 0 && ret != -EACCES)
    268 			return conn_status;
    269 	}
    270 
    271 	nv_encoder = nouveau_connector_ddc_detect(connector);
    272 	if (nv_encoder && (i2c = nv_encoder->i2c) != NULL) {
    273 		nv_connector->edid = drm_get_edid(connector, i2c);
    274 		drm_mode_connector_update_edid_property(connector,
    275 							nv_connector->edid);
    276 		if (!nv_connector->edid) {
    277 			NV_ERROR(drm, "DDC responded, but no EDID for %s\n",
    278 				 connector->name);
    279 			goto detect_analog;
    280 		}
    281 
    282 		/* Override encoder type for DVI-I based on whether EDID
    283 		 * says the display is digital or analog, both use the
    284 		 * same i2c channel so the value returned from ddc_detect
    285 		 * isn't necessarily correct.
    286 		 */
    287 		nv_partner = NULL;
    288 		if (nv_encoder->dcb->type == DCB_OUTPUT_TMDS)
    289 			nv_partner = find_encoder(connector, DCB_OUTPUT_ANALOG);
    290 		if (nv_encoder->dcb->type == DCB_OUTPUT_ANALOG)
    291 			nv_partner = find_encoder(connector, DCB_OUTPUT_TMDS);
    292 
    293 		if (nv_partner && ((nv_encoder->dcb->type == DCB_OUTPUT_ANALOG &&
    294 				    nv_partner->dcb->type == DCB_OUTPUT_TMDS) ||
    295 				   (nv_encoder->dcb->type == DCB_OUTPUT_TMDS &&
    296 				    nv_partner->dcb->type == DCB_OUTPUT_ANALOG))) {
    297 			if (nv_connector->edid->input & DRM_EDID_INPUT_DIGITAL)
    298 				type = DCB_OUTPUT_TMDS;
    299 			else
    300 				type = DCB_OUTPUT_ANALOG;
    301 
    302 			nv_encoder = find_encoder(connector, type);
    303 		}
    304 
    305 		nouveau_connector_set_encoder(connector, nv_encoder);
    306 		conn_status = connector_status_connected;
    307 		goto out;
    308 	}
    309 
    310 	nv_encoder = nouveau_connector_of_detect(connector);
    311 	if (nv_encoder) {
    312 		nouveau_connector_set_encoder(connector, nv_encoder);
    313 		conn_status = connector_status_connected;
    314 		goto out;
    315 	}
    316 
    317 detect_analog:
    318 	nv_encoder = find_encoder(connector, DCB_OUTPUT_ANALOG);
    319 	if (!nv_encoder && !nouveau_tv_disable)
    320 		nv_encoder = find_encoder(connector, DCB_OUTPUT_TV);
    321 	if (nv_encoder && force) {
    322 		struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
    323 		const struct drm_encoder_helper_funcs *helper =
    324 						encoder->helper_private;
    325 
    326 		if (helper->detect(encoder, connector) ==
    327 						connector_status_connected) {
    328 			nouveau_connector_set_encoder(connector, nv_encoder);
    329 			conn_status = connector_status_connected;
    330 			goto out;
    331 		}
    332 
    333 	}
    334 
    335  out:
    336 
    337 	if (!drm_kms_helper_is_poll_worker()) {
    338 		pm_runtime_mark_last_busy(connector->dev->dev);
    339 		pm_runtime_put_autosuspend(connector->dev->dev);
    340 	}
    341 
    342 	return conn_status;
    343 }
    344 
    345 static enum drm_connector_status
    346 nouveau_connector_detect_lvds(struct drm_connector *connector, bool force)
    347 {
    348 	struct drm_device *dev = connector->dev;
    349 	struct nouveau_drm *drm = nouveau_drm(dev);
    350 	struct nouveau_connector *nv_connector = nouveau_connector(connector);
    351 	struct nouveau_encoder *nv_encoder = NULL;
    352 	enum drm_connector_status status = connector_status_disconnected;
    353 
    354 	/* Cleanup the previous EDID block. */
    355 	if (nv_connector->edid) {
    356 		drm_mode_connector_update_edid_property(connector, NULL);
    357 		kfree(nv_connector->edid);
    358 		nv_connector->edid = NULL;
    359 	}
    360 
    361 	nv_encoder = find_encoder(connector, DCB_OUTPUT_LVDS);
    362 	if (!nv_encoder)
    363 		return connector_status_disconnected;
    364 
    365 	/* Try retrieving EDID via DDC */
    366 	if (!drm->vbios.fp_no_ddc) {
    367 		status = nouveau_connector_detect(connector, force);
    368 		if (status == connector_status_connected)
    369 			goto out;
    370 	}
    371 
    372 	/* On some laptops (Sony, i'm looking at you) there appears to
    373 	 * be no direct way of accessing the panel's EDID.  The only
    374 	 * option available to us appears to be to ask ACPI for help..
    375 	 *
    376 	 * It's important this check's before trying straps, one of the
    377 	 * said manufacturer's laptops are configured in such a way
    378 	 * the nouveau decides an entry in the VBIOS FP mode table is
    379 	 * valid - it's not (rh#613284)
    380 	 */
    381 	if (nv_encoder->dcb->lvdsconf.use_acpi_for_edid) {
    382 		if ((nv_connector->edid = nouveau_acpi_edid(dev, connector))) {
    383 			status = connector_status_connected;
    384 			goto out;
    385 		}
    386 	}
    387 
    388 	/* If no EDID found above, and the VBIOS indicates a hardcoded
    389 	 * modeline is avalilable for the panel, set it as the panel's
    390 	 * native mode and exit.
    391 	 */
    392 	if (nouveau_bios_fp_mode(dev, NULL) && (drm->vbios.fp_no_ddc ||
    393 	    nv_encoder->dcb->lvdsconf.use_straps_for_mode)) {
    394 		status = connector_status_connected;
    395 		goto out;
    396 	}
    397 
    398 	/* Still nothing, some VBIOS images have a hardcoded EDID block
    399 	 * stored for the panel stored in them.
    400 	 */
    401 	if (!drm->vbios.fp_no_ddc) {
    402 		struct edid *edid =
    403 			(struct edid *)nouveau_bios_embedded_edid(dev);
    404 		if (edid) {
    405 			nv_connector->edid =
    406 					kmemdup(edid, EDID_LENGTH, GFP_KERNEL);
    407 			if (nv_connector->edid)
    408 				status = connector_status_connected;
    409 		}
    410 	}
    411 
    412 out:
    413 #if defined(CONFIG_ACPI_BUTTON) || \
    414 	(defined(CONFIG_ACPI_BUTTON_MODULE) && defined(MODULE))
    415 	if (status == connector_status_connected &&
    416 	    !nouveau_ignorelid && !acpi_lid_open())
    417 		status = connector_status_unknown;
    418 #endif
    419 
    420 	drm_mode_connector_update_edid_property(connector, nv_connector->edid);
    421 	nouveau_connector_set_encoder(connector, nv_encoder);
    422 	return status;
    423 }
    424 
    425 static void
    426 nouveau_connector_force(struct drm_connector *connector)
    427 {
    428 	struct nouveau_drm *drm = nouveau_drm(connector->dev);
    429 	struct nouveau_connector *nv_connector = nouveau_connector(connector);
    430 	struct nouveau_encoder *nv_encoder;
    431 	int type;
    432 
    433 	if (nv_connector->type == DCB_CONNECTOR_DVI_I) {
    434 		if (connector->force == DRM_FORCE_ON_DIGITAL)
    435 			type = DCB_OUTPUT_TMDS;
    436 		else
    437 			type = DCB_OUTPUT_ANALOG;
    438 	} else
    439 		type = DCB_OUTPUT_ANY;
    440 
    441 	nv_encoder = find_encoder(connector, type);
    442 	if (!nv_encoder) {
    443 		NV_ERROR(drm, "can't find encoder to force %s on!\n",
    444 			 connector->name);
    445 		connector->status = connector_status_disconnected;
    446 		return;
    447 	}
    448 
    449 	nouveau_connector_set_encoder(connector, nv_encoder);
    450 }
    451 
    452 static int
    453 nouveau_connector_set_property(struct drm_connector *connector,
    454 			       struct drm_property *property, uint64_t value)
    455 {
    456 	struct nouveau_display *disp = nouveau_display(connector->dev);
    457 	struct nouveau_connector *nv_connector = nouveau_connector(connector);
    458 	struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
    459 	struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
    460 	struct drm_device *dev = connector->dev;
    461 	struct nouveau_crtc *nv_crtc;
    462 	int ret;
    463 
    464 	nv_crtc = NULL;
    465 	if (connector->encoder && connector->encoder->crtc)
    466 		nv_crtc = nouveau_crtc(connector->encoder->crtc);
    467 
    468 	/* Scaling mode */
    469 	if (property == dev->mode_config.scaling_mode_property) {
    470 		bool modeset = false;
    471 
    472 		switch (value) {
    473 		case DRM_MODE_SCALE_NONE:
    474 			/* We allow 'None' for EDID modes, even on a fixed
    475 			 * panel (some exist with support for lower refresh
    476 			 * rates, which people might want to use for power
    477 			 * saving purposes).
    478 			 *
    479 			 * Non-EDID modes will force the use of GPU scaling
    480 			 * to the native mode regardless of this setting.
    481 			 */
    482 			switch (nv_connector->type) {
    483 			case DCB_CONNECTOR_LVDS:
    484 			case DCB_CONNECTOR_LVDS_SPWG:
    485 			case DCB_CONNECTOR_eDP:
    486 				/* ... except prior to G80, where the code
    487 				 * doesn't support such things.
    488 				 */
    489 				if (disp->disp.oclass < NV50_DISP)
    490 					return -EINVAL;
    491 				break;
    492 			default:
    493 				break;
    494 			}
    495 			break;
    496 		case DRM_MODE_SCALE_FULLSCREEN:
    497 		case DRM_MODE_SCALE_CENTER:
    498 		case DRM_MODE_SCALE_ASPECT:
    499 			break;
    500 		default:
    501 			return -EINVAL;
    502 		}
    503 
    504 		/* Changing between GPU and panel scaling requires a full
    505 		 * modeset
    506 		 */
    507 		if ((nv_connector->scaling_mode == DRM_MODE_SCALE_NONE) ||
    508 		    (value == DRM_MODE_SCALE_NONE))
    509 			modeset = true;
    510 		nv_connector->scaling_mode = value;
    511 
    512 		if (!nv_crtc)
    513 			return 0;
    514 
    515 		if (modeset || !nv_crtc->set_scale) {
    516 			ret = drm_crtc_helper_set_mode(&nv_crtc->base,
    517 							&nv_crtc->base.mode,
    518 							nv_crtc->base.x,
    519 							nv_crtc->base.y, NULL);
    520 			if (!ret)
    521 				return -EINVAL;
    522 		} else {
    523 			ret = nv_crtc->set_scale(nv_crtc, true);
    524 			if (ret)
    525 				return ret;
    526 		}
    527 
    528 		return 0;
    529 	}
    530 
    531 	/* Underscan */
    532 	if (property == disp->underscan_property) {
    533 		if (nv_connector->underscan != value) {
    534 			nv_connector->underscan = value;
    535 			if (!nv_crtc || !nv_crtc->set_scale)
    536 				return 0;
    537 
    538 			return nv_crtc->set_scale(nv_crtc, true);
    539 		}
    540 
    541 		return 0;
    542 	}
    543 
    544 	if (property == disp->underscan_hborder_property) {
    545 		if (nv_connector->underscan_hborder != value) {
    546 			nv_connector->underscan_hborder = value;
    547 			if (!nv_crtc || !nv_crtc->set_scale)
    548 				return 0;
    549 
    550 			return nv_crtc->set_scale(nv_crtc, true);
    551 		}
    552 
    553 		return 0;
    554 	}
    555 
    556 	if (property == disp->underscan_vborder_property) {
    557 		if (nv_connector->underscan_vborder != value) {
    558 			nv_connector->underscan_vborder = value;
    559 			if (!nv_crtc || !nv_crtc->set_scale)
    560 				return 0;
    561 
    562 			return nv_crtc->set_scale(nv_crtc, true);
    563 		}
    564 
    565 		return 0;
    566 	}
    567 
    568 	/* Dithering */
    569 	if (property == disp->dithering_mode) {
    570 		nv_connector->dithering_mode = value;
    571 		if (!nv_crtc || !nv_crtc->set_dither)
    572 			return 0;
    573 
    574 		return nv_crtc->set_dither(nv_crtc, true);
    575 	}
    576 
    577 	if (property == disp->dithering_depth) {
    578 		nv_connector->dithering_depth = value;
    579 		if (!nv_crtc || !nv_crtc->set_dither)
    580 			return 0;
    581 
    582 		return nv_crtc->set_dither(nv_crtc, true);
    583 	}
    584 
    585 	if (nv_crtc && nv_crtc->set_color_vibrance) {
    586 		/* Hue */
    587 		if (property == disp->vibrant_hue_property) {
    588 			nv_crtc->vibrant_hue = value - 90;
    589 			return nv_crtc->set_color_vibrance(nv_crtc, true);
    590 		}
    591 		/* Saturation */
    592 		if (property == disp->color_vibrance_property) {
    593 			nv_crtc->color_vibrance = value - 100;
    594 			return nv_crtc->set_color_vibrance(nv_crtc, true);
    595 		}
    596 	}
    597 
    598 	if (nv_encoder && nv_encoder->dcb->type == DCB_OUTPUT_TV)
    599 		return get_slave_funcs(encoder)->set_property(
    600 			encoder, connector, property, value);
    601 
    602 	return -EINVAL;
    603 }
    604 
    605 static struct drm_display_mode *
    606 nouveau_connector_native_mode(struct drm_connector *connector)
    607 {
    608 	const struct drm_connector_helper_funcs *helper = connector->helper_private;
    609 	struct nouveau_drm *drm = nouveau_drm(connector->dev);
    610 	struct nouveau_connector *nv_connector = nouveau_connector(connector);
    611 	struct drm_device *dev = connector->dev;
    612 	struct drm_display_mode *mode, *largest = NULL;
    613 	int high_w = 0, high_h = 0, high_v = 0;
    614 
    615 	list_for_each_entry(mode, &nv_connector->base.probed_modes, head) {
    616 		mode->vrefresh = drm_mode_vrefresh(mode);
    617 		if (helper->mode_valid(connector, mode) != MODE_OK ||
    618 		    (mode->flags & DRM_MODE_FLAG_INTERLACE))
    619 			continue;
    620 
    621 		/* Use preferred mode if there is one.. */
    622 		if (mode->type & DRM_MODE_TYPE_PREFERRED) {
    623 			NV_DEBUG(drm, "native mode from preferred\n");
    624 			return drm_mode_duplicate(dev, mode);
    625 		}
    626 
    627 		/* Otherwise, take the resolution with the largest width, then
    628 		 * height, then vertical refresh
    629 		 */
    630 		if (mode->hdisplay < high_w)
    631 			continue;
    632 
    633 		if (mode->hdisplay == high_w && mode->vdisplay < high_h)
    634 			continue;
    635 
    636 		if (mode->hdisplay == high_w && mode->vdisplay == high_h &&
    637 		    mode->vrefresh < high_v)
    638 			continue;
    639 
    640 		high_w = mode->hdisplay;
    641 		high_h = mode->vdisplay;
    642 		high_v = mode->vrefresh;
    643 		largest = mode;
    644 	}
    645 
    646 	NV_DEBUG(drm, "native mode from largest: %dx%d@%d\n",
    647 		      high_w, high_h, high_v);
    648 	return largest ? drm_mode_duplicate(dev, largest) : NULL;
    649 }
    650 
    651 struct moderec {
    652 	int hdisplay;
    653 	int vdisplay;
    654 };
    655 
    656 static struct moderec scaler_modes[] = {
    657 	{ 1920, 1200 },
    658 	{ 1920, 1080 },
    659 	{ 1680, 1050 },
    660 	{ 1600, 1200 },
    661 	{ 1400, 1050 },
    662 	{ 1280, 1024 },
    663 	{ 1280, 960 },
    664 	{ 1152, 864 },
    665 	{ 1024, 768 },
    666 	{ 800, 600 },
    667 	{ 720, 400 },
    668 	{ 640, 480 },
    669 	{ 640, 400 },
    670 	{ 640, 350 },
    671 	{}
    672 };
    673 
    674 static int
    675 nouveau_connector_scaler_modes_add(struct drm_connector *connector)
    676 {
    677 	struct nouveau_connector *nv_connector = nouveau_connector(connector);
    678 	struct drm_display_mode *native = nv_connector->native_mode, *m;
    679 	struct drm_device *dev = connector->dev;
    680 	struct moderec *mode = &scaler_modes[0];
    681 	int modes = 0;
    682 
    683 	if (!native)
    684 		return 0;
    685 
    686 	while (mode->hdisplay) {
    687 		if (mode->hdisplay <= native->hdisplay &&
    688 		    mode->vdisplay <= native->vdisplay &&
    689 		    (mode->hdisplay != native->hdisplay ||
    690 		     mode->vdisplay != native->vdisplay)) {
    691 			m = drm_cvt_mode(dev, mode->hdisplay, mode->vdisplay,
    692 					 drm_mode_vrefresh(native), false,
    693 					 false, false);
    694 			if (!m)
    695 				continue;
    696 
    697 			drm_mode_probed_add(connector, m);
    698 			modes++;
    699 		}
    700 
    701 		mode++;
    702 	}
    703 
    704 	return modes;
    705 }
    706 
    707 static void
    708 nouveau_connector_detect_depth(struct drm_connector *connector)
    709 {
    710 	struct nouveau_drm *drm = nouveau_drm(connector->dev);
    711 	struct nouveau_connector *nv_connector = nouveau_connector(connector);
    712 	struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
    713 	struct nvbios *bios = &drm->vbios;
    714 	struct drm_display_mode *mode = nv_connector->native_mode;
    715 	bool duallink;
    716 
    717 	/* if the edid is feeling nice enough to provide this info, use it */
    718 	if (nv_connector->edid && connector->display_info.bpc)
    719 		return;
    720 
    721 	/* EDID 1.4 is *supposed* to be supported on eDP, but, Apple... */
    722 	if (nv_connector->type == DCB_CONNECTOR_eDP) {
    723 		connector->display_info.bpc = 6;
    724 		return;
    725 	}
    726 
    727 	/* we're out of options unless we're LVDS, default to 8bpc */
    728 	if (nv_encoder->dcb->type != DCB_OUTPUT_LVDS) {
    729 		connector->display_info.bpc = 8;
    730 		return;
    731 	}
    732 
    733 	connector->display_info.bpc = 6;
    734 
    735 	/* LVDS: panel straps */
    736 	if (bios->fp_no_ddc) {
    737 		if (bios->fp.if_is_24bit)
    738 			connector->display_info.bpc = 8;
    739 		return;
    740 	}
    741 
    742 	/* LVDS: DDC panel, need to first determine the number of links to
    743 	 * know which if_is_24bit flag to check...
    744 	 */
    745 	if (nv_connector->edid &&
    746 	    nv_connector->type == DCB_CONNECTOR_LVDS_SPWG)
    747 		duallink = ((u8 *)nv_connector->edid)[121] == 2;
    748 	else
    749 		duallink = mode->clock >= bios->fp.duallink_transition_clk;
    750 
    751 	if ((!duallink && (bios->fp.strapless_is_24bit & 1)) ||
    752 	    ( duallink && (bios->fp.strapless_is_24bit & 2)))
    753 		connector->display_info.bpc = 8;
    754 }
    755 
    756 static int
    757 nouveau_connector_get_modes(struct drm_connector *connector)
    758 {
    759 	struct drm_device *dev = connector->dev;
    760 	struct nouveau_drm *drm = nouveau_drm(dev);
    761 	struct nouveau_connector *nv_connector = nouveau_connector(connector);
    762 	struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
    763 	struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
    764 	int ret = 0;
    765 
    766 	/* destroy the native mode, the attached monitor could have changed.
    767 	 */
    768 	if (nv_connector->native_mode) {
    769 		drm_mode_destroy(dev, nv_connector->native_mode);
    770 		nv_connector->native_mode = NULL;
    771 	}
    772 
    773 	if (nv_connector->edid)
    774 		ret = drm_add_edid_modes(connector, nv_connector->edid);
    775 	else
    776 	if (nv_encoder->dcb->type == DCB_OUTPUT_LVDS &&
    777 	    (nv_encoder->dcb->lvdsconf.use_straps_for_mode ||
    778 	     drm->vbios.fp_no_ddc) && nouveau_bios_fp_mode(dev, NULL)) {
    779 		struct drm_display_mode mode;
    780 
    781 		nouveau_bios_fp_mode(dev, &mode);
    782 		nv_connector->native_mode = drm_mode_duplicate(dev, &mode);
    783 	}
    784 
    785 	/* Determine display colour depth for everything except LVDS now,
    786 	 * DP requires this before mode_valid() is called.
    787 	 */
    788 	if (connector->connector_type != DRM_MODE_CONNECTOR_LVDS)
    789 		nouveau_connector_detect_depth(connector);
    790 
    791 	/* Find the native mode if this is a digital panel, if we didn't
    792 	 * find any modes through DDC previously add the native mode to
    793 	 * the list of modes.
    794 	 */
    795 	if (!nv_connector->native_mode)
    796 		nv_connector->native_mode =
    797 			nouveau_connector_native_mode(connector);
    798 	if (ret == 0 && nv_connector->native_mode) {
    799 		struct drm_display_mode *mode;
    800 
    801 		mode = drm_mode_duplicate(dev, nv_connector->native_mode);
    802 		drm_mode_probed_add(connector, mode);
    803 		ret = 1;
    804 	}
    805 
    806 	/* Determine LVDS colour depth, must happen after determining
    807 	 * "native" mode as some VBIOS tables require us to use the
    808 	 * pixel clock as part of the lookup...
    809 	 */
    810 	if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS)
    811 		nouveau_connector_detect_depth(connector);
    812 
    813 	if (nv_encoder->dcb->type == DCB_OUTPUT_TV)
    814 		ret = get_slave_funcs(encoder)->get_modes(encoder, connector);
    815 
    816 	if (nv_connector->type == DCB_CONNECTOR_LVDS ||
    817 	    nv_connector->type == DCB_CONNECTOR_LVDS_SPWG ||
    818 	    nv_connector->type == DCB_CONNECTOR_eDP)
    819 		ret += nouveau_connector_scaler_modes_add(connector);
    820 
    821 	return ret;
    822 }
    823 
    824 static unsigned
    825 get_tmds_link_bandwidth(struct drm_connector *connector)
    826 {
    827 	struct nouveau_connector *nv_connector = nouveau_connector(connector);
    828 	struct nouveau_drm *drm = nouveau_drm(connector->dev);
    829 	struct dcb_output *dcb = nv_connector->detected_encoder->dcb;
    830 
    831 	if (dcb->location != DCB_LOC_ON_CHIP ||
    832 	    drm->device.info.chipset >= 0x46)
    833 		return 165000;
    834 	else if (drm->device.info.chipset >= 0x40)
    835 		return 155000;
    836 	else if (drm->device.info.chipset >= 0x18)
    837 		return 135000;
    838 	else
    839 		return 112000;
    840 }
    841 
    842 static int
    843 nouveau_connector_mode_valid(struct drm_connector *connector,
    844 			     struct drm_display_mode *mode)
    845 {
    846 	struct nouveau_connector *nv_connector = nouveau_connector(connector);
    847 	struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
    848 	struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
    849 	unsigned min_clock = 25000, max_clock = min_clock;
    850 	unsigned clock = mode->clock;
    851 
    852 	switch (nv_encoder->dcb->type) {
    853 	case DCB_OUTPUT_LVDS:
    854 		if (nv_connector->native_mode &&
    855 		    (mode->hdisplay > nv_connector->native_mode->hdisplay ||
    856 		     mode->vdisplay > nv_connector->native_mode->vdisplay))
    857 			return MODE_PANEL;
    858 
    859 		min_clock = 0;
    860 		max_clock = 400000;
    861 		break;
    862 	case DCB_OUTPUT_TMDS:
    863 		max_clock = get_tmds_link_bandwidth(connector);
    864 		if (nouveau_duallink && nv_encoder->dcb->duallink_possible)
    865 			max_clock *= 2;
    866 		break;
    867 	case DCB_OUTPUT_ANALOG:
    868 		max_clock = nv_encoder->dcb->crtconf.maxfreq;
    869 		if (!max_clock)
    870 			max_clock = 350000;
    871 		break;
    872 	case DCB_OUTPUT_TV:
    873 		return get_slave_funcs(encoder)->mode_valid(encoder, mode);
    874 	case DCB_OUTPUT_DP:
    875 		max_clock  = nv_encoder->dp.link_nr;
    876 		max_clock *= nv_encoder->dp.link_bw;
    877 		clock = clock * (connector->display_info.bpc * 3) / 10;
    878 		break;
    879 	default:
    880 		BUG_ON(1);
    881 		return MODE_BAD;
    882 	}
    883 
    884 	if (clock < min_clock)
    885 		return MODE_CLOCK_LOW;
    886 
    887 	if (clock > max_clock)
    888 		return MODE_CLOCK_HIGH;
    889 
    890 	return MODE_OK;
    891 }
    892 
    893 static struct drm_encoder *
    894 nouveau_connector_best_encoder(struct drm_connector *connector)
    895 {
    896 	struct nouveau_connector *nv_connector = nouveau_connector(connector);
    897 
    898 	if (nv_connector->detected_encoder)
    899 		return to_drm_encoder(nv_connector->detected_encoder);
    900 
    901 	return NULL;
    902 }
    903 
    904 static const struct drm_connector_helper_funcs
    905 nouveau_connector_helper_funcs = {
    906 	.get_modes = nouveau_connector_get_modes,
    907 	.mode_valid = nouveau_connector_mode_valid,
    908 	.best_encoder = nouveau_connector_best_encoder,
    909 };
    910 
    911 static const struct drm_connector_funcs
    912 nouveau_connector_funcs = {
    913 	.dpms = drm_helper_connector_dpms,
    914 	.save = NULL,
    915 	.restore = NULL,
    916 	.detect = nouveau_connector_detect,
    917 	.destroy = nouveau_connector_destroy,
    918 	.fill_modes = drm_helper_probe_single_connector_modes,
    919 	.set_property = nouveau_connector_set_property,
    920 	.force = nouveau_connector_force
    921 };
    922 
    923 static const struct drm_connector_funcs
    924 nouveau_connector_funcs_lvds = {
    925 	.dpms = drm_helper_connector_dpms,
    926 	.save = NULL,
    927 	.restore = NULL,
    928 	.detect = nouveau_connector_detect_lvds,
    929 	.destroy = nouveau_connector_destroy,
    930 	.fill_modes = drm_helper_probe_single_connector_modes,
    931 	.set_property = nouveau_connector_set_property,
    932 	.force = nouveau_connector_force
    933 };
    934 
    935 static int
    936 nouveau_connector_dp_dpms(struct drm_connector *connector, int mode)
    937 {
    938 	struct nouveau_encoder *nv_encoder = NULL;
    939 
    940 	if (connector->encoder)
    941 		nv_encoder = nouveau_encoder(connector->encoder);
    942 	if (nv_encoder && nv_encoder->dcb &&
    943 	    nv_encoder->dcb->type == DCB_OUTPUT_DP) {
    944 		if (mode == DRM_MODE_DPMS_ON) {
    945 			u8 data = DP_SET_POWER_D0;
    946 			nvkm_wraux(nv_encoder->aux, DP_SET_POWER, &data, 1);
    947 			usleep_range(1000, 2000);
    948 		} else {
    949 			u8 data = DP_SET_POWER_D3;
    950 			nvkm_wraux(nv_encoder->aux, DP_SET_POWER, &data, 1);
    951 		}
    952 	}
    953 
    954 	return drm_helper_connector_dpms(connector, mode);
    955 }
    956 
    957 static const struct drm_connector_funcs
    958 nouveau_connector_funcs_dp = {
    959 	.dpms = nouveau_connector_dp_dpms,
    960 	.save = NULL,
    961 	.restore = NULL,
    962 	.detect = nouveau_connector_detect,
    963 	.destroy = nouveau_connector_destroy,
    964 	.fill_modes = drm_helper_probe_single_connector_modes,
    965 	.set_property = nouveau_connector_set_property,
    966 	.force = nouveau_connector_force
    967 };
    968 
    969 static int
    970 nouveau_connector_hotplug(struct nvif_notify *notify)
    971 {
    972 	struct nouveau_connector *nv_connector =
    973 		container_of(notify, typeof(*nv_connector), hpd);
    974 	struct drm_connector *connector = &nv_connector->base;
    975 	struct nouveau_drm *drm = nouveau_drm(connector->dev);
    976 	const struct nvif_notify_conn_rep_v0 *rep = notify->data;
    977 	const char *name = connector->name;
    978 
    979 	if (rep->mask & NVIF_NOTIFY_CONN_V0_IRQ) {
    980 	} else {
    981 		bool plugged = (rep->mask != NVIF_NOTIFY_CONN_V0_UNPLUG);
    982 
    983 		NV_DEBUG(drm, "%splugged %s\n", plugged ? "" : "un", name);
    984 
    985 		mutex_lock(&drm->dev->mode_config.mutex);
    986 		if (plugged)
    987 			drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
    988 		else
    989 			drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
    990 		mutex_unlock(&drm->dev->mode_config.mutex);
    991 
    992 		drm_helper_hpd_irq_event(connector->dev);
    993 	}
    994 
    995 	return NVIF_NOTIFY_KEEP;
    996 }
    997 
    998 static ssize_t
    999 nouveau_connector_aux_xfer(struct drm_dp_aux *obj, struct drm_dp_aux_msg *msg)
   1000 {
   1001 	struct nouveau_connector *nv_connector =
   1002 		container_of(obj, typeof(*nv_connector), aux);
   1003 	struct nouveau_encoder *nv_encoder;
   1004 	struct nvkm_i2c_aux *aux;
   1005 	int ret;
   1006 
   1007 	nv_encoder = find_encoder(&nv_connector->base, DCB_OUTPUT_DP);
   1008 	if (!nv_encoder || !(aux = nv_encoder->aux))
   1009 		return -ENODEV;
   1010 	if (WARN_ON(msg->size > 16))
   1011 		return -E2BIG;
   1012 	if (msg->size == 0)
   1013 		return msg->size;
   1014 
   1015 	ret = nvkm_i2c_aux_acquire(aux);
   1016 	if (ret)
   1017 		return ret;
   1018 
   1019 	ret = nvkm_i2c_aux_xfer(aux, false, msg->request, msg->address,
   1020 				msg->buffer, msg->size);
   1021 	nvkm_i2c_aux_release(aux);
   1022 	if (ret >= 0) {
   1023 		msg->reply = ret;
   1024 		return msg->size;
   1025 	}
   1026 
   1027 	return ret;
   1028 }
   1029 
   1030 static int
   1031 drm_conntype_from_dcb(enum dcb_connector_type dcb)
   1032 {
   1033 	switch (dcb) {
   1034 	case DCB_CONNECTOR_VGA      : return DRM_MODE_CONNECTOR_VGA;
   1035 	case DCB_CONNECTOR_TV_0     :
   1036 	case DCB_CONNECTOR_TV_1     :
   1037 	case DCB_CONNECTOR_TV_3     : return DRM_MODE_CONNECTOR_TV;
   1038 	case DCB_CONNECTOR_DMS59_0  :
   1039 	case DCB_CONNECTOR_DMS59_1  :
   1040 	case DCB_CONNECTOR_DVI_I    : return DRM_MODE_CONNECTOR_DVII;
   1041 	case DCB_CONNECTOR_DVI_D    : return DRM_MODE_CONNECTOR_DVID;
   1042 	case DCB_CONNECTOR_LVDS     :
   1043 	case DCB_CONNECTOR_LVDS_SPWG: return DRM_MODE_CONNECTOR_LVDS;
   1044 	case DCB_CONNECTOR_DMS59_DP0:
   1045 	case DCB_CONNECTOR_DMS59_DP1:
   1046 	case DCB_CONNECTOR_DP       : return DRM_MODE_CONNECTOR_DisplayPort;
   1047 	case DCB_CONNECTOR_eDP      : return DRM_MODE_CONNECTOR_eDP;
   1048 	case DCB_CONNECTOR_HDMI_0   :
   1049 	case DCB_CONNECTOR_HDMI_1   :
   1050 	case DCB_CONNECTOR_HDMI_C   : return DRM_MODE_CONNECTOR_HDMIA;
   1051 	default:
   1052 		break;
   1053 	}
   1054 
   1055 	return DRM_MODE_CONNECTOR_Unknown;
   1056 }
   1057 
   1058 struct drm_connector *
   1059 nouveau_connector_create(struct drm_device *dev, int index)
   1060 {
   1061 	const struct drm_connector_funcs *funcs = &nouveau_connector_funcs;
   1062 	struct nouveau_drm *drm = nouveau_drm(dev);
   1063 	struct nouveau_display *disp = nouveau_display(dev);
   1064 	struct nouveau_connector *nv_connector = NULL;
   1065 	struct drm_connector *connector;
   1066 	int type, ret = 0;
   1067 	bool dummy;
   1068 
   1069 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
   1070 		nv_connector = nouveau_connector(connector);
   1071 		if (nv_connector->index == index)
   1072 			return connector;
   1073 	}
   1074 
   1075 	nv_connector = kzalloc(sizeof(*nv_connector), GFP_KERNEL);
   1076 	if (!nv_connector)
   1077 		return ERR_PTR(-ENOMEM);
   1078 
   1079 	connector = &nv_connector->base;
   1080 	nv_connector->index = index;
   1081 
   1082 	/* attempt to parse vbios connector type and hotplug gpio */
   1083 	nv_connector->dcb = olddcb_conn(dev, index);
   1084 	if (nv_connector->dcb) {
   1085 		u32 entry = ROM16(nv_connector->dcb[0]);
   1086 		if (olddcb_conntab(dev)[3] >= 4)
   1087 			entry |= (u32)ROM16(nv_connector->dcb[2]) << 16;
   1088 
   1089 		nv_connector->type = nv_connector->dcb[0];
   1090 		if (drm_conntype_from_dcb(nv_connector->type) ==
   1091 					  DRM_MODE_CONNECTOR_Unknown) {
   1092 			NV_WARN(drm, "unknown connector type %02x\n",
   1093 				nv_connector->type);
   1094 			nv_connector->type = DCB_CONNECTOR_NONE;
   1095 		}
   1096 
   1097 		/* Gigabyte NX85T */
   1098 		if (nv_match_device(dev, 0x0421, 0x1458, 0x344c)) {
   1099 			if (nv_connector->type == DCB_CONNECTOR_HDMI_1)
   1100 				nv_connector->type = DCB_CONNECTOR_DVI_I;
   1101 		}
   1102 
   1103 		/* Gigabyte GV-NX86T512H */
   1104 		if (nv_match_device(dev, 0x0402, 0x1458, 0x3455)) {
   1105 			if (nv_connector->type == DCB_CONNECTOR_HDMI_1)
   1106 				nv_connector->type = DCB_CONNECTOR_DVI_I;
   1107 		}
   1108 	} else {
   1109 		nv_connector->type = DCB_CONNECTOR_NONE;
   1110 	}
   1111 
   1112 	/* no vbios data, or an unknown dcb connector type - attempt to
   1113 	 * figure out something suitable ourselves
   1114 	 */
   1115 	if (nv_connector->type == DCB_CONNECTOR_NONE) {
   1116 		struct nouveau_drm *drm = nouveau_drm(dev);
   1117 		struct dcb_table *dcbt = &drm->vbios.dcb;
   1118 		u32 encoders = 0;
   1119 		int i;
   1120 
   1121 		for (i = 0; i < dcbt->entries; i++) {
   1122 			if (dcbt->entry[i].connector == nv_connector->index)
   1123 				encoders |= (1 << dcbt->entry[i].type);
   1124 		}
   1125 
   1126 		if (encoders & (1 << DCB_OUTPUT_DP)) {
   1127 			if (encoders & (1 << DCB_OUTPUT_TMDS))
   1128 				nv_connector->type = DCB_CONNECTOR_DP;
   1129 			else
   1130 				nv_connector->type = DCB_CONNECTOR_eDP;
   1131 		} else
   1132 		if (encoders & (1 << DCB_OUTPUT_TMDS)) {
   1133 			if (encoders & (1 << DCB_OUTPUT_ANALOG))
   1134 				nv_connector->type = DCB_CONNECTOR_DVI_I;
   1135 			else
   1136 				nv_connector->type = DCB_CONNECTOR_DVI_D;
   1137 		} else
   1138 		if (encoders & (1 << DCB_OUTPUT_ANALOG)) {
   1139 			nv_connector->type = DCB_CONNECTOR_VGA;
   1140 		} else
   1141 		if (encoders & (1 << DCB_OUTPUT_LVDS)) {
   1142 			nv_connector->type = DCB_CONNECTOR_LVDS;
   1143 		} else
   1144 		if (encoders & (1 << DCB_OUTPUT_TV)) {
   1145 			nv_connector->type = DCB_CONNECTOR_TV_0;
   1146 		}
   1147 	}
   1148 
   1149 	switch ((type = drm_conntype_from_dcb(nv_connector->type))) {
   1150 	case DRM_MODE_CONNECTOR_LVDS:
   1151 		ret = nouveau_bios_parse_lvds_table(dev, 0, &dummy, &dummy);
   1152 		if (ret) {
   1153 			NV_ERROR(drm, "Error parsing LVDS table, disabling\n");
   1154 			kfree(nv_connector);
   1155 			return ERR_PTR(ret);
   1156 		}
   1157 
   1158 		funcs = &nouveau_connector_funcs_lvds;
   1159 		break;
   1160 	case DRM_MODE_CONNECTOR_DisplayPort:
   1161 	case DRM_MODE_CONNECTOR_eDP:
   1162 		nv_connector->aux.dev = dev->dev;
   1163 		nv_connector->aux.transfer = nouveau_connector_aux_xfer;
   1164 		ret = drm_dp_aux_register(&nv_connector->aux);
   1165 		if (ret) {
   1166 			NV_ERROR(drm, "failed to register aux channel\n");
   1167 			kfree(nv_connector);
   1168 			return ERR_PTR(ret);
   1169 		}
   1170 
   1171 		funcs = &nouveau_connector_funcs_dp;
   1172 		break;
   1173 	default:
   1174 		funcs = &nouveau_connector_funcs;
   1175 		break;
   1176 	}
   1177 
   1178 	/* defaults, will get overridden in detect() */
   1179 	connector->interlace_allowed = false;
   1180 	connector->doublescan_allowed = false;
   1181 
   1182 	drm_connector_init(dev, connector, funcs, type);
   1183 	drm_connector_helper_add(connector, &nouveau_connector_helper_funcs);
   1184 
   1185 	/* Init DVI-I specific properties */
   1186 	if (nv_connector->type == DCB_CONNECTOR_DVI_I)
   1187 		drm_object_attach_property(&connector->base, dev->mode_config.dvi_i_subconnector_property, 0);
   1188 
   1189 	/* Add overscan compensation options to digital outputs */
   1190 	if (disp->underscan_property &&
   1191 	    (type == DRM_MODE_CONNECTOR_DVID ||
   1192 	     type == DRM_MODE_CONNECTOR_DVII ||
   1193 	     type == DRM_MODE_CONNECTOR_HDMIA ||
   1194 	     type == DRM_MODE_CONNECTOR_DisplayPort)) {
   1195 		drm_object_attach_property(&connector->base,
   1196 					      disp->underscan_property,
   1197 					      UNDERSCAN_OFF);
   1198 		drm_object_attach_property(&connector->base,
   1199 					      disp->underscan_hborder_property,
   1200 					      0);
   1201 		drm_object_attach_property(&connector->base,
   1202 					      disp->underscan_vborder_property,
   1203 					      0);
   1204 	}
   1205 
   1206 	/* Add hue and saturation options */
   1207 	if (disp->vibrant_hue_property)
   1208 		drm_object_attach_property(&connector->base,
   1209 					      disp->vibrant_hue_property,
   1210 					      90);
   1211 	if (disp->color_vibrance_property)
   1212 		drm_object_attach_property(&connector->base,
   1213 					      disp->color_vibrance_property,
   1214 					      150);
   1215 
   1216 	/* default scaling mode */
   1217 	switch (nv_connector->type) {
   1218 	case DCB_CONNECTOR_LVDS:
   1219 	case DCB_CONNECTOR_LVDS_SPWG:
   1220 	case DCB_CONNECTOR_eDP:
   1221 		/* see note in nouveau_connector_set_property() */
   1222 		if (disp->disp.oclass < NV50_DISP) {
   1223 			nv_connector->scaling_mode = DRM_MODE_SCALE_FULLSCREEN;
   1224 			break;
   1225 		}
   1226 		nv_connector->scaling_mode = DRM_MODE_SCALE_NONE;
   1227 		break;
   1228 	default:
   1229 		nv_connector->scaling_mode = DRM_MODE_SCALE_NONE;
   1230 		break;
   1231 	}
   1232 
   1233 	/* scaling mode property */
   1234 	switch (nv_connector->type) {
   1235 	case DCB_CONNECTOR_TV_0:
   1236 	case DCB_CONNECTOR_TV_1:
   1237 	case DCB_CONNECTOR_TV_3:
   1238 		break;
   1239 	case DCB_CONNECTOR_VGA:
   1240 		if (disp->disp.oclass < NV50_DISP)
   1241 			break; /* can only scale on DFPs */
   1242 		/* fall-through */
   1243 	default:
   1244 		drm_object_attach_property(&connector->base, dev->mode_config.
   1245 					   scaling_mode_property,
   1246 					   nv_connector->scaling_mode);
   1247 		break;
   1248 	}
   1249 
   1250 	/* dithering properties */
   1251 	switch (nv_connector->type) {
   1252 	case DCB_CONNECTOR_TV_0:
   1253 	case DCB_CONNECTOR_TV_1:
   1254 	case DCB_CONNECTOR_TV_3:
   1255 	case DCB_CONNECTOR_VGA:
   1256 		break;
   1257 	default:
   1258 		if (disp->dithering_mode) {
   1259 			drm_object_attach_property(&connector->base,
   1260 						   disp->dithering_mode,
   1261 						   nv_connector->
   1262 						   dithering_mode);
   1263 			nv_connector->dithering_mode = DITHERING_MODE_AUTO;
   1264 		}
   1265 		if (disp->dithering_depth) {
   1266 			drm_object_attach_property(&connector->base,
   1267 						   disp->dithering_depth,
   1268 						   nv_connector->
   1269 						   dithering_depth);
   1270 			nv_connector->dithering_depth = DITHERING_DEPTH_AUTO;
   1271 		}
   1272 		break;
   1273 	}
   1274 
   1275 	ret = nvif_notify_init(&disp->disp, nouveau_connector_hotplug, true,
   1276 			       NV04_DISP_NTFY_CONN,
   1277 			       &(struct nvif_notify_conn_req_v0) {
   1278 				.mask = NVIF_NOTIFY_CONN_V0_ANY,
   1279 				.conn = index,
   1280 			       },
   1281 			       sizeof(struct nvif_notify_conn_req_v0),
   1282 			       sizeof(struct nvif_notify_conn_rep_v0),
   1283 			       &nv_connector->hpd);
   1284 	if (ret)
   1285 		connector->polled = DRM_CONNECTOR_POLL_CONNECT;
   1286 	else
   1287 		connector->polled = DRM_CONNECTOR_POLL_HPD;
   1288 
   1289 	drm_connector_register(connector);
   1290 	return connector;
   1291 }
   1292