]> bbs.cooldavid.org Git - net-next-2.6.git/blobdiff - drivers/video/sh_mobile_lcdcfb.c
Merge master.kernel.org:/pub/scm/linux/kernel/git/lethal/genesis-2.6 into devel-stable
[net-next-2.6.git] / drivers / video / sh_mobile_lcdcfb.c
index 946a810801afdba68b1eb4de68e17234b5f1754e..50963739a40977832e03285b1049be65f4cde1dd 100644 (file)
@@ -20,6 +20,7 @@
 #include <linux/vmalloc.h>
 #include <linux/ioctl.h>
 #include <linux/slab.h>
+#include <linux/console.h>
 #include <video/sh_mobile_lcdc.h>
 #include <asm/atomic.h>
 
@@ -53,6 +54,9 @@ static int lcdc_shared_regs[] = {
 };
 #define NR_SHARED_REGS ARRAY_SIZE(lcdc_shared_regs)
 
+#define DEFAULT_XRES 1280
+#define DEFAULT_YRES 1024
+
 static unsigned long lcdc_offs_mainlcd[NR_CH_REGS] = {
        [LDDCKPAT1R] = 0x400,
        [LDDCKPAT2R] = 0x404,
@@ -106,6 +110,23 @@ static unsigned long lcdc_offs_sublcd[NR_CH_REGS] = {
 #define LDRCNTR_MRC    0x00000001
 #define LDSR_MRS       0x00000100
 
+static const struct fb_videomode default_720p = {
+       .name = "HDMI 720p",
+       .xres = 1280,
+       .yres = 720,
+
+       .left_margin = 200,
+       .right_margin = 88,
+       .hsync_len = 48,
+
+       .upper_margin = 20,
+       .lower_margin = 5,
+       .vsync_len = 5,
+
+       .pixclock = 13468,
+       .sync = FB_SYNC_VERT_HIGH_ACT | FB_SYNC_HOR_HIGH_ACT,
+};
+
 struct sh_mobile_lcdc_priv {
        void __iomem *base;
        int irq;
@@ -479,6 +500,7 @@ static int sh_mobile_lcdc_start(struct sh_mobile_lcdc_priv *priv)
                        m = 1 << 6;
                tmp |= m << (lcdc_chan_is_sublcd(ch) ? 8 : 0);
 
+               /* FIXME: sh7724 can only use 42, 48, 54 and 60 for the divider denominator */
                lcdc_write_chan(ch, LDDCKPAT1R, 0);
                lcdc_write_chan(ch, LDDCKPAT2R, (1 << (m/2)) - 1);
        }
@@ -815,6 +837,103 @@ static int sh_mobile_ioctl(struct fb_info *info, unsigned int cmd,
        return retval;
 }
 
+static void sh_mobile_fb_reconfig(struct fb_info *info)
+{
+       struct sh_mobile_lcdc_chan *ch = info->par;
+       struct fb_videomode mode1, mode2;
+       struct fb_event event;
+       int evnt = FB_EVENT_MODE_CHANGE_ALL;
+
+       if (ch->use_count > 1 || (ch->use_count == 1 && !info->fbcon_par))
+               /* More framebuffer users are active */
+               return;
+
+       fb_var_to_videomode(&mode1, &ch->display_var);
+       fb_var_to_videomode(&mode2, &info->var);
+
+       if (fb_mode_is_equal(&mode1, &mode2))
+               return;
+
+       /* Display has been re-plugged, framebuffer is free now, reconfigure */
+       if (fb_set_var(info, &ch->display_var) < 0)
+               /* Couldn't reconfigure, hopefully, can continue as before */
+               return;
+
+       info->fix.line_length = mode2.xres * (ch->cfg.bpp / 8);
+
+       /*
+        * fb_set_var() calls the notifier change internally, only if
+        * FBINFO_MISC_USEREVENT flag is set. Since we do not want to fake a
+        * user event, we have to call the chain ourselves.
+        */
+       event.info = info;
+       event.data = &mode2;
+       fb_notifier_call_chain(evnt, &event);
+}
+
+/*
+ * Locking: both .fb_release() and .fb_open() are called with info->lock held if
+ * user == 1, or with console sem held, if user == 0.
+ */
+static int sh_mobile_release(struct fb_info *info, int user)
+{
+       struct sh_mobile_lcdc_chan *ch = info->par;
+
+       mutex_lock(&ch->open_lock);
+       dev_dbg(info->dev, "%s(): %d users\n", __func__, ch->use_count);
+
+       ch->use_count--;
+
+       /* Nothing to reconfigure, when called from fbcon */
+       if (user) {
+               acquire_console_sem();
+               sh_mobile_fb_reconfig(info);
+               release_console_sem();
+       }
+
+       mutex_unlock(&ch->open_lock);
+
+       return 0;
+}
+
+static int sh_mobile_open(struct fb_info *info, int user)
+{
+       struct sh_mobile_lcdc_chan *ch = info->par;
+
+       mutex_lock(&ch->open_lock);
+       ch->use_count++;
+
+       dev_dbg(info->dev, "%s(): %d users\n", __func__, ch->use_count);
+       mutex_unlock(&ch->open_lock);
+
+       return 0;
+}
+
+static int sh_mobile_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
+{
+       struct sh_mobile_lcdc_chan *ch = info->par;
+
+       if (var->xres < 160 || var->xres > 1920 ||
+           var->yres < 120 || var->yres > 1080 ||
+           var->left_margin < 32 || var->left_margin > 320 ||
+           var->right_margin < 12 || var->right_margin > 240 ||
+           var->upper_margin < 12 || var->upper_margin > 120 ||
+           var->lower_margin < 1 || var->lower_margin > 64 ||
+           var->hsync_len < 32 || var->hsync_len > 240 ||
+           var->vsync_len < 2 || var->vsync_len > 64 ||
+           var->pixclock < 6000 || var->pixclock > 40000 ||
+           var->xres * var->yres * (ch->cfg.bpp / 8) * 2 > info->fix.smem_len) {
+               dev_warn(info->dev, "Invalid info: %u %u %u %u %u %u %u %u %u!\n",
+                        var->xres, var->yres,
+                        var->left_margin, var->right_margin,
+                        var->upper_margin, var->lower_margin,
+                        var->hsync_len, var->vsync_len,
+                        var->pixclock);
+               return -EINVAL;
+       }
+       return 0;
+}
+
 static struct fb_ops sh_mobile_lcdc_ops = {
        .owner          = THIS_MODULE,
        .fb_setcolreg   = sh_mobile_lcdc_setcolreg,
@@ -825,6 +944,9 @@ static struct fb_ops sh_mobile_lcdc_ops = {
        .fb_imageblit   = sh_mobile_lcdc_imageblit,
        .fb_pan_display = sh_mobile_fb_pan_display,
        .fb_ioctl       = sh_mobile_ioctl,
+       .fb_open        = sh_mobile_open,
+       .fb_release     = sh_mobile_release,
+       .fb_check_var   = sh_mobile_check_var,
 };
 
 static int sh_mobile_lcdc_set_bpp(struct fb_var_screeninfo *var, int bpp)
@@ -943,7 +1065,7 @@ static int sh_mobile_lcdc_notify(struct notifier_block *nb,
        struct fb_info *info = event->info;
        struct sh_mobile_lcdc_chan *ch = info->par;
        struct sh_mobile_lcdc_board_cfg *board_cfg = &ch->cfg.board_cfg;
-       struct fb_var_screeninfo *var;
+       int ret;
 
        if (&ch->lcdc->notifier != nb)
                return NOTIFY_DONE;
@@ -958,41 +1080,22 @@ static int sh_mobile_lcdc_notify(struct notifier_block *nb,
                        module_put(board_cfg->owner);
                }
                pm_runtime_put(info->device);
+               sh_mobile_lcdc_stop(ch->lcdc);
                break;
        case FB_EVENT_RESUME:
-               var = &info->var;
+               mutex_lock(&ch->open_lock);
+               sh_mobile_fb_reconfig(info);
+               mutex_unlock(&ch->open_lock);
 
                /* HDMI must be enabled before LCDC configuration */
                if (try_module_get(board_cfg->owner) && board_cfg->display_on) {
-                       board_cfg->display_on(board_cfg->board_data, ch->info);
+                       board_cfg->display_on(board_cfg->board_data, info);
                        module_put(board_cfg->owner);
                }
 
-               /* Check if the new display is not in our modelist */
-               if (ch->info->modelist.next &&
-                   !fb_match_mode(var, &ch->info->modelist)) {
-                       struct fb_videomode mode;
-                       int ret;
-
-                       /* Can we handle this display? */
-                       if (var->xres > ch->cfg.lcd_cfg[0].xres ||
-                           var->yres > ch->cfg.lcd_cfg[0].yres)
-                               /*
-                                * LCDC resume failed, no need to continue with
-                                * the notifier chain
-                                */
-                               return notifier_from_errno(-ENOMEM);
-
-                       /* Add to the modelist */
-                       fb_var_to_videomode(&mode, var);
-                       ret = fb_add_videomode(&mode, &ch->info->modelist);
-                       if (ret < 0)
-                               return notifier_from_errno(ret);
-               }
-
-               pm_runtime_get_sync(info->device);
-
-               sh_mobile_lcdc_geometry(ch);
+               ret = sh_mobile_lcdc_start(ch->lcdc);
+               if (!ret)
+                       pm_runtime_get_sync(info->device);
        }
 
        return NOTIFY_OK;
@@ -1005,7 +1108,6 @@ static int __devinit sh_mobile_lcdc_probe(struct platform_device *pdev)
        struct fb_info *info;
        struct sh_mobile_lcdc_priv *priv;
        struct sh_mobile_lcdc_info *pdata = pdev->dev.platform_data;
-       struct sh_mobile_lcdc_chan_cfg *cfg;
        struct resource *res;
        int error;
        void *buf;
@@ -1091,11 +1193,11 @@ static int __devinit sh_mobile_lcdc_probe(struct platform_device *pdev)
                struct fb_var_screeninfo *var;
                const struct fb_videomode *lcd_cfg, *max_cfg = NULL;
                struct sh_mobile_lcdc_chan *ch = priv->ch + i;
+               struct sh_mobile_lcdc_chan_cfg *cfg = &ch->cfg;
+               const struct fb_videomode *mode = cfg->lcd_cfg;
                unsigned long max_size = 0;
                int k;
 
-               cfg = &ch->cfg;
-
                ch->info = framebuffer_alloc(0, &pdev->dev);
                if (!ch->info) {
                        dev_err(&pdev->dev, "unable to allocate fb_info\n");
@@ -1106,16 +1208,12 @@ static int __devinit sh_mobile_lcdc_probe(struct platform_device *pdev)
                info = ch->info;
                var = &info->var;
                info->fbops = &sh_mobile_lcdc_ops;
-               fb_videomode_to_var(var, &cfg->lcd_cfg[0]);
-               /* Default Y virtual resolution is 2x panel size */
-               var->yres_virtual = var->yres * 2;
+               info->par = ch;
 
-               error = sh_mobile_lcdc_set_bpp(var, cfg->bpp);
-               if (error)
-                       break;
+               mutex_init(&ch->open_lock);
 
-               for (k = 0, lcd_cfg = cfg->lcd_cfg;
-                    k < cfg->num_cfg;
+               for (k = 0, lcd_cfg = mode;
+                    k < cfg->num_cfg && lcd_cfg;
                     k++, lcd_cfg++) {
                        unsigned long size = lcd_cfg->yres * lcd_cfg->xres;
 
@@ -1125,13 +1223,27 @@ static int __devinit sh_mobile_lcdc_probe(struct platform_device *pdev)
                        }
                }
 
-               dev_dbg(&pdev->dev, "Found largest videomode %ux%u\n",
-                       max_cfg->xres, max_cfg->yres);
+               if (!mode)
+                       max_size = DEFAULT_XRES * DEFAULT_YRES;
+               else if (max_cfg)
+                       dev_dbg(&pdev->dev, "Found largest videomode %ux%u\n",
+                               max_cfg->xres, max_cfg->yres);
 
                info->fix = sh_mobile_lcdc_fix;
-               info->fix.line_length = cfg->lcd_cfg[0].xres * (cfg->bpp / 8);
                info->fix.smem_len = max_size * (cfg->bpp / 8) * 2;
 
+               if (!mode)
+                       mode = &default_720p;
+
+               fb_videomode_to_var(var, mode);
+               /* Default Y virtual resolution is 2x panel size */
+               var->yres_virtual = var->yres * 2;
+               var->activate = FB_ACTIVATE_NOW;
+
+               error = sh_mobile_lcdc_set_bpp(var, cfg->bpp);
+               if (error)
+                       break;
+
                buf = dma_alloc_coherent(&pdev->dev, info->fix.smem_len,
                                         &ch->dma_handle, GFP_KERNEL);
                if (!buf) {
@@ -1152,9 +1264,9 @@ static int __devinit sh_mobile_lcdc_probe(struct platform_device *pdev)
                }
 
                info->fix.smem_start = ch->dma_handle;
+               info->fix.line_length = var->xres * (cfg->bpp / 8);
                info->screen_base = buf;
                info->device = &pdev->dev;
-               info->par = ch;
                ch->display_var = *var;
        }
 
@@ -1169,6 +1281,10 @@ static int __devinit sh_mobile_lcdc_probe(struct platform_device *pdev)
 
        for (i = 0; i < j; i++) {
                struct sh_mobile_lcdc_chan *ch = priv->ch + i;
+               const struct fb_videomode *mode = ch->cfg.lcd_cfg;
+
+               if (!mode)
+                       mode = &default_720p;
 
                info = ch->info;
 
@@ -1181,6 +1297,7 @@ static int __devinit sh_mobile_lcdc_probe(struct platform_device *pdev)
                        }
                }
 
+               fb_videomode_to_modelist(mode, ch->cfg.num_cfg, &info->modelist);
                error = register_framebuffer(info);
                if (error < 0)
                        goto err1;
@@ -1190,8 +1307,7 @@ static int __devinit sh_mobile_lcdc_probe(struct platform_device *pdev)
                         pdev->name,
                         (ch->cfg.chan == LCDC_CHAN_MAINLCD) ?
                         "mainlcd" : "sublcd",
-                        (int) ch->cfg.lcd_cfg[0].xres,
-                        (int) ch->cfg.lcd_cfg[0].yres,
+                        info->var.xres, info->var.yres,
                         ch->cfg.bpp);
 
                /* deferred io mode: disable clock to save power */
@@ -1233,8 +1349,10 @@ static int sh_mobile_lcdc_remove(struct platform_device *pdev)
                if (priv->ch[i].sglist)
                        vfree(priv->ch[i].sglist);
 
-               dma_free_coherent(&pdev->dev, info->fix.smem_len,
-                                 info->screen_base, priv->ch[i].dma_handle);
+               if (info->screen_base)
+                       dma_free_coherent(&pdev->dev, info->fix.smem_len,
+                                         info->screen_base,
+                                         priv->ch[i].dma_handle);
                fb_dealloc_cmap(&info->cmap);
                framebuffer_release(info);
        }