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