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