video: tegra: fb: Default stride from HW
[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_modedb modedb;
329         struct fb_modelist *modelist;
330         int i;
331
332         switch (cmd) {
333         case FBIO_TEGRA_GET_MODEDB:
334                 if (copy_from_user(&modedb, (void __user *)arg, sizeof(modedb)))
335                         return -EFAULT;
336
337                 i = 0;
338                 list_for_each_entry(modelist, &info->modelist, list) {
339                         struct fb_var_screeninfo var;
340
341                         if (i >= modedb.modedb_len)
342                                 break;
343
344                         /* fb_videomode_to_var doesn't fill out all the members
345                            of fb_var_screeninfo */
346                         memset(&var, 0x0, sizeof(var));
347
348                         fb_videomode_to_var(&var, &modelist->mode);
349
350                         if (copy_to_user((void __user *)&modedb.modedb[i],
351                                          &var, sizeof(var)))
352                                 return -EFAULT;
353                         i++;
354
355                         if (var.vmode & FB_VMODE_STEREO_MASK) {
356                                 if (i >= modedb.modedb_len)
357                                         break;
358                                 var.vmode &= ~FB_VMODE_STEREO_MASK;
359                                 if (copy_to_user(
360                                         (void __user *)&modedb.modedb[i],
361                                          &var, sizeof(var)))
362                                         return -EFAULT;
363                                 i++;
364                         }
365                 }
366                 modedb.modedb_len = i;
367
368                 if (copy_to_user((void __user *)arg, &modedb, sizeof(modedb)))
369                         return -EFAULT;
370                 break;
371
372         default:
373                 return -ENOTTY;
374         }
375
376         return 0;
377 }
378
379 int tegra_fb_get_mode(struct tegra_dc *dc) {
380         return dc->fb->info->mode->refresh;
381 }
382
383 int tegra_fb_set_mode(struct tegra_dc *dc, int fps) {
384         size_t stereo;
385         struct list_head *pos;
386         struct fb_videomode *best_mode = NULL;
387         int curr_diff = INT_MAX; /* difference of best_mode refresh rate */
388         struct fb_modelist *modelist;
389         struct fb_info *info = dc->fb->info;
390
391         list_for_each(pos, &info->modelist) {
392                 struct fb_videomode *mode;
393
394                 modelist = list_entry(pos, struct fb_modelist, list);
395                 mode = &modelist->mode;
396                 if (fps <= mode->refresh && curr_diff > (mode->refresh - fps)) {
397                         curr_diff = mode->refresh - fps;
398                         best_mode = mode;
399                 }
400         }
401         if (best_mode) {
402                 info->mode = best_mode;
403                 stereo = !!(info->var.vmode & info->mode->vmode &
404 #ifndef CONFIG_TEGRA_HDMI_74MHZ_LIMIT
405                                 FB_VMODE_STEREO_FRAME_PACK);
406 #else
407                                 FB_VMODE_STEREO_LEFT_RIGHT);
408 #endif
409                 return tegra_dc_set_fb_mode(dc, best_mode, stereo);
410         }
411         return -EIO;
412 }
413
414 static struct fb_ops tegra_fb_ops = {
415         .owner = THIS_MODULE,
416         .fb_check_var = tegra_fb_check_var,
417         .fb_set_par = tegra_fb_set_par,
418         .fb_setcmap = tegra_fb_setcmap,
419         .fb_blank = tegra_fb_blank,
420         .fb_pan_display = tegra_fb_pan_display,
421         .fb_fillrect = tegra_fb_fillrect,
422         .fb_copyarea = tegra_fb_copyarea,
423         .fb_imageblit = tegra_fb_imageblit,
424         .fb_ioctl = tegra_fb_ioctl,
425 };
426
427 void tegra_fb_update_monspecs(struct tegra_fb_info *fb_info,
428                               struct fb_monspecs *specs,
429                               bool (*mode_filter)(const struct tegra_dc *dc,
430                                                   struct fb_videomode *mode))
431 {
432         struct fb_event event;
433         int i;
434
435         mutex_lock(&fb_info->info->lock);
436         fb_destroy_modedb(fb_info->info->monspecs.modedb);
437
438         fb_destroy_modelist(&fb_info->info->modelist);
439
440         if (specs == NULL) {
441                 struct tegra_dc_mode mode;
442                 memset(&fb_info->info->monspecs, 0x0,
443                        sizeof(fb_info->info->monspecs));
444                 memset(&mode, 0x0, sizeof(mode));
445
446                 /*
447                  * reset video mode properties to prevent garbage being displayed on 'mode' device.
448                  */
449                 fb_info->info->mode = (struct fb_videomode*) NULL;
450
451                 tegra_dc_set_mode(fb_info->win->dc, &mode);
452                 mutex_unlock(&fb_info->info->lock);
453                 return;
454         }
455
456         memcpy(&fb_info->info->monspecs, specs,
457                sizeof(fb_info->info->monspecs));
458         fb_info->info->mode = specs->modedb;
459
460         for (i = 0; i < specs->modedb_len; i++) {
461                 if (mode_filter) {
462                         if (mode_filter(fb_info->win->dc, &specs->modedb[i]))
463                                 fb_add_videomode(&specs->modedb[i],
464                                                  &fb_info->info->modelist);
465                 } else {
466                         fb_add_videomode(&specs->modedb[i],
467                                          &fb_info->info->modelist);
468                 }
469         }
470
471         event.info = fb_info->info;
472         fb_notifier_call_chain(FB_EVENT_NEW_MODELIST, &event);
473         mutex_unlock(&fb_info->info->lock);
474 }
475
476 struct tegra_fb_info *tegra_fb_register(struct nvhost_device *ndev,
477                                         struct tegra_dc *dc,
478                                         struct tegra_fb_data *fb_data,
479                                         struct resource *fb_mem)
480 {
481         struct tegra_dc_win *win;
482         struct fb_info *info;
483         struct tegra_fb_info *tegra_fb;
484         void __iomem *fb_base = NULL;
485         unsigned long fb_size = 0;
486         unsigned long fb_phys = 0;
487         int ret = 0;
488         unsigned stride;
489
490         win = tegra_dc_get_window(dc, fb_data->win);
491         if (!win) {
492                 dev_err(&ndev->dev, "dc does not have a window at index %d\n",
493                         fb_data->win);
494                 return ERR_PTR(-ENOENT);
495         }
496
497         info = framebuffer_alloc(sizeof(struct tegra_fb_info), &ndev->dev);
498         if (!info) {
499                 ret = -ENOMEM;
500                 goto err;
501         }
502
503         tegra_fb = info->par;
504         tegra_fb->win = win;
505         tegra_fb->ndev = ndev;
506         tegra_fb->fb_mem = fb_mem;
507         tegra_fb->xres = fb_data->xres;
508         tegra_fb->yres = fb_data->yres;
509
510         if (fb_mem) {
511                 fb_size = resource_size(fb_mem);
512                 fb_phys = fb_mem->start;
513                 fb_base = ioremap_nocache(fb_phys, fb_size);
514                 if (!fb_base) {
515                         dev_err(&ndev->dev, "fb can't be mapped\n");
516                         ret = -EBUSY;
517                         goto err_free;
518                 }
519                 tegra_fb->valid = true;
520         }
521
522         info->fix.line_length = fb_data->xres * fb_data->bits_per_pixel / 8;
523
524         stride = tegra_dc_get_stride(dc, 0);
525         if (!stride) /* default to pad the stride */
526                 stride = round_up(info->fix.line_length,
527                         TEGRA_LINEAR_PITCH_ALIGNMENT);
528
529         info->fbops = &tegra_fb_ops;
530         info->pseudo_palette = pseudo_palette;
531         info->screen_base = fb_base;
532         info->screen_size = fb_size;
533
534         strlcpy(info->fix.id, "tegra_fb", sizeof(info->fix.id));
535         info->fix.type          = FB_TYPE_PACKED_PIXELS;
536         info->fix.visual        = FB_VISUAL_TRUECOLOR;
537         info->fix.xpanstep      = 1;
538         info->fix.ypanstep      = 1;
539         info->fix.accel         = FB_ACCEL_NONE;
540         info->fix.smem_start    = fb_phys;
541         info->fix.smem_len      = fb_size;
542         info->fix.line_length = stride;
543
544         info->var.xres                  = fb_data->xres;
545         info->var.yres                  = fb_data->yres;
546         info->var.xres_virtual          = fb_data->xres;
547         info->var.yres_virtual          = fb_data->yres * 2;
548         info->var.bits_per_pixel        = fb_data->bits_per_pixel;
549         info->var.activate              = FB_ACTIVATE_VBL;
550         info->var.height                = tegra_dc_get_out_height(dc);
551         info->var.width                 = tegra_dc_get_out_width(dc);
552         info->var.pixclock              = 0;
553         info->var.left_margin           = 0;
554         info->var.right_margin          = 0;
555         info->var.upper_margin          = 0;
556         info->var.lower_margin          = 0;
557         info->var.hsync_len             = 0;
558         info->var.vsync_len             = 0;
559         info->var.vmode                 = FB_VMODE_NONINTERLACED;
560
561         win->x.full = dfixed_const(0);
562         win->y.full = dfixed_const(0);
563         win->w.full = dfixed_const(fb_data->xres);
564         win->h.full = dfixed_const(fb_data->yres);
565         /* TODO: set to output res dc */
566         win->out_x = 0;
567         win->out_y = 0;
568         win->out_w = fb_data->xres;
569         win->out_h = fb_data->yres;
570         win->z = 0;
571         win->phys_addr = fb_phys;
572         win->virt_addr = fb_base;
573         win->phys_addr_u = 0;
574         win->phys_addr_v = 0;
575         win->stride = info->fix.line_length;
576         win->stride_uv = 0;
577         win->flags = TEGRA_WIN_FLAG_ENABLED;
578
579         if (fb_mem)
580                 tegra_fb_set_par(info);
581
582         if (register_framebuffer(info)) {
583                 dev_err(&ndev->dev, "failed to register framebuffer\n");
584                 ret = -ENODEV;
585                 goto err_iounmap_fb;
586         }
587
588         tegra_fb->info = info;
589
590         dev_info(&ndev->dev, "probed\n");
591
592         if (fb_data->flags & TEGRA_FB_FLIP_ON_PROBE) {
593                 tegra_dc_update_windows(&tegra_fb->win, 1);
594                 tegra_dc_sync_windows(&tegra_fb->win, 1);
595         }
596
597         if (dc->mode.pclk > 1000) {
598                 struct tegra_dc_mode *mode = &dc->mode;
599                 struct fb_videomode vmode;
600
601                 if (dc->out->flags & TEGRA_DC_OUT_ONE_SHOT_MODE)
602                         info->var.pixclock = KHZ2PICOS(mode->rated_pclk / 1000);
603                 else
604                         info->var.pixclock = KHZ2PICOS(mode->pclk / 1000);
605                 info->var.left_margin = mode->h_back_porch;
606                 info->var.right_margin = mode->h_front_porch;
607                 info->var.upper_margin = mode->v_back_porch;
608                 info->var.lower_margin = mode->v_front_porch;
609                 info->var.hsync_len = mode->h_sync_width;
610                 info->var.vsync_len = mode->v_sync_width;
611
612                 /* Keep info->var consistent with info->modelist. */
613                 fb_var_to_videomode(&vmode, &info->var);
614                 fb_add_videomode(&vmode, &info->modelist);
615         }
616
617         return tegra_fb;
618
619 err_iounmap_fb:
620         if (fb_base)
621                 iounmap(fb_base);
622 err_free:
623         framebuffer_release(info);
624 err:
625         return ERR_PTR(ret);
626 }
627
628 void tegra_fb_unregister(struct tegra_fb_info *fb_info)
629 {
630         struct fb_info *info = fb_info->info;
631
632         unregister_framebuffer(info);
633
634         iounmap(info->screen_base);
635         framebuffer_release(info);
636 }