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