video: tegra: dc: remove emc clock worker
[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 /* Pad pitch to 16-byte boundary. */
47 #define TEGRA_LINEAR_PITCH_ALIGNMENT 16
48
49 struct tegra_fb_info {
50         struct tegra_dc_win     *win;
51         struct nvhost_device    *ndev;
52         struct fb_info          *info;
53         bool                    valid;
54
55         struct resource         *fb_mem;
56
57         int                     xres;
58         int                     yres;
59
60         atomic_t                in_use;
61         struct nvmap_client     *user_nvmap;
62         struct nvmap_client     *fb_nvmap;
63
64         struct workqueue_struct *flip_wq;
65 };
66
67 struct tegra_fb_flip_win {
68         struct tegra_fb_windowattr      attr;
69         struct nvmap_handle_ref         *handle;
70         dma_addr_t                      phys_addr;
71 };
72
73 struct tegra_fb_flip_data {
74         struct work_struct              work;
75         struct tegra_fb_info            *fb;
76         struct tegra_fb_flip_win        win[TEGRA_FB_FLIP_N_WINDOWS];
77         u32                             syncpt_max;
78 };
79
80 /* palette array used by the fbcon */
81 static u32 pseudo_palette[16];
82
83 static int tegra_fb_open(struct fb_info *info, int user)
84 {
85         struct tegra_fb_info *tegra_fb = info->par;
86
87         if (atomic_xchg(&tegra_fb->in_use, 1))
88                 return -EBUSY;
89
90         tegra_fb->user_nvmap = NULL;
91
92         return 0;
93 }
94
95 static int tegra_fb_release(struct fb_info *info, int user)
96 {
97         struct tegra_fb_info *tegra_fb = info->par;
98         struct fb_var_screeninfo *var = &info->var;
99
100         flush_workqueue(tegra_fb->flip_wq);
101
102         if (tegra_fb->win->cur_handle) {
103                 nvmap_unpin(tegra_fb->fb_nvmap, tegra_fb->win->cur_handle);
104                 nvmap_free(tegra_fb->fb_nvmap, tegra_fb->win->cur_handle);
105
106                 tegra_fb->win->cur_handle = NULL;
107
108                 tegra_fb->win->x = 0;
109                 tegra_fb->win->y = 0;
110                 tegra_fb->win->w = var->xres;
111                 tegra_fb->win->h = var->yres;
112                 tegra_fb->win->out_x = 0;
113                 tegra_fb->win->out_y = 0;
114                 tegra_fb->win->out_w = var->xres;
115                 tegra_fb->win->out_h = var->yres;
116                 tegra_fb->win->flags = TEGRA_WIN_FLAG_ENABLED;
117         }
118
119         if (tegra_fb->user_nvmap) {
120                 nvmap_client_put(tegra_fb->user_nvmap);
121                 tegra_fb->user_nvmap = NULL;
122         }
123
124         WARN_ON(!atomic_xchg(&tegra_fb->in_use, 0));
125
126         return 0;
127 }
128
129 static int tegra_fb_check_var(struct fb_var_screeninfo *var,
130                               struct fb_info *info)
131 {
132         if ((var->yres * var->xres * var->bits_per_pixel / 8 * 2) >
133             info->screen_size)
134                 return -EINVAL;
135
136         /* double yres_virtual to allow double buffering through pan_display */
137         var->yres_virtual = var->yres * 2;
138
139         return 0;
140 }
141
142 static int tegra_fb_set_par(struct fb_info *info)
143 {
144         struct tegra_fb_info *tegra_fb = info->par;
145         struct fb_var_screeninfo *var = &info->var;
146
147         if (var->bits_per_pixel) {
148                 /* we only support RGB ordering for now */
149                 switch (var->bits_per_pixel) {
150                 case 32:
151                         var->red.offset = 0;
152                         var->red.length = 8;
153                         var->green.offset = 8;
154                         var->green.length = 8;
155                         var->blue.offset = 16;
156                         var->blue.length = 8;
157                         var->transp.offset = 24;
158                         var->transp.length = 8;
159                         tegra_fb->win->fmt = TEGRA_WIN_FMT_R8G8B8A8;
160                         break;
161                 case 16:
162                         var->red.offset = 11;
163                         var->red.length = 5;
164                         var->green.offset = 5;
165                         var->green.length = 6;
166                         var->blue.offset = 0;
167                         var->blue.length = 5;
168                         tegra_fb->win->fmt = TEGRA_WIN_FMT_B5G6R5;
169                         break;
170
171                 default:
172                         return -EINVAL;
173                 }
174                 info->fix.line_length = var->xres * var->bits_per_pixel / 8;
175                 /* Pad the stride to 16-byte boundary. */
176                 tegra_fb->win->stride = round_up(info->fix.line_length,
177                                                 TEGRA_LINEAR_PITCH_ALIGNMENT);
178                 tegra_fb->win->stride_uv = 0;
179                 tegra_fb->win->offset_u = 0;
180                 tegra_fb->win->offset_v = 0;
181         }
182
183         if (var->pixclock) {
184                 bool stereo;
185                 struct tegra_dc_mode mode;
186                 struct fb_videomode m;
187
188                 fb_var_to_videomode(&m, var);
189
190                 info->mode = (struct fb_videomode *)
191                         fb_find_nearest_mode(&m, &info->modelist);
192                 if (!info->mode) {
193                         dev_warn(&tegra_fb->ndev->dev, "can't match video mode\n");
194                         return -EINVAL;
195                 }
196
197                 /*
198                  * only enable stereo if the mode supports it and
199                  * client requests it
200                  */
201                 stereo = !!(var->vmode & info->mode->vmode &
202                                         FB_VMODE_STEREO_FRAME_PACK);
203
204                 tegra_dc_set_fb_mode(tegra_fb->win->dc, info->mode, stereo);
205
206                 tegra_fb->win->w = info->mode->xres;
207                 tegra_fb->win->h = info->mode->yres;
208                 tegra_fb->win->out_w = info->mode->xres;
209                 tegra_fb->win->out_h = info->mode->yres;
210         }
211         return 0;
212 }
213
214 static int tegra_fb_setcolreg(unsigned regno, unsigned red, unsigned green,
215         unsigned blue, unsigned transp, struct fb_info *info)
216 {
217         struct fb_var_screeninfo *var = &info->var;
218
219         if (info->fix.visual == FB_VISUAL_TRUECOLOR ||
220             info->fix.visual == FB_VISUAL_DIRECTCOLOR) {
221                 u32 v;
222
223                 if (regno >= 16)
224                         return -EINVAL;
225
226                 red = (red >> (16 - info->var.red.length));
227                 green = (green >> (16 - info->var.green.length));
228                 blue = (blue >> (16 - info->var.blue.length));
229
230                 v = (red << var->red.offset) |
231                         (green << var->green.offset) |
232                         (blue << var->blue.offset);
233
234                 ((u32 *)info->pseudo_palette)[regno] = v;
235         }
236
237         return 0;
238 }
239
240 static int tegra_fb_blank(int blank, struct fb_info *info)
241 {
242         struct tegra_fb_info *tegra_fb = info->par;
243
244         switch (blank) {
245         case FB_BLANK_UNBLANK:
246                 dev_dbg(&tegra_fb->ndev->dev, "unblank\n");
247                 tegra_dc_enable(tegra_fb->win->dc);
248                 return 0;
249
250         case FB_BLANK_NORMAL:
251         case FB_BLANK_VSYNC_SUSPEND:
252         case FB_BLANK_HSYNC_SUSPEND:
253         case FB_BLANK_POWERDOWN:
254                 dev_dbg(&tegra_fb->ndev->dev, "blank\n");
255                 flush_workqueue(tegra_fb->flip_wq);
256                 tegra_dc_disable(tegra_fb->win->dc);
257                 return 0;
258
259         default:
260                 return -ENOTTY;
261         }
262 }
263
264 void tegra_fb_suspend(struct tegra_fb_info *tegra_fb)
265 {
266         flush_workqueue(tegra_fb->flip_wq);
267 }
268
269
270 static int tegra_fb_pan_display(struct fb_var_screeninfo *var,
271                                 struct fb_info *info)
272 {
273         struct tegra_fb_info *tegra_fb = info->par;
274         char __iomem *flush_start;
275         char __iomem *flush_end;
276         u32 addr;
277
278         if (!tegra_fb->win->cur_handle) {
279                 flush_start = info->screen_base + (var->yoffset * info->fix.line_length);
280                 flush_end = flush_start + (var->yres * info->fix.line_length);
281
282                 info->var.xoffset = var->xoffset;
283                 info->var.yoffset = var->yoffset;
284
285                 addr = info->fix.smem_start + (var->yoffset * info->fix.line_length) +
286                         (var->xoffset * (var->bits_per_pixel/8));
287
288                 tegra_fb->win->phys_addr = addr;
289                 /* TODO: update virt_addr */
290
291                 tegra_dc_update_windows(&tegra_fb->win, 1);
292                 tegra_dc_sync_windows(&tegra_fb->win, 1);
293         }
294
295         return 0;
296 }
297
298 static void tegra_fb_fillrect(struct fb_info *info,
299                               const struct fb_fillrect *rect)
300 {
301         cfb_fillrect(info, rect);
302 }
303
304 static void tegra_fb_copyarea(struct fb_info *info,
305                               const struct fb_copyarea *region)
306 {
307         cfb_copyarea(info, region);
308 }
309
310 static void tegra_fb_imageblit(struct fb_info *info,
311                                const struct fb_image *image)
312 {
313         cfb_imageblit(info, image);
314 }
315
316 /* TODO: implement ALLOC, FREE, BLANK ioctls */
317
318 static int tegra_fb_set_nvmap_fd(struct tegra_fb_info *tegra_fb, int fd)
319 {
320         struct nvmap_client *nvmap = NULL;
321
322         if (fd < 0)
323                 return -EINVAL;
324
325         nvmap = nvmap_client_get_file(fd);
326         if (IS_ERR(nvmap))
327                 return PTR_ERR(nvmap);
328
329         if (tegra_fb->user_nvmap)
330                 nvmap_client_put(tegra_fb->user_nvmap);
331
332         tegra_fb->user_nvmap = nvmap;
333
334         return 0;
335 }
336
337 static int tegra_fb_pin_window(struct tegra_fb_info *tegra_fb,
338                                struct tegra_fb_flip_win *flip_win)
339 {
340         struct nvmap_handle_ref *win_dupe;
341         struct nvmap_handle *win_handle;
342         unsigned long buff_id = flip_win->attr.buff_id;
343
344         if (!buff_id)
345                 return 0;
346
347         win_handle = nvmap_get_handle_id(tegra_fb->user_nvmap, buff_id);
348         if (win_handle == NULL) {
349                 dev_err(&tegra_fb->ndev->dev, "%s: flip invalid "
350                         "handle %08lx\n", current->comm, buff_id);
351                 return -EPERM;
352         }
353
354         /* duplicate the new framebuffer's handle into the fb driver's
355          * nvmap context, to ensure that the handle won't be freed as
356          * long as it is in-use by the fb driver */
357         win_dupe = nvmap_duplicate_handle_id(tegra_fb->fb_nvmap, buff_id);
358         nvmap_handle_put(win_handle);
359
360         if (IS_ERR(win_dupe)) {
361                 dev_err(&tegra_fb->ndev->dev, "couldn't duplicate handle\n");
362                 return PTR_ERR(win_dupe);
363         }
364
365         flip_win->handle = win_dupe;
366
367         flip_win->phys_addr = nvmap_pin(tegra_fb->fb_nvmap, win_dupe);
368         if (IS_ERR((void *)flip_win->phys_addr)) {
369                 dev_err(&tegra_fb->ndev->dev, "couldn't pin handle\n");
370                 nvmap_free(tegra_fb->fb_nvmap, win_dupe);
371                 return PTR_ERR((void *)flip_win->phys_addr);
372         }
373
374         return 0;
375 }
376
377 static int tegra_fb_set_windowattr(struct tegra_fb_info *tegra_fb,
378                                    struct tegra_dc_win *win,
379                                    const struct tegra_fb_flip_win *flip_win)
380 {
381         int xres, yres;
382         if (flip_win->handle == NULL) {
383                 win->flags = 0;
384                 win->cur_handle = NULL;
385                 return 0;
386         }
387
388         xres = tegra_fb->win->dc->mode.h_active;
389         yres = tegra_fb->win->dc->mode.v_active;
390
391         win->flags = TEGRA_WIN_FLAG_ENABLED;
392         if (flip_win->attr.blend == TEGRA_FB_WIN_BLEND_PREMULT)
393                 win->flags |= TEGRA_WIN_FLAG_BLEND_PREMULT;
394         else if (flip_win->attr.blend == TEGRA_FB_WIN_BLEND_COVERAGE)
395                 win->flags |= TEGRA_WIN_FLAG_BLEND_COVERAGE;
396         if (flip_win->attr.flags & TEGRA_FB_WIN_FLAG_INVERT_H)
397                 win->flags |= TEGRA_WIN_FLAG_INVERT_H;
398         if (flip_win->attr.flags & TEGRA_FB_WIN_FLAG_INVERT_V)
399                 win->flags |= TEGRA_WIN_FLAG_INVERT_V;
400         if (flip_win->attr.flags & TEGRA_FB_WIN_FLAG_TILED)
401                 win->flags |= TEGRA_WIN_FLAG_TILED;
402
403         win->fmt = flip_win->attr.pixformat;
404         win->x = flip_win->attr.x;
405         win->y = flip_win->attr.y;
406         win->w = flip_win->attr.w;
407         win->h = flip_win->attr.h;
408         win->out_x = flip_win->attr.out_x;
409         win->out_y = flip_win->attr.out_y;
410         win->out_w = flip_win->attr.out_w;
411         win->out_h = flip_win->attr.out_h;
412
413         WARN_ONCE(win->out_x >= xres,
414                 "%s:application window x offset(%d) exceeds display width(%d)\n",
415                 dev_name(&win->dc->ndev->dev), win->out_x, xres);
416         WARN_ONCE(win->out_y >= yres,
417                 "%s:application window y offset(%d) exceeds display height(%d)\n",
418                 dev_name(&win->dc->ndev->dev), win->out_y, yres);
419         WARN_ONCE(win->out_x + win->out_w > xres && win->out_x < xres,
420                 "%s:application window width(%d) exceeds display width(%d)\n",
421                 dev_name(&win->dc->ndev->dev), win->out_x + win->out_w, xres);
422         WARN_ONCE(win->out_y + win->out_h > yres && win->out_y < yres,
423                 "%s:application window height(%d) exceeds display height(%d)\n",
424                 dev_name(&win->dc->ndev->dev), win->out_y + win->out_h, yres);
425
426         if (((win->out_x + win->out_w) > xres) && (win->out_x < xres)) {
427                 long new_w = xres - win->out_x;
428                 win->w = win->w * new_w / win->out_w;
429                 win->out_w = new_w;
430         }
431         if (((win->out_y + win->out_h) > yres) && (win->out_y < yres)) {
432                 long new_h = yres - win->out_y;
433                 win->h = win->h * new_h / win->out_h;
434                 win->out_h = new_h;
435         }
436
437         win->z = flip_win->attr.z;
438         win->cur_handle = flip_win->handle;
439
440         /* STOPSHIP verify that this won't read outside of the surface */
441         win->phys_addr = flip_win->phys_addr;
442         win->offset = flip_win->attr.offset;
443         win->offset_u = flip_win->attr.offset_u;
444         win->offset_v = flip_win->attr.offset_v;
445         win->stride = flip_win->attr.stride;
446         win->stride_uv = flip_win->attr.stride_uv;
447
448         if ((s32)flip_win->attr.pre_syncpt_id >= 0) {
449                 nvhost_syncpt_wait_timeout(&tegra_fb->ndev->host->syncpt,
450                                            flip_win->attr.pre_syncpt_id,
451                                            flip_win->attr.pre_syncpt_val,
452                                            msecs_to_jiffies(500),
453                                            NULL);
454         }
455
456
457         return 0;
458 }
459
460 static void tegra_fb_flip_worker(struct work_struct *work)
461 {
462         struct tegra_fb_flip_data *data =
463                 container_of(work, struct tegra_fb_flip_data, work);
464         struct tegra_fb_info *tegra_fb = data->fb;
465         struct tegra_dc_win *win;
466         struct tegra_dc_win *wins[TEGRA_FB_FLIP_N_WINDOWS];
467         struct nvmap_handle_ref *unpin_handles[TEGRA_FB_FLIP_N_WINDOWS];
468         int i, nr_win = 0, nr_unpin = 0;
469
470         data = container_of(work, struct tegra_fb_flip_data, work);
471
472         for (i = 0; i < TEGRA_FB_FLIP_N_WINDOWS; i++) {
473                 struct tegra_fb_flip_win *flip_win = &data->win[i];
474                 int idx = flip_win->attr.index;
475                 win = tegra_dc_get_window(tegra_fb->win->dc, idx);
476
477                 if (!win)
478                         continue;
479
480                 if (win->flags && win->cur_handle)
481                         unpin_handles[nr_unpin++] = win->cur_handle;
482
483                 tegra_fb_set_windowattr(tegra_fb, win, &data->win[i]);
484
485                 wins[nr_win++] = win;
486
487 #if 0
488                 if (flip_win->attr.pre_syncpt_id < 0)
489                         continue;
490                 printk("%08x %08x\n",
491                        flip_win->attr.pre_syncpt_id,
492                        flip_win->attr.pre_syncpt_val);
493
494                 nvhost_syncpt_wait_timeout(&tegra_fb->ndev->host->syncpt,
495                                            flip_win->attr.pre_syncpt_id,
496                                            flip_win->attr.pre_syncpt_val,
497                                            msecs_to_jiffies(500));
498 #endif
499         }
500
501         tegra_dc_update_windows(wins, nr_win);
502         /* TODO: implement swapinterval here */
503         tegra_dc_sync_windows(wins, nr_win);
504
505         tegra_dc_incr_syncpt_min(tegra_fb->win->dc, data->syncpt_max);
506
507         /* unpin and deref previous front buffers */
508         for (i = 0; i < nr_unpin; i++) {
509                 nvmap_unpin(tegra_fb->fb_nvmap, unpin_handles[i]);
510                 nvmap_free(tegra_fb->fb_nvmap, unpin_handles[i]);
511         }
512
513         kfree(data);
514 }
515
516 static int tegra_fb_flip(struct tegra_fb_info *tegra_fb,
517                          struct tegra_fb_flip_args *args)
518 {
519         struct tegra_fb_flip_data *data;
520         struct tegra_fb_flip_win *flip_win;
521         u32 syncpt_max;
522         int i, err;
523
524         if (WARN_ON(!tegra_fb->user_nvmap))
525                 return -EFAULT;
526
527         if (WARN_ON(!tegra_fb->ndev))
528                 return -EFAULT;
529
530         data = kzalloc(sizeof(*data), GFP_KERNEL);
531         if (data == NULL) {
532                 dev_err(&tegra_fb->ndev->dev,
533                         "can't allocate memory for flip\n");
534                 return -ENOMEM;
535         }
536
537         INIT_WORK(&data->work, tegra_fb_flip_worker);
538         data->fb = tegra_fb;
539
540         for (i = 0; i < TEGRA_FB_FLIP_N_WINDOWS; i++) {
541                 flip_win = &data->win[i];
542
543                 memcpy(&flip_win->attr, &args->win[i], sizeof(flip_win->attr));
544
545                 err = tegra_fb_pin_window(tegra_fb, flip_win);
546                 if (err < 0) {
547                         dev_err(&tegra_fb->ndev->dev,
548                                 "error setting window attributes\n");
549                         goto surf_err;
550                 }
551         }
552
553         syncpt_max = tegra_dc_incr_syncpt_max(tegra_fb->win->dc);
554         data->syncpt_max = syncpt_max;
555
556         queue_work(tegra_fb->flip_wq, &data->work);
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_put_client;
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_delete_wq;
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         /* Pad the stride to 16-byte boundary. */
836         win->stride = round_up(win->stride, TEGRA_LINEAR_PITCH_ALIGNMENT);
837         win->stride_uv = 0;
838         win->flags = TEGRA_WIN_FLAG_ENABLED;
839
840         if (fb_mem)
841                 tegra_fb_set_par(info);
842
843         if (register_framebuffer(info)) {
844                 dev_err(&ndev->dev, "failed to register framebuffer\n");
845                 ret = -ENODEV;
846                 goto err_iounmap_fb;
847         }
848
849         tegra_fb->info = info;
850
851         dev_info(&ndev->dev, "probed\n");
852
853         if (fb_data->flags & TEGRA_FB_FLIP_ON_PROBE) {
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_delete_wq:
863         destroy_workqueue(tegra_fb->flip_wq);
864 err_put_client:
865         nvmap_client_put(tegra_fb->fb_nvmap);
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 }