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