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