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