2a42ae56d71b371b187ad3bce2d4c8a399426654
[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                 flush_workqueue(tegra_fb->flip_wq);
220                 tegra_dc_disable(tegra_fb->win->dc);
221                 return 0;
222
223         default:
224                 return -ENOTTY;
225         }
226 }
227
228 void tegra_fb_suspend(struct tegra_fb_info *tegra_fb)
229 {
230         flush_workqueue(tegra_fb->flip_wq);
231 }
232
233
234 static int tegra_fb_pan_display(struct fb_var_screeninfo *var,
235                                 struct fb_info *info)
236 {
237         struct tegra_fb_info *tegra_fb = info->par;
238         char __iomem *flush_start;
239         char __iomem *flush_end;
240         u32 addr;
241
242         flush_start = info->screen_base + (var->yoffset * info->fix.line_length);
243         flush_end = flush_start + (var->yres * info->fix.line_length);
244
245         info->var.xoffset = var->xoffset;
246         info->var.yoffset = var->yoffset;
247
248         addr = info->fix.smem_start + (var->yoffset * info->fix.line_length) +
249                 (var->xoffset * (var->bits_per_pixel/8));
250
251         tegra_fb->win->phys_addr = addr;
252         /* TODO: update virt_addr */
253
254         tegra_dc_update_windows(&tegra_fb->win, 1);
255         tegra_dc_sync_windows(&tegra_fb->win, 1);
256
257         if (WARN_ON(tegra_fb->win->cur_handle)) {
258                 nvmap_unpin(tegra_fb->fb_nvmap, tegra_fb->win->cur_handle);
259                 nvmap_free(tegra_fb->fb_nvmap, tegra_fb->win->cur_handle);
260                 tegra_fb->win->cur_handle = NULL;
261         }
262
263         return 0;
264 }
265
266 static void tegra_fb_fillrect(struct fb_info *info,
267                               const struct fb_fillrect *rect)
268 {
269         cfb_fillrect(info, rect);
270 }
271
272 static void tegra_fb_copyarea(struct fb_info *info,
273                               const struct fb_copyarea *region)
274 {
275         cfb_copyarea(info, region);
276 }
277
278 static void tegra_fb_imageblit(struct fb_info *info,
279                                const struct fb_image *image)
280 {
281         cfb_imageblit(info, image);
282 }
283
284 /* TODO: implement ALLOC, FREE, BLANK ioctls */
285
286 static int tegra_fb_set_nvmap_fd(struct tegra_fb_info *tegra_fb, int fd)
287 {
288         struct nvmap_client *nvmap = NULL;
289
290         if (fd < 0)
291                 return -EINVAL;
292
293         nvmap = nvmap_client_get_file(fd);
294         if (IS_ERR(nvmap))
295                 return PTR_ERR(nvmap);
296
297         if (tegra_fb->user_nvmap)
298                 nvmap_client_put(tegra_fb->user_nvmap);
299
300         tegra_fb->user_nvmap = nvmap;
301
302         return 0;
303 }
304
305 static int tegra_fb_pin_window(struct tegra_fb_info *tegra_fb,
306                                struct tegra_fb_flip_win *flip_win)
307 {
308         struct nvmap_handle_ref *win_dupe;
309         struct nvmap_handle *win_handle;
310         unsigned long buff_id = flip_win->attr.buff_id;
311
312         if (!buff_id)
313                 return 0;
314
315         win_handle = nvmap_get_handle_id(tegra_fb->user_nvmap, buff_id);
316         if (win_handle == NULL) {
317                 dev_err(&tegra_fb->ndev->dev, "%s: flip invalid "
318                         "handle %08lx\n", current->comm, buff_id);
319                 return -EPERM;
320         }
321
322         /* duplicate the new framebuffer's handle into the fb driver's
323          * nvmap context, to ensure that the handle won't be freed as
324          * long as it is in-use by the fb driver */
325         win_dupe = nvmap_duplicate_handle_id(tegra_fb->fb_nvmap, buff_id);
326         nvmap_handle_put(win_handle);
327
328         if (IS_ERR(win_dupe)) {
329                 dev_err(&tegra_fb->ndev->dev, "couldn't duplicate handle\n");
330                 return PTR_ERR(win_dupe);
331         }
332
333         flip_win->handle = win_dupe;
334
335         flip_win->phys_addr = nvmap_pin(tegra_fb->fb_nvmap, win_dupe);
336         if (IS_ERR((void *)flip_win->phys_addr)) {
337                 dev_err(&tegra_fb->ndev->dev, "couldn't pin handle\n");
338                 nvmap_free(tegra_fb->fb_nvmap, win_dupe);
339                 return PTR_ERR((void *)flip_win->phys_addr);
340         }
341
342         return 0;
343 }
344
345 static int tegra_fb_set_windowattr(struct tegra_fb_info *tegra_fb,
346                                    struct tegra_dc_win *win,
347                                    const struct tegra_fb_flip_win *flip_win)
348 {
349         if (flip_win->handle == NULL) {
350                 win->flags = 0;
351                 return 0;
352         }
353
354         win->flags = TEGRA_WIN_FLAG_ENABLED;
355         if (flip_win->attr.blend == TEGRA_FB_WIN_BLEND_PREMULT)
356                 win->flags |= TEGRA_WIN_FLAG_BLEND_PREMULT;
357         else if (flip_win->attr.blend == TEGRA_FB_WIN_BLEND_COVERAGE)
358                 win->flags |= TEGRA_WIN_FLAG_BLEND_COVERAGE;
359         win->fmt = flip_win->attr.pixformat;
360         win->x = flip_win->attr.x;
361         win->y = flip_win->attr.y;
362         win->w = flip_win->attr.w;
363         win->h = flip_win->attr.h;
364         win->out_x = flip_win->attr.out_x;
365         win->out_y = flip_win->attr.out_y;
366         win->out_w = flip_win->attr.out_w;
367         win->out_h = flip_win->attr.out_h;
368         win->z = flip_win->attr.z;
369         win->cur_handle = flip_win->handle;
370
371         /* STOPSHIP verify that this won't read outside of the surface */
372         win->phys_addr = flip_win->phys_addr + flip_win->attr.offset;
373         win->stride = flip_win->attr.stride;
374
375         if ((s32)flip_win->attr.pre_syncpt_id >= 0) {
376                 nvhost_syncpt_wait_timeout(&tegra_fb->ndev->host->syncpt,
377                                            flip_win->attr.pre_syncpt_id,
378                                            flip_win->attr.pre_syncpt_val,
379                                            msecs_to_jiffies(500));
380         }
381
382
383         return 0;
384 }
385
386 static void tegra_fb_flip_worker(struct work_struct *work)
387 {
388         struct tegra_fb_flip_data *data =
389                 container_of(work, struct tegra_fb_flip_data, work);
390         struct tegra_fb_info *tegra_fb = data->fb;
391         struct tegra_dc_win *win;
392         struct tegra_dc_win *wins[TEGRA_FB_FLIP_N_WINDOWS];
393         struct nvmap_handle_ref *unpin_handles[TEGRA_FB_FLIP_N_WINDOWS];
394         int i, nr_win = 0, nr_unpin = 0;
395
396         data = container_of(work, struct tegra_fb_flip_data, work);
397
398         for (i = 0; i < TEGRA_FB_FLIP_N_WINDOWS; i++) {
399                 struct tegra_fb_flip_win *flip_win = &data->win[i];
400                 int idx = flip_win->attr.index;
401                 win = tegra_dc_get_window(tegra_fb->win->dc, idx);
402
403                 if (!win)
404                         continue;
405
406                 if (win->flags && win->cur_handle)
407                         unpin_handles[nr_unpin++] = win->cur_handle;
408
409                 tegra_fb_set_windowattr(tegra_fb, win, &data->win[i]);
410
411                 wins[nr_win++] = win;
412
413 #if 0
414                 if (flip_win->attr.pre_syncpt_id < 0)
415                         continue;
416                 printk("%08x %08x\n",
417                        flip_win->attr.pre_syncpt_id,
418                        flip_win->attr.pre_syncpt_val);
419
420                 nvhost_syncpt_wait_timeout(&tegra_fb->ndev->host->syncpt,
421                                            flip_win->attr.pre_syncpt_id,
422                                            flip_win->attr.pre_syncpt_val,
423                                            msecs_to_jiffies(500));
424 #endif
425         }
426
427         tegra_dc_update_windows(wins, nr_win);
428         /* TODO: implement swapinterval here */
429         tegra_dc_sync_windows(wins, nr_win);
430
431         tegra_dc_incr_syncpt_min(tegra_fb->win->dc, data->syncpt_max);
432
433         /* unpin and deref previous front buffers */
434         for (i = 0; i < nr_unpin; i++) {
435                 nvmap_unpin(tegra_fb->fb_nvmap, unpin_handles[i]);
436                 nvmap_free(tegra_fb->fb_nvmap, unpin_handles[i]);
437         }
438
439         kfree(data);
440 }
441
442 static int tegra_fb_flip(struct tegra_fb_info *tegra_fb,
443                          struct tegra_fb_flip_args *args)
444 {
445         struct tegra_fb_flip_data *data;
446         struct tegra_fb_flip_win *flip_win;
447         u32 syncpt_max;
448         int i, err;
449
450         if (WARN_ON(!tegra_fb->user_nvmap))
451                 return -EFAULT;
452
453         if (WARN_ON(!tegra_fb->ndev))
454                 return -EFAULT;
455
456         data = kzalloc(sizeof(*data), GFP_KERNEL);
457         if (data == NULL) {
458                 dev_err(&tegra_fb->ndev->dev,
459                         "can't allocate memory for flip\n");
460                 return -ENOMEM;
461         }
462
463         INIT_WORK(&data->work, tegra_fb_flip_worker);
464         data->fb = tegra_fb;
465
466         for (i = 0; i < TEGRA_FB_FLIP_N_WINDOWS; i++) {
467                 flip_win = &data->win[i];
468
469                 memcpy(&flip_win->attr, &args->win[i], sizeof(flip_win->attr));
470
471                 err = tegra_fb_pin_window(tegra_fb, flip_win);
472                 if (err < 0) {
473                         dev_err(&tegra_fb->ndev->dev,
474                                 "error setting window attributes\n");
475                         goto surf_err;
476                 }
477         }
478
479         syncpt_max = tegra_dc_incr_syncpt_max(tegra_fb->win->dc);
480         data->syncpt_max = syncpt_max;
481
482         queue_work(tegra_fb->flip_wq, &data->work);
483
484         args->post_syncpt_val = syncpt_max;
485         args->post_syncpt_id = tegra_dc_get_syncpt_id(tegra_fb->win->dc);
486
487         return 0;
488
489 surf_err:
490         while (i--) {
491                 if (data->win[i].handle) {
492                         nvmap_unpin(tegra_fb->fb_nvmap,
493                                     data->win[i].handle);
494                         nvmap_free(tegra_fb->fb_nvmap,
495                                    data->win[i].handle);
496                 }
497         }
498         kfree(data);
499         return err;
500 }
501
502 /* TODO: implement private window ioctls to set overlay x,y */
503
504 static int tegra_fb_ioctl(struct fb_info *info, unsigned int cmd, unsigned long arg)
505 {
506         struct tegra_fb_info *tegra_fb = info->par;
507         struct tegra_fb_flip_args flip_args;
508         struct tegra_fb_modedb modedb;
509         struct fb_modelist *modelist;
510         int i;
511         int fd;
512         int ret;
513
514         switch (cmd) {
515         case FBIO_TEGRA_SET_NVMAP_FD:
516                 if (copy_from_user(&fd, (void __user *)arg, sizeof(fd)))
517                         return -EFAULT;
518
519                 return tegra_fb_set_nvmap_fd(tegra_fb, fd);
520
521         case FBIO_TEGRA_FLIP:
522                 if (copy_from_user(&flip_args, (void __user *)arg, sizeof(flip_args)))
523                         return -EFAULT;
524
525                 ret = tegra_fb_flip(tegra_fb, &flip_args);
526
527                 if (copy_to_user((void __user *)arg, &flip_args, sizeof(flip_args)))
528                         return -EFAULT;
529
530                 return ret;
531
532         case FBIO_TEGRA_GET_MODEDB:
533                 if (copy_from_user(&modedb, (void __user *)arg, sizeof(modedb)))
534                         return -EFAULT;
535
536                 i = 0;
537                 list_for_each_entry(modelist, &info->modelist, list) {
538                         struct fb_var_screeninfo var;
539
540                         if (i >= modedb.modedb_len)
541                                 break;
542                         fb_videomode_to_var(&var, &modelist->mode);
543
544                         if (copy_to_user((void __user *)&modedb.modedb[i],
545                                          &var, sizeof(var)))
546                                 return -EFAULT;
547                         i++;
548                 }
549                 modedb.modedb_len = i;
550
551                 if (copy_to_user((void __user *)arg, &modedb, sizeof(modedb)))
552                         return -EFAULT;
553                 break;
554
555         default:
556                 return -ENOTTY;
557         }
558
559         return 0;
560 }
561
562 static struct fb_ops tegra_fb_ops = {
563         .owner = THIS_MODULE,
564         .fb_open = tegra_fb_open,
565         .fb_release = tegra_fb_release,
566         .fb_check_var = tegra_fb_check_var,
567         .fb_set_par = tegra_fb_set_par,
568         .fb_setcolreg = tegra_fb_setcolreg,
569         .fb_blank = tegra_fb_blank,
570         .fb_pan_display = tegra_fb_pan_display,
571         .fb_fillrect = tegra_fb_fillrect,
572         .fb_copyarea = tegra_fb_copyarea,
573         .fb_imageblit = tegra_fb_imageblit,
574         .fb_ioctl = tegra_fb_ioctl,
575 };
576
577 void tegra_fb_update_monspecs(struct tegra_fb_info *fb_info,
578                               struct fb_monspecs *specs,
579                               bool (*mode_filter)(struct fb_videomode *mode))
580 {
581         struct fb_event event;
582         struct fb_modelist *m;
583         int i;
584
585         mutex_lock(&fb_info->info->lock);
586         fb_destroy_modedb(fb_info->info->monspecs.modedb);
587
588         fb_destroy_modelist(&fb_info->info->modelist);
589
590         if (specs == NULL) {
591                 struct tegra_dc_mode mode;
592                 memset(&fb_info->info->monspecs, 0x0,
593                        sizeof(fb_info->info->monspecs));
594                 memset(&mode, 0x0, sizeof(mode));
595                 tegra_dc_set_mode(fb_info->win->dc, &mode);
596                 mutex_unlock(&fb_info->info->lock);
597                 return;
598         }
599
600         memcpy(&fb_info->info->monspecs, specs,
601                sizeof(fb_info->info->monspecs));
602
603         for (i = 0; i < specs->modedb_len; i++) {
604                 if (mode_filter) {
605                         if (mode_filter(&specs->modedb[i]))
606                                 fb_add_videomode(&specs->modedb[i],
607                                                  &fb_info->info->modelist);
608                 } else {
609                         fb_add_videomode(&specs->modedb[i],
610                                          &fb_info->info->modelist);
611                 }
612         }
613
614         if (list_empty(&fb_info->info->modelist)) {
615                 struct tegra_dc_mode mode;
616                 memset(&fb_info->info->var, 0x0, sizeof(fb_info->info->var));
617                 memset(&mode, 0x0, sizeof(mode));
618                 tegra_dc_set_mode(fb_info->win->dc, &mode);
619         } else {
620                 /* in case the first mode was not matched */
621                 m = list_first_entry(&fb_info->info->modelist, struct fb_modelist, list);
622                 m->mode.flag |= FB_MODE_IS_FIRST;
623                 fb_info->info->mode = (struct fb_videomode *)
624                         fb_find_best_display(specs, &fb_info->info->modelist);
625
626                 fb_videomode_to_var(&fb_info->info->var, fb_info->info->mode);
627                 tegra_fb_set_par(fb_info->info);
628         }
629
630         event.info = fb_info->info;
631         fb_notifier_call_chain(FB_EVENT_NEW_MODELIST, &event);
632         mutex_unlock(&fb_info->info->lock);
633 }
634
635 struct tegra_fb_info *tegra_fb_register(struct nvhost_device *ndev,
636                                         struct tegra_dc *dc,
637                                         struct tegra_fb_data *fb_data,
638                                         struct resource *fb_mem)
639 {
640         struct tegra_dc_win *win;
641         struct fb_info *info;
642         struct tegra_fb_info *tegra_fb;
643         void __iomem *fb_base = NULL;
644         unsigned long fb_size = 0;
645         unsigned long fb_phys = 0;
646         int ret = 0;
647
648         win = tegra_dc_get_window(dc, fb_data->win);
649         if (!win) {
650                 dev_err(&ndev->dev, "dc does not have a window at index %d\n",
651                         fb_data->win);
652                 return ERR_PTR(-ENOENT);
653         }
654
655         info = framebuffer_alloc(sizeof(struct tegra_fb_info), &ndev->dev);
656         if (!info) {
657                 ret = -ENOMEM;
658                 goto err;
659         }
660
661         tegra_fb = info->par;
662         tegra_fb->win = win;
663         tegra_fb->ndev = ndev;
664         tegra_fb->fb_mem = fb_mem;
665         tegra_fb->xres = fb_data->xres;
666         tegra_fb->yres = fb_data->yres;
667         tegra_fb->fb_nvmap = nvmap_create_client(nvmap_dev, "tegra-fb");
668         if (!tegra_fb->fb_nvmap) {
669                 dev_err(&ndev->dev, "couldn't create nvmap client\n");
670                 ret = -ENOMEM;
671                 goto err_free;
672         }
673         atomic_set(&tegra_fb->in_use, 0);
674
675         tegra_fb->flip_wq = create_singlethread_workqueue(dev_name(&ndev->dev));
676         if (!tegra_fb->flip_wq) {
677                 dev_err(&ndev->dev, "couldn't create flip work-queue\n");
678                 ret = -ENOMEM;
679                 goto err_delete_wq;
680         }
681
682         if (fb_mem) {
683                 fb_size = resource_size(fb_mem);
684                 fb_phys = fb_mem->start;
685                 fb_base = ioremap_nocache(fb_phys, fb_size);
686                 if (!fb_base) {
687                         dev_err(&ndev->dev, "fb can't be mapped\n");
688                         ret = -EBUSY;
689                         goto err_put_client;
690                 }
691                 tegra_fb->valid = true;
692         }
693
694         info->fbops = &tegra_fb_ops;
695         info->pseudo_palette = pseudo_palette;
696         info->screen_base = fb_base;
697         info->screen_size = fb_size;
698
699         strlcpy(info->fix.id, "tegra_fb", sizeof(info->fix.id));
700         info->fix.type          = FB_TYPE_PACKED_PIXELS;
701         info->fix.visual        = FB_VISUAL_TRUECOLOR;
702         info->fix.xpanstep      = 1;
703         info->fix.ypanstep      = 1;
704         info->fix.accel         = FB_ACCEL_NONE;
705         info->fix.smem_start    = fb_phys;
706         info->fix.smem_len      = fb_size;
707
708         info->var.xres                  = fb_data->xres;
709         info->var.yres                  = fb_data->yres;
710         info->var.xres_virtual          = fb_data->xres;
711         info->var.yres_virtual          = fb_data->yres * 2;
712         info->var.bits_per_pixel        = fb_data->bits_per_pixel;
713         info->var.activate              = FB_ACTIVATE_VBL;
714         /* TODO: fill in the following by querying the DC */
715         info->var.height                = -1;
716         info->var.width                 = -1;
717         info->var.pixclock              = 0;
718         info->var.left_margin           = 0;
719         info->var.right_margin          = 0;
720         info->var.upper_margin          = 0;
721         info->var.lower_margin          = 0;
722         info->var.hsync_len             = 0;
723         info->var.vsync_len             = 0;
724         info->var.vmode                 = FB_VMODE_NONINTERLACED;
725
726         win->x = 0;
727         win->y = 0;
728         win->w = fb_data->xres;
729         win->h = fb_data->yres;
730         /* TODO: set to output res dc */
731         win->out_x = 0;
732         win->out_y = 0;
733         win->out_w = fb_data->xres;
734         win->out_h = fb_data->yres;
735         win->z = 0;
736         win->phys_addr = fb_phys;
737         win->virt_addr = fb_base;
738         win->stride = fb_data->xres * fb_data->bits_per_pixel / 8;
739         win->flags = TEGRA_WIN_FLAG_ENABLED;
740
741         if (fb_mem)
742                 tegra_fb_set_par(info);
743
744         if (register_framebuffer(info)) {
745                 dev_err(&ndev->dev, "failed to register framebuffer\n");
746                 ret = -ENODEV;
747                 goto err_iounmap_fb;
748         }
749
750         tegra_fb->info = info;
751
752         dev_info(&ndev->dev, "probed\n");
753
754         if (fb_data->flags & TEGRA_FB_FLIP_ON_PROBE) {
755                 tegra_dc_update_windows(&tegra_fb->win, 1);
756                 tegra_dc_sync_windows(&tegra_fb->win, 1);
757         }
758
759         return tegra_fb;
760
761 err_iounmap_fb:
762         iounmap(fb_base);
763 err_put_client:
764         nvmap_client_put(tegra_fb->fb_nvmap);
765 err_delete_wq:
766         destroy_workqueue(tegra_fb->flip_wq);
767 err_free:
768         framebuffer_release(info);
769 err:
770         return ERR_PTR(ret);
771 }
772
773 void tegra_fb_unregister(struct tegra_fb_info *fb_info)
774 {
775         struct fb_info *info = fb_info->info;
776
777         if (fb_info->win->cur_handle) {
778                 nvmap_unpin(fb_info->fb_nvmap, fb_info->win->cur_handle);
779                 nvmap_free(fb_info->fb_nvmap, fb_info->win->cur_handle);
780         }
781
782         if (fb_info->fb_nvmap)
783                 nvmap_client_put(fb_info->fb_nvmap);
784
785         unregister_framebuffer(info);
786
787         flush_workqueue(fb_info->flip_wq);
788         destroy_workqueue(fb_info->flip_wq);
789
790         iounmap(info->screen_base);
791         framebuffer_release(info);
792 }