xref: /linux/drivers/gpu/drm/radeon/radeon_connectors.c (revision ec8c17e5ecb4a5a74069687ccb6d2cfe1851302e)
1 /*
2  * Copyright 2007-8 Advanced Micro Devices, Inc.
3  * Copyright 2008 Red Hat Inc.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21  * OTHER DEALINGS IN THE SOFTWARE.
22  *
23  * Authors: Dave Airlie
24  *          Alex Deucher
25  */
26 
27 #include <drm/display/drm_dp_mst_helper.h>
28 #include <drm/drm_edid.h>
29 #include <drm/drm_crtc_helper.h>
30 #include <drm/drm_modeset_helper_vtables.h>
31 #include <drm/drm_probe_helper.h>
32 #include <drm/radeon_drm.h>
33 #include "radeon.h"
34 #include "radeon_audio.h"
35 #include "atom.h"
36 
37 #include <linux/pm_runtime.h>
38 #include <linux/vga_switcheroo.h>
39 
radeon_connector_hotplug(struct drm_connector * connector)40 void radeon_connector_hotplug(struct drm_connector *connector)
41 {
42 	struct drm_device *dev = connector->dev;
43 	struct radeon_device *rdev = dev->dev_private;
44 	struct radeon_connector *radeon_connector = to_radeon_connector(connector);
45 
46 	/* bail if the connector does not have hpd pin, e.g.,
47 	 * VGA, TV, etc.
48 	 */
49 	if (radeon_connector->hpd.hpd == RADEON_HPD_NONE)
50 		return;
51 
52 	radeon_hpd_set_polarity(rdev, radeon_connector->hpd.hpd);
53 
54 	/* if the connector is already off, don't turn it back on */
55 	/* FIXME: This access isn't protected by any locks. */
56 	if (connector->dpms != DRM_MODE_DPMS_ON)
57 		return;
58 
59 	/* just deal with DP (not eDP) here. */
60 	if (connector->connector_type == DRM_MODE_CONNECTOR_DisplayPort) {
61 		struct radeon_connector_atom_dig *dig_connector =
62 			radeon_connector->con_priv;
63 
64 		/* if existing sink type was not DP no need to retrain */
65 		if (dig_connector->dp_sink_type != CONNECTOR_OBJECT_ID_DISPLAYPORT)
66 			return;
67 
68 		/* first get sink type as it may be reset after (un)plug */
69 		dig_connector->dp_sink_type = radeon_dp_getsinktype(radeon_connector);
70 		/* don't do anything if sink is not display port, i.e.,
71 		 * passive dp->(dvi|hdmi) adaptor
72 		 */
73 		if (dig_connector->dp_sink_type == CONNECTOR_OBJECT_ID_DISPLAYPORT &&
74 		    radeon_hpd_sense(rdev, radeon_connector->hpd.hpd) &&
75 		    radeon_dp_needs_link_train(radeon_connector)) {
76 			/* Don't start link training before we have the DPCD */
77 			if (!radeon_dp_getdpcd(radeon_connector))
78 				return;
79 
80 			/* Turn the connector off and back on immediately, which
81 			 * will trigger link training
82 			 */
83 			drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
84 			drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
85 		}
86 	}
87 }
88 
radeon_property_change_mode(struct drm_encoder * encoder)89 static void radeon_property_change_mode(struct drm_encoder *encoder)
90 {
91 	struct drm_crtc *crtc = encoder->crtc;
92 
93 	if (crtc && crtc->enabled) {
94 		drm_crtc_helper_set_mode(crtc, &crtc->mode,
95 					 crtc->x, crtc->y, crtc->primary->fb);
96 	}
97 }
98 
radeon_get_monitor_bpc(struct drm_connector * connector)99 int radeon_get_monitor_bpc(struct drm_connector *connector)
100 {
101 	struct drm_device *dev = connector->dev;
102 	struct radeon_device *rdev = dev->dev_private;
103 	struct radeon_connector *radeon_connector = to_radeon_connector(connector);
104 	struct radeon_connector_atom_dig *dig_connector;
105 	int bpc = 8;
106 	int mode_clock, max_tmds_clock;
107 
108 	switch (connector->connector_type) {
109 	case DRM_MODE_CONNECTOR_DVII:
110 	case DRM_MODE_CONNECTOR_HDMIB:
111 		if (radeon_connector->use_digital) {
112 			if (connector->display_info.is_hdmi) {
113 				if (connector->display_info.bpc)
114 					bpc = connector->display_info.bpc;
115 			}
116 		}
117 		break;
118 	case DRM_MODE_CONNECTOR_DVID:
119 	case DRM_MODE_CONNECTOR_HDMIA:
120 		if (connector->display_info.is_hdmi) {
121 			if (connector->display_info.bpc)
122 				bpc = connector->display_info.bpc;
123 		}
124 		break;
125 	case DRM_MODE_CONNECTOR_DisplayPort:
126 		dig_connector = radeon_connector->con_priv;
127 		if ((dig_connector->dp_sink_type == CONNECTOR_OBJECT_ID_DISPLAYPORT) ||
128 		    (dig_connector->dp_sink_type == CONNECTOR_OBJECT_ID_eDP) ||
129 		    connector->display_info.is_hdmi) {
130 			if (connector->display_info.bpc)
131 				bpc = connector->display_info.bpc;
132 		}
133 		break;
134 	case DRM_MODE_CONNECTOR_eDP:
135 	case DRM_MODE_CONNECTOR_LVDS:
136 		if (connector->display_info.bpc)
137 			bpc = connector->display_info.bpc;
138 		else if (ASIC_IS_DCE41(rdev) || ASIC_IS_DCE5(rdev)) {
139 			const struct drm_connector_helper_funcs *connector_funcs =
140 				connector->helper_private;
141 			struct drm_encoder *encoder = connector_funcs->best_encoder(connector);
142 			struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
143 			struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
144 
145 			if (dig->lcd_misc & ATOM_PANEL_MISC_V13_6BIT_PER_COLOR)
146 				bpc = 6;
147 			else if (dig->lcd_misc & ATOM_PANEL_MISC_V13_8BIT_PER_COLOR)
148 				bpc = 8;
149 		}
150 		break;
151 	}
152 
153 	if (connector->display_info.is_hdmi) {
154 		/* hdmi deep color only implemented on DCE4+ */
155 		if ((bpc > 8) && !ASIC_IS_DCE4(rdev)) {
156 			DRM_DEBUG("%s: HDMI deep color %d bpc unsupported. Using 8 bpc.\n",
157 					  connector->name, bpc);
158 			bpc = 8;
159 		}
160 
161 		/*
162 		 * Pre DCE-8 hw can't handle > 12 bpc, and more than 12 bpc doesn't make
163 		 * much sense without support for > 12 bpc framebuffers. RGB 4:4:4 at
164 		 * 12 bpc is always supported on hdmi deep color sinks, as this is
165 		 * required by the HDMI-1.3 spec. Clamp to a safe 12 bpc maximum.
166 		 */
167 		if (bpc > 12) {
168 			DRM_DEBUG("%s: HDMI deep color %d bpc unsupported. Using 12 bpc.\n",
169 					  connector->name, bpc);
170 			bpc = 12;
171 		}
172 
173 		/* Any defined maximum tmds clock limit we must not exceed? */
174 		if (connector->display_info.max_tmds_clock > 0) {
175 			/* mode_clock is clock in kHz for mode to be modeset on this connector */
176 			mode_clock = radeon_connector->pixelclock_for_modeset;
177 
178 			/* Maximum allowable input clock in kHz */
179 			max_tmds_clock = connector->display_info.max_tmds_clock;
180 
181 			DRM_DEBUG("%s: hdmi mode dotclock %d kHz, max tmds input clock %d kHz.\n",
182 					  connector->name, mode_clock, max_tmds_clock);
183 
184 			/* Check if bpc is within clock limit. Try to degrade gracefully otherwise */
185 			if ((bpc == 12) && (mode_clock * 3/2 > max_tmds_clock)) {
186 				if ((connector->display_info.edid_hdmi_rgb444_dc_modes & DRM_EDID_HDMI_DC_30) &&
187 					(mode_clock * 5/4 <= max_tmds_clock))
188 					bpc = 10;
189 				else
190 					bpc = 8;
191 
192 				DRM_DEBUG("%s: HDMI deep color 12 bpc exceeds max tmds clock. Using %d bpc.\n",
193 						  connector->name, bpc);
194 			}
195 
196 			if ((bpc == 10) && (mode_clock * 5/4 > max_tmds_clock)) {
197 				bpc = 8;
198 				DRM_DEBUG("%s: HDMI deep color 10 bpc exceeds max tmds clock. Using %d bpc.\n",
199 						  connector->name, bpc);
200 			}
201 		} else if (bpc > 8) {
202 			/* max_tmds_clock missing, but hdmi spec mandates it for deep color. */
203 			DRM_DEBUG("%s: Required max tmds clock for HDMI deep color missing. Using 8 bpc.\n",
204 					  connector->name);
205 			bpc = 8;
206 		}
207 	}
208 
209 	if ((radeon_deep_color == 0) && (bpc > 8)) {
210 		DRM_DEBUG("%s: Deep color disabled. Set radeon module param deep_color=1 to enable.\n",
211 				  connector->name);
212 		bpc = 8;
213 	}
214 
215 	DRM_DEBUG("%s: Display bpc=%d, returned bpc=%d\n",
216 			  connector->name, connector->display_info.bpc, bpc);
217 
218 	return bpc;
219 }
220 
221 static void
radeon_connector_update_scratch_regs(struct drm_connector * connector,enum drm_connector_status status)222 radeon_connector_update_scratch_regs(struct drm_connector *connector, enum drm_connector_status status)
223 {
224 	struct drm_device *dev = connector->dev;
225 	struct radeon_device *rdev = dev->dev_private;
226 	struct drm_encoder *best_encoder;
227 	struct drm_encoder *encoder;
228 	const struct drm_connector_helper_funcs *connector_funcs = connector->helper_private;
229 	bool connected;
230 
231 	best_encoder = connector_funcs->best_encoder(connector);
232 
233 	drm_connector_for_each_possible_encoder(connector, encoder) {
234 		if ((encoder == best_encoder) && (status == connector_status_connected))
235 			connected = true;
236 		else
237 			connected = false;
238 
239 		if (rdev->is_atom_bios)
240 			radeon_atombios_connected_scratch_regs(connector, encoder, connected);
241 		else
242 			radeon_combios_connected_scratch_regs(connector, encoder, connected);
243 	}
244 }
245 
radeon_find_encoder(struct drm_connector * connector,int encoder_type)246 static struct drm_encoder *radeon_find_encoder(struct drm_connector *connector, int encoder_type)
247 {
248 	struct drm_encoder *encoder;
249 
250 	drm_connector_for_each_possible_encoder(connector, encoder) {
251 		if (encoder->encoder_type == encoder_type)
252 			return encoder;
253 	}
254 
255 	return NULL;
256 }
257 
radeon_connector_get_edid(struct drm_connector * connector)258 static void radeon_connector_get_edid(struct drm_connector *connector)
259 {
260 	struct drm_device *dev = connector->dev;
261 	struct radeon_device *rdev = dev->dev_private;
262 	struct radeon_connector *radeon_connector = to_radeon_connector(connector);
263 
264 	if (radeon_connector->edid)
265 		return;
266 
267 	/* on hw with routers, select right port */
268 	if (radeon_connector->router.ddc_valid)
269 		radeon_router_select_ddc_port(radeon_connector);
270 
271 	if ((radeon_connector_encoder_get_dp_bridge_encoder_id(connector) !=
272 	     ENCODER_OBJECT_ID_NONE) &&
273 	    radeon_connector->ddc_bus->has_aux) {
274 		radeon_connector->edid = drm_get_edid(connector,
275 						      &radeon_connector->ddc_bus->aux.ddc);
276 	} else if ((connector->connector_type == DRM_MODE_CONNECTOR_DisplayPort) ||
277 		   (connector->connector_type == DRM_MODE_CONNECTOR_eDP)) {
278 		struct radeon_connector_atom_dig *dig = radeon_connector->con_priv;
279 
280 		if ((dig->dp_sink_type == CONNECTOR_OBJECT_ID_DISPLAYPORT ||
281 		     dig->dp_sink_type == CONNECTOR_OBJECT_ID_eDP) &&
282 		    radeon_connector->ddc_bus->has_aux)
283 			radeon_connector->edid = drm_get_edid(&radeon_connector->base,
284 							      &radeon_connector->ddc_bus->aux.ddc);
285 		else if (radeon_connector->ddc_bus)
286 			radeon_connector->edid = drm_get_edid(&radeon_connector->base,
287 							      &radeon_connector->ddc_bus->adapter);
288 	} else if (vga_switcheroo_handler_flags() & VGA_SWITCHEROO_CAN_SWITCH_DDC &&
289 		   connector->connector_type == DRM_MODE_CONNECTOR_LVDS &&
290 		   radeon_connector->ddc_bus) {
291 		radeon_connector->edid = drm_get_edid_switcheroo(&radeon_connector->base,
292 								 &radeon_connector->ddc_bus->adapter);
293 	} else if (radeon_connector->ddc_bus) {
294 		radeon_connector->edid = drm_get_edid(&radeon_connector->base,
295 						      &radeon_connector->ddc_bus->adapter);
296 	}
297 
298 	if (!radeon_connector->edid) {
299 		/* don't fetch the edid from the vbios if ddc fails and runpm is
300 		 * enabled so we report disconnected.
301 		 */
302 		if ((rdev->flags & RADEON_IS_PX) && (radeon_runtime_pm != 0))
303 			return;
304 
305 		if (rdev->is_atom_bios) {
306 			/* some laptops provide a hardcoded edid in rom for LCDs */
307 			if (((connector->connector_type == DRM_MODE_CONNECTOR_LVDS) ||
308 			     (connector->connector_type == DRM_MODE_CONNECTOR_eDP)))
309 				radeon_connector->edid = radeon_bios_get_hardcoded_edid(rdev);
310 		} else {
311 			/* some servers provide a hardcoded edid in rom for KVMs */
312 			radeon_connector->edid = radeon_bios_get_hardcoded_edid(rdev);
313 		}
314 	}
315 }
316 
radeon_connector_free_edid(struct drm_connector * connector)317 static void radeon_connector_free_edid(struct drm_connector *connector)
318 {
319 	struct radeon_connector *radeon_connector = to_radeon_connector(connector);
320 
321 	kfree(radeon_connector->edid);
322 	radeon_connector->edid = NULL;
323 }
324 
radeon_ddc_get_modes(struct drm_connector * connector)325 static int radeon_ddc_get_modes(struct drm_connector *connector)
326 {
327 	struct radeon_connector *radeon_connector = to_radeon_connector(connector);
328 	int ret;
329 
330 	if (radeon_connector->edid) {
331 		drm_connector_update_edid_property(connector, radeon_connector->edid);
332 		ret = drm_add_edid_modes(connector, radeon_connector->edid);
333 		return ret;
334 	}
335 	drm_connector_update_edid_property(connector, NULL);
336 	return 0;
337 }
338 
radeon_best_single_encoder(struct drm_connector * connector)339 static struct drm_encoder *radeon_best_single_encoder(struct drm_connector *connector)
340 {
341 	struct drm_encoder *encoder;
342 
343 	/* pick the first one */
344 	drm_connector_for_each_possible_encoder(connector, encoder)
345 		return encoder;
346 
347 	return NULL;
348 }
349 
radeon_get_native_mode(struct drm_connector * connector)350 static void radeon_get_native_mode(struct drm_connector *connector)
351 {
352 	struct drm_encoder *encoder = radeon_best_single_encoder(connector);
353 	struct radeon_encoder *radeon_encoder;
354 
355 	if (encoder == NULL)
356 		return;
357 
358 	radeon_encoder = to_radeon_encoder(encoder);
359 
360 	if (!list_empty(&connector->probed_modes)) {
361 		struct drm_display_mode *preferred_mode =
362 			list_first_entry(&connector->probed_modes,
363 					 struct drm_display_mode, head);
364 
365 		radeon_encoder->native_mode = *preferred_mode;
366 	} else {
367 		radeon_encoder->native_mode.clock = 0;
368 	}
369 }
370 
371 /*
372  * radeon_connector_analog_encoder_conflict_solve
373  * - search for other connectors sharing this encoder
374  *   if priority is true, then set them disconnected if this is connected
375  *   if priority is false, set us disconnected if they are connected
376  */
377 static enum drm_connector_status
radeon_connector_analog_encoder_conflict_solve(struct drm_connector * connector,struct drm_encoder * encoder,enum drm_connector_status current_status,bool priority)378 radeon_connector_analog_encoder_conflict_solve(struct drm_connector *connector,
379 					       struct drm_encoder *encoder,
380 					       enum drm_connector_status current_status,
381 					       bool priority)
382 {
383 	struct drm_device *dev = connector->dev;
384 	struct drm_connector *conflict;
385 	struct radeon_connector *radeon_conflict;
386 
387 	list_for_each_entry(conflict, &dev->mode_config.connector_list, head) {
388 		struct drm_encoder *enc;
389 
390 		if (conflict == connector)
391 			continue;
392 
393 		radeon_conflict = to_radeon_connector(conflict);
394 
395 		drm_connector_for_each_possible_encoder(conflict, enc) {
396 			/* if the IDs match */
397 			if (enc == encoder) {
398 				if (conflict->status != connector_status_connected)
399 					continue;
400 
401 				if (radeon_conflict->use_digital)
402 					continue;
403 
404 				if (priority) {
405 					DRM_DEBUG_KMS("1: conflicting encoders switching off %s\n",
406 						      conflict->name);
407 					DRM_DEBUG_KMS("in favor of %s\n",
408 						      connector->name);
409 					conflict->status = connector_status_disconnected;
410 					radeon_connector_update_scratch_regs(conflict, connector_status_disconnected);
411 				} else {
412 					DRM_DEBUG_KMS("2: conflicting encoders switching off %s\n",
413 						      connector->name);
414 					DRM_DEBUG_KMS("in favor of %s\n",
415 						      conflict->name);
416 					current_status = connector_status_disconnected;
417 				}
418 				break;
419 			}
420 		}
421 	}
422 	return current_status;
423 
424 }
425 
radeon_fp_native_mode(struct drm_encoder * encoder)426 static struct drm_display_mode *radeon_fp_native_mode(struct drm_encoder *encoder)
427 {
428 	struct drm_device *dev = encoder->dev;
429 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
430 	struct drm_display_mode *mode = NULL;
431 	struct drm_display_mode *native_mode = &radeon_encoder->native_mode;
432 
433 	if (native_mode->hdisplay != 0 &&
434 	    native_mode->vdisplay != 0 &&
435 	    native_mode->clock != 0) {
436 		mode = drm_mode_duplicate(dev, native_mode);
437 		if (!mode)
438 			return NULL;
439 		mode->type = DRM_MODE_TYPE_PREFERRED | DRM_MODE_TYPE_DRIVER;
440 		drm_mode_set_name(mode);
441 
442 		DRM_DEBUG_KMS("Adding native panel mode %s\n", mode->name);
443 	} else if (native_mode->hdisplay != 0 &&
444 		   native_mode->vdisplay != 0) {
445 		/* mac laptops without an edid */
446 		/* Note that this is not necessarily the exact panel mode,
447 		 * but an approximation based on the cvt formula.  For these
448 		 * systems we should ideally read the mode info out of the
449 		 * registers or add a mode table, but this works and is much
450 		 * simpler.
451 		 */
452 		mode = drm_cvt_mode(dev, native_mode->hdisplay, native_mode->vdisplay, 60, true, false, false);
453 		if (!mode)
454 			return NULL;
455 		mode->type = DRM_MODE_TYPE_PREFERRED | DRM_MODE_TYPE_DRIVER;
456 		DRM_DEBUG_KMS("Adding cvt approximation of native panel mode %s\n", mode->name);
457 	}
458 	return mode;
459 }
460 
radeon_add_common_modes(struct drm_encoder * encoder,struct drm_connector * connector)461 static void radeon_add_common_modes(struct drm_encoder *encoder, struct drm_connector *connector)
462 {
463 	struct drm_device *dev = encoder->dev;
464 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
465 	struct drm_display_mode *mode = NULL;
466 	struct drm_display_mode *native_mode = &radeon_encoder->native_mode;
467 	int i;
468 	struct mode_size {
469 		int w;
470 		int h;
471 	} common_modes[17] = {
472 		{ 640,  480},
473 		{ 720,  480},
474 		{ 800,  600},
475 		{ 848,  480},
476 		{1024,  768},
477 		{1152,  768},
478 		{1280,  720},
479 		{1280,  800},
480 		{1280,  854},
481 		{1280,  960},
482 		{1280, 1024},
483 		{1440,  900},
484 		{1400, 1050},
485 		{1680, 1050},
486 		{1600, 1200},
487 		{1920, 1080},
488 		{1920, 1200}
489 	};
490 
491 	for (i = 0; i < 17; i++) {
492 		if (radeon_encoder->devices & (ATOM_DEVICE_TV_SUPPORT)) {
493 			if (common_modes[i].w > 1024 ||
494 			    common_modes[i].h > 768)
495 				continue;
496 		}
497 		if (radeon_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT)) {
498 			if (common_modes[i].w > native_mode->hdisplay ||
499 			    common_modes[i].h > native_mode->vdisplay ||
500 			    (common_modes[i].w == native_mode->hdisplay &&
501 			     common_modes[i].h == native_mode->vdisplay))
502 				continue;
503 		}
504 		if (common_modes[i].w < 320 || common_modes[i].h < 200)
505 			continue;
506 
507 		mode = drm_cvt_mode(dev, common_modes[i].w, common_modes[i].h, 60, false, false, false);
508 		if (!mode)
509 			continue;
510 
511 		drm_mode_probed_add(connector, mode);
512 	}
513 }
514 
radeon_connector_set_property(struct drm_connector * connector,struct drm_property * property,uint64_t val)515 static int radeon_connector_set_property(struct drm_connector *connector, struct drm_property *property,
516 				  uint64_t val)
517 {
518 	struct drm_device *dev = connector->dev;
519 	struct radeon_device *rdev = dev->dev_private;
520 	struct drm_encoder *encoder;
521 	struct radeon_encoder *radeon_encoder;
522 
523 	if (property == rdev->mode_info.coherent_mode_property) {
524 		struct radeon_encoder_atom_dig *dig;
525 		bool new_coherent_mode;
526 
527 		/* need to find digital encoder on connector */
528 		encoder = radeon_find_encoder(connector, DRM_MODE_ENCODER_TMDS);
529 		if (!encoder)
530 			return 0;
531 
532 		radeon_encoder = to_radeon_encoder(encoder);
533 
534 		if (!radeon_encoder->enc_priv)
535 			return 0;
536 
537 		dig = radeon_encoder->enc_priv;
538 		new_coherent_mode = val ? true : false;
539 		if (dig->coherent_mode != new_coherent_mode) {
540 			dig->coherent_mode = new_coherent_mode;
541 			radeon_property_change_mode(&radeon_encoder->base);
542 		}
543 	}
544 
545 	if (property == rdev->mode_info.audio_property) {
546 		struct radeon_connector *radeon_connector = to_radeon_connector(connector);
547 		/* need to find digital encoder on connector */
548 		encoder = radeon_find_encoder(connector, DRM_MODE_ENCODER_TMDS);
549 		if (!encoder)
550 			return 0;
551 
552 		radeon_encoder = to_radeon_encoder(encoder);
553 
554 		if (radeon_connector->audio != val) {
555 			radeon_connector->audio = val;
556 			radeon_property_change_mode(&radeon_encoder->base);
557 		}
558 	}
559 
560 	if (property == rdev->mode_info.dither_property) {
561 		struct radeon_connector *radeon_connector = to_radeon_connector(connector);
562 		/* need to find digital encoder on connector */
563 		encoder = radeon_find_encoder(connector, DRM_MODE_ENCODER_TMDS);
564 		if (!encoder)
565 			return 0;
566 
567 		radeon_encoder = to_radeon_encoder(encoder);
568 
569 		if (radeon_connector->dither != val) {
570 			radeon_connector->dither = val;
571 			radeon_property_change_mode(&radeon_encoder->base);
572 		}
573 	}
574 
575 	if (property == rdev->mode_info.underscan_property) {
576 		/* need to find digital encoder on connector */
577 		encoder = radeon_find_encoder(connector, DRM_MODE_ENCODER_TMDS);
578 		if (!encoder)
579 			return 0;
580 
581 		radeon_encoder = to_radeon_encoder(encoder);
582 
583 		if (radeon_encoder->underscan_type != val) {
584 			radeon_encoder->underscan_type = val;
585 			radeon_property_change_mode(&radeon_encoder->base);
586 		}
587 	}
588 
589 	if (property == rdev->mode_info.underscan_hborder_property) {
590 		/* need to find digital encoder on connector */
591 		encoder = radeon_find_encoder(connector, DRM_MODE_ENCODER_TMDS);
592 		if (!encoder)
593 			return 0;
594 
595 		radeon_encoder = to_radeon_encoder(encoder);
596 
597 		if (radeon_encoder->underscan_hborder != val) {
598 			radeon_encoder->underscan_hborder = val;
599 			radeon_property_change_mode(&radeon_encoder->base);
600 		}
601 	}
602 
603 	if (property == rdev->mode_info.underscan_vborder_property) {
604 		/* need to find digital encoder on connector */
605 		encoder = radeon_find_encoder(connector, DRM_MODE_ENCODER_TMDS);
606 		if (!encoder)
607 			return 0;
608 
609 		radeon_encoder = to_radeon_encoder(encoder);
610 
611 		if (radeon_encoder->underscan_vborder != val) {
612 			radeon_encoder->underscan_vborder = val;
613 			radeon_property_change_mode(&radeon_encoder->base);
614 		}
615 	}
616 
617 	if (property == rdev->mode_info.tv_std_property) {
618 		encoder = radeon_find_encoder(connector, DRM_MODE_ENCODER_TVDAC);
619 		if (!encoder) {
620 			encoder = radeon_find_encoder(connector, DRM_MODE_ENCODER_DAC);
621 		}
622 
623 		if (!encoder)
624 			return 0;
625 
626 		radeon_encoder = to_radeon_encoder(encoder);
627 		if (!radeon_encoder->enc_priv)
628 			return 0;
629 		if (ASIC_IS_AVIVO(rdev) || radeon_r4xx_atom) {
630 			struct radeon_encoder_atom_dac *dac_int;
631 			dac_int = radeon_encoder->enc_priv;
632 			dac_int->tv_std = val;
633 		} else {
634 			struct radeon_encoder_tv_dac *dac_int;
635 			dac_int = radeon_encoder->enc_priv;
636 			dac_int->tv_std = val;
637 		}
638 		radeon_property_change_mode(&radeon_encoder->base);
639 	}
640 
641 	if (property == rdev->mode_info.load_detect_property) {
642 		struct radeon_connector *radeon_connector =
643 			to_radeon_connector(connector);
644 
645 		if (val == 0)
646 			radeon_connector->dac_load_detect = false;
647 		else
648 			radeon_connector->dac_load_detect = true;
649 	}
650 
651 	if (property == rdev->mode_info.tmds_pll_property) {
652 		struct radeon_encoder_int_tmds *tmds = NULL;
653 		bool ret = false;
654 		/* need to find digital encoder on connector */
655 		encoder = radeon_find_encoder(connector, DRM_MODE_ENCODER_TMDS);
656 		if (!encoder)
657 			return 0;
658 
659 		radeon_encoder = to_radeon_encoder(encoder);
660 
661 		tmds = radeon_encoder->enc_priv;
662 		if (!tmds)
663 			return 0;
664 
665 		if (val == 0) {
666 			if (rdev->is_atom_bios)
667 				ret = radeon_atombios_get_tmds_info(radeon_encoder, tmds);
668 			else
669 				ret = radeon_legacy_get_tmds_info_from_combios(radeon_encoder, tmds);
670 		}
671 		if (val == 1 || !ret)
672 			radeon_legacy_get_tmds_info_from_table(radeon_encoder, tmds);
673 
674 		radeon_property_change_mode(&radeon_encoder->base);
675 	}
676 
677 	if (property == dev->mode_config.scaling_mode_property) {
678 		enum radeon_rmx_type rmx_type;
679 
680 		if (connector->encoder)
681 			radeon_encoder = to_radeon_encoder(connector->encoder);
682 		else {
683 			const struct drm_connector_helper_funcs *connector_funcs = connector->helper_private;
684 			radeon_encoder = to_radeon_encoder(connector_funcs->best_encoder(connector));
685 		}
686 
687 		switch (val) {
688 		default:
689 		case DRM_MODE_SCALE_NONE: rmx_type = RMX_OFF; break;
690 		case DRM_MODE_SCALE_CENTER: rmx_type = RMX_CENTER; break;
691 		case DRM_MODE_SCALE_ASPECT: rmx_type = RMX_ASPECT; break;
692 		case DRM_MODE_SCALE_FULLSCREEN: rmx_type = RMX_FULL; break;
693 		}
694 		if (radeon_encoder->rmx_type == rmx_type)
695 			return 0;
696 
697 		if ((rmx_type != DRM_MODE_SCALE_NONE) &&
698 		    (radeon_encoder->native_mode.clock == 0))
699 			return 0;
700 
701 		radeon_encoder->rmx_type = rmx_type;
702 
703 		radeon_property_change_mode(&radeon_encoder->base);
704 	}
705 
706 	if (property == rdev->mode_info.output_csc_property) {
707 		if (connector->encoder)
708 			radeon_encoder = to_radeon_encoder(connector->encoder);
709 		else {
710 			const struct drm_connector_helper_funcs *connector_funcs = connector->helper_private;
711 			radeon_encoder = to_radeon_encoder(connector_funcs->best_encoder(connector));
712 		}
713 
714 		if (radeon_encoder->output_csc == val)
715 			return 0;
716 
717 		radeon_encoder->output_csc = val;
718 
719 		if (connector->encoder && connector->encoder->crtc) {
720 			struct drm_crtc *crtc  = connector->encoder->crtc;
721 			struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
722 
723 			radeon_crtc->output_csc = radeon_encoder->output_csc;
724 
725 			/*
726 			 * Our .gamma_set assumes the .gamma_store has been
727 			 * prefilled and don't care about its arguments.
728 			 */
729 			crtc->funcs->gamma_set(crtc, NULL, NULL, NULL, 0, NULL);
730 		}
731 	}
732 
733 	return 0;
734 }
735 
radeon_fixup_lvds_native_mode(struct drm_encoder * encoder,struct drm_connector * connector)736 static void radeon_fixup_lvds_native_mode(struct drm_encoder *encoder,
737 					  struct drm_connector *connector)
738 {
739 	struct radeon_encoder *radeon_encoder =	to_radeon_encoder(encoder);
740 	struct drm_display_mode *native_mode = &radeon_encoder->native_mode;
741 	struct drm_display_mode *t, *mode;
742 
743 	/* If the EDID preferred mode doesn't match the native mode, use it */
744 	list_for_each_entry_safe(mode, t, &connector->probed_modes, head) {
745 		if (mode->type & DRM_MODE_TYPE_PREFERRED) {
746 			if (mode->hdisplay != native_mode->hdisplay ||
747 			    mode->vdisplay != native_mode->vdisplay)
748 				drm_mode_copy(native_mode, mode);
749 		}
750 	}
751 
752 	/* Try to get native mode details from EDID if necessary */
753 	if (!native_mode->clock) {
754 		list_for_each_entry_safe(mode, t, &connector->probed_modes, head) {
755 			if (mode->hdisplay == native_mode->hdisplay &&
756 			    mode->vdisplay == native_mode->vdisplay) {
757 				drm_mode_copy(native_mode, mode);
758 				drm_mode_set_crtcinfo(native_mode, CRTC_INTERLACE_HALVE_V);
759 				DRM_DEBUG_KMS("Determined LVDS native mode details from EDID\n");
760 				break;
761 			}
762 		}
763 	}
764 
765 	if (!native_mode->clock) {
766 		DRM_DEBUG_KMS("No LVDS native mode details, disabling RMX\n");
767 		radeon_encoder->rmx_type = RMX_OFF;
768 	}
769 }
770 
radeon_lvds_get_modes(struct drm_connector * connector)771 static int radeon_lvds_get_modes(struct drm_connector *connector)
772 {
773 	struct drm_encoder *encoder;
774 	int ret = 0;
775 	struct drm_display_mode *mode;
776 
777 	radeon_connector_get_edid(connector);
778 	ret = radeon_ddc_get_modes(connector);
779 	if (ret > 0) {
780 		encoder = radeon_best_single_encoder(connector);
781 		if (encoder) {
782 			radeon_fixup_lvds_native_mode(encoder, connector);
783 			/* add scaled modes */
784 			radeon_add_common_modes(encoder, connector);
785 		}
786 		return ret;
787 	}
788 
789 	encoder = radeon_best_single_encoder(connector);
790 	if (!encoder)
791 		return 0;
792 
793 	/* we have no EDID modes */
794 	mode = radeon_fp_native_mode(encoder);
795 	if (mode) {
796 		ret = 1;
797 		drm_mode_probed_add(connector, mode);
798 		/* add the width/height from vbios tables if available */
799 		connector->display_info.width_mm = mode->width_mm;
800 		connector->display_info.height_mm = mode->height_mm;
801 		/* add scaled modes */
802 		radeon_add_common_modes(encoder, connector);
803 	}
804 
805 	return ret;
806 }
807 
radeon_lvds_mode_valid(struct drm_connector * connector,struct drm_display_mode * mode)808 static enum drm_mode_status radeon_lvds_mode_valid(struct drm_connector *connector,
809 				  struct drm_display_mode *mode)
810 {
811 	struct drm_encoder *encoder = radeon_best_single_encoder(connector);
812 
813 	if ((mode->hdisplay < 320) || (mode->vdisplay < 240))
814 		return MODE_PANEL;
815 
816 	if (encoder) {
817 		struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
818 		struct drm_display_mode *native_mode = &radeon_encoder->native_mode;
819 
820 		/* AVIVO hardware supports downscaling modes larger than the panel
821 		 * to the panel size, but I'm not sure this is desirable.
822 		 */
823 		if ((mode->hdisplay > native_mode->hdisplay) ||
824 		    (mode->vdisplay > native_mode->vdisplay))
825 			return MODE_PANEL;
826 
827 		/* if scaling is disabled, block non-native modes */
828 		if (radeon_encoder->rmx_type == RMX_OFF) {
829 			if ((mode->hdisplay != native_mode->hdisplay) ||
830 			    (mode->vdisplay != native_mode->vdisplay))
831 				return MODE_PANEL;
832 		}
833 	}
834 
835 	return MODE_OK;
836 }
837 
838 static enum drm_connector_status
radeon_lvds_detect(struct drm_connector * connector,bool force)839 radeon_lvds_detect(struct drm_connector *connector, bool force)
840 {
841 	struct drm_device *dev = connector->dev;
842 	struct radeon_device *rdev = dev->dev_private;
843 	struct radeon_connector *radeon_connector = to_radeon_connector(connector);
844 	struct drm_encoder *encoder = radeon_best_single_encoder(connector);
845 	enum drm_connector_status ret = connector_status_disconnected;
846 	int r;
847 
848 	if (!drm_kms_helper_is_poll_worker()) {
849 		r = pm_runtime_get_sync(connector->dev->dev);
850 		if (r < 0) {
851 			pm_runtime_put_autosuspend(connector->dev->dev);
852 			return connector_status_disconnected;
853 		}
854 	}
855 
856 	if (encoder) {
857 		struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
858 		struct drm_display_mode *native_mode = &radeon_encoder->native_mode;
859 
860 		/* check if panel is valid */
861 		if (native_mode->hdisplay >= 320 && native_mode->vdisplay >= 240)
862 			ret = connector_status_connected;
863 		/* don't fetch the edid from the vbios if ddc fails and runpm is
864 		 * enabled so we report disconnected.
865 		 */
866 		if ((rdev->flags & RADEON_IS_PX) && (radeon_runtime_pm != 0))
867 			ret = connector_status_disconnected;
868 	}
869 
870 	/* check for edid as well */
871 	radeon_connector_get_edid(connector);
872 	if (radeon_connector->edid)
873 		ret = connector_status_connected;
874 	/* check acpi lid status ??? */
875 
876 	radeon_connector_update_scratch_regs(connector, ret);
877 
878 	if (!drm_kms_helper_is_poll_worker()) {
879 		pm_runtime_mark_last_busy(connector->dev->dev);
880 		pm_runtime_put_autosuspend(connector->dev->dev);
881 	}
882 
883 	return ret;
884 }
885 
radeon_connector_unregister(struct drm_connector * connector)886 static void radeon_connector_unregister(struct drm_connector *connector)
887 {
888 	struct radeon_connector *radeon_connector = to_radeon_connector(connector);
889 
890 	if (radeon_connector->ddc_bus && radeon_connector->ddc_bus->has_aux) {
891 		drm_dp_aux_unregister(&radeon_connector->ddc_bus->aux);
892 		radeon_connector->ddc_bus->has_aux = false;
893 	}
894 }
895 
radeon_connector_destroy(struct drm_connector * connector)896 static void radeon_connector_destroy(struct drm_connector *connector)
897 {
898 	struct radeon_connector *radeon_connector = to_radeon_connector(connector);
899 
900 	radeon_connector_free_edid(connector);
901 	kfree(radeon_connector->con_priv);
902 	drm_connector_unregister(connector);
903 	drm_connector_cleanup(connector);
904 	kfree(connector);
905 }
906 
radeon_lvds_set_property(struct drm_connector * connector,struct drm_property * property,uint64_t value)907 static int radeon_lvds_set_property(struct drm_connector *connector,
908 				    struct drm_property *property,
909 				    uint64_t value)
910 {
911 	struct drm_device *dev = connector->dev;
912 	struct radeon_encoder *radeon_encoder;
913 	enum radeon_rmx_type rmx_type;
914 
915 	DRM_DEBUG_KMS("\n");
916 	if (property != dev->mode_config.scaling_mode_property)
917 		return 0;
918 
919 	if (connector->encoder)
920 		radeon_encoder = to_radeon_encoder(connector->encoder);
921 	else {
922 		const struct drm_connector_helper_funcs *connector_funcs = connector->helper_private;
923 		radeon_encoder = to_radeon_encoder(connector_funcs->best_encoder(connector));
924 	}
925 
926 	switch (value) {
927 	case DRM_MODE_SCALE_NONE: rmx_type = RMX_OFF; break;
928 	case DRM_MODE_SCALE_CENTER: rmx_type = RMX_CENTER; break;
929 	case DRM_MODE_SCALE_ASPECT: rmx_type = RMX_ASPECT; break;
930 	default:
931 	case DRM_MODE_SCALE_FULLSCREEN: rmx_type = RMX_FULL; break;
932 	}
933 	if (radeon_encoder->rmx_type == rmx_type)
934 		return 0;
935 
936 	radeon_encoder->rmx_type = rmx_type;
937 
938 	radeon_property_change_mode(&radeon_encoder->base);
939 	return 0;
940 }
941 
942 
943 static const struct drm_connector_helper_funcs radeon_lvds_connector_helper_funcs = {
944 	.get_modes = radeon_lvds_get_modes,
945 	.mode_valid = radeon_lvds_mode_valid,
946 	.best_encoder = radeon_best_single_encoder,
947 };
948 
949 static const struct drm_connector_funcs radeon_lvds_connector_funcs = {
950 	.dpms = drm_helper_connector_dpms,
951 	.detect = radeon_lvds_detect,
952 	.fill_modes = drm_helper_probe_single_connector_modes,
953 	.early_unregister = radeon_connector_unregister,
954 	.destroy = radeon_connector_destroy,
955 	.set_property = radeon_lvds_set_property,
956 };
957 
radeon_vga_get_modes(struct drm_connector * connector)958 static int radeon_vga_get_modes(struct drm_connector *connector)
959 {
960 	int ret;
961 
962 	radeon_connector_get_edid(connector);
963 	ret = radeon_ddc_get_modes(connector);
964 
965 	radeon_get_native_mode(connector);
966 
967 	return ret;
968 }
969 
radeon_vga_mode_valid(struct drm_connector * connector,struct drm_display_mode * mode)970 static enum drm_mode_status radeon_vga_mode_valid(struct drm_connector *connector,
971 				  struct drm_display_mode *mode)
972 {
973 	struct drm_device *dev = connector->dev;
974 	struct radeon_device *rdev = dev->dev_private;
975 
976 	/* XXX check mode bandwidth */
977 
978 	if ((mode->clock / 10) > rdev->clock.max_pixel_clock)
979 		return MODE_CLOCK_HIGH;
980 
981 	return MODE_OK;
982 }
983 
984 static enum drm_connector_status
radeon_vga_detect(struct drm_connector * connector,bool force)985 radeon_vga_detect(struct drm_connector *connector, bool force)
986 {
987 	struct drm_device *dev = connector->dev;
988 	struct radeon_device *rdev = dev->dev_private;
989 	struct radeon_connector *radeon_connector = to_radeon_connector(connector);
990 	struct drm_encoder *encoder;
991 	const struct drm_encoder_helper_funcs *encoder_funcs;
992 	bool dret = false;
993 	enum drm_connector_status ret = connector_status_disconnected;
994 	int r;
995 
996 	if (!drm_kms_helper_is_poll_worker()) {
997 		r = pm_runtime_get_sync(connector->dev->dev);
998 		if (r < 0) {
999 			pm_runtime_put_autosuspend(connector->dev->dev);
1000 			return connector_status_disconnected;
1001 		}
1002 	}
1003 
1004 	encoder = radeon_best_single_encoder(connector);
1005 	if (!encoder)
1006 		ret = connector_status_disconnected;
1007 
1008 	if (radeon_connector->ddc_bus)
1009 		dret = radeon_ddc_probe(radeon_connector, false);
1010 	if (dret) {
1011 		radeon_connector->detected_by_load = false;
1012 		radeon_connector_free_edid(connector);
1013 		radeon_connector_get_edid(connector);
1014 
1015 		if (!radeon_connector->edid) {
1016 			DRM_ERROR("%s: probed a monitor but no|invalid EDID\n",
1017 					connector->name);
1018 			ret = connector_status_connected;
1019 		} else {
1020 			radeon_connector->use_digital =
1021 				!!(radeon_connector->edid->input & DRM_EDID_INPUT_DIGITAL);
1022 
1023 			/* some oems have boards with separate digital and analog connectors
1024 			 * with a shared ddc line (often vga + hdmi)
1025 			 */
1026 			if (radeon_connector->use_digital && radeon_connector->shared_ddc) {
1027 				radeon_connector_free_edid(connector);
1028 				ret = connector_status_disconnected;
1029 			} else {
1030 				ret = connector_status_connected;
1031 			}
1032 		}
1033 	} else {
1034 
1035 		/* if we aren't forcing don't do destructive polling */
1036 		if (!force) {
1037 			/* only return the previous status if we last
1038 			 * detected a monitor via load.
1039 			 */
1040 			if (radeon_connector->detected_by_load)
1041 				ret = connector->status;
1042 			goto out;
1043 		}
1044 
1045 		if (radeon_connector->dac_load_detect && encoder) {
1046 			encoder_funcs = encoder->helper_private;
1047 			ret = encoder_funcs->detect(encoder, connector);
1048 			if (ret != connector_status_disconnected)
1049 				radeon_connector->detected_by_load = true;
1050 		}
1051 	}
1052 
1053 	if (ret == connector_status_connected)
1054 		ret = radeon_connector_analog_encoder_conflict_solve(connector, encoder, ret, true);
1055 
1056 	/* RN50 and some RV100 asics in servers often have a hardcoded EDID in the
1057 	 * vbios to deal with KVMs. If we have one and are not able to detect a monitor
1058 	 * by other means, assume the CRT is connected and use that EDID.
1059 	 */
1060 	if ((!rdev->is_atom_bios) &&
1061 	    (ret == connector_status_disconnected) &&
1062 	    rdev->mode_info.bios_hardcoded_edid) {
1063 		ret = connector_status_connected;
1064 	}
1065 
1066 	radeon_connector_update_scratch_regs(connector, ret);
1067 
1068 out:
1069 	if (!drm_kms_helper_is_poll_worker()) {
1070 		pm_runtime_mark_last_busy(connector->dev->dev);
1071 		pm_runtime_put_autosuspend(connector->dev->dev);
1072 	}
1073 
1074 	return ret;
1075 }
1076 
1077 static const struct drm_connector_helper_funcs radeon_vga_connector_helper_funcs = {
1078 	.get_modes = radeon_vga_get_modes,
1079 	.mode_valid = radeon_vga_mode_valid,
1080 	.best_encoder = radeon_best_single_encoder,
1081 };
1082 
1083 static const struct drm_connector_funcs radeon_vga_connector_funcs = {
1084 	.dpms = drm_helper_connector_dpms,
1085 	.detect = radeon_vga_detect,
1086 	.fill_modes = drm_helper_probe_single_connector_modes,
1087 	.early_unregister = radeon_connector_unregister,
1088 	.destroy = radeon_connector_destroy,
1089 	.set_property = radeon_connector_set_property,
1090 };
1091 
radeon_tv_get_modes(struct drm_connector * connector)1092 static int radeon_tv_get_modes(struct drm_connector *connector)
1093 {
1094 	struct drm_device *dev = connector->dev;
1095 	struct radeon_device *rdev = dev->dev_private;
1096 	struct drm_display_mode *tv_mode;
1097 	struct drm_encoder *encoder;
1098 
1099 	encoder = radeon_best_single_encoder(connector);
1100 	if (!encoder)
1101 		return 0;
1102 
1103 	/* avivo chips can scale any mode */
1104 	if (rdev->family >= CHIP_RS600)
1105 		/* add scaled modes */
1106 		radeon_add_common_modes(encoder, connector);
1107 	else {
1108 		/* only 800x600 is supported right now on pre-avivo chips */
1109 		tv_mode = drm_cvt_mode(dev, 800, 600, 60, false, false, false);
1110 		if (!tv_mode)
1111 			return 0;
1112 		tv_mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
1113 		drm_mode_probed_add(connector, tv_mode);
1114 	}
1115 	return 1;
1116 }
1117 
radeon_tv_mode_valid(struct drm_connector * connector,struct drm_display_mode * mode)1118 static enum drm_mode_status radeon_tv_mode_valid(struct drm_connector *connector,
1119 				struct drm_display_mode *mode)
1120 {
1121 	if ((mode->hdisplay > 1024) || (mode->vdisplay > 768))
1122 		return MODE_CLOCK_RANGE;
1123 	return MODE_OK;
1124 }
1125 
1126 static enum drm_connector_status
radeon_tv_detect(struct drm_connector * connector,bool force)1127 radeon_tv_detect(struct drm_connector *connector, bool force)
1128 {
1129 	struct drm_encoder *encoder;
1130 	const struct drm_encoder_helper_funcs *encoder_funcs;
1131 	struct radeon_connector *radeon_connector = to_radeon_connector(connector);
1132 	enum drm_connector_status ret = connector_status_disconnected;
1133 	int r;
1134 
1135 	if (!radeon_connector->dac_load_detect)
1136 		return ret;
1137 
1138 	if (!drm_kms_helper_is_poll_worker()) {
1139 		r = pm_runtime_get_sync(connector->dev->dev);
1140 		if (r < 0) {
1141 			pm_runtime_put_autosuspend(connector->dev->dev);
1142 			return connector_status_disconnected;
1143 		}
1144 	}
1145 
1146 	encoder = radeon_best_single_encoder(connector);
1147 	if (!encoder)
1148 		ret = connector_status_disconnected;
1149 	else {
1150 		encoder_funcs = encoder->helper_private;
1151 		ret = encoder_funcs->detect(encoder, connector);
1152 	}
1153 	if (ret == connector_status_connected)
1154 		ret = radeon_connector_analog_encoder_conflict_solve(connector, encoder, ret, false);
1155 	radeon_connector_update_scratch_regs(connector, ret);
1156 
1157 	if (!drm_kms_helper_is_poll_worker()) {
1158 		pm_runtime_mark_last_busy(connector->dev->dev);
1159 		pm_runtime_put_autosuspend(connector->dev->dev);
1160 	}
1161 
1162 	return ret;
1163 }
1164 
1165 static const struct drm_connector_helper_funcs radeon_tv_connector_helper_funcs = {
1166 	.get_modes = radeon_tv_get_modes,
1167 	.mode_valid = radeon_tv_mode_valid,
1168 	.best_encoder = radeon_best_single_encoder,
1169 };
1170 
1171 static const struct drm_connector_funcs radeon_tv_connector_funcs = {
1172 	.dpms = drm_helper_connector_dpms,
1173 	.detect = radeon_tv_detect,
1174 	.fill_modes = drm_helper_probe_single_connector_modes,
1175 	.early_unregister = radeon_connector_unregister,
1176 	.destroy = radeon_connector_destroy,
1177 	.set_property = radeon_connector_set_property,
1178 };
1179 
radeon_check_hpd_status_unchanged(struct drm_connector * connector)1180 static bool radeon_check_hpd_status_unchanged(struct drm_connector *connector)
1181 {
1182 	struct drm_device *dev = connector->dev;
1183 	struct radeon_device *rdev = dev->dev_private;
1184 	struct radeon_connector *radeon_connector = to_radeon_connector(connector);
1185 	enum drm_connector_status status;
1186 
1187 	/* We only trust HPD on R600 and newer ASICS. */
1188 	if (rdev->family >= CHIP_R600
1189 	  && radeon_connector->hpd.hpd != RADEON_HPD_NONE) {
1190 		if (radeon_hpd_sense(rdev, radeon_connector->hpd.hpd))
1191 			status = connector_status_connected;
1192 		else
1193 			status = connector_status_disconnected;
1194 		if (connector->status == status)
1195 			return true;
1196 	}
1197 
1198 	return false;
1199 }
1200 
1201 /*
1202  * DVI is complicated
1203  * Do a DDC probe, if DDC probe passes, get the full EDID so
1204  * we can do analog/digital monitor detection at this point.
1205  * If the monitor is an analog monitor or we got no DDC,
1206  * we need to find the DAC encoder object for this connector.
1207  * If we got no DDC, we do load detection on the DAC encoder object.
1208  * If we got analog DDC or load detection passes on the DAC encoder
1209  * we have to check if this analog encoder is shared with anyone else (TV)
1210  * if its shared we have to set the other connector to disconnected.
1211  */
1212 static enum drm_connector_status
radeon_dvi_detect(struct drm_connector * connector,bool force)1213 radeon_dvi_detect(struct drm_connector *connector, bool force)
1214 {
1215 	struct drm_device *dev = connector->dev;
1216 	struct radeon_device *rdev = dev->dev_private;
1217 	struct radeon_connector *radeon_connector = to_radeon_connector(connector);
1218 	struct drm_encoder *encoder = NULL;
1219 	const struct drm_encoder_helper_funcs *encoder_funcs;
1220 	int r;
1221 	enum drm_connector_status ret = connector_status_disconnected;
1222 	bool dret = false, broken_edid = false;
1223 
1224 	if (!drm_kms_helper_is_poll_worker()) {
1225 		r = pm_runtime_get_sync(connector->dev->dev);
1226 		if (r < 0) {
1227 			pm_runtime_put_autosuspend(connector->dev->dev);
1228 			return connector_status_disconnected;
1229 		}
1230 	}
1231 
1232 	if (radeon_connector->detected_hpd_without_ddc) {
1233 		force = true;
1234 		radeon_connector->detected_hpd_without_ddc = false;
1235 	}
1236 
1237 	if (!force && radeon_check_hpd_status_unchanged(connector)) {
1238 		ret = connector->status;
1239 		goto exit;
1240 	}
1241 
1242 	if (radeon_connector->ddc_bus) {
1243 		dret = radeon_ddc_probe(radeon_connector, false);
1244 
1245 		/* Sometimes the pins required for the DDC probe on DVI
1246 		 * connectors don't make contact at the same time that the ones
1247 		 * for HPD do. If the DDC probe fails even though we had an HPD
1248 		 * signal, try again later */
1249 		if (!dret && !force &&
1250 		    connector->status != connector_status_connected) {
1251 			DRM_DEBUG_KMS("hpd detected without ddc, retrying in 1 second\n");
1252 			radeon_connector->detected_hpd_without_ddc = true;
1253 			schedule_delayed_work(&rdev->hotplug_work,
1254 					      msecs_to_jiffies(1000));
1255 			goto exit;
1256 		}
1257 	}
1258 	if (dret) {
1259 		radeon_connector->detected_by_load = false;
1260 		radeon_connector_free_edid(connector);
1261 		radeon_connector_get_edid(connector);
1262 
1263 		if (!radeon_connector->edid) {
1264 			DRM_ERROR("%s: probed a monitor but no|invalid EDID\n",
1265 					connector->name);
1266 			/* rs690 seems to have a problem with connectors not existing and always
1267 			 * return a block of 0's. If we see this just stop polling on this output */
1268 			if ((rdev->family == CHIP_RS690 || rdev->family == CHIP_RS740) &&
1269 			    radeon_connector->base.null_edid_counter) {
1270 				ret = connector_status_disconnected;
1271 				DRM_ERROR("%s: detected RS690 floating bus bug, stopping ddc detect\n",
1272 					  connector->name);
1273 				radeon_connector->ddc_bus = NULL;
1274 			} else {
1275 				ret = connector_status_connected;
1276 				broken_edid = true; /* defer use_digital to later */
1277 			}
1278 		} else {
1279 			radeon_connector->use_digital =
1280 				!!(radeon_connector->edid->input & DRM_EDID_INPUT_DIGITAL);
1281 
1282 			/* some oems have boards with separate digital and analog connectors
1283 			 * with a shared ddc line (often vga + hdmi)
1284 			 */
1285 			if ((!radeon_connector->use_digital) && radeon_connector->shared_ddc) {
1286 				radeon_connector_free_edid(connector);
1287 				ret = connector_status_disconnected;
1288 			} else {
1289 				ret = connector_status_connected;
1290 			}
1291 			/* This gets complicated.  We have boards with VGA + HDMI with a
1292 			 * shared DDC line and we have boards with DVI-D + HDMI with a shared
1293 			 * DDC line.  The latter is more complex because with DVI<->HDMI adapters
1294 			 * you don't really know what's connected to which port as both are digital.
1295 			 */
1296 			if (radeon_connector->shared_ddc && (ret == connector_status_connected)) {
1297 				struct drm_connector *list_connector;
1298 				struct radeon_connector *list_radeon_connector;
1299 				list_for_each_entry(list_connector, &dev->mode_config.connector_list, head) {
1300 					if (connector == list_connector)
1301 						continue;
1302 					list_radeon_connector = to_radeon_connector(list_connector);
1303 					if (list_radeon_connector->shared_ddc &&
1304 					    (list_radeon_connector->ddc_bus->rec.i2c_id ==
1305 					     radeon_connector->ddc_bus->rec.i2c_id)) {
1306 						/* cases where both connectors are digital */
1307 						if (list_connector->connector_type != DRM_MODE_CONNECTOR_VGA) {
1308 							/* hpd is our only option in this case */
1309 							if (!radeon_hpd_sense(rdev, radeon_connector->hpd.hpd)) {
1310 								radeon_connector_free_edid(connector);
1311 								ret = connector_status_disconnected;
1312 							}
1313 						}
1314 					}
1315 				}
1316 			}
1317 		}
1318 	}
1319 
1320 	if ((ret == connector_status_connected) && (radeon_connector->use_digital == true))
1321 		goto out;
1322 
1323 	/* DVI-D and HDMI-A are digital only */
1324 	if ((connector->connector_type == DRM_MODE_CONNECTOR_DVID) ||
1325 	    (connector->connector_type == DRM_MODE_CONNECTOR_HDMIA))
1326 		goto out;
1327 
1328 	/* if we aren't forcing don't do destructive polling */
1329 	if (!force) {
1330 		/* only return the previous status if we last
1331 		 * detected a monitor via load.
1332 		 */
1333 		if (radeon_connector->detected_by_load)
1334 			ret = connector->status;
1335 		goto out;
1336 	}
1337 
1338 	/* find analog encoder */
1339 	if (radeon_connector->dac_load_detect) {
1340 		drm_connector_for_each_possible_encoder(connector, encoder) {
1341 			if (encoder->encoder_type != DRM_MODE_ENCODER_DAC &&
1342 			    encoder->encoder_type != DRM_MODE_ENCODER_TVDAC)
1343 				continue;
1344 
1345 			encoder_funcs = encoder->helper_private;
1346 			if (encoder_funcs->detect) {
1347 				if (!broken_edid) {
1348 					if (ret != connector_status_connected) {
1349 						/* deal with analog monitors without DDC */
1350 						ret = encoder_funcs->detect(encoder, connector);
1351 						if (ret == connector_status_connected) {
1352 							radeon_connector->use_digital = false;
1353 						}
1354 						if (ret != connector_status_disconnected)
1355 							radeon_connector->detected_by_load = true;
1356 					}
1357 				} else {
1358 					enum drm_connector_status lret;
1359 					/* assume digital unless load detected otherwise */
1360 					radeon_connector->use_digital = true;
1361 					lret = encoder_funcs->detect(encoder, connector);
1362 					DRM_DEBUG_KMS("load_detect %x returned: %x\n", encoder->encoder_type, lret);
1363 					if (lret == connector_status_connected)
1364 						radeon_connector->use_digital = false;
1365 				}
1366 				break;
1367 			}
1368 		}
1369 	}
1370 
1371 	if ((ret == connector_status_connected) && (radeon_connector->use_digital == false) &&
1372 	    encoder) {
1373 		ret = radeon_connector_analog_encoder_conflict_solve(connector, encoder, ret, true);
1374 	}
1375 
1376 	/* RN50 and some RV100 asics in servers often have a hardcoded EDID in the
1377 	 * vbios to deal with KVMs. If we have one and are not able to detect a monitor
1378 	 * by other means, assume the DFP is connected and use that EDID.  In most
1379 	 * cases the DVI port is actually a virtual KVM port connected to the service
1380 	 * processor.
1381 	 */
1382 out:
1383 	if ((!rdev->is_atom_bios) &&
1384 	    (ret == connector_status_disconnected) &&
1385 	    rdev->mode_info.bios_hardcoded_edid) {
1386 		radeon_connector->use_digital = true;
1387 		ret = connector_status_connected;
1388 	}
1389 
1390 	/* updated in get modes as well since we need to know if it's analog or digital */
1391 	radeon_connector_update_scratch_regs(connector, ret);
1392 
1393 	if ((radeon_audio != 0) && radeon_connector->use_digital) {
1394 		const struct drm_connector_helper_funcs *connector_funcs =
1395 			connector->helper_private;
1396 
1397 		encoder = connector_funcs->best_encoder(connector);
1398 		if (encoder && (encoder->encoder_type == DRM_MODE_ENCODER_TMDS)) {
1399 			radeon_connector_get_edid(connector);
1400 			radeon_audio_detect(connector, encoder, ret);
1401 		}
1402 	}
1403 
1404 exit:
1405 	if (!drm_kms_helper_is_poll_worker()) {
1406 		pm_runtime_mark_last_busy(connector->dev->dev);
1407 		pm_runtime_put_autosuspend(connector->dev->dev);
1408 	}
1409 
1410 	return ret;
1411 }
1412 
1413 /* okay need to be smart in here about which encoder to pick */
radeon_dvi_encoder(struct drm_connector * connector)1414 static struct drm_encoder *radeon_dvi_encoder(struct drm_connector *connector)
1415 {
1416 	struct radeon_connector *radeon_connector = to_radeon_connector(connector);
1417 	struct drm_encoder *encoder;
1418 
1419 	drm_connector_for_each_possible_encoder(connector, encoder) {
1420 		if (radeon_connector->use_digital == true) {
1421 			if (encoder->encoder_type == DRM_MODE_ENCODER_TMDS)
1422 				return encoder;
1423 		} else {
1424 			if (encoder->encoder_type == DRM_MODE_ENCODER_DAC ||
1425 			    encoder->encoder_type == DRM_MODE_ENCODER_TVDAC)
1426 				return encoder;
1427 		}
1428 	}
1429 
1430 	/* see if we have a default encoder  TODO */
1431 
1432 	/* then check use digitial */
1433 	/* pick the first one */
1434 	drm_connector_for_each_possible_encoder(connector, encoder)
1435 		return encoder;
1436 
1437 	return NULL;
1438 }
1439 
radeon_dvi_force(struct drm_connector * connector)1440 static void radeon_dvi_force(struct drm_connector *connector)
1441 {
1442 	struct radeon_connector *radeon_connector = to_radeon_connector(connector);
1443 	if (connector->force == DRM_FORCE_ON)
1444 		radeon_connector->use_digital = false;
1445 	if (connector->force == DRM_FORCE_ON_DIGITAL)
1446 		radeon_connector->use_digital = true;
1447 }
1448 
radeon_dvi_mode_valid(struct drm_connector * connector,struct drm_display_mode * mode)1449 static enum drm_mode_status radeon_dvi_mode_valid(struct drm_connector *connector,
1450 				  struct drm_display_mode *mode)
1451 {
1452 	struct drm_device *dev = connector->dev;
1453 	struct radeon_device *rdev = dev->dev_private;
1454 	struct radeon_connector *radeon_connector = to_radeon_connector(connector);
1455 
1456 	/* XXX check mode bandwidth */
1457 
1458 	/* clocks over 135 MHz have heat issues with DVI on RV100 */
1459 	if (radeon_connector->use_digital &&
1460 	    (rdev->family == CHIP_RV100) &&
1461 	    (mode->clock > 135000))
1462 		return MODE_CLOCK_HIGH;
1463 
1464 	if (radeon_connector->use_digital && (mode->clock > 165000)) {
1465 		if ((radeon_connector->connector_object_id == CONNECTOR_OBJECT_ID_DUAL_LINK_DVI_I) ||
1466 		    (radeon_connector->connector_object_id == CONNECTOR_OBJECT_ID_DUAL_LINK_DVI_D) ||
1467 		    (radeon_connector->connector_object_id == CONNECTOR_OBJECT_ID_HDMI_TYPE_B))
1468 			return MODE_OK;
1469 		else if (ASIC_IS_DCE6(rdev) && connector->display_info.is_hdmi) {
1470 			/* HDMI 1.3+ supports max clock of 340 Mhz */
1471 			if (mode->clock > 340000)
1472 				return MODE_CLOCK_HIGH;
1473 			else
1474 				return MODE_OK;
1475 		} else {
1476 			return MODE_CLOCK_HIGH;
1477 		}
1478 	}
1479 
1480 	/* check against the max pixel clock */
1481 	if ((mode->clock / 10) > rdev->clock.max_pixel_clock)
1482 		return MODE_CLOCK_HIGH;
1483 
1484 	return MODE_OK;
1485 }
1486 
1487 static const struct drm_connector_helper_funcs radeon_dvi_connector_helper_funcs = {
1488 	.get_modes = radeon_vga_get_modes,
1489 	.mode_valid = radeon_dvi_mode_valid,
1490 	.best_encoder = radeon_dvi_encoder,
1491 };
1492 
1493 static const struct drm_connector_funcs radeon_dvi_connector_funcs = {
1494 	.dpms = drm_helper_connector_dpms,
1495 	.detect = radeon_dvi_detect,
1496 	.fill_modes = drm_helper_probe_single_connector_modes,
1497 	.set_property = radeon_connector_set_property,
1498 	.early_unregister = radeon_connector_unregister,
1499 	.destroy = radeon_connector_destroy,
1500 	.force = radeon_dvi_force,
1501 };
1502 
radeon_dp_get_modes(struct drm_connector * connector)1503 static int radeon_dp_get_modes(struct drm_connector *connector)
1504 {
1505 	struct radeon_connector *radeon_connector = to_radeon_connector(connector);
1506 	struct radeon_connector_atom_dig *radeon_dig_connector = radeon_connector->con_priv;
1507 	struct drm_encoder *encoder = radeon_best_single_encoder(connector);
1508 	int ret;
1509 
1510 	if ((connector->connector_type == DRM_MODE_CONNECTOR_eDP) ||
1511 	    (connector->connector_type == DRM_MODE_CONNECTOR_LVDS)) {
1512 		struct drm_display_mode *mode;
1513 
1514 		if (connector->connector_type == DRM_MODE_CONNECTOR_eDP) {
1515 			if (!radeon_dig_connector->edp_on)
1516 				atombios_set_edp_panel_power(connector,
1517 							     ATOM_TRANSMITTER_ACTION_POWER_ON);
1518 			radeon_connector_get_edid(connector);
1519 			ret = radeon_ddc_get_modes(connector);
1520 			if (!radeon_dig_connector->edp_on)
1521 				atombios_set_edp_panel_power(connector,
1522 							     ATOM_TRANSMITTER_ACTION_POWER_OFF);
1523 		} else {
1524 			/* need to setup ddc on the bridge */
1525 			if (radeon_connector_encoder_get_dp_bridge_encoder_id(connector) !=
1526 			    ENCODER_OBJECT_ID_NONE) {
1527 				if (encoder)
1528 					radeon_atom_ext_encoder_setup_ddc(encoder);
1529 			}
1530 			radeon_connector_get_edid(connector);
1531 			ret = radeon_ddc_get_modes(connector);
1532 		}
1533 
1534 		if (ret > 0) {
1535 			if (encoder) {
1536 				radeon_fixup_lvds_native_mode(encoder, connector);
1537 				/* add scaled modes */
1538 				radeon_add_common_modes(encoder, connector);
1539 			}
1540 			return ret;
1541 		}
1542 
1543 		if (!encoder)
1544 			return 0;
1545 
1546 		/* we have no EDID modes */
1547 		mode = radeon_fp_native_mode(encoder);
1548 		if (mode) {
1549 			ret = 1;
1550 			drm_mode_probed_add(connector, mode);
1551 			/* add the width/height from vbios tables if available */
1552 			connector->display_info.width_mm = mode->width_mm;
1553 			connector->display_info.height_mm = mode->height_mm;
1554 			/* add scaled modes */
1555 			radeon_add_common_modes(encoder, connector);
1556 		}
1557 	} else {
1558 		/* need to setup ddc on the bridge */
1559 		if (radeon_connector_encoder_get_dp_bridge_encoder_id(connector) !=
1560 			ENCODER_OBJECT_ID_NONE) {
1561 			if (encoder)
1562 				radeon_atom_ext_encoder_setup_ddc(encoder);
1563 		}
1564 		radeon_connector_get_edid(connector);
1565 		ret = radeon_ddc_get_modes(connector);
1566 
1567 		radeon_get_native_mode(connector);
1568 	}
1569 
1570 	return ret;
1571 }
1572 
radeon_connector_encoder_get_dp_bridge_encoder_id(struct drm_connector * connector)1573 u16 radeon_connector_encoder_get_dp_bridge_encoder_id(struct drm_connector *connector)
1574 {
1575 	struct drm_encoder *encoder;
1576 	struct radeon_encoder *radeon_encoder;
1577 
1578 	drm_connector_for_each_possible_encoder(connector, encoder) {
1579 		radeon_encoder = to_radeon_encoder(encoder);
1580 
1581 		switch (radeon_encoder->encoder_id) {
1582 		case ENCODER_OBJECT_ID_TRAVIS:
1583 		case ENCODER_OBJECT_ID_NUTMEG:
1584 			return radeon_encoder->encoder_id;
1585 		default:
1586 			break;
1587 		}
1588 	}
1589 
1590 	return ENCODER_OBJECT_ID_NONE;
1591 }
1592 
radeon_connector_encoder_is_hbr2(struct drm_connector * connector)1593 static bool radeon_connector_encoder_is_hbr2(struct drm_connector *connector)
1594 {
1595 	struct drm_encoder *encoder;
1596 	struct radeon_encoder *radeon_encoder;
1597 	bool found = false;
1598 
1599 	drm_connector_for_each_possible_encoder(connector, encoder) {
1600 		radeon_encoder = to_radeon_encoder(encoder);
1601 		if (radeon_encoder->caps & ATOM_ENCODER_CAP_RECORD_HBR2)
1602 			found = true;
1603 	}
1604 
1605 	return found;
1606 }
1607 
radeon_connector_is_dp12_capable(struct drm_connector * connector)1608 bool radeon_connector_is_dp12_capable(struct drm_connector *connector)
1609 {
1610 	struct drm_device *dev = connector->dev;
1611 	struct radeon_device *rdev = dev->dev_private;
1612 
1613 	if (ASIC_IS_DCE5(rdev) &&
1614 	    (rdev->clock.default_dispclk >= 53900) &&
1615 	    radeon_connector_encoder_is_hbr2(connector)) {
1616 		return true;
1617 	}
1618 
1619 	return false;
1620 }
1621 
1622 static enum drm_connector_status
radeon_dp_detect(struct drm_connector * connector,bool force)1623 radeon_dp_detect(struct drm_connector *connector, bool force)
1624 {
1625 	struct drm_device *dev = connector->dev;
1626 	struct radeon_device *rdev = dev->dev_private;
1627 	struct radeon_connector *radeon_connector = to_radeon_connector(connector);
1628 	enum drm_connector_status ret = connector_status_disconnected;
1629 	struct radeon_connector_atom_dig *radeon_dig_connector = radeon_connector->con_priv;
1630 	struct drm_encoder *encoder = radeon_best_single_encoder(connector);
1631 	int r;
1632 
1633 	if (!drm_kms_helper_is_poll_worker()) {
1634 		r = pm_runtime_get_sync(connector->dev->dev);
1635 		if (r < 0) {
1636 			pm_runtime_put_autosuspend(connector->dev->dev);
1637 			return connector_status_disconnected;
1638 		}
1639 	}
1640 
1641 	if (!force && radeon_check_hpd_status_unchanged(connector)) {
1642 		ret = connector->status;
1643 		goto out;
1644 	}
1645 
1646 	radeon_connector_free_edid(connector);
1647 
1648 	if ((connector->connector_type == DRM_MODE_CONNECTOR_eDP) ||
1649 	    (connector->connector_type == DRM_MODE_CONNECTOR_LVDS)) {
1650 		if (encoder) {
1651 			struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
1652 			struct drm_display_mode *native_mode = &radeon_encoder->native_mode;
1653 
1654 			/* check if panel is valid */
1655 			if (native_mode->hdisplay >= 320 && native_mode->vdisplay >= 240)
1656 				ret = connector_status_connected;
1657 			/* don't fetch the edid from the vbios if ddc fails and runpm is
1658 			 * enabled so we report disconnected.
1659 			 */
1660 			if ((rdev->flags & RADEON_IS_PX) && (radeon_runtime_pm != 0))
1661 				ret = connector_status_disconnected;
1662 		}
1663 		/* eDP is always DP */
1664 		radeon_dig_connector->dp_sink_type = CONNECTOR_OBJECT_ID_DISPLAYPORT;
1665 		if (!radeon_dig_connector->edp_on)
1666 			atombios_set_edp_panel_power(connector,
1667 						     ATOM_TRANSMITTER_ACTION_POWER_ON);
1668 		if (radeon_dp_getdpcd(radeon_connector))
1669 			ret = connector_status_connected;
1670 		if (!radeon_dig_connector->edp_on)
1671 			atombios_set_edp_panel_power(connector,
1672 						     ATOM_TRANSMITTER_ACTION_POWER_OFF);
1673 	} else if (radeon_connector_encoder_get_dp_bridge_encoder_id(connector) !=
1674 		   ENCODER_OBJECT_ID_NONE) {
1675 		/* DP bridges are always DP */
1676 		radeon_dig_connector->dp_sink_type = CONNECTOR_OBJECT_ID_DISPLAYPORT;
1677 		/* get the DPCD from the bridge */
1678 		radeon_dp_getdpcd(radeon_connector);
1679 
1680 		if (encoder) {
1681 			/* setup ddc on the bridge */
1682 			radeon_atom_ext_encoder_setup_ddc(encoder);
1683 			/* bridge chips are always aux */
1684 			if (radeon_ddc_probe(radeon_connector, true)) /* try DDC */
1685 				ret = connector_status_connected;
1686 			else if (radeon_connector->dac_load_detect) { /* try load detection */
1687 				const struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
1688 				ret = encoder_funcs->detect(encoder, connector);
1689 			}
1690 		}
1691 	} else {
1692 		radeon_dig_connector->dp_sink_type = radeon_dp_getsinktype(radeon_connector);
1693 		if (radeon_hpd_sense(rdev, radeon_connector->hpd.hpd)) {
1694 			ret = connector_status_connected;
1695 			if (radeon_dig_connector->dp_sink_type == CONNECTOR_OBJECT_ID_DISPLAYPORT)
1696 				radeon_dp_getdpcd(radeon_connector);
1697 		} else {
1698 			if (radeon_dig_connector->dp_sink_type == CONNECTOR_OBJECT_ID_DISPLAYPORT) {
1699 				if (radeon_dp_getdpcd(radeon_connector))
1700 					ret = connector_status_connected;
1701 			} else {
1702 				/* try non-aux ddc (DP to DVI/HDMI/etc. adapter) */
1703 				if (radeon_ddc_probe(radeon_connector, false))
1704 					ret = connector_status_connected;
1705 			}
1706 		}
1707 	}
1708 
1709 	radeon_connector_update_scratch_regs(connector, ret);
1710 
1711 	if ((radeon_audio != 0) && encoder) {
1712 		radeon_connector_get_edid(connector);
1713 		radeon_audio_detect(connector, encoder, ret);
1714 	}
1715 
1716 out:
1717 	if (!drm_kms_helper_is_poll_worker()) {
1718 		pm_runtime_mark_last_busy(connector->dev->dev);
1719 		pm_runtime_put_autosuspend(connector->dev->dev);
1720 	}
1721 
1722 	return ret;
1723 }
1724 
radeon_dp_mode_valid(struct drm_connector * connector,struct drm_display_mode * mode)1725 static enum drm_mode_status radeon_dp_mode_valid(struct drm_connector *connector,
1726 				  struct drm_display_mode *mode)
1727 {
1728 	struct drm_device *dev = connector->dev;
1729 	struct radeon_device *rdev = dev->dev_private;
1730 	struct radeon_connector *radeon_connector = to_radeon_connector(connector);
1731 	struct radeon_connector_atom_dig *radeon_dig_connector = radeon_connector->con_priv;
1732 
1733 	/* XXX check mode bandwidth */
1734 
1735 	if ((connector->connector_type == DRM_MODE_CONNECTOR_eDP) ||
1736 	    (connector->connector_type == DRM_MODE_CONNECTOR_LVDS)) {
1737 		struct drm_encoder *encoder = radeon_best_single_encoder(connector);
1738 
1739 		if ((mode->hdisplay < 320) || (mode->vdisplay < 240))
1740 			return MODE_PANEL;
1741 
1742 		if (encoder) {
1743 			struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
1744 			struct drm_display_mode *native_mode = &radeon_encoder->native_mode;
1745 
1746 			/* AVIVO hardware supports downscaling modes larger than the panel
1747 			 * to the panel size, but I'm not sure this is desirable.
1748 			 */
1749 			if ((mode->hdisplay > native_mode->hdisplay) ||
1750 			    (mode->vdisplay > native_mode->vdisplay))
1751 				return MODE_PANEL;
1752 
1753 			/* if scaling is disabled, block non-native modes */
1754 			if (radeon_encoder->rmx_type == RMX_OFF) {
1755 				if ((mode->hdisplay != native_mode->hdisplay) ||
1756 				    (mode->vdisplay != native_mode->vdisplay))
1757 					return MODE_PANEL;
1758 			}
1759 		}
1760 	} else {
1761 		if ((radeon_dig_connector->dp_sink_type == CONNECTOR_OBJECT_ID_DISPLAYPORT) ||
1762 		    (radeon_dig_connector->dp_sink_type == CONNECTOR_OBJECT_ID_eDP)) {
1763 			return radeon_dp_mode_valid_helper(connector, mode);
1764 		} else {
1765 			if (ASIC_IS_DCE6(rdev) && connector->display_info.is_hdmi) {
1766 				/* HDMI 1.3+ supports max clock of 340 Mhz */
1767 				if (mode->clock > 340000)
1768 					return MODE_CLOCK_HIGH;
1769 			} else {
1770 				if (mode->clock > 165000)
1771 					return MODE_CLOCK_HIGH;
1772 			}
1773 		}
1774 	}
1775 
1776 	return MODE_OK;
1777 }
1778 
1779 static int
radeon_connector_late_register(struct drm_connector * connector)1780 radeon_connector_late_register(struct drm_connector *connector)
1781 {
1782 	struct radeon_connector *radeon_connector = to_radeon_connector(connector);
1783 	int r = 0;
1784 
1785 	if (radeon_connector->ddc_bus->has_aux) {
1786 		radeon_connector->ddc_bus->aux.dev = radeon_connector->base.kdev;
1787 		r = drm_dp_aux_register(&radeon_connector->ddc_bus->aux);
1788 	}
1789 
1790 	return r;
1791 }
1792 
1793 static const struct drm_connector_helper_funcs radeon_dp_connector_helper_funcs = {
1794 	.get_modes = radeon_dp_get_modes,
1795 	.mode_valid = radeon_dp_mode_valid,
1796 	.best_encoder = radeon_dvi_encoder,
1797 };
1798 
1799 static const struct drm_connector_funcs radeon_dp_connector_funcs = {
1800 	.dpms = drm_helper_connector_dpms,
1801 	.detect = radeon_dp_detect,
1802 	.fill_modes = drm_helper_probe_single_connector_modes,
1803 	.set_property = radeon_connector_set_property,
1804 	.early_unregister = radeon_connector_unregister,
1805 	.destroy = radeon_connector_destroy,
1806 	.force = radeon_dvi_force,
1807 	.late_register = radeon_connector_late_register,
1808 };
1809 
1810 static const struct drm_connector_funcs radeon_edp_connector_funcs = {
1811 	.dpms = drm_helper_connector_dpms,
1812 	.detect = radeon_dp_detect,
1813 	.fill_modes = drm_helper_probe_single_connector_modes,
1814 	.set_property = radeon_lvds_set_property,
1815 	.early_unregister = radeon_connector_unregister,
1816 	.destroy = radeon_connector_destroy,
1817 	.force = radeon_dvi_force,
1818 	.late_register = radeon_connector_late_register,
1819 };
1820 
1821 static const struct drm_connector_funcs radeon_lvds_bridge_connector_funcs = {
1822 	.dpms = drm_helper_connector_dpms,
1823 	.detect = radeon_dp_detect,
1824 	.fill_modes = drm_helper_probe_single_connector_modes,
1825 	.set_property = radeon_lvds_set_property,
1826 	.early_unregister = radeon_connector_unregister,
1827 	.destroy = radeon_connector_destroy,
1828 	.force = radeon_dvi_force,
1829 	.late_register = radeon_connector_late_register,
1830 };
1831 
1832 void
radeon_add_atom_connector(struct drm_device * dev,uint32_t connector_id,uint32_t supported_device,int connector_type,struct radeon_i2c_bus_rec * i2c_bus,uint32_t igp_lane_info,uint16_t connector_object_id,struct radeon_hpd * hpd,struct radeon_router * router)1833 radeon_add_atom_connector(struct drm_device *dev,
1834 			  uint32_t connector_id,
1835 			  uint32_t supported_device,
1836 			  int connector_type,
1837 			  struct radeon_i2c_bus_rec *i2c_bus,
1838 			  uint32_t igp_lane_info,
1839 			  uint16_t connector_object_id,
1840 			  struct radeon_hpd *hpd,
1841 			  struct radeon_router *router)
1842 {
1843 	struct radeon_device *rdev = dev->dev_private;
1844 	struct drm_connector *connector;
1845 	struct radeon_connector *radeon_connector;
1846 	struct radeon_connector_atom_dig *radeon_dig_connector;
1847 	struct drm_encoder *encoder;
1848 	struct radeon_encoder *radeon_encoder;
1849 	struct i2c_adapter *ddc = NULL;
1850 	uint32_t subpixel_order = SubPixelNone;
1851 	bool shared_ddc = false;
1852 	bool is_dp_bridge = false;
1853 	bool has_aux = false;
1854 
1855 	if (connector_type == DRM_MODE_CONNECTOR_Unknown)
1856 		return;
1857 
1858 	/* if the user selected tv=0 don't try and add the connector */
1859 	if (((connector_type == DRM_MODE_CONNECTOR_SVIDEO) ||
1860 	     (connector_type == DRM_MODE_CONNECTOR_Composite) ||
1861 	     (connector_type == DRM_MODE_CONNECTOR_9PinDIN)) &&
1862 	    (radeon_tv == 0))
1863 		return;
1864 
1865 	/* see if we already added it */
1866 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1867 		radeon_connector = to_radeon_connector(connector);
1868 		if (radeon_connector->connector_id == connector_id) {
1869 			radeon_connector->devices |= supported_device;
1870 			return;
1871 		}
1872 		if (radeon_connector->ddc_bus && i2c_bus->valid) {
1873 			if (radeon_connector->ddc_bus->rec.i2c_id == i2c_bus->i2c_id) {
1874 				radeon_connector->shared_ddc = true;
1875 				shared_ddc = true;
1876 			}
1877 			if (radeon_connector->router_bus && router->ddc_valid &&
1878 			    (radeon_connector->router.router_id == router->router_id)) {
1879 				radeon_connector->shared_ddc = false;
1880 				shared_ddc = false;
1881 			}
1882 		}
1883 	}
1884 
1885 	/* check if it's a dp bridge */
1886 	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1887 		radeon_encoder = to_radeon_encoder(encoder);
1888 		if (radeon_encoder->devices & supported_device) {
1889 			switch (radeon_encoder->encoder_id) {
1890 			case ENCODER_OBJECT_ID_TRAVIS:
1891 			case ENCODER_OBJECT_ID_NUTMEG:
1892 				is_dp_bridge = true;
1893 				break;
1894 			default:
1895 				break;
1896 			}
1897 		}
1898 	}
1899 
1900 	radeon_connector = kzalloc(sizeof(struct radeon_connector), GFP_KERNEL);
1901 	if (!radeon_connector)
1902 		return;
1903 
1904 	connector = &radeon_connector->base;
1905 
1906 	radeon_connector->connector_id = connector_id;
1907 	radeon_connector->devices = supported_device;
1908 	radeon_connector->shared_ddc = shared_ddc;
1909 	radeon_connector->connector_object_id = connector_object_id;
1910 	radeon_connector->hpd = *hpd;
1911 
1912 	radeon_connector->router = *router;
1913 	if (router->ddc_valid || router->cd_valid) {
1914 		radeon_connector->router_bus = radeon_i2c_lookup(rdev, &router->i2c_info);
1915 		if (!radeon_connector->router_bus)
1916 			DRM_ERROR("Failed to assign router i2c bus! Check dmesg for i2c errors.\n");
1917 	}
1918 
1919 	if (is_dp_bridge) {
1920 		radeon_dig_connector = kzalloc(sizeof(struct radeon_connector_atom_dig), GFP_KERNEL);
1921 		if (!radeon_dig_connector)
1922 			goto failed;
1923 		radeon_dig_connector->igp_lane_info = igp_lane_info;
1924 		radeon_connector->con_priv = radeon_dig_connector;
1925 		if (i2c_bus->valid) {
1926 			radeon_connector->ddc_bus = radeon_i2c_lookup(rdev, i2c_bus);
1927 			if (radeon_connector->ddc_bus) {
1928 				has_aux = true;
1929 				ddc = &radeon_connector->ddc_bus->adapter;
1930 			} else {
1931 				DRM_ERROR("DP: Failed to assign ddc bus! Check dmesg for i2c errors.\n");
1932 			}
1933 		}
1934 		switch (connector_type) {
1935 		case DRM_MODE_CONNECTOR_VGA:
1936 		case DRM_MODE_CONNECTOR_DVIA:
1937 		default:
1938 			drm_connector_init_with_ddc(dev, &radeon_connector->base,
1939 						    &radeon_dp_connector_funcs,
1940 						    connector_type,
1941 						    ddc);
1942 			drm_connector_helper_add(&radeon_connector->base,
1943 						 &radeon_dp_connector_helper_funcs);
1944 			connector->interlace_allowed = true;
1945 			connector->doublescan_allowed = true;
1946 			radeon_connector->dac_load_detect = true;
1947 			drm_object_attach_property(&radeon_connector->base.base,
1948 						      rdev->mode_info.load_detect_property,
1949 						      1);
1950 			drm_object_attach_property(&radeon_connector->base.base,
1951 						   dev->mode_config.scaling_mode_property,
1952 						   DRM_MODE_SCALE_NONE);
1953 			if (ASIC_IS_DCE5(rdev))
1954 				drm_object_attach_property(&radeon_connector->base.base,
1955 							   rdev->mode_info.output_csc_property,
1956 							   RADEON_OUTPUT_CSC_BYPASS);
1957 			break;
1958 		case DRM_MODE_CONNECTOR_DVII:
1959 		case DRM_MODE_CONNECTOR_DVID:
1960 		case DRM_MODE_CONNECTOR_HDMIA:
1961 		case DRM_MODE_CONNECTOR_HDMIB:
1962 		case DRM_MODE_CONNECTOR_DisplayPort:
1963 			drm_connector_init_with_ddc(dev, &radeon_connector->base,
1964 						    &radeon_dp_connector_funcs,
1965 						    connector_type,
1966 						    ddc);
1967 			drm_connector_helper_add(&radeon_connector->base,
1968 						 &radeon_dp_connector_helper_funcs);
1969 			drm_object_attach_property(&radeon_connector->base.base,
1970 						      rdev->mode_info.underscan_property,
1971 						      UNDERSCAN_OFF);
1972 			drm_object_attach_property(&radeon_connector->base.base,
1973 						      rdev->mode_info.underscan_hborder_property,
1974 						      0);
1975 			drm_object_attach_property(&radeon_connector->base.base,
1976 						      rdev->mode_info.underscan_vborder_property,
1977 						      0);
1978 
1979 			drm_object_attach_property(&radeon_connector->base.base,
1980 						      dev->mode_config.scaling_mode_property,
1981 						      DRM_MODE_SCALE_NONE);
1982 
1983 			drm_object_attach_property(&radeon_connector->base.base,
1984 						   rdev->mode_info.dither_property,
1985 						   RADEON_FMT_DITHER_DISABLE);
1986 
1987 			if (radeon_audio != 0) {
1988 				drm_object_attach_property(&radeon_connector->base.base,
1989 							   rdev->mode_info.audio_property,
1990 							   RADEON_AUDIO_AUTO);
1991 				radeon_connector->audio = RADEON_AUDIO_AUTO;
1992 			}
1993 			if (ASIC_IS_DCE5(rdev))
1994 				drm_object_attach_property(&radeon_connector->base.base,
1995 							   rdev->mode_info.output_csc_property,
1996 							   RADEON_OUTPUT_CSC_BYPASS);
1997 
1998 			subpixel_order = SubPixelHorizontalRGB;
1999 			connector->interlace_allowed = true;
2000 			if (connector_type == DRM_MODE_CONNECTOR_HDMIB)
2001 				connector->doublescan_allowed = true;
2002 			else
2003 				connector->doublescan_allowed = false;
2004 			if (connector_type == DRM_MODE_CONNECTOR_DVII) {
2005 				radeon_connector->dac_load_detect = true;
2006 				drm_object_attach_property(&radeon_connector->base.base,
2007 							      rdev->mode_info.load_detect_property,
2008 							      1);
2009 			}
2010 			break;
2011 		case DRM_MODE_CONNECTOR_LVDS:
2012 		case DRM_MODE_CONNECTOR_eDP:
2013 			drm_connector_init_with_ddc(dev, &radeon_connector->base,
2014 						    &radeon_lvds_bridge_connector_funcs,
2015 						    connector_type,
2016 						    ddc);
2017 			drm_connector_helper_add(&radeon_connector->base,
2018 						 &radeon_dp_connector_helper_funcs);
2019 			drm_object_attach_property(&radeon_connector->base.base,
2020 						      dev->mode_config.scaling_mode_property,
2021 						      DRM_MODE_SCALE_FULLSCREEN);
2022 			subpixel_order = SubPixelHorizontalRGB;
2023 			connector->interlace_allowed = false;
2024 			connector->doublescan_allowed = false;
2025 			break;
2026 		}
2027 	} else {
2028 		switch (connector_type) {
2029 		case DRM_MODE_CONNECTOR_VGA:
2030 			if (i2c_bus->valid) {
2031 				radeon_connector->ddc_bus = radeon_i2c_lookup(rdev, i2c_bus);
2032 				if (!radeon_connector->ddc_bus)
2033 					DRM_ERROR("VGA: Failed to assign ddc bus! Check dmesg for i2c errors.\n");
2034 				else
2035 					ddc = &radeon_connector->ddc_bus->adapter;
2036 			}
2037 			drm_connector_init_with_ddc(dev, &radeon_connector->base,
2038 						    &radeon_vga_connector_funcs,
2039 						    connector_type,
2040 						    ddc);
2041 			drm_connector_helper_add(&radeon_connector->base, &radeon_vga_connector_helper_funcs);
2042 			radeon_connector->dac_load_detect = true;
2043 			drm_object_attach_property(&radeon_connector->base.base,
2044 						      rdev->mode_info.load_detect_property,
2045 						      1);
2046 			if (ASIC_IS_AVIVO(rdev))
2047 				drm_object_attach_property(&radeon_connector->base.base,
2048 							   dev->mode_config.scaling_mode_property,
2049 							   DRM_MODE_SCALE_NONE);
2050 			if (ASIC_IS_DCE5(rdev))
2051 				drm_object_attach_property(&radeon_connector->base.base,
2052 							   rdev->mode_info.output_csc_property,
2053 							   RADEON_OUTPUT_CSC_BYPASS);
2054 			/* no HPD on analog connectors */
2055 			radeon_connector->hpd.hpd = RADEON_HPD_NONE;
2056 			connector->interlace_allowed = true;
2057 			connector->doublescan_allowed = true;
2058 			break;
2059 		case DRM_MODE_CONNECTOR_DVIA:
2060 			if (i2c_bus->valid) {
2061 				radeon_connector->ddc_bus = radeon_i2c_lookup(rdev, i2c_bus);
2062 				if (!radeon_connector->ddc_bus)
2063 					DRM_ERROR("DVIA: Failed to assign ddc bus! Check dmesg for i2c errors.\n");
2064 				else
2065 					ddc = &radeon_connector->ddc_bus->adapter;
2066 			}
2067 			drm_connector_init_with_ddc(dev, &radeon_connector->base,
2068 						    &radeon_vga_connector_funcs,
2069 						    connector_type,
2070 						    ddc);
2071 			drm_connector_helper_add(&radeon_connector->base, &radeon_vga_connector_helper_funcs);
2072 			radeon_connector->dac_load_detect = true;
2073 			drm_object_attach_property(&radeon_connector->base.base,
2074 						      rdev->mode_info.load_detect_property,
2075 						      1);
2076 			if (ASIC_IS_AVIVO(rdev))
2077 				drm_object_attach_property(&radeon_connector->base.base,
2078 							   dev->mode_config.scaling_mode_property,
2079 							   DRM_MODE_SCALE_NONE);
2080 			if (ASIC_IS_DCE5(rdev))
2081 				drm_object_attach_property(&radeon_connector->base.base,
2082 							   rdev->mode_info.output_csc_property,
2083 							   RADEON_OUTPUT_CSC_BYPASS);
2084 			/* no HPD on analog connectors */
2085 			radeon_connector->hpd.hpd = RADEON_HPD_NONE;
2086 			connector->interlace_allowed = true;
2087 			connector->doublescan_allowed = true;
2088 			break;
2089 		case DRM_MODE_CONNECTOR_DVII:
2090 		case DRM_MODE_CONNECTOR_DVID:
2091 			radeon_dig_connector = kzalloc(sizeof(struct radeon_connector_atom_dig), GFP_KERNEL);
2092 			if (!radeon_dig_connector)
2093 				goto failed;
2094 			radeon_dig_connector->igp_lane_info = igp_lane_info;
2095 			radeon_connector->con_priv = radeon_dig_connector;
2096 			if (i2c_bus->valid) {
2097 				radeon_connector->ddc_bus = radeon_i2c_lookup(rdev, i2c_bus);
2098 				if (!radeon_connector->ddc_bus)
2099 					DRM_ERROR("DVI: Failed to assign ddc bus! Check dmesg for i2c errors.\n");
2100 				else
2101 					ddc = &radeon_connector->ddc_bus->adapter;
2102 			}
2103 			drm_connector_init_with_ddc(dev, &radeon_connector->base,
2104 						    &radeon_dvi_connector_funcs,
2105 						    connector_type,
2106 						    ddc);
2107 			drm_connector_helper_add(&radeon_connector->base, &radeon_dvi_connector_helper_funcs);
2108 			subpixel_order = SubPixelHorizontalRGB;
2109 			drm_object_attach_property(&radeon_connector->base.base,
2110 						      rdev->mode_info.coherent_mode_property,
2111 						      1);
2112 			if (ASIC_IS_AVIVO(rdev)) {
2113 				drm_object_attach_property(&radeon_connector->base.base,
2114 							      rdev->mode_info.underscan_property,
2115 							      UNDERSCAN_OFF);
2116 				drm_object_attach_property(&radeon_connector->base.base,
2117 							      rdev->mode_info.underscan_hborder_property,
2118 							      0);
2119 				drm_object_attach_property(&radeon_connector->base.base,
2120 							      rdev->mode_info.underscan_vborder_property,
2121 							      0);
2122 				drm_object_attach_property(&radeon_connector->base.base,
2123 							   rdev->mode_info.dither_property,
2124 							   RADEON_FMT_DITHER_DISABLE);
2125 				drm_object_attach_property(&radeon_connector->base.base,
2126 							   dev->mode_config.scaling_mode_property,
2127 							   DRM_MODE_SCALE_NONE);
2128 			}
2129 			if (ASIC_IS_DCE2(rdev) && (radeon_audio != 0)) {
2130 				drm_object_attach_property(&radeon_connector->base.base,
2131 							   rdev->mode_info.audio_property,
2132 							   RADEON_AUDIO_AUTO);
2133 				radeon_connector->audio = RADEON_AUDIO_AUTO;
2134 			}
2135 			if (connector_type == DRM_MODE_CONNECTOR_DVII) {
2136 				radeon_connector->dac_load_detect = true;
2137 				drm_object_attach_property(&radeon_connector->base.base,
2138 							      rdev->mode_info.load_detect_property,
2139 							      1);
2140 			}
2141 			if (ASIC_IS_DCE5(rdev))
2142 				drm_object_attach_property(&radeon_connector->base.base,
2143 							   rdev->mode_info.output_csc_property,
2144 							   RADEON_OUTPUT_CSC_BYPASS);
2145 			connector->interlace_allowed = true;
2146 			if (connector_type == DRM_MODE_CONNECTOR_DVII)
2147 				connector->doublescan_allowed = true;
2148 			else
2149 				connector->doublescan_allowed = false;
2150 			break;
2151 		case DRM_MODE_CONNECTOR_HDMIA:
2152 		case DRM_MODE_CONNECTOR_HDMIB:
2153 			radeon_dig_connector = kzalloc(sizeof(struct radeon_connector_atom_dig), GFP_KERNEL);
2154 			if (!radeon_dig_connector)
2155 				goto failed;
2156 			radeon_dig_connector->igp_lane_info = igp_lane_info;
2157 			radeon_connector->con_priv = radeon_dig_connector;
2158 			if (i2c_bus->valid) {
2159 				radeon_connector->ddc_bus = radeon_i2c_lookup(rdev, i2c_bus);
2160 				if (!radeon_connector->ddc_bus)
2161 					DRM_ERROR("HDMI: Failed to assign ddc bus! Check dmesg for i2c errors.\n");
2162 				else
2163 					ddc = &radeon_connector->ddc_bus->adapter;
2164 			}
2165 			drm_connector_init_with_ddc(dev, &radeon_connector->base,
2166 						    &radeon_dvi_connector_funcs,
2167 						    connector_type,
2168 						    ddc);
2169 			drm_connector_helper_add(&radeon_connector->base, &radeon_dvi_connector_helper_funcs);
2170 			drm_object_attach_property(&radeon_connector->base.base,
2171 						      rdev->mode_info.coherent_mode_property,
2172 						      1);
2173 			if (ASIC_IS_AVIVO(rdev)) {
2174 				drm_object_attach_property(&radeon_connector->base.base,
2175 							      rdev->mode_info.underscan_property,
2176 							      UNDERSCAN_OFF);
2177 				drm_object_attach_property(&radeon_connector->base.base,
2178 							      rdev->mode_info.underscan_hborder_property,
2179 							      0);
2180 				drm_object_attach_property(&radeon_connector->base.base,
2181 							      rdev->mode_info.underscan_vborder_property,
2182 							      0);
2183 				drm_object_attach_property(&radeon_connector->base.base,
2184 							   rdev->mode_info.dither_property,
2185 							   RADEON_FMT_DITHER_DISABLE);
2186 				drm_object_attach_property(&radeon_connector->base.base,
2187 							   dev->mode_config.scaling_mode_property,
2188 							   DRM_MODE_SCALE_NONE);
2189 			}
2190 			if (ASIC_IS_DCE2(rdev) && (radeon_audio != 0)) {
2191 				drm_object_attach_property(&radeon_connector->base.base,
2192 							   rdev->mode_info.audio_property,
2193 							   RADEON_AUDIO_AUTO);
2194 				radeon_connector->audio = RADEON_AUDIO_AUTO;
2195 			}
2196 			if (ASIC_IS_DCE5(rdev))
2197 				drm_object_attach_property(&radeon_connector->base.base,
2198 							   rdev->mode_info.output_csc_property,
2199 							   RADEON_OUTPUT_CSC_BYPASS);
2200 			subpixel_order = SubPixelHorizontalRGB;
2201 			connector->interlace_allowed = true;
2202 			if (connector_type == DRM_MODE_CONNECTOR_HDMIB)
2203 				connector->doublescan_allowed = true;
2204 			else
2205 				connector->doublescan_allowed = false;
2206 			break;
2207 		case DRM_MODE_CONNECTOR_DisplayPort:
2208 			radeon_dig_connector = kzalloc(sizeof(struct radeon_connector_atom_dig), GFP_KERNEL);
2209 			if (!radeon_dig_connector)
2210 				goto failed;
2211 			radeon_dig_connector->igp_lane_info = igp_lane_info;
2212 			radeon_connector->con_priv = radeon_dig_connector;
2213 			if (i2c_bus->valid) {
2214 				radeon_connector->ddc_bus = radeon_i2c_lookup(rdev, i2c_bus);
2215 				if (radeon_connector->ddc_bus) {
2216 					has_aux = true;
2217 					ddc = &radeon_connector->ddc_bus->adapter;
2218 				} else {
2219 					DRM_ERROR("DP: Failed to assign ddc bus! Check dmesg for i2c errors.\n");
2220 				}
2221 			}
2222 			drm_connector_init_with_ddc(dev, &radeon_connector->base,
2223 						    &radeon_dp_connector_funcs,
2224 						    connector_type,
2225 						    ddc);
2226 			drm_connector_helper_add(&radeon_connector->base, &radeon_dp_connector_helper_funcs);
2227 			subpixel_order = SubPixelHorizontalRGB;
2228 			drm_object_attach_property(&radeon_connector->base.base,
2229 						      rdev->mode_info.coherent_mode_property,
2230 						      1);
2231 			if (ASIC_IS_AVIVO(rdev)) {
2232 				drm_object_attach_property(&radeon_connector->base.base,
2233 							      rdev->mode_info.underscan_property,
2234 							      UNDERSCAN_OFF);
2235 				drm_object_attach_property(&radeon_connector->base.base,
2236 							      rdev->mode_info.underscan_hborder_property,
2237 							      0);
2238 				drm_object_attach_property(&radeon_connector->base.base,
2239 							      rdev->mode_info.underscan_vborder_property,
2240 							      0);
2241 				drm_object_attach_property(&radeon_connector->base.base,
2242 							   rdev->mode_info.dither_property,
2243 							   RADEON_FMT_DITHER_DISABLE);
2244 				drm_object_attach_property(&radeon_connector->base.base,
2245 							   dev->mode_config.scaling_mode_property,
2246 							   DRM_MODE_SCALE_NONE);
2247 			}
2248 			if (ASIC_IS_DCE2(rdev) && (radeon_audio != 0)) {
2249 				drm_object_attach_property(&radeon_connector->base.base,
2250 							   rdev->mode_info.audio_property,
2251 							   RADEON_AUDIO_AUTO);
2252 				radeon_connector->audio = RADEON_AUDIO_AUTO;
2253 			}
2254 			if (ASIC_IS_DCE5(rdev))
2255 				drm_object_attach_property(&radeon_connector->base.base,
2256 							   rdev->mode_info.output_csc_property,
2257 							   RADEON_OUTPUT_CSC_BYPASS);
2258 			connector->interlace_allowed = true;
2259 			/* in theory with a DP to VGA converter... */
2260 			connector->doublescan_allowed = false;
2261 			break;
2262 		case DRM_MODE_CONNECTOR_eDP:
2263 			radeon_dig_connector = kzalloc(sizeof(struct radeon_connector_atom_dig), GFP_KERNEL);
2264 			if (!radeon_dig_connector)
2265 				goto failed;
2266 			radeon_dig_connector->igp_lane_info = igp_lane_info;
2267 			radeon_connector->con_priv = radeon_dig_connector;
2268 			if (i2c_bus->valid) {
2269 				radeon_connector->ddc_bus = radeon_i2c_lookup(rdev, i2c_bus);
2270 				if (radeon_connector->ddc_bus) {
2271 					has_aux = true;
2272 					ddc = &radeon_connector->ddc_bus->adapter;
2273 				} else {
2274 					DRM_ERROR("DP: Failed to assign ddc bus! Check dmesg for i2c errors.\n");
2275 				}
2276 			}
2277 			drm_connector_init_with_ddc(dev, &radeon_connector->base,
2278 						    &radeon_edp_connector_funcs,
2279 						    connector_type,
2280 						    ddc);
2281 			drm_connector_helper_add(&radeon_connector->base, &radeon_dp_connector_helper_funcs);
2282 			drm_object_attach_property(&radeon_connector->base.base,
2283 						      dev->mode_config.scaling_mode_property,
2284 						      DRM_MODE_SCALE_FULLSCREEN);
2285 			subpixel_order = SubPixelHorizontalRGB;
2286 			connector->interlace_allowed = false;
2287 			connector->doublescan_allowed = false;
2288 			break;
2289 		case DRM_MODE_CONNECTOR_SVIDEO:
2290 		case DRM_MODE_CONNECTOR_Composite:
2291 		case DRM_MODE_CONNECTOR_9PinDIN:
2292 			drm_connector_init_with_ddc(dev, &radeon_connector->base,
2293 						    &radeon_tv_connector_funcs,
2294 						    connector_type,
2295 						    ddc);
2296 			drm_connector_helper_add(&radeon_connector->base, &radeon_tv_connector_helper_funcs);
2297 			radeon_connector->dac_load_detect = true;
2298 			drm_object_attach_property(&radeon_connector->base.base,
2299 						      rdev->mode_info.load_detect_property,
2300 						      1);
2301 			drm_object_attach_property(&radeon_connector->base.base,
2302 						      rdev->mode_info.tv_std_property,
2303 						      radeon_atombios_get_tv_info(rdev));
2304 			/* no HPD on analog connectors */
2305 			radeon_connector->hpd.hpd = RADEON_HPD_NONE;
2306 			connector->interlace_allowed = false;
2307 			connector->doublescan_allowed = false;
2308 			break;
2309 		case DRM_MODE_CONNECTOR_LVDS:
2310 			radeon_dig_connector = kzalloc(sizeof(struct radeon_connector_atom_dig), GFP_KERNEL);
2311 			if (!radeon_dig_connector)
2312 				goto failed;
2313 			radeon_dig_connector->igp_lane_info = igp_lane_info;
2314 			radeon_connector->con_priv = radeon_dig_connector;
2315 			if (i2c_bus->valid) {
2316 				radeon_connector->ddc_bus = radeon_i2c_lookup(rdev, i2c_bus);
2317 				if (!radeon_connector->ddc_bus)
2318 					DRM_ERROR("LVDS: Failed to assign ddc bus! Check dmesg for i2c errors.\n");
2319 				else
2320 					ddc = &radeon_connector->ddc_bus->adapter;
2321 			}
2322 			drm_connector_init_with_ddc(dev, &radeon_connector->base,
2323 						    &radeon_lvds_connector_funcs,
2324 						    connector_type,
2325 						    ddc);
2326 			drm_connector_helper_add(&radeon_connector->base, &radeon_lvds_connector_helper_funcs);
2327 			drm_object_attach_property(&radeon_connector->base.base,
2328 						      dev->mode_config.scaling_mode_property,
2329 						      DRM_MODE_SCALE_FULLSCREEN);
2330 			subpixel_order = SubPixelHorizontalRGB;
2331 			connector->interlace_allowed = false;
2332 			connector->doublescan_allowed = false;
2333 			break;
2334 		}
2335 	}
2336 
2337 	if (radeon_connector->hpd.hpd == RADEON_HPD_NONE) {
2338 		if (i2c_bus->valid) {
2339 			connector->polled = DRM_CONNECTOR_POLL_CONNECT |
2340 			                    DRM_CONNECTOR_POLL_DISCONNECT;
2341 		}
2342 	} else
2343 		connector->polled = DRM_CONNECTOR_POLL_HPD;
2344 
2345 	connector->display_info.subpixel_order = subpixel_order;
2346 	drm_connector_register(connector);
2347 
2348 	if (has_aux)
2349 		radeon_dp_aux_init(radeon_connector);
2350 
2351 	return;
2352 
2353 failed:
2354 	drm_connector_cleanup(connector);
2355 	kfree(connector);
2356 }
2357 
2358 void
radeon_add_legacy_connector(struct drm_device * dev,uint32_t connector_id,uint32_t supported_device,int connector_type,struct radeon_i2c_bus_rec * i2c_bus,uint16_t connector_object_id,struct radeon_hpd * hpd)2359 radeon_add_legacy_connector(struct drm_device *dev,
2360 			    uint32_t connector_id,
2361 			    uint32_t supported_device,
2362 			    int connector_type,
2363 			    struct radeon_i2c_bus_rec *i2c_bus,
2364 			    uint16_t connector_object_id,
2365 			    struct radeon_hpd *hpd)
2366 {
2367 	struct radeon_device *rdev = dev->dev_private;
2368 	struct drm_connector *connector;
2369 	struct radeon_connector *radeon_connector;
2370 	struct i2c_adapter *ddc = NULL;
2371 	uint32_t subpixel_order = SubPixelNone;
2372 
2373 	if (connector_type == DRM_MODE_CONNECTOR_Unknown)
2374 		return;
2375 
2376 	/* if the user selected tv=0 don't try and add the connector */
2377 	if (((connector_type == DRM_MODE_CONNECTOR_SVIDEO) ||
2378 	     (connector_type == DRM_MODE_CONNECTOR_Composite) ||
2379 	     (connector_type == DRM_MODE_CONNECTOR_9PinDIN)) &&
2380 	    (radeon_tv == 0))
2381 		return;
2382 
2383 	/* see if we already added it */
2384 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2385 		radeon_connector = to_radeon_connector(connector);
2386 		if (radeon_connector->connector_id == connector_id) {
2387 			radeon_connector->devices |= supported_device;
2388 			return;
2389 		}
2390 	}
2391 
2392 	radeon_connector = kzalloc(sizeof(struct radeon_connector), GFP_KERNEL);
2393 	if (!radeon_connector)
2394 		return;
2395 
2396 	connector = &radeon_connector->base;
2397 
2398 	radeon_connector->connector_id = connector_id;
2399 	radeon_connector->devices = supported_device;
2400 	radeon_connector->connector_object_id = connector_object_id;
2401 	radeon_connector->hpd = *hpd;
2402 
2403 	switch (connector_type) {
2404 	case DRM_MODE_CONNECTOR_VGA:
2405 		if (i2c_bus->valid) {
2406 			radeon_connector->ddc_bus = radeon_i2c_lookup(rdev, i2c_bus);
2407 			if (!radeon_connector->ddc_bus)
2408 				DRM_ERROR("VGA: Failed to assign ddc bus! Check dmesg for i2c errors.\n");
2409 			else
2410 				ddc = &radeon_connector->ddc_bus->adapter;
2411 		}
2412 		drm_connector_init_with_ddc(dev, &radeon_connector->base,
2413 					    &radeon_vga_connector_funcs,
2414 					    connector_type,
2415 					    ddc);
2416 		drm_connector_helper_add(&radeon_connector->base, &radeon_vga_connector_helper_funcs);
2417 		radeon_connector->dac_load_detect = true;
2418 		drm_object_attach_property(&radeon_connector->base.base,
2419 					      rdev->mode_info.load_detect_property,
2420 					      1);
2421 		/* no HPD on analog connectors */
2422 		radeon_connector->hpd.hpd = RADEON_HPD_NONE;
2423 		connector->interlace_allowed = true;
2424 		connector->doublescan_allowed = true;
2425 		break;
2426 	case DRM_MODE_CONNECTOR_DVIA:
2427 		if (i2c_bus->valid) {
2428 			radeon_connector->ddc_bus = radeon_i2c_lookup(rdev, i2c_bus);
2429 			if (!radeon_connector->ddc_bus)
2430 				DRM_ERROR("DVIA: Failed to assign ddc bus! Check dmesg for i2c errors.\n");
2431 			else
2432 				ddc = &radeon_connector->ddc_bus->adapter;
2433 		}
2434 		drm_connector_init_with_ddc(dev, &radeon_connector->base,
2435 					    &radeon_vga_connector_funcs,
2436 					    connector_type,
2437 					    ddc);
2438 		drm_connector_helper_add(&radeon_connector->base, &radeon_vga_connector_helper_funcs);
2439 		radeon_connector->dac_load_detect = true;
2440 		drm_object_attach_property(&radeon_connector->base.base,
2441 					      rdev->mode_info.load_detect_property,
2442 					      1);
2443 		/* no HPD on analog connectors */
2444 		radeon_connector->hpd.hpd = RADEON_HPD_NONE;
2445 		connector->interlace_allowed = true;
2446 		connector->doublescan_allowed = true;
2447 		break;
2448 	case DRM_MODE_CONNECTOR_DVII:
2449 	case DRM_MODE_CONNECTOR_DVID:
2450 		if (i2c_bus->valid) {
2451 			radeon_connector->ddc_bus = radeon_i2c_lookup(rdev, i2c_bus);
2452 			if (!radeon_connector->ddc_bus)
2453 				DRM_ERROR("DVI: Failed to assign ddc bus! Check dmesg for i2c errors.\n");
2454 			else
2455 				ddc = &radeon_connector->ddc_bus->adapter;
2456 		}
2457 		drm_connector_init_with_ddc(dev, &radeon_connector->base,
2458 					    &radeon_dvi_connector_funcs,
2459 					    connector_type,
2460 					    ddc);
2461 		drm_connector_helper_add(&radeon_connector->base, &radeon_dvi_connector_helper_funcs);
2462 		if (connector_type == DRM_MODE_CONNECTOR_DVII) {
2463 			radeon_connector->dac_load_detect = true;
2464 			drm_object_attach_property(&radeon_connector->base.base,
2465 						      rdev->mode_info.load_detect_property,
2466 						      1);
2467 		}
2468 		subpixel_order = SubPixelHorizontalRGB;
2469 		connector->interlace_allowed = true;
2470 		if (connector_type == DRM_MODE_CONNECTOR_DVII)
2471 			connector->doublescan_allowed = true;
2472 		else
2473 			connector->doublescan_allowed = false;
2474 		break;
2475 	case DRM_MODE_CONNECTOR_SVIDEO:
2476 	case DRM_MODE_CONNECTOR_Composite:
2477 	case DRM_MODE_CONNECTOR_9PinDIN:
2478 		drm_connector_init_with_ddc(dev, &radeon_connector->base,
2479 					    &radeon_tv_connector_funcs,
2480 					    connector_type,
2481 					    ddc);
2482 		drm_connector_helper_add(&radeon_connector->base, &radeon_tv_connector_helper_funcs);
2483 		radeon_connector->dac_load_detect = true;
2484 		/* RS400,RC410,RS480 chipset seems to report a lot
2485 		 * of false positive on load detect, we haven't yet
2486 		 * found a way to make load detect reliable on those
2487 		 * chipset, thus just disable it for TV.
2488 		 */
2489 		if (rdev->family == CHIP_RS400 || rdev->family == CHIP_RS480)
2490 			radeon_connector->dac_load_detect = false;
2491 		drm_object_attach_property(&radeon_connector->base.base,
2492 					      rdev->mode_info.load_detect_property,
2493 					      radeon_connector->dac_load_detect);
2494 		drm_object_attach_property(&radeon_connector->base.base,
2495 					      rdev->mode_info.tv_std_property,
2496 					      radeon_combios_get_tv_info(rdev));
2497 		/* no HPD on analog connectors */
2498 		radeon_connector->hpd.hpd = RADEON_HPD_NONE;
2499 		connector->interlace_allowed = false;
2500 		connector->doublescan_allowed = false;
2501 		break;
2502 	case DRM_MODE_CONNECTOR_LVDS:
2503 		if (i2c_bus->valid) {
2504 			radeon_connector->ddc_bus = radeon_i2c_lookup(rdev, i2c_bus);
2505 			if (!radeon_connector->ddc_bus)
2506 				DRM_ERROR("LVDS: Failed to assign ddc bus! Check dmesg for i2c errors.\n");
2507 			else
2508 				ddc = &radeon_connector->ddc_bus->adapter;
2509 		}
2510 		drm_connector_init_with_ddc(dev, &radeon_connector->base,
2511 					    &radeon_lvds_connector_funcs,
2512 					    connector_type,
2513 					    ddc);
2514 		drm_connector_helper_add(&radeon_connector->base, &radeon_lvds_connector_helper_funcs);
2515 		drm_object_attach_property(&radeon_connector->base.base,
2516 					      dev->mode_config.scaling_mode_property,
2517 					      DRM_MODE_SCALE_FULLSCREEN);
2518 		subpixel_order = SubPixelHorizontalRGB;
2519 		connector->interlace_allowed = false;
2520 		connector->doublescan_allowed = false;
2521 		break;
2522 	}
2523 
2524 	if (radeon_connector->hpd.hpd == RADEON_HPD_NONE) {
2525 		if (i2c_bus->valid) {
2526 			connector->polled = DRM_CONNECTOR_POLL_CONNECT |
2527 			                    DRM_CONNECTOR_POLL_DISCONNECT;
2528 		}
2529 	} else
2530 		connector->polled = DRM_CONNECTOR_POLL_HPD;
2531 
2532 	connector->display_info.subpixel_order = subpixel_order;
2533 	drm_connector_register(connector);
2534 }
2535