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