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