1b843c749SSergey Zigachev /*
2b843c749SSergey Zigachev  * Copyright 2007-11 Advanced Micro Devices, Inc.
3b843c749SSergey Zigachev  * Copyright 2008 Red Hat Inc.
4b843c749SSergey Zigachev  *
5b843c749SSergey Zigachev  * Permission is hereby granted, free of charge, to any person obtaining a
6b843c749SSergey Zigachev  * copy of this software and associated documentation files (the "Software"),
7b843c749SSergey Zigachev  * to deal in the Software without restriction, including without limitation
8b843c749SSergey Zigachev  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9b843c749SSergey Zigachev  * and/or sell copies of the Software, and to permit persons to whom the
10b843c749SSergey Zigachev  * Software is furnished to do so, subject to the following conditions:
11b843c749SSergey Zigachev  *
12b843c749SSergey Zigachev  * The above copyright notice and this permission notice shall be included in
13b843c749SSergey Zigachev  * all copies or substantial portions of the Software.
14b843c749SSergey Zigachev  *
15b843c749SSergey Zigachev  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16b843c749SSergey Zigachev  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17b843c749SSergey Zigachev  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18b843c749SSergey Zigachev  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19b843c749SSergey Zigachev  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20b843c749SSergey Zigachev  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21b843c749SSergey Zigachev  * OTHER DEALINGS IN THE SOFTWARE.
22b843c749SSergey Zigachev  *
23b843c749SSergey Zigachev  * Authors: Dave Airlie
24b843c749SSergey Zigachev  *          Alex Deucher
25b843c749SSergey Zigachev  */
26b843c749SSergey Zigachev #include <drm/drmP.h>
27b843c749SSergey Zigachev #include <drm/drm_crtc_helper.h>
28b843c749SSergey Zigachev #include <drm/amdgpu_drm.h>
29b843c749SSergey Zigachev #include "amdgpu.h"
30b843c749SSergey Zigachev #include "amdgpu_connectors.h"
31b843c749SSergey Zigachev #include "atom.h"
32b843c749SSergey Zigachev #include "atombios_encoders.h"
33b843c749SSergey Zigachev #include "atombios_dp.h"
34b843c749SSergey Zigachev #include <linux/backlight.h>
35b843c749SSergey Zigachev #include "bif/bif_4_1_d.h"
36b843c749SSergey Zigachev 
37b843c749SSergey Zigachev u8
amdgpu_atombios_encoder_get_backlight_level_from_reg(struct amdgpu_device * adev)38b843c749SSergey Zigachev amdgpu_atombios_encoder_get_backlight_level_from_reg(struct amdgpu_device *adev)
39b843c749SSergey Zigachev {
40b843c749SSergey Zigachev 	u8 backlight_level;
41b843c749SSergey Zigachev 	u32 bios_2_scratch;
42b843c749SSergey Zigachev 
43b843c749SSergey Zigachev 	bios_2_scratch = RREG32(mmBIOS_SCRATCH_2);
44b843c749SSergey Zigachev 
45b843c749SSergey Zigachev 	backlight_level = ((bios_2_scratch & ATOM_S2_CURRENT_BL_LEVEL_MASK) >>
46b843c749SSergey Zigachev 			   ATOM_S2_CURRENT_BL_LEVEL_SHIFT);
47b843c749SSergey Zigachev 
48b843c749SSergey Zigachev 	return backlight_level;
49b843c749SSergey Zigachev }
50b843c749SSergey Zigachev 
51b843c749SSergey Zigachev void
amdgpu_atombios_encoder_set_backlight_level_to_reg(struct amdgpu_device * adev,u8 backlight_level)52b843c749SSergey Zigachev amdgpu_atombios_encoder_set_backlight_level_to_reg(struct amdgpu_device *adev,
53b843c749SSergey Zigachev 					    u8 backlight_level)
54b843c749SSergey Zigachev {
55b843c749SSergey Zigachev 	u32 bios_2_scratch;
56b843c749SSergey Zigachev 
57b843c749SSergey Zigachev 	bios_2_scratch = RREG32(mmBIOS_SCRATCH_2);
58b843c749SSergey Zigachev 
59b843c749SSergey Zigachev 	bios_2_scratch &= ~ATOM_S2_CURRENT_BL_LEVEL_MASK;
60b843c749SSergey Zigachev 	bios_2_scratch |= ((backlight_level << ATOM_S2_CURRENT_BL_LEVEL_SHIFT) &
61b843c749SSergey Zigachev 			   ATOM_S2_CURRENT_BL_LEVEL_MASK);
62b843c749SSergey Zigachev 
63b843c749SSergey Zigachev 	WREG32(mmBIOS_SCRATCH_2, bios_2_scratch);
64b843c749SSergey Zigachev }
65b843c749SSergey Zigachev 
66b843c749SSergey Zigachev u8
amdgpu_atombios_encoder_get_backlight_level(struct amdgpu_encoder * amdgpu_encoder)67b843c749SSergey Zigachev amdgpu_atombios_encoder_get_backlight_level(struct amdgpu_encoder *amdgpu_encoder)
68b843c749SSergey Zigachev {
69b843c749SSergey Zigachev 	struct drm_device *dev = amdgpu_encoder->base.dev;
70b843c749SSergey Zigachev 	struct amdgpu_device *adev = dev->dev_private;
71b843c749SSergey Zigachev 
72b843c749SSergey Zigachev 	if (!(adev->mode_info.firmware_flags & ATOM_BIOS_INFO_BL_CONTROLLED_BY_GPU))
73b843c749SSergey Zigachev 		return 0;
74b843c749SSergey Zigachev 
75b843c749SSergey Zigachev 	return amdgpu_atombios_encoder_get_backlight_level_from_reg(adev);
76b843c749SSergey Zigachev }
77b843c749SSergey Zigachev 
78b843c749SSergey Zigachev void
amdgpu_atombios_encoder_set_backlight_level(struct amdgpu_encoder * amdgpu_encoder,u8 level)79b843c749SSergey Zigachev amdgpu_atombios_encoder_set_backlight_level(struct amdgpu_encoder *amdgpu_encoder,
80b843c749SSergey Zigachev 				     u8 level)
81b843c749SSergey Zigachev {
82b843c749SSergey Zigachev 	struct drm_encoder *encoder = &amdgpu_encoder->base;
83b843c749SSergey Zigachev 	struct drm_device *dev = amdgpu_encoder->base.dev;
84b843c749SSergey Zigachev 	struct amdgpu_device *adev = dev->dev_private;
85b843c749SSergey Zigachev 	struct amdgpu_encoder_atom_dig *dig;
86b843c749SSergey Zigachev 
87b843c749SSergey Zigachev 	if (!(adev->mode_info.firmware_flags & ATOM_BIOS_INFO_BL_CONTROLLED_BY_GPU))
88b843c749SSergey Zigachev 		return;
89b843c749SSergey Zigachev 
90b843c749SSergey Zigachev 	if ((amdgpu_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT)) &&
91b843c749SSergey Zigachev 	    amdgpu_encoder->enc_priv) {
92b843c749SSergey Zigachev 		dig = amdgpu_encoder->enc_priv;
93b843c749SSergey Zigachev 		dig->backlight_level = level;
94b843c749SSergey Zigachev 		amdgpu_atombios_encoder_set_backlight_level_to_reg(adev, dig->backlight_level);
95b843c749SSergey Zigachev 
96b843c749SSergey Zigachev 		switch (amdgpu_encoder->encoder_id) {
97b843c749SSergey Zigachev 		case ENCODER_OBJECT_ID_INTERNAL_UNIPHY:
98b843c749SSergey Zigachev 		case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_LVTMA:
99b843c749SSergey Zigachev 		case ENCODER_OBJECT_ID_INTERNAL_UNIPHY1:
100b843c749SSergey Zigachev 		case ENCODER_OBJECT_ID_INTERNAL_UNIPHY2:
101b843c749SSergey Zigachev 		case ENCODER_OBJECT_ID_INTERNAL_UNIPHY3:
102b843c749SSergey Zigachev 			if (dig->backlight_level == 0)
103b843c749SSergey Zigachev 				amdgpu_atombios_encoder_setup_dig_transmitter(encoder,
104b843c749SSergey Zigachev 								       ATOM_TRANSMITTER_ACTION_LCD_BLOFF, 0, 0);
105b843c749SSergey Zigachev 			else {
106b843c749SSergey Zigachev 				amdgpu_atombios_encoder_setup_dig_transmitter(encoder,
107b843c749SSergey Zigachev 								       ATOM_TRANSMITTER_ACTION_BL_BRIGHTNESS_CONTROL, 0, 0);
108b843c749SSergey Zigachev 				amdgpu_atombios_encoder_setup_dig_transmitter(encoder,
109b843c749SSergey Zigachev 								       ATOM_TRANSMITTER_ACTION_LCD_BLON, 0, 0);
110b843c749SSergey Zigachev 			}
111b843c749SSergey Zigachev 			break;
112b843c749SSergey Zigachev 		default:
113b843c749SSergey Zigachev 			break;
114b843c749SSergey Zigachev 		}
115b843c749SSergey Zigachev 	}
116b843c749SSergey Zigachev }
117b843c749SSergey Zigachev 
118b843c749SSergey Zigachev #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) || defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
119b843c749SSergey Zigachev 
120*78973132SSergey Zigachev #if 0
121b843c749SSergey Zigachev static u8 amdgpu_atombios_encoder_backlight_level(struct backlight_device *bd)
122b843c749SSergey Zigachev {
123b843c749SSergey Zigachev 	u8 level;
124b843c749SSergey Zigachev 
125b843c749SSergey Zigachev 	/* Convert brightness to hardware level */
126b843c749SSergey Zigachev 	if (bd->props.brightness < 0)
127b843c749SSergey Zigachev 		level = 0;
128b843c749SSergey Zigachev 	else if (bd->props.brightness > AMDGPU_MAX_BL_LEVEL)
129b843c749SSergey Zigachev 		level = AMDGPU_MAX_BL_LEVEL;
130b843c749SSergey Zigachev 	else
131b843c749SSergey Zigachev 		level = bd->props.brightness;
132b843c749SSergey Zigachev 
133b843c749SSergey Zigachev 	return level;
134b843c749SSergey Zigachev }
135b843c749SSergey Zigachev 
136b843c749SSergey Zigachev static int amdgpu_atombios_encoder_update_backlight_status(struct backlight_device *bd)
137b843c749SSergey Zigachev {
138b843c749SSergey Zigachev 	struct amdgpu_backlight_privdata *pdata = bl_get_data(bd);
139b843c749SSergey Zigachev 	struct amdgpu_encoder *amdgpu_encoder = pdata->encoder;
140b843c749SSergey Zigachev 
141b843c749SSergey Zigachev 	amdgpu_atombios_encoder_set_backlight_level(amdgpu_encoder,
142b843c749SSergey Zigachev 					     amdgpu_atombios_encoder_backlight_level(bd));
143b843c749SSergey Zigachev 
144b843c749SSergey Zigachev 	return 0;
145b843c749SSergey Zigachev }
146b843c749SSergey Zigachev 
147b843c749SSergey Zigachev static int
148b843c749SSergey Zigachev amdgpu_atombios_encoder_get_backlight_brightness(struct backlight_device *bd)
149b843c749SSergey Zigachev {
150b843c749SSergey Zigachev 	struct amdgpu_backlight_privdata *pdata = bl_get_data(bd);
151b843c749SSergey Zigachev 	struct amdgpu_encoder *amdgpu_encoder = pdata->encoder;
152b843c749SSergey Zigachev 	struct drm_device *dev = amdgpu_encoder->base.dev;
153b843c749SSergey Zigachev 	struct amdgpu_device *adev = dev->dev_private;
154b843c749SSergey Zigachev 
155b843c749SSergey Zigachev 	return amdgpu_atombios_encoder_get_backlight_level_from_reg(adev);
156b843c749SSergey Zigachev }
157b843c749SSergey Zigachev 
158b843c749SSergey Zigachev static const struct backlight_ops amdgpu_atombios_encoder_backlight_ops = {
159b843c749SSergey Zigachev 	.get_brightness = amdgpu_atombios_encoder_get_backlight_brightness,
160b843c749SSergey Zigachev 	.update_status	= amdgpu_atombios_encoder_update_backlight_status,
161b843c749SSergey Zigachev };
162*78973132SSergey Zigachev #endif
163*78973132SSergey Zigachev 
164*78973132SSergey Zigachev /*
165*78973132SSergey Zigachev  * Read max backlight level
166*78973132SSergey Zigachev  */
167*78973132SSergey Zigachev static int
sysctl_backlight_max(SYSCTL_HANDLER_ARGS)168*78973132SSergey Zigachev sysctl_backlight_max(SYSCTL_HANDLER_ARGS)
169*78973132SSergey Zigachev {
170*78973132SSergey Zigachev 	int err, val;
171*78973132SSergey Zigachev 
172*78973132SSergey Zigachev 	val = AMDGPU_MAX_BL_LEVEL;
173*78973132SSergey Zigachev 	err = sysctl_handle_int(oidp, &val, 0, req);
174*78973132SSergey Zigachev 	return(err);
175*78973132SSergey Zigachev }
176*78973132SSergey Zigachev 
177*78973132SSergey Zigachev /*
178*78973132SSergey Zigachev  * Read/write backlight level
179*78973132SSergey Zigachev  */
180*78973132SSergey Zigachev static int
sysctl_backlight_handler(SYSCTL_HANDLER_ARGS)181*78973132SSergey Zigachev sysctl_backlight_handler(SYSCTL_HANDLER_ARGS)
182*78973132SSergey Zigachev {
183*78973132SSergey Zigachev 	struct amdgpu_encoder *encoder;
184*78973132SSergey Zigachev 	struct amdgpu_encoder_atom_dig *dig;
185*78973132SSergey Zigachev 	int err, val;
186*78973132SSergey Zigachev 
187*78973132SSergey Zigachev 	encoder = (struct amdgpu_encoder *)arg1;
188*78973132SSergey Zigachev 	dig = encoder->enc_priv;
189*78973132SSergey Zigachev 	val = dig->backlight_level;
190*78973132SSergey Zigachev 
191*78973132SSergey Zigachev 	err = sysctl_handle_int(oidp, &val, 0, req);
192*78973132SSergey Zigachev 	if (err != 0 || req->newptr == NULL) {
193*78973132SSergey Zigachev 		return(err);
194*78973132SSergey Zigachev 	}
195*78973132SSergey Zigachev 	if (dig->backlight_level != val && val >= 0 &&
196*78973132SSergey Zigachev 	    val <= AMDGPU_MAX_BL_LEVEL) {
197*78973132SSergey Zigachev 		amdgpu_atombios_encoder_set_backlight_level(encoder, val);
198*78973132SSergey Zigachev 	}
199*78973132SSergey Zigachev 
200*78973132SSergey Zigachev 	return(err);
201*78973132SSergey Zigachev }
202b843c749SSergey Zigachev 
amdgpu_atombios_encoder_init_backlight(struct amdgpu_encoder * amdgpu_encoder,struct drm_connector * drm_connector)203b843c749SSergey Zigachev void amdgpu_atombios_encoder_init_backlight(struct amdgpu_encoder *amdgpu_encoder,
204b843c749SSergey Zigachev 				     struct drm_connector *drm_connector)
205b843c749SSergey Zigachev {
206b843c749SSergey Zigachev 	struct drm_device *dev = amdgpu_encoder->base.dev;
207b843c749SSergey Zigachev 	struct amdgpu_device *adev = dev->dev_private;
208*78973132SSergey Zigachev #if 0
209b843c749SSergey Zigachev 	struct backlight_device *bd;
210b843c749SSergey Zigachev 	struct backlight_properties props;
211b843c749SSergey Zigachev 	struct amdgpu_backlight_privdata *pdata;
212*78973132SSergey Zigachev #endif
213b843c749SSergey Zigachev 	struct amdgpu_encoder_atom_dig *dig;
214b843c749SSergey Zigachev 	u8 backlight_level;
215*78973132SSergey Zigachev #if 0
216b843c749SSergey Zigachev 	char bl_name[16];
217*78973132SSergey Zigachev #endif
218b843c749SSergey Zigachev 
219b843c749SSergey Zigachev 	/* Mac laptops with multiple GPUs use the gmux driver for backlight
220b843c749SSergey Zigachev 	 * so don't register a backlight device
221b843c749SSergey Zigachev 	 */
222b843c749SSergey Zigachev 	if ((adev->pdev->subsystem_vendor == PCI_VENDOR_ID_APPLE) &&
223b843c749SSergey Zigachev 	    (adev->pdev->device == 0x6741))
224b843c749SSergey Zigachev 		return;
225b843c749SSergey Zigachev 
226b843c749SSergey Zigachev 	if (!amdgpu_encoder->enc_priv)
227b843c749SSergey Zigachev 		return;
228b843c749SSergey Zigachev 
229b843c749SSergey Zigachev 	if (!(adev->mode_info.firmware_flags & ATOM_BIOS_INFO_BL_CONTROLLED_BY_GPU))
230b843c749SSergey Zigachev 		return;
231b843c749SSergey Zigachev 
232*78973132SSergey Zigachev #if 0
233*78973132SSergey Zigachev 	pdata = kmalloc(sizeof(struct amdgpu_backlight_privdata), M_DRM, GFP_KERNEL);
234b843c749SSergey Zigachev 	if (!pdata) {
235b843c749SSergey Zigachev 		DRM_ERROR("Memory allocation failed\n");
236b843c749SSergey Zigachev 		goto error;
237b843c749SSergey Zigachev 	}
238b843c749SSergey Zigachev 
239b843c749SSergey Zigachev 	memset(&props, 0, sizeof(props));
240b843c749SSergey Zigachev 	props.max_brightness = AMDGPU_MAX_BL_LEVEL;
241b843c749SSergey Zigachev 	props.type = BACKLIGHT_RAW;
242b843c749SSergey Zigachev 	snprintf(bl_name, sizeof(bl_name),
243b843c749SSergey Zigachev 		 "amdgpu_bl%d", dev->primary->index);
244b843c749SSergey Zigachev 	bd = backlight_device_register(bl_name, drm_connector->kdev,
245b843c749SSergey Zigachev 				       pdata, &amdgpu_atombios_encoder_backlight_ops, &props);
246b843c749SSergey Zigachev 	if (IS_ERR(bd)) {
247b843c749SSergey Zigachev 		DRM_ERROR("Backlight registration failed\n");
248b843c749SSergey Zigachev 		goto error;
249b843c749SSergey Zigachev 	}
250b843c749SSergey Zigachev 
251b843c749SSergey Zigachev 	pdata->encoder = amdgpu_encoder;
252*78973132SSergey Zigachev #endif
253b843c749SSergey Zigachev 
254b843c749SSergey Zigachev 	backlight_level = amdgpu_atombios_encoder_get_backlight_level_from_reg(adev);
255b843c749SSergey Zigachev 
256b843c749SSergey Zigachev 	dig = amdgpu_encoder->enc_priv;
257*78973132SSergey Zigachev #if 0
258b843c749SSergey Zigachev 	dig->bl_dev = bd;
259b843c749SSergey Zigachev 
260b843c749SSergey Zigachev 	bd->props.brightness = amdgpu_atombios_encoder_get_backlight_brightness(bd);
261b843c749SSergey Zigachev 	bd->props.power = FB_BLANK_UNBLANK;
262b843c749SSergey Zigachev 	backlight_update_status(bd);
263*78973132SSergey Zigachev #endif
264b843c749SSergey Zigachev 
265b843c749SSergey Zigachev 	DRM_INFO("amdgpu atom DIG backlight initialized\n");
266b843c749SSergey Zigachev 
267*78973132SSergey Zigachev #ifdef __DragonFly__
268*78973132SSergey Zigachev 	dig->backlight_level = backlight_level;
269*78973132SSergey Zigachev 
270*78973132SSergey Zigachev 	adev->mode_info.bl_encoder = amdgpu_encoder;
271*78973132SSergey Zigachev 
272*78973132SSergey Zigachev 	SYSCTL_ADD_PROC(&drm_connector->dev->sysctl->ctx, &sysctl__hw_children,
273*78973132SSergey Zigachev 			OID_AUTO, "backlight_max",
274*78973132SSergey Zigachev 			CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_ANYBODY,
275*78973132SSergey Zigachev 			amdgpu_encoder, sizeof(int),
276*78973132SSergey Zigachev 			sysctl_backlight_max,
277*78973132SSergey Zigachev 			"I", "Max backlight level");
278*78973132SSergey Zigachev 	SYSCTL_ADD_PROC(&drm_connector->dev->sysctl->ctx, &sysctl__hw_children,
279*78973132SSergey Zigachev 			OID_AUTO, "backlight_level",
280*78973132SSergey Zigachev 			CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY,
281*78973132SSergey Zigachev 			amdgpu_encoder, sizeof(int),
282*78973132SSergey Zigachev 			sysctl_backlight_handler,
283*78973132SSergey Zigachev 			"I", "Backlight level");
284*78973132SSergey Zigachev #endif
285*78973132SSergey Zigachev 
286b843c749SSergey Zigachev 	return;
287b843c749SSergey Zigachev 
288*78973132SSergey Zigachev #if 0
289b843c749SSergey Zigachev  error:
290b843c749SSergey Zigachev  	kfree(pdata);
291b843c749SSergey Zigachev  	return;
292*78973132SSergey Zigachev #endif
293b843c749SSergey Zigachev }
294b843c749SSergey Zigachev 
295b843c749SSergey Zigachev void
amdgpu_atombios_encoder_fini_backlight(struct amdgpu_encoder * amdgpu_encoder)296b843c749SSergey Zigachev amdgpu_atombios_encoder_fini_backlight(struct amdgpu_encoder *amdgpu_encoder)
297b843c749SSergey Zigachev {
298*78973132SSergey Zigachev #if 0
299b843c749SSergey Zigachev 	struct drm_device *dev = amdgpu_encoder->base.dev;
300b843c749SSergey Zigachev 	struct amdgpu_device *adev = dev->dev_private;
301b843c749SSergey Zigachev 	struct backlight_device *bd = NULL;
302b843c749SSergey Zigachev 	struct amdgpu_encoder_atom_dig *dig;
303b843c749SSergey Zigachev 
304b843c749SSergey Zigachev 	if (!amdgpu_encoder->enc_priv)
305b843c749SSergey Zigachev 		return;
306b843c749SSergey Zigachev 
307b843c749SSergey Zigachev 	if (!(adev->mode_info.firmware_flags & ATOM_BIOS_INFO_BL_CONTROLLED_BY_GPU))
308b843c749SSergey Zigachev 		return;
309b843c749SSergey Zigachev 
310b843c749SSergey Zigachev 	dig = amdgpu_encoder->enc_priv;
311b843c749SSergey Zigachev 	bd = dig->bl_dev;
312b843c749SSergey Zigachev 	dig->bl_dev = NULL;
313b843c749SSergey Zigachev 
314b843c749SSergey Zigachev 	if (bd) {
315b843c749SSergey Zigachev 		struct amdgpu_legacy_backlight_privdata *pdata;
316b843c749SSergey Zigachev 
317b843c749SSergey Zigachev 		pdata = bl_get_data(bd);
318b843c749SSergey Zigachev 		backlight_device_unregister(bd);
319b843c749SSergey Zigachev 		kfree(pdata);
320b843c749SSergey Zigachev 
321b843c749SSergey Zigachev 		DRM_INFO("amdgpu atom LVDS backlight unloaded\n");
322b843c749SSergey Zigachev 	}
323*78973132SSergey Zigachev #endif
324b843c749SSergey Zigachev }
325b843c749SSergey Zigachev 
326b843c749SSergey Zigachev #else /* !CONFIG_BACKLIGHT_CLASS_DEVICE */
327b843c749SSergey Zigachev 
amdgpu_atombios_encoder_init_backlight(struct amdgpu_encoder * encoder)328b843c749SSergey Zigachev void amdgpu_atombios_encoder_init_backlight(struct amdgpu_encoder *encoder)
329b843c749SSergey Zigachev {
330b843c749SSergey Zigachev }
331b843c749SSergey Zigachev 
amdgpu_atombios_encoder_fini_backlight(struct amdgpu_encoder * encoder)332b843c749SSergey Zigachev void amdgpu_atombios_encoder_fini_backlight(struct amdgpu_encoder *encoder)
333b843c749SSergey Zigachev {
334b843c749SSergey Zigachev }
335b843c749SSergey Zigachev 
336b843c749SSergey Zigachev #endif
337b843c749SSergey Zigachev 
amdgpu_atombios_encoder_is_digital(struct drm_encoder * encoder)338b843c749SSergey Zigachev bool amdgpu_atombios_encoder_is_digital(struct drm_encoder *encoder)
339b843c749SSergey Zigachev {
340b843c749SSergey Zigachev 	struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
341b843c749SSergey Zigachev 	switch (amdgpu_encoder->encoder_id) {
342b843c749SSergey Zigachev 	case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DVO1:
343b843c749SSergey Zigachev 	case ENCODER_OBJECT_ID_INTERNAL_UNIPHY:
344b843c749SSergey Zigachev 	case ENCODER_OBJECT_ID_INTERNAL_UNIPHY1:
345b843c749SSergey Zigachev 	case ENCODER_OBJECT_ID_INTERNAL_UNIPHY2:
346b843c749SSergey Zigachev 	case ENCODER_OBJECT_ID_INTERNAL_UNIPHY3:
347b843c749SSergey Zigachev 		return true;
348b843c749SSergey Zigachev 	default:
349b843c749SSergey Zigachev 		return false;
350b843c749SSergey Zigachev 	}
351b843c749SSergey Zigachev }
352b843c749SSergey Zigachev 
amdgpu_atombios_encoder_mode_fixup(struct drm_encoder * encoder,const struct drm_display_mode * mode,struct drm_display_mode * adjusted_mode)353b843c749SSergey Zigachev bool amdgpu_atombios_encoder_mode_fixup(struct drm_encoder *encoder,
354b843c749SSergey Zigachev 				 const struct drm_display_mode *mode,
355b843c749SSergey Zigachev 				 struct drm_display_mode *adjusted_mode)
356b843c749SSergey Zigachev {
357b843c749SSergey Zigachev 	struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
358b843c749SSergey Zigachev 
359b843c749SSergey Zigachev 	/* set the active encoder to connector routing */
360b843c749SSergey Zigachev 	amdgpu_encoder_set_active_device(encoder);
361b843c749SSergey Zigachev 	drm_mode_set_crtcinfo(adjusted_mode, 0);
362b843c749SSergey Zigachev 
363b843c749SSergey Zigachev 	/* hw bug */
364b843c749SSergey Zigachev 	if ((mode->flags & DRM_MODE_FLAG_INTERLACE)
365b843c749SSergey Zigachev 	    && (mode->crtc_vsync_start < (mode->crtc_vdisplay + 2)))
366b843c749SSergey Zigachev 		adjusted_mode->crtc_vsync_start = adjusted_mode->crtc_vdisplay + 2;
367b843c749SSergey Zigachev 
368b843c749SSergey Zigachev 	/* vertical FP must be at least 1 */
369b843c749SSergey Zigachev 	if (mode->crtc_vsync_start == mode->crtc_vdisplay)
370b843c749SSergey Zigachev 		adjusted_mode->crtc_vsync_start++;
371b843c749SSergey Zigachev 
372b843c749SSergey Zigachev 	/* get the native mode for scaling */
373b843c749SSergey Zigachev 	if (amdgpu_encoder->active_device & (ATOM_DEVICE_LCD_SUPPORT))
374b843c749SSergey Zigachev 		amdgpu_panel_mode_fixup(encoder, adjusted_mode);
375b843c749SSergey Zigachev 	else if (amdgpu_encoder->rmx_type != RMX_OFF)
376b843c749SSergey Zigachev 		amdgpu_panel_mode_fixup(encoder, adjusted_mode);
377b843c749SSergey Zigachev 
378b843c749SSergey Zigachev 	if ((amdgpu_encoder->active_device & (ATOM_DEVICE_DFP_SUPPORT | ATOM_DEVICE_LCD_SUPPORT)) ||
379b843c749SSergey Zigachev 	    (amdgpu_encoder_get_dp_bridge_encoder_id(encoder) != ENCODER_OBJECT_ID_NONE)) {
380b843c749SSergey Zigachev 		struct drm_connector *connector = amdgpu_get_connector_for_encoder(encoder);
381b843c749SSergey Zigachev 		amdgpu_atombios_dp_set_link_config(connector, adjusted_mode);
382b843c749SSergey Zigachev 	}
383b843c749SSergey Zigachev 
384b843c749SSergey Zigachev 	return true;
385b843c749SSergey Zigachev }
386b843c749SSergey Zigachev 
387b843c749SSergey Zigachev static void
amdgpu_atombios_encoder_setup_dac(struct drm_encoder * encoder,int action)388b843c749SSergey Zigachev amdgpu_atombios_encoder_setup_dac(struct drm_encoder *encoder, int action)
389b843c749SSergey Zigachev {
390b843c749SSergey Zigachev 	struct drm_device *dev = encoder->dev;
391b843c749SSergey Zigachev 	struct amdgpu_device *adev = dev->dev_private;
392b843c749SSergey Zigachev 	struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
393b843c749SSergey Zigachev 	DAC_ENCODER_CONTROL_PS_ALLOCATION args;
394b843c749SSergey Zigachev 	int index = 0;
395b843c749SSergey Zigachev 
396b843c749SSergey Zigachev 	memset(&args, 0, sizeof(args));
397b843c749SSergey Zigachev 
398b843c749SSergey Zigachev 	switch (amdgpu_encoder->encoder_id) {
399b843c749SSergey Zigachev 	case ENCODER_OBJECT_ID_INTERNAL_DAC1:
400b843c749SSergey Zigachev 	case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DAC1:
401b843c749SSergey Zigachev 		index = GetIndexIntoMasterTable(COMMAND, DAC1EncoderControl);
402b843c749SSergey Zigachev 		break;
403b843c749SSergey Zigachev 	case ENCODER_OBJECT_ID_INTERNAL_DAC2:
404b843c749SSergey Zigachev 	case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DAC2:
405b843c749SSergey Zigachev 		index = GetIndexIntoMasterTable(COMMAND, DAC2EncoderControl);
406b843c749SSergey Zigachev 		break;
407b843c749SSergey Zigachev 	}
408b843c749SSergey Zigachev 
409b843c749SSergey Zigachev 	args.ucAction = action;
410b843c749SSergey Zigachev 	args.ucDacStandard = ATOM_DAC1_PS2;
411b843c749SSergey Zigachev 	args.usPixelClock = cpu_to_le16(amdgpu_encoder->pixel_clock / 10);
412b843c749SSergey Zigachev 
413b843c749SSergey Zigachev 	amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
414b843c749SSergey Zigachev 
415b843c749SSergey Zigachev }
416b843c749SSergey Zigachev 
amdgpu_atombios_encoder_get_bpc(struct drm_encoder * encoder)417b843c749SSergey Zigachev static u8 amdgpu_atombios_encoder_get_bpc(struct drm_encoder *encoder)
418b843c749SSergey Zigachev {
419b843c749SSergey Zigachev 	int bpc = 8;
420b843c749SSergey Zigachev 
421b843c749SSergey Zigachev 	if (encoder->crtc) {
422b843c749SSergey Zigachev 		struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(encoder->crtc);
423b843c749SSergey Zigachev 		bpc = amdgpu_crtc->bpc;
424b843c749SSergey Zigachev 	}
425b843c749SSergey Zigachev 
426b843c749SSergey Zigachev 	switch (bpc) {
427b843c749SSergey Zigachev 	case 0:
428b843c749SSergey Zigachev 		return PANEL_BPC_UNDEFINE;
429b843c749SSergey Zigachev 	case 6:
430b843c749SSergey Zigachev 		return PANEL_6BIT_PER_COLOR;
431b843c749SSergey Zigachev 	case 8:
432b843c749SSergey Zigachev 	default:
433b843c749SSergey Zigachev 		return PANEL_8BIT_PER_COLOR;
434b843c749SSergey Zigachev 	case 10:
435b843c749SSergey Zigachev 		return PANEL_10BIT_PER_COLOR;
436b843c749SSergey Zigachev 	case 12:
437b843c749SSergey Zigachev 		return PANEL_12BIT_PER_COLOR;
438b843c749SSergey Zigachev 	case 16:
439b843c749SSergey Zigachev 		return PANEL_16BIT_PER_COLOR;
440b843c749SSergey Zigachev 	}
441b843c749SSergey Zigachev }
442b843c749SSergey Zigachev 
443b843c749SSergey Zigachev union dvo_encoder_control {
444b843c749SSergey Zigachev 	ENABLE_EXTERNAL_TMDS_ENCODER_PS_ALLOCATION ext_tmds;
445b843c749SSergey Zigachev 	DVO_ENCODER_CONTROL_PS_ALLOCATION dvo;
446b843c749SSergey Zigachev 	DVO_ENCODER_CONTROL_PS_ALLOCATION_V3 dvo_v3;
447b843c749SSergey Zigachev 	DVO_ENCODER_CONTROL_PS_ALLOCATION_V1_4 dvo_v4;
448b843c749SSergey Zigachev };
449b843c749SSergey Zigachev 
450b843c749SSergey Zigachev static void
amdgpu_atombios_encoder_setup_dvo(struct drm_encoder * encoder,int action)451b843c749SSergey Zigachev amdgpu_atombios_encoder_setup_dvo(struct drm_encoder *encoder, int action)
452b843c749SSergey Zigachev {
453b843c749SSergey Zigachev 	struct drm_device *dev = encoder->dev;
454b843c749SSergey Zigachev 	struct amdgpu_device *adev = dev->dev_private;
455b843c749SSergey Zigachev 	struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
456b843c749SSergey Zigachev 	union dvo_encoder_control args;
457b843c749SSergey Zigachev 	int index = GetIndexIntoMasterTable(COMMAND, DVOEncoderControl);
458b843c749SSergey Zigachev 	uint8_t frev, crev;
459b843c749SSergey Zigachev 
460b843c749SSergey Zigachev 	memset(&args, 0, sizeof(args));
461b843c749SSergey Zigachev 
462b843c749SSergey Zigachev 	if (!amdgpu_atom_parse_cmd_header(adev->mode_info.atom_context, index, &frev, &crev))
463b843c749SSergey Zigachev 		return;
464b843c749SSergey Zigachev 
465b843c749SSergey Zigachev 	switch (frev) {
466b843c749SSergey Zigachev 	case 1:
467b843c749SSergey Zigachev 		switch (crev) {
468b843c749SSergey Zigachev 		case 1:
469b843c749SSergey Zigachev 			/* R4xx, R5xx */
470b843c749SSergey Zigachev 			args.ext_tmds.sXTmdsEncoder.ucEnable = action;
471b843c749SSergey Zigachev 
472b843c749SSergey Zigachev 			if (amdgpu_dig_monitor_is_duallink(encoder, amdgpu_encoder->pixel_clock))
473b843c749SSergey Zigachev 				args.ext_tmds.sXTmdsEncoder.ucMisc |= PANEL_ENCODER_MISC_DUAL;
474b843c749SSergey Zigachev 
475b843c749SSergey Zigachev 			args.ext_tmds.sXTmdsEncoder.ucMisc |= ATOM_PANEL_MISC_888RGB;
476b843c749SSergey Zigachev 			break;
477b843c749SSergey Zigachev 		case 2:
478b843c749SSergey Zigachev 			/* RS600/690/740 */
479b843c749SSergey Zigachev 			args.dvo.sDVOEncoder.ucAction = action;
480b843c749SSergey Zigachev 			args.dvo.sDVOEncoder.usPixelClock = cpu_to_le16(amdgpu_encoder->pixel_clock / 10);
481b843c749SSergey Zigachev 			/* DFP1, CRT1, TV1 depending on the type of port */
482b843c749SSergey Zigachev 			args.dvo.sDVOEncoder.ucDeviceType = ATOM_DEVICE_DFP1_INDEX;
483b843c749SSergey Zigachev 
484b843c749SSergey Zigachev 			if (amdgpu_dig_monitor_is_duallink(encoder, amdgpu_encoder->pixel_clock))
485b843c749SSergey Zigachev 				args.dvo.sDVOEncoder.usDevAttr.sDigAttrib.ucAttribute |= PANEL_ENCODER_MISC_DUAL;
486b843c749SSergey Zigachev 			break;
487b843c749SSergey Zigachev 		case 3:
488b843c749SSergey Zigachev 			/* R6xx */
489b843c749SSergey Zigachev 			args.dvo_v3.ucAction = action;
490b843c749SSergey Zigachev 			args.dvo_v3.usPixelClock = cpu_to_le16(amdgpu_encoder->pixel_clock / 10);
491b843c749SSergey Zigachev 			args.dvo_v3.ucDVOConfig = 0; /* XXX */
492b843c749SSergey Zigachev 			break;
493b843c749SSergey Zigachev 		case 4:
494b843c749SSergey Zigachev 			/* DCE8 */
495b843c749SSergey Zigachev 			args.dvo_v4.ucAction = action;
496b843c749SSergey Zigachev 			args.dvo_v4.usPixelClock = cpu_to_le16(amdgpu_encoder->pixel_clock / 10);
497b843c749SSergey Zigachev 			args.dvo_v4.ucDVOConfig = 0; /* XXX */
498b843c749SSergey Zigachev 			args.dvo_v4.ucBitPerColor = amdgpu_atombios_encoder_get_bpc(encoder);
499b843c749SSergey Zigachev 			break;
500b843c749SSergey Zigachev 		default:
501b843c749SSergey Zigachev 			DRM_ERROR("Unknown table version %d, %d\n", frev, crev);
502b843c749SSergey Zigachev 			break;
503b843c749SSergey Zigachev 		}
504b843c749SSergey Zigachev 		break;
505b843c749SSergey Zigachev 	default:
506b843c749SSergey Zigachev 		DRM_ERROR("Unknown table version %d, %d\n", frev, crev);
507b843c749SSergey Zigachev 		break;
508b843c749SSergey Zigachev 	}
509b843c749SSergey Zigachev 
510b843c749SSergey Zigachev 	amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
511b843c749SSergey Zigachev }
512b843c749SSergey Zigachev 
amdgpu_atombios_encoder_get_encoder_mode(struct drm_encoder * encoder)513b843c749SSergey Zigachev int amdgpu_atombios_encoder_get_encoder_mode(struct drm_encoder *encoder)
514b843c749SSergey Zigachev {
515b843c749SSergey Zigachev 	struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
516b843c749SSergey Zigachev 	struct drm_connector *connector;
517b843c749SSergey Zigachev 	struct amdgpu_connector *amdgpu_connector;
518b843c749SSergey Zigachev 	struct amdgpu_connector_atom_dig *dig_connector;
519b843c749SSergey Zigachev 
520b843c749SSergey Zigachev 	/* dp bridges are always DP */
521b843c749SSergey Zigachev 	if (amdgpu_encoder_get_dp_bridge_encoder_id(encoder) != ENCODER_OBJECT_ID_NONE)
522b843c749SSergey Zigachev 		return ATOM_ENCODER_MODE_DP;
523b843c749SSergey Zigachev 
524b843c749SSergey Zigachev 	/* DVO is always DVO */
525b843c749SSergey Zigachev 	if ((amdgpu_encoder->encoder_id == ENCODER_OBJECT_ID_INTERNAL_DVO1) ||
526b843c749SSergey Zigachev 	    (amdgpu_encoder->encoder_id == ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DVO1))
527b843c749SSergey Zigachev 		return ATOM_ENCODER_MODE_DVO;
528b843c749SSergey Zigachev 
529b843c749SSergey Zigachev 	connector = amdgpu_get_connector_for_encoder(encoder);
530b843c749SSergey Zigachev 	/* if we don't have an active device yet, just use one of
531b843c749SSergey Zigachev 	 * the connectors tied to the encoder.
532b843c749SSergey Zigachev 	 */
533b843c749SSergey Zigachev 	if (!connector)
534b843c749SSergey Zigachev 		connector = amdgpu_get_connector_for_encoder_init(encoder);
535b843c749SSergey Zigachev 	amdgpu_connector = to_amdgpu_connector(connector);
536b843c749SSergey Zigachev 
537b843c749SSergey Zigachev 	switch (connector->connector_type) {
538b843c749SSergey Zigachev 	case DRM_MODE_CONNECTOR_DVII:
539b843c749SSergey Zigachev 	case DRM_MODE_CONNECTOR_HDMIB: /* HDMI-B is basically DL-DVI; analog works fine */
540b843c749SSergey Zigachev 		if (amdgpu_audio != 0) {
541b843c749SSergey Zigachev 			if (amdgpu_connector->use_digital &&
542b843c749SSergey Zigachev 			    (amdgpu_connector->audio == AMDGPU_AUDIO_ENABLE))
543b843c749SSergey Zigachev 				return ATOM_ENCODER_MODE_HDMI;
544b843c749SSergey Zigachev 			else if (drm_detect_hdmi_monitor(amdgpu_connector_edid(connector)) &&
545b843c749SSergey Zigachev 				 (amdgpu_connector->audio == AMDGPU_AUDIO_AUTO))
546b843c749SSergey Zigachev 				return ATOM_ENCODER_MODE_HDMI;
547b843c749SSergey Zigachev 			else if (amdgpu_connector->use_digital)
548b843c749SSergey Zigachev 				return ATOM_ENCODER_MODE_DVI;
549b843c749SSergey Zigachev 			else
550b843c749SSergey Zigachev 				return ATOM_ENCODER_MODE_CRT;
551b843c749SSergey Zigachev 		} else if (amdgpu_connector->use_digital) {
552b843c749SSergey Zigachev 			return ATOM_ENCODER_MODE_DVI;
553b843c749SSergey Zigachev 		} else {
554b843c749SSergey Zigachev 			return ATOM_ENCODER_MODE_CRT;
555b843c749SSergey Zigachev 		}
556b843c749SSergey Zigachev 		break;
557b843c749SSergey Zigachev 	case DRM_MODE_CONNECTOR_DVID:
558b843c749SSergey Zigachev 	case DRM_MODE_CONNECTOR_HDMIA:
559b843c749SSergey Zigachev 	default:
560b843c749SSergey Zigachev 		if (amdgpu_audio != 0) {
561b843c749SSergey Zigachev 			if (amdgpu_connector->audio == AMDGPU_AUDIO_ENABLE)
562b843c749SSergey Zigachev 				return ATOM_ENCODER_MODE_HDMI;
563b843c749SSergey Zigachev 			else if (drm_detect_hdmi_monitor(amdgpu_connector_edid(connector)) &&
564b843c749SSergey Zigachev 				 (amdgpu_connector->audio == AMDGPU_AUDIO_AUTO))
565b843c749SSergey Zigachev 				return ATOM_ENCODER_MODE_HDMI;
566b843c749SSergey Zigachev 			else
567b843c749SSergey Zigachev 				return ATOM_ENCODER_MODE_DVI;
568b843c749SSergey Zigachev 		} else {
569b843c749SSergey Zigachev 			return ATOM_ENCODER_MODE_DVI;
570b843c749SSergey Zigachev 		}
571b843c749SSergey Zigachev 		break;
572b843c749SSergey Zigachev 	case DRM_MODE_CONNECTOR_LVDS:
573b843c749SSergey Zigachev 		return ATOM_ENCODER_MODE_LVDS;
574b843c749SSergey Zigachev 		break;
575b843c749SSergey Zigachev 	case DRM_MODE_CONNECTOR_DisplayPort:
576b843c749SSergey Zigachev 		dig_connector = amdgpu_connector->con_priv;
577b843c749SSergey Zigachev 		if ((dig_connector->dp_sink_type == CONNECTOR_OBJECT_ID_DISPLAYPORT) ||
578b843c749SSergey Zigachev 		    (dig_connector->dp_sink_type == CONNECTOR_OBJECT_ID_eDP)) {
579b843c749SSergey Zigachev 			return ATOM_ENCODER_MODE_DP;
580b843c749SSergey Zigachev 		} else if (amdgpu_audio != 0) {
581b843c749SSergey Zigachev 			if (amdgpu_connector->audio == AMDGPU_AUDIO_ENABLE)
582b843c749SSergey Zigachev 				return ATOM_ENCODER_MODE_HDMI;
583b843c749SSergey Zigachev 			else if (drm_detect_hdmi_monitor(amdgpu_connector_edid(connector)) &&
584b843c749SSergey Zigachev 				 (amdgpu_connector->audio == AMDGPU_AUDIO_AUTO))
585b843c749SSergey Zigachev 				return ATOM_ENCODER_MODE_HDMI;
586b843c749SSergey Zigachev 			else
587b843c749SSergey Zigachev 				return ATOM_ENCODER_MODE_DVI;
588b843c749SSergey Zigachev 		} else {
589b843c749SSergey Zigachev 			return ATOM_ENCODER_MODE_DVI;
590b843c749SSergey Zigachev 		}
591b843c749SSergey Zigachev 		break;
592b843c749SSergey Zigachev 	case DRM_MODE_CONNECTOR_eDP:
593b843c749SSergey Zigachev 		return ATOM_ENCODER_MODE_DP;
594b843c749SSergey Zigachev 	case DRM_MODE_CONNECTOR_DVIA:
595b843c749SSergey Zigachev 	case DRM_MODE_CONNECTOR_VGA:
596b843c749SSergey Zigachev 		return ATOM_ENCODER_MODE_CRT;
597b843c749SSergey Zigachev 		break;
598b843c749SSergey Zigachev 	case DRM_MODE_CONNECTOR_Composite:
599b843c749SSergey Zigachev 	case DRM_MODE_CONNECTOR_SVIDEO:
600b843c749SSergey Zigachev 	case DRM_MODE_CONNECTOR_9PinDIN:
601b843c749SSergey Zigachev 		/* fix me */
602b843c749SSergey Zigachev 		return ATOM_ENCODER_MODE_TV;
603b843c749SSergey Zigachev 		/*return ATOM_ENCODER_MODE_CV;*/
604b843c749SSergey Zigachev 		break;
605b843c749SSergey Zigachev 	}
606b843c749SSergey Zigachev }
607b843c749SSergey Zigachev 
608b843c749SSergey Zigachev /*
609b843c749SSergey Zigachev  * DIG Encoder/Transmitter Setup
610b843c749SSergey Zigachev  *
611b843c749SSergey Zigachev  * DCE 6.0
612b843c749SSergey Zigachev  * - 3 DIG transmitter blocks UNIPHY0/1/2 (links A and B).
613b843c749SSergey Zigachev  * Supports up to 6 digital outputs
614b843c749SSergey Zigachev  * - 6 DIG encoder blocks.
615b843c749SSergey Zigachev  * - DIG to PHY mapping is hardcoded
616b843c749SSergey Zigachev  * DIG1 drives UNIPHY0 link A, A+B
617b843c749SSergey Zigachev  * DIG2 drives UNIPHY0 link B
618b843c749SSergey Zigachev  * DIG3 drives UNIPHY1 link A, A+B
619b843c749SSergey Zigachev  * DIG4 drives UNIPHY1 link B
620b843c749SSergey Zigachev  * DIG5 drives UNIPHY2 link A, A+B
621b843c749SSergey Zigachev  * DIG6 drives UNIPHY2 link B
622b843c749SSergey Zigachev  *
623b843c749SSergey Zigachev  * Routing
624b843c749SSergey Zigachev  * crtc -> dig encoder -> UNIPHY/LVTMA (1 or 2 links)
625b843c749SSergey Zigachev  * Examples:
626b843c749SSergey Zigachev  * crtc0 -> dig2 -> LVTMA   links A+B -> TMDS/HDMI
627b843c749SSergey Zigachev  * crtc1 -> dig1 -> UNIPHY0 link  B   -> DP
628b843c749SSergey Zigachev  * crtc0 -> dig1 -> UNIPHY2 link  A   -> LVDS
629b843c749SSergey Zigachev  * crtc1 -> dig2 -> UNIPHY1 link  B+A -> TMDS/HDMI
630b843c749SSergey Zigachev  */
631b843c749SSergey Zigachev 
632b843c749SSergey Zigachev union dig_encoder_control {
633b843c749SSergey Zigachev 	DIG_ENCODER_CONTROL_PS_ALLOCATION v1;
634b843c749SSergey Zigachev 	DIG_ENCODER_CONTROL_PARAMETERS_V2 v2;
635b843c749SSergey Zigachev 	DIG_ENCODER_CONTROL_PARAMETERS_V3 v3;
636b843c749SSergey Zigachev 	DIG_ENCODER_CONTROL_PARAMETERS_V4 v4;
637b843c749SSergey Zigachev 	DIG_ENCODER_CONTROL_PARAMETERS_V5 v5;
638b843c749SSergey Zigachev };
639b843c749SSergey Zigachev 
640b843c749SSergey Zigachev void
amdgpu_atombios_encoder_setup_dig_encoder(struct drm_encoder * encoder,int action,int panel_mode)641b843c749SSergey Zigachev amdgpu_atombios_encoder_setup_dig_encoder(struct drm_encoder *encoder,
642b843c749SSergey Zigachev 				   int action, int panel_mode)
643b843c749SSergey Zigachev {
644b843c749SSergey Zigachev 	struct drm_device *dev = encoder->dev;
645b843c749SSergey Zigachev 	struct amdgpu_device *adev = dev->dev_private;
646b843c749SSergey Zigachev 	struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
647b843c749SSergey Zigachev 	struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
648b843c749SSergey Zigachev 	struct drm_connector *connector = amdgpu_get_connector_for_encoder(encoder);
649b843c749SSergey Zigachev 	union dig_encoder_control args;
650b843c749SSergey Zigachev 	int index = GetIndexIntoMasterTable(COMMAND, DIGxEncoderControl);
651b843c749SSergey Zigachev 	uint8_t frev, crev;
652b843c749SSergey Zigachev 	int dp_clock = 0;
653b843c749SSergey Zigachev 	int dp_lane_count = 0;
654b843c749SSergey Zigachev 	int hpd_id = AMDGPU_HPD_NONE;
655b843c749SSergey Zigachev 
656b843c749SSergey Zigachev 	if (connector) {
657b843c749SSergey Zigachev 		struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
658b843c749SSergey Zigachev 		struct amdgpu_connector_atom_dig *dig_connector =
659b843c749SSergey Zigachev 			amdgpu_connector->con_priv;
660b843c749SSergey Zigachev 
661b843c749SSergey Zigachev 		dp_clock = dig_connector->dp_clock;
662b843c749SSergey Zigachev 		dp_lane_count = dig_connector->dp_lane_count;
663b843c749SSergey Zigachev 		hpd_id = amdgpu_connector->hpd.hpd;
664b843c749SSergey Zigachev 	}
665b843c749SSergey Zigachev 
666b843c749SSergey Zigachev 	/* no dig encoder assigned */
667b843c749SSergey Zigachev 	if (dig->dig_encoder == -1)
668b843c749SSergey Zigachev 		return;
669b843c749SSergey Zigachev 
670b843c749SSergey Zigachev 	memset(&args, 0, sizeof(args));
671b843c749SSergey Zigachev 
672b843c749SSergey Zigachev 	if (!amdgpu_atom_parse_cmd_header(adev->mode_info.atom_context, index, &frev, &crev))
673b843c749SSergey Zigachev 		return;
674b843c749SSergey Zigachev 
675b843c749SSergey Zigachev 	switch (frev) {
676b843c749SSergey Zigachev 	case 1:
677b843c749SSergey Zigachev 		switch (crev) {
678b843c749SSergey Zigachev 		case 1:
679b843c749SSergey Zigachev 			args.v1.ucAction = action;
680b843c749SSergey Zigachev 			args.v1.usPixelClock = cpu_to_le16(amdgpu_encoder->pixel_clock / 10);
681b843c749SSergey Zigachev 			if (action == ATOM_ENCODER_CMD_SETUP_PANEL_MODE)
682b843c749SSergey Zigachev 				args.v3.ucPanelMode = panel_mode;
683b843c749SSergey Zigachev 			else
684b843c749SSergey Zigachev 				args.v1.ucEncoderMode = amdgpu_atombios_encoder_get_encoder_mode(encoder);
685b843c749SSergey Zigachev 
686b843c749SSergey Zigachev 			if (ENCODER_MODE_IS_DP(args.v1.ucEncoderMode))
687b843c749SSergey Zigachev 				args.v1.ucLaneNum = dp_lane_count;
688b843c749SSergey Zigachev 			else if (amdgpu_dig_monitor_is_duallink(encoder, amdgpu_encoder->pixel_clock))
689b843c749SSergey Zigachev 				args.v1.ucLaneNum = 8;
690b843c749SSergey Zigachev 			else
691b843c749SSergey Zigachev 				args.v1.ucLaneNum = 4;
692b843c749SSergey Zigachev 
693b843c749SSergey Zigachev 			if (ENCODER_MODE_IS_DP(args.v1.ucEncoderMode) && (dp_clock == 270000))
694b843c749SSergey Zigachev 				args.v1.ucConfig |= ATOM_ENCODER_CONFIG_DPLINKRATE_2_70GHZ;
695b843c749SSergey Zigachev 			switch (amdgpu_encoder->encoder_id) {
696b843c749SSergey Zigachev 			case ENCODER_OBJECT_ID_INTERNAL_UNIPHY:
697b843c749SSergey Zigachev 				args.v1.ucConfig = ATOM_ENCODER_CONFIG_V2_TRANSMITTER1;
698b843c749SSergey Zigachev 				break;
699b843c749SSergey Zigachev 			case ENCODER_OBJECT_ID_INTERNAL_UNIPHY1:
700b843c749SSergey Zigachev 			case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_LVTMA:
701b843c749SSergey Zigachev 				args.v1.ucConfig = ATOM_ENCODER_CONFIG_V2_TRANSMITTER2;
702b843c749SSergey Zigachev 				break;
703b843c749SSergey Zigachev 			case ENCODER_OBJECT_ID_INTERNAL_UNIPHY2:
704b843c749SSergey Zigachev 				args.v1.ucConfig = ATOM_ENCODER_CONFIG_V2_TRANSMITTER3;
705b843c749SSergey Zigachev 				break;
706b843c749SSergey Zigachev 			}
707b843c749SSergey Zigachev 			if (dig->linkb)
708b843c749SSergey Zigachev 				args.v1.ucConfig |= ATOM_ENCODER_CONFIG_LINKB;
709b843c749SSergey Zigachev 			else
710b843c749SSergey Zigachev 				args.v1.ucConfig |= ATOM_ENCODER_CONFIG_LINKA;
711b843c749SSergey Zigachev 			break;
712b843c749SSergey Zigachev 		case 2:
713b843c749SSergey Zigachev 		case 3:
714b843c749SSergey Zigachev 			args.v3.ucAction = action;
715b843c749SSergey Zigachev 			args.v3.usPixelClock = cpu_to_le16(amdgpu_encoder->pixel_clock / 10);
716b843c749SSergey Zigachev 			if (action == ATOM_ENCODER_CMD_SETUP_PANEL_MODE)
717b843c749SSergey Zigachev 				args.v3.ucPanelMode = panel_mode;
718b843c749SSergey Zigachev 			else
719b843c749SSergey Zigachev 				args.v3.ucEncoderMode = amdgpu_atombios_encoder_get_encoder_mode(encoder);
720b843c749SSergey Zigachev 
721b843c749SSergey Zigachev 			if (ENCODER_MODE_IS_DP(args.v3.ucEncoderMode))
722b843c749SSergey Zigachev 				args.v3.ucLaneNum = dp_lane_count;
723b843c749SSergey Zigachev 			else if (amdgpu_dig_monitor_is_duallink(encoder, amdgpu_encoder->pixel_clock))
724b843c749SSergey Zigachev 				args.v3.ucLaneNum = 8;
725b843c749SSergey Zigachev 			else
726b843c749SSergey Zigachev 				args.v3.ucLaneNum = 4;
727b843c749SSergey Zigachev 
728b843c749SSergey Zigachev 			if (ENCODER_MODE_IS_DP(args.v3.ucEncoderMode) && (dp_clock == 270000))
729b843c749SSergey Zigachev 				args.v1.ucConfig |= ATOM_ENCODER_CONFIG_V3_DPLINKRATE_2_70GHZ;
730b843c749SSergey Zigachev 			args.v3.acConfig.ucDigSel = dig->dig_encoder;
731b843c749SSergey Zigachev 			args.v3.ucBitPerColor = amdgpu_atombios_encoder_get_bpc(encoder);
732b843c749SSergey Zigachev 			break;
733b843c749SSergey Zigachev 		case 4:
734b843c749SSergey Zigachev 			args.v4.ucAction = action;
735b843c749SSergey Zigachev 			args.v4.usPixelClock = cpu_to_le16(amdgpu_encoder->pixel_clock / 10);
736b843c749SSergey Zigachev 			if (action == ATOM_ENCODER_CMD_SETUP_PANEL_MODE)
737b843c749SSergey Zigachev 				args.v4.ucPanelMode = panel_mode;
738b843c749SSergey Zigachev 			else
739b843c749SSergey Zigachev 				args.v4.ucEncoderMode = amdgpu_atombios_encoder_get_encoder_mode(encoder);
740b843c749SSergey Zigachev 
741b843c749SSergey Zigachev 			if (ENCODER_MODE_IS_DP(args.v4.ucEncoderMode))
742b843c749SSergey Zigachev 				args.v4.ucLaneNum = dp_lane_count;
743b843c749SSergey Zigachev 			else if (amdgpu_dig_monitor_is_duallink(encoder, amdgpu_encoder->pixel_clock))
744b843c749SSergey Zigachev 				args.v4.ucLaneNum = 8;
745b843c749SSergey Zigachev 			else
746b843c749SSergey Zigachev 				args.v4.ucLaneNum = 4;
747b843c749SSergey Zigachev 
748b843c749SSergey Zigachev 			if (ENCODER_MODE_IS_DP(args.v4.ucEncoderMode)) {
749b843c749SSergey Zigachev 				if (dp_clock == 540000)
750b843c749SSergey Zigachev 					args.v1.ucConfig |= ATOM_ENCODER_CONFIG_V4_DPLINKRATE_5_40GHZ;
751b843c749SSergey Zigachev 				else if (dp_clock == 324000)
752b843c749SSergey Zigachev 					args.v1.ucConfig |= ATOM_ENCODER_CONFIG_V4_DPLINKRATE_3_24GHZ;
753b843c749SSergey Zigachev 				else if (dp_clock == 270000)
754b843c749SSergey Zigachev 					args.v1.ucConfig |= ATOM_ENCODER_CONFIG_V4_DPLINKRATE_2_70GHZ;
755b843c749SSergey Zigachev 				else
756b843c749SSergey Zigachev 					args.v1.ucConfig |= ATOM_ENCODER_CONFIG_V4_DPLINKRATE_1_62GHZ;
757b843c749SSergey Zigachev 			}
758b843c749SSergey Zigachev 			args.v4.acConfig.ucDigSel = dig->dig_encoder;
759b843c749SSergey Zigachev 			args.v4.ucBitPerColor = amdgpu_atombios_encoder_get_bpc(encoder);
760b843c749SSergey Zigachev 			if (hpd_id == AMDGPU_HPD_NONE)
761b843c749SSergey Zigachev 				args.v4.ucHPD_ID = 0;
762b843c749SSergey Zigachev 			else
763b843c749SSergey Zigachev 				args.v4.ucHPD_ID = hpd_id + 1;
764b843c749SSergey Zigachev 			break;
765b843c749SSergey Zigachev 		case 5:
766b843c749SSergey Zigachev 			switch (action) {
767b843c749SSergey Zigachev 			case ATOM_ENCODER_CMD_SETUP_PANEL_MODE:
768b843c749SSergey Zigachev 				args.v5.asDPPanelModeParam.ucAction = action;
769b843c749SSergey Zigachev 				args.v5.asDPPanelModeParam.ucPanelMode = panel_mode;
770b843c749SSergey Zigachev 				args.v5.asDPPanelModeParam.ucDigId = dig->dig_encoder;
771b843c749SSergey Zigachev 				break;
772b843c749SSergey Zigachev 			case ATOM_ENCODER_CMD_STREAM_SETUP:
773b843c749SSergey Zigachev 				args.v5.asStreamParam.ucAction = action;
774b843c749SSergey Zigachev 				args.v5.asStreamParam.ucDigId = dig->dig_encoder;
775b843c749SSergey Zigachev 				args.v5.asStreamParam.ucDigMode =
776b843c749SSergey Zigachev 					amdgpu_atombios_encoder_get_encoder_mode(encoder);
777b843c749SSergey Zigachev 				if (ENCODER_MODE_IS_DP(args.v5.asStreamParam.ucDigMode))
778b843c749SSergey Zigachev 					args.v5.asStreamParam.ucLaneNum = dp_lane_count;
779b843c749SSergey Zigachev 				else if (amdgpu_dig_monitor_is_duallink(encoder,
780b843c749SSergey Zigachev 									amdgpu_encoder->pixel_clock))
781b843c749SSergey Zigachev 					args.v5.asStreamParam.ucLaneNum = 8;
782b843c749SSergey Zigachev 				else
783b843c749SSergey Zigachev 					args.v5.asStreamParam.ucLaneNum = 4;
784b843c749SSergey Zigachev 				args.v5.asStreamParam.ulPixelClock =
785b843c749SSergey Zigachev 					cpu_to_le32(amdgpu_encoder->pixel_clock / 10);
786b843c749SSergey Zigachev 				args.v5.asStreamParam.ucBitPerColor =
787b843c749SSergey Zigachev 					amdgpu_atombios_encoder_get_bpc(encoder);
788b843c749SSergey Zigachev 				args.v5.asStreamParam.ucLinkRateIn270Mhz = dp_clock / 27000;
789b843c749SSergey Zigachev 				break;
790b843c749SSergey Zigachev 			case ATOM_ENCODER_CMD_DP_LINK_TRAINING_START:
791b843c749SSergey Zigachev 			case ATOM_ENCODER_CMD_DP_LINK_TRAINING_PATTERN1:
792b843c749SSergey Zigachev 			case ATOM_ENCODER_CMD_DP_LINK_TRAINING_PATTERN2:
793b843c749SSergey Zigachev 			case ATOM_ENCODER_CMD_DP_LINK_TRAINING_PATTERN3:
794b843c749SSergey Zigachev 			case ATOM_ENCODER_CMD_DP_LINK_TRAINING_PATTERN4:
795b843c749SSergey Zigachev 			case ATOM_ENCODER_CMD_DP_LINK_TRAINING_COMPLETE:
796b843c749SSergey Zigachev 			case ATOM_ENCODER_CMD_DP_VIDEO_OFF:
797b843c749SSergey Zigachev 			case ATOM_ENCODER_CMD_DP_VIDEO_ON:
798b843c749SSergey Zigachev 				args.v5.asCmdParam.ucAction = action;
799b843c749SSergey Zigachev 				args.v5.asCmdParam.ucDigId = dig->dig_encoder;
800b843c749SSergey Zigachev 				break;
801b843c749SSergey Zigachev 			default:
802b843c749SSergey Zigachev 				DRM_ERROR("Unsupported action 0x%x\n", action);
803b843c749SSergey Zigachev 				break;
804b843c749SSergey Zigachev 			}
805b843c749SSergey Zigachev 			break;
806b843c749SSergey Zigachev 		default:
807b843c749SSergey Zigachev 			DRM_ERROR("Unknown table version %d, %d\n", frev, crev);
808b843c749SSergey Zigachev 			break;
809b843c749SSergey Zigachev 		}
810b843c749SSergey Zigachev 		break;
811b843c749SSergey Zigachev 	default:
812b843c749SSergey Zigachev 		DRM_ERROR("Unknown table version %d, %d\n", frev, crev);
813b843c749SSergey Zigachev 		break;
814b843c749SSergey Zigachev 	}
815b843c749SSergey Zigachev 
816b843c749SSergey Zigachev 	amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
817b843c749SSergey Zigachev 
818b843c749SSergey Zigachev }
819b843c749SSergey Zigachev 
820b843c749SSergey Zigachev union dig_transmitter_control {
821b843c749SSergey Zigachev 	DIG_TRANSMITTER_CONTROL_PS_ALLOCATION v1;
822b843c749SSergey Zigachev 	DIG_TRANSMITTER_CONTROL_PARAMETERS_V2 v2;
823b843c749SSergey Zigachev 	DIG_TRANSMITTER_CONTROL_PARAMETERS_V3 v3;
824b843c749SSergey Zigachev 	DIG_TRANSMITTER_CONTROL_PARAMETERS_V4 v4;
825b843c749SSergey Zigachev 	DIG_TRANSMITTER_CONTROL_PARAMETERS_V1_5 v5;
826b843c749SSergey Zigachev 	DIG_TRANSMITTER_CONTROL_PARAMETERS_V1_6 v6;
827b843c749SSergey Zigachev };
828b843c749SSergey Zigachev 
829b843c749SSergey Zigachev void
amdgpu_atombios_encoder_setup_dig_transmitter(struct drm_encoder * encoder,int action,uint8_t lane_num,uint8_t lane_set)830b843c749SSergey Zigachev amdgpu_atombios_encoder_setup_dig_transmitter(struct drm_encoder *encoder, int action,
831b843c749SSergey Zigachev 					      uint8_t lane_num, uint8_t lane_set)
832b843c749SSergey Zigachev {
833b843c749SSergey Zigachev 	struct drm_device *dev = encoder->dev;
834b843c749SSergey Zigachev 	struct amdgpu_device *adev = dev->dev_private;
835b843c749SSergey Zigachev 	struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
836b843c749SSergey Zigachev 	struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
837b843c749SSergey Zigachev 	struct drm_connector *connector;
838b843c749SSergey Zigachev 	union dig_transmitter_control args;
839b843c749SSergey Zigachev 	int index = 0;
840b843c749SSergey Zigachev 	uint8_t frev, crev;
841b843c749SSergey Zigachev 	bool is_dp = false;
842b843c749SSergey Zigachev 	int pll_id = 0;
843b843c749SSergey Zigachev 	int dp_clock = 0;
844b843c749SSergey Zigachev 	int dp_lane_count = 0;
845b843c749SSergey Zigachev 	int connector_object_id = 0;
846b843c749SSergey Zigachev 	int igp_lane_info = 0;
847b843c749SSergey Zigachev 	int dig_encoder = dig->dig_encoder;
848b843c749SSergey Zigachev 	int hpd_id = AMDGPU_HPD_NONE;
849b843c749SSergey Zigachev 
850b843c749SSergey Zigachev 	if (action == ATOM_TRANSMITTER_ACTION_INIT) {
851b843c749SSergey Zigachev 		connector = amdgpu_get_connector_for_encoder_init(encoder);
852b843c749SSergey Zigachev 		/* just needed to avoid bailing in the encoder check.  the encoder
853b843c749SSergey Zigachev 		 * isn't used for init
854b843c749SSergey Zigachev 		 */
855b843c749SSergey Zigachev 		dig_encoder = 0;
856b843c749SSergey Zigachev 	} else
857b843c749SSergey Zigachev 		connector = amdgpu_get_connector_for_encoder(encoder);
858b843c749SSergey Zigachev 
859b843c749SSergey Zigachev 	if (connector) {
860b843c749SSergey Zigachev 		struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
861b843c749SSergey Zigachev 		struct amdgpu_connector_atom_dig *dig_connector =
862b843c749SSergey Zigachev 			amdgpu_connector->con_priv;
863b843c749SSergey Zigachev 
864b843c749SSergey Zigachev 		hpd_id = amdgpu_connector->hpd.hpd;
865b843c749SSergey Zigachev 		dp_clock = dig_connector->dp_clock;
866b843c749SSergey Zigachev 		dp_lane_count = dig_connector->dp_lane_count;
867b843c749SSergey Zigachev 		connector_object_id =
868b843c749SSergey Zigachev 			(amdgpu_connector->connector_object_id & OBJECT_ID_MASK) >> OBJECT_ID_SHIFT;
869b843c749SSergey Zigachev 	}
870b843c749SSergey Zigachev 
871b843c749SSergey Zigachev 	if (encoder->crtc) {
872b843c749SSergey Zigachev 		struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(encoder->crtc);
873b843c749SSergey Zigachev 		pll_id = amdgpu_crtc->pll_id;
874b843c749SSergey Zigachev 	}
875b843c749SSergey Zigachev 
876b843c749SSergey Zigachev 	/* no dig encoder assigned */
877b843c749SSergey Zigachev 	if (dig_encoder == -1)
878b843c749SSergey Zigachev 		return;
879b843c749SSergey Zigachev 
880b843c749SSergey Zigachev 	if (ENCODER_MODE_IS_DP(amdgpu_atombios_encoder_get_encoder_mode(encoder)))
881b843c749SSergey Zigachev 		is_dp = true;
882b843c749SSergey Zigachev 
883b843c749SSergey Zigachev 	memset(&args, 0, sizeof(args));
884b843c749SSergey Zigachev 
885b843c749SSergey Zigachev 	switch (amdgpu_encoder->encoder_id) {
886b843c749SSergey Zigachev 	case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DVO1:
887b843c749SSergey Zigachev 		index = GetIndexIntoMasterTable(COMMAND, DVOOutputControl);
888b843c749SSergey Zigachev 		break;
889b843c749SSergey Zigachev 	case ENCODER_OBJECT_ID_INTERNAL_UNIPHY:
890b843c749SSergey Zigachev 	case ENCODER_OBJECT_ID_INTERNAL_UNIPHY1:
891b843c749SSergey Zigachev 	case ENCODER_OBJECT_ID_INTERNAL_UNIPHY2:
892b843c749SSergey Zigachev 	case ENCODER_OBJECT_ID_INTERNAL_UNIPHY3:
893b843c749SSergey Zigachev 		index = GetIndexIntoMasterTable(COMMAND, UNIPHYTransmitterControl);
894b843c749SSergey Zigachev 		break;
895b843c749SSergey Zigachev 	case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_LVTMA:
896b843c749SSergey Zigachev 		index = GetIndexIntoMasterTable(COMMAND, LVTMATransmitterControl);
897b843c749SSergey Zigachev 		break;
898b843c749SSergey Zigachev 	}
899b843c749SSergey Zigachev 
900b843c749SSergey Zigachev 	if (!amdgpu_atom_parse_cmd_header(adev->mode_info.atom_context, index, &frev, &crev))
901b843c749SSergey Zigachev 		return;
902b843c749SSergey Zigachev 
903b843c749SSergey Zigachev 	switch (frev) {
904b843c749SSergey Zigachev 	case 1:
905b843c749SSergey Zigachev 		switch (crev) {
906b843c749SSergey Zigachev 		case 1:
907b843c749SSergey Zigachev 			args.v1.ucAction = action;
908b843c749SSergey Zigachev 			if (action == ATOM_TRANSMITTER_ACTION_INIT) {
909b843c749SSergey Zigachev 				args.v1.usInitInfo = cpu_to_le16(connector_object_id);
910b843c749SSergey Zigachev 			} else if (action == ATOM_TRANSMITTER_ACTION_SETUP_VSEMPH) {
911b843c749SSergey Zigachev 				args.v1.asMode.ucLaneSel = lane_num;
912b843c749SSergey Zigachev 				args.v1.asMode.ucLaneSet = lane_set;
913b843c749SSergey Zigachev 			} else {
914b843c749SSergey Zigachev 				if (is_dp)
915b843c749SSergey Zigachev 					args.v1.usPixelClock = cpu_to_le16(dp_clock / 10);
916b843c749SSergey Zigachev 				else if (amdgpu_dig_monitor_is_duallink(encoder, amdgpu_encoder->pixel_clock))
917b843c749SSergey Zigachev 					args.v1.usPixelClock = cpu_to_le16((amdgpu_encoder->pixel_clock / 2) / 10);
918b843c749SSergey Zigachev 				else
919b843c749SSergey Zigachev 					args.v1.usPixelClock = cpu_to_le16(amdgpu_encoder->pixel_clock / 10);
920b843c749SSergey Zigachev 			}
921b843c749SSergey Zigachev 
922b843c749SSergey Zigachev 			args.v1.ucConfig = ATOM_TRANSMITTER_CONFIG_CLKSRC_PPLL;
923b843c749SSergey Zigachev 
924b843c749SSergey Zigachev 			if (dig_encoder)
925b843c749SSergey Zigachev 				args.v1.ucConfig |= ATOM_TRANSMITTER_CONFIG_DIG2_ENCODER;
926b843c749SSergey Zigachev 			else
927b843c749SSergey Zigachev 				args.v1.ucConfig |= ATOM_TRANSMITTER_CONFIG_DIG1_ENCODER;
928b843c749SSergey Zigachev 
929b843c749SSergey Zigachev 			if ((adev->flags & AMD_IS_APU) &&
930b843c749SSergey Zigachev 			    (amdgpu_encoder->encoder_id == ENCODER_OBJECT_ID_INTERNAL_UNIPHY)) {
931b843c749SSergey Zigachev 				if (is_dp ||
932b843c749SSergey Zigachev 				    !amdgpu_dig_monitor_is_duallink(encoder, amdgpu_encoder->pixel_clock)) {
933b843c749SSergey Zigachev 					if (igp_lane_info & 0x1)
934b843c749SSergey Zigachev 						args.v1.ucConfig |= ATOM_TRANSMITTER_CONFIG_LANE_0_3;
935b843c749SSergey Zigachev 					else if (igp_lane_info & 0x2)
936b843c749SSergey Zigachev 						args.v1.ucConfig |= ATOM_TRANSMITTER_CONFIG_LANE_4_7;
937b843c749SSergey Zigachev 					else if (igp_lane_info & 0x4)
938b843c749SSergey Zigachev 						args.v1.ucConfig |= ATOM_TRANSMITTER_CONFIG_LANE_8_11;
939b843c749SSergey Zigachev 					else if (igp_lane_info & 0x8)
940b843c749SSergey Zigachev 						args.v1.ucConfig |= ATOM_TRANSMITTER_CONFIG_LANE_12_15;
941b843c749SSergey Zigachev 				} else {
942b843c749SSergey Zigachev 					if (igp_lane_info & 0x3)
943b843c749SSergey Zigachev 						args.v1.ucConfig |= ATOM_TRANSMITTER_CONFIG_LANE_0_7;
944b843c749SSergey Zigachev 					else if (igp_lane_info & 0xc)
945b843c749SSergey Zigachev 						args.v1.ucConfig |= ATOM_TRANSMITTER_CONFIG_LANE_8_15;
946b843c749SSergey Zigachev 				}
947b843c749SSergey Zigachev 			}
948b843c749SSergey Zigachev 
949b843c749SSergey Zigachev 			if (dig->linkb)
950b843c749SSergey Zigachev 				args.v1.ucConfig |= ATOM_TRANSMITTER_CONFIG_LINKB;
951b843c749SSergey Zigachev 			else
952b843c749SSergey Zigachev 				args.v1.ucConfig |= ATOM_TRANSMITTER_CONFIG_LINKA;
953b843c749SSergey Zigachev 
954b843c749SSergey Zigachev 			if (is_dp)
955b843c749SSergey Zigachev 				args.v1.ucConfig |= ATOM_TRANSMITTER_CONFIG_COHERENT;
956b843c749SSergey Zigachev 			else if (amdgpu_encoder->devices & (ATOM_DEVICE_DFP_SUPPORT)) {
957b843c749SSergey Zigachev 				if (dig->coherent_mode)
958b843c749SSergey Zigachev 					args.v1.ucConfig |= ATOM_TRANSMITTER_CONFIG_COHERENT;
959b843c749SSergey Zigachev 				if (amdgpu_dig_monitor_is_duallink(encoder, amdgpu_encoder->pixel_clock))
960b843c749SSergey Zigachev 					args.v1.ucConfig |= ATOM_TRANSMITTER_CONFIG_8LANE_LINK;
961b843c749SSergey Zigachev 			}
962b843c749SSergey Zigachev 			break;
963b843c749SSergey Zigachev 		case 2:
964b843c749SSergey Zigachev 			args.v2.ucAction = action;
965b843c749SSergey Zigachev 			if (action == ATOM_TRANSMITTER_ACTION_INIT) {
966b843c749SSergey Zigachev 				args.v2.usInitInfo = cpu_to_le16(connector_object_id);
967b843c749SSergey Zigachev 			} else if (action == ATOM_TRANSMITTER_ACTION_SETUP_VSEMPH) {
968b843c749SSergey Zigachev 				args.v2.asMode.ucLaneSel = lane_num;
969b843c749SSergey Zigachev 				args.v2.asMode.ucLaneSet = lane_set;
970b843c749SSergey Zigachev 			} else {
971b843c749SSergey Zigachev 				if (is_dp)
972b843c749SSergey Zigachev 					args.v2.usPixelClock = cpu_to_le16(dp_clock / 10);
973b843c749SSergey Zigachev 				else if (amdgpu_dig_monitor_is_duallink(encoder, amdgpu_encoder->pixel_clock))
974b843c749SSergey Zigachev 					args.v2.usPixelClock = cpu_to_le16((amdgpu_encoder->pixel_clock / 2) / 10);
975b843c749SSergey Zigachev 				else
976b843c749SSergey Zigachev 					args.v2.usPixelClock = cpu_to_le16(amdgpu_encoder->pixel_clock / 10);
977b843c749SSergey Zigachev 			}
978b843c749SSergey Zigachev 
979b843c749SSergey Zigachev 			args.v2.acConfig.ucEncoderSel = dig_encoder;
980b843c749SSergey Zigachev 			if (dig->linkb)
981b843c749SSergey Zigachev 				args.v2.acConfig.ucLinkSel = 1;
982b843c749SSergey Zigachev 
983b843c749SSergey Zigachev 			switch (amdgpu_encoder->encoder_id) {
984b843c749SSergey Zigachev 			case ENCODER_OBJECT_ID_INTERNAL_UNIPHY:
985b843c749SSergey Zigachev 				args.v2.acConfig.ucTransmitterSel = 0;
986b843c749SSergey Zigachev 				break;
987b843c749SSergey Zigachev 			case ENCODER_OBJECT_ID_INTERNAL_UNIPHY1:
988b843c749SSergey Zigachev 				args.v2.acConfig.ucTransmitterSel = 1;
989b843c749SSergey Zigachev 				break;
990b843c749SSergey Zigachev 			case ENCODER_OBJECT_ID_INTERNAL_UNIPHY2:
991b843c749SSergey Zigachev 				args.v2.acConfig.ucTransmitterSel = 2;
992b843c749SSergey Zigachev 				break;
993b843c749SSergey Zigachev 			}
994b843c749SSergey Zigachev 
995b843c749SSergey Zigachev 			if (is_dp) {
996b843c749SSergey Zigachev 				args.v2.acConfig.fCoherentMode = 1;
997b843c749SSergey Zigachev 				args.v2.acConfig.fDPConnector = 1;
998b843c749SSergey Zigachev 			} else if (amdgpu_encoder->devices & (ATOM_DEVICE_DFP_SUPPORT)) {
999b843c749SSergey Zigachev 				if (dig->coherent_mode)
1000b843c749SSergey Zigachev 					args.v2.acConfig.fCoherentMode = 1;
1001b843c749SSergey Zigachev 				if (amdgpu_dig_monitor_is_duallink(encoder, amdgpu_encoder->pixel_clock))
1002b843c749SSergey Zigachev 					args.v2.acConfig.fDualLinkConnector = 1;
1003b843c749SSergey Zigachev 			}
1004b843c749SSergey Zigachev 			break;
1005b843c749SSergey Zigachev 		case 3:
1006b843c749SSergey Zigachev 			args.v3.ucAction = action;
1007b843c749SSergey Zigachev 			if (action == ATOM_TRANSMITTER_ACTION_INIT) {
1008b843c749SSergey Zigachev 				args.v3.usInitInfo = cpu_to_le16(connector_object_id);
1009b843c749SSergey Zigachev 			} else if (action == ATOM_TRANSMITTER_ACTION_SETUP_VSEMPH) {
1010b843c749SSergey Zigachev 				args.v3.asMode.ucLaneSel = lane_num;
1011b843c749SSergey Zigachev 				args.v3.asMode.ucLaneSet = lane_set;
1012b843c749SSergey Zigachev 			} else {
1013b843c749SSergey Zigachev 				if (is_dp)
1014b843c749SSergey Zigachev 					args.v3.usPixelClock = cpu_to_le16(dp_clock / 10);
1015b843c749SSergey Zigachev 				else if (amdgpu_dig_monitor_is_duallink(encoder, amdgpu_encoder->pixel_clock))
1016b843c749SSergey Zigachev 					args.v3.usPixelClock = cpu_to_le16((amdgpu_encoder->pixel_clock / 2) / 10);
1017b843c749SSergey Zigachev 				else
1018b843c749SSergey Zigachev 					args.v3.usPixelClock = cpu_to_le16(amdgpu_encoder->pixel_clock / 10);
1019b843c749SSergey Zigachev 			}
1020b843c749SSergey Zigachev 
1021b843c749SSergey Zigachev 			if (is_dp)
1022b843c749SSergey Zigachev 				args.v3.ucLaneNum = dp_lane_count;
1023b843c749SSergey Zigachev 			else if (amdgpu_dig_monitor_is_duallink(encoder, amdgpu_encoder->pixel_clock))
1024b843c749SSergey Zigachev 				args.v3.ucLaneNum = 8;
1025b843c749SSergey Zigachev 			else
1026b843c749SSergey Zigachev 				args.v3.ucLaneNum = 4;
1027b843c749SSergey Zigachev 
1028b843c749SSergey Zigachev 			if (dig->linkb)
1029b843c749SSergey Zigachev 				args.v3.acConfig.ucLinkSel = 1;
1030b843c749SSergey Zigachev 			if (dig_encoder & 1)
1031b843c749SSergey Zigachev 				args.v3.acConfig.ucEncoderSel = 1;
1032b843c749SSergey Zigachev 
1033b843c749SSergey Zigachev 			/* Select the PLL for the PHY
1034b843c749SSergey Zigachev 			 * DP PHY should be clocked from external src if there is
1035b843c749SSergey Zigachev 			 * one.
1036b843c749SSergey Zigachev 			 */
1037b843c749SSergey Zigachev 			/* On DCE4, if there is an external clock, it generates the DP ref clock */
1038b843c749SSergey Zigachev 			if (is_dp && adev->clock.dp_extclk)
1039b843c749SSergey Zigachev 				args.v3.acConfig.ucRefClkSource = 2; /* external src */
1040b843c749SSergey Zigachev 			else
1041b843c749SSergey Zigachev 				args.v3.acConfig.ucRefClkSource = pll_id;
1042b843c749SSergey Zigachev 
1043b843c749SSergey Zigachev 			switch (amdgpu_encoder->encoder_id) {
1044b843c749SSergey Zigachev 			case ENCODER_OBJECT_ID_INTERNAL_UNIPHY:
1045b843c749SSergey Zigachev 				args.v3.acConfig.ucTransmitterSel = 0;
1046b843c749SSergey Zigachev 				break;
1047b843c749SSergey Zigachev 			case ENCODER_OBJECT_ID_INTERNAL_UNIPHY1:
1048b843c749SSergey Zigachev 				args.v3.acConfig.ucTransmitterSel = 1;
1049b843c749SSergey Zigachev 				break;
1050b843c749SSergey Zigachev 			case ENCODER_OBJECT_ID_INTERNAL_UNIPHY2:
1051b843c749SSergey Zigachev 				args.v3.acConfig.ucTransmitterSel = 2;
1052b843c749SSergey Zigachev 				break;
1053b843c749SSergey Zigachev 			}
1054b843c749SSergey Zigachev 
1055b843c749SSergey Zigachev 			if (is_dp)
1056b843c749SSergey Zigachev 				args.v3.acConfig.fCoherentMode = 1; /* DP requires coherent */
1057b843c749SSergey Zigachev 			else if (amdgpu_encoder->devices & (ATOM_DEVICE_DFP_SUPPORT)) {
1058b843c749SSergey Zigachev 				if (dig->coherent_mode)
1059b843c749SSergey Zigachev 					args.v3.acConfig.fCoherentMode = 1;
1060b843c749SSergey Zigachev 				if (amdgpu_dig_monitor_is_duallink(encoder, amdgpu_encoder->pixel_clock))
1061b843c749SSergey Zigachev 					args.v3.acConfig.fDualLinkConnector = 1;
1062b843c749SSergey Zigachev 			}
1063b843c749SSergey Zigachev 			break;
1064b843c749SSergey Zigachev 		case 4:
1065b843c749SSergey Zigachev 			args.v4.ucAction = action;
1066b843c749SSergey Zigachev 			if (action == ATOM_TRANSMITTER_ACTION_INIT) {
1067b843c749SSergey Zigachev 				args.v4.usInitInfo = cpu_to_le16(connector_object_id);
1068b843c749SSergey Zigachev 			} else if (action == ATOM_TRANSMITTER_ACTION_SETUP_VSEMPH) {
1069b843c749SSergey Zigachev 				args.v4.asMode.ucLaneSel = lane_num;
1070b843c749SSergey Zigachev 				args.v4.asMode.ucLaneSet = lane_set;
1071b843c749SSergey Zigachev 			} else {
1072b843c749SSergey Zigachev 				if (is_dp)
1073b843c749SSergey Zigachev 					args.v4.usPixelClock = cpu_to_le16(dp_clock / 10);
1074b843c749SSergey Zigachev 				else if (amdgpu_dig_monitor_is_duallink(encoder, amdgpu_encoder->pixel_clock))
1075b843c749SSergey Zigachev 					args.v4.usPixelClock = cpu_to_le16((amdgpu_encoder->pixel_clock / 2) / 10);
1076b843c749SSergey Zigachev 				else
1077b843c749SSergey Zigachev 					args.v4.usPixelClock = cpu_to_le16(amdgpu_encoder->pixel_clock / 10);
1078b843c749SSergey Zigachev 			}
1079b843c749SSergey Zigachev 
1080b843c749SSergey Zigachev 			if (is_dp)
1081b843c749SSergey Zigachev 				args.v4.ucLaneNum = dp_lane_count;
1082b843c749SSergey Zigachev 			else if (amdgpu_dig_monitor_is_duallink(encoder, amdgpu_encoder->pixel_clock))
1083b843c749SSergey Zigachev 				args.v4.ucLaneNum = 8;
1084b843c749SSergey Zigachev 			else
1085b843c749SSergey Zigachev 				args.v4.ucLaneNum = 4;
1086b843c749SSergey Zigachev 
1087b843c749SSergey Zigachev 			if (dig->linkb)
1088b843c749SSergey Zigachev 				args.v4.acConfig.ucLinkSel = 1;
1089b843c749SSergey Zigachev 			if (dig_encoder & 1)
1090b843c749SSergey Zigachev 				args.v4.acConfig.ucEncoderSel = 1;
1091b843c749SSergey Zigachev 
1092b843c749SSergey Zigachev 			/* Select the PLL for the PHY
1093b843c749SSergey Zigachev 			 * DP PHY should be clocked from external src if there is
1094b843c749SSergey Zigachev 			 * one.
1095b843c749SSergey Zigachev 			 */
1096b843c749SSergey Zigachev 			/* On DCE5 DCPLL usually generates the DP ref clock */
1097b843c749SSergey Zigachev 			if (is_dp) {
1098b843c749SSergey Zigachev 				if (adev->clock.dp_extclk)
1099b843c749SSergey Zigachev 					args.v4.acConfig.ucRefClkSource = ENCODER_REFCLK_SRC_EXTCLK;
1100b843c749SSergey Zigachev 				else
1101b843c749SSergey Zigachev 					args.v4.acConfig.ucRefClkSource = ENCODER_REFCLK_SRC_DCPLL;
1102b843c749SSergey Zigachev 			} else
1103b843c749SSergey Zigachev 				args.v4.acConfig.ucRefClkSource = pll_id;
1104b843c749SSergey Zigachev 
1105b843c749SSergey Zigachev 			switch (amdgpu_encoder->encoder_id) {
1106b843c749SSergey Zigachev 			case ENCODER_OBJECT_ID_INTERNAL_UNIPHY:
1107b843c749SSergey Zigachev 				args.v4.acConfig.ucTransmitterSel = 0;
1108b843c749SSergey Zigachev 				break;
1109b843c749SSergey Zigachev 			case ENCODER_OBJECT_ID_INTERNAL_UNIPHY1:
1110b843c749SSergey Zigachev 				args.v4.acConfig.ucTransmitterSel = 1;
1111b843c749SSergey Zigachev 				break;
1112b843c749SSergey Zigachev 			case ENCODER_OBJECT_ID_INTERNAL_UNIPHY2:
1113b843c749SSergey Zigachev 				args.v4.acConfig.ucTransmitterSel = 2;
1114b843c749SSergey Zigachev 				break;
1115b843c749SSergey Zigachev 			}
1116b843c749SSergey Zigachev 
1117b843c749SSergey Zigachev 			if (is_dp)
1118b843c749SSergey Zigachev 				args.v4.acConfig.fCoherentMode = 1; /* DP requires coherent */
1119b843c749SSergey Zigachev 			else if (amdgpu_encoder->devices & (ATOM_DEVICE_DFP_SUPPORT)) {
1120b843c749SSergey Zigachev 				if (dig->coherent_mode)
1121b843c749SSergey Zigachev 					args.v4.acConfig.fCoherentMode = 1;
1122b843c749SSergey Zigachev 				if (amdgpu_dig_monitor_is_duallink(encoder, amdgpu_encoder->pixel_clock))
1123b843c749SSergey Zigachev 					args.v4.acConfig.fDualLinkConnector = 1;
1124b843c749SSergey Zigachev 			}
1125b843c749SSergey Zigachev 			break;
1126b843c749SSergey Zigachev 		case 5:
1127b843c749SSergey Zigachev 			args.v5.ucAction = action;
1128b843c749SSergey Zigachev 			if (is_dp)
1129b843c749SSergey Zigachev 				args.v5.usSymClock = cpu_to_le16(dp_clock / 10);
1130b843c749SSergey Zigachev 			else
1131b843c749SSergey Zigachev 				args.v5.usSymClock = cpu_to_le16(amdgpu_encoder->pixel_clock / 10);
1132b843c749SSergey Zigachev 
1133b843c749SSergey Zigachev 			switch (amdgpu_encoder->encoder_id) {
1134b843c749SSergey Zigachev 			case ENCODER_OBJECT_ID_INTERNAL_UNIPHY:
1135b843c749SSergey Zigachev 				if (dig->linkb)
1136b843c749SSergey Zigachev 					args.v5.ucPhyId = ATOM_PHY_ID_UNIPHYB;
1137b843c749SSergey Zigachev 				else
1138b843c749SSergey Zigachev 					args.v5.ucPhyId = ATOM_PHY_ID_UNIPHYA;
1139b843c749SSergey Zigachev 				break;
1140b843c749SSergey Zigachev 			case ENCODER_OBJECT_ID_INTERNAL_UNIPHY1:
1141b843c749SSergey Zigachev 				if (dig->linkb)
1142b843c749SSergey Zigachev 					args.v5.ucPhyId = ATOM_PHY_ID_UNIPHYD;
1143b843c749SSergey Zigachev 				else
1144b843c749SSergey Zigachev 					args.v5.ucPhyId = ATOM_PHY_ID_UNIPHYC;
1145b843c749SSergey Zigachev 				break;
1146b843c749SSergey Zigachev 			case ENCODER_OBJECT_ID_INTERNAL_UNIPHY2:
1147b843c749SSergey Zigachev 				if (dig->linkb)
1148b843c749SSergey Zigachev 					args.v5.ucPhyId = ATOM_PHY_ID_UNIPHYF;
1149b843c749SSergey Zigachev 				else
1150b843c749SSergey Zigachev 					args.v5.ucPhyId = ATOM_PHY_ID_UNIPHYE;
1151b843c749SSergey Zigachev 				break;
1152b843c749SSergey Zigachev 			case ENCODER_OBJECT_ID_INTERNAL_UNIPHY3:
1153b843c749SSergey Zigachev 				args.v5.ucPhyId = ATOM_PHY_ID_UNIPHYG;
1154b843c749SSergey Zigachev 				break;
1155b843c749SSergey Zigachev 			}
1156b843c749SSergey Zigachev 			if (is_dp)
1157b843c749SSergey Zigachev 				args.v5.ucLaneNum = dp_lane_count;
1158b843c749SSergey Zigachev 			else if (amdgpu_dig_monitor_is_duallink(encoder, amdgpu_encoder->pixel_clock))
1159b843c749SSergey Zigachev 				args.v5.ucLaneNum = 8;
1160b843c749SSergey Zigachev 			else
1161b843c749SSergey Zigachev 				args.v5.ucLaneNum = 4;
1162b843c749SSergey Zigachev 			args.v5.ucConnObjId = connector_object_id;
1163b843c749SSergey Zigachev 			args.v5.ucDigMode = amdgpu_atombios_encoder_get_encoder_mode(encoder);
1164b843c749SSergey Zigachev 
1165b843c749SSergey Zigachev 			if (is_dp && adev->clock.dp_extclk)
1166b843c749SSergey Zigachev 				args.v5.asConfig.ucPhyClkSrcId = ENCODER_REFCLK_SRC_EXTCLK;
1167b843c749SSergey Zigachev 			else
1168b843c749SSergey Zigachev 				args.v5.asConfig.ucPhyClkSrcId = pll_id;
1169b843c749SSergey Zigachev 
1170b843c749SSergey Zigachev 			if (is_dp)
1171b843c749SSergey Zigachev 				args.v5.asConfig.ucCoherentMode = 1; /* DP requires coherent */
1172b843c749SSergey Zigachev 			else if (amdgpu_encoder->devices & (ATOM_DEVICE_DFP_SUPPORT)) {
1173b843c749SSergey Zigachev 				if (dig->coherent_mode)
1174b843c749SSergey Zigachev 					args.v5.asConfig.ucCoherentMode = 1;
1175b843c749SSergey Zigachev 			}
1176b843c749SSergey Zigachev 			if (hpd_id == AMDGPU_HPD_NONE)
1177b843c749SSergey Zigachev 				args.v5.asConfig.ucHPDSel = 0;
1178b843c749SSergey Zigachev 			else
1179b843c749SSergey Zigachev 				args.v5.asConfig.ucHPDSel = hpd_id + 1;
1180b843c749SSergey Zigachev 			args.v5.ucDigEncoderSel = 1 << dig_encoder;
1181b843c749SSergey Zigachev 			args.v5.ucDPLaneSet = lane_set;
1182b843c749SSergey Zigachev 			break;
1183b843c749SSergey Zigachev 		case 6:
1184b843c749SSergey Zigachev 			args.v6.ucAction = action;
1185b843c749SSergey Zigachev 			if (is_dp)
1186b843c749SSergey Zigachev 				args.v6.ulSymClock = cpu_to_le32(dp_clock / 10);
1187b843c749SSergey Zigachev 			else
1188b843c749SSergey Zigachev 				args.v6.ulSymClock = cpu_to_le32(amdgpu_encoder->pixel_clock / 10);
1189b843c749SSergey Zigachev 
1190b843c749SSergey Zigachev 			switch (amdgpu_encoder->encoder_id) {
1191b843c749SSergey Zigachev 			case ENCODER_OBJECT_ID_INTERNAL_UNIPHY:
1192b843c749SSergey Zigachev 				if (dig->linkb)
1193b843c749SSergey Zigachev 					args.v6.ucPhyId = ATOM_PHY_ID_UNIPHYB;
1194b843c749SSergey Zigachev 				else
1195b843c749SSergey Zigachev 					args.v6.ucPhyId = ATOM_PHY_ID_UNIPHYA;
1196b843c749SSergey Zigachev 				break;
1197b843c749SSergey Zigachev 			case ENCODER_OBJECT_ID_INTERNAL_UNIPHY1:
1198b843c749SSergey Zigachev 				if (dig->linkb)
1199b843c749SSergey Zigachev 					args.v6.ucPhyId = ATOM_PHY_ID_UNIPHYD;
1200b843c749SSergey Zigachev 				else
1201b843c749SSergey Zigachev 					args.v6.ucPhyId = ATOM_PHY_ID_UNIPHYC;
1202b843c749SSergey Zigachev 				break;
1203b843c749SSergey Zigachev 			case ENCODER_OBJECT_ID_INTERNAL_UNIPHY2:
1204b843c749SSergey Zigachev 				if (dig->linkb)
1205b843c749SSergey Zigachev 					args.v6.ucPhyId = ATOM_PHY_ID_UNIPHYF;
1206b843c749SSergey Zigachev 				else
1207b843c749SSergey Zigachev 					args.v6.ucPhyId = ATOM_PHY_ID_UNIPHYE;
1208b843c749SSergey Zigachev 				break;
1209b843c749SSergey Zigachev 			case ENCODER_OBJECT_ID_INTERNAL_UNIPHY3:
1210b843c749SSergey Zigachev 				args.v6.ucPhyId = ATOM_PHY_ID_UNIPHYG;
1211b843c749SSergey Zigachev 				break;
1212b843c749SSergey Zigachev 			}
1213b843c749SSergey Zigachev 			if (is_dp)
1214b843c749SSergey Zigachev 				args.v6.ucLaneNum = dp_lane_count;
1215b843c749SSergey Zigachev 			else if (amdgpu_dig_monitor_is_duallink(encoder, amdgpu_encoder->pixel_clock))
1216b843c749SSergey Zigachev 				args.v6.ucLaneNum = 8;
1217b843c749SSergey Zigachev 			else
1218b843c749SSergey Zigachev 				args.v6.ucLaneNum = 4;
1219b843c749SSergey Zigachev 			args.v6.ucConnObjId = connector_object_id;
1220b843c749SSergey Zigachev 			if (action == ATOM_TRANSMITTER_ACTION_SETUP_VSEMPH)
1221b843c749SSergey Zigachev 				args.v6.ucDPLaneSet = lane_set;
1222b843c749SSergey Zigachev 			else
1223b843c749SSergey Zigachev 				args.v6.ucDigMode = amdgpu_atombios_encoder_get_encoder_mode(encoder);
1224b843c749SSergey Zigachev 
1225b843c749SSergey Zigachev 			if (hpd_id == AMDGPU_HPD_NONE)
1226b843c749SSergey Zigachev 				args.v6.ucHPDSel = 0;
1227b843c749SSergey Zigachev 			else
1228b843c749SSergey Zigachev 				args.v6.ucHPDSel = hpd_id + 1;
1229b843c749SSergey Zigachev 			args.v6.ucDigEncoderSel = 1 << dig_encoder;
1230b843c749SSergey Zigachev 			break;
1231b843c749SSergey Zigachev 		default:
1232b843c749SSergey Zigachev 			DRM_ERROR("Unknown table version %d, %d\n", frev, crev);
1233b843c749SSergey Zigachev 			break;
1234b843c749SSergey Zigachev 		}
1235b843c749SSergey Zigachev 		break;
1236b843c749SSergey Zigachev 	default:
1237b843c749SSergey Zigachev 		DRM_ERROR("Unknown table version %d, %d\n", frev, crev);
1238b843c749SSergey Zigachev 		break;
1239b843c749SSergey Zigachev 	}
1240b843c749SSergey Zigachev 
1241b843c749SSergey Zigachev 	amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
1242b843c749SSergey Zigachev }
1243b843c749SSergey Zigachev 
1244b843c749SSergey Zigachev bool
amdgpu_atombios_encoder_set_edp_panel_power(struct drm_connector * connector,int action)1245b843c749SSergey Zigachev amdgpu_atombios_encoder_set_edp_panel_power(struct drm_connector *connector,
1246b843c749SSergey Zigachev 				     int action)
1247b843c749SSergey Zigachev {
1248b843c749SSergey Zigachev 	struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
1249b843c749SSergey Zigachev 	struct drm_device *dev = amdgpu_connector->base.dev;
1250b843c749SSergey Zigachev 	struct amdgpu_device *adev = dev->dev_private;
1251b843c749SSergey Zigachev 	union dig_transmitter_control args;
1252b843c749SSergey Zigachev 	int index = GetIndexIntoMasterTable(COMMAND, UNIPHYTransmitterControl);
1253b843c749SSergey Zigachev 	uint8_t frev, crev;
1254b843c749SSergey Zigachev 
1255b843c749SSergey Zigachev 	if (connector->connector_type != DRM_MODE_CONNECTOR_eDP)
1256b843c749SSergey Zigachev 		goto done;
1257b843c749SSergey Zigachev 
1258b843c749SSergey Zigachev 	if ((action != ATOM_TRANSMITTER_ACTION_POWER_ON) &&
1259b843c749SSergey Zigachev 	    (action != ATOM_TRANSMITTER_ACTION_POWER_OFF))
1260b843c749SSergey Zigachev 		goto done;
1261b843c749SSergey Zigachev 
1262b843c749SSergey Zigachev 	if (!amdgpu_atom_parse_cmd_header(adev->mode_info.atom_context, index, &frev, &crev))
1263b843c749SSergey Zigachev 		goto done;
1264b843c749SSergey Zigachev 
1265b843c749SSergey Zigachev 	memset(&args, 0, sizeof(args));
1266b843c749SSergey Zigachev 
1267b843c749SSergey Zigachev 	args.v1.ucAction = action;
1268b843c749SSergey Zigachev 
1269b843c749SSergey Zigachev 	amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
1270b843c749SSergey Zigachev 
1271b843c749SSergey Zigachev 	/* wait for the panel to power up */
1272b843c749SSergey Zigachev 	if (action == ATOM_TRANSMITTER_ACTION_POWER_ON) {
1273b843c749SSergey Zigachev 		int i;
1274b843c749SSergey Zigachev 
1275b843c749SSergey Zigachev 		for (i = 0; i < 300; i++) {
1276b843c749SSergey Zigachev 			if (amdgpu_display_hpd_sense(adev, amdgpu_connector->hpd.hpd))
1277b843c749SSergey Zigachev 				return true;
1278b843c749SSergey Zigachev 			mdelay(1);
1279b843c749SSergey Zigachev 		}
1280b843c749SSergey Zigachev 		return false;
1281b843c749SSergey Zigachev 	}
1282b843c749SSergey Zigachev done:
1283b843c749SSergey Zigachev 	return true;
1284b843c749SSergey Zigachev }
1285b843c749SSergey Zigachev 
1286b843c749SSergey Zigachev union external_encoder_control {
1287b843c749SSergey Zigachev 	EXTERNAL_ENCODER_CONTROL_PS_ALLOCATION v1;
1288b843c749SSergey Zigachev 	EXTERNAL_ENCODER_CONTROL_PS_ALLOCATION_V3 v3;
1289b843c749SSergey Zigachev };
1290b843c749SSergey Zigachev 
1291b843c749SSergey Zigachev static void
amdgpu_atombios_encoder_setup_external_encoder(struct drm_encoder * encoder,struct drm_encoder * ext_encoder,int action)1292b843c749SSergey Zigachev amdgpu_atombios_encoder_setup_external_encoder(struct drm_encoder *encoder,
1293b843c749SSergey Zigachev 					struct drm_encoder *ext_encoder,
1294b843c749SSergey Zigachev 					int action)
1295b843c749SSergey Zigachev {
1296b843c749SSergey Zigachev 	struct drm_device *dev = encoder->dev;
1297b843c749SSergey Zigachev 	struct amdgpu_device *adev = dev->dev_private;
1298b843c749SSergey Zigachev 	struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
1299b843c749SSergey Zigachev 	struct amdgpu_encoder *ext_amdgpu_encoder = to_amdgpu_encoder(ext_encoder);
1300b843c749SSergey Zigachev 	union external_encoder_control args;
1301b843c749SSergey Zigachev 	struct drm_connector *connector;
1302b843c749SSergey Zigachev 	int index = GetIndexIntoMasterTable(COMMAND, ExternalEncoderControl);
1303b843c749SSergey Zigachev 	u8 frev, crev;
1304b843c749SSergey Zigachev 	int dp_clock = 0;
1305b843c749SSergey Zigachev 	int dp_lane_count = 0;
1306b843c749SSergey Zigachev 	int connector_object_id = 0;
1307b843c749SSergey Zigachev 	u32 ext_enum = (ext_amdgpu_encoder->encoder_enum & ENUM_ID_MASK) >> ENUM_ID_SHIFT;
1308b843c749SSergey Zigachev 
1309b843c749SSergey Zigachev 	if (action == EXTERNAL_ENCODER_ACTION_V3_ENCODER_INIT)
1310b843c749SSergey Zigachev 		connector = amdgpu_get_connector_for_encoder_init(encoder);
1311b843c749SSergey Zigachev 	else
1312b843c749SSergey Zigachev 		connector = amdgpu_get_connector_for_encoder(encoder);
1313b843c749SSergey Zigachev 
1314b843c749SSergey Zigachev 	if (connector) {
1315b843c749SSergey Zigachev 		struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
1316b843c749SSergey Zigachev 		struct amdgpu_connector_atom_dig *dig_connector =
1317b843c749SSergey Zigachev 			amdgpu_connector->con_priv;
1318b843c749SSergey Zigachev 
1319b843c749SSergey Zigachev 		dp_clock = dig_connector->dp_clock;
1320b843c749SSergey Zigachev 		dp_lane_count = dig_connector->dp_lane_count;
1321b843c749SSergey Zigachev 		connector_object_id =
1322b843c749SSergey Zigachev 			(amdgpu_connector->connector_object_id & OBJECT_ID_MASK) >> OBJECT_ID_SHIFT;
1323b843c749SSergey Zigachev 	}
1324b843c749SSergey Zigachev 
1325b843c749SSergey Zigachev 	memset(&args, 0, sizeof(args));
1326b843c749SSergey Zigachev 
1327b843c749SSergey Zigachev 	if (!amdgpu_atom_parse_cmd_header(adev->mode_info.atom_context, index, &frev, &crev))
1328b843c749SSergey Zigachev 		return;
1329b843c749SSergey Zigachev 
1330b843c749SSergey Zigachev 	switch (frev) {
1331b843c749SSergey Zigachev 	case 1:
1332b843c749SSergey Zigachev 		/* no params on frev 1 */
1333b843c749SSergey Zigachev 		break;
1334b843c749SSergey Zigachev 	case 2:
1335b843c749SSergey Zigachev 		switch (crev) {
1336b843c749SSergey Zigachev 		case 1:
1337b843c749SSergey Zigachev 		case 2:
1338b843c749SSergey Zigachev 			args.v1.sDigEncoder.ucAction = action;
1339b843c749SSergey Zigachev 			args.v1.sDigEncoder.usPixelClock = cpu_to_le16(amdgpu_encoder->pixel_clock / 10);
1340b843c749SSergey Zigachev 			args.v1.sDigEncoder.ucEncoderMode =
1341b843c749SSergey Zigachev 				amdgpu_atombios_encoder_get_encoder_mode(encoder);
1342b843c749SSergey Zigachev 
1343b843c749SSergey Zigachev 			if (ENCODER_MODE_IS_DP(args.v1.sDigEncoder.ucEncoderMode)) {
1344b843c749SSergey Zigachev 				if (dp_clock == 270000)
1345b843c749SSergey Zigachev 					args.v1.sDigEncoder.ucConfig |= ATOM_ENCODER_CONFIG_DPLINKRATE_2_70GHZ;
1346b843c749SSergey Zigachev 				args.v1.sDigEncoder.ucLaneNum = dp_lane_count;
1347b843c749SSergey Zigachev 			} else if (amdgpu_dig_monitor_is_duallink(encoder, amdgpu_encoder->pixel_clock))
1348b843c749SSergey Zigachev 				args.v1.sDigEncoder.ucLaneNum = 8;
1349b843c749SSergey Zigachev 			else
1350b843c749SSergey Zigachev 				args.v1.sDigEncoder.ucLaneNum = 4;
1351b843c749SSergey Zigachev 			break;
1352b843c749SSergey Zigachev 		case 3:
1353b843c749SSergey Zigachev 			args.v3.sExtEncoder.ucAction = action;
1354b843c749SSergey Zigachev 			if (action == EXTERNAL_ENCODER_ACTION_V3_ENCODER_INIT)
1355b843c749SSergey Zigachev 				args.v3.sExtEncoder.usConnectorId = cpu_to_le16(connector_object_id);
1356b843c749SSergey Zigachev 			else
1357b843c749SSergey Zigachev 				args.v3.sExtEncoder.usPixelClock = cpu_to_le16(amdgpu_encoder->pixel_clock / 10);
1358b843c749SSergey Zigachev 			args.v3.sExtEncoder.ucEncoderMode =
1359b843c749SSergey Zigachev 				amdgpu_atombios_encoder_get_encoder_mode(encoder);
1360b843c749SSergey Zigachev 
1361b843c749SSergey Zigachev 			if (ENCODER_MODE_IS_DP(args.v3.sExtEncoder.ucEncoderMode)) {
1362b843c749SSergey Zigachev 				if (dp_clock == 270000)
1363b843c749SSergey Zigachev 					args.v3.sExtEncoder.ucConfig |= EXTERNAL_ENCODER_CONFIG_V3_DPLINKRATE_2_70GHZ;
1364b843c749SSergey Zigachev 				else if (dp_clock == 540000)
1365b843c749SSergey Zigachev 					args.v3.sExtEncoder.ucConfig |= EXTERNAL_ENCODER_CONFIG_V3_DPLINKRATE_5_40GHZ;
1366b843c749SSergey Zigachev 				args.v3.sExtEncoder.ucLaneNum = dp_lane_count;
1367b843c749SSergey Zigachev 			} else if (amdgpu_dig_monitor_is_duallink(encoder, amdgpu_encoder->pixel_clock))
1368b843c749SSergey Zigachev 				args.v3.sExtEncoder.ucLaneNum = 8;
1369b843c749SSergey Zigachev 			else
1370b843c749SSergey Zigachev 				args.v3.sExtEncoder.ucLaneNum = 4;
1371b843c749SSergey Zigachev 			switch (ext_enum) {
1372b843c749SSergey Zigachev 			case GRAPH_OBJECT_ENUM_ID1:
1373b843c749SSergey Zigachev 				args.v3.sExtEncoder.ucConfig |= EXTERNAL_ENCODER_CONFIG_V3_ENCODER1;
1374b843c749SSergey Zigachev 				break;
1375b843c749SSergey Zigachev 			case GRAPH_OBJECT_ENUM_ID2:
1376b843c749SSergey Zigachev 				args.v3.sExtEncoder.ucConfig |= EXTERNAL_ENCODER_CONFIG_V3_ENCODER2;
1377b843c749SSergey Zigachev 				break;
1378b843c749SSergey Zigachev 			case GRAPH_OBJECT_ENUM_ID3:
1379b843c749SSergey Zigachev 				args.v3.sExtEncoder.ucConfig |= EXTERNAL_ENCODER_CONFIG_V3_ENCODER3;
1380b843c749SSergey Zigachev 				break;
1381b843c749SSergey Zigachev 			}
1382b843c749SSergey Zigachev 			args.v3.sExtEncoder.ucBitPerColor = amdgpu_atombios_encoder_get_bpc(encoder);
1383b843c749SSergey Zigachev 			break;
1384b843c749SSergey Zigachev 		default:
1385b843c749SSergey Zigachev 			DRM_ERROR("Unknown table version: %d, %d\n", frev, crev);
1386b843c749SSergey Zigachev 			return;
1387b843c749SSergey Zigachev 		}
1388b843c749SSergey Zigachev 		break;
1389b843c749SSergey Zigachev 	default:
1390b843c749SSergey Zigachev 		DRM_ERROR("Unknown table version: %d, %d\n", frev, crev);
1391b843c749SSergey Zigachev 		return;
1392b843c749SSergey Zigachev 	}
1393b843c749SSergey Zigachev 	amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
1394b843c749SSergey Zigachev }
1395b843c749SSergey Zigachev 
1396b843c749SSergey Zigachev static void
amdgpu_atombios_encoder_setup_dig(struct drm_encoder * encoder,int action)1397b843c749SSergey Zigachev amdgpu_atombios_encoder_setup_dig(struct drm_encoder *encoder, int action)
1398b843c749SSergey Zigachev {
1399b843c749SSergey Zigachev 	struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
1400b843c749SSergey Zigachev 	struct drm_encoder *ext_encoder = amdgpu_get_external_encoder(encoder);
1401b843c749SSergey Zigachev 	struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
1402b843c749SSergey Zigachev 	struct drm_connector *connector = amdgpu_get_connector_for_encoder(encoder);
1403b843c749SSergey Zigachev 	struct amdgpu_connector *amdgpu_connector = NULL;
1404b843c749SSergey Zigachev 	struct amdgpu_connector_atom_dig *amdgpu_dig_connector = NULL;
1405b843c749SSergey Zigachev 
1406b843c749SSergey Zigachev 	if (connector) {
1407b843c749SSergey Zigachev 		amdgpu_connector = to_amdgpu_connector(connector);
1408b843c749SSergey Zigachev 		amdgpu_dig_connector = amdgpu_connector->con_priv;
1409b843c749SSergey Zigachev 	}
1410b843c749SSergey Zigachev 
1411b843c749SSergey Zigachev 	if (action == ATOM_ENABLE) {
1412b843c749SSergey Zigachev 		if (!connector)
1413b843c749SSergey Zigachev 			dig->panel_mode = DP_PANEL_MODE_EXTERNAL_DP_MODE;
1414b843c749SSergey Zigachev 		else
1415b843c749SSergey Zigachev 			dig->panel_mode = amdgpu_atombios_dp_get_panel_mode(encoder, connector);
1416b843c749SSergey Zigachev 
1417b843c749SSergey Zigachev 		/* setup and enable the encoder */
1418b843c749SSergey Zigachev 		amdgpu_atombios_encoder_setup_dig_encoder(encoder, ATOM_ENCODER_CMD_SETUP, 0);
1419b843c749SSergey Zigachev 		amdgpu_atombios_encoder_setup_dig_encoder(encoder,
1420b843c749SSergey Zigachev 						   ATOM_ENCODER_CMD_SETUP_PANEL_MODE,
1421b843c749SSergey Zigachev 						   dig->panel_mode);
1422b843c749SSergey Zigachev 		if (ext_encoder)
1423b843c749SSergey Zigachev 			amdgpu_atombios_encoder_setup_external_encoder(encoder, ext_encoder,
1424b843c749SSergey Zigachev 								EXTERNAL_ENCODER_ACTION_V3_ENCODER_SETUP);
1425b843c749SSergey Zigachev 		if (ENCODER_MODE_IS_DP(amdgpu_atombios_encoder_get_encoder_mode(encoder)) &&
1426b843c749SSergey Zigachev 		    connector) {
1427b843c749SSergey Zigachev 			if (connector->connector_type == DRM_MODE_CONNECTOR_eDP) {
1428b843c749SSergey Zigachev 				amdgpu_atombios_encoder_set_edp_panel_power(connector,
1429b843c749SSergey Zigachev 								     ATOM_TRANSMITTER_ACTION_POWER_ON);
1430b843c749SSergey Zigachev 				amdgpu_dig_connector->edp_on = true;
1431b843c749SSergey Zigachev 			}
1432b843c749SSergey Zigachev 		}
1433b843c749SSergey Zigachev 		/* enable the transmitter */
1434b843c749SSergey Zigachev 		amdgpu_atombios_encoder_setup_dig_transmitter(encoder,
1435b843c749SSergey Zigachev 						       ATOM_TRANSMITTER_ACTION_ENABLE,
1436b843c749SSergey Zigachev 						       0, 0);
1437b843c749SSergey Zigachev 		if (ENCODER_MODE_IS_DP(amdgpu_atombios_encoder_get_encoder_mode(encoder)) &&
1438b843c749SSergey Zigachev 		    connector) {
1439b843c749SSergey Zigachev 			/* DP_SET_POWER_D0 is set in amdgpu_atombios_dp_link_train */
1440b843c749SSergey Zigachev 			amdgpu_atombios_dp_link_train(encoder, connector);
1441b843c749SSergey Zigachev 			amdgpu_atombios_encoder_setup_dig_encoder(encoder, ATOM_ENCODER_CMD_DP_VIDEO_ON, 0);
1442b843c749SSergey Zigachev 		}
1443b843c749SSergey Zigachev 		if (amdgpu_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT))
1444b843c749SSergey Zigachev 			amdgpu_atombios_encoder_set_backlight_level(amdgpu_encoder, dig->backlight_level);
1445b843c749SSergey Zigachev 		if (ext_encoder)
1446b843c749SSergey Zigachev 			amdgpu_atombios_encoder_setup_external_encoder(encoder, ext_encoder, ATOM_ENABLE);
1447b843c749SSergey Zigachev 	} else {
1448b843c749SSergey Zigachev 		if (ENCODER_MODE_IS_DP(amdgpu_atombios_encoder_get_encoder_mode(encoder)) &&
1449b843c749SSergey Zigachev 		    connector)
1450b843c749SSergey Zigachev 			amdgpu_atombios_encoder_setup_dig_encoder(encoder,
1451b843c749SSergey Zigachev 							   ATOM_ENCODER_CMD_DP_VIDEO_OFF, 0);
1452b843c749SSergey Zigachev 		if (ext_encoder)
1453b843c749SSergey Zigachev 			amdgpu_atombios_encoder_setup_external_encoder(encoder, ext_encoder, ATOM_DISABLE);
1454b843c749SSergey Zigachev 		if (amdgpu_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT))
1455b843c749SSergey Zigachev 			amdgpu_atombios_encoder_setup_dig_transmitter(encoder,
1456b843c749SSergey Zigachev 							       ATOM_TRANSMITTER_ACTION_LCD_BLOFF, 0, 0);
1457b843c749SSergey Zigachev 
1458b843c749SSergey Zigachev 		if (ENCODER_MODE_IS_DP(amdgpu_atombios_encoder_get_encoder_mode(encoder)) &&
1459b843c749SSergey Zigachev 		    connector)
1460b843c749SSergey Zigachev 			amdgpu_atombios_dp_set_rx_power_state(connector, DP_SET_POWER_D3);
1461b843c749SSergey Zigachev 		/* disable the transmitter */
1462b843c749SSergey Zigachev 		amdgpu_atombios_encoder_setup_dig_transmitter(encoder,
1463b843c749SSergey Zigachev 						       ATOM_TRANSMITTER_ACTION_DISABLE, 0, 0);
1464b843c749SSergey Zigachev 		if (ENCODER_MODE_IS_DP(amdgpu_atombios_encoder_get_encoder_mode(encoder)) &&
1465b843c749SSergey Zigachev 		    connector) {
1466b843c749SSergey Zigachev 			if (connector->connector_type == DRM_MODE_CONNECTOR_eDP) {
1467b843c749SSergey Zigachev 				amdgpu_atombios_encoder_set_edp_panel_power(connector,
1468b843c749SSergey Zigachev 								     ATOM_TRANSMITTER_ACTION_POWER_OFF);
1469b843c749SSergey Zigachev 				amdgpu_dig_connector->edp_on = false;
1470b843c749SSergey Zigachev 			}
1471b843c749SSergey Zigachev 		}
1472b843c749SSergey Zigachev 	}
1473b843c749SSergey Zigachev }
1474b843c749SSergey Zigachev 
1475b843c749SSergey Zigachev void
amdgpu_atombios_encoder_dpms(struct drm_encoder * encoder,int mode)1476b843c749SSergey Zigachev amdgpu_atombios_encoder_dpms(struct drm_encoder *encoder, int mode)
1477b843c749SSergey Zigachev {
1478b843c749SSergey Zigachev 	struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
1479b843c749SSergey Zigachev 
1480b843c749SSergey Zigachev 	DRM_DEBUG_KMS("encoder dpms %d to mode %d, devices %08x, active_devices %08x\n",
1481b843c749SSergey Zigachev 		  amdgpu_encoder->encoder_id, mode, amdgpu_encoder->devices,
1482b843c749SSergey Zigachev 		  amdgpu_encoder->active_device);
1483b843c749SSergey Zigachev 	switch (amdgpu_encoder->encoder_id) {
1484b843c749SSergey Zigachev 	case ENCODER_OBJECT_ID_INTERNAL_UNIPHY:
1485b843c749SSergey Zigachev 	case ENCODER_OBJECT_ID_INTERNAL_UNIPHY1:
1486b843c749SSergey Zigachev 	case ENCODER_OBJECT_ID_INTERNAL_UNIPHY2:
1487b843c749SSergey Zigachev 	case ENCODER_OBJECT_ID_INTERNAL_UNIPHY3:
1488b843c749SSergey Zigachev 		switch (mode) {
1489b843c749SSergey Zigachev 		case DRM_MODE_DPMS_ON:
1490b843c749SSergey Zigachev 			amdgpu_atombios_encoder_setup_dig(encoder, ATOM_ENABLE);
1491b843c749SSergey Zigachev 			break;
1492b843c749SSergey Zigachev 		case DRM_MODE_DPMS_STANDBY:
1493b843c749SSergey Zigachev 		case DRM_MODE_DPMS_SUSPEND:
1494b843c749SSergey Zigachev 		case DRM_MODE_DPMS_OFF:
1495b843c749SSergey Zigachev 			amdgpu_atombios_encoder_setup_dig(encoder, ATOM_DISABLE);
1496b843c749SSergey Zigachev 			break;
1497b843c749SSergey Zigachev 		}
1498b843c749SSergey Zigachev 		break;
1499b843c749SSergey Zigachev 	case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DVO1:
1500b843c749SSergey Zigachev 		switch (mode) {
1501b843c749SSergey Zigachev 		case DRM_MODE_DPMS_ON:
1502b843c749SSergey Zigachev 			amdgpu_atombios_encoder_setup_dvo(encoder, ATOM_ENABLE);
1503b843c749SSergey Zigachev 			break;
1504b843c749SSergey Zigachev 		case DRM_MODE_DPMS_STANDBY:
1505b843c749SSergey Zigachev 		case DRM_MODE_DPMS_SUSPEND:
1506b843c749SSergey Zigachev 		case DRM_MODE_DPMS_OFF:
1507b843c749SSergey Zigachev 			amdgpu_atombios_encoder_setup_dvo(encoder, ATOM_DISABLE);
1508b843c749SSergey Zigachev 			break;
1509b843c749SSergey Zigachev 		}
1510b843c749SSergey Zigachev 		break;
1511b843c749SSergey Zigachev 	case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DAC1:
1512b843c749SSergey Zigachev 		switch (mode) {
1513b843c749SSergey Zigachev 		case DRM_MODE_DPMS_ON:
1514b843c749SSergey Zigachev 			amdgpu_atombios_encoder_setup_dac(encoder, ATOM_ENABLE);
1515b843c749SSergey Zigachev 			break;
1516b843c749SSergey Zigachev 		case DRM_MODE_DPMS_STANDBY:
1517b843c749SSergey Zigachev 		case DRM_MODE_DPMS_SUSPEND:
1518b843c749SSergey Zigachev 		case DRM_MODE_DPMS_OFF:
1519b843c749SSergey Zigachev 			amdgpu_atombios_encoder_setup_dac(encoder, ATOM_DISABLE);
1520b843c749SSergey Zigachev 			break;
1521b843c749SSergey Zigachev 		}
1522b843c749SSergey Zigachev 		break;
1523b843c749SSergey Zigachev 	default:
1524b843c749SSergey Zigachev 		return;
1525b843c749SSergey Zigachev 	}
1526b843c749SSergey Zigachev }
1527b843c749SSergey Zigachev 
1528b843c749SSergey Zigachev union crtc_source_param {
1529b843c749SSergey Zigachev 	SELECT_CRTC_SOURCE_PS_ALLOCATION v1;
1530b843c749SSergey Zigachev 	SELECT_CRTC_SOURCE_PARAMETERS_V2 v2;
1531b843c749SSergey Zigachev 	SELECT_CRTC_SOURCE_PARAMETERS_V3 v3;
1532b843c749SSergey Zigachev };
1533b843c749SSergey Zigachev 
1534b843c749SSergey Zigachev void
amdgpu_atombios_encoder_set_crtc_source(struct drm_encoder * encoder)1535b843c749SSergey Zigachev amdgpu_atombios_encoder_set_crtc_source(struct drm_encoder *encoder)
1536b843c749SSergey Zigachev {
1537b843c749SSergey Zigachev 	struct drm_device *dev = encoder->dev;
1538b843c749SSergey Zigachev 	struct amdgpu_device *adev = dev->dev_private;
1539b843c749SSergey Zigachev 	struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
1540b843c749SSergey Zigachev 	struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(encoder->crtc);
1541b843c749SSergey Zigachev 	union crtc_source_param args;
1542b843c749SSergey Zigachev 	int index = GetIndexIntoMasterTable(COMMAND, SelectCRTC_Source);
1543b843c749SSergey Zigachev 	uint8_t frev, crev;
1544b843c749SSergey Zigachev 	struct amdgpu_encoder_atom_dig *dig;
1545b843c749SSergey Zigachev 
1546b843c749SSergey Zigachev 	memset(&args, 0, sizeof(args));
1547b843c749SSergey Zigachev 
1548b843c749SSergey Zigachev 	if (!amdgpu_atom_parse_cmd_header(adev->mode_info.atom_context, index, &frev, &crev))
1549b843c749SSergey Zigachev 		return;
1550b843c749SSergey Zigachev 
1551b843c749SSergey Zigachev 	switch (frev) {
1552b843c749SSergey Zigachev 	case 1:
1553b843c749SSergey Zigachev 		switch (crev) {
1554b843c749SSergey Zigachev 		case 1:
1555b843c749SSergey Zigachev 		default:
1556b843c749SSergey Zigachev 			args.v1.ucCRTC = amdgpu_crtc->crtc_id;
1557b843c749SSergey Zigachev 			switch (amdgpu_encoder->encoder_id) {
1558b843c749SSergey Zigachev 			case ENCODER_OBJECT_ID_INTERNAL_TMDS1:
1559b843c749SSergey Zigachev 			case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_TMDS1:
1560b843c749SSergey Zigachev 				args.v1.ucDevice = ATOM_DEVICE_DFP1_INDEX;
1561b843c749SSergey Zigachev 				break;
1562b843c749SSergey Zigachev 			case ENCODER_OBJECT_ID_INTERNAL_LVDS:
1563b843c749SSergey Zigachev 			case ENCODER_OBJECT_ID_INTERNAL_LVTM1:
1564b843c749SSergey Zigachev 				if (amdgpu_encoder->devices & ATOM_DEVICE_LCD1_SUPPORT)
1565b843c749SSergey Zigachev 					args.v1.ucDevice = ATOM_DEVICE_LCD1_INDEX;
1566b843c749SSergey Zigachev 				else
1567b843c749SSergey Zigachev 					args.v1.ucDevice = ATOM_DEVICE_DFP3_INDEX;
1568b843c749SSergey Zigachev 				break;
1569b843c749SSergey Zigachev 			case ENCODER_OBJECT_ID_INTERNAL_DVO1:
1570b843c749SSergey Zigachev 			case ENCODER_OBJECT_ID_INTERNAL_DDI:
1571b843c749SSergey Zigachev 			case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DVO1:
1572b843c749SSergey Zigachev 				args.v1.ucDevice = ATOM_DEVICE_DFP2_INDEX;
1573b843c749SSergey Zigachev 				break;
1574b843c749SSergey Zigachev 			case ENCODER_OBJECT_ID_INTERNAL_DAC1:
1575b843c749SSergey Zigachev 			case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DAC1:
1576b843c749SSergey Zigachev 				if (amdgpu_encoder->active_device & (ATOM_DEVICE_TV_SUPPORT))
1577b843c749SSergey Zigachev 					args.v1.ucDevice = ATOM_DEVICE_TV1_INDEX;
1578b843c749SSergey Zigachev 				else if (amdgpu_encoder->active_device & (ATOM_DEVICE_CV_SUPPORT))
1579b843c749SSergey Zigachev 					args.v1.ucDevice = ATOM_DEVICE_CV_INDEX;
1580b843c749SSergey Zigachev 				else
1581b843c749SSergey Zigachev 					args.v1.ucDevice = ATOM_DEVICE_CRT1_INDEX;
1582b843c749SSergey Zigachev 				break;
1583b843c749SSergey Zigachev 			case ENCODER_OBJECT_ID_INTERNAL_DAC2:
1584b843c749SSergey Zigachev 			case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DAC2:
1585b843c749SSergey Zigachev 				if (amdgpu_encoder->active_device & (ATOM_DEVICE_TV_SUPPORT))
1586b843c749SSergey Zigachev 					args.v1.ucDevice = ATOM_DEVICE_TV1_INDEX;
1587b843c749SSergey Zigachev 				else if (amdgpu_encoder->active_device & (ATOM_DEVICE_CV_SUPPORT))
1588b843c749SSergey Zigachev 					args.v1.ucDevice = ATOM_DEVICE_CV_INDEX;
1589b843c749SSergey Zigachev 				else
1590b843c749SSergey Zigachev 					args.v1.ucDevice = ATOM_DEVICE_CRT2_INDEX;
1591b843c749SSergey Zigachev 				break;
1592b843c749SSergey Zigachev 			}
1593b843c749SSergey Zigachev 			break;
1594b843c749SSergey Zigachev 		case 2:
1595b843c749SSergey Zigachev 			args.v2.ucCRTC = amdgpu_crtc->crtc_id;
1596b843c749SSergey Zigachev 			if (amdgpu_encoder_get_dp_bridge_encoder_id(encoder) != ENCODER_OBJECT_ID_NONE) {
1597b843c749SSergey Zigachev 				struct drm_connector *connector = amdgpu_get_connector_for_encoder(encoder);
1598b843c749SSergey Zigachev 
1599b843c749SSergey Zigachev 				if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS)
1600b843c749SSergey Zigachev 					args.v2.ucEncodeMode = ATOM_ENCODER_MODE_LVDS;
1601b843c749SSergey Zigachev 				else if (connector->connector_type == DRM_MODE_CONNECTOR_VGA)
1602b843c749SSergey Zigachev 					args.v2.ucEncodeMode = ATOM_ENCODER_MODE_CRT;
1603b843c749SSergey Zigachev 				else
1604b843c749SSergey Zigachev 					args.v2.ucEncodeMode = amdgpu_atombios_encoder_get_encoder_mode(encoder);
1605b843c749SSergey Zigachev 			} else if (amdgpu_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT)) {
1606b843c749SSergey Zigachev 				args.v2.ucEncodeMode = ATOM_ENCODER_MODE_LVDS;
1607b843c749SSergey Zigachev 			} else {
1608b843c749SSergey Zigachev 				args.v2.ucEncodeMode = amdgpu_atombios_encoder_get_encoder_mode(encoder);
1609b843c749SSergey Zigachev 			}
1610b843c749SSergey Zigachev 			switch (amdgpu_encoder->encoder_id) {
1611b843c749SSergey Zigachev 			case ENCODER_OBJECT_ID_INTERNAL_UNIPHY:
1612b843c749SSergey Zigachev 			case ENCODER_OBJECT_ID_INTERNAL_UNIPHY1:
1613b843c749SSergey Zigachev 			case ENCODER_OBJECT_ID_INTERNAL_UNIPHY2:
1614b843c749SSergey Zigachev 			case ENCODER_OBJECT_ID_INTERNAL_UNIPHY3:
1615b843c749SSergey Zigachev 			case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_LVTMA:
1616b843c749SSergey Zigachev 				dig = amdgpu_encoder->enc_priv;
1617b843c749SSergey Zigachev 				switch (dig->dig_encoder) {
1618b843c749SSergey Zigachev 				case 0:
1619b843c749SSergey Zigachev 					args.v2.ucEncoderID = ASIC_INT_DIG1_ENCODER_ID;
1620b843c749SSergey Zigachev 					break;
1621b843c749SSergey Zigachev 				case 1:
1622b843c749SSergey Zigachev 					args.v2.ucEncoderID = ASIC_INT_DIG2_ENCODER_ID;
1623b843c749SSergey Zigachev 					break;
1624b843c749SSergey Zigachev 				case 2:
1625b843c749SSergey Zigachev 					args.v2.ucEncoderID = ASIC_INT_DIG3_ENCODER_ID;
1626b843c749SSergey Zigachev 					break;
1627b843c749SSergey Zigachev 				case 3:
1628b843c749SSergey Zigachev 					args.v2.ucEncoderID = ASIC_INT_DIG4_ENCODER_ID;
1629b843c749SSergey Zigachev 					break;
1630b843c749SSergey Zigachev 				case 4:
1631b843c749SSergey Zigachev 					args.v2.ucEncoderID = ASIC_INT_DIG5_ENCODER_ID;
1632b843c749SSergey Zigachev 					break;
1633b843c749SSergey Zigachev 				case 5:
1634b843c749SSergey Zigachev 					args.v2.ucEncoderID = ASIC_INT_DIG6_ENCODER_ID;
1635b843c749SSergey Zigachev 					break;
1636b843c749SSergey Zigachev 				case 6:
1637b843c749SSergey Zigachev 					args.v2.ucEncoderID = ASIC_INT_DIG7_ENCODER_ID;
1638b843c749SSergey Zigachev 					break;
1639b843c749SSergey Zigachev 				}
1640b843c749SSergey Zigachev 				break;
1641b843c749SSergey Zigachev 			case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DVO1:
1642b843c749SSergey Zigachev 				args.v2.ucEncoderID = ASIC_INT_DVO_ENCODER_ID;
1643b843c749SSergey Zigachev 				break;
1644b843c749SSergey Zigachev 			case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DAC1:
1645b843c749SSergey Zigachev 				if (amdgpu_encoder->active_device & (ATOM_DEVICE_TV_SUPPORT))
1646b843c749SSergey Zigachev 					args.v2.ucEncoderID = ASIC_INT_TV_ENCODER_ID;
1647b843c749SSergey Zigachev 				else if (amdgpu_encoder->active_device & (ATOM_DEVICE_CV_SUPPORT))
1648b843c749SSergey Zigachev 					args.v2.ucEncoderID = ASIC_INT_TV_ENCODER_ID;
1649b843c749SSergey Zigachev 				else
1650b843c749SSergey Zigachev 					args.v2.ucEncoderID = ASIC_INT_DAC1_ENCODER_ID;
1651b843c749SSergey Zigachev 				break;
1652b843c749SSergey Zigachev 			case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DAC2:
1653b843c749SSergey Zigachev 				if (amdgpu_encoder->active_device & (ATOM_DEVICE_TV_SUPPORT))
1654b843c749SSergey Zigachev 					args.v2.ucEncoderID = ASIC_INT_TV_ENCODER_ID;
1655b843c749SSergey Zigachev 				else if (amdgpu_encoder->active_device & (ATOM_DEVICE_CV_SUPPORT))
1656b843c749SSergey Zigachev 					args.v2.ucEncoderID = ASIC_INT_TV_ENCODER_ID;
1657b843c749SSergey Zigachev 				else
1658b843c749SSergey Zigachev 					args.v2.ucEncoderID = ASIC_INT_DAC2_ENCODER_ID;
1659b843c749SSergey Zigachev 				break;
1660b843c749SSergey Zigachev 			}
1661b843c749SSergey Zigachev 			break;
1662b843c749SSergey Zigachev 		case 3:
1663b843c749SSergey Zigachev 			args.v3.ucCRTC = amdgpu_crtc->crtc_id;
1664b843c749SSergey Zigachev 			if (amdgpu_encoder_get_dp_bridge_encoder_id(encoder) != ENCODER_OBJECT_ID_NONE) {
1665b843c749SSergey Zigachev 				struct drm_connector *connector = amdgpu_get_connector_for_encoder(encoder);
1666b843c749SSergey Zigachev 
1667b843c749SSergey Zigachev 				if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS)
1668b843c749SSergey Zigachev 					args.v2.ucEncodeMode = ATOM_ENCODER_MODE_LVDS;
1669b843c749SSergey Zigachev 				else if (connector->connector_type == DRM_MODE_CONNECTOR_VGA)
1670b843c749SSergey Zigachev 					args.v2.ucEncodeMode = ATOM_ENCODER_MODE_CRT;
1671b843c749SSergey Zigachev 				else
1672b843c749SSergey Zigachev 					args.v2.ucEncodeMode = amdgpu_atombios_encoder_get_encoder_mode(encoder);
1673b843c749SSergey Zigachev 			} else if (amdgpu_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT)) {
1674b843c749SSergey Zigachev 				args.v2.ucEncodeMode = ATOM_ENCODER_MODE_LVDS;
1675b843c749SSergey Zigachev 			} else {
1676b843c749SSergey Zigachev 				args.v2.ucEncodeMode = amdgpu_atombios_encoder_get_encoder_mode(encoder);
1677b843c749SSergey Zigachev 			}
1678b843c749SSergey Zigachev 			args.v3.ucDstBpc = amdgpu_atombios_encoder_get_bpc(encoder);
1679b843c749SSergey Zigachev 			switch (amdgpu_encoder->encoder_id) {
1680b843c749SSergey Zigachev 			case ENCODER_OBJECT_ID_INTERNAL_UNIPHY:
1681b843c749SSergey Zigachev 			case ENCODER_OBJECT_ID_INTERNAL_UNIPHY1:
1682b843c749SSergey Zigachev 			case ENCODER_OBJECT_ID_INTERNAL_UNIPHY2:
1683b843c749SSergey Zigachev 			case ENCODER_OBJECT_ID_INTERNAL_UNIPHY3:
1684b843c749SSergey Zigachev 			case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_LVTMA:
1685b843c749SSergey Zigachev 				dig = amdgpu_encoder->enc_priv;
1686b843c749SSergey Zigachev 				switch (dig->dig_encoder) {
1687b843c749SSergey Zigachev 				case 0:
1688b843c749SSergey Zigachev 					args.v3.ucEncoderID = ASIC_INT_DIG1_ENCODER_ID;
1689b843c749SSergey Zigachev 					break;
1690b843c749SSergey Zigachev 				case 1:
1691b843c749SSergey Zigachev 					args.v3.ucEncoderID = ASIC_INT_DIG2_ENCODER_ID;
1692b843c749SSergey Zigachev 					break;
1693b843c749SSergey Zigachev 				case 2:
1694b843c749SSergey Zigachev 					args.v3.ucEncoderID = ASIC_INT_DIG3_ENCODER_ID;
1695b843c749SSergey Zigachev 					break;
1696b843c749SSergey Zigachev 				case 3:
1697b843c749SSergey Zigachev 					args.v3.ucEncoderID = ASIC_INT_DIG4_ENCODER_ID;
1698b843c749SSergey Zigachev 					break;
1699b843c749SSergey Zigachev 				case 4:
1700b843c749SSergey Zigachev 					args.v3.ucEncoderID = ASIC_INT_DIG5_ENCODER_ID;
1701b843c749SSergey Zigachev 					break;
1702b843c749SSergey Zigachev 				case 5:
1703b843c749SSergey Zigachev 					args.v3.ucEncoderID = ASIC_INT_DIG6_ENCODER_ID;
1704b843c749SSergey Zigachev 					break;
1705b843c749SSergey Zigachev 				case 6:
1706b843c749SSergey Zigachev 					args.v3.ucEncoderID = ASIC_INT_DIG7_ENCODER_ID;
1707b843c749SSergey Zigachev 					break;
1708b843c749SSergey Zigachev 				}
1709b843c749SSergey Zigachev 				break;
1710b843c749SSergey Zigachev 			case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DVO1:
1711b843c749SSergey Zigachev 				args.v3.ucEncoderID = ASIC_INT_DVO_ENCODER_ID;
1712b843c749SSergey Zigachev 				break;
1713b843c749SSergey Zigachev 			case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DAC1:
1714b843c749SSergey Zigachev 				if (amdgpu_encoder->active_device & (ATOM_DEVICE_TV_SUPPORT))
1715b843c749SSergey Zigachev 					args.v3.ucEncoderID = ASIC_INT_TV_ENCODER_ID;
1716b843c749SSergey Zigachev 				else if (amdgpu_encoder->active_device & (ATOM_DEVICE_CV_SUPPORT))
1717b843c749SSergey Zigachev 					args.v3.ucEncoderID = ASIC_INT_TV_ENCODER_ID;
1718b843c749SSergey Zigachev 				else
1719b843c749SSergey Zigachev 					args.v3.ucEncoderID = ASIC_INT_DAC1_ENCODER_ID;
1720b843c749SSergey Zigachev 				break;
1721b843c749SSergey Zigachev 			case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DAC2:
1722b843c749SSergey Zigachev 				if (amdgpu_encoder->active_device & (ATOM_DEVICE_TV_SUPPORT))
1723b843c749SSergey Zigachev 					args.v3.ucEncoderID = ASIC_INT_TV_ENCODER_ID;
1724b843c749SSergey Zigachev 				else if (amdgpu_encoder->active_device & (ATOM_DEVICE_CV_SUPPORT))
1725b843c749SSergey Zigachev 					args.v3.ucEncoderID = ASIC_INT_TV_ENCODER_ID;
1726b843c749SSergey Zigachev 				else
1727b843c749SSergey Zigachev 					args.v3.ucEncoderID = ASIC_INT_DAC2_ENCODER_ID;
1728b843c749SSergey Zigachev 				break;
1729b843c749SSergey Zigachev 			}
1730b843c749SSergey Zigachev 			break;
1731b843c749SSergey Zigachev 		}
1732b843c749SSergey Zigachev 		break;
1733b843c749SSergey Zigachev 	default:
1734b843c749SSergey Zigachev 		DRM_ERROR("Unknown table version: %d, %d\n", frev, crev);
1735b843c749SSergey Zigachev 		return;
1736b843c749SSergey Zigachev 	}
1737b843c749SSergey Zigachev 
1738b843c749SSergey Zigachev 	amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
1739b843c749SSergey Zigachev }
1740b843c749SSergey Zigachev 
1741b843c749SSergey Zigachev /* This only needs to be called once at startup */
1742b843c749SSergey Zigachev void
amdgpu_atombios_encoder_init_dig(struct amdgpu_device * adev)1743b843c749SSergey Zigachev amdgpu_atombios_encoder_init_dig(struct amdgpu_device *adev)
1744b843c749SSergey Zigachev {
1745b843c749SSergey Zigachev 	struct drm_device *dev = adev->ddev;
1746b843c749SSergey Zigachev 	struct drm_encoder *encoder;
1747b843c749SSergey Zigachev 
1748b843c749SSergey Zigachev 	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1749b843c749SSergey Zigachev 		struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
1750b843c749SSergey Zigachev 		struct drm_encoder *ext_encoder = amdgpu_get_external_encoder(encoder);
1751b843c749SSergey Zigachev 
1752b843c749SSergey Zigachev 		switch (amdgpu_encoder->encoder_id) {
1753b843c749SSergey Zigachev 		case ENCODER_OBJECT_ID_INTERNAL_UNIPHY:
1754b843c749SSergey Zigachev 		case ENCODER_OBJECT_ID_INTERNAL_UNIPHY1:
1755b843c749SSergey Zigachev 		case ENCODER_OBJECT_ID_INTERNAL_UNIPHY2:
1756b843c749SSergey Zigachev 		case ENCODER_OBJECT_ID_INTERNAL_UNIPHY3:
1757b843c749SSergey Zigachev 			amdgpu_atombios_encoder_setup_dig_transmitter(encoder, ATOM_TRANSMITTER_ACTION_INIT,
1758b843c749SSergey Zigachev 							       0, 0);
1759b843c749SSergey Zigachev 			break;
1760b843c749SSergey Zigachev 		}
1761b843c749SSergey Zigachev 
1762b843c749SSergey Zigachev 		if (ext_encoder)
1763b843c749SSergey Zigachev 			amdgpu_atombios_encoder_setup_external_encoder(encoder, ext_encoder,
1764b843c749SSergey Zigachev 								EXTERNAL_ENCODER_ACTION_V3_ENCODER_INIT);
1765b843c749SSergey Zigachev 	}
1766b843c749SSergey Zigachev }
1767b843c749SSergey Zigachev 
1768b843c749SSergey Zigachev static bool
amdgpu_atombios_encoder_dac_load_detect(struct drm_encoder * encoder,struct drm_connector * connector)1769b843c749SSergey Zigachev amdgpu_atombios_encoder_dac_load_detect(struct drm_encoder *encoder,
1770b843c749SSergey Zigachev 				 struct drm_connector *connector)
1771b843c749SSergey Zigachev {
1772b843c749SSergey Zigachev 	struct drm_device *dev = encoder->dev;
1773b843c749SSergey Zigachev 	struct amdgpu_device *adev = dev->dev_private;
1774b843c749SSergey Zigachev 	struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
1775b843c749SSergey Zigachev 	struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
1776b843c749SSergey Zigachev 
1777b843c749SSergey Zigachev 	if (amdgpu_encoder->devices & (ATOM_DEVICE_TV_SUPPORT |
1778b843c749SSergey Zigachev 				       ATOM_DEVICE_CV_SUPPORT |
1779b843c749SSergey Zigachev 				       ATOM_DEVICE_CRT_SUPPORT)) {
1780b843c749SSergey Zigachev 		DAC_LOAD_DETECTION_PS_ALLOCATION args;
1781b843c749SSergey Zigachev 		int index = GetIndexIntoMasterTable(COMMAND, DAC_LoadDetection);
1782b843c749SSergey Zigachev 		uint8_t frev, crev;
1783b843c749SSergey Zigachev 
1784b843c749SSergey Zigachev 		memset(&args, 0, sizeof(args));
1785b843c749SSergey Zigachev 
1786b843c749SSergey Zigachev 		if (!amdgpu_atom_parse_cmd_header(adev->mode_info.atom_context, index, &frev, &crev))
1787b843c749SSergey Zigachev 			return false;
1788b843c749SSergey Zigachev 
1789b843c749SSergey Zigachev 		args.sDacload.ucMisc = 0;
1790b843c749SSergey Zigachev 
1791b843c749SSergey Zigachev 		if ((amdgpu_encoder->encoder_id == ENCODER_OBJECT_ID_INTERNAL_DAC1) ||
1792b843c749SSergey Zigachev 		    (amdgpu_encoder->encoder_id == ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DAC1))
1793b843c749SSergey Zigachev 			args.sDacload.ucDacType = ATOM_DAC_A;
1794b843c749SSergey Zigachev 		else
1795b843c749SSergey Zigachev 			args.sDacload.ucDacType = ATOM_DAC_B;
1796b843c749SSergey Zigachev 
1797b843c749SSergey Zigachev 		if (amdgpu_connector->devices & ATOM_DEVICE_CRT1_SUPPORT)
1798b843c749SSergey Zigachev 			args.sDacload.usDeviceID = cpu_to_le16(ATOM_DEVICE_CRT1_SUPPORT);
1799b843c749SSergey Zigachev 		else if (amdgpu_connector->devices & ATOM_DEVICE_CRT2_SUPPORT)
1800b843c749SSergey Zigachev 			args.sDacload.usDeviceID = cpu_to_le16(ATOM_DEVICE_CRT2_SUPPORT);
1801b843c749SSergey Zigachev 		else if (amdgpu_connector->devices & ATOM_DEVICE_CV_SUPPORT) {
1802b843c749SSergey Zigachev 			args.sDacload.usDeviceID = cpu_to_le16(ATOM_DEVICE_CV_SUPPORT);
1803b843c749SSergey Zigachev 			if (crev >= 3)
1804b843c749SSergey Zigachev 				args.sDacload.ucMisc = DAC_LOAD_MISC_YPrPb;
1805b843c749SSergey Zigachev 		} else if (amdgpu_connector->devices & ATOM_DEVICE_TV1_SUPPORT) {
1806b843c749SSergey Zigachev 			args.sDacload.usDeviceID = cpu_to_le16(ATOM_DEVICE_TV1_SUPPORT);
1807b843c749SSergey Zigachev 			if (crev >= 3)
1808b843c749SSergey Zigachev 				args.sDacload.ucMisc = DAC_LOAD_MISC_YPrPb;
1809b843c749SSergey Zigachev 		}
1810b843c749SSergey Zigachev 
1811b843c749SSergey Zigachev 		amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
1812b843c749SSergey Zigachev 
1813b843c749SSergey Zigachev 		return true;
1814b843c749SSergey Zigachev 	} else
1815b843c749SSergey Zigachev 		return false;
1816b843c749SSergey Zigachev }
1817b843c749SSergey Zigachev 
1818b843c749SSergey Zigachev enum drm_connector_status
amdgpu_atombios_encoder_dac_detect(struct drm_encoder * encoder,struct drm_connector * connector)1819b843c749SSergey Zigachev amdgpu_atombios_encoder_dac_detect(struct drm_encoder *encoder,
1820b843c749SSergey Zigachev 			    struct drm_connector *connector)
1821b843c749SSergey Zigachev {
1822b843c749SSergey Zigachev 	struct drm_device *dev = encoder->dev;
1823b843c749SSergey Zigachev 	struct amdgpu_device *adev = dev->dev_private;
1824b843c749SSergey Zigachev 	struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
1825b843c749SSergey Zigachev 	struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
1826b843c749SSergey Zigachev 	uint32_t bios_0_scratch;
1827b843c749SSergey Zigachev 
1828b843c749SSergey Zigachev 	if (!amdgpu_atombios_encoder_dac_load_detect(encoder, connector)) {
1829b843c749SSergey Zigachev 		DRM_DEBUG_KMS("detect returned false \n");
1830b843c749SSergey Zigachev 		return connector_status_unknown;
1831b843c749SSergey Zigachev 	}
1832b843c749SSergey Zigachev 
1833b843c749SSergey Zigachev 	bios_0_scratch = RREG32(mmBIOS_SCRATCH_0);
1834b843c749SSergey Zigachev 
1835b843c749SSergey Zigachev 	DRM_DEBUG_KMS("Bios 0 scratch %x %08x\n", bios_0_scratch, amdgpu_encoder->devices);
1836b843c749SSergey Zigachev 	if (amdgpu_connector->devices & ATOM_DEVICE_CRT1_SUPPORT) {
1837b843c749SSergey Zigachev 		if (bios_0_scratch & ATOM_S0_CRT1_MASK)
1838b843c749SSergey Zigachev 			return connector_status_connected;
1839b843c749SSergey Zigachev 	}
1840b843c749SSergey Zigachev 	if (amdgpu_connector->devices & ATOM_DEVICE_CRT2_SUPPORT) {
1841b843c749SSergey Zigachev 		if (bios_0_scratch & ATOM_S0_CRT2_MASK)
1842b843c749SSergey Zigachev 			return connector_status_connected;
1843b843c749SSergey Zigachev 	}
1844b843c749SSergey Zigachev 	if (amdgpu_connector->devices & ATOM_DEVICE_CV_SUPPORT) {
1845b843c749SSergey Zigachev 		if (bios_0_scratch & (ATOM_S0_CV_MASK|ATOM_S0_CV_MASK_A))
1846b843c749SSergey Zigachev 			return connector_status_connected;
1847b843c749SSergey Zigachev 	}
1848b843c749SSergey Zigachev 	if (amdgpu_connector->devices & ATOM_DEVICE_TV1_SUPPORT) {
1849b843c749SSergey Zigachev 		if (bios_0_scratch & (ATOM_S0_TV1_COMPOSITE | ATOM_S0_TV1_COMPOSITE_A))
1850b843c749SSergey Zigachev 			return connector_status_connected; /* CTV */
1851b843c749SSergey Zigachev 		else if (bios_0_scratch & (ATOM_S0_TV1_SVIDEO | ATOM_S0_TV1_SVIDEO_A))
1852b843c749SSergey Zigachev 			return connector_status_connected; /* STV */
1853b843c749SSergey Zigachev 	}
1854b843c749SSergey Zigachev 	return connector_status_disconnected;
1855b843c749SSergey Zigachev }
1856b843c749SSergey Zigachev 
1857b843c749SSergey Zigachev enum drm_connector_status
amdgpu_atombios_encoder_dig_detect(struct drm_encoder * encoder,struct drm_connector * connector)1858b843c749SSergey Zigachev amdgpu_atombios_encoder_dig_detect(struct drm_encoder *encoder,
1859b843c749SSergey Zigachev 			    struct drm_connector *connector)
1860b843c749SSergey Zigachev {
1861b843c749SSergey Zigachev 	struct drm_device *dev = encoder->dev;
1862b843c749SSergey Zigachev 	struct amdgpu_device *adev = dev->dev_private;
1863b843c749SSergey Zigachev 	struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
1864b843c749SSergey Zigachev 	struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
1865b843c749SSergey Zigachev 	struct drm_encoder *ext_encoder = amdgpu_get_external_encoder(encoder);
1866b843c749SSergey Zigachev 	u32 bios_0_scratch;
1867b843c749SSergey Zigachev 
1868b843c749SSergey Zigachev 	if (!ext_encoder)
1869b843c749SSergey Zigachev 		return connector_status_unknown;
1870b843c749SSergey Zigachev 
1871b843c749SSergey Zigachev 	if ((amdgpu_connector->devices & ATOM_DEVICE_CRT_SUPPORT) == 0)
1872b843c749SSergey Zigachev 		return connector_status_unknown;
1873b843c749SSergey Zigachev 
1874b843c749SSergey Zigachev 	/* load detect on the dp bridge */
1875b843c749SSergey Zigachev 	amdgpu_atombios_encoder_setup_external_encoder(encoder, ext_encoder,
1876b843c749SSergey Zigachev 						EXTERNAL_ENCODER_ACTION_V3_DACLOAD_DETECTION);
1877b843c749SSergey Zigachev 
1878b843c749SSergey Zigachev 	bios_0_scratch = RREG32(mmBIOS_SCRATCH_0);
1879b843c749SSergey Zigachev 
1880b843c749SSergey Zigachev 	DRM_DEBUG_KMS("Bios 0 scratch %x %08x\n", bios_0_scratch, amdgpu_encoder->devices);
1881b843c749SSergey Zigachev 	if (amdgpu_connector->devices & ATOM_DEVICE_CRT1_SUPPORT) {
1882b843c749SSergey Zigachev 		if (bios_0_scratch & ATOM_S0_CRT1_MASK)
1883b843c749SSergey Zigachev 			return connector_status_connected;
1884b843c749SSergey Zigachev 	}
1885b843c749SSergey Zigachev 	if (amdgpu_connector->devices & ATOM_DEVICE_CRT2_SUPPORT) {
1886b843c749SSergey Zigachev 		if (bios_0_scratch & ATOM_S0_CRT2_MASK)
1887b843c749SSergey Zigachev 			return connector_status_connected;
1888b843c749SSergey Zigachev 	}
1889b843c749SSergey Zigachev 	if (amdgpu_connector->devices & ATOM_DEVICE_CV_SUPPORT) {
1890b843c749SSergey Zigachev 		if (bios_0_scratch & (ATOM_S0_CV_MASK|ATOM_S0_CV_MASK_A))
1891b843c749SSergey Zigachev 			return connector_status_connected;
1892b843c749SSergey Zigachev 	}
1893b843c749SSergey Zigachev 	if (amdgpu_connector->devices & ATOM_DEVICE_TV1_SUPPORT) {
1894b843c749SSergey Zigachev 		if (bios_0_scratch & (ATOM_S0_TV1_COMPOSITE | ATOM_S0_TV1_COMPOSITE_A))
1895b843c749SSergey Zigachev 			return connector_status_connected; /* CTV */
1896b843c749SSergey Zigachev 		else if (bios_0_scratch & (ATOM_S0_TV1_SVIDEO | ATOM_S0_TV1_SVIDEO_A))
1897b843c749SSergey Zigachev 			return connector_status_connected; /* STV */
1898b843c749SSergey Zigachev 	}
1899b843c749SSergey Zigachev 	return connector_status_disconnected;
1900b843c749SSergey Zigachev }
1901b843c749SSergey Zigachev 
1902b843c749SSergey Zigachev void
amdgpu_atombios_encoder_setup_ext_encoder_ddc(struct drm_encoder * encoder)1903b843c749SSergey Zigachev amdgpu_atombios_encoder_setup_ext_encoder_ddc(struct drm_encoder *encoder)
1904b843c749SSergey Zigachev {
1905b843c749SSergey Zigachev 	struct drm_encoder *ext_encoder = amdgpu_get_external_encoder(encoder);
1906b843c749SSergey Zigachev 
1907b843c749SSergey Zigachev 	if (ext_encoder)
1908b843c749SSergey Zigachev 		/* ddc_setup on the dp bridge */
1909b843c749SSergey Zigachev 		amdgpu_atombios_encoder_setup_external_encoder(encoder, ext_encoder,
1910b843c749SSergey Zigachev 							EXTERNAL_ENCODER_ACTION_V3_DDC_SETUP);
1911b843c749SSergey Zigachev 
1912b843c749SSergey Zigachev }
1913b843c749SSergey Zigachev 
1914b843c749SSergey Zigachev void
amdgpu_atombios_encoder_set_bios_scratch_regs(struct drm_connector * connector,struct drm_encoder * encoder,bool connected)1915b843c749SSergey Zigachev amdgpu_atombios_encoder_set_bios_scratch_regs(struct drm_connector *connector,
1916b843c749SSergey Zigachev 				       struct drm_encoder *encoder,
1917b843c749SSergey Zigachev 				       bool connected)
1918b843c749SSergey Zigachev {
1919b843c749SSergey Zigachev 	struct drm_device *dev = connector->dev;
1920b843c749SSergey Zigachev 	struct amdgpu_device *adev = dev->dev_private;
1921b843c749SSergey Zigachev 	struct amdgpu_connector *amdgpu_connector =
1922b843c749SSergey Zigachev 	    to_amdgpu_connector(connector);
1923b843c749SSergey Zigachev 	struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
1924b843c749SSergey Zigachev 	uint32_t bios_0_scratch, bios_3_scratch, bios_6_scratch;
1925b843c749SSergey Zigachev 
1926b843c749SSergey Zigachev 	bios_0_scratch = RREG32(mmBIOS_SCRATCH_0);
1927b843c749SSergey Zigachev 	bios_3_scratch = RREG32(mmBIOS_SCRATCH_3);
1928b843c749SSergey Zigachev 	bios_6_scratch = RREG32(mmBIOS_SCRATCH_6);
1929b843c749SSergey Zigachev 
1930b843c749SSergey Zigachev 	if ((amdgpu_encoder->devices & ATOM_DEVICE_LCD1_SUPPORT) &&
1931b843c749SSergey Zigachev 	    (amdgpu_connector->devices & ATOM_DEVICE_LCD1_SUPPORT)) {
1932b843c749SSergey Zigachev 		if (connected) {
1933b843c749SSergey Zigachev 			DRM_DEBUG_KMS("LCD1 connected\n");
1934b843c749SSergey Zigachev 			bios_0_scratch |= ATOM_S0_LCD1;
1935b843c749SSergey Zigachev 			bios_3_scratch |= ATOM_S3_LCD1_ACTIVE;
1936b843c749SSergey Zigachev 			bios_6_scratch |= ATOM_S6_ACC_REQ_LCD1;
1937b843c749SSergey Zigachev 		} else {
1938b843c749SSergey Zigachev 			DRM_DEBUG_KMS("LCD1 disconnected\n");
1939b843c749SSergey Zigachev 			bios_0_scratch &= ~ATOM_S0_LCD1;
1940b843c749SSergey Zigachev 			bios_3_scratch &= ~ATOM_S3_LCD1_ACTIVE;
1941b843c749SSergey Zigachev 			bios_6_scratch &= ~ATOM_S6_ACC_REQ_LCD1;
1942b843c749SSergey Zigachev 		}
1943b843c749SSergey Zigachev 	}
1944b843c749SSergey Zigachev 	if ((amdgpu_encoder->devices & ATOM_DEVICE_CRT1_SUPPORT) &&
1945b843c749SSergey Zigachev 	    (amdgpu_connector->devices & ATOM_DEVICE_CRT1_SUPPORT)) {
1946b843c749SSergey Zigachev 		if (connected) {
1947b843c749SSergey Zigachev 			DRM_DEBUG_KMS("CRT1 connected\n");
1948b843c749SSergey Zigachev 			bios_0_scratch |= ATOM_S0_CRT1_COLOR;
1949b843c749SSergey Zigachev 			bios_3_scratch |= ATOM_S3_CRT1_ACTIVE;
1950b843c749SSergey Zigachev 			bios_6_scratch |= ATOM_S6_ACC_REQ_CRT1;
1951b843c749SSergey Zigachev 		} else {
1952b843c749SSergey Zigachev 			DRM_DEBUG_KMS("CRT1 disconnected\n");
1953b843c749SSergey Zigachev 			bios_0_scratch &= ~ATOM_S0_CRT1_MASK;
1954b843c749SSergey Zigachev 			bios_3_scratch &= ~ATOM_S3_CRT1_ACTIVE;
1955b843c749SSergey Zigachev 			bios_6_scratch &= ~ATOM_S6_ACC_REQ_CRT1;
1956b843c749SSergey Zigachev 		}
1957b843c749SSergey Zigachev 	}
1958b843c749SSergey Zigachev 	if ((amdgpu_encoder->devices & ATOM_DEVICE_CRT2_SUPPORT) &&
1959b843c749SSergey Zigachev 	    (amdgpu_connector->devices & ATOM_DEVICE_CRT2_SUPPORT)) {
1960b843c749SSergey Zigachev 		if (connected) {
1961b843c749SSergey Zigachev 			DRM_DEBUG_KMS("CRT2 connected\n");
1962b843c749SSergey Zigachev 			bios_0_scratch |= ATOM_S0_CRT2_COLOR;
1963b843c749SSergey Zigachev 			bios_3_scratch |= ATOM_S3_CRT2_ACTIVE;
1964b843c749SSergey Zigachev 			bios_6_scratch |= ATOM_S6_ACC_REQ_CRT2;
1965b843c749SSergey Zigachev 		} else {
1966b843c749SSergey Zigachev 			DRM_DEBUG_KMS("CRT2 disconnected\n");
1967b843c749SSergey Zigachev 			bios_0_scratch &= ~ATOM_S0_CRT2_MASK;
1968b843c749SSergey Zigachev 			bios_3_scratch &= ~ATOM_S3_CRT2_ACTIVE;
1969b843c749SSergey Zigachev 			bios_6_scratch &= ~ATOM_S6_ACC_REQ_CRT2;
1970b843c749SSergey Zigachev 		}
1971b843c749SSergey Zigachev 	}
1972b843c749SSergey Zigachev 	if ((amdgpu_encoder->devices & ATOM_DEVICE_DFP1_SUPPORT) &&
1973b843c749SSergey Zigachev 	    (amdgpu_connector->devices & ATOM_DEVICE_DFP1_SUPPORT)) {
1974b843c749SSergey Zigachev 		if (connected) {
1975b843c749SSergey Zigachev 			DRM_DEBUG_KMS("DFP1 connected\n");
1976b843c749SSergey Zigachev 			bios_0_scratch |= ATOM_S0_DFP1;
1977b843c749SSergey Zigachev 			bios_3_scratch |= ATOM_S3_DFP1_ACTIVE;
1978b843c749SSergey Zigachev 			bios_6_scratch |= ATOM_S6_ACC_REQ_DFP1;
1979b843c749SSergey Zigachev 		} else {
1980b843c749SSergey Zigachev 			DRM_DEBUG_KMS("DFP1 disconnected\n");
1981b843c749SSergey Zigachev 			bios_0_scratch &= ~ATOM_S0_DFP1;
1982b843c749SSergey Zigachev 			bios_3_scratch &= ~ATOM_S3_DFP1_ACTIVE;
1983b843c749SSergey Zigachev 			bios_6_scratch &= ~ATOM_S6_ACC_REQ_DFP1;
1984b843c749SSergey Zigachev 		}
1985b843c749SSergey Zigachev 	}
1986b843c749SSergey Zigachev 	if ((amdgpu_encoder->devices & ATOM_DEVICE_DFP2_SUPPORT) &&
1987b843c749SSergey Zigachev 	    (amdgpu_connector->devices & ATOM_DEVICE_DFP2_SUPPORT)) {
1988b843c749SSergey Zigachev 		if (connected) {
1989b843c749SSergey Zigachev 			DRM_DEBUG_KMS("DFP2 connected\n");
1990b843c749SSergey Zigachev 			bios_0_scratch |= ATOM_S0_DFP2;
1991b843c749SSergey Zigachev 			bios_3_scratch |= ATOM_S3_DFP2_ACTIVE;
1992b843c749SSergey Zigachev 			bios_6_scratch |= ATOM_S6_ACC_REQ_DFP2;
1993b843c749SSergey Zigachev 		} else {
1994b843c749SSergey Zigachev 			DRM_DEBUG_KMS("DFP2 disconnected\n");
1995b843c749SSergey Zigachev 			bios_0_scratch &= ~ATOM_S0_DFP2;
1996b843c749SSergey Zigachev 			bios_3_scratch &= ~ATOM_S3_DFP2_ACTIVE;
1997b843c749SSergey Zigachev 			bios_6_scratch &= ~ATOM_S6_ACC_REQ_DFP2;
1998b843c749SSergey Zigachev 		}
1999b843c749SSergey Zigachev 	}
2000b843c749SSergey Zigachev 	if ((amdgpu_encoder->devices & ATOM_DEVICE_DFP3_SUPPORT) &&
2001b843c749SSergey Zigachev 	    (amdgpu_connector->devices & ATOM_DEVICE_DFP3_SUPPORT)) {
2002b843c749SSergey Zigachev 		if (connected) {
2003b843c749SSergey Zigachev 			DRM_DEBUG_KMS("DFP3 connected\n");
2004b843c749SSergey Zigachev 			bios_0_scratch |= ATOM_S0_DFP3;
2005b843c749SSergey Zigachev 			bios_3_scratch |= ATOM_S3_DFP3_ACTIVE;
2006b843c749SSergey Zigachev 			bios_6_scratch |= ATOM_S6_ACC_REQ_DFP3;
2007b843c749SSergey Zigachev 		} else {
2008b843c749SSergey Zigachev 			DRM_DEBUG_KMS("DFP3 disconnected\n");
2009b843c749SSergey Zigachev 			bios_0_scratch &= ~ATOM_S0_DFP3;
2010b843c749SSergey Zigachev 			bios_3_scratch &= ~ATOM_S3_DFP3_ACTIVE;
2011b843c749SSergey Zigachev 			bios_6_scratch &= ~ATOM_S6_ACC_REQ_DFP3;
2012b843c749SSergey Zigachev 		}
2013b843c749SSergey Zigachev 	}
2014b843c749SSergey Zigachev 	if ((amdgpu_encoder->devices & ATOM_DEVICE_DFP4_SUPPORT) &&
2015b843c749SSergey Zigachev 	    (amdgpu_connector->devices & ATOM_DEVICE_DFP4_SUPPORT)) {
2016b843c749SSergey Zigachev 		if (connected) {
2017b843c749SSergey Zigachev 			DRM_DEBUG_KMS("DFP4 connected\n");
2018b843c749SSergey Zigachev 			bios_0_scratch |= ATOM_S0_DFP4;
2019b843c749SSergey Zigachev 			bios_3_scratch |= ATOM_S3_DFP4_ACTIVE;
2020b843c749SSergey Zigachev 			bios_6_scratch |= ATOM_S6_ACC_REQ_DFP4;
2021b843c749SSergey Zigachev 		} else {
2022b843c749SSergey Zigachev 			DRM_DEBUG_KMS("DFP4 disconnected\n");
2023b843c749SSergey Zigachev 			bios_0_scratch &= ~ATOM_S0_DFP4;
2024b843c749SSergey Zigachev 			bios_3_scratch &= ~ATOM_S3_DFP4_ACTIVE;
2025b843c749SSergey Zigachev 			bios_6_scratch &= ~ATOM_S6_ACC_REQ_DFP4;
2026b843c749SSergey Zigachev 		}
2027b843c749SSergey Zigachev 	}
2028b843c749SSergey Zigachev 	if ((amdgpu_encoder->devices & ATOM_DEVICE_DFP5_SUPPORT) &&
2029b843c749SSergey Zigachev 	    (amdgpu_connector->devices & ATOM_DEVICE_DFP5_SUPPORT)) {
2030b843c749SSergey Zigachev 		if (connected) {
2031b843c749SSergey Zigachev 			DRM_DEBUG_KMS("DFP5 connected\n");
2032b843c749SSergey Zigachev 			bios_0_scratch |= ATOM_S0_DFP5;
2033b843c749SSergey Zigachev 			bios_3_scratch |= ATOM_S3_DFP5_ACTIVE;
2034b843c749SSergey Zigachev 			bios_6_scratch |= ATOM_S6_ACC_REQ_DFP5;
2035b843c749SSergey Zigachev 		} else {
2036b843c749SSergey Zigachev 			DRM_DEBUG_KMS("DFP5 disconnected\n");
2037b843c749SSergey Zigachev 			bios_0_scratch &= ~ATOM_S0_DFP5;
2038b843c749SSergey Zigachev 			bios_3_scratch &= ~ATOM_S3_DFP5_ACTIVE;
2039b843c749SSergey Zigachev 			bios_6_scratch &= ~ATOM_S6_ACC_REQ_DFP5;
2040b843c749SSergey Zigachev 		}
2041b843c749SSergey Zigachev 	}
2042b843c749SSergey Zigachev 	if ((amdgpu_encoder->devices & ATOM_DEVICE_DFP6_SUPPORT) &&
2043b843c749SSergey Zigachev 	    (amdgpu_connector->devices & ATOM_DEVICE_DFP6_SUPPORT)) {
2044b843c749SSergey Zigachev 		if (connected) {
2045b843c749SSergey Zigachev 			DRM_DEBUG_KMS("DFP6 connected\n");
2046b843c749SSergey Zigachev 			bios_0_scratch |= ATOM_S0_DFP6;
2047b843c749SSergey Zigachev 			bios_3_scratch |= ATOM_S3_DFP6_ACTIVE;
2048b843c749SSergey Zigachev 			bios_6_scratch |= ATOM_S6_ACC_REQ_DFP6;
2049b843c749SSergey Zigachev 		} else {
2050b843c749SSergey Zigachev 			DRM_DEBUG_KMS("DFP6 disconnected\n");
2051b843c749SSergey Zigachev 			bios_0_scratch &= ~ATOM_S0_DFP6;
2052b843c749SSergey Zigachev 			bios_3_scratch &= ~ATOM_S3_DFP6_ACTIVE;
2053b843c749SSergey Zigachev 			bios_6_scratch &= ~ATOM_S6_ACC_REQ_DFP6;
2054b843c749SSergey Zigachev 		}
2055b843c749SSergey Zigachev 	}
2056b843c749SSergey Zigachev 
2057b843c749SSergey Zigachev 	WREG32(mmBIOS_SCRATCH_0, bios_0_scratch);
2058b843c749SSergey Zigachev 	WREG32(mmBIOS_SCRATCH_3, bios_3_scratch);
2059b843c749SSergey Zigachev 	WREG32(mmBIOS_SCRATCH_6, bios_6_scratch);
2060b843c749SSergey Zigachev }
2061b843c749SSergey Zigachev 
2062b843c749SSergey Zigachev union lvds_info {
2063b843c749SSergey Zigachev 	struct _ATOM_LVDS_INFO info;
2064b843c749SSergey Zigachev 	struct _ATOM_LVDS_INFO_V12 info_12;
2065b843c749SSergey Zigachev };
2066b843c749SSergey Zigachev 
2067b843c749SSergey Zigachev struct amdgpu_encoder_atom_dig *
amdgpu_atombios_encoder_get_lcd_info(struct amdgpu_encoder * encoder)2068b843c749SSergey Zigachev amdgpu_atombios_encoder_get_lcd_info(struct amdgpu_encoder *encoder)
2069b843c749SSergey Zigachev {
2070b843c749SSergey Zigachev 	struct drm_device *dev = encoder->base.dev;
2071b843c749SSergey Zigachev 	struct amdgpu_device *adev = dev->dev_private;
2072b843c749SSergey Zigachev 	struct amdgpu_mode_info *mode_info = &adev->mode_info;
2073b843c749SSergey Zigachev 	int index = GetIndexIntoMasterTable(DATA, LVDS_Info);
2074b843c749SSergey Zigachev 	uint16_t data_offset, misc;
2075b843c749SSergey Zigachev 	union lvds_info *lvds_info;
2076b843c749SSergey Zigachev 	uint8_t frev, crev;
2077b843c749SSergey Zigachev 	struct amdgpu_encoder_atom_dig *lvds = NULL;
2078b843c749SSergey Zigachev 	int encoder_enum = (encoder->encoder_enum & ENUM_ID_MASK) >> ENUM_ID_SHIFT;
2079b843c749SSergey Zigachev 
2080b843c749SSergey Zigachev 	if (amdgpu_atom_parse_data_header(mode_info->atom_context, index, NULL,
2081b843c749SSergey Zigachev 				   &frev, &crev, &data_offset)) {
2082b843c749SSergey Zigachev 		lvds_info =
2083b843c749SSergey Zigachev 			(union lvds_info *)(mode_info->atom_context->bios + data_offset);
2084b843c749SSergey Zigachev 		lvds =
2085b843c749SSergey Zigachev 		    kzalloc(sizeof(struct amdgpu_encoder_atom_dig), GFP_KERNEL);
2086b843c749SSergey Zigachev 
2087b843c749SSergey Zigachev 		if (!lvds)
2088b843c749SSergey Zigachev 			return NULL;
2089b843c749SSergey Zigachev 
2090b843c749SSergey Zigachev 		lvds->native_mode.clock =
2091b843c749SSergey Zigachev 		    le16_to_cpu(lvds_info->info.sLCDTiming.usPixClk) * 10;
2092b843c749SSergey Zigachev 		lvds->native_mode.hdisplay =
2093b843c749SSergey Zigachev 		    le16_to_cpu(lvds_info->info.sLCDTiming.usHActive);
2094b843c749SSergey Zigachev 		lvds->native_mode.vdisplay =
2095b843c749SSergey Zigachev 		    le16_to_cpu(lvds_info->info.sLCDTiming.usVActive);
2096b843c749SSergey Zigachev 		lvds->native_mode.htotal = lvds->native_mode.hdisplay +
2097b843c749SSergey Zigachev 			le16_to_cpu(lvds_info->info.sLCDTiming.usHBlanking_Time);
2098b843c749SSergey Zigachev 		lvds->native_mode.hsync_start = lvds->native_mode.hdisplay +
2099b843c749SSergey Zigachev 			le16_to_cpu(lvds_info->info.sLCDTiming.usHSyncOffset);
2100b843c749SSergey Zigachev 		lvds->native_mode.hsync_end = lvds->native_mode.hsync_start +
2101b843c749SSergey Zigachev 			le16_to_cpu(lvds_info->info.sLCDTiming.usHSyncWidth);
2102b843c749SSergey Zigachev 		lvds->native_mode.vtotal = lvds->native_mode.vdisplay +
2103b843c749SSergey Zigachev 			le16_to_cpu(lvds_info->info.sLCDTiming.usVBlanking_Time);
2104b843c749SSergey Zigachev 		lvds->native_mode.vsync_start = lvds->native_mode.vdisplay +
2105b843c749SSergey Zigachev 			le16_to_cpu(lvds_info->info.sLCDTiming.usVSyncOffset);
2106b843c749SSergey Zigachev 		lvds->native_mode.vsync_end = lvds->native_mode.vsync_start +
2107b843c749SSergey Zigachev 			le16_to_cpu(lvds_info->info.sLCDTiming.usVSyncWidth);
2108b843c749SSergey Zigachev 		lvds->panel_pwr_delay =
2109b843c749SSergey Zigachev 		    le16_to_cpu(lvds_info->info.usOffDelayInMs);
2110b843c749SSergey Zigachev 		lvds->lcd_misc = lvds_info->info.ucLVDS_Misc;
2111b843c749SSergey Zigachev 
2112b843c749SSergey Zigachev 		misc = le16_to_cpu(lvds_info->info.sLCDTiming.susModeMiscInfo.usAccess);
2113b843c749SSergey Zigachev 		if (misc & ATOM_VSYNC_POLARITY)
2114b843c749SSergey Zigachev 			lvds->native_mode.flags |= DRM_MODE_FLAG_NVSYNC;
2115b843c749SSergey Zigachev 		if (misc & ATOM_HSYNC_POLARITY)
2116b843c749SSergey Zigachev 			lvds->native_mode.flags |= DRM_MODE_FLAG_NHSYNC;
2117b843c749SSergey Zigachev 		if (misc & ATOM_COMPOSITESYNC)
2118b843c749SSergey Zigachev 			lvds->native_mode.flags |= DRM_MODE_FLAG_CSYNC;
2119b843c749SSergey Zigachev 		if (misc & ATOM_INTERLACE)
2120b843c749SSergey Zigachev 			lvds->native_mode.flags |= DRM_MODE_FLAG_INTERLACE;
2121b843c749SSergey Zigachev 		if (misc & ATOM_DOUBLE_CLOCK_MODE)
2122b843c749SSergey Zigachev 			lvds->native_mode.flags |= DRM_MODE_FLAG_DBLSCAN;
2123b843c749SSergey Zigachev 
2124b843c749SSergey Zigachev 		lvds->native_mode.width_mm = le16_to_cpu(lvds_info->info.sLCDTiming.usImageHSize);
2125b843c749SSergey Zigachev 		lvds->native_mode.height_mm = le16_to_cpu(lvds_info->info.sLCDTiming.usImageVSize);
2126b843c749SSergey Zigachev 
2127b843c749SSergey Zigachev 		/* set crtc values */
2128b843c749SSergey Zigachev 		drm_mode_set_crtcinfo(&lvds->native_mode, CRTC_INTERLACE_HALVE_V);
2129b843c749SSergey Zigachev 
2130b843c749SSergey Zigachev 		lvds->lcd_ss_id = lvds_info->info.ucSS_Id;
2131b843c749SSergey Zigachev 
2132b843c749SSergey Zigachev 		encoder->native_mode = lvds->native_mode;
2133b843c749SSergey Zigachev 
2134b843c749SSergey Zigachev 		if (encoder_enum == 2)
2135b843c749SSergey Zigachev 			lvds->linkb = true;
2136b843c749SSergey Zigachev 		else
2137b843c749SSergey Zigachev 			lvds->linkb = false;
2138b843c749SSergey Zigachev 
2139b843c749SSergey Zigachev 		/* parse the lcd record table */
2140b843c749SSergey Zigachev 		if (le16_to_cpu(lvds_info->info.usModePatchTableOffset)) {
2141b843c749SSergey Zigachev 			ATOM_FAKE_EDID_PATCH_RECORD *fake_edid_record;
2142b843c749SSergey Zigachev 			ATOM_PANEL_RESOLUTION_PATCH_RECORD *panel_res_record;
2143b843c749SSergey Zigachev 			bool bad_record = false;
2144b843c749SSergey Zigachev 			u8 *record;
2145b843c749SSergey Zigachev 
2146b843c749SSergey Zigachev 			if ((frev == 1) && (crev < 2))
2147b843c749SSergey Zigachev 				/* absolute */
2148b843c749SSergey Zigachev 				record = (u8 *)(mode_info->atom_context->bios +
2149b843c749SSergey Zigachev 						le16_to_cpu(lvds_info->info.usModePatchTableOffset));
2150b843c749SSergey Zigachev 			else
2151b843c749SSergey Zigachev 				/* relative */
2152b843c749SSergey Zigachev 				record = (u8 *)(mode_info->atom_context->bios +
2153b843c749SSergey Zigachev 						data_offset +
2154b843c749SSergey Zigachev 						le16_to_cpu(lvds_info->info.usModePatchTableOffset));
2155b843c749SSergey Zigachev 			while (*record != ATOM_RECORD_END_TYPE) {
2156b843c749SSergey Zigachev 				switch (*record) {
2157b843c749SSergey Zigachev 				case LCD_MODE_PATCH_RECORD_MODE_TYPE:
2158b843c749SSergey Zigachev 					record += sizeof(ATOM_PATCH_RECORD_MODE);
2159b843c749SSergey Zigachev 					break;
2160b843c749SSergey Zigachev 				case LCD_RTS_RECORD_TYPE:
2161b843c749SSergey Zigachev 					record += sizeof(ATOM_LCD_RTS_RECORD);
2162b843c749SSergey Zigachev 					break;
2163b843c749SSergey Zigachev 				case LCD_CAP_RECORD_TYPE:
2164b843c749SSergey Zigachev 					record += sizeof(ATOM_LCD_MODE_CONTROL_CAP);
2165b843c749SSergey Zigachev 					break;
2166b843c749SSergey Zigachev 				case LCD_FAKE_EDID_PATCH_RECORD_TYPE:
2167b843c749SSergey Zigachev 					fake_edid_record = (ATOM_FAKE_EDID_PATCH_RECORD *)record;
2168b843c749SSergey Zigachev 					if (fake_edid_record->ucFakeEDIDLength) {
2169b843c749SSergey Zigachev 						struct edid *edid;
2170b843c749SSergey Zigachev 						int edid_size =
2171b843c749SSergey Zigachev 							max((int)EDID_LENGTH, (int)fake_edid_record->ucFakeEDIDLength);
2172*78973132SSergey Zigachev 						edid = kmalloc(edid_size, M_DRM, GFP_KERNEL);
2173b843c749SSergey Zigachev 						if (edid) {
2174b843c749SSergey Zigachev 							memcpy((u8 *)edid, (u8 *)&fake_edid_record->ucFakeEDIDString[0],
2175b843c749SSergey Zigachev 							       fake_edid_record->ucFakeEDIDLength);
2176b843c749SSergey Zigachev 
2177b843c749SSergey Zigachev 							if (drm_edid_is_valid(edid)) {
2178b843c749SSergey Zigachev 								adev->mode_info.bios_hardcoded_edid = edid;
2179b843c749SSergey Zigachev 								adev->mode_info.bios_hardcoded_edid_size = edid_size;
2180b843c749SSergey Zigachev 							} else
2181b843c749SSergey Zigachev 								kfree(edid);
2182b843c749SSergey Zigachev 						}
2183b843c749SSergey Zigachev 					}
2184b843c749SSergey Zigachev 					record += fake_edid_record->ucFakeEDIDLength ?
2185b843c749SSergey Zigachev 						fake_edid_record->ucFakeEDIDLength + 2 :
2186b843c749SSergey Zigachev 						sizeof(ATOM_FAKE_EDID_PATCH_RECORD);
2187b843c749SSergey Zigachev 					break;
2188b843c749SSergey Zigachev 				case LCD_PANEL_RESOLUTION_RECORD_TYPE:
2189b843c749SSergey Zigachev 					panel_res_record = (ATOM_PANEL_RESOLUTION_PATCH_RECORD *)record;
2190b843c749SSergey Zigachev 					lvds->native_mode.width_mm = panel_res_record->usHSize;
2191b843c749SSergey Zigachev 					lvds->native_mode.height_mm = panel_res_record->usVSize;
2192b843c749SSergey Zigachev 					record += sizeof(ATOM_PANEL_RESOLUTION_PATCH_RECORD);
2193b843c749SSergey Zigachev 					break;
2194b843c749SSergey Zigachev 				default:
2195b843c749SSergey Zigachev 					DRM_ERROR("Bad LCD record %d\n", *record);
2196b843c749SSergey Zigachev 					bad_record = true;
2197b843c749SSergey Zigachev 					break;
2198b843c749SSergey Zigachev 				}
2199b843c749SSergey Zigachev 				if (bad_record)
2200b843c749SSergey Zigachev 					break;
2201b843c749SSergey Zigachev 			}
2202b843c749SSergey Zigachev 		}
2203b843c749SSergey Zigachev 	}
2204b843c749SSergey Zigachev 	return lvds;
2205b843c749SSergey Zigachev }
2206b843c749SSergey Zigachev 
2207b843c749SSergey Zigachev struct amdgpu_encoder_atom_dig *
amdgpu_atombios_encoder_get_dig_info(struct amdgpu_encoder * amdgpu_encoder)2208b843c749SSergey Zigachev amdgpu_atombios_encoder_get_dig_info(struct amdgpu_encoder *amdgpu_encoder)
2209b843c749SSergey Zigachev {
2210b843c749SSergey Zigachev 	int encoder_enum = (amdgpu_encoder->encoder_enum & ENUM_ID_MASK) >> ENUM_ID_SHIFT;
2211b843c749SSergey Zigachev 	struct amdgpu_encoder_atom_dig *dig = kzalloc(sizeof(struct amdgpu_encoder_atom_dig), GFP_KERNEL);
2212b843c749SSergey Zigachev 
2213b843c749SSergey Zigachev 	if (!dig)
2214b843c749SSergey Zigachev 		return NULL;
2215b843c749SSergey Zigachev 
2216b843c749SSergey Zigachev 	/* coherent mode by default */
2217b843c749SSergey Zigachev 	dig->coherent_mode = true;
2218b843c749SSergey Zigachev 	dig->dig_encoder = -1;
2219b843c749SSergey Zigachev 
2220b843c749SSergey Zigachev 	if (encoder_enum == 2)
2221b843c749SSergey Zigachev 		dig->linkb = true;
2222b843c749SSergey Zigachev 	else
2223b843c749SSergey Zigachev 		dig->linkb = false;
2224b843c749SSergey Zigachev 
2225b843c749SSergey Zigachev 	return dig;
2226b843c749SSergey Zigachev }
2227b843c749SSergey Zigachev 
2228