xref: /dragonfly/sys/dev/drm/radeon/radeon_encoders.c (revision c59a5c48)
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_crtc_helper.h>
28 #include <drm/radeon_drm.h>
29 #include "radeon.h"
30 #include "atom.h"
31 
32 
radeon_encoder_clones(struct drm_encoder * encoder)33 static uint32_t radeon_encoder_clones(struct drm_encoder *encoder)
34 {
35 	struct drm_device *dev = encoder->dev;
36 	struct radeon_device *rdev = dev->dev_private;
37 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
38 	struct drm_encoder *clone_encoder;
39 	uint32_t index_mask = 0;
40 	int count;
41 
42 	/* DIG routing gets problematic */
43 	if (rdev->family >= CHIP_R600)
44 		return index_mask;
45 	/* LVDS/TV are too wacky */
46 	if (radeon_encoder->devices & ATOM_DEVICE_LCD_SUPPORT)
47 		return index_mask;
48 	/* DVO requires 2x ppll clocks depending on tmds chip */
49 	if (radeon_encoder->devices & ATOM_DEVICE_DFP2_SUPPORT)
50 		return index_mask;
51 
52 	count = -1;
53 	list_for_each_entry(clone_encoder, &dev->mode_config.encoder_list, head) {
54 		struct radeon_encoder *radeon_clone = to_radeon_encoder(clone_encoder);
55 		count++;
56 
57 		if (clone_encoder == encoder)
58 			continue;
59 		if (radeon_clone->devices & (ATOM_DEVICE_LCD_SUPPORT))
60 			continue;
61 		if (radeon_clone->devices & ATOM_DEVICE_DFP2_SUPPORT)
62 			continue;
63 		else
64 			index_mask |= (1 << count);
65 	}
66 	return index_mask;
67 }
68 
radeon_setup_encoder_clones(struct drm_device * dev)69 void radeon_setup_encoder_clones(struct drm_device *dev)
70 {
71 	struct drm_encoder *encoder;
72 
73 	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
74 		encoder->possible_clones = radeon_encoder_clones(encoder);
75 	}
76 }
77 
78 uint32_t
radeon_get_encoder_enum(struct drm_device * dev,uint32_t supported_device,uint8_t dac)79 radeon_get_encoder_enum(struct drm_device *dev, uint32_t supported_device, uint8_t dac)
80 {
81 	struct radeon_device *rdev = dev->dev_private;
82 	uint32_t ret = 0;
83 
84 	switch (supported_device) {
85 	case ATOM_DEVICE_CRT1_SUPPORT:
86 	case ATOM_DEVICE_TV1_SUPPORT:
87 	case ATOM_DEVICE_TV2_SUPPORT:
88 	case ATOM_DEVICE_CRT2_SUPPORT:
89 	case ATOM_DEVICE_CV_SUPPORT:
90 		switch (dac) {
91 		case 1: /* dac a */
92 			if ((rdev->family == CHIP_RS300) ||
93 			    (rdev->family == CHIP_RS400) ||
94 			    (rdev->family == CHIP_RS480))
95 				ret = ENCODER_INTERNAL_DAC2_ENUM_ID1;
96 			else if (ASIC_IS_AVIVO(rdev))
97 				ret = ENCODER_INTERNAL_KLDSCP_DAC1_ENUM_ID1;
98 			else
99 				ret = ENCODER_INTERNAL_DAC1_ENUM_ID1;
100 			break;
101 		case 2: /* dac b */
102 			if (ASIC_IS_AVIVO(rdev))
103 				ret = ENCODER_INTERNAL_KLDSCP_DAC2_ENUM_ID1;
104 			else {
105 				/*if (rdev->family == CHIP_R200)
106 				  ret = ENCODER_INTERNAL_DVO1_ENUM_ID1;
107 				  else*/
108 				ret = ENCODER_INTERNAL_DAC2_ENUM_ID1;
109 			}
110 			break;
111 		case 3: /* external dac */
112 			if (ASIC_IS_AVIVO(rdev))
113 				ret = ENCODER_INTERNAL_KLDSCP_DVO1_ENUM_ID1;
114 			else
115 				ret = ENCODER_INTERNAL_DVO1_ENUM_ID1;
116 			break;
117 		}
118 		break;
119 	case ATOM_DEVICE_LCD1_SUPPORT:
120 		if (ASIC_IS_AVIVO(rdev))
121 			ret = ENCODER_INTERNAL_LVTM1_ENUM_ID1;
122 		else
123 			ret = ENCODER_INTERNAL_LVDS_ENUM_ID1;
124 		break;
125 	case ATOM_DEVICE_DFP1_SUPPORT:
126 		if ((rdev->family == CHIP_RS300) ||
127 		    (rdev->family == CHIP_RS400) ||
128 		    (rdev->family == CHIP_RS480))
129 			ret = ENCODER_INTERNAL_DVO1_ENUM_ID1;
130 		else if (ASIC_IS_AVIVO(rdev))
131 			ret = ENCODER_INTERNAL_KLDSCP_TMDS1_ENUM_ID1;
132 		else
133 			ret = ENCODER_INTERNAL_TMDS1_ENUM_ID1;
134 		break;
135 	case ATOM_DEVICE_LCD2_SUPPORT:
136 	case ATOM_DEVICE_DFP2_SUPPORT:
137 		if ((rdev->family == CHIP_RS600) ||
138 		    (rdev->family == CHIP_RS690) ||
139 		    (rdev->family == CHIP_RS740))
140 			ret = ENCODER_INTERNAL_DDI_ENUM_ID1;
141 		else if (ASIC_IS_AVIVO(rdev))
142 			ret = ENCODER_INTERNAL_KLDSCP_DVO1_ENUM_ID1;
143 		else
144 			ret = ENCODER_INTERNAL_DVO1_ENUM_ID1;
145 		break;
146 	case ATOM_DEVICE_DFP3_SUPPORT:
147 		ret = ENCODER_INTERNAL_LVTM1_ENUM_ID1;
148 		break;
149 	}
150 
151 	return ret;
152 }
153 
radeon_encoder_add_backlight(struct radeon_encoder * radeon_encoder,struct drm_connector * connector)154 static void radeon_encoder_add_backlight(struct radeon_encoder *radeon_encoder,
155 					 struct drm_connector *connector)
156 {
157 	struct drm_device *dev = radeon_encoder->base.dev;
158 	struct radeon_device *rdev = dev->dev_private;
159 	bool use_bl = false;
160 
161 	if (!(radeon_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT)))
162 		return;
163 
164 	if (radeon_backlight == 0) {
165 		return;
166 	} else if (radeon_backlight == 1) {
167 		use_bl = true;
168 	} else if (radeon_backlight == -1) {
169 		/* Quirks */
170 		/* Amilo Xi 2550 only works with acpi bl */
171 		if ((rdev->pdev->device == 0x9583) &&
172 		    (rdev->pdev->subsystem_vendor == 0x1734) &&
173 		    (rdev->pdev->subsystem_device == 0x1107))
174 			use_bl = false;
175 /* Older PPC macs use on-GPU backlight controller */
176 #ifndef CONFIG_PPC_PMAC
177 		/* disable native backlight control on older asics */
178 		else if (rdev->family < CHIP_R600)
179 			use_bl = false;
180 #endif
181 		else
182 			use_bl = true;
183 	}
184 
185 	if (use_bl) {
186 		if (rdev->is_atom_bios)
187 			radeon_atom_backlight_init(radeon_encoder, connector);
188 		else
189 			radeon_legacy_backlight_init(radeon_encoder, connector);
190 	}
191 }
192 
193 void
radeon_link_encoder_connector(struct drm_device * dev)194 radeon_link_encoder_connector(struct drm_device *dev)
195 {
196 	struct drm_connector *connector;
197 	struct radeon_connector *radeon_connector;
198 	struct drm_encoder *encoder;
199 	struct radeon_encoder *radeon_encoder;
200 
201 	/* walk the list and link encoders to connectors */
202 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
203 		radeon_connector = to_radeon_connector(connector);
204 		list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
205 			radeon_encoder = to_radeon_encoder(encoder);
206 			if (radeon_encoder->devices & radeon_connector->devices) {
207 				drm_mode_connector_attach_encoder(connector, encoder);
208 				if (radeon_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT))
209 					radeon_encoder_add_backlight(radeon_encoder, connector);
210 			}
211 		}
212 	}
213 }
214 
radeon_encoder_set_active_device(struct drm_encoder * encoder)215 void radeon_encoder_set_active_device(struct drm_encoder *encoder)
216 {
217 	struct drm_device *dev = encoder->dev;
218 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
219 	struct drm_connector *connector;
220 
221 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
222 		if (connector->encoder == encoder) {
223 			struct radeon_connector *radeon_connector = to_radeon_connector(connector);
224 			radeon_encoder->active_device = radeon_encoder->devices & radeon_connector->devices;
225 			DRM_DEBUG_KMS("setting active device to %08x from %08x %08x for encoder %d\n",
226 				  radeon_encoder->active_device, radeon_encoder->devices,
227 				  radeon_connector->devices, encoder->encoder_type);
228 		}
229 	}
230 }
231 
232 struct drm_connector *
radeon_get_connector_for_encoder(struct drm_encoder * encoder)233 radeon_get_connector_for_encoder(struct drm_encoder *encoder)
234 {
235 	struct drm_device *dev = encoder->dev;
236 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
237 	struct drm_connector *connector;
238 	struct radeon_connector *radeon_connector;
239 
240 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
241 		radeon_connector = to_radeon_connector(connector);
242 		if (radeon_encoder->is_mst_encoder) {
243 			struct radeon_encoder_mst *mst_enc;
244 
245 			if (!radeon_connector->is_mst_connector)
246 				continue;
247 
248 			mst_enc = radeon_encoder->enc_priv;
249 			if (mst_enc->connector == radeon_connector->mst_port)
250 				return connector;
251 		} else if (radeon_encoder->active_device & radeon_connector->devices)
252 			return connector;
253 	}
254 	return NULL;
255 }
256 
257 struct drm_connector *
radeon_get_connector_for_encoder_init(struct drm_encoder * encoder)258 radeon_get_connector_for_encoder_init(struct drm_encoder *encoder)
259 {
260 	struct drm_device *dev = encoder->dev;
261 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
262 	struct drm_connector *connector;
263 	struct radeon_connector *radeon_connector;
264 
265 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
266 		radeon_connector = to_radeon_connector(connector);
267 		if (radeon_encoder->devices & radeon_connector->devices)
268 			return connector;
269 	}
270 	return NULL;
271 }
272 
radeon_get_external_encoder(struct drm_encoder * encoder)273 struct drm_encoder *radeon_get_external_encoder(struct drm_encoder *encoder)
274 {
275 	struct drm_device *dev = encoder->dev;
276 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
277 	struct drm_encoder *other_encoder;
278 	struct radeon_encoder *other_radeon_encoder;
279 
280 	if (radeon_encoder->is_ext_encoder)
281 		return NULL;
282 
283 	list_for_each_entry(other_encoder, &dev->mode_config.encoder_list, head) {
284 		if (other_encoder == encoder)
285 			continue;
286 		other_radeon_encoder = to_radeon_encoder(other_encoder);
287 		if (other_radeon_encoder->is_ext_encoder &&
288 		    (radeon_encoder->devices & other_radeon_encoder->devices))
289 			return other_encoder;
290 	}
291 	return NULL;
292 }
293 
radeon_encoder_get_dp_bridge_encoder_id(struct drm_encoder * encoder)294 u16 radeon_encoder_get_dp_bridge_encoder_id(struct drm_encoder *encoder)
295 {
296 	struct drm_encoder *other_encoder = radeon_get_external_encoder(encoder);
297 
298 	if (other_encoder) {
299 		struct radeon_encoder *radeon_encoder = to_radeon_encoder(other_encoder);
300 
301 		switch (radeon_encoder->encoder_id) {
302 		case ENCODER_OBJECT_ID_TRAVIS:
303 		case ENCODER_OBJECT_ID_NUTMEG:
304 			return radeon_encoder->encoder_id;
305 		default:
306 			return ENCODER_OBJECT_ID_NONE;
307 		}
308 	}
309 	return ENCODER_OBJECT_ID_NONE;
310 }
311 
radeon_panel_mode_fixup(struct drm_encoder * encoder,struct drm_display_mode * adjusted_mode)312 void radeon_panel_mode_fixup(struct drm_encoder *encoder,
313 			     struct drm_display_mode *adjusted_mode)
314 {
315 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
316 	struct drm_device *dev = encoder->dev;
317 	struct radeon_device *rdev = dev->dev_private;
318 	struct drm_display_mode *native_mode = &radeon_encoder->native_mode;
319 	unsigned hblank = native_mode->htotal - native_mode->hdisplay;
320 	unsigned vblank = native_mode->vtotal - native_mode->vdisplay;
321 	unsigned hover = native_mode->hsync_start - native_mode->hdisplay;
322 	unsigned vover = native_mode->vsync_start - native_mode->vdisplay;
323 	unsigned hsync_width = native_mode->hsync_end - native_mode->hsync_start;
324 	unsigned vsync_width = native_mode->vsync_end - native_mode->vsync_start;
325 
326 	adjusted_mode->clock = native_mode->clock;
327 	adjusted_mode->flags = native_mode->flags;
328 
329 	if (ASIC_IS_AVIVO(rdev)) {
330 		adjusted_mode->hdisplay = native_mode->hdisplay;
331 		adjusted_mode->vdisplay = native_mode->vdisplay;
332 	}
333 
334 	adjusted_mode->htotal = native_mode->hdisplay + hblank;
335 	adjusted_mode->hsync_start = native_mode->hdisplay + hover;
336 	adjusted_mode->hsync_end = adjusted_mode->hsync_start + hsync_width;
337 
338 	adjusted_mode->vtotal = native_mode->vdisplay + vblank;
339 	adjusted_mode->vsync_start = native_mode->vdisplay + vover;
340 	adjusted_mode->vsync_end = adjusted_mode->vsync_start + vsync_width;
341 
342 	drm_mode_set_crtcinfo(adjusted_mode, CRTC_INTERLACE_HALVE_V);
343 
344 	if (ASIC_IS_AVIVO(rdev)) {
345 		adjusted_mode->crtc_hdisplay = native_mode->hdisplay;
346 		adjusted_mode->crtc_vdisplay = native_mode->vdisplay;
347 	}
348 
349 	adjusted_mode->crtc_htotal = adjusted_mode->crtc_hdisplay + hblank;
350 	adjusted_mode->crtc_hsync_start = adjusted_mode->crtc_hdisplay + hover;
351 	adjusted_mode->crtc_hsync_end = adjusted_mode->crtc_hsync_start + hsync_width;
352 
353 	adjusted_mode->crtc_vtotal = adjusted_mode->crtc_vdisplay + vblank;
354 	adjusted_mode->crtc_vsync_start = adjusted_mode->crtc_vdisplay + vover;
355 	adjusted_mode->crtc_vsync_end = adjusted_mode->crtc_vsync_start + vsync_width;
356 
357 }
358 
radeon_dig_monitor_is_duallink(struct drm_encoder * encoder,u32 pixel_clock)359 bool radeon_dig_monitor_is_duallink(struct drm_encoder *encoder,
360 				    u32 pixel_clock)
361 {
362 	struct drm_device *dev = encoder->dev;
363 	struct radeon_device *rdev = dev->dev_private;
364 	struct drm_connector *connector;
365 	struct radeon_connector *radeon_connector;
366 	struct radeon_connector_atom_dig *dig_connector;
367 
368 	connector = radeon_get_connector_for_encoder(encoder);
369 	/* if we don't have an active device yet, just use one of
370 	 * the connectors tied to the encoder.
371 	 */
372 	if (!connector)
373 		connector = radeon_get_connector_for_encoder_init(encoder);
374 	radeon_connector = to_radeon_connector(connector);
375 
376 	switch (connector->connector_type) {
377 	case DRM_MODE_CONNECTOR_DVII:
378 	case DRM_MODE_CONNECTOR_HDMIB:
379 		if (radeon_connector->use_digital) {
380 			/* HDMI 1.3 supports up to 340 Mhz over single link */
381 			if (ASIC_IS_DCE6(rdev) && drm_detect_hdmi_monitor(radeon_connector_edid(connector))) {
382 				if (pixel_clock > 340000)
383 					return true;
384 				else
385 					return false;
386 			} else {
387 				if (pixel_clock > 165000)
388 					return true;
389 				else
390 					return false;
391 			}
392 		} else
393 			return false;
394 	case DRM_MODE_CONNECTOR_DVID:
395 	case DRM_MODE_CONNECTOR_HDMIA:
396 	case DRM_MODE_CONNECTOR_DisplayPort:
397 		if (radeon_connector->is_mst_connector)
398 			return false;
399 
400 		dig_connector = radeon_connector->con_priv;
401 		if ((dig_connector->dp_sink_type == CONNECTOR_OBJECT_ID_DISPLAYPORT) ||
402 		    (dig_connector->dp_sink_type == CONNECTOR_OBJECT_ID_eDP))
403 			return false;
404 		else {
405 			/* HDMI 1.3 supports up to 340 Mhz over single link */
406 			if (ASIC_IS_DCE6(rdev) && drm_detect_hdmi_monitor(radeon_connector_edid(connector))) {
407 				if (pixel_clock > 340000)
408 					return true;
409 				else
410 					return false;
411 			} else {
412 				if (pixel_clock > 165000)
413 					return true;
414 				else
415 					return false;
416 			}
417 		}
418 	default:
419 		return false;
420 	}
421 }
422 
radeon_encoder_is_digital(struct drm_encoder * encoder)423 bool radeon_encoder_is_digital(struct drm_encoder *encoder)
424 {
425 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
426 	switch (radeon_encoder->encoder_id) {
427 	case ENCODER_OBJECT_ID_INTERNAL_LVDS:
428 	case ENCODER_OBJECT_ID_INTERNAL_TMDS1:
429 	case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_TMDS1:
430 	case ENCODER_OBJECT_ID_INTERNAL_LVTM1:
431 	case ENCODER_OBJECT_ID_INTERNAL_DVO1:
432 	case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DVO1:
433 	case ENCODER_OBJECT_ID_INTERNAL_DDI:
434 	case ENCODER_OBJECT_ID_INTERNAL_UNIPHY:
435 	case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_LVTMA:
436 	case ENCODER_OBJECT_ID_INTERNAL_UNIPHY1:
437 	case ENCODER_OBJECT_ID_INTERNAL_UNIPHY2:
438 	case ENCODER_OBJECT_ID_INTERNAL_UNIPHY3:
439 		return true;
440 	default:
441 		return false;
442 	}
443 }
444