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