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