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