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