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 static struct nouveau_encoder *
43 find_encoder_by_type(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_by_type(connector, OUTPUT_TMDS)) ||
170 	      (nv_encoder = find_encoder_by_type(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_i2c_chan *i2c;
233 	int type;
234 
235 	/* Cleanup the previous EDID block. */
236 	if (nv_connector->edid) {
237 		drm_mode_connector_update_edid_property(connector, NULL);
238 		kfree(nv_connector->edid);
239 		nv_connector->edid = NULL;
240 	}
241 
242 	i2c = nouveau_connector_ddc_detect(connector, &nv_encoder);
243 	if (i2c) {
244 		nv_connector->edid = drm_get_edid(connector, &i2c->adapter);
245 		drm_mode_connector_update_edid_property(connector,
246 							nv_connector->edid);
247 		if (!nv_connector->edid) {
248 			NV_ERROR(dev, "DDC responded, but no EDID for %s\n",
249 				 drm_get_connector_name(connector));
250 			goto detect_analog;
251 		}
252 
253 		if (nv_encoder->dcb->type == OUTPUT_DP &&
254 		    !nouveau_dp_detect(to_drm_encoder(nv_encoder))) {
255 			NV_ERROR(dev, "Detected %s, but failed init\n",
256 				 drm_get_connector_name(connector));
257 			return connector_status_disconnected;
258 		}
259 
260 		/* Override encoder type for DVI-I based on whether EDID
261 		 * says the display is digital or analog, both use the
262 		 * same i2c channel so the value returned from ddc_detect
263 		 * isn't necessarily correct.
264 		 */
265 		if (nv_connector->dcb->type == DCB_CONNECTOR_DVI_I) {
266 			if (nv_connector->edid->input & DRM_EDID_INPUT_DIGITAL)
267 				type = OUTPUT_TMDS;
268 			else
269 				type = OUTPUT_ANALOG;
270 
271 			nv_encoder = find_encoder_by_type(connector, type);
272 			if (!nv_encoder) {
273 				NV_ERROR(dev, "Detected %d encoder on %s, "
274 					      "but no object!\n", type,
275 					 drm_get_connector_name(connector));
276 				return connector_status_disconnected;
277 			}
278 		}
279 
280 		nouveau_connector_set_encoder(connector, nv_encoder);
281 		return connector_status_connected;
282 	}
283 
284 	nv_encoder = nouveau_connector_of_detect(connector);
285 	if (nv_encoder) {
286 		nouveau_connector_set_encoder(connector, nv_encoder);
287 		return connector_status_connected;
288 	}
289 
290 detect_analog:
291 	nv_encoder = find_encoder_by_type(connector, OUTPUT_ANALOG);
292 	if (!nv_encoder && !nouveau_tv_disable)
293 		nv_encoder = find_encoder_by_type(connector, OUTPUT_TV);
294 	if (nv_encoder && force) {
295 		struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
296 		struct drm_encoder_helper_funcs *helper =
297 						encoder->helper_private;
298 
299 		if (helper->detect(encoder, connector) ==
300 						connector_status_connected) {
301 			nouveau_connector_set_encoder(connector, nv_encoder);
302 			return connector_status_connected;
303 		}
304 
305 	}
306 
307 	return connector_status_disconnected;
308 }
309 
310 static enum drm_connector_status
311 nouveau_connector_detect_lvds(struct drm_connector *connector, bool force)
312 {
313 	struct drm_device *dev = connector->dev;
314 	struct drm_nouveau_private *dev_priv = dev->dev_private;
315 	struct nouveau_connector *nv_connector = nouveau_connector(connector);
316 	struct nouveau_encoder *nv_encoder = NULL;
317 	enum drm_connector_status status = connector_status_disconnected;
318 
319 	/* Cleanup the previous EDID block. */
320 	if (nv_connector->edid) {
321 		drm_mode_connector_update_edid_property(connector, NULL);
322 		kfree(nv_connector->edid);
323 		nv_connector->edid = NULL;
324 	}
325 
326 	nv_encoder = find_encoder_by_type(connector, OUTPUT_LVDS);
327 	if (!nv_encoder)
328 		return connector_status_disconnected;
329 
330 	/* Try retrieving EDID via DDC */
331 	if (!dev_priv->vbios.fp_no_ddc) {
332 		status = nouveau_connector_detect(connector, force);
333 		if (status == connector_status_connected)
334 			goto out;
335 	}
336 
337 	/* On some laptops (Sony, i'm looking at you) there appears to
338 	 * be no direct way of accessing the panel's EDID.  The only
339 	 * option available to us appears to be to ask ACPI for help..
340 	 *
341 	 * It's important this check's before trying straps, one of the
342 	 * said manufacturer's laptops are configured in such a way
343 	 * the nouveau decides an entry in the VBIOS FP mode table is
344 	 * valid - it's not (rh#613284)
345 	 */
346 	if (nv_encoder->dcb->lvdsconf.use_acpi_for_edid) {
347 		if (!nouveau_acpi_edid(dev, connector)) {
348 			status = connector_status_connected;
349 			goto out;
350 		}
351 	}
352 
353 	/* If no EDID found above, and the VBIOS indicates a hardcoded
354 	 * modeline is avalilable for the panel, set it as the panel's
355 	 * native mode and exit.
356 	 */
357 	if (nouveau_bios_fp_mode(dev, NULL) && (dev_priv->vbios.fp_no_ddc ||
358 	    nv_encoder->dcb->lvdsconf.use_straps_for_mode)) {
359 		status = connector_status_connected;
360 		goto out;
361 	}
362 
363 	/* Still nothing, some VBIOS images have a hardcoded EDID block
364 	 * stored for the panel stored in them.
365 	 */
366 	if (!dev_priv->vbios.fp_no_ddc) {
367 		struct edid *edid =
368 			(struct edid *)nouveau_bios_embedded_edid(dev);
369 		if (edid) {
370 			nv_connector->edid = kmalloc(EDID_LENGTH, GFP_KERNEL);
371 			*(nv_connector->edid) = *edid;
372 			status = connector_status_connected;
373 		}
374 	}
375 
376 out:
377 #if defined(CONFIG_ACPI_BUTTON) || \
378 	(defined(CONFIG_ACPI_BUTTON_MODULE) && defined(MODULE))
379 	if (status == connector_status_connected &&
380 	    !nouveau_ignorelid && !acpi_lid_open())
381 		status = connector_status_unknown;
382 #endif
383 
384 	drm_mode_connector_update_edid_property(connector, nv_connector->edid);
385 	nouveau_connector_set_encoder(connector, nv_encoder);
386 	return status;
387 }
388 
389 static void
390 nouveau_connector_force(struct drm_connector *connector)
391 {
392 	struct nouveau_connector *nv_connector = nouveau_connector(connector);
393 	struct nouveau_encoder *nv_encoder;
394 	int type;
395 
396 	if (nv_connector->dcb->type == DCB_CONNECTOR_DVI_I) {
397 		if (connector->force == DRM_FORCE_ON_DIGITAL)
398 			type = OUTPUT_TMDS;
399 		else
400 			type = OUTPUT_ANALOG;
401 	} else
402 		type = OUTPUT_ANY;
403 
404 	nv_encoder = find_encoder_by_type(connector, type);
405 	if (!nv_encoder) {
406 		NV_ERROR(connector->dev, "can't find encoder to force %s on!\n",
407 			 drm_get_connector_name(connector));
408 		connector->status = connector_status_disconnected;
409 		return;
410 	}
411 
412 	nouveau_connector_set_encoder(connector, nv_encoder);
413 }
414 
415 static int
416 nouveau_connector_set_property(struct drm_connector *connector,
417 			       struct drm_property *property, uint64_t value)
418 {
419 	struct nouveau_connector *nv_connector = nouveau_connector(connector);
420 	struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
421 	struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
422 	struct drm_device *dev = connector->dev;
423 	int ret;
424 
425 	/* Scaling mode */
426 	if (property == dev->mode_config.scaling_mode_property) {
427 		struct nouveau_crtc *nv_crtc = NULL;
428 		bool modeset = false;
429 
430 		switch (value) {
431 		case DRM_MODE_SCALE_NONE:
432 		case DRM_MODE_SCALE_FULLSCREEN:
433 		case DRM_MODE_SCALE_CENTER:
434 		case DRM_MODE_SCALE_ASPECT:
435 			break;
436 		default:
437 			return -EINVAL;
438 		}
439 
440 		/* LVDS always needs gpu scaling */
441 		if (nv_connector->dcb->type == DCB_CONNECTOR_LVDS &&
442 		    value == DRM_MODE_SCALE_NONE)
443 			return -EINVAL;
444 
445 		/* Changing between GPU and panel scaling requires a full
446 		 * modeset
447 		 */
448 		if ((nv_connector->scaling_mode == DRM_MODE_SCALE_NONE) ||
449 		    (value == DRM_MODE_SCALE_NONE))
450 			modeset = true;
451 		nv_connector->scaling_mode = value;
452 
453 		if (connector->encoder && connector->encoder->crtc)
454 			nv_crtc = nouveau_crtc(connector->encoder->crtc);
455 		if (!nv_crtc)
456 			return 0;
457 
458 		if (modeset || !nv_crtc->set_scale) {
459 			ret = drm_crtc_helper_set_mode(&nv_crtc->base,
460 							&nv_crtc->base.mode,
461 							nv_crtc->base.x,
462 							nv_crtc->base.y, NULL);
463 			if (!ret)
464 				return -EINVAL;
465 		} else {
466 			ret = nv_crtc->set_scale(nv_crtc, value, true);
467 			if (ret)
468 				return ret;
469 		}
470 
471 		return 0;
472 	}
473 
474 	/* Dithering */
475 	if (property == dev->mode_config.dithering_mode_property) {
476 		struct nouveau_crtc *nv_crtc = NULL;
477 
478 		if (value == DRM_MODE_DITHERING_ON)
479 			nv_connector->use_dithering = true;
480 		else
481 			nv_connector->use_dithering = false;
482 
483 		if (connector->encoder && connector->encoder->crtc)
484 			nv_crtc = nouveau_crtc(connector->encoder->crtc);
485 
486 		if (!nv_crtc || !nv_crtc->set_dither)
487 			return 0;
488 
489 		return nv_crtc->set_dither(nv_crtc, nv_connector->use_dithering,
490 					   true);
491 	}
492 
493 	if (nv_encoder && nv_encoder->dcb->type == OUTPUT_TV)
494 		return get_slave_funcs(encoder)->set_property(
495 			encoder, connector, property, value);
496 
497 	return -EINVAL;
498 }
499 
500 static struct drm_display_mode *
501 nouveau_connector_native_mode(struct drm_connector *connector)
502 {
503 	struct drm_connector_helper_funcs *helper = connector->helper_private;
504 	struct nouveau_connector *nv_connector = nouveau_connector(connector);
505 	struct drm_device *dev = connector->dev;
506 	struct drm_display_mode *mode, *largest = NULL;
507 	int high_w = 0, high_h = 0, high_v = 0;
508 
509 	list_for_each_entry(mode, &nv_connector->base.probed_modes, head) {
510 		mode->vrefresh = drm_mode_vrefresh(mode);
511 		if (helper->mode_valid(connector, mode) != MODE_OK ||
512 		    (mode->flags & DRM_MODE_FLAG_INTERLACE))
513 			continue;
514 
515 		/* Use preferred mode if there is one.. */
516 		if (mode->type & DRM_MODE_TYPE_PREFERRED) {
517 			NV_DEBUG_KMS(dev, "native mode from preferred\n");
518 			return drm_mode_duplicate(dev, mode);
519 		}
520 
521 		/* Otherwise, take the resolution with the largest width, then
522 		 * height, then vertical refresh
523 		 */
524 		if (mode->hdisplay < high_w)
525 			continue;
526 
527 		if (mode->hdisplay == high_w && mode->vdisplay < high_h)
528 			continue;
529 
530 		if (mode->hdisplay == high_w && mode->vdisplay == high_h &&
531 		    mode->vrefresh < high_v)
532 			continue;
533 
534 		high_w = mode->hdisplay;
535 		high_h = mode->vdisplay;
536 		high_v = mode->vrefresh;
537 		largest = mode;
538 	}
539 
540 	NV_DEBUG_KMS(dev, "native mode from largest: %dx%d@%d\n",
541 		      high_w, high_h, high_v);
542 	return largest ? drm_mode_duplicate(dev, largest) : NULL;
543 }
544 
545 struct moderec {
546 	int hdisplay;
547 	int vdisplay;
548 };
549 
550 static struct moderec scaler_modes[] = {
551 	{ 1920, 1200 },
552 	{ 1920, 1080 },
553 	{ 1680, 1050 },
554 	{ 1600, 1200 },
555 	{ 1400, 1050 },
556 	{ 1280, 1024 },
557 	{ 1280, 960 },
558 	{ 1152, 864 },
559 	{ 1024, 768 },
560 	{ 800, 600 },
561 	{ 720, 400 },
562 	{ 640, 480 },
563 	{ 640, 400 },
564 	{ 640, 350 },
565 	{}
566 };
567 
568 static int
569 nouveau_connector_scaler_modes_add(struct drm_connector *connector)
570 {
571 	struct nouveau_connector *nv_connector = nouveau_connector(connector);
572 	struct drm_display_mode *native = nv_connector->native_mode, *m;
573 	struct drm_device *dev = connector->dev;
574 	struct moderec *mode = &scaler_modes[0];
575 	int modes = 0;
576 
577 	if (!native)
578 		return 0;
579 
580 	while (mode->hdisplay) {
581 		if (mode->hdisplay <= native->hdisplay &&
582 		    mode->vdisplay <= native->vdisplay) {
583 			m = drm_cvt_mode(dev, mode->hdisplay, mode->vdisplay,
584 					 drm_mode_vrefresh(native), false,
585 					 false, false);
586 			if (!m)
587 				continue;
588 
589 			m->type |= DRM_MODE_TYPE_DRIVER;
590 
591 			drm_mode_probed_add(connector, m);
592 			modes++;
593 		}
594 
595 		mode++;
596 	}
597 
598 	return modes;
599 }
600 
601 static int
602 nouveau_connector_get_modes(struct drm_connector *connector)
603 {
604 	struct drm_device *dev = connector->dev;
605 	struct drm_nouveau_private *dev_priv = dev->dev_private;
606 	struct nouveau_connector *nv_connector = nouveau_connector(connector);
607 	struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
608 	struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
609 	int ret = 0;
610 
611 	/* destroy the native mode, the attached monitor could have changed.
612 	 */
613 	if (nv_connector->native_mode) {
614 		drm_mode_destroy(dev, nv_connector->native_mode);
615 		nv_connector->native_mode = NULL;
616 	}
617 
618 	if (nv_connector->edid)
619 		ret = drm_add_edid_modes(connector, nv_connector->edid);
620 	else
621 	if (nv_encoder->dcb->type == OUTPUT_LVDS &&
622 	    (nv_encoder->dcb->lvdsconf.use_straps_for_mode ||
623 	     dev_priv->vbios.fp_no_ddc) && nouveau_bios_fp_mode(dev, NULL)) {
624 		struct drm_display_mode mode;
625 
626 		nouveau_bios_fp_mode(dev, &mode);
627 		nv_connector->native_mode = drm_mode_duplicate(dev, &mode);
628 	}
629 
630 	/* Find the native mode if this is a digital panel, if we didn't
631 	 * find any modes through DDC previously add the native mode to
632 	 * the list of modes.
633 	 */
634 	if (!nv_connector->native_mode)
635 		nv_connector->native_mode =
636 			nouveau_connector_native_mode(connector);
637 	if (ret == 0 && nv_connector->native_mode) {
638 		struct drm_display_mode *mode;
639 
640 		mode = drm_mode_duplicate(dev, nv_connector->native_mode);
641 		drm_mode_probed_add(connector, mode);
642 		ret = 1;
643 	}
644 
645 	if (nv_encoder->dcb->type == OUTPUT_TV)
646 		ret = get_slave_funcs(encoder)->get_modes(encoder, connector);
647 
648 	if (nv_connector->dcb->type == DCB_CONNECTOR_LVDS ||
649 	    nv_connector->dcb->type == DCB_CONNECTOR_eDP)
650 		ret += nouveau_connector_scaler_modes_add(connector);
651 
652 	return ret;
653 }
654 
655 static unsigned
656 get_tmds_link_bandwidth(struct drm_connector *connector)
657 {
658 	struct nouveau_connector *nv_connector = nouveau_connector(connector);
659 	struct drm_nouveau_private *dev_priv = connector->dev->dev_private;
660 	struct dcb_entry *dcb = nv_connector->detected_encoder->dcb;
661 
662 	if (dcb->location != DCB_LOC_ON_CHIP ||
663 	    dev_priv->chipset >= 0x46)
664 		return 165000;
665 	else if (dev_priv->chipset >= 0x40)
666 		return 155000;
667 	else if (dev_priv->chipset >= 0x18)
668 		return 135000;
669 	else
670 		return 112000;
671 }
672 
673 static int
674 nouveau_connector_mode_valid(struct drm_connector *connector,
675 			     struct drm_display_mode *mode)
676 {
677 	struct nouveau_connector *nv_connector = nouveau_connector(connector);
678 	struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
679 	struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
680 	unsigned min_clock = 25000, max_clock = min_clock;
681 	unsigned clock = mode->clock;
682 
683 	switch (nv_encoder->dcb->type) {
684 	case OUTPUT_LVDS:
685 		if (nv_connector->native_mode &&
686 		    (mode->hdisplay > nv_connector->native_mode->hdisplay ||
687 		     mode->vdisplay > nv_connector->native_mode->vdisplay))
688 			return MODE_PANEL;
689 
690 		min_clock = 0;
691 		max_clock = 400000;
692 		break;
693 	case OUTPUT_TMDS:
694 		max_clock = get_tmds_link_bandwidth(connector);
695 		if (nouveau_duallink && nv_encoder->dcb->duallink_possible)
696 			max_clock *= 2;
697 		break;
698 	case OUTPUT_ANALOG:
699 		max_clock = nv_encoder->dcb->crtconf.maxfreq;
700 		if (!max_clock)
701 			max_clock = 350000;
702 		break;
703 	case OUTPUT_TV:
704 		return get_slave_funcs(encoder)->mode_valid(encoder, mode);
705 	case OUTPUT_DP:
706 		if (nv_encoder->dp.link_bw == DP_LINK_BW_2_7)
707 			max_clock = nv_encoder->dp.link_nr * 270000;
708 		else
709 			max_clock = nv_encoder->dp.link_nr * 162000;
710 
711 		clock = clock * nouveau_connector_bpp(connector) / 8;
712 		break;
713 	default:
714 		BUG_ON(1);
715 		return MODE_BAD;
716 	}
717 
718 	if (clock < min_clock)
719 		return MODE_CLOCK_LOW;
720 
721 	if (clock > max_clock)
722 		return MODE_CLOCK_HIGH;
723 
724 	return MODE_OK;
725 }
726 
727 static struct drm_encoder *
728 nouveau_connector_best_encoder(struct drm_connector *connector)
729 {
730 	struct nouveau_connector *nv_connector = nouveau_connector(connector);
731 
732 	if (nv_connector->detected_encoder)
733 		return to_drm_encoder(nv_connector->detected_encoder);
734 
735 	return NULL;
736 }
737 
738 static const struct drm_connector_helper_funcs
739 nouveau_connector_helper_funcs = {
740 	.get_modes = nouveau_connector_get_modes,
741 	.mode_valid = nouveau_connector_mode_valid,
742 	.best_encoder = nouveau_connector_best_encoder,
743 };
744 
745 static const struct drm_connector_funcs
746 nouveau_connector_funcs = {
747 	.dpms = drm_helper_connector_dpms,
748 	.save = NULL,
749 	.restore = NULL,
750 	.detect = nouveau_connector_detect,
751 	.destroy = nouveau_connector_destroy,
752 	.fill_modes = drm_helper_probe_single_connector_modes,
753 	.set_property = nouveau_connector_set_property,
754 	.force = nouveau_connector_force
755 };
756 
757 static const struct drm_connector_funcs
758 nouveau_connector_funcs_lvds = {
759 	.dpms = drm_helper_connector_dpms,
760 	.save = NULL,
761 	.restore = NULL,
762 	.detect = nouveau_connector_detect_lvds,
763 	.destroy = nouveau_connector_destroy,
764 	.fill_modes = drm_helper_probe_single_connector_modes,
765 	.set_property = nouveau_connector_set_property,
766 	.force = nouveau_connector_force
767 };
768 
769 struct drm_connector *
770 nouveau_connector_create(struct drm_device *dev, int index)
771 {
772 	const struct drm_connector_funcs *funcs = &nouveau_connector_funcs;
773 	struct drm_nouveau_private *dev_priv = dev->dev_private;
774 	struct nouveau_gpio_engine *pgpio = &dev_priv->engine.gpio;
775 	struct nouveau_connector *nv_connector = NULL;
776 	struct dcb_connector_table_entry *dcb = NULL;
777 	struct drm_connector *connector;
778 	int type, ret = 0;
779 
780 	NV_DEBUG_KMS(dev, "\n");
781 
782 	if (index >= dev_priv->vbios.dcb.connector.entries)
783 		return ERR_PTR(-EINVAL);
784 
785 	dcb = &dev_priv->vbios.dcb.connector.entry[index];
786 	if (dcb->drm)
787 		return dcb->drm;
788 
789 	switch (dcb->type) {
790 	case DCB_CONNECTOR_VGA:
791 		type = DRM_MODE_CONNECTOR_VGA;
792 		break;
793 	case DCB_CONNECTOR_TV_0:
794 	case DCB_CONNECTOR_TV_1:
795 	case DCB_CONNECTOR_TV_3:
796 		type = DRM_MODE_CONNECTOR_TV;
797 		break;
798 	case DCB_CONNECTOR_DVI_I:
799 		type = DRM_MODE_CONNECTOR_DVII;
800 		break;
801 	case DCB_CONNECTOR_DVI_D:
802 		type = DRM_MODE_CONNECTOR_DVID;
803 		break;
804 	case DCB_CONNECTOR_HDMI_0:
805 	case DCB_CONNECTOR_HDMI_1:
806 		type = DRM_MODE_CONNECTOR_HDMIA;
807 		break;
808 	case DCB_CONNECTOR_LVDS:
809 		type = DRM_MODE_CONNECTOR_LVDS;
810 		funcs = &nouveau_connector_funcs_lvds;
811 		break;
812 	case DCB_CONNECTOR_DP:
813 		type = DRM_MODE_CONNECTOR_DisplayPort;
814 		break;
815 	case DCB_CONNECTOR_eDP:
816 		type = DRM_MODE_CONNECTOR_eDP;
817 		break;
818 	default:
819 		NV_ERROR(dev, "unknown connector type: 0x%02x!!\n", dcb->type);
820 		return ERR_PTR(-EINVAL);
821 	}
822 
823 	nv_connector = kzalloc(sizeof(*nv_connector), GFP_KERNEL);
824 	if (!nv_connector)
825 		return ERR_PTR(-ENOMEM);
826 	nv_connector->dcb = dcb;
827 	connector = &nv_connector->base;
828 
829 	/* defaults, will get overridden in detect() */
830 	connector->interlace_allowed = false;
831 	connector->doublescan_allowed = false;
832 
833 	drm_connector_init(dev, connector, funcs, type);
834 	drm_connector_helper_add(connector, &nouveau_connector_helper_funcs);
835 
836 	/* Check if we need dithering enabled */
837 	if (dcb->type == DCB_CONNECTOR_LVDS) {
838 		bool dummy, is_24bit = false;
839 
840 		ret = nouveau_bios_parse_lvds_table(dev, 0, &dummy, &is_24bit);
841 		if (ret) {
842 			NV_ERROR(dev, "Error parsing LVDS table, disabling "
843 				 "LVDS\n");
844 			goto fail;
845 		}
846 
847 		nv_connector->use_dithering = !is_24bit;
848 	}
849 
850 	/* Init DVI-I specific properties */
851 	if (dcb->type == DCB_CONNECTOR_DVI_I) {
852 		drm_mode_create_dvi_i_properties(dev);
853 		drm_connector_attach_property(connector, dev->mode_config.dvi_i_subconnector_property, 0);
854 		drm_connector_attach_property(connector, dev->mode_config.dvi_i_select_subconnector_property, 0);
855 	}
856 
857 	switch (dcb->type) {
858 	case DCB_CONNECTOR_VGA:
859 		if (dev_priv->card_type >= NV_50) {
860 			drm_connector_attach_property(connector,
861 					dev->mode_config.scaling_mode_property,
862 					nv_connector->scaling_mode);
863 		}
864 		connector->polled = DRM_CONNECTOR_POLL_CONNECT;
865 		/* fall-through */
866 	case DCB_CONNECTOR_TV_0:
867 	case DCB_CONNECTOR_TV_1:
868 	case DCB_CONNECTOR_TV_3:
869 		nv_connector->scaling_mode = DRM_MODE_SCALE_NONE;
870 		break;
871 	default:
872 		nv_connector->scaling_mode = DRM_MODE_SCALE_FULLSCREEN;
873 
874 		drm_connector_attach_property(connector,
875 				dev->mode_config.scaling_mode_property,
876 				nv_connector->scaling_mode);
877 		drm_connector_attach_property(connector,
878 				dev->mode_config.dithering_mode_property,
879 				nv_connector->use_dithering ?
880 				DRM_MODE_DITHERING_ON : DRM_MODE_DITHERING_OFF);
881 
882 		if (dcb->type != DCB_CONNECTOR_LVDS) {
883 			if (dev_priv->card_type >= NV_50)
884 				connector->polled = DRM_CONNECTOR_POLL_HPD;
885 			else
886 				connector->polled = DRM_CONNECTOR_POLL_CONNECT;
887 		}
888 		break;
889 	}
890 
891 	if (pgpio->irq_register) {
892 		pgpio->irq_register(dev, nv_connector->dcb->gpio_tag,
893 				    nouveau_connector_hotplug, connector);
894 	}
895 
896 	drm_sysfs_connector_add(connector);
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_INFO(dev, "%splugged %s\n", plugged ? "" : "un",
914 		drm_get_connector_name(connector));
915 
916 	if (connector->encoder && connector->encoder->crtc &&
917 	    connector->encoder->crtc->enabled) {
918 		struct nouveau_encoder *nv_encoder = nouveau_encoder(connector->encoder);
919 		struct drm_encoder_helper_funcs *helper =
920 			connector->encoder->helper_private;
921 
922 		if (nv_encoder->dcb->type == OUTPUT_DP) {
923 			if (plugged)
924 				helper->dpms(connector->encoder, DRM_MODE_DPMS_ON);
925 			else
926 				helper->dpms(connector->encoder, DRM_MODE_DPMS_OFF);
927 		}
928 	}
929 
930 	drm_helper_hpd_irq_event(dev);
931 }
932