drm/radeon: Sync to Linux 3.11
[dragonfly.git] / sys / dev / drm / radeon / radeon_encoders.c
CommitLineData
926deccb
FT
1/*
2 * Copyright 2007-8 Advanced Micro Devices, Inc.
3 * Copyright 2008 Red Hat Inc.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * Authors: Dave Airlie
24 * Alex Deucher
926deccb 25 */
926deccb
FT
26#include <drm/drmP.h>
27#include <drm/drm_crtc_helper.h>
28#include <uapi_drm/radeon_drm.h>
29#include "radeon.h"
30#include "atom.h"
31
32
33static uint32_t radeon_encoder_clones(struct drm_encoder *encoder)
34{
35 struct drm_device *dev = encoder->dev;
36 struct radeon_device *rdev = dev->dev_private;
37 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
38 struct drm_encoder *clone_encoder;
39 uint32_t index_mask = 0;
40 int count;
41
42 /* DIG routing gets problematic */
43 if (rdev->family >= CHIP_R600)
44 return index_mask;
45 /* LVDS/TV are too wacky */
46 if (radeon_encoder->devices & ATOM_DEVICE_LCD_SUPPORT)
47 return index_mask;
48 /* DVO requires 2x ppll clocks depending on tmds chip */
49 if (radeon_encoder->devices & ATOM_DEVICE_DFP2_SUPPORT)
50 return index_mask;
51
52 count = -1;
53 list_for_each_entry(clone_encoder, &dev->mode_config.encoder_list, head) {
54 struct radeon_encoder *radeon_clone = to_radeon_encoder(clone_encoder);
55 count++;
56
57 if (clone_encoder == encoder)
58 continue;
59 if (radeon_clone->devices & (ATOM_DEVICE_LCD_SUPPORT))
60 continue;
61 if (radeon_clone->devices & ATOM_DEVICE_DFP2_SUPPORT)
62 continue;
63 else
64 index_mask |= (1 << count);
65 }
66 return index_mask;
67}
68
69void radeon_setup_encoder_clones(struct drm_device *dev)
70{
71 struct drm_encoder *encoder;
72
73 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
74 encoder->possible_clones = radeon_encoder_clones(encoder);
75 }
76}
77
78uint32_t
79radeon_get_encoder_enum(struct drm_device *dev, uint32_t supported_device, uint8_t dac)
80{
81 struct radeon_device *rdev = dev->dev_private;
82 uint32_t ret = 0;
83
84 switch (supported_device) {
85 case ATOM_DEVICE_CRT1_SUPPORT:
86 case ATOM_DEVICE_TV1_SUPPORT:
87 case ATOM_DEVICE_TV2_SUPPORT:
88 case ATOM_DEVICE_CRT2_SUPPORT:
89 case ATOM_DEVICE_CV_SUPPORT:
90 switch (dac) {
91 case 1: /* dac a */
92 if ((rdev->family == CHIP_RS300) ||
93 (rdev->family == CHIP_RS400) ||
94 (rdev->family == CHIP_RS480))
95 ret = ENCODER_INTERNAL_DAC2_ENUM_ID1;
96 else if (ASIC_IS_AVIVO(rdev))
97 ret = ENCODER_INTERNAL_KLDSCP_DAC1_ENUM_ID1;
98 else
99 ret = ENCODER_INTERNAL_DAC1_ENUM_ID1;
100 break;
101 case 2: /* dac b */
102 if (ASIC_IS_AVIVO(rdev))
103 ret = ENCODER_INTERNAL_KLDSCP_DAC2_ENUM_ID1;
104 else {
105 /*if (rdev->family == CHIP_R200)
106 ret = ENCODER_INTERNAL_DVO1_ENUM_ID1;
107 else*/
108 ret = ENCODER_INTERNAL_DAC2_ENUM_ID1;
109 }
110 break;
111 case 3: /* external dac */
112 if (ASIC_IS_AVIVO(rdev))
113 ret = ENCODER_INTERNAL_KLDSCP_DVO1_ENUM_ID1;
114 else
115 ret = ENCODER_INTERNAL_DVO1_ENUM_ID1;
116 break;
117 }
118 break;
119 case ATOM_DEVICE_LCD1_SUPPORT:
120 if (ASIC_IS_AVIVO(rdev))
121 ret = ENCODER_INTERNAL_LVTM1_ENUM_ID1;
122 else
123 ret = ENCODER_INTERNAL_LVDS_ENUM_ID1;
124 break;
125 case ATOM_DEVICE_DFP1_SUPPORT:
126 if ((rdev->family == CHIP_RS300) ||
127 (rdev->family == CHIP_RS400) ||
128 (rdev->family == CHIP_RS480))
129 ret = ENCODER_INTERNAL_DVO1_ENUM_ID1;
130 else if (ASIC_IS_AVIVO(rdev))
131 ret = ENCODER_INTERNAL_KLDSCP_TMDS1_ENUM_ID1;
132 else
133 ret = ENCODER_INTERNAL_TMDS1_ENUM_ID1;
134 break;
135 case ATOM_DEVICE_LCD2_SUPPORT:
136 case ATOM_DEVICE_DFP2_SUPPORT:
137 if ((rdev->family == CHIP_RS600) ||
138 (rdev->family == CHIP_RS690) ||
139 (rdev->family == CHIP_RS740))
140 ret = ENCODER_INTERNAL_DDI_ENUM_ID1;
141 else if (ASIC_IS_AVIVO(rdev))
142 ret = ENCODER_INTERNAL_KLDSCP_DVO1_ENUM_ID1;
143 else
144 ret = ENCODER_INTERNAL_DVO1_ENUM_ID1;
145 break;
146 case ATOM_DEVICE_DFP3_SUPPORT:
147 ret = ENCODER_INTERNAL_LVTM1_ENUM_ID1;
148 break;
149 }
150
151 return ret;
152}
153
154void
155radeon_link_encoder_connector(struct drm_device *dev)
156{
157 struct radeon_device *rdev = dev->dev_private;
158 struct drm_connector *connector;
159 struct radeon_connector *radeon_connector;
160 struct drm_encoder *encoder;
161 struct radeon_encoder *radeon_encoder;
162
163 /* walk the list and link encoders to connectors */
164 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
165 radeon_connector = to_radeon_connector(connector);
166 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
167 radeon_encoder = to_radeon_encoder(encoder);
168 if (radeon_encoder->devices & radeon_connector->devices) {
169 drm_mode_connector_attach_encoder(connector, encoder);
170 if (radeon_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT)) {
171 if (rdev->is_atom_bios)
172 radeon_atom_backlight_init(radeon_encoder, connector);
173 else
174 radeon_legacy_backlight_init(radeon_encoder, connector);
175 rdev->mode_info.bl_encoder = radeon_encoder;
176 }
177 }
178 }
179 }
180}
181
182void radeon_encoder_set_active_device(struct drm_encoder *encoder)
183{
184 struct drm_device *dev = encoder->dev;
185 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
186 struct drm_connector *connector;
187
188 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
189 if (connector->encoder == encoder) {
190 struct radeon_connector *radeon_connector = to_radeon_connector(connector);
191 radeon_encoder->active_device = radeon_encoder->devices & radeon_connector->devices;
192 DRM_DEBUG_KMS("setting active device to %08x from %08x %08x for encoder %d\n",
193 radeon_encoder->active_device, radeon_encoder->devices,
194 radeon_connector->devices, encoder->encoder_type);
195 }
196 }
197}
198
199struct drm_connector *
200radeon_get_connector_for_encoder(struct drm_encoder *encoder)
201{
202 struct drm_device *dev = encoder->dev;
203 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
204 struct drm_connector *connector;
205 struct radeon_connector *radeon_connector;
206
207 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
208 radeon_connector = to_radeon_connector(connector);
209 if (radeon_encoder->active_device & radeon_connector->devices)
210 return connector;
211 }
212 return NULL;
213}
214
215struct drm_connector *
216radeon_get_connector_for_encoder_init(struct drm_encoder *encoder)
217{
218 struct drm_device *dev = encoder->dev;
219 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
220 struct drm_connector *connector;
221 struct radeon_connector *radeon_connector;
222
223 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
224 radeon_connector = to_radeon_connector(connector);
225 if (radeon_encoder->devices & radeon_connector->devices)
226 return connector;
227 }
228 return NULL;
229}
230
231struct drm_encoder *radeon_get_external_encoder(struct drm_encoder *encoder)
232{
233 struct drm_device *dev = encoder->dev;
234 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
235 struct drm_encoder *other_encoder;
236 struct radeon_encoder *other_radeon_encoder;
237
238 if (radeon_encoder->is_ext_encoder)
239 return NULL;
240
241 list_for_each_entry(other_encoder, &dev->mode_config.encoder_list, head) {
242 if (other_encoder == encoder)
243 continue;
244 other_radeon_encoder = to_radeon_encoder(other_encoder);
245 if (other_radeon_encoder->is_ext_encoder &&
246 (radeon_encoder->devices & other_radeon_encoder->devices))
247 return other_encoder;
248 }
249 return NULL;
250}
251
252u16 radeon_encoder_get_dp_bridge_encoder_id(struct drm_encoder *encoder)
253{
254 struct drm_encoder *other_encoder = radeon_get_external_encoder(encoder);
255
256 if (other_encoder) {
257 struct radeon_encoder *radeon_encoder = to_radeon_encoder(other_encoder);
258
259 switch (radeon_encoder->encoder_id) {
260 case ENCODER_OBJECT_ID_TRAVIS:
261 case ENCODER_OBJECT_ID_NUTMEG:
262 return radeon_encoder->encoder_id;
263 default:
264 return ENCODER_OBJECT_ID_NONE;
265 }
266 }
267 return ENCODER_OBJECT_ID_NONE;
268}
269
270void radeon_panel_mode_fixup(struct drm_encoder *encoder,
271 struct drm_display_mode *adjusted_mode)
272{
273 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
274 struct drm_device *dev = encoder->dev;
275 struct radeon_device *rdev = dev->dev_private;
276 struct drm_display_mode *native_mode = &radeon_encoder->native_mode;
277 unsigned hblank = native_mode->htotal - native_mode->hdisplay;
278 unsigned vblank = native_mode->vtotal - native_mode->vdisplay;
279 unsigned hover = native_mode->hsync_start - native_mode->hdisplay;
280 unsigned vover = native_mode->vsync_start - native_mode->vdisplay;
281 unsigned hsync_width = native_mode->hsync_end - native_mode->hsync_start;
282 unsigned vsync_width = native_mode->vsync_end - native_mode->vsync_start;
283
284 adjusted_mode->clock = native_mode->clock;
285 adjusted_mode->flags = native_mode->flags;
286
287 if (ASIC_IS_AVIVO(rdev)) {
288 adjusted_mode->hdisplay = native_mode->hdisplay;
289 adjusted_mode->vdisplay = native_mode->vdisplay;
290 }
291
292 adjusted_mode->htotal = native_mode->hdisplay + hblank;
293 adjusted_mode->hsync_start = native_mode->hdisplay + hover;
294 adjusted_mode->hsync_end = adjusted_mode->hsync_start + hsync_width;
295
296 adjusted_mode->vtotal = native_mode->vdisplay + vblank;
297 adjusted_mode->vsync_start = native_mode->vdisplay + vover;
298 adjusted_mode->vsync_end = adjusted_mode->vsync_start + vsync_width;
299
300 drm_mode_set_crtcinfo(adjusted_mode, CRTC_INTERLACE_HALVE_V);
301
302 if (ASIC_IS_AVIVO(rdev)) {
303 adjusted_mode->crtc_hdisplay = native_mode->hdisplay;
304 adjusted_mode->crtc_vdisplay = native_mode->vdisplay;
305 }
306
307 adjusted_mode->crtc_htotal = adjusted_mode->crtc_hdisplay + hblank;
308 adjusted_mode->crtc_hsync_start = adjusted_mode->crtc_hdisplay + hover;
309 adjusted_mode->crtc_hsync_end = adjusted_mode->crtc_hsync_start + hsync_width;
310
311 adjusted_mode->crtc_vtotal = adjusted_mode->crtc_vdisplay + vblank;
312 adjusted_mode->crtc_vsync_start = adjusted_mode->crtc_vdisplay + vover;
313 adjusted_mode->crtc_vsync_end = adjusted_mode->crtc_vsync_start + vsync_width;
314
315}
316
317bool radeon_dig_monitor_is_duallink(struct drm_encoder *encoder,
318 u32 pixel_clock)
319{
320 struct drm_device *dev = encoder->dev;
321 struct radeon_device *rdev = dev->dev_private;
322 struct drm_connector *connector;
323 struct radeon_connector *radeon_connector;
324 struct radeon_connector_atom_dig *dig_connector;
325
326 connector = radeon_get_connector_for_encoder(encoder);
327 /* if we don't have an active device yet, just use one of
328 * the connectors tied to the encoder.
329 */
330 if (!connector)
331 connector = radeon_get_connector_for_encoder_init(encoder);
332 radeon_connector = to_radeon_connector(connector);
333
334 switch (connector->connector_type) {
335 case DRM_MODE_CONNECTOR_DVII:
336 case DRM_MODE_CONNECTOR_HDMIB:
337 if (radeon_connector->use_digital) {
338 /* HDMI 1.3 supports up to 340 Mhz over single link */
339 if (ASIC_IS_DCE6(rdev) && drm_detect_hdmi_monitor(radeon_connector->edid)) {
340 if (pixel_clock > 340000)
341 return true;
342 else
343 return false;
344 } else {
345 if (pixel_clock > 165000)
346 return true;
347 else
348 return false;
349 }
350 } else
351 return false;
352 case DRM_MODE_CONNECTOR_DVID:
353 case DRM_MODE_CONNECTOR_HDMIA:
354 case DRM_MODE_CONNECTOR_DisplayPort:
355 dig_connector = radeon_connector->con_priv;
356 if ((dig_connector->dp_sink_type == CONNECTOR_OBJECT_ID_DISPLAYPORT) ||
357 (dig_connector->dp_sink_type == CONNECTOR_OBJECT_ID_eDP))
358 return false;
359 else {
360 /* HDMI 1.3 supports up to 340 Mhz over single link */
361 if (ASIC_IS_DCE6(rdev) && drm_detect_hdmi_monitor(radeon_connector->edid)) {
362 if (pixel_clock > 340000)
363 return true;
364 else
365 return false;
366 } else {
367 if (pixel_clock > 165000)
368 return true;
369 else
370 return false;
371 }
372 }
373 default:
374 return false;
375 }
376}
377