OMAPFB: use dma_alloc_attrs to allocate memory
[pandora-kernel.git] / drivers / video / omap2 / omapfb / omapfb-main.c
index 602b71a..fadb1be 100644 (file)
@@ -48,6 +48,7 @@ static int def_rotate;
 static int def_mirror;
 static bool auto_update;
 static unsigned int auto_update_freq;
+static bool def_vram_cache = true;
 module_param(auto_update, bool, 0);
 module_param(auto_update_freq, uint, 0644);
 
@@ -808,19 +809,15 @@ static unsigned calc_rotation_offset_vrfb(const struct fb_var_screeninfo *var,
 static void omapfb_calc_addr(const struct omapfb_info *ofbi,
                             const struct fb_var_screeninfo *var,
                             const struct fb_fix_screeninfo *fix,
-                            int rotation, u32 *paddr, void __iomem **vaddr)
+                            int rotation, u32 *paddr)
 {
        u32 data_start_p;
-       void __iomem *data_start_v;
        int offset;
 
-       if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB) {
+       if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB)
                data_start_p = omapfb_get_region_rot_paddr(ofbi, rotation);
-               data_start_v = NULL;
-       } else {
+       else
                data_start_p = omapfb_get_region_paddr(ofbi);
-               data_start_v = omapfb_get_region_vaddr(ofbi);
-       }
 
        if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB)
                offset = calc_rotation_offset_vrfb(var, fix, rotation);
@@ -828,16 +825,14 @@ static void omapfb_calc_addr(const struct omapfb_info *ofbi,
                offset = calc_rotation_offset_dma(var, fix, rotation);
 
        data_start_p += offset;
-       data_start_v += offset;
 
        if (offset)
                DBG("offset %d, %d = %d\n",
                    var->xoffset, var->yoffset, offset);
 
-       DBG("paddr %x, vaddr %p\n", data_start_p, data_start_v);
+       DBG("paddr %x\n", data_start_p);
 
        *paddr = data_start_p;
-       *vaddr = data_start_v;
 }
 
 /* setup overlay according to the fb */
