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