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