@@ -850,7 +845,6 @@ int omapfb_setup_overlay(struct fb_info *fbi, struct omap_overlay *ovl,
        struct fb_fix_screeninfo *fix = &fbi->fix;
        enum omap_color_mode mode = 0;
        u32 data_start_p = 0;
-       void __iomem *data_start_v = NULL;
        struct omap_overlay_info info;
        int xres, yres;
        int screen_width;
@@ -880,8 +874,7 @@ int omapfb_setup_overlay(struct fb_info *fbi, struct omap_overlay *ovl,
        }
 
        if (ofbi->region->size)
-               omapfb_calc_addr(ofbi, var, fix, rotation,
-                                &data_start_p, &data_start_v);
+               omapfb_calc_addr(ofbi, var, fix, rotation, &data_start_p);
 
        r = fb_mode_to_dss_mode(var, &mode);
        if (r) {
@@ -910,7 +903,6 @@ int omapfb_setup_overlay(struct fb_info *fbi, struct omap_overlay *ovl,
                mirror = ofbi->mirror;
 
        info.paddr = data_start_p;
-       info.vaddr = data_start_v;
        info.screen_width = screen_width;
        info.width = xres;
        info.height = yres;
@@ -1128,7 +1120,10 @@ static int omapfb_mmap(struct fb_info *fbi, struct vm_area_struct *vma)
 
        vma->vm_pgoff = off >> PAGE_SHIFT;
        vma->vm_flags |= VM_IO | VM_RESERVED;
-       vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
+       if (def_vram_cache)
+               vma->vm_page_prot = pgprot_writethrough(vma->vm_page_prot);
+       else
+               vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
        vma->vm_ops = &mmap_user_ops;
        vma->vm_private_data = rg;
        if (io_remap_pfn_range(vma, vma->vm_start, off >> PAGE_SHIFT,
@@ -1192,7 +1187,7 @@ static int _setcolreg(struct fb_info *fbi, u_int regno, u_int red, u_int green,
                        break;
 
                if (regno < 16) {
-                       u16 pal;
+                       u32 pal;
                        pal = ((red >> (16 - var->red.length)) <<
                                        var->red.offset) |
                                ((green >> (16 - var->green.length)) <<
@@ -1335,24 +1330,25 @@ static void omapfb_free_fbmem(struct fb_info *fbi)
 
        rg = ofbi->region;
 
-       WARN_ON(atomic_read(&rg->map_count));
-
-       if (rg->paddr)
-               if (omap_vram_free(rg->paddr, rg->size))
-                       dev_err(fbdev->dev, "VRAM FREE failed\n");
+       if (rg->token == NULL)
+               return;
 
-       if (rg->vaddr)
-               iounmap(rg->vaddr);
+       WARN_ON(atomic_read(&rg->map_count));
 
        if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB) {
                /* unmap the 0 angle rotation */
                if (rg->vrfb.vaddr[0]) {
                        iounmap(rg->vrfb.vaddr[0]);
-                       omap_vrfb_release_ctx(&rg->vrfb);
                        rg->vrfb.vaddr[0] = NULL;
                }
+
+               omap_vrfb_release_ctx(&rg->vrfb);
        }
 
+       dma_free_attrs(fbdev->dev, rg->size, rg->token, rg->dma_handle,
+                       &rg->attrs);
+
+       rg->token = NULL;
        rg->vaddr = NULL;
        rg->paddr = 0;
        rg->alloc = 0;
@@ -1387,7 +1383,9 @@ static int omapfb_alloc_fbmem(struct fb_info *fbi, unsigned long size,
        struct omapfb_info *ofbi = FB2OFB(fbi);
        struct omapfb2_device *fbdev = ofbi->fbdev;
        struct omapfb2_mem_region *rg;
-       void __iomem *vaddr;
+       void *token;
+       DEFINE_DMA_ATTRS(attrs);
+       dma_addr_t dma_handle;
        int r;
 
        rg = ofbi->region;
@@ -1402,42 +1400,43 @@ static int omapfb_alloc_fbmem(struct fb_info *fbi, unsigned long size,
 
        size = PAGE_ALIGN(size);
 
-       if (!paddr) {
-               DBG("allocating %lu bytes for fb %d\n", size, ofbi->id);
-               r = omap_vram_alloc(OMAP_VRAM_MEMTYPE_SDRAM, size, &paddr);
-       } else {
-               DBG("reserving %lu bytes at %lx for fb %d\n", size, paddr,
-                               ofbi->id);
-               r = omap_vram_reserve(paddr, size);
-       }
+       WARN_ONCE(paddr,
+               "reserving memory at predefined address not supported\n");
 
-       if (r) {
+       dma_set_attr(DMA_ATTR_WRITE_COMBINE, &attrs);
+
+       if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB)
+               dma_set_attr(DMA_ATTR_NO_KERNEL_MAPPING, &attrs);
+
+       DBG("allocating %lu bytes for fb %d\n", size, ofbi->id);
+
+       token = dma_alloc_attrs(fbdev->dev, size, &dma_handle,
+                       GFP_KERNEL, &attrs);
+
+       if (token == NULL) {
                dev_err(fbdev->dev, "failed to allocate framebuffer\n");
                return -ENOMEM;
        }
 
-       if (ofbi->rotation_type != OMAP_DSS_ROT_VRFB) {
-               vaddr = ioremap_wc(paddr, size);
-
-               if (!vaddr) {
-                       dev_err(fbdev->dev, "failed to ioremap framebuffer\n");
-                       omap_vram_free(paddr, size);
-                       return -ENOMEM;
-               }
+       DBG("allocated VRAM paddr %lx, vaddr %p\n",
+                       (unsigned long)dma_handle, token);
 
-               DBG("allocated VRAM paddr %lx, vaddr %p\n", paddr, vaddr);
-       } else {
+       if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB) {
                r = omap_vrfb_request_ctx(&rg->vrfb);
                if (r) {
+                       dma_free_attrs(fbdev->dev, size, token, dma_handle,
+                                       &attrs);
                        dev_err(fbdev->dev, "vrfb create ctx failed\n");
                        return r;
                }
-
-               vaddr = NULL;
        }
 
-       rg->paddr = paddr;
-       rg->vaddr = vaddr;
+       rg->attrs = attrs;
+       rg->token = token;
+       rg->dma_handle = dma_handle;
+
+       rg->paddr = (unsigned long)dma_handle;
+       rg->vaddr = (void __iomem *)token;
        rg->size = size;
        rg->alloc = 1;
 
@@ -2276,6 +2275,87 @@ static int omapfb_parse_def_modes(struct omapfb2_device *fbdev)
        return r;
 }
 
+static void fb_videomode_to_omap_timings(struct fb_videomode *m,
+               struct omap_video_timings *t)
+{
+       t->x_res = m->xres;
+       t->y_res = m->yres;
+       t->pixel_clock = PICOS2KHZ(m->pixclock);
+       t->hsw = m->hsync_len;
+       t->hfp = m->right_margin;
+       t->hbp = m->left_margin;
+       t->vsw = m->vsync_len;
+       t->vfp = m->lower_margin;
+       t->vbp = m->upper_margin;
+}
+
+static int omapfb_find_best_mode(struct omap_dss_device *display,
+               struct omap_video_timings *timings)
+{
+       struct fb_monspecs *specs;
+       u8 *edid;
+       int r, i, best_xres, best_idx, len;
+
+       if (!display->driver->read_edid)
+               return -ENODEV;
+
+       len = 0x80 * 2;
+       edid = kmalloc(len, GFP_KERNEL);
+
+       r = display->driver->read_edid(display, edid, len);
+       if (r < 0)
+               goto err1;
+
+       specs = kzalloc(sizeof(*specs), GFP_KERNEL);
+
+       fb_edid_to_monspecs(edid, specs);
+
+       if (edid[126] > 0)
+               fb_edid_add_monspecs(edid + 0x80, specs);
+
+       best_xres = 0;
+       best_idx = -1;
+
+       for (i = 0; i < specs->modedb_len; ++i) {
+               struct fb_videomode *m;
+               struct omap_video_timings t;
+
+               m = &specs->modedb[i];
+
+               if (m->pixclock == 0)
+                       continue;
+
+               /* skip repeated pixel modes */
+               if (m->xres == 2880 || m->xres == 1440)
+                       continue;
+
+               fb_videomode_to_omap_timings(m, &t);
+
+               r = display->driver->check_timings(display, &t);
+               if (r == 0 && best_xres < m->xres) {
+                       best_xres = m->xres;
+                       best_idx = i;
+               }
+       }
+
+       if (best_xres == 0) {
+               r = -ENOENT;
+               goto err2;
+       }
+
+       fb_videomode_to_omap_timings(&specs->modedb[best_idx], timings);
+
+       r = 0;
+
+err2:
+       fb_destroy_modedb(specs->modedb);
+       kfree(specs);
+err1:
+       kfree(edid);
+
+       return r;
+}
+
 static int omapfb_init_display(struct omapfb2_device *fbdev,
                struct omap_dss_device *dssdev)
 {
@@ -2373,8 +2453,10 @@ static int omapfb_probe(struct platform_device *pdev)
                omap_dss_get_device(dssdev);
 
                if (!dssdev->driver) {
-                       dev_err(&pdev->dev, "no driver for display\n");
-                       r = -ENODEV;
+                       dev_warn(&pdev->dev, "no driver for display: %s\n",
+                               dssdev->name);
+                       omap_dss_put_device(dssdev);
+                       continue;
                }
 
                d = &fbdev->displays[fbdev->num_displays++];
@@ -2402,9 +2484,27 @@ static int omapfb_probe(struct platform_device *pdev)
        for (i = 0; i < fbdev->num_managers; i++)
                fbdev->managers[i] = omap_dss_get_overlay_manager(i);
 
+       /* gfx overlay should be the default one. find a display
+        * connected to that, and use it as default display */
+       ovl = omap_dss_get_overlay(0);
+       if (ovl->manager && ovl->manager->device) {
+               def_display = ovl->manager->device;
+       } else {
+               dev_warn(&pdev->dev, "cannot find default display\n");
+               def_display = NULL;
+       }
+
        if (def_mode && strlen(def_mode) > 0) {
                if (omapfb_parse_def_modes(fbdev))
                        dev_warn(&pdev->dev, "cannot parse default modes\n");
+       } else if (def_display && def_display->driver->set_timings &&
+                       def_display->driver->check_timings) {
+               struct omap_video_timings t;
+
+               r = omapfb_find_best_mode(def_display, &t);
+
+               if (r == 0)
+                       def_display->driver->set_timings(def_display, &t);
        }
 
        r = omapfb_create_framebuffers(fbdev);
@@ -2421,16 +2521,6 @@ static int omapfb_probe(struct platform_device *pdev)
 
        DBG("mgr->apply'ed\n");
 
-       /* gfx overlay should be the default one. find a display
-        * connected to that, and use it as default display */
-       ovl = omap_dss_get_overlay(0);
-       if (ovl->manager && ovl->manager->device) {
-               def_display = ovl->manager->device;
-       } else {
-               dev_warn(&pdev->dev, "cannot find default display\n");
-               def_display = NULL;
-       }
-
        if (def_display) {
                r = omapfb_init_display(fbdev, def_display);
                if (r) {
@@ -2502,6 +2592,7 @@ module_param_named(vram, def_vram, charp, 0);
 module_param_named(rotate, def_rotate, int, 0);
 module_param_named(vrfb, def_vrfb, bool, 0);
 module_param_named(mirror, def_mirror, bool, 0);
+module_param_named(vram_cache, def_vram_cache, bool, 0644);
 
 /* late_initcall to let panel/ctrl drivers loaded first.
  * I guess better option would be a more dynamic approach,