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