video: tegra: dc: support for vblank sync
[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-2012, NVIDIA CORPORATION, All rights reserved.
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 <linux/nvhost.h>
40 #include <linux/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 32
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
61 /* palette array used by the fbcon */
62 static u32 pseudo_palette[16];
63
64 static int tegra_fb_check_var(struct fb_var_screeninfo *var,
65                               struct fb_info *info)
66 {
67         struct tegra_fb_info *tegra_fb = info->par;
68         struct tegra_dc *dc = tegra_fb->win->dc;
69         struct tegra_dc_out_ops *ops = dc->out_ops;
70         struct fb_videomode mode;
71
72         if ((var->yres * var->xres * var->bits_per_pixel / 8 * 2) >
73             info->screen_size)
74                 return -EINVAL;
75
76         /* Apply mode filter for HDMI only -LVDS supports only fix mode */
77         if (ops && ops->mode_filter) {
78
79                 fb_var_to_videomode(&mode, var);
80                 if (!ops->mode_filter(dc, &mode))
81                         return -EINVAL;
82
83                 /* Mode filter may have modified the mode */
84                 fb_videomode_to_var(var, &mode);
85         }
86
87         /* Double yres_virtual to allow double buffering through pan_display */
88         var->yres_virtual = var->yres * 2;
89
90         return 0;
91 }
92
93 static int tegra_fb_set_par(struct fb_info *info)
94 {
95         struct tegra_fb_info *tegra_fb = info->par;
96         struct fb_var_screeninfo *var = &info->var;
97
98         if (var->bits_per_pixel) {
99                 /* we only support RGB ordering for now */
100                 switch (var->bits_per_pixel) {
101                 case 32:
102                         var->red.offset = 0;
103                         var->red.length = 8;
104                         var->green.offset = 8;
105                         var->green.length = 8;
106                         var->blue.offset = 16;
107                         var->blue.length = 8;
108                         var->transp.offset = 24;
109                         var->transp.length = 8;
110                         tegra_fb->win->fmt = TEGRA_WIN_FMT_R8G8B8A8;
111                         break;
112                 case 16:
113                         var->red.offset = 11;
114                         var->red.length = 5;
115                         var->green.offset = 5;
116                         var->green.length = 6;
117                         var->blue.offset = 0;
118                         var->blue.length = 5;
119                         tegra_fb->win->fmt = TEGRA_WIN_FMT_B5G6R5;
120                         break;
121
122                 default:
123                         return -EINVAL;
124                 }
125                 /* if line_length unset, then pad the stride */
126                 if (!info->fix.line_length) {
127                         info->fix.line_length = var->xres * var->bits_per_pixel
128                                 / 8;
129                         info->fix.line_length = round_up(info->fix.line_length,
130                                                 TEGRA_LINEAR_PITCH_ALIGNMENT);
131                 }
132                 tegra_fb->win->stride = info->fix.line_length;
133                 tegra_fb->win->stride_uv = 0;
134                 tegra_fb->win->phys_addr_u = 0;
135                 tegra_fb->win->phys_addr_v = 0;
136         }
137
138         if (var->pixclock) {
139                 bool stereo;
140                 struct fb_videomode m;
141
142                 fb_var_to_videomode(&m, var);
143
144                 info->mode = (struct fb_videomode *)
145                         fb_find_nearest_mode(&m, &info->modelist);
146                 if (!info->mode) {
147                         dev_warn(&tegra_fb->ndev->dev, "can't match video mode\n");
148                         return -EINVAL;
149                 }
150
151                 /*
152                  * only enable stereo if the mode supports it and
153                  * client requests it
154                  */
155                 stereo = !!(var->vmode & info->mode->vmode &
156 #ifndef CONFIG_TEGRA_HDMI_74MHZ_LIMIT
157                                         FB_VMODE_STEREO_FRAME_PACK);
158 #else
159                                         FB_VMODE_STEREO_LEFT_RIGHT);
160 #endif
161
162                 tegra_dc_set_fb_mode(tegra_fb->win->dc, info->mode, stereo);
163
164                 tegra_fb->win->w.full = dfixed_const(info->mode->xres);
165                 tegra_fb->win->h.full = dfixed_const(info->mode->yres);
166                 tegra_fb->win->out_w = info->mode->xres;
167                 tegra_fb->win->out_h = info->mode->yres;
168         }
169         return 0;
170 }
171
172 static int tegra_fb_setcolreg(unsigned regno, unsigned red, unsigned green,
173         unsigned blue, unsigned transp, struct fb_info *info)
174 {
175         struct fb_var_screeninfo *var = &info->var;
176
177         if (info->fix.visual == FB_VISUAL_TRUECOLOR ||
178             info->fix.visual == FB_VISUAL_DIRECTCOLOR) {
179                 u32 v;
180
181                 if (regno >= 16)
182                         return -EINVAL;
183
184                 red = (red >> (16 - info->var.red.length));
185                 green = (green >> (16 - info->var.green.length));
186                 blue = (blue >> (16 - info->var.blue.length));
187
188                 v = (red << var->red.offset) |
189                         (green << var->green.offset) |
190                         (blue << var->blue.offset);
191
192                 ((u32 *)info->pseudo_palette)[regno] = v;
193         }
194
195         return 0;
196 }
197
198
199 static int tegra_fb_setcmap(struct fb_cmap *cmap, struct fb_info *info)
200 {
201         struct tegra_fb_info *tegra_fb = info->par;
202         struct tegra_dc *dc = tegra_fb->win->dc;
203         int i;
204         u16 *red = cmap->red;
205         u16 *green = cmap->green;
206         u16 *blue = cmap->blue;
207         int start = cmap->start;
208
209         if (((unsigned)start > 255) || ((start + cmap->len) > 256))
210                 return -EINVAL;
211
212         if (info->fix.visual == FB_VISUAL_TRUECOLOR ||
213                 info->fix.visual == FB_VISUAL_DIRECTCOLOR) {
214                 /*
215                  * For now we are considering color schemes with
216                  * cmap->len <=16 as special case of basic color
217                  * scheme to support fbconsole.But for DirectColor
218                  * visuals(like the one we actually have, that include
219                  * a HW LUT),the way it's intended to work is that the
220                  * actual LUT HW is programmed to the intended values,
221                  * even for small color maps like those with 16 or fewer
222                  * entries. The pseudo_palette is then programmed to the
223                  * identity transform.
224                  */
225                 if (cmap->len <= 16) {
226                         /* Low-color schemes like fbconsole*/
227                         u16 *transp = cmap->transp;
228                         u_int vtransp = 0xffff;
229
230                         for (i = 0; i < cmap->len; i++) {
231                                 if (transp)
232                                         vtransp = *transp++;
233                                 if (tegra_fb_setcolreg(start++, *red++,
234                                         *green++, *blue++,
235                                         vtransp, info))
236                                                 return -EINVAL;
237                         }
238                 } else {
239                         /* High-color schemes*/
240                         for (i = 0; i < cmap->len; i++) {
241                                 dc->fb_lut.r[start+i] = *red++ >> 8;
242                                 dc->fb_lut.g[start+i] = *green++ >> 8;
243                                 dc->fb_lut.b[start+i] = *blue++ >> 8;
244                         }
245                         tegra_dc_update_lut(dc, -1, -1);
246                 }
247         }
248         return 0;
249 }
250
251 static int tegra_fb_blank(int blank, struct fb_info *info)
252 {
253         struct tegra_fb_info *tegra_fb = info->par;
254
255         switch (blank) {
256         case FB_BLANK_UNBLANK:
257                 dev_dbg(&tegra_fb->ndev->dev, "unblank\n");
258                 tegra_fb->win->flags = TEGRA_WIN_FLAG_ENABLED;
259                 tegra_dc_enable(tegra_fb->win->dc);
260                 return 0;
261
262         case FB_BLANK_NORMAL:
263                 dev_dbg(&tegra_fb->ndev->dev, "blank - normal\n");
264                 tegra_dc_blank(tegra_fb->win->dc);
265                 return 0;
266
267         case FB_BLANK_VSYNC_SUSPEND:
268         case FB_BLANK_HSYNC_SUSPEND:
269         case FB_BLANK_POWERDOWN:
270                 dev_dbg(&tegra_fb->ndev->dev, "blank - powerdown\n");
271                 tegra_dc_disable(tegra_fb->win->dc);
272                 return 0;
273
274         default:
275                 return -ENOTTY;
276         }
277 }
278
279 static int tegra_fb_pan_display(struct fb_var_screeninfo *var,
280                                 struct fb_info *info)
281 {
282         struct tegra_fb_info *tegra_fb = info->par;
283         char __iomem *flush_start;
284         char __iomem *flush_end;
285         u32 addr;
286
287         if (!tegra_fb->win->cur_handle) {
288                 flush_start = info->screen_base + (var->yoffset * info->fix.line_length);
289                 flush_end = flush_start + (var->yres * info->fix.line_length);
290
291                 info->var.xoffset = var->xoffset;
292                 info->var.yoffset = var->yoffset;
293
294                 addr = info->fix.smem_start + (var->yoffset * info->fix.line_length) +
295                         (var->xoffset * (var->bits_per_pixel/8));
296
297                 tegra_fb->win->phys_addr = addr;
298                 tegra_fb->win->flags = TEGRA_WIN_FLAG_ENABLED;
299                 tegra_fb->win->virt_addr = info->screen_base;
300
301                 tegra_dc_update_windows(&tegra_fb->win, 1);
302                 tegra_dc_sync_windows(&tegra_fb->win, 1);
303         }
304
305         return 0;
306 }
307
308 static void tegra_fb_fillrect(struct fb_info *info,
309                               const struct fb_fillrect *rect)
310 {
311         cfb_fillrect(info, rect);
312 }
313
314 static void tegra_fb_copyarea(struct fb_info *info,
315                               const struct fb_copyarea *region)
316 {
317         cfb_copyarea(info, region);
318 }
319
320 static void tegra_fb_imageblit(struct fb_info *info,
321                                const struct fb_image *image)
322 {
323         cfb_imageblit(info, image);
324 }
325
326 static int tegra_fb_ioctl(struct fb_info *info, unsigned int cmd, unsigned long arg)
327 {
328         struct tegra_fb_info *tegra_fb = (struct tegra_fb_info *)info->par;
329         struct tegra_fb_modedb modedb;
330         struct fb_modelist *modelist;
331         struct fb_vblank vblank = {};
332         int i;
333
334         switch (cmd) {
335         case FBIO_TEGRA_GET_MODEDB:
336                 if (copy_from_user(&modedb, (void __user *)arg, sizeof(modedb)))
337                         return -EFAULT;
338
339                 i = 0;
340                 list_for_each_entry(modelist, &info->modelist, list) {
341                         struct fb_var_screeninfo var;
342
343                         if (i >= modedb.modedb_len)
344                                 break;
345
346                         /* fb_videomode_to_var doesn't fill out all the members
347                            of fb_var_screeninfo */
348                         memset(&var, 0x0, sizeof(var));
349
350                         fb_videomode_to_var(&var, &modelist->mode);
351
352                         if (copy_to_user((void __user *)&modedb.modedb[i],
353                                          &var, sizeof(var)))
354                                 return -EFAULT;
355                         i++;
356
357                         if (var.vmode & FB_VMODE_STEREO_MASK) {
358                                 if (i >= modedb.modedb_len)
359                                         break;
360                                 var.vmode &= ~FB_VMODE_STEREO_MASK;
361                                 if (copy_to_user(
362                                         (void __user *)&modedb.modedb[i],
363                                          &var, sizeof(var)))
364                                         return -EFAULT;
365                                 i++;
366                         }
367                 }
368                 modedb.modedb_len = i;
369
370                 if (copy_to_user((void __user *)arg, &modedb, sizeof(modedb)))
371                         return -EFAULT;
372                 break;
373
374         case FBIOGET_VBLANK:
375                 tegra_dc_get_fbvblank(tegra_fb->win->dc, &vblank);
376
377                 if (copy_to_user(
378                         (void __user *)arg, &vblank, sizeof(vblank)))
379                         return -EFAULT;
380                 break;
381
382         case FBIO_WAITFORVSYNC:
383                 return tegra_dc_wait_for_vsync(tegra_fb->win->dc);
384
385         default:
386                 return -ENOTTY;
387         }
388
389         return 0;
390 }
391
392 int tegra_fb_get_mode(struct tegra_dc *dc) {
393         return dc->fb->info->mode->refresh;
394 }
395
396 int tegra_fb_set_mode(struct tegra_dc *dc, int fps) {
397         size_t stereo;
398         struct list_head *pos;
399         struct fb_videomode *best_mode = NULL;
400         int curr_diff = INT_MAX; /* difference of best_mode refresh rate */
401         struct fb_modelist *modelist;
402         struct fb_info *info = dc->fb->info;
403
404         list_for_each(pos, &info->modelist) {
405                 struct fb_videomode *mode;
406
407                 modelist = list_entry(pos, struct fb_modelist, list);
408                 mode = &modelist->mode;
409                 if (fps <= mode->refresh && curr_diff > (mode->refresh - fps)) {
410                         curr_diff = mode->refresh - fps;
411                         best_mode = mode;
412                 }
413         }
414         if (best_mode) {
415                 info->mode = best_mode;
416                 stereo = !!(info->var.vmode & info->mode->vmode &
417 #ifndef CONFIG_TEGRA_HDMI_74MHZ_LIMIT
418                                 FB_VMODE_STEREO_FRAME_PACK);
419 #else
420                                 FB_VMODE_STEREO_LEFT_RIGHT);
421 #endif
422                 return tegra_dc_set_fb_mode(dc, best_mode, stereo);
423         }
424         return -EIO;
425 }
426
427 static struct fb_ops tegra_fb_ops = {
428         .owner = THIS_MODULE,
429         .fb_check_var = tegra_fb_check_var,
430         .fb_set_par = tegra_fb_set_par,
431         .fb_setcmap = tegra_fb_setcmap,
432         .fb_blank = tegra_fb_blank,
433         .fb_pan_display = tegra_fb_pan_display,
434         .fb_fillrect = tegra_fb_fillrect,
435         .fb_copyarea = tegra_fb_copyarea,
436         .fb_imageblit = tegra_fb_imageblit,
437         .fb_ioctl = tegra_fb_ioctl,
438 };
439
440 void tegra_fb_update_monspecs(struct tegra_fb_info *fb_info,
441                               struct fb_monspecs *specs,
442                               bool (*mode_filter)(const struct tegra_dc *dc,
443                                                   struct fb_videomode *mode))
444 {
445         struct fb_event event;
446         int i;
447
448         mutex_lock(&fb_info->info->lock);
449         fb_destroy_modedb(fb_info->info->monspecs.modedb);
450
451         fb_destroy_modelist(&fb_info->info->modelist);
452
453         if (specs == NULL) {
454                 struct tegra_dc_mode mode;
455                 memset(&fb_info->info->monspecs, 0x0,
456                        sizeof(fb_info->info->monspecs));
457                 memset(&mode, 0x0, sizeof(mode));
458
459                 /*
460                  * reset video mode properties to prevent garbage being displayed on 'mode' device.
461                  */
462                 fb_info->info->mode = (struct fb_videomode*) NULL;
463
464                 tegra_dc_set_mode(fb_info->win->dc, &mode);
465                 mutex_unlock(&fb_info->info->lock);
466                 return;
467         }
468
469         memcpy(&fb_info->info->monspecs, specs,
470                sizeof(fb_info->info->monspecs));
471         fb_info->info->mode = specs->modedb;
472
473         for (i = 0; i < specs->modedb_len; i++) {
474                 if (mode_filter) {
475                         if (mode_filter(fb_info->win->dc, &specs->modedb[i]))
476                                 fb_add_videomode(&specs->modedb[i],
477                                                  &fb_info->info->modelist);
478                 } else {
479                         fb_add_videomode(&specs->modedb[i],
480                                          &fb_info->info->modelist);
481                 }
482         }
483
484         event.info = fb_info->info;
485         fb_notifier_call_chain(FB_EVENT_NEW_MODELIST, &event);
486         mutex_unlock(&fb_info->info->lock);
487 }
488
489 struct tegra_fb_info *tegra_fb_register(struct nvhost_device *ndev,
490                                         struct tegra_dc *dc,
491                                         struct tegra_fb_data *fb_data,
492                                         struct resource *fb_mem)
493 {
494         struct tegra_dc_win *win;
495         struct fb_info *info;
496         struct tegra_fb_info *tegra_fb;
497         void __iomem *fb_base = NULL;
498         unsigned long fb_size = 0;
499         unsigned long fb_phys = 0;
500         int ret = 0;
501         unsigned stride;
502
503         win = tegra_dc_get_window(dc, fb_data->win);
504         if (!win) {
505                 dev_err(&ndev->dev, "dc does not have a window at index %d\n",
506                         fb_data->win);
507                 return ERR_PTR(-ENOENT);
508         }
509
510         info = framebuffer_alloc(sizeof(struct tegra_fb_info), &ndev->dev);
511         if (!info) {
512                 ret = -ENOMEM;
513                 goto err;
514         }
515
516         tegra_fb = info->par;
517         tegra_fb->win = win;
518         tegra_fb->ndev = ndev;
519         tegra_fb->fb_mem = fb_mem;
520         tegra_fb->xres = fb_data->xres;
521         tegra_fb->yres = fb_data->yres;
522
523         if (fb_mem) {
524                 fb_size = resource_size(fb_mem);
525                 fb_phys = fb_mem->start;
526                 fb_base = ioremap_nocache(fb_phys, fb_size);
527                 if (!fb_base) {
528                         dev_err(&ndev->dev, "fb can't be mapped\n");
529                         ret = -EBUSY;
530                         goto err_free;
531                 }
532                 tegra_fb->valid = true;
533         }
534
535         info->fix.line_length = fb_data->xres * fb_data->bits_per_pixel / 8;
536
537         stride = tegra_dc_get_stride(dc, 0);
538         if (!stride) /* default to pad the stride */
539                 stride = round_up(info->fix.line_length,
540                         TEGRA_LINEAR_PITCH_ALIGNMENT);
541
542         info->fbops = &tegra_fb_ops;
543         info->pseudo_palette = pseudo_palette;
544         info->screen_base = fb_base;
545         info->screen_size = fb_size;
546
547         strlcpy(info->fix.id, "tegra_fb", sizeof(info->fix.id));
548         info->fix.type          = FB_TYPE_PACKED_PIXELS;
549         info->fix.visual        = FB_VISUAL_TRUECOLOR;
550         info->fix.xpanstep      = 1;
551         info->fix.ypanstep      = 1;
552         info->fix.accel         = FB_ACCEL_NONE;
553         info->fix.smem_start    = fb_phys;
554         info->fix.smem_len      = fb_size;
555         info->fix.line_length = stride;
556
557         info->var.xres                  = fb_data->xres;
558         info->var.yres                  = fb_data->yres;
559         info->var.xres_virtual          = fb_data->xres;
560         info->var.yres_virtual          = fb_data->yres * 2;
561         info->var.bits_per_pixel        = fb_data->bits_per_pixel;
562         info->var.activate              = FB_ACTIVATE_VBL;
563         info->var.height                = tegra_dc_get_out_height(dc);
564         info->var.width                 = tegra_dc_get_out_width(dc);
565         info->var.pixclock              = 0;
566         info->var.left_margin           = 0;
567         info->var.right_margin          = 0;
568         info->var.upper_margin          = 0;
569         info->var.lower_margin          = 0;
570         info->var.hsync_len             = 0;
571         info->var.vsync_len             = 0;
572         info->var.vmode                 = FB_VMODE_NONINTERLACED;
573
574         win->x.full = dfixed_const(0);
575         win->y.full = dfixed_const(0);
576         win->w.full = dfixed_const(fb_data->xres);
577         win->h.full = dfixed_const(fb_data->yres);
578         /* TODO: set to output res dc */
579         win->out_x = 0;
580         win->out_y = 0;
581         win->out_w = fb_data->xres;
582         win->out_h = fb_data->yres;
583         win->z = 0;
584         win->phys_addr = fb_phys;
585         win->virt_addr = fb_base;
586         win->phys_addr_u = 0;
587         win->phys_addr_v = 0;
588         win->stride = info->fix.line_length;
589         win->stride_uv = 0;
590         win->flags = TEGRA_WIN_FLAG_ENABLED;
591
592         if (fb_mem)
593                 tegra_fb_set_par(info);
594
595         if (register_framebuffer(info)) {
596                 dev_err(&ndev->dev, "failed to register framebuffer\n");
597                 ret = -ENODEV;
598                 goto err_iounmap_fb;
599         }
600
601         tegra_fb->info = info;
602
603         dev_info(&ndev->dev, "probed\n");
604
605         if (fb_data->flags & TEGRA_FB_FLIP_ON_PROBE) {
606                 tegra_dc_update_windows(&tegra_fb->win, 1);
607                 tegra_dc_sync_windows(&tegra_fb->win, 1);
608         }
609
610         if (dc->mode.pclk > 1000) {
611                 struct tegra_dc_mode *mode = &dc->mode;
612                 struct fb_videomode vmode;
613
614                 if (dc->out->flags & TEGRA_DC_OUT_ONE_SHOT_MODE)
615                         info->var.pixclock = KHZ2PICOS(mode->rated_pclk / 1000);
616                 else
617                         info->var.pixclock = KHZ2PICOS(mode->pclk / 1000);
618                 info->var.left_margin = mode->h_back_porch;
619                 info->var.right_margin = mode->h_front_porch;
620                 info->var.upper_margin = mode->v_back_porch;
621                 info->var.lower_margin = mode->v_front_porch;
622                 info->var.hsync_len = mode->h_sync_width;
623                 info->var.vsync_len = mode->v_sync_width;
624
625                 /* Keep info->var consistent with info->modelist. */
626                 fb_var_to_videomode(&vmode, &info->var);
627                 fb_add_videomode(&vmode, &info->modelist);
628         }
629
630         return tegra_fb;
631
632 err_iounmap_fb:
633         if (fb_base)
634                 iounmap(fb_base);
635 err_free:
636         framebuffer_release(info);
637 err:
638         return ERR_PTR(ret);
639 }
640
641 void tegra_fb_unregister(struct tegra_fb_info *fb_info)
642 {
643         struct fb_info *info = fb_info->info;
644
645         unregister_framebuffer(info);
646
647         iounmap(info->screen_base);
648         framebuffer_release(info);
649 }