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