video: tegra: clean-up warnings and code style
[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 #if defined(CONFIG_FRAMEBUFFER_CONSOLE)
233 static void tegra_fb_flip_win(struct tegra_fb_info *tegra_fb)
234 {
235         struct tegra_dc_win *win = tegra_fb->win;
236         struct fb_info *info = tegra_fb->info;
237
238         win->x.full = dfixed_const(0);
239         win->y.full = dfixed_const(0);
240         win->w.full = dfixed_const(tegra_fb->xres);
241         win->h.full = dfixed_const(tegra_fb->yres);
242
243         /* TODO: set to output res dc */
244         win->out_x = 0;
245         win->out_y = 0;
246         win->out_w = tegra_fb->xres;
247         win->out_h = tegra_fb->yres;
248         win->z = 0;
249         win->phys_addr = info->fix.smem_start +
250                 (info->var.yoffset * info->fix.line_length) +
251                 (info->var.xoffset * (info->var.bits_per_pixel / 8));
252         win->virt_addr = info->screen_base;
253
254         win->phys_addr_u = 0;
255         win->phys_addr_v = 0;
256         win->stride = info->fix.line_length;
257         win->stride_uv = 0;
258
259         switch (info->var.bits_per_pixel) {
260         default:
261                 WARN_ON(1);
262                 /* fall through */
263         case 32:
264                 tegra_fb->win->fmt = TEGRA_WIN_FMT_R8G8B8A8;
265                 break;
266         case 16:
267                 tegra_fb->win->fmt = TEGRA_WIN_FMT_B5G6R5;
268                 break;
269         }
270         win->flags = TEGRA_WIN_FLAG_ENABLED;
271
272         tegra_dc_update_windows(&tegra_fb->win, 1);
273         tegra_dc_sync_windows(&tegra_fb->win, 1);
274 }
275 #endif
276
277 static int tegra_fb_blank(int blank, struct fb_info *info)
278 {
279         struct tegra_fb_info *tegra_fb = info->par;
280
281         switch (blank) {
282         case FB_BLANK_UNBLANK:
283                 dev_dbg(&tegra_fb->ndev->dev, "unblank\n");
284                 tegra_fb->win->flags = TEGRA_WIN_FLAG_ENABLED;
285                 tegra_dc_enable(tegra_fb->win->dc);
286 #if defined(CONFIG_FRAMEBUFFER_CONSOLE)
287                 /*
288                 * TODO:
289                 * This is a work around to provide an unblanking flip
290                 * to dc driver, required to display fb-console after
291                 * a blank event,and needs to be replaced by a proper
292                 * unblanking mechanism
293                 */
294                 tegra_fb_flip_win(tegra_fb);
295 #endif
296                 return 0;
297
298         case FB_BLANK_NORMAL:
299                 dev_dbg(&tegra_fb->ndev->dev, "blank - normal\n");
300                 tegra_dc_blank(tegra_fb->win->dc);
301                 return 0;
302
303         case FB_BLANK_VSYNC_SUSPEND:
304         case FB_BLANK_HSYNC_SUSPEND:
305         case FB_BLANK_POWERDOWN:
306                 dev_dbg(&tegra_fb->ndev->dev, "blank - powerdown\n");
307                 tegra_dc_disable(tegra_fb->win->dc);
308                 return 0;
309
310         default:
311                 return -ENOTTY;
312         }
313 }
314
315 static int tegra_fb_pan_display(struct fb_var_screeninfo *var,
316                                 struct fb_info *info)
317 {
318         struct tegra_fb_info *tegra_fb = info->par;
319         char __iomem *flush_start;
320         char __iomem *flush_end;
321         u32 addr;
322
323         if (!tegra_fb->win->cur_handle) {
324                 flush_start = info->screen_base + (var->yoffset * info->fix.line_length);
325                 flush_end = flush_start + (var->yres * info->fix.line_length);
326
327                 info->var.xoffset = var->xoffset;
328                 info->var.yoffset = var->yoffset;
329
330                 addr = info->fix.smem_start + (var->yoffset * info->fix.line_length) +
331                         (var->xoffset * (var->bits_per_pixel/8));
332
333                 tegra_fb->win->phys_addr = addr;
334                 /* TODO: update virt_addr */
335
336                 tegra_dc_update_windows(&tegra_fb->win, 1);
337                 tegra_dc_sync_windows(&tegra_fb->win, 1);
338         }
339
340         return 0;
341 }
342
343 static void tegra_fb_fillrect(struct fb_info *info,
344                               const struct fb_fillrect *rect)
345 {
346         cfb_fillrect(info, rect);
347 }
348
349 static void tegra_fb_copyarea(struct fb_info *info,
350                               const struct fb_copyarea *region)
351 {
352         cfb_copyarea(info, region);
353 }
354
355 static void tegra_fb_imageblit(struct fb_info *info,
356                                const struct fb_image *image)
357 {
358         cfb_imageblit(info, image);
359 }
360
361 static int tegra_fb_ioctl(struct fb_info *info, unsigned int cmd, unsigned long arg)
362 {
363         struct tegra_fb_modedb modedb;
364         struct fb_modelist *modelist;
365         int i;
366
367         switch (cmd) {
368         case FBIO_TEGRA_GET_MODEDB:
369                 if (copy_from_user(&modedb, (void __user *)arg, sizeof(modedb)))
370                         return -EFAULT;
371
372                 i = 0;
373                 list_for_each_entry(modelist, &info->modelist, list) {
374                         struct fb_var_screeninfo var;
375
376                         if (i >= modedb.modedb_len)
377                                 break;
378
379                         /* fb_videomode_to_var doesn't fill out all the members
380                            of fb_var_screeninfo */
381                         memset(&var, 0x0, sizeof(var));
382
383                         fb_videomode_to_var(&var, &modelist->mode);
384
385                         if (copy_to_user((void __user *)&modedb.modedb[i],
386                                          &var, sizeof(var)))
387                                 return -EFAULT;
388                         i++;
389
390                         if (var.vmode & FB_VMODE_STEREO_MASK) {
391                                 if (i >= modedb.modedb_len)
392                                         break;
393                                 var.vmode &= ~FB_VMODE_STEREO_MASK;
394                                 if (copy_to_user(
395                                         (void __user *)&modedb.modedb[i],
396                                          &var, sizeof(var)))
397                                         return -EFAULT;
398                                 i++;
399                         }
400                 }
401                 modedb.modedb_len = i;
402
403                 if (copy_to_user((void __user *)arg, &modedb, sizeof(modedb)))
404                         return -EFAULT;
405                 break;
406
407         default:
408                 return -ENOTTY;
409         }
410
411         return 0;
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
459         for (i = 0; i < specs->modedb_len; i++) {
460                 if (mode_filter) {
461                         if (mode_filter(fb_info->win->dc, &specs->modedb[i]))
462                                 fb_add_videomode(&specs->modedb[i],
463                                                  &fb_info->info->modelist);
464                 } else {
465                         fb_add_videomode(&specs->modedb[i],
466                                          &fb_info->info->modelist);
467                 }
468         }
469
470         event.info = fb_info->info;
471         fb_notifier_call_chain(FB_EVENT_NEW_MODELIST, &event);
472         mutex_unlock(&fb_info->info->lock);
473 }
474
475 struct tegra_fb_info *tegra_fb_register(struct nvhost_device *ndev,
476                                         struct tegra_dc *dc,
477                                         struct tegra_fb_data *fb_data,
478                                         struct resource *fb_mem)
479 {
480         struct tegra_dc_win *win;
481         struct fb_info *info;
482         struct tegra_fb_info *tegra_fb;
483         void __iomem *fb_base = NULL;
484         unsigned long fb_size = 0;
485         unsigned long fb_phys = 0;
486         int ret = 0;
487
488         win = tegra_dc_get_window(dc, fb_data->win);
489         if (!win) {
490                 dev_err(&ndev->dev, "dc does not have a window at index %d\n",
491                         fb_data->win);
492                 return ERR_PTR(-ENOENT);
493         }
494
495         info = framebuffer_alloc(sizeof(struct tegra_fb_info), &ndev->dev);
496         if (!info) {
497                 ret = -ENOMEM;
498                 goto err;
499         }
500
501         tegra_fb = info->par;
502         tegra_fb->win = win;
503         tegra_fb->ndev = ndev;
504         tegra_fb->fb_mem = fb_mem;
505         tegra_fb->xres = fb_data->xres;
506         tegra_fb->yres = fb_data->yres;
507
508         if (fb_mem) {
509                 fb_size = resource_size(fb_mem);
510                 fb_phys = fb_mem->start;
511                 fb_base = ioremap_nocache(fb_phys, fb_size);
512                 if (!fb_base) {
513                         dev_err(&ndev->dev, "fb can't be mapped\n");
514                         ret = -EBUSY;
515                         goto err_free;
516                 }
517                 tegra_fb->valid = true;
518         }
519
520         info->fbops = &tegra_fb_ops;
521         info->pseudo_palette = pseudo_palette;
522         info->screen_base = fb_base;
523         info->screen_size = fb_size;
524
525         strlcpy(info->fix.id, "tegra_fb", sizeof(info->fix.id));
526         info->fix.type          = FB_TYPE_PACKED_PIXELS;
527         info->fix.visual        = FB_VISUAL_TRUECOLOR;
528         info->fix.xpanstep      = 1;
529         info->fix.ypanstep      = 1;
530         info->fix.accel         = FB_ACCEL_NONE;
531         info->fix.smem_start    = fb_phys;
532         info->fix.smem_len      = fb_size;
533         info->fix.line_length = fb_data->xres * fb_data->bits_per_pixel / 8;
534         /* Pad the stride to 16-byte boundary. */
535         info->fix.line_length = round_up(info->fix.line_length,
536                                         TEGRA_LINEAR_PITCH_ALIGNMENT);
537
538         info->var.xres                  = fb_data->xres;
539         info->var.yres                  = fb_data->yres;
540         info->var.xres_virtual          = fb_data->xres;
541         info->var.yres_virtual          = fb_data->yres * 2;
542         info->var.bits_per_pixel        = fb_data->bits_per_pixel;
543         info->var.activate              = FB_ACTIVATE_VBL;
544         info->var.height                = tegra_dc_get_out_height(dc);
545         info->var.width                 = tegra_dc_get_out_width(dc);
546         info->var.pixclock              = 0;
547         info->var.left_margin           = 0;
548         info->var.right_margin          = 0;
549         info->var.upper_margin          = 0;
550         info->var.lower_margin          = 0;
551         info->var.hsync_len             = 0;
552         info->var.vsync_len             = 0;
553         info->var.vmode                 = FB_VMODE_NONINTERLACED;
554
555         win->x.full = dfixed_const(0);
556         win->y.full = dfixed_const(0);
557         win->w.full = dfixed_const(fb_data->xres);
558         win->h.full = dfixed_const(fb_data->yres);
559         /* TODO: set to output res dc */
560         win->out_x = 0;
561         win->out_y = 0;
562         win->out_w = fb_data->xres;
563         win->out_h = fb_data->yres;
564         win->z = 0;
565         win->phys_addr = fb_phys;
566         win->virt_addr = fb_base;
567         win->phys_addr_u = 0;
568         win->phys_addr_v = 0;
569         win->stride = info->fix.line_length;
570         win->stride_uv = 0;
571         win->flags = TEGRA_WIN_FLAG_ENABLED;
572
573         if (fb_mem)
574                 tegra_fb_set_par(info);
575
576         if (register_framebuffer(info)) {
577                 dev_err(&ndev->dev, "failed to register framebuffer\n");
578                 ret = -ENODEV;
579                 goto err_iounmap_fb;
580         }
581
582         tegra_fb->info = info;
583
584         dev_info(&ndev->dev, "probed\n");
585
586         if (fb_data->flags & TEGRA_FB_FLIP_ON_PROBE) {
587                 tegra_dc_update_windows(&tegra_fb->win, 1);
588                 tegra_dc_sync_windows(&tegra_fb->win, 1);
589         }
590
591         if (dc->mode.pclk > 1000) {
592                 struct tegra_dc_mode *mode = &dc->mode;
593
594                 if (dc->out->flags & TEGRA_DC_OUT_ONE_SHOT_MODE)
595                         info->var.pixclock = KHZ2PICOS(mode->rated_pclk / 1000);
596                 else
597                         info->var.pixclock = KHZ2PICOS(mode->pclk / 1000);
598                 info->var.left_margin = mode->h_back_porch;
599                 info->var.right_margin = mode->h_front_porch;
600                 info->var.upper_margin = mode->v_back_porch;
601                 info->var.lower_margin = mode->v_front_porch;
602                 info->var.hsync_len = mode->h_sync_width;
603                 info->var.vsync_len = mode->v_sync_width;
604         }
605
606         return tegra_fb;
607
608 err_iounmap_fb:
609         if (fb_base)
610                 iounmap(fb_base);
611 err_free:
612         framebuffer_release(info);
613 err:
614         return ERR_PTR(ret);
615 }
616
617 void tegra_fb_unregister(struct tegra_fb_info *fb_info)
618 {
619         struct fb_info *info = fb_info->info;
620
621         unregister_framebuffer(info);
622
623         iounmap(info->screen_base);
624         framebuffer_release(info);
625 }