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 "drmP.h"
27 #include "drm_edid.h"
28 #include "drm_crtc_helper.h"
29 #include "drm_fb_helper.h"
30 #include "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 	if (radeon_connector->hpd.hpd != RADEON_HPD_NONE)
50 		radeon_hpd_set_polarity(rdev, radeon_connector->hpd.hpd);
51 
52 	if ((connector->connector_type == DRM_MODE_CONNECTOR_DisplayPort) ||
53 	    (connector->connector_type == DRM_MODE_CONNECTOR_eDP)) {
54 		if ((radeon_dp_getsinktype(radeon_connector) == CONNECTOR_OBJECT_ID_DISPLAYPORT) ||
55 		    (radeon_dp_getsinktype(radeon_connector) == CONNECTOR_OBJECT_ID_eDP)) {
56 			if (radeon_dp_needs_link_train(radeon_connector)) {
57 				if (connector->encoder)
58 					dp_link_train(connector->encoder, connector);
59 			}
60 		}
61 	}
62 
63 }
64 
65 static void radeon_property_change_mode(struct drm_encoder *encoder)
66 {
67 	struct drm_crtc *crtc = encoder->crtc;
68 
69 	if (crtc && crtc->enabled) {
70 		drm_crtc_helper_set_mode(crtc, &crtc->mode,
71 					 crtc->x, crtc->y, crtc->fb);
72 	}
73 }
74 static void
75 radeon_connector_update_scratch_regs(struct drm_connector *connector, enum drm_connector_status status)
76 {
77 	struct drm_device *dev = connector->dev;
78 	struct radeon_device *rdev = dev->dev_private;
79 	struct drm_encoder *best_encoder = NULL;
80 	struct drm_encoder *encoder = NULL;
81 	struct drm_connector_helper_funcs *connector_funcs = connector->helper_private;
82 	struct drm_mode_object *obj;
83 	bool connected;
84 	int i;
85 
86 	best_encoder = connector_funcs->best_encoder(connector);
87 
88 	for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
89 		if (connector->encoder_ids[i] == 0)
90 			break;
91 
92 		obj = drm_mode_object_find(connector->dev,
93 					   connector->encoder_ids[i],
94 					   DRM_MODE_OBJECT_ENCODER);
95 		if (!obj)
96 			continue;
97 
98 		encoder = obj_to_encoder(obj);
99 
100 		if ((encoder == best_encoder) && (status == connector_status_connected))
101 			connected = true;
102 		else
103 			connected = false;
104 
105 		if (rdev->is_atom_bios)
106 			radeon_atombios_connected_scratch_regs(connector, encoder, connected);
107 		else
108 			radeon_combios_connected_scratch_regs(connector, encoder, connected);
109 
110 	}
111 }
112 
113 struct drm_encoder *radeon_find_encoder(struct drm_connector *connector, int encoder_type)
114 {
115 	struct drm_mode_object *obj;
116 	struct drm_encoder *encoder;
117 	int i;
118 
119 	for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
120 		if (connector->encoder_ids[i] == 0)
121 			break;
122 
123 		obj = drm_mode_object_find(connector->dev, connector->encoder_ids[i], DRM_MODE_OBJECT_ENCODER);
124 		if (!obj)
125 			continue;
126 
127 		encoder = obj_to_encoder(obj);
128 		if (encoder->encoder_type == encoder_type)
129 			return encoder;
130 	}
131 	return NULL;
132 }
133 
134 struct drm_encoder *radeon_best_single_encoder(struct drm_connector *connector)
135 {
136 	int enc_id = connector->encoder_ids[0];
137 	struct drm_mode_object *obj;
138 	struct drm_encoder *encoder;
139 
140 	/* pick the encoder ids */
141 	if (enc_id) {
142 		obj = drm_mode_object_find(connector->dev, enc_id, DRM_MODE_OBJECT_ENCODER);
143 		if (!obj)
144 			return NULL;
145 		encoder = obj_to_encoder(obj);
146 		return encoder;
147 	}
148 	return NULL;
149 }
150 
151 /*
152  * radeon_connector_analog_encoder_conflict_solve
153  * - search for other connectors sharing this encoder
154  *   if priority is true, then set them disconnected if this is connected
155  *   if priority is false, set us disconnected if they are connected
156  */
157 static enum drm_connector_status
158 radeon_connector_analog_encoder_conflict_solve(struct drm_connector *connector,
159 					       struct drm_encoder *encoder,
160 					       enum drm_connector_status current_status,
161 					       bool priority)
162 {
163 	struct drm_device *dev = connector->dev;
164 	struct drm_connector *conflict;
165 	struct radeon_connector *radeon_conflict;
166 	int i;
167 
168 	list_for_each_entry(conflict, &dev->mode_config.connector_list, head) {
169 		if (conflict == connector)
170 			continue;
171 
172 		radeon_conflict = to_radeon_connector(conflict);
173 		for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
174 			if (conflict->encoder_ids[i] == 0)
175 				break;
176 
177 			/* if the IDs match */
178 			if (conflict->encoder_ids[i] == encoder->base.id) {
179 				if (conflict->status != connector_status_connected)
180 					continue;
181 
182 				if (radeon_conflict->use_digital)
183 					continue;
184 
185 				if (priority == true) {
186 					DRM_DEBUG_KMS("1: conflicting encoders switching off %s\n", drm_get_connector_name(conflict));
187 					DRM_DEBUG_KMS("in favor of %s\n", drm_get_connector_name(connector));
188 					conflict->status = connector_status_disconnected;
189 					radeon_connector_update_scratch_regs(conflict, connector_status_disconnected);
190 				} else {
191 					DRM_DEBUG_KMS("2: conflicting encoders switching off %s\n", drm_get_connector_name(connector));
192 					DRM_DEBUG_KMS("in favor of %s\n", drm_get_connector_name(conflict));
193 					current_status = connector_status_disconnected;
194 				}
195 				break;
196 			}
197 		}
198 	}
199 	return current_status;
200 
201 }
202 
203 static struct drm_display_mode *radeon_fp_native_mode(struct drm_encoder *encoder)
204 {
205 	struct drm_device *dev = encoder->dev;
206 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
207 	struct drm_display_mode *mode = NULL;
208 	struct drm_display_mode *native_mode = &radeon_encoder->native_mode;
209 
210 	if (native_mode->hdisplay != 0 &&
211 	    native_mode->vdisplay != 0 &&
212 	    native_mode->clock != 0) {
213 		mode = drm_mode_duplicate(dev, native_mode);
214 		mode->type = DRM_MODE_TYPE_PREFERRED | DRM_MODE_TYPE_DRIVER;
215 		drm_mode_set_name(mode);
216 
217 		DRM_DEBUG_KMS("Adding native panel mode %s\n", mode->name);
218 	} else if (native_mode->hdisplay != 0 &&
219 		   native_mode->vdisplay != 0) {
220 		/* mac laptops without an edid */
221 		/* Note that this is not necessarily the exact panel mode,
222 		 * but an approximation based on the cvt formula.  For these
223 		 * systems we should ideally read the mode info out of the
224 		 * registers or add a mode table, but this works and is much
225 		 * simpler.
226 		 */
227 		mode = drm_cvt_mode(dev, native_mode->hdisplay, native_mode->vdisplay, 60, true, false, false);
228 		mode->type = DRM_MODE_TYPE_PREFERRED | DRM_MODE_TYPE_DRIVER;
229 		DRM_DEBUG_KMS("Adding cvt approximation of native panel mode %s\n", mode->name);
230 	}
231 	return mode;
232 }
233 
234 static void radeon_add_common_modes(struct drm_encoder *encoder, struct drm_connector *connector)
235 {
236 	struct drm_device *dev = encoder->dev;
237 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
238 	struct drm_display_mode *mode = NULL;
239 	struct drm_display_mode *native_mode = &radeon_encoder->native_mode;
240 	int i;
241 	struct mode_size {
242 		int w;
243 		int h;
244 	} common_modes[17] = {
245 		{ 640,  480},
246 		{ 720,  480},
247 		{ 800,  600},
248 		{ 848,  480},
249 		{1024,  768},
250 		{1152,  768},
251 		{1280,  720},
252 		{1280,  800},
253 		{1280,  854},
254 		{1280,  960},
255 		{1280, 1024},
256 		{1440,  900},
257 		{1400, 1050},
258 		{1680, 1050},
259 		{1600, 1200},
260 		{1920, 1080},
261 		{1920, 1200}
262 	};
263 
264 	for (i = 0; i < 17; i++) {
265 		if (radeon_encoder->devices & (ATOM_DEVICE_TV_SUPPORT)) {
266 			if (common_modes[i].w > 1024 ||
267 			    common_modes[i].h > 768)
268 				continue;
269 		}
270 		if (radeon_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT)) {
271 			if (common_modes[i].w > native_mode->hdisplay ||
272 			    common_modes[i].h > native_mode->vdisplay ||
273 			    (common_modes[i].w == native_mode->hdisplay &&
274 			     common_modes[i].h == native_mode->vdisplay))
275 				continue;
276 		}
277 		if (common_modes[i].w < 320 || common_modes[i].h < 200)
278 			continue;
279 
280 		mode = drm_cvt_mode(dev, common_modes[i].w, common_modes[i].h, 60, false, false, false);
281 		drm_mode_probed_add(connector, mode);
282 	}
283 }
284 
285 int radeon_connector_set_property(struct drm_connector *connector, struct drm_property *property,
286 				  uint64_t val)
287 {
288 	struct drm_device *dev = connector->dev;
289 	struct radeon_device *rdev = dev->dev_private;
290 	struct drm_encoder *encoder;
291 	struct radeon_encoder *radeon_encoder;
292 
293 	if (property == rdev->mode_info.coherent_mode_property) {
294 		struct radeon_encoder_atom_dig *dig;
295 		bool new_coherent_mode;
296 
297 		/* need to find digital encoder on connector */
298 		encoder = radeon_find_encoder(connector, DRM_MODE_ENCODER_TMDS);
299 		if (!encoder)
300 			return 0;
301 
302 		radeon_encoder = to_radeon_encoder(encoder);
303 
304 		if (!radeon_encoder->enc_priv)
305 			return 0;
306 
307 		dig = radeon_encoder->enc_priv;
308 		new_coherent_mode = val ? true : false;
309 		if (dig->coherent_mode != new_coherent_mode) {
310 			dig->coherent_mode = new_coherent_mode;
311 			radeon_property_change_mode(&radeon_encoder->base);
312 		}
313 	}
314 
315 	if (property == rdev->mode_info.underscan_property) {
316 		/* need to find digital encoder on connector */
317 		encoder = radeon_find_encoder(connector, DRM_MODE_ENCODER_TMDS);
318 		if (!encoder)
319 			return 0;
320 
321 		radeon_encoder = to_radeon_encoder(encoder);
322 
323 		if (radeon_encoder->underscan_type != val) {
324 			radeon_encoder->underscan_type = val;
325 			radeon_property_change_mode(&radeon_encoder->base);
326 		}
327 	}
328 
329 	if (property == rdev->mode_info.underscan_hborder_property) {
330 		/* need to find digital encoder on connector */
331 		encoder = radeon_find_encoder(connector, DRM_MODE_ENCODER_TMDS);
332 		if (!encoder)
333 			return 0;
334 
335 		radeon_encoder = to_radeon_encoder(encoder);
336 
337 		if (radeon_encoder->underscan_hborder != val) {
338 			radeon_encoder->underscan_hborder = val;
339 			radeon_property_change_mode(&radeon_encoder->base);
340 		}
341 	}
342 
343 	if (property == rdev->mode_info.underscan_vborder_property) {
344 		/* need to find digital encoder on connector */
345 		encoder = radeon_find_encoder(connector, DRM_MODE_ENCODER_TMDS);
346 		if (!encoder)
347 			return 0;
348 
349 		radeon_encoder = to_radeon_encoder(encoder);
350 
351 		if (radeon_encoder->underscan_vborder != val) {
352 			radeon_encoder->underscan_vborder = val;
353 			radeon_property_change_mode(&radeon_encoder->base);
354 		}
355 	}
356 
357 	if (property == rdev->mode_info.tv_std_property) {
358 		encoder = radeon_find_encoder(connector, DRM_MODE_ENCODER_TVDAC);
359 		if (!encoder) {
360 			encoder = radeon_find_encoder(connector, DRM_MODE_ENCODER_DAC);
361 		}
362 
363 		if (!encoder)
364 			return 0;
365 
366 		radeon_encoder = to_radeon_encoder(encoder);
367 		if (!radeon_encoder->enc_priv)
368 			return 0;
369 		if (ASIC_IS_AVIVO(rdev) || radeon_r4xx_atom) {
370 			struct radeon_encoder_atom_dac *dac_int;
371 			dac_int = radeon_encoder->enc_priv;
372 			dac_int->tv_std = val;
373 		} else {
374 			struct radeon_encoder_tv_dac *dac_int;
375 			dac_int = radeon_encoder->enc_priv;
376 			dac_int->tv_std = val;
377 		}
378 		radeon_property_change_mode(&radeon_encoder->base);
379 	}
380 
381 	if (property == rdev->mode_info.load_detect_property) {
382 		struct radeon_connector *radeon_connector =
383 			to_radeon_connector(connector);
384 
385 		if (val == 0)
386 			radeon_connector->dac_load_detect = false;
387 		else
388 			radeon_connector->dac_load_detect = true;
389 	}
390 
391 	if (property == rdev->mode_info.tmds_pll_property) {
392 		struct radeon_encoder_int_tmds *tmds = NULL;
393 		bool ret = false;
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 		tmds = radeon_encoder->enc_priv;
402 		if (!tmds)
403 			return 0;
404 
405 		if (val == 0) {
406 			if (rdev->is_atom_bios)
407 				ret = radeon_atombios_get_tmds_info(radeon_encoder, tmds);
408 			else
409 				ret = radeon_legacy_get_tmds_info_from_combios(radeon_encoder, tmds);
410 		}
411 		if (val == 1 || ret == false) {
412 			radeon_legacy_get_tmds_info_from_table(radeon_encoder, tmds);
413 		}
414 		radeon_property_change_mode(&radeon_encoder->base);
415 	}
416 
417 	return 0;
418 }
419 
420 static void radeon_fixup_lvds_native_mode(struct drm_encoder *encoder,
421 					  struct drm_connector *connector)
422 {
423 	struct radeon_encoder *radeon_encoder =	to_radeon_encoder(encoder);
424 	struct drm_display_mode *native_mode = &radeon_encoder->native_mode;
425 
426 	/* Try to get native mode details from EDID if necessary */
427 	if (!native_mode->clock) {
428 		struct drm_display_mode *t, *mode;
429 
430 		list_for_each_entry_safe(mode, t, &connector->probed_modes, head) {
431 			if (mode->hdisplay == native_mode->hdisplay &&
432 			    mode->vdisplay == native_mode->vdisplay) {
433 				*native_mode = *mode;
434 				drm_mode_set_crtcinfo(native_mode, CRTC_INTERLACE_HALVE_V);
435 				DRM_DEBUG_KMS("Determined LVDS native mode details from EDID\n");
436 				break;
437 			}
438 		}
439 	}
440 	if (!native_mode->clock) {
441 		DRM_DEBUG_KMS("No LVDS native mode details, disabling RMX\n");
442 		radeon_encoder->rmx_type = RMX_OFF;
443 	}
444 }
445 
446 static int radeon_lvds_get_modes(struct drm_connector *connector)
447 {
448 	struct radeon_connector *radeon_connector = to_radeon_connector(connector);
449 	struct drm_encoder *encoder;
450 	int ret = 0;
451 	struct drm_display_mode *mode;
452 
453 	if (radeon_connector->ddc_bus) {
454 		ret = radeon_ddc_get_modes(radeon_connector);
455 		if (ret > 0) {
456 			encoder = radeon_best_single_encoder(connector);
457 			if (encoder) {
458 				radeon_fixup_lvds_native_mode(encoder, connector);
459 				/* add scaled modes */
460 				radeon_add_common_modes(encoder, connector);
461 			}
462 			return ret;
463 		}
464 	}
465 
466 	encoder = radeon_best_single_encoder(connector);
467 	if (!encoder)
468 		return 0;
469 
470 	/* we have no EDID modes */
471 	mode = radeon_fp_native_mode(encoder);
472 	if (mode) {
473 		ret = 1;
474 		drm_mode_probed_add(connector, mode);
475 		/* add the width/height from vbios tables if available */
476 		connector->display_info.width_mm = mode->width_mm;
477 		connector->display_info.height_mm = mode->height_mm;
478 		/* add scaled modes */
479 		radeon_add_common_modes(encoder, connector);
480 	}
481 
482 	return ret;
483 }
484 
485 static int radeon_lvds_mode_valid(struct drm_connector *connector,
486 				  struct drm_display_mode *mode)
487 {
488 	struct drm_encoder *encoder = radeon_best_single_encoder(connector);
489 
490 	if ((mode->hdisplay < 320) || (mode->vdisplay < 240))
491 		return MODE_PANEL;
492 
493 	if (encoder) {
494 		struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
495 		struct drm_display_mode *native_mode = &radeon_encoder->native_mode;
496 
497 		/* AVIVO hardware supports downscaling modes larger than the panel
498 		 * to the panel size, but I'm not sure this is desirable.
499 		 */
500 		if ((mode->hdisplay > native_mode->hdisplay) ||
501 		    (mode->vdisplay > native_mode->vdisplay))
502 			return MODE_PANEL;
503 
504 		/* if scaling is disabled, block non-native modes */
505 		if (radeon_encoder->rmx_type == RMX_OFF) {
506 			if ((mode->hdisplay != native_mode->hdisplay) ||
507 			    (mode->vdisplay != native_mode->vdisplay))
508 				return MODE_PANEL;
509 		}
510 	}
511 
512 	return MODE_OK;
513 }
514 
515 static enum drm_connector_status
516 radeon_lvds_detect(struct drm_connector *connector, bool force)
517 {
518 	struct radeon_connector *radeon_connector = to_radeon_connector(connector);
519 	struct drm_encoder *encoder = radeon_best_single_encoder(connector);
520 	enum drm_connector_status ret = connector_status_disconnected;
521 
522 	if (encoder) {
523 		struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
524 		struct drm_display_mode *native_mode = &radeon_encoder->native_mode;
525 
526 		/* check if panel is valid */
527 		if (native_mode->hdisplay >= 320 && native_mode->vdisplay >= 240)
528 			ret = connector_status_connected;
529 
530 	}
531 
532 	/* check for edid as well */
533 	if (radeon_connector->edid)
534 		ret = connector_status_connected;
535 	else {
536 		if (radeon_connector->ddc_bus) {
537 			radeon_connector->edid = drm_get_edid(&radeon_connector->base,
538 							      &radeon_connector->ddc_bus->adapter);
539 			if (radeon_connector->edid)
540 				ret = connector_status_connected;
541 		}
542 	}
543 	/* check acpi lid status ??? */
544 
545 	radeon_connector_update_scratch_regs(connector, ret);
546 	return ret;
547 }
548 
549 static void radeon_connector_destroy(struct drm_connector *connector)
550 {
551 	struct radeon_connector *radeon_connector = to_radeon_connector(connector);
552 
553 	if (radeon_connector->edid)
554 		kfree(radeon_connector->edid);
555 	kfree(radeon_connector->con_priv);
556 	drm_sysfs_connector_remove(connector);
557 	drm_connector_cleanup(connector);
558 	kfree(connector);
559 }
560 
561 static int radeon_lvds_set_property(struct drm_connector *connector,
562 				    struct drm_property *property,
563 				    uint64_t value)
564 {
565 	struct drm_device *dev = connector->dev;
566 	struct radeon_encoder *radeon_encoder;
567 	enum radeon_rmx_type rmx_type;
568 
569 	DRM_DEBUG_KMS("\n");
570 	if (property != dev->mode_config.scaling_mode_property)
571 		return 0;
572 
573 	if (connector->encoder)
574 		radeon_encoder = to_radeon_encoder(connector->encoder);
575 	else {
576 		struct drm_connector_helper_funcs *connector_funcs = connector->helper_private;
577 		radeon_encoder = to_radeon_encoder(connector_funcs->best_encoder(connector));
578 	}
579 
580 	switch (value) {
581 	case DRM_MODE_SCALE_NONE: rmx_type = RMX_OFF; break;
582 	case DRM_MODE_SCALE_CENTER: rmx_type = RMX_CENTER; break;
583 	case DRM_MODE_SCALE_ASPECT: rmx_type = RMX_ASPECT; break;
584 	default:
585 	case DRM_MODE_SCALE_FULLSCREEN: rmx_type = RMX_FULL; break;
586 	}
587 	if (radeon_encoder->rmx_type == rmx_type)
588 		return 0;
589 
590 	radeon_encoder->rmx_type = rmx_type;
591 
592 	radeon_property_change_mode(&radeon_encoder->base);
593 	return 0;
594 }
595 
596 
597 struct drm_connector_helper_funcs radeon_lvds_connector_helper_funcs = {
598 	.get_modes = radeon_lvds_get_modes,
599 	.mode_valid = radeon_lvds_mode_valid,
600 	.best_encoder = radeon_best_single_encoder,
601 };
602 
603 struct drm_connector_funcs radeon_lvds_connector_funcs = {
604 	.dpms = drm_helper_connector_dpms,
605 	.detect = radeon_lvds_detect,
606 	.fill_modes = drm_helper_probe_single_connector_modes,
607 	.destroy = radeon_connector_destroy,
608 	.set_property = radeon_lvds_set_property,
609 };
610 
611 static int radeon_vga_get_modes(struct drm_connector *connector)
612 {
613 	struct radeon_connector *radeon_connector = to_radeon_connector(connector);
614 	int ret;
615 
616 	ret = radeon_ddc_get_modes(radeon_connector);
617 
618 	return ret;
619 }
620 
621 static int radeon_vga_mode_valid(struct drm_connector *connector,
622 				  struct drm_display_mode *mode)
623 {
624 	/* XXX check mode bandwidth */
625 	/* XXX verify against max DAC output frequency */
626 	return MODE_OK;
627 }
628 
629 static enum drm_connector_status
630 radeon_vga_detect(struct drm_connector *connector, bool force)
631 {
632 	struct radeon_connector *radeon_connector = to_radeon_connector(connector);
633 	struct drm_encoder *encoder;
634 	struct drm_encoder_helper_funcs *encoder_funcs;
635 	bool dret = false;
636 	enum drm_connector_status ret = connector_status_disconnected;
637 
638 	encoder = radeon_best_single_encoder(connector);
639 	if (!encoder)
640 		ret = connector_status_disconnected;
641 
642 	if (radeon_connector->ddc_bus)
643 		dret = radeon_ddc_probe(radeon_connector);
644 	if (dret) {
645 		if (radeon_connector->edid) {
646 			kfree(radeon_connector->edid);
647 			radeon_connector->edid = NULL;
648 		}
649 		radeon_connector->edid = drm_get_edid(&radeon_connector->base, &radeon_connector->ddc_bus->adapter);
650 
651 		if (!radeon_connector->edid) {
652 			DRM_ERROR("%s: probed a monitor but no|invalid EDID\n",
653 					drm_get_connector_name(connector));
654 			ret = connector_status_connected;
655 		} else {
656 			radeon_connector->use_digital = !!(radeon_connector->edid->input & DRM_EDID_INPUT_DIGITAL);
657 
658 			/* some oems have boards with separate digital and analog connectors
659 			 * with a shared ddc line (often vga + hdmi)
660 			 */
661 			if (radeon_connector->use_digital && radeon_connector->shared_ddc) {
662 				kfree(radeon_connector->edid);
663 				radeon_connector->edid = NULL;
664 				ret = connector_status_disconnected;
665 			} else
666 				ret = connector_status_connected;
667 		}
668 	} else {
669 
670 		/* if we aren't forcing don't do destructive polling */
671 		if (!force)
672 			return connector->status;
673 
674 		if (radeon_connector->dac_load_detect && encoder) {
675 			encoder_funcs = encoder->helper_private;
676 			ret = encoder_funcs->detect(encoder, connector);
677 		}
678 	}
679 
680 	if (ret == connector_status_connected)
681 		ret = radeon_connector_analog_encoder_conflict_solve(connector, encoder, ret, true);
682 	radeon_connector_update_scratch_regs(connector, ret);
683 	return ret;
684 }
685 
686 struct drm_connector_helper_funcs radeon_vga_connector_helper_funcs = {
687 	.get_modes = radeon_vga_get_modes,
688 	.mode_valid = radeon_vga_mode_valid,
689 	.best_encoder = radeon_best_single_encoder,
690 };
691 
692 struct drm_connector_funcs radeon_vga_connector_funcs = {
693 	.dpms = drm_helper_connector_dpms,
694 	.detect = radeon_vga_detect,
695 	.fill_modes = drm_helper_probe_single_connector_modes,
696 	.destroy = radeon_connector_destroy,
697 	.set_property = radeon_connector_set_property,
698 };
699 
700 static int radeon_tv_get_modes(struct drm_connector *connector)
701 {
702 	struct drm_device *dev = connector->dev;
703 	struct radeon_device *rdev = dev->dev_private;
704 	struct drm_display_mode *tv_mode;
705 	struct drm_encoder *encoder;
706 
707 	encoder = radeon_best_single_encoder(connector);
708 	if (!encoder)
709 		return 0;
710 
711 	/* avivo chips can scale any mode */
712 	if (rdev->family >= CHIP_RS600)
713 		/* add scaled modes */
714 		radeon_add_common_modes(encoder, connector);
715 	else {
716 		/* only 800x600 is supported right now on pre-avivo chips */
717 		tv_mode = drm_cvt_mode(dev, 800, 600, 60, false, false, false);
718 		tv_mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
719 		drm_mode_probed_add(connector, tv_mode);
720 	}
721 	return 1;
722 }
723 
724 static int radeon_tv_mode_valid(struct drm_connector *connector,
725 				struct drm_display_mode *mode)
726 {
727 	if ((mode->hdisplay > 1024) || (mode->vdisplay > 768))
728 		return MODE_CLOCK_RANGE;
729 	return MODE_OK;
730 }
731 
732 static enum drm_connector_status
733 radeon_tv_detect(struct drm_connector *connector, bool force)
734 {
735 	struct drm_encoder *encoder;
736 	struct drm_encoder_helper_funcs *encoder_funcs;
737 	struct radeon_connector *radeon_connector = to_radeon_connector(connector);
738 	enum drm_connector_status ret = connector_status_disconnected;
739 
740 	if (!radeon_connector->dac_load_detect)
741 		return ret;
742 
743 	encoder = radeon_best_single_encoder(connector);
744 	if (!encoder)
745 		ret = connector_status_disconnected;
746 	else {
747 		encoder_funcs = encoder->helper_private;
748 		ret = encoder_funcs->detect(encoder, connector);
749 	}
750 	if (ret == connector_status_connected)
751 		ret = radeon_connector_analog_encoder_conflict_solve(connector, encoder, ret, false);
752 	radeon_connector_update_scratch_regs(connector, ret);
753 	return ret;
754 }
755 
756 struct drm_connector_helper_funcs radeon_tv_connector_helper_funcs = {
757 	.get_modes = radeon_tv_get_modes,
758 	.mode_valid = radeon_tv_mode_valid,
759 	.best_encoder = radeon_best_single_encoder,
760 };
761 
762 struct drm_connector_funcs radeon_tv_connector_funcs = {
763 	.dpms = drm_helper_connector_dpms,
764 	.detect = radeon_tv_detect,
765 	.fill_modes = drm_helper_probe_single_connector_modes,
766 	.destroy = radeon_connector_destroy,
767 	.set_property = radeon_connector_set_property,
768 };
769 
770 static int radeon_dvi_get_modes(struct drm_connector *connector)
771 {
772 	struct radeon_connector *radeon_connector = to_radeon_connector(connector);
773 	int ret;
774 
775 	ret = radeon_ddc_get_modes(radeon_connector);
776 	return ret;
777 }
778 
779 /*
780  * DVI is complicated
781  * Do a DDC probe, if DDC probe passes, get the full EDID so
782  * we can do analog/digital monitor detection at this point.
783  * If the monitor is an analog monitor or we got no DDC,
784  * we need to find the DAC encoder object for this connector.
785  * If we got no DDC, we do load detection on the DAC encoder object.
786  * If we got analog DDC or load detection passes on the DAC encoder
787  * we have to check if this analog encoder is shared with anyone else (TV)
788  * if its shared we have to set the other connector to disconnected.
789  */
790 static enum drm_connector_status
791 radeon_dvi_detect(struct drm_connector *connector, bool force)
792 {
793 	struct radeon_connector *radeon_connector = to_radeon_connector(connector);
794 	struct drm_encoder *encoder = NULL;
795 	struct drm_encoder_helper_funcs *encoder_funcs;
796 	struct drm_mode_object *obj;
797 	int i;
798 	enum drm_connector_status ret = connector_status_disconnected;
799 	bool dret = false;
800 
801 	if (radeon_connector->ddc_bus)
802 		dret = radeon_ddc_probe(radeon_connector);
803 	if (dret) {
804 		if (radeon_connector->edid) {
805 			kfree(radeon_connector->edid);
806 			radeon_connector->edid = NULL;
807 		}
808 		radeon_connector->edid = drm_get_edid(&radeon_connector->base, &radeon_connector->ddc_bus->adapter);
809 
810 		if (!radeon_connector->edid) {
811 			DRM_ERROR("%s: probed a monitor but no|invalid EDID\n",
812 					drm_get_connector_name(connector));
813 		} else {
814 			radeon_connector->use_digital = !!(radeon_connector->edid->input & DRM_EDID_INPUT_DIGITAL);
815 
816 			/* some oems have boards with separate digital and analog connectors
817 			 * with a shared ddc line (often vga + hdmi)
818 			 */
819 			if ((!radeon_connector->use_digital) && radeon_connector->shared_ddc) {
820 				kfree(radeon_connector->edid);
821 				radeon_connector->edid = NULL;
822 				ret = connector_status_disconnected;
823 			} else
824 				ret = connector_status_connected;
825 
826 			/* This gets complicated.  We have boards with VGA + HDMI with a
827 			 * shared DDC line and we have boards with DVI-D + HDMI with a shared
828 			 * DDC line.  The latter is more complex because with DVI<->HDMI adapters
829 			 * you don't really know what's connected to which port as both are digital.
830 			 */
831 			if (radeon_connector->shared_ddc && (ret == connector_status_connected)) {
832 				struct drm_device *dev = connector->dev;
833 				struct radeon_device *rdev = dev->dev_private;
834 				struct drm_connector *list_connector;
835 				struct radeon_connector *list_radeon_connector;
836 				list_for_each_entry(list_connector, &dev->mode_config.connector_list, head) {
837 					if (connector == list_connector)
838 						continue;
839 					list_radeon_connector = to_radeon_connector(list_connector);
840 					if (list_radeon_connector->shared_ddc &&
841 					    (list_radeon_connector->ddc_bus->rec.i2c_id ==
842 					     radeon_connector->ddc_bus->rec.i2c_id)) {
843 						/* cases where both connectors are digital */
844 						if (list_connector->connector_type != DRM_MODE_CONNECTOR_VGA) {
845 							/* hpd is our only option in this case */
846 							if (!radeon_hpd_sense(rdev, radeon_connector->hpd.hpd)) {
847 								kfree(radeon_connector->edid);
848 								radeon_connector->edid = NULL;
849 								ret = connector_status_disconnected;
850 							}
851 						}
852 					}
853 				}
854 			}
855 		}
856 	}
857 
858 	if ((ret == connector_status_connected) && (radeon_connector->use_digital == true))
859 		goto out;
860 
861 	if (!force) {
862 		ret = connector->status;
863 		goto out;
864 	}
865 
866 	/* find analog encoder */
867 	if (radeon_connector->dac_load_detect) {
868 		for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
869 			if (connector->encoder_ids[i] == 0)
870 				break;
871 
872 			obj = drm_mode_object_find(connector->dev,
873 						   connector->encoder_ids[i],
874 						   DRM_MODE_OBJECT_ENCODER);
875 			if (!obj)
876 				continue;
877 
878 			encoder = obj_to_encoder(obj);
879 
880 			encoder_funcs = encoder->helper_private;
881 			if (encoder_funcs->detect) {
882 				if (ret != connector_status_connected) {
883 					ret = encoder_funcs->detect(encoder, connector);
884 					if (ret == connector_status_connected) {
885 						radeon_connector->use_digital = false;
886 					}
887 				}
888 				break;
889 			}
890 		}
891 	}
892 
893 	if ((ret == connector_status_connected) && (radeon_connector->use_digital == false) &&
894 	    encoder) {
895 		ret = radeon_connector_analog_encoder_conflict_solve(connector, encoder, ret, true);
896 	}
897 
898 out:
899 	/* updated in get modes as well since we need to know if it's analog or digital */
900 	radeon_connector_update_scratch_regs(connector, ret);
901 	return ret;
902 }
903 
904 /* okay need to be smart in here about which encoder to pick */
905 struct drm_encoder *radeon_dvi_encoder(struct drm_connector *connector)
906 {
907 	int enc_id = connector->encoder_ids[0];
908 	struct radeon_connector *radeon_connector = to_radeon_connector(connector);
909 	struct drm_mode_object *obj;
910 	struct drm_encoder *encoder;
911 	int i;
912 	for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
913 		if (connector->encoder_ids[i] == 0)
914 			break;
915 
916 		obj = drm_mode_object_find(connector->dev, connector->encoder_ids[i], DRM_MODE_OBJECT_ENCODER);
917 		if (!obj)
918 			continue;
919 
920 		encoder = obj_to_encoder(obj);
921 
922 		if (radeon_connector->use_digital == true) {
923 			if (encoder->encoder_type == DRM_MODE_ENCODER_TMDS)
924 				return encoder;
925 		} else {
926 			if (encoder->encoder_type == DRM_MODE_ENCODER_DAC ||
927 			    encoder->encoder_type == DRM_MODE_ENCODER_TVDAC)
928 				return encoder;
929 		}
930 	}
931 
932 	/* see if we have a default encoder  TODO */
933 
934 	/* then check use digitial */
935 	/* pick the first one */
936 	if (enc_id) {
937 		obj = drm_mode_object_find(connector->dev, enc_id, DRM_MODE_OBJECT_ENCODER);
938 		if (!obj)
939 			return NULL;
940 		encoder = obj_to_encoder(obj);
941 		return encoder;
942 	}
943 	return NULL;
944 }
945 
946 static void radeon_dvi_force(struct drm_connector *connector)
947 {
948 	struct radeon_connector *radeon_connector = to_radeon_connector(connector);
949 	if (connector->force == DRM_FORCE_ON)
950 		radeon_connector->use_digital = false;
951 	if (connector->force == DRM_FORCE_ON_DIGITAL)
952 		radeon_connector->use_digital = true;
953 }
954 
955 static int radeon_dvi_mode_valid(struct drm_connector *connector,
956 				  struct drm_display_mode *mode)
957 {
958 	struct drm_device *dev = connector->dev;
959 	struct radeon_device *rdev = dev->dev_private;
960 	struct radeon_connector *radeon_connector = to_radeon_connector(connector);
961 
962 	/* XXX check mode bandwidth */
963 
964 	/* clocks over 135 MHz have heat issues with DVI on RV100 */
965 	if (radeon_connector->use_digital &&
966 	    (rdev->family == CHIP_RV100) &&
967 	    (mode->clock > 135000))
968 		return MODE_CLOCK_HIGH;
969 
970 	if (radeon_connector->use_digital && (mode->clock > 165000)) {
971 		if ((radeon_connector->connector_object_id == CONNECTOR_OBJECT_ID_DUAL_LINK_DVI_I) ||
972 		    (radeon_connector->connector_object_id == CONNECTOR_OBJECT_ID_DUAL_LINK_DVI_D) ||
973 		    (radeon_connector->connector_object_id == CONNECTOR_OBJECT_ID_HDMI_TYPE_B))
974 			return MODE_OK;
975 		else
976 			return MODE_CLOCK_HIGH;
977 	}
978 	return MODE_OK;
979 }
980 
981 struct drm_connector_helper_funcs radeon_dvi_connector_helper_funcs = {
982 	.get_modes = radeon_dvi_get_modes,
983 	.mode_valid = radeon_dvi_mode_valid,
984 	.best_encoder = radeon_dvi_encoder,
985 };
986 
987 struct drm_connector_funcs radeon_dvi_connector_funcs = {
988 	.dpms = drm_helper_connector_dpms,
989 	.detect = radeon_dvi_detect,
990 	.fill_modes = drm_helper_probe_single_connector_modes,
991 	.set_property = radeon_connector_set_property,
992 	.destroy = radeon_connector_destroy,
993 	.force = radeon_dvi_force,
994 };
995 
996 static void radeon_dp_connector_destroy(struct drm_connector *connector)
997 {
998 	struct radeon_connector *radeon_connector = to_radeon_connector(connector);
999 	struct radeon_connector_atom_dig *radeon_dig_connector = radeon_connector->con_priv;
1000 
1001 	if (radeon_connector->edid)
1002 		kfree(radeon_connector->edid);
1003 	if (radeon_dig_connector->dp_i2c_bus)
1004 		radeon_i2c_destroy(radeon_dig_connector->dp_i2c_bus);
1005 	kfree(radeon_connector->con_priv);
1006 	drm_sysfs_connector_remove(connector);
1007 	drm_connector_cleanup(connector);
1008 	kfree(connector);
1009 }
1010 
1011 static int radeon_dp_get_modes(struct drm_connector *connector)
1012 {
1013 	struct radeon_connector *radeon_connector = to_radeon_connector(connector);
1014 	struct radeon_connector_atom_dig *radeon_dig_connector = radeon_connector->con_priv;
1015 	int ret;
1016 
1017 	if (connector->connector_type == DRM_MODE_CONNECTOR_eDP) {
1018 		if (!radeon_dig_connector->edp_on)
1019 			atombios_set_edp_panel_power(connector,
1020 						     ATOM_TRANSMITTER_ACTION_POWER_ON);
1021 	}
1022 	ret = radeon_ddc_get_modes(radeon_connector);
1023 	if (connector->connector_type == DRM_MODE_CONNECTOR_eDP) {
1024 		if (!radeon_dig_connector->edp_on)
1025 			atombios_set_edp_panel_power(connector,
1026 						     ATOM_TRANSMITTER_ACTION_POWER_OFF);
1027 	}
1028 
1029 	return ret;
1030 }
1031 
1032 static enum drm_connector_status
1033 radeon_dp_detect(struct drm_connector *connector, bool force)
1034 {
1035 	struct radeon_connector *radeon_connector = to_radeon_connector(connector);
1036 	enum drm_connector_status ret = connector_status_disconnected;
1037 	struct radeon_connector_atom_dig *radeon_dig_connector = radeon_connector->con_priv;
1038 
1039 	if (radeon_connector->edid) {
1040 		kfree(radeon_connector->edid);
1041 		radeon_connector->edid = NULL;
1042 	}
1043 
1044 	if (connector->connector_type == DRM_MODE_CONNECTOR_eDP) {
1045 		/* eDP is always DP */
1046 		radeon_dig_connector->dp_sink_type = CONNECTOR_OBJECT_ID_DISPLAYPORT;
1047 		if (!radeon_dig_connector->edp_on)
1048 			atombios_set_edp_panel_power(connector,
1049 						     ATOM_TRANSMITTER_ACTION_POWER_ON);
1050 		if (radeon_dp_getdpcd(radeon_connector))
1051 			ret = connector_status_connected;
1052 		if (!radeon_dig_connector->edp_on)
1053 			atombios_set_edp_panel_power(connector,
1054 						     ATOM_TRANSMITTER_ACTION_POWER_OFF);
1055 	} else {
1056 		radeon_dig_connector->dp_sink_type = radeon_dp_getsinktype(radeon_connector);
1057 		if (radeon_dig_connector->dp_sink_type == CONNECTOR_OBJECT_ID_DISPLAYPORT) {
1058 			if (radeon_dp_getdpcd(radeon_connector))
1059 				ret = connector_status_connected;
1060 		} else {
1061 			if (radeon_ddc_probe(radeon_connector))
1062 				ret = connector_status_connected;
1063 		}
1064 	}
1065 
1066 	radeon_connector_update_scratch_regs(connector, ret);
1067 	return ret;
1068 }
1069 
1070 static int radeon_dp_mode_valid(struct drm_connector *connector,
1071 				  struct drm_display_mode *mode)
1072 {
1073 	struct radeon_connector *radeon_connector = to_radeon_connector(connector);
1074 	struct radeon_connector_atom_dig *radeon_dig_connector = radeon_connector->con_priv;
1075 
1076 	/* XXX check mode bandwidth */
1077 
1078 	if ((radeon_dig_connector->dp_sink_type == CONNECTOR_OBJECT_ID_DISPLAYPORT) ||
1079 	    (radeon_dig_connector->dp_sink_type == CONNECTOR_OBJECT_ID_eDP))
1080 		return radeon_dp_mode_valid_helper(radeon_connector, mode);
1081 	else
1082 		return MODE_OK;
1083 }
1084 
1085 struct drm_connector_helper_funcs radeon_dp_connector_helper_funcs = {
1086 	.get_modes = radeon_dp_get_modes,
1087 	.mode_valid = radeon_dp_mode_valid,
1088 	.best_encoder = radeon_dvi_encoder,
1089 };
1090 
1091 struct drm_connector_funcs radeon_dp_connector_funcs = {
1092 	.dpms = drm_helper_connector_dpms,
1093 	.detect = radeon_dp_detect,
1094 	.fill_modes = drm_helper_probe_single_connector_modes,
1095 	.set_property = radeon_connector_set_property,
1096 	.destroy = radeon_dp_connector_destroy,
1097 	.force = radeon_dvi_force,
1098 };
1099 
1100 void
1101 radeon_add_atom_connector(struct drm_device *dev,
1102 			  uint32_t connector_id,
1103 			  uint32_t supported_device,
1104 			  int connector_type,
1105 			  struct radeon_i2c_bus_rec *i2c_bus,
1106 			  uint32_t igp_lane_info,
1107 			  uint16_t connector_object_id,
1108 			  struct radeon_hpd *hpd,
1109 			  struct radeon_router *router)
1110 {
1111 	struct radeon_device *rdev = dev->dev_private;
1112 	struct drm_connector *connector;
1113 	struct radeon_connector *radeon_connector;
1114 	struct radeon_connector_atom_dig *radeon_dig_connector;
1115 	uint32_t subpixel_order = SubPixelNone;
1116 	bool shared_ddc = false;
1117 
1118 	if (connector_type == DRM_MODE_CONNECTOR_Unknown)
1119 		return;
1120 
1121 	/* if the user selected tv=0 don't try and add the connector */
1122 	if (((connector_type == DRM_MODE_CONNECTOR_SVIDEO) ||
1123 	     (connector_type == DRM_MODE_CONNECTOR_Composite) ||
1124 	     (connector_type == DRM_MODE_CONNECTOR_9PinDIN)) &&
1125 	    (radeon_tv == 0))
1126 		return;
1127 
1128 	/* see if we already added it */
1129 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1130 		radeon_connector = to_radeon_connector(connector);
1131 		if (radeon_connector->connector_id == connector_id) {
1132 			radeon_connector->devices |= supported_device;
1133 			return;
1134 		}
1135 		if (radeon_connector->ddc_bus && i2c_bus->valid) {
1136 			if (radeon_connector->ddc_bus->rec.i2c_id == i2c_bus->i2c_id) {
1137 				radeon_connector->shared_ddc = true;
1138 				shared_ddc = true;
1139 			}
1140 			if (radeon_connector->router_bus && router->ddc_valid &&
1141 			    (radeon_connector->router.router_id == router->router_id)) {
1142 				radeon_connector->shared_ddc = false;
1143 				shared_ddc = false;
1144 			}
1145 		}
1146 	}
1147 
1148 	radeon_connector = kzalloc(sizeof(struct radeon_connector), GFP_KERNEL);
1149 	if (!radeon_connector)
1150 		return;
1151 
1152 	connector = &radeon_connector->base;
1153 
1154 	radeon_connector->connector_id = connector_id;
1155 	radeon_connector->devices = supported_device;
1156 	radeon_connector->shared_ddc = shared_ddc;
1157 	radeon_connector->connector_object_id = connector_object_id;
1158 	radeon_connector->hpd = *hpd;
1159 	radeon_connector->router = *router;
1160 	if (router->ddc_valid || router->cd_valid) {
1161 		radeon_connector->router_bus = radeon_i2c_lookup(rdev, &router->i2c_info);
1162 		if (!radeon_connector->router_bus)
1163 			goto failed;
1164 	}
1165 	switch (connector_type) {
1166 	case DRM_MODE_CONNECTOR_VGA:
1167 		drm_connector_init(dev, &radeon_connector->base, &radeon_vga_connector_funcs, connector_type);
1168 		drm_connector_helper_add(&radeon_connector->base, &radeon_vga_connector_helper_funcs);
1169 		if (i2c_bus->valid) {
1170 			radeon_connector->ddc_bus = radeon_i2c_lookup(rdev, i2c_bus);
1171 			if (!radeon_connector->ddc_bus)
1172 				goto failed;
1173 		}
1174 		radeon_connector->dac_load_detect = true;
1175 		drm_connector_attach_property(&radeon_connector->base,
1176 					      rdev->mode_info.load_detect_property,
1177 					      1);
1178 		/* no HPD on analog connectors */
1179 		radeon_connector->hpd.hpd = RADEON_HPD_NONE;
1180 		connector->polled = DRM_CONNECTOR_POLL_CONNECT;
1181 		connector->interlace_allowed = true;
1182 		connector->doublescan_allowed = true;
1183 		break;
1184 	case DRM_MODE_CONNECTOR_DVIA:
1185 		drm_connector_init(dev, &radeon_connector->base, &radeon_vga_connector_funcs, connector_type);
1186 		drm_connector_helper_add(&radeon_connector->base, &radeon_vga_connector_helper_funcs);
1187 		if (i2c_bus->valid) {
1188 			radeon_connector->ddc_bus = radeon_i2c_lookup(rdev, i2c_bus);
1189 			if (!radeon_connector->ddc_bus)
1190 				goto failed;
1191 		}
1192 		radeon_connector->dac_load_detect = true;
1193 		drm_connector_attach_property(&radeon_connector->base,
1194 					      rdev->mode_info.load_detect_property,
1195 					      1);
1196 		/* no HPD on analog connectors */
1197 		radeon_connector->hpd.hpd = RADEON_HPD_NONE;
1198 		connector->interlace_allowed = true;
1199 		connector->doublescan_allowed = true;
1200 		break;
1201 	case DRM_MODE_CONNECTOR_DVII:
1202 	case DRM_MODE_CONNECTOR_DVID:
1203 		radeon_dig_connector = kzalloc(sizeof(struct radeon_connector_atom_dig), GFP_KERNEL);
1204 		if (!radeon_dig_connector)
1205 			goto failed;
1206 		radeon_dig_connector->igp_lane_info = igp_lane_info;
1207 		radeon_connector->con_priv = radeon_dig_connector;
1208 		drm_connector_init(dev, &radeon_connector->base, &radeon_dvi_connector_funcs, connector_type);
1209 		drm_connector_helper_add(&radeon_connector->base, &radeon_dvi_connector_helper_funcs);
1210 		if (i2c_bus->valid) {
1211 			radeon_connector->ddc_bus = radeon_i2c_lookup(rdev, i2c_bus);
1212 			if (!radeon_connector->ddc_bus)
1213 				goto failed;
1214 		}
1215 		subpixel_order = SubPixelHorizontalRGB;
1216 		drm_connector_attach_property(&radeon_connector->base,
1217 					      rdev->mode_info.coherent_mode_property,
1218 					      1);
1219 		if (ASIC_IS_AVIVO(rdev)) {
1220 			drm_connector_attach_property(&radeon_connector->base,
1221 						      rdev->mode_info.underscan_property,
1222 						      UNDERSCAN_OFF);
1223 			drm_connector_attach_property(&radeon_connector->base,
1224 						      rdev->mode_info.underscan_hborder_property,
1225 						      0);
1226 			drm_connector_attach_property(&radeon_connector->base,
1227 						      rdev->mode_info.underscan_vborder_property,
1228 						      0);
1229 		}
1230 		if (connector_type == DRM_MODE_CONNECTOR_DVII) {
1231 			radeon_connector->dac_load_detect = true;
1232 			drm_connector_attach_property(&radeon_connector->base,
1233 						      rdev->mode_info.load_detect_property,
1234 						      1);
1235 		}
1236 		connector->interlace_allowed = true;
1237 		if (connector_type == DRM_MODE_CONNECTOR_DVII)
1238 			connector->doublescan_allowed = true;
1239 		else
1240 			connector->doublescan_allowed = false;
1241 		break;
1242 	case DRM_MODE_CONNECTOR_HDMIA:
1243 	case DRM_MODE_CONNECTOR_HDMIB:
1244 		radeon_dig_connector = kzalloc(sizeof(struct radeon_connector_atom_dig), GFP_KERNEL);
1245 		if (!radeon_dig_connector)
1246 			goto failed;
1247 		radeon_dig_connector->igp_lane_info = igp_lane_info;
1248 		radeon_connector->con_priv = radeon_dig_connector;
1249 		drm_connector_init(dev, &radeon_connector->base, &radeon_dvi_connector_funcs, connector_type);
1250 		drm_connector_helper_add(&radeon_connector->base, &radeon_dvi_connector_helper_funcs);
1251 		if (i2c_bus->valid) {
1252 			radeon_connector->ddc_bus = radeon_i2c_lookup(rdev, i2c_bus);
1253 			if (!radeon_connector->ddc_bus)
1254 				goto failed;
1255 		}
1256 		drm_connector_attach_property(&radeon_connector->base,
1257 					      rdev->mode_info.coherent_mode_property,
1258 					      1);
1259 		if (ASIC_IS_AVIVO(rdev)) {
1260 			drm_connector_attach_property(&radeon_connector->base,
1261 						      rdev->mode_info.underscan_property,
1262 						      UNDERSCAN_OFF);
1263 			drm_connector_attach_property(&radeon_connector->base,
1264 						      rdev->mode_info.underscan_hborder_property,
1265 						      0);
1266 			drm_connector_attach_property(&radeon_connector->base,
1267 						      rdev->mode_info.underscan_vborder_property,
1268 						      0);
1269 		}
1270 		subpixel_order = SubPixelHorizontalRGB;
1271 		connector->interlace_allowed = true;
1272 		if (connector_type == DRM_MODE_CONNECTOR_HDMIB)
1273 			connector->doublescan_allowed = true;
1274 		else
1275 			connector->doublescan_allowed = false;
1276 		break;
1277 	case DRM_MODE_CONNECTOR_DisplayPort:
1278 	case DRM_MODE_CONNECTOR_eDP:
1279 		radeon_dig_connector = kzalloc(sizeof(struct radeon_connector_atom_dig), GFP_KERNEL);
1280 		if (!radeon_dig_connector)
1281 			goto failed;
1282 		radeon_dig_connector->igp_lane_info = igp_lane_info;
1283 		radeon_connector->con_priv = radeon_dig_connector;
1284 		drm_connector_init(dev, &radeon_connector->base, &radeon_dp_connector_funcs, connector_type);
1285 		drm_connector_helper_add(&radeon_connector->base, &radeon_dp_connector_helper_funcs);
1286 		if (i2c_bus->valid) {
1287 			/* add DP i2c bus */
1288 			if (connector_type == DRM_MODE_CONNECTOR_eDP)
1289 				radeon_dig_connector->dp_i2c_bus = radeon_i2c_create_dp(dev, i2c_bus, "eDP-auxch");
1290 			else
1291 				radeon_dig_connector->dp_i2c_bus = radeon_i2c_create_dp(dev, i2c_bus, "DP-auxch");
1292 			if (!radeon_dig_connector->dp_i2c_bus)
1293 				goto failed;
1294 			radeon_connector->ddc_bus = radeon_i2c_lookup(rdev, i2c_bus);
1295 			if (!radeon_connector->ddc_bus)
1296 				goto failed;
1297 		}
1298 		subpixel_order = SubPixelHorizontalRGB;
1299 		drm_connector_attach_property(&radeon_connector->base,
1300 					      rdev->mode_info.coherent_mode_property,
1301 					      1);
1302 		if (ASIC_IS_AVIVO(rdev)) {
1303 			drm_connector_attach_property(&radeon_connector->base,
1304 						      rdev->mode_info.underscan_property,
1305 						      UNDERSCAN_OFF);
1306 			drm_connector_attach_property(&radeon_connector->base,
1307 						      rdev->mode_info.underscan_hborder_property,
1308 						      0);
1309 			drm_connector_attach_property(&radeon_connector->base,
1310 						      rdev->mode_info.underscan_vborder_property,
1311 						      0);
1312 		}
1313 		connector->interlace_allowed = true;
1314 		/* in theory with a DP to VGA converter... */
1315 		connector->doublescan_allowed = false;
1316 		break;
1317 	case DRM_MODE_CONNECTOR_SVIDEO:
1318 	case DRM_MODE_CONNECTOR_Composite:
1319 	case DRM_MODE_CONNECTOR_9PinDIN:
1320 		drm_connector_init(dev, &radeon_connector->base, &radeon_tv_connector_funcs, connector_type);
1321 		drm_connector_helper_add(&radeon_connector->base, &radeon_tv_connector_helper_funcs);
1322 		radeon_connector->dac_load_detect = true;
1323 		drm_connector_attach_property(&radeon_connector->base,
1324 					      rdev->mode_info.load_detect_property,
1325 					      1);
1326 		drm_connector_attach_property(&radeon_connector->base,
1327 					      rdev->mode_info.tv_std_property,
1328 					      radeon_atombios_get_tv_info(rdev));
1329 		/* no HPD on analog connectors */
1330 		radeon_connector->hpd.hpd = RADEON_HPD_NONE;
1331 		connector->interlace_allowed = false;
1332 		connector->doublescan_allowed = false;
1333 		break;
1334 	case DRM_MODE_CONNECTOR_LVDS:
1335 		radeon_dig_connector = kzalloc(sizeof(struct radeon_connector_atom_dig), GFP_KERNEL);
1336 		if (!radeon_dig_connector)
1337 			goto failed;
1338 		radeon_dig_connector->igp_lane_info = igp_lane_info;
1339 		radeon_connector->con_priv = radeon_dig_connector;
1340 		drm_connector_init(dev, &radeon_connector->base, &radeon_lvds_connector_funcs, connector_type);
1341 		drm_connector_helper_add(&radeon_connector->base, &radeon_lvds_connector_helper_funcs);
1342 		if (i2c_bus->valid) {
1343 			radeon_connector->ddc_bus = radeon_i2c_lookup(rdev, i2c_bus);
1344 			if (!radeon_connector->ddc_bus)
1345 				goto failed;
1346 		}
1347 		drm_connector_attach_property(&radeon_connector->base,
1348 					      dev->mode_config.scaling_mode_property,
1349 					      DRM_MODE_SCALE_FULLSCREEN);
1350 		subpixel_order = SubPixelHorizontalRGB;
1351 		connector->interlace_allowed = false;
1352 		connector->doublescan_allowed = false;
1353 		break;
1354 	}
1355 
1356 	if (radeon_connector->hpd.hpd == RADEON_HPD_NONE) {
1357 		if (i2c_bus->valid)
1358 			connector->polled = DRM_CONNECTOR_POLL_CONNECT;
1359 	} else
1360 		connector->polled = DRM_CONNECTOR_POLL_HPD;
1361 
1362 	connector->display_info.subpixel_order = subpixel_order;
1363 	drm_sysfs_connector_add(connector);
1364 	return;
1365 
1366 failed:
1367 	drm_connector_cleanup(connector);
1368 	kfree(connector);
1369 }
1370 
1371 void
1372 radeon_add_legacy_connector(struct drm_device *dev,
1373 			    uint32_t connector_id,
1374 			    uint32_t supported_device,
1375 			    int connector_type,
1376 			    struct radeon_i2c_bus_rec *i2c_bus,
1377 			    uint16_t connector_object_id,
1378 			    struct radeon_hpd *hpd)
1379 {
1380 	struct radeon_device *rdev = dev->dev_private;
1381 	struct drm_connector *connector;
1382 	struct radeon_connector *radeon_connector;
1383 	uint32_t subpixel_order = SubPixelNone;
1384 
1385 	if (connector_type == DRM_MODE_CONNECTOR_Unknown)
1386 		return;
1387 
1388 	/* if the user selected tv=0 don't try and add the connector */
1389 	if (((connector_type == DRM_MODE_CONNECTOR_SVIDEO) ||
1390 	     (connector_type == DRM_MODE_CONNECTOR_Composite) ||
1391 	     (connector_type == DRM_MODE_CONNECTOR_9PinDIN)) &&
1392 	    (radeon_tv == 0))
1393 		return;
1394 
1395 	/* see if we already added it */
1396 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1397 		radeon_connector = to_radeon_connector(connector);
1398 		if (radeon_connector->connector_id == connector_id) {
1399 			radeon_connector->devices |= supported_device;
1400 			return;
1401 		}
1402 	}
1403 
1404 	radeon_connector = kzalloc(sizeof(struct radeon_connector), GFP_KERNEL);
1405 	if (!radeon_connector)
1406 		return;
1407 
1408 	connector = &radeon_connector->base;
1409 
1410 	radeon_connector->connector_id = connector_id;
1411 	radeon_connector->devices = supported_device;
1412 	radeon_connector->connector_object_id = connector_object_id;
1413 	radeon_connector->hpd = *hpd;
1414 	switch (connector_type) {
1415 	case DRM_MODE_CONNECTOR_VGA:
1416 		drm_connector_init(dev, &radeon_connector->base, &radeon_vga_connector_funcs, connector_type);
1417 		drm_connector_helper_add(&radeon_connector->base, &radeon_vga_connector_helper_funcs);
1418 		if (i2c_bus->valid) {
1419 			radeon_connector->ddc_bus = radeon_i2c_lookup(rdev, i2c_bus);
1420 			if (!radeon_connector->ddc_bus)
1421 				goto failed;
1422 		}
1423 		radeon_connector->dac_load_detect = true;
1424 		drm_connector_attach_property(&radeon_connector->base,
1425 					      rdev->mode_info.load_detect_property,
1426 					      1);
1427 		/* no HPD on analog connectors */
1428 		radeon_connector->hpd.hpd = RADEON_HPD_NONE;
1429 		connector->polled = DRM_CONNECTOR_POLL_CONNECT;
1430 		connector->interlace_allowed = true;
1431 		connector->doublescan_allowed = true;
1432 		break;
1433 	case DRM_MODE_CONNECTOR_DVIA:
1434 		drm_connector_init(dev, &radeon_connector->base, &radeon_vga_connector_funcs, connector_type);
1435 		drm_connector_helper_add(&radeon_connector->base, &radeon_vga_connector_helper_funcs);
1436 		if (i2c_bus->valid) {
1437 			radeon_connector->ddc_bus = radeon_i2c_lookup(rdev, i2c_bus);
1438 			if (!radeon_connector->ddc_bus)
1439 				goto failed;
1440 		}
1441 		radeon_connector->dac_load_detect = true;
1442 		drm_connector_attach_property(&radeon_connector->base,
1443 					      rdev->mode_info.load_detect_property,
1444 					      1);
1445 		/* no HPD on analog connectors */
1446 		radeon_connector->hpd.hpd = RADEON_HPD_NONE;
1447 		connector->interlace_allowed = true;
1448 		connector->doublescan_allowed = true;
1449 		break;
1450 	case DRM_MODE_CONNECTOR_DVII:
1451 	case DRM_MODE_CONNECTOR_DVID:
1452 		drm_connector_init(dev, &radeon_connector->base, &radeon_dvi_connector_funcs, connector_type);
1453 		drm_connector_helper_add(&radeon_connector->base, &radeon_dvi_connector_helper_funcs);
1454 		if (i2c_bus->valid) {
1455 			radeon_connector->ddc_bus = radeon_i2c_lookup(rdev, i2c_bus);
1456 			if (!radeon_connector->ddc_bus)
1457 				goto failed;
1458 		}
1459 		if (connector_type == DRM_MODE_CONNECTOR_DVII) {
1460 			radeon_connector->dac_load_detect = true;
1461 			drm_connector_attach_property(&radeon_connector->base,
1462 						      rdev->mode_info.load_detect_property,
1463 						      1);
1464 		}
1465 		subpixel_order = SubPixelHorizontalRGB;
1466 		connector->interlace_allowed = true;
1467 		if (connector_type == DRM_MODE_CONNECTOR_DVII)
1468 			connector->doublescan_allowed = true;
1469 		else
1470 			connector->doublescan_allowed = false;
1471 		break;
1472 	case DRM_MODE_CONNECTOR_SVIDEO:
1473 	case DRM_MODE_CONNECTOR_Composite:
1474 	case DRM_MODE_CONNECTOR_9PinDIN:
1475 		drm_connector_init(dev, &radeon_connector->base, &radeon_tv_connector_funcs, connector_type);
1476 		drm_connector_helper_add(&radeon_connector->base, &radeon_tv_connector_helper_funcs);
1477 		radeon_connector->dac_load_detect = true;
1478 		/* RS400,RC410,RS480 chipset seems to report a lot
1479 		 * of false positive on load detect, we haven't yet
1480 		 * found a way to make load detect reliable on those
1481 		 * chipset, thus just disable it for TV.
1482 		 */
1483 		if (rdev->family == CHIP_RS400 || rdev->family == CHIP_RS480)
1484 			radeon_connector->dac_load_detect = false;
1485 		drm_connector_attach_property(&radeon_connector->base,
1486 					      rdev->mode_info.load_detect_property,
1487 					      radeon_connector->dac_load_detect);
1488 		drm_connector_attach_property(&radeon_connector->base,
1489 					      rdev->mode_info.tv_std_property,
1490 					      radeon_combios_get_tv_info(rdev));
1491 		/* no HPD on analog connectors */
1492 		radeon_connector->hpd.hpd = RADEON_HPD_NONE;
1493 		connector->interlace_allowed = false;
1494 		connector->doublescan_allowed = false;
1495 		break;
1496 	case DRM_MODE_CONNECTOR_LVDS:
1497 		drm_connector_init(dev, &radeon_connector->base, &radeon_lvds_connector_funcs, connector_type);
1498 		drm_connector_helper_add(&radeon_connector->base, &radeon_lvds_connector_helper_funcs);
1499 		if (i2c_bus->valid) {
1500 			radeon_connector->ddc_bus = radeon_i2c_lookup(rdev, i2c_bus);
1501 			if (!radeon_connector->ddc_bus)
1502 				goto failed;
1503 		}
1504 		drm_connector_attach_property(&radeon_connector->base,
1505 					      dev->mode_config.scaling_mode_property,
1506 					      DRM_MODE_SCALE_FULLSCREEN);
1507 		subpixel_order = SubPixelHorizontalRGB;
1508 		connector->interlace_allowed = false;
1509 		connector->doublescan_allowed = false;
1510 		break;
1511 	}
1512 
1513 	if (radeon_connector->hpd.hpd == RADEON_HPD_NONE) {
1514 		if (i2c_bus->valid)
1515 			connector->polled = DRM_CONNECTOR_POLL_CONNECT;
1516 	} else
1517 		connector->polled = DRM_CONNECTOR_POLL_HPD;
1518 	connector->display_info.subpixel_order = subpixel_order;
1519 	drm_sysfs_connector_add(connector);
1520 	return;
1521 
1522 failed:
1523 	drm_connector_cleanup(connector);
1524 	kfree(connector);
1525 }
1526