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