Merge commit 'v2.6.28-rc8' into x86/mm
[linux-2.6] / drivers / video / omap / omapfb_main.c
1 /*
2  * Framebuffer driver for TI OMAP boards
3  *
4  * Copyright (C) 2004 Nokia Corporation
5  * Author: Imre Deak <imre.deak@nokia.com>
6  *
7  * Acknowledgements:
8  *   Alex McMains <aam@ridgerun.com>       - Original driver
9  *   Juha Yrjola <juha.yrjola@nokia.com>   - Original driver and improvements
10  *   Dirk Behme <dirk.behme@de.bosch.com>  - changes for 2.6 kernel API
11  *   Texas Instruments                     - H3 support
12  *
13  * This program is free software; you can redistribute it and/or modify it
14  * under the terms of the GNU General Public License as published by the
15  * Free Software Foundation; either version 2 of the License, or (at your
16  * option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful, but
19  * WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21  * General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License along
24  * with this program; if not, write to the Free Software Foundation, Inc.,
25  * 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26  */
27 #include <linux/platform_device.h>
28 #include <linux/mm.h>
29 #include <linux/uaccess.h>
30
31 #include <mach/dma.h>
32 #include <mach/omapfb.h>
33
34 #include "lcdc.h"
35 #include "dispc.h"
36
37 #define MODULE_NAME     "omapfb"
38
39 static unsigned int     def_accel;
40 static unsigned long    def_vram[OMAPFB_PLANE_NUM];
41 static unsigned int     def_vram_cnt;
42 static unsigned long    def_vxres;
43 static unsigned long    def_vyres;
44 static unsigned int     def_rotate;
45 static unsigned int     def_mirror;
46
47 #ifdef CONFIG_FB_OMAP_MANUAL_UPDATE
48 static int              manual_update = 1;
49 #else
50 static int              manual_update;
51 #endif
52
53 static struct platform_device   *fbdev_pdev;
54 static struct lcd_panel         *fbdev_panel;
55 static struct omapfb_device     *omapfb_dev;
56
57 struct caps_table_struct {
58         unsigned long flag;
59         const char *name;
60 };
61
62 static struct caps_table_struct ctrl_caps[] = {
63         { OMAPFB_CAPS_MANUAL_UPDATE,  "manual update" },
64         { OMAPFB_CAPS_TEARSYNC,       "tearing synchronization" },
65         { OMAPFB_CAPS_PLANE_RELOCATE_MEM, "relocate plane memory" },
66         { OMAPFB_CAPS_PLANE_SCALE,    "scale plane" },
67         { OMAPFB_CAPS_WINDOW_PIXEL_DOUBLE, "pixel double window" },
68         { OMAPFB_CAPS_WINDOW_SCALE,   "scale window" },
69         { OMAPFB_CAPS_WINDOW_OVERLAY, "overlay window" },
70         { OMAPFB_CAPS_SET_BACKLIGHT,  "backlight setting" },
71 };
72
73 static struct caps_table_struct color_caps[] = {
74         { 1 << OMAPFB_COLOR_RGB565,     "RGB565", },
75         { 1 << OMAPFB_COLOR_YUV422,     "YUV422", },
76         { 1 << OMAPFB_COLOR_YUV420,     "YUV420", },
77         { 1 << OMAPFB_COLOR_CLUT_8BPP,  "CLUT8", },
78         { 1 << OMAPFB_COLOR_CLUT_4BPP,  "CLUT4", },
79         { 1 << OMAPFB_COLOR_CLUT_2BPP,  "CLUT2", },
80         { 1 << OMAPFB_COLOR_CLUT_1BPP,  "CLUT1", },
81         { 1 << OMAPFB_COLOR_RGB444,     "RGB444", },
82         { 1 << OMAPFB_COLOR_YUY422,     "YUY422", },
83 };
84
85 /*
86  * ---------------------------------------------------------------------------
87  * LCD panel
88  * ---------------------------------------------------------------------------
89  */
90 extern struct lcd_ctrl hwa742_ctrl;
91 extern struct lcd_ctrl blizzard_ctrl;
92
93 static const struct lcd_ctrl *ctrls[] = {
94 #ifdef CONFIG_ARCH_OMAP1
95         &omap1_int_ctrl,
96 #else
97         &omap2_int_ctrl,
98 #endif
99
100 #ifdef CONFIG_FB_OMAP_LCDC_HWA742
101         &hwa742_ctrl,
102 #endif
103 #ifdef CONFIG_FB_OMAP_LCDC_BLIZZARD
104         &blizzard_ctrl,
105 #endif
106 };
107
108 #ifdef CONFIG_FB_OMAP_LCDC_EXTERNAL
109 #ifdef CONFIG_ARCH_OMAP1
110 extern struct lcd_ctrl_extif omap1_ext_if;
111 #else
112 extern struct lcd_ctrl_extif omap2_ext_if;
113 #endif
114 #endif
115
116 static void omapfb_rqueue_lock(struct omapfb_device *fbdev)
117 {
118         mutex_lock(&fbdev->rqueue_mutex);
119 }
120
121 static void omapfb_rqueue_unlock(struct omapfb_device *fbdev)
122 {
123         mutex_unlock(&fbdev->rqueue_mutex);
124 }
125
126 /*
127  * ---------------------------------------------------------------------------
128  * LCD controller and LCD DMA
129  * ---------------------------------------------------------------------------
130  */
131 /* Lookup table to map elem size to elem type. */
132 static const int dma_elem_type[] = {
133         0,
134         OMAP_DMA_DATA_TYPE_S8,
135         OMAP_DMA_DATA_TYPE_S16,
136         0,
137         OMAP_DMA_DATA_TYPE_S32,
138 };
139
140 /*
141  * Allocate resources needed for LCD controller and LCD DMA operations. Video
142  * memory is allocated from system memory according to the virtual display
143  * size, except if a bigger memory size is specified explicitly as a kernel
144  * parameter.
145  */
146 static int ctrl_init(struct omapfb_device *fbdev)
147 {
148         int r;
149         int i;
150
151         /* kernel/module vram parameters override boot tags/board config */
152         if (def_vram_cnt) {
153                 for (i = 0; i < def_vram_cnt; i++)
154                         fbdev->mem_desc.region[i].size =
155                                 PAGE_ALIGN(def_vram[i]);
156                 fbdev->mem_desc.region_cnt = i;
157         } else {
158                 struct omapfb_platform_data *conf;
159
160                 conf = fbdev->dev->platform_data;
161                 fbdev->mem_desc = conf->mem_desc;
162         }
163
164         if (!fbdev->mem_desc.region_cnt) {
165                 struct lcd_panel *panel = fbdev->panel;
166                 int def_size;
167                 int bpp = panel->bpp;
168
169                 /* 12 bpp is packed in 16 bits */
170                 if (bpp == 12)
171                         bpp = 16;
172                 def_size = def_vxres * def_vyres * bpp / 8;
173                 fbdev->mem_desc.region_cnt = 1;
174                 fbdev->mem_desc.region[0].size = PAGE_ALIGN(def_size);
175         }
176         r = fbdev->ctrl->init(fbdev, 0, &fbdev->mem_desc);
177         if (r < 0) {
178                 dev_err(fbdev->dev, "controller initialization failed (%d)\n",
179                         r);
180                 return r;
181         }
182
183 #ifdef DEBUG
184         for (i = 0; i < fbdev->mem_desc.region_cnt; i++) {
185                 dev_dbg(fbdev->dev, "region%d phys %08x virt %p size=%lu\n",
186                          i,
187                          fbdev->mem_desc.region[i].paddr,
188                          fbdev->mem_desc.region[i].vaddr,
189                          fbdev->mem_desc.region[i].size);
190         }
191 #endif
192         return 0;
193 }
194
195 static void ctrl_cleanup(struct omapfb_device *fbdev)
196 {
197         fbdev->ctrl->cleanup();
198 }
199
200 /* Must be called with fbdev->rqueue_mutex held. */
201 static int ctrl_change_mode(struct fb_info *fbi)
202 {
203         int r;
204         unsigned long offset;
205         struct omapfb_plane_struct *plane = fbi->par;
206         struct omapfb_device *fbdev = plane->fbdev;
207         struct fb_var_screeninfo *var = &fbi->var;
208
209         offset = var->yoffset * fbi->fix.line_length +
210                  var->xoffset * var->bits_per_pixel / 8;
211
212         if (fbdev->ctrl->sync)
213                 fbdev->ctrl->sync();
214         r = fbdev->ctrl->setup_plane(plane->idx, plane->info.channel_out,
215                                  offset, var->xres_virtual,
216                                  plane->info.pos_x, plane->info.pos_y,
217                                  var->xres, var->yres, plane->color_mode);
218         if (fbdev->ctrl->set_scale != NULL)
219                 r = fbdev->ctrl->set_scale(plane->idx,
220                                    var->xres, var->yres,
221                                    plane->info.out_width,
222                                    plane->info.out_height);
223
224         return r;
225 }
226
227 /*
228  * ---------------------------------------------------------------------------
229  * fbdev framework callbacks and the ioctl interface
230  * ---------------------------------------------------------------------------
231  */
232 /* Called each time the omapfb device is opened */
233 static int omapfb_open(struct fb_info *info, int user)
234 {
235         return 0;
236 }
237
238 static void omapfb_sync(struct fb_info *info);
239
240 /* Called when the omapfb device is closed. We make sure that any pending
241  * gfx DMA operations are ended, before we return. */
242 static int omapfb_release(struct fb_info *info, int user)
243 {
244         omapfb_sync(info);
245         return 0;
246 }
247
248 /* Store a single color palette entry into a pseudo palette or the hardware
249  * palette if one is available. For now we support only 16bpp and thus store
250  * the entry only to the pseudo palette.
251  */
252 static int _setcolreg(struct fb_info *info, u_int regno, u_int red, u_int green,
253                         u_int blue, u_int transp, int update_hw_pal)
254 {
255         struct omapfb_plane_struct *plane = info->par;
256         struct omapfb_device *fbdev = plane->fbdev;
257         struct fb_var_screeninfo *var = &info->var;
258         int r = 0;
259
260         switch (plane->color_mode) {
261         case OMAPFB_COLOR_YUV422:
262         case OMAPFB_COLOR_YUV420:
263         case OMAPFB_COLOR_YUY422:
264                 r = -EINVAL;
265                 break;
266         case OMAPFB_COLOR_CLUT_8BPP:
267         case OMAPFB_COLOR_CLUT_4BPP:
268         case OMAPFB_COLOR_CLUT_2BPP:
269         case OMAPFB_COLOR_CLUT_1BPP:
270                 if (fbdev->ctrl->setcolreg)
271                         r = fbdev->ctrl->setcolreg(regno, red, green, blue,
272                                                         transp, update_hw_pal);
273                 /* Fallthrough */
274         case OMAPFB_COLOR_RGB565:
275         case OMAPFB_COLOR_RGB444:
276                 if (r != 0)
277                         break;
278
279                 if (regno < 0) {
280                         r = -EINVAL;
281                         break;
282                 }
283
284                 if (regno < 16) {
285                         u16 pal;
286                         pal = ((red >> (16 - var->red.length)) <<
287                                         var->red.offset) |
288                               ((green >> (16 - var->green.length)) <<
289                                         var->green.offset) |
290                               (blue >> (16 - var->blue.length));
291                         ((u32 *)(info->pseudo_palette))[regno] = pal;
292                 }
293                 break;
294         default:
295                 BUG();
296         }
297         return r;
298 }
299
300 static int omapfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
301                             u_int transp, struct fb_info *info)
302 {
303         return _setcolreg(info, regno, red, green, blue, transp, 1);
304 }
305
306 static int omapfb_setcmap(struct fb_cmap *cmap, struct fb_info *info)
307 {
308         int count, index, r;
309         u16 *red, *green, *blue, *transp;
310         u16 trans = 0xffff;
311
312         red     = cmap->red;
313         green   = cmap->green;
314         blue    = cmap->blue;
315         transp  = cmap->transp;
316         index   = cmap->start;
317
318         for (count = 0; count < cmap->len; count++) {
319                 if (transp)
320                         trans = *transp++;
321                 r = _setcolreg(info, index++, *red++, *green++, *blue++, trans,
322                                 count == cmap->len - 1);
323                 if (r != 0)
324                         return r;
325         }
326
327         return 0;
328 }
329
330 static int omapfb_update_full_screen(struct fb_info *fbi);
331
332 static int omapfb_blank(int blank, struct fb_info *fbi)
333 {
334         struct omapfb_plane_struct *plane = fbi->par;
335         struct omapfb_device *fbdev = plane->fbdev;
336         int do_update = 0;
337         int r = 0;
338
339         omapfb_rqueue_lock(fbdev);
340         switch (blank) {
341         case VESA_NO_BLANKING:
342                 if (fbdev->state == OMAPFB_SUSPENDED) {
343                         if (fbdev->ctrl->resume)
344                                 fbdev->ctrl->resume();
345                         fbdev->panel->enable(fbdev->panel);
346                         fbdev->state = OMAPFB_ACTIVE;
347                         if (fbdev->ctrl->get_update_mode() ==
348                                         OMAPFB_MANUAL_UPDATE)
349                                 do_update = 1;
350                 }
351                 break;
352         case VESA_POWERDOWN:
353                 if (fbdev->state == OMAPFB_ACTIVE) {
354                         fbdev->panel->disable(fbdev->panel);
355                         if (fbdev->ctrl->suspend)
356                                 fbdev->ctrl->suspend();
357                         fbdev->state = OMAPFB_SUSPENDED;
358                 }
359                 break;
360         default:
361                 r = -EINVAL;
362         }
363         omapfb_rqueue_unlock(fbdev);
364
365         if (r == 0 && do_update)
366                 r = omapfb_update_full_screen(fbi);
367
368         return r;
369 }
370
371 static void omapfb_sync(struct fb_info *fbi)
372 {
373         struct omapfb_plane_struct *plane = fbi->par;
374         struct omapfb_device *fbdev = plane->fbdev;
375
376         omapfb_rqueue_lock(fbdev);
377         if (fbdev->ctrl->sync)
378                 fbdev->ctrl->sync();
379         omapfb_rqueue_unlock(fbdev);
380 }
381
382 /*
383  * Set fb_info.fix fields and also updates fbdev.
384  * When calling this fb_info.var must be set up already.
385  */
386 static void set_fb_fix(struct fb_info *fbi)
387 {
388         struct fb_fix_screeninfo *fix = &fbi->fix;
389         struct fb_var_screeninfo *var = &fbi->var;
390         struct omapfb_plane_struct *plane = fbi->par;
391         struct omapfb_mem_region *rg;
392         int bpp;
393
394         rg = &plane->fbdev->mem_desc.region[plane->idx];
395         fbi->screen_base        = rg->vaddr;
396         fix->smem_start         = rg->paddr;
397         fix->smem_len           = rg->size;
398
399         fix->type = FB_TYPE_PACKED_PIXELS;
400         bpp = var->bits_per_pixel;
401         if (var->nonstd)
402                 fix->visual = FB_VISUAL_PSEUDOCOLOR;
403         else switch (var->bits_per_pixel) {
404         case 16:
405         case 12:
406                 fix->visual = FB_VISUAL_TRUECOLOR;
407                 /* 12bpp is stored in 16 bits */
408                 bpp = 16;
409                 break;
410         case 1:
411         case 2:
412         case 4:
413         case 8:
414                 fix->visual = FB_VISUAL_PSEUDOCOLOR;
415                 break;
416         }
417         fix->accel              = FB_ACCEL_OMAP1610;
418         fix->line_length        = var->xres_virtual * bpp / 8;
419 }
420
421 static int set_color_mode(struct omapfb_plane_struct *plane,
422                           struct fb_var_screeninfo *var)
423 {
424         switch (var->nonstd) {
425         case 0:
426                 break;
427         case OMAPFB_COLOR_YUV422:
428                 var->bits_per_pixel = 16;
429                 plane->color_mode = var->nonstd;
430                 return 0;
431         case OMAPFB_COLOR_YUV420:
432                 var->bits_per_pixel = 12;
433                 plane->color_mode = var->nonstd;
434                 return 0;
435         case OMAPFB_COLOR_YUY422:
436                 var->bits_per_pixel = 16;
437                 plane->color_mode = var->nonstd;
438                 return 0;
439         default:
440                 return -EINVAL;
441         }
442
443         switch (var->bits_per_pixel) {
444         case 1:
445                 plane->color_mode = OMAPFB_COLOR_CLUT_1BPP;
446                 return 0;
447         case 2:
448                 plane->color_mode = OMAPFB_COLOR_CLUT_2BPP;
449                 return 0;
450         case 4:
451                 plane->color_mode = OMAPFB_COLOR_CLUT_4BPP;
452                 return 0;
453         case 8:
454                 plane->color_mode = OMAPFB_COLOR_CLUT_8BPP;
455                 return 0;
456         case 12:
457                 var->bits_per_pixel = 16;
458                 plane->color_mode = OMAPFB_COLOR_RGB444;
459                 return 0;
460         case 16:
461                 plane->color_mode = OMAPFB_COLOR_RGB565;
462                 return 0;
463         default:
464                 return -EINVAL;
465         }
466 }
467
468 /*
469  * Check the values in var against our capabilities and in case of out of
470  * bound values try to adjust them.
471  */
472 static int set_fb_var(struct fb_info *fbi,
473                       struct fb_var_screeninfo *var)
474 {
475         int             bpp;
476         unsigned long   max_frame_size;
477         unsigned long   line_size;
478         int             xres_min, xres_max;
479         int             yres_min, yres_max;
480         struct omapfb_plane_struct *plane = fbi->par;
481         struct omapfb_device *fbdev = plane->fbdev;
482         struct lcd_panel *panel = fbdev->panel;
483
484         if (set_color_mode(plane, var) < 0)
485                 return -EINVAL;
486
487         bpp = var->bits_per_pixel;
488         if (plane->color_mode == OMAPFB_COLOR_RGB444)
489                 bpp = 16;
490
491         switch (var->rotate) {
492         case 0:
493         case 180:
494                 xres_min = OMAPFB_PLANE_XRES_MIN;
495                 xres_max = panel->x_res;
496                 yres_min = OMAPFB_PLANE_YRES_MIN;
497                 yres_max = panel->y_res;
498                 if (cpu_is_omap15xx()) {
499                         var->xres = panel->x_res;
500                         var->yres = panel->y_res;
501                 }
502                 break;
503         case 90:
504         case 270:
505                 xres_min = OMAPFB_PLANE_YRES_MIN;
506                 xres_max = panel->y_res;
507                 yres_min = OMAPFB_PLANE_XRES_MIN;
508                 yres_max = panel->x_res;
509                 if (cpu_is_omap15xx()) {
510                         var->xres = panel->y_res;
511                         var->yres = panel->x_res;
512                 }
513                 break;
514         default:
515                 return -EINVAL;
516         }
517
518         if (var->xres < xres_min)
519                 var->xres = xres_min;
520         if (var->yres < yres_min)
521                 var->yres = yres_min;
522         if (var->xres > xres_max)
523                 var->xres = xres_max;
524         if (var->yres > yres_max)
525                 var->yres = yres_max;
526
527         if (var->xres_virtual < var->xres)
528                 var->xres_virtual = var->xres;
529         if (var->yres_virtual < var->yres)
530                 var->yres_virtual = var->yres;
531         max_frame_size = fbdev->mem_desc.region[plane->idx].size;
532         line_size = var->xres_virtual * bpp / 8;
533         if (line_size * var->yres_virtual > max_frame_size) {
534                 /* Try to keep yres_virtual first */
535                 line_size = max_frame_size / var->yres_virtual;
536                 var->xres_virtual = line_size * 8 / bpp;
537                 if (var->xres_virtual < var->xres) {
538                         /* Still doesn't fit. Shrink yres_virtual too */
539                         var->xres_virtual = var->xres;
540                         line_size = var->xres * bpp / 8;
541                         var->yres_virtual = max_frame_size / line_size;
542                 }
543                 /* Recheck this, as the virtual size changed. */
544                 if (var->xres_virtual < var->xres)
545                         var->xres = var->xres_virtual;
546                 if (var->yres_virtual < var->yres)
547                         var->yres = var->yres_virtual;
548                 if (var->xres < xres_min || var->yres < yres_min)
549                         return -EINVAL;
550         }
551         if (var->xres + var->xoffset > var->xres_virtual)
552                 var->xoffset = var->xres_virtual - var->xres;
553         if (var->yres + var->yoffset > var->yres_virtual)
554                 var->yoffset = var->yres_virtual - var->yres;
555         line_size = var->xres * bpp / 8;
556
557         if (plane->color_mode == OMAPFB_COLOR_RGB444) {
558                 var->red.offset   = 8; var->red.length   = 4;
559                                                 var->red.msb_right   = 0;
560                 var->green.offset = 4; var->green.length = 4;
561                                                 var->green.msb_right = 0;
562                 var->blue.offset  = 0; var->blue.length  = 4;
563                                                 var->blue.msb_right  = 0;
564         } else {
565                 var->red.offset  = 11; var->red.length   = 5;
566                                                 var->red.msb_right   = 0;
567                 var->green.offset = 5;  var->green.length = 6;
568                                                 var->green.msb_right = 0;
569                 var->blue.offset = 0;  var->blue.length  = 5;
570                                                 var->blue.msb_right  = 0;
571         }
572
573         var->height             = -1;
574         var->width              = -1;
575         var->grayscale          = 0;
576
577         /* pixclock in ps, the rest in pixclock */
578         var->pixclock           = 10000000 / (panel->pixel_clock / 100);
579         var->left_margin        = panel->hfp;
580         var->right_margin       = panel->hbp;
581         var->upper_margin       = panel->vfp;
582         var->lower_margin       = panel->vbp;
583         var->hsync_len          = panel->hsw;
584         var->vsync_len          = panel->vsw;
585
586         /* TODO: get these from panel->config */
587         var->vmode              = FB_VMODE_NONINTERLACED;
588         var->sync               = 0;
589
590         return 0;
591 }
592
593
594 /* Set rotation (0, 90, 180, 270 degree), and switch to the new mode. */
595 static void omapfb_rotate(struct fb_info *fbi, int rotate)
596 {
597         struct omapfb_plane_struct *plane = fbi->par;
598         struct omapfb_device *fbdev = plane->fbdev;
599
600         omapfb_rqueue_lock(fbdev);
601         if (cpu_is_omap15xx() && rotate != fbi->var.rotate) {
602                 struct fb_var_screeninfo *new_var = &fbdev->new_var;
603
604                 memcpy(new_var, &fbi->var, sizeof(*new_var));
605                 new_var->rotate = rotate;
606                 if (set_fb_var(fbi, new_var) == 0 &&
607                     memcmp(new_var, &fbi->var, sizeof(*new_var))) {
608                         memcpy(&fbi->var, new_var, sizeof(*new_var));
609                         ctrl_change_mode(fbi);
610                 }
611         }
612         omapfb_rqueue_unlock(fbdev);
613 }
614
615 /*
616  * Set new x,y offsets in the virtual display for the visible area and switch
617  * to the new mode.
618  */
619 static int omapfb_pan_display(struct fb_var_screeninfo *var,
620                                struct fb_info *fbi)
621 {
622         struct omapfb_plane_struct *plane = fbi->par;
623         struct omapfb_device *fbdev = plane->fbdev;
624         int r = 0;
625
626         omapfb_rqueue_lock(fbdev);
627         if (var->xoffset != fbi->var.xoffset ||
628             var->yoffset != fbi->var.yoffset) {
629                 struct fb_var_screeninfo *new_var = &fbdev->new_var;
630
631                 memcpy(new_var, &fbi->var, sizeof(*new_var));
632                 new_var->xoffset = var->xoffset;
633                 new_var->yoffset = var->yoffset;
634                 if (set_fb_var(fbi, new_var))
635                         r = -EINVAL;
636                 else {
637                         memcpy(&fbi->var, new_var, sizeof(*new_var));
638                         ctrl_change_mode(fbi);
639                 }
640         }
641         omapfb_rqueue_unlock(fbdev);
642
643         return r;
644 }
645
646 /* Set mirror to vertical axis and switch to the new mode. */
647 static int omapfb_mirror(struct fb_info *fbi, int mirror)
648 {
649         struct omapfb_plane_struct *plane = fbi->par;
650         struct omapfb_device *fbdev = plane->fbdev;
651         int r = 0;
652
653         omapfb_rqueue_lock(fbdev);
654         mirror = mirror ? 1 : 0;
655         if (cpu_is_omap15xx())
656                 r = -EINVAL;
657         else if (mirror != plane->info.mirror) {
658                 plane->info.mirror = mirror;
659                 r = ctrl_change_mode(fbi);
660         }
661         omapfb_rqueue_unlock(fbdev);
662
663         return r;
664 }
665
666 /*
667  * Check values in var, try to adjust them in case of out of bound values if
668  * possible, or return error.
669  */
670 static int omapfb_check_var(struct fb_var_screeninfo *var, struct fb_info *fbi)
671 {
672         struct omapfb_plane_struct *plane = fbi->par;
673         struct omapfb_device *fbdev = plane->fbdev;
674         int r;
675
676         omapfb_rqueue_lock(fbdev);
677         if (fbdev->ctrl->sync != NULL)
678                 fbdev->ctrl->sync();
679         r = set_fb_var(fbi, var);
680         omapfb_rqueue_unlock(fbdev);
681
682         return r;
683 }
684
685 /*
686  * Switch to a new mode. The parameters for it has been check already by
687  * omapfb_check_var.
688  */
689 static int omapfb_set_par(struct fb_info *fbi)
690 {
691         struct omapfb_plane_struct *plane = fbi->par;
692         struct omapfb_device *fbdev = plane->fbdev;
693         int r = 0;
694
695         omapfb_rqueue_lock(fbdev);
696         set_fb_fix(fbi);
697         r = ctrl_change_mode(fbi);
698         omapfb_rqueue_unlock(fbdev);
699
700         return r;
701 }
702
703 int omapfb_update_window_async(struct fb_info *fbi,
704                                 struct omapfb_update_window *win,
705                                 void (*callback)(void *),
706                                 void *callback_data)
707 {
708         struct omapfb_plane_struct *plane = fbi->par;
709         struct omapfb_device *fbdev = plane->fbdev;
710         struct fb_var_screeninfo *var;
711
712         var = &fbi->var;
713         if (win->x >= var->xres || win->y >= var->yres ||
714             win->out_x > var->xres || win->out_y >= var->yres)
715                 return -EINVAL;
716
717         if (!fbdev->ctrl->update_window ||
718             fbdev->ctrl->get_update_mode() != OMAPFB_MANUAL_UPDATE)
719                 return -ENODEV;
720
721         if (win->x + win->width >= var->xres)
722                 win->width = var->xres - win->x;
723         if (win->y + win->height >= var->yres)
724                 win->height = var->yres - win->y;
725         /* The out sizes should be cropped to the LCD size */
726         if (win->out_x + win->out_width > fbdev->panel->x_res)
727                 win->out_width = fbdev->panel->x_res - win->out_x;
728         if (win->out_y + win->out_height > fbdev->panel->y_res)
729                 win->out_height = fbdev->panel->y_res - win->out_y;
730         if (!win->width || !win->height || !win->out_width || !win->out_height)
731                 return 0;
732
733         return fbdev->ctrl->update_window(fbi, win, callback, callback_data);
734 }
735 EXPORT_SYMBOL(omapfb_update_window_async);
736
737 static int omapfb_update_win(struct fb_info *fbi,
738                                 struct omapfb_update_window *win)
739 {
740         struct omapfb_plane_struct *plane = fbi->par;
741         int ret;
742
743         omapfb_rqueue_lock(plane->fbdev);
744         ret = omapfb_update_window_async(fbi, win, NULL, NULL);
745         omapfb_rqueue_unlock(plane->fbdev);
746
747         return ret;
748 }
749
750 static int omapfb_update_full_screen(struct fb_info *fbi)
751 {
752         struct omapfb_plane_struct *plane = fbi->par;
753         struct omapfb_device *fbdev = plane->fbdev;
754         struct omapfb_update_window win;
755         int r;
756
757         if (!fbdev->ctrl->update_window ||
758             fbdev->ctrl->get_update_mode() != OMAPFB_MANUAL_UPDATE)
759                 return -ENODEV;
760
761         win.x = 0;
762         win.y = 0;
763         win.width = fbi->var.xres;
764         win.height = fbi->var.yres;
765         win.out_x = 0;
766         win.out_y = 0;
767         win.out_width = fbi->var.xres;
768         win.out_height = fbi->var.yres;
769         win.format = 0;
770
771         omapfb_rqueue_lock(fbdev);
772         r = fbdev->ctrl->update_window(fbi, &win, NULL, NULL);
773         omapfb_rqueue_unlock(fbdev);
774
775         return r;
776 }
777
778 static int omapfb_setup_plane(struct fb_info *fbi, struct omapfb_plane_info *pi)
779 {
780         struct omapfb_plane_struct *plane = fbi->par;
781         struct omapfb_device *fbdev = plane->fbdev;
782         struct lcd_panel *panel = fbdev->panel;
783         struct omapfb_plane_info old_info;
784         int r = 0;
785
786         if (pi->pos_x + pi->out_width > panel->x_res ||
787             pi->pos_y + pi->out_height > panel->y_res)
788                 return -EINVAL;
789
790         omapfb_rqueue_lock(fbdev);
791         if (pi->enabled && !fbdev->mem_desc.region[plane->idx].size) {
792                 /*
793                  * This plane's memory was freed, can't enable it
794                  * until it's reallocated.
795                  */
796                 r = -EINVAL;
797                 goto out;
798         }
799         old_info = plane->info;
800         plane->info = *pi;
801         if (pi->enabled) {
802                 r = ctrl_change_mode(fbi);
803                 if (r < 0) {
804                         plane->info = old_info;
805                         goto out;
806                 }
807         }
808         r = fbdev->ctrl->enable_plane(plane->idx, pi->enabled);
809         if (r < 0) {
810                 plane->info = old_info;
811                 goto out;
812         }
813 out:
814         omapfb_rqueue_unlock(fbdev);
815         return r;
816 }
817
818 static int omapfb_query_plane(struct fb_info *fbi, struct omapfb_plane_info *pi)
819 {
820         struct omapfb_plane_struct *plane = fbi->par;
821
822         *pi = plane->info;
823         return 0;
824 }
825
826 static int omapfb_setup_mem(struct fb_info *fbi, struct omapfb_mem_info *mi)
827 {
828         struct omapfb_plane_struct *plane = fbi->par;
829         struct omapfb_device *fbdev = plane->fbdev;
830         struct omapfb_mem_region *rg = &fbdev->mem_desc.region[plane->idx];
831         size_t size;
832         int r = 0;
833
834         if (fbdev->ctrl->setup_mem == NULL)
835                 return -ENODEV;
836         if (mi->type > OMAPFB_MEMTYPE_MAX)
837                 return -EINVAL;
838
839         size = PAGE_ALIGN(mi->size);
840         omapfb_rqueue_lock(fbdev);
841         if (plane->info.enabled) {
842                 r = -EBUSY;
843                 goto out;
844         }
845         if (rg->size != size || rg->type != mi->type) {
846                 struct fb_var_screeninfo *new_var = &fbdev->new_var;
847                 unsigned long old_size = rg->size;
848                 u8            old_type = rg->type;
849                 unsigned long paddr;
850
851                 rg->size = size;
852                 rg->type = mi->type;
853                 /*
854                  * size == 0 is a special case, for which we
855                  * don't check / adjust the screen parameters.
856                  * This isn't a problem since the plane can't
857                  * be reenabled unless its size is > 0.
858                  */
859                 if (old_size != size && size) {
860                         if (size) {
861                                 memcpy(new_var, &fbi->var, sizeof(*new_var));
862                                 r = set_fb_var(fbi, new_var);
863                                 if (r < 0)
864                                         goto out;
865                         }
866                 }
867
868                 if (fbdev->ctrl->sync)
869                         fbdev->ctrl->sync();
870                 r = fbdev->ctrl->setup_mem(plane->idx, size, mi->type, &paddr);
871                 if (r < 0) {
872                         /* Revert changes. */
873                         rg->size = old_size;
874                         rg->type = old_type;
875                         goto out;
876                 }
877                 rg->paddr = paddr;
878
879                 if (old_size != size) {
880                         if (size) {
881                                 memcpy(&fbi->var, new_var, sizeof(fbi->var));
882                                 set_fb_fix(fbi);
883                         } else {
884                                 /*
885                                  * Set these explicitly to indicate that the
886                                  * plane memory is dealloce'd, the other
887                                  * screen parameters in var / fix are invalid.
888                                  */
889                                 fbi->fix.smem_start = 0;
890                                 fbi->fix.smem_len = 0;
891                         }
892                 }
893         }
894 out:
895         omapfb_rqueue_unlock(fbdev);
896
897         return r;
898 }
899
900 static int omapfb_query_mem(struct fb_info *fbi, struct omapfb_mem_info *mi)
901 {
902         struct omapfb_plane_struct *plane = fbi->par;
903         struct omapfb_device *fbdev = plane->fbdev;
904         struct omapfb_mem_region *rg;
905
906         rg = &fbdev->mem_desc.region[plane->idx];
907         memset(mi, 0, sizeof(*mi));
908         mi->size = rg->size;
909         mi->type = rg->type;
910
911         return 0;
912 }
913
914 static int omapfb_set_color_key(struct omapfb_device *fbdev,
915                                 struct omapfb_color_key *ck)
916 {
917         int r;
918
919         if (!fbdev->ctrl->set_color_key)
920                 return -ENODEV;
921
922         omapfb_rqueue_lock(fbdev);
923         r = fbdev->ctrl->set_color_key(ck);
924         omapfb_rqueue_unlock(fbdev);
925
926         return r;
927 }
928
929 static int omapfb_get_color_key(struct omapfb_device *fbdev,
930                                 struct omapfb_color_key *ck)
931 {
932         int r;
933
934         if (!fbdev->ctrl->get_color_key)
935                 return -ENODEV;
936
937         omapfb_rqueue_lock(fbdev);
938         r = fbdev->ctrl->get_color_key(ck);
939         omapfb_rqueue_unlock(fbdev);
940
941         return r;
942 }
943
944 static struct blocking_notifier_head omapfb_client_list[OMAPFB_PLANE_NUM];
945 static int notifier_inited;
946
947 static void omapfb_init_notifier(void)
948 {
949         int i;
950
951         for (i = 0; i < OMAPFB_PLANE_NUM; i++)
952                 BLOCKING_INIT_NOTIFIER_HEAD(&omapfb_client_list[i]);
953 }
954
955 int omapfb_register_client(struct omapfb_notifier_block *omapfb_nb,
956                                 omapfb_notifier_callback_t callback,
957                                 void *callback_data)
958 {
959         int r;
960
961         if ((unsigned)omapfb_nb->plane_idx > OMAPFB_PLANE_NUM)
962                 return -EINVAL;
963
964         if (!notifier_inited) {
965                 omapfb_init_notifier();
966                 notifier_inited = 1;
967         }
968
969         omapfb_nb->nb.notifier_call = (int (*)(struct notifier_block *,
970                                         unsigned long, void *))callback;
971         omapfb_nb->data = callback_data;
972         r = blocking_notifier_chain_register(
973                                 &omapfb_client_list[omapfb_nb->plane_idx],
974                                 &omapfb_nb->nb);
975         if (r)
976                 return r;
977         if (omapfb_dev != NULL &&
978             omapfb_dev->ctrl && omapfb_dev->ctrl->bind_client) {
979                 omapfb_dev->ctrl->bind_client(omapfb_nb);
980         }
981
982         return 0;
983 }
984 EXPORT_SYMBOL(omapfb_register_client);
985
986 int omapfb_unregister_client(struct omapfb_notifier_block *omapfb_nb)
987 {
988         return blocking_notifier_chain_unregister(
989                 &omapfb_client_list[omapfb_nb->plane_idx], &omapfb_nb->nb);
990 }
991 EXPORT_SYMBOL(omapfb_unregister_client);
992
993 void omapfb_notify_clients(struct omapfb_device *fbdev, unsigned long event)
994 {
995         int i;
996
997         if (!notifier_inited)
998                 /* no client registered yet */
999                 return;
1000
1001         for (i = 0; i < OMAPFB_PLANE_NUM; i++)
1002                 blocking_notifier_call_chain(&omapfb_client_list[i], event,
1003                                     fbdev->fb_info[i]);
1004 }
1005 EXPORT_SYMBOL(omapfb_notify_clients);
1006
1007 static int omapfb_set_update_mode(struct omapfb_device *fbdev,
1008                                    enum omapfb_update_mode mode)
1009 {
1010         int r;
1011
1012         omapfb_rqueue_lock(fbdev);
1013         r = fbdev->ctrl->set_update_mode(mode);
1014         omapfb_rqueue_unlock(fbdev);
1015
1016         return r;
1017 }
1018
1019 static enum omapfb_update_mode omapfb_get_update_mode(struct omapfb_device *fbdev)
1020 {
1021         int r;
1022
1023         omapfb_rqueue_lock(fbdev);
1024         r = fbdev->ctrl->get_update_mode();
1025         omapfb_rqueue_unlock(fbdev);
1026
1027         return r;
1028 }
1029
1030 static void omapfb_get_caps(struct omapfb_device *fbdev, int plane,
1031                                      struct omapfb_caps *caps)
1032 {
1033         memset(caps, 0, sizeof(*caps));
1034         fbdev->ctrl->get_caps(plane, caps);
1035         caps->ctrl |= fbdev->panel->get_caps(fbdev->panel);
1036 }
1037
1038 /* For lcd testing */
1039 void omapfb_write_first_pixel(struct omapfb_device *fbdev, u16 pixval)
1040 {
1041         omapfb_rqueue_lock(fbdev);
1042         *(u16 *)fbdev->mem_desc.region[0].vaddr = pixval;
1043         if (fbdev->ctrl->get_update_mode() == OMAPFB_MANUAL_UPDATE) {
1044                 struct omapfb_update_window win;
1045
1046                 memset(&win, 0, sizeof(win));
1047                 win.width = 2;
1048                 win.height = 2;
1049                 win.out_width = 2;
1050                 win.out_height = 2;
1051                 fbdev->ctrl->update_window(fbdev->fb_info[0], &win, NULL, NULL);
1052         }
1053         omapfb_rqueue_unlock(fbdev);
1054 }
1055 EXPORT_SYMBOL(omapfb_write_first_pixel);
1056
1057 /*
1058  * Ioctl interface. Part of the kernel mode frame buffer API is duplicated
1059  * here to be accessible by user mode code.
1060  */
1061 static int omapfb_ioctl(struct fb_info *fbi, unsigned int cmd,
1062                         unsigned long arg)
1063 {
1064         struct omapfb_plane_struct *plane = fbi->par;
1065         struct omapfb_device    *fbdev = plane->fbdev;
1066         struct fb_ops           *ops = fbi->fbops;
1067         union {
1068                 struct omapfb_update_window     update_window;
1069                 struct omapfb_plane_info        plane_info;
1070                 struct omapfb_mem_info          mem_info;
1071                 struct omapfb_color_key         color_key;
1072                 enum omapfb_update_mode         update_mode;
1073                 struct omapfb_caps              caps;
1074                 unsigned int            mirror;
1075                 int                     plane_out;
1076                 int                     enable_plane;
1077         } p;
1078         int r = 0;
1079
1080         BUG_ON(!ops);
1081         switch (cmd) {
1082         case OMAPFB_MIRROR:
1083                 if (get_user(p.mirror, (int __user *)arg))
1084                         r = -EFAULT;
1085                 else
1086                         omapfb_mirror(fbi, p.mirror);
1087                 break;
1088         case OMAPFB_SYNC_GFX:
1089                 omapfb_sync(fbi);
1090                 break;
1091         case OMAPFB_VSYNC:
1092                 break;
1093         case OMAPFB_SET_UPDATE_MODE:
1094                 if (get_user(p.update_mode, (int __user *)arg))
1095                         r = -EFAULT;
1096                 else
1097                         r = omapfb_set_update_mode(fbdev, p.update_mode);
1098                 break;
1099         case OMAPFB_GET_UPDATE_MODE:
1100                 p.update_mode = omapfb_get_update_mode(fbdev);
1101                 if (put_user(p.update_mode,
1102                                         (enum omapfb_update_mode __user *)arg))
1103                         r = -EFAULT;
1104                 break;
1105         case OMAPFB_UPDATE_WINDOW_OLD:
1106                 if (copy_from_user(&p.update_window, (void __user *)arg,
1107                                    sizeof(struct omapfb_update_window_old)))
1108                         r = -EFAULT;
1109                 else {
1110                         struct omapfb_update_window *u = &p.update_window;
1111                         u->out_x = u->x;
1112                         u->out_y = u->y;
1113                         u->out_width = u->width;
1114                         u->out_height = u->height;
1115                         memset(u->reserved, 0, sizeof(u->reserved));
1116                         r = omapfb_update_win(fbi, u);
1117                 }
1118                 break;
1119         case OMAPFB_UPDATE_WINDOW:
1120                 if (copy_from_user(&p.update_window, (void __user *)arg,
1121                                    sizeof(p.update_window)))
1122                         r = -EFAULT;
1123                 else
1124                         r = omapfb_update_win(fbi, &p.update_window);
1125                 break;
1126         case OMAPFB_SETUP_PLANE:
1127                 if (copy_from_user(&p.plane_info, (void __user *)arg,
1128                                    sizeof(p.plane_info)))
1129                         r = -EFAULT;
1130                 else
1131                         r = omapfb_setup_plane(fbi, &p.plane_info);
1132                 break;
1133         case OMAPFB_QUERY_PLANE:
1134                 if ((r = omapfb_query_plane(fbi, &p.plane_info)) < 0)
1135                         break;
1136                 if (copy_to_user((void __user *)arg, &p.plane_info,
1137                                    sizeof(p.plane_info)))
1138                         r = -EFAULT;
1139                 break;
1140         case OMAPFB_SETUP_MEM:
1141                 if (copy_from_user(&p.mem_info, (void __user *)arg,
1142                                    sizeof(p.mem_info)))
1143                         r = -EFAULT;
1144                 else
1145                         r = omapfb_setup_mem(fbi, &p.mem_info);
1146                 break;
1147         case OMAPFB_QUERY_MEM:
1148                 if ((r = omapfb_query_mem(fbi, &p.mem_info)) < 0)
1149                         break;
1150                 if (copy_to_user((void __user *)arg, &p.mem_info,
1151                                    sizeof(p.mem_info)))
1152                         r = -EFAULT;
1153                 break;
1154         case OMAPFB_SET_COLOR_KEY:
1155                 if (copy_from_user(&p.color_key, (void __user *)arg,
1156                                    sizeof(p.color_key)))
1157                         r = -EFAULT;
1158                 else
1159                         r = omapfb_set_color_key(fbdev, &p.color_key);
1160                 break;
1161         case OMAPFB_GET_COLOR_KEY:
1162                 if ((r = omapfb_get_color_key(fbdev, &p.color_key)) < 0)
1163                         break;
1164                 if (copy_to_user((void __user *)arg, &p.color_key,
1165                                  sizeof(p.color_key)))
1166                         r = -EFAULT;
1167                 break;
1168         case OMAPFB_GET_CAPS:
1169                 omapfb_get_caps(fbdev, plane->idx, &p.caps);
1170                 if (copy_to_user((void __user *)arg, &p.caps, sizeof(p.caps)))
1171                         r = -EFAULT;
1172                 break;
1173         case OMAPFB_LCD_TEST:
1174                 {
1175                         int test_num;
1176
1177                         if (get_user(test_num, (int __user *)arg)) {
1178                                 r = -EFAULT;
1179                                 break;
1180                         }
1181                         if (!fbdev->panel->run_test) {
1182                                 r = -EINVAL;
1183                                 break;
1184                         }
1185                         r = fbdev->panel->run_test(fbdev->panel, test_num);
1186                         break;
1187                 }
1188         case OMAPFB_CTRL_TEST:
1189                 {
1190                         int test_num;
1191
1192                         if (get_user(test_num, (int __user *)arg)) {
1193                                 r = -EFAULT;
1194                                 break;
1195                         }
1196                         if (!fbdev->ctrl->run_test) {
1197                                 r = -EINVAL;
1198                                 break;
1199                         }
1200                         r = fbdev->ctrl->run_test(test_num);
1201                         break;
1202                 }
1203         default:
1204                 r = -EINVAL;
1205         }
1206
1207         return r;
1208 }
1209
1210 static int omapfb_mmap(struct fb_info *info, struct vm_area_struct *vma)
1211 {
1212         struct omapfb_plane_struct *plane = info->par;
1213         struct omapfb_device *fbdev = plane->fbdev;
1214         int r;
1215
1216         omapfb_rqueue_lock(fbdev);
1217         r = fbdev->ctrl->mmap(info, vma);
1218         omapfb_rqueue_unlock(fbdev);
1219
1220         return r;
1221 }
1222
1223 /*
1224  * Callback table for the frame buffer framework. Some of these pointers
1225  * will be changed according to the current setting of fb_info->accel_flags.
1226  */
1227 static struct fb_ops omapfb_ops = {
1228         .owner          = THIS_MODULE,
1229         .fb_open        = omapfb_open,
1230         .fb_release     = omapfb_release,
1231         .fb_setcolreg   = omapfb_setcolreg,
1232         .fb_setcmap     = omapfb_setcmap,
1233         .fb_fillrect    = cfb_fillrect,
1234         .fb_copyarea    = cfb_copyarea,
1235         .fb_imageblit   = cfb_imageblit,
1236         .fb_blank       = omapfb_blank,
1237         .fb_ioctl       = omapfb_ioctl,
1238         .fb_check_var   = omapfb_check_var,
1239         .fb_set_par     = omapfb_set_par,
1240         .fb_rotate      = omapfb_rotate,
1241         .fb_pan_display = omapfb_pan_display,
1242 };
1243
1244 /*
1245  * ---------------------------------------------------------------------------
1246  * Sysfs interface
1247  * ---------------------------------------------------------------------------
1248  */
1249 /* omapfbX sysfs entries */
1250 static ssize_t omapfb_show_caps_num(struct device *dev,
1251                                     struct device_attribute *attr, char *buf)
1252 {
1253         struct omapfb_device *fbdev = (struct omapfb_device *)dev->driver_data;
1254         int plane;
1255         size_t size;
1256         struct omapfb_caps caps;
1257
1258         plane = 0;
1259         size = 0;
1260         while (size < PAGE_SIZE && plane < OMAPFB_PLANE_NUM) {
1261                 omapfb_get_caps(fbdev, plane, &caps);
1262                 size += snprintf(&buf[size], PAGE_SIZE - size,
1263                         "plane#%d %#010x %#010x %#010x\n",
1264                         plane, caps.ctrl, caps.plane_color, caps.wnd_color);
1265                 plane++;
1266         }
1267         return size;
1268 }
1269
1270 static ssize_t omapfb_show_caps_text(struct device *dev,
1271                                      struct device_attribute *attr, char *buf)
1272 {
1273         struct omapfb_device *fbdev = (struct omapfb_device *)dev->driver_data;
1274         int i;
1275         struct omapfb_caps caps;
1276         int plane;
1277         size_t size;
1278
1279         plane = 0;
1280         size = 0;
1281         while (size < PAGE_SIZE && plane < OMAPFB_PLANE_NUM) {
1282                 omapfb_get_caps(fbdev, plane, &caps);
1283                 size += snprintf(&buf[size], PAGE_SIZE - size,
1284                                  "plane#%d:\n", plane);
1285                 for (i = 0; i < ARRAY_SIZE(ctrl_caps) &&
1286                      size < PAGE_SIZE; i++) {
1287                         if (ctrl_caps[i].flag & caps.ctrl)
1288                                 size += snprintf(&buf[size], PAGE_SIZE - size,
1289                                         " %s\n", ctrl_caps[i].name);
1290                 }
1291                 size += snprintf(&buf[size], PAGE_SIZE - size,
1292                                  " plane colors:\n");
1293                 for (i = 0; i < ARRAY_SIZE(color_caps) &&
1294                      size < PAGE_SIZE; i++) {
1295                         if (color_caps[i].flag & caps.plane_color)
1296                                 size += snprintf(&buf[size], PAGE_SIZE - size,
1297                                         "  %s\n", color_caps[i].name);
1298                 }
1299                 size += snprintf(&buf[size], PAGE_SIZE - size,
1300                                  " window colors:\n");
1301                 for (i = 0; i < ARRAY_SIZE(color_caps) &&
1302                      size < PAGE_SIZE; i++) {
1303                         if (color_caps[i].flag & caps.wnd_color)
1304                                 size += snprintf(&buf[size], PAGE_SIZE - size,
1305                                         "  %s\n", color_caps[i].name);
1306                 }
1307
1308                 plane++;
1309         }
1310         return size;
1311 }
1312
1313 static DEVICE_ATTR(caps_num, 0444, omapfb_show_caps_num, NULL);
1314 static DEVICE_ATTR(caps_text, 0444, omapfb_show_caps_text, NULL);
1315
1316 /* panel sysfs entries */
1317 static ssize_t omapfb_show_panel_name(struct device *dev,
1318                                       struct device_attribute *attr, char *buf)
1319 {
1320         struct omapfb_device *fbdev = (struct omapfb_device *)dev->driver_data;
1321
1322         return snprintf(buf, PAGE_SIZE, "%s\n", fbdev->panel->name);
1323 }
1324
1325 static ssize_t omapfb_show_bklight_level(struct device *dev,
1326                                          struct device_attribute *attr,
1327                                          char *buf)
1328 {
1329         struct omapfb_device *fbdev = (struct omapfb_device *)dev->driver_data;
1330         int r;
1331
1332         if (fbdev->panel->get_bklight_level) {
1333                 r = snprintf(buf, PAGE_SIZE, "%d\n",
1334                              fbdev->panel->get_bklight_level(fbdev->panel));
1335         } else
1336                 r = -ENODEV;
1337         return r;
1338 }
1339
1340 static ssize_t omapfb_store_bklight_level(struct device *dev,
1341                                           struct device_attribute *attr,
1342                                           const char *buf, size_t size)
1343 {
1344         struct omapfb_device *fbdev = (struct omapfb_device *)dev->driver_data;
1345         int r;
1346
1347         if (fbdev->panel->set_bklight_level) {
1348                 unsigned int level;
1349
1350                 if (sscanf(buf, "%10d", &level) == 1) {
1351                         r = fbdev->panel->set_bklight_level(fbdev->panel,
1352                                                             level);
1353                 } else
1354                         r = -EINVAL;
1355         } else
1356                 r = -ENODEV;
1357         return r ? r : size;
1358 }
1359
1360 static ssize_t omapfb_show_bklight_max(struct device *dev,
1361                                        struct device_attribute *attr, char *buf)
1362 {
1363         struct omapfb_device *fbdev = (struct omapfb_device *)dev->driver_data;
1364         int r;
1365
1366         if (fbdev->panel->get_bklight_level) {
1367                 r = snprintf(buf, PAGE_SIZE, "%d\n",
1368                              fbdev->panel->get_bklight_max(fbdev->panel));
1369         } else
1370                 r = -ENODEV;
1371         return r;
1372 }
1373
1374 static struct device_attribute dev_attr_panel_name =
1375         __ATTR(name, 0444, omapfb_show_panel_name, NULL);
1376 static DEVICE_ATTR(backlight_level, 0664,
1377                    omapfb_show_bklight_level, omapfb_store_bklight_level);
1378 static DEVICE_ATTR(backlight_max, 0444, omapfb_show_bklight_max, NULL);
1379
1380 static struct attribute *panel_attrs[] = {
1381         &dev_attr_panel_name.attr,
1382         &dev_attr_backlight_level.attr,
1383         &dev_attr_backlight_max.attr,
1384         NULL,
1385 };
1386
1387 static struct attribute_group panel_attr_grp = {
1388         .name  = "panel",
1389         .attrs = panel_attrs,
1390 };
1391
1392 /* ctrl sysfs entries */
1393 static ssize_t omapfb_show_ctrl_name(struct device *dev,
1394                                      struct device_attribute *attr, char *buf)
1395 {
1396         struct omapfb_device *fbdev = (struct omapfb_device *)dev->driver_data;
1397
1398         return snprintf(buf, PAGE_SIZE, "%s\n", fbdev->ctrl->name);
1399 }
1400
1401 static struct device_attribute dev_attr_ctrl_name =
1402         __ATTR(name, 0444, omapfb_show_ctrl_name, NULL);
1403
1404 static struct attribute *ctrl_attrs[] = {
1405         &dev_attr_ctrl_name.attr,
1406         NULL,
1407 };
1408
1409 static struct attribute_group ctrl_attr_grp = {
1410         .name  = "ctrl",
1411         .attrs = ctrl_attrs,
1412 };
1413
1414 static int omapfb_register_sysfs(struct omapfb_device *fbdev)
1415 {
1416         int r;
1417
1418         if ((r = device_create_file(fbdev->dev, &dev_attr_caps_num)))
1419                 goto fail0;
1420
1421         if ((r = device_create_file(fbdev->dev, &dev_attr_caps_text)))
1422                 goto fail1;
1423
1424         if ((r = sysfs_create_group(&fbdev->dev->kobj, &panel_attr_grp)))
1425                 goto fail2;
1426
1427         if ((r = sysfs_create_group(&fbdev->dev->kobj, &ctrl_attr_grp)))
1428                 goto fail3;
1429
1430         return 0;
1431 fail3:
1432         sysfs_remove_group(&fbdev->dev->kobj, &panel_attr_grp);
1433 fail2:
1434         device_remove_file(fbdev->dev, &dev_attr_caps_text);
1435 fail1:
1436         device_remove_file(fbdev->dev, &dev_attr_caps_num);
1437 fail0:
1438         dev_err(fbdev->dev, "unable to register sysfs interface\n");
1439         return r;
1440 }
1441
1442 static void omapfb_unregister_sysfs(struct omapfb_device *fbdev)
1443 {
1444         sysfs_remove_group(&fbdev->dev->kobj, &ctrl_attr_grp);
1445         sysfs_remove_group(&fbdev->dev->kobj, &panel_attr_grp);
1446         device_remove_file(fbdev->dev, &dev_attr_caps_num);
1447         device_remove_file(fbdev->dev, &dev_attr_caps_text);
1448 }
1449
1450 /*
1451  * ---------------------------------------------------------------------------
1452  * LDM callbacks
1453  * ---------------------------------------------------------------------------
1454  */
1455 /* Initialize system fb_info object and set the default video mode.
1456  * The frame buffer memory already allocated by lcddma_init
1457  */
1458 static int fbinfo_init(struct omapfb_device *fbdev, struct fb_info *info)
1459 {
1460         struct fb_var_screeninfo        *var = &info->var;
1461         struct fb_fix_screeninfo        *fix = &info->fix;
1462         int                             r = 0;
1463
1464         info->fbops = &omapfb_ops;
1465         info->flags = FBINFO_FLAG_DEFAULT;
1466
1467         strncpy(fix->id, MODULE_NAME, sizeof(fix->id));
1468
1469         info->pseudo_palette = fbdev->pseudo_palette;
1470
1471         var->accel_flags  = def_accel ? FB_ACCELF_TEXT : 0;
1472         var->xres = def_vxres;
1473         var->yres = def_vyres;
1474         var->xres_virtual = def_vxres;
1475         var->yres_virtual = def_vyres;
1476         var->rotate       = def_rotate;
1477         var->bits_per_pixel = fbdev->panel->bpp;
1478
1479         set_fb_var(info, var);
1480         set_fb_fix(info);
1481
1482         r = fb_alloc_cmap(&info->cmap, 16, 0);
1483         if (r != 0)
1484                 dev_err(fbdev->dev, "unable to allocate color map memory\n");
1485
1486         return r;
1487 }
1488
1489 /* Release the fb_info object */
1490 static void fbinfo_cleanup(struct omapfb_device *fbdev, struct fb_info *fbi)
1491 {
1492         fb_dealloc_cmap(&fbi->cmap);
1493 }
1494
1495 static void planes_cleanup(struct omapfb_device *fbdev)
1496 {
1497         int i;
1498
1499         for (i = 0; i < fbdev->mem_desc.region_cnt; i++) {
1500                 if (fbdev->fb_info[i] == NULL)
1501                         break;
1502                 fbinfo_cleanup(fbdev, fbdev->fb_info[i]);
1503                 framebuffer_release(fbdev->fb_info[i]);
1504         }
1505 }
1506
1507 static int planes_init(struct omapfb_device *fbdev)
1508 {
1509         struct fb_info *fbi;
1510         int i;
1511         int r;
1512
1513         for (i = 0; i < fbdev->mem_desc.region_cnt; i++) {
1514                 struct omapfb_plane_struct *plane;
1515                 fbi = framebuffer_alloc(sizeof(struct omapfb_plane_struct),
1516                                         fbdev->dev);
1517                 if (fbi == NULL) {
1518                         dev_err(fbdev->dev,
1519                                 "unable to allocate memory for plane info\n");
1520                         planes_cleanup(fbdev);
1521                         return -ENOMEM;
1522                 }
1523                 plane = fbi->par;
1524                 plane->idx = i;
1525                 plane->fbdev = fbdev;
1526                 plane->info.mirror = def_mirror;
1527                 fbdev->fb_info[i] = fbi;
1528
1529                 if ((r = fbinfo_init(fbdev, fbi)) < 0) {
1530                         framebuffer_release(fbi);
1531                         planes_cleanup(fbdev);
1532                         return r;
1533                 }
1534                 plane->info.out_width = fbi->var.xres;
1535                 plane->info.out_height = fbi->var.yres;
1536         }
1537         return 0;
1538 }
1539
1540 /*
1541  * Free driver resources. Can be called to rollback an aborted initialization
1542  * sequence.
1543  */
1544 static void omapfb_free_resources(struct omapfb_device *fbdev, int state)
1545 {
1546         int i;
1547
1548         switch (state) {
1549         case OMAPFB_ACTIVE:
1550                 for (i = 0; i < fbdev->mem_desc.region_cnt; i++)
1551                         unregister_framebuffer(fbdev->fb_info[i]);
1552         case 7:
1553                 omapfb_unregister_sysfs(fbdev);
1554         case 6:
1555                 fbdev->panel->disable(fbdev->panel);
1556         case 5:
1557                 omapfb_set_update_mode(fbdev, OMAPFB_UPDATE_DISABLED);
1558         case 4:
1559                 planes_cleanup(fbdev);
1560         case 3:
1561                 ctrl_cleanup(fbdev);
1562         case 2:
1563                 fbdev->panel->cleanup(fbdev->panel);
1564         case 1:
1565                 dev_set_drvdata(fbdev->dev, NULL);
1566                 kfree(fbdev);
1567         case 0:
1568                 /* nothing to free */
1569                 break;
1570         default:
1571                 BUG();
1572         }
1573 }
1574
1575 static int omapfb_find_ctrl(struct omapfb_device *fbdev)
1576 {
1577         struct omapfb_platform_data *conf;
1578         char name[17];
1579         int i;
1580
1581         conf = fbdev->dev->platform_data;
1582
1583         fbdev->ctrl = NULL;
1584
1585         strncpy(name, conf->lcd.ctrl_name, sizeof(name) - 1);
1586         name[sizeof(name) - 1] = '\0';
1587
1588         if (strcmp(name, "internal") == 0) {
1589                 fbdev->ctrl = fbdev->int_ctrl;
1590                 return 0;
1591         }
1592
1593         for (i = 0; i < ARRAY_SIZE(ctrls); i++) {
1594                 dev_dbg(fbdev->dev, "ctrl %s\n", ctrls[i]->name);
1595                 if (strcmp(ctrls[i]->name, name) == 0) {
1596                         fbdev->ctrl = ctrls[i];
1597                         break;
1598                 }
1599         }
1600
1601         if (fbdev->ctrl == NULL) {
1602                 dev_dbg(fbdev->dev, "ctrl %s not supported\n", name);
1603                 return -1;
1604         }
1605
1606         return 0;
1607 }
1608
1609 static void check_required_callbacks(struct omapfb_device *fbdev)
1610 {
1611 #define _C(x) (fbdev->ctrl->x != NULL)
1612 #define _P(x) (fbdev->panel->x != NULL)
1613         BUG_ON(fbdev->ctrl == NULL || fbdev->panel == NULL);
1614         BUG_ON(!(_C(init) && _C(cleanup) && _C(get_caps) &&
1615                  _C(set_update_mode) && _C(setup_plane) && _C(enable_plane) &&
1616                  _P(init) && _P(cleanup) && _P(enable) && _P(disable) &&
1617                  _P(get_caps)));
1618 #undef _P
1619 #undef _C
1620 }
1621
1622 /*
1623  * Called by LDM binding to probe and attach a new device.
1624  * Initialization sequence:
1625  *   1. allocate system omapfb_device structure
1626  *   2. select controller type according to platform configuration
1627  *      init LCD panel
1628  *   3. init LCD controller and LCD DMA
1629  *   4. init system fb_info structure for all planes
1630  *   5. setup video mode for first plane and enable it
1631  *   6. enable LCD panel
1632  *   7. register sysfs attributes
1633  *   OMAPFB_ACTIVE: register system fb_info structure for all planes
1634  */
1635 static int omapfb_do_probe(struct platform_device *pdev,
1636                                 struct lcd_panel *panel)
1637 {
1638         struct omapfb_device    *fbdev = NULL;
1639         int                     init_state;
1640         unsigned long           phz, hhz, vhz;
1641         unsigned long           vram;
1642         int                     i;
1643         int                     r = 0;
1644
1645         init_state = 0;
1646
1647         if (pdev->num_resources != 0) {
1648                 dev_err(&pdev->dev, "probed for an unknown device\n");
1649                 r = -ENODEV;
1650                 goto cleanup;
1651         }
1652
1653         if (pdev->dev.platform_data == NULL) {
1654                 dev_err(&pdev->dev, "missing platform data\n");
1655                 r = -ENOENT;
1656                 goto cleanup;
1657         }
1658
1659         fbdev = kzalloc(sizeof(struct omapfb_device), GFP_KERNEL);
1660         if (fbdev == NULL) {
1661                 dev_err(&pdev->dev,
1662                         "unable to allocate memory for device info\n");
1663                 r = -ENOMEM;
1664                 goto cleanup;
1665         }
1666         init_state++;
1667
1668         fbdev->dev = &pdev->dev;
1669         fbdev->panel = panel;
1670         platform_set_drvdata(pdev, fbdev);
1671
1672         mutex_init(&fbdev->rqueue_mutex);
1673
1674 #ifdef CONFIG_ARCH_OMAP1
1675         fbdev->int_ctrl = &omap1_int_ctrl;
1676 #ifdef CONFIG_FB_OMAP_LCDC_EXTERNAL
1677         fbdev->ext_if = &omap1_ext_if;
1678 #endif
1679 #else   /* OMAP2 */
1680         fbdev->int_ctrl = &omap2_int_ctrl;
1681 #ifdef CONFIG_FB_OMAP_LCDC_EXTERNAL
1682         fbdev->ext_if = &omap2_ext_if;
1683 #endif
1684 #endif
1685         if (omapfb_find_ctrl(fbdev) < 0) {
1686                 dev_err(fbdev->dev,
1687                         "LCD controller not found, board not supported\n");
1688                 r = -ENODEV;
1689                 goto cleanup;
1690         }
1691
1692         r = fbdev->panel->init(fbdev->panel, fbdev);
1693         if (r)
1694                 goto cleanup;
1695
1696         pr_info("omapfb: configured for panel %s\n", fbdev->panel->name);
1697
1698         def_vxres = def_vxres ? : fbdev->panel->x_res;
1699         def_vyres = def_vyres ? : fbdev->panel->y_res;
1700
1701         init_state++;
1702
1703         r = ctrl_init(fbdev);
1704         if (r)
1705                 goto cleanup;
1706         if (fbdev->ctrl->mmap != NULL)
1707                 omapfb_ops.fb_mmap = omapfb_mmap;
1708         init_state++;
1709
1710         check_required_callbacks(fbdev);
1711
1712         r = planes_init(fbdev);
1713         if (r)
1714                 goto cleanup;
1715         init_state++;
1716
1717 #ifdef CONFIG_FB_OMAP_DMA_TUNE
1718         /* Set DMA priority for EMIFF access to highest */
1719         if (cpu_class_is_omap1())
1720                 omap_set_dma_priority(0, OMAP_DMA_PORT_EMIFF, 15);
1721 #endif
1722
1723         r = ctrl_change_mode(fbdev->fb_info[0]);
1724         if (r) {
1725                 dev_err(fbdev->dev, "mode setting failed\n");
1726                 goto cleanup;
1727         }
1728
1729         /* GFX plane is enabled by default */
1730         r = fbdev->ctrl->enable_plane(OMAPFB_PLANE_GFX, 1);
1731         if (r)
1732                 goto cleanup;
1733
1734         omapfb_set_update_mode(fbdev, manual_update ?
1735                                    OMAPFB_MANUAL_UPDATE : OMAPFB_AUTO_UPDATE);
1736         init_state++;
1737
1738         r = fbdev->panel->enable(fbdev->panel);
1739         if (r)
1740                 goto cleanup;
1741         init_state++;
1742
1743         r = omapfb_register_sysfs(fbdev);
1744         if (r)
1745                 goto cleanup;
1746         init_state++;
1747
1748         vram = 0;
1749         for (i = 0; i < fbdev->mem_desc.region_cnt; i++) {
1750                 r = register_framebuffer(fbdev->fb_info[i]);
1751                 if (r != 0) {
1752                         dev_err(fbdev->dev,
1753                                 "registering framebuffer %d failed\n", i);
1754                         goto cleanup;
1755                 }
1756                 vram += fbdev->mem_desc.region[i].size;
1757         }
1758
1759         fbdev->state = OMAPFB_ACTIVE;
1760
1761         panel = fbdev->panel;
1762         phz = panel->pixel_clock * 1000;
1763         hhz = phz * 10 / (panel->hfp + panel->x_res + panel->hbp + panel->hsw);
1764         vhz = hhz / (panel->vfp + panel->y_res + panel->vbp + panel->vsw);
1765
1766         omapfb_dev = fbdev;
1767
1768         pr_info("omapfb: Framebuffer initialized. Total vram %lu planes %d\n",
1769                         vram, fbdev->mem_desc.region_cnt);
1770         pr_info("omapfb: Pixclock %lu kHz hfreq %lu.%lu kHz "
1771                         "vfreq %lu.%lu Hz\n",
1772                         phz / 1000, hhz / 10000, hhz % 10, vhz / 10, vhz % 10);
1773
1774         return 0;
1775
1776 cleanup:
1777         omapfb_free_resources(fbdev, init_state);
1778
1779         return r;
1780 }
1781
1782 static int omapfb_probe(struct platform_device *pdev)
1783 {
1784         BUG_ON(fbdev_pdev != NULL);
1785
1786         /* Delay actual initialization until the LCD is registered */
1787         fbdev_pdev = pdev;
1788         if (fbdev_panel != NULL)
1789                 omapfb_do_probe(fbdev_pdev, fbdev_panel);
1790         return 0;
1791 }
1792
1793 void omapfb_register_panel(struct lcd_panel *panel)
1794 {
1795         BUG_ON(fbdev_panel != NULL);
1796
1797         fbdev_panel = panel;
1798         if (fbdev_pdev != NULL)
1799                 omapfb_do_probe(fbdev_pdev, fbdev_panel);
1800 }
1801
1802 /* Called when the device is being detached from the driver */
1803 static int omapfb_remove(struct platform_device *pdev)
1804 {
1805         struct omapfb_device *fbdev = platform_get_drvdata(pdev);
1806         enum omapfb_state saved_state = fbdev->state;
1807
1808         /* FIXME: wait till completion of pending events */
1809
1810         fbdev->state = OMAPFB_DISABLED;
1811         omapfb_free_resources(fbdev, saved_state);
1812
1813         return 0;
1814 }
1815
1816 /* PM suspend */
1817 static int omapfb_suspend(struct platform_device *pdev, pm_message_t mesg)
1818 {
1819         struct omapfb_device *fbdev = platform_get_drvdata(pdev);
1820
1821         omapfb_blank(VESA_POWERDOWN, fbdev->fb_info[0]);
1822
1823         return 0;
1824 }
1825
1826 /* PM resume */
1827 static int omapfb_resume(struct platform_device *pdev)
1828 {
1829         struct omapfb_device *fbdev = platform_get_drvdata(pdev);
1830
1831         omapfb_blank(VESA_NO_BLANKING, fbdev->fb_info[0]);
1832         return 0;
1833 }
1834
1835 static struct platform_driver omapfb_driver = {
1836         .probe          = omapfb_probe,
1837         .remove         = omapfb_remove,
1838         .suspend        = omapfb_suspend,
1839         .resume         = omapfb_resume,
1840         .driver         = {
1841                 .name   = MODULE_NAME,
1842                 .owner  = THIS_MODULE,
1843         },
1844 };
1845
1846 #ifndef MODULE
1847
1848 /* Process kernel command line parameters */
1849 static int __init omapfb_setup(char *options)
1850 {
1851         char *this_opt = NULL;
1852         int r = 0;
1853
1854         pr_debug("omapfb: options %s\n", options);
1855
1856         if (!options || !*options)
1857                 return 0;
1858
1859         while (!r && (this_opt = strsep(&options, ",")) != NULL) {
1860                 if (!strncmp(this_opt, "accel", 5))
1861                         def_accel = 1;
1862                 else if (!strncmp(this_opt, "vram:", 5)) {
1863                         char *suffix;
1864                         unsigned long vram;
1865                         vram = (simple_strtoul(this_opt + 5, &suffix, 0));
1866                         switch (suffix[0]) {
1867                         case '\0':
1868                                 break;
1869                         case 'm':
1870                         case 'M':
1871                                 vram *= 1024;
1872                                 /* Fall through */
1873                         case 'k':
1874                         case 'K':
1875                                 vram *= 1024;
1876                                 break;
1877                         default:
1878                                 pr_debug("omapfb: invalid vram suffix %c\n",
1879                                          suffix[0]);
1880                                 r = -1;
1881                         }
1882                         def_vram[def_vram_cnt++] = vram;
1883                 }
1884                 else if (!strncmp(this_opt, "vxres:", 6))
1885                         def_vxres = simple_strtoul(this_opt + 6, NULL, 0);
1886                 else if (!strncmp(this_opt, "vyres:", 6))
1887                         def_vyres = simple_strtoul(this_opt + 6, NULL, 0);
1888                 else if (!strncmp(this_opt, "rotate:", 7))
1889                         def_rotate = (simple_strtoul(this_opt + 7, NULL, 0));
1890                 else if (!strncmp(this_opt, "mirror:", 7))
1891                         def_mirror = (simple_strtoul(this_opt + 7, NULL, 0));
1892                 else if (!strncmp(this_opt, "manual_update", 13))
1893                         manual_update = 1;
1894                 else {
1895                         pr_debug("omapfb: invalid option\n");
1896                         r = -1;
1897                 }
1898         }
1899
1900         return r;
1901 }
1902
1903 #endif
1904
1905 /* Register both the driver and the device */
1906 static int __init omapfb_init(void)
1907 {
1908 #ifndef MODULE
1909         char *option;
1910
1911         if (fb_get_options("omapfb", &option))
1912                 return -ENODEV;
1913         omapfb_setup(option);
1914 #endif
1915         /* Register the driver with LDM */
1916         if (platform_driver_register(&omapfb_driver)) {
1917                 pr_debug("failed to register omapfb driver\n");
1918                 return -ENODEV;
1919         }
1920
1921         return 0;
1922 }
1923
1924 static void __exit omapfb_cleanup(void)
1925 {
1926         platform_driver_unregister(&omapfb_driver);
1927 }
1928
1929 module_param_named(accel, def_accel, uint, 0664);
1930 module_param_array_named(vram, def_vram, ulong, &def_vram_cnt, 0664);
1931 module_param_named(vxres, def_vxres, long, 0664);
1932 module_param_named(vyres, def_vyres, long, 0664);
1933 module_param_named(rotate, def_rotate, uint, 0664);
1934 module_param_named(mirror, def_mirror, uint, 0664);
1935 module_param_named(manual_update, manual_update, bool, 0664);
1936
1937 module_init(omapfb_init);
1938 module_exit(omapfb_cleanup);
1939
1940 MODULE_DESCRIPTION("TI OMAP framebuffer driver");
1941 MODULE_AUTHOR("Imre Deak <imre.deak@nokia.com>");
1942 MODULE_LICENSE("GPL");