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