blob: ff5f09953c0a3b78ee202f04c5230267470d4880 [file] [log] [blame]
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001/*
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
25 */
26#include "drmP.h"
27#include "radeon_drm.h"
28#include "radeon.h"
29
30#include "atom.h"
31#include <asm/div64.h>
32
33#include "drm_crtc_helper.h"
34#include "drm_edid.h"
35
36static int radeon_ddc_dump(struct drm_connector *connector);
37
38static void avivo_crtc_load_lut(struct drm_crtc *crtc)
39{
40 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
41 struct drm_device *dev = crtc->dev;
42 struct radeon_device *rdev = dev->dev_private;
43 int i;
44
45 DRM_DEBUG("%d\n", radeon_crtc->crtc_id);
46 WREG32(AVIVO_DC_LUTA_CONTROL + radeon_crtc->crtc_offset, 0);
47
48 WREG32(AVIVO_DC_LUTA_BLACK_OFFSET_BLUE + radeon_crtc->crtc_offset, 0);
49 WREG32(AVIVO_DC_LUTA_BLACK_OFFSET_GREEN + radeon_crtc->crtc_offset, 0);
50 WREG32(AVIVO_DC_LUTA_BLACK_OFFSET_RED + radeon_crtc->crtc_offset, 0);
51
52 WREG32(AVIVO_DC_LUTA_WHITE_OFFSET_BLUE + radeon_crtc->crtc_offset, 0xffff);
53 WREG32(AVIVO_DC_LUTA_WHITE_OFFSET_GREEN + radeon_crtc->crtc_offset, 0xffff);
54 WREG32(AVIVO_DC_LUTA_WHITE_OFFSET_RED + radeon_crtc->crtc_offset, 0xffff);
55
56 WREG32(AVIVO_DC_LUT_RW_SELECT, radeon_crtc->crtc_id);
57 WREG32(AVIVO_DC_LUT_RW_MODE, 0);
58 WREG32(AVIVO_DC_LUT_WRITE_EN_MASK, 0x0000003f);
59
60 WREG8(AVIVO_DC_LUT_RW_INDEX, 0);
61 for (i = 0; i < 256; i++) {
62 WREG32(AVIVO_DC_LUT_30_COLOR,
63 (radeon_crtc->lut_r[i] << 20) |
64 (radeon_crtc->lut_g[i] << 10) |
65 (radeon_crtc->lut_b[i] << 0));
66 }
67
68 WREG32(AVIVO_D1GRPH_LUT_SEL + radeon_crtc->crtc_offset, radeon_crtc->crtc_id);
69}
70
Alex Deucherbcc1c2a2010-01-12 17:54:34 -050071static void evergreen_crtc_load_lut(struct drm_crtc *crtc)
72{
73 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
74 struct drm_device *dev = crtc->dev;
75 struct radeon_device *rdev = dev->dev_private;
76 int i;
77
78 DRM_DEBUG("%d\n", radeon_crtc->crtc_id);
79 WREG32(EVERGREEN_DC_LUT_CONTROL + radeon_crtc->crtc_offset, 0);
80
81 WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_BLUE + radeon_crtc->crtc_offset, 0);
82 WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_GREEN + radeon_crtc->crtc_offset, 0);
83 WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_RED + radeon_crtc->crtc_offset, 0);
84
85 WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_BLUE + radeon_crtc->crtc_offset, 0xffff);
86 WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_GREEN + radeon_crtc->crtc_offset, 0xffff);
87 WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_RED + radeon_crtc->crtc_offset, 0xffff);
88
89 WREG32(EVERGREEN_DC_LUT_RW_MODE, radeon_crtc->crtc_id);
90 WREG32(EVERGREEN_DC_LUT_WRITE_EN_MASK, 0x00000007);
91
92 WREG32(EVERGREEN_DC_LUT_RW_INDEX, 0);
93 for (i = 0; i < 256; i++) {
94 WREG32(EVERGREEN_DC_LUT_30_COLOR,
95 (radeon_crtc->lut_r[i] << 20) |
96 (radeon_crtc->lut_g[i] << 10) |
97 (radeon_crtc->lut_b[i] << 0));
98 }
99}
100
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200101static void legacy_crtc_load_lut(struct drm_crtc *crtc)
102{
103 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
104 struct drm_device *dev = crtc->dev;
105 struct radeon_device *rdev = dev->dev_private;
106 int i;
107 uint32_t dac2_cntl;
108
109 dac2_cntl = RREG32(RADEON_DAC_CNTL2);
110 if (radeon_crtc->crtc_id == 0)
111 dac2_cntl &= (uint32_t)~RADEON_DAC2_PALETTE_ACC_CTL;
112 else
113 dac2_cntl |= RADEON_DAC2_PALETTE_ACC_CTL;
114 WREG32(RADEON_DAC_CNTL2, dac2_cntl);
115
116 WREG8(RADEON_PALETTE_INDEX, 0);
117 for (i = 0; i < 256; i++) {
118 WREG32(RADEON_PALETTE_30_DATA,
119 (radeon_crtc->lut_r[i] << 20) |
120 (radeon_crtc->lut_g[i] << 10) |
121 (radeon_crtc->lut_b[i] << 0));
122 }
123}
124
125void radeon_crtc_load_lut(struct drm_crtc *crtc)
126{
127 struct drm_device *dev = crtc->dev;
128 struct radeon_device *rdev = dev->dev_private;
129
130 if (!crtc->enabled)
131 return;
132
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500133 if (ASIC_IS_DCE4(rdev))
134 evergreen_crtc_load_lut(crtc);
135 else if (ASIC_IS_AVIVO(rdev))
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200136 avivo_crtc_load_lut(crtc);
137 else
138 legacy_crtc_load_lut(crtc);
139}
140
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000141/** Sets the color ramps on behalf of fbcon */
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200142void radeon_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
143 u16 blue, int regno)
144{
145 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
146
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200147 radeon_crtc->lut_r[regno] = red >> 6;
148 radeon_crtc->lut_g[regno] = green >> 6;
149 radeon_crtc->lut_b[regno] = blue >> 6;
150}
151
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000152/** Gets the color ramps on behalf of fbcon */
153void radeon_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
154 u16 *blue, int regno)
155{
156 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
157
158 *red = radeon_crtc->lut_r[regno] << 6;
159 *green = radeon_crtc->lut_g[regno] << 6;
160 *blue = radeon_crtc->lut_b[regno] << 6;
161}
162
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200163static void radeon_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
164 u16 *blue, uint32_t size)
165{
166 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000167 int i;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200168
169 if (size != 256) {
170 return;
171 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200172
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000173 /* userspace palettes are always correct as is */
174 for (i = 0; i < 256; i++) {
175 radeon_crtc->lut_r[i] = red[i] >> 6;
176 radeon_crtc->lut_g[i] = green[i] >> 6;
177 radeon_crtc->lut_b[i] = blue[i] >> 6;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200178 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200179 radeon_crtc_load_lut(crtc);
180}
181
182static void radeon_crtc_destroy(struct drm_crtc *crtc)
183{
184 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
185
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200186 drm_crtc_cleanup(crtc);
187 kfree(radeon_crtc);
188}
189
190static const struct drm_crtc_funcs radeon_crtc_funcs = {
191 .cursor_set = radeon_crtc_cursor_set,
192 .cursor_move = radeon_crtc_cursor_move,
193 .gamma_set = radeon_crtc_gamma_set,
194 .set_config = drm_crtc_helper_set_config,
195 .destroy = radeon_crtc_destroy,
196};
197
198static void radeon_crtc_init(struct drm_device *dev, int index)
199{
200 struct radeon_device *rdev = dev->dev_private;
201 struct radeon_crtc *radeon_crtc;
202 int i;
203
204 radeon_crtc = kzalloc(sizeof(struct radeon_crtc) + (RADEONFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
205 if (radeon_crtc == NULL)
206 return;
207
208 drm_crtc_init(dev, &radeon_crtc->base, &radeon_crtc_funcs);
209
210 drm_mode_crtc_set_gamma_size(&radeon_crtc->base, 256);
211 radeon_crtc->crtc_id = index;
Jerome Glissec93bb852009-07-13 21:04:08 +0200212 rdev->mode_info.crtcs[index] = radeon_crtc;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200213
Dave Airlie785b93e2009-08-28 15:46:53 +1000214#if 0
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200215 radeon_crtc->mode_set.crtc = &radeon_crtc->base;
216 radeon_crtc->mode_set.connectors = (struct drm_connector **)(radeon_crtc + 1);
217 radeon_crtc->mode_set.num_connectors = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000218#endif
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200219
220 for (i = 0; i < 256; i++) {
221 radeon_crtc->lut_r[i] = i << 2;
222 radeon_crtc->lut_g[i] = i << 2;
223 radeon_crtc->lut_b[i] = i << 2;
224 }
225
226 if (rdev->is_atom_bios && (ASIC_IS_AVIVO(rdev) || radeon_r4xx_atom))
227 radeon_atombios_init_crtc(dev, radeon_crtc);
228 else
229 radeon_legacy_init_crtc(dev, radeon_crtc);
230}
231
232static const char *encoder_names[34] = {
233 "NONE",
234 "INTERNAL_LVDS",
235 "INTERNAL_TMDS1",
236 "INTERNAL_TMDS2",
237 "INTERNAL_DAC1",
238 "INTERNAL_DAC2",
239 "INTERNAL_SDVOA",
240 "INTERNAL_SDVOB",
241 "SI170B",
242 "CH7303",
243 "CH7301",
244 "INTERNAL_DVO1",
245 "EXTERNAL_SDVOA",
246 "EXTERNAL_SDVOB",
247 "TITFP513",
248 "INTERNAL_LVTM1",
249 "VT1623",
250 "HDMI_SI1930",
251 "HDMI_INTERNAL",
252 "INTERNAL_KLDSCP_TMDS1",
253 "INTERNAL_KLDSCP_DVO1",
254 "INTERNAL_KLDSCP_DAC1",
255 "INTERNAL_KLDSCP_DAC2",
256 "SI178",
257 "MVPU_FPGA",
258 "INTERNAL_DDI",
259 "VT1625",
260 "HDMI_SI1932",
261 "DP_AN9801",
262 "DP_DP501",
263 "INTERNAL_UNIPHY",
264 "INTERNAL_KLDSCP_LVTMA",
265 "INTERNAL_UNIPHY1",
266 "INTERNAL_UNIPHY2",
267};
268
Alex Deucher196c58d2010-01-07 14:22:32 -0500269static const char *connector_names[15] = {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200270 "Unknown",
271 "VGA",
272 "DVI-I",
273 "DVI-D",
274 "DVI-A",
275 "Composite",
276 "S-video",
277 "LVDS",
278 "Component",
279 "DIN",
280 "DisplayPort",
281 "HDMI-A",
282 "HDMI-B",
Alex Deucher196c58d2010-01-07 14:22:32 -0500283 "TV",
284 "eDP",
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200285};
286
Alex Deuchereed45b32009-12-04 14:45:27 -0500287static const char *hpd_names[7] = {
288 "NONE",
289 "HPD1",
290 "HPD2",
291 "HPD3",
292 "HPD4",
293 "HPD5",
294 "HPD6",
295};
296
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200297static void radeon_print_display_setup(struct drm_device *dev)
298{
299 struct drm_connector *connector;
300 struct radeon_connector *radeon_connector;
301 struct drm_encoder *encoder;
302 struct radeon_encoder *radeon_encoder;
303 uint32_t devices;
304 int i = 0;
305
306 DRM_INFO("Radeon Display Connectors\n");
307 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
308 radeon_connector = to_radeon_connector(connector);
309 DRM_INFO("Connector %d:\n", i);
310 DRM_INFO(" %s\n", connector_names[connector->connector_type]);
Alex Deuchereed45b32009-12-04 14:45:27 -0500311 if (radeon_connector->hpd.hpd != RADEON_HPD_NONE)
312 DRM_INFO(" %s\n", hpd_names[radeon_connector->hpd.hpd]);
Dave Airlie4b9d2a22010-02-08 13:16:55 +1000313 if (radeon_connector->ddc_bus) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200314 DRM_INFO(" DDC: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
315 radeon_connector->ddc_bus->rec.mask_clk_reg,
316 radeon_connector->ddc_bus->rec.mask_data_reg,
317 radeon_connector->ddc_bus->rec.a_clk_reg,
318 radeon_connector->ddc_bus->rec.a_data_reg,
Alex Deucher9b9fe722009-11-10 15:59:44 -0500319 radeon_connector->ddc_bus->rec.en_clk_reg,
320 radeon_connector->ddc_bus->rec.en_data_reg,
321 radeon_connector->ddc_bus->rec.y_clk_reg,
322 radeon_connector->ddc_bus->rec.y_data_reg);
Dave Airlie4b9d2a22010-02-08 13:16:55 +1000323 } else {
324 if (connector->connector_type == DRM_MODE_CONNECTOR_VGA ||
325 connector->connector_type == DRM_MODE_CONNECTOR_DVII ||
326 connector->connector_type == DRM_MODE_CONNECTOR_DVID ||
327 connector->connector_type == DRM_MODE_CONNECTOR_DVIA ||
328 connector->connector_type == DRM_MODE_CONNECTOR_HDMIA ||
329 connector->connector_type == DRM_MODE_CONNECTOR_HDMIB)
330 DRM_INFO(" DDC: no ddc bus - possible BIOS bug - please report to xorg-driver-ati@lists.x.org\n");
331 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200332 DRM_INFO(" Encoders:\n");
333 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
334 radeon_encoder = to_radeon_encoder(encoder);
335 devices = radeon_encoder->devices & radeon_connector->devices;
336 if (devices) {
337 if (devices & ATOM_DEVICE_CRT1_SUPPORT)
338 DRM_INFO(" CRT1: %s\n", encoder_names[radeon_encoder->encoder_id]);
339 if (devices & ATOM_DEVICE_CRT2_SUPPORT)
340 DRM_INFO(" CRT2: %s\n", encoder_names[radeon_encoder->encoder_id]);
341 if (devices & ATOM_DEVICE_LCD1_SUPPORT)
342 DRM_INFO(" LCD1: %s\n", encoder_names[radeon_encoder->encoder_id]);
343 if (devices & ATOM_DEVICE_DFP1_SUPPORT)
344 DRM_INFO(" DFP1: %s\n", encoder_names[radeon_encoder->encoder_id]);
345 if (devices & ATOM_DEVICE_DFP2_SUPPORT)
346 DRM_INFO(" DFP2: %s\n", encoder_names[radeon_encoder->encoder_id]);
347 if (devices & ATOM_DEVICE_DFP3_SUPPORT)
348 DRM_INFO(" DFP3: %s\n", encoder_names[radeon_encoder->encoder_id]);
349 if (devices & ATOM_DEVICE_DFP4_SUPPORT)
350 DRM_INFO(" DFP4: %s\n", encoder_names[radeon_encoder->encoder_id]);
351 if (devices & ATOM_DEVICE_DFP5_SUPPORT)
352 DRM_INFO(" DFP5: %s\n", encoder_names[radeon_encoder->encoder_id]);
353 if (devices & ATOM_DEVICE_TV1_SUPPORT)
354 DRM_INFO(" TV1: %s\n", encoder_names[radeon_encoder->encoder_id]);
355 if (devices & ATOM_DEVICE_CV_SUPPORT)
356 DRM_INFO(" CV: %s\n", encoder_names[radeon_encoder->encoder_id]);
357 }
358 }
359 i++;
360 }
361}
362
Dave Airlie4ce001a2009-08-13 16:32:14 +1000363static bool radeon_setup_enc_conn(struct drm_device *dev)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200364{
365 struct radeon_device *rdev = dev->dev_private;
366 struct drm_connector *drm_connector;
367 bool ret = false;
368
369 if (rdev->bios) {
370 if (rdev->is_atom_bios) {
371 if (rdev->family >= CHIP_R600)
372 ret = radeon_get_atom_connector_info_from_object_table(dev);
373 else
374 ret = radeon_get_atom_connector_info_from_supported_devices_table(dev);
Alex Deucherb9597a12010-01-04 19:12:02 -0500375 } else {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200376 ret = radeon_get_legacy_connector_info_from_bios(dev);
Alex Deucherb9597a12010-01-04 19:12:02 -0500377 if (ret == false)
378 ret = radeon_get_legacy_connector_info_from_table(dev);
379 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200380 } else {
381 if (!ASIC_IS_AVIVO(rdev))
382 ret = radeon_get_legacy_connector_info_from_table(dev);
383 }
384 if (ret) {
Dave Airlie1f3b6a42009-10-13 14:10:37 +1000385 radeon_setup_encoder_clones(dev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200386 radeon_print_display_setup(dev);
387 list_for_each_entry(drm_connector, &dev->mode_config.connector_list, head)
388 radeon_ddc_dump(drm_connector);
389 }
390
391 return ret;
392}
393
394int radeon_ddc_get_modes(struct radeon_connector *radeon_connector)
395{
Alex Deucher3c537882010-02-05 04:21:19 -0500396 struct drm_device *dev = radeon_connector->base.dev;
397 struct radeon_device *rdev = dev->dev_private;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200398 int ret = 0;
399
Alex Deucher196c58d2010-01-07 14:22:32 -0500400 if ((radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_DisplayPort) ||
401 (radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_eDP)) {
Dave Airlie746c1aa2009-12-08 07:07:28 +1000402 struct radeon_connector_atom_dig *dig = radeon_connector->con_priv;
Dave Airlie7a15cbd2010-01-14 11:42:17 +1000403 if ((dig->dp_sink_type == CONNECTOR_OBJECT_ID_DISPLAYPORT ||
404 dig->dp_sink_type == CONNECTOR_OBJECT_ID_eDP) && dig->dp_i2c_bus)
Alex Deucher9fa05c92009-11-27 13:01:46 -0500405 radeon_connector->edid = drm_get_edid(&radeon_connector->base, &dig->dp_i2c_bus->adapter);
Dave Airlie746c1aa2009-12-08 07:07:28 +1000406 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200407 if (!radeon_connector->ddc_bus)
408 return -1;
Dave Airlie4ce001a2009-08-13 16:32:14 +1000409 if (!radeon_connector->edid) {
Alex Deucher0294cf4f2009-10-15 16:16:35 -0400410 radeon_connector->edid = drm_get_edid(&radeon_connector->base, &radeon_connector->ddc_bus->adapter);
Alex Deucher0294cf4f2009-10-15 16:16:35 -0400411 }
Alex Deucher3c537882010-02-05 04:21:19 -0500412 /* some servers provide a hardcoded edid in rom for KVMs */
413 if (!radeon_connector->edid)
414 radeon_connector->edid = radeon_combios_get_hardcoded_edid(rdev);
Alex Deucher0294cf4f2009-10-15 16:16:35 -0400415 if (radeon_connector->edid) {
416 drm_mode_connector_update_edid_property(&radeon_connector->base, radeon_connector->edid);
417 ret = drm_add_edid_modes(&radeon_connector->base, radeon_connector->edid);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200418 return ret;
419 }
420 drm_mode_connector_update_edid_property(&radeon_connector->base, NULL);
Dave Airlie42dea5d2009-09-15 20:21:11 +1000421 return 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200422}
423
424static int radeon_ddc_dump(struct drm_connector *connector)
425{
426 struct edid *edid;
427 struct radeon_connector *radeon_connector = to_radeon_connector(connector);
428 int ret = 0;
429
430 if (!radeon_connector->ddc_bus)
431 return -1;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200432 edid = drm_get_edid(connector, &radeon_connector->ddc_bus->adapter);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200433 if (edid) {
434 kfree(edid);
435 }
436 return ret;
437}
438
439static inline uint32_t radeon_div(uint64_t n, uint32_t d)
440{
441 uint64_t mod;
442
443 n += d / 2;
444
445 mod = do_div(n, d);
446 return n;
447}
448
Alex Deucher7c27f872010-02-02 12:05:01 -0500449static void radeon_compute_pll_legacy(struct radeon_pll *pll,
450 uint64_t freq,
451 uint32_t *dot_clock_p,
452 uint32_t *fb_div_p,
453 uint32_t *frac_fb_div_p,
454 uint32_t *ref_div_p,
455 uint32_t *post_div_p)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200456{
457 uint32_t min_ref_div = pll->min_ref_div;
458 uint32_t max_ref_div = pll->max_ref_div;
Alex Deucherfc103322010-01-19 17:16:10 -0500459 uint32_t min_post_div = pll->min_post_div;
460 uint32_t max_post_div = pll->max_post_div;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200461 uint32_t min_fractional_feed_div = 0;
462 uint32_t max_fractional_feed_div = 0;
463 uint32_t best_vco = pll->best_vco;
464 uint32_t best_post_div = 1;
465 uint32_t best_ref_div = 1;
466 uint32_t best_feedback_div = 1;
467 uint32_t best_frac_feedback_div = 0;
468 uint32_t best_freq = -1;
469 uint32_t best_error = 0xffffffff;
470 uint32_t best_vco_diff = 1;
471 uint32_t post_div;
Alex Deucher86cb2bb2010-03-08 12:55:16 -0500472 u32 pll_out_min, pll_out_max;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200473
474 DRM_DEBUG("PLL freq %llu %u %u\n", freq, pll->min_ref_div, pll->max_ref_div);
475 freq = freq * 1000;
476
Alex Deucher86cb2bb2010-03-08 12:55:16 -0500477 if (pll->flags & RADEON_PLL_IS_LCD) {
478 pll_out_min = pll->lcd_pll_out_min;
479 pll_out_max = pll->lcd_pll_out_max;
480 } else {
481 pll_out_min = pll->pll_out_min;
482 pll_out_max = pll->pll_out_max;
483 }
484
Alex Deucherfc103322010-01-19 17:16:10 -0500485 if (pll->flags & RADEON_PLL_USE_REF_DIV)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200486 min_ref_div = max_ref_div = pll->reference_div;
487 else {
488 while (min_ref_div < max_ref_div-1) {
489 uint32_t mid = (min_ref_div + max_ref_div) / 2;
490 uint32_t pll_in = pll->reference_freq / mid;
491 if (pll_in < pll->pll_in_min)
492 max_ref_div = mid;
493 else if (pll_in > pll->pll_in_max)
494 min_ref_div = mid;
495 else
496 break;
497 }
498 }
499
Alex Deucherfc103322010-01-19 17:16:10 -0500500 if (pll->flags & RADEON_PLL_USE_POST_DIV)
501 min_post_div = max_post_div = pll->post_div;
502
503 if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200504 min_fractional_feed_div = pll->min_frac_feedback_div;
505 max_fractional_feed_div = pll->max_frac_feedback_div;
506 }
507
Alex Deucherfc103322010-01-19 17:16:10 -0500508 for (post_div = min_post_div; post_div <= max_post_div; ++post_div) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200509 uint32_t ref_div;
510
Alex Deucherfc103322010-01-19 17:16:10 -0500511 if ((pll->flags & RADEON_PLL_NO_ODD_POST_DIV) && (post_div & 1))
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200512 continue;
513
514 /* legacy radeons only have a few post_divs */
Alex Deucherfc103322010-01-19 17:16:10 -0500515 if (pll->flags & RADEON_PLL_LEGACY) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200516 if ((post_div == 5) ||
517 (post_div == 7) ||
518 (post_div == 9) ||
519 (post_div == 10) ||
520 (post_div == 11) ||
521 (post_div == 13) ||
522 (post_div == 14) ||
523 (post_div == 15))
524 continue;
525 }
526
527 for (ref_div = min_ref_div; ref_div <= max_ref_div; ++ref_div) {
528 uint32_t feedback_div, current_freq = 0, error, vco_diff;
529 uint32_t pll_in = pll->reference_freq / ref_div;
530 uint32_t min_feed_div = pll->min_feedback_div;
531 uint32_t max_feed_div = pll->max_feedback_div + 1;
532
533 if (pll_in < pll->pll_in_min || pll_in > pll->pll_in_max)
534 continue;
535
536 while (min_feed_div < max_feed_div) {
537 uint32_t vco;
538 uint32_t min_frac_feed_div = min_fractional_feed_div;
539 uint32_t max_frac_feed_div = max_fractional_feed_div + 1;
540 uint32_t frac_feedback_div;
541 uint64_t tmp;
542
543 feedback_div = (min_feed_div + max_feed_div) / 2;
544
545 tmp = (uint64_t)pll->reference_freq * feedback_div;
546 vco = radeon_div(tmp, ref_div);
547
Alex Deucher86cb2bb2010-03-08 12:55:16 -0500548 if (vco < pll_out_min) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200549 min_feed_div = feedback_div + 1;
550 continue;
Alex Deucher86cb2bb2010-03-08 12:55:16 -0500551 } else if (vco > pll_out_max) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200552 max_feed_div = feedback_div;
553 continue;
554 }
555
556 while (min_frac_feed_div < max_frac_feed_div) {
557 frac_feedback_div = (min_frac_feed_div + max_frac_feed_div) / 2;
558 tmp = (uint64_t)pll->reference_freq * 10000 * feedback_div;
559 tmp += (uint64_t)pll->reference_freq * 1000 * frac_feedback_div;
560 current_freq = radeon_div(tmp, ref_div * post_div);
561
Alex Deucherfc103322010-01-19 17:16:10 -0500562 if (pll->flags & RADEON_PLL_PREFER_CLOSEST_LOWER) {
Alex Deucherd0e275a2009-07-13 11:08:18 -0400563 error = freq - current_freq;
564 error = error < 0 ? 0xffffffff : error;
565 } else
566 error = abs(current_freq - freq);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200567 vco_diff = abs(vco - best_vco);
568
569 if ((best_vco == 0 && error < best_error) ||
570 (best_vco != 0 &&
571 (error < best_error - 100 ||
572 (abs(error - best_error) < 100 && vco_diff < best_vco_diff)))) {
573 best_post_div = post_div;
574 best_ref_div = ref_div;
575 best_feedback_div = feedback_div;
576 best_frac_feedback_div = frac_feedback_div;
577 best_freq = current_freq;
578 best_error = error;
579 best_vco_diff = vco_diff;
580 } else if (current_freq == freq) {
581 if (best_freq == -1) {
582 best_post_div = post_div;
583 best_ref_div = ref_div;
584 best_feedback_div = feedback_div;
585 best_frac_feedback_div = frac_feedback_div;
586 best_freq = current_freq;
587 best_error = error;
588 best_vco_diff = vco_diff;
Alex Deucherfc103322010-01-19 17:16:10 -0500589 } else if (((pll->flags & RADEON_PLL_PREFER_LOW_REF_DIV) && (ref_div < best_ref_div)) ||
590 ((pll->flags & RADEON_PLL_PREFER_HIGH_REF_DIV) && (ref_div > best_ref_div)) ||
591 ((pll->flags & RADEON_PLL_PREFER_LOW_FB_DIV) && (feedback_div < best_feedback_div)) ||
592 ((pll->flags & RADEON_PLL_PREFER_HIGH_FB_DIV) && (feedback_div > best_feedback_div)) ||
593 ((pll->flags & RADEON_PLL_PREFER_LOW_POST_DIV) && (post_div < best_post_div)) ||
594 ((pll->flags & RADEON_PLL_PREFER_HIGH_POST_DIV) && (post_div > best_post_div))) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200595 best_post_div = post_div;
596 best_ref_div = ref_div;
597 best_feedback_div = feedback_div;
598 best_frac_feedback_div = frac_feedback_div;
599 best_freq = current_freq;
600 best_error = error;
601 best_vco_diff = vco_diff;
602 }
603 }
604 if (current_freq < freq)
605 min_frac_feed_div = frac_feedback_div + 1;
606 else
607 max_frac_feed_div = frac_feedback_div;
608 }
609 if (current_freq < freq)
610 min_feed_div = feedback_div + 1;
611 else
612 max_feed_div = feedback_div;
613 }
614 }
615 }
616
617 *dot_clock_p = best_freq / 10000;
618 *fb_div_p = best_feedback_div;
619 *frac_fb_div_p = best_frac_feedback_div;
620 *ref_div_p = best_ref_div;
621 *post_div_p = best_post_div;
622}
623
Alex Deucher383be5d2010-02-23 03:24:38 -0500624static bool
625calc_fb_div(struct radeon_pll *pll,
626 uint32_t freq,
627 uint32_t post_div,
628 uint32_t ref_div,
629 uint32_t *fb_div,
630 uint32_t *fb_div_frac)
Alex Deucherb27b6372009-12-09 17:44:25 -0500631{
Alex Deucher383be5d2010-02-23 03:24:38 -0500632 fixed20_12 feedback_divider, a, b;
633 u32 vco_freq;
Alex Deucherb27b6372009-12-09 17:44:25 -0500634
Alex Deucher383be5d2010-02-23 03:24:38 -0500635 vco_freq = freq * post_div;
636 /* feedback_divider = vco_freq * ref_div / pll->reference_freq; */
637 a.full = rfixed_const(pll->reference_freq);
638 feedback_divider.full = rfixed_const(vco_freq);
639 feedback_divider.full = rfixed_div(feedback_divider, a);
640 a.full = rfixed_const(ref_div);
641 feedback_divider.full = rfixed_mul(feedback_divider, a);
642
643 if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV) {
644 /* feedback_divider = floor((feedback_divider * 10.0) + 0.5) * 0.1; */
645 a.full = rfixed_const(10);
646 feedback_divider.full = rfixed_mul(feedback_divider, a);
647 feedback_divider.full += rfixed_const_half(0);
648 feedback_divider.full = rfixed_floor(feedback_divider);
649 feedback_divider.full = rfixed_div(feedback_divider, a);
650
651 /* *fb_div = floor(feedback_divider); */
652 a.full = rfixed_floor(feedback_divider);
653 *fb_div = rfixed_trunc(a);
654 /* *fb_div_frac = fmod(feedback_divider, 1.0) * 10.0; */
655 a.full = rfixed_const(10);
656 b.full = rfixed_mul(feedback_divider, a);
657
658 feedback_divider.full = rfixed_floor(feedback_divider);
659 feedback_divider.full = rfixed_mul(feedback_divider, a);
660 feedback_divider.full = b.full - feedback_divider.full;
661 *fb_div_frac = rfixed_trunc(feedback_divider);
662 } else {
663 /* *fb_div = floor(feedback_divider + 0.5); */
664 feedback_divider.full += rfixed_const_half(0);
665 feedback_divider.full = rfixed_floor(feedback_divider);
666
667 *fb_div = rfixed_trunc(feedback_divider);
668 *fb_div_frac = 0;
669 }
670
671 if (((*fb_div) < pll->min_feedback_div) || ((*fb_div) > pll->max_feedback_div))
672 return false;
673 else
674 return true;
675}
676
677static bool
678calc_fb_ref_div(struct radeon_pll *pll,
679 uint32_t freq,
680 uint32_t post_div,
681 uint32_t *fb_div,
682 uint32_t *fb_div_frac,
683 uint32_t *ref_div)
684{
685 fixed20_12 ffreq, max_error, error, pll_out, a;
686 u32 vco;
Alex Deucher86cb2bb2010-03-08 12:55:16 -0500687 u32 pll_out_min, pll_out_max;
688
689 if (pll->flags & RADEON_PLL_IS_LCD) {
690 pll_out_min = pll->lcd_pll_out_min;
691 pll_out_max = pll->lcd_pll_out_max;
692 } else {
693 pll_out_min = pll->pll_out_min;
694 pll_out_max = pll->pll_out_max;
695 }
Alex Deucher383be5d2010-02-23 03:24:38 -0500696
Alex Deucherb27b6372009-12-09 17:44:25 -0500697 ffreq.full = rfixed_const(freq);
Alex Deucher383be5d2010-02-23 03:24:38 -0500698 /* max_error = ffreq * 0.0025; */
699 a.full = rfixed_const(400);
700 max_error.full = rfixed_div(ffreq, a);
Alex Deucherb27b6372009-12-09 17:44:25 -0500701
Alex Deucher383be5d2010-02-23 03:24:38 -0500702 for ((*ref_div) = pll->min_ref_div; (*ref_div) < pll->max_ref_div; ++(*ref_div)) {
703 if (calc_fb_div(pll, freq, post_div, (*ref_div), fb_div, fb_div_frac)) {
704 vco = pll->reference_freq * (((*fb_div) * 10) + (*fb_div_frac));
705 vco = vco / ((*ref_div) * 10);
Alex Deucherb27b6372009-12-09 17:44:25 -0500706
Alex Deucher86cb2bb2010-03-08 12:55:16 -0500707 if ((vco < pll_out_min) || (vco > pll_out_max))
Alex Deucher383be5d2010-02-23 03:24:38 -0500708 continue;
Alex Deucherb27b6372009-12-09 17:44:25 -0500709
Alex Deucher383be5d2010-02-23 03:24:38 -0500710 /* pll_out = vco / post_div; */
711 a.full = rfixed_const(post_div);
712 pll_out.full = rfixed_const(vco);
713 pll_out.full = rfixed_div(pll_out, a);
Alex Deucherb27b6372009-12-09 17:44:25 -0500714
Alex Deucher383be5d2010-02-23 03:24:38 -0500715 if (pll_out.full >= ffreq.full) {
716 error.full = pll_out.full - ffreq.full;
717 if (error.full <= max_error.full)
718 return true;
719 }
720 }
721 }
722 return false;
723}
Alex Deucherb27b6372009-12-09 17:44:25 -0500724
Alex Deucher383be5d2010-02-23 03:24:38 -0500725static void radeon_compute_pll_new(struct radeon_pll *pll,
726 uint64_t freq,
727 uint32_t *dot_clock_p,
728 uint32_t *fb_div_p,
729 uint32_t *frac_fb_div_p,
730 uint32_t *ref_div_p,
731 uint32_t *post_div_p)
732{
733 u32 fb_div = 0, fb_div_frac = 0, post_div = 0, ref_div = 0;
734 u32 best_freq = 0, vco_frequency;
Alex Deucher86cb2bb2010-03-08 12:55:16 -0500735 u32 pll_out_min, pll_out_max;
736
737 if (pll->flags & RADEON_PLL_IS_LCD) {
738 pll_out_min = pll->lcd_pll_out_min;
739 pll_out_max = pll->lcd_pll_out_max;
740 } else {
741 pll_out_min = pll->pll_out_min;
742 pll_out_max = pll->pll_out_max;
743 }
Alex Deucherb27b6372009-12-09 17:44:25 -0500744
Alex Deucher383be5d2010-02-23 03:24:38 -0500745 /* freq = freq / 10; */
746 do_div(freq, 10);
Alex Deucherb27b6372009-12-09 17:44:25 -0500747
Alex Deucher383be5d2010-02-23 03:24:38 -0500748 if (pll->flags & RADEON_PLL_USE_POST_DIV) {
749 post_div = pll->post_div;
750 if ((post_div < pll->min_post_div) || (post_div > pll->max_post_div))
751 goto done;
Alex Deucherb27b6372009-12-09 17:44:25 -0500752
Alex Deucher383be5d2010-02-23 03:24:38 -0500753 vco_frequency = freq * post_div;
Alex Deucher86cb2bb2010-03-08 12:55:16 -0500754 if ((vco_frequency < pll_out_min) || (vco_frequency > pll_out_max))
Alex Deucher383be5d2010-02-23 03:24:38 -0500755 goto done;
Alex Deucherb27b6372009-12-09 17:44:25 -0500756
Alex Deucher383be5d2010-02-23 03:24:38 -0500757 if (pll->flags & RADEON_PLL_USE_REF_DIV) {
758 ref_div = pll->reference_div;
759 if ((ref_div < pll->min_ref_div) || (ref_div > pll->max_ref_div))
760 goto done;
761 if (!calc_fb_div(pll, freq, post_div, ref_div, &fb_div, &fb_div_frac))
762 goto done;
763 }
764 } else {
765 for (post_div = pll->max_post_div; post_div >= pll->min_post_div; --post_div) {
766 if (pll->flags & RADEON_PLL_LEGACY) {
767 if ((post_div == 5) ||
768 (post_div == 7) ||
769 (post_div == 9) ||
770 (post_div == 10) ||
771 (post_div == 11))
772 continue;
773 }
Alex Deucherb27b6372009-12-09 17:44:25 -0500774
Alex Deucher383be5d2010-02-23 03:24:38 -0500775 if ((pll->flags & RADEON_PLL_NO_ODD_POST_DIV) && (post_div & 1))
776 continue;
777
778 vco_frequency = freq * post_div;
Alex Deucher86cb2bb2010-03-08 12:55:16 -0500779 if ((vco_frequency < pll_out_min) || (vco_frequency > pll_out_max))
Alex Deucher383be5d2010-02-23 03:24:38 -0500780 continue;
781 if (pll->flags & RADEON_PLL_USE_REF_DIV) {
782 ref_div = pll->reference_div;
783 if ((ref_div < pll->min_ref_div) || (ref_div > pll->max_ref_div))
784 goto done;
785 if (calc_fb_div(pll, freq, post_div, ref_div, &fb_div, &fb_div_frac))
786 break;
787 } else {
788 if (calc_fb_ref_div(pll, freq, post_div, &fb_div, &fb_div_frac, &ref_div))
789 break;
790 }
Alex Deucherb27b6372009-12-09 17:44:25 -0500791 }
792 }
793
Alex Deucher383be5d2010-02-23 03:24:38 -0500794 best_freq = pll->reference_freq * 10 * fb_div;
795 best_freq += pll->reference_freq * fb_div_frac;
796 best_freq = best_freq / (ref_div * post_div);
Alex Deucherb27b6372009-12-09 17:44:25 -0500797
Alex Deucher383be5d2010-02-23 03:24:38 -0500798done:
799 if (best_freq == 0)
800 DRM_ERROR("Couldn't find valid PLL dividers\n");
Alex Deucherb27b6372009-12-09 17:44:25 -0500801
Alex Deucher383be5d2010-02-23 03:24:38 -0500802 *dot_clock_p = best_freq / 10;
803 *fb_div_p = fb_div;
804 *frac_fb_div_p = fb_div_frac;
805 *ref_div_p = ref_div;
806 *post_div_p = post_div;
Alex Deucherb27b6372009-12-09 17:44:25 -0500807
Alex Deucher383be5d2010-02-23 03:24:38 -0500808 DRM_DEBUG("%u %d.%d, %d, %d\n", *dot_clock_p, *fb_div_p, *frac_fb_div_p, *ref_div_p, *post_div_p);
Alex Deucherb27b6372009-12-09 17:44:25 -0500809}
810
Alex Deucher7c27f872010-02-02 12:05:01 -0500811void radeon_compute_pll(struct radeon_pll *pll,
812 uint64_t freq,
813 uint32_t *dot_clock_p,
814 uint32_t *fb_div_p,
815 uint32_t *frac_fb_div_p,
816 uint32_t *ref_div_p,
817 uint32_t *post_div_p)
818{
819 switch (pll->algo) {
Alex Deucher383be5d2010-02-23 03:24:38 -0500820 case PLL_ALGO_NEW:
821 radeon_compute_pll_new(pll, freq, dot_clock_p, fb_div_p,
822 frac_fb_div_p, ref_div_p, post_div_p);
Alex Deucher7c27f872010-02-02 12:05:01 -0500823 break;
824 case PLL_ALGO_LEGACY:
825 default:
826 radeon_compute_pll_legacy(pll, freq, dot_clock_p, fb_div_p,
827 frac_fb_div_p, ref_div_p, post_div_p);
828 break;
829 }
Dave Airlie445282d2009-09-09 17:40:54 +1000830}
831
832static void radeon_user_framebuffer_destroy(struct drm_framebuffer *fb)
833{
834 struct radeon_framebuffer *radeon_fb = to_radeon_framebuffer(fb);
835 struct drm_device *dev = fb->dev;
836
837 if (fb->fbdev)
838 radeonfb_remove(dev, fb);
839
Luca Barbieribc9025b2010-02-09 05:49:12 +0000840 if (radeon_fb->obj)
841 drm_gem_object_unreference_unlocked(radeon_fb->obj);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200842 drm_framebuffer_cleanup(fb);
843 kfree(radeon_fb);
844}
845
846static int radeon_user_framebuffer_create_handle(struct drm_framebuffer *fb,
847 struct drm_file *file_priv,
848 unsigned int *handle)
849{
850 struct radeon_framebuffer *radeon_fb = to_radeon_framebuffer(fb);
851
852 return drm_gem_handle_create(file_priv, radeon_fb->obj, handle);
853}
854
855static const struct drm_framebuffer_funcs radeon_fb_funcs = {
856 .destroy = radeon_user_framebuffer_destroy,
857 .create_handle = radeon_user_framebuffer_create_handle,
858};
859
860struct drm_framebuffer *
861radeon_framebuffer_create(struct drm_device *dev,
862 struct drm_mode_fb_cmd *mode_cmd,
863 struct drm_gem_object *obj)
864{
865 struct radeon_framebuffer *radeon_fb;
866
867 radeon_fb = kzalloc(sizeof(*radeon_fb), GFP_KERNEL);
868 if (radeon_fb == NULL) {
869 return NULL;
870 }
871 drm_framebuffer_init(dev, &radeon_fb->base, &radeon_fb_funcs);
872 drm_helper_mode_fill_fb_struct(&radeon_fb->base, mode_cmd);
873 radeon_fb->obj = obj;
874 return &radeon_fb->base;
875}
876
877static struct drm_framebuffer *
878radeon_user_framebuffer_create(struct drm_device *dev,
879 struct drm_file *file_priv,
880 struct drm_mode_fb_cmd *mode_cmd)
881{
882 struct drm_gem_object *obj;
883
884 obj = drm_gem_object_lookup(dev, file_priv, mode_cmd->handle);
Jerome Glisse7e71c9e2010-01-17 21:21:41 +0100885 if (obj == NULL) {
886 dev_err(&dev->pdev->dev, "No GEM object associated to handle 0x%08X, "
887 "can't create framebuffer\n", mode_cmd->handle);
888 return NULL;
889 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200890 return radeon_framebuffer_create(dev, mode_cmd, obj);
891}
892
893static const struct drm_mode_config_funcs radeon_mode_funcs = {
894 .fb_create = radeon_user_framebuffer_create,
895 .fb_changed = radeonfb_probe,
896};
897
Dave Airlie445282d2009-09-09 17:40:54 +1000898struct drm_prop_enum_list {
899 int type;
900 char *name;
901};
902
903static struct drm_prop_enum_list radeon_tmds_pll_enum_list[] =
904{ { 0, "driver" },
905 { 1, "bios" },
906};
907
908static struct drm_prop_enum_list radeon_tv_std_enum_list[] =
909{ { TV_STD_NTSC, "ntsc" },
910 { TV_STD_PAL, "pal" },
911 { TV_STD_PAL_M, "pal-m" },
912 { TV_STD_PAL_60, "pal-60" },
913 { TV_STD_NTSC_J, "ntsc-j" },
914 { TV_STD_SCART_PAL, "scart-pal" },
915 { TV_STD_PAL_CN, "pal-cn" },
916 { TV_STD_SECAM, "secam" },
917};
918
Alex Deucherd79766f2009-12-17 19:00:29 -0500919static int radeon_modeset_create_props(struct radeon_device *rdev)
Dave Airlie445282d2009-09-09 17:40:54 +1000920{
921 int i, sz;
922
923 if (rdev->is_atom_bios) {
924 rdev->mode_info.coherent_mode_property =
925 drm_property_create(rdev->ddev,
926 DRM_MODE_PROP_RANGE,
927 "coherent", 2);
928 if (!rdev->mode_info.coherent_mode_property)
929 return -ENOMEM;
930
931 rdev->mode_info.coherent_mode_property->values[0] = 0;
Alex Deucher390d0bb2009-12-08 12:48:20 -0500932 rdev->mode_info.coherent_mode_property->values[1] = 1;
Dave Airlie445282d2009-09-09 17:40:54 +1000933 }
934
935 if (!ASIC_IS_AVIVO(rdev)) {
936 sz = ARRAY_SIZE(radeon_tmds_pll_enum_list);
937 rdev->mode_info.tmds_pll_property =
938 drm_property_create(rdev->ddev,
939 DRM_MODE_PROP_ENUM,
940 "tmds_pll", sz);
941 for (i = 0; i < sz; i++) {
942 drm_property_add_enum(rdev->mode_info.tmds_pll_property,
943 i,
944 radeon_tmds_pll_enum_list[i].type,
945 radeon_tmds_pll_enum_list[i].name);
946 }
947 }
948
949 rdev->mode_info.load_detect_property =
950 drm_property_create(rdev->ddev,
951 DRM_MODE_PROP_RANGE,
952 "load detection", 2);
953 if (!rdev->mode_info.load_detect_property)
954 return -ENOMEM;
955 rdev->mode_info.load_detect_property->values[0] = 0;
Alex Deucher390d0bb2009-12-08 12:48:20 -0500956 rdev->mode_info.load_detect_property->values[1] = 1;
Dave Airlie445282d2009-09-09 17:40:54 +1000957
958 drm_mode_create_scaling_mode_property(rdev->ddev);
959
960 sz = ARRAY_SIZE(radeon_tv_std_enum_list);
961 rdev->mode_info.tv_std_property =
962 drm_property_create(rdev->ddev,
963 DRM_MODE_PROP_ENUM,
964 "tv standard", sz);
965 for (i = 0; i < sz; i++) {
966 drm_property_add_enum(rdev->mode_info.tv_std_property,
967 i,
968 radeon_tv_std_enum_list[i].type,
969 radeon_tv_std_enum_list[i].name);
970 }
971
972 return 0;
973}
974
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200975int radeon_modeset_init(struct radeon_device *rdev)
976{
Alex Deucher18917b62010-02-01 16:02:25 -0500977 int i;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200978 int ret;
979
980 drm_mode_config_init(rdev->ddev);
981 rdev->mode_info.mode_config_initialized = true;
982
983 rdev->ddev->mode_config.funcs = (void *)&radeon_mode_funcs;
984
985 if (ASIC_IS_AVIVO(rdev)) {
986 rdev->ddev->mode_config.max_width = 8192;
987 rdev->ddev->mode_config.max_height = 8192;
988 } else {
989 rdev->ddev->mode_config.max_width = 4096;
990 rdev->ddev->mode_config.max_height = 4096;
991 }
992
993 rdev->ddev->mode_config.fb_base = rdev->mc.aper_base;
994
Dave Airlie445282d2009-09-09 17:40:54 +1000995 ret = radeon_modeset_create_props(rdev);
996 if (ret) {
997 return ret;
998 }
Dave Airliedfee5612009-10-02 09:19:09 +1000999
Alex Deucher3c537882010-02-05 04:21:19 -05001000 /* check combios for a valid hardcoded EDID - Sun servers */
1001 if (!rdev->is_atom_bios) {
1002 /* check for hardcoded EDID in BIOS */
1003 radeon_combios_check_hardcoded_edid(rdev);
1004 }
1005
Dave Airliedfee5612009-10-02 09:19:09 +10001006 if (rdev->flags & RADEON_SINGLE_CRTC)
Alex Deucher18917b62010-02-01 16:02:25 -05001007 rdev->num_crtc = 1;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001008 else {
1009 if (ASIC_IS_DCE4(rdev))
1010 rdev->num_crtc = 6;
1011 else
1012 rdev->num_crtc = 2;
1013 }
Dave Airliedfee5612009-10-02 09:19:09 +10001014
1015 /* allocate crtcs */
Alex Deucher18917b62010-02-01 16:02:25 -05001016 for (i = 0; i < rdev->num_crtc; i++) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001017 radeon_crtc_init(rdev->ddev, i);
1018 }
1019
1020 /* okay we should have all the bios connectors */
1021 ret = radeon_setup_enc_conn(rdev->ddev);
1022 if (!ret) {
1023 return ret;
1024 }
Alex Deucherd4877cf2009-12-04 16:56:37 -05001025 /* initialize hpd */
1026 radeon_hpd_init(rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001027 drm_helper_initial_config(rdev->ddev);
1028 return 0;
1029}
1030
1031void radeon_modeset_fini(struct radeon_device *rdev)
1032{
Alex Deucher3c537882010-02-05 04:21:19 -05001033 kfree(rdev->mode_info.bios_hardcoded_edid);
1034
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001035 if (rdev->mode_info.mode_config_initialized) {
Alex Deucherd4877cf2009-12-04 16:56:37 -05001036 radeon_hpd_fini(rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001037 drm_mode_config_cleanup(rdev->ddev);
1038 rdev->mode_info.mode_config_initialized = false;
1039 }
1040}
1041
Jerome Glissec93bb852009-07-13 21:04:08 +02001042bool radeon_crtc_scaling_mode_fixup(struct drm_crtc *crtc,
1043 struct drm_display_mode *mode,
1044 struct drm_display_mode *adjusted_mode)
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001045{
Jerome Glissec93bb852009-07-13 21:04:08 +02001046 struct drm_device *dev = crtc->dev;
1047 struct drm_encoder *encoder;
1048 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1049 struct radeon_encoder *radeon_encoder;
1050 bool first = true;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001051
Jerome Glissec93bb852009-07-13 21:04:08 +02001052 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1053 radeon_encoder = to_radeon_encoder(encoder);
1054 if (encoder->crtc != crtc)
1055 continue;
1056 if (first) {
Alex Deucher80297e82009-11-12 14:55:14 -05001057 /* set scaling */
1058 if (radeon_encoder->rmx_type == RMX_OFF)
1059 radeon_crtc->rmx_type = RMX_OFF;
1060 else if (mode->hdisplay < radeon_encoder->native_mode.hdisplay ||
1061 mode->vdisplay < radeon_encoder->native_mode.vdisplay)
1062 radeon_crtc->rmx_type = radeon_encoder->rmx_type;
1063 else
1064 radeon_crtc->rmx_type = RMX_OFF;
1065 /* copy native mode */
Jerome Glissec93bb852009-07-13 21:04:08 +02001066 memcpy(&radeon_crtc->native_mode,
Alex Deucher80297e82009-11-12 14:55:14 -05001067 &radeon_encoder->native_mode,
Alex Deucherde2103e2009-10-09 15:14:30 -04001068 sizeof(struct drm_display_mode));
Jerome Glissec93bb852009-07-13 21:04:08 +02001069 first = false;
1070 } else {
1071 if (radeon_crtc->rmx_type != radeon_encoder->rmx_type) {
1072 /* WARNING: Right now this can't happen but
1073 * in the future we need to check that scaling
1074 * are consistent accross different encoder
1075 * (ie all encoder can work with the same
1076 * scaling).
1077 */
1078 DRM_ERROR("Scaling not consistent accross encoder.\n");
1079 return false;
1080 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001081 }
1082 }
Jerome Glissec93bb852009-07-13 21:04:08 +02001083 if (radeon_crtc->rmx_type != RMX_OFF) {
1084 fixed20_12 a, b;
1085 a.full = rfixed_const(crtc->mode.vdisplay);
Alex Deucherde2103e2009-10-09 15:14:30 -04001086 b.full = rfixed_const(radeon_crtc->native_mode.hdisplay);
Jerome Glissec93bb852009-07-13 21:04:08 +02001087 radeon_crtc->vsc.full = rfixed_div(a, b);
1088 a.full = rfixed_const(crtc->mode.hdisplay);
Alex Deucherde2103e2009-10-09 15:14:30 -04001089 b.full = rfixed_const(radeon_crtc->native_mode.vdisplay);
Jerome Glissec93bb852009-07-13 21:04:08 +02001090 radeon_crtc->hsc.full = rfixed_div(a, b);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001091 } else {
Jerome Glissec93bb852009-07-13 21:04:08 +02001092 radeon_crtc->vsc.full = rfixed_const(1);
1093 radeon_crtc->hsc.full = rfixed_const(1);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001094 }
Jerome Glissec93bb852009-07-13 21:04:08 +02001095 return true;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001096}