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