fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / drivers / video / modedb.c
index 929bbba..5df41f6 100644 (file)
@@ -12,7 +12,6 @@
  */
 
 #include <linux/module.h>
-#include <linux/tty.h>
 #include <linux/fb.h>
 #include <linux/sched.h>
 
@@ -35,8 +34,6 @@ const char *global_mode_option;
      *  Standard video mode definitions (taken from XFree86)
      */
 
-#define DEFAULT_MODEDB_INDEX   0
-
 static const struct fb_videomode modedb[] = {
     {
        /* 640x400 @ 70 Hz, 31.5 kHz hsync */
@@ -182,6 +179,10 @@ static const struct fb_videomode modedb[] = {
        /* 1600x1200 @ 75 Hz, 93.75 kHz hsync */
        NULL, 75, 1600, 1200, 4938, 304, 64, 46, 1, 192, 3,
        FB_SYNC_HOR_HIGH_ACT|FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
+    }, {
+       /* 1680x1050 @ 60 Hz, 65.191 kHz hsync */
+       NULL, 60, 1680, 1050, 6848, 280, 104, 30, 3, 176, 6,
+       FB_SYNC_HOR_HIGH_ACT|FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
     }, {
        /* 1600x1200 @ 85 Hz, 105.77 kHz hsync */
        NULL, 85, 1600, 1200, 4545, 272, 16, 37, 4, 192, 3,
@@ -246,6 +247,19 @@ static const struct fb_videomode modedb[] = {
        /* 480x300 @ 72 Hz, 48.0 kHz hsync */
        NULL, 72, 480, 300, 33386, 40, 24, 11, 19, 80, 3,
        0, FB_VMODE_DOUBLE
+    }, {
+       /* 1920x1200 @ 60 Hz, 74.5 Khz hsync */
+       NULL, 60, 1920, 1200, 5177, 128, 336, 1, 38, 208, 3,
+       FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
+       FB_VMODE_NONINTERLACED
+    }, {
+       /* 1152x768, 60 Hz, PowerBook G4 Titanium I and II */
+       NULL, 60, 1152, 768, 15386, 158, 26, 29, 3, 136, 6,
+       FB_SYNC_HOR_HIGH_ACT|FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
+    }, {
+       /* 1366x768, 60 Hz, 47.403 kHz hsync, WXGA 16:9 aspect ratio */
+       NULL, 60, 1366, 768, 13806, 120, 10, 14, 3, 32, 5,
+       0, FB_VMODE_NONINTERLACED
     },
 };
 
@@ -404,8 +418,8 @@ static int my_atoi(const char *name)
  *
  */
 
-int fb_try_mode(struct fb_var_screeninfo *var, struct fb_info *info,
-               const struct fb_videomode *mode, unsigned int bpp)
+static int fb_try_mode(struct fb_var_screeninfo *var, struct fb_info *info,
+                      const struct fb_videomode *mode, unsigned int bpp)
 {
     int err = 0;
 
@@ -451,12 +465,22 @@ int fb_try_mode(struct fb_var_screeninfo *var, struct fb_info *info,
  *
  *     Valid mode specifiers for @mode_option:
  *
- *     <xres>x<yres>[-<bpp>][@<refresh>] or
+ *     <xres>x<yres>[M][R][-<bpp>][@<refresh>][i][m] or
  *     <name>[-<bpp>][@<refresh>]
  *
  *     with <xres>, <yres>, <bpp> and <refresh> decimal numbers and
  *     <name> a string.
  *
+ *      If 'M' is present after yres (and before refresh/bpp if present),
+ *      the function will compute the timings using VESA(tm) Coordinated
+ *      Video Timings (CVT).  If 'R' is present after 'M', will compute with
+ *      reduced blanking (for flatpanels).  If 'i' is present, compute
+ *      interlaced mode.  If 'm' is present, add margins equal to 1.8%
+ *      of xres rounded down to 8 pixels, and 1.8% of yres. The char
+ *      'i' and 'm' must be after 'M' and 'R'. Example:
+ *
+ *      1024x768MR-8@60m - Reduced blank with margins at 60Hz.
+ *
  *     NOTE: The passed struct @var is _not_ cleared!  This allows you
  *     to supply values for e.g. the grayscale and accel_flags fields.
  *
@@ -477,10 +501,12 @@ int fb_find_mode(struct fb_var_screeninfo *var,
     /* Set up defaults */
     if (!db) {
        db = modedb;
-       dbsize = sizeof(modedb)/sizeof(*modedb);
+       dbsize = ARRAY_SIZE(modedb);
     }
+
     if (!default_mode)
-       default_mode = &modedb[DEFAULT_MODEDB_INDEX];
+       default_mode = &db[0];
+
     if (!default_bpp)
        default_bpp = 8;
 
@@ -490,7 +516,7 @@ int fb_find_mode(struct fb_var_screeninfo *var,
        unsigned int namelen = strlen(name);
        int res_specified = 0, bpp_specified = 0, refresh_specified = 0;
        unsigned int xres = 0, yres = 0, bpp = default_bpp, refresh = 0;
-       int yres_specified = 0;
+       int yres_specified = 0, cvt = 0, rb = 0, interlace = 0, margins = 0;
        u32 best, diff;
 
        for (i = namelen-1; i >= 0; i--) {
@@ -501,6 +527,8 @@ int fb_find_mode(struct fb_var_screeninfo *var,
                        !yres_specified) {
                        refresh = my_atoi(&name[i+1]);
                        refresh_specified = 1;
+                       if (cvt || rb)
+                           cvt = 0;
                    } else
                        goto done;
                    break;
@@ -509,6 +537,8 @@ int fb_find_mode(struct fb_var_screeninfo *var,
                    if (!bpp_specified && !yres_specified) {
                        bpp = my_atoi(&name[i+1]);
                        bpp_specified = 1;
+                       if (cvt || rb)
+                           cvt = 0;
                    } else
                        goto done;
                    break;
@@ -521,6 +551,22 @@ int fb_find_mode(struct fb_var_screeninfo *var,
                    break;
                case '0'...'9':
                    break;
+               case 'M':
+                   if (!yres_specified)
+                       cvt = 1;
+                   break;
+               case 'R':
+                   if (!cvt)
+                       rb = 1;
+                   break;
+               case 'm':
+                   if (!cvt)
+                       margins = 1;
+                   break;
+               case 'i':
+                   if (!cvt)
+                       interlace = 1;
+                   break;
                default:
                    goto done;
            }
@@ -530,6 +576,34 @@ int fb_find_mode(struct fb_var_screeninfo *var,
            res_specified = 1;
        }
 done:
+       if (cvt) {
+           struct fb_videomode cvt_mode;
+           int ret;
+
+           DPRINTK("CVT mode %dx%d@%dHz%s%s%s\n", xres, yres,
+                   (refresh) ? refresh : 60, (rb) ? " reduced blanking" :
+                   "", (margins) ? " with margins" : "", (interlace) ?
+                   " interlaced" : "");
+
+           cvt_mode.xres = xres;
+           cvt_mode.yres = yres;
+           cvt_mode.refresh = (refresh) ? refresh : 60;
+
+           if (interlace)
+               cvt_mode.vmode |= FB_VMODE_INTERLACED;
+           else
+               cvt_mode.vmode &= ~FB_VMODE_INTERLACED;
+
+           ret = fb_find_mode_cvt(&cvt_mode, margins, rb);
+
+           if (!ret && !fb_try_mode(var, info, &cvt_mode, bpp)) {
+               DPRINTK("modedb CVT: CVT mode ok\n");
+               return 1;
+           }
+
+           DPRINTK("CVT mode invalid, getting mode from database\n");
+       }
+
        DPRINTK("Trying specified video mode%s %ix%i\n",
            refresh_specified ? "" : " (ignoring refresh rate)", xres, yres);
 
@@ -613,6 +687,8 @@ void fb_var_to_videomode(struct fb_videomode *mode,
        mode->sync = var->sync;
        mode->vmode = var->vmode & FB_VMODE_MASK;
        mode->flag = FB_MODE_IS_FROM_VAR;
+       mode->refresh = 0;
+
        if (!var->pixclock)
                return;
 
@@ -714,10 +790,52 @@ struct fb_videomode *fb_find_best_mode(struct fb_var_screeninfo *var,
                        if (diff > d) {
                                diff = d;
                                best = mode;
-                       } else if (diff == d && mode->refresh > best->refresh)
-                           best = mode;
+                       } else if (diff == d && best &&
+                                  mode->refresh > best->refresh)
+                               best = mode;
+               }
+       }
+       return best;
+}
+
+/**
+ * fb_find_nearest_mode - find closest videomode
+ *
+ * @mode: pointer to struct fb_videomode
+ * @head: pointer to modelist
+ *
+ * Finds best matching videomode, smaller or greater in dimension.
+ * If more than 1 videomode is found, will return the videomode with
+ * the closest refresh rate.
+ */
+struct fb_videomode *fb_find_nearest_mode(struct fb_videomode *mode,
+                                         struct list_head *head)
+{
+       struct list_head *pos;
+       struct fb_modelist *modelist;
+       struct fb_videomode *cmode, *best = NULL;
+       u32 diff = -1, diff_refresh = -1;
+
+       list_for_each(pos, head) {
+               u32 d;
+
+               modelist = list_entry(pos, struct fb_modelist, list);
+               cmode = &modelist->mode;
+
+               d = abs(cmode->xres - mode->xres) +
+                       abs(cmode->yres - mode->yres);
+               if (diff > d) {
+                       diff = d;
+                       best = cmode;
+               } else if (diff == d) {
+                       d = abs(cmode->refresh - mode->refresh);
+                       if (diff_refresh > d) {
+                               diff_refresh = d;
+                               best = cmode;
+                       }
                }
        }
+
        return best;
 }
 
@@ -838,13 +956,72 @@ void fb_videomode_to_modelist(struct fb_videomode *modedb, int num,
        }
 }
 
+struct fb_videomode *fb_find_best_display(struct fb_monspecs *specs,
+                                         struct list_head *head)
+{
+       struct list_head *pos;
+       struct fb_modelist *modelist;
+       struct fb_videomode *m, *m1 = NULL, *md = NULL, *best = NULL;
+       int first = 0;
+
+       if (!head->prev || !head->next || list_empty(head))
+               goto finished;
+
+       /* get the first detailed mode and the very first mode */
+       list_for_each(pos, head) {
+               modelist = list_entry(pos, struct fb_modelist, list);
+               m = &modelist->mode;
+
+               if (!first) {
+                       m1 = m;
+                       first = 1;
+               }
+
+               if (m->flag & FB_MODE_IS_FIRST) {
+                       md = m;
+                       break;
+               }
+       }
+
+       /* first detailed timing is preferred */
+       if (specs->misc & FB_MISC_1ST_DETAIL) {
+               best = md;
+               goto finished;
+       }
+
+       /* find best mode based on display width and height */
+       if (specs->max_x && specs->max_y) {
+               struct fb_var_screeninfo var;
+
+               memset(&var, 0, sizeof(struct fb_var_screeninfo));
+               var.xres = (specs->max_x * 7200)/254;
+               var.yres = (specs->max_y * 7200)/254;
+               m = fb_find_best_mode(&var, head);
+               if (m) {
+                       best = m;
+                       goto finished;
+               }
+       }
+
+       /* use first detailed mode */
+       if (md) {
+               best = md;
+               goto finished;
+       }
+
+       /* last resort, use the very first mode */
+       best = m1;
+finished:
+       return best;
+}
+EXPORT_SYMBOL(fb_find_best_display);
+
 EXPORT_SYMBOL(fb_videomode_to_var);
 EXPORT_SYMBOL(fb_var_to_videomode);
 EXPORT_SYMBOL(fb_mode_is_equal);
 EXPORT_SYMBOL(fb_add_videomode);
-EXPORT_SYMBOL(fb_delete_videomode);
-EXPORT_SYMBOL(fb_destroy_modelist);
 EXPORT_SYMBOL(fb_match_mode);
 EXPORT_SYMBOL(fb_find_best_mode);
+EXPORT_SYMBOL(fb_find_nearest_mode);
 EXPORT_SYMBOL(fb_videomode_to_modelist);
 EXPORT_SYMBOL(fb_find_mode);