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