vserver 1.9.3
[linux-2.6.git] / drivers / video / aty / radeon_monitor.c
1 #include "radeonfb.h"
2 #include "../edid.h"
3
4 #ifdef CONFIG_PPC_OF
5 #include <asm/prom.h>
6 #include <asm/pci-bridge.h>
7 #endif /* CONFIG_PPC_OF */
8
9 static struct fb_var_screeninfo radeonfb_default_var = {
10         640, 480, 640, 480, 0, 0, 8, 0,
11         {0, 6, 0}, {0, 6, 0}, {0, 6, 0}, {0, 0, 0},
12         0, 0, -1, -1, 0, 39721, 40, 24, 32, 11, 96, 2,
13         0, FB_VMODE_NONINTERLACED
14 };
15
16 static char *radeon_get_mon_name(int type)
17 {
18         char *pret = NULL;
19
20         switch (type) {
21                 case MT_NONE:
22                         pret = "no";
23                         break;
24                 case MT_CRT:
25                         pret = "CRT";
26                         break;
27                 case MT_DFP:
28                         pret = "DFP";
29                         break;
30                 case MT_LCD:
31                         pret = "LCD";
32                         break;
33                 case MT_CTV:
34                         pret = "CTV";
35                         break;
36                 case MT_STV:
37                         pret = "STV";
38                         break;
39         }
40
41         return pret;
42 }
43
44
45 #ifdef CONFIG_PPC_OF
46 /*
47  * Try to find monitor informations & EDID data out of the Open Firmware
48  * device-tree. This also contains some "hacks" to work around a few machine
49  * models with broken OF probing by hard-coding known EDIDs for some Mac
50  * laptops internal LVDS panel. (XXX: not done yet)
51  */
52 static int __devinit radeon_parse_montype_prop(struct device_node *dp, u8 **out_EDID, int hdno)
53 {
54         static char *propnames[] = { "DFP,EDID", "LCD,EDID", "EDID", "EDID1", "EDID2",  NULL };
55         u8 *pedid = NULL;
56         u8 *pmt = NULL;
57         u8 *tmp;
58         int i, mt = MT_NONE;  
59         
60         RTRACE("analyzing OF properties...\n");
61         pmt = (u8 *)get_property(dp, "display-type", NULL);
62         if (!pmt)
63                 return MT_NONE;
64         RTRACE("display-type: %s\n", pmt);
65         /* OF says "LCD" for DFP as well, we discriminate from the caller of this
66          * function
67          */
68         if (!strcmp(pmt, "LCD") || !strcmp(pmt, "DFP"))
69                 mt = MT_DFP;
70         else if (!strcmp(pmt, "CRT"))
71                 mt = MT_CRT;
72         else if (strcmp(pmt, "NONE")) {
73                 printk(KERN_WARNING "radeonfb: Unknown OF display-type: %s\n", pmt);
74                 return MT_NONE;
75         } else
76                 return MT_NONE;
77
78         for (i = 0; propnames[i] != NULL; ++i) {
79                 pedid = (u8 *)get_property(dp, propnames[i], NULL);
80                 if (pedid != NULL)
81                         break;
82         }
83         /* We didn't find the EDID in the leaf node, some cards will actually
84          * put EDID1/EDID2 in the parent, look for these (typically M6 tipb).
85          * single-head cards have hdno == -1 and skip this step
86          */
87         if (pedid == NULL && dp->parent && (hdno != -1))
88                 pedid = get_property(dp->parent, (hdno == 0) ? "EDID1" : "EDID2", NULL);
89         if (pedid == NULL && dp->parent && (hdno == 0))
90                 pedid = get_property(dp->parent, "EDID", NULL);
91         if (pedid == NULL)
92                 return mt;
93
94         tmp = (u8 *)kmalloc(EDID_LENGTH, GFP_KERNEL);
95         if (!tmp)
96                 return mt;
97         memcpy(tmp, pedid, EDID_LENGTH);
98         *out_EDID = tmp;
99         return mt;
100 }
101
102 static int __devinit radeon_probe_OF_head(struct radeonfb_info *rinfo, int head_no,
103                                           u8 **out_EDID)
104 {
105         struct device_node *dp;
106
107         RTRACE("radeon_probe_OF_head\n");
108
109         dp = pci_device_to_OF_node(rinfo->pdev);
110         while (dp == NULL)
111                 return MT_NONE;
112
113         if (rinfo->has_CRTC2) {
114                 char *pname;
115                 int len, second = 0;
116
117                 dp = dp->child;
118                 do {
119                         if (!dp)
120                                 return MT_NONE;
121                         pname = (char *)get_property(dp, "name", NULL);
122                         if (!pname)
123                                 return MT_NONE;
124                         len = strlen(pname);
125                         RTRACE("head: %s (letter: %c, head_no: %d)\n",
126                                pname, pname[len-1], head_no);
127                         if (pname[len-1] == 'A' && head_no == 0) {
128                                 int mt = radeon_parse_montype_prop(dp, out_EDID, 0);
129                                 /* Maybe check for LVDS_GEN_CNTL here ? I need to check out
130                                  * what OF does when booting with lid closed
131                                  */
132                                 if (mt == MT_DFP && rinfo->is_mobility)
133                                         mt = MT_LCD;
134                                 return mt;
135                         } else if (pname[len-1] == 'B' && head_no == 1)
136                                 return radeon_parse_montype_prop(dp, out_EDID, 1);
137                         second = 1;
138                         dp = dp->sibling;
139                 } while(!second);
140         } else {
141                 if (head_no > 0)
142                         return MT_NONE;
143                 return radeon_parse_montype_prop(dp, out_EDID, -1);
144         }
145         return MT_NONE;
146 }
147 #endif /* CONFIG_PPC_OF */
148
149
150 static int __devinit radeon_get_panel_info_BIOS(struct radeonfb_info *rinfo)
151 {
152         unsigned long tmp, tmp0;
153         char stmp[30];
154         int i;
155
156         if (!rinfo->bios_seg)
157                 return 0;
158
159         if (!(tmp = BIOS_IN16(rinfo->fp_bios_start + 0x40))) {
160                 printk(KERN_ERR "radeonfb: Failed to detect DFP panel info using BIOS\n");
161                 rinfo->panel_info.pwr_delay = 200;
162                 return 0;
163         }
164
165         for(i=0; i<24; i++)
166                 stmp[i] = BIOS_IN8(tmp+i+1);
167         stmp[24] = 0;
168         printk("radeonfb: panel ID string: %s\n", stmp);
169         rinfo->panel_info.xres = BIOS_IN16(tmp + 25);
170         rinfo->panel_info.yres = BIOS_IN16(tmp + 27);
171         printk("radeonfb: detected LVDS panel size from BIOS: %dx%d\n",
172                 rinfo->panel_info.xres, rinfo->panel_info.yres);
173
174         rinfo->panel_info.pwr_delay = BIOS_IN16(tmp + 44);
175         RTRACE("BIOS provided panel power delay: %d\n", rinfo->panel_info.pwr_delay);
176         if (rinfo->panel_info.pwr_delay > 2000 || rinfo->panel_info.pwr_delay <= 0)
177                 rinfo->panel_info.pwr_delay = 2000;
178
179         /*
180          * Some panels only work properly with some divider combinations
181          */
182         rinfo->panel_info.ref_divider = BIOS_IN16(tmp + 46);
183         rinfo->panel_info.post_divider = BIOS_IN8(tmp + 48);
184         rinfo->panel_info.fbk_divider = BIOS_IN16(tmp + 49);
185         if (rinfo->panel_info.ref_divider != 0 &&
186             rinfo->panel_info.fbk_divider > 3) {
187                 rinfo->panel_info.use_bios_dividers = 1;
188                 printk(KERN_INFO "radeondb: BIOS provided dividers will be used\n");
189                 RTRACE("ref_divider = %x\n", rinfo->panel_info.ref_divider);
190                 RTRACE("post_divider = %x\n", rinfo->panel_info.post_divider);
191                 RTRACE("fbk_divider = %x\n", rinfo->panel_info.fbk_divider);
192         }
193         RTRACE("Scanning BIOS table ...\n");
194         for(i=0; i<32; i++) {
195                 tmp0 = BIOS_IN16(tmp+64+i*2);
196                 if (tmp0 == 0)
197                         break;
198                 RTRACE(" %d x %d\n", BIOS_IN16(tmp0), BIOS_IN16(tmp0+2));
199                 if ((BIOS_IN16(tmp0) == rinfo->panel_info.xres) &&
200                     (BIOS_IN16(tmp0+2) == rinfo->panel_info.yres)) {
201                         rinfo->panel_info.hblank = (BIOS_IN16(tmp0+17) - BIOS_IN16(tmp0+19)) * 8;
202                         rinfo->panel_info.hOver_plus = ((BIOS_IN16(tmp0+21) -
203                                                          BIOS_IN16(tmp0+19) -1) * 8) & 0x7fff;
204                         rinfo->panel_info.hSync_width = BIOS_IN8(tmp0+23) * 8;
205                         rinfo->panel_info.vblank = BIOS_IN16(tmp0+24) - BIOS_IN16(tmp0+26);
206                         rinfo->panel_info.vOver_plus = (BIOS_IN16(tmp0+28) & 0x7ff) - BIOS_IN16(tmp0+26);
207                         rinfo->panel_info.vSync_width = (BIOS_IN16(tmp0+28) & 0xf800) >> 11;
208                         rinfo->panel_info.clock = BIOS_IN16(tmp0+9);
209                         /* Assume high active syncs for now until ATI tells me more... maybe we
210                          * can probe register values here ?
211                          */
212                         rinfo->panel_info.hAct_high = 1;
213                         rinfo->panel_info.vAct_high = 1;
214                         /* Mark panel infos valid */
215                         rinfo->panel_info.valid = 1;
216
217                         RTRACE("Found panel in BIOS table:\n");
218                         RTRACE("  hblank: %d\n", rinfo->panel_info.hblank);
219                         RTRACE("  hOver_plus: %d\n", rinfo->panel_info.hOver_plus);
220                         RTRACE("  hSync_width: %d\n", rinfo->panel_info.hSync_width);
221                         RTRACE("  vblank: %d\n", rinfo->panel_info.vblank);
222                         RTRACE("  vOver_plus: %d\n", rinfo->panel_info.vOver_plus);
223                         RTRACE("  vSync_width: %d\n", rinfo->panel_info.vSync_width);
224                         RTRACE("  clock: %d\n", rinfo->panel_info.clock);
225                                 
226                         return 1;
227                 }
228         }
229         RTRACE("Didn't find panel in BIOS table !\n");
230
231         return 0;
232 }
233
234 /* Try to extract the connector informations from the BIOS. This
235  * doesn't quite work yet, but it's output is still useful for
236  * debugging
237  */
238 static void __devinit radeon_parse_connector_info(struct radeonfb_info *rinfo)
239 {
240         int offset, chips, connectors, tmp, i, conn, type;
241
242         static char* __conn_type_table[16] = {
243                 "NONE", "Proprietary", "CRT", "DVI-I", "DVI-D", "Unknown", "Unknown",
244                 "Unknown", "Unknown", "Unknown", "Unknown", "Unknown", "Unknown",
245                 "Unknown", "Unknown", "Unknown"
246         };
247
248         if (!rinfo->bios_seg)
249                 return;
250
251         offset = BIOS_IN16(rinfo->fp_bios_start + 0x50);
252         if (offset == 0) {
253                 printk(KERN_WARNING "radeonfb: No connector info table detected\n");
254                 return;
255         }
256
257         /* Don't do much more at this point but displaying the data if
258          * DEBUG is enabled
259          */
260         chips = BIOS_IN8(offset++) >> 4;
261         RTRACE("%d chips in connector info\n", chips);
262         for (i = 0; i < chips; i++) {
263                 tmp = BIOS_IN8(offset++);
264                 connectors = tmp & 0x0f;
265                 RTRACE(" - chip %d has %d connectors\n", tmp >> 4, connectors);
266                 for (conn = 0; ; conn++) {
267                         tmp = BIOS_IN16(offset);
268                         if (tmp == 0)
269                                 break;
270                         offset += 2;
271                         type = (tmp >> 12) & 0x0f;
272                         RTRACE("  * connector %d of type %d (%s) : %04x\n",
273                                conn, type, __conn_type_table[type], tmp);
274                 }
275         }
276 }
277
278
279 /*
280  * Probe physical connection of a CRT. This code comes from XFree
281  * as well and currently is only implemented for the CRT DAC, the
282  * code for the TVDAC is commented out in XFree as "non working"
283  */
284 static int __devinit radeon_crt_is_connected(struct radeonfb_info *rinfo, int is_crt_dac)
285 {
286     int           connected = 0;
287
288     /* the monitor either wasn't connected or it is a non-DDC CRT.
289      * try to probe it
290      */
291     if (is_crt_dac) {
292         unsigned long ulOrigVCLK_ECP_CNTL;
293         unsigned long ulOrigDAC_CNTL;
294         unsigned long ulOrigDAC_EXT_CNTL;
295         unsigned long ulOrigCRTC_EXT_CNTL;
296         unsigned long ulData;
297         unsigned long ulMask;
298
299         ulOrigVCLK_ECP_CNTL = INPLL(VCLK_ECP_CNTL);
300
301         ulData              = ulOrigVCLK_ECP_CNTL;
302         ulData             &= ~(PIXCLK_ALWAYS_ONb
303                                 | PIXCLK_DAC_ALWAYS_ONb);
304         ulMask              = ~(PIXCLK_ALWAYS_ONb
305                                 | PIXCLK_DAC_ALWAYS_ONb);
306         OUTPLLP(VCLK_ECP_CNTL, ulData, ulMask);
307
308         ulOrigCRTC_EXT_CNTL = INREG(CRTC_EXT_CNTL);
309         ulData              = ulOrigCRTC_EXT_CNTL;
310         ulData             |= CRTC_CRT_ON;
311         OUTREG(CRTC_EXT_CNTL, ulData);
312    
313         ulOrigDAC_EXT_CNTL = INREG(DAC_EXT_CNTL);
314         ulData             = ulOrigDAC_EXT_CNTL;
315         ulData            &= ~DAC_FORCE_DATA_MASK;
316         ulData            |=  (DAC_FORCE_BLANK_OFF_EN
317                                |DAC_FORCE_DATA_EN
318                                |DAC_FORCE_DATA_SEL_MASK);
319         if ((rinfo->family == CHIP_FAMILY_RV250) ||
320             (rinfo->family == CHIP_FAMILY_RV280))
321             ulData |= (0x01b6 << DAC_FORCE_DATA_SHIFT);
322         else
323             ulData |= (0x01ac << DAC_FORCE_DATA_SHIFT);
324
325         OUTREG(DAC_EXT_CNTL, ulData);
326
327         ulOrigDAC_CNTL     = INREG(DAC_CNTL);
328         ulData             = ulOrigDAC_CNTL;
329         ulData            |= DAC_CMP_EN;
330         ulData            &= ~(DAC_RANGE_CNTL_MASK
331                                | DAC_PDWN);
332         ulData            |= 0x2;
333         OUTREG(DAC_CNTL, ulData);
334
335         mdelay(1);
336
337         ulData     = INREG(DAC_CNTL);
338         connected =  (DAC_CMP_OUTPUT & ulData) ? 1 : 0;
339   
340         ulData    = ulOrigVCLK_ECP_CNTL;
341         ulMask    = 0xFFFFFFFFL;
342         OUTPLLP(VCLK_ECP_CNTL, ulData, ulMask);
343
344         OUTREG(DAC_CNTL,      ulOrigDAC_CNTL     );
345         OUTREG(DAC_EXT_CNTL,  ulOrigDAC_EXT_CNTL );
346         OUTREG(CRTC_EXT_CNTL, ulOrigCRTC_EXT_CNTL);
347     }
348
349     return connected ? MT_CRT : MT_NONE;
350 }
351
352 /*
353  * Parse the "monitor_layout" string if any. This code is mostly
354  * copied from XFree's radeon driver
355  */
356 static int __devinit radeon_parse_monitor_layout(struct radeonfb_info *rinfo,
357                                                  const char *monitor_layout)
358 {
359         char s1[5], s2[5];
360         int i = 0, second = 0;
361         const char *s;
362
363         if (!monitor_layout)
364                 return 0;
365
366         s = monitor_layout;
367         do {
368                 switch(*s) {
369                 case ',':
370                         s1[i] = '\0';
371                         i = 0;
372                         second = 1;
373                         break;
374                 case ' ':
375                 case '\0':
376                         break;
377                 default:
378                         if (i > 4)
379                                 break;
380                         if (second)
381                                 s2[i] = *s;
382                         else
383                                 s1[i] = *s;
384                         i++;
385                 }
386         } while (*s++);
387         if (second)
388                 s2[i] = 0;
389         else {
390                 s1[i] = 0;
391                 s2[0] = 0;
392         }
393         if (strcmp(s1, "CRT") == 0)
394                 rinfo->mon1_type = MT_CRT;
395         else if (strcmp(s1, "TMDS") == 0)
396                 rinfo->mon1_type = MT_DFP;
397         else if (strcmp(s1, "LVDS") == 0)
398                 rinfo->mon1_type = MT_LCD;
399
400         if (strcmp(s2, "CRT") == 0)
401                 rinfo->mon2_type = MT_CRT;
402         else if (strcmp(s2, "TMDS") == 0)
403                 rinfo->mon2_type = MT_DFP;
404         else if (strcmp(s2, "LVDS") == 0)
405                 rinfo->mon2_type = MT_LCD;
406
407         return 1;
408 }
409
410 /*
411  * Probe display on both primary and secondary card's connector (if any)
412  * by various available techniques (i2c, OF device tree, BIOS, ...) and
413  * try to retreive EDID. The algorithm here comes from XFree's radeon
414  * driver
415  */
416 void __devinit radeon_probe_screens(struct radeonfb_info *rinfo,
417                                     const char *monitor_layout, int ignore_edid)
418 {
419 #ifdef CONFIG_FB_RADEON_I2C
420         int ddc_crt2_used = 0;  
421 #endif
422         int tmp, i;
423
424         radeon_parse_connector_info(rinfo);
425
426         if (radeon_parse_monitor_layout(rinfo, monitor_layout)) {
427
428                 /*
429                  * If user specified a monitor_layout option, use it instead
430                  * of auto-detecting. Maybe we should only use this argument
431                  * on the first radeon card probed or provide a way to specify
432                  * a layout for each card ?
433                  */
434
435                 RTRACE("Using specified monitor layout: %s", monitor_layout);
436 #ifdef CONFIG_FB_RADEON_I2C
437                 if (!ignore_edid) {
438                         if (rinfo->mon1_type != MT_NONE)
439                                 if (!radeon_probe_i2c_connector(rinfo, ddc_dvi, &rinfo->mon1_EDID)) {
440                                         radeon_probe_i2c_connector(rinfo, ddc_crt2, &rinfo->mon1_EDID);
441                                         ddc_crt2_used = 1;
442                                 }
443                         if (rinfo->mon2_type != MT_NONE)
444                                 if (!radeon_probe_i2c_connector(rinfo, ddc_vga, &rinfo->mon2_EDID) &&
445                                     !ddc_crt2_used)
446                                         radeon_probe_i2c_connector(rinfo, ddc_crt2, &rinfo->mon2_EDID);
447                 }
448 #endif /* CONFIG_FB_RADEON_I2C */
449                 if (rinfo->mon1_type == MT_NONE) {
450                         if (rinfo->mon2_type != MT_NONE) {
451                                 rinfo->mon1_type = rinfo->mon2_type;
452                                 rinfo->mon1_EDID = rinfo->mon2_EDID;
453                         } else {
454                                 rinfo->mon1_type = MT_CRT;
455                                 printk(KERN_INFO "radeonfb: No valid monitor, assuming CRT on first port\n");
456                         }
457                         rinfo->mon2_type = MT_NONE;
458                         rinfo->mon2_EDID = NULL;
459                 }
460         } else {
461                 /*
462                  * Auto-detecting display type (well... trying to ...)
463                  */
464                 
465                 RTRACE("Starting monitor auto detection...\n");
466
467 #if DEBUG && defined(CONFIG_FB_RADEON_I2C)
468                 {
469                         u8 *EDIDs[4] = { NULL, NULL, NULL, NULL };
470                         int mon_types[4] = {MT_NONE, MT_NONE, MT_NONE, MT_NONE};
471                         int i;
472
473                         for (i = 0; i < 4; i++)
474                                 mon_types[i] = radeon_probe_i2c_connector(rinfo,
475                                                                           i+1, &EDIDs[i]);
476                 }
477 #endif /* DEBUG */
478                 /*
479                  * Old single head cards
480                  */
481                 if (!rinfo->has_CRTC2) {
482 #ifdef CONFIG_PPC_OF
483                         if (rinfo->mon1_type == MT_NONE)
484                                 rinfo->mon1_type = radeon_probe_OF_head(rinfo, 0,
485                                                                         &rinfo->mon1_EDID);
486 #endif /* CONFIG_PPC_OF */
487 #ifdef CONFIG_FB_RADEON_I2C
488                         if (rinfo->mon1_type == MT_NONE)
489                                 rinfo->mon1_type = radeon_probe_i2c_connector(rinfo, ddc_dvi,
490                                                                       &rinfo->mon1_EDID);
491                         if (rinfo->mon1_type == MT_NONE)
492                                 rinfo->mon1_type =
493                                         radeon_probe_i2c_connector(rinfo, ddc_vga,
494                                                                    &rinfo->mon1_EDID);
495                         if (rinfo->mon1_type == MT_NONE)
496                                 rinfo->mon1_type =
497                                         radeon_probe_i2c_connector(rinfo, ddc_crt2,
498                                                                    &rinfo->mon1_EDID);  
499 #endif /* CONFIG_FB_RADEON_I2C */
500                         if (rinfo->mon1_type == MT_NONE)
501                                 rinfo->mon1_type = MT_CRT;
502                         goto bail;
503                 }
504
505                 /*
506                  * Check for cards with reversed DACs or TMDS controllers using BIOS
507                  */
508                 if (rinfo->bios_seg &&
509                     (tmp = BIOS_IN16(rinfo->fp_bios_start + 0x50))) {
510                         for (i = 1; i < 4; i++) {
511                                 unsigned int tmp0;
512
513                                 if (!BIOS_IN8(tmp + i*2) && i > 1)
514                                         break;
515                                 tmp0 = BIOS_IN16(tmp + i*2);
516                                 if ((!(tmp0 & 0x01)) && (((tmp0 >> 8) & 0x0f) == ddc_dvi)) {
517                                         rinfo->reversed_DAC = 1;
518                                         printk(KERN_INFO "radeonfb: Reversed DACs detected\n");
519                                 }
520                                 if ((((tmp0 >> 8) & 0x0f) == ddc_dvi) && ((tmp0 >> 4) & 0x01)) {
521                                         rinfo->reversed_TMDS = 1;
522                                         printk(KERN_INFO "radeonfb: Reversed TMDS detected\n");
523                                 }
524                         }
525                 }
526
527                 /*
528                  * Probe primary head (DVI or laptop internal panel)
529                  */
530 #ifdef CONFIG_PPC_OF
531                 if (rinfo->mon1_type == MT_NONE)
532                         rinfo->mon1_type = radeon_probe_OF_head(rinfo, 0, &rinfo->mon1_EDID);
533 #endif /* CONFIG_PPC_OF */
534 #ifdef CONFIG_FB_RADEON_I2C
535                 if (rinfo->mon1_type == MT_NONE)
536                         rinfo->mon1_type = radeon_probe_i2c_connector(rinfo, ddc_dvi,
537                                                                       &rinfo->mon1_EDID);
538                 if (rinfo->mon1_type == MT_NONE) {
539                         rinfo->mon1_type = radeon_probe_i2c_connector(rinfo, ddc_crt2,
540                                                                       &rinfo->mon1_EDID);
541                         if (rinfo->mon1_type != MT_NONE)
542                                 ddc_crt2_used = 1;
543                 }
544 #endif /* CONFIG_FB_RADEON_I2C */
545                 if (rinfo->mon1_type == MT_NONE && rinfo->is_mobility &&
546                     ((rinfo->bios_seg && (INREG(BIOS_4_SCRATCH) & 4))
547                      || (INREG(LVDS_GEN_CNTL) & LVDS_ON))) {
548                         rinfo->mon1_type = MT_LCD;
549                         printk("Non-DDC laptop panel detected\n");
550                 }
551                 if (rinfo->mon1_type == MT_NONE)
552                         rinfo->mon1_type = radeon_crt_is_connected(rinfo, rinfo->reversed_DAC);
553
554                 /*
555                  * Probe secondary head (mostly VGA, can be DVI)
556                  */
557 #ifdef CONFIG_PPC_OF
558                 if (rinfo->mon2_type == MT_NONE)
559                         rinfo->mon2_type = radeon_probe_OF_head(rinfo, 1, &rinfo->mon2_EDID);
560 #endif /* CONFIG_PPC_OF */
561 #ifdef CONFIG_FB_RADEON_I2C
562                 if (rinfo->mon2_type == MT_NONE)
563                         rinfo->mon2_type = radeon_probe_i2c_connector(rinfo, ddc_vga,
564                                                                       &rinfo->mon2_EDID);
565                 if (rinfo->mon2_type == MT_NONE && !ddc_crt2_used)
566                         rinfo->mon2_type = radeon_probe_i2c_connector(rinfo, ddc_crt2,
567                                                                       &rinfo->mon2_EDID);
568 #endif /* CONFIG_FB_RADEON_I2C */
569                 if (rinfo->mon2_type == MT_NONE)
570                         rinfo->mon2_type = radeon_crt_is_connected(rinfo, !rinfo->reversed_DAC);
571
572                 /*
573                  * If we only detected port 2, we swap them, if none detected,
574                  * assume CRT (maybe fallback to old BIOS_SCRATCH stuff ? or look
575                  * at FP registers ?)
576                  */
577                 if (rinfo->mon1_type == MT_NONE) {
578                         if (rinfo->mon2_type != MT_NONE) {
579                                 rinfo->mon1_type = rinfo->mon2_type;
580                                 rinfo->mon1_EDID = rinfo->mon2_EDID;
581                         } else
582                                 rinfo->mon1_type = MT_CRT;
583                         rinfo->mon2_type = MT_NONE;
584                         rinfo->mon2_EDID = NULL;
585                 }
586
587                 /*
588                  * Deal with reversed TMDS
589                  */
590                 if (rinfo->reversed_TMDS) {
591                         /* Always keep internal TMDS as primary head */
592                         if (rinfo->mon1_type == MT_DFP || rinfo->mon2_type == MT_DFP) {
593                                 int tmp_type = rinfo->mon1_type;
594                                 u8 *tmp_EDID = rinfo->mon1_EDID;
595                                 rinfo->mon1_type = rinfo->mon2_type;
596                                 rinfo->mon1_EDID = rinfo->mon2_EDID;
597                                 rinfo->mon2_type = tmp_type;
598                                 rinfo->mon2_EDID = tmp_EDID;
599                                 if (rinfo->mon1_type == MT_CRT || rinfo->mon2_type == MT_CRT)
600                                         rinfo->reversed_DAC ^= 1;
601                         }
602                 }
603         }
604         if (ignore_edid) {
605                 if (rinfo->mon1_EDID)
606                         kfree(rinfo->mon1_EDID);
607                 rinfo->mon1_EDID = NULL;
608                 if (rinfo->mon2_EDID)
609                         kfree(rinfo->mon2_EDID);
610                 rinfo->mon2_EDID = NULL;
611         }
612
613  bail:
614         printk(KERN_INFO "radeonfb: Monitor 1 type %s found\n",
615                radeon_get_mon_name(rinfo->mon1_type));
616         if (rinfo->mon1_EDID)
617                 printk(KERN_INFO "radeonfb: EDID probed\n");
618         if (!rinfo->has_CRTC2)
619                 return;
620         printk(KERN_INFO "radeonfb: Monitor 2 type %s found\n",
621                radeon_get_mon_name(rinfo->mon2_type));
622         if (rinfo->mon2_EDID)
623                 printk(KERN_INFO "radeonfb: EDID probed\n");
624 }
625
626
627 /*
628  * This functions applyes any arch/model/machine specific fixups
629  * to the panel info. It may eventually alter EDID block as
630  * well or whatever is specific to a given model and not probed
631  * properly by the default code
632  */
633 static void radeon_fixup_panel_info(struct radeonfb_info *rinfo)
634 {
635         /*
636          * A few iBook laptop panels seem to need a fixed PLL setting
637          *
638          * We should probably do this differently based on the panel
639          * type/model or eventually some other device-tree informations,
640          * but these tweaks below work enough for now. --BenH
641          */
642 #ifdef CONFIG_PPC_OF
643         /* iBook2's */
644         if (machine_is_compatible("PowerBook4,3")) {
645                 rinfo->panel_info.ref_divider = rinfo->pll.ref_div;
646                 rinfo->panel_info.post_divider = 0x6;
647                 rinfo->panel_info.fbk_divider = 0xad;
648                 rinfo->panel_info.use_bios_dividers = 1;
649         }
650         /* Aluminium PowerBook 15" */
651         if (machine_is_compatible("PowerBook5,4")) {
652                 rinfo->panel_info.ref_divider = rinfo->pll.ref_div;
653                 rinfo->panel_info.post_divider = 0x2;
654                 rinfo->panel_info.fbk_divider = 0x8e;
655                 rinfo->panel_info.use_bios_dividers = 1;
656         }
657         /* Aluminium PowerBook 17" */
658         if (machine_is_compatible("PowerBook5,3") ||
659             machine_is_compatible("PowerBook5,5")) {
660                 rinfo->panel_info.ref_divider = rinfo->pll.ref_div;
661                 rinfo->panel_info.post_divider = 0x4;
662                 rinfo->panel_info.fbk_divider = 0x80;
663                 rinfo->panel_info.use_bios_dividers = 1;
664         }
665         /* iBook G4 */
666         if (machine_is_compatible("PowerBook6,3") ||
667             machine_is_compatible("PowerBook6,5")) {
668                 rinfo->panel_info.ref_divider = rinfo->pll.ref_div;
669                 rinfo->panel_info.post_divider = 0x6;
670                 rinfo->panel_info.fbk_divider = 0xad;
671                 rinfo->panel_info.use_bios_dividers = 1;
672         }
673 #endif /* CONFIG_PPC_OF */
674 }
675
676
677 /*
678  * Fill up panel infos from a mode definition, either returned by the EDID
679  * or from the default mode when we can't do any better
680  */
681 static void radeon_var_to_panel_info(struct radeonfb_info *rinfo, struct fb_var_screeninfo *var)
682 {
683         rinfo->panel_info.xres = var->xres;
684         rinfo->panel_info.yres = var->yres;
685         rinfo->panel_info.clock = 100000000 / var->pixclock;
686         rinfo->panel_info.hOver_plus = var->right_margin;
687         rinfo->panel_info.hSync_width = var->hsync_len;
688         rinfo->panel_info.hblank = var->left_margin +
689                 (var->right_margin + var->hsync_len);
690         rinfo->panel_info.vOver_plus = var->lower_margin;
691         rinfo->panel_info.vSync_width = var->vsync_len;
692         rinfo->panel_info.vblank = var->upper_margin +
693                 (var->lower_margin + var->vsync_len);
694         rinfo->panel_info.hAct_high =
695                 (var->sync & FB_SYNC_HOR_HIGH_ACT) != 0;
696         rinfo->panel_info.vAct_high =
697                 (var->sync & FB_SYNC_VERT_HIGH_ACT) != 0;
698         rinfo->panel_info.valid = 1;
699         /* We use a default of 200ms for the panel power delay, 
700          * I need to have a real schedule() instead of mdelay's in the panel code.
701          * we might be possible to figure out a better power delay either from
702          * MacOS OF tree or from the EDID block (proprietary extensions ?)
703          */
704         rinfo->panel_info.pwr_delay = 200;
705 }
706
707 static void radeon_var_to_videomode(struct fb_videomode *mode,
708                                     const struct fb_var_screeninfo *var)
709 {
710     mode->xres = var->xres;
711     mode->yres = var->yres;
712     mode->pixclock = var->pixclock;
713     mode->left_margin = var->left_margin;
714     mode->right_margin = var->right_margin;
715     mode->upper_margin = var->upper_margin;
716     mode->lower_margin = var->lower_margin;
717     mode->hsync_len = var->hsync_len;
718     mode->vsync_len = var->vsync_len;
719     mode->sync = var->sync;
720     mode->vmode = var->vmode;
721 }
722
723 static void radeon_videomode_to_var(struct fb_var_screeninfo *var,
724                                     const struct fb_videomode *mode)
725 {
726     var->xres = mode->xres;
727     var->yres = mode->yres;
728     var->xres_virtual = mode->xres;
729     var->yres_virtual = mode->yres;
730     var->xoffset = 0;
731     var->yoffset = 0;
732     var->pixclock = mode->pixclock;
733     var->left_margin = mode->left_margin;
734     var->right_margin = mode->right_margin;
735     var->upper_margin = mode->upper_margin;
736     var->lower_margin = mode->lower_margin;
737     var->hsync_len = mode->hsync_len;
738     var->vsync_len = mode->vsync_len;
739     var->sync = mode->sync;
740     var->vmode = mode->vmode;
741 }
742
743 /*
744  * Build the modedb for head 1 (head 2 will come later), check panel infos
745  * from either BIOS or EDID, and pick up the default mode
746  */
747 void __devinit radeon_check_modes(struct radeonfb_info *rinfo, const char *mode_option)
748 {
749         int has_default_mode = 0;
750
751         /*
752          * Fill default var first
753          */
754         rinfo->info->var = radeonfb_default_var;
755
756         /*
757          * First check out what BIOS has to say
758          */
759         if (rinfo->mon1_type == MT_LCD)
760                 radeon_get_panel_info_BIOS(rinfo);
761
762         /*
763          * Parse EDID detailed timings and deduce panel infos if any. Right now
764          * we only deal with first entry returned by parse_EDID, we may do better
765          * some day...
766          */
767         if (!rinfo->panel_info.use_bios_dividers && rinfo->mon1_type != MT_CRT
768             && rinfo->mon1_EDID) {
769                 struct fb_var_screeninfo var;
770                 RTRACE("Parsing EDID data for panel info\n");
771                 if (fb_parse_edid(rinfo->mon1_EDID, &var) == 0) {
772                         if (var.xres >= rinfo->panel_info.xres &&
773                             var.yres >= rinfo->panel_info.yres)
774                                 radeon_var_to_panel_info(rinfo, &var);
775                 }
776         }
777
778         /*
779          * Do any additional platform/arch fixups to the panel infos
780          */
781         radeon_fixup_panel_info(rinfo);
782
783         /*
784          * If we have some valid panel infos, we setup the default mode based on
785          * those
786          */
787         if (rinfo->mon1_type != MT_CRT && rinfo->panel_info.valid) {
788                 struct fb_var_screeninfo *var = &rinfo->info->var;
789
790                 RTRACE("Setting up default mode based on panel info\n");
791                 var->xres = rinfo->panel_info.xres;
792                 var->yres = rinfo->panel_info.yres;
793                 var->xres_virtual = rinfo->panel_info.xres;
794                 var->yres_virtual = rinfo->panel_info.yres;
795                 var->xoffset = var->yoffset = 0;
796                 var->bits_per_pixel = 8;
797                 var->pixclock = 100000000 / rinfo->panel_info.clock;
798                 var->left_margin = (rinfo->panel_info.hblank - rinfo->panel_info.hOver_plus
799                                     - rinfo->panel_info.hSync_width);
800                 var->right_margin = rinfo->panel_info.hOver_plus;
801                 var->upper_margin = (rinfo->panel_info.vblank - rinfo->panel_info.vOver_plus
802                                      - rinfo->panel_info.vSync_width);
803                 var->lower_margin = rinfo->panel_info.vOver_plus;
804                 var->hsync_len = rinfo->panel_info.hSync_width;
805                 var->vsync_len = rinfo->panel_info.vSync_width;
806                 var->sync = 0;
807                 if (rinfo->panel_info.hAct_high)
808                         var->sync |= FB_SYNC_HOR_HIGH_ACT;
809                 if (rinfo->panel_info.vAct_high)
810                         var->sync |= FB_SYNC_VERT_HIGH_ACT;
811                 var->vmode = 0;
812                 has_default_mode = 1;
813         }
814
815         /*
816          * Now build modedb from EDID
817          */
818         if (rinfo->mon1_EDID) {
819                 rinfo->mon1_modedb = fb_create_modedb(rinfo->mon1_EDID,
820                                                       &rinfo->mon1_dbsize);
821                 fb_get_monitor_limits(rinfo->mon1_EDID, &rinfo->info->monspecs);
822         }
823
824         
825         /*
826          * Finally, if we don't have panel infos we need to figure some (or
827          * we try to read it from card), we try to pick a default mode
828          * and create some panel infos. Whatever...
829          */
830         if (rinfo->mon1_type != MT_CRT && !rinfo->panel_info.valid) {
831                 struct fb_videomode     *modedb;
832                 int                     dbsize;
833                 char                    modename[32];
834
835                 RTRACE("Guessing panel info...\n");
836                 if (rinfo->panel_info.xres == 0 || rinfo->panel_info.yres == 0) {
837                         u32 tmp = INREG(FP_HORZ_STRETCH) & HORZ_PANEL_SIZE;
838                         rinfo->panel_info.xres = ((tmp >> HORZ_PANEL_SHIFT) + 1) * 8;
839                         tmp = INREG(FP_VERT_STRETCH) & VERT_PANEL_SIZE;
840                         rinfo->panel_info.yres = (tmp >> VERT_PANEL_SHIFT) + 1;
841                 }
842                 if (rinfo->panel_info.xres == 0 || rinfo->panel_info.yres == 0) {
843                         printk(KERN_WARNING "radeonfb: Can't find panel size, going back to CRT\n");
844                         rinfo->mon1_type = MT_CRT;
845                         goto pickup_default;
846                 }
847                 printk(KERN_WARNING "radeonfb: Assuming panel size %dx%d\n",
848                        rinfo->panel_info.xres, rinfo->panel_info.yres);
849                 modedb = rinfo->mon1_modedb;
850                 dbsize = rinfo->mon1_dbsize;
851                 snprintf(modename, 31, "%dx%d", rinfo->panel_info.xres, rinfo->panel_info.yres);
852                 if (fb_find_mode(&rinfo->info->var, rinfo->info, modename,
853                                  modedb, dbsize, NULL, 8) == 0) {
854                         printk(KERN_WARNING "radeonfb: Can't find mode for panel size, going back to CRT\n");
855                         rinfo->mon1_type = MT_CRT;
856                         goto pickup_default;
857                 }
858                 has_default_mode = 1;
859                 radeon_var_to_panel_info(rinfo, &rinfo->info->var);
860         }
861
862  pickup_default:
863         /*
864          * Pick up a random default mode
865          */
866         if (!has_default_mode || mode_option) {
867                 struct fb_videomode default_mode;
868                 if (has_default_mode)
869                         radeon_var_to_videomode(&default_mode, &rinfo->info->var);
870                 else
871                         radeon_var_to_videomode(&default_mode, &radeonfb_default_var);
872                 if (fb_find_mode(&rinfo->info->var, rinfo->info, mode_option,
873                                  rinfo->mon1_modedb, rinfo->mon1_dbsize, &default_mode, 8) == 0)
874                         rinfo->info->var = radeonfb_default_var;
875         }
876
877 }
878
879 /*
880  * The code below is used to pick up a mode in check_var and
881  * set_var. It should be made generic
882  */
883
884 /*
885  * This is used when looking for modes. We assign a "goodness" value
886  * to a mode in the modedb depending how "close" it is from what we
887  * are looking for.
888  * Currently, we don't compare that much, we could do better but
889  * the current fbcon doesn't quite mind ;)
890  */
891 static int radeon_compare_modes(const struct fb_var_screeninfo *var,
892                                 const struct fb_videomode *mode)
893 {
894         int goodness = 0;
895
896         if (var->yres == mode->yres)
897                 goodness += 10;
898         if (var->xres == mode->xres)
899                 goodness += 9;
900         return goodness;
901 }
902
903 /*
904  * This function is called by check_var, it gets the passed in mode parameter, and
905  * outputs a valid mode matching the passed-in one as closely as possible.
906  * We need something better ultimately. Things like fbcon basically pass us out
907  * current mode with xres/yres hacked, while things like XFree will actually
908  * produce a full timing that we should respect as much as possible.
909  *
910  * This is why I added the FB_ACTIVATE_FIND that is used by fbcon. Without this,
911  * we do a simple spec match, that's all. With it, we actually look for a mode in
912  * either our monitor modedb or the vesa one if none
913  *
914  */
915 int  radeon_match_mode(struct radeonfb_info *rinfo,
916                        struct fb_var_screeninfo *dest,
917                        const struct fb_var_screeninfo *src)
918 {
919         const struct fb_videomode       *db = vesa_modes;
920         int                             i, dbsize = 34;
921         int                             has_rmx, native_db = 0;
922         int                             goodness = 0;
923         const struct fb_videomode       *candidate = NULL;
924
925         /* Start with a copy of the requested mode */
926         memcpy(dest, src, sizeof(struct fb_var_screeninfo));
927
928         /* Check if we have a modedb built from EDID */
929         if (rinfo->mon1_modedb) {
930                 db = rinfo->mon1_modedb;
931                 dbsize = rinfo->mon1_dbsize;
932                 native_db = 1;
933         }
934
935         /* Check if we have a scaler allowing any fancy mode */
936         has_rmx = rinfo->mon1_type == MT_LCD || rinfo->mon1_type == MT_DFP;
937
938         /* If we have a scaler and are passed FB_ACTIVATE_TEST or
939          * FB_ACTIVATE_NOW, just do basic checking and return if the
940          * mode match
941          */
942         if ((src->activate & FB_ACTIVATE_MASK) == FB_ACTIVATE_TEST ||
943             (src->activate & FB_ACTIVATE_MASK) == FB_ACTIVATE_NOW) {
944                 /* We don't have an RMX, validate timings. If we don't have
945                  * monspecs, we should be paranoid and not let use go above
946                  * 640x480-60, but I assume userland knows what it's doing here
947                  * (though I may be proven wrong...)
948                  */
949                 if (has_rmx == 0 && rinfo->mon1_modedb)
950                         if (fb_validate_mode((struct fb_var_screeninfo *)src, rinfo->info))
951                                 return -EINVAL;
952                 return 0;
953         }
954
955         /* Now look for a mode in the database */
956         while (db) {
957                 for (i = 0; i < dbsize; i++) {
958                         int g;
959
960                         if (db[i].yres < src->yres)
961                                 continue;       
962                         if (db[i].xres < src->xres)
963                                 continue;
964                         g = radeon_compare_modes(src, &db[i]);
965                         /* If the new mode is at least as good as the previous one,
966                          * then it's our new candidate
967                          */
968                         if (g >= goodness) {
969                                 candidate = &db[i];
970                                 goodness = g;
971                         }
972                 }
973                 db = NULL;
974                 /* If we have a scaler, we allow any mode from the database */
975                 if (native_db && has_rmx) {
976                         db = vesa_modes;
977                         dbsize = 34;
978                         native_db = 0;
979                 }
980         }
981
982         /* If we have found a match, return it */
983         if (candidate != NULL) {
984                 radeon_videomode_to_var(dest, candidate);
985                 return 0;
986         }
987
988         /* If we haven't and don't have a scaler, fail */
989         if (!has_rmx)
990                 return -EINVAL;
991
992         return 0;
993 }