1b843c749SSergey Zigachev /*
2b843c749SSergey Zigachev  * Copyright 2007-8 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 
34b843c749SSergey Zigachev void
amdgpu_link_encoder_connector(struct drm_device * dev)35b843c749SSergey Zigachev amdgpu_link_encoder_connector(struct drm_device *dev)
36b843c749SSergey Zigachev {
37b843c749SSergey Zigachev 	struct amdgpu_device *adev = dev->dev_private;
38b843c749SSergey Zigachev 	struct drm_connector *connector;
39b843c749SSergey Zigachev 	struct amdgpu_connector *amdgpu_connector;
40b843c749SSergey Zigachev 	struct drm_encoder *encoder;
41b843c749SSergey Zigachev 	struct amdgpu_encoder *amdgpu_encoder;
42b843c749SSergey Zigachev 
43b843c749SSergey Zigachev 	/* walk the list and link encoders to connectors */
44b843c749SSergey Zigachev 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
45b843c749SSergey Zigachev 		amdgpu_connector = to_amdgpu_connector(connector);
46b843c749SSergey Zigachev 		list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
47b843c749SSergey Zigachev 			amdgpu_encoder = to_amdgpu_encoder(encoder);
48b843c749SSergey Zigachev 			if (amdgpu_encoder->devices & amdgpu_connector->devices) {
49*78973132SSergey Zigachev 				drm_mode_connector_attach_encoder(connector, encoder);
50b843c749SSergey Zigachev 				if (amdgpu_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT)) {
51b843c749SSergey Zigachev 					amdgpu_atombios_encoder_init_backlight(amdgpu_encoder, connector);
52b843c749SSergey Zigachev 					adev->mode_info.bl_encoder = amdgpu_encoder;
53b843c749SSergey Zigachev 				}
54b843c749SSergey Zigachev 			}
55b843c749SSergey Zigachev 		}
56b843c749SSergey Zigachev 	}
57b843c749SSergey Zigachev }
58b843c749SSergey Zigachev 
amdgpu_encoder_set_active_device(struct drm_encoder * encoder)59b843c749SSergey Zigachev void amdgpu_encoder_set_active_device(struct drm_encoder *encoder)
60b843c749SSergey Zigachev {
61b843c749SSergey Zigachev 	struct drm_device *dev = encoder->dev;
62b843c749SSergey Zigachev 	struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
63b843c749SSergey Zigachev 	struct drm_connector *connector;
64b843c749SSergey Zigachev 
65b843c749SSergey Zigachev 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
66b843c749SSergey Zigachev 		if (connector->encoder == encoder) {
67b843c749SSergey Zigachev 			struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
68b843c749SSergey Zigachev 			amdgpu_encoder->active_device = amdgpu_encoder->devices & amdgpu_connector->devices;
69b843c749SSergey Zigachev 			DRM_DEBUG_KMS("setting active device to %08x from %08x %08x for encoder %d\n",
70b843c749SSergey Zigachev 				  amdgpu_encoder->active_device, amdgpu_encoder->devices,
71b843c749SSergey Zigachev 				  amdgpu_connector->devices, encoder->encoder_type);
72b843c749SSergey Zigachev 		}
73b843c749SSergey Zigachev 	}
74b843c749SSergey Zigachev }
75b843c749SSergey Zigachev 
76b843c749SSergey Zigachev struct drm_connector *
amdgpu_get_connector_for_encoder(struct drm_encoder * encoder)77b843c749SSergey Zigachev amdgpu_get_connector_for_encoder(struct drm_encoder *encoder)
78b843c749SSergey Zigachev {
79b843c749SSergey Zigachev 	struct drm_device *dev = encoder->dev;
80b843c749SSergey Zigachev 	struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
81b843c749SSergey Zigachev 	struct drm_connector *connector;
82b843c749SSergey Zigachev 	struct amdgpu_connector *amdgpu_connector;
83b843c749SSergey Zigachev 
84b843c749SSergey Zigachev 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
85b843c749SSergey Zigachev 		amdgpu_connector = to_amdgpu_connector(connector);
86b843c749SSergey Zigachev 		if (amdgpu_encoder->active_device & amdgpu_connector->devices)
87b843c749SSergey Zigachev 			return connector;
88b843c749SSergey Zigachev 	}
89b843c749SSergey Zigachev 	return NULL;
90b843c749SSergey Zigachev }
91b843c749SSergey Zigachev 
92b843c749SSergey Zigachev struct drm_connector *
amdgpu_get_connector_for_encoder_init(struct drm_encoder * encoder)93b843c749SSergey Zigachev amdgpu_get_connector_for_encoder_init(struct drm_encoder *encoder)
94b843c749SSergey Zigachev {
95b843c749SSergey Zigachev 	struct drm_device *dev = encoder->dev;
96b843c749SSergey Zigachev 	struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
97b843c749SSergey Zigachev 	struct drm_connector *connector;
98b843c749SSergey Zigachev 	struct amdgpu_connector *amdgpu_connector;
99b843c749SSergey Zigachev 
100b843c749SSergey Zigachev 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
101b843c749SSergey Zigachev 		amdgpu_connector = to_amdgpu_connector(connector);
102b843c749SSergey Zigachev 		if (amdgpu_encoder->devices & amdgpu_connector->devices)
103b843c749SSergey Zigachev 			return connector;
104b843c749SSergey Zigachev 	}
105b843c749SSergey Zigachev 	return NULL;
106b843c749SSergey Zigachev }
107b843c749SSergey Zigachev 
amdgpu_get_external_encoder(struct drm_encoder * encoder)108b843c749SSergey Zigachev struct drm_encoder *amdgpu_get_external_encoder(struct drm_encoder *encoder)
109b843c749SSergey Zigachev {
110b843c749SSergey Zigachev 	struct drm_device *dev = encoder->dev;
111b843c749SSergey Zigachev 	struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
112b843c749SSergey Zigachev 	struct drm_encoder *other_encoder;
113b843c749SSergey Zigachev 	struct amdgpu_encoder *other_amdgpu_encoder;
114b843c749SSergey Zigachev 
115b843c749SSergey Zigachev 	if (amdgpu_encoder->is_ext_encoder)
116b843c749SSergey Zigachev 		return NULL;
117b843c749SSergey Zigachev 
118b843c749SSergey Zigachev 	list_for_each_entry(other_encoder, &dev->mode_config.encoder_list, head) {
119b843c749SSergey Zigachev 		if (other_encoder == encoder)
120b843c749SSergey Zigachev 			continue;
121b843c749SSergey Zigachev 		other_amdgpu_encoder = to_amdgpu_encoder(other_encoder);
122b843c749SSergey Zigachev 		if (other_amdgpu_encoder->is_ext_encoder &&
123b843c749SSergey Zigachev 		    (amdgpu_encoder->devices & other_amdgpu_encoder->devices))
124b843c749SSergey Zigachev 			return other_encoder;
125b843c749SSergey Zigachev 	}
126b843c749SSergey Zigachev 	return NULL;
127b843c749SSergey Zigachev }
128b843c749SSergey Zigachev 
amdgpu_encoder_get_dp_bridge_encoder_id(struct drm_encoder * encoder)129b843c749SSergey Zigachev u16 amdgpu_encoder_get_dp_bridge_encoder_id(struct drm_encoder *encoder)
130b843c749SSergey Zigachev {
131b843c749SSergey Zigachev 	struct drm_encoder *other_encoder = amdgpu_get_external_encoder(encoder);
132b843c749SSergey Zigachev 
133b843c749SSergey Zigachev 	if (other_encoder) {
134b843c749SSergey Zigachev 		struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(other_encoder);
135b843c749SSergey Zigachev 
136b843c749SSergey Zigachev 		switch (amdgpu_encoder->encoder_id) {
137b843c749SSergey Zigachev 		case ENCODER_OBJECT_ID_TRAVIS:
138b843c749SSergey Zigachev 		case ENCODER_OBJECT_ID_NUTMEG:
139b843c749SSergey Zigachev 			return amdgpu_encoder->encoder_id;
140b843c749SSergey Zigachev 		default:
141b843c749SSergey Zigachev 			return ENCODER_OBJECT_ID_NONE;
142b843c749SSergey Zigachev 		}
143b843c749SSergey Zigachev 	}
144b843c749SSergey Zigachev 	return ENCODER_OBJECT_ID_NONE;
145b843c749SSergey Zigachev }
146b843c749SSergey Zigachev 
amdgpu_panel_mode_fixup(struct drm_encoder * encoder,struct drm_display_mode * adjusted_mode)147b843c749SSergey Zigachev void amdgpu_panel_mode_fixup(struct drm_encoder *encoder,
148b843c749SSergey Zigachev 			     struct drm_display_mode *adjusted_mode)
149b843c749SSergey Zigachev {
150b843c749SSergey Zigachev 	struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
151b843c749SSergey Zigachev 	struct drm_display_mode *native_mode = &amdgpu_encoder->native_mode;
152b843c749SSergey Zigachev 	unsigned hblank = native_mode->htotal - native_mode->hdisplay;
153b843c749SSergey Zigachev 	unsigned vblank = native_mode->vtotal - native_mode->vdisplay;
154b843c749SSergey Zigachev 	unsigned hover = native_mode->hsync_start - native_mode->hdisplay;
155b843c749SSergey Zigachev 	unsigned vover = native_mode->vsync_start - native_mode->vdisplay;
156b843c749SSergey Zigachev 	unsigned hsync_width = native_mode->hsync_end - native_mode->hsync_start;
157b843c749SSergey Zigachev 	unsigned vsync_width = native_mode->vsync_end - native_mode->vsync_start;
158b843c749SSergey Zigachev 
159b843c749SSergey Zigachev 	adjusted_mode->clock = native_mode->clock;
160b843c749SSergey Zigachev 	adjusted_mode->flags = native_mode->flags;
161b843c749SSergey Zigachev 
162b843c749SSergey Zigachev 	adjusted_mode->hdisplay = native_mode->hdisplay;
163b843c749SSergey Zigachev 	adjusted_mode->vdisplay = native_mode->vdisplay;
164b843c749SSergey Zigachev 
165b843c749SSergey Zigachev 	adjusted_mode->htotal = native_mode->hdisplay + hblank;
166b843c749SSergey Zigachev 	adjusted_mode->hsync_start = native_mode->hdisplay + hover;
167b843c749SSergey Zigachev 	adjusted_mode->hsync_end = adjusted_mode->hsync_start + hsync_width;
168b843c749SSergey Zigachev 
169b843c749SSergey Zigachev 	adjusted_mode->vtotal = native_mode->vdisplay + vblank;
170b843c749SSergey Zigachev 	adjusted_mode->vsync_start = native_mode->vdisplay + vover;
171b843c749SSergey Zigachev 	adjusted_mode->vsync_end = adjusted_mode->vsync_start + vsync_width;
172b843c749SSergey Zigachev 
173b843c749SSergey Zigachev 	drm_mode_set_crtcinfo(adjusted_mode, CRTC_INTERLACE_HALVE_V);
174b843c749SSergey Zigachev 
175b843c749SSergey Zigachev 	adjusted_mode->crtc_hdisplay = native_mode->hdisplay;
176b843c749SSergey Zigachev 	adjusted_mode->crtc_vdisplay = native_mode->vdisplay;
177b843c749SSergey Zigachev 
178b843c749SSergey Zigachev 	adjusted_mode->crtc_htotal = adjusted_mode->crtc_hdisplay + hblank;
179b843c749SSergey Zigachev 	adjusted_mode->crtc_hsync_start = adjusted_mode->crtc_hdisplay + hover;
180b843c749SSergey Zigachev 	adjusted_mode->crtc_hsync_end = adjusted_mode->crtc_hsync_start + hsync_width;
181b843c749SSergey Zigachev 
182b843c749SSergey Zigachev 	adjusted_mode->crtc_vtotal = adjusted_mode->crtc_vdisplay + vblank;
183b843c749SSergey Zigachev 	adjusted_mode->crtc_vsync_start = adjusted_mode->crtc_vdisplay + vover;
184b843c749SSergey Zigachev 	adjusted_mode->crtc_vsync_end = adjusted_mode->crtc_vsync_start + vsync_width;
185b843c749SSergey Zigachev 
186b843c749SSergey Zigachev }
187b843c749SSergey Zigachev 
amdgpu_dig_monitor_is_duallink(struct drm_encoder * encoder,u32 pixel_clock)188b843c749SSergey Zigachev bool amdgpu_dig_monitor_is_duallink(struct drm_encoder *encoder,
189b843c749SSergey Zigachev 				    u32 pixel_clock)
190b843c749SSergey Zigachev {
191b843c749SSergey Zigachev 	struct drm_connector *connector;
192b843c749SSergey Zigachev 	struct amdgpu_connector *amdgpu_connector;
193b843c749SSergey Zigachev 	struct amdgpu_connector_atom_dig *dig_connector;
194b843c749SSergey Zigachev 
195b843c749SSergey Zigachev 	connector = amdgpu_get_connector_for_encoder(encoder);
196b843c749SSergey Zigachev 	/* if we don't have an active device yet, just use one of
197b843c749SSergey Zigachev 	 * the connectors tied to the encoder.
198b843c749SSergey Zigachev 	 */
199b843c749SSergey Zigachev 	if (!connector)
200b843c749SSergey Zigachev 		connector = amdgpu_get_connector_for_encoder_init(encoder);
201b843c749SSergey Zigachev 	amdgpu_connector = to_amdgpu_connector(connector);
202b843c749SSergey Zigachev 
203b843c749SSergey Zigachev 	switch (connector->connector_type) {
204b843c749SSergey Zigachev 	case DRM_MODE_CONNECTOR_DVII:
205b843c749SSergey Zigachev 	case DRM_MODE_CONNECTOR_HDMIB:
206b843c749SSergey Zigachev 		if (amdgpu_connector->use_digital) {
207b843c749SSergey Zigachev 			/* HDMI 1.3 supports up to 340 Mhz over single link */
208b843c749SSergey Zigachev 			if (drm_detect_hdmi_monitor(amdgpu_connector_edid(connector))) {
209b843c749SSergey Zigachev 				if (pixel_clock > 340000)
210b843c749SSergey Zigachev 					return true;
211b843c749SSergey Zigachev 				else
212b843c749SSergey Zigachev 					return false;
213b843c749SSergey Zigachev 			} else {
214b843c749SSergey Zigachev 				if (pixel_clock > 165000)
215b843c749SSergey Zigachev 					return true;
216b843c749SSergey Zigachev 				else
217b843c749SSergey Zigachev 					return false;
218b843c749SSergey Zigachev 			}
219b843c749SSergey Zigachev 		} else
220b843c749SSergey Zigachev 			return false;
221b843c749SSergey Zigachev 	case DRM_MODE_CONNECTOR_DVID:
222b843c749SSergey Zigachev 	case DRM_MODE_CONNECTOR_HDMIA:
223b843c749SSergey Zigachev 	case DRM_MODE_CONNECTOR_DisplayPort:
224b843c749SSergey Zigachev 		dig_connector = amdgpu_connector->con_priv;
225b843c749SSergey Zigachev 		if ((dig_connector->dp_sink_type == CONNECTOR_OBJECT_ID_DISPLAYPORT) ||
226b843c749SSergey Zigachev 		    (dig_connector->dp_sink_type == CONNECTOR_OBJECT_ID_eDP))
227b843c749SSergey Zigachev 			return false;
228b843c749SSergey Zigachev 		else {
229b843c749SSergey Zigachev 			/* HDMI 1.3 supports up to 340 Mhz over single link */
230b843c749SSergey Zigachev 			if (drm_detect_hdmi_monitor(amdgpu_connector_edid(connector))) {
231b843c749SSergey Zigachev 				if (pixel_clock > 340000)
232b843c749SSergey Zigachev 					return true;
233b843c749SSergey Zigachev 				else
234b843c749SSergey Zigachev 					return false;
235b843c749SSergey Zigachev 			} else {
236b843c749SSergey Zigachev 				if (pixel_clock > 165000)
237b843c749SSergey Zigachev 					return true;
238b843c749SSergey Zigachev 				else
239b843c749SSergey Zigachev 					return false;
240b843c749SSergey Zigachev 			}
241b843c749SSergey Zigachev 		}
242b843c749SSergey Zigachev 	default:
243b843c749SSergey Zigachev 		return false;
244b843c749SSergey Zigachev 	}
245b843c749SSergey Zigachev }
246