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