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