xref: /openbmc/linux/drivers/gpu/drm/nouveau/nouveau_connector.c (revision baa7eb025ab14f3cba2e35c0a8648f9c9f01d24f)
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_hw.h"
39 
40 static struct nouveau_encoder *
41 find_encoder_by_type(struct drm_connector *connector, int type)
42 {
43 	struct drm_device *dev = connector->dev;
44 	struct nouveau_encoder *nv_encoder;
45 	struct drm_mode_object *obj;
46 	int i, id;
47 
48 	for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
49 		id = connector->encoder_ids[i];
50 		if (!id)
51 			break;
52 
53 		obj = drm_mode_object_find(dev, id, DRM_MODE_OBJECT_ENCODER);
54 		if (!obj)
55 			continue;
56 		nv_encoder = nouveau_encoder(obj_to_encoder(obj));
57 
58 		if (type == OUTPUT_ANY || nv_encoder->dcb->type == type)
59 			return nv_encoder;
60 	}
61 
62 	return NULL;
63 }
64 
65 struct nouveau_connector *
66 nouveau_encoder_connector_get(struct nouveau_encoder *encoder)
67 {
68 	struct drm_device *dev = to_drm_encoder(encoder)->dev;
69 	struct drm_connector *drm_connector;
70 
71 	list_for_each_entry(drm_connector, &dev->mode_config.connector_list, head) {
72 		if (drm_connector->encoder == to_drm_encoder(encoder))
73 			return nouveau_connector(drm_connector);
74 	}
75 
76 	return NULL;
77 }
78 
79 /*TODO: This could use improvement, and learn to handle the fixed
80  *      BIOS tables etc.  It's fine currently, for its only user.
81  */
82 int
83 nouveau_connector_bpp(struct drm_connector *connector)
84 {
85 	struct nouveau_connector *nv_connector = nouveau_connector(connector);
86 
87 	if (nv_connector->edid && nv_connector->edid->revision >= 4) {
88 		u8 bpc = ((nv_connector->edid->input & 0x70) >> 3) + 4;
89 		if (bpc > 4)
90 			return bpc;
91 	}
92 
93 	return 18;
94 }
95 
96 static void
97 nouveau_connector_destroy(struct drm_connector *drm_connector)
98 {
99 	struct nouveau_connector *nv_connector =
100 		nouveau_connector(drm_connector);
101 	struct drm_device *dev;
102 
103 	if (!nv_connector)
104 		return;
105 
106 	dev = nv_connector->base.dev;
107 	NV_DEBUG_KMS(dev, "\n");
108 
109 	kfree(nv_connector->edid);
110 	drm_sysfs_connector_remove(drm_connector);
111 	drm_connector_cleanup(drm_connector);
112 	kfree(drm_connector);
113 }
114 
115 static struct nouveau_i2c_chan *
116 nouveau_connector_ddc_detect(struct drm_connector *connector,
117 			     struct nouveau_encoder **pnv_encoder)
118 {
119 	struct drm_device *dev = connector->dev;
120 	int i;
121 
122 	for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
123 		struct nouveau_i2c_chan *i2c = NULL;
124 		struct nouveau_encoder *nv_encoder;
125 		struct drm_mode_object *obj;
126 		int id;
127 
128 		id = connector->encoder_ids[i];
129 		if (!id)
130 			break;
131 
132 		obj = drm_mode_object_find(dev, id, DRM_MODE_OBJECT_ENCODER);
133 		if (!obj)
134 			continue;
135 		nv_encoder = nouveau_encoder(obj_to_encoder(obj));
136 
137 		if (nv_encoder->dcb->i2c_index < 0xf)
138 			i2c = nouveau_i2c_find(dev, nv_encoder->dcb->i2c_index);
139 
140 		if (i2c && nouveau_probe_i2c_addr(i2c, 0x50)) {
141 			*pnv_encoder = nv_encoder;
142 			return i2c;
143 		}
144 	}
145 
146 	return NULL;
147 }
148 
149 static struct nouveau_encoder *
150 nouveau_connector_of_detect(struct drm_connector *connector)
151 {
152 #ifdef __powerpc__
153 	struct drm_device *dev = connector->dev;
154 	struct nouveau_connector *nv_connector = nouveau_connector(connector);
155 	struct nouveau_encoder *nv_encoder;
156 	struct device_node *cn, *dn = pci_device_to_OF_node(dev->pdev);
157 
158 	if (!dn ||
159 	    !((nv_encoder = find_encoder_by_type(connector, OUTPUT_TMDS)) ||
160 	      (nv_encoder = find_encoder_by_type(connector, OUTPUT_ANALOG))))
161 		return NULL;
162 
163 	for_each_child_of_node(dn, cn) {
164 		const char *name = of_get_property(cn, "name", NULL);
165 		const void *edid = of_get_property(cn, "EDID", NULL);
166 		int idx = name ? name[strlen(name) - 1] - 'A' : 0;
167 
168 		if (nv_encoder->dcb->i2c_index == idx && edid) {
169 			nv_connector->edid =
170 				kmemdup(edid, EDID_LENGTH, GFP_KERNEL);
171 			of_node_put(cn);
172 			return nv_encoder;
173 		}
174 	}
175 #endif
176 	return NULL;
177 }
178 
179 static void
180 nouveau_connector_set_encoder(struct drm_connector *connector,
181 			      struct nouveau_encoder *nv_encoder)
182 {
183 	struct nouveau_connector *nv_connector = nouveau_connector(connector);
184 	struct drm_nouveau_private *dev_priv = connector->dev->dev_private;
185 	struct drm_device *dev = connector->dev;
186 
187 	if (nv_connector->detected_encoder == nv_encoder)
188 		return;
189 	nv_connector->detected_encoder = nv_encoder;
190 
191 	if (nv_encoder->dcb->type == OUTPUT_LVDS ||
192 	    nv_encoder->dcb->type == OUTPUT_TMDS) {
193 		connector->doublescan_allowed = false;
194 		connector->interlace_allowed = false;
195 	} else {
196 		connector->doublescan_allowed = true;
197 		if (dev_priv->card_type == NV_20 ||
198 		   (dev_priv->card_type == NV_10 &&
199 		    (dev->pci_device & 0x0ff0) != 0x0100 &&
200 		    (dev->pci_device & 0x0ff0) != 0x0150))
201 			/* HW is broken */
202 			connector->interlace_allowed = false;
203 		else
204 			connector->interlace_allowed = true;
205 	}
206 
207 	if (nv_connector->dcb->type == DCB_CONNECTOR_DVI_I) {
208 		drm_connector_property_set_value(connector,
209 			dev->mode_config.dvi_i_subconnector_property,
210 			nv_encoder->dcb->type == OUTPUT_TMDS ?
211 			DRM_MODE_SUBCONNECTOR_DVID :
212 			DRM_MODE_SUBCONNECTOR_DVIA);
213 	}
214 }
215 
216 static enum drm_connector_status
217 nouveau_connector_detect(struct drm_connector *connector, bool force)
218 {
219 	struct drm_device *dev = connector->dev;
220 	struct nouveau_connector *nv_connector = nouveau_connector(connector);
221 	struct nouveau_encoder *nv_encoder = NULL;
222 	struct nouveau_i2c_chan *i2c;
223 	int type;
224 
225 	/* Cleanup the previous EDID block. */
226 	if (nv_connector->edid) {
227 		drm_mode_connector_update_edid_property(connector, NULL);
228 		kfree(nv_connector->edid);
229 		nv_connector->edid = NULL;
230 	}
231 
232 	i2c = nouveau_connector_ddc_detect(connector, &nv_encoder);
233 	if (i2c) {
234 		nv_connector->edid = drm_get_edid(connector, &i2c->adapter);
235 		drm_mode_connector_update_edid_property(connector,
236 							nv_connector->edid);
237 		if (!nv_connector->edid) {
238 			NV_ERROR(dev, "DDC responded, but no EDID for %s\n",
239 				 drm_get_connector_name(connector));
240 			goto detect_analog;
241 		}
242 
243 		if (nv_encoder->dcb->type == OUTPUT_DP &&
244 		    !nouveau_dp_detect(to_drm_encoder(nv_encoder))) {
245 			NV_ERROR(dev, "Detected %s, but failed init\n",
246 				 drm_get_connector_name(connector));
247 			return connector_status_disconnected;
248 		}
249 
250 		/* Override encoder type for DVI-I based on whether EDID
251 		 * says the display is digital or analog, both use the
252 		 * same i2c channel so the value returned from ddc_detect
253 		 * isn't necessarily correct.
254 		 */
255 		if (nv_connector->dcb->type == DCB_CONNECTOR_DVI_I) {
256 			if (nv_connector->edid->input & DRM_EDID_INPUT_DIGITAL)
257 				type = OUTPUT_TMDS;
258 			else
259 				type = OUTPUT_ANALOG;
260 
261 			nv_encoder = find_encoder_by_type(connector, type);
262 			if (!nv_encoder) {
263 				NV_ERROR(dev, "Detected %d encoder on %s, "
264 					      "but no object!\n", type,
265 					 drm_get_connector_name(connector));
266 				return connector_status_disconnected;
267 			}
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_by_type(connector, OUTPUT_ANALOG);
282 	if (!nv_encoder && !nouveau_tv_disable)
283 		nv_encoder = find_encoder_by_type(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_by_type(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->dcb->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_by_type(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 nouveau_connector *nv_connector = nouveau_connector(connector);
410 	struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
411 	struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
412 	struct drm_device *dev = connector->dev;
413 	int ret;
414 
415 	/* Scaling mode */
416 	if (property == dev->mode_config.scaling_mode_property) {
417 		struct nouveau_crtc *nv_crtc = NULL;
418 		bool modeset = false;
419 
420 		switch (value) {
421 		case DRM_MODE_SCALE_NONE:
422 		case DRM_MODE_SCALE_FULLSCREEN:
423 		case DRM_MODE_SCALE_CENTER:
424 		case DRM_MODE_SCALE_ASPECT:
425 			break;
426 		default:
427 			return -EINVAL;
428 		}
429 
430 		/* LVDS always needs gpu scaling */
431 		if (nv_connector->dcb->type == DCB_CONNECTOR_LVDS &&
432 		    value == DRM_MODE_SCALE_NONE)
433 			return -EINVAL;
434 
435 		/* Changing between GPU and panel scaling requires a full
436 		 * modeset
437 		 */
438 		if ((nv_connector->scaling_mode == DRM_MODE_SCALE_NONE) ||
439 		    (value == DRM_MODE_SCALE_NONE))
440 			modeset = true;
441 		nv_connector->scaling_mode = value;
442 
443 		if (connector->encoder && connector->encoder->crtc)
444 			nv_crtc = nouveau_crtc(connector->encoder->crtc);
445 		if (!nv_crtc)
446 			return 0;
447 
448 		if (modeset || !nv_crtc->set_scale) {
449 			ret = drm_crtc_helper_set_mode(&nv_crtc->base,
450 							&nv_crtc->base.mode,
451 							nv_crtc->base.x,
452 							nv_crtc->base.y, NULL);
453 			if (!ret)
454 				return -EINVAL;
455 		} else {
456 			ret = nv_crtc->set_scale(nv_crtc, value, true);
457 			if (ret)
458 				return ret;
459 		}
460 
461 		return 0;
462 	}
463 
464 	/* Dithering */
465 	if (property == dev->mode_config.dithering_mode_property) {
466 		struct nouveau_crtc *nv_crtc = NULL;
467 
468 		if (value == DRM_MODE_DITHERING_ON)
469 			nv_connector->use_dithering = true;
470 		else
471 			nv_connector->use_dithering = false;
472 
473 		if (connector->encoder && connector->encoder->crtc)
474 			nv_crtc = nouveau_crtc(connector->encoder->crtc);
475 
476 		if (!nv_crtc || !nv_crtc->set_dither)
477 			return 0;
478 
479 		return nv_crtc->set_dither(nv_crtc, nv_connector->use_dithering,
480 					   true);
481 	}
482 
483 	if (nv_encoder && nv_encoder->dcb->type == OUTPUT_TV)
484 		return get_slave_funcs(encoder)->set_property(
485 			encoder, connector, property, value);
486 
487 	return -EINVAL;
488 }
489 
490 static struct drm_display_mode *
491 nouveau_connector_native_mode(struct drm_connector *connector)
492 {
493 	struct drm_connector_helper_funcs *helper = connector->helper_private;
494 	struct nouveau_connector *nv_connector = nouveau_connector(connector);
495 	struct drm_device *dev = connector->dev;
496 	struct drm_display_mode *mode, *largest = NULL;
497 	int high_w = 0, high_h = 0, high_v = 0;
498 
499 	list_for_each_entry(mode, &nv_connector->base.probed_modes, head) {
500 		if (helper->mode_valid(connector, mode) != MODE_OK ||
501 		    (mode->flags & DRM_MODE_FLAG_INTERLACE))
502 			continue;
503 
504 		/* Use preferred mode if there is one.. */
505 		if (mode->type & DRM_MODE_TYPE_PREFERRED) {
506 			NV_DEBUG_KMS(dev, "native mode from preferred\n");
507 			return drm_mode_duplicate(dev, mode);
508 		}
509 
510 		/* Otherwise, take the resolution with the largest width, then
511 		 * height, then vertical refresh
512 		 */
513 		if (mode->hdisplay < high_w)
514 			continue;
515 
516 		if (mode->hdisplay == high_w && mode->vdisplay < high_h)
517 			continue;
518 
519 		if (mode->hdisplay == high_w && mode->vdisplay == high_h &&
520 		    mode->vrefresh < high_v)
521 			continue;
522 
523 		high_w = mode->hdisplay;
524 		high_h = mode->vdisplay;
525 		high_v = mode->vrefresh;
526 		largest = mode;
527 	}
528 
529 	NV_DEBUG_KMS(dev, "native mode from largest: %dx%d@%d\n",
530 		      high_w, high_h, high_v);
531 	return largest ? drm_mode_duplicate(dev, largest) : NULL;
532 }
533 
534 struct moderec {
535 	int hdisplay;
536 	int vdisplay;
537 };
538 
539 static struct moderec scaler_modes[] = {
540 	{ 1920, 1200 },
541 	{ 1920, 1080 },
542 	{ 1680, 1050 },
543 	{ 1600, 1200 },
544 	{ 1400, 1050 },
545 	{ 1280, 1024 },
546 	{ 1280, 960 },
547 	{ 1152, 864 },
548 	{ 1024, 768 },
549 	{ 800, 600 },
550 	{ 720, 400 },
551 	{ 640, 480 },
552 	{ 640, 400 },
553 	{ 640, 350 },
554 	{}
555 };
556 
557 static int
558 nouveau_connector_scaler_modes_add(struct drm_connector *connector)
559 {
560 	struct nouveau_connector *nv_connector = nouveau_connector(connector);
561 	struct drm_display_mode *native = nv_connector->native_mode, *m;
562 	struct drm_device *dev = connector->dev;
563 	struct moderec *mode = &scaler_modes[0];
564 	int modes = 0;
565 
566 	if (!native)
567 		return 0;
568 
569 	while (mode->hdisplay) {
570 		if (mode->hdisplay <= native->hdisplay &&
571 		    mode->vdisplay <= native->vdisplay) {
572 			m = drm_cvt_mode(dev, mode->hdisplay, mode->vdisplay,
573 					 drm_mode_vrefresh(native), false,
574 					 false, false);
575 			if (!m)
576 				continue;
577 
578 			m->type |= DRM_MODE_TYPE_DRIVER;
579 
580 			drm_mode_probed_add(connector, m);
581 			modes++;
582 		}
583 
584 		mode++;
585 	}
586 
587 	return modes;
588 }
589 
590 static int
591 nouveau_connector_get_modes(struct drm_connector *connector)
592 {
593 	struct drm_device *dev = connector->dev;
594 	struct drm_nouveau_private *dev_priv = dev->dev_private;
595 	struct nouveau_connector *nv_connector = nouveau_connector(connector);
596 	struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
597 	struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
598 	int ret = 0;
599 
600 	/* destroy the native mode, the attached monitor could have changed.
601 	 */
602 	if (nv_connector->native_mode) {
603 		drm_mode_destroy(dev, nv_connector->native_mode);
604 		nv_connector->native_mode = NULL;
605 	}
606 
607 	if (nv_connector->edid)
608 		ret = drm_add_edid_modes(connector, nv_connector->edid);
609 	else
610 	if (nv_encoder->dcb->type == OUTPUT_LVDS &&
611 	    (nv_encoder->dcb->lvdsconf.use_straps_for_mode ||
612 	     dev_priv->vbios.fp_no_ddc) && nouveau_bios_fp_mode(dev, NULL)) {
613 		struct drm_display_mode mode;
614 
615 		nouveau_bios_fp_mode(dev, &mode);
616 		nv_connector->native_mode = drm_mode_duplicate(dev, &mode);
617 	}
618 
619 	/* Find the native mode if this is a digital panel, if we didn't
620 	 * find any modes through DDC previously add the native mode to
621 	 * the list of modes.
622 	 */
623 	if (!nv_connector->native_mode)
624 		nv_connector->native_mode =
625 			nouveau_connector_native_mode(connector);
626 	if (ret == 0 && nv_connector->native_mode) {
627 		struct drm_display_mode *mode;
628 
629 		mode = drm_mode_duplicate(dev, nv_connector->native_mode);
630 		drm_mode_probed_add(connector, mode);
631 		ret = 1;
632 	}
633 
634 	if (nv_encoder->dcb->type == OUTPUT_TV)
635 		ret = get_slave_funcs(encoder)->get_modes(encoder, connector);
636 
637 	if (nv_connector->dcb->type == DCB_CONNECTOR_LVDS ||
638 	    nv_connector->dcb->type == DCB_CONNECTOR_eDP)
639 		ret += nouveau_connector_scaler_modes_add(connector);
640 
641 	return ret;
642 }
643 
644 static unsigned
645 get_tmds_link_bandwidth(struct drm_connector *connector)
646 {
647 	struct nouveau_connector *nv_connector = nouveau_connector(connector);
648 	struct drm_nouveau_private *dev_priv = connector->dev->dev_private;
649 	struct dcb_entry *dcb = nv_connector->detected_encoder->dcb;
650 
651 	if (dcb->location != DCB_LOC_ON_CHIP ||
652 	    dev_priv->chipset >= 0x46)
653 		return 165000;
654 	else if (dev_priv->chipset >= 0x40)
655 		return 155000;
656 	else if (dev_priv->chipset >= 0x18)
657 		return 135000;
658 	else
659 		return 112000;
660 }
661 
662 static int
663 nouveau_connector_mode_valid(struct drm_connector *connector,
664 			     struct drm_display_mode *mode)
665 {
666 	struct nouveau_connector *nv_connector = nouveau_connector(connector);
667 	struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
668 	struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
669 	unsigned min_clock = 25000, max_clock = min_clock;
670 	unsigned clock = mode->clock;
671 
672 	switch (nv_encoder->dcb->type) {
673 	case OUTPUT_LVDS:
674 		if (nv_connector->native_mode &&
675 		    (mode->hdisplay > nv_connector->native_mode->hdisplay ||
676 		     mode->vdisplay > nv_connector->native_mode->vdisplay))
677 			return MODE_PANEL;
678 
679 		min_clock = 0;
680 		max_clock = 400000;
681 		break;
682 	case OUTPUT_TMDS:
683 		max_clock = get_tmds_link_bandwidth(connector);
684 		if (nouveau_duallink && nv_encoder->dcb->duallink_possible)
685 			max_clock *= 2;
686 		break;
687 	case OUTPUT_ANALOG:
688 		max_clock = nv_encoder->dcb->crtconf.maxfreq;
689 		if (!max_clock)
690 			max_clock = 350000;
691 		break;
692 	case OUTPUT_TV:
693 		return get_slave_funcs(encoder)->mode_valid(encoder, mode);
694 	case OUTPUT_DP:
695 		if (nv_encoder->dp.link_bw == DP_LINK_BW_2_7)
696 			max_clock = nv_encoder->dp.link_nr * 270000;
697 		else
698 			max_clock = nv_encoder->dp.link_nr * 162000;
699 
700 		clock = clock * nouveau_connector_bpp(connector) / 8;
701 		break;
702 	default:
703 		BUG_ON(1);
704 		return MODE_BAD;
705 	}
706 
707 	if (clock < min_clock)
708 		return MODE_CLOCK_LOW;
709 
710 	if (clock > max_clock)
711 		return MODE_CLOCK_HIGH;
712 
713 	return MODE_OK;
714 }
715 
716 static struct drm_encoder *
717 nouveau_connector_best_encoder(struct drm_connector *connector)
718 {
719 	struct nouveau_connector *nv_connector = nouveau_connector(connector);
720 
721 	if (nv_connector->detected_encoder)
722 		return to_drm_encoder(nv_connector->detected_encoder);
723 
724 	return NULL;
725 }
726 
727 static const struct drm_connector_helper_funcs
728 nouveau_connector_helper_funcs = {
729 	.get_modes = nouveau_connector_get_modes,
730 	.mode_valid = nouveau_connector_mode_valid,
731 	.best_encoder = nouveau_connector_best_encoder,
732 };
733 
734 static const struct drm_connector_funcs
735 nouveau_connector_funcs = {
736 	.dpms = drm_helper_connector_dpms,
737 	.save = NULL,
738 	.restore = NULL,
739 	.detect = nouveau_connector_detect,
740 	.destroy = nouveau_connector_destroy,
741 	.fill_modes = drm_helper_probe_single_connector_modes,
742 	.set_property = nouveau_connector_set_property,
743 	.force = nouveau_connector_force
744 };
745 
746 static const struct drm_connector_funcs
747 nouveau_connector_funcs_lvds = {
748 	.dpms = drm_helper_connector_dpms,
749 	.save = NULL,
750 	.restore = NULL,
751 	.detect = nouveau_connector_detect_lvds,
752 	.destroy = nouveau_connector_destroy,
753 	.fill_modes = drm_helper_probe_single_connector_modes,
754 	.set_property = nouveau_connector_set_property,
755 	.force = nouveau_connector_force
756 };
757 
758 struct drm_connector *
759 nouveau_connector_create(struct drm_device *dev, int index)
760 {
761 	const struct drm_connector_funcs *funcs = &nouveau_connector_funcs;
762 	struct drm_nouveau_private *dev_priv = dev->dev_private;
763 	struct nouveau_connector *nv_connector = NULL;
764 	struct dcb_connector_table_entry *dcb = NULL;
765 	struct drm_connector *connector;
766 	int type, ret = 0;
767 
768 	NV_DEBUG_KMS(dev, "\n");
769 
770 	if (index >= dev_priv->vbios.dcb.connector.entries)
771 		return ERR_PTR(-EINVAL);
772 
773 	dcb = &dev_priv->vbios.dcb.connector.entry[index];
774 	if (dcb->drm)
775 		return dcb->drm;
776 
777 	switch (dcb->type) {
778 	case DCB_CONNECTOR_VGA:
779 		type = DRM_MODE_CONNECTOR_VGA;
780 		break;
781 	case DCB_CONNECTOR_TV_0:
782 	case DCB_CONNECTOR_TV_1:
783 	case DCB_CONNECTOR_TV_3:
784 		type = DRM_MODE_CONNECTOR_TV;
785 		break;
786 	case DCB_CONNECTOR_DVI_I:
787 		type = DRM_MODE_CONNECTOR_DVII;
788 		break;
789 	case DCB_CONNECTOR_DVI_D:
790 		type = DRM_MODE_CONNECTOR_DVID;
791 		break;
792 	case DCB_CONNECTOR_HDMI_0:
793 	case DCB_CONNECTOR_HDMI_1:
794 		type = DRM_MODE_CONNECTOR_HDMIA;
795 		break;
796 	case DCB_CONNECTOR_LVDS:
797 		type = DRM_MODE_CONNECTOR_LVDS;
798 		funcs = &nouveau_connector_funcs_lvds;
799 		break;
800 	case DCB_CONNECTOR_DP:
801 		type = DRM_MODE_CONNECTOR_DisplayPort;
802 		break;
803 	case DCB_CONNECTOR_eDP:
804 		type = DRM_MODE_CONNECTOR_eDP;
805 		break;
806 	default:
807 		NV_ERROR(dev, "unknown connector type: 0x%02x!!\n", dcb->type);
808 		return ERR_PTR(-EINVAL);
809 	}
810 
811 	nv_connector = kzalloc(sizeof(*nv_connector), GFP_KERNEL);
812 	if (!nv_connector)
813 		return ERR_PTR(-ENOMEM);
814 	nv_connector->dcb = dcb;
815 	connector = &nv_connector->base;
816 
817 	/* defaults, will get overridden in detect() */
818 	connector->interlace_allowed = false;
819 	connector->doublescan_allowed = false;
820 
821 	drm_connector_init(dev, connector, funcs, type);
822 	drm_connector_helper_add(connector, &nouveau_connector_helper_funcs);
823 
824 	/* Check if we need dithering enabled */
825 	if (dcb->type == DCB_CONNECTOR_LVDS) {
826 		bool dummy, is_24bit = false;
827 
828 		ret = nouveau_bios_parse_lvds_table(dev, 0, &dummy, &is_24bit);
829 		if (ret) {
830 			NV_ERROR(dev, "Error parsing LVDS table, disabling "
831 				 "LVDS\n");
832 			goto fail;
833 		}
834 
835 		nv_connector->use_dithering = !is_24bit;
836 	}
837 
838 	/* Init DVI-I specific properties */
839 	if (dcb->type == DCB_CONNECTOR_DVI_I) {
840 		drm_mode_create_dvi_i_properties(dev);
841 		drm_connector_attach_property(connector, dev->mode_config.dvi_i_subconnector_property, 0);
842 		drm_connector_attach_property(connector, dev->mode_config.dvi_i_select_subconnector_property, 0);
843 	}
844 
845 	switch (dcb->type) {
846 	case DCB_CONNECTOR_VGA:
847 		if (dev_priv->card_type >= NV_50) {
848 			drm_connector_attach_property(connector,
849 					dev->mode_config.scaling_mode_property,
850 					nv_connector->scaling_mode);
851 		}
852 		connector->polled = DRM_CONNECTOR_POLL_CONNECT;
853 		/* fall-through */
854 	case DCB_CONNECTOR_TV_0:
855 	case DCB_CONNECTOR_TV_1:
856 	case DCB_CONNECTOR_TV_3:
857 		nv_connector->scaling_mode = DRM_MODE_SCALE_NONE;
858 		break;
859 	default:
860 		nv_connector->scaling_mode = DRM_MODE_SCALE_FULLSCREEN;
861 
862 		drm_connector_attach_property(connector,
863 				dev->mode_config.scaling_mode_property,
864 				nv_connector->scaling_mode);
865 		drm_connector_attach_property(connector,
866 				dev->mode_config.dithering_mode_property,
867 				nv_connector->use_dithering ?
868 				DRM_MODE_DITHERING_ON : DRM_MODE_DITHERING_OFF);
869 
870 		if (dcb->type != DCB_CONNECTOR_LVDS) {
871 			if (dev_priv->card_type >= NV_50)
872 				connector->polled = DRM_CONNECTOR_POLL_HPD;
873 			else
874 				connector->polled = DRM_CONNECTOR_POLL_CONNECT;
875 		}
876 		break;
877 	}
878 
879 	drm_sysfs_connector_add(connector);
880 	dcb->drm = connector;
881 	return dcb->drm;
882 
883 fail:
884 	drm_connector_cleanup(connector);
885 	kfree(connector);
886 	return ERR_PTR(ret);
887 
888 }
889