video: tegra: enable fb window on UNBLANK
[linux-2.6.git] / drivers / video / tegra / fb.c
1 /*
2  * drivers/video/tegra/fb.c
3  *
4  * Copyright (C) 2010 Google, Inc.
5  * Author: Erik Gilling <konkers@android.com>
6  *         Colin Cross <ccross@android.com>
7  *         Travis Geiselbrecht <travis@palm.com>
8  *
9  * Copyright (C) 2010-2011 NVIDIA Corporation
10  *
11  * This software is licensed under the terms of the GNU General Public
12  * License version 2, as published by the Free Software Foundation, and
13  * may be copied, distributed, and modified under those terms.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  */
21
22 #include <linux/fb.h>
23 #include <linux/module.h>
24 #include <linux/kernel.h>
25 #include <linux/errno.h>
26 #include <linux/string.h>
27 #include <linux/mm.h>
28 #include <linux/uaccess.h>
29 #include <linux/slab.h>
30 #include <linux/file.h>
31 #include <linux/workqueue.h>
32
33 #include <asm/atomic.h>
34
35 #include <video/tegrafb.h>
36
37 #include <mach/dc.h>
38 #include <mach/fb.h>
39 #include <linux/nvhost.h>
40 #include <mach/nvmap.h>
41
42 #include "host/dev.h"
43 #include "nvmap/nvmap.h"
44 #include "dc/dc_priv.h"
45
46 /* Pad pitch to 16-byte boundary. */
47 #define TEGRA_LINEAR_PITCH_ALIGNMENT 16
48
49 struct tegra_fb_info {
50         struct tegra_dc_win     *win;
51         struct nvhost_device    *ndev;
52         struct fb_info          *info;
53         bool                    valid;
54
55         struct resource         *fb_mem;
56
57         int                     xres;
58         int                     yres;
59 };
60
61 /* palette array used by the fbcon */
62 static u32 pseudo_palette[16];
63
64 static int tegra_fb_check_var(struct fb_var_screeninfo *var,
65                               struct fb_info *info)
66 {
67         if ((var->yres * var->xres * var->bits_per_pixel / 8 * 2) >
68             info->screen_size)
69                 return -EINVAL;
70
71         /* double yres_virtual to allow double buffering through pan_display */
72         var->yres_virtual = var->yres * 2;
73
74         return 0;
75 }
76
77 static int tegra_fb_set_par(struct fb_info *info)
78 {
79         struct tegra_fb_info *tegra_fb = info->par;
80         struct fb_var_screeninfo *var = &info->var;
81
82         if (var->bits_per_pixel) {
83                 /* we only support RGB ordering for now */
84                 switch (var->bits_per_pixel) {
85                 case 32:
86                         var->red.offset = 0;
87                         var->red.length = 8;
88                         var->green.offset = 8;
89                         var->green.length = 8;
90                         var->blue.offset = 16;
91                         var->blue.length = 8;
92                         var->transp.offset = 24;
93                         var->transp.length = 8;
94                         tegra_fb->win->fmt = TEGRA_WIN_FMT_R8G8B8A8;
95                         break;
96                 case 16:
97                         var->red.offset = 11;
98                         var->red.length = 5;
99                         var->green.offset = 5;
100                         var->green.length = 6;
101                         var->blue.offset = 0;
102                         var->blue.length = 5;
103                         tegra_fb->win->fmt = TEGRA_WIN_FMT_B5G6R5;
104                         break;
105
106                 default:
107                         return -EINVAL;
108                 }
109                 info->fix.line_length = var->xres * var->bits_per_pixel / 8;
110                 /* Pad the stride to 16-byte boundary. */
111                 info->fix.line_length = round_up(info->fix.line_length,
112                                                 TEGRA_LINEAR_PITCH_ALIGNMENT);
113                 tegra_fb->win->stride = info->fix.line_length;
114                 tegra_fb->win->stride_uv = 0;
115                 tegra_fb->win->phys_addr_u = 0;
116                 tegra_fb->win->phys_addr_v = 0;
117         }
118
119         if (var->pixclock) {
120                 bool stereo;
121                 struct fb_videomode m;
122
123                 fb_var_to_videomode(&m, var);
124
125                 info->mode = (struct fb_videomode *)
126                         fb_find_nearest_mode(&m, &info->modelist);
127                 if (!info->mode) {
128                         dev_warn(&tegra_fb->ndev->dev, "can't match video mode\n");
129                         return -EINVAL;
130                 }
131
132                 /*
133                  * only enable stereo if the mode supports it and
134                  * client requests it
135                  */
136                 stereo = !!(var->vmode & info->mode->vmode &
137                                         FB_VMODE_STEREO_FRAME_PACK);
138
139                 tegra_dc_set_fb_mode(tegra_fb->win->dc, info->mode, stereo);
140
141                 tegra_fb->win->w.full = dfixed_const(info->mode->xres);
142                 tegra_fb->win->h.full = dfixed_const(info->mode->yres);
143                 tegra_fb->win->out_w = info->mode->xres;
144                 tegra_fb->win->out_h = info->mode->yres;
145         }
146         return 0;
147 }
148
149 static int tegra_fb_setcmap(struct fb_cmap *cmap, struct fb_info *info)
150 {
151         struct tegra_fb_info *tegra_fb = info->par;
152         struct tegra_dc *dc = tegra_fb->win->dc;
153         int i;
154         u16 *red = cmap->red;
155         u16 *green = cmap->green;
156         u16 *blue = cmap->blue;
157         int start = cmap->start;
158
159         if (((unsigned)start > 255) || ((start + cmap->len) > 256))
160                 return -EINVAL;
161
162         if (info->fix.visual == FB_VISUAL_TRUECOLOR ||
163             info->fix.visual == FB_VISUAL_DIRECTCOLOR) {
164                 for (i = 0; i < cmap->len; i++) {
165                         dc->fb_lut.r[start+i] = *red++ >> 8;
166                         dc->fb_lut.g[start+i] = *green++ >> 8;
167                         dc->fb_lut.b[start+i] = *blue++ >> 8;
168                 }
169
170                 tegra_dc_update_lut(dc, -1, -1);
171         }
172
173         return 0;
174 }
175
176 static int tegra_fb_blank(int blank, struct fb_info *info)
177 {
178         struct tegra_fb_info *tegra_fb = info->par;
179
180         switch (blank) {
181         case FB_BLANK_UNBLANK:
182                 dev_dbg(&tegra_fb->ndev->dev, "unblank\n");
183                 tegra_fb->win->flags = TEGRA_WIN_FLAG_ENABLED;
184                 tegra_dc_enable(tegra_fb->win->dc);
185                 return 0;
186
187         case FB_BLANK_NORMAL:
188                 dev_dbg(&tegra_fb->ndev->dev, "blank - normal\n");
189                 tegra_dc_blank(tegra_fb->win->dc);
190                 return 0;
191
192         case FB_BLANK_VSYNC_SUSPEND:
193         case FB_BLANK_HSYNC_SUSPEND:
194         case FB_BLANK_POWERDOWN:
195                 dev_dbg(&tegra_fb->ndev->dev, "blank - powerdown\n");
196                 tegra_dc_disable(tegra_fb->win->dc);
197                 return 0;
198
199         default:
200                 return -ENOTTY;
201         }
202 }
203
204 static int tegra_fb_pan_display(struct fb_var_screeninfo *var,
205                                 struct fb_info *info)
206 {
207         struct tegra_fb_info *tegra_fb = info->par;
208         char __iomem *flush_start;
209         char __iomem *flush_end;
210         u32 addr;
211
212         if (!tegra_fb->win->cur_handle) {
213                 flush_start = info->screen_base + (var->yoffset * info->fix.line_length);
214                 flush_end = flush_start + (var->yres * info->fix.line_length);
215
216                 info->var.xoffset = var->xoffset;
217                 info->var.yoffset = var->yoffset;
218
219                 addr = info->fix.smem_start + (var->yoffset * info->fix.line_length) +
220                         (var->xoffset * (var->bits_per_pixel/8));
221
222                 tegra_fb->win->phys_addr = addr;
223                 /* TODO: update virt_addr */
224
225                 tegra_dc_update_windows(&tegra_fb->win, 1);
226                 tegra_dc_sync_windows(&tegra_fb->win, 1);
227         }
228
229         return 0;
230 }
231
232 static void tegra_fb_fillrect(struct fb_info *info,
233                               const struct fb_fillrect *rect)
234 {
235         cfb_fillrect(info, rect);
236 }
237
238 static void tegra_fb_copyarea(struct fb_info *info,
239                               const struct fb_copyarea *region)
240 {
241         cfb_copyarea(info, region);
242 }
243
244 static void tegra_fb_imageblit(struct fb_info *info,
245                                const struct fb_image *image)
246 {
247         cfb_imageblit(info, image);
248 }
249
250 static int tegra_fb_ioctl(struct fb_info *info, unsigned int cmd, unsigned long arg)
251 {
252         struct tegra_fb_modedb modedb;
253         struct fb_modelist *modelist;
254         int i;
255
256         switch (cmd) {
257         case FBIO_TEGRA_GET_MODEDB:
258                 if (copy_from_user(&modedb, (void __user *)arg, sizeof(modedb)))
259                         return -EFAULT;
260
261                 i = 0;
262                 list_for_each_entry(modelist, &info->modelist, list) {
263                         struct fb_var_screeninfo var;
264
265                         if (i >= modedb.modedb_len)
266                                 break;
267
268                         /* fb_videomode_to_var doesn't fill out all the members
269                            of fb_var_screeninfo */
270                         memset(&var, 0x0, sizeof(var));
271
272                         fb_videomode_to_var(&var, &modelist->mode);
273
274                         if (copy_to_user((void __user *)&modedb.modedb[i],
275                                          &var, sizeof(var)))
276                                 return -EFAULT;
277                         i++;
278
279                         if (var.vmode & FB_VMODE_STEREO_MASK) {
280                                 if (i >= modedb.modedb_len)
281                                         break;
282                                 var.vmode &= ~FB_VMODE_STEREO_MASK;
283                                 if (copy_to_user(
284                                         (void __user *)&modedb.modedb[i],
285                                          &var, sizeof(var)))
286                                         return -EFAULT;
287                                 i++;
288                         }
289                 }
290                 modedb.modedb_len = i;
291
292                 if (copy_to_user((void __user *)arg, &modedb, sizeof(modedb)))
293                         return -EFAULT;
294                 break;
295
296         default:
297                 return -ENOTTY;
298         }
299
300         return 0;
301 }
302
303 static struct fb_ops tegra_fb_ops = {
304         .owner = THIS_MODULE,
305         .fb_check_var = tegra_fb_check_var,
306         .fb_set_par = tegra_fb_set_par,
307         .fb_setcmap = tegra_fb_setcmap,
308         .fb_blank = tegra_fb_blank,
309         .fb_pan_display = tegra_fb_pan_display,
310         .fb_fillrect = tegra_fb_fillrect,
311         .fb_copyarea = tegra_fb_copyarea,
312         .fb_imageblit = tegra_fb_imageblit,
313         .fb_ioctl = tegra_fb_ioctl,
314 };
315
316 void tegra_fb_update_monspecs(struct tegra_fb_info *fb_info,
317                               struct fb_monspecs *specs,
318                               bool (*mode_filter)(const struct tegra_dc *dc,
319                                                   struct fb_videomode *mode))
320 {
321         struct fb_event event;
322         int i;
323
324         mutex_lock(&fb_info->info->lock);
325         fb_destroy_modedb(fb_info->info->monspecs.modedb);
326
327         fb_destroy_modelist(&fb_info->info->modelist);
328
329         if (specs == NULL) {
330                 struct tegra_dc_mode mode;
331                 memset(&fb_info->info->monspecs, 0x0,
332                        sizeof(fb_info->info->monspecs));
333                 memset(&mode, 0x0, sizeof(mode));
334                 tegra_dc_set_mode(fb_info->win->dc, &mode);
335                 mutex_unlock(&fb_info->info->lock);
336                 return;
337         }
338
339         memcpy(&fb_info->info->monspecs, specs,
340                sizeof(fb_info->info->monspecs));
341
342         for (i = 0; i < specs->modedb_len; i++) {
343                 if (mode_filter) {
344                         if (mode_filter(fb_info->win->dc, &specs->modedb[i]))
345                                 fb_add_videomode(&specs->modedb[i],
346                                                  &fb_info->info->modelist);
347                 } else {
348                         fb_add_videomode(&specs->modedb[i],
349                                          &fb_info->info->modelist);
350                 }
351         }
352
353         event.info = fb_info->info;
354         fb_notifier_call_chain(FB_EVENT_NEW_MODELIST, &event);
355         mutex_unlock(&fb_info->info->lock);
356 }
357
358 struct tegra_fb_info *tegra_fb_register(struct nvhost_device *ndev,
359                                         struct tegra_dc *dc,
360                                         struct tegra_fb_data *fb_data,
361                                         struct resource *fb_mem)
362 {
363         struct tegra_dc_win *win;
364         struct fb_info *info;
365         struct tegra_fb_info *tegra_fb;
366         void __iomem *fb_base = NULL;
367         unsigned long fb_size = 0;
368         unsigned long fb_phys = 0;
369         int ret = 0;
370
371         win = tegra_dc_get_window(dc, fb_data->win);
372         if (!win) {
373                 dev_err(&ndev->dev, "dc does not have a window at index %d\n",
374                         fb_data->win);
375                 return ERR_PTR(-ENOENT);
376         }
377
378         info = framebuffer_alloc(sizeof(struct tegra_fb_info), &ndev->dev);
379         if (!info) {
380                 ret = -ENOMEM;
381                 goto err;
382         }
383
384         tegra_fb = info->par;
385         tegra_fb->win = win;
386         tegra_fb->ndev = ndev;
387         tegra_fb->fb_mem = fb_mem;
388         tegra_fb->xres = fb_data->xres;
389         tegra_fb->yres = fb_data->yres;
390
391         if (fb_mem) {
392                 fb_size = resource_size(fb_mem);
393                 fb_phys = fb_mem->start;
394                 fb_base = ioremap_nocache(fb_phys, fb_size);
395                 if (!fb_base) {
396                         dev_err(&ndev->dev, "fb can't be mapped\n");
397                         ret = -EBUSY;
398                         goto err_free;
399                 }
400                 tegra_fb->valid = true;
401         }
402
403         info->fbops = &tegra_fb_ops;
404         info->pseudo_palette = pseudo_palette;
405         info->screen_base = fb_base;
406         info->screen_size = fb_size;
407
408         strlcpy(info->fix.id, "tegra_fb", sizeof(info->fix.id));
409         info->fix.type          = FB_TYPE_PACKED_PIXELS;
410         info->fix.visual        = FB_VISUAL_TRUECOLOR;
411         info->fix.xpanstep      = 1;
412         info->fix.ypanstep      = 1;
413         info->fix.accel         = FB_ACCEL_NONE;
414         info->fix.smem_start    = fb_phys;
415         info->fix.smem_len      = fb_size;
416         info->fix.line_length = fb_data->xres * fb_data->bits_per_pixel / 8;
417         /* Pad the stride to 16-byte boundary. */
418         info->fix.line_length = round_up(info->fix.line_length,
419                                         TEGRA_LINEAR_PITCH_ALIGNMENT);
420
421         info->var.xres                  = fb_data->xres;
422         info->var.yres                  = fb_data->yres;
423         info->var.xres_virtual          = fb_data->xres;
424         info->var.yres_virtual          = fb_data->yres * 2;
425         info->var.bits_per_pixel        = fb_data->bits_per_pixel;
426         info->var.activate              = FB_ACTIVATE_VBL;
427         info->var.height                = tegra_dc_get_out_height(dc);
428         info->var.width                 = tegra_dc_get_out_width(dc);
429         info->var.pixclock              = 0;
430         info->var.left_margin           = 0;
431         info->var.right_margin          = 0;
432         info->var.upper_margin          = 0;
433         info->var.lower_margin          = 0;
434         info->var.hsync_len             = 0;
435         info->var.vsync_len             = 0;
436         info->var.vmode                 = FB_VMODE_NONINTERLACED;
437
438         win->x.full = dfixed_const(0);
439         win->y.full = dfixed_const(0);
440         win->w.full = dfixed_const(fb_data->xres);
441         win->h.full = dfixed_const(fb_data->yres);
442         /* TODO: set to output res dc */
443         win->out_x = 0;
444         win->out_y = 0;
445         win->out_w = fb_data->xres;
446         win->out_h = fb_data->yres;
447         win->z = 0;
448         win->phys_addr = fb_phys;
449         win->virt_addr = fb_base;
450         win->phys_addr_u = 0;
451         win->phys_addr_v = 0;
452         win->stride = info->fix.line_length;
453         win->stride_uv = 0;
454         win->flags = TEGRA_WIN_FLAG_ENABLED;
455
456         if (fb_mem)
457                 tegra_fb_set_par(info);
458
459         if (register_framebuffer(info)) {
460                 dev_err(&ndev->dev, "failed to register framebuffer\n");
461                 ret = -ENODEV;
462                 goto err_iounmap_fb;
463         }
464
465         tegra_fb->info = info;
466
467         dev_info(&ndev->dev, "probed\n");
468
469         if (fb_data->flags & TEGRA_FB_FLIP_ON_PROBE) {
470                 tegra_dc_update_windows(&tegra_fb->win, 1);
471                 tegra_dc_sync_windows(&tegra_fb->win, 1);
472         }
473
474         return tegra_fb;
475
476 err_iounmap_fb:
477         if (fb_base)
478                 iounmap(fb_base);
479 err_free:
480         framebuffer_release(info);
481 err:
482         return ERR_PTR(ret);
483 }
484
485 void tegra_fb_unregister(struct tegra_fb_info *fb_info)
486 {
487         struct fb_info *info = fb_info->info;
488
489         unregister_framebuffer(info);
490
491         iounmap(info->screen_base);
492         framebuffer_release(info);
493 }