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