9d996c6d4cbe6a0aeeebe73b381aa900af968ddc
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / drm / radeon / radeon_atombios.c
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
25  */
26 #include "drmP.h"
27 #include "radeon_drm.h"
28 #include "radeon.h"
29
30 #include "atom.h"
31 #include "atom-bits.h"
32
33 /* from radeon_encoder.c */
34 extern uint32_t
35 radeon_get_encoder_enum(struct drm_device *dev, uint32_t supported_device,
36                         uint8_t dac);
37 extern void radeon_link_encoder_connector(struct drm_device *dev);
38 extern void
39 radeon_add_atom_encoder(struct drm_device *dev, uint32_t encoder_enum,
40                         uint32_t supported_device, u16 caps);
41
42 /* from radeon_connector.c */
43 extern void
44 radeon_add_atom_connector(struct drm_device *dev,
45                           uint32_t connector_id,
46                           uint32_t supported_device,
47                           int connector_type,
48                           struct radeon_i2c_bus_rec *i2c_bus,
49                           uint32_t igp_lane_info,
50                           uint16_t connector_object_id,
51                           struct radeon_hpd *hpd,
52                           struct radeon_router *router);
53
54 /* from radeon_legacy_encoder.c */
55 extern void
56 radeon_add_legacy_encoder(struct drm_device *dev, uint32_t encoder_enum,
57                           uint32_t supported_device);
58
59 union atom_supported_devices {
60         struct _ATOM_SUPPORTED_DEVICES_INFO info;
61         struct _ATOM_SUPPORTED_DEVICES_INFO_2 info_2;
62         struct _ATOM_SUPPORTED_DEVICES_INFO_2d1 info_2d1;
63 };
64
65 static inline struct radeon_i2c_bus_rec radeon_lookup_i2c_gpio(struct radeon_device *rdev,
66                                                                uint8_t id)
67 {
68         struct atom_context *ctx = rdev->mode_info.atom_context;
69         ATOM_GPIO_I2C_ASSIGMENT *gpio;
70         struct radeon_i2c_bus_rec i2c;
71         int index = GetIndexIntoMasterTable(DATA, GPIO_I2C_Info);
72         struct _ATOM_GPIO_I2C_INFO *i2c_info;
73         uint16_t data_offset, size;
74         int i, num_indices;
75
76         memset(&i2c, 0, sizeof(struct radeon_i2c_bus_rec));
77         i2c.valid = false;
78
79         if (atom_parse_data_header(ctx, index, &size, NULL, NULL, &data_offset)) {
80                 i2c_info = (struct _ATOM_GPIO_I2C_INFO *)(ctx->bios + data_offset);
81
82                 num_indices = (size - sizeof(ATOM_COMMON_TABLE_HEADER)) /
83                         sizeof(ATOM_GPIO_I2C_ASSIGMENT);
84
85                 for (i = 0; i < num_indices; i++) {
86                         gpio = &i2c_info->asGPIO_Info[i];
87
88                         /* some evergreen boards have bad data for this entry */
89                         if (ASIC_IS_DCE4(rdev)) {
90                                 if ((i == 7) &&
91                                     (le16_to_cpu(gpio->usClkMaskRegisterIndex) == 0x1936) &&
92                                     (gpio->sucI2cId.ucAccess == 0)) {
93                                         gpio->sucI2cId.ucAccess = 0x97;
94                                         gpio->ucDataMaskShift = 8;
95                                         gpio->ucDataEnShift = 8;
96                                         gpio->ucDataY_Shift = 8;
97                                         gpio->ucDataA_Shift = 8;
98                                 }
99                         }
100
101                         /* some DCE3 boards have bad data for this entry */
102                         if (ASIC_IS_DCE3(rdev)) {
103                                 if ((i == 4) &&
104                                     (le16_to_cpu(gpio->usClkMaskRegisterIndex) == 0x1fda) &&
105                                     (gpio->sucI2cId.ucAccess == 0x94))
106                                         gpio->sucI2cId.ucAccess = 0x14;
107                         }
108
109                         if (gpio->sucI2cId.ucAccess == id) {
110                                 i2c.mask_clk_reg = le16_to_cpu(gpio->usClkMaskRegisterIndex) * 4;
111                                 i2c.mask_data_reg = le16_to_cpu(gpio->usDataMaskRegisterIndex) * 4;
112                                 i2c.en_clk_reg = le16_to_cpu(gpio->usClkEnRegisterIndex) * 4;
113                                 i2c.en_data_reg = le16_to_cpu(gpio->usDataEnRegisterIndex) * 4;
114                                 i2c.y_clk_reg = le16_to_cpu(gpio->usClkY_RegisterIndex) * 4;
115                                 i2c.y_data_reg = le16_to_cpu(gpio->usDataY_RegisterIndex) * 4;
116                                 i2c.a_clk_reg = le16_to_cpu(gpio->usClkA_RegisterIndex) * 4;
117                                 i2c.a_data_reg = le16_to_cpu(gpio->usDataA_RegisterIndex) * 4;
118                                 i2c.mask_clk_mask = (1 << gpio->ucClkMaskShift);
119                                 i2c.mask_data_mask = (1 << gpio->ucDataMaskShift);
120                                 i2c.en_clk_mask = (1 << gpio->ucClkEnShift);
121                                 i2c.en_data_mask = (1 << gpio->ucDataEnShift);
122                                 i2c.y_clk_mask = (1 << gpio->ucClkY_Shift);
123                                 i2c.y_data_mask = (1 << gpio->ucDataY_Shift);
124                                 i2c.a_clk_mask = (1 << gpio->ucClkA_Shift);
125                                 i2c.a_data_mask = (1 << gpio->ucDataA_Shift);
126
127                                 if (gpio->sucI2cId.sbfAccess.bfHW_Capable)
128                                         i2c.hw_capable = true;
129                                 else
130                                         i2c.hw_capable = false;
131
132                                 if (gpio->sucI2cId.ucAccess == 0xa0)
133                                         i2c.mm_i2c = true;
134                                 else
135                                         i2c.mm_i2c = false;
136
137                                 i2c.i2c_id = gpio->sucI2cId.ucAccess;
138
139                                 if (i2c.mask_clk_reg)
140                                         i2c.valid = true;
141                                 break;
142                         }
143                 }
144         }
145
146         return i2c;
147 }
148
149 void radeon_atombios_i2c_init(struct radeon_device *rdev)
150 {
151         struct atom_context *ctx = rdev->mode_info.atom_context;
152         ATOM_GPIO_I2C_ASSIGMENT *gpio;
153         struct radeon_i2c_bus_rec i2c;
154         int index = GetIndexIntoMasterTable(DATA, GPIO_I2C_Info);
155         struct _ATOM_GPIO_I2C_INFO *i2c_info;
156         uint16_t data_offset, size;
157         int i, num_indices;
158         char stmp[32];
159
160         memset(&i2c, 0, sizeof(struct radeon_i2c_bus_rec));
161
162         if (atom_parse_data_header(ctx, index, &size, NULL, NULL, &data_offset)) {
163                 i2c_info = (struct _ATOM_GPIO_I2C_INFO *)(ctx->bios + data_offset);
164
165                 num_indices = (size - sizeof(ATOM_COMMON_TABLE_HEADER)) /
166                         sizeof(ATOM_GPIO_I2C_ASSIGMENT);
167
168                 for (i = 0; i < num_indices; i++) {
169                         gpio = &i2c_info->asGPIO_Info[i];
170                         i2c.valid = false;
171
172                         /* r4xx mask is technically not used by the hw, so patch in the legacy mask bits */
173                         if ((rdev->family == CHIP_R420) ||
174                             (rdev->family == CHIP_R423) ||
175                             (rdev->family == CHIP_RV410)) {
176                                 if ((le16_to_cpu(gpio->usClkMaskRegisterIndex) == 0x0018) ||
177                                     (le16_to_cpu(gpio->usClkMaskRegisterIndex) == 0x0019) ||
178                                     (le16_to_cpu(gpio->usClkMaskRegisterIndex) == 0x001a)) {
179                                         gpio->ucClkMaskShift = 0x19;
180                                         gpio->ucDataMaskShift = 0x18;
181                                 }
182                         }
183
184                         /* some evergreen boards have bad data for this entry */
185                         if (ASIC_IS_DCE4(rdev)) {
186                                 if ((i == 7) &&
187                                     (le16_to_cpu(gpio->usClkMaskRegisterIndex) == 0x1936) &&
188                                     (gpio->sucI2cId.ucAccess == 0)) {
189                                         gpio->sucI2cId.ucAccess = 0x97;
190                                         gpio->ucDataMaskShift = 8;
191                                         gpio->ucDataEnShift = 8;
192                                         gpio->ucDataY_Shift = 8;
193                                         gpio->ucDataA_Shift = 8;
194                                 }
195                         }
196
197                         /* some DCE3 boards have bad data for this entry */
198                         if (ASIC_IS_DCE3(rdev)) {
199                                 if ((i == 4) &&
200                                     (le16_to_cpu(gpio->usClkMaskRegisterIndex) == 0x1fda) &&
201                                     (gpio->sucI2cId.ucAccess == 0x94))
202                                         gpio->sucI2cId.ucAccess = 0x14;
203                         }
204
205                         i2c.mask_clk_reg = le16_to_cpu(gpio->usClkMaskRegisterIndex) * 4;
206                         i2c.mask_data_reg = le16_to_cpu(gpio->usDataMaskRegisterIndex) * 4;
207                         i2c.en_clk_reg = le16_to_cpu(gpio->usClkEnRegisterIndex) * 4;
208                         i2c.en_data_reg = le16_to_cpu(gpio->usDataEnRegisterIndex) * 4;
209                         i2c.y_clk_reg = le16_to_cpu(gpio->usClkY_RegisterIndex) * 4;
210                         i2c.y_data_reg = le16_to_cpu(gpio->usDataY_RegisterIndex) * 4;
211                         i2c.a_clk_reg = le16_to_cpu(gpio->usClkA_RegisterIndex) * 4;
212                         i2c.a_data_reg = le16_to_cpu(gpio->usDataA_RegisterIndex) * 4;
213                         i2c.mask_clk_mask = (1 << gpio->ucClkMaskShift);
214                         i2c.mask_data_mask = (1 << gpio->ucDataMaskShift);
215                         i2c.en_clk_mask = (1 << gpio->ucClkEnShift);
216                         i2c.en_data_mask = (1 << gpio->ucDataEnShift);
217                         i2c.y_clk_mask = (1 << gpio->ucClkY_Shift);
218                         i2c.y_data_mask = (1 << gpio->ucDataY_Shift);
219                         i2c.a_clk_mask = (1 << gpio->ucClkA_Shift);
220                         i2c.a_data_mask = (1 << gpio->ucDataA_Shift);
221
222                         if (gpio->sucI2cId.sbfAccess.bfHW_Capable)
223                                 i2c.hw_capable = true;
224                         else
225                                 i2c.hw_capable = false;
226
227                         if (gpio->sucI2cId.ucAccess == 0xa0)
228                                 i2c.mm_i2c = true;
229                         else
230                                 i2c.mm_i2c = false;
231
232                         i2c.i2c_id = gpio->sucI2cId.ucAccess;
233
234                         if (i2c.mask_clk_reg) {
235                                 i2c.valid = true;
236                                 sprintf(stmp, "0x%x", i2c.i2c_id);
237                                 rdev->i2c_bus[i] = radeon_i2c_create(rdev->ddev, &i2c, stmp);
238                         }
239                 }
240         }
241 }
242
243 static inline struct radeon_gpio_rec radeon_lookup_gpio(struct radeon_device *rdev,
244                                                         u8 id)
245 {
246         struct atom_context *ctx = rdev->mode_info.atom_context;
247         struct radeon_gpio_rec gpio;
248         int index = GetIndexIntoMasterTable(DATA, GPIO_Pin_LUT);
249         struct _ATOM_GPIO_PIN_LUT *gpio_info;
250         ATOM_GPIO_PIN_ASSIGNMENT *pin;
251         u16 data_offset, size;
252         int i, num_indices;
253
254         memset(&gpio, 0, sizeof(struct radeon_gpio_rec));
255         gpio.valid = false;
256
257         if (atom_parse_data_header(ctx, index, &size, NULL, NULL, &data_offset)) {
258                 gpio_info = (struct _ATOM_GPIO_PIN_LUT *)(ctx->bios + data_offset);
259
260                 num_indices = (size - sizeof(ATOM_COMMON_TABLE_HEADER)) /
261                         sizeof(ATOM_GPIO_PIN_ASSIGNMENT);
262
263                 for (i = 0; i < num_indices; i++) {
264                         pin = &gpio_info->asGPIO_Pin[i];
265                         if (id == pin->ucGPIO_ID) {
266                                 gpio.id = pin->ucGPIO_ID;
267                                 gpio.reg = le16_to_cpu(pin->usGpioPin_AIndex) * 4;
268                                 gpio.mask = (1 << pin->ucGpioPinBitShift);
269                                 gpio.valid = true;
270                                 break;
271                         }
272                 }
273         }
274
275         return gpio;
276 }
277
278 static struct radeon_hpd radeon_atom_get_hpd_info_from_gpio(struct radeon_device *rdev,
279                                                             struct radeon_gpio_rec *gpio)
280 {
281         struct radeon_hpd hpd;
282         u32 reg;
283
284         memset(&hpd, 0, sizeof(struct radeon_hpd));
285
286         if (ASIC_IS_DCE4(rdev))
287                 reg = EVERGREEN_DC_GPIO_HPD_A;
288         else
289                 reg = AVIVO_DC_GPIO_HPD_A;
290
291         hpd.gpio = *gpio;
292         if (gpio->reg == reg) {
293                 switch(gpio->mask) {
294                 case (1 << 0):
295                         hpd.hpd = RADEON_HPD_1;
296                         break;
297                 case (1 << 8):
298                         hpd.hpd = RADEON_HPD_2;
299                         break;
300                 case (1 << 16):
301                         hpd.hpd = RADEON_HPD_3;
302                         break;
303                 case (1 << 24):
304                         hpd.hpd = RADEON_HPD_4;
305                         break;
306                 case (1 << 26):
307                         hpd.hpd = RADEON_HPD_5;
308                         break;
309                 case (1 << 28):
310                         hpd.hpd = RADEON_HPD_6;
311                         break;
312                 default:
313                         hpd.hpd = RADEON_HPD_NONE;
314                         break;
315                 }
316         } else
317                 hpd.hpd = RADEON_HPD_NONE;
318         return hpd;
319 }
320
321 static bool radeon_atom_apply_quirks(struct drm_device *dev,
322                                      uint32_t supported_device,
323                                      int *connector_type,
324                                      struct radeon_i2c_bus_rec *i2c_bus,
325                                      uint16_t *line_mux,
326                                      struct radeon_hpd *hpd)
327 {
328
329         /* Asus M2A-VM HDMI board lists the DVI port as HDMI */
330         if ((dev->pdev->device == 0x791e) &&
331             (dev->pdev->subsystem_vendor == 0x1043) &&
332             (dev->pdev->subsystem_device == 0x826d)) {
333                 if ((*connector_type == DRM_MODE_CONNECTOR_HDMIA) &&
334                     (supported_device == ATOM_DEVICE_DFP3_SUPPORT))
335                         *connector_type = DRM_MODE_CONNECTOR_DVID;
336         }
337
338         /* Asrock RS600 board lists the DVI port as HDMI */
339         if ((dev->pdev->device == 0x7941) &&
340             (dev->pdev->subsystem_vendor == 0x1849) &&
341             (dev->pdev->subsystem_device == 0x7941)) {
342                 if ((*connector_type == DRM_MODE_CONNECTOR_HDMIA) &&
343                     (supported_device == ATOM_DEVICE_DFP3_SUPPORT))
344                         *connector_type = DRM_MODE_CONNECTOR_DVID;
345         }
346
347         /* MSI K9A2GM V2/V3 board has no HDMI or DVI */
348         if ((dev->pdev->device == 0x796e) &&
349             (dev->pdev->subsystem_vendor == 0x1462) &&
350             (dev->pdev->subsystem_device == 0x7302)) {
351                 if ((supported_device == ATOM_DEVICE_DFP2_SUPPORT) ||
352                     (supported_device == ATOM_DEVICE_DFP3_SUPPORT))
353                         return false;
354         }
355
356         /* a-bit f-i90hd - ciaranm on #radeonhd - this board has no DVI */
357         if ((dev->pdev->device == 0x7941) &&
358             (dev->pdev->subsystem_vendor == 0x147b) &&
359             (dev->pdev->subsystem_device == 0x2412)) {
360                 if (*connector_type == DRM_MODE_CONNECTOR_DVII)
361                         return false;
362         }
363
364         /* Falcon NW laptop lists vga ddc line for LVDS */
365         if ((dev->pdev->device == 0x5653) &&
366             (dev->pdev->subsystem_vendor == 0x1462) &&
367             (dev->pdev->subsystem_device == 0x0291)) {
368                 if (*connector_type == DRM_MODE_CONNECTOR_LVDS) {
369                         i2c_bus->valid = false;
370                         *line_mux = 53;
371                 }
372         }
373
374         /* HIS X1300 is DVI+VGA, not DVI+DVI */
375         if ((dev->pdev->device == 0x7146) &&
376             (dev->pdev->subsystem_vendor == 0x17af) &&
377             (dev->pdev->subsystem_device == 0x2058)) {
378                 if (supported_device == ATOM_DEVICE_DFP1_SUPPORT)
379                         return false;
380         }
381
382         /* Gigabyte X1300 is DVI+VGA, not DVI+DVI */
383         if ((dev->pdev->device == 0x7142) &&
384             (dev->pdev->subsystem_vendor == 0x1458) &&
385             (dev->pdev->subsystem_device == 0x2134)) {
386                 if (supported_device == ATOM_DEVICE_DFP1_SUPPORT)
387                         return false;
388         }
389
390
391         /* Funky macbooks */
392         if ((dev->pdev->device == 0x71C5) &&
393             (dev->pdev->subsystem_vendor == 0x106b) &&
394             (dev->pdev->subsystem_device == 0x0080)) {
395                 if ((supported_device == ATOM_DEVICE_CRT1_SUPPORT) ||
396                     (supported_device == ATOM_DEVICE_DFP2_SUPPORT))
397                         return false;
398                 if (supported_device == ATOM_DEVICE_CRT2_SUPPORT)
399                         *line_mux = 0x90;
400         }
401
402         /* mac rv630, rv730, others */
403         if ((supported_device == ATOM_DEVICE_TV1_SUPPORT) &&
404             (*connector_type == DRM_MODE_CONNECTOR_DVII)) {
405                 *connector_type = DRM_MODE_CONNECTOR_9PinDIN;
406                 *line_mux = CONNECTOR_7PIN_DIN_ENUM_ID1;
407         }
408
409         /* ASUS HD 3600 XT board lists the DVI port as HDMI */
410         if ((dev->pdev->device == 0x9598) &&
411             (dev->pdev->subsystem_vendor == 0x1043) &&
412             (dev->pdev->subsystem_device == 0x01da)) {
413                 if (*connector_type == DRM_MODE_CONNECTOR_HDMIA) {
414                         *connector_type = DRM_MODE_CONNECTOR_DVII;
415                 }
416         }
417
418         /* ASUS HD 3600 board lists the DVI port as HDMI */
419         if ((dev->pdev->device == 0x9598) &&
420             (dev->pdev->subsystem_vendor == 0x1043) &&
421             (dev->pdev->subsystem_device == 0x01e4)) {
422                 if (*connector_type == DRM_MODE_CONNECTOR_HDMIA) {
423                         *connector_type = DRM_MODE_CONNECTOR_DVII;
424                 }
425         }
426
427         /* ASUS HD 3450 board lists the DVI port as HDMI */
428         if ((dev->pdev->device == 0x95C5) &&
429             (dev->pdev->subsystem_vendor == 0x1043) &&
430             (dev->pdev->subsystem_device == 0x01e2)) {
431                 if (*connector_type == DRM_MODE_CONNECTOR_HDMIA) {
432                         *connector_type = DRM_MODE_CONNECTOR_DVII;
433                 }
434         }
435
436         /* some BIOSes seem to report DAC on HDMI - usually this is a board with
437          * HDMI + VGA reporting as HDMI
438          */
439         if (*connector_type == DRM_MODE_CONNECTOR_HDMIA) {
440                 if (supported_device & (ATOM_DEVICE_CRT_SUPPORT)) {
441                         *connector_type = DRM_MODE_CONNECTOR_VGA;
442                         *line_mux = 0;
443                 }
444         }
445
446         /* Acer laptop (Acer TravelMate 5730/5730G) has an HDMI port
447          * on the laptop and a DVI port on the docking station and
448          * both share the same encoder, hpd pin, and ddc line.
449          * So while the bios table is technically correct,
450          * we drop the DVI port here since xrandr has no concept of
451          * encoders and will try and drive both connectors
452          * with different crtcs which isn't possible on the hardware
453          * side and leaves no crtcs for LVDS or VGA.
454          */
455         if (((dev->pdev->device == 0x95c4) || (dev->pdev->device == 0x9591)) &&
456             (dev->pdev->subsystem_vendor == 0x1025) &&
457             (dev->pdev->subsystem_device == 0x013c)) {
458                 if ((*connector_type == DRM_MODE_CONNECTOR_DVII) &&
459                     (supported_device == ATOM_DEVICE_DFP1_SUPPORT)) {
460                         /* actually it's a DVI-D port not DVI-I */
461                         *connector_type = DRM_MODE_CONNECTOR_DVID;
462                         return false;
463                 }
464         }
465
466         /* XFX Pine Group device rv730 reports no VGA DDC lines
467          * even though they are wired up to record 0x93
468          */
469         if ((dev->pdev->device == 0x9498) &&
470             (dev->pdev->subsystem_vendor == 0x1682) &&
471             (dev->pdev->subsystem_device == 0x2452)) {
472                 struct radeon_device *rdev = dev->dev_private;
473                 *i2c_bus = radeon_lookup_i2c_gpio(rdev, 0x93);
474         }
475         return true;
476 }
477
478 const int supported_devices_connector_convert[] = {
479         DRM_MODE_CONNECTOR_Unknown,
480         DRM_MODE_CONNECTOR_VGA,
481         DRM_MODE_CONNECTOR_DVII,
482         DRM_MODE_CONNECTOR_DVID,
483         DRM_MODE_CONNECTOR_DVIA,
484         DRM_MODE_CONNECTOR_SVIDEO,
485         DRM_MODE_CONNECTOR_Composite,
486         DRM_MODE_CONNECTOR_LVDS,
487         DRM_MODE_CONNECTOR_Unknown,
488         DRM_MODE_CONNECTOR_Unknown,
489         DRM_MODE_CONNECTOR_HDMIA,
490         DRM_MODE_CONNECTOR_HDMIB,
491         DRM_MODE_CONNECTOR_Unknown,
492         DRM_MODE_CONNECTOR_Unknown,
493         DRM_MODE_CONNECTOR_9PinDIN,
494         DRM_MODE_CONNECTOR_DisplayPort
495 };
496
497 const uint16_t supported_devices_connector_object_id_convert[] = {
498         CONNECTOR_OBJECT_ID_NONE,
499         CONNECTOR_OBJECT_ID_VGA,
500         CONNECTOR_OBJECT_ID_DUAL_LINK_DVI_I, /* not all boards support DL */
501         CONNECTOR_OBJECT_ID_DUAL_LINK_DVI_D, /* not all boards support DL */
502         CONNECTOR_OBJECT_ID_VGA, /* technically DVI-A */
503         CONNECTOR_OBJECT_ID_COMPOSITE,
504         CONNECTOR_OBJECT_ID_SVIDEO,
505         CONNECTOR_OBJECT_ID_LVDS,
506         CONNECTOR_OBJECT_ID_9PIN_DIN,
507         CONNECTOR_OBJECT_ID_9PIN_DIN,
508         CONNECTOR_OBJECT_ID_DISPLAYPORT,
509         CONNECTOR_OBJECT_ID_HDMI_TYPE_A,
510         CONNECTOR_OBJECT_ID_HDMI_TYPE_B,
511         CONNECTOR_OBJECT_ID_SVIDEO
512 };
513
514 const int object_connector_convert[] = {
515         DRM_MODE_CONNECTOR_Unknown,
516         DRM_MODE_CONNECTOR_DVII,
517         DRM_MODE_CONNECTOR_DVII,
518         DRM_MODE_CONNECTOR_DVID,
519         DRM_MODE_CONNECTOR_DVID,
520         DRM_MODE_CONNECTOR_VGA,
521         DRM_MODE_CONNECTOR_Composite,
522         DRM_MODE_CONNECTOR_SVIDEO,
523         DRM_MODE_CONNECTOR_Unknown,
524         DRM_MODE_CONNECTOR_Unknown,
525         DRM_MODE_CONNECTOR_9PinDIN,
526         DRM_MODE_CONNECTOR_Unknown,
527         DRM_MODE_CONNECTOR_HDMIA,
528         DRM_MODE_CONNECTOR_HDMIB,
529         DRM_MODE_CONNECTOR_LVDS,
530         DRM_MODE_CONNECTOR_9PinDIN,
531         DRM_MODE_CONNECTOR_Unknown,
532         DRM_MODE_CONNECTOR_Unknown,
533         DRM_MODE_CONNECTOR_Unknown,
534         DRM_MODE_CONNECTOR_DisplayPort,
535         DRM_MODE_CONNECTOR_eDP,
536         DRM_MODE_CONNECTOR_Unknown
537 };
538
539 bool radeon_get_atom_connector_info_from_object_table(struct drm_device *dev)
540 {
541         struct radeon_device *rdev = dev->dev_private;
542         struct radeon_mode_info *mode_info = &rdev->mode_info;
543         struct atom_context *ctx = mode_info->atom_context;
544         int index = GetIndexIntoMasterTable(DATA, Object_Header);
545         u16 size, data_offset;
546         u8 frev, crev;
547         ATOM_CONNECTOR_OBJECT_TABLE *con_obj;
548         ATOM_ENCODER_OBJECT_TABLE *enc_obj;
549         ATOM_OBJECT_TABLE *router_obj;
550         ATOM_DISPLAY_OBJECT_PATH_TABLE *path_obj;
551         ATOM_OBJECT_HEADER *obj_header;
552         int i, j, k, path_size, device_support;
553         int connector_type;
554         u16 igp_lane_info, conn_id, connector_object_id;
555         struct radeon_i2c_bus_rec ddc_bus;
556         struct radeon_router router;
557         struct radeon_gpio_rec gpio;
558         struct radeon_hpd hpd;
559
560         if (!atom_parse_data_header(ctx, index, &size, &frev, &crev, &data_offset))
561                 return false;
562
563         if (crev < 2)
564                 return false;
565
566         obj_header = (ATOM_OBJECT_HEADER *) (ctx->bios + data_offset);
567         path_obj = (ATOM_DISPLAY_OBJECT_PATH_TABLE *)
568             (ctx->bios + data_offset +
569              le16_to_cpu(obj_header->usDisplayPathTableOffset));
570         con_obj = (ATOM_CONNECTOR_OBJECT_TABLE *)
571             (ctx->bios + data_offset +
572              le16_to_cpu(obj_header->usConnectorObjectTableOffset));
573         enc_obj = (ATOM_ENCODER_OBJECT_TABLE *)
574             (ctx->bios + data_offset +
575              le16_to_cpu(obj_header->usEncoderObjectTableOffset));
576         router_obj = (ATOM_OBJECT_TABLE *)
577                 (ctx->bios + data_offset +
578                  le16_to_cpu(obj_header->usRouterObjectTableOffset));
579         device_support = le16_to_cpu(obj_header->usDeviceSupport);
580
581         path_size = 0;
582         for (i = 0; i < path_obj->ucNumOfDispPath; i++) {
583                 uint8_t *addr = (uint8_t *) path_obj->asDispPath;
584                 ATOM_DISPLAY_OBJECT_PATH *path;
585                 addr += path_size;
586                 path = (ATOM_DISPLAY_OBJECT_PATH *) addr;
587                 path_size += le16_to_cpu(path->usSize);
588
589                 if (device_support & le16_to_cpu(path->usDeviceTag)) {
590                         uint8_t con_obj_id, con_obj_num, con_obj_type;
591
592                         con_obj_id =
593                             (le16_to_cpu(path->usConnObjectId) & OBJECT_ID_MASK)
594                             >> OBJECT_ID_SHIFT;
595                         con_obj_num =
596                             (le16_to_cpu(path->usConnObjectId) & ENUM_ID_MASK)
597                             >> ENUM_ID_SHIFT;
598                         con_obj_type =
599                             (le16_to_cpu(path->usConnObjectId) &
600                              OBJECT_TYPE_MASK) >> OBJECT_TYPE_SHIFT;
601
602                         /* TODO CV support */
603                         if (le16_to_cpu(path->usDeviceTag) ==
604                                 ATOM_DEVICE_CV_SUPPORT)
605                                 continue;
606
607                         /* IGP chips */
608                         if ((rdev->flags & RADEON_IS_IGP) &&
609                             (con_obj_id ==
610                              CONNECTOR_OBJECT_ID_PCIE_CONNECTOR)) {
611                                 uint16_t igp_offset = 0;
612                                 ATOM_INTEGRATED_SYSTEM_INFO_V2 *igp_obj;
613
614                                 index =
615                                     GetIndexIntoMasterTable(DATA,
616                                                             IntegratedSystemInfo);
617
618                                 if (atom_parse_data_header(ctx, index, &size, &frev,
619                                                            &crev, &igp_offset)) {
620
621                                         if (crev >= 2) {
622                                                 igp_obj =
623                                                         (ATOM_INTEGRATED_SYSTEM_INFO_V2
624                                                          *) (ctx->bios + igp_offset);
625
626                                                 if (igp_obj) {
627                                                         uint32_t slot_config, ct;
628
629                                                         if (con_obj_num == 1)
630                                                                 slot_config =
631                                                                         igp_obj->
632                                                                         ulDDISlot1Config;
633                                                         else
634                                                                 slot_config =
635                                                                         igp_obj->
636                                                                         ulDDISlot2Config;
637
638                                                         ct = (slot_config >> 16) & 0xff;
639                                                         connector_type =
640                                                                 object_connector_convert
641                                                                 [ct];
642                                                         connector_object_id = ct;
643                                                         igp_lane_info =
644                                                                 slot_config & 0xffff;
645                                                 } else
646                                                         continue;
647                                         } else
648                                                 continue;
649                                 } else {
650                                         igp_lane_info = 0;
651                                         connector_type =
652                                                 object_connector_convert[con_obj_id];
653                                         connector_object_id = con_obj_id;
654                                 }
655                         } else {
656                                 igp_lane_info = 0;
657                                 connector_type =
658                                     object_connector_convert[con_obj_id];
659                                 connector_object_id = con_obj_id;
660                         }
661
662                         if (connector_type == DRM_MODE_CONNECTOR_Unknown)
663                                 continue;
664
665                         router.ddc_valid = false;
666                         router.cd_valid = false;
667                         for (j = 0; j < ((le16_to_cpu(path->usSize) - 8) / 2); j++) {
668                                 uint8_t grph_obj_id, grph_obj_num, grph_obj_type;
669
670                                 grph_obj_id =
671                                     (le16_to_cpu(path->usGraphicObjIds[j]) &
672                                      OBJECT_ID_MASK) >> OBJECT_ID_SHIFT;
673                                 grph_obj_num =
674                                     (le16_to_cpu(path->usGraphicObjIds[j]) &
675                                      ENUM_ID_MASK) >> ENUM_ID_SHIFT;
676                                 grph_obj_type =
677                                     (le16_to_cpu(path->usGraphicObjIds[j]) &
678                                      OBJECT_TYPE_MASK) >> OBJECT_TYPE_SHIFT;
679
680                                 if (grph_obj_type == GRAPH_OBJECT_TYPE_ENCODER) {
681                                         for (k = 0; k < enc_obj->ucNumberOfObjects; k++) {
682                                                 u16 encoder_obj = le16_to_cpu(enc_obj->asObjects[k].usObjectID);
683                                                 if (le16_to_cpu(path->usGraphicObjIds[j]) == encoder_obj) {
684                                                         ATOM_COMMON_RECORD_HEADER *record = (ATOM_COMMON_RECORD_HEADER *)
685                                                                 (ctx->bios + data_offset +
686                                                                  le16_to_cpu(enc_obj->asObjects[k].usRecordOffset));
687                                                         ATOM_ENCODER_CAP_RECORD *cap_record;
688                                                         u16 caps = 0;
689
690                                                         while (record->ucRecordSize > 0 &&
691                                                                record->ucRecordType > 0 &&
692                                                                record->ucRecordType <= ATOM_MAX_OBJECT_RECORD_NUMBER) {
693                                                                 switch (record->ucRecordType) {
694                                                                 case ATOM_ENCODER_CAP_RECORD_TYPE:
695                                                                         cap_record =(ATOM_ENCODER_CAP_RECORD *)
696                                                                                 record;
697                                                                         caps = le16_to_cpu(cap_record->usEncoderCap);
698                                                                         break;
699                                                                 }
700                                                                 record = (ATOM_COMMON_RECORD_HEADER *)
701                                                                         ((char *)record + record->ucRecordSize);
702                                                         }
703                                                         radeon_add_atom_encoder(dev,
704                                                                                 encoder_obj,
705                                                                                 le16_to_cpu
706                                                                                 (path->
707                                                                                  usDeviceTag),
708                                                                                 caps);
709                                                 }
710                                         }
711                                 } else if (grph_obj_type == GRAPH_OBJECT_TYPE_ROUTER) {
712                                         for (k = 0; k < router_obj->ucNumberOfObjects; k++) {
713                                                 u16 router_obj_id = le16_to_cpu(router_obj->asObjects[k].usObjectID);
714                                                 if (le16_to_cpu(path->usGraphicObjIds[j]) == router_obj_id) {
715                                                         ATOM_COMMON_RECORD_HEADER *record = (ATOM_COMMON_RECORD_HEADER *)
716                                                                 (ctx->bios + data_offset +
717                                                                  le16_to_cpu(router_obj->asObjects[k].usRecordOffset));
718                                                         ATOM_I2C_RECORD *i2c_record;
719                                                         ATOM_I2C_ID_CONFIG_ACCESS *i2c_config;
720                                                         ATOM_ROUTER_DDC_PATH_SELECT_RECORD *ddc_path;
721                                                         ATOM_ROUTER_DATA_CLOCK_PATH_SELECT_RECORD *cd_path;
722                                                         ATOM_SRC_DST_TABLE_FOR_ONE_OBJECT *router_src_dst_table =
723                                                                 (ATOM_SRC_DST_TABLE_FOR_ONE_OBJECT *)
724                                                                 (ctx->bios + data_offset +
725                                                                  le16_to_cpu(router_obj->asObjects[k].usSrcDstTableOffset));
726                                                         int enum_id;
727
728                                                         router.router_id = router_obj_id;
729                                                         for (enum_id = 0; enum_id < router_src_dst_table->ucNumberOfDst;
730                                                              enum_id++) {
731                                                                 if (le16_to_cpu(path->usConnObjectId) ==
732                                                                     le16_to_cpu(router_src_dst_table->usDstObjectID[enum_id]))
733                                                                         break;
734                                                         }
735
736                                                         while (record->ucRecordSize > 0 &&
737                                                                record->ucRecordType > 0 &&
738                                                                record->ucRecordType <= ATOM_MAX_OBJECT_RECORD_NUMBER) {
739                                                                 switch (record->ucRecordType) {
740                                                                 case ATOM_I2C_RECORD_TYPE:
741                                                                         i2c_record =
742                                                                                 (ATOM_I2C_RECORD *)
743                                                                                 record;
744                                                                         i2c_config =
745                                                                                 (ATOM_I2C_ID_CONFIG_ACCESS *)
746                                                                                 &i2c_record->sucI2cId;
747                                                                         router.i2c_info =
748                                                                                 radeon_lookup_i2c_gpio(rdev,
749                                                                                                        i2c_config->
750                                                                                                        ucAccess);
751                                                                         router.i2c_addr = i2c_record->ucI2CAddr >> 1;
752                                                                         break;
753                                                                 case ATOM_ROUTER_DDC_PATH_SELECT_RECORD_TYPE:
754                                                                         ddc_path = (ATOM_ROUTER_DDC_PATH_SELECT_RECORD *)
755                                                                                 record;
756                                                                         router.ddc_valid = true;
757                                                                         router.ddc_mux_type = ddc_path->ucMuxType;
758                                                                         router.ddc_mux_control_pin = ddc_path->ucMuxControlPin;
759                                                                         router.ddc_mux_state = ddc_path->ucMuxState[enum_id];
760                                                                         break;
761                                                                 case ATOM_ROUTER_DATA_CLOCK_PATH_SELECT_RECORD_TYPE:
762                                                                         cd_path = (ATOM_ROUTER_DATA_CLOCK_PATH_SELECT_RECORD *)
763                                                                                 record;
764                                                                         router.cd_valid = true;
765                                                                         router.cd_mux_type = cd_path->ucMuxType;
766                                                                         router.cd_mux_control_pin = cd_path->ucMuxControlPin;
767                                                                         router.cd_mux_state = cd_path->ucMuxState[enum_id];
768                                                                         break;
769                                                                 }
770                                                                 record = (ATOM_COMMON_RECORD_HEADER *)
771                                                                         ((char *)record + record->ucRecordSize);
772                                                         }
773                                                 }
774                                         }
775                                 }
776                         }
777
778                         /* look up gpio for ddc, hpd */
779                         ddc_bus.valid = false;
780                         hpd.hpd = RADEON_HPD_NONE;
781                         if ((le16_to_cpu(path->usDeviceTag) &
782                              (ATOM_DEVICE_TV_SUPPORT | ATOM_DEVICE_CV_SUPPORT)) == 0) {
783                                 for (j = 0; j < con_obj->ucNumberOfObjects; j++) {
784                                         if (le16_to_cpu(path->usConnObjectId) ==
785                                             le16_to_cpu(con_obj->asObjects[j].
786                                                         usObjectID)) {
787                                                 ATOM_COMMON_RECORD_HEADER
788                                                     *record =
789                                                     (ATOM_COMMON_RECORD_HEADER
790                                                      *)
791                                                     (ctx->bios + data_offset +
792                                                      le16_to_cpu(con_obj->
793                                                                  asObjects[j].
794                                                                  usRecordOffset));
795                                                 ATOM_I2C_RECORD *i2c_record;
796                                                 ATOM_HPD_INT_RECORD *hpd_record;
797                                                 ATOM_I2C_ID_CONFIG_ACCESS *i2c_config;
798
799                                                 while (record->ucRecordSize > 0 &&
800                                                        record->ucRecordType > 0 &&
801                                                        record->ucRecordType <= ATOM_MAX_OBJECT_RECORD_NUMBER) {
802                                                         switch (record->ucRecordType) {
803                                                         case ATOM_I2C_RECORD_TYPE:
804                                                                 i2c_record =
805                                                                     (ATOM_I2C_RECORD *)
806                                                                         record;
807                                                                 i2c_config =
808                                                                         (ATOM_I2C_ID_CONFIG_ACCESS *)
809                                                                         &i2c_record->sucI2cId;
810                                                                 ddc_bus = radeon_lookup_i2c_gpio(rdev,
811                                                                                                  i2c_config->
812                                                                                                  ucAccess);
813                                                                 break;
814                                                         case ATOM_HPD_INT_RECORD_TYPE:
815                                                                 hpd_record =
816                                                                         (ATOM_HPD_INT_RECORD *)
817                                                                         record;
818                                                                 gpio = radeon_lookup_gpio(rdev,
819                                                                                           hpd_record->ucHPDIntGPIOID);
820                                                                 hpd = radeon_atom_get_hpd_info_from_gpio(rdev, &gpio);
821                                                                 hpd.plugged_state = hpd_record->ucPlugged_PinState;
822                                                                 break;
823                                                         }
824                                                         record =
825                                                             (ATOM_COMMON_RECORD_HEADER
826                                                              *) ((char *)record
827                                                                  +
828                                                                  record->
829                                                                  ucRecordSize);
830                                                 }
831                                                 break;
832                                         }
833                                 }
834                         }
835
836                         /* needed for aux chan transactions */
837                         ddc_bus.hpd = hpd.hpd;
838
839                         conn_id = le16_to_cpu(path->usConnObjectId);
840
841                         if (!radeon_atom_apply_quirks
842                             (dev, le16_to_cpu(path->usDeviceTag), &connector_type,
843                              &ddc_bus, &conn_id, &hpd))
844                                 continue;
845
846                         radeon_add_atom_connector(dev,
847                                                   conn_id,
848                                                   le16_to_cpu(path->
849                                                               usDeviceTag),
850                                                   connector_type, &ddc_bus,
851                                                   igp_lane_info,
852                                                   connector_object_id,
853                                                   &hpd,
854                                                   &router);
855
856                 }
857         }
858
859         radeon_link_encoder_connector(dev);
860
861         return true;
862 }
863
864 static uint16_t atombios_get_connector_object_id(struct drm_device *dev,
865                                                  int connector_type,
866                                                  uint16_t devices)
867 {
868         struct radeon_device *rdev = dev->dev_private;
869
870         if (rdev->flags & RADEON_IS_IGP) {
871                 return supported_devices_connector_object_id_convert
872                         [connector_type];
873         } else if (((connector_type == DRM_MODE_CONNECTOR_DVII) ||
874                     (connector_type == DRM_MODE_CONNECTOR_DVID)) &&
875                    (devices & ATOM_DEVICE_DFP2_SUPPORT))  {
876                 struct radeon_mode_info *mode_info = &rdev->mode_info;
877                 struct atom_context *ctx = mode_info->atom_context;
878                 int index = GetIndexIntoMasterTable(DATA, XTMDS_Info);
879                 uint16_t size, data_offset;
880                 uint8_t frev, crev;
881                 ATOM_XTMDS_INFO *xtmds;
882
883                 if (atom_parse_data_header(ctx, index, &size, &frev, &crev, &data_offset)) {
884                         xtmds = (ATOM_XTMDS_INFO *)(ctx->bios + data_offset);
885
886                         if (xtmds->ucSupportedLink & ATOM_XTMDS_SUPPORTED_DUALLINK) {
887                                 if (connector_type == DRM_MODE_CONNECTOR_DVII)
888                                         return CONNECTOR_OBJECT_ID_DUAL_LINK_DVI_I;
889                                 else
890                                         return CONNECTOR_OBJECT_ID_DUAL_LINK_DVI_D;
891                         } else {
892                                 if (connector_type == DRM_MODE_CONNECTOR_DVII)
893                                         return CONNECTOR_OBJECT_ID_SINGLE_LINK_DVI_I;
894                                 else
895                                         return CONNECTOR_OBJECT_ID_SINGLE_LINK_DVI_D;
896                         }
897                 } else
898                         return supported_devices_connector_object_id_convert
899                                 [connector_type];
900         } else {
901                 return supported_devices_connector_object_id_convert
902                         [connector_type];
903         }
904 }
905
906 struct bios_connector {
907         bool valid;
908         uint16_t line_mux;
909         uint16_t devices;
910         int connector_type;
911         struct radeon_i2c_bus_rec ddc_bus;
912         struct radeon_hpd hpd;
913 };
914
915 bool radeon_get_atom_connector_info_from_supported_devices_table(struct
916                                                                  drm_device
917                                                                  *dev)
918 {
919         struct radeon_device *rdev = dev->dev_private;
920         struct radeon_mode_info *mode_info = &rdev->mode_info;
921         struct atom_context *ctx = mode_info->atom_context;
922         int index = GetIndexIntoMasterTable(DATA, SupportedDevicesInfo);
923         uint16_t size, data_offset;
924         uint8_t frev, crev;
925         uint16_t device_support;
926         uint8_t dac;
927         union atom_supported_devices *supported_devices;
928         int i, j, max_device;
929         struct bios_connector *bios_connectors;
930         size_t bc_size = sizeof(*bios_connectors) * ATOM_MAX_SUPPORTED_DEVICE;
931         struct radeon_router router;
932
933         router.ddc_valid = false;
934         router.cd_valid = false;
935
936         bios_connectors = kzalloc(bc_size, GFP_KERNEL);
937         if (!bios_connectors)
938                 return false;
939
940         if (!atom_parse_data_header(ctx, index, &size, &frev, &crev,
941                                     &data_offset)) {
942                 kfree(bios_connectors);
943                 return false;
944         }
945
946         supported_devices =
947             (union atom_supported_devices *)(ctx->bios + data_offset);
948
949         device_support = le16_to_cpu(supported_devices->info.usDeviceSupport);
950
951         if (frev > 1)
952                 max_device = ATOM_MAX_SUPPORTED_DEVICE;
953         else
954                 max_device = ATOM_MAX_SUPPORTED_DEVICE_INFO;
955
956         for (i = 0; i < max_device; i++) {
957                 ATOM_CONNECTOR_INFO_I2C ci =
958                     supported_devices->info.asConnInfo[i];
959
960                 bios_connectors[i].valid = false;
961
962                 if (!(device_support & (1 << i))) {
963                         continue;
964                 }
965
966                 if (i == ATOM_DEVICE_CV_INDEX) {
967                         DRM_DEBUG_KMS("Skipping Component Video\n");
968                         continue;
969                 }
970
971                 bios_connectors[i].connector_type =
972                     supported_devices_connector_convert[ci.sucConnectorInfo.
973                                                         sbfAccess.
974                                                         bfConnectorType];
975
976                 if (bios_connectors[i].connector_type ==
977                     DRM_MODE_CONNECTOR_Unknown)
978                         continue;
979
980                 dac = ci.sucConnectorInfo.sbfAccess.bfAssociatedDAC;
981
982                 bios_connectors[i].line_mux =
983                         ci.sucI2cId.ucAccess;
984
985                 /* give tv unique connector ids */
986                 if (i == ATOM_DEVICE_TV1_INDEX) {
987                         bios_connectors[i].ddc_bus.valid = false;
988                         bios_connectors[i].line_mux = 50;
989                 } else if (i == ATOM_DEVICE_TV2_INDEX) {
990                         bios_connectors[i].ddc_bus.valid = false;
991                         bios_connectors[i].line_mux = 51;
992                 } else if (i == ATOM_DEVICE_CV_INDEX) {
993                         bios_connectors[i].ddc_bus.valid = false;
994                         bios_connectors[i].line_mux = 52;
995                 } else
996                         bios_connectors[i].ddc_bus =
997                             radeon_lookup_i2c_gpio(rdev,
998                                                    bios_connectors[i].line_mux);
999
1000                 if ((crev > 1) && (frev > 1)) {
1001                         u8 isb = supported_devices->info_2d1.asIntSrcInfo[i].ucIntSrcBitmap;
1002                         switch (isb) {
1003                         case 0x4:
1004                                 bios_connectors[i].hpd.hpd = RADEON_HPD_1;
1005                                 break;
1006                         case 0xa:
1007                                 bios_connectors[i].hpd.hpd = RADEON_HPD_2;
1008                                 break;
1009                         default:
1010                                 bios_connectors[i].hpd.hpd = RADEON_HPD_NONE;
1011                                 break;
1012                         }
1013                 } else {
1014                         if (i == ATOM_DEVICE_DFP1_INDEX)
1015                                 bios_connectors[i].hpd.hpd = RADEON_HPD_1;
1016                         else if (i == ATOM_DEVICE_DFP2_INDEX)
1017                                 bios_connectors[i].hpd.hpd = RADEON_HPD_2;
1018                         else
1019                                 bios_connectors[i].hpd.hpd = RADEON_HPD_NONE;
1020                 }
1021
1022                 /* Always set the connector type to VGA for CRT1/CRT2. if they are
1023                  * shared with a DVI port, we'll pick up the DVI connector when we
1024                  * merge the outputs.  Some bioses incorrectly list VGA ports as DVI.
1025                  */
1026                 if (i == ATOM_DEVICE_CRT1_INDEX || i == ATOM_DEVICE_CRT2_INDEX)
1027                         bios_connectors[i].connector_type =
1028                             DRM_MODE_CONNECTOR_VGA;
1029
1030                 if (!radeon_atom_apply_quirks
1031                     (dev, (1 << i), &bios_connectors[i].connector_type,
1032                      &bios_connectors[i].ddc_bus, &bios_connectors[i].line_mux,
1033                      &bios_connectors[i].hpd))
1034                         continue;
1035
1036                 bios_connectors[i].valid = true;
1037                 bios_connectors[i].devices = (1 << i);
1038
1039                 if (ASIC_IS_AVIVO(rdev) || radeon_r4xx_atom)
1040                         radeon_add_atom_encoder(dev,
1041                                                 radeon_get_encoder_enum(dev,
1042                                                                       (1 << i),
1043                                                                       dac),
1044                                                 (1 << i),
1045                                                 0);
1046                 else
1047                         radeon_add_legacy_encoder(dev,
1048                                                   radeon_get_encoder_enum(dev,
1049                                                                         (1 << i),
1050                                                                         dac),
1051                                                   (1 << i));
1052         }
1053
1054         /* combine shared connectors */
1055         for (i = 0; i < max_device; i++) {
1056                 if (bios_connectors[i].valid) {
1057                         for (j = 0; j < max_device; j++) {
1058                                 if (bios_connectors[j].valid && (i != j)) {
1059                                         if (bios_connectors[i].line_mux ==
1060                                             bios_connectors[j].line_mux) {
1061                                                 /* make sure not to combine LVDS */
1062                                                 if (bios_connectors[i].devices & (ATOM_DEVICE_LCD_SUPPORT)) {
1063                                                         bios_connectors[i].line_mux = 53;
1064                                                         bios_connectors[i].ddc_bus.valid = false;
1065                                                         continue;
1066                                                 }
1067                                                 if (bios_connectors[j].devices & (ATOM_DEVICE_LCD_SUPPORT)) {
1068                                                         bios_connectors[j].line_mux = 53;
1069                                                         bios_connectors[j].ddc_bus.valid = false;
1070                                                         continue;
1071                                                 }
1072                                                 /* combine analog and digital for DVI-I */
1073                                                 if (((bios_connectors[i].devices & (ATOM_DEVICE_DFP_SUPPORT)) &&
1074                                                      (bios_connectors[j].devices & (ATOM_DEVICE_CRT_SUPPORT))) ||
1075                                                     ((bios_connectors[j].devices & (ATOM_DEVICE_DFP_SUPPORT)) &&
1076                                                      (bios_connectors[i].devices & (ATOM_DEVICE_CRT_SUPPORT)))) {
1077                                                         bios_connectors[i].devices |=
1078                                                                 bios_connectors[j].devices;
1079                                                         bios_connectors[i].connector_type =
1080                                                                 DRM_MODE_CONNECTOR_DVII;
1081                                                         if (bios_connectors[j].devices & (ATOM_DEVICE_DFP_SUPPORT))
1082                                                                 bios_connectors[i].hpd =
1083                                                                         bios_connectors[j].hpd;
1084                                                         bios_connectors[j].valid = false;
1085                                                 }
1086                                         }
1087                                 }
1088                         }
1089                 }
1090         }
1091
1092         /* add the connectors */
1093         for (i = 0; i < max_device; i++) {
1094                 if (bios_connectors[i].valid) {
1095                         uint16_t connector_object_id =
1096                                 atombios_get_connector_object_id(dev,
1097                                                       bios_connectors[i].connector_type,
1098                                                       bios_connectors[i].devices);
1099                         radeon_add_atom_connector(dev,
1100                                                   bios_connectors[i].line_mux,
1101                                                   bios_connectors[i].devices,
1102                                                   bios_connectors[i].
1103                                                   connector_type,
1104                                                   &bios_connectors[i].ddc_bus,
1105                                                   0,
1106                                                   connector_object_id,
1107                                                   &bios_connectors[i].hpd,
1108                                                   &router);
1109                 }
1110         }
1111
1112         radeon_link_encoder_connector(dev);
1113
1114         kfree(bios_connectors);
1115         return true;
1116 }
1117
1118 union firmware_info {
1119         ATOM_FIRMWARE_INFO info;
1120         ATOM_FIRMWARE_INFO_V1_2 info_12;
1121         ATOM_FIRMWARE_INFO_V1_3 info_13;
1122         ATOM_FIRMWARE_INFO_V1_4 info_14;
1123         ATOM_FIRMWARE_INFO_V2_1 info_21;
1124         ATOM_FIRMWARE_INFO_V2_2 info_22;
1125 };
1126
1127 bool radeon_atom_get_clock_info(struct drm_device *dev)
1128 {
1129         struct radeon_device *rdev = dev->dev_private;
1130         struct radeon_mode_info *mode_info = &rdev->mode_info;
1131         int index = GetIndexIntoMasterTable(DATA, FirmwareInfo);
1132         union firmware_info *firmware_info;
1133         uint8_t frev, crev;
1134         struct radeon_pll *p1pll = &rdev->clock.p1pll;
1135         struct radeon_pll *p2pll = &rdev->clock.p2pll;
1136         struct radeon_pll *dcpll = &rdev->clock.dcpll;
1137         struct radeon_pll *spll = &rdev->clock.spll;
1138         struct radeon_pll *mpll = &rdev->clock.mpll;
1139         uint16_t data_offset;
1140
1141         if (atom_parse_data_header(mode_info->atom_context, index, NULL,
1142                                    &frev, &crev, &data_offset)) {
1143                 firmware_info =
1144                         (union firmware_info *)(mode_info->atom_context->bios +
1145                                                 data_offset);
1146                 /* pixel clocks */
1147                 p1pll->reference_freq =
1148                     le16_to_cpu(firmware_info->info.usReferenceClock);
1149                 p1pll->reference_div = 0;
1150
1151                 if (crev < 2)
1152                         p1pll->pll_out_min =
1153                                 le16_to_cpu(firmware_info->info.usMinPixelClockPLL_Output);
1154                 else
1155                         p1pll->pll_out_min =
1156                                 le32_to_cpu(firmware_info->info_12.ulMinPixelClockPLL_Output);
1157                 p1pll->pll_out_max =
1158                     le32_to_cpu(firmware_info->info.ulMaxPixelClockPLL_Output);
1159
1160                 if (crev >= 4) {
1161                         p1pll->lcd_pll_out_min =
1162                                 le16_to_cpu(firmware_info->info_14.usLcdMinPixelClockPLL_Output) * 100;
1163                         if (p1pll->lcd_pll_out_min == 0)
1164                                 p1pll->lcd_pll_out_min = p1pll->pll_out_min;
1165                         p1pll->lcd_pll_out_max =
1166                                 le16_to_cpu(firmware_info->info_14.usLcdMaxPixelClockPLL_Output) * 100;
1167                         if (p1pll->lcd_pll_out_max == 0)
1168                                 p1pll->lcd_pll_out_max = p1pll->pll_out_max;
1169                 } else {
1170                         p1pll->lcd_pll_out_min = p1pll->pll_out_min;
1171                         p1pll->lcd_pll_out_max = p1pll->pll_out_max;
1172                 }
1173
1174                 if (p1pll->pll_out_min == 0) {
1175                         if (ASIC_IS_AVIVO(rdev))
1176                                 p1pll->pll_out_min = 64800;
1177                         else
1178                                 p1pll->pll_out_min = 20000;
1179                 }
1180
1181                 p1pll->pll_in_min =
1182                     le16_to_cpu(firmware_info->info.usMinPixelClockPLL_Input);
1183                 p1pll->pll_in_max =
1184                     le16_to_cpu(firmware_info->info.usMaxPixelClockPLL_Input);
1185
1186                 *p2pll = *p1pll;
1187
1188                 /* system clock */
1189                 if (ASIC_IS_DCE4(rdev))
1190                         spll->reference_freq =
1191                                 le16_to_cpu(firmware_info->info_21.usCoreReferenceClock);
1192                 else
1193                         spll->reference_freq =
1194                                 le16_to_cpu(firmware_info->info.usReferenceClock);
1195                 spll->reference_div = 0;
1196
1197                 spll->pll_out_min =
1198                     le16_to_cpu(firmware_info->info.usMinEngineClockPLL_Output);
1199                 spll->pll_out_max =
1200                     le32_to_cpu(firmware_info->info.ulMaxEngineClockPLL_Output);
1201
1202                 /* ??? */
1203                 if (spll->pll_out_min == 0) {
1204                         if (ASIC_IS_AVIVO(rdev))
1205                                 spll->pll_out_min = 64800;
1206                         else
1207                                 spll->pll_out_min = 20000;
1208                 }
1209
1210                 spll->pll_in_min =
1211                     le16_to_cpu(firmware_info->info.usMinEngineClockPLL_Input);
1212                 spll->pll_in_max =
1213                     le16_to_cpu(firmware_info->info.usMaxEngineClockPLL_Input);
1214
1215                 /* memory clock */
1216                 if (ASIC_IS_DCE4(rdev))
1217                         mpll->reference_freq =
1218                                 le16_to_cpu(firmware_info->info_21.usMemoryReferenceClock);
1219                 else
1220                         mpll->reference_freq =
1221                                 le16_to_cpu(firmware_info->info.usReferenceClock);
1222                 mpll->reference_div = 0;
1223
1224                 mpll->pll_out_min =
1225                     le16_to_cpu(firmware_info->info.usMinMemoryClockPLL_Output);
1226                 mpll->pll_out_max =
1227                     le32_to_cpu(firmware_info->info.ulMaxMemoryClockPLL_Output);
1228
1229                 /* ??? */
1230                 if (mpll->pll_out_min == 0) {
1231                         if (ASIC_IS_AVIVO(rdev))
1232                                 mpll->pll_out_min = 64800;
1233                         else
1234                                 mpll->pll_out_min = 20000;
1235                 }
1236
1237                 mpll->pll_in_min =
1238                     le16_to_cpu(firmware_info->info.usMinMemoryClockPLL_Input);
1239                 mpll->pll_in_max =
1240                     le16_to_cpu(firmware_info->info.usMaxMemoryClockPLL_Input);
1241
1242                 rdev->clock.default_sclk =
1243                     le32_to_cpu(firmware_info->info.ulDefaultEngineClock);
1244                 rdev->clock.default_mclk =
1245                     le32_to_cpu(firmware_info->info.ulDefaultMemoryClock);
1246
1247                 if (ASIC_IS_DCE4(rdev)) {
1248                         rdev->clock.default_dispclk =
1249                                 le32_to_cpu(firmware_info->info_21.ulDefaultDispEngineClkFreq);
1250                         if (rdev->clock.default_dispclk == 0) {
1251                                 if (ASIC_IS_DCE5(rdev))
1252                                         rdev->clock.default_dispclk = 54000; /* 540 Mhz */
1253                                 else
1254                                         rdev->clock.default_dispclk = 60000; /* 600 Mhz */
1255                         }
1256                         rdev->clock.dp_extclk =
1257                                 le16_to_cpu(firmware_info->info_21.usUniphyDPModeExtClkFreq);
1258                 }
1259                 *dcpll = *p1pll;
1260
1261                 rdev->clock.max_pixel_clock = le16_to_cpu(firmware_info->info.usMaxPixelClock);
1262                 if (rdev->clock.max_pixel_clock == 0)
1263                         rdev->clock.max_pixel_clock = 40000;
1264
1265                 return true;
1266         }
1267
1268         return false;
1269 }
1270
1271 union igp_info {
1272         struct _ATOM_INTEGRATED_SYSTEM_INFO info;
1273         struct _ATOM_INTEGRATED_SYSTEM_INFO_V2 info_2;
1274 };
1275
1276 bool radeon_atombios_sideport_present(struct radeon_device *rdev)
1277 {
1278         struct radeon_mode_info *mode_info = &rdev->mode_info;
1279         int index = GetIndexIntoMasterTable(DATA, IntegratedSystemInfo);
1280         union igp_info *igp_info;
1281         u8 frev, crev;
1282         u16 data_offset;
1283
1284         /* sideport is AMD only */
1285         if (rdev->family == CHIP_RS600)
1286                 return false;
1287
1288         if (atom_parse_data_header(mode_info->atom_context, index, NULL,
1289                                    &frev, &crev, &data_offset)) {
1290                 igp_info = (union igp_info *)(mode_info->atom_context->bios +
1291                                       data_offset);
1292                 switch (crev) {
1293                 case 1:
1294                         if (le32_to_cpu(igp_info->info.ulBootUpMemoryClock))
1295                                 return true;
1296                         break;
1297                 case 2:
1298                         if (le32_to_cpu(igp_info->info_2.ulBootUpSidePortClock))
1299                                 return true;
1300                         break;
1301                 default:
1302                         DRM_ERROR("Unsupported IGP table: %d %d\n", frev, crev);
1303                         break;
1304                 }
1305         }
1306         return false;
1307 }
1308
1309 bool radeon_atombios_get_tmds_info(struct radeon_encoder *encoder,
1310                                    struct radeon_encoder_int_tmds *tmds)
1311 {
1312         struct drm_device *dev = encoder->base.dev;
1313         struct radeon_device *rdev = dev->dev_private;
1314         struct radeon_mode_info *mode_info = &rdev->mode_info;
1315         int index = GetIndexIntoMasterTable(DATA, TMDS_Info);
1316         uint16_t data_offset;
1317         struct _ATOM_TMDS_INFO *tmds_info;
1318         uint8_t frev, crev;
1319         uint16_t maxfreq;
1320         int i;
1321
1322         if (atom_parse_data_header(mode_info->atom_context, index, NULL,
1323                                    &frev, &crev, &data_offset)) {
1324                 tmds_info =
1325                         (struct _ATOM_TMDS_INFO *)(mode_info->atom_context->bios +
1326                                                    data_offset);
1327
1328                 maxfreq = le16_to_cpu(tmds_info->usMaxFrequency);
1329                 for (i = 0; i < 4; i++) {
1330                         tmds->tmds_pll[i].freq =
1331                             le16_to_cpu(tmds_info->asMiscInfo[i].usFrequency);
1332                         tmds->tmds_pll[i].value =
1333                             tmds_info->asMiscInfo[i].ucPLL_ChargePump & 0x3f;
1334                         tmds->tmds_pll[i].value |=
1335                             (tmds_info->asMiscInfo[i].
1336                              ucPLL_VCO_Gain & 0x3f) << 6;
1337                         tmds->tmds_pll[i].value |=
1338                             (tmds_info->asMiscInfo[i].
1339                              ucPLL_DutyCycle & 0xf) << 12;
1340                         tmds->tmds_pll[i].value |=
1341                             (tmds_info->asMiscInfo[i].
1342                              ucPLL_VoltageSwing & 0xf) << 16;
1343
1344                         DRM_DEBUG_KMS("TMDS PLL From ATOMBIOS %u %x\n",
1345                                   tmds->tmds_pll[i].freq,
1346                                   tmds->tmds_pll[i].value);
1347
1348                         if (maxfreq == tmds->tmds_pll[i].freq) {
1349                                 tmds->tmds_pll[i].freq = 0xffffffff;
1350                                 break;
1351                         }
1352                 }
1353                 return true;
1354         }
1355         return false;
1356 }
1357
1358 bool radeon_atombios_get_ppll_ss_info(struct radeon_device *rdev,
1359                                       struct radeon_atom_ss *ss,
1360                                       int id)
1361 {
1362         struct radeon_mode_info *mode_info = &rdev->mode_info;
1363         int index = GetIndexIntoMasterTable(DATA, PPLL_SS_Info);
1364         uint16_t data_offset, size;
1365         struct _ATOM_SPREAD_SPECTRUM_INFO *ss_info;
1366         uint8_t frev, crev;
1367         int i, num_indices;
1368
1369         memset(ss, 0, sizeof(struct radeon_atom_ss));
1370         if (atom_parse_data_header(mode_info->atom_context, index, &size,
1371                                    &frev, &crev, &data_offset)) {
1372                 ss_info =
1373                         (struct _ATOM_SPREAD_SPECTRUM_INFO *)(mode_info->atom_context->bios + data_offset);
1374
1375                 num_indices = (size - sizeof(ATOM_COMMON_TABLE_HEADER)) /
1376                         sizeof(ATOM_SPREAD_SPECTRUM_ASSIGNMENT);
1377
1378                 for (i = 0; i < num_indices; i++) {
1379                         if (ss_info->asSS_Info[i].ucSS_Id == id) {
1380                                 ss->percentage =
1381                                         le16_to_cpu(ss_info->asSS_Info[i].usSpreadSpectrumPercentage);
1382                                 ss->type = ss_info->asSS_Info[i].ucSpreadSpectrumType;
1383                                 ss->step = ss_info->asSS_Info[i].ucSS_Step;
1384                                 ss->delay = ss_info->asSS_Info[i].ucSS_Delay;
1385                                 ss->range = ss_info->asSS_Info[i].ucSS_Range;
1386                                 ss->refdiv = ss_info->asSS_Info[i].ucRecommendedRef_Div;
1387                                 return true;
1388                         }
1389                 }
1390         }
1391         return false;
1392 }
1393
1394 static void radeon_atombios_get_igp_ss_overrides(struct radeon_device *rdev,
1395                                                  struct radeon_atom_ss *ss,
1396                                                  int id)
1397 {
1398         struct radeon_mode_info *mode_info = &rdev->mode_info;
1399         int index = GetIndexIntoMasterTable(DATA, IntegratedSystemInfo);
1400         u16 data_offset, size;
1401         struct _ATOM_INTEGRATED_SYSTEM_INFO_V6 *igp_info;
1402         u8 frev, crev;
1403         u16 percentage = 0, rate = 0;
1404
1405         /* get any igp specific overrides */
1406         if (atom_parse_data_header(mode_info->atom_context, index, &size,
1407                                    &frev, &crev, &data_offset)) {
1408                 igp_info = (struct _ATOM_INTEGRATED_SYSTEM_INFO_V6 *)
1409                         (mode_info->atom_context->bios + data_offset);
1410                 switch (id) {
1411                 case ASIC_INTERNAL_SS_ON_TMDS:
1412                         percentage = le16_to_cpu(igp_info->usDVISSPercentage);
1413                         rate = le16_to_cpu(igp_info->usDVISSpreadRateIn10Hz);
1414                         break;
1415                 case ASIC_INTERNAL_SS_ON_HDMI:
1416                         percentage = le16_to_cpu(igp_info->usHDMISSPercentage);
1417                         rate = le16_to_cpu(igp_info->usHDMISSpreadRateIn10Hz);
1418                         break;
1419                 case ASIC_INTERNAL_SS_ON_LVDS:
1420                         percentage = le16_to_cpu(igp_info->usLvdsSSPercentage);
1421                         rate = le16_to_cpu(igp_info->usLvdsSSpreadRateIn10Hz);
1422                         break;
1423                 }
1424                 if (percentage)
1425                         ss->percentage = percentage;
1426                 if (rate)
1427                         ss->rate = rate;
1428         }
1429 }
1430
1431 union asic_ss_info {
1432         struct _ATOM_ASIC_INTERNAL_SS_INFO info;
1433         struct _ATOM_ASIC_INTERNAL_SS_INFO_V2 info_2;
1434         struct _ATOM_ASIC_INTERNAL_SS_INFO_V3 info_3;
1435 };
1436
1437 bool radeon_atombios_get_asic_ss_info(struct radeon_device *rdev,
1438                                       struct radeon_atom_ss *ss,
1439                                       int id, u32 clock)
1440 {
1441         struct radeon_mode_info *mode_info = &rdev->mode_info;
1442         int index = GetIndexIntoMasterTable(DATA, ASIC_InternalSS_Info);
1443         uint16_t data_offset, size;
1444         union asic_ss_info *ss_info;
1445         uint8_t frev, crev;
1446         int i, num_indices;
1447
1448         memset(ss, 0, sizeof(struct radeon_atom_ss));
1449         if (atom_parse_data_header(mode_info->atom_context, index, &size,
1450                                    &frev, &crev, &data_offset)) {
1451
1452                 ss_info =
1453                         (union asic_ss_info *)(mode_info->atom_context->bios + data_offset);
1454
1455                 switch (frev) {
1456                 case 1:
1457                         num_indices = (size - sizeof(ATOM_COMMON_TABLE_HEADER)) /
1458                                 sizeof(ATOM_ASIC_SS_ASSIGNMENT);
1459
1460                         for (i = 0; i < num_indices; i++) {
1461                                 if ((ss_info->info.asSpreadSpectrum[i].ucClockIndication == id) &&
1462                                     (clock <= le32_to_cpu(ss_info->info.asSpreadSpectrum[i].ulTargetClockRange))) {
1463                                         ss->percentage =
1464                                                 le16_to_cpu(ss_info->info.asSpreadSpectrum[i].usSpreadSpectrumPercentage);
1465                                         ss->type = ss_info->info.asSpreadSpectrum[i].ucSpreadSpectrumMode;
1466                                         ss->rate = le16_to_cpu(ss_info->info.asSpreadSpectrum[i].usSpreadRateInKhz);
1467                                         return true;
1468                                 }
1469                         }
1470                         break;
1471                 case 2:
1472                         num_indices = (size - sizeof(ATOM_COMMON_TABLE_HEADER)) /
1473                                 sizeof(ATOM_ASIC_SS_ASSIGNMENT_V2);
1474                         for (i = 0; i < num_indices; i++) {
1475                                 if ((ss_info->info_2.asSpreadSpectrum[i].ucClockIndication == id) &&
1476                                     (clock <= le32_to_cpu(ss_info->info_2.asSpreadSpectrum[i].ulTargetClockRange))) {
1477                                         ss->percentage =
1478                                                 le16_to_cpu(ss_info->info_2.asSpreadSpectrum[i].usSpreadSpectrumPercentage);
1479                                         ss->type = ss_info->info_2.asSpreadSpectrum[i].ucSpreadSpectrumMode;
1480                                         ss->rate = le16_to_cpu(ss_info->info_2.asSpreadSpectrum[i].usSpreadRateIn10Hz);
1481                                         return true;
1482                                 }
1483                         }
1484                         break;
1485                 case 3:
1486                         num_indices = (size - sizeof(ATOM_COMMON_TABLE_HEADER)) /
1487                                 sizeof(ATOM_ASIC_SS_ASSIGNMENT_V3);
1488                         for (i = 0; i < num_indices; i++) {
1489                                 if ((ss_info->info_3.asSpreadSpectrum[i].ucClockIndication == id) &&
1490                                     (clock <= le32_to_cpu(ss_info->info_3.asSpreadSpectrum[i].ulTargetClockRange))) {
1491                                         ss->percentage =
1492                                                 le16_to_cpu(ss_info->info_3.asSpreadSpectrum[i].usSpreadSpectrumPercentage);
1493                                         ss->type = ss_info->info_3.asSpreadSpectrum[i].ucSpreadSpectrumMode;
1494                                         ss->rate = le16_to_cpu(ss_info->info_3.asSpreadSpectrum[i].usSpreadRateIn10Hz);
1495                                         if (rdev->flags & RADEON_IS_IGP)
1496                                                 radeon_atombios_get_igp_ss_overrides(rdev, ss, id);
1497                                         return true;
1498                                 }
1499                         }
1500                         break;
1501                 default:
1502                         DRM_ERROR("Unsupported ASIC_InternalSS_Info table: %d %d\n", frev, crev);
1503                         break;
1504                 }
1505
1506         }
1507         return false;
1508 }
1509
1510 union lvds_info {
1511         struct _ATOM_LVDS_INFO info;
1512         struct _ATOM_LVDS_INFO_V12 info_12;
1513 };
1514
1515 struct radeon_encoder_atom_dig *radeon_atombios_get_lvds_info(struct
1516                                                               radeon_encoder
1517                                                               *encoder)
1518 {
1519         struct drm_device *dev = encoder->base.dev;
1520         struct radeon_device *rdev = dev->dev_private;
1521         struct radeon_mode_info *mode_info = &rdev->mode_info;
1522         int index = GetIndexIntoMasterTable(DATA, LVDS_Info);
1523         uint16_t data_offset, misc;
1524         union lvds_info *lvds_info;
1525         uint8_t frev, crev;
1526         struct radeon_encoder_atom_dig *lvds = NULL;
1527         int encoder_enum = (encoder->encoder_enum & ENUM_ID_MASK) >> ENUM_ID_SHIFT;
1528
1529         if (atom_parse_data_header(mode_info->atom_context, index, NULL,
1530                                    &frev, &crev, &data_offset)) {
1531                 lvds_info =
1532                         (union lvds_info *)(mode_info->atom_context->bios + data_offset);
1533                 lvds =
1534                     kzalloc(sizeof(struct radeon_encoder_atom_dig), GFP_KERNEL);
1535
1536                 if (!lvds)
1537                         return NULL;
1538
1539                 lvds->native_mode.clock =
1540                     le16_to_cpu(lvds_info->info.sLCDTiming.usPixClk) * 10;
1541                 lvds->native_mode.hdisplay =
1542                     le16_to_cpu(lvds_info->info.sLCDTiming.usHActive);
1543                 lvds->native_mode.vdisplay =
1544                     le16_to_cpu(lvds_info->info.sLCDTiming.usVActive);
1545                 lvds->native_mode.htotal = lvds->native_mode.hdisplay +
1546                         le16_to_cpu(lvds_info->info.sLCDTiming.usHBlanking_Time);
1547                 lvds->native_mode.hsync_start = lvds->native_mode.hdisplay +
1548                         le16_to_cpu(lvds_info->info.sLCDTiming.usHSyncOffset);
1549                 lvds->native_mode.hsync_end = lvds->native_mode.hsync_start +
1550                         le16_to_cpu(lvds_info->info.sLCDTiming.usHSyncWidth);
1551                 lvds->native_mode.vtotal = lvds->native_mode.vdisplay +
1552                         le16_to_cpu(lvds_info->info.sLCDTiming.usVBlanking_Time);
1553                 lvds->native_mode.vsync_start = lvds->native_mode.vdisplay +
1554                         le16_to_cpu(lvds_info->info.sLCDTiming.usVSyncOffset);
1555                 lvds->native_mode.vsync_end = lvds->native_mode.vsync_start +
1556                         le16_to_cpu(lvds_info->info.sLCDTiming.usVSyncWidth);
1557                 lvds->panel_pwr_delay =
1558                     le16_to_cpu(lvds_info->info.usOffDelayInMs);
1559                 lvds->lcd_misc = lvds_info->info.ucLVDS_Misc;
1560
1561                 misc = le16_to_cpu(lvds_info->info.sLCDTiming.susModeMiscInfo.usAccess);
1562                 if (misc & ATOM_VSYNC_POLARITY)
1563                         lvds->native_mode.flags |= DRM_MODE_FLAG_NVSYNC;
1564                 if (misc & ATOM_HSYNC_POLARITY)
1565                         lvds->native_mode.flags |= DRM_MODE_FLAG_NHSYNC;
1566                 if (misc & ATOM_COMPOSITESYNC)
1567                         lvds->native_mode.flags |= DRM_MODE_FLAG_CSYNC;
1568                 if (misc & ATOM_INTERLACE)
1569                         lvds->native_mode.flags |= DRM_MODE_FLAG_INTERLACE;
1570                 if (misc & ATOM_DOUBLE_CLOCK_MODE)
1571                         lvds->native_mode.flags |= DRM_MODE_FLAG_DBLSCAN;
1572
1573                 lvds->native_mode.width_mm = le16_to_cpu(lvds_info->info.sLCDTiming.usImageHSize);
1574                 lvds->native_mode.height_mm = le16_to_cpu(lvds_info->info.sLCDTiming.usImageVSize);
1575
1576                 /* set crtc values */
1577                 drm_mode_set_crtcinfo(&lvds->native_mode, CRTC_INTERLACE_HALVE_V);
1578
1579                 lvds->lcd_ss_id = lvds_info->info.ucSS_Id;
1580
1581                 encoder->native_mode = lvds->native_mode;
1582
1583                 if (encoder_enum == 2)
1584                         lvds->linkb = true;
1585                 else
1586                         lvds->linkb = false;
1587
1588                 /* parse the lcd record table */
1589                 if (le16_to_cpu(lvds_info->info.usModePatchTableOffset)) {
1590                         ATOM_FAKE_EDID_PATCH_RECORD *fake_edid_record;
1591                         ATOM_PANEL_RESOLUTION_PATCH_RECORD *panel_res_record;
1592                         bool bad_record = false;
1593                         u8 *record;
1594
1595                         if ((frev == 1) && (crev < 2))
1596                                 /* absolute */
1597                                 record = (u8 *)(mode_info->atom_context->bios +
1598                                                 le16_to_cpu(lvds_info->info.usModePatchTableOffset));
1599                         else
1600                                 /* relative */
1601                                 record = (u8 *)(mode_info->atom_context->bios +
1602                                                 data_offset +
1603                                                 le16_to_cpu(lvds_info->info.usModePatchTableOffset));
1604                         while (*record != ATOM_RECORD_END_TYPE) {
1605                                 switch (*record) {
1606                                 case LCD_MODE_PATCH_RECORD_MODE_TYPE:
1607                                         record += sizeof(ATOM_PATCH_RECORD_MODE);
1608                                         break;
1609                                 case LCD_RTS_RECORD_TYPE:
1610                                         record += sizeof(ATOM_LCD_RTS_RECORD);
1611                                         break;
1612                                 case LCD_CAP_RECORD_TYPE:
1613                                         record += sizeof(ATOM_LCD_MODE_CONTROL_CAP);
1614                                         break;
1615                                 case LCD_FAKE_EDID_PATCH_RECORD_TYPE:
1616                                         fake_edid_record = (ATOM_FAKE_EDID_PATCH_RECORD *)record;
1617                                         if (fake_edid_record->ucFakeEDIDLength) {
1618                                                 struct edid *edid;
1619                                                 int edid_size =
1620                                                         max((int)EDID_LENGTH, (int)fake_edid_record->ucFakeEDIDLength);
1621                                                 edid = kmalloc(edid_size, GFP_KERNEL);
1622                                                 if (edid) {
1623                                                         memcpy((u8 *)edid, (u8 *)&fake_edid_record->ucFakeEDIDString[0],
1624                                                                fake_edid_record->ucFakeEDIDLength);
1625
1626                                                         if (drm_edid_is_valid(edid)) {
1627                                                                 rdev->mode_info.bios_hardcoded_edid = edid;
1628                                                                 rdev->mode_info.bios_hardcoded_edid_size = edid_size;
1629                                                         } else
1630                                                                 kfree(edid);
1631                                                 }
1632                                         }
1633                                         record += sizeof(ATOM_FAKE_EDID_PATCH_RECORD);
1634                                         break;
1635                                 case LCD_PANEL_RESOLUTION_RECORD_TYPE:
1636                                         panel_res_record = (ATOM_PANEL_RESOLUTION_PATCH_RECORD *)record;
1637                                         lvds->native_mode.width_mm = panel_res_record->usHSize;
1638                                         lvds->native_mode.height_mm = panel_res_record->usVSize;
1639                                         record += sizeof(ATOM_PANEL_RESOLUTION_PATCH_RECORD);
1640                                         break;
1641                                 default:
1642                                         DRM_ERROR("Bad LCD record %d\n", *record);
1643                                         bad_record = true;
1644                                         break;
1645                                 }
1646                                 if (bad_record)
1647                                         break;
1648                         }
1649                 }
1650         }
1651         return lvds;
1652 }
1653
1654 struct radeon_encoder_primary_dac *
1655 radeon_atombios_get_primary_dac_info(struct radeon_encoder *encoder)
1656 {
1657         struct drm_device *dev = encoder->base.dev;
1658         struct radeon_device *rdev = dev->dev_private;
1659         struct radeon_mode_info *mode_info = &rdev->mode_info;
1660         int index = GetIndexIntoMasterTable(DATA, CompassionateData);
1661         uint16_t data_offset;
1662         struct _COMPASSIONATE_DATA *dac_info;
1663         uint8_t frev, crev;
1664         uint8_t bg, dac;
1665         struct radeon_encoder_primary_dac *p_dac = NULL;
1666
1667         if (atom_parse_data_header(mode_info->atom_context, index, NULL,
1668                                    &frev, &crev, &data_offset)) {
1669                 dac_info = (struct _COMPASSIONATE_DATA *)
1670                         (mode_info->atom_context->bios + data_offset);
1671
1672                 p_dac = kzalloc(sizeof(struct radeon_encoder_primary_dac), GFP_KERNEL);
1673
1674                 if (!p_dac)
1675                         return NULL;
1676
1677                 bg = dac_info->ucDAC1_BG_Adjustment;
1678                 dac = dac_info->ucDAC1_DAC_Adjustment;
1679                 p_dac->ps2_pdac_adj = (bg << 8) | (dac);
1680
1681         }
1682         return p_dac;
1683 }
1684
1685 bool radeon_atom_get_tv_timings(struct radeon_device *rdev, int index,
1686                                 struct drm_display_mode *mode)
1687 {
1688         struct radeon_mode_info *mode_info = &rdev->mode_info;
1689         ATOM_ANALOG_TV_INFO *tv_info;
1690         ATOM_ANALOG_TV_INFO_V1_2 *tv_info_v1_2;
1691         ATOM_DTD_FORMAT *dtd_timings;
1692         int data_index = GetIndexIntoMasterTable(DATA, AnalogTV_Info);
1693         u8 frev, crev;
1694         u16 data_offset, misc;
1695
1696         if (!atom_parse_data_header(mode_info->atom_context, data_index, NULL,
1697                                     &frev, &crev, &data_offset))
1698                 return false;
1699
1700         switch (crev) {
1701         case 1:
1702                 tv_info = (ATOM_ANALOG_TV_INFO *)(mode_info->atom_context->bios + data_offset);
1703                 if (index >= MAX_SUPPORTED_TV_TIMING)
1704                         return false;
1705
1706                 mode->crtc_htotal = le16_to_cpu(tv_info->aModeTimings[index].usCRTC_H_Total);
1707                 mode->crtc_hdisplay = le16_to_cpu(tv_info->aModeTimings[index].usCRTC_H_Disp);
1708                 mode->crtc_hsync_start = le16_to_cpu(tv_info->aModeTimings[index].usCRTC_H_SyncStart);
1709                 mode->crtc_hsync_end = le16_to_cpu(tv_info->aModeTimings[index].usCRTC_H_SyncStart) +
1710                         le16_to_cpu(tv_info->aModeTimings[index].usCRTC_H_SyncWidth);
1711
1712                 mode->crtc_vtotal = le16_to_cpu(tv_info->aModeTimings[index].usCRTC_V_Total);
1713                 mode->crtc_vdisplay = le16_to_cpu(tv_info->aModeTimings[index].usCRTC_V_Disp);
1714                 mode->crtc_vsync_start = le16_to_cpu(tv_info->aModeTimings[index].usCRTC_V_SyncStart);
1715                 mode->crtc_vsync_end = le16_to_cpu(tv_info->aModeTimings[index].usCRTC_V_SyncStart) +
1716                         le16_to_cpu(tv_info->aModeTimings[index].usCRTC_V_SyncWidth);
1717
1718                 mode->flags = 0;
1719                 misc = le16_to_cpu(tv_info->aModeTimings[index].susModeMiscInfo.usAccess);
1720                 if (misc & ATOM_VSYNC_POLARITY)
1721                         mode->flags |= DRM_MODE_FLAG_NVSYNC;
1722                 if (misc & ATOM_HSYNC_POLARITY)
1723                         mode->flags |= DRM_MODE_FLAG_NHSYNC;
1724                 if (misc & ATOM_COMPOSITESYNC)
1725                         mode->flags |= DRM_MODE_FLAG_CSYNC;
1726                 if (misc & ATOM_INTERLACE)
1727                         mode->flags |= DRM_MODE_FLAG_INTERLACE;
1728                 if (misc & ATOM_DOUBLE_CLOCK_MODE)
1729                         mode->flags |= DRM_MODE_FLAG_DBLSCAN;
1730
1731                 mode->clock = le16_to_cpu(tv_info->aModeTimings[index].usPixelClock) * 10;
1732
1733                 if (index == 1) {
1734                         /* PAL timings appear to have wrong values for totals */
1735                         mode->crtc_htotal -= 1;
1736                         mode->crtc_vtotal -= 1;
1737                 }
1738                 break;
1739         case 2:
1740                 tv_info_v1_2 = (ATOM_ANALOG_TV_INFO_V1_2 *)(mode_info->atom_context->bios + data_offset);
1741                 if (index >= MAX_SUPPORTED_TV_TIMING_V1_2)
1742                         return false;
1743
1744                 dtd_timings = &tv_info_v1_2->aModeTimings[index];
1745                 mode->crtc_htotal = le16_to_cpu(dtd_timings->usHActive) +
1746                         le16_to_cpu(dtd_timings->usHBlanking_Time);
1747                 mode->crtc_hdisplay = le16_to_cpu(dtd_timings->usHActive);
1748                 mode->crtc_hsync_start = le16_to_cpu(dtd_timings->usHActive) +
1749                         le16_to_cpu(dtd_timings->usHSyncOffset);
1750                 mode->crtc_hsync_end = mode->crtc_hsync_start +
1751                         le16_to_cpu(dtd_timings->usHSyncWidth);
1752
1753                 mode->crtc_vtotal = le16_to_cpu(dtd_timings->usVActive) +
1754                         le16_to_cpu(dtd_timings->usVBlanking_Time);
1755                 mode->crtc_vdisplay = le16_to_cpu(dtd_timings->usVActive);
1756                 mode->crtc_vsync_start = le16_to_cpu(dtd_timings->usVActive) +
1757                         le16_to_cpu(dtd_timings->usVSyncOffset);
1758                 mode->crtc_vsync_end = mode->crtc_vsync_start +
1759                         le16_to_cpu(dtd_timings->usVSyncWidth);
1760
1761                 mode->flags = 0;
1762                 misc = le16_to_cpu(dtd_timings->susModeMiscInfo.usAccess);
1763                 if (misc & ATOM_VSYNC_POLARITY)
1764                         mode->flags |= DRM_MODE_FLAG_NVSYNC;
1765                 if (misc & ATOM_HSYNC_POLARITY)
1766                         mode->flags |= DRM_MODE_FLAG_NHSYNC;
1767                 if (misc & ATOM_COMPOSITESYNC)
1768                         mode->flags |= DRM_MODE_FLAG_CSYNC;
1769                 if (misc & ATOM_INTERLACE)
1770                         mode->flags |= DRM_MODE_FLAG_INTERLACE;
1771                 if (misc & ATOM_DOUBLE_CLOCK_MODE)
1772                         mode->flags |= DRM_MODE_FLAG_DBLSCAN;
1773
1774                 mode->clock = le16_to_cpu(dtd_timings->usPixClk) * 10;
1775                 break;
1776         }
1777         return true;
1778 }
1779
1780 enum radeon_tv_std
1781 radeon_atombios_get_tv_info(struct radeon_device *rdev)
1782 {
1783         struct radeon_mode_info *mode_info = &rdev->mode_info;
1784         int index = GetIndexIntoMasterTable(DATA, AnalogTV_Info);
1785         uint16_t data_offset;
1786         uint8_t frev, crev;
1787         struct _ATOM_ANALOG_TV_INFO *tv_info;
1788         enum radeon_tv_std tv_std = TV_STD_NTSC;
1789
1790         if (atom_parse_data_header(mode_info->atom_context, index, NULL,
1791                                    &frev, &crev, &data_offset)) {
1792
1793                 tv_info = (struct _ATOM_ANALOG_TV_INFO *)
1794                         (mode_info->atom_context->bios + data_offset);
1795
1796                 switch (tv_info->ucTV_BootUpDefaultStandard) {
1797                 case ATOM_TV_NTSC:
1798                         tv_std = TV_STD_NTSC;
1799                         DRM_DEBUG_KMS("Default TV standard: NTSC\n");
1800                         break;
1801                 case ATOM_TV_NTSCJ:
1802                         tv_std = TV_STD_NTSC_J;
1803                         DRM_DEBUG_KMS("Default TV standard: NTSC-J\n");
1804                         break;
1805                 case ATOM_TV_PAL:
1806                         tv_std = TV_STD_PAL;
1807                         DRM_DEBUG_KMS("Default TV standard: PAL\n");
1808                         break;
1809                 case ATOM_TV_PALM:
1810                         tv_std = TV_STD_PAL_M;
1811                         DRM_DEBUG_KMS("Default TV standard: PAL-M\n");
1812                         break;
1813                 case ATOM_TV_PALN:
1814                         tv_std = TV_STD_PAL_N;
1815                         DRM_DEBUG_KMS("Default TV standard: PAL-N\n");
1816                         break;
1817                 case ATOM_TV_PALCN:
1818                         tv_std = TV_STD_PAL_CN;
1819                         DRM_DEBUG_KMS("Default TV standard: PAL-CN\n");
1820                         break;
1821                 case ATOM_TV_PAL60:
1822                         tv_std = TV_STD_PAL_60;
1823                         DRM_DEBUG_KMS("Default TV standard: PAL-60\n");
1824                         break;
1825                 case ATOM_TV_SECAM:
1826                         tv_std = TV_STD_SECAM;
1827                         DRM_DEBUG_KMS("Default TV standard: SECAM\n");
1828                         break;
1829                 default:
1830                         tv_std = TV_STD_NTSC;
1831                         DRM_DEBUG_KMS("Unknown TV standard; defaulting to NTSC\n");
1832                         break;
1833                 }
1834         }
1835         return tv_std;
1836 }
1837
1838 struct radeon_encoder_tv_dac *
1839 radeon_atombios_get_tv_dac_info(struct radeon_encoder *encoder)
1840 {
1841         struct drm_device *dev = encoder->base.dev;
1842         struct radeon_device *rdev = dev->dev_private;
1843         struct radeon_mode_info *mode_info = &rdev->mode_info;
1844         int index = GetIndexIntoMasterTable(DATA, CompassionateData);
1845         uint16_t data_offset;
1846         struct _COMPASSIONATE_DATA *dac_info;
1847         uint8_t frev, crev;
1848         uint8_t bg, dac;
1849         struct radeon_encoder_tv_dac *tv_dac = NULL;
1850
1851         if (atom_parse_data_header(mode_info->atom_context, index, NULL,
1852                                    &frev, &crev, &data_offset)) {
1853
1854                 dac_info = (struct _COMPASSIONATE_DATA *)
1855                         (mode_info->atom_context->bios + data_offset);
1856
1857                 tv_dac = kzalloc(sizeof(struct radeon_encoder_tv_dac), GFP_KERNEL);
1858
1859                 if (!tv_dac)
1860                         return NULL;
1861
1862                 bg = dac_info->ucDAC2_CRT2_BG_Adjustment;
1863                 dac = dac_info->ucDAC2_CRT2_DAC_Adjustment;
1864                 tv_dac->ps2_tvdac_adj = (bg << 16) | (dac << 20);
1865
1866                 bg = dac_info->ucDAC2_PAL_BG_Adjustment;
1867                 dac = dac_info->ucDAC2_PAL_DAC_Adjustment;
1868                 tv_dac->pal_tvdac_adj = (bg << 16) | (dac << 20);
1869
1870                 bg = dac_info->ucDAC2_NTSC_BG_Adjustment;
1871                 dac = dac_info->ucDAC2_NTSC_DAC_Adjustment;
1872                 tv_dac->ntsc_tvdac_adj = (bg << 16) | (dac << 20);
1873
1874                 tv_dac->tv_std = radeon_atombios_get_tv_info(rdev);
1875         }
1876         return tv_dac;
1877 }
1878
1879 static const char *thermal_controller_names[] = {
1880         "NONE",
1881         "lm63",
1882         "adm1032",
1883         "adm1030",
1884         "max6649",
1885         "lm64",
1886         "f75375",
1887         "asc7xxx",
1888 };
1889
1890 static const char *pp_lib_thermal_controller_names[] = {
1891         "NONE",
1892         "lm63",
1893         "adm1032",
1894         "adm1030",
1895         "max6649",
1896         "lm64",
1897         "f75375",
1898         "RV6xx",
1899         "RV770",
1900         "adt7473",
1901         "NONE",
1902         "External GPIO",
1903         "Evergreen",
1904         "emc2103",
1905         "Sumo",
1906         "Northern Islands",
1907 };
1908
1909 union power_info {
1910         struct _ATOM_POWERPLAY_INFO info;
1911         struct _ATOM_POWERPLAY_INFO_V2 info_2;
1912         struct _ATOM_POWERPLAY_INFO_V3 info_3;
1913         struct _ATOM_PPLIB_POWERPLAYTABLE pplib;
1914         struct _ATOM_PPLIB_POWERPLAYTABLE2 pplib2;
1915         struct _ATOM_PPLIB_POWERPLAYTABLE3 pplib3;
1916 };
1917
1918 union pplib_clock_info {
1919         struct _ATOM_PPLIB_R600_CLOCK_INFO r600;
1920         struct _ATOM_PPLIB_RS780_CLOCK_INFO rs780;
1921         struct _ATOM_PPLIB_EVERGREEN_CLOCK_INFO evergreen;
1922         struct _ATOM_PPLIB_SUMO_CLOCK_INFO sumo;
1923 };
1924
1925 union pplib_power_state {
1926         struct _ATOM_PPLIB_STATE v1;
1927         struct _ATOM_PPLIB_STATE_V2 v2;
1928 };
1929
1930 static void radeon_atombios_parse_misc_flags_1_3(struct radeon_device *rdev,
1931                                                  int state_index,
1932                                                  u32 misc, u32 misc2)
1933 {
1934         rdev->pm.power_state[state_index].misc = misc;
1935         rdev->pm.power_state[state_index].misc2 = misc2;
1936         /* order matters! */
1937         if (misc & ATOM_PM_MISCINFO_POWER_SAVING_MODE)
1938                 rdev->pm.power_state[state_index].type =
1939                         POWER_STATE_TYPE_POWERSAVE;
1940         if (misc & ATOM_PM_MISCINFO_DEFAULT_DC_STATE_ENTRY_TRUE)
1941                 rdev->pm.power_state[state_index].type =
1942                         POWER_STATE_TYPE_BATTERY;
1943         if (misc & ATOM_PM_MISCINFO_DEFAULT_LOW_DC_STATE_ENTRY_TRUE)
1944                 rdev->pm.power_state[state_index].type =
1945                         POWER_STATE_TYPE_BATTERY;
1946         if (misc & ATOM_PM_MISCINFO_LOAD_BALANCE_EN)
1947                 rdev->pm.power_state[state_index].type =
1948                         POWER_STATE_TYPE_BALANCED;
1949         if (misc & ATOM_PM_MISCINFO_3D_ACCELERATION_EN) {
1950                 rdev->pm.power_state[state_index].type =
1951                         POWER_STATE_TYPE_PERFORMANCE;
1952                 rdev->pm.power_state[state_index].flags &=
1953                         ~RADEON_PM_STATE_SINGLE_DISPLAY_ONLY;
1954         }
1955         if (misc2 & ATOM_PM_MISCINFO2_SYSTEM_AC_LITE_MODE)
1956                 rdev->pm.power_state[state_index].type =
1957                         POWER_STATE_TYPE_BALANCED;
1958         if (misc & ATOM_PM_MISCINFO_DRIVER_DEFAULT_MODE) {
1959                 rdev->pm.power_state[state_index].type =
1960                         POWER_STATE_TYPE_DEFAULT;
1961                 rdev->pm.default_power_state_index = state_index;
1962                 rdev->pm.power_state[state_index].default_clock_mode =
1963                         &rdev->pm.power_state[state_index].clock_info[0];
1964         } else if (state_index == 0) {
1965                 rdev->pm.power_state[state_index].clock_info[0].flags |=
1966                         RADEON_PM_MODE_NO_DISPLAY;
1967         }
1968 }
1969
1970 static int radeon_atombios_parse_power_table_1_3(struct radeon_device *rdev)
1971 {
1972         struct radeon_mode_info *mode_info = &rdev->mode_info;
1973         u32 misc, misc2 = 0;
1974         int num_modes = 0, i;
1975         int state_index = 0;
1976         struct radeon_i2c_bus_rec i2c_bus;
1977         union power_info *power_info;
1978         int index = GetIndexIntoMasterTable(DATA, PowerPlayInfo);
1979         u16 data_offset;
1980         u8 frev, crev;
1981
1982         if (!atom_parse_data_header(mode_info->atom_context, index, NULL,
1983                                    &frev, &crev, &data_offset))
1984                 return state_index;
1985         power_info = (union power_info *)(mode_info->atom_context->bios + data_offset);
1986
1987         /* add the i2c bus for thermal/fan chip */
1988         if (power_info->info.ucOverdriveThermalController > 0) {
1989                 DRM_INFO("Possible %s thermal controller at 0x%02x\n",
1990                          thermal_controller_names[power_info->info.ucOverdriveThermalController],
1991                          power_info->info.ucOverdriveControllerAddress >> 1);
1992                 i2c_bus = radeon_lookup_i2c_gpio(rdev, power_info->info.ucOverdriveI2cLine);
1993                 rdev->pm.i2c_bus = radeon_i2c_lookup(rdev, &i2c_bus);
1994                 if (rdev->pm.i2c_bus) {
1995                         struct i2c_board_info info = { };
1996                         const char *name = thermal_controller_names[power_info->info.
1997                                                                     ucOverdriveThermalController];
1998                         info.addr = power_info->info.ucOverdriveControllerAddress >> 1;
1999                         strlcpy(info.type, name, sizeof(info.type));
2000                         i2c_new_device(&rdev->pm.i2c_bus->adapter, &info);
2001                 }
2002         }
2003         num_modes = power_info->info.ucNumOfPowerModeEntries;
2004         if (num_modes > ATOM_MAX_NUMBEROF_POWER_BLOCK)
2005                 num_modes = ATOM_MAX_NUMBEROF_POWER_BLOCK;
2006         rdev->pm.power_state = kzalloc(sizeof(struct radeon_power_state) * num_modes, GFP_KERNEL);
2007         if (!rdev->pm.power_state)
2008                 return state_index;
2009         /* last mode is usually default, array is low to high */
2010         for (i = 0; i < num_modes; i++) {
2011                 rdev->pm.power_state[state_index].clock_info[0].voltage.type = VOLTAGE_NONE;
2012                 switch (frev) {
2013                 case 1:
2014                         rdev->pm.power_state[state_index].num_clock_modes = 1;
2015                         rdev->pm.power_state[state_index].clock_info[0].mclk =
2016                                 le16_to_cpu(power_info->info.asPowerPlayInfo[i].usMemoryClock);
2017                         rdev->pm.power_state[state_index].clock_info[0].sclk =
2018                                 le16_to_cpu(power_info->info.asPowerPlayInfo[i].usEngineClock);
2019                         /* skip invalid modes */
2020                         if ((rdev->pm.power_state[state_index].clock_info[0].mclk == 0) ||
2021                             (rdev->pm.power_state[state_index].clock_info[0].sclk == 0))
2022                                 continue;
2023                         rdev->pm.power_state[state_index].pcie_lanes =
2024                                 power_info->info.asPowerPlayInfo[i].ucNumPciELanes;
2025                         misc = le32_to_cpu(power_info->info.asPowerPlayInfo[i].ulMiscInfo);
2026                         if ((misc & ATOM_PM_MISCINFO_VOLTAGE_DROP_SUPPORT) ||
2027                             (misc & ATOM_PM_MISCINFO_VOLTAGE_DROP_ACTIVE_HIGH)) {
2028                                 rdev->pm.power_state[state_index].clock_info[0].voltage.type =
2029                                         VOLTAGE_GPIO;
2030                                 rdev->pm.power_state[state_index].clock_info[0].voltage.gpio =
2031                                         radeon_lookup_gpio(rdev,
2032                                                            power_info->info.asPowerPlayInfo[i].ucVoltageDropIndex);
2033                                 if (misc & ATOM_PM_MISCINFO_VOLTAGE_DROP_ACTIVE_HIGH)
2034                                         rdev->pm.power_state[state_index].clock_info[0].voltage.active_high =
2035                                                 true;
2036                                 else
2037                                         rdev->pm.power_state[state_index].clock_info[0].voltage.active_high =
2038                                                 false;
2039                         } else if (misc & ATOM_PM_MISCINFO_PROGRAM_VOLTAGE) {
2040                                 rdev->pm.power_state[state_index].clock_info[0].voltage.type =
2041                                         VOLTAGE_VDDC;
2042                                 rdev->pm.power_state[state_index].clock_info[0].voltage.vddc_id =
2043                                         power_info->info.asPowerPlayInfo[i].ucVoltageDropIndex;
2044                         }
2045                         rdev->pm.power_state[state_index].flags = RADEON_PM_STATE_SINGLE_DISPLAY_ONLY;
2046                         radeon_atombios_parse_misc_flags_1_3(rdev, state_index, misc, 0);
2047                         state_index++;
2048                         break;
2049                 case 2:
2050                         rdev->pm.power_state[state_index].num_clock_modes = 1;
2051                         rdev->pm.power_state[state_index].clock_info[0].mclk =
2052                                 le32_to_cpu(power_info->info_2.asPowerPlayInfo[i].ulMemoryClock);
2053                         rdev->pm.power_state[state_index].clock_info[0].sclk =
2054                                 le32_to_cpu(power_info->info_2.asPowerPlayInfo[i].ulEngineClock);
2055                         /* skip invalid modes */
2056                         if ((rdev->pm.power_state[state_index].clock_info[0].mclk == 0) ||
2057                             (rdev->pm.power_state[state_index].clock_info[0].sclk == 0))
2058                                 continue;
2059                         rdev->pm.power_state[state_index].pcie_lanes =
2060                                 power_info->info_2.asPowerPlayInfo[i].ucNumPciELanes;
2061                         misc = le32_to_cpu(power_info->info_2.asPowerPlayInfo[i].ulMiscInfo);
2062                         misc2 = le32_to_cpu(power_info->info_2.asPowerPlayInfo[i].ulMiscInfo2);
2063                         if ((misc & ATOM_PM_MISCINFO_VOLTAGE_DROP_SUPPORT) ||
2064                             (misc & ATOM_PM_MISCINFO_VOLTAGE_DROP_ACTIVE_HIGH)) {
2065                                 rdev->pm.power_state[state_index].clock_info[0].voltage.type =
2066                                         VOLTAGE_GPIO;
2067                                 rdev->pm.power_state[state_index].clock_info[0].voltage.gpio =
2068                                         radeon_lookup_gpio(rdev,
2069                                                            power_info->info_2.asPowerPlayInfo[i].ucVoltageDropIndex);
2070                                 if (misc & ATOM_PM_MISCINFO_VOLTAGE_DROP_ACTIVE_HIGH)
2071                                         rdev->pm.power_state[state_index].clock_info[0].voltage.active_high =
2072                                                 true;
2073                                 else
2074                                         rdev->pm.power_state[state_index].clock_info[0].voltage.active_high =
2075                                                 false;
2076                         } else if (misc & ATOM_PM_MISCINFO_PROGRAM_VOLTAGE) {
2077                                 rdev->pm.power_state[state_index].clock_info[0].voltage.type =
2078                                         VOLTAGE_VDDC;
2079                                 rdev->pm.power_state[state_index].clock_info[0].voltage.vddc_id =
2080                                         power_info->info_2.asPowerPlayInfo[i].ucVoltageDropIndex;
2081                         }
2082                         rdev->pm.power_state[state_index].flags = RADEON_PM_STATE_SINGLE_DISPLAY_ONLY;
2083                         radeon_atombios_parse_misc_flags_1_3(rdev, state_index, misc, misc2);
2084                         state_index++;
2085                         break;
2086                 case 3:
2087                         rdev->pm.power_state[state_index].num_clock_modes = 1;
2088                         rdev->pm.power_state[state_index].clock_info[0].mclk =
2089                                 le32_to_cpu(power_info->info_3.asPowerPlayInfo[i].ulMemoryClock);
2090                         rdev->pm.power_state[state_index].clock_info[0].sclk =
2091                                 le32_to_cpu(power_info->info_3.asPowerPlayInfo[i].ulEngineClock);
2092                         /* skip invalid modes */
2093                         if ((rdev->pm.power_state[state_index].clock_info[0].mclk == 0) ||
2094                             (rdev->pm.power_state[state_index].clock_info[0].sclk == 0))
2095                                 continue;
2096                         rdev->pm.power_state[state_index].pcie_lanes =
2097                                 power_info->info_3.asPowerPlayInfo[i].ucNumPciELanes;
2098                         misc = le32_to_cpu(power_info->info_3.asPowerPlayInfo[i].ulMiscInfo);
2099                         misc2 = le32_to_cpu(power_info->info_3.asPowerPlayInfo[i].ulMiscInfo2);
2100                         if ((misc & ATOM_PM_MISCINFO_VOLTAGE_DROP_SUPPORT) ||
2101                             (misc & ATOM_PM_MISCINFO_VOLTAGE_DROP_ACTIVE_HIGH)) {
2102                                 rdev->pm.power_state[state_index].clock_info[0].voltage.type =
2103                                         VOLTAGE_GPIO;
2104                                 rdev->pm.power_state[state_index].clock_info[0].voltage.gpio =
2105                                         radeon_lookup_gpio(rdev,
2106                                                            power_info->info_3.asPowerPlayInfo[i].ucVoltageDropIndex);
2107                                 if (misc & ATOM_PM_MISCINFO_VOLTAGE_DROP_ACTIVE_HIGH)
2108                                         rdev->pm.power_state[state_index].clock_info[0].voltage.active_high =
2109                                                 true;
2110                                 else
2111                                         rdev->pm.power_state[state_index].clock_info[0].voltage.active_high =
2112                                                 false;
2113                         } else if (misc & ATOM_PM_MISCINFO_PROGRAM_VOLTAGE) {
2114                                 rdev->pm.power_state[state_index].clock_info[0].voltage.type =
2115                                         VOLTAGE_VDDC;
2116                                 rdev->pm.power_state[state_index].clock_info[0].voltage.vddc_id =
2117                                         power_info->info_3.asPowerPlayInfo[i].ucVoltageDropIndex;
2118                                 if (misc2 & ATOM_PM_MISCINFO2_VDDCI_DYNAMIC_VOLTAGE_EN) {
2119                                         rdev->pm.power_state[state_index].clock_info[0].voltage.vddci_enabled =
2120                                                 true;
2121                                         rdev->pm.power_state[state_index].clock_info[0].voltage.vddci_id =
2122                                                 power_info->info_3.asPowerPlayInfo[i].ucVDDCI_VoltageDropIndex;
2123                                 }
2124                         }
2125                         rdev->pm.power_state[state_index].flags = RADEON_PM_STATE_SINGLE_DISPLAY_ONLY;
2126                         radeon_atombios_parse_misc_flags_1_3(rdev, state_index, misc, misc2);
2127                         state_index++;
2128                         break;
2129                 }
2130         }
2131         /* last mode is usually default */
2132         if (rdev->pm.default_power_state_index == -1) {
2133                 rdev->pm.power_state[state_index - 1].type =
2134                         POWER_STATE_TYPE_DEFAULT;
2135                 rdev->pm.default_power_state_index = state_index - 1;
2136                 rdev->pm.power_state[state_index - 1].default_clock_mode =
2137                         &rdev->pm.power_state[state_index - 1].clock_info[0];
2138                 rdev->pm.power_state[state_index].flags &=
2139                         ~RADEON_PM_STATE_SINGLE_DISPLAY_ONLY;
2140                 rdev->pm.power_state[state_index].misc = 0;
2141                 rdev->pm.power_state[state_index].misc2 = 0;
2142         }
2143         return state_index;
2144 }
2145
2146 static void radeon_atombios_add_pplib_thermal_controller(struct radeon_device *rdev,
2147                                                          ATOM_PPLIB_THERMALCONTROLLER *controller)
2148 {
2149         struct radeon_i2c_bus_rec i2c_bus;
2150
2151         /* add the i2c bus for thermal/fan chip */
2152         if (controller->ucType > 0) {
2153                 if (controller->ucType == ATOM_PP_THERMALCONTROLLER_RV6xx) {
2154                         DRM_INFO("Internal thermal controller %s fan control\n",
2155                                  (controller->ucFanParameters &
2156                                   ATOM_PP_FANPARAMETERS_NOFAN) ? "without" : "with");
2157                         rdev->pm.int_thermal_type = THERMAL_TYPE_RV6XX;
2158                 } else if (controller->ucType == ATOM_PP_THERMALCONTROLLER_RV770) {
2159                         DRM_INFO("Internal thermal controller %s fan control\n",
2160                                  (controller->ucFanParameters &
2161                                   ATOM_PP_FANPARAMETERS_NOFAN) ? "without" : "with");
2162                         rdev->pm.int_thermal_type = THERMAL_TYPE_RV770;
2163                 } else if (controller->ucType == ATOM_PP_THERMALCONTROLLER_EVERGREEN) {
2164                         DRM_INFO("Internal thermal controller %s fan control\n",
2165                                  (controller->ucFanParameters &
2166                                   ATOM_PP_FANPARAMETERS_NOFAN) ? "without" : "with");
2167                         rdev->pm.int_thermal_type = THERMAL_TYPE_EVERGREEN;
2168                 } else if (controller->ucType == ATOM_PP_THERMALCONTROLLER_SUMO) {
2169                         DRM_INFO("Internal thermal controller %s fan control\n",
2170                                  (controller->ucFanParameters &
2171                                   ATOM_PP_FANPARAMETERS_NOFAN) ? "without" : "with");
2172                         rdev->pm.int_thermal_type = THERMAL_TYPE_SUMO;
2173                 } else if (controller->ucType == ATOM_PP_THERMALCONTROLLER_NISLANDS) {
2174                         DRM_INFO("Internal thermal controller %s fan control\n",
2175                                  (controller->ucFanParameters &
2176                                   ATOM_PP_FANPARAMETERS_NOFAN) ? "without" : "with");
2177                         rdev->pm.int_thermal_type = THERMAL_TYPE_NI;
2178                 } else if ((controller->ucType ==
2179                             ATOM_PP_THERMALCONTROLLER_EXTERNAL_GPIO) ||
2180                            (controller->ucType ==
2181                             ATOM_PP_THERMALCONTROLLER_ADT7473_WITH_INTERNAL) ||
2182                            (controller->ucType ==
2183                             ATOM_PP_THERMALCONTROLLER_EMC2103_WITH_INTERNAL)) {
2184                         DRM_INFO("Special thermal controller config\n");
2185                 } else {
2186                         DRM_INFO("Possible %s thermal controller at 0x%02x %s fan control\n",
2187                                  pp_lib_thermal_controller_names[controller->ucType],
2188                                  controller->ucI2cAddress >> 1,
2189                                  (controller->ucFanParameters &
2190                                   ATOM_PP_FANPARAMETERS_NOFAN) ? "without" : "with");
2191                         i2c_bus = radeon_lookup_i2c_gpio(rdev, controller->ucI2cLine);
2192                         rdev->pm.i2c_bus = radeon_i2c_lookup(rdev, &i2c_bus);
2193                         if (rdev->pm.i2c_bus) {
2194                                 struct i2c_board_info info = { };
2195                                 const char *name = pp_lib_thermal_controller_names[controller->ucType];
2196                                 info.addr = controller->ucI2cAddress >> 1;
2197                                 strlcpy(info.type, name, sizeof(info.type));
2198                                 i2c_new_device(&rdev->pm.i2c_bus->adapter, &info);
2199                         }
2200                 }
2201         }
2202 }
2203
2204 static void radeon_atombios_get_default_voltages(struct radeon_device *rdev,
2205                                                  u16 *vddc, u16 *vddci)
2206 {
2207         struct radeon_mode_info *mode_info = &rdev->mode_info;
2208         int index = GetIndexIntoMasterTable(DATA, FirmwareInfo);
2209         u8 frev, crev;
2210         u16 data_offset;
2211         union firmware_info *firmware_info;
2212
2213         *vddc = 0;
2214         *vddci = 0;
2215
2216         if (atom_parse_data_header(mode_info->atom_context, index, NULL,
2217                                    &frev, &crev, &data_offset)) {
2218                 firmware_info =
2219                         (union firmware_info *)(mode_info->atom_context->bios +
2220                                                 data_offset);
2221                 *vddc = le16_to_cpu(firmware_info->info_14.usBootUpVDDCVoltage);
2222                 if ((frev == 2) && (crev >= 2))
2223                         *vddci = le16_to_cpu(firmware_info->info_22.usBootUpVDDCIVoltage);
2224         }
2225 }
2226
2227 static void radeon_atombios_parse_pplib_non_clock_info(struct radeon_device *rdev,
2228                                                        int state_index, int mode_index,
2229                                                        struct _ATOM_PPLIB_NONCLOCK_INFO *non_clock_info)
2230 {
2231         int j;
2232         u32 misc = le32_to_cpu(non_clock_info->ulCapsAndSettings);
2233         u32 misc2 = le16_to_cpu(non_clock_info->usClassification);
2234         u16 vddc, vddci;
2235
2236         radeon_atombios_get_default_voltages(rdev, &vddc, &vddci);
2237
2238         rdev->pm.power_state[state_index].misc = misc;
2239         rdev->pm.power_state[state_index].misc2 = misc2;
2240         rdev->pm.power_state[state_index].pcie_lanes =
2241                 ((misc & ATOM_PPLIB_PCIE_LINK_WIDTH_MASK) >>
2242                  ATOM_PPLIB_PCIE_LINK_WIDTH_SHIFT) + 1;
2243         switch (misc2 & ATOM_PPLIB_CLASSIFICATION_UI_MASK) {
2244         case ATOM_PPLIB_CLASSIFICATION_UI_BATTERY:
2245                 rdev->pm.power_state[state_index].type =
2246                         POWER_STATE_TYPE_BATTERY;
2247                 break;
2248         case ATOM_PPLIB_CLASSIFICATION_UI_BALANCED:
2249                 rdev->pm.power_state[state_index].type =
2250                         POWER_STATE_TYPE_BALANCED;
2251                 break;
2252         case ATOM_PPLIB_CLASSIFICATION_UI_PERFORMANCE:
2253                 rdev->pm.power_state[state_index].type =
2254                         POWER_STATE_TYPE_PERFORMANCE;
2255                 break;
2256         case ATOM_PPLIB_CLASSIFICATION_UI_NONE:
2257                 if (misc2 & ATOM_PPLIB_CLASSIFICATION_3DPERFORMANCE)
2258                         rdev->pm.power_state[state_index].type =
2259                                 POWER_STATE_TYPE_PERFORMANCE;
2260                 break;
2261         }
2262         rdev->pm.power_state[state_index].flags = 0;
2263         if (misc & ATOM_PPLIB_SINGLE_DISPLAY_ONLY)
2264                 rdev->pm.power_state[state_index].flags |=
2265                         RADEON_PM_STATE_SINGLE_DISPLAY_ONLY;
2266         if (misc2 & ATOM_PPLIB_CLASSIFICATION_BOOT) {
2267                 rdev->pm.power_state[state_index].type =
2268                         POWER_STATE_TYPE_DEFAULT;
2269                 rdev->pm.default_power_state_index = state_index;
2270                 rdev->pm.power_state[state_index].default_clock_mode =
2271                         &rdev->pm.power_state[state_index].clock_info[mode_index - 1];
2272                 if (ASIC_IS_DCE5(rdev)) {
2273                         /* NI chips post without MC ucode, so default clocks are strobe mode only */
2274                         rdev->pm.default_sclk = rdev->pm.power_state[state_index].clock_info[0].sclk;
2275                         rdev->pm.default_mclk = rdev->pm.power_state[state_index].clock_info[0].mclk;
2276                         rdev->pm.default_vddc = rdev->pm.power_state[state_index].clock_info[0].voltage.voltage;
2277                         rdev->pm.default_vddci = rdev->pm.power_state[state_index].clock_info[0].voltage.vddci;
2278                 } else {
2279                         /* patch the table values with the default slck/mclk from firmware info */
2280                         for (j = 0; j < mode_index; j++) {
2281                                 rdev->pm.power_state[state_index].clock_info[j].mclk =
2282                                         rdev->clock.default_mclk;
2283                                 rdev->pm.power_state[state_index].clock_info[j].sclk =
2284                                         rdev->clock.default_sclk;
2285                                 if (vddc)
2286                                         rdev->pm.power_state[state_index].clock_info[j].voltage.voltage =
2287                                                 vddc;
2288                         }
2289                 }
2290         }
2291 }
2292
2293 static bool radeon_atombios_parse_pplib_clock_info(struct radeon_device *rdev,
2294                                                    int state_index, int mode_index,
2295                                                    union pplib_clock_info *clock_info)
2296 {
2297         u32 sclk, mclk;
2298
2299         if (rdev->flags & RADEON_IS_IGP) {
2300                 if (rdev->family >= CHIP_PALM) {
2301                         sclk = le16_to_cpu(clock_info->sumo.usEngineClockLow);
2302                         sclk |= clock_info->sumo.ucEngineClockHigh << 16;
2303                         rdev->pm.power_state[state_index].clock_info[mode_index].sclk = sclk;
2304                 } else {
2305                         sclk = le16_to_cpu(clock_info->rs780.usLowEngineClockLow);
2306                         sclk |= clock_info->rs780.ucLowEngineClockHigh << 16;
2307                         rdev->pm.power_state[state_index].clock_info[mode_index].sclk = sclk;
2308                 }
2309         } else if (ASIC_IS_DCE4(rdev)) {
2310                 sclk = le16_to_cpu(clock_info->evergreen.usEngineClockLow);
2311                 sclk |= clock_info->evergreen.ucEngineClockHigh << 16;
2312                 mclk = le16_to_cpu(clock_info->evergreen.usMemoryClockLow);
2313                 mclk |= clock_info->evergreen.ucMemoryClockHigh << 16;
2314                 rdev->pm.power_state[state_index].clock_info[mode_index].mclk = mclk;
2315                 rdev->pm.power_state[state_index].clock_info[mode_index].sclk = sclk;
2316                 rdev->pm.power_state[state_index].clock_info[mode_index].voltage.type =
2317                         VOLTAGE_SW;
2318                 rdev->pm.power_state[state_index].clock_info[mode_index].voltage.voltage =
2319                         le16_to_cpu(clock_info->evergreen.usVDDC);
2320                 rdev->pm.power_state[state_index].clock_info[mode_index].voltage.vddci =
2321                         le16_to_cpu(clock_info->evergreen.usVDDCI);
2322         } else {
2323                 sclk = le16_to_cpu(clock_info->r600.usEngineClockLow);
2324                 sclk |= clock_info->r600.ucEngineClockHigh << 16;
2325                 mclk = le16_to_cpu(clock_info->r600.usMemoryClockLow);
2326                 mclk |= clock_info->r600.ucMemoryClockHigh << 16;
2327                 rdev->pm.power_state[state_index].clock_info[mode_index].mclk = mclk;
2328                 rdev->pm.power_state[state_index].clock_info[mode_index].sclk = sclk;
2329                 rdev->pm.power_state[state_index].clock_info[mode_index].voltage.type =
2330                         VOLTAGE_SW;
2331                 rdev->pm.power_state[state_index].clock_info[mode_index].voltage.voltage =
2332                         le16_to_cpu(clock_info->r600.usVDDC);
2333         }
2334
2335         /* patch up vddc if necessary */
2336         if (rdev->pm.power_state[state_index].clock_info[mode_index].voltage.voltage == 0xff01) {
2337                 u16 vddc;
2338
2339                 if (radeon_atom_get_max_vddc(rdev, &vddc) == 0)
2340                         rdev->pm.power_state[state_index].clock_info[mode_index].voltage.voltage = vddc;
2341         }
2342
2343         if (rdev->flags & RADEON_IS_IGP) {
2344                 /* skip invalid modes */
2345                 if (rdev->pm.power_state[state_index].clock_info[mode_index].sclk == 0)
2346                         return false;
2347         } else {
2348                 /* skip invalid modes */
2349                 if ((rdev->pm.power_state[state_index].clock_info[mode_index].mclk == 0) ||
2350                     (rdev->pm.power_state[state_index].clock_info[mode_index].sclk == 0))
2351                         return false;
2352         }
2353         return true;
2354 }
2355
2356 static int radeon_atombios_parse_power_table_4_5(struct radeon_device *rdev)
2357 {
2358         struct radeon_mode_info *mode_info = &rdev->mode_info;
2359         struct _ATOM_PPLIB_NONCLOCK_INFO *non_clock_info;
2360         union pplib_power_state *power_state;
2361         int i, j;
2362         int state_index = 0, mode_index = 0;
2363         union pplib_clock_info *clock_info;
2364         bool valid;
2365         union power_info *power_info;
2366         int index = GetIndexIntoMasterTable(DATA, PowerPlayInfo);
2367         u16 data_offset;
2368         u8 frev, crev;
2369
2370         if (!atom_parse_data_header(mode_info->atom_context, index, NULL,
2371                                    &frev, &crev, &data_offset))
2372                 return state_index;
2373         power_info = (union power_info *)(mode_info->atom_context->bios + data_offset);
2374
2375         radeon_atombios_add_pplib_thermal_controller(rdev, &power_info->pplib.sThermalController);
2376         rdev->pm.power_state = kzalloc(sizeof(struct radeon_power_state) *
2377                                        power_info->pplib.ucNumStates, GFP_KERNEL);
2378         if (!rdev->pm.power_state)
2379                 return state_index;
2380         /* first mode is usually default, followed by low to high */
2381         for (i = 0; i < power_info->pplib.ucNumStates; i++) {
2382                 mode_index = 0;
2383                 power_state = (union pplib_power_state *)
2384                         (mode_info->atom_context->bios + data_offset +
2385                          le16_to_cpu(power_info->pplib.usStateArrayOffset) +
2386                          i * power_info->pplib.ucStateEntrySize);
2387                 non_clock_info = (struct _ATOM_PPLIB_NONCLOCK_INFO *)
2388                         (mode_info->atom_context->bios + data_offset +
2389                          le16_to_cpu(power_info->pplib.usNonClockInfoArrayOffset) +
2390                          (power_state->v1.ucNonClockStateIndex *
2391                           power_info->pplib.ucNonClockSize));
2392                 for (j = 0; j < (power_info->pplib.ucStateEntrySize - 1); j++) {
2393                         clock_info = (union pplib_clock_info *)
2394                                 (mode_info->atom_context->bios + data_offset +
2395                                  le16_to_cpu(power_info->pplib.usClockInfoArrayOffset) +
2396                                  (power_state->v1.ucClockStateIndices[j] *
2397                                   power_info->pplib.ucClockInfoSize));
2398                         valid = radeon_atombios_parse_pplib_clock_info(rdev,
2399                                                                        state_index, mode_index,
2400                                                                        clock_info);
2401                         if (valid)
2402                                 mode_index++;
2403                 }
2404                 rdev->pm.power_state[state_index].num_clock_modes = mode_index;
2405                 if (mode_index) {
2406                         radeon_atombios_parse_pplib_non_clock_info(rdev, state_index, mode_index,
2407                                                                    non_clock_info);
2408                         state_index++;
2409                 }
2410         }
2411         /* if multiple clock modes, mark the lowest as no display */
2412         for (i = 0; i < state_index; i++) {
2413                 if (rdev->pm.power_state[i].num_clock_modes > 1)
2414                         rdev->pm.power_state[i].clock_info[0].flags |=
2415                                 RADEON_PM_MODE_NO_DISPLAY;
2416         }
2417         /* first mode is usually default */
2418         if (rdev->pm.default_power_state_index == -1) {
2419                 rdev->pm.power_state[0].type =
2420                         POWER_STATE_TYPE_DEFAULT;
2421                 rdev->pm.default_power_state_index = 0;
2422                 rdev->pm.power_state[0].default_clock_mode =
2423                         &rdev->pm.power_state[0].clock_info[0];
2424         }
2425         return state_index;
2426 }
2427
2428 static int radeon_atombios_parse_power_table_6(struct radeon_device *rdev)
2429 {
2430         struct radeon_mode_info *mode_info = &rdev->mode_info;
2431         struct _ATOM_PPLIB_NONCLOCK_INFO *non_clock_info;
2432         union pplib_power_state *power_state;
2433         int i, j, non_clock_array_index, clock_array_index;
2434         int state_index = 0, mode_index = 0;
2435         union pplib_clock_info *clock_info;
2436         struct StateArray *state_array;
2437         struct ClockInfoArray *clock_info_array;
2438         struct NonClockInfoArray *non_clock_info_array;
2439         bool valid;
2440         union power_info *power_info;
2441         int index = GetIndexIntoMasterTable(DATA, PowerPlayInfo);
2442         u16 data_offset;
2443         u8 frev, crev;
2444
2445         if (!atom_parse_data_header(mode_info->atom_context, index, NULL,
2446                                    &frev, &crev, &data_offset))
2447                 return state_index;
2448         power_info = (union power_info *)(mode_info->atom_context->bios + data_offset);
2449
2450         radeon_atombios_add_pplib_thermal_controller(rdev, &power_info->pplib.sThermalController);
2451         state_array = (struct StateArray *)
2452                 (mode_info->atom_context->bios + data_offset +
2453                  le16_to_cpu(power_info->pplib.usStateArrayOffset));
2454         clock_info_array = (struct ClockInfoArray *)
2455                 (mode_info->atom_context->bios + data_offset +
2456                  le16_to_cpu(power_info->pplib.usClockInfoArrayOffset));
2457         non_clock_info_array = (struct NonClockInfoArray *)
2458                 (mode_info->atom_context->bios + data_offset +
2459                  le16_to_cpu(power_info->pplib.usNonClockInfoArrayOffset));
2460         rdev->pm.power_state = kzalloc(sizeof(struct radeon_power_state) *
2461                                        state_array->ucNumEntries, GFP_KERNEL);
2462         if (!rdev->pm.power_state)
2463                 return state_index;
2464         for (i = 0; i < state_array->ucNumEntries; i++) {
2465                 mode_index = 0;
2466                 power_state = (union pplib_power_state *)&state_array->states[i];
2467                 /* XXX this might be an inagua bug... */
2468                 non_clock_array_index = i; /* power_state->v2.nonClockInfoIndex */
2469                 non_clock_info = (struct _ATOM_PPLIB_NONCLOCK_INFO *)
2470                         &non_clock_info_array->nonClockInfo[non_clock_array_index];
2471                 for (j = 0; j < power_state->v2.ucNumDPMLevels; j++) {
2472                         clock_array_index = power_state->v2.clockInfoIndex[j];
2473                         /* XXX this might be an inagua bug... */
2474                         if (clock_array_index >= clock_info_array->ucNumEntries)
2475                                 continue;
2476                         clock_info = (union pplib_clock_info *)
2477                                 &clock_info_array->clockInfo[clock_array_index];
2478                         valid = radeon_atombios_parse_pplib_clock_info(rdev,
2479                                                                        state_index, mode_index,
2480                                                                        clock_info);
2481                         if (valid)
2482                                 mode_index++;
2483                 }
2484                 rdev->pm.power_state[state_index].num_clock_modes = mode_index;
2485                 if (mode_index) {
2486                         radeon_atombios_parse_pplib_non_clock_info(rdev, state_index, mode_index,
2487                                                                    non_clock_info);
2488                         state_index++;
2489                 }
2490         }
2491         /* if multiple clock modes, mark the lowest as no display */
2492         for (i = 0; i < state_index; i++) {
2493                 if (rdev->pm.power_state[i].num_clock_modes > 1)
2494                         rdev->pm.power_state[i].clock_info[0].flags |=
2495                                 RADEON_PM_MODE_NO_DISPLAY;
2496         }
2497         /* first mode is usually default */
2498         if (rdev->pm.default_power_state_index == -1) {
2499                 rdev->pm.power_state[0].type =
2500                         POWER_STATE_TYPE_DEFAULT;
2501                 rdev->pm.default_power_state_index = 0;
2502                 rdev->pm.power_state[0].default_clock_mode =
2503                         &rdev->pm.power_state[0].clock_info[0];
2504         }
2505         return state_index;
2506 }
2507
2508 void radeon_atombios_get_power_modes(struct radeon_device *rdev)
2509 {
2510         struct radeon_mode_info *mode_info = &rdev->mode_info;
2511         int index = GetIndexIntoMasterTable(DATA, PowerPlayInfo);
2512         u16 data_offset;
2513         u8 frev, crev;
2514         int state_index = 0;
2515
2516         rdev->pm.default_power_state_index = -1;
2517
2518         if (atom_parse_data_header(mode_info->atom_context, index, NULL,
2519                                    &frev, &crev, &data_offset)) {
2520                 switch (frev) {
2521                 case 1:
2522                 case 2:
2523                 case 3:
2524                         state_index = radeon_atombios_parse_power_table_1_3(rdev);
2525                         break;
2526                 case 4:
2527                 case 5:
2528                         state_index = radeon_atombios_parse_power_table_4_5(rdev);
2529                         break;
2530                 case 6:
2531                         state_index = radeon_atombios_parse_power_table_6(rdev);
2532                         break;
2533                 default:
2534                         break;
2535                 }
2536         } else {
2537                 rdev->pm.power_state = kzalloc(sizeof(struct radeon_power_state), GFP_KERNEL);
2538                 if (rdev->pm.power_state) {
2539                         /* add the default mode */
2540                         rdev->pm.power_state[state_index].type =
2541                                 POWER_STATE_TYPE_DEFAULT;
2542                         rdev->pm.power_state[state_index].num_clock_modes = 1;
2543                         rdev->pm.power_state[state_index].clock_info[0].mclk = rdev->clock.default_mclk;
2544                         rdev->pm.power_state[state_index].clock_info[0].sclk = rdev->clock.default_sclk;
2545                         rdev->pm.power_state[state_index].default_clock_mode =
2546                                 &rdev->pm.power_state[state_index].clock_info[0];
2547                         rdev->pm.power_state[state_index].clock_info[0].voltage.type = VOLTAGE_NONE;
2548                         rdev->pm.power_state[state_index].pcie_lanes = 16;
2549                         rdev->pm.default_power_state_index = state_index;
2550                         rdev->pm.power_state[state_index].flags = 0;
2551                         state_index++;
2552                 }
2553         }
2554
2555         rdev->pm.num_power_states = state_index;
2556
2557         rdev->pm.current_power_state_index = rdev->pm.default_power_state_index;
2558         rdev->pm.current_clock_mode_index = 0;
2559         rdev->pm.current_vddc = rdev->pm.power_state[rdev->pm.default_power_state_index].clock_info[0].voltage.voltage;
2560 }
2561
2562 void radeon_atom_set_clock_gating(struct radeon_device *rdev, int enable)
2563 {
2564         DYNAMIC_CLOCK_GATING_PS_ALLOCATION args;
2565         int index = GetIndexIntoMasterTable(COMMAND, DynamicClockGating);
2566
2567         args.ucEnable = enable;
2568
2569         atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
2570 }
2571
2572 uint32_t radeon_atom_get_engine_clock(struct radeon_device *rdev)
2573 {
2574         GET_ENGINE_CLOCK_PS_ALLOCATION args;
2575         int index = GetIndexIntoMasterTable(COMMAND, GetEngineClock);
2576
2577         atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
2578         return le32_to_cpu(args.ulReturnEngineClock);
2579 }
2580
2581 uint32_t radeon_atom_get_memory_clock(struct radeon_device *rdev)
2582 {
2583         GET_MEMORY_CLOCK_PS_ALLOCATION args;
2584         int index = GetIndexIntoMasterTable(COMMAND, GetMemoryClock);
2585
2586         atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
2587         return le32_to_cpu(args.ulReturnMemoryClock);
2588 }
2589
2590 void radeon_atom_set_engine_clock(struct radeon_device *rdev,
2591                                   uint32_t eng_clock)
2592 {
2593         SET_ENGINE_CLOCK_PS_ALLOCATION args;
2594         int index = GetIndexIntoMasterTable(COMMAND, SetEngineClock);
2595
2596         args.ulTargetEngineClock = cpu_to_le32(eng_clock);      /* 10 khz */
2597
2598         atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
2599 }
2600
2601 void radeon_atom_set_memory_clock(struct radeon_device *rdev,
2602                                   uint32_t mem_clock)
2603 {
2604         SET_MEMORY_CLOCK_PS_ALLOCATION args;
2605         int index = GetIndexIntoMasterTable(COMMAND, SetMemoryClock);
2606
2607         if (rdev->flags & RADEON_IS_IGP)
2608                 return;
2609
2610         args.ulTargetMemoryClock = cpu_to_le32(mem_clock);      /* 10 khz */
2611
2612         atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
2613 }
2614
2615 union set_voltage {
2616         struct _SET_VOLTAGE_PS_ALLOCATION alloc;
2617         struct _SET_VOLTAGE_PARAMETERS v1;
2618         struct _SET_VOLTAGE_PARAMETERS_V2 v2;
2619 };
2620
2621 void radeon_atom_set_voltage(struct radeon_device *rdev, u16 voltage_level, u8 voltage_type)
2622 {
2623         union set_voltage args;
2624         int index = GetIndexIntoMasterTable(COMMAND, SetVoltage);
2625         u8 frev, crev, volt_index = voltage_level;
2626
2627         if (!atom_parse_cmd_header(rdev->mode_info.atom_context, index, &frev, &crev))
2628                 return;
2629
2630         /* 0xff01 is a flag rather then an actual voltage */
2631         if (voltage_level == 0xff01)
2632                 return;
2633
2634         switch (crev) {
2635         case 1:
2636                 args.v1.ucVoltageType = voltage_type;
2637                 args.v1.ucVoltageMode = SET_ASIC_VOLTAGE_MODE_ALL_SOURCE;
2638                 args.v1.ucVoltageIndex = volt_index;
2639                 break;
2640         case 2:
2641                 args.v2.ucVoltageType = voltage_type;
2642                 args.v2.ucVoltageMode = SET_ASIC_VOLTAGE_MODE_SET_VOLTAGE;
2643                 args.v2.usVoltageLevel = cpu_to_le16(voltage_level);
2644                 break;
2645         default:
2646                 DRM_ERROR("Unknown table version %d, %d\n", frev, crev);
2647                 return;
2648         }
2649
2650         atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
2651 }
2652
2653 int radeon_atom_get_max_vddc(struct radeon_device *rdev,
2654                              u16 *voltage)
2655 {
2656         union set_voltage args;
2657         int index = GetIndexIntoMasterTable(COMMAND, SetVoltage);
2658         u8 frev, crev;
2659
2660         if (!atom_parse_cmd_header(rdev->mode_info.atom_context, index, &frev, &crev))
2661                 return -EINVAL;
2662
2663         switch (crev) {
2664         case 1:
2665                 return -EINVAL;
2666         case 2:
2667                 args.v2.ucVoltageType = SET_VOLTAGE_GET_MAX_VOLTAGE;
2668                 args.v2.ucVoltageMode = 0;
2669                 args.v2.usVoltageLevel = 0;
2670
2671                 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
2672
2673                 *voltage = le16_to_cpu(args.v2.usVoltageLevel);
2674                 break;
2675         default:
2676                 DRM_ERROR("Unknown table version %d, %d\n", frev, crev);
2677                 return -EINVAL;
2678         }
2679
2680         return 0;
2681 }
2682
2683 void radeon_atom_initialize_bios_scratch_regs(struct drm_device *dev)
2684 {
2685         struct radeon_device *rdev = dev->dev_private;
2686         uint32_t bios_2_scratch, bios_6_scratch;
2687
2688         if (rdev->family >= CHIP_R600) {
2689                 bios_2_scratch = RREG32(R600_BIOS_2_SCRATCH);
2690                 bios_6_scratch = RREG32(R600_BIOS_6_SCRATCH);
2691         } else {
2692                 bios_2_scratch = RREG32(RADEON_BIOS_2_SCRATCH);
2693                 bios_6_scratch = RREG32(RADEON_BIOS_6_SCRATCH);
2694         }
2695
2696         /* let the bios control the backlight */
2697         bios_2_scratch &= ~ATOM_S2_VRI_BRIGHT_ENABLE;
2698
2699         /* tell the bios not to handle mode switching */
2700         bios_6_scratch |= ATOM_S6_ACC_BLOCK_DISPLAY_SWITCH;
2701
2702         if (rdev->family >= CHIP_R600) {
2703                 WREG32(R600_BIOS_2_SCRATCH, bios_2_scratch);
2704                 WREG32(R600_BIOS_6_SCRATCH, bios_6_scratch);
2705         } else {
2706                 WREG32(RADEON_BIOS_2_SCRATCH, bios_2_scratch);
2707                 WREG32(RADEON_BIOS_6_SCRATCH, bios_6_scratch);
2708         }
2709
2710 }
2711
2712 void radeon_save_bios_scratch_regs(struct radeon_device *rdev)
2713 {
2714         uint32_t scratch_reg;
2715         int i;
2716
2717         if (rdev->family >= CHIP_R600)
2718                 scratch_reg = R600_BIOS_0_SCRATCH;
2719         else
2720                 scratch_reg = RADEON_BIOS_0_SCRATCH;
2721
2722         for (i = 0; i < RADEON_BIOS_NUM_SCRATCH; i++)
2723                 rdev->bios_scratch[i] = RREG32(scratch_reg + (i * 4));
2724 }
2725
2726 void radeon_restore_bios_scratch_regs(struct radeon_device *rdev)
2727 {
2728         uint32_t scratch_reg;
2729         int i;
2730
2731         if (rdev->family >= CHIP_R600)
2732                 scratch_reg = R600_BIOS_0_SCRATCH;
2733         else
2734                 scratch_reg = RADEON_BIOS_0_SCRATCH;
2735
2736         for (i = 0; i < RADEON_BIOS_NUM_SCRATCH; i++)
2737                 WREG32(scratch_reg + (i * 4), rdev->bios_scratch[i]);
2738 }
2739
2740 void radeon_atom_output_lock(struct drm_encoder *encoder, bool lock)
2741 {
2742         struct drm_device *dev = encoder->dev;
2743         struct radeon_device *rdev = dev->dev_private;
2744         uint32_t bios_6_scratch;
2745
2746         if (rdev->family >= CHIP_R600)
2747                 bios_6_scratch = RREG32(R600_BIOS_6_SCRATCH);
2748         else
2749                 bios_6_scratch = RREG32(RADEON_BIOS_6_SCRATCH);
2750
2751         if (lock) {
2752                 bios_6_scratch |= ATOM_S6_CRITICAL_STATE;
2753                 bios_6_scratch &= ~ATOM_S6_ACC_MODE;
2754         } else {
2755                 bios_6_scratch &= ~ATOM_S6_CRITICAL_STATE;
2756                 bios_6_scratch |= ATOM_S6_ACC_MODE;
2757         }
2758
2759         if (rdev->family >= CHIP_R600)
2760                 WREG32(R600_BIOS_6_SCRATCH, bios_6_scratch);
2761         else
2762                 WREG32(RADEON_BIOS_6_SCRATCH, bios_6_scratch);
2763 }
2764
2765 /* at some point we may want to break this out into individual functions */
2766 void
2767 radeon_atombios_connected_scratch_regs(struct drm_connector *connector,
2768                                        struct drm_encoder *encoder,
2769                                        bool connected)
2770 {
2771         struct drm_device *dev = connector->dev;
2772         struct radeon_device *rdev = dev->dev_private;
2773         struct radeon_connector *radeon_connector =
2774             to_radeon_connector(connector);
2775         struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
2776         uint32_t bios_0_scratch, bios_3_scratch, bios_6_scratch;
2777
2778         if (rdev->family >= CHIP_R600) {
2779                 bios_0_scratch = RREG32(R600_BIOS_0_SCRATCH);
2780                 bios_3_scratch = RREG32(R600_BIOS_3_SCRATCH);
2781                 bios_6_scratch = RREG32(R600_BIOS_6_SCRATCH);
2782         } else {
2783                 bios_0_scratch = RREG32(RADEON_BIOS_0_SCRATCH);
2784                 bios_3_scratch = RREG32(RADEON_BIOS_3_SCRATCH);
2785                 bios_6_scratch = RREG32(RADEON_BIOS_6_SCRATCH);
2786         }
2787
2788         if ((radeon_encoder->devices & ATOM_DEVICE_TV1_SUPPORT) &&
2789             (radeon_connector->devices & ATOM_DEVICE_TV1_SUPPORT)) {
2790                 if (connected) {
2791                         DRM_DEBUG_KMS("TV1 connected\n");
2792                         bios_3_scratch |= ATOM_S3_TV1_ACTIVE;
2793                         bios_6_scratch |= ATOM_S6_ACC_REQ_TV1;
2794                 } else {
2795                         DRM_DEBUG_KMS("TV1 disconnected\n");
2796                         bios_0_scratch &= ~ATOM_S0_TV1_MASK;
2797                         bios_3_scratch &= ~ATOM_S3_TV1_ACTIVE;
2798                         bios_6_scratch &= ~ATOM_S6_ACC_REQ_TV1;
2799                 }
2800         }
2801         if ((radeon_encoder->devices & ATOM_DEVICE_CV_SUPPORT) &&
2802             (radeon_connector->devices & ATOM_DEVICE_CV_SUPPORT)) {
2803                 if (connected) {
2804                         DRM_DEBUG_KMS("CV connected\n");
2805                         bios_3_scratch |= ATOM_S3_CV_ACTIVE;
2806                         bios_6_scratch |= ATOM_S6_ACC_REQ_CV;
2807                 } else {
2808                         DRM_DEBUG_KMS("CV disconnected\n");
2809                         bios_0_scratch &= ~ATOM_S0_CV_MASK;
2810                         bios_3_scratch &= ~ATOM_S3_CV_ACTIVE;
2811                         bios_6_scratch &= ~ATOM_S6_ACC_REQ_CV;
2812                 }
2813         }
2814         if ((radeon_encoder->devices & ATOM_DEVICE_LCD1_SUPPORT) &&
2815             (radeon_connector->devices & ATOM_DEVICE_LCD1_SUPPORT)) {
2816                 if (connected) {
2817                         DRM_DEBUG_KMS("LCD1 connected\n");
2818                         bios_0_scratch |= ATOM_S0_LCD1;
2819                         bios_3_scratch |= ATOM_S3_LCD1_ACTIVE;
2820                         bios_6_scratch |= ATOM_S6_ACC_REQ_LCD1;
2821                 } else {
2822                         DRM_DEBUG_KMS("LCD1 disconnected\n");
2823                         bios_0_scratch &= ~ATOM_S0_LCD1;
2824                         bios_3_scratch &= ~ATOM_S3_LCD1_ACTIVE;
2825                         bios_6_scratch &= ~ATOM_S6_ACC_REQ_LCD1;
2826                 }
2827         }
2828         if ((radeon_encoder->devices & ATOM_DEVICE_CRT1_SUPPORT) &&
2829             (radeon_connector->devices & ATOM_DEVICE_CRT1_SUPPORT)) {
2830                 if (connected) {
2831                         DRM_DEBUG_KMS("CRT1 connected\n");
2832                         bios_0_scratch |= ATOM_S0_CRT1_COLOR;
2833                         bios_3_scratch |= ATOM_S3_CRT1_ACTIVE;
2834                         bios_6_scratch |= ATOM_S6_ACC_REQ_CRT1;
2835                 } else {
2836                         DRM_DEBUG_KMS("CRT1 disconnected\n");
2837                         bios_0_scratch &= ~ATOM_S0_CRT1_MASK;
2838                         bios_3_scratch &= ~ATOM_S3_CRT1_ACTIVE;
2839                         bios_6_scratch &= ~ATOM_S6_ACC_REQ_CRT1;
2840                 }
2841         }
2842         if ((radeon_encoder->devices & ATOM_DEVICE_CRT2_SUPPORT) &&
2843             (radeon_connector->devices & ATOM_DEVICE_CRT2_SUPPORT)) {
2844                 if (connected) {
2845                         DRM_DEBUG_KMS("CRT2 connected\n");
2846                         bios_0_scratch |= ATOM_S0_CRT2_COLOR;
2847                         bios_3_scratch |= ATOM_S3_CRT2_ACTIVE;
2848                         bios_6_scratch |= ATOM_S6_ACC_REQ_CRT2;
2849                 } else {
2850                         DRM_DEBUG_KMS("CRT2 disconnected\n");
2851                         bios_0_scratch &= ~ATOM_S0_CRT2_MASK;
2852                         bios_3_scratch &= ~ATOM_S3_CRT2_ACTIVE;
2853                         bios_6_scratch &= ~ATOM_S6_ACC_REQ_CRT2;
2854                 }
2855         }
2856         if ((radeon_encoder->devices & ATOM_DEVICE_DFP1_SUPPORT) &&
2857             (radeon_connector->devices & ATOM_DEVICE_DFP1_SUPPORT)) {
2858                 if (connected) {
2859                         DRM_DEBUG_KMS("DFP1 connected\n");
2860                         bios_0_scratch |= ATOM_S0_DFP1;
2861                         bios_3_scratch |= ATOM_S3_DFP1_ACTIVE;
2862                         bios_6_scratch |= ATOM_S6_ACC_REQ_DFP1;
2863                 } else {
2864                         DRM_DEBUG_KMS("DFP1 disconnected\n");
2865                         bios_0_scratch &= ~ATOM_S0_DFP1;
2866                         bios_3_scratch &= ~ATOM_S3_DFP1_ACTIVE;
2867                         bios_6_scratch &= ~ATOM_S6_ACC_REQ_DFP1;
2868                 }
2869         }
2870         if ((radeon_encoder->devices & ATOM_DEVICE_DFP2_SUPPORT) &&
2871             (radeon_connector->devices & ATOM_DEVICE_DFP2_SUPPORT)) {
2872                 if (connected) {
2873                         DRM_DEBUG_KMS("DFP2 connected\n");
2874                         bios_0_scratch |= ATOM_S0_DFP2;
2875                         bios_3_scratch |= ATOM_S3_DFP2_ACTIVE;
2876                         bios_6_scratch |= ATOM_S6_ACC_REQ_DFP2;
2877                 } else {
2878                         DRM_DEBUG_KMS("DFP2 disconnected\n");
2879                         bios_0_scratch &= ~ATOM_S0_DFP2;
2880                         bios_3_scratch &= ~ATOM_S3_DFP2_ACTIVE;
2881                         bios_6_scratch &= ~ATOM_S6_ACC_REQ_DFP2;
2882                 }
2883         }
2884         if ((radeon_encoder->devices & ATOM_DEVICE_DFP3_SUPPORT) &&
2885             (radeon_connector->devices & ATOM_DEVICE_DFP3_SUPPORT)) {
2886                 if (connected) {
2887                         DRM_DEBUG_KMS("DFP3 connected\n");
2888                         bios_0_scratch |= ATOM_S0_DFP3;
2889                         bios_3_scratch |= ATOM_S3_DFP3_ACTIVE;
2890                         bios_6_scratch |= ATOM_S6_ACC_REQ_DFP3;
2891                 } else {
2892                         DRM_DEBUG_KMS("DFP3 disconnected\n");
2893                         bios_0_scratch &= ~ATOM_S0_DFP3;
2894                         bios_3_scratch &= ~ATOM_S3_DFP3_ACTIVE;
2895                         bios_6_scratch &= ~ATOM_S6_ACC_REQ_DFP3;
2896                 }
2897         }
2898         if ((radeon_encoder->devices & ATOM_DEVICE_DFP4_SUPPORT) &&
2899             (radeon_connector->devices & ATOM_DEVICE_DFP4_SUPPORT)) {
2900                 if (connected) {
2901                         DRM_DEBUG_KMS("DFP4 connected\n");
2902                         bios_0_scratch |= ATOM_S0_DFP4;
2903                         bios_3_scratch |= ATOM_S3_DFP4_ACTIVE;
2904                         bios_6_scratch |= ATOM_S6_ACC_REQ_DFP4;
2905                 } else {
2906                         DRM_DEBUG_KMS("DFP4 disconnected\n");
2907                         bios_0_scratch &= ~ATOM_S0_DFP4;
2908                         bios_3_scratch &= ~ATOM_S3_DFP4_ACTIVE;
2909                         bios_6_scratch &= ~ATOM_S6_ACC_REQ_DFP4;
2910                 }
2911         }
2912         if ((radeon_encoder->devices & ATOM_DEVICE_DFP5_SUPPORT) &&
2913             (radeon_connector->devices & ATOM_DEVICE_DFP5_SUPPORT)) {
2914                 if (connected) {
2915                         DRM_DEBUG_KMS("DFP5 connected\n");
2916                         bios_0_scratch |= ATOM_S0_DFP5;
2917                         bios_3_scratch |= ATOM_S3_DFP5_ACTIVE;
2918                         bios_6_scratch |= ATOM_S6_ACC_REQ_DFP5;
2919                 } else {
2920                         DRM_DEBUG_KMS("DFP5 disconnected\n");
2921                         bios_0_scratch &= ~ATOM_S0_DFP5;
2922                         bios_3_scratch &= ~ATOM_S3_DFP5_ACTIVE;
2923                         bios_6_scratch &= ~ATOM_S6_ACC_REQ_DFP5;
2924                 }
2925         }
2926
2927         if (rdev->family >= CHIP_R600) {
2928                 WREG32(R600_BIOS_0_SCRATCH, bios_0_scratch);
2929                 WREG32(R600_BIOS_3_SCRATCH, bios_3_scratch);
2930                 WREG32(R600_BIOS_6_SCRATCH, bios_6_scratch);
2931         } else {
2932                 WREG32(RADEON_BIOS_0_SCRATCH, bios_0_scratch);
2933                 WREG32(RADEON_BIOS_3_SCRATCH, bios_3_scratch);
2934                 WREG32(RADEON_BIOS_6_SCRATCH, bios_6_scratch);
2935         }
2936 }
2937
2938 void
2939 radeon_atombios_encoder_crtc_scratch_regs(struct drm_encoder *encoder, int crtc)
2940 {
2941         struct drm_device *dev = encoder->dev;
2942         struct radeon_device *rdev = dev->dev_private;
2943         struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
2944         uint32_t bios_3_scratch;
2945
2946         if (rdev->family >= CHIP_R600)
2947                 bios_3_scratch = RREG32(R600_BIOS_3_SCRATCH);
2948         else
2949                 bios_3_scratch = RREG32(RADEON_BIOS_3_SCRATCH);
2950
2951         if (radeon_encoder->devices & ATOM_DEVICE_TV1_SUPPORT) {
2952                 bios_3_scratch &= ~ATOM_S3_TV1_CRTC_ACTIVE;
2953                 bios_3_scratch |= (crtc << 18);
2954         }
2955         if (radeon_encoder->devices & ATOM_DEVICE_CV_SUPPORT) {
2956                 bios_3_scratch &= ~ATOM_S3_CV_CRTC_ACTIVE;
2957                 bios_3_scratch |= (crtc << 24);
2958         }
2959         if (radeon_encoder->devices & ATOM_DEVICE_CRT1_SUPPORT) {
2960                 bios_3_scratch &= ~ATOM_S3_CRT1_CRTC_ACTIVE;
2961                 bios_3_scratch |= (crtc << 16);
2962         }
2963         if (radeon_encoder->devices & ATOM_DEVICE_CRT2_SUPPORT) {
2964                 bios_3_scratch &= ~ATOM_S3_CRT2_CRTC_ACTIVE;
2965                 bios_3_scratch |= (crtc << 20);
2966         }
2967         if (radeon_encoder->devices & ATOM_DEVICE_LCD1_SUPPORT) {
2968                 bios_3_scratch &= ~ATOM_S3_LCD1_CRTC_ACTIVE;
2969                 bios_3_scratch |= (crtc << 17);
2970         }
2971         if (radeon_encoder->devices & ATOM_DEVICE_DFP1_SUPPORT) {
2972                 bios_3_scratch &= ~ATOM_S3_DFP1_CRTC_ACTIVE;
2973                 bios_3_scratch |= (crtc << 19);
2974         }
2975         if (radeon_encoder->devices & ATOM_DEVICE_DFP2_SUPPORT) {
2976                 bios_3_scratch &= ~ATOM_S3_DFP2_CRTC_ACTIVE;
2977                 bios_3_scratch |= (crtc << 23);
2978         }
2979         if (radeon_encoder->devices & ATOM_DEVICE_DFP3_SUPPORT) {
2980                 bios_3_scratch &= ~ATOM_S3_DFP3_CRTC_ACTIVE;
2981                 bios_3_scratch |= (crtc << 25);
2982         }
2983
2984         if (rdev->family >= CHIP_R600)
2985                 WREG32(R600_BIOS_3_SCRATCH, bios_3_scratch);
2986         else
2987                 WREG32(RADEON_BIOS_3_SCRATCH, bios_3_scratch);
2988 }
2989
2990 void
2991 radeon_atombios_encoder_dpms_scratch_regs(struct drm_encoder *encoder, bool on)
2992 {
2993         struct drm_device *dev = encoder->dev;
2994         struct radeon_device *rdev = dev->dev_private;
2995         struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
2996         uint32_t bios_2_scratch;
2997
2998         if (rdev->family >= CHIP_R600)
2999                 bios_2_scratch = RREG32(R600_BIOS_2_SCRATCH);
3000         else
3001                 bios_2_scratch = RREG32(RADEON_BIOS_2_SCRATCH);
3002
3003         if (radeon_encoder->devices & ATOM_DEVICE_TV1_SUPPORT) {
3004                 if (on)
3005                         bios_2_scratch &= ~ATOM_S2_TV1_DPMS_STATE;
3006                 else
3007                         bios_2_scratch |= ATOM_S2_TV1_DPMS_STATE;
3008         }
3009         if (radeon_encoder->devices & ATOM_DEVICE_CV_SUPPORT) {
3010                 if (on)
3011                         bios_2_scratch &= ~ATOM_S2_CV_DPMS_STATE;
3012                 else
3013                         bios_2_scratch |= ATOM_S2_CV_DPMS_STATE;
3014         }
3015         if (radeon_encoder->devices & ATOM_DEVICE_CRT1_SUPPORT) {
3016                 if (on)
3017                         bios_2_scratch &= ~ATOM_S2_CRT1_DPMS_STATE;
3018                 else
3019                         bios_2_scratch |= ATOM_S2_CRT1_DPMS_STATE;
3020         }
3021         if (radeon_encoder->devices & ATOM_DEVICE_CRT2_SUPPORT) {
3022                 if (on)
3023                         bios_2_scratch &= ~ATOM_S2_CRT2_DPMS_STATE;
3024                 else
3025                         bios_2_scratch |= ATOM_S2_CRT2_DPMS_STATE;
3026         }
3027         if (radeon_encoder->devices & ATOM_DEVICE_LCD1_SUPPORT) {
3028                 if (on)
3029                         bios_2_scratch &= ~ATOM_S2_LCD1_DPMS_STATE;
3030                 else
3031                         bios_2_scratch |= ATOM_S2_LCD1_DPMS_STATE;
3032         }
3033         if (radeon_encoder->devices & ATOM_DEVICE_DFP1_SUPPORT) {
3034                 if (on)
3035                         bios_2_scratch &= ~ATOM_S2_DFP1_DPMS_STATE;
3036                 else
3037                         bios_2_scratch |= ATOM_S2_DFP1_DPMS_STATE;
3038         }
3039         if (radeon_encoder->devices & ATOM_DEVICE_DFP2_SUPPORT) {
3040                 if (on)
3041                         bios_2_scratch &= ~ATOM_S2_DFP2_DPMS_STATE;
3042                 else
3043                         bios_2_scratch |= ATOM_S2_DFP2_DPMS_STATE;
3044         }
3045         if (radeon_encoder->devices & ATOM_DEVICE_DFP3_SUPPORT) {
3046                 if (on)
3047                         bios_2_scratch &= ~ATOM_S2_DFP3_DPMS_STATE;
3048                 else
3049                         bios_2_scratch |= ATOM_S2_DFP3_DPMS_STATE;
3050         }
3051         if (radeon_encoder->devices & ATOM_DEVICE_DFP4_SUPPORT) {
3052                 if (on)
3053                         bios_2_scratch &= ~ATOM_S2_DFP4_DPMS_STATE;
3054                 else
3055                         bios_2_scratch |= ATOM_S2_DFP4_DPMS_STATE;
3056         }
3057         if (radeon_encoder->devices & ATOM_DEVICE_DFP5_SUPPORT) {
3058                 if (on)
3059                         bios_2_scratch &= ~ATOM_S2_DFP5_DPMS_STATE;
3060                 else
3061                         bios_2_scratch |= ATOM_S2_DFP5_DPMS_STATE;
3062         }
3063
3064         if (rdev->family >= CHIP_R600)
3065                 WREG32(R600_BIOS_2_SCRATCH, bios_2_scratch);
3066         else
3067                 WREG32(RADEON_BIOS_2_SCRATCH, bios_2_scratch);
3068 }