video: tegra: set fb line_length to physical pitch
[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                                         FB_VMODE_STEREO_FRAME_PACK);
138
139                 tegra_dc_set_fb_mode(tegra_fb->win->dc, info->mode, stereo);
140
141                 tegra_fb->win->w.full = dfixed_const(info->mode->xres);
142                 tegra_fb->win->h.full = dfixed_const(info->mode->yres);
143                 tegra_fb->win->out_w = info->mode->xres;
144                 tegra_fb->win->out_h = info->mode->yres;
145         }
146         return 0;
147 }
148
149 static int tegra_fb_setcolreg(unsigned regno, unsigned red, unsigned green,
150         unsigned blue, unsigned transp, struct fb_info *info)
151 {
152         struct fb_var_screeninfo *var = &info->var;
153
154         if (info->fix.visual == FB_VISUAL_TRUECOLOR ||
155             info->fix.visual == FB_VISUAL_DIRECTCOLOR) {
156                 u32 v;
157
158                 if (regno >= 16)
159                         return -EINVAL;
160
161                 red = (red >> (16 - info->var.red.length));
162                 green = (green >> (16 - info->var.green.length));
163                 blue = (blue >> (16 - info->var.blue.length));
164
165                 v = (red << var->red.offset) |
166                         (green << var->green.offset) |
167                         (blue << var->blue.offset);
168
169                 ((u32 *)info->pseudo_palette)[regno] = v;
170         }
171
172         return 0;
173 }
174
175 static int tegra_fb_blank(int blank, struct fb_info *info)
176 {
177         struct tegra_fb_info *tegra_fb = info->par;
178
179         switch (blank) {
180         case FB_BLANK_UNBLANK:
181                 dev_dbg(&tegra_fb->ndev->dev, "unblank\n");
182                 tegra_dc_enable(tegra_fb->win->dc);
183                 return 0;
184
185         case FB_BLANK_NORMAL:
186                 dev_dbg(&tegra_fb->ndev->dev, "blank - normal\n");
187                 tegra_dc_blank(tegra_fb->win->dc);
188                 return 0;
189
190         case FB_BLANK_VSYNC_SUSPEND:
191         case FB_BLANK_HSYNC_SUSPEND:
192         case FB_BLANK_POWERDOWN:
193                 dev_dbg(&tegra_fb->ndev->dev, "blank - powerdown\n");
194                 tegra_dc_disable(tegra_fb->win->dc);
195                 return 0;
196
197         default:
198                 return -ENOTTY;
199         }
200 }
201
202 static int tegra_fb_pan_display(struct fb_var_screeninfo *var,
203                                 struct fb_info *info)
204 {
205         struct tegra_fb_info *tegra_fb = info->par;
206         char __iomem *flush_start;
207         char __iomem *flush_end;
208         u32 addr;
209
210         if (!tegra_fb->win->cur_handle) {
211                 flush_start = info->screen_base + (var->yoffset * info->fix.line_length);
212                 flush_end = flush_start + (var->yres * info->fix.line_length);
213
214                 info->var.xoffset = var->xoffset;
215                 info->var.yoffset = var->yoffset;
216
217                 addr = info->fix.smem_start + (var->yoffset * info->fix.line_length) +
218                         (var->xoffset * (var->bits_per_pixel/8));
219
220                 tegra_fb->win->phys_addr = addr;
221                 /* TODO: update virt_addr */
222
223                 tegra_dc_update_windows(&tegra_fb->win, 1);
224                 tegra_dc_sync_windows(&tegra_fb->win, 1);
225         }
226
227         return 0;
228 }
229
230 static void tegra_fb_fillrect(struct fb_info *info,
231                               const struct fb_fillrect *rect)
232 {
233         cfb_fillrect(info, rect);
234 }
235
236 static void tegra_fb_copyarea(struct fb_info *info,
237                               const struct fb_copyarea *region)
238 {
239         cfb_copyarea(info, region);
240 }
241
242 static void tegra_fb_imageblit(struct fb_info *info,
243                                const struct fb_image *image)
244 {
245         cfb_imageblit(info, image);
246 }
247
248 static int tegra_fb_ioctl(struct fb_info *info, unsigned int cmd, unsigned long arg)
249 {
250         struct tegra_fb_modedb modedb;
251         struct fb_modelist *modelist;
252         int i;
253
254         switch (cmd) {
255         case FBIO_TEGRA_GET_MODEDB:
256                 if (copy_from_user(&modedb, (void __user *)arg, sizeof(modedb)))
257                         return -EFAULT;
258
259                 i = 0;
260                 list_for_each_entry(modelist, &info->modelist, list) {
261                         struct fb_var_screeninfo var;
262
263                         if (i >= modedb.modedb_len)
264                                 break;
265
266                         /* fb_videomode_to_var doesn't fill out all the members
267                            of fb_var_screeninfo */
268                         memset(&var, 0x0, sizeof(var));
269
270                         fb_videomode_to_var(&var, &modelist->mode);
271
272                         if (copy_to_user((void __user *)&modedb.modedb[i],
273                                          &var, sizeof(var)))
274                                 return -EFAULT;
275                         i++;
276
277                         if (var.vmode & FB_VMODE_STEREO_MASK) {
278                                 if (i >= modedb.modedb_len)
279                                         break;
280                                 var.vmode &= ~FB_VMODE_STEREO_MASK;
281                                 if (copy_to_user(
282                                         (void __user *)&modedb.modedb[i],
283                                          &var, sizeof(var)))
284                                         return -EFAULT;
285                                 i++;
286                         }
287                 }
288                 modedb.modedb_len = i;
289
290                 if (copy_to_user((void __user *)arg, &modedb, sizeof(modedb)))
291                         return -EFAULT;
292                 break;
293
294         default:
295                 return -ENOTTY;
296         }
297
298         return 0;
299 }
300
301 static struct fb_ops tegra_fb_ops = {
302         .owner = THIS_MODULE,
303         .fb_check_var = tegra_fb_check_var,
304         .fb_set_par = tegra_fb_set_par,
305         .fb_setcolreg = tegra_fb_setcolreg,
306         .fb_blank = tegra_fb_blank,
307         .fb_pan_display = tegra_fb_pan_display,
308         .fb_fillrect = tegra_fb_fillrect,
309         .fb_copyarea = tegra_fb_copyarea,
310         .fb_imageblit = tegra_fb_imageblit,
311         .fb_ioctl = tegra_fb_ioctl,
312 };
313
314 void tegra_fb_update_monspecs(struct tegra_fb_info *fb_info,
315                               struct fb_monspecs *specs,
316                               bool (*mode_filter)(const struct tegra_dc *dc,
317                                                   struct fb_videomode *mode))
318 {
319         struct fb_event event;
320         int i;
321
322         mutex_lock(&fb_info->info->lock);
323         fb_destroy_modedb(fb_info->info->monspecs.modedb);
324
325         fb_destroy_modelist(&fb_info->info->modelist);
326
327         if (specs == NULL) {
328                 struct tegra_dc_mode mode;
329                 memset(&fb_info->info->monspecs, 0x0,
330                        sizeof(fb_info->info->monspecs));
331                 memset(&mode, 0x0, sizeof(mode));
332                 tegra_dc_set_mode(fb_info->win->dc, &mode);
333                 mutex_unlock(&fb_info->info->lock);
334                 return;
335         }
336
337         memcpy(&fb_info->info->monspecs, specs,
338                sizeof(fb_info->info->monspecs));
339
340         for (i = 0; i < specs->modedb_len; i++) {
341                 if (mode_filter) {
342                         if (mode_filter(fb_info->win->dc, &specs->modedb[i]))
343                                 fb_add_videomode(&specs->modedb[i],
344                                                  &fb_info->info->modelist);
345                 } else {
346                         fb_add_videomode(&specs->modedb[i],
347                                          &fb_info->info->modelist);
348                 }
349         }
350
351         event.info = fb_info->info;
352         fb_notifier_call_chain(FB_EVENT_NEW_MODELIST, &event);
353         mutex_unlock(&fb_info->info->lock);
354 }
355
356 struct tegra_fb_info *tegra_fb_register(struct nvhost_device *ndev,
357                                         struct tegra_dc *dc,
358                                         struct tegra_fb_data *fb_data,
359                                         struct resource *fb_mem)
360 {
361         struct tegra_dc_win *win;
362         struct fb_info *info;
363         struct tegra_fb_info *tegra_fb;
364         void __iomem *fb_base = NULL;
365         unsigned long fb_size = 0;
366         unsigned long fb_phys = 0;
367         int ret = 0;
368
369         win = tegra_dc_get_window(dc, fb_data->win);
370         if (!win) {
371                 dev_err(&ndev->dev, "dc does not have a window at index %d\n",
372                         fb_data->win);
373                 return ERR_PTR(-ENOENT);
374         }
375
376         info = framebuffer_alloc(sizeof(struct tegra_fb_info), &ndev->dev);
377         if (!info) {
378                 ret = -ENOMEM;
379                 goto err;
380         }
381
382         tegra_fb = info->par;
383         tegra_fb->win = win;
384         tegra_fb->ndev = ndev;
385         tegra_fb->fb_mem = fb_mem;
386         tegra_fb->xres = fb_data->xres;
387         tegra_fb->yres = fb_data->yres;
388
389         if (fb_mem) {
390                 fb_size = resource_size(fb_mem);
391                 fb_phys = fb_mem->start;
392                 fb_base = ioremap_nocache(fb_phys, fb_size);
393                 if (!fb_base) {
394                         dev_err(&ndev->dev, "fb can't be mapped\n");
395                         ret = -EBUSY;
396                         goto err_free;
397                 }
398                 tegra_fb->valid = true;
399         }
400
401         info->fbops = &tegra_fb_ops;
402         info->pseudo_palette = pseudo_palette;
403         info->screen_base = fb_base;
404         info->screen_size = fb_size;
405
406         strlcpy(info->fix.id, "tegra_fb", sizeof(info->fix.id));
407         info->fix.type          = FB_TYPE_PACKED_PIXELS;
408         info->fix.visual        = FB_VISUAL_TRUECOLOR;
409         info->fix.xpanstep      = 1;
410         info->fix.ypanstep      = 1;
411         info->fix.accel         = FB_ACCEL_NONE;
412         info->fix.smem_start    = fb_phys;
413         info->fix.smem_len      = fb_size;
414         info->fix.line_length = fb_data->xres * fb_data->bits_per_pixel / 8;
415         /* Pad the stride to 16-byte boundary. */
416         info->fix.line_length = round_up(info->fix.line_length,
417                                         TEGRA_LINEAR_PITCH_ALIGNMENT);
418
419         info->var.xres                  = fb_data->xres;
420         info->var.yres                  = fb_data->yres;
421         info->var.xres_virtual          = fb_data->xres;
422         info->var.yres_virtual          = fb_data->yres * 2;
423         info->var.bits_per_pixel        = fb_data->bits_per_pixel;
424         info->var.activate              = FB_ACTIVATE_VBL;
425         info->var.height                = tegra_dc_get_out_height(dc);
426         info->var.width                 = tegra_dc_get_out_width(dc);
427         info->var.pixclock              = 0;
428         info->var.left_margin           = 0;
429         info->var.right_margin          = 0;
430         info->var.upper_margin          = 0;
431         info->var.lower_margin          = 0;
432         info->var.hsync_len             = 0;
433         info->var.vsync_len             = 0;
434         info->var.vmode                 = FB_VMODE_NONINTERLACED;
435
436         win->x.full = dfixed_const(0);
437         win->y.full = dfixed_const(0);
438         win->w.full = dfixed_const(fb_data->xres);
439         win->h.full = dfixed_const(fb_data->yres);
440         /* TODO: set to output res dc */
441         win->out_x = 0;
442         win->out_y = 0;
443         win->out_w = fb_data->xres;
444         win->out_h = fb_data->yres;
445         win->z = 0;
446         win->phys_addr = fb_phys;
447         win->virt_addr = fb_base;
448         win->phys_addr_u = 0;
449         win->phys_addr_v = 0;
450         win->stride = info->fix.line_length;
451         win->stride_uv = 0;
452         win->flags = TEGRA_WIN_FLAG_ENABLED;
453
454         if (fb_mem)
455                 tegra_fb_set_par(info);
456
457         if (register_framebuffer(info)) {
458                 dev_err(&ndev->dev, "failed to register framebuffer\n");
459                 ret = -ENODEV;
460                 goto err_iounmap_fb;
461         }
462
463         tegra_fb->info = info;
464
465         dev_info(&ndev->dev, "probed\n");
466
467         if (fb_data->flags & TEGRA_FB_FLIP_ON_PROBE) {
468                 tegra_dc_update_windows(&tegra_fb->win, 1);
469                 tegra_dc_sync_windows(&tegra_fb->win, 1);
470         }
471
472         return tegra_fb;
473
474 err_iounmap_fb:
475         if (fb_base)
476                 iounmap(fb_base);
477 err_free:
478         framebuffer_release(info);
479 err:
480         return ERR_PTR(ret);
481 }
482
483 void tegra_fb_unregister(struct tegra_fb_info *fb_info)
484 {
485         struct fb_info *info = fb_info->info;
486
487         unregister_framebuffer(info);
488
489         iounmap(info->screen_base);
490         framebuffer_release(info);
491 }