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