xref: /linux/drivers/gpu/drm/nouveau/nouveau_connector.c (revision f2ee442115c9b6219083c019939a9cc0c9abb2f8)
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 "drmP.h"
30 #include "drm_edid.h"
31 #include "drm_crtc_helper.h"
32 
33 #include "nouveau_reg.h"
34 #include "nouveau_drv.h"
35 #include "nouveau_encoder.h"
36 #include "nouveau_crtc.h"
37 #include "nouveau_connector.h"
38 #include "nouveau_hw.h"
39 
40 static void nouveau_connector_hotplug(void *, int);
41 
42 struct nouveau_encoder *
43 find_encoder(struct drm_connector *connector, int type)
44 {
45 	struct drm_device *dev = connector->dev;
46 	struct nouveau_encoder *nv_encoder;
47 	struct drm_mode_object *obj;
48 	int i, id;
49 
50 	for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
51 		id = connector->encoder_ids[i];
52 		if (!id)
53 			break;
54 
55 		obj = drm_mode_object_find(dev, id, DRM_MODE_OBJECT_ENCODER);
56 		if (!obj)
57 			continue;
58 		nv_encoder = nouveau_encoder(obj_to_encoder(obj));
59 
60 		if (type == OUTPUT_ANY || nv_encoder->dcb->type == type)
61 			return nv_encoder;
62 	}
63 
64 	return NULL;
65 }
66 
67 struct nouveau_connector *
68 nouveau_encoder_connector_get(struct nouveau_encoder *encoder)
69 {
70 	struct drm_device *dev = to_drm_encoder(encoder)->dev;
71 	struct drm_connector *drm_connector;
72 
73 	list_for_each_entry(drm_connector, &dev->mode_config.connector_list, head) {
74 		if (drm_connector->encoder == to_drm_encoder(encoder))
75 			return nouveau_connector(drm_connector);
76 	}
77 
78 	return NULL;
79 }
80 
81 /*TODO: This could use improvement, and learn to handle the fixed
82  *      BIOS tables etc.  It's fine currently, for its only user.
83  */
84 int
85 nouveau_connector_bpp(struct drm_connector *connector)
86 {
87 	struct nouveau_connector *nv_connector = nouveau_connector(connector);
88 
89 	if (nv_connector->edid && nv_connector->edid->revision >= 4) {
90 		u8 bpc = ((nv_connector->edid->input & 0x70) >> 3) + 4;
91 		if (bpc > 4)
92 			return bpc;
93 	}
94 
95 	return 18;
96 }
97 
98 static void
99 nouveau_connector_destroy(struct drm_connector *connector)
100 {
101 	struct nouveau_connector *nv_connector = nouveau_connector(connector);
102 	struct drm_nouveau_private *dev_priv;
103 	struct nouveau_gpio_engine *pgpio;
104 	struct drm_device *dev;
105 
106 	if (!nv_connector)
107 		return;
108 
109 	dev = nv_connector->base.dev;
110 	dev_priv = dev->dev_private;
111 	NV_DEBUG_KMS(dev, "\n");
112 
113 	pgpio = &dev_priv->engine.gpio;
114 	if (pgpio->irq_unregister) {
115 		pgpio->irq_unregister(dev, nv_connector->dcb->gpio_tag,
116 				      nouveau_connector_hotplug, connector);
117 	}
118 
119 	kfree(nv_connector->edid);
120 	drm_sysfs_connector_remove(connector);
121 	drm_connector_cleanup(connector);
122 	kfree(connector);
123 }
124 
125 static struct nouveau_i2c_chan *
126 nouveau_connector_ddc_detect(struct drm_connector *connector,
127 			     struct nouveau_encoder **pnv_encoder)
128 {
129 	struct drm_device *dev = connector->dev;
130 	int i;
131 
132 	for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
133 		struct nouveau_i2c_chan *i2c = NULL;
134 		struct nouveau_encoder *nv_encoder;
135 		struct drm_mode_object *obj;
136 		int id;
137 
138 		id = connector->encoder_ids[i];
139 		if (!id)
140 			break;
141 
142 		obj = drm_mode_object_find(dev, id, DRM_MODE_OBJECT_ENCODER);
143 		if (!obj)
144 			continue;
145 		nv_encoder = nouveau_encoder(obj_to_encoder(obj));
146 
147 		if (nv_encoder->dcb->i2c_index < 0xf)
148 			i2c = nouveau_i2c_find(dev, nv_encoder->dcb->i2c_index);
149 
150 		if (i2c && nouveau_probe_i2c_addr(i2c, 0x50)) {
151 			*pnv_encoder = nv_encoder;
152 			return i2c;
153 		}
154 	}
155 
156 	return NULL;
157 }
158 
159 static struct nouveau_encoder *
160 nouveau_connector_of_detect(struct drm_connector *connector)
161 {
162 #ifdef __powerpc__
163 	struct drm_device *dev = connector->dev;
164 	struct nouveau_connector *nv_connector = nouveau_connector(connector);
165 	struct nouveau_encoder *nv_encoder;
166 	struct device_node *cn, *dn = pci_device_to_OF_node(dev->pdev);
167 
168 	if (!dn ||
169 	    !((nv_encoder = find_encoder(connector, OUTPUT_TMDS)) ||
170 	      (nv_encoder = find_encoder(connector, OUTPUT_ANALOG))))
171 		return NULL;
172 
173 	for_each_child_of_node(dn, cn) {
174 		const char *name = of_get_property(cn, "name", NULL);
175 		const void *edid = of_get_property(cn, "EDID", NULL);
176 		int idx = name ? name[strlen(name) - 1] - 'A' : 0;
177 
178 		if (nv_encoder->dcb->i2c_index == idx && edid) {
179 			nv_connector->edid =
180 				kmemdup(edid, EDID_LENGTH, GFP_KERNEL);
181 			of_node_put(cn);
182 			return nv_encoder;
183 		}
184 	}
185 #endif
186 	return NULL;
187 }
188 
189 static void
190 nouveau_connector_set_encoder(struct drm_connector *connector,
191 			      struct nouveau_encoder *nv_encoder)
192 {
193 	struct nouveau_connector *nv_connector = nouveau_connector(connector);
194 	struct drm_nouveau_private *dev_priv = connector->dev->dev_private;
195 	struct drm_device *dev = connector->dev;
196 
197 	if (nv_connector->detected_encoder == nv_encoder)
198 		return;
199 	nv_connector->detected_encoder = nv_encoder;
200 
201 	if (nv_encoder->dcb->type == OUTPUT_LVDS ||
202 	    nv_encoder->dcb->type == OUTPUT_TMDS) {
203 		connector->doublescan_allowed = false;
204 		connector->interlace_allowed = false;
205 	} else {
206 		connector->doublescan_allowed = true;
207 		if (dev_priv->card_type == NV_20 ||
208 		   (dev_priv->card_type == NV_10 &&
209 		    (dev->pci_device & 0x0ff0) != 0x0100 &&
210 		    (dev->pci_device & 0x0ff0) != 0x0150))
211 			/* HW is broken */
212 			connector->interlace_allowed = false;
213 		else
214 			connector->interlace_allowed = true;
215 	}
216 
217 	if (nv_connector->dcb->type == DCB_CONNECTOR_DVI_I) {
218 		drm_connector_property_set_value(connector,
219 			dev->mode_config.dvi_i_subconnector_property,
220 			nv_encoder->dcb->type == OUTPUT_TMDS ?
221 			DRM_MODE_SUBCONNECTOR_DVID :
222 			DRM_MODE_SUBCONNECTOR_DVIA);
223 	}
224 }
225 
226 static enum drm_connector_status
227 nouveau_connector_detect(struct drm_connector *connector, bool force)
228 {
229 	struct drm_device *dev = connector->dev;
230 	struct nouveau_connector *nv_connector = nouveau_connector(connector);
231 	struct nouveau_encoder *nv_encoder = NULL;
232 	struct nouveau_encoder *nv_partner;
233 	struct nouveau_i2c_chan *i2c;
234 	int type;
235 
236 	/* Cleanup the previous EDID block. */
237 	if (nv_connector->edid) {
238 		drm_mode_connector_update_edid_property(connector, NULL);
239 		kfree(nv_connector->edid);
240 		nv_connector->edid = NULL;
241 	}
242 
243 	i2c = nouveau_connector_ddc_detect(connector, &nv_encoder);
244 	if (i2c) {
245 		nv_connector->edid = drm_get_edid(connector, &i2c->adapter);
246 		drm_mode_connector_update_edid_property(connector,
247 							nv_connector->edid);
248 		if (!nv_connector->edid) {
249 			NV_ERROR(dev, "DDC responded, but no EDID for %s\n",
250 				 drm_get_connector_name(connector));
251 			goto detect_analog;
252 		}
253 
254 		if (nv_encoder->dcb->type == OUTPUT_DP &&
255 		    !nouveau_dp_detect(to_drm_encoder(nv_encoder))) {
256 			NV_ERROR(dev, "Detected %s, but failed init\n",
257 				 drm_get_connector_name(connector));
258 			return connector_status_disconnected;
259 		}
260 
261 		/* Override encoder type for DVI-I based on whether EDID
262 		 * says the display is digital or analog, both use the
263 		 * same i2c channel so the value returned from ddc_detect
264 		 * isn't necessarily correct.
265 		 */
266 		nv_partner = NULL;
267 		if (nv_encoder->dcb->type == OUTPUT_TMDS)
268 			nv_partner = find_encoder(connector, OUTPUT_ANALOG);
269 		if (nv_encoder->dcb->type == OUTPUT_ANALOG)
270 			nv_partner = find_encoder(connector, OUTPUT_TMDS);
271 
272 		if (nv_partner && ((nv_encoder->dcb->type == OUTPUT_ANALOG &&
273 				    nv_partner->dcb->type == OUTPUT_TMDS) ||
274 				   (nv_encoder->dcb->type == OUTPUT_TMDS &&
275 				    nv_partner->dcb->type == OUTPUT_ANALOG))) {
276 			if (nv_connector->edid->input & DRM_EDID_INPUT_DIGITAL)
277 				type = OUTPUT_TMDS;
278 			else
279 				type = OUTPUT_ANALOG;
280 
281 			nv_encoder = find_encoder(connector, type);
282 		}
283 
284 		nouveau_connector_set_encoder(connector, nv_encoder);
285 		return connector_status_connected;
286 	}
287 
288 	nv_encoder = nouveau_connector_of_detect(connector);
289 	if (nv_encoder) {
290 		nouveau_connector_set_encoder(connector, nv_encoder);
291 		return connector_status_connected;
292 	}
293 
294 detect_analog:
295 	nv_encoder = find_encoder(connector, OUTPUT_ANALOG);
296 	if (!nv_encoder && !nouveau_tv_disable)
297 		nv_encoder = find_encoder(connector, OUTPUT_TV);
298 	if (nv_encoder && force) {
299 		struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
300 		struct drm_encoder_helper_funcs *helper =
301 						encoder->helper_private;
302 
303 		if (helper->detect(encoder, connector) ==
304 						connector_status_connected) {
305 			nouveau_connector_set_encoder(connector, nv_encoder);
306 			return connector_status_connected;
307 		}
308 
309 	}
310 
311 	return connector_status_disconnected;
312 }
313 
314 static enum drm_connector_status
315 nouveau_connector_detect_lvds(struct drm_connector *connector, bool force)
316 {
317 	struct drm_device *dev = connector->dev;
318 	struct drm_nouveau_private *dev_priv = dev->dev_private;
319 	struct nouveau_connector *nv_connector = nouveau_connector(connector);
320 	struct nouveau_encoder *nv_encoder = NULL;
321 	enum drm_connector_status status = connector_status_disconnected;
322 
323 	/* Cleanup the previous EDID block. */
324 	if (nv_connector->edid) {
325 		drm_mode_connector_update_edid_property(connector, NULL);
326 		kfree(nv_connector->edid);
327 		nv_connector->edid = NULL;
328 	}
329 
330 	nv_encoder = find_encoder(connector, OUTPUT_LVDS);
331 	if (!nv_encoder)
332 		return connector_status_disconnected;
333 
334 	/* Try retrieving EDID via DDC */
335 	if (!dev_priv->vbios.fp_no_ddc) {
336 		status = nouveau_connector_detect(connector, force);
337 		if (status == connector_status_connected)
338 			goto out;
339 	}
340 
341 	/* On some laptops (Sony, i'm looking at you) there appears to
342 	 * be no direct way of accessing the panel's EDID.  The only
343 	 * option available to us appears to be to ask ACPI for help..
344 	 *
345 	 * It's important this check's before trying straps, one of the
346 	 * said manufacturer's laptops are configured in such a way
347 	 * the nouveau decides an entry in the VBIOS FP mode table is
348 	 * valid - it's not (rh#613284)
349 	 */
350 	if (nv_encoder->dcb->lvdsconf.use_acpi_for_edid) {
351 		if (!nouveau_acpi_edid(dev, connector)) {
352 			status = connector_status_connected;
353 			goto out;
354 		}
355 	}
356 
357 	/* If no EDID found above, and the VBIOS indicates a hardcoded
358 	 * modeline is avalilable for the panel, set it as the panel's
359 	 * native mode and exit.
360 	 */
361 	if (nouveau_bios_fp_mode(dev, NULL) && (dev_priv->vbios.fp_no_ddc ||
362 	    nv_encoder->dcb->lvdsconf.use_straps_for_mode)) {
363 		status = connector_status_connected;
364 		goto out;
365 	}
366 
367 	/* Still nothing, some VBIOS images have a hardcoded EDID block
368 	 * stored for the panel stored in them.
369 	 */
370 	if (!dev_priv->vbios.fp_no_ddc) {
371 		struct edid *edid =
372 			(struct edid *)nouveau_bios_embedded_edid(dev);
373 		if (edid) {
374 			nv_connector->edid = kmalloc(EDID_LENGTH, GFP_KERNEL);
375 			*(nv_connector->edid) = *edid;
376 			status = connector_status_connected;
377 		}
378 	}
379 
380 out:
381 #if defined(CONFIG_ACPI_BUTTON) || \
382 	(defined(CONFIG_ACPI_BUTTON_MODULE) && defined(MODULE))
383 	if (status == connector_status_connected &&
384 	    !nouveau_ignorelid && !acpi_lid_open())
385 		status = connector_status_unknown;
386 #endif
387 
388 	drm_mode_connector_update_edid_property(connector, nv_connector->edid);
389 	nouveau_connector_set_encoder(connector, nv_encoder);
390 	return status;
391 }
392 
393 static void
394 nouveau_connector_force(struct drm_connector *connector)
395 {
396 	struct nouveau_connector *nv_connector = nouveau_connector(connector);
397 	struct nouveau_encoder *nv_encoder;
398 	int type;
399 
400 	if (nv_connector->dcb->type == DCB_CONNECTOR_DVI_I) {
401 		if (connector->force == DRM_FORCE_ON_DIGITAL)
402 			type = OUTPUT_TMDS;
403 		else
404 			type = OUTPUT_ANALOG;
405 	} else
406 		type = OUTPUT_ANY;
407 
408 	nv_encoder = find_encoder(connector, type);
409 	if (!nv_encoder) {
410 		NV_ERROR(connector->dev, "can't find encoder to force %s on!\n",
411 			 drm_get_connector_name(connector));
412 		connector->status = connector_status_disconnected;
413 		return;
414 	}
415 
416 	nouveau_connector_set_encoder(connector, nv_encoder);
417 }
418 
419 static int
420 nouveau_connector_set_property(struct drm_connector *connector,
421 			       struct drm_property *property, uint64_t value)
422 {
423 	struct nouveau_connector *nv_connector = nouveau_connector(connector);
424 	struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
425 	struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
426 	struct drm_device *dev = connector->dev;
427 	int ret;
428 
429 	/* Scaling mode */
430 	if (property == dev->mode_config.scaling_mode_property) {
431 		struct nouveau_crtc *nv_crtc = NULL;
432 		bool modeset = false;
433 
434 		switch (value) {
435 		case DRM_MODE_SCALE_NONE:
436 		case DRM_MODE_SCALE_FULLSCREEN:
437 		case DRM_MODE_SCALE_CENTER:
438 		case DRM_MODE_SCALE_ASPECT:
439 			break;
440 		default:
441 			return -EINVAL;
442 		}
443 
444 		/* LVDS always needs gpu scaling */
445 		if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS &&
446 		    value == DRM_MODE_SCALE_NONE)
447 			return -EINVAL;
448 
449 		/* Changing between GPU and panel scaling requires a full
450 		 * modeset
451 		 */
452 		if ((nv_connector->scaling_mode == DRM_MODE_SCALE_NONE) ||
453 		    (value == DRM_MODE_SCALE_NONE))
454 			modeset = true;
455 		nv_connector->scaling_mode = value;
456 
457 		if (connector->encoder && connector->encoder->crtc)
458 			nv_crtc = nouveau_crtc(connector->encoder->crtc);
459 		if (!nv_crtc)
460 			return 0;
461 
462 		if (modeset || !nv_crtc->set_scale) {
463 			ret = drm_crtc_helper_set_mode(&nv_crtc->base,
464 							&nv_crtc->base.mode,
465 							nv_crtc->base.x,
466 							nv_crtc->base.y, NULL);
467 			if (!ret)
468 				return -EINVAL;
469 		} else {
470 			ret = nv_crtc->set_scale(nv_crtc, value, true);
471 			if (ret)
472 				return ret;
473 		}
474 
475 		return 0;
476 	}
477 
478 	/* Dithering */
479 	if (property == dev->mode_config.dithering_mode_property) {
480 		struct nouveau_crtc *nv_crtc = NULL;
481 
482 		if (value == DRM_MODE_DITHERING_ON)
483 			nv_connector->use_dithering = true;
484 		else
485 			nv_connector->use_dithering = false;
486 
487 		if (connector->encoder && connector->encoder->crtc)
488 			nv_crtc = nouveau_crtc(connector->encoder->crtc);
489 
490 		if (!nv_crtc || !nv_crtc->set_dither)
491 			return 0;
492 
493 		return nv_crtc->set_dither(nv_crtc, nv_connector->use_dithering,
494 					   true);
495 	}
496 
497 	if (nv_encoder && nv_encoder->dcb->type == OUTPUT_TV)
498 		return get_slave_funcs(encoder)->set_property(
499 			encoder, connector, property, value);
500 
501 	return -EINVAL;
502 }
503 
504 static struct drm_display_mode *
505 nouveau_connector_native_mode(struct drm_connector *connector)
506 {
507 	struct drm_connector_helper_funcs *helper = connector->helper_private;
508 	struct nouveau_connector *nv_connector = nouveau_connector(connector);
509 	struct drm_device *dev = connector->dev;
510 	struct drm_display_mode *mode, *largest = NULL;
511 	int high_w = 0, high_h = 0, high_v = 0;
512 
513 	list_for_each_entry(mode, &nv_connector->base.probed_modes, head) {
514 		mode->vrefresh = drm_mode_vrefresh(mode);
515 		if (helper->mode_valid(connector, mode) != MODE_OK ||
516 		    (mode->flags & DRM_MODE_FLAG_INTERLACE))
517 			continue;
518 
519 		/* Use preferred mode if there is one.. */
520 		if (mode->type & DRM_MODE_TYPE_PREFERRED) {
521 			NV_DEBUG_KMS(dev, "native mode from preferred\n");
522 			return drm_mode_duplicate(dev, mode);
523 		}
524 
525 		/* Otherwise, take the resolution with the largest width, then
526 		 * height, then vertical refresh
527 		 */
528 		if (mode->hdisplay < high_w)
529 			continue;
530 
531 		if (mode->hdisplay == high_w && mode->vdisplay < high_h)
532 			continue;
533 
534 		if (mode->hdisplay == high_w && mode->vdisplay == high_h &&
535 		    mode->vrefresh < high_v)
536 			continue;
537 
538 		high_w = mode->hdisplay;
539 		high_h = mode->vdisplay;
540 		high_v = mode->vrefresh;
541 		largest = mode;
542 	}
543 
544 	NV_DEBUG_KMS(dev, "native mode from largest: %dx%d@%d\n",
545 		      high_w, high_h, high_v);
546 	return largest ? drm_mode_duplicate(dev, largest) : NULL;
547 }
548 
549 struct moderec {
550 	int hdisplay;
551 	int vdisplay;
552 };
553 
554 static struct moderec scaler_modes[] = {
555 	{ 1920, 1200 },
556 	{ 1920, 1080 },
557 	{ 1680, 1050 },
558 	{ 1600, 1200 },
559 	{ 1400, 1050 },
560 	{ 1280, 1024 },
561 	{ 1280, 960 },
562 	{ 1152, 864 },
563 	{ 1024, 768 },
564 	{ 800, 600 },
565 	{ 720, 400 },
566 	{ 640, 480 },
567 	{ 640, 400 },
568 	{ 640, 350 },
569 	{}
570 };
571 
572 static int
573 nouveau_connector_scaler_modes_add(struct drm_connector *connector)
574 {
575 	struct nouveau_connector *nv_connector = nouveau_connector(connector);
576 	struct drm_display_mode *native = nv_connector->native_mode, *m;
577 	struct drm_device *dev = connector->dev;
578 	struct moderec *mode = &scaler_modes[0];
579 	int modes = 0;
580 
581 	if (!native)
582 		return 0;
583 
584 	while (mode->hdisplay) {
585 		if (mode->hdisplay <= native->hdisplay &&
586 		    mode->vdisplay <= native->vdisplay) {
587 			m = drm_cvt_mode(dev, mode->hdisplay, mode->vdisplay,
588 					 drm_mode_vrefresh(native), false,
589 					 false, false);
590 			if (!m)
591 				continue;
592 
593 			m->type |= DRM_MODE_TYPE_DRIVER;
594 
595 			drm_mode_probed_add(connector, m);
596 			modes++;
597 		}
598 
599 		mode++;
600 	}
601 
602 	return modes;
603 }
604 
605 static int
606 nouveau_connector_get_modes(struct drm_connector *connector)
607 {
608 	struct drm_device *dev = connector->dev;
609 	struct drm_nouveau_private *dev_priv = dev->dev_private;
610 	struct nouveau_connector *nv_connector = nouveau_connector(connector);
611 	struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
612 	struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
613 	int ret = 0;
614 
615 	/* destroy the native mode, the attached monitor could have changed.
616 	 */
617 	if (nv_connector->native_mode) {
618 		drm_mode_destroy(dev, nv_connector->native_mode);
619 		nv_connector->native_mode = NULL;
620 	}
621 
622 	if (nv_connector->edid)
623 		ret = drm_add_edid_modes(connector, nv_connector->edid);
624 	else
625 	if (nv_encoder->dcb->type == OUTPUT_LVDS &&
626 	    (nv_encoder->dcb->lvdsconf.use_straps_for_mode ||
627 	     dev_priv->vbios.fp_no_ddc) && nouveau_bios_fp_mode(dev, NULL)) {
628 		struct drm_display_mode mode;
629 
630 		nouveau_bios_fp_mode(dev, &mode);
631 		nv_connector->native_mode = drm_mode_duplicate(dev, &mode);
632 	}
633 
634 	/* Find the native mode if this is a digital panel, if we didn't
635 	 * find any modes through DDC previously add the native mode to
636 	 * the list of modes.
637 	 */
638 	if (!nv_connector->native_mode)
639 		nv_connector->native_mode =
640 			nouveau_connector_native_mode(connector);
641 	if (ret == 0 && nv_connector->native_mode) {
642 		struct drm_display_mode *mode;
643 
644 		mode = drm_mode_duplicate(dev, nv_connector->native_mode);
645 		drm_mode_probed_add(connector, mode);
646 		ret = 1;
647 	}
648 
649 	if (nv_encoder->dcb->type == OUTPUT_TV)
650 		ret = get_slave_funcs(encoder)->get_modes(encoder, connector);
651 
652 	if (nv_connector->dcb->type == DCB_CONNECTOR_LVDS ||
653 	    nv_connector->dcb->type == DCB_CONNECTOR_LVDS_SPWG ||
654 	    nv_connector->dcb->type == DCB_CONNECTOR_eDP)
655 		ret += nouveau_connector_scaler_modes_add(connector);
656 
657 	return ret;
658 }
659 
660 static unsigned
661 get_tmds_link_bandwidth(struct drm_connector *connector)
662 {
663 	struct nouveau_connector *nv_connector = nouveau_connector(connector);
664 	struct drm_nouveau_private *dev_priv = connector->dev->dev_private;
665 	struct dcb_entry *dcb = nv_connector->detected_encoder->dcb;
666 
667 	if (dcb->location != DCB_LOC_ON_CHIP ||
668 	    dev_priv->chipset >= 0x46)
669 		return 165000;
670 	else if (dev_priv->chipset >= 0x40)
671 		return 155000;
672 	else if (dev_priv->chipset >= 0x18)
673 		return 135000;
674 	else
675 		return 112000;
676 }
677 
678 static int
679 nouveau_connector_mode_valid(struct drm_connector *connector,
680 			     struct drm_display_mode *mode)
681 {
682 	struct nouveau_connector *nv_connector = nouveau_connector(connector);
683 	struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
684 	struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
685 	unsigned min_clock = 25000, max_clock = min_clock;
686 	unsigned clock = mode->clock;
687 
688 	switch (nv_encoder->dcb->type) {
689 	case OUTPUT_LVDS:
690 		if (nv_connector->native_mode &&
691 		    (mode->hdisplay > nv_connector->native_mode->hdisplay ||
692 		     mode->vdisplay > nv_connector->native_mode->vdisplay))
693 			return MODE_PANEL;
694 
695 		min_clock = 0;
696 		max_clock = 400000;
697 		break;
698 	case OUTPUT_TMDS:
699 		max_clock = get_tmds_link_bandwidth(connector);
700 		if (nouveau_duallink && nv_encoder->dcb->duallink_possible)
701 			max_clock *= 2;
702 		break;
703 	case OUTPUT_ANALOG:
704 		max_clock = nv_encoder->dcb->crtconf.maxfreq;
705 		if (!max_clock)
706 			max_clock = 350000;
707 		break;
708 	case OUTPUT_TV:
709 		return get_slave_funcs(encoder)->mode_valid(encoder, mode);
710 	case OUTPUT_DP:
711 		max_clock  = nv_encoder->dp.link_nr;
712 		max_clock *= nv_encoder->dp.link_bw;
713 		clock = clock * nouveau_connector_bpp(connector) / 10;
714 		break;
715 	default:
716 		BUG_ON(1);
717 		return MODE_BAD;
718 	}
719 
720 	if (clock < min_clock)
721 		return MODE_CLOCK_LOW;
722 
723 	if (clock > max_clock)
724 		return MODE_CLOCK_HIGH;
725 
726 	return MODE_OK;
727 }
728 
729 static struct drm_encoder *
730 nouveau_connector_best_encoder(struct drm_connector *connector)
731 {
732 	struct nouveau_connector *nv_connector = nouveau_connector(connector);
733 
734 	if (nv_connector->detected_encoder)
735 		return to_drm_encoder(nv_connector->detected_encoder);
736 
737 	return NULL;
738 }
739 
740 static const struct drm_connector_helper_funcs
741 nouveau_connector_helper_funcs = {
742 	.get_modes = nouveau_connector_get_modes,
743 	.mode_valid = nouveau_connector_mode_valid,
744 	.best_encoder = nouveau_connector_best_encoder,
745 };
746 
747 static const struct drm_connector_funcs
748 nouveau_connector_funcs = {
749 	.dpms = drm_helper_connector_dpms,
750 	.save = NULL,
751 	.restore = NULL,
752 	.detect = nouveau_connector_detect,
753 	.destroy = nouveau_connector_destroy,
754 	.fill_modes = drm_helper_probe_single_connector_modes,
755 	.set_property = nouveau_connector_set_property,
756 	.force = nouveau_connector_force
757 };
758 
759 static const struct drm_connector_funcs
760 nouveau_connector_funcs_lvds = {
761 	.dpms = drm_helper_connector_dpms,
762 	.save = NULL,
763 	.restore = NULL,
764 	.detect = nouveau_connector_detect_lvds,
765 	.destroy = nouveau_connector_destroy,
766 	.fill_modes = drm_helper_probe_single_connector_modes,
767 	.set_property = nouveau_connector_set_property,
768 	.force = nouveau_connector_force
769 };
770 
771 struct drm_connector *
772 nouveau_connector_create(struct drm_device *dev, int index)
773 {
774 	const struct drm_connector_funcs *funcs = &nouveau_connector_funcs;
775 	struct drm_nouveau_private *dev_priv = dev->dev_private;
776 	struct nouveau_gpio_engine *pgpio = &dev_priv->engine.gpio;
777 	struct nouveau_connector *nv_connector = NULL;
778 	struct dcb_connector_table_entry *dcb = NULL;
779 	struct drm_connector *connector;
780 	int type, ret = 0;
781 
782 	NV_DEBUG_KMS(dev, "\n");
783 
784 	if (index >= dev_priv->vbios.dcb.connector.entries)
785 		return ERR_PTR(-EINVAL);
786 
787 	dcb = &dev_priv->vbios.dcb.connector.entry[index];
788 	if (dcb->drm)
789 		return dcb->drm;
790 
791 	switch (dcb->type) {
792 	case DCB_CONNECTOR_VGA:
793 		type = DRM_MODE_CONNECTOR_VGA;
794 		break;
795 	case DCB_CONNECTOR_TV_0:
796 	case DCB_CONNECTOR_TV_1:
797 	case DCB_CONNECTOR_TV_3:
798 		type = DRM_MODE_CONNECTOR_TV;
799 		break;
800 	case DCB_CONNECTOR_DVI_I:
801 		type = DRM_MODE_CONNECTOR_DVII;
802 		break;
803 	case DCB_CONNECTOR_DVI_D:
804 		type = DRM_MODE_CONNECTOR_DVID;
805 		break;
806 	case DCB_CONNECTOR_HDMI_0:
807 	case DCB_CONNECTOR_HDMI_1:
808 		type = DRM_MODE_CONNECTOR_HDMIA;
809 		break;
810 	case DCB_CONNECTOR_LVDS:
811 	case DCB_CONNECTOR_LVDS_SPWG:
812 		type = DRM_MODE_CONNECTOR_LVDS;
813 		funcs = &nouveau_connector_funcs_lvds;
814 		break;
815 	case DCB_CONNECTOR_DP:
816 		type = DRM_MODE_CONNECTOR_DisplayPort;
817 		break;
818 	case DCB_CONNECTOR_eDP:
819 		type = DRM_MODE_CONNECTOR_eDP;
820 		break;
821 	default:
822 		NV_ERROR(dev, "unknown connector type: 0x%02x!!\n", dcb->type);
823 		return ERR_PTR(-EINVAL);
824 	}
825 
826 	nv_connector = kzalloc(sizeof(*nv_connector), GFP_KERNEL);
827 	if (!nv_connector)
828 		return ERR_PTR(-ENOMEM);
829 	nv_connector->dcb = dcb;
830 	connector = &nv_connector->base;
831 
832 	/* defaults, will get overridden in detect() */
833 	connector->interlace_allowed = false;
834 	connector->doublescan_allowed = false;
835 
836 	drm_connector_init(dev, connector, funcs, type);
837 	drm_connector_helper_add(connector, &nouveau_connector_helper_funcs);
838 
839 	/* Check if we need dithering enabled */
840 	if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS) {
841 		bool dummy, is_24bit = false;
842 
843 		ret = nouveau_bios_parse_lvds_table(dev, 0, &dummy, &is_24bit);
844 		if (ret) {
845 			NV_ERROR(dev, "Error parsing LVDS table, disabling "
846 				 "LVDS\n");
847 			goto fail;
848 		}
849 
850 		nv_connector->use_dithering = !is_24bit;
851 	}
852 
853 	/* Init DVI-I specific properties */
854 	if (dcb->type == DCB_CONNECTOR_DVI_I) {
855 		drm_mode_create_dvi_i_properties(dev);
856 		drm_connector_attach_property(connector, dev->mode_config.dvi_i_subconnector_property, 0);
857 		drm_connector_attach_property(connector, dev->mode_config.dvi_i_select_subconnector_property, 0);
858 	}
859 
860 	switch (dcb->type) {
861 	case DCB_CONNECTOR_VGA:
862 		if (dev_priv->card_type >= NV_50) {
863 			drm_connector_attach_property(connector,
864 					dev->mode_config.scaling_mode_property,
865 					nv_connector->scaling_mode);
866 		}
867 		/* fall-through */
868 	case DCB_CONNECTOR_TV_0:
869 	case DCB_CONNECTOR_TV_1:
870 	case DCB_CONNECTOR_TV_3:
871 		nv_connector->scaling_mode = DRM_MODE_SCALE_NONE;
872 		break;
873 	default:
874 		nv_connector->scaling_mode = DRM_MODE_SCALE_FULLSCREEN;
875 
876 		drm_connector_attach_property(connector,
877 				dev->mode_config.scaling_mode_property,
878 				nv_connector->scaling_mode);
879 		drm_connector_attach_property(connector,
880 				dev->mode_config.dithering_mode_property,
881 				nv_connector->use_dithering ?
882 				DRM_MODE_DITHERING_ON : DRM_MODE_DITHERING_OFF);
883 		break;
884 	}
885 
886 	if (nv_connector->dcb->gpio_tag != 0xff && pgpio->irq_register) {
887 		pgpio->irq_register(dev, nv_connector->dcb->gpio_tag,
888 				    nouveau_connector_hotplug, connector);
889 
890 		connector->polled = DRM_CONNECTOR_POLL_HPD;
891 	} else {
892 		connector->polled = DRM_CONNECTOR_POLL_CONNECT;
893 	}
894 
895 	drm_sysfs_connector_add(connector);
896 
897 	dcb->drm = connector;
898 	return dcb->drm;
899 
900 fail:
901 	drm_connector_cleanup(connector);
902 	kfree(connector);
903 	return ERR_PTR(ret);
904 
905 }
906 
907 static void
908 nouveau_connector_hotplug(void *data, int plugged)
909 {
910 	struct drm_connector *connector = data;
911 	struct drm_device *dev = connector->dev;
912 
913 	NV_DEBUG(dev, "%splugged %s\n", plugged ? "" : "un",
914 		 drm_get_connector_name(connector));
915 
916 	if (plugged)
917 		drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
918 	else
919 		drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
920 
921 	drm_helper_hpd_irq_event(dev);
922 }
923