i2c-eg20t: change timeout value 50msec to 1000msec
[zen-stable.git] / drivers / staging / omapdrm / omap_fbdev.c
blob96940bbfc6f433b833cc935e42f566f246e7c0d4
1 /*
2 * drivers/staging/omapdrm/omap_fbdev.c
4 * Copyright (C) 2011 Texas Instruments
5 * Author: Rob Clark <rob@ti.com>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License version 2 as published by
9 * the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
16 * You should have received a copy of the GNU General Public License along with
17 * this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "omap_drv.h"
22 #include "drm_crtc.h"
23 #include "drm_fb_helper.h"
25 MODULE_PARM_DESC(ywrap, "Enable ywrap scrolling (omap44xx and later, default 'y')");
26 static bool ywrap_enabled = true;
27 module_param_named(ywrap, ywrap_enabled, bool, 0644);
30 * fbdev funcs, to implement legacy fbdev interface on top of drm driver
33 #define to_omap_fbdev(x) container_of(x, struct omap_fbdev, base)
35 struct omap_fbdev {
36 struct drm_fb_helper base;
37 struct drm_framebuffer *fb;
38 struct drm_gem_object *bo;
39 bool ywrap_enabled;
42 static void omap_fbdev_flush(struct fb_info *fbi, int x, int y, int w, int h);
43 static struct drm_fb_helper *get_fb(struct fb_info *fbi);
45 static ssize_t omap_fbdev_write(struct fb_info *fbi, const char __user *buf,
46 size_t count, loff_t *ppos)
48 ssize_t res;
50 res = fb_sys_write(fbi, buf, count, ppos);
51 omap_fbdev_flush(fbi, 0, 0, fbi->var.xres, fbi->var.yres);
53 return res;
56 static void omap_fbdev_fillrect(struct fb_info *fbi,
57 const struct fb_fillrect *rect)
59 sys_fillrect(fbi, rect);
60 omap_fbdev_flush(fbi, rect->dx, rect->dy, rect->width, rect->height);
63 static void omap_fbdev_copyarea(struct fb_info *fbi,
64 const struct fb_copyarea *area)
66 sys_copyarea(fbi, area);
67 omap_fbdev_flush(fbi, area->dx, area->dy, area->width, area->height);
70 static void omap_fbdev_imageblit(struct fb_info *fbi,
71 const struct fb_image *image)
73 sys_imageblit(fbi, image);
74 omap_fbdev_flush(fbi, image->dx, image->dy,
75 image->width, image->height);
78 static int omap_fbdev_pan_display(struct fb_var_screeninfo *var,
79 struct fb_info *fbi)
81 struct drm_fb_helper *helper = get_fb(fbi);
82 struct omap_fbdev *fbdev = to_omap_fbdev(helper);
83 int npages;
85 if (!helper)
86 goto fallback;
88 if (!fbdev->ywrap_enabled)
89 goto fallback;
91 /* DMM roll shifts in 4K pages: */
92 npages = fbi->fix.line_length >> PAGE_SHIFT;
93 omap_gem_roll(fbdev->bo, var->yoffset * npages);
95 return 0;
97 fallback:
98 return drm_fb_helper_pan_display(var, fbi);
101 static struct fb_ops omap_fb_ops = {
102 .owner = THIS_MODULE,
104 /* Note: to properly handle manual update displays, we wrap the
105 * basic fbdev ops which write to the framebuffer
107 .fb_read = fb_sys_read,
108 .fb_write = omap_fbdev_write,
109 .fb_fillrect = omap_fbdev_fillrect,
110 .fb_copyarea = omap_fbdev_copyarea,
111 .fb_imageblit = omap_fbdev_imageblit,
113 .fb_check_var = drm_fb_helper_check_var,
114 .fb_set_par = drm_fb_helper_set_par,
115 .fb_pan_display = omap_fbdev_pan_display,
116 .fb_blank = drm_fb_helper_blank,
117 .fb_setcmap = drm_fb_helper_setcmap,
119 .fb_debug_enter = drm_fb_helper_debug_enter,
120 .fb_debug_leave = drm_fb_helper_debug_leave,
123 static int omap_fbdev_create(struct drm_fb_helper *helper,
124 struct drm_fb_helper_surface_size *sizes)
126 struct omap_fbdev *fbdev = to_omap_fbdev(helper);
127 struct drm_device *dev = helper->dev;
128 struct omap_drm_private *priv = dev->dev_private;
129 struct drm_framebuffer *fb = NULL;
130 union omap_gem_size gsize;
131 struct fb_info *fbi = NULL;
132 struct drm_mode_fb_cmd2 mode_cmd = {0};
133 dma_addr_t paddr;
134 int ret;
136 /* only doing ARGB32 since this is what is needed to alpha-blend
137 * with video overlays:
139 sizes->surface_bpp = 32;
140 sizes->surface_depth = 32;
142 DBG("create fbdev: %dx%d@%d (%dx%d)", sizes->surface_width,
143 sizes->surface_height, sizes->surface_bpp,
144 sizes->fb_width, sizes->fb_height);
146 mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
147 sizes->surface_depth);
149 mode_cmd.width = sizes->surface_width;
150 mode_cmd.height = sizes->surface_height;
152 mode_cmd.pitches[0] = align_pitch(
153 mode_cmd.width * ((sizes->surface_bpp + 7) / 8),
154 mode_cmd.width, sizes->surface_bpp);
156 fbdev->ywrap_enabled = priv->has_dmm && ywrap_enabled;
157 if (fbdev->ywrap_enabled) {
158 /* need to align pitch to page size if using DMM scrolling */
159 mode_cmd.pitches[0] = ALIGN(mode_cmd.pitches[0], PAGE_SIZE);
162 /* allocate backing bo */
163 gsize = (union omap_gem_size){
164 .bytes = PAGE_ALIGN(mode_cmd.pitches[0] * mode_cmd.height),
166 DBG("allocating %d bytes for fb %d", gsize.bytes, dev->primary->index);
167 fbdev->bo = omap_gem_new(dev, gsize, OMAP_BO_SCANOUT | OMAP_BO_WC);
168 if (!fbdev->bo) {
169 dev_err(dev->dev, "failed to allocate buffer object\n");
170 ret = -ENOMEM;
171 goto fail;
174 fb = omap_framebuffer_init(dev, &mode_cmd, &fbdev->bo);
175 if (IS_ERR(fb)) {
176 dev_err(dev->dev, "failed to allocate fb\n");
177 /* note: if fb creation failed, we can't rely on fb destroy
178 * to unref the bo:
180 drm_gem_object_unreference(fbdev->bo);
181 ret = PTR_ERR(fb);
182 goto fail;
185 /* note: this keeps the bo pinned.. which is perhaps not ideal,
186 * but is needed as long as we use fb_mmap() to mmap to userspace
187 * (since this happens using fix.smem_start). Possibly we could
188 * implement our own mmap using GEM mmap support to avoid this
189 * (non-tiled buffer doesn't need to be pinned for fbcon to write
190 * to it). Then we just need to be sure that we are able to re-
191 * pin it in case of an opps.
193 ret = omap_gem_get_paddr(fbdev->bo, &paddr, true);
194 if (ret) {
195 dev_err(dev->dev, "could not map (paddr)!\n");
196 ret = -ENOMEM;
197 goto fail;
200 mutex_lock(&dev->struct_mutex);
202 fbi = framebuffer_alloc(0, dev->dev);
203 if (!fbi) {
204 dev_err(dev->dev, "failed to allocate fb info\n");
205 ret = -ENOMEM;
206 goto fail_unlock;
209 DBG("fbi=%p, dev=%p", fbi, dev);
211 fbdev->fb = fb;
212 helper->fb = fb;
213 helper->fbdev = fbi;
215 fbi->par = helper;
216 fbi->flags = FBINFO_DEFAULT;
217 fbi->fbops = &omap_fb_ops;
219 strcpy(fbi->fix.id, MODULE_NAME);
221 ret = fb_alloc_cmap(&fbi->cmap, 256, 0);
222 if (ret) {
223 ret = -ENOMEM;
224 goto fail_unlock;
227 drm_fb_helper_fill_fix(fbi, fb->pitches[0], fb->depth);
228 drm_fb_helper_fill_var(fbi, helper, sizes->fb_width, sizes->fb_height);
230 dev->mode_config.fb_base = paddr;
232 fbi->screen_base = omap_gem_vaddr(fbdev->bo);
233 fbi->screen_size = fbdev->bo->size;
234 fbi->fix.smem_start = paddr;
235 fbi->fix.smem_len = fbdev->bo->size;
237 /* if we have DMM, then we can use it for scrolling by just
238 * shuffling pages around in DMM rather than doing sw blit.
240 if (fbdev->ywrap_enabled) {
241 DRM_INFO("Enabling DMM ywrap scrolling\n");
242 fbi->flags |= FBINFO_HWACCEL_YWRAP | FBINFO_READS_FAST;
243 fbi->fix.ywrapstep = 1;
247 DBG("par=%p, %dx%d", fbi->par, fbi->var.xres, fbi->var.yres);
248 DBG("allocated %dx%d fb", fbdev->fb->width, fbdev->fb->height);
250 mutex_unlock(&dev->struct_mutex);
252 return 0;
254 fail_unlock:
255 mutex_unlock(&dev->struct_mutex);
256 fail:
258 if (ret) {
259 if (fbi)
260 framebuffer_release(fbi);
261 if (fb)
262 fb->funcs->destroy(fb);
265 return ret;
268 static void omap_crtc_fb_gamma_set(struct drm_crtc *crtc,
269 u16 red, u16 green, u16 blue, int regno)
271 DBG("fbdev: set gamma");
274 static void omap_crtc_fb_gamma_get(struct drm_crtc *crtc,
275 u16 *red, u16 *green, u16 *blue, int regno)
277 DBG("fbdev: get gamma");
280 static int omap_fbdev_probe(struct drm_fb_helper *helper,
281 struct drm_fb_helper_surface_size *sizes)
283 int new_fb = 0;
284 int ret;
286 if (!helper->fb) {
287 ret = omap_fbdev_create(helper, sizes);
288 if (ret)
289 return ret;
290 new_fb = 1;
292 return new_fb;
295 static struct drm_fb_helper_funcs omap_fb_helper_funcs = {
296 .gamma_set = omap_crtc_fb_gamma_set,
297 .gamma_get = omap_crtc_fb_gamma_get,
298 .fb_probe = omap_fbdev_probe,
301 static struct drm_fb_helper *get_fb(struct fb_info *fbi)
303 if (!fbi || strcmp(fbi->fix.id, MODULE_NAME)) {
304 /* these are not the fb's you're looking for */
305 return NULL;
307 return fbi->par;
310 /* flush an area of the framebuffer (in case of manual update display that
311 * is not automatically flushed)
313 static void omap_fbdev_flush(struct fb_info *fbi, int x, int y, int w, int h)
315 struct drm_fb_helper *helper = get_fb(fbi);
317 if (!helper)
318 return;
320 VERB("flush fbdev: %d,%d %dx%d, fbi=%p", x, y, w, h, fbi);
322 omap_framebuffer_flush(helper->fb, x, y, w, h);
325 /* initialize fbdev helper */
326 struct drm_fb_helper *omap_fbdev_init(struct drm_device *dev)
328 struct omap_drm_private *priv = dev->dev_private;
329 struct omap_fbdev *fbdev = NULL;
330 struct drm_fb_helper *helper;
331 int ret = 0;
333 fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL);
334 if (!fbdev) {
335 dev_err(dev->dev, "could not allocate fbdev\n");
336 goto fail;
339 helper = &fbdev->base;
341 helper->funcs = &omap_fb_helper_funcs;
343 ret = drm_fb_helper_init(dev, helper,
344 priv->num_crtcs, priv->num_connectors);
345 if (ret) {
346 dev_err(dev->dev, "could not init fbdev: ret=%d\n", ret);
347 goto fail;
350 drm_fb_helper_single_add_all_connectors(helper);
351 drm_fb_helper_initial_config(helper, 32);
353 priv->fbdev = helper;
355 return helper;
357 fail:
358 kfree(fbdev);
359 return NULL;
362 void omap_fbdev_free(struct drm_device *dev)
364 struct omap_drm_private *priv = dev->dev_private;
365 struct drm_fb_helper *helper = priv->fbdev;
366 struct omap_fbdev *fbdev;
367 struct fb_info *fbi;
369 DBG();
371 fbi = helper->fbdev;
373 unregister_framebuffer(fbi);
374 framebuffer_release(fbi);
376 drm_fb_helper_fini(helper);
378 fbdev = to_omap_fbdev(priv->fbdev);
380 /* this will free the backing object */
381 if (fbdev->fb)
382 fbdev->fb->funcs->destroy(fbdev->fb);
384 kfree(fbdev);
386 priv->fbdev = NULL;