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