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