Update copyrights
[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 <mach/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 struct tegra_fb_info {
47         struct tegra_dc_win     *win;
48         struct nvhost_device    *ndev;
49         struct fb_info          *info;
50         bool                    valid;
51
52         struct resource         *fb_mem;
53
54         int                     xres;
55         int                     yres;
56
57         atomic_t                in_use;
58         struct nvmap_client     *user_nvmap;
59         struct nvmap_client     *fb_nvmap;
60
61         struct workqueue_struct *flip_wq;
62 };
63
64 struct tegra_fb_flip_win {
65         struct tegra_fb_windowattr      attr;
66         struct nvmap_handle_ref         *handle;
67         dma_addr_t                      phys_addr;
68 };
69
70 struct tegra_fb_flip_data {
71         struct work_struct              work;
72         struct tegra_fb_info            *fb;
73         struct tegra_fb_flip_win        win[TEGRA_FB_FLIP_N_WINDOWS];
74         u32                             syncpt_max;
75 };
76
77 /* palette array used by the fbcon */
78 static u32 pseudo_palette[16];
79
80 static int tegra_fb_open(struct fb_info *info, int user)
81 {
82         struct tegra_fb_info *tegra_fb = info->par;
83
84         if (atomic_xchg(&tegra_fb->in_use, 1))
85                 return -EBUSY;
86
87         tegra_fb->user_nvmap = NULL;
88
89         return 0;
90 }
91
92 static int tegra_fb_release(struct fb_info *info, int user)
93 {
94         struct tegra_fb_info *tegra_fb = info->par;
95         struct fb_var_screeninfo *var = &info->var;
96
97         flush_workqueue(tegra_fb->flip_wq);
98
99         if (tegra_fb->win->cur_handle) {
100                 nvmap_unpin(tegra_fb->fb_nvmap, tegra_fb->win->cur_handle);
101                 nvmap_free(tegra_fb->fb_nvmap, tegra_fb->win->cur_handle);
102
103                 tegra_fb->win->cur_handle = NULL;
104
105                 tegra_fb->win->x = 0;
106                 tegra_fb->win->y = 0;
107                 tegra_fb->win->w = var->xres;
108                 tegra_fb->win->h = var->yres;
109                 tegra_fb->win->out_x = 0;
110                 tegra_fb->win->out_y = 0;
111                 tegra_fb->win->out_w = var->xres;
112                 tegra_fb->win->out_h = var->yres;
113                 tegra_fb->win->flags = TEGRA_WIN_FLAG_ENABLED;
114         }
115
116         if (tegra_fb->user_nvmap) {
117                 nvmap_client_put(tegra_fb->user_nvmap);
118                 tegra_fb->user_nvmap = NULL;
119         }
120
121         WARN_ON(!atomic_xchg(&tegra_fb->in_use, 0));
122
123         return 0;
124 }
125
126 static int tegra_fb_check_var(struct fb_var_screeninfo *var,
127                               struct fb_info *info)
128 {
129         if ((var->yres * var->xres * var->bits_per_pixel / 8 * 2) >
130             info->screen_size)
131                 return -EINVAL;
132
133         /* double yres_virtual to allow double buffering through pan_display */
134         var->yres_virtual = var->yres * 2;
135
136         return 0;
137 }
138
139 static int tegra_fb_set_par(struct fb_info *info)
140 {
141         struct tegra_fb_info *tegra_fb = info->par;
142         struct fb_var_screeninfo *var = &info->var;
143
144         if (var->bits_per_pixel) {
145                 /* we only support RGB ordering for now */
146                 switch (var->bits_per_pixel) {
147                 case 32:
148                         var->red.offset = 0;
149                         var->red.length = 8;
150                         var->green.offset = 8;
151                         var->green.length = 8;
152                         var->blue.offset = 16;
153                         var->blue.length = 8;
154                         var->transp.offset = 24;
155                         var->transp.length = 8;
156                         tegra_fb->win->fmt = TEGRA_WIN_FMT_R8G8B8A8;
157                         break;
158                 case 16:
159                         var->red.offset = 11;
160                         var->red.length = 5;
161                         var->green.offset = 5;
162                         var->green.length = 6;
163                         var->blue.offset = 0;
164                         var->blue.length = 5;
165                         tegra_fb->win->fmt = TEGRA_WIN_FMT_B5G6R5;
166                         break;
167
168                 default:
169                         return -EINVAL;
170                 }
171                 info->fix.line_length = var->xres * var->bits_per_pixel / 8;
172                 tegra_fb->win->stride = info->fix.line_length;
173                 tegra_fb->win->stride_uv = 0;
174                 tegra_fb->win->offset_u = 0;
175                 tegra_fb->win->offset_v = 0;
176         }
177
178         if (var->pixclock) {
179                 bool stereo;
180
181                 info->mode = (struct fb_videomode *)
182                         fb_find_best_mode(var, &info->modelist);
183                 if (!info->mode) {
184                         dev_warn(&tegra_fb->ndev->dev, "can't match video mode\n");
185                         return -EINVAL;
186                 }
187
188                 /*
189                  * only enable stereo if the mode supports it and
190                  * client requests it
191                  */
192                 stereo = !!(var->vmode & info->mode->vmode &
193                                         FB_VMODE_STEREO_FRAME_PACK);
194
195                 tegra_dc_set_fb_mode(tegra_fb->win->dc, info->mode, stereo);
196
197                 tegra_fb->win->w = info->mode->xres;
198                 tegra_fb->win->h = info->mode->yres;
199                 tegra_fb->win->out_w = info->mode->xres;
200                 tegra_fb->win->out_h = info->mode->yres;
201         }
202         return 0;
203 }
204
205 static int tegra_fb_setcolreg(unsigned regno, unsigned red, unsigned green,
206         unsigned blue, unsigned transp, struct fb_info *info)
207 {
208         struct fb_var_screeninfo *var = &info->var;
209
210         if (info->fix.visual == FB_VISUAL_TRUECOLOR ||
211             info->fix.visual == FB_VISUAL_DIRECTCOLOR) {
212                 u32 v;
213
214                 if (regno >= 16)
215                         return -EINVAL;
216
217                 red = (red >> (16 - info->var.red.length));
218                 green = (green >> (16 - info->var.green.length));
219                 blue = (blue >> (16 - info->var.blue.length));
220
221                 v = (red << var->red.offset) |
222                         (green << var->green.offset) |
223                         (blue << var->blue.offset);
224
225                 ((u32 *)info->pseudo_palette)[regno] = v;
226         }
227
228         return 0;
229 }
230
231 static int tegra_fb_blank(int blank, struct fb_info *info)
232 {
233         struct tegra_fb_info *tegra_fb = info->par;
234
235         switch (blank) {
236         case FB_BLANK_UNBLANK:
237                 dev_dbg(&tegra_fb->ndev->dev, "unblank\n");
238                 tegra_dc_enable(tegra_fb->win->dc);
239                 return 0;
240
241         case FB_BLANK_NORMAL:
242         case FB_BLANK_VSYNC_SUSPEND:
243         case FB_BLANK_HSYNC_SUSPEND:
244         case FB_BLANK_POWERDOWN:
245                 dev_dbg(&tegra_fb->ndev->dev, "blank\n");
246                 flush_workqueue(tegra_fb->flip_wq);
247                 tegra_dc_disable(tegra_fb->win->dc);
248                 return 0;
249
250         default:
251                 return -ENOTTY;
252         }
253 }
254
255 void tegra_fb_suspend(struct tegra_fb_info *tegra_fb)
256 {
257         flush_workqueue(tegra_fb->flip_wq);
258 }
259
260
261 static int tegra_fb_pan_display(struct fb_var_screeninfo *var,
262                                 struct fb_info *info)
263 {
264         struct tegra_fb_info *tegra_fb = info->par;
265         char __iomem *flush_start;
266         char __iomem *flush_end;
267         u32 addr;
268
269         if (!tegra_fb->win->cur_handle) {
270                 flush_start = info->screen_base + (var->yoffset * info->fix.line_length);
271                 flush_end = flush_start + (var->yres * info->fix.line_length);
272
273                 info->var.xoffset = var->xoffset;
274                 info->var.yoffset = var->yoffset;
275
276                 addr = info->fix.smem_start + (var->yoffset * info->fix.line_length) +
277                         (var->xoffset * (var->bits_per_pixel/8));
278
279                 tegra_fb->win->phys_addr = addr;
280                 /* TODO: update virt_addr */
281
282                 tegra_dc_update_windows(&tegra_fb->win, 1);
283                 tegra_dc_sync_windows(&tegra_fb->win, 1);
284         }
285
286         return 0;
287 }
288
289 static void tegra_fb_fillrect(struct fb_info *info,
290                               const struct fb_fillrect *rect)
291 {
292         cfb_fillrect(info, rect);
293 }
294
295 static void tegra_fb_copyarea(struct fb_info *info,
296                               const struct fb_copyarea *region)
297 {
298         cfb_copyarea(info, region);
299 }
300
301 static void tegra_fb_imageblit(struct fb_info *info,
302                                const struct fb_image *image)
303 {
304         cfb_imageblit(info, image);
305 }
306
307 /* TODO: implement ALLOC, FREE, BLANK ioctls */
308
309 static int tegra_fb_set_nvmap_fd(struct tegra_fb_info *tegra_fb, int fd)
310 {
311         struct nvmap_client *nvmap = NULL;
312
313         if (fd < 0)
314                 return -EINVAL;
315
316         nvmap = nvmap_client_get_file(fd);
317         if (IS_ERR(nvmap))
318                 return PTR_ERR(nvmap);
319
320         if (tegra_fb->user_nvmap)
321                 nvmap_client_put(tegra_fb->user_nvmap);
322
323         tegra_fb->user_nvmap = nvmap;
324
325         return 0;
326 }
327
328 static int tegra_fb_pin_window(struct tegra_fb_info *tegra_fb,
329                                struct tegra_fb_flip_win *flip_win)
330 {
331         struct nvmap_handle_ref *win_dupe;
332         struct nvmap_handle *win_handle;
333         unsigned long buff_id = flip_win->attr.buff_id;
334
335         if (!buff_id)
336                 return 0;
337
338         win_handle = nvmap_get_handle_id(tegra_fb->user_nvmap, buff_id);
339         if (win_handle == NULL) {
340                 dev_err(&tegra_fb->ndev->dev, "%s: flip invalid "
341                         "handle %08lx\n", current->comm, buff_id);
342                 return -EPERM;
343         }
344
345         /* duplicate the new framebuffer's handle into the fb driver's
346          * nvmap context, to ensure that the handle won't be freed as
347          * long as it is in-use by the fb driver */
348         win_dupe = nvmap_duplicate_handle_id(tegra_fb->fb_nvmap, buff_id);
349         nvmap_handle_put(win_handle);
350
351         if (IS_ERR(win_dupe)) {
352                 dev_err(&tegra_fb->ndev->dev, "couldn't duplicate handle\n");
353                 return PTR_ERR(win_dupe);
354         }
355
356         flip_win->handle = win_dupe;
357
358         flip_win->phys_addr = nvmap_pin(tegra_fb->fb_nvmap, win_dupe);
359         if (IS_ERR((void *)flip_win->phys_addr)) {
360                 dev_err(&tegra_fb->ndev->dev, "couldn't pin handle\n");
361                 nvmap_free(tegra_fb->fb_nvmap, win_dupe);
362                 return PTR_ERR((void *)flip_win->phys_addr);
363         }
364
365         return 0;
366 }
367
368 static int tegra_fb_set_windowattr(struct tegra_fb_info *tegra_fb,
369                                    struct tegra_dc_win *win,
370                                    const struct tegra_fb_flip_win *flip_win)
371 {
372         int xres, yres;
373         if (flip_win->handle == NULL) {
374                 win->flags = 0;
375                 win->cur_handle = NULL;
376                 return 0;
377         }
378
379         xres = tegra_fb->win->dc->mode.h_active;
380         yres = tegra_fb->win->dc->mode.v_active;
381
382         win->flags = TEGRA_WIN_FLAG_ENABLED;
383         if (flip_win->attr.blend == TEGRA_FB_WIN_BLEND_PREMULT)
384                 win->flags |= TEGRA_WIN_FLAG_BLEND_PREMULT;
385         else if (flip_win->attr.blend == TEGRA_FB_WIN_BLEND_COVERAGE)
386                 win->flags |= TEGRA_WIN_FLAG_BLEND_COVERAGE;
387         if (flip_win->attr.flags & TEGRA_FB_WIN_FLAG_INVERT_H)
388                 win->flags |= TEGRA_WIN_FLAG_INVERT_H;
389         if (flip_win->attr.flags & TEGRA_FB_WIN_FLAG_INVERT_V)
390                 win->flags |= TEGRA_WIN_FLAG_INVERT_V;
391         if (flip_win->attr.flags & TEGRA_FB_WIN_FLAG_TILED)
392                 win->flags |= TEGRA_WIN_FLAG_TILED;
393
394         win->fmt = flip_win->attr.pixformat;
395         win->x = flip_win->attr.x;
396         win->y = flip_win->attr.y;
397         win->w = flip_win->attr.w;
398         win->h = flip_win->attr.h;
399         win->out_x = flip_win->attr.out_x;
400         win->out_y = flip_win->attr.out_y;
401         win->out_w = flip_win->attr.out_w;
402         win->out_h = flip_win->attr.out_h;
403
404         if ((((win->out_x + win->out_w) > xres) && (win->out_x < xres)) ||
405                 (((win->out_y + win->out_h) > yres) && (win->out_y < yres))) {
406                 pr_warning("outside of FB: "
407                                 "FB=(%d,%d,%d,%d) "
408                                 "src=(%d,%d,%d,%d) ",
409                                 "dst=(%d,%d,%d,%d)",
410                                 0, 0, xres, yres,
411                                 win->x, win->y, win->w, win->h,
412                                 win->out_x, win->out_y, win->out_w, win->out_h);
413         }
414
415         win->z = flip_win->attr.z;
416         win->cur_handle = flip_win->handle;
417
418         /* STOPSHIP verify that this won't read outside of the surface */
419         win->phys_addr = flip_win->phys_addr;
420         win->offset = flip_win->attr.offset;
421         win->offset_u = flip_win->attr.offset_u;
422         win->offset_v = flip_win->attr.offset_v;
423         win->stride = flip_win->attr.stride;
424         win->stride_uv = flip_win->attr.stride_uv;
425
426         if ((s32)flip_win->attr.pre_syncpt_id >= 0) {
427                 nvhost_syncpt_wait_timeout(&tegra_fb->ndev->host->syncpt,
428                                            flip_win->attr.pre_syncpt_id,
429                                            flip_win->attr.pre_syncpt_val,
430                                            msecs_to_jiffies(500));
431         }
432
433
434         return 0;
435 }
436
437 static void tegra_fb_flip_worker(struct work_struct *work)
438 {
439         struct tegra_fb_flip_data *data =
440                 container_of(work, struct tegra_fb_flip_data, work);
441         struct tegra_fb_info *tegra_fb = data->fb;
442         struct tegra_dc_win *win;
443         struct tegra_dc_win *wins[TEGRA_FB_FLIP_N_WINDOWS];
444         struct nvmap_handle_ref *unpin_handles[TEGRA_FB_FLIP_N_WINDOWS];
445         int i, nr_win = 0, nr_unpin = 0;
446
447         data = container_of(work, struct tegra_fb_flip_data, work);
448
449         for (i = 0; i < TEGRA_FB_FLIP_N_WINDOWS; i++) {
450                 struct tegra_fb_flip_win *flip_win = &data->win[i];
451                 int idx = flip_win->attr.index;
452                 win = tegra_dc_get_window(tegra_fb->win->dc, idx);
453
454                 if (!win)
455                         continue;
456
457                 if (win->flags && win->cur_handle)
458                         unpin_handles[nr_unpin++] = win->cur_handle;
459
460                 tegra_fb_set_windowattr(tegra_fb, win, &data->win[i]);
461
462                 wins[nr_win++] = win;
463
464 #if 0
465                 if (flip_win->attr.pre_syncpt_id < 0)
466                         continue;
467                 printk("%08x %08x\n",
468                        flip_win->attr.pre_syncpt_id,
469                        flip_win->attr.pre_syncpt_val);
470
471                 nvhost_syncpt_wait_timeout(&tegra_fb->ndev->host->syncpt,
472                                            flip_win->attr.pre_syncpt_id,
473                                            flip_win->attr.pre_syncpt_val,
474                                            msecs_to_jiffies(500));
475 #endif
476         }
477
478         tegra_dc_update_windows(wins, nr_win);
479         /* TODO: implement swapinterval here */
480         tegra_dc_sync_windows(wins, nr_win);
481
482         tegra_dc_incr_syncpt_min(tegra_fb->win->dc, data->syncpt_max);
483
484         /* unpin and deref previous front buffers */
485         for (i = 0; i < nr_unpin; i++) {
486                 nvmap_unpin(tegra_fb->fb_nvmap, unpin_handles[i]);
487                 nvmap_free(tegra_fb->fb_nvmap, unpin_handles[i]);
488         }
489
490         kfree(data);
491 }
492
493 static int tegra_fb_flip(struct tegra_fb_info *tegra_fb,
494                          struct tegra_fb_flip_args *args)
495 {
496         struct tegra_fb_flip_data *data;
497         struct tegra_fb_flip_win *flip_win;
498         u32 syncpt_max;
499         int i, err;
500
501         if (WARN_ON(!tegra_fb->user_nvmap))
502                 return -EFAULT;
503
504         if (WARN_ON(!tegra_fb->ndev))
505                 return -EFAULT;
506
507         data = kzalloc(sizeof(*data), GFP_KERNEL);
508         if (data == NULL) {
509                 dev_err(&tegra_fb->ndev->dev,
510                         "can't allocate memory for flip\n");
511                 return -ENOMEM;
512         }
513
514         INIT_WORK(&data->work, tegra_fb_flip_worker);
515         data->fb = tegra_fb;
516
517         for (i = 0; i < TEGRA_FB_FLIP_N_WINDOWS; i++) {
518                 flip_win = &data->win[i];
519
520                 memcpy(&flip_win->attr, &args->win[i], sizeof(flip_win->attr));
521
522                 err = tegra_fb_pin_window(tegra_fb, flip_win);
523                 if (err < 0) {
524                         dev_err(&tegra_fb->ndev->dev,
525                                 "error setting window attributes\n");
526                         goto surf_err;
527                 }
528         }
529
530         syncpt_max = tegra_dc_incr_syncpt_max(tegra_fb->win->dc);
531         data->syncpt_max = syncpt_max;
532
533         queue_work(tegra_fb->flip_wq, &data->work);
534
535         args->post_syncpt_val = syncpt_max;
536         args->post_syncpt_id = tegra_dc_get_syncpt_id(tegra_fb->win->dc);
537
538         return 0;
539
540 surf_err:
541         while (i--) {
542                 if (data->win[i].handle) {
543                         nvmap_unpin(tegra_fb->fb_nvmap,
544                                     data->win[i].handle);
545                         nvmap_free(tegra_fb->fb_nvmap,
546                                    data->win[i].handle);
547                 }
548         }
549         kfree(data);
550         return err;
551 }
552
553 /* TODO: implement private window ioctls to set overlay x,y */
554
555 static int tegra_fb_ioctl(struct fb_info *info, unsigned int cmd, unsigned long arg)
556 {
557         struct tegra_fb_info *tegra_fb = info->par;
558         struct tegra_fb_flip_args flip_args;
559         struct tegra_fb_modedb modedb;
560         struct fb_modelist *modelist;
561         int i;
562         int fd;
563         int ret;
564
565         switch (cmd) {
566         case FBIO_TEGRA_SET_NVMAP_FD:
567                 if (copy_from_user(&fd, (void __user *)arg, sizeof(fd)))
568                         return -EFAULT;
569
570                 return tegra_fb_set_nvmap_fd(tegra_fb, fd);
571
572         case FBIO_TEGRA_FLIP:
573                 if (copy_from_user(&flip_args, (void __user *)arg, sizeof(flip_args)))
574                         return -EFAULT;
575
576                 ret = tegra_fb_flip(tegra_fb, &flip_args);
577
578                 if (copy_to_user((void __user *)arg, &flip_args, sizeof(flip_args)))
579                         return -EFAULT;
580
581                 return ret;
582
583         case FBIO_TEGRA_GET_MODEDB:
584                 if (copy_from_user(&modedb, (void __user *)arg, sizeof(modedb)))
585                         return -EFAULT;
586
587                 i = 0;
588                 list_for_each_entry(modelist, &info->modelist, list) {
589                         struct fb_var_screeninfo var;
590
591                         if (i >= modedb.modedb_len)
592                                 break;
593                         memset(&var, 0x0, sizeof(var));
594                         fb_videomode_to_var(&var, &modelist->mode);
595
596                         if (copy_to_user((void __user *)&modedb.modedb[i],
597                                          &var, sizeof(var)))
598                                 return -EFAULT;
599                         i++;
600
601                         if (var.vmode & FB_VMODE_STEREO_MASK) {
602                                 if (i >= modedb.modedb_len)
603                                         break;
604                                 var.vmode &= ~FB_VMODE_STEREO_MASK;
605                                 if (copy_to_user(
606                                         (void __user *)&modedb.modedb[i],
607                                          &var, sizeof(var)))
608                                         return -EFAULT;
609                                 i++;
610                         }
611                 }
612                 modedb.modedb_len = i;
613
614                 if (copy_to_user((void __user *)arg, &modedb, sizeof(modedb)))
615                         return -EFAULT;
616                 break;
617
618         default:
619                 return -ENOTTY;
620         }
621
622         return 0;
623 }
624
625 static struct fb_ops tegra_fb_ops = {
626         .owner = THIS_MODULE,
627         .fb_open = tegra_fb_open,
628         .fb_release = tegra_fb_release,
629         .fb_check_var = tegra_fb_check_var,
630         .fb_set_par = tegra_fb_set_par,
631         .fb_setcolreg = tegra_fb_setcolreg,
632         .fb_blank = tegra_fb_blank,
633         .fb_pan_display = tegra_fb_pan_display,
634         .fb_fillrect = tegra_fb_fillrect,
635         .fb_copyarea = tegra_fb_copyarea,
636         .fb_imageblit = tegra_fb_imageblit,
637         .fb_ioctl = tegra_fb_ioctl,
638 };
639
640 void tegra_fb_update_monspecs(struct tegra_fb_info *fb_info,
641                               struct fb_monspecs *specs,
642                               bool (*mode_filter)(struct fb_videomode *mode))
643 {
644         struct fb_event event;
645         struct fb_modelist *m;
646         int i;
647
648         mutex_lock(&fb_info->info->lock);
649         fb_destroy_modedb(fb_info->info->monspecs.modedb);
650
651         fb_destroy_modelist(&fb_info->info->modelist);
652
653         if (specs == NULL) {
654                 struct tegra_dc_mode mode;
655                 memset(&fb_info->info->monspecs, 0x0,
656                        sizeof(fb_info->info->monspecs));
657                 memset(&mode, 0x0, sizeof(mode));
658                 tegra_dc_set_mode(fb_info->win->dc, &mode);
659                 mutex_unlock(&fb_info->info->lock);
660                 return;
661         }
662
663         memcpy(&fb_info->info->monspecs, specs,
664                sizeof(fb_info->info->monspecs));
665
666         for (i = 0; i < specs->modedb_len; i++) {
667                 if (mode_filter) {
668                         if (mode_filter(&specs->modedb[i]))
669                                 fb_add_videomode(&specs->modedb[i],
670                                                  &fb_info->info->modelist);
671                 } else {
672                         fb_add_videomode(&specs->modedb[i],
673                                          &fb_info->info->modelist);
674                 }
675         }
676
677         if (list_empty(&fb_info->info->modelist)) {
678                 struct tegra_dc_mode mode;
679                 memset(&fb_info->info->var, 0x0, sizeof(fb_info->info->var));
680                 memset(&mode, 0x0, sizeof(mode));
681                 tegra_dc_set_mode(fb_info->win->dc, &mode);
682         } else {
683                 /* in case the first mode was not matched */
684                 m = list_first_entry(&fb_info->info->modelist, struct fb_modelist, list);
685                 m->mode.flag |= FB_MODE_IS_FIRST;
686                 fb_info->info->mode = (struct fb_videomode *)
687                         fb_find_best_display(specs, &fb_info->info->modelist);
688
689                 memset(&fb_info->info->var, 0x0,
690                        sizeof(fb_info->info->var));
691                 fb_videomode_to_var(&fb_info->info->var, fb_info->info->mode);
692                 tegra_fb_set_par(fb_info->info);
693         }
694
695         event.info = fb_info->info;
696         fb_notifier_call_chain(FB_EVENT_NEW_MODELIST, &event);
697         mutex_unlock(&fb_info->info->lock);
698 }
699
700 struct tegra_fb_info *tegra_fb_register(struct nvhost_device *ndev,
701                                         struct tegra_dc *dc,
702                                         struct tegra_fb_data *fb_data,
703                                         struct resource *fb_mem)
704 {
705         struct tegra_dc_win *win;
706         struct fb_info *info;
707         struct tegra_fb_info *tegra_fb;
708         void __iomem *fb_base = NULL;
709         unsigned long fb_size = 0;
710         unsigned long fb_phys = 0;
711         int ret = 0;
712
713         win = tegra_dc_get_window(dc, fb_data->win);
714         if (!win) {
715                 dev_err(&ndev->dev, "dc does not have a window at index %d\n",
716                         fb_data->win);
717                 return ERR_PTR(-ENOENT);
718         }
719
720         info = framebuffer_alloc(sizeof(struct tegra_fb_info), &ndev->dev);
721         if (!info) {
722                 ret = -ENOMEM;
723                 goto err;
724         }
725
726         tegra_fb = info->par;
727         tegra_fb->win = win;
728         tegra_fb->ndev = ndev;
729         tegra_fb->fb_mem = fb_mem;
730         tegra_fb->xres = fb_data->xres;
731         tegra_fb->yres = fb_data->yres;
732         tegra_fb->fb_nvmap = nvmap_create_client(nvmap_dev, "tegra-fb");
733         if (!tegra_fb->fb_nvmap) {
734                 dev_err(&ndev->dev, "couldn't create nvmap client\n");
735                 ret = -ENOMEM;
736                 goto err_free;
737         }
738         atomic_set(&tegra_fb->in_use, 0);
739
740         tegra_fb->flip_wq = create_singlethread_workqueue(dev_name(&ndev->dev));
741         if (!tegra_fb->flip_wq) {
742                 dev_err(&ndev->dev, "couldn't create flip work-queue\n");
743                 ret = -ENOMEM;
744                 goto err_delete_wq;
745         }
746
747         if (fb_mem) {
748                 fb_size = resource_size(fb_mem);
749                 fb_phys = fb_mem->start;
750                 fb_base = ioremap_nocache(fb_phys, fb_size);
751                 if (!fb_base) {
752                         dev_err(&ndev->dev, "fb can't be mapped\n");
753                         ret = -EBUSY;
754                         goto err_put_client;
755                 }
756                 tegra_fb->valid = true;
757         }
758
759         info->fbops = &tegra_fb_ops;
760         info->pseudo_palette = pseudo_palette;
761         info->screen_base = fb_base;
762         info->screen_size = fb_size;
763
764         strlcpy(info->fix.id, "tegra_fb", sizeof(info->fix.id));
765         info->fix.type          = FB_TYPE_PACKED_PIXELS;
766         info->fix.visual        = FB_VISUAL_TRUECOLOR;
767         info->fix.xpanstep      = 1;
768         info->fix.ypanstep      = 1;
769         info->fix.accel         = FB_ACCEL_NONE;
770         info->fix.smem_start    = fb_phys;
771         info->fix.smem_len      = fb_size;
772
773         info->var.xres                  = fb_data->xres;
774         info->var.yres                  = fb_data->yres;
775         info->var.xres_virtual          = fb_data->xres;
776         info->var.yres_virtual          = fb_data->yres * 2;
777         info->var.bits_per_pixel        = fb_data->bits_per_pixel;
778         info->var.activate              = FB_ACTIVATE_VBL;
779         info->var.height                = tegra_dc_get_out_height(dc);
780         info->var.width                 = tegra_dc_get_out_width(dc);
781         info->var.pixclock              = 0;
782         info->var.left_margin           = 0;
783         info->var.right_margin          = 0;
784         info->var.upper_margin          = 0;
785         info->var.lower_margin          = 0;
786         info->var.hsync_len             = 0;
787         info->var.vsync_len             = 0;
788         info->var.vmode                 = FB_VMODE_NONINTERLACED;
789
790         win->x = 0;
791         win->y = 0;
792         win->w = fb_data->xres;
793         win->h = fb_data->yres;
794         /* TODO: set to output res dc */
795         win->out_x = 0;
796         win->out_y = 0;
797         win->out_w = fb_data->xres;
798         win->out_h = fb_data->yres;
799         win->z = 0;
800         win->phys_addr = fb_phys;
801         win->virt_addr = fb_base;
802         win->offset_u = 0;
803         win->offset_v = 0;
804         win->stride = fb_data->xres * fb_data->bits_per_pixel / 8;
805         win->stride_uv = 0;
806         win->flags = TEGRA_WIN_FLAG_ENABLED;
807
808         if (fb_mem)
809                 tegra_fb_set_par(info);
810
811         if (register_framebuffer(info)) {
812                 dev_err(&ndev->dev, "failed to register framebuffer\n");
813                 ret = -ENODEV;
814                 goto err_iounmap_fb;
815         }
816
817         tegra_fb->info = info;
818
819         dev_info(&ndev->dev, "probed\n");
820
821         if (fb_data->flags & TEGRA_FB_FLIP_ON_PROBE) {
822                 tegra_dc_update_windows(&tegra_fb->win, 1);
823                 tegra_dc_sync_windows(&tegra_fb->win, 1);
824         }
825
826         return tegra_fb;
827
828 err_iounmap_fb:
829         iounmap(fb_base);
830 err_put_client:
831         nvmap_client_put(tegra_fb->fb_nvmap);
832 err_delete_wq:
833         destroy_workqueue(tegra_fb->flip_wq);
834 err_free:
835         framebuffer_release(info);
836 err:
837         return ERR_PTR(ret);
838 }
839
840 void tegra_fb_unregister(struct tegra_fb_info *fb_info)
841 {
842         struct fb_info *info = fb_info->info;
843
844         if (fb_info->win->cur_handle) {
845                 nvmap_unpin(fb_info->fb_nvmap, fb_info->win->cur_handle);
846                 nvmap_free(fb_info->fb_nvmap, fb_info->win->cur_handle);
847         }
848
849         if (fb_info->fb_nvmap)
850                 nvmap_client_put(fb_info->fb_nvmap);
851
852         unregister_framebuffer(info);
853
854         flush_workqueue(fb_info->flip_wq);
855         destroy_workqueue(fb_info->flip_wq);
856
857         iounmap(info->screen_base);
858         framebuffer_release(info);
859 }