1/*	$NetBSD: nouveau_connector.c,v 1.7 2021/12/19 10:49:21 riastradh Exp $	*/
2
3/*
4 * Copyright (C) 2008 Maarten Maathuis.
5 * All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial
17 * portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
23 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 *
27 */
28
29#include <sys/cdefs.h>
30__KERNEL_RCSID(0, "$NetBSD: nouveau_connector.c,v 1.7 2021/12/19 10:49:21 riastradh Exp $");
31
32#include <acpi/button.h>
33
34#include <linux/pm_runtime.h>
35#include <linux/vga_switcheroo.h>
36
37#include <drm/drm_atomic_helper.h>
38#include <drm/drm_edid.h>
39#include <drm/drm_crtc_helper.h>
40#include <drm/drm_probe_helper.h>
41#include <drm/drm_atomic.h>
42
43#include "nouveau_reg.h"
44#include "nouveau_drv.h"
45#include "dispnv04/hw.h"
46#include "nouveau_acpi.h"
47
48#include "nouveau_display.h"
49#include "nouveau_connector.h"
50#include "nouveau_encoder.h"
51#include "nouveau_crtc.h"
52
53#include <nvif/class.h>
54#include <nvif/cl0046.h>
55#include <nvif/event.h>
56
57struct drm_display_mode *
58nouveau_conn_native_mode(struct drm_connector *connector)
59{
60	const struct drm_connector_helper_funcs *helper = connector->helper_private;
61	struct nouveau_drm *drm = nouveau_drm(connector->dev);
62	struct drm_device *dev = connector->dev;
63	struct drm_display_mode *mode, *largest = NULL;
64	int high_w = 0, high_h = 0, high_v = 0;
65
66	list_for_each_entry(mode, &connector->probed_modes, head) {
67		mode->vrefresh = drm_mode_vrefresh(mode);
68		if (helper->mode_valid(connector, mode) != MODE_OK ||
69		    (mode->flags & DRM_MODE_FLAG_INTERLACE))
70			continue;
71
72		/* Use preferred mode if there is one.. */
73		if (mode->type & DRM_MODE_TYPE_PREFERRED) {
74			NV_DEBUG(drm, "native mode from preferred\n");
75			return drm_mode_duplicate(dev, mode);
76		}
77
78		/* Otherwise, take the resolution with the largest width, then
79		 * height, then vertical refresh
80		 */
81		if (mode->hdisplay < high_w)
82			continue;
83
84		if (mode->hdisplay == high_w && mode->vdisplay < high_h)
85			continue;
86
87		if (mode->hdisplay == high_w && mode->vdisplay == high_h &&
88		    mode->vrefresh < high_v)
89			continue;
90
91		high_w = mode->hdisplay;
92		high_h = mode->vdisplay;
93		high_v = mode->vrefresh;
94		largest = mode;
95	}
96
97	NV_DEBUG(drm, "native mode from largest: %dx%d@%d\n",
98		      high_w, high_h, high_v);
99	return largest ? drm_mode_duplicate(dev, largest) : NULL;
100}
101
102int
103nouveau_conn_atomic_get_property(struct drm_connector *connector,
104				 const struct drm_connector_state *state,
105				 struct drm_property *property, u64 *val)
106{
107	const struct nouveau_conn_atom *asyc = nouveau_conn_atom_const(state);
108	struct nouveau_display *disp = nouveau_display(connector->dev);
109	struct drm_device *dev = connector->dev;
110
111	if (property == dev->mode_config.scaling_mode_property)
112		*val = asyc->scaler.mode;
113	else if (property == disp->underscan_property)
114		*val = asyc->scaler.underscan.mode;
115	else if (property == disp->underscan_hborder_property)
116		*val = asyc->scaler.underscan.hborder;
117	else if (property == disp->underscan_vborder_property)
118		*val = asyc->scaler.underscan.vborder;
119	else if (property == disp->dithering_mode)
120		*val = asyc->dither.mode;
121	else if (property == disp->dithering_depth)
122		*val = asyc->dither.depth;
123	else if (property == disp->vibrant_hue_property)
124		*val = asyc->procamp.vibrant_hue;
125	else if (property == disp->color_vibrance_property)
126		*val = asyc->procamp.color_vibrance;
127	else
128		return -EINVAL;
129
130	return 0;
131}
132
133int
134nouveau_conn_atomic_set_property(struct drm_connector *connector,
135				 struct drm_connector_state *state,
136				 struct drm_property *property, u64 val)
137{
138	struct drm_device *dev = connector->dev;
139	struct nouveau_conn_atom *asyc = nouveau_conn_atom(state);
140	struct nouveau_display *disp = nouveau_display(dev);
141
142	if (property == dev->mode_config.scaling_mode_property) {
143		switch (val) {
144		case DRM_MODE_SCALE_NONE:
145			/* We allow 'None' for EDID modes, even on a fixed
146			 * panel (some exist with support for lower refresh
147			 * rates, which people might want to use for power-
148			 * saving purposes).
149			 *
150			 * Non-EDID modes will force the use of GPU scaling
151			 * to the native mode regardless of this setting.
152			 */
153			switch (connector->connector_type) {
154			case DRM_MODE_CONNECTOR_LVDS:
155			case DRM_MODE_CONNECTOR_eDP:
156				/* ... except prior to G80, where the code
157				 * doesn't support such things.
158				 */
159				if (disp->disp.object.oclass < NV50_DISP)
160					return -EINVAL;
161				break;
162			default:
163				break;
164			}
165		case DRM_MODE_SCALE_FULLSCREEN:
166		case DRM_MODE_SCALE_CENTER:
167		case DRM_MODE_SCALE_ASPECT:
168			break;
169		default:
170			return -EINVAL;
171		}
172
173		if (asyc->scaler.mode != val) {
174			asyc->scaler.mode = val;
175			asyc->set.scaler = true;
176		}
177	} else
178	if (property == disp->underscan_property) {
179		if (asyc->scaler.underscan.mode != val) {
180			asyc->scaler.underscan.mode = val;
181			asyc->set.scaler = true;
182		}
183	} else
184	if (property == disp->underscan_hborder_property) {
185		if (asyc->scaler.underscan.hborder != val) {
186			asyc->scaler.underscan.hborder = val;
187			asyc->set.scaler = true;
188		}
189	} else
190	if (property == disp->underscan_vborder_property) {
191		if (asyc->scaler.underscan.vborder != val) {
192			asyc->scaler.underscan.vborder = val;
193			asyc->set.scaler = true;
194		}
195	} else
196	if (property == disp->dithering_mode) {
197		if (asyc->dither.mode != val) {
198			asyc->dither.mode = val;
199			asyc->set.dither = true;
200		}
201	} else
202	if (property == disp->dithering_depth) {
203		if (asyc->dither.mode != val) {
204			asyc->dither.depth = val;
205			asyc->set.dither = true;
206		}
207	} else
208	if (property == disp->vibrant_hue_property) {
209		if (asyc->procamp.vibrant_hue != val) {
210			asyc->procamp.vibrant_hue = val;
211			asyc->set.procamp = true;
212		}
213	} else
214	if (property == disp->color_vibrance_property) {
215		if (asyc->procamp.color_vibrance != val) {
216			asyc->procamp.color_vibrance = val;
217			asyc->set.procamp = true;
218		}
219	} else {
220		return -EINVAL;
221	}
222
223	return 0;
224}
225
226void
227nouveau_conn_atomic_destroy_state(struct drm_connector *connector,
228				  struct drm_connector_state *state)
229{
230	struct nouveau_conn_atom *asyc = nouveau_conn_atom(state);
231	__drm_atomic_helper_connector_destroy_state(&asyc->state);
232	kfree(asyc);
233}
234
235struct drm_connector_state *
236nouveau_conn_atomic_duplicate_state(struct drm_connector *connector)
237{
238	struct nouveau_conn_atom *armc = nouveau_conn_atom(connector->state);
239	struct nouveau_conn_atom *asyc;
240	if (!(asyc = kmalloc(sizeof(*asyc), GFP_KERNEL)))
241		return NULL;
242	__drm_atomic_helper_connector_duplicate_state(connector, &asyc->state);
243	asyc->dither = armc->dither;
244	asyc->scaler = armc->scaler;
245	asyc->procamp = armc->procamp;
246	asyc->set.mask = 0;
247	return &asyc->state;
248}
249
250void
251nouveau_conn_reset(struct drm_connector *connector)
252{
253	struct nouveau_connector *nv_connector = nouveau_connector(connector);
254	struct nouveau_conn_atom *asyc;
255
256	if (drm_drv_uses_atomic_modeset(connector->dev)) {
257		if (WARN_ON(!(asyc = kzalloc(sizeof(*asyc), GFP_KERNEL))))
258			return;
259
260		if (connector->state)
261			nouveau_conn_atomic_destroy_state(connector,
262							  connector->state);
263
264		__drm_atomic_helper_connector_reset(connector, &asyc->state);
265	} else {
266		asyc = &nv_connector->properties_state;
267	}
268
269	asyc->dither.mode = DITHERING_MODE_AUTO;
270	asyc->dither.depth = DITHERING_DEPTH_AUTO;
271	asyc->scaler.mode = DRM_MODE_SCALE_NONE;
272	asyc->scaler.underscan.mode = UNDERSCAN_OFF;
273	asyc->procamp.color_vibrance = 150;
274	asyc->procamp.vibrant_hue = 90;
275
276	if (nouveau_display(connector->dev)->disp.object.oclass < NV50_DISP) {
277		switch (connector->connector_type) {
278		case DRM_MODE_CONNECTOR_LVDS:
279			/* See note in nouveau_conn_atomic_set_property(). */
280			asyc->scaler.mode = DRM_MODE_SCALE_FULLSCREEN;
281			break;
282		default:
283			break;
284		}
285	}
286}
287
288void
289nouveau_conn_attach_properties(struct drm_connector *connector)
290{
291	struct drm_device *dev = connector->dev;
292	struct nouveau_display *disp = nouveau_display(dev);
293	struct nouveau_connector *nv_connector = nouveau_connector(connector);
294	struct nouveau_conn_atom *armc;
295
296	if (drm_drv_uses_atomic_modeset(connector->dev))
297		armc = nouveau_conn_atom(connector->state);
298	else
299		armc = &nv_connector->properties_state;
300
301	/* Init DVI-I specific properties. */
302	if (connector->connector_type == DRM_MODE_CONNECTOR_DVII)
303		drm_object_attach_property(&connector->base, dev->mode_config.
304					   dvi_i_subconnector_property, 0);
305
306	/* Add overscan compensation options to digital outputs. */
307	if (disp->underscan_property &&
308	    (connector->connector_type == DRM_MODE_CONNECTOR_DVID ||
309	     connector->connector_type == DRM_MODE_CONNECTOR_DVII ||
310	     connector->connector_type == DRM_MODE_CONNECTOR_HDMIA ||
311	     connector->connector_type == DRM_MODE_CONNECTOR_DisplayPort)) {
312		drm_object_attach_property(&connector->base,
313					   disp->underscan_property,
314					   UNDERSCAN_OFF);
315		drm_object_attach_property(&connector->base,
316					   disp->underscan_hborder_property, 0);
317		drm_object_attach_property(&connector->base,
318					   disp->underscan_vborder_property, 0);
319	}
320
321	/* Add hue and saturation options. */
322	if (disp->vibrant_hue_property)
323		drm_object_attach_property(&connector->base,
324					   disp->vibrant_hue_property,
325					   armc->procamp.vibrant_hue);
326	if (disp->color_vibrance_property)
327		drm_object_attach_property(&connector->base,
328					   disp->color_vibrance_property,
329					   armc->procamp.color_vibrance);
330
331	/* Scaling mode property. */
332	switch (connector->connector_type) {
333	case DRM_MODE_CONNECTOR_TV:
334		break;
335	case DRM_MODE_CONNECTOR_VGA:
336		if (disp->disp.object.oclass < NV50_DISP)
337			break; /* Can only scale on DFPs. */
338		/* Fall-through. */
339	default:
340		drm_object_attach_property(&connector->base, dev->mode_config.
341					   scaling_mode_property,
342					   armc->scaler.mode);
343		break;
344	}
345
346	/* Dithering properties. */
347	switch (connector->connector_type) {
348	case DRM_MODE_CONNECTOR_TV:
349	case DRM_MODE_CONNECTOR_VGA:
350		break;
351	default:
352		if (disp->dithering_mode) {
353			drm_object_attach_property(&connector->base,
354						   disp->dithering_mode,
355						   armc->dither.mode);
356		}
357		if (disp->dithering_depth) {
358			drm_object_attach_property(&connector->base,
359						   disp->dithering_depth,
360						   armc->dither.depth);
361		}
362		break;
363	}
364}
365
366MODULE_PARM_DESC(tv_disable, "Disable TV-out detection");
367int nouveau_tv_disable = 0;
368module_param_named(tv_disable, nouveau_tv_disable, int, 0400);
369
370#if defined(CONFIG_ACPI_BUTTON) || \
371	(defined(CONFIG_ACPI_BUTTON_MODULE) && defined(MODULE))
372MODULE_PARM_DESC(ignorelid, "Ignore ACPI lid status");
373int nouveau_ignorelid = 0;
374module_param_named(ignorelid, nouveau_ignorelid, int, 0400);
375#endif
376
377MODULE_PARM_DESC(duallink, "Allow dual-link TMDS (default: enabled)");
378int nouveau_duallink = 1;
379module_param_named(duallink, nouveau_duallink, int, 0400);
380
381MODULE_PARM_DESC(hdmimhz, "Force a maximum HDMI pixel clock (in MHz)");
382int nouveau_hdmimhz = 0;
383module_param_named(hdmimhz, nouveau_hdmimhz, int, 0400);
384
385struct nouveau_encoder *
386find_encoder(struct drm_connector *connector, int type)
387{
388	struct nouveau_encoder *nv_encoder;
389	struct drm_encoder *enc;
390
391	drm_connector_for_each_possible_encoder(connector, enc) {
392		nv_encoder = nouveau_encoder(enc);
393
394		if (type == DCB_OUTPUT_ANY ||
395		    (nv_encoder->dcb && nv_encoder->dcb->type == type))
396			return nv_encoder;
397	}
398
399	return NULL;
400}
401
402struct nouveau_connector *
403nouveau_encoder_connector_get(struct nouveau_encoder *encoder)
404{
405	struct drm_device *dev = to_drm_encoder(encoder)->dev;
406	struct drm_connector *drm_connector;
407
408	list_for_each_entry(drm_connector, &dev->mode_config.connector_list, head) {
409		if (drm_connector->encoder == to_drm_encoder(encoder))
410			return nouveau_connector(drm_connector);
411	}
412
413	return NULL;
414}
415
416static void
417nouveau_connector_destroy(struct drm_connector *connector)
418{
419	struct nouveau_connector *nv_connector = nouveau_connector(connector);
420	nvif_notify_fini(&nv_connector->hpd);
421	kfree(nv_connector->edid);
422	drm_connector_unregister(connector);
423	drm_connector_cleanup(connector);
424	if (nv_connector->aux.transfer) {
425		drm_dp_cec_unregister_connector(&nv_connector->aux);
426		drm_dp_aux_unregister(&nv_connector->aux);
427		kfree(__UNCONST(nv_connector->aux.name));
428	}
429	kfree(connector);
430}
431
432static struct nouveau_encoder *
433nouveau_connector_ddc_detect(struct drm_connector *connector)
434{
435	struct drm_device *dev = connector->dev;
436	struct nouveau_encoder *nv_encoder = NULL, *found = NULL;
437	struct drm_encoder *encoder;
438	int ret;
439	bool switcheroo_ddc = false;
440
441	drm_connector_for_each_possible_encoder(connector, encoder) {
442		nv_encoder = nouveau_encoder(encoder);
443
444		switch (nv_encoder->dcb->type) {
445		case DCB_OUTPUT_DP:
446			ret = nouveau_dp_detect(nv_encoder);
447			if (ret == NOUVEAU_DP_MST)
448				return NULL;
449			else if (ret == NOUVEAU_DP_SST)
450				found = nv_encoder;
451
452			break;
453		case DCB_OUTPUT_LVDS:
454			switcheroo_ddc = !!(vga_switcheroo_handler_flags() &
455					    VGA_SWITCHEROO_CAN_SWITCH_DDC);
456		/* fall-through */
457		default:
458			if (!nv_encoder->i2c)
459				break;
460
461#ifdef __NetBSD__
462			__USE(switcheroo_ddc);
463			__USE(dev);
464#else
465			if (switcheroo_ddc)
466				vga_switcheroo_lock_ddc(dev->pdev);
467#endif
468			if (nvkm_probe_i2c(nv_encoder->i2c, 0x50))
469				found = nv_encoder;
470#ifndef __NetBSD__
471			if (switcheroo_ddc)
472				vga_switcheroo_unlock_ddc(dev->pdev);
473#endif
474
475			break;
476		}
477		if (found)
478			break;
479	}
480
481	return found;
482}
483
484static struct nouveau_encoder *
485nouveau_connector_of_detect(struct drm_connector *connector)
486{
487#ifdef __powerpc__
488	struct drm_device *dev = connector->dev;
489	struct nouveau_connector *nv_connector = nouveau_connector(connector);
490	struct nouveau_encoder *nv_encoder;
491	struct device_node *cn, *dn = pci_device_to_OF_node(dev->pdev);
492
493	if (!dn ||
494	    !((nv_encoder = find_encoder(connector, DCB_OUTPUT_TMDS)) ||
495	      (nv_encoder = find_encoder(connector, DCB_OUTPUT_ANALOG))))
496		return NULL;
497
498	for_each_child_of_node(dn, cn) {
499		const char *name = of_get_property(cn, "name", NULL);
500		const void *edid = of_get_property(cn, "EDID", NULL);
501		int idx = name ? name[strlen(name) - 1] - 'A' : 0;
502
503		if (nv_encoder->dcb->i2c_index == idx && edid) {
504			nv_connector->edid =
505				kmemdup(edid, EDID_LENGTH, GFP_KERNEL);
506			of_node_put(cn);
507			return nv_encoder;
508		}
509	}
510#endif
511	return NULL;
512}
513
514static void
515nouveau_connector_set_encoder(struct drm_connector *connector,
516			      struct nouveau_encoder *nv_encoder)
517{
518	struct nouveau_connector *nv_connector = nouveau_connector(connector);
519	struct nouveau_drm *drm = nouveau_drm(connector->dev);
520	struct drm_device *dev = connector->dev;
521
522	if (nv_connector->detected_encoder == nv_encoder)
523		return;
524	nv_connector->detected_encoder = nv_encoder;
525
526	if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_TESLA) {
527		connector->interlace_allowed = true;
528		connector->doublescan_allowed = true;
529	} else
530	if (nv_encoder->dcb->type == DCB_OUTPUT_LVDS ||
531	    nv_encoder->dcb->type == DCB_OUTPUT_TMDS) {
532		connector->doublescan_allowed = false;
533		connector->interlace_allowed = false;
534	} else {
535		connector->doublescan_allowed = true;
536		if (drm->client.device.info.family == NV_DEVICE_INFO_V0_KELVIN ||
537		    (drm->client.device.info.family == NV_DEVICE_INFO_V0_CELSIUS &&
538		     (dev->pdev->device & 0x0ff0) != 0x0100 &&
539		     (dev->pdev->device & 0x0ff0) != 0x0150))
540			/* HW is broken */
541			connector->interlace_allowed = false;
542		else
543			connector->interlace_allowed = true;
544	}
545
546	if (nv_connector->type == DCB_CONNECTOR_DVI_I) {
547		drm_object_property_set_value(&connector->base,
548			dev->mode_config.dvi_i_subconnector_property,
549			nv_encoder->dcb->type == DCB_OUTPUT_TMDS ?
550			DRM_MODE_SUBCONNECTOR_DVID :
551			DRM_MODE_SUBCONNECTOR_DVIA);
552	}
553}
554
555static enum drm_connector_status
556nouveau_connector_detect(struct drm_connector *connector, bool force)
557{
558	struct drm_device *dev = connector->dev;
559	struct nouveau_drm *drm = nouveau_drm(dev);
560	struct nouveau_connector *nv_connector = nouveau_connector(connector);
561	struct nouveau_encoder *nv_encoder = NULL;
562	struct nouveau_encoder *nv_partner;
563	struct i2c_adapter *i2c;
564	int type;
565	int ret;
566	enum drm_connector_status conn_status = connector_status_disconnected;
567
568	/* Cleanup the previous EDID block. */
569	if (nv_connector->edid) {
570		drm_connector_update_edid_property(connector, NULL);
571		kfree(nv_connector->edid);
572		nv_connector->edid = NULL;
573	}
574
575	/* Outputs are only polled while runtime active, so resuming the
576	 * device here is unnecessary (and would deadlock upon runtime suspend
577	 * because it waits for polling to finish). We do however, want to
578	 * prevent the autosuspend timer from elapsing during this operation
579	 * if possible.
580	 */
581	if (drm_kms_helper_is_poll_worker()) {
582		pm_runtime_get_noresume(dev->dev);
583	} else {
584		ret = pm_runtime_get_sync(dev->dev);
585		if (ret < 0 && ret != -EACCES)
586			return conn_status;
587	}
588
589	nv_encoder = nouveau_connector_ddc_detect(connector);
590	if (nv_encoder && (i2c = nv_encoder->i2c) != NULL) {
591		if ((vga_switcheroo_handler_flags() &
592		     VGA_SWITCHEROO_CAN_SWITCH_DDC) &&
593		    nv_connector->type == DCB_CONNECTOR_LVDS)
594			nv_connector->edid = drm_get_edid_switcheroo(connector,
595								     i2c);
596		else
597			nv_connector->edid = drm_get_edid(connector, i2c);
598
599		drm_connector_update_edid_property(connector,
600							nv_connector->edid);
601		if (!nv_connector->edid) {
602			NV_ERROR(drm, "DDC responded, but no EDID for %s\n",
603				 connector->name);
604			goto detect_analog;
605		}
606
607		/* Override encoder type for DVI-I based on whether EDID
608		 * says the display is digital or analog, both use the
609		 * same i2c channel so the value returned from ddc_detect
610		 * isn't necessarily correct.
611		 */
612		nv_partner = NULL;
613		if (nv_encoder->dcb->type == DCB_OUTPUT_TMDS)
614			nv_partner = find_encoder(connector, DCB_OUTPUT_ANALOG);
615		if (nv_encoder->dcb->type == DCB_OUTPUT_ANALOG)
616			nv_partner = find_encoder(connector, DCB_OUTPUT_TMDS);
617
618		if (nv_partner && ((nv_encoder->dcb->type == DCB_OUTPUT_ANALOG &&
619				    nv_partner->dcb->type == DCB_OUTPUT_TMDS) ||
620				   (nv_encoder->dcb->type == DCB_OUTPUT_TMDS &&
621				    nv_partner->dcb->type == DCB_OUTPUT_ANALOG))) {
622			if (nv_connector->edid->input & DRM_EDID_INPUT_DIGITAL)
623				type = DCB_OUTPUT_TMDS;
624			else
625				type = DCB_OUTPUT_ANALOG;
626
627			nv_encoder = find_encoder(connector, type);
628			BUG_ON(nv_encoder == NULL);
629		}
630
631		nouveau_connector_set_encoder(connector, nv_encoder);
632		conn_status = connector_status_connected;
633		drm_dp_cec_set_edid(&nv_connector->aux, nv_connector->edid);
634		goto out;
635	}
636
637	nv_encoder = nouveau_connector_of_detect(connector);
638	if (nv_encoder) {
639		nouveau_connector_set_encoder(connector, nv_encoder);
640		conn_status = connector_status_connected;
641		goto out;
642	}
643
644detect_analog:
645	nv_encoder = find_encoder(connector, DCB_OUTPUT_ANALOG);
646	if (!nv_encoder && !nouveau_tv_disable)
647		nv_encoder = find_encoder(connector, DCB_OUTPUT_TV);
648	if (nv_encoder && force) {
649		struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
650		const struct drm_encoder_helper_funcs *helper =
651						encoder->helper_private;
652
653		if (helper->detect(encoder, connector) ==
654						connector_status_connected) {
655			nouveau_connector_set_encoder(connector, nv_encoder);
656			conn_status = connector_status_connected;
657			goto out;
658		}
659
660	}
661
662 out:
663
664	pm_runtime_mark_last_busy(dev->dev);
665	pm_runtime_put_autosuspend(dev->dev);
666
667	return conn_status;
668}
669
670static enum drm_connector_status
671nouveau_connector_detect_lvds(struct drm_connector *connector, bool force)
672{
673	struct drm_device *dev = connector->dev;
674	struct nouveau_drm *drm = nouveau_drm(dev);
675	struct nouveau_connector *nv_connector = nouveau_connector(connector);
676	struct nouveau_encoder *nv_encoder = NULL;
677	enum drm_connector_status status = connector_status_disconnected;
678
679	/* Cleanup the previous EDID block. */
680	if (nv_connector->edid) {
681		drm_connector_update_edid_property(connector, NULL);
682		kfree(nv_connector->edid);
683		nv_connector->edid = NULL;
684	}
685
686	nv_encoder = find_encoder(connector, DCB_OUTPUT_LVDS);
687	if (!nv_encoder)
688		return connector_status_disconnected;
689
690	/* Try retrieving EDID via DDC */
691	if (!drm->vbios.fp_no_ddc) {
692		status = nouveau_connector_detect(connector, force);
693		if (status == connector_status_connected)
694			goto out;
695	}
696
697	/* On some laptops (Sony, i'm looking at you) there appears to
698	 * be no direct way of accessing the panel's EDID.  The only
699	 * option available to us appears to be to ask ACPI for help..
700	 *
701	 * It's important this check's before trying straps, one of the
702	 * said manufacturer's laptops are configured in such a way
703	 * the nouveau decides an entry in the VBIOS FP mode table is
704	 * valid - it's not (rh#613284)
705	 */
706	if (nv_encoder->dcb->lvdsconf.use_acpi_for_edid) {
707		if ((nv_connector->edid = nouveau_acpi_edid(dev, connector))) {
708			status = connector_status_connected;
709			goto out;
710		}
711	}
712
713	/* If no EDID found above, and the VBIOS indicates a hardcoded
714	 * modeline is avalilable for the panel, set it as the panel's
715	 * native mode and exit.
716	 */
717	if (nouveau_bios_fp_mode(dev, NULL) && (drm->vbios.fp_no_ddc ||
718	    nv_encoder->dcb->lvdsconf.use_straps_for_mode)) {
719		status = connector_status_connected;
720		goto out;
721	}
722
723	/* Still nothing, some VBIOS images have a hardcoded EDID block
724	 * stored for the panel stored in them.
725	 */
726	if (!drm->vbios.fp_no_ddc) {
727		struct edid *edid =
728			(struct edid *)nouveau_bios_embedded_edid(dev);
729		if (edid) {
730			nv_connector->edid =
731					kmemdup(edid, EDID_LENGTH, GFP_KERNEL);
732			if (nv_connector->edid)
733				status = connector_status_connected;
734		}
735	}
736
737out:
738#if defined(CONFIG_ACPI_BUTTON) || \
739	(defined(CONFIG_ACPI_BUTTON_MODULE) && defined(MODULE))
740	if (status == connector_status_connected &&
741	    !nouveau_ignorelid && !acpi_lid_open())
742		status = connector_status_unknown;
743#endif
744
745	drm_connector_update_edid_property(connector, nv_connector->edid);
746	nouveau_connector_set_encoder(connector, nv_encoder);
747	return status;
748}
749
750static void
751nouveau_connector_force(struct drm_connector *connector)
752{
753	struct nouveau_drm *drm = nouveau_drm(connector->dev);
754	struct nouveau_connector *nv_connector = nouveau_connector(connector);
755	struct nouveau_encoder *nv_encoder;
756	int type;
757
758	if (nv_connector->type == DCB_CONNECTOR_DVI_I) {
759		if (connector->force == DRM_FORCE_ON_DIGITAL)
760			type = DCB_OUTPUT_TMDS;
761		else
762			type = DCB_OUTPUT_ANALOG;
763	} else
764		type = DCB_OUTPUT_ANY;
765
766	nv_encoder = find_encoder(connector, type);
767	if (!nv_encoder) {
768		NV_ERROR(drm, "can't find encoder to force %s on!\n",
769			 connector->name);
770		connector->status = connector_status_disconnected;
771		return;
772	}
773
774	nouveau_connector_set_encoder(connector, nv_encoder);
775}
776
777static int
778nouveau_connector_set_property(struct drm_connector *connector,
779			       struct drm_property *property, uint64_t value)
780{
781	struct nouveau_connector *nv_connector = nouveau_connector(connector);
782	struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
783	struct nouveau_conn_atom *asyc = &nv_connector->properties_state;
784	struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
785	int ret;
786
787	ret = connector->funcs->atomic_set_property(&nv_connector->base,
788						    &asyc->state,
789						    property, value);
790	if (ret) {
791		if (nv_encoder && nv_encoder->dcb->type == DCB_OUTPUT_TV)
792			return get_slave_funcs(encoder)->set_property(
793				encoder, connector, property, value);
794		return ret;
795	}
796
797	nv_connector->scaling_mode = asyc->scaler.mode;
798	nv_connector->dithering_mode = asyc->dither.mode;
799
800	if (connector->encoder && connector->encoder->crtc) {
801		ret = drm_crtc_helper_set_mode(connector->encoder->crtc,
802					      &connector->encoder->crtc->mode,
803					       connector->encoder->crtc->x,
804					       connector->encoder->crtc->y,
805					       NULL);
806		if (!ret)
807			return -EINVAL;
808	}
809
810	return 0;
811}
812
813struct moderec {
814	int hdisplay;
815	int vdisplay;
816};
817
818static struct moderec scaler_modes[] = {
819	{ 1920, 1200 },
820	{ 1920, 1080 },
821	{ 1680, 1050 },
822	{ 1600, 1200 },
823	{ 1400, 1050 },
824	{ 1280, 1024 },
825	{ 1280, 960 },
826	{ 1152, 864 },
827	{ 1024, 768 },
828	{ 800, 600 },
829	{ 720, 400 },
830	{ 640, 480 },
831	{ 640, 400 },
832	{ 640, 350 },
833	{}
834};
835
836static int
837nouveau_connector_scaler_modes_add(struct drm_connector *connector)
838{
839	struct nouveau_connector *nv_connector = nouveau_connector(connector);
840	struct drm_display_mode *native = nv_connector->native_mode, *m;
841	struct drm_device *dev = connector->dev;
842	struct moderec *mode = &scaler_modes[0];
843	int modes = 0;
844
845	if (!native)
846		return 0;
847
848	while (mode->hdisplay) {
849		if (mode->hdisplay <= native->hdisplay &&
850		    mode->vdisplay <= native->vdisplay &&
851		    (mode->hdisplay != native->hdisplay ||
852		     mode->vdisplay != native->vdisplay)) {
853			m = drm_cvt_mode(dev, mode->hdisplay, mode->vdisplay,
854					 drm_mode_vrefresh(native), false,
855					 false, false);
856			if (!m)
857				continue;
858
859			drm_mode_probed_add(connector, m);
860			modes++;
861		}
862
863		mode++;
864	}
865
866	return modes;
867}
868
869static void
870nouveau_connector_detect_depth(struct drm_connector *connector)
871{
872	struct nouveau_drm *drm = nouveau_drm(connector->dev);
873	struct nouveau_connector *nv_connector = nouveau_connector(connector);
874	struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
875	struct nvbios *bios = &drm->vbios;
876	struct drm_display_mode *mode = nv_connector->native_mode;
877	bool duallink;
878
879	/* if the edid is feeling nice enough to provide this info, use it */
880	if (nv_connector->edid && connector->display_info.bpc)
881		return;
882
883	/* EDID 1.4 is *supposed* to be supported on eDP, but, Apple... */
884	if (nv_connector->type == DCB_CONNECTOR_eDP) {
885		connector->display_info.bpc = 6;
886		return;
887	}
888
889	/* we're out of options unless we're LVDS, default to 8bpc */
890	if (nv_encoder->dcb->type != DCB_OUTPUT_LVDS) {
891		connector->display_info.bpc = 8;
892		return;
893	}
894
895	connector->display_info.bpc = 6;
896
897	/* LVDS: panel straps */
898	if (bios->fp_no_ddc) {
899		if (bios->fp.if_is_24bit)
900			connector->display_info.bpc = 8;
901		return;
902	}
903
904	/* LVDS: DDC panel, need to first determine the number of links to
905	 * know which if_is_24bit flag to check...
906	 */
907	if (nv_connector->edid &&
908	    nv_connector->type == DCB_CONNECTOR_LVDS_SPWG)
909		duallink = ((u8 *)nv_connector->edid)[121] == 2;
910	else
911		duallink = mode->clock >= bios->fp.duallink_transition_clk;
912
913	if ((!duallink && (bios->fp.strapless_is_24bit & 1)) ||
914	    ( duallink && (bios->fp.strapless_is_24bit & 2)))
915		connector->display_info.bpc = 8;
916}
917
918static int
919nouveau_connector_late_register(struct drm_connector *connector)
920{
921	int ret;
922
923	ret = nouveau_backlight_init(connector);
924
925	return ret;
926}
927
928static void
929nouveau_connector_early_unregister(struct drm_connector *connector)
930{
931	nouveau_backlight_fini(connector);
932}
933
934static int
935nouveau_connector_get_modes(struct drm_connector *connector)
936{
937	struct drm_device *dev = connector->dev;
938	struct nouveau_drm *drm = nouveau_drm(dev);
939	struct nouveau_connector *nv_connector = nouveau_connector(connector);
940	struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
941	struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
942	int ret = 0;
943
944	/* destroy the native mode, the attached monitor could have changed.
945	 */
946	if (nv_connector->native_mode) {
947		drm_mode_destroy(dev, nv_connector->native_mode);
948		nv_connector->native_mode = NULL;
949	}
950
951	if (nv_connector->edid)
952		ret = drm_add_edid_modes(connector, nv_connector->edid);
953	else
954	if (nv_encoder->dcb->type == DCB_OUTPUT_LVDS &&
955	    (nv_encoder->dcb->lvdsconf.use_straps_for_mode ||
956	     drm->vbios.fp_no_ddc) && nouveau_bios_fp_mode(dev, NULL)) {
957		struct drm_display_mode mode;
958
959		nouveau_bios_fp_mode(dev, &mode);
960		nv_connector->native_mode = drm_mode_duplicate(dev, &mode);
961	}
962
963	/* Determine display colour depth for everything except LVDS now,
964	 * DP requires this before mode_valid() is called.
965	 */
966	if (connector->connector_type != DRM_MODE_CONNECTOR_LVDS)
967		nouveau_connector_detect_depth(connector);
968
969	/* Find the native mode if this is a digital panel, if we didn't
970	 * find any modes through DDC previously add the native mode to
971	 * the list of modes.
972	 */
973	if (!nv_connector->native_mode)
974		nv_connector->native_mode = nouveau_conn_native_mode(connector);
975	if (ret == 0 && nv_connector->native_mode) {
976		struct drm_display_mode *mode;
977
978		mode = drm_mode_duplicate(dev, nv_connector->native_mode);
979		drm_mode_probed_add(connector, mode);
980		ret = 1;
981	}
982
983	/* Determine LVDS colour depth, must happen after determining
984	 * "native" mode as some VBIOS tables require us to use the
985	 * pixel clock as part of the lookup...
986	 */
987	if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS)
988		nouveau_connector_detect_depth(connector);
989
990	if (nv_encoder->dcb->type == DCB_OUTPUT_TV)
991		ret = get_slave_funcs(encoder)->get_modes(encoder, connector);
992
993	if (nv_connector->type == DCB_CONNECTOR_LVDS ||
994	    nv_connector->type == DCB_CONNECTOR_LVDS_SPWG ||
995	    nv_connector->type == DCB_CONNECTOR_eDP)
996		ret += nouveau_connector_scaler_modes_add(connector);
997
998	return ret;
999}
1000
1001static unsigned
1002get_tmds_link_bandwidth(struct drm_connector *connector)
1003{
1004	struct nouveau_connector *nv_connector = nouveau_connector(connector);
1005	struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
1006	struct nouveau_drm *drm = nouveau_drm(connector->dev);
1007	struct dcb_output *dcb = nv_connector->detected_encoder->dcb;
1008	struct drm_display_info *info = NULL;
1009	unsigned duallink_scale =
1010		nouveau_duallink && nv_encoder->dcb->duallink_possible ? 2 : 1;
1011
1012	if (drm_detect_hdmi_monitor(nv_connector->edid)) {
1013		info = &nv_connector->base.display_info;
1014		duallink_scale = 1;
1015	}
1016
1017	if (info) {
1018		if (nouveau_hdmimhz > 0)
1019			return nouveau_hdmimhz * 1000;
1020		/* Note: these limits are conservative, some Fermi's
1021		 * can do 297 MHz. Unclear how this can be determined.
1022		 */
1023		if (drm->client.device.info.chipset >= 0x120) {
1024			const int max_tmds_clock =
1025				info->hdmi.scdc.scrambling.supported ?
1026				594000 : 340000;
1027			return info->max_tmds_clock ?
1028				min(info->max_tmds_clock, max_tmds_clock) :
1029				max_tmds_clock;
1030		}
1031		if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_KEPLER)
1032			return 297000;
1033		if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_FERMI)
1034			return 225000;
1035	}
1036
1037	if (dcb->location != DCB_LOC_ON_CHIP ||
1038	    drm->client.device.info.chipset >= 0x46)
1039		return 165000 * duallink_scale;
1040	else if (drm->client.device.info.chipset >= 0x40)
1041		return 155000 * duallink_scale;
1042	else if (drm->client.device.info.chipset >= 0x18)
1043		return 135000 * duallink_scale;
1044	else
1045		return 112000 * duallink_scale;
1046}
1047
1048static enum drm_mode_status
1049nouveau_connector_mode_valid(struct drm_connector *connector,
1050			     struct drm_display_mode *mode)
1051{
1052	struct nouveau_connector *nv_connector = nouveau_connector(connector);
1053	struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
1054	struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
1055	unsigned min_clock = 25000, max_clock = min_clock;
1056	unsigned clock = mode->clock;
1057
1058	switch (nv_encoder->dcb->type) {
1059	case DCB_OUTPUT_LVDS:
1060		if (nv_connector->native_mode &&
1061		    (mode->hdisplay > nv_connector->native_mode->hdisplay ||
1062		     mode->vdisplay > nv_connector->native_mode->vdisplay))
1063			return MODE_PANEL;
1064
1065		min_clock = 0;
1066		max_clock = 400000;
1067		break;
1068	case DCB_OUTPUT_TMDS:
1069		max_clock = get_tmds_link_bandwidth(connector);
1070		break;
1071	case DCB_OUTPUT_ANALOG:
1072		max_clock = nv_encoder->dcb->crtconf.maxfreq;
1073		if (!max_clock)
1074			max_clock = 350000;
1075		break;
1076	case DCB_OUTPUT_TV:
1077		return get_slave_funcs(encoder)->mode_valid(encoder, mode);
1078	case DCB_OUTPUT_DP:
1079		max_clock  = nv_encoder->dp.link_nr;
1080		max_clock *= nv_encoder->dp.link_bw;
1081		clock = clock * (connector->display_info.bpc * 3) / 10;
1082		break;
1083	default:
1084		BUG();
1085		return MODE_BAD;
1086	}
1087
1088	if ((mode->flags & DRM_MODE_FLAG_3D_MASK) == DRM_MODE_FLAG_3D_FRAME_PACKING)
1089		clock *= 2;
1090
1091	if (clock < min_clock)
1092		return MODE_CLOCK_LOW;
1093
1094	if (clock > max_clock)
1095		return MODE_CLOCK_HIGH;
1096
1097	return MODE_OK;
1098}
1099
1100static struct drm_encoder *
1101nouveau_connector_best_encoder(struct drm_connector *connector)
1102{
1103	struct nouveau_connector *nv_connector = nouveau_connector(connector);
1104
1105	if (nv_connector->detected_encoder)
1106		return to_drm_encoder(nv_connector->detected_encoder);
1107
1108	return NULL;
1109}
1110
1111static const struct drm_connector_helper_funcs
1112nouveau_connector_helper_funcs = {
1113	.get_modes = nouveau_connector_get_modes,
1114	.mode_valid = nouveau_connector_mode_valid,
1115	.best_encoder = nouveau_connector_best_encoder,
1116};
1117
1118static const struct drm_connector_funcs
1119nouveau_connector_funcs = {
1120	.dpms = drm_helper_connector_dpms,
1121	.reset = nouveau_conn_reset,
1122	.detect = nouveau_connector_detect,
1123	.force = nouveau_connector_force,
1124	.fill_modes = drm_helper_probe_single_connector_modes,
1125	.set_property = nouveau_connector_set_property,
1126	.destroy = nouveau_connector_destroy,
1127	.atomic_duplicate_state = nouveau_conn_atomic_duplicate_state,
1128	.atomic_destroy_state = nouveau_conn_atomic_destroy_state,
1129	.atomic_set_property = nouveau_conn_atomic_set_property,
1130	.atomic_get_property = nouveau_conn_atomic_get_property,
1131	.late_register = nouveau_connector_late_register,
1132	.early_unregister = nouveau_connector_early_unregister,
1133};
1134
1135static const struct drm_connector_funcs
1136nouveau_connector_funcs_lvds = {
1137	.dpms = drm_helper_connector_dpms,
1138	.reset = nouveau_conn_reset,
1139	.detect = nouveau_connector_detect_lvds,
1140	.force = nouveau_connector_force,
1141	.fill_modes = drm_helper_probe_single_connector_modes,
1142	.set_property = nouveau_connector_set_property,
1143	.destroy = nouveau_connector_destroy,
1144	.atomic_duplicate_state = nouveau_conn_atomic_duplicate_state,
1145	.atomic_destroy_state = nouveau_conn_atomic_destroy_state,
1146	.atomic_set_property = nouveau_conn_atomic_set_property,
1147	.atomic_get_property = nouveau_conn_atomic_get_property,
1148	.late_register = nouveau_connector_late_register,
1149	.early_unregister = nouveau_connector_early_unregister,
1150};
1151
1152static int
1153nouveau_connector_hotplug(struct nvif_notify *notify)
1154{
1155	struct nouveau_connector *nv_connector =
1156		container_of(notify, typeof(*nv_connector), hpd);
1157	struct drm_connector *connector = &nv_connector->base;
1158	struct nouveau_drm *drm = nouveau_drm(connector->dev);
1159	const struct nvif_notify_conn_rep_v0 *rep = notify->data;
1160	const char *name = connector->name;
1161	struct nouveau_encoder *nv_encoder;
1162	int ret;
1163	bool plugged = (rep->mask != NVIF_NOTIFY_CONN_V0_UNPLUG);
1164
1165	if (rep->mask & NVIF_NOTIFY_CONN_V0_IRQ) {
1166		NV_DEBUG(drm, "service %s\n", name);
1167		drm_dp_cec_irq(&nv_connector->aux);
1168		if ((nv_encoder = find_encoder(connector, DCB_OUTPUT_DP)))
1169			nv50_mstm_service(nv_encoder->dp.mstm);
1170
1171		return NVIF_NOTIFY_KEEP;
1172	}
1173
1174	ret = pm_runtime_get(drm->dev->dev);
1175	if (ret == 0) {
1176		/* We can't block here if there's a pending PM request
1177		 * running, as we'll deadlock nouveau_display_fini() when it
1178		 * calls nvif_put() on our nvif_notify struct. So, simply
1179		 * defer the hotplug event until the device finishes resuming
1180		 */
1181		NV_DEBUG(drm, "Deferring HPD on %s until runtime resume\n",
1182			 name);
1183		schedule_work(&drm->hpd_work);
1184
1185		pm_runtime_put_noidle(drm->dev->dev);
1186		return NVIF_NOTIFY_KEEP;
1187	} else if (ret != 1 && ret != -EACCES) {
1188		NV_WARN(drm, "HPD on %s dropped due to RPM failure: %d\n",
1189			name, ret);
1190		return NVIF_NOTIFY_DROP;
1191	}
1192
1193	if (!plugged)
1194		drm_dp_cec_unset_edid(&nv_connector->aux);
1195	NV_DEBUG(drm, "%splugged %s\n", plugged ? "" : "un", name);
1196	if ((nv_encoder = find_encoder(connector, DCB_OUTPUT_DP))) {
1197		if (!plugged)
1198			nv50_mstm_remove(nv_encoder->dp.mstm);
1199	}
1200
1201	drm_helper_hpd_irq_event(connector->dev);
1202
1203	pm_runtime_mark_last_busy(drm->dev->dev);
1204	pm_runtime_put_autosuspend(drm->dev->dev);
1205	return NVIF_NOTIFY_KEEP;
1206}
1207
1208static ssize_t
1209nouveau_connector_aux_xfer(struct drm_dp_aux *obj, struct drm_dp_aux_msg *msg)
1210{
1211	struct nouveau_connector *nv_connector =
1212		container_of(obj, typeof(*nv_connector), aux);
1213	struct nouveau_encoder *nv_encoder;
1214	struct nvkm_i2c_aux *aux;
1215	u8 size = msg->size;
1216	int ret;
1217
1218	nv_encoder = find_encoder(&nv_connector->base, DCB_OUTPUT_DP);
1219	if (!nv_encoder || !(aux = nv_encoder->aux))
1220		return -ENODEV;
1221	if (WARN_ON(msg->size > 16))
1222		return -E2BIG;
1223
1224	ret = nvkm_i2c_aux_acquire(aux);
1225	if (ret)
1226		return ret;
1227
1228	ret = nvkm_i2c_aux_xfer(aux, false, msg->request, msg->address,
1229				msg->buffer, &size);
1230	nvkm_i2c_aux_release(aux);
1231	if (ret >= 0) {
1232		msg->reply = ret;
1233		return size;
1234	}
1235
1236	return ret;
1237}
1238
1239static int
1240drm_conntype_from_dcb(enum dcb_connector_type dcb)
1241{
1242	switch (dcb) {
1243	case DCB_CONNECTOR_VGA      : return DRM_MODE_CONNECTOR_VGA;
1244	case DCB_CONNECTOR_TV_0     :
1245	case DCB_CONNECTOR_TV_1     :
1246	case DCB_CONNECTOR_TV_3     : return DRM_MODE_CONNECTOR_TV;
1247	case DCB_CONNECTOR_DMS59_0  :
1248	case DCB_CONNECTOR_DMS59_1  :
1249	case DCB_CONNECTOR_DVI_I    : return DRM_MODE_CONNECTOR_DVII;
1250	case DCB_CONNECTOR_DVI_D    : return DRM_MODE_CONNECTOR_DVID;
1251	case DCB_CONNECTOR_LVDS     :
1252	case DCB_CONNECTOR_LVDS_SPWG: return DRM_MODE_CONNECTOR_LVDS;
1253	case DCB_CONNECTOR_DMS59_DP0:
1254	case DCB_CONNECTOR_DMS59_DP1:
1255	case DCB_CONNECTOR_DP       :
1256	case DCB_CONNECTOR_USB_C    : return DRM_MODE_CONNECTOR_DisplayPort;
1257	case DCB_CONNECTOR_eDP      : return DRM_MODE_CONNECTOR_eDP;
1258	case DCB_CONNECTOR_HDMI_0   :
1259	case DCB_CONNECTOR_HDMI_1   :
1260	case DCB_CONNECTOR_HDMI_C   : return DRM_MODE_CONNECTOR_HDMIA;
1261	case DCB_CONNECTOR_WFD	    : return DRM_MODE_CONNECTOR_VIRTUAL;
1262	default:
1263		break;
1264	}
1265
1266	return DRM_MODE_CONNECTOR_Unknown;
1267}
1268
1269struct drm_connector *
1270nouveau_connector_create(struct drm_device *dev,
1271			 const struct dcb_output *dcbe)
1272{
1273	const struct drm_connector_funcs *funcs = &nouveau_connector_funcs;
1274	struct nouveau_drm *drm = nouveau_drm(dev);
1275	struct nouveau_display *disp = nouveau_display(dev);
1276	struct nouveau_connector *nv_connector = NULL;
1277	struct drm_connector *connector;
1278	struct drm_connector_list_iter conn_iter;
1279	char aux_name[48] = {0};
1280	int index = dcbe->connector;
1281	int type, ret = 0;
1282	bool dummy;
1283
1284	drm_connector_list_iter_begin(dev, &conn_iter);
1285	nouveau_for_each_non_mst_connector_iter(connector, &conn_iter) {
1286		nv_connector = nouveau_connector(connector);
1287		if (nv_connector->index == index) {
1288			drm_connector_list_iter_end(&conn_iter);
1289			return connector;
1290		}
1291	}
1292	drm_connector_list_iter_end(&conn_iter);
1293
1294	nv_connector = kzalloc(sizeof(*nv_connector), GFP_KERNEL);
1295	if (!nv_connector)
1296		return ERR_PTR(-ENOMEM);
1297
1298	connector = &nv_connector->base;
1299	nv_connector->index = index;
1300
1301	/* attempt to parse vbios connector type and hotplug gpio */
1302	nv_connector->dcb = olddcb_conn(dev, index);
1303	if (nv_connector->dcb) {
1304		u32 entry = ROM16(nv_connector->dcb[0]);
1305		if (olddcb_conntab(dev)[3] >= 4)
1306			entry |= (u32)ROM16(nv_connector->dcb[2]) << 16;
1307
1308		nv_connector->type = nv_connector->dcb[0];
1309		if (drm_conntype_from_dcb(nv_connector->type) ==
1310					  DRM_MODE_CONNECTOR_Unknown) {
1311			NV_WARN(drm, "unknown connector type %02x\n",
1312				nv_connector->type);
1313			nv_connector->type = DCB_CONNECTOR_NONE;
1314		}
1315
1316		/* Gigabyte NX85T */
1317		if (nv_match_device(dev, 0x0421, 0x1458, 0x344c)) {
1318			if (nv_connector->type == DCB_CONNECTOR_HDMI_1)
1319				nv_connector->type = DCB_CONNECTOR_DVI_I;
1320		}
1321
1322		/* Gigabyte GV-NX86T512H */
1323		if (nv_match_device(dev, 0x0402, 0x1458, 0x3455)) {
1324			if (nv_connector->type == DCB_CONNECTOR_HDMI_1)
1325				nv_connector->type = DCB_CONNECTOR_DVI_I;
1326		}
1327	} else {
1328		nv_connector->type = DCB_CONNECTOR_NONE;
1329	}
1330
1331	/* no vbios data, or an unknown dcb connector type - attempt to
1332	 * figure out something suitable ourselves
1333	 */
1334	if (nv_connector->type == DCB_CONNECTOR_NONE) {
1335		struct dcb_table *dcbt = &drm->vbios.dcb;
1336		u32 encoders = 0;
1337		int i;
1338
1339		for (i = 0; i < dcbt->entries; i++) {
1340			if (dcbt->entry[i].connector == nv_connector->index)
1341				encoders |= (1 << dcbt->entry[i].type);
1342		}
1343
1344		if (encoders & (1 << DCB_OUTPUT_DP)) {
1345			if (encoders & (1 << DCB_OUTPUT_TMDS))
1346				nv_connector->type = DCB_CONNECTOR_DP;
1347			else
1348				nv_connector->type = DCB_CONNECTOR_eDP;
1349		} else
1350		if (encoders & (1 << DCB_OUTPUT_TMDS)) {
1351			if (encoders & (1 << DCB_OUTPUT_ANALOG))
1352				nv_connector->type = DCB_CONNECTOR_DVI_I;
1353			else
1354				nv_connector->type = DCB_CONNECTOR_DVI_D;
1355		} else
1356		if (encoders & (1 << DCB_OUTPUT_ANALOG)) {
1357			nv_connector->type = DCB_CONNECTOR_VGA;
1358		} else
1359		if (encoders & (1 << DCB_OUTPUT_LVDS)) {
1360			nv_connector->type = DCB_CONNECTOR_LVDS;
1361		} else
1362		if (encoders & (1 << DCB_OUTPUT_TV)) {
1363			nv_connector->type = DCB_CONNECTOR_TV_0;
1364		}
1365	}
1366
1367	switch ((type = drm_conntype_from_dcb(nv_connector->type))) {
1368	case DRM_MODE_CONNECTOR_LVDS:
1369		ret = nouveau_bios_parse_lvds_table(dev, 0, &dummy, &dummy);
1370		if (ret) {
1371			NV_ERROR(drm, "Error parsing LVDS table, disabling\n");
1372			kfree(nv_connector);
1373			return ERR_PTR(ret);
1374		}
1375
1376		funcs = &nouveau_connector_funcs_lvds;
1377		break;
1378	case DRM_MODE_CONNECTOR_DisplayPort:
1379	case DRM_MODE_CONNECTOR_eDP:
1380		nv_connector->aux.dev = connector->kdev;
1381		nv_connector->aux.transfer = nouveau_connector_aux_xfer;
1382		snprintf(aux_name, sizeof(aux_name), "sor-%04x-%04x",
1383			 dcbe->hasht, dcbe->hashm);
1384		nv_connector->aux.name = kstrdup(aux_name, GFP_KERNEL);
1385		ret = drm_dp_aux_register(&nv_connector->aux);
1386		if (ret) {
1387			NV_ERROR(drm, "failed to register aux channel\n");
1388			kfree(nv_connector);
1389			return ERR_PTR(ret);
1390		}
1391		funcs = &nouveau_connector_funcs;
1392		break;
1393	default:
1394		funcs = &nouveau_connector_funcs;
1395		break;
1396	}
1397
1398	/* HDMI 3D support */
1399	if ((disp->disp.object.oclass >= G82_DISP)
1400	    && ((type == DRM_MODE_CONNECTOR_DisplayPort)
1401		|| (type == DRM_MODE_CONNECTOR_eDP)
1402		|| (type == DRM_MODE_CONNECTOR_HDMIA)))
1403		connector->stereo_allowed = true;
1404
1405	/* defaults, will get overridden in detect() */
1406	connector->interlace_allowed = false;
1407	connector->doublescan_allowed = false;
1408
1409	drm_connector_init(dev, connector, funcs, type);
1410	drm_connector_helper_add(connector, &nouveau_connector_helper_funcs);
1411
1412	connector->funcs->reset(connector);
1413	nouveau_conn_attach_properties(connector);
1414
1415	/* Default scaling mode */
1416	switch (nv_connector->type) {
1417	case DCB_CONNECTOR_LVDS:
1418	case DCB_CONNECTOR_LVDS_SPWG:
1419	case DCB_CONNECTOR_eDP:
1420		/* see note in nouveau_connector_set_property() */
1421		if (disp->disp.object.oclass < NV50_DISP) {
1422			nv_connector->scaling_mode = DRM_MODE_SCALE_FULLSCREEN;
1423			break;
1424		}
1425		nv_connector->scaling_mode = DRM_MODE_SCALE_NONE;
1426		break;
1427	default:
1428		nv_connector->scaling_mode = DRM_MODE_SCALE_NONE;
1429		break;
1430	}
1431
1432	/* dithering properties */
1433	switch (nv_connector->type) {
1434	case DCB_CONNECTOR_TV_0:
1435	case DCB_CONNECTOR_TV_1:
1436	case DCB_CONNECTOR_TV_3:
1437	case DCB_CONNECTOR_VGA:
1438		break;
1439	default:
1440		nv_connector->dithering_mode = DITHERING_MODE_AUTO;
1441		break;
1442	}
1443
1444	switch (type) {
1445	case DRM_MODE_CONNECTOR_DisplayPort:
1446	case DRM_MODE_CONNECTOR_eDP:
1447		drm_dp_cec_register_connector(&nv_connector->aux, connector);
1448		break;
1449	}
1450
1451	ret = nvif_notify_init(&disp->disp.object, nouveau_connector_hotplug,
1452			       true, NV04_DISP_NTFY_CONN,
1453			       &(struct nvif_notify_conn_req_v0) {
1454				.mask = NVIF_NOTIFY_CONN_V0_ANY,
1455				.conn = index,
1456			       },
1457			       sizeof(struct nvif_notify_conn_req_v0),
1458			       sizeof(struct nvif_notify_conn_rep_v0),
1459			       &nv_connector->hpd);
1460	if (ret)
1461		connector->polled = DRM_CONNECTOR_POLL_CONNECT;
1462	else
1463		connector->polled = DRM_CONNECTOR_POLL_HPD;
1464
1465	drm_connector_register(connector);
1466	return connector;
1467}
1468