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