• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2007-11 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/backlight.h>
28 #include <linux/dmi.h>
29 #include <linux/pci.h>
30 
31 #include <drm/drm_crtc_helper.h>
32 #include <drm/drm_file.h>
33 #include <drm/radeon_drm.h>
34 
35 #include <acpi/video.h>
36 
37 #include "atom.h"
38 #include "radeon_atombios.h"
39 #include "radeon.h"
40 #include "radeon_asic.h"
41 #include "radeon_audio.h"
42 
43 extern int atom_debug;
44 
45 static u8
radeon_atom_get_backlight_level_from_reg(struct radeon_device * rdev)46 radeon_atom_get_backlight_level_from_reg(struct radeon_device *rdev)
47 {
48 	u8 backlight_level;
49 	u32 bios_2_scratch;
50 
51 	if (rdev->family >= CHIP_R600)
52 		bios_2_scratch = RREG32(R600_BIOS_2_SCRATCH);
53 	else
54 		bios_2_scratch = RREG32(RADEON_BIOS_2_SCRATCH);
55 
56 	backlight_level = ((bios_2_scratch & ATOM_S2_CURRENT_BL_LEVEL_MASK) >>
57 			   ATOM_S2_CURRENT_BL_LEVEL_SHIFT);
58 
59 	return backlight_level;
60 }
61 
62 static void
radeon_atom_set_backlight_level_to_reg(struct radeon_device * rdev,u8 backlight_level)63 radeon_atom_set_backlight_level_to_reg(struct radeon_device *rdev,
64 				       u8 backlight_level)
65 {
66 	u32 bios_2_scratch;
67 
68 	if (rdev->family >= CHIP_R600)
69 		bios_2_scratch = RREG32(R600_BIOS_2_SCRATCH);
70 	else
71 		bios_2_scratch = RREG32(RADEON_BIOS_2_SCRATCH);
72 
73 	bios_2_scratch &= ~ATOM_S2_CURRENT_BL_LEVEL_MASK;
74 	bios_2_scratch |= ((backlight_level << ATOM_S2_CURRENT_BL_LEVEL_SHIFT) &
75 			   ATOM_S2_CURRENT_BL_LEVEL_MASK);
76 
77 	if (rdev->family >= CHIP_R600)
78 		WREG32(R600_BIOS_2_SCRATCH, bios_2_scratch);
79 	else
80 		WREG32(RADEON_BIOS_2_SCRATCH, bios_2_scratch);
81 }
82 
83 u8
atombios_get_backlight_level(struct radeon_encoder * radeon_encoder)84 atombios_get_backlight_level(struct radeon_encoder *radeon_encoder)
85 {
86 	struct drm_device *dev = radeon_encoder->base.dev;
87 	struct radeon_device *rdev = dev->dev_private;
88 
89 	if (!(rdev->mode_info.firmware_flags & ATOM_BIOS_INFO_BL_CONTROLLED_BY_GPU))
90 		return 0;
91 
92 	return radeon_atom_get_backlight_level_from_reg(rdev);
93 }
94 
95 void
atombios_set_backlight_level(struct radeon_encoder * radeon_encoder,u8 level)96 atombios_set_backlight_level(struct radeon_encoder *radeon_encoder, u8 level)
97 {
98 	struct drm_encoder *encoder = &radeon_encoder->base;
99 	struct drm_device *dev = radeon_encoder->base.dev;
100 	struct radeon_device *rdev = dev->dev_private;
101 	struct radeon_encoder_atom_dig *dig;
102 	DISPLAY_DEVICE_OUTPUT_CONTROL_PS_ALLOCATION args;
103 	int index;
104 
105 	if (!(rdev->mode_info.firmware_flags & ATOM_BIOS_INFO_BL_CONTROLLED_BY_GPU))
106 		return;
107 
108 	if ((radeon_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT)) &&
109 	    radeon_encoder->enc_priv) {
110 		dig = radeon_encoder->enc_priv;
111 		dig->backlight_level = level;
112 		radeon_atom_set_backlight_level_to_reg(rdev, dig->backlight_level);
113 
114 		switch (radeon_encoder->encoder_id) {
115 		case ENCODER_OBJECT_ID_INTERNAL_LVDS:
116 		case ENCODER_OBJECT_ID_INTERNAL_LVTM1:
117 			index = GetIndexIntoMasterTable(COMMAND, LCD1OutputControl);
118 			if (dig->backlight_level == 0) {
119 				args.ucAction = ATOM_LCD_BLOFF;
120 				atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
121 			} else {
122 				args.ucAction = ATOM_LCD_BL_BRIGHTNESS_CONTROL;
123 				atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
124 				args.ucAction = ATOM_LCD_BLON;
125 				atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
126 			}
127 			break;
128 		case ENCODER_OBJECT_ID_INTERNAL_UNIPHY:
129 		case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_LVTMA:
130 		case ENCODER_OBJECT_ID_INTERNAL_UNIPHY1:
131 		case ENCODER_OBJECT_ID_INTERNAL_UNIPHY2:
132 		case ENCODER_OBJECT_ID_INTERNAL_UNIPHY3:
133 			if (dig->backlight_level == 0)
134 				atombios_dig_transmitter_setup(encoder, ATOM_TRANSMITTER_ACTION_LCD_BLOFF, 0, 0);
135 			else {
136 				atombios_dig_transmitter_setup(encoder, ATOM_TRANSMITTER_ACTION_BL_BRIGHTNESS_CONTROL, 0, 0);
137 				atombios_dig_transmitter_setup(encoder, ATOM_TRANSMITTER_ACTION_LCD_BLON, 0, 0);
138 			}
139 			break;
140 		default:
141 			break;
142 		}
143 	}
144 }
145 
radeon_atom_bl_level(struct backlight_device * bd)146 static u8 radeon_atom_bl_level(struct backlight_device *bd)
147 {
148 	u8 level;
149 
150 	/* Convert brightness to hardware level */
151 	if (bd->props.brightness < 0)
152 		level = 0;
153 	else if (bd->props.brightness > RADEON_MAX_BL_LEVEL)
154 		level = RADEON_MAX_BL_LEVEL;
155 	else
156 		level = bd->props.brightness;
157 
158 	return level;
159 }
160 
radeon_atom_backlight_update_status(struct backlight_device * bd)161 static int radeon_atom_backlight_update_status(struct backlight_device *bd)
162 {
163 	struct radeon_backlight_privdata *pdata = bl_get_data(bd);
164 	struct radeon_encoder *radeon_encoder = pdata->encoder;
165 
166 	atombios_set_backlight_level(radeon_encoder, radeon_atom_bl_level(bd));
167 
168 	return 0;
169 }
170 
radeon_atom_backlight_get_brightness(struct backlight_device * bd)171 static int radeon_atom_backlight_get_brightness(struct backlight_device *bd)
172 {
173 	struct radeon_backlight_privdata *pdata = bl_get_data(bd);
174 	struct radeon_encoder *radeon_encoder = pdata->encoder;
175 	struct drm_device *dev = radeon_encoder->base.dev;
176 	struct radeon_device *rdev = dev->dev_private;
177 
178 	return radeon_atom_get_backlight_level_from_reg(rdev);
179 }
180 
181 static const struct backlight_ops radeon_atom_backlight_ops = {
182 	.get_brightness = radeon_atom_backlight_get_brightness,
183 	.update_status	= radeon_atom_backlight_update_status,
184 };
185 
radeon_atom_backlight_init(struct radeon_encoder * radeon_encoder,struct drm_connector * drm_connector)186 void radeon_atom_backlight_init(struct radeon_encoder *radeon_encoder,
187 				struct drm_connector *drm_connector)
188 {
189 	struct drm_device *dev = radeon_encoder->base.dev;
190 	struct radeon_device *rdev = dev->dev_private;
191 	struct backlight_device *bd;
192 	struct backlight_properties props;
193 	struct radeon_backlight_privdata *pdata;
194 	struct radeon_encoder_atom_dig *dig;
195 	char bl_name[16];
196 
197 	/* Mac laptops with multiple GPUs use the gmux driver for backlight
198 	 * so don't register a backlight device
199 	 */
200 	if ((rdev->pdev->subsystem_vendor == PCI_VENDOR_ID_APPLE) &&
201 	    (rdev->pdev->device == 0x6741) &&
202 	    !dmi_match(DMI_PRODUCT_NAME, "iMac12,1"))
203 		return;
204 
205 	if (!radeon_encoder->enc_priv)
206 		return;
207 
208 	if (!rdev->is_atom_bios)
209 		return;
210 
211 	if (!(rdev->mode_info.firmware_flags & ATOM_BIOS_INFO_BL_CONTROLLED_BY_GPU))
212 		return;
213 
214 	if (!acpi_video_backlight_use_native()) {
215 		drm_info(dev, "Skipping radeon atom DIG backlight registration\n");
216 		return;
217 	}
218 
219 	pdata = kmalloc(sizeof(struct radeon_backlight_privdata), GFP_KERNEL);
220 	if (!pdata) {
221 		DRM_ERROR("Memory allocation failed\n");
222 		goto error;
223 	}
224 
225 	memset(&props, 0, sizeof(props));
226 	props.max_brightness = RADEON_MAX_BL_LEVEL;
227 	props.type = BACKLIGHT_RAW;
228 	snprintf(bl_name, sizeof(bl_name),
229 		 "radeon_bl%d", dev->primary->index);
230 	bd = backlight_device_register(bl_name, drm_connector->kdev,
231 				       pdata, &radeon_atom_backlight_ops, &props);
232 	if (IS_ERR(bd)) {
233 		DRM_ERROR("Backlight registration failed\n");
234 		goto error;
235 	}
236 
237 	pdata->encoder = radeon_encoder;
238 
239 	dig = radeon_encoder->enc_priv;
240 	dig->bl_dev = bd;
241 
242 	bd->props.brightness = radeon_atom_backlight_get_brightness(bd);
243 	/* Set a reasonable default here if the level is 0 otherwise
244 	 * fbdev will attempt to turn the backlight on after console
245 	 * unblanking and it will try and restore 0 which turns the backlight
246 	 * off again.
247 	 */
248 	if (bd->props.brightness == 0)
249 		bd->props.brightness = RADEON_MAX_BL_LEVEL;
250 	bd->props.power = FB_BLANK_UNBLANK;
251 	backlight_update_status(bd);
252 
253 	DRM_INFO("radeon atom DIG backlight initialized\n");
254 	rdev->mode_info.bl_encoder = radeon_encoder;
255 
256 	return;
257 
258 error:
259 	kfree(pdata);
260 	return;
261 }
262 
radeon_atom_backlight_exit(struct radeon_encoder * radeon_encoder)263 static void radeon_atom_backlight_exit(struct radeon_encoder *radeon_encoder)
264 {
265 	struct drm_device *dev = radeon_encoder->base.dev;
266 	struct radeon_device *rdev = dev->dev_private;
267 	struct backlight_device *bd = NULL;
268 	struct radeon_encoder_atom_dig *dig;
269 
270 	if (!radeon_encoder->enc_priv)
271 		return;
272 
273 	if (!rdev->is_atom_bios)
274 		return;
275 
276 	if (!(rdev->mode_info.firmware_flags & ATOM_BIOS_INFO_BL_CONTROLLED_BY_GPU))
277 		return;
278 
279 	dig = radeon_encoder->enc_priv;
280 	bd = dig->bl_dev;
281 	dig->bl_dev = NULL;
282 
283 	if (bd) {
284 		struct radeon_legacy_backlight_privdata *pdata;
285 
286 		pdata = bl_get_data(bd);
287 		backlight_device_unregister(bd);
288 		kfree(pdata);
289 
290 		DRM_INFO("radeon atom LVDS backlight unloaded\n");
291 	}
292 }
293 
radeon_atom_mode_fixup(struct drm_encoder * encoder,const struct drm_display_mode * mode,struct drm_display_mode * adjusted_mode)294 static bool radeon_atom_mode_fixup(struct drm_encoder *encoder,
295 				   const struct drm_display_mode *mode,
296 				   struct drm_display_mode *adjusted_mode)
297 {
298 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
299 	struct drm_device *dev = encoder->dev;
300 	struct radeon_device *rdev = dev->dev_private;
301 
302 	/* set the active encoder to connector routing */
303 	radeon_encoder_set_active_device(encoder);
304 	drm_mode_set_crtcinfo(adjusted_mode, 0);
305 
306 	/* hw bug */
307 	if ((mode->flags & DRM_MODE_FLAG_INTERLACE)
308 	    && (mode->crtc_vsync_start < (mode->crtc_vdisplay + 2)))
309 		adjusted_mode->crtc_vsync_start = adjusted_mode->crtc_vdisplay + 2;
310 
311 	/* vertical FP must be at least 1 */
312 	if (mode->crtc_vsync_start == mode->crtc_vdisplay)
313 		adjusted_mode->crtc_vsync_start++;
314 
315 	/* get the native mode for scaling */
316 	if (radeon_encoder->active_device & (ATOM_DEVICE_LCD_SUPPORT)) {
317 		radeon_panel_mode_fixup(encoder, adjusted_mode);
318 	} else if (radeon_encoder->active_device & (ATOM_DEVICE_TV_SUPPORT)) {
319 		struct radeon_encoder_atom_dac *tv_dac = radeon_encoder->enc_priv;
320 		if (tv_dac) {
321 			if (tv_dac->tv_std == TV_STD_NTSC ||
322 			    tv_dac->tv_std == TV_STD_NTSC_J ||
323 			    tv_dac->tv_std == TV_STD_PAL_M)
324 				radeon_atom_get_tv_timings(rdev, 0, adjusted_mode);
325 			else
326 				radeon_atom_get_tv_timings(rdev, 1, adjusted_mode);
327 		}
328 	} else if (radeon_encoder->rmx_type != RMX_OFF) {
329 		radeon_panel_mode_fixup(encoder, adjusted_mode);
330 	}
331 
332 	if (ASIC_IS_DCE3(rdev) &&
333 	    ((radeon_encoder->active_device & (ATOM_DEVICE_DFP_SUPPORT | ATOM_DEVICE_LCD_SUPPORT)) ||
334 	     (radeon_encoder_get_dp_bridge_encoder_id(encoder) != ENCODER_OBJECT_ID_NONE))) {
335 		struct drm_connector *connector = radeon_get_connector_for_encoder(encoder);
336 		radeon_dp_set_link_config(connector, adjusted_mode);
337 	}
338 
339 	return true;
340 }
341 
342 static void
atombios_dac_setup(struct drm_encoder * encoder,int action)343 atombios_dac_setup(struct drm_encoder *encoder, int action)
344 {
345 	struct drm_device *dev = encoder->dev;
346 	struct radeon_device *rdev = dev->dev_private;
347 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
348 	DAC_ENCODER_CONTROL_PS_ALLOCATION args;
349 	int index = 0;
350 	struct radeon_encoder_atom_dac *dac_info = radeon_encoder->enc_priv;
351 
352 	memset(&args, 0, sizeof(args));
353 
354 	switch (radeon_encoder->encoder_id) {
355 	case ENCODER_OBJECT_ID_INTERNAL_DAC1:
356 	case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DAC1:
357 		index = GetIndexIntoMasterTable(COMMAND, DAC1EncoderControl);
358 		break;
359 	case ENCODER_OBJECT_ID_INTERNAL_DAC2:
360 	case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DAC2:
361 		index = GetIndexIntoMasterTable(COMMAND, DAC2EncoderControl);
362 		break;
363 	}
364 
365 	args.ucAction = action;
366 
367 	if (radeon_encoder->active_device & (ATOM_DEVICE_CRT_SUPPORT))
368 		args.ucDacStandard = ATOM_DAC1_PS2;
369 	else if (radeon_encoder->active_device & (ATOM_DEVICE_CV_SUPPORT))
370 		args.ucDacStandard = ATOM_DAC1_CV;
371 	else {
372 		switch (dac_info->tv_std) {
373 		case TV_STD_PAL:
374 		case TV_STD_PAL_M:
375 		case TV_STD_SCART_PAL:
376 		case TV_STD_SECAM:
377 		case TV_STD_PAL_CN:
378 			args.ucDacStandard = ATOM_DAC1_PAL;
379 			break;
380 		case TV_STD_NTSC:
381 		case TV_STD_NTSC_J:
382 		case TV_STD_PAL_60:
383 		default:
384 			args.ucDacStandard = ATOM_DAC1_NTSC;
385 			break;
386 		}
387 	}
388 	args.usPixelClock = cpu_to_le16(radeon_encoder->pixel_clock / 10);
389 
390 	atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
391 
392 }
393 
394 static void
atombios_tv_setup(struct drm_encoder * encoder,int action)395 atombios_tv_setup(struct drm_encoder *encoder, int action)
396 {
397 	struct drm_device *dev = encoder->dev;
398 	struct radeon_device *rdev = dev->dev_private;
399 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
400 	TV_ENCODER_CONTROL_PS_ALLOCATION args;
401 	int index = 0;
402 	struct radeon_encoder_atom_dac *dac_info = radeon_encoder->enc_priv;
403 
404 	memset(&args, 0, sizeof(args));
405 
406 	index = GetIndexIntoMasterTable(COMMAND, TVEncoderControl);
407 
408 	args.sTVEncoder.ucAction = action;
409 
410 	if (radeon_encoder->active_device & (ATOM_DEVICE_CV_SUPPORT))
411 		args.sTVEncoder.ucTvStandard = ATOM_TV_CV;
412 	else {
413 		switch (dac_info->tv_std) {
414 		case TV_STD_NTSC:
415 			args.sTVEncoder.ucTvStandard = ATOM_TV_NTSC;
416 			break;
417 		case TV_STD_PAL:
418 			args.sTVEncoder.ucTvStandard = ATOM_TV_PAL;
419 			break;
420 		case TV_STD_PAL_M:
421 			args.sTVEncoder.ucTvStandard = ATOM_TV_PALM;
422 			break;
423 		case TV_STD_PAL_60:
424 			args.sTVEncoder.ucTvStandard = ATOM_TV_PAL60;
425 			break;
426 		case TV_STD_NTSC_J:
427 			args.sTVEncoder.ucTvStandard = ATOM_TV_NTSCJ;
428 			break;
429 		case TV_STD_SCART_PAL:
430 			args.sTVEncoder.ucTvStandard = ATOM_TV_PAL; /* ??? */
431 			break;
432 		case TV_STD_SECAM:
433 			args.sTVEncoder.ucTvStandard = ATOM_TV_SECAM;
434 			break;
435 		case TV_STD_PAL_CN:
436 			args.sTVEncoder.ucTvStandard = ATOM_TV_PALCN;
437 			break;
438 		default:
439 			args.sTVEncoder.ucTvStandard = ATOM_TV_NTSC;
440 			break;
441 		}
442 	}
443 
444 	args.sTVEncoder.usPixelClock = cpu_to_le16(radeon_encoder->pixel_clock / 10);
445 
446 	atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
447 
448 }
449 
radeon_atom_get_bpc(struct drm_encoder * encoder)450 static u8 radeon_atom_get_bpc(struct drm_encoder *encoder)
451 {
452 	int bpc = 8;
453 
454 	if (encoder->crtc) {
455 		struct radeon_crtc *radeon_crtc = to_radeon_crtc(encoder->crtc);
456 		bpc = radeon_crtc->bpc;
457 	}
458 
459 	switch (bpc) {
460 	case 0:
461 		return PANEL_BPC_UNDEFINE;
462 	case 6:
463 		return PANEL_6BIT_PER_COLOR;
464 	case 8:
465 	default:
466 		return PANEL_8BIT_PER_COLOR;
467 	case 10:
468 		return PANEL_10BIT_PER_COLOR;
469 	case 12:
470 		return PANEL_12BIT_PER_COLOR;
471 	case 16:
472 		return PANEL_16BIT_PER_COLOR;
473 	}
474 }
475 
476 union dvo_encoder_control {
477 	ENABLE_EXTERNAL_TMDS_ENCODER_PS_ALLOCATION ext_tmds;
478 	DVO_ENCODER_CONTROL_PS_ALLOCATION dvo;
479 	DVO_ENCODER_CONTROL_PS_ALLOCATION_V3 dvo_v3;
480 	DVO_ENCODER_CONTROL_PS_ALLOCATION_V1_4 dvo_v4;
481 };
482 
483 void
atombios_dvo_setup(struct drm_encoder * encoder,int action)484 atombios_dvo_setup(struct drm_encoder *encoder, int action)
485 {
486 	struct drm_device *dev = encoder->dev;
487 	struct radeon_device *rdev = dev->dev_private;
488 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
489 	union dvo_encoder_control args;
490 	int index = GetIndexIntoMasterTable(COMMAND, DVOEncoderControl);
491 	uint8_t frev, crev;
492 
493 	memset(&args, 0, sizeof(args));
494 
495 	if (!atom_parse_cmd_header(rdev->mode_info.atom_context, index, &frev, &crev))
496 		return;
497 
498 	/* some R4xx chips have the wrong frev */
499 	if (rdev->family <= CHIP_RV410)
500 		frev = 1;
501 
502 	switch (frev) {
503 	case 1:
504 		switch (crev) {
505 		case 1:
506 			/* R4xx, R5xx */
507 			args.ext_tmds.sXTmdsEncoder.ucEnable = action;
508 
509 			if (radeon_dig_monitor_is_duallink(encoder, radeon_encoder->pixel_clock))
510 				args.ext_tmds.sXTmdsEncoder.ucMisc |= PANEL_ENCODER_MISC_DUAL;
511 
512 			args.ext_tmds.sXTmdsEncoder.ucMisc |= ATOM_PANEL_MISC_888RGB;
513 			break;
514 		case 2:
515 			/* RS600/690/740 */
516 			args.dvo.sDVOEncoder.ucAction = action;
517 			args.dvo.sDVOEncoder.usPixelClock = cpu_to_le16(radeon_encoder->pixel_clock / 10);
518 			/* DFP1, CRT1, TV1 depending on the type of port */
519 			args.dvo.sDVOEncoder.ucDeviceType = ATOM_DEVICE_DFP1_INDEX;
520 
521 			if (radeon_dig_monitor_is_duallink(encoder, radeon_encoder->pixel_clock))
522 				args.dvo.sDVOEncoder.usDevAttr.sDigAttrib.ucAttribute |= PANEL_ENCODER_MISC_DUAL;
523 			break;
524 		case 3:
525 			/* R6xx */
526 			args.dvo_v3.ucAction = action;
527 			args.dvo_v3.usPixelClock = cpu_to_le16(radeon_encoder->pixel_clock / 10);
528 			args.dvo_v3.ucDVOConfig = 0; /* XXX */
529 			break;
530 		case 4:
531 			/* DCE8 */
532 			args.dvo_v4.ucAction = action;
533 			args.dvo_v4.usPixelClock = cpu_to_le16(radeon_encoder->pixel_clock / 10);
534 			args.dvo_v4.ucDVOConfig = 0; /* XXX */
535 			args.dvo_v4.ucBitPerColor = radeon_atom_get_bpc(encoder);
536 			break;
537 		default:
538 			DRM_ERROR("Unknown table version %d, %d\n", frev, crev);
539 			break;
540 		}
541 		break;
542 	default:
543 		DRM_ERROR("Unknown table version %d, %d\n", frev, crev);
544 		break;
545 	}
546 
547 	atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
548 }
549 
550 union lvds_encoder_control {
551 	LVDS_ENCODER_CONTROL_PS_ALLOCATION    v1;
552 	LVDS_ENCODER_CONTROL_PS_ALLOCATION_V2 v2;
553 };
554 
555 void
atombios_digital_setup(struct drm_encoder * encoder,int action)556 atombios_digital_setup(struct drm_encoder *encoder, int action)
557 {
558 	struct drm_device *dev = encoder->dev;
559 	struct radeon_device *rdev = dev->dev_private;
560 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
561 	struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
562 	union lvds_encoder_control args;
563 	int index = 0;
564 	int hdmi_detected = 0;
565 	uint8_t frev, crev;
566 
567 	if (!dig)
568 		return;
569 
570 	if (atombios_get_encoder_mode(encoder) == ATOM_ENCODER_MODE_HDMI)
571 		hdmi_detected = 1;
572 
573 	memset(&args, 0, sizeof(args));
574 
575 	switch (radeon_encoder->encoder_id) {
576 	case ENCODER_OBJECT_ID_INTERNAL_LVDS:
577 		index = GetIndexIntoMasterTable(COMMAND, LVDSEncoderControl);
578 		break;
579 	case ENCODER_OBJECT_ID_INTERNAL_TMDS1:
580 	case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_TMDS1:
581 		index = GetIndexIntoMasterTable(COMMAND, TMDS1EncoderControl);
582 		break;
583 	case ENCODER_OBJECT_ID_INTERNAL_LVTM1:
584 		if (radeon_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT))
585 			index = GetIndexIntoMasterTable(COMMAND, LVDSEncoderControl);
586 		else
587 			index = GetIndexIntoMasterTable(COMMAND, TMDS2EncoderControl);
588 		break;
589 	}
590 
591 	if (!atom_parse_cmd_header(rdev->mode_info.atom_context, index, &frev, &crev))
592 		return;
593 
594 	switch (frev) {
595 	case 1:
596 	case 2:
597 		switch (crev) {
598 		case 1:
599 			args.v1.ucMisc = 0;
600 			args.v1.ucAction = action;
601 			if (hdmi_detected)
602 				args.v1.ucMisc |= PANEL_ENCODER_MISC_HDMI_TYPE;
603 			args.v1.usPixelClock = cpu_to_le16(radeon_encoder->pixel_clock / 10);
604 			if (radeon_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT)) {
605 				if (dig->lcd_misc & ATOM_PANEL_MISC_DUAL)
606 					args.v1.ucMisc |= PANEL_ENCODER_MISC_DUAL;
607 				if (dig->lcd_misc & ATOM_PANEL_MISC_888RGB)
608 					args.v1.ucMisc |= ATOM_PANEL_MISC_888RGB;
609 			} else {
610 				if (dig->linkb)
611 					args.v1.ucMisc |= PANEL_ENCODER_MISC_TMDS_LINKB;
612 				if (radeon_dig_monitor_is_duallink(encoder, radeon_encoder->pixel_clock))
613 					args.v1.ucMisc |= PANEL_ENCODER_MISC_DUAL;
614 				/*if (pScrn->rgbBits == 8) */
615 				args.v1.ucMisc |= ATOM_PANEL_MISC_888RGB;
616 			}
617 			break;
618 		case 2:
619 		case 3:
620 			args.v2.ucMisc = 0;
621 			args.v2.ucAction = action;
622 			if (crev == 3) {
623 				if (dig->coherent_mode)
624 					args.v2.ucMisc |= PANEL_ENCODER_MISC_COHERENT;
625 			}
626 			if (hdmi_detected)
627 				args.v2.ucMisc |= PANEL_ENCODER_MISC_HDMI_TYPE;
628 			args.v2.usPixelClock = cpu_to_le16(radeon_encoder->pixel_clock / 10);
629 			args.v2.ucTruncate = 0;
630 			args.v2.ucSpatial = 0;
631 			args.v2.ucTemporal = 0;
632 			args.v2.ucFRC = 0;
633 			if (radeon_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT)) {
634 				if (dig->lcd_misc & ATOM_PANEL_MISC_DUAL)
635 					args.v2.ucMisc |= PANEL_ENCODER_MISC_DUAL;
636 				if (dig->lcd_misc & ATOM_PANEL_MISC_SPATIAL) {
637 					args.v2.ucSpatial = PANEL_ENCODER_SPATIAL_DITHER_EN;
638 					if (dig->lcd_misc & ATOM_PANEL_MISC_888RGB)
639 						args.v2.ucSpatial |= PANEL_ENCODER_SPATIAL_DITHER_DEPTH;
640 				}
641 				if (dig->lcd_misc & ATOM_PANEL_MISC_TEMPORAL) {
642 					args.v2.ucTemporal = PANEL_ENCODER_TEMPORAL_DITHER_EN;
643 					if (dig->lcd_misc & ATOM_PANEL_MISC_888RGB)
644 						args.v2.ucTemporal |= PANEL_ENCODER_TEMPORAL_DITHER_DEPTH;
645 					if (((dig->lcd_misc >> ATOM_PANEL_MISC_GREY_LEVEL_SHIFT) & 0x3) == 2)
646 						args.v2.ucTemporal |= PANEL_ENCODER_TEMPORAL_LEVEL_4;
647 				}
648 			} else {
649 				if (dig->linkb)
650 					args.v2.ucMisc |= PANEL_ENCODER_MISC_TMDS_LINKB;
651 				if (radeon_dig_monitor_is_duallink(encoder, radeon_encoder->pixel_clock))
652 					args.v2.ucMisc |= PANEL_ENCODER_MISC_DUAL;
653 			}
654 			break;
655 		default:
656 			DRM_ERROR("Unknown table version %d, %d\n", frev, crev);
657 			break;
658 		}
659 		break;
660 	default:
661 		DRM_ERROR("Unknown table version %d, %d\n", frev, crev);
662 		break;
663 	}
664 
665 	atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
666 }
667 
668 int
atombios_get_encoder_mode(struct drm_encoder * encoder)669 atombios_get_encoder_mode(struct drm_encoder *encoder)
670 {
671 	struct drm_device *dev = encoder->dev;
672 	struct radeon_device *rdev = dev->dev_private;
673 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
674 	struct drm_connector *connector;
675 	struct radeon_connector *radeon_connector;
676 	struct radeon_connector_atom_dig *dig_connector;
677 
678 	/* dp bridges are always DP */
679 	if (radeon_encoder_get_dp_bridge_encoder_id(encoder) != ENCODER_OBJECT_ID_NONE)
680 		return ATOM_ENCODER_MODE_DP;
681 
682 	/* DVO is always DVO */
683 	if ((radeon_encoder->encoder_id == ENCODER_OBJECT_ID_INTERNAL_DVO1) ||
684 	    (radeon_encoder->encoder_id == ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DVO1))
685 		return ATOM_ENCODER_MODE_DVO;
686 
687 	connector = radeon_get_connector_for_encoder(encoder);
688 	/* if we don't have an active device yet, just use one of
689 	 * the connectors tied to the encoder.
690 	 */
691 	if (!connector)
692 		connector = radeon_get_connector_for_encoder_init(encoder);
693 	radeon_connector = to_radeon_connector(connector);
694 
695 	switch (connector->connector_type) {
696 	case DRM_MODE_CONNECTOR_DVII:
697 	case DRM_MODE_CONNECTOR_HDMIB: /* HDMI-B is basically DL-DVI; analog works fine */
698 		if (radeon_audio != 0) {
699 			if (radeon_connector->use_digital &&
700 			    (radeon_connector->audio == RADEON_AUDIO_ENABLE))
701 				return ATOM_ENCODER_MODE_HDMI;
702 			else if (drm_detect_hdmi_monitor(radeon_connector_edid(connector)) &&
703 				 (radeon_connector->audio == RADEON_AUDIO_AUTO))
704 				return ATOM_ENCODER_MODE_HDMI;
705 			else if (radeon_connector->use_digital)
706 				return ATOM_ENCODER_MODE_DVI;
707 			else
708 				return ATOM_ENCODER_MODE_CRT;
709 		} else if (radeon_connector->use_digital) {
710 			return ATOM_ENCODER_MODE_DVI;
711 		} else {
712 			return ATOM_ENCODER_MODE_CRT;
713 		}
714 		break;
715 	case DRM_MODE_CONNECTOR_DVID:
716 	case DRM_MODE_CONNECTOR_HDMIA:
717 	default:
718 		if (radeon_audio != 0) {
719 			if (radeon_connector->audio == RADEON_AUDIO_ENABLE)
720 				return ATOM_ENCODER_MODE_HDMI;
721 			else if (drm_detect_hdmi_monitor(radeon_connector_edid(connector)) &&
722 				 (radeon_connector->audio == RADEON_AUDIO_AUTO))
723 				return ATOM_ENCODER_MODE_HDMI;
724 			else
725 				return ATOM_ENCODER_MODE_DVI;
726 		} else {
727 			return ATOM_ENCODER_MODE_DVI;
728 		}
729 		break;
730 	case DRM_MODE_CONNECTOR_LVDS:
731 		return ATOM_ENCODER_MODE_LVDS;
732 		break;
733 	case DRM_MODE_CONNECTOR_DisplayPort:
734 		dig_connector = radeon_connector->con_priv;
735 		if ((dig_connector->dp_sink_type == CONNECTOR_OBJECT_ID_DISPLAYPORT) ||
736 		    (dig_connector->dp_sink_type == CONNECTOR_OBJECT_ID_eDP)) {
737 			if (radeon_audio != 0 &&
738 			    drm_detect_monitor_audio(radeon_connector_edid(connector)) &&
739 			    ASIC_IS_DCE4(rdev) && !ASIC_IS_DCE5(rdev))
740 				return ATOM_ENCODER_MODE_DP_AUDIO;
741 			return ATOM_ENCODER_MODE_DP;
742 		} else if (radeon_audio != 0) {
743 			if (radeon_connector->audio == RADEON_AUDIO_ENABLE)
744 				return ATOM_ENCODER_MODE_HDMI;
745 			else if (drm_detect_hdmi_monitor(radeon_connector_edid(connector)) &&
746 				 (radeon_connector->audio == RADEON_AUDIO_AUTO))
747 				return ATOM_ENCODER_MODE_HDMI;
748 			else
749 				return ATOM_ENCODER_MODE_DVI;
750 		} else {
751 			return ATOM_ENCODER_MODE_DVI;
752 		}
753 		break;
754 	case DRM_MODE_CONNECTOR_eDP:
755 		if (radeon_audio != 0 &&
756 		    drm_detect_monitor_audio(radeon_connector_edid(connector)) &&
757 		    ASIC_IS_DCE4(rdev) && !ASIC_IS_DCE5(rdev))
758 			return ATOM_ENCODER_MODE_DP_AUDIO;
759 		return ATOM_ENCODER_MODE_DP;
760 	case DRM_MODE_CONNECTOR_DVIA:
761 	case DRM_MODE_CONNECTOR_VGA:
762 		return ATOM_ENCODER_MODE_CRT;
763 		break;
764 	case DRM_MODE_CONNECTOR_Composite:
765 	case DRM_MODE_CONNECTOR_SVIDEO:
766 	case DRM_MODE_CONNECTOR_9PinDIN:
767 		/* fix me */
768 		return ATOM_ENCODER_MODE_TV;
769 		/*return ATOM_ENCODER_MODE_CV;*/
770 		break;
771 	}
772 }
773 
774 /*
775  * DIG Encoder/Transmitter Setup
776  *
777  * DCE 3.0/3.1
778  * - 2 DIG transmitter blocks. UNIPHY (links A and B) and LVTMA.
779  * Supports up to 3 digital outputs
780  * - 2 DIG encoder blocks.
781  * DIG1 can drive UNIPHY link A or link B
782  * DIG2 can drive UNIPHY link B or LVTMA
783  *
784  * DCE 3.2
785  * - 3 DIG transmitter blocks. UNIPHY0/1/2 (links A and B).
786  * Supports up to 5 digital outputs
787  * - 2 DIG encoder blocks.
788  * DIG1/2 can drive UNIPHY0/1/2 link A or link B
789  *
790  * DCE 4.0/5.0/6.0
791  * - 3 DIG transmitter blocks UNIPHY0/1/2 (links A and B).
792  * Supports up to 6 digital outputs
793  * - 6 DIG encoder blocks.
794  * - DIG to PHY mapping is hardcoded
795  * DIG1 drives UNIPHY0 link A, A+B
796  * DIG2 drives UNIPHY0 link B
797  * DIG3 drives UNIPHY1 link A, A+B
798  * DIG4 drives UNIPHY1 link B
799  * DIG5 drives UNIPHY2 link A, A+B
800  * DIG6 drives UNIPHY2 link B
801  *
802  * DCE 4.1
803  * - 3 DIG transmitter blocks UNIPHY0/1/2 (links A and B).
804  * Supports up to 6 digital outputs
805  * - 2 DIG encoder blocks.
806  * llano
807  * DIG1/2 can drive UNIPHY0/1/2 link A or link B
808  * ontario
809  * DIG1 drives UNIPHY0/1/2 link A
810  * DIG2 drives UNIPHY0/1/2 link B
811  *
812  * Routing
813  * crtc -> dig encoder -> UNIPHY/LVTMA (1 or 2 links)
814  * Examples:
815  * crtc0 -> dig2 -> LVTMA   links A+B -> TMDS/HDMI
816  * crtc1 -> dig1 -> UNIPHY0 link  B   -> DP
817  * crtc0 -> dig1 -> UNIPHY2 link  A   -> LVDS
818  * crtc1 -> dig2 -> UNIPHY1 link  B+A -> TMDS/HDMI
819  */
820 
821 union dig_encoder_control {
822 	DIG_ENCODER_CONTROL_PS_ALLOCATION v1;
823 	DIG_ENCODER_CONTROL_PARAMETERS_V2 v2;
824 	DIG_ENCODER_CONTROL_PARAMETERS_V3 v3;
825 	DIG_ENCODER_CONTROL_PARAMETERS_V4 v4;
826 };
827 
828 void
atombios_dig_encoder_setup2(struct drm_encoder * encoder,int action,int panel_mode,int enc_override)829 atombios_dig_encoder_setup2(struct drm_encoder *encoder, int action, int panel_mode, int enc_override)
830 {
831 	struct drm_device *dev = encoder->dev;
832 	struct radeon_device *rdev = dev->dev_private;
833 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
834 	struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
835 	struct drm_connector *connector = radeon_get_connector_for_encoder(encoder);
836 	union dig_encoder_control args;
837 	int index = 0;
838 	uint8_t frev, crev;
839 	int dp_clock = 0;
840 	int dp_lane_count = 0;
841 	int hpd_id = RADEON_HPD_NONE;
842 
843 	if (connector) {
844 		struct radeon_connector *radeon_connector = to_radeon_connector(connector);
845 		struct radeon_connector_atom_dig *dig_connector =
846 			radeon_connector->con_priv;
847 
848 		dp_clock = dig_connector->dp_clock;
849 		dp_lane_count = dig_connector->dp_lane_count;
850 		hpd_id = radeon_connector->hpd.hpd;
851 	}
852 
853 	/* no dig encoder assigned */
854 	if (dig->dig_encoder == -1)
855 		return;
856 
857 	memset(&args, 0, sizeof(args));
858 
859 	if (ASIC_IS_DCE4(rdev))
860 		index = GetIndexIntoMasterTable(COMMAND, DIGxEncoderControl);
861 	else {
862 		if (dig->dig_encoder)
863 			index = GetIndexIntoMasterTable(COMMAND, DIG2EncoderControl);
864 		else
865 			index = GetIndexIntoMasterTable(COMMAND, DIG1EncoderControl);
866 	}
867 
868 	if (!atom_parse_cmd_header(rdev->mode_info.atom_context, index, &frev, &crev))
869 		return;
870 
871 	switch (frev) {
872 	case 1:
873 		switch (crev) {
874 		case 1:
875 			args.v1.ucAction = action;
876 			args.v1.usPixelClock = cpu_to_le16(radeon_encoder->pixel_clock / 10);
877 			if (action == ATOM_ENCODER_CMD_SETUP_PANEL_MODE)
878 				args.v3.ucPanelMode = panel_mode;
879 			else
880 				args.v1.ucEncoderMode = atombios_get_encoder_mode(encoder);
881 
882 			if (ENCODER_MODE_IS_DP(args.v1.ucEncoderMode))
883 				args.v1.ucLaneNum = dp_lane_count;
884 			else if (radeon_dig_monitor_is_duallink(encoder, radeon_encoder->pixel_clock))
885 				args.v1.ucLaneNum = 8;
886 			else
887 				args.v1.ucLaneNum = 4;
888 
889 			switch (radeon_encoder->encoder_id) {
890 			case ENCODER_OBJECT_ID_INTERNAL_UNIPHY:
891 				args.v1.ucConfig = ATOM_ENCODER_CONFIG_V2_TRANSMITTER1;
892 				break;
893 			case ENCODER_OBJECT_ID_INTERNAL_UNIPHY1:
894 			case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_LVTMA:
895 				args.v1.ucConfig = ATOM_ENCODER_CONFIG_V2_TRANSMITTER2;
896 				break;
897 			case ENCODER_OBJECT_ID_INTERNAL_UNIPHY2:
898 				args.v1.ucConfig = ATOM_ENCODER_CONFIG_V2_TRANSMITTER3;
899 				break;
900 			}
901 			if (dig->linkb)
902 				args.v1.ucConfig |= ATOM_ENCODER_CONFIG_LINKB;
903 			else
904 				args.v1.ucConfig |= ATOM_ENCODER_CONFIG_LINKA;
905 
906 			if (ENCODER_MODE_IS_DP(args.v1.ucEncoderMode) && (dp_clock == 270000))
907 				args.v1.ucConfig |= ATOM_ENCODER_CONFIG_DPLINKRATE_2_70GHZ;
908 
909 			break;
910 		case 2:
911 		case 3:
912 			args.v3.ucAction = action;
913 			args.v3.usPixelClock = cpu_to_le16(radeon_encoder->pixel_clock / 10);
914 			if (action == ATOM_ENCODER_CMD_SETUP_PANEL_MODE)
915 				args.v3.ucPanelMode = panel_mode;
916 			else
917 				args.v3.ucEncoderMode = atombios_get_encoder_mode(encoder);
918 
919 			if (ENCODER_MODE_IS_DP(args.v3.ucEncoderMode))
920 				args.v3.ucLaneNum = dp_lane_count;
921 			else if (radeon_dig_monitor_is_duallink(encoder, radeon_encoder->pixel_clock))
922 				args.v3.ucLaneNum = 8;
923 			else
924 				args.v3.ucLaneNum = 4;
925 
926 			if (ENCODER_MODE_IS_DP(args.v3.ucEncoderMode) && (dp_clock == 270000))
927 				args.v1.ucConfig |= ATOM_ENCODER_CONFIG_V3_DPLINKRATE_2_70GHZ;
928 			if (enc_override != -1)
929 				args.v3.acConfig.ucDigSel = enc_override;
930 			else
931 				args.v3.acConfig.ucDigSel = dig->dig_encoder;
932 			args.v3.ucBitPerColor = radeon_atom_get_bpc(encoder);
933 			break;
934 		case 4:
935 			args.v4.ucAction = action;
936 			args.v4.usPixelClock = cpu_to_le16(radeon_encoder->pixel_clock / 10);
937 			if (action == ATOM_ENCODER_CMD_SETUP_PANEL_MODE)
938 				args.v4.ucPanelMode = panel_mode;
939 			else
940 				args.v4.ucEncoderMode = atombios_get_encoder_mode(encoder);
941 
942 			if (ENCODER_MODE_IS_DP(args.v4.ucEncoderMode))
943 				args.v4.ucLaneNum = dp_lane_count;
944 			else if (radeon_dig_monitor_is_duallink(encoder, radeon_encoder->pixel_clock))
945 				args.v4.ucLaneNum = 8;
946 			else
947 				args.v4.ucLaneNum = 4;
948 
949 			if (ENCODER_MODE_IS_DP(args.v4.ucEncoderMode)) {
950 				if (dp_clock == 540000)
951 					args.v1.ucConfig |= ATOM_ENCODER_CONFIG_V4_DPLINKRATE_5_40GHZ;
952 				else if (dp_clock == 324000)
953 					args.v1.ucConfig |= ATOM_ENCODER_CONFIG_V4_DPLINKRATE_3_24GHZ;
954 				else if (dp_clock == 270000)
955 					args.v1.ucConfig |= ATOM_ENCODER_CONFIG_V4_DPLINKRATE_2_70GHZ;
956 				else
957 					args.v1.ucConfig |= ATOM_ENCODER_CONFIG_V4_DPLINKRATE_1_62GHZ;
958 			}
959 
960 			if (enc_override != -1)
961 				args.v4.acConfig.ucDigSel = enc_override;
962 			else
963 				args.v4.acConfig.ucDigSel = dig->dig_encoder;
964 			args.v4.ucBitPerColor = radeon_atom_get_bpc(encoder);
965 			if (hpd_id == RADEON_HPD_NONE)
966 				args.v4.ucHPD_ID = 0;
967 			else
968 				args.v4.ucHPD_ID = hpd_id + 1;
969 			break;
970 		default:
971 			DRM_ERROR("Unknown table version %d, %d\n", frev, crev);
972 			break;
973 		}
974 		break;
975 	default:
976 		DRM_ERROR("Unknown table version %d, %d\n", frev, crev);
977 		break;
978 	}
979 
980 	atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
981 
982 }
983 
984 void
atombios_dig_encoder_setup(struct drm_encoder * encoder,int action,int panel_mode)985 atombios_dig_encoder_setup(struct drm_encoder *encoder, int action, int panel_mode)
986 {
987 	atombios_dig_encoder_setup2(encoder, action, panel_mode, -1);
988 }
989 
990 union dig_transmitter_control {
991 	DIG_TRANSMITTER_CONTROL_PS_ALLOCATION v1;
992 	DIG_TRANSMITTER_CONTROL_PARAMETERS_V2 v2;
993 	DIG_TRANSMITTER_CONTROL_PARAMETERS_V3 v3;
994 	DIG_TRANSMITTER_CONTROL_PARAMETERS_V4 v4;
995 	DIG_TRANSMITTER_CONTROL_PARAMETERS_V1_5 v5;
996 };
997 
998 void
atombios_dig_transmitter_setup2(struct drm_encoder * encoder,int action,uint8_t lane_num,uint8_t lane_set,int fe)999 atombios_dig_transmitter_setup2(struct drm_encoder *encoder, int action, uint8_t lane_num, uint8_t lane_set, int fe)
1000 {
1001 	struct drm_device *dev = encoder->dev;
1002 	struct radeon_device *rdev = dev->dev_private;
1003 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
1004 	struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
1005 	struct drm_connector *connector;
1006 	union dig_transmitter_control args;
1007 	int index = 0;
1008 	uint8_t frev, crev;
1009 	bool is_dp = false;
1010 	int pll_id = 0;
1011 	int dp_clock = 0;
1012 	int dp_lane_count = 0;
1013 	int connector_object_id = 0;
1014 	int igp_lane_info = 0;
1015 	int dig_encoder = dig->dig_encoder;
1016 	int hpd_id = RADEON_HPD_NONE;
1017 
1018 	if (action == ATOM_TRANSMITTER_ACTION_INIT) {
1019 		connector = radeon_get_connector_for_encoder_init(encoder);
1020 		/* just needed to avoid bailing in the encoder check.  the encoder
1021 		 * isn't used for init
1022 		 */
1023 		dig_encoder = 0;
1024 	} else
1025 		connector = radeon_get_connector_for_encoder(encoder);
1026 
1027 	if (connector) {
1028 		struct radeon_connector *radeon_connector = to_radeon_connector(connector);
1029 		struct radeon_connector_atom_dig *dig_connector =
1030 			radeon_connector->con_priv;
1031 
1032 		hpd_id = radeon_connector->hpd.hpd;
1033 		dp_clock = dig_connector->dp_clock;
1034 		dp_lane_count = dig_connector->dp_lane_count;
1035 		connector_object_id =
1036 			(radeon_connector->connector_object_id & OBJECT_ID_MASK) >> OBJECT_ID_SHIFT;
1037 		igp_lane_info = dig_connector->igp_lane_info;
1038 	}
1039 
1040 	if (encoder->crtc) {
1041 		struct radeon_crtc *radeon_crtc = to_radeon_crtc(encoder->crtc);
1042 		pll_id = radeon_crtc->pll_id;
1043 	}
1044 
1045 	/* no dig encoder assigned */
1046 	if (dig_encoder == -1)
1047 		return;
1048 
1049 	if (ENCODER_MODE_IS_DP(atombios_get_encoder_mode(encoder)))
1050 		is_dp = true;
1051 
1052 	memset(&args, 0, sizeof(args));
1053 
1054 	switch (radeon_encoder->encoder_id) {
1055 	case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DVO1:
1056 		index = GetIndexIntoMasterTable(COMMAND, DVOOutputControl);
1057 		break;
1058 	case ENCODER_OBJECT_ID_INTERNAL_UNIPHY:
1059 	case ENCODER_OBJECT_ID_INTERNAL_UNIPHY1:
1060 	case ENCODER_OBJECT_ID_INTERNAL_UNIPHY2:
1061 	case ENCODER_OBJECT_ID_INTERNAL_UNIPHY3:
1062 		index = GetIndexIntoMasterTable(COMMAND, UNIPHYTransmitterControl);
1063 		break;
1064 	case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_LVTMA:
1065 		index = GetIndexIntoMasterTable(COMMAND, LVTMATransmitterControl);
1066 		break;
1067 	}
1068 
1069 	if (!atom_parse_cmd_header(rdev->mode_info.atom_context, index, &frev, &crev))
1070 		return;
1071 
1072 	switch (frev) {
1073 	case 1:
1074 		switch (crev) {
1075 		case 1:
1076 			args.v1.ucAction = action;
1077 			if (action == ATOM_TRANSMITTER_ACTION_INIT) {
1078 				args.v1.usInitInfo = cpu_to_le16(connector_object_id);
1079 			} else if (action == ATOM_TRANSMITTER_ACTION_SETUP_VSEMPH) {
1080 				args.v1.asMode.ucLaneSel = lane_num;
1081 				args.v1.asMode.ucLaneSet = lane_set;
1082 			} else {
1083 				if (is_dp)
1084 					args.v1.usPixelClock = cpu_to_le16(dp_clock / 10);
1085 				else if (radeon_dig_monitor_is_duallink(encoder, radeon_encoder->pixel_clock))
1086 					args.v1.usPixelClock = cpu_to_le16((radeon_encoder->pixel_clock / 2) / 10);
1087 				else
1088 					args.v1.usPixelClock = cpu_to_le16(radeon_encoder->pixel_clock / 10);
1089 			}
1090 
1091 			args.v1.ucConfig = ATOM_TRANSMITTER_CONFIG_CLKSRC_PPLL;
1092 
1093 			if (dig_encoder)
1094 				args.v1.ucConfig |= ATOM_TRANSMITTER_CONFIG_DIG2_ENCODER;
1095 			else
1096 				args.v1.ucConfig |= ATOM_TRANSMITTER_CONFIG_DIG1_ENCODER;
1097 
1098 			if ((rdev->flags & RADEON_IS_IGP) &&
1099 			    (radeon_encoder->encoder_id == ENCODER_OBJECT_ID_INTERNAL_UNIPHY)) {
1100 				if (is_dp ||
1101 				    !radeon_dig_monitor_is_duallink(encoder, radeon_encoder->pixel_clock)) {
1102 					if (igp_lane_info & 0x1)
1103 						args.v1.ucConfig |= ATOM_TRANSMITTER_CONFIG_LANE_0_3;
1104 					else if (igp_lane_info & 0x2)
1105 						args.v1.ucConfig |= ATOM_TRANSMITTER_CONFIG_LANE_4_7;
1106 					else if (igp_lane_info & 0x4)
1107 						args.v1.ucConfig |= ATOM_TRANSMITTER_CONFIG_LANE_8_11;
1108 					else if (igp_lane_info & 0x8)
1109 						args.v1.ucConfig |= ATOM_TRANSMITTER_CONFIG_LANE_12_15;
1110 				} else {
1111 					if (igp_lane_info & 0x3)
1112 						args.v1.ucConfig |= ATOM_TRANSMITTER_CONFIG_LANE_0_7;
1113 					else if (igp_lane_info & 0xc)
1114 						args.v1.ucConfig |= ATOM_TRANSMITTER_CONFIG_LANE_8_15;
1115 				}
1116 			}
1117 
1118 			if (dig->linkb)
1119 				args.v1.ucConfig |= ATOM_TRANSMITTER_CONFIG_LINKB;
1120 			else
1121 				args.v1.ucConfig |= ATOM_TRANSMITTER_CONFIG_LINKA;
1122 
1123 			if (is_dp)
1124 				args.v1.ucConfig |= ATOM_TRANSMITTER_CONFIG_COHERENT;
1125 			else if (radeon_encoder->devices & (ATOM_DEVICE_DFP_SUPPORT)) {
1126 				if (dig->coherent_mode)
1127 					args.v1.ucConfig |= ATOM_TRANSMITTER_CONFIG_COHERENT;
1128 				if (radeon_dig_monitor_is_duallink(encoder, radeon_encoder->pixel_clock))
1129 					args.v1.ucConfig |= ATOM_TRANSMITTER_CONFIG_8LANE_LINK;
1130 			}
1131 			break;
1132 		case 2:
1133 			args.v2.ucAction = action;
1134 			if (action == ATOM_TRANSMITTER_ACTION_INIT) {
1135 				args.v2.usInitInfo = cpu_to_le16(connector_object_id);
1136 			} else if (action == ATOM_TRANSMITTER_ACTION_SETUP_VSEMPH) {
1137 				args.v2.asMode.ucLaneSel = lane_num;
1138 				args.v2.asMode.ucLaneSet = lane_set;
1139 			} else {
1140 				if (is_dp)
1141 					args.v2.usPixelClock = cpu_to_le16(dp_clock / 10);
1142 				else if (radeon_dig_monitor_is_duallink(encoder, radeon_encoder->pixel_clock))
1143 					args.v2.usPixelClock = cpu_to_le16((radeon_encoder->pixel_clock / 2) / 10);
1144 				else
1145 					args.v2.usPixelClock = cpu_to_le16(radeon_encoder->pixel_clock / 10);
1146 			}
1147 
1148 			args.v2.acConfig.ucEncoderSel = dig_encoder;
1149 			if (dig->linkb)
1150 				args.v2.acConfig.ucLinkSel = 1;
1151 
1152 			switch (radeon_encoder->encoder_id) {
1153 			case ENCODER_OBJECT_ID_INTERNAL_UNIPHY:
1154 				args.v2.acConfig.ucTransmitterSel = 0;
1155 				break;
1156 			case ENCODER_OBJECT_ID_INTERNAL_UNIPHY1:
1157 				args.v2.acConfig.ucTransmitterSel = 1;
1158 				break;
1159 			case ENCODER_OBJECT_ID_INTERNAL_UNIPHY2:
1160 				args.v2.acConfig.ucTransmitterSel = 2;
1161 				break;
1162 			}
1163 
1164 			if (is_dp) {
1165 				args.v2.acConfig.fCoherentMode = 1;
1166 				args.v2.acConfig.fDPConnector = 1;
1167 			} else if (radeon_encoder->devices & (ATOM_DEVICE_DFP_SUPPORT)) {
1168 				if (dig->coherent_mode)
1169 					args.v2.acConfig.fCoherentMode = 1;
1170 				if (radeon_dig_monitor_is_duallink(encoder, radeon_encoder->pixel_clock))
1171 					args.v2.acConfig.fDualLinkConnector = 1;
1172 			}
1173 			break;
1174 		case 3:
1175 			args.v3.ucAction = action;
1176 			if (action == ATOM_TRANSMITTER_ACTION_INIT) {
1177 				args.v3.usInitInfo = cpu_to_le16(connector_object_id);
1178 			} else if (action == ATOM_TRANSMITTER_ACTION_SETUP_VSEMPH) {
1179 				args.v3.asMode.ucLaneSel = lane_num;
1180 				args.v3.asMode.ucLaneSet = lane_set;
1181 			} else {
1182 				if (is_dp)
1183 					args.v3.usPixelClock = cpu_to_le16(dp_clock / 10);
1184 				else if (radeon_dig_monitor_is_duallink(encoder, radeon_encoder->pixel_clock))
1185 					args.v3.usPixelClock = cpu_to_le16((radeon_encoder->pixel_clock / 2) / 10);
1186 				else
1187 					args.v3.usPixelClock = cpu_to_le16(radeon_encoder->pixel_clock / 10);
1188 			}
1189 
1190 			if (is_dp)
1191 				args.v3.ucLaneNum = dp_lane_count;
1192 			else if (radeon_dig_monitor_is_duallink(encoder, radeon_encoder->pixel_clock))
1193 				args.v3.ucLaneNum = 8;
1194 			else
1195 				args.v3.ucLaneNum = 4;
1196 
1197 			if (dig->linkb)
1198 				args.v3.acConfig.ucLinkSel = 1;
1199 			if (dig_encoder & 1)
1200 				args.v3.acConfig.ucEncoderSel = 1;
1201 
1202 			/* Select the PLL for the PHY
1203 			 * DP PHY should be clocked from external src if there is
1204 			 * one.
1205 			 */
1206 			/* On DCE4, if there is an external clock, it generates the DP ref clock */
1207 			if (is_dp && rdev->clock.dp_extclk)
1208 				args.v3.acConfig.ucRefClkSource = 2; /* external src */
1209 			else
1210 				args.v3.acConfig.ucRefClkSource = pll_id;
1211 
1212 			switch (radeon_encoder->encoder_id) {
1213 			case ENCODER_OBJECT_ID_INTERNAL_UNIPHY:
1214 				args.v3.acConfig.ucTransmitterSel = 0;
1215 				break;
1216 			case ENCODER_OBJECT_ID_INTERNAL_UNIPHY1:
1217 				args.v3.acConfig.ucTransmitterSel = 1;
1218 				break;
1219 			case ENCODER_OBJECT_ID_INTERNAL_UNIPHY2:
1220 				args.v3.acConfig.ucTransmitterSel = 2;
1221 				break;
1222 			}
1223 
1224 			if (is_dp)
1225 				args.v3.acConfig.fCoherentMode = 1; /* DP requires coherent */
1226 			else if (radeon_encoder->devices & (ATOM_DEVICE_DFP_SUPPORT)) {
1227 				if (dig->coherent_mode)
1228 					args.v3.acConfig.fCoherentMode = 1;
1229 				if (radeon_dig_monitor_is_duallink(encoder, radeon_encoder->pixel_clock))
1230 					args.v3.acConfig.fDualLinkConnector = 1;
1231 			}
1232 			break;
1233 		case 4:
1234 			args.v4.ucAction = action;
1235 			if (action == ATOM_TRANSMITTER_ACTION_INIT) {
1236 				args.v4.usInitInfo = cpu_to_le16(connector_object_id);
1237 			} else if (action == ATOM_TRANSMITTER_ACTION_SETUP_VSEMPH) {
1238 				args.v4.asMode.ucLaneSel = lane_num;
1239 				args.v4.asMode.ucLaneSet = lane_set;
1240 			} else {
1241 				if (is_dp)
1242 					args.v4.usPixelClock = cpu_to_le16(dp_clock / 10);
1243 				else if (radeon_dig_monitor_is_duallink(encoder, radeon_encoder->pixel_clock))
1244 					args.v4.usPixelClock = cpu_to_le16((radeon_encoder->pixel_clock / 2) / 10);
1245 				else
1246 					args.v4.usPixelClock = cpu_to_le16(radeon_encoder->pixel_clock / 10);
1247 			}
1248 
1249 			if (is_dp)
1250 				args.v4.ucLaneNum = dp_lane_count;
1251 			else if (radeon_dig_monitor_is_duallink(encoder, radeon_encoder->pixel_clock))
1252 				args.v4.ucLaneNum = 8;
1253 			else
1254 				args.v4.ucLaneNum = 4;
1255 
1256 			if (dig->linkb)
1257 				args.v4.acConfig.ucLinkSel = 1;
1258 			if (dig_encoder & 1)
1259 				args.v4.acConfig.ucEncoderSel = 1;
1260 
1261 			/* Select the PLL for the PHY
1262 			 * DP PHY should be clocked from external src if there is
1263 			 * one.
1264 			 */
1265 			/* On DCE5 DCPLL usually generates the DP ref clock */
1266 			if (is_dp) {
1267 				if (rdev->clock.dp_extclk)
1268 					args.v4.acConfig.ucRefClkSource = ENCODER_REFCLK_SRC_EXTCLK;
1269 				else
1270 					args.v4.acConfig.ucRefClkSource = ENCODER_REFCLK_SRC_DCPLL;
1271 			} else
1272 				args.v4.acConfig.ucRefClkSource = pll_id;
1273 
1274 			switch (radeon_encoder->encoder_id) {
1275 			case ENCODER_OBJECT_ID_INTERNAL_UNIPHY:
1276 				args.v4.acConfig.ucTransmitterSel = 0;
1277 				break;
1278 			case ENCODER_OBJECT_ID_INTERNAL_UNIPHY1:
1279 				args.v4.acConfig.ucTransmitterSel = 1;
1280 				break;
1281 			case ENCODER_OBJECT_ID_INTERNAL_UNIPHY2:
1282 				args.v4.acConfig.ucTransmitterSel = 2;
1283 				break;
1284 			}
1285 
1286 			if (is_dp)
1287 				args.v4.acConfig.fCoherentMode = 1; /* DP requires coherent */
1288 			else if (radeon_encoder->devices & (ATOM_DEVICE_DFP_SUPPORT)) {
1289 				if (dig->coherent_mode)
1290 					args.v4.acConfig.fCoherentMode = 1;
1291 				if (radeon_dig_monitor_is_duallink(encoder, radeon_encoder->pixel_clock))
1292 					args.v4.acConfig.fDualLinkConnector = 1;
1293 			}
1294 			break;
1295 		case 5:
1296 			args.v5.ucAction = action;
1297 			if (is_dp)
1298 				args.v5.usSymClock = cpu_to_le16(dp_clock / 10);
1299 			else
1300 				args.v5.usSymClock = cpu_to_le16(radeon_encoder->pixel_clock / 10);
1301 
1302 			switch (radeon_encoder->encoder_id) {
1303 			case ENCODER_OBJECT_ID_INTERNAL_UNIPHY:
1304 				if (dig->linkb)
1305 					args.v5.ucPhyId = ATOM_PHY_ID_UNIPHYB;
1306 				else
1307 					args.v5.ucPhyId = ATOM_PHY_ID_UNIPHYA;
1308 				break;
1309 			case ENCODER_OBJECT_ID_INTERNAL_UNIPHY1:
1310 				if (dig->linkb)
1311 					args.v5.ucPhyId = ATOM_PHY_ID_UNIPHYD;
1312 				else
1313 					args.v5.ucPhyId = ATOM_PHY_ID_UNIPHYC;
1314 				break;
1315 			case ENCODER_OBJECT_ID_INTERNAL_UNIPHY2:
1316 				if (dig->linkb)
1317 					args.v5.ucPhyId = ATOM_PHY_ID_UNIPHYF;
1318 				else
1319 					args.v5.ucPhyId = ATOM_PHY_ID_UNIPHYE;
1320 				break;
1321 			case ENCODER_OBJECT_ID_INTERNAL_UNIPHY3:
1322 				args.v5.ucPhyId = ATOM_PHY_ID_UNIPHYG;
1323 				break;
1324 			}
1325 			if (is_dp)
1326 				args.v5.ucLaneNum = dp_lane_count;
1327 			else if (radeon_dig_monitor_is_duallink(encoder, radeon_encoder->pixel_clock))
1328 				args.v5.ucLaneNum = 8;
1329 			else
1330 				args.v5.ucLaneNum = 4;
1331 			args.v5.ucConnObjId = connector_object_id;
1332 			args.v5.ucDigMode = atombios_get_encoder_mode(encoder);
1333 
1334 			if (is_dp && rdev->clock.dp_extclk)
1335 				args.v5.asConfig.ucPhyClkSrcId = ENCODER_REFCLK_SRC_EXTCLK;
1336 			else
1337 				args.v5.asConfig.ucPhyClkSrcId = pll_id;
1338 
1339 			if (is_dp)
1340 				args.v5.asConfig.ucCoherentMode = 1; /* DP requires coherent */
1341 			else if (radeon_encoder->devices & (ATOM_DEVICE_DFP_SUPPORT)) {
1342 				if (dig->coherent_mode)
1343 					args.v5.asConfig.ucCoherentMode = 1;
1344 			}
1345 			if (hpd_id == RADEON_HPD_NONE)
1346 				args.v5.asConfig.ucHPDSel = 0;
1347 			else
1348 				args.v5.asConfig.ucHPDSel = hpd_id + 1;
1349 			args.v5.ucDigEncoderSel = (fe != -1) ? (1 << fe) : (1 << dig_encoder);
1350 			args.v5.ucDPLaneSet = lane_set;
1351 			break;
1352 		default:
1353 			DRM_ERROR("Unknown table version %d, %d\n", frev, crev);
1354 			break;
1355 		}
1356 		break;
1357 	default:
1358 		DRM_ERROR("Unknown table version %d, %d\n", frev, crev);
1359 		break;
1360 	}
1361 
1362 	atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
1363 }
1364 
1365 void
atombios_dig_transmitter_setup(struct drm_encoder * encoder,int action,uint8_t lane_num,uint8_t lane_set)1366 atombios_dig_transmitter_setup(struct drm_encoder *encoder, int action, uint8_t lane_num, uint8_t lane_set)
1367 {
1368 	atombios_dig_transmitter_setup2(encoder, action, lane_num, lane_set, -1);
1369 }
1370 
1371 bool
atombios_set_edp_panel_power(struct drm_connector * connector,int action)1372 atombios_set_edp_panel_power(struct drm_connector *connector, int action)
1373 {
1374 	struct radeon_connector *radeon_connector = to_radeon_connector(connector);
1375 	struct drm_device *dev = radeon_connector->base.dev;
1376 	struct radeon_device *rdev = dev->dev_private;
1377 	union dig_transmitter_control args;
1378 	int index = GetIndexIntoMasterTable(COMMAND, UNIPHYTransmitterControl);
1379 	uint8_t frev, crev;
1380 
1381 	if (connector->connector_type != DRM_MODE_CONNECTOR_eDP)
1382 		goto done;
1383 
1384 	if (!ASIC_IS_DCE4(rdev))
1385 		goto done;
1386 
1387 	if ((action != ATOM_TRANSMITTER_ACTION_POWER_ON) &&
1388 	    (action != ATOM_TRANSMITTER_ACTION_POWER_OFF))
1389 		goto done;
1390 
1391 	if (!atom_parse_cmd_header(rdev->mode_info.atom_context, index, &frev, &crev))
1392 		goto done;
1393 
1394 	memset(&args, 0, sizeof(args));
1395 
1396 	args.v1.ucAction = action;
1397 
1398 	atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
1399 
1400 	/* wait for the panel to power up */
1401 	if (action == ATOM_TRANSMITTER_ACTION_POWER_ON) {
1402 		int i;
1403 
1404 		for (i = 0; i < 300; i++) {
1405 			if (radeon_hpd_sense(rdev, radeon_connector->hpd.hpd))
1406 				return true;
1407 			mdelay(1);
1408 		}
1409 		return false;
1410 	}
1411 done:
1412 	return true;
1413 }
1414 
1415 union external_encoder_control {
1416 	EXTERNAL_ENCODER_CONTROL_PS_ALLOCATION v1;
1417 	EXTERNAL_ENCODER_CONTROL_PS_ALLOCATION_V3 v3;
1418 };
1419 
1420 static void
atombios_external_encoder_setup(struct drm_encoder * encoder,struct drm_encoder * ext_encoder,int action)1421 atombios_external_encoder_setup(struct drm_encoder *encoder,
1422 				struct drm_encoder *ext_encoder,
1423 				int action)
1424 {
1425 	struct drm_device *dev = encoder->dev;
1426 	struct radeon_device *rdev = dev->dev_private;
1427 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
1428 	struct radeon_encoder *ext_radeon_encoder = to_radeon_encoder(ext_encoder);
1429 	union external_encoder_control args;
1430 	struct drm_connector *connector;
1431 	int index = GetIndexIntoMasterTable(COMMAND, ExternalEncoderControl);
1432 	u8 frev, crev;
1433 	int dp_clock = 0;
1434 	int dp_lane_count = 0;
1435 	int connector_object_id = 0;
1436 	u32 ext_enum = (ext_radeon_encoder->encoder_enum & ENUM_ID_MASK) >> ENUM_ID_SHIFT;
1437 
1438 	if (action == EXTERNAL_ENCODER_ACTION_V3_ENCODER_INIT)
1439 		connector = radeon_get_connector_for_encoder_init(encoder);
1440 	else
1441 		connector = radeon_get_connector_for_encoder(encoder);
1442 
1443 	if (connector) {
1444 		struct radeon_connector *radeon_connector = to_radeon_connector(connector);
1445 		struct radeon_connector_atom_dig *dig_connector =
1446 			radeon_connector->con_priv;
1447 
1448 		dp_clock = dig_connector->dp_clock;
1449 		dp_lane_count = dig_connector->dp_lane_count;
1450 		connector_object_id =
1451 			(radeon_connector->connector_object_id & OBJECT_ID_MASK) >> OBJECT_ID_SHIFT;
1452 	}
1453 
1454 	memset(&args, 0, sizeof(args));
1455 
1456 	if (!atom_parse_cmd_header(rdev->mode_info.atom_context, index, &frev, &crev))
1457 		return;
1458 
1459 	switch (frev) {
1460 	case 1:
1461 		/* no params on frev 1 */
1462 		break;
1463 	case 2:
1464 		switch (crev) {
1465 		case 1:
1466 		case 2:
1467 			args.v1.sDigEncoder.ucAction = action;
1468 			args.v1.sDigEncoder.usPixelClock = cpu_to_le16(radeon_encoder->pixel_clock / 10);
1469 			args.v1.sDigEncoder.ucEncoderMode = atombios_get_encoder_mode(encoder);
1470 
1471 			if (ENCODER_MODE_IS_DP(args.v1.sDigEncoder.ucEncoderMode)) {
1472 				if (dp_clock == 270000)
1473 					args.v1.sDigEncoder.ucConfig |= ATOM_ENCODER_CONFIG_DPLINKRATE_2_70GHZ;
1474 				args.v1.sDigEncoder.ucLaneNum = dp_lane_count;
1475 			} else if (radeon_dig_monitor_is_duallink(encoder, radeon_encoder->pixel_clock))
1476 				args.v1.sDigEncoder.ucLaneNum = 8;
1477 			else
1478 				args.v1.sDigEncoder.ucLaneNum = 4;
1479 			break;
1480 		case 3:
1481 			args.v3.sExtEncoder.ucAction = action;
1482 			if (action == EXTERNAL_ENCODER_ACTION_V3_ENCODER_INIT)
1483 				args.v3.sExtEncoder.usConnectorId = cpu_to_le16(connector_object_id);
1484 			else
1485 				args.v3.sExtEncoder.usPixelClock = cpu_to_le16(radeon_encoder->pixel_clock / 10);
1486 			args.v3.sExtEncoder.ucEncoderMode = atombios_get_encoder_mode(encoder);
1487 
1488 			if (ENCODER_MODE_IS_DP(args.v3.sExtEncoder.ucEncoderMode)) {
1489 				if (dp_clock == 270000)
1490 					args.v3.sExtEncoder.ucConfig |= EXTERNAL_ENCODER_CONFIG_V3_DPLINKRATE_2_70GHZ;
1491 				else if (dp_clock == 540000)
1492 					args.v3.sExtEncoder.ucConfig |= EXTERNAL_ENCODER_CONFIG_V3_DPLINKRATE_5_40GHZ;
1493 				args.v3.sExtEncoder.ucLaneNum = dp_lane_count;
1494 			} else if (radeon_dig_monitor_is_duallink(encoder, radeon_encoder->pixel_clock))
1495 				args.v3.sExtEncoder.ucLaneNum = 8;
1496 			else
1497 				args.v3.sExtEncoder.ucLaneNum = 4;
1498 			switch (ext_enum) {
1499 			case GRAPH_OBJECT_ENUM_ID1:
1500 				args.v3.sExtEncoder.ucConfig |= EXTERNAL_ENCODER_CONFIG_V3_ENCODER1;
1501 				break;
1502 			case GRAPH_OBJECT_ENUM_ID2:
1503 				args.v3.sExtEncoder.ucConfig |= EXTERNAL_ENCODER_CONFIG_V3_ENCODER2;
1504 				break;
1505 			case GRAPH_OBJECT_ENUM_ID3:
1506 				args.v3.sExtEncoder.ucConfig |= EXTERNAL_ENCODER_CONFIG_V3_ENCODER3;
1507 				break;
1508 			}
1509 			args.v3.sExtEncoder.ucBitPerColor = radeon_atom_get_bpc(encoder);
1510 			break;
1511 		default:
1512 			DRM_ERROR("Unknown table version: %d, %d\n", frev, crev);
1513 			return;
1514 		}
1515 		break;
1516 	default:
1517 		DRM_ERROR("Unknown table version: %d, %d\n", frev, crev);
1518 		return;
1519 	}
1520 	atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
1521 }
1522 
1523 static void
atombios_yuv_setup(struct drm_encoder * encoder,bool enable)1524 atombios_yuv_setup(struct drm_encoder *encoder, bool enable)
1525 {
1526 	struct drm_device *dev = encoder->dev;
1527 	struct radeon_device *rdev = dev->dev_private;
1528 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
1529 	struct radeon_crtc *radeon_crtc = to_radeon_crtc(encoder->crtc);
1530 	ENABLE_YUV_PS_ALLOCATION args;
1531 	int index = GetIndexIntoMasterTable(COMMAND, EnableYUV);
1532 	uint32_t temp, reg;
1533 
1534 	memset(&args, 0, sizeof(args));
1535 
1536 	if (rdev->family >= CHIP_R600)
1537 		reg = R600_BIOS_3_SCRATCH;
1538 	else
1539 		reg = RADEON_BIOS_3_SCRATCH;
1540 
1541 	/* XXX: fix up scratch reg handling */
1542 	temp = RREG32(reg);
1543 	if (radeon_encoder->active_device & (ATOM_DEVICE_TV_SUPPORT))
1544 		WREG32(reg, (ATOM_S3_TV1_ACTIVE |
1545 			     (radeon_crtc->crtc_id << 18)));
1546 	else if (radeon_encoder->active_device & (ATOM_DEVICE_CV_SUPPORT))
1547 		WREG32(reg, (ATOM_S3_CV_ACTIVE | (radeon_crtc->crtc_id << 24)));
1548 	else
1549 		WREG32(reg, 0);
1550 
1551 	if (enable)
1552 		args.ucEnable = ATOM_ENABLE;
1553 	args.ucCRTC = radeon_crtc->crtc_id;
1554 
1555 	atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
1556 
1557 	WREG32(reg, temp);
1558 }
1559 
1560 static void
radeon_atom_encoder_dpms_avivo(struct drm_encoder * encoder,int mode)1561 radeon_atom_encoder_dpms_avivo(struct drm_encoder *encoder, int mode)
1562 {
1563 	struct drm_device *dev = encoder->dev;
1564 	struct radeon_device *rdev = dev->dev_private;
1565 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
1566 	DISPLAY_DEVICE_OUTPUT_CONTROL_PS_ALLOCATION args;
1567 	int index = 0;
1568 
1569 	memset(&args, 0, sizeof(args));
1570 
1571 	switch (radeon_encoder->encoder_id) {
1572 	case ENCODER_OBJECT_ID_INTERNAL_TMDS1:
1573 	case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_TMDS1:
1574 		index = GetIndexIntoMasterTable(COMMAND, TMDSAOutputControl);
1575 		break;
1576 	case ENCODER_OBJECT_ID_INTERNAL_DVO1:
1577 	case ENCODER_OBJECT_ID_INTERNAL_DDI:
1578 	case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DVO1:
1579 		index = GetIndexIntoMasterTable(COMMAND, DVOOutputControl);
1580 		break;
1581 	case ENCODER_OBJECT_ID_INTERNAL_LVDS:
1582 		index = GetIndexIntoMasterTable(COMMAND, LCD1OutputControl);
1583 		break;
1584 	case ENCODER_OBJECT_ID_INTERNAL_LVTM1:
1585 		if (radeon_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT))
1586 			index = GetIndexIntoMasterTable(COMMAND, LCD1OutputControl);
1587 		else
1588 			index = GetIndexIntoMasterTable(COMMAND, LVTMAOutputControl);
1589 		break;
1590 	case ENCODER_OBJECT_ID_INTERNAL_DAC1:
1591 	case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DAC1:
1592 		if (radeon_encoder->active_device & (ATOM_DEVICE_TV_SUPPORT))
1593 			index = GetIndexIntoMasterTable(COMMAND, TV1OutputControl);
1594 		else if (radeon_encoder->active_device & (ATOM_DEVICE_CV_SUPPORT))
1595 			index = GetIndexIntoMasterTable(COMMAND, CV1OutputControl);
1596 		else
1597 			index = GetIndexIntoMasterTable(COMMAND, DAC1OutputControl);
1598 		break;
1599 	case ENCODER_OBJECT_ID_INTERNAL_DAC2:
1600 	case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DAC2:
1601 		if (radeon_encoder->active_device & (ATOM_DEVICE_TV_SUPPORT))
1602 			index = GetIndexIntoMasterTable(COMMAND, TV1OutputControl);
1603 		else if (radeon_encoder->active_device & (ATOM_DEVICE_CV_SUPPORT))
1604 			index = GetIndexIntoMasterTable(COMMAND, CV1OutputControl);
1605 		else
1606 			index = GetIndexIntoMasterTable(COMMAND, DAC2OutputControl);
1607 		break;
1608 	default:
1609 		return;
1610 	}
1611 
1612 	switch (mode) {
1613 	case DRM_MODE_DPMS_ON:
1614 		args.ucAction = ATOM_ENABLE;
1615 		/* workaround for DVOOutputControl on some RS690 systems */
1616 		if (radeon_encoder->encoder_id == ENCODER_OBJECT_ID_INTERNAL_DDI) {
1617 			u32 reg = RREG32(RADEON_BIOS_3_SCRATCH);
1618 			WREG32(RADEON_BIOS_3_SCRATCH, reg & ~ATOM_S3_DFP2I_ACTIVE);
1619 			atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
1620 			WREG32(RADEON_BIOS_3_SCRATCH, reg);
1621 		} else
1622 			atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
1623 		if (radeon_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT)) {
1624 			if (rdev->mode_info.bl_encoder) {
1625 				struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
1626 
1627 				atombios_set_backlight_level(radeon_encoder, dig->backlight_level);
1628 			} else {
1629 				args.ucAction = ATOM_LCD_BLON;
1630 				atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
1631 			}
1632 		}
1633 		break;
1634 	case DRM_MODE_DPMS_STANDBY:
1635 	case DRM_MODE_DPMS_SUSPEND:
1636 	case DRM_MODE_DPMS_OFF:
1637 		args.ucAction = ATOM_DISABLE;
1638 		atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
1639 		if (radeon_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT)) {
1640 			args.ucAction = ATOM_LCD_BLOFF;
1641 			atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
1642 		}
1643 		break;
1644 	}
1645 }
1646 
1647 static void
radeon_atom_encoder_dpms_dig(struct drm_encoder * encoder,int mode)1648 radeon_atom_encoder_dpms_dig(struct drm_encoder *encoder, int mode)
1649 {
1650 	struct drm_device *dev = encoder->dev;
1651 	struct radeon_device *rdev = dev->dev_private;
1652 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
1653 	struct drm_encoder *ext_encoder = radeon_get_external_encoder(encoder);
1654 	struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
1655 	struct drm_connector *connector = radeon_get_connector_for_encoder(encoder);
1656 	struct radeon_connector *radeon_connector = NULL;
1657 	struct radeon_connector_atom_dig *radeon_dig_connector = NULL;
1658 	bool travis_quirk = false;
1659 
1660 	if (connector) {
1661 		radeon_connector = to_radeon_connector(connector);
1662 		radeon_dig_connector = radeon_connector->con_priv;
1663 		if ((radeon_connector_encoder_get_dp_bridge_encoder_id(connector) ==
1664 		     ENCODER_OBJECT_ID_TRAVIS) &&
1665 		    (radeon_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT)) &&
1666 		    !ASIC_IS_DCE5(rdev))
1667 			travis_quirk = true;
1668 	}
1669 
1670 	switch (mode) {
1671 	case DRM_MODE_DPMS_ON:
1672 		if (ASIC_IS_DCE41(rdev) || ASIC_IS_DCE5(rdev)) {
1673 			if (!connector)
1674 				dig->panel_mode = DP_PANEL_MODE_EXTERNAL_DP_MODE;
1675 			else
1676 				dig->panel_mode = radeon_dp_get_panel_mode(encoder, connector);
1677 
1678 			/* setup and enable the encoder */
1679 			atombios_dig_encoder_setup(encoder, ATOM_ENCODER_CMD_SETUP, 0);
1680 			atombios_dig_encoder_setup(encoder,
1681 						   ATOM_ENCODER_CMD_SETUP_PANEL_MODE,
1682 						   dig->panel_mode);
1683 			if (ext_encoder) {
1684 				if (ASIC_IS_DCE41(rdev) || ASIC_IS_DCE61(rdev))
1685 					atombios_external_encoder_setup(encoder, ext_encoder,
1686 									EXTERNAL_ENCODER_ACTION_V3_ENCODER_SETUP);
1687 			}
1688 		} else if (ASIC_IS_DCE4(rdev)) {
1689 			/* setup and enable the encoder */
1690 			atombios_dig_encoder_setup(encoder, ATOM_ENCODER_CMD_SETUP, 0);
1691 		} else {
1692 			/* setup and enable the encoder and transmitter */
1693 			atombios_dig_encoder_setup(encoder, ATOM_ENABLE, 0);
1694 			atombios_dig_transmitter_setup(encoder, ATOM_TRANSMITTER_ACTION_SETUP, 0, 0);
1695 		}
1696 		if (ENCODER_MODE_IS_DP(atombios_get_encoder_mode(encoder)) && connector) {
1697 			if (connector->connector_type == DRM_MODE_CONNECTOR_eDP) {
1698 				atombios_set_edp_panel_power(connector,
1699 							     ATOM_TRANSMITTER_ACTION_POWER_ON);
1700 				radeon_dig_connector->edp_on = true;
1701 			}
1702 		}
1703 		/* enable the transmitter */
1704 		atombios_dig_transmitter_setup(encoder, ATOM_TRANSMITTER_ACTION_ENABLE, 0, 0);
1705 		if (ENCODER_MODE_IS_DP(atombios_get_encoder_mode(encoder)) && connector) {
1706 			/* DP_SET_POWER_D0 is set in radeon_dp_link_train */
1707 			radeon_dp_link_train(encoder, connector);
1708 			if (ASIC_IS_DCE4(rdev))
1709 				atombios_dig_encoder_setup(encoder, ATOM_ENCODER_CMD_DP_VIDEO_ON, 0);
1710 		}
1711 		if (radeon_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT)) {
1712 			if (rdev->mode_info.bl_encoder)
1713 				atombios_set_backlight_level(radeon_encoder, dig->backlight_level);
1714 			else
1715 				atombios_dig_transmitter_setup(encoder,
1716 							       ATOM_TRANSMITTER_ACTION_LCD_BLON, 0, 0);
1717 		}
1718 		if (ext_encoder)
1719 			atombios_external_encoder_setup(encoder, ext_encoder, ATOM_ENABLE);
1720 		break;
1721 	case DRM_MODE_DPMS_STANDBY:
1722 	case DRM_MODE_DPMS_SUSPEND:
1723 	case DRM_MODE_DPMS_OFF:
1724 
1725 		if (ASIC_IS_DCE4(rdev)) {
1726 			if (ENCODER_MODE_IS_DP(atombios_get_encoder_mode(encoder)) && connector)
1727 				atombios_dig_encoder_setup(encoder, ATOM_ENCODER_CMD_DP_VIDEO_OFF, 0);
1728 		}
1729 		if (ext_encoder)
1730 			atombios_external_encoder_setup(encoder, ext_encoder, ATOM_DISABLE);
1731 		if (radeon_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT))
1732 			atombios_dig_transmitter_setup(encoder,
1733 						       ATOM_TRANSMITTER_ACTION_LCD_BLOFF, 0, 0);
1734 
1735 		if (ENCODER_MODE_IS_DP(atombios_get_encoder_mode(encoder)) &&
1736 		    connector && !travis_quirk)
1737 			radeon_dp_set_rx_power_state(connector, DP_SET_POWER_D3);
1738 		if (ASIC_IS_DCE4(rdev)) {
1739 			/* disable the transmitter */
1740 			atombios_dig_transmitter_setup(encoder,
1741 						       ATOM_TRANSMITTER_ACTION_DISABLE, 0, 0);
1742 		} else {
1743 			/* disable the encoder and transmitter */
1744 			atombios_dig_transmitter_setup(encoder,
1745 						       ATOM_TRANSMITTER_ACTION_DISABLE, 0, 0);
1746 			atombios_dig_encoder_setup(encoder, ATOM_DISABLE, 0);
1747 		}
1748 		if (ENCODER_MODE_IS_DP(atombios_get_encoder_mode(encoder)) && connector) {
1749 			if (travis_quirk)
1750 				radeon_dp_set_rx_power_state(connector, DP_SET_POWER_D3);
1751 			if (connector->connector_type == DRM_MODE_CONNECTOR_eDP) {
1752 				atombios_set_edp_panel_power(connector,
1753 							     ATOM_TRANSMITTER_ACTION_POWER_OFF);
1754 				radeon_dig_connector->edp_on = false;
1755 			}
1756 		}
1757 		break;
1758 	}
1759 }
1760 
1761 static void
radeon_atom_encoder_dpms(struct drm_encoder * encoder,int mode)1762 radeon_atom_encoder_dpms(struct drm_encoder *encoder, int mode)
1763 {
1764 	struct drm_device *dev = encoder->dev;
1765 	struct radeon_device *rdev = dev->dev_private;
1766 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
1767 	int encoder_mode = atombios_get_encoder_mode(encoder);
1768 
1769 	DRM_DEBUG_KMS("encoder dpms %d to mode %d, devices %08x, active_devices %08x\n",
1770 		  radeon_encoder->encoder_id, mode, radeon_encoder->devices,
1771 		  radeon_encoder->active_device);
1772 
1773 	if ((radeon_audio != 0) &&
1774 	    ((encoder_mode == ATOM_ENCODER_MODE_HDMI) ||
1775 	     ENCODER_MODE_IS_DP(encoder_mode)))
1776 		radeon_audio_dpms(encoder, mode);
1777 
1778 	switch (radeon_encoder->encoder_id) {
1779 	case ENCODER_OBJECT_ID_INTERNAL_TMDS1:
1780 	case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_TMDS1:
1781 	case ENCODER_OBJECT_ID_INTERNAL_LVDS:
1782 	case ENCODER_OBJECT_ID_INTERNAL_LVTM1:
1783 	case ENCODER_OBJECT_ID_INTERNAL_DVO1:
1784 	case ENCODER_OBJECT_ID_INTERNAL_DDI:
1785 	case ENCODER_OBJECT_ID_INTERNAL_DAC2:
1786 	case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DAC2:
1787 		radeon_atom_encoder_dpms_avivo(encoder, mode);
1788 		break;
1789 	case ENCODER_OBJECT_ID_INTERNAL_UNIPHY:
1790 	case ENCODER_OBJECT_ID_INTERNAL_UNIPHY1:
1791 	case ENCODER_OBJECT_ID_INTERNAL_UNIPHY2:
1792 	case ENCODER_OBJECT_ID_INTERNAL_UNIPHY3:
1793 	case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_LVTMA:
1794 		radeon_atom_encoder_dpms_dig(encoder, mode);
1795 		break;
1796 	case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DVO1:
1797 		if (ASIC_IS_DCE5(rdev)) {
1798 			switch (mode) {
1799 			case DRM_MODE_DPMS_ON:
1800 				atombios_dvo_setup(encoder, ATOM_ENABLE);
1801 				break;
1802 			case DRM_MODE_DPMS_STANDBY:
1803 			case DRM_MODE_DPMS_SUSPEND:
1804 			case DRM_MODE_DPMS_OFF:
1805 				atombios_dvo_setup(encoder, ATOM_DISABLE);
1806 				break;
1807 			}
1808 		} else if (ASIC_IS_DCE3(rdev))
1809 			radeon_atom_encoder_dpms_dig(encoder, mode);
1810 		else
1811 			radeon_atom_encoder_dpms_avivo(encoder, mode);
1812 		break;
1813 	case ENCODER_OBJECT_ID_INTERNAL_DAC1:
1814 	case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DAC1:
1815 		if (ASIC_IS_DCE5(rdev)) {
1816 			switch (mode) {
1817 			case DRM_MODE_DPMS_ON:
1818 				atombios_dac_setup(encoder, ATOM_ENABLE);
1819 				break;
1820 			case DRM_MODE_DPMS_STANDBY:
1821 			case DRM_MODE_DPMS_SUSPEND:
1822 			case DRM_MODE_DPMS_OFF:
1823 				atombios_dac_setup(encoder, ATOM_DISABLE);
1824 				break;
1825 			}
1826 		} else
1827 			radeon_atom_encoder_dpms_avivo(encoder, mode);
1828 		break;
1829 	default:
1830 		return;
1831 	}
1832 
1833 	radeon_atombios_encoder_dpms_scratch_regs(encoder, (mode == DRM_MODE_DPMS_ON) ? true : false);
1834 
1835 }
1836 
1837 union crtc_source_param {
1838 	SELECT_CRTC_SOURCE_PS_ALLOCATION v1;
1839 	SELECT_CRTC_SOURCE_PARAMETERS_V2 v2;
1840 };
1841 
1842 static void
atombios_set_encoder_crtc_source(struct drm_encoder * encoder)1843 atombios_set_encoder_crtc_source(struct drm_encoder *encoder)
1844 {
1845 	struct drm_device *dev = encoder->dev;
1846 	struct radeon_device *rdev = dev->dev_private;
1847 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
1848 	struct radeon_crtc *radeon_crtc = to_radeon_crtc(encoder->crtc);
1849 	union crtc_source_param args;
1850 	int index = GetIndexIntoMasterTable(COMMAND, SelectCRTC_Source);
1851 	uint8_t frev, crev;
1852 	struct radeon_encoder_atom_dig *dig;
1853 
1854 	memset(&args, 0, sizeof(args));
1855 
1856 	if (!atom_parse_cmd_header(rdev->mode_info.atom_context, index, &frev, &crev))
1857 		return;
1858 
1859 	switch (frev) {
1860 	case 1:
1861 		switch (crev) {
1862 		case 1:
1863 		default:
1864 			if (ASIC_IS_AVIVO(rdev))
1865 				args.v1.ucCRTC = radeon_crtc->crtc_id;
1866 			else {
1867 				if (radeon_encoder->encoder_id == ENCODER_OBJECT_ID_INTERNAL_DAC1)
1868 					args.v1.ucCRTC = radeon_crtc->crtc_id;
1869 				else
1870 					args.v1.ucCRTC = radeon_crtc->crtc_id << 2;
1871 			}
1872 			switch (radeon_encoder->encoder_id) {
1873 			case ENCODER_OBJECT_ID_INTERNAL_TMDS1:
1874 			case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_TMDS1:
1875 				args.v1.ucDevice = ATOM_DEVICE_DFP1_INDEX;
1876 				break;
1877 			case ENCODER_OBJECT_ID_INTERNAL_LVDS:
1878 			case ENCODER_OBJECT_ID_INTERNAL_LVTM1:
1879 				if (radeon_encoder->devices & ATOM_DEVICE_LCD1_SUPPORT)
1880 					args.v1.ucDevice = ATOM_DEVICE_LCD1_INDEX;
1881 				else
1882 					args.v1.ucDevice = ATOM_DEVICE_DFP3_INDEX;
1883 				break;
1884 			case ENCODER_OBJECT_ID_INTERNAL_DVO1:
1885 			case ENCODER_OBJECT_ID_INTERNAL_DDI:
1886 			case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DVO1:
1887 				args.v1.ucDevice = ATOM_DEVICE_DFP2_INDEX;
1888 				break;
1889 			case ENCODER_OBJECT_ID_INTERNAL_DAC1:
1890 			case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DAC1:
1891 				if (radeon_encoder->active_device & (ATOM_DEVICE_TV_SUPPORT))
1892 					args.v1.ucDevice = ATOM_DEVICE_TV1_INDEX;
1893 				else if (radeon_encoder->active_device & (ATOM_DEVICE_CV_SUPPORT))
1894 					args.v1.ucDevice = ATOM_DEVICE_CV_INDEX;
1895 				else
1896 					args.v1.ucDevice = ATOM_DEVICE_CRT1_INDEX;
1897 				break;
1898 			case ENCODER_OBJECT_ID_INTERNAL_DAC2:
1899 			case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DAC2:
1900 				if (radeon_encoder->active_device & (ATOM_DEVICE_TV_SUPPORT))
1901 					args.v1.ucDevice = ATOM_DEVICE_TV1_INDEX;
1902 				else if (radeon_encoder->active_device & (ATOM_DEVICE_CV_SUPPORT))
1903 					args.v1.ucDevice = ATOM_DEVICE_CV_INDEX;
1904 				else
1905 					args.v1.ucDevice = ATOM_DEVICE_CRT2_INDEX;
1906 				break;
1907 			}
1908 			break;
1909 		case 2:
1910 			args.v2.ucCRTC = radeon_crtc->crtc_id;
1911 			if (radeon_encoder_get_dp_bridge_encoder_id(encoder) != ENCODER_OBJECT_ID_NONE) {
1912 				struct drm_connector *connector = radeon_get_connector_for_encoder(encoder);
1913 
1914 				if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS)
1915 					args.v2.ucEncodeMode = ATOM_ENCODER_MODE_LVDS;
1916 				else if (connector->connector_type == DRM_MODE_CONNECTOR_VGA)
1917 					args.v2.ucEncodeMode = ATOM_ENCODER_MODE_CRT;
1918 				else
1919 					args.v2.ucEncodeMode = atombios_get_encoder_mode(encoder);
1920 			} else if (radeon_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT)) {
1921 				args.v2.ucEncodeMode = ATOM_ENCODER_MODE_LVDS;
1922 			} else {
1923 				args.v2.ucEncodeMode = atombios_get_encoder_mode(encoder);
1924 			}
1925 			switch (radeon_encoder->encoder_id) {
1926 			case ENCODER_OBJECT_ID_INTERNAL_UNIPHY:
1927 			case ENCODER_OBJECT_ID_INTERNAL_UNIPHY1:
1928 			case ENCODER_OBJECT_ID_INTERNAL_UNIPHY2:
1929 			case ENCODER_OBJECT_ID_INTERNAL_UNIPHY3:
1930 			case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_LVTMA:
1931 				dig = radeon_encoder->enc_priv;
1932 				switch (dig->dig_encoder) {
1933 				case 0:
1934 					args.v2.ucEncoderID = ASIC_INT_DIG1_ENCODER_ID;
1935 					break;
1936 				case 1:
1937 					args.v2.ucEncoderID = ASIC_INT_DIG2_ENCODER_ID;
1938 					break;
1939 				case 2:
1940 					args.v2.ucEncoderID = ASIC_INT_DIG3_ENCODER_ID;
1941 					break;
1942 				case 3:
1943 					args.v2.ucEncoderID = ASIC_INT_DIG4_ENCODER_ID;
1944 					break;
1945 				case 4:
1946 					args.v2.ucEncoderID = ASIC_INT_DIG5_ENCODER_ID;
1947 					break;
1948 				case 5:
1949 					args.v2.ucEncoderID = ASIC_INT_DIG6_ENCODER_ID;
1950 					break;
1951 				case 6:
1952 					args.v2.ucEncoderID = ASIC_INT_DIG7_ENCODER_ID;
1953 					break;
1954 				}
1955 				break;
1956 			case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DVO1:
1957 				args.v2.ucEncoderID = ASIC_INT_DVO_ENCODER_ID;
1958 				break;
1959 			case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DAC1:
1960 				if (radeon_encoder->active_device & (ATOM_DEVICE_TV_SUPPORT))
1961 					args.v2.ucEncoderID = ASIC_INT_TV_ENCODER_ID;
1962 				else if (radeon_encoder->active_device & (ATOM_DEVICE_CV_SUPPORT))
1963 					args.v2.ucEncoderID = ASIC_INT_TV_ENCODER_ID;
1964 				else
1965 					args.v2.ucEncoderID = ASIC_INT_DAC1_ENCODER_ID;
1966 				break;
1967 			case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DAC2:
1968 				if (radeon_encoder->active_device & (ATOM_DEVICE_TV_SUPPORT))
1969 					args.v2.ucEncoderID = ASIC_INT_TV_ENCODER_ID;
1970 				else if (radeon_encoder->active_device & (ATOM_DEVICE_CV_SUPPORT))
1971 					args.v2.ucEncoderID = ASIC_INT_TV_ENCODER_ID;
1972 				else
1973 					args.v2.ucEncoderID = ASIC_INT_DAC2_ENCODER_ID;
1974 				break;
1975 			}
1976 			break;
1977 		}
1978 		break;
1979 	default:
1980 		DRM_ERROR("Unknown table version: %d, %d\n", frev, crev);
1981 		return;
1982 	}
1983 
1984 	atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
1985 
1986 	/* update scratch regs with new routing */
1987 	radeon_atombios_encoder_crtc_scratch_regs(encoder, radeon_crtc->crtc_id);
1988 }
1989 
1990 static void
atombios_apply_encoder_quirks(struct drm_encoder * encoder,struct drm_display_mode * mode)1991 atombios_apply_encoder_quirks(struct drm_encoder *encoder,
1992 			      struct drm_display_mode *mode)
1993 {
1994 	struct drm_device *dev = encoder->dev;
1995 	struct radeon_device *rdev = dev->dev_private;
1996 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
1997 	struct radeon_crtc *radeon_crtc = to_radeon_crtc(encoder->crtc);
1998 
1999 	/* Funky macbooks */
2000 	if ((rdev->pdev->device == 0x71C5) &&
2001 	    (rdev->pdev->subsystem_vendor == 0x106b) &&
2002 	    (rdev->pdev->subsystem_device == 0x0080)) {
2003 		if (radeon_encoder->devices & ATOM_DEVICE_LCD1_SUPPORT) {
2004 			uint32_t lvtma_bit_depth_control = RREG32(AVIVO_LVTMA_BIT_DEPTH_CONTROL);
2005 
2006 			lvtma_bit_depth_control &= ~AVIVO_LVTMA_BIT_DEPTH_CONTROL_TRUNCATE_EN;
2007 			lvtma_bit_depth_control &= ~AVIVO_LVTMA_BIT_DEPTH_CONTROL_SPATIAL_DITHER_EN;
2008 
2009 			WREG32(AVIVO_LVTMA_BIT_DEPTH_CONTROL, lvtma_bit_depth_control);
2010 		}
2011 	}
2012 
2013 	/* set scaler clears this on some chips */
2014 	if (ASIC_IS_AVIVO(rdev) &&
2015 	    (!(radeon_encoder->active_device & (ATOM_DEVICE_TV_SUPPORT)))) {
2016 		if (ASIC_IS_DCE8(rdev)) {
2017 			if (mode->flags & DRM_MODE_FLAG_INTERLACE)
2018 				WREG32(CIK_LB_DATA_FORMAT + radeon_crtc->crtc_offset,
2019 				       CIK_INTERLEAVE_EN);
2020 			else
2021 				WREG32(CIK_LB_DATA_FORMAT + radeon_crtc->crtc_offset, 0);
2022 		} else if (ASIC_IS_DCE4(rdev)) {
2023 			if (mode->flags & DRM_MODE_FLAG_INTERLACE)
2024 				WREG32(EVERGREEN_DATA_FORMAT + radeon_crtc->crtc_offset,
2025 				       EVERGREEN_INTERLEAVE_EN);
2026 			else
2027 				WREG32(EVERGREEN_DATA_FORMAT + radeon_crtc->crtc_offset, 0);
2028 		} else {
2029 			if (mode->flags & DRM_MODE_FLAG_INTERLACE)
2030 				WREG32(AVIVO_D1MODE_DATA_FORMAT + radeon_crtc->crtc_offset,
2031 				       AVIVO_D1MODE_INTERLEAVE_EN);
2032 			else
2033 				WREG32(AVIVO_D1MODE_DATA_FORMAT + radeon_crtc->crtc_offset, 0);
2034 		}
2035 	}
2036 }
2037 
radeon_atom_release_dig_encoder(struct radeon_device * rdev,int enc_idx)2038 void radeon_atom_release_dig_encoder(struct radeon_device *rdev, int enc_idx)
2039 {
2040 	if (enc_idx < 0)
2041 		return;
2042 	rdev->mode_info.active_encoders &= ~(1 << enc_idx);
2043 }
2044 
radeon_atom_pick_dig_encoder(struct drm_encoder * encoder,int fe_idx)2045 int radeon_atom_pick_dig_encoder(struct drm_encoder *encoder, int fe_idx)
2046 {
2047 	struct drm_device *dev = encoder->dev;
2048 	struct radeon_device *rdev = dev->dev_private;
2049 	struct radeon_crtc *radeon_crtc = to_radeon_crtc(encoder->crtc);
2050 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
2051 	struct drm_encoder *test_encoder;
2052 	struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
2053 	uint32_t dig_enc_in_use = 0;
2054 	int enc_idx = -1;
2055 
2056 	if (fe_idx >= 0) {
2057 		enc_idx = fe_idx;
2058 		goto assigned;
2059 	}
2060 	if (ASIC_IS_DCE6(rdev)) {
2061 		/* DCE6 */
2062 		switch (radeon_encoder->encoder_id) {
2063 		case ENCODER_OBJECT_ID_INTERNAL_UNIPHY:
2064 			if (dig->linkb)
2065 				enc_idx = 1;
2066 			else
2067 				enc_idx = 0;
2068 			break;
2069 		case ENCODER_OBJECT_ID_INTERNAL_UNIPHY1:
2070 			if (dig->linkb)
2071 				enc_idx = 3;
2072 			else
2073 				enc_idx = 2;
2074 			break;
2075 		case ENCODER_OBJECT_ID_INTERNAL_UNIPHY2:
2076 			if (dig->linkb)
2077 				enc_idx = 5;
2078 			else
2079 				enc_idx = 4;
2080 			break;
2081 		case ENCODER_OBJECT_ID_INTERNAL_UNIPHY3:
2082 			enc_idx = 6;
2083 			break;
2084 		}
2085 		goto assigned;
2086 	} else if (ASIC_IS_DCE4(rdev)) {
2087 		/* DCE4/5 */
2088 		if (ASIC_IS_DCE41(rdev) && !ASIC_IS_DCE61(rdev)) {
2089 			/* ontario follows DCE4 */
2090 			if (rdev->family == CHIP_PALM) {
2091 				if (dig->linkb)
2092 					enc_idx = 1;
2093 				else
2094 					enc_idx = 0;
2095 			} else
2096 				/* llano follows DCE3.2 */
2097 				enc_idx = radeon_crtc->crtc_id;
2098 		} else {
2099 			switch (radeon_encoder->encoder_id) {
2100 			case ENCODER_OBJECT_ID_INTERNAL_UNIPHY:
2101 				if (dig->linkb)
2102 					enc_idx = 1;
2103 				else
2104 					enc_idx = 0;
2105 				break;
2106 			case ENCODER_OBJECT_ID_INTERNAL_UNIPHY1:
2107 				if (dig->linkb)
2108 					enc_idx = 3;
2109 				else
2110 					enc_idx = 2;
2111 				break;
2112 			case ENCODER_OBJECT_ID_INTERNAL_UNIPHY2:
2113 				if (dig->linkb)
2114 					enc_idx = 5;
2115 				else
2116 					enc_idx = 4;
2117 				break;
2118 			}
2119 		}
2120 		goto assigned;
2121 	}
2122 
2123 	/*
2124 	 * On DCE32 any encoder can drive any block so usually just use crtc id,
2125 	 * but Apple thinks different at least on iMac10,1 and iMac11,2, so there use linkb,
2126 	 * otherwise the internal eDP panel will stay dark.
2127 	 */
2128 	if (ASIC_IS_DCE32(rdev)) {
2129 		if (dmi_match(DMI_PRODUCT_NAME, "iMac10,1") ||
2130 		    dmi_match(DMI_PRODUCT_NAME, "iMac11,2"))
2131 			enc_idx = (dig->linkb) ? 1 : 0;
2132 		else
2133 			enc_idx = radeon_crtc->crtc_id;
2134 
2135 		goto assigned;
2136 	}
2137 
2138 	/* on DCE3 - LVTMA can only be driven by DIGB */
2139 	list_for_each_entry(test_encoder, &dev->mode_config.encoder_list, head) {
2140 		struct radeon_encoder *radeon_test_encoder;
2141 
2142 		if (encoder == test_encoder)
2143 			continue;
2144 
2145 		if (!radeon_encoder_is_digital(test_encoder))
2146 			continue;
2147 
2148 		radeon_test_encoder = to_radeon_encoder(test_encoder);
2149 		dig = radeon_test_encoder->enc_priv;
2150 
2151 		if (dig->dig_encoder >= 0)
2152 			dig_enc_in_use |= (1 << dig->dig_encoder);
2153 	}
2154 
2155 	if (radeon_encoder->encoder_id == ENCODER_OBJECT_ID_INTERNAL_KLDSCP_LVTMA) {
2156 		if (dig_enc_in_use & 0x2)
2157 			DRM_ERROR("LVDS required digital encoder 2 but it was in use - stealing\n");
2158 		return 1;
2159 	}
2160 	if (!(dig_enc_in_use & 1))
2161 		return 0;
2162 	return 1;
2163 
2164 assigned:
2165 	if (enc_idx == -1) {
2166 		DRM_ERROR("Got encoder index incorrect - returning 0\n");
2167 		return 0;
2168 	}
2169 	if (rdev->mode_info.active_encoders & (1 << enc_idx))
2170 		DRM_ERROR("chosen encoder in use %d\n", enc_idx);
2171 
2172 	rdev->mode_info.active_encoders |= (1 << enc_idx);
2173 	return enc_idx;
2174 }
2175 
2176 /* This only needs to be called once at startup */
2177 void
radeon_atom_encoder_init(struct radeon_device * rdev)2178 radeon_atom_encoder_init(struct radeon_device *rdev)
2179 {
2180 	struct drm_device *dev = rdev->ddev;
2181 	struct drm_encoder *encoder;
2182 
2183 	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
2184 		struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
2185 		struct drm_encoder *ext_encoder = radeon_get_external_encoder(encoder);
2186 
2187 		switch (radeon_encoder->encoder_id) {
2188 		case ENCODER_OBJECT_ID_INTERNAL_UNIPHY:
2189 		case ENCODER_OBJECT_ID_INTERNAL_UNIPHY1:
2190 		case ENCODER_OBJECT_ID_INTERNAL_UNIPHY2:
2191 		case ENCODER_OBJECT_ID_INTERNAL_UNIPHY3:
2192 		case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_LVTMA:
2193 			atombios_dig_transmitter_setup(encoder, ATOM_TRANSMITTER_ACTION_INIT, 0, 0);
2194 			break;
2195 		default:
2196 			break;
2197 		}
2198 
2199 		if (ext_encoder && (ASIC_IS_DCE41(rdev) || ASIC_IS_DCE61(rdev)))
2200 			atombios_external_encoder_setup(encoder, ext_encoder,
2201 							EXTERNAL_ENCODER_ACTION_V3_ENCODER_INIT);
2202 	}
2203 }
2204 
2205 static void
radeon_atom_encoder_mode_set(struct drm_encoder * encoder,struct drm_display_mode * mode,struct drm_display_mode * adjusted_mode)2206 radeon_atom_encoder_mode_set(struct drm_encoder *encoder,
2207 			     struct drm_display_mode *mode,
2208 			     struct drm_display_mode *adjusted_mode)
2209 {
2210 	struct drm_device *dev = encoder->dev;
2211 	struct radeon_device *rdev = dev->dev_private;
2212 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
2213 	struct drm_connector *connector = radeon_get_connector_for_encoder(encoder);
2214 	int encoder_mode;
2215 
2216 	radeon_encoder->pixel_clock = adjusted_mode->clock;
2217 
2218 	/* need to call this here rather than in prepare() since we need some crtc info */
2219 	radeon_atom_encoder_dpms(encoder, DRM_MODE_DPMS_OFF);
2220 
2221 	if (ASIC_IS_AVIVO(rdev) && !ASIC_IS_DCE4(rdev)) {
2222 		if (radeon_encoder->active_device & (ATOM_DEVICE_CV_SUPPORT | ATOM_DEVICE_TV_SUPPORT))
2223 			atombios_yuv_setup(encoder, true);
2224 		else
2225 			atombios_yuv_setup(encoder, false);
2226 	}
2227 
2228 	switch (radeon_encoder->encoder_id) {
2229 	case ENCODER_OBJECT_ID_INTERNAL_TMDS1:
2230 	case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_TMDS1:
2231 	case ENCODER_OBJECT_ID_INTERNAL_LVDS:
2232 	case ENCODER_OBJECT_ID_INTERNAL_LVTM1:
2233 		atombios_digital_setup(encoder, PANEL_ENCODER_ACTION_ENABLE);
2234 		break;
2235 	case ENCODER_OBJECT_ID_INTERNAL_UNIPHY:
2236 	case ENCODER_OBJECT_ID_INTERNAL_UNIPHY1:
2237 	case ENCODER_OBJECT_ID_INTERNAL_UNIPHY2:
2238 	case ENCODER_OBJECT_ID_INTERNAL_UNIPHY3:
2239 	case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_LVTMA:
2240 		/* handled in dpms */
2241 		break;
2242 	case ENCODER_OBJECT_ID_INTERNAL_DDI:
2243 	case ENCODER_OBJECT_ID_INTERNAL_DVO1:
2244 	case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DVO1:
2245 		atombios_dvo_setup(encoder, ATOM_ENABLE);
2246 		break;
2247 	case ENCODER_OBJECT_ID_INTERNAL_DAC1:
2248 	case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DAC1:
2249 	case ENCODER_OBJECT_ID_INTERNAL_DAC2:
2250 	case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DAC2:
2251 		atombios_dac_setup(encoder, ATOM_ENABLE);
2252 		if (radeon_encoder->devices & (ATOM_DEVICE_TV_SUPPORT | ATOM_DEVICE_CV_SUPPORT)) {
2253 			if (radeon_encoder->active_device & (ATOM_DEVICE_TV_SUPPORT | ATOM_DEVICE_CV_SUPPORT))
2254 				atombios_tv_setup(encoder, ATOM_ENABLE);
2255 			else
2256 				atombios_tv_setup(encoder, ATOM_DISABLE);
2257 		}
2258 		break;
2259 	}
2260 
2261 	atombios_apply_encoder_quirks(encoder, adjusted_mode);
2262 
2263 	encoder_mode = atombios_get_encoder_mode(encoder);
2264 	if (connector && (radeon_audio != 0) &&
2265 	    ((encoder_mode == ATOM_ENCODER_MODE_HDMI) ||
2266 	     ENCODER_MODE_IS_DP(encoder_mode)))
2267 		radeon_audio_mode_set(encoder, adjusted_mode);
2268 }
2269 
2270 static bool
atombios_dac_load_detect(struct drm_encoder * encoder,struct drm_connector * connector)2271 atombios_dac_load_detect(struct drm_encoder *encoder, struct drm_connector *connector)
2272 {
2273 	struct drm_device *dev = encoder->dev;
2274 	struct radeon_device *rdev = dev->dev_private;
2275 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
2276 	struct radeon_connector *radeon_connector = to_radeon_connector(connector);
2277 
2278 	if (radeon_encoder->devices & (ATOM_DEVICE_TV_SUPPORT |
2279 				       ATOM_DEVICE_CV_SUPPORT |
2280 				       ATOM_DEVICE_CRT_SUPPORT)) {
2281 		DAC_LOAD_DETECTION_PS_ALLOCATION args;
2282 		int index = GetIndexIntoMasterTable(COMMAND, DAC_LoadDetection);
2283 		uint8_t frev, crev;
2284 
2285 		memset(&args, 0, sizeof(args));
2286 
2287 		if (!atom_parse_cmd_header(rdev->mode_info.atom_context, index, &frev, &crev))
2288 			return false;
2289 
2290 		args.sDacload.ucMisc = 0;
2291 
2292 		if ((radeon_encoder->encoder_id == ENCODER_OBJECT_ID_INTERNAL_DAC1) ||
2293 		    (radeon_encoder->encoder_id == ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DAC1))
2294 			args.sDacload.ucDacType = ATOM_DAC_A;
2295 		else
2296 			args.sDacload.ucDacType = ATOM_DAC_B;
2297 
2298 		if (radeon_connector->devices & ATOM_DEVICE_CRT1_SUPPORT)
2299 			args.sDacload.usDeviceID = cpu_to_le16(ATOM_DEVICE_CRT1_SUPPORT);
2300 		else if (radeon_connector->devices & ATOM_DEVICE_CRT2_SUPPORT)
2301 			args.sDacload.usDeviceID = cpu_to_le16(ATOM_DEVICE_CRT2_SUPPORT);
2302 		else if (radeon_connector->devices & ATOM_DEVICE_CV_SUPPORT) {
2303 			args.sDacload.usDeviceID = cpu_to_le16(ATOM_DEVICE_CV_SUPPORT);
2304 			if (crev >= 3)
2305 				args.sDacload.ucMisc = DAC_LOAD_MISC_YPrPb;
2306 		} else if (radeon_connector->devices & ATOM_DEVICE_TV1_SUPPORT) {
2307 			args.sDacload.usDeviceID = cpu_to_le16(ATOM_DEVICE_TV1_SUPPORT);
2308 			if (crev >= 3)
2309 				args.sDacload.ucMisc = DAC_LOAD_MISC_YPrPb;
2310 		}
2311 
2312 		atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
2313 
2314 		return true;
2315 	} else
2316 		return false;
2317 }
2318 
2319 static enum drm_connector_status
radeon_atom_dac_detect(struct drm_encoder * encoder,struct drm_connector * connector)2320 radeon_atom_dac_detect(struct drm_encoder *encoder, struct drm_connector *connector)
2321 {
2322 	struct drm_device *dev = encoder->dev;
2323 	struct radeon_device *rdev = dev->dev_private;
2324 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
2325 	struct radeon_connector *radeon_connector = to_radeon_connector(connector);
2326 	uint32_t bios_0_scratch;
2327 
2328 	if (!atombios_dac_load_detect(encoder, connector)) {
2329 		DRM_DEBUG_KMS("detect returned false \n");
2330 		return connector_status_unknown;
2331 	}
2332 
2333 	if (rdev->family >= CHIP_R600)
2334 		bios_0_scratch = RREG32(R600_BIOS_0_SCRATCH);
2335 	else
2336 		bios_0_scratch = RREG32(RADEON_BIOS_0_SCRATCH);
2337 
2338 	DRM_DEBUG_KMS("Bios 0 scratch %x %08x\n", bios_0_scratch, radeon_encoder->devices);
2339 	if (radeon_connector->devices & ATOM_DEVICE_CRT1_SUPPORT) {
2340 		if (bios_0_scratch & ATOM_S0_CRT1_MASK)
2341 			return connector_status_connected;
2342 	}
2343 	if (radeon_connector->devices & ATOM_DEVICE_CRT2_SUPPORT) {
2344 		if (bios_0_scratch & ATOM_S0_CRT2_MASK)
2345 			return connector_status_connected;
2346 	}
2347 	if (radeon_connector->devices & ATOM_DEVICE_CV_SUPPORT) {
2348 		if (bios_0_scratch & (ATOM_S0_CV_MASK|ATOM_S0_CV_MASK_A))
2349 			return connector_status_connected;
2350 	}
2351 	if (radeon_connector->devices & ATOM_DEVICE_TV1_SUPPORT) {
2352 		if (bios_0_scratch & (ATOM_S0_TV1_COMPOSITE | ATOM_S0_TV1_COMPOSITE_A))
2353 			return connector_status_connected; /* CTV */
2354 		else if (bios_0_scratch & (ATOM_S0_TV1_SVIDEO | ATOM_S0_TV1_SVIDEO_A))
2355 			return connector_status_connected; /* STV */
2356 	}
2357 	return connector_status_disconnected;
2358 }
2359 
2360 static enum drm_connector_status
radeon_atom_dig_detect(struct drm_encoder * encoder,struct drm_connector * connector)2361 radeon_atom_dig_detect(struct drm_encoder *encoder, struct drm_connector *connector)
2362 {
2363 	struct drm_device *dev = encoder->dev;
2364 	struct radeon_device *rdev = dev->dev_private;
2365 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
2366 	struct radeon_connector *radeon_connector = to_radeon_connector(connector);
2367 	struct drm_encoder *ext_encoder = radeon_get_external_encoder(encoder);
2368 	u32 bios_0_scratch;
2369 
2370 	if (!ASIC_IS_DCE4(rdev))
2371 		return connector_status_unknown;
2372 
2373 	if (!ext_encoder)
2374 		return connector_status_unknown;
2375 
2376 	if ((radeon_connector->devices & ATOM_DEVICE_CRT_SUPPORT) == 0)
2377 		return connector_status_unknown;
2378 
2379 	/* load detect on the dp bridge */
2380 	atombios_external_encoder_setup(encoder, ext_encoder,
2381 					EXTERNAL_ENCODER_ACTION_V3_DACLOAD_DETECTION);
2382 
2383 	bios_0_scratch = RREG32(R600_BIOS_0_SCRATCH);
2384 
2385 	DRM_DEBUG_KMS("Bios 0 scratch %x %08x\n", bios_0_scratch, radeon_encoder->devices);
2386 	if (radeon_connector->devices & ATOM_DEVICE_CRT1_SUPPORT) {
2387 		if (bios_0_scratch & ATOM_S0_CRT1_MASK)
2388 			return connector_status_connected;
2389 	}
2390 	if (radeon_connector->devices & ATOM_DEVICE_CRT2_SUPPORT) {
2391 		if (bios_0_scratch & ATOM_S0_CRT2_MASK)
2392 			return connector_status_connected;
2393 	}
2394 	if (radeon_connector->devices & ATOM_DEVICE_CV_SUPPORT) {
2395 		if (bios_0_scratch & (ATOM_S0_CV_MASK|ATOM_S0_CV_MASK_A))
2396 			return connector_status_connected;
2397 	}
2398 	if (radeon_connector->devices & ATOM_DEVICE_TV1_SUPPORT) {
2399 		if (bios_0_scratch & (ATOM_S0_TV1_COMPOSITE | ATOM_S0_TV1_COMPOSITE_A))
2400 			return connector_status_connected; /* CTV */
2401 		else if (bios_0_scratch & (ATOM_S0_TV1_SVIDEO | ATOM_S0_TV1_SVIDEO_A))
2402 			return connector_status_connected; /* STV */
2403 	}
2404 	return connector_status_disconnected;
2405 }
2406 
2407 void
radeon_atom_ext_encoder_setup_ddc(struct drm_encoder * encoder)2408 radeon_atom_ext_encoder_setup_ddc(struct drm_encoder *encoder)
2409 {
2410 	struct drm_encoder *ext_encoder = radeon_get_external_encoder(encoder);
2411 
2412 	if (ext_encoder)
2413 		/* ddc_setup on the dp bridge */
2414 		atombios_external_encoder_setup(encoder, ext_encoder,
2415 						EXTERNAL_ENCODER_ACTION_V3_DDC_SETUP);
2416 
2417 }
2418 
radeon_atom_encoder_prepare(struct drm_encoder * encoder)2419 static void radeon_atom_encoder_prepare(struct drm_encoder *encoder)
2420 {
2421 	struct radeon_device *rdev = encoder->dev->dev_private;
2422 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
2423 	struct drm_connector *connector = radeon_get_connector_for_encoder(encoder);
2424 
2425 	if ((radeon_encoder->active_device &
2426 	     (ATOM_DEVICE_DFP_SUPPORT | ATOM_DEVICE_LCD_SUPPORT)) ||
2427 	    (radeon_encoder_get_dp_bridge_encoder_id(encoder) !=
2428 	     ENCODER_OBJECT_ID_NONE)) {
2429 		struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
2430 		if (dig) {
2431 			if (dig->dig_encoder >= 0)
2432 				radeon_atom_release_dig_encoder(rdev, dig->dig_encoder);
2433 			dig->dig_encoder = radeon_atom_pick_dig_encoder(encoder, -1);
2434 			if (radeon_encoder->active_device & ATOM_DEVICE_DFP_SUPPORT) {
2435 				if (rdev->family >= CHIP_R600)
2436 					dig->afmt = rdev->mode_info.afmt[dig->dig_encoder];
2437 				else
2438 					/* RS600/690/740 have only 1 afmt block */
2439 					dig->afmt = rdev->mode_info.afmt[0];
2440 			}
2441 		}
2442 	}
2443 
2444 	radeon_atom_output_lock(encoder, true);
2445 
2446 	if (connector) {
2447 		struct radeon_connector *radeon_connector = to_radeon_connector(connector);
2448 
2449 		/* select the clock/data port if it uses a router */
2450 		if (radeon_connector->router.cd_valid)
2451 			radeon_router_select_cd_port(radeon_connector);
2452 
2453 		/* turn eDP panel on for mode set */
2454 		if (connector->connector_type == DRM_MODE_CONNECTOR_eDP)
2455 			atombios_set_edp_panel_power(connector,
2456 						     ATOM_TRANSMITTER_ACTION_POWER_ON);
2457 	}
2458 
2459 	/* this is needed for the pll/ss setup to work correctly in some cases */
2460 	atombios_set_encoder_crtc_source(encoder);
2461 	/* set up the FMT blocks */
2462 	if (ASIC_IS_DCE8(rdev))
2463 		dce8_program_fmt(encoder);
2464 	else if (ASIC_IS_DCE4(rdev))
2465 		dce4_program_fmt(encoder);
2466 	else if (ASIC_IS_DCE3(rdev))
2467 		dce3_program_fmt(encoder);
2468 	else if (ASIC_IS_AVIVO(rdev))
2469 		avivo_program_fmt(encoder);
2470 }
2471 
radeon_atom_encoder_commit(struct drm_encoder * encoder)2472 static void radeon_atom_encoder_commit(struct drm_encoder *encoder)
2473 {
2474 	/* need to call this here as we need the crtc set up */
2475 	radeon_atom_encoder_dpms(encoder, DRM_MODE_DPMS_ON);
2476 	radeon_atom_output_lock(encoder, false);
2477 }
2478 
radeon_atom_encoder_disable(struct drm_encoder * encoder)2479 static void radeon_atom_encoder_disable(struct drm_encoder *encoder)
2480 {
2481 	struct drm_device *dev = encoder->dev;
2482 	struct radeon_device *rdev = dev->dev_private;
2483 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
2484 	struct radeon_encoder_atom_dig *dig;
2485 
2486 	/* check for pre-DCE3 cards with shared encoders;
2487 	 * can't really use the links individually, so don't disable
2488 	 * the encoder if it's in use by another connector
2489 	 */
2490 	if (!ASIC_IS_DCE3(rdev)) {
2491 		struct drm_encoder *other_encoder;
2492 		struct radeon_encoder *other_radeon_encoder;
2493 
2494 		list_for_each_entry(other_encoder, &dev->mode_config.encoder_list, head) {
2495 			other_radeon_encoder = to_radeon_encoder(other_encoder);
2496 			if ((radeon_encoder->encoder_id == other_radeon_encoder->encoder_id) &&
2497 			    drm_helper_encoder_in_use(other_encoder))
2498 				goto disable_done;
2499 		}
2500 	}
2501 
2502 	radeon_atom_encoder_dpms(encoder, DRM_MODE_DPMS_OFF);
2503 
2504 	switch (radeon_encoder->encoder_id) {
2505 	case ENCODER_OBJECT_ID_INTERNAL_TMDS1:
2506 	case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_TMDS1:
2507 	case ENCODER_OBJECT_ID_INTERNAL_LVDS:
2508 	case ENCODER_OBJECT_ID_INTERNAL_LVTM1:
2509 		atombios_digital_setup(encoder, PANEL_ENCODER_ACTION_DISABLE);
2510 		break;
2511 	case ENCODER_OBJECT_ID_INTERNAL_UNIPHY:
2512 	case ENCODER_OBJECT_ID_INTERNAL_UNIPHY1:
2513 	case ENCODER_OBJECT_ID_INTERNAL_UNIPHY2:
2514 	case ENCODER_OBJECT_ID_INTERNAL_UNIPHY3:
2515 	case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_LVTMA:
2516 		/* handled in dpms */
2517 		break;
2518 	case ENCODER_OBJECT_ID_INTERNAL_DDI:
2519 	case ENCODER_OBJECT_ID_INTERNAL_DVO1:
2520 	case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DVO1:
2521 		atombios_dvo_setup(encoder, ATOM_DISABLE);
2522 		break;
2523 	case ENCODER_OBJECT_ID_INTERNAL_DAC1:
2524 	case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DAC1:
2525 	case ENCODER_OBJECT_ID_INTERNAL_DAC2:
2526 	case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DAC2:
2527 		atombios_dac_setup(encoder, ATOM_DISABLE);
2528 		if (radeon_encoder->devices & (ATOM_DEVICE_TV_SUPPORT | ATOM_DEVICE_CV_SUPPORT))
2529 			atombios_tv_setup(encoder, ATOM_DISABLE);
2530 		break;
2531 	}
2532 
2533 disable_done:
2534 	if (radeon_encoder_is_digital(encoder)) {
2535 		if (atombios_get_encoder_mode(encoder) == ATOM_ENCODER_MODE_HDMI) {
2536 			if (rdev->asic->display.hdmi_enable)
2537 				radeon_hdmi_enable(rdev, encoder, false);
2538 		}
2539 		if (atombios_get_encoder_mode(encoder) != ATOM_ENCODER_MODE_DP_MST) {
2540 			dig = radeon_encoder->enc_priv;
2541 			radeon_atom_release_dig_encoder(rdev, dig->dig_encoder);
2542 			dig->dig_encoder = -1;
2543 			radeon_encoder->active_device = 0;
2544 		}
2545 	} else
2546 		radeon_encoder->active_device = 0;
2547 }
2548 
2549 /* these are handled by the primary encoders */
radeon_atom_ext_prepare(struct drm_encoder * encoder)2550 static void radeon_atom_ext_prepare(struct drm_encoder *encoder)
2551 {
2552 
2553 }
2554 
radeon_atom_ext_commit(struct drm_encoder * encoder)2555 static void radeon_atom_ext_commit(struct drm_encoder *encoder)
2556 {
2557 
2558 }
2559 
2560 static void
radeon_atom_ext_mode_set(struct drm_encoder * encoder,struct drm_display_mode * mode,struct drm_display_mode * adjusted_mode)2561 radeon_atom_ext_mode_set(struct drm_encoder *encoder,
2562 			 struct drm_display_mode *mode,
2563 			 struct drm_display_mode *adjusted_mode)
2564 {
2565 
2566 }
2567 
radeon_atom_ext_disable(struct drm_encoder * encoder)2568 static void radeon_atom_ext_disable(struct drm_encoder *encoder)
2569 {
2570 
2571 }
2572 
2573 static void
radeon_atom_ext_dpms(struct drm_encoder * encoder,int mode)2574 radeon_atom_ext_dpms(struct drm_encoder *encoder, int mode)
2575 {
2576 
2577 }
2578 
2579 static const struct drm_encoder_helper_funcs radeon_atom_ext_helper_funcs = {
2580 	.dpms = radeon_atom_ext_dpms,
2581 	.prepare = radeon_atom_ext_prepare,
2582 	.mode_set = radeon_atom_ext_mode_set,
2583 	.commit = radeon_atom_ext_commit,
2584 	.disable = radeon_atom_ext_disable,
2585 	/* no detect for TMDS/LVDS yet */
2586 };
2587 
2588 static const struct drm_encoder_helper_funcs radeon_atom_dig_helper_funcs = {
2589 	.dpms = radeon_atom_encoder_dpms,
2590 	.mode_fixup = radeon_atom_mode_fixup,
2591 	.prepare = radeon_atom_encoder_prepare,
2592 	.mode_set = radeon_atom_encoder_mode_set,
2593 	.commit = radeon_atom_encoder_commit,
2594 	.disable = radeon_atom_encoder_disable,
2595 	.detect = radeon_atom_dig_detect,
2596 };
2597 
2598 static const struct drm_encoder_helper_funcs radeon_atom_dac_helper_funcs = {
2599 	.dpms = radeon_atom_encoder_dpms,
2600 	.mode_fixup = radeon_atom_mode_fixup,
2601 	.prepare = radeon_atom_encoder_prepare,
2602 	.mode_set = radeon_atom_encoder_mode_set,
2603 	.commit = radeon_atom_encoder_commit,
2604 	.detect = radeon_atom_dac_detect,
2605 };
2606 
radeon_enc_destroy(struct drm_encoder * encoder)2607 void radeon_enc_destroy(struct drm_encoder *encoder)
2608 {
2609 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
2610 	if (radeon_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT))
2611 		radeon_atom_backlight_exit(radeon_encoder);
2612 	kfree(radeon_encoder->enc_priv);
2613 	drm_encoder_cleanup(encoder);
2614 	kfree(radeon_encoder);
2615 }
2616 
2617 static const struct drm_encoder_funcs radeon_atom_enc_funcs = {
2618 	.destroy = radeon_enc_destroy,
2619 };
2620 
2621 static struct radeon_encoder_atom_dac *
radeon_atombios_set_dac_info(struct radeon_encoder * radeon_encoder)2622 radeon_atombios_set_dac_info(struct radeon_encoder *radeon_encoder)
2623 {
2624 	struct drm_device *dev = radeon_encoder->base.dev;
2625 	struct radeon_device *rdev = dev->dev_private;
2626 	struct radeon_encoder_atom_dac *dac = kzalloc(sizeof(struct radeon_encoder_atom_dac), GFP_KERNEL);
2627 
2628 	if (!dac)
2629 		return NULL;
2630 
2631 	dac->tv_std = radeon_atombios_get_tv_info(rdev);
2632 	return dac;
2633 }
2634 
2635 static struct radeon_encoder_atom_dig *
radeon_atombios_set_dig_info(struct radeon_encoder * radeon_encoder)2636 radeon_atombios_set_dig_info(struct radeon_encoder *radeon_encoder)
2637 {
2638 	int encoder_enum = (radeon_encoder->encoder_enum & ENUM_ID_MASK) >> ENUM_ID_SHIFT;
2639 	struct radeon_encoder_atom_dig *dig = kzalloc(sizeof(struct radeon_encoder_atom_dig), GFP_KERNEL);
2640 
2641 	if (!dig)
2642 		return NULL;
2643 
2644 	/* coherent mode by default */
2645 	dig->coherent_mode = true;
2646 	dig->dig_encoder = -1;
2647 
2648 	if (encoder_enum == 2)
2649 		dig->linkb = true;
2650 	else
2651 		dig->linkb = false;
2652 
2653 	return dig;
2654 }
2655 
2656 void
radeon_add_atom_encoder(struct drm_device * dev,uint32_t encoder_enum,uint32_t supported_device,u16 caps)2657 radeon_add_atom_encoder(struct drm_device *dev,
2658 			uint32_t encoder_enum,
2659 			uint32_t supported_device,
2660 			u16 caps)
2661 {
2662 	struct radeon_device *rdev = dev->dev_private;
2663 	struct drm_encoder *encoder;
2664 	struct radeon_encoder *radeon_encoder;
2665 
2666 	/* see if we already added it */
2667 	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
2668 		radeon_encoder = to_radeon_encoder(encoder);
2669 		if (radeon_encoder->encoder_enum == encoder_enum) {
2670 			radeon_encoder->devices |= supported_device;
2671 			return;
2672 		}
2673 
2674 	}
2675 
2676 	/* add a new one */
2677 	radeon_encoder = kzalloc(sizeof(struct radeon_encoder), GFP_KERNEL);
2678 	if (!radeon_encoder)
2679 		return;
2680 
2681 	encoder = &radeon_encoder->base;
2682 	switch (rdev->num_crtc) {
2683 	case 1:
2684 		encoder->possible_crtcs = 0x1;
2685 		break;
2686 	case 2:
2687 	default:
2688 		encoder->possible_crtcs = 0x3;
2689 		break;
2690 	case 4:
2691 		encoder->possible_crtcs = 0xf;
2692 		break;
2693 	case 6:
2694 		encoder->possible_crtcs = 0x3f;
2695 		break;
2696 	}
2697 
2698 	radeon_encoder->enc_priv = NULL;
2699 
2700 	radeon_encoder->encoder_enum = encoder_enum;
2701 	radeon_encoder->encoder_id = (encoder_enum & OBJECT_ID_MASK) >> OBJECT_ID_SHIFT;
2702 	radeon_encoder->devices = supported_device;
2703 	radeon_encoder->rmx_type = RMX_OFF;
2704 	radeon_encoder->underscan_type = UNDERSCAN_OFF;
2705 	radeon_encoder->is_ext_encoder = false;
2706 	radeon_encoder->caps = caps;
2707 
2708 	switch (radeon_encoder->encoder_id) {
2709 	case ENCODER_OBJECT_ID_INTERNAL_LVDS:
2710 	case ENCODER_OBJECT_ID_INTERNAL_TMDS1:
2711 	case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_TMDS1:
2712 	case ENCODER_OBJECT_ID_INTERNAL_LVTM1:
2713 		if (radeon_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT)) {
2714 			radeon_encoder->rmx_type = RMX_FULL;
2715 			drm_encoder_init(dev, encoder, &radeon_atom_enc_funcs,
2716 					 DRM_MODE_ENCODER_LVDS, NULL);
2717 			radeon_encoder->enc_priv = radeon_atombios_get_lvds_info(radeon_encoder);
2718 		} else {
2719 			drm_encoder_init(dev, encoder, &radeon_atom_enc_funcs,
2720 					 DRM_MODE_ENCODER_TMDS, NULL);
2721 			radeon_encoder->enc_priv = radeon_atombios_set_dig_info(radeon_encoder);
2722 		}
2723 		drm_encoder_helper_add(encoder, &radeon_atom_dig_helper_funcs);
2724 		break;
2725 	case ENCODER_OBJECT_ID_INTERNAL_DAC1:
2726 		drm_encoder_init(dev, encoder, &radeon_atom_enc_funcs,
2727 				 DRM_MODE_ENCODER_DAC, NULL);
2728 		radeon_encoder->enc_priv = radeon_atombios_set_dac_info(radeon_encoder);
2729 		drm_encoder_helper_add(encoder, &radeon_atom_dac_helper_funcs);
2730 		break;
2731 	case ENCODER_OBJECT_ID_INTERNAL_DAC2:
2732 	case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DAC1:
2733 	case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DAC2:
2734 		drm_encoder_init(dev, encoder, &radeon_atom_enc_funcs,
2735 				 DRM_MODE_ENCODER_TVDAC, NULL);
2736 		radeon_encoder->enc_priv = radeon_atombios_set_dac_info(radeon_encoder);
2737 		drm_encoder_helper_add(encoder, &radeon_atom_dac_helper_funcs);
2738 		break;
2739 	case ENCODER_OBJECT_ID_INTERNAL_DVO1:
2740 	case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DVO1:
2741 	case ENCODER_OBJECT_ID_INTERNAL_DDI:
2742 	case ENCODER_OBJECT_ID_INTERNAL_UNIPHY:
2743 	case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_LVTMA:
2744 	case ENCODER_OBJECT_ID_INTERNAL_UNIPHY1:
2745 	case ENCODER_OBJECT_ID_INTERNAL_UNIPHY2:
2746 	case ENCODER_OBJECT_ID_INTERNAL_UNIPHY3:
2747 		if (radeon_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT)) {
2748 			radeon_encoder->rmx_type = RMX_FULL;
2749 			drm_encoder_init(dev, encoder, &radeon_atom_enc_funcs,
2750 					 DRM_MODE_ENCODER_LVDS, NULL);
2751 			radeon_encoder->enc_priv = radeon_atombios_get_lvds_info(radeon_encoder);
2752 		} else if (radeon_encoder->devices & (ATOM_DEVICE_CRT_SUPPORT)) {
2753 			drm_encoder_init(dev, encoder, &radeon_atom_enc_funcs,
2754 					 DRM_MODE_ENCODER_DAC, NULL);
2755 			radeon_encoder->enc_priv = radeon_atombios_set_dig_info(radeon_encoder);
2756 		} else {
2757 			drm_encoder_init(dev, encoder, &radeon_atom_enc_funcs,
2758 					 DRM_MODE_ENCODER_TMDS, NULL);
2759 			radeon_encoder->enc_priv = radeon_atombios_set_dig_info(radeon_encoder);
2760 		}
2761 		drm_encoder_helper_add(encoder, &radeon_atom_dig_helper_funcs);
2762 		break;
2763 	case ENCODER_OBJECT_ID_SI170B:
2764 	case ENCODER_OBJECT_ID_CH7303:
2765 	case ENCODER_OBJECT_ID_EXTERNAL_SDVOA:
2766 	case ENCODER_OBJECT_ID_EXTERNAL_SDVOB:
2767 	case ENCODER_OBJECT_ID_TITFP513:
2768 	case ENCODER_OBJECT_ID_VT1623:
2769 	case ENCODER_OBJECT_ID_HDMI_SI1930:
2770 	case ENCODER_OBJECT_ID_TRAVIS:
2771 	case ENCODER_OBJECT_ID_NUTMEG:
2772 		/* these are handled by the primary encoders */
2773 		radeon_encoder->is_ext_encoder = true;
2774 		if (radeon_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT))
2775 			drm_encoder_init(dev, encoder, &radeon_atom_enc_funcs,
2776 					 DRM_MODE_ENCODER_LVDS, NULL);
2777 		else if (radeon_encoder->devices & (ATOM_DEVICE_CRT_SUPPORT))
2778 			drm_encoder_init(dev, encoder, &radeon_atom_enc_funcs,
2779 					 DRM_MODE_ENCODER_DAC, NULL);
2780 		else
2781 			drm_encoder_init(dev, encoder, &radeon_atom_enc_funcs,
2782 					 DRM_MODE_ENCODER_TMDS, NULL);
2783 		drm_encoder_helper_add(encoder, &radeon_atom_ext_helper_funcs);
2784 		break;
2785 	}
2786 }
2787