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