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