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