Linux 2.6.34-rc3
[pohmelfs.git] / drivers / video / omap / omapfb_main.c
blob8ce60e1b220a23f366a9eb886a0460efe634a4b5
1 /*
2 * Framebuffer driver for TI OMAP boards
4 * Copyright (C) 2004 Nokia Corporation
5 * Author: Imre Deak <imre.deak@nokia.com>
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
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.
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.
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.
27 #include <linux/platform_device.h>
28 #include <linux/mm.h>
29 #include <linux/uaccess.h>
31 #include <plat/dma.h>
33 #include "omapfb.h"
34 #include "lcdc.h"
35 #include "dispc.h"
37 #define MODULE_NAME "omapfb"
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;
47 #ifdef CONFIG_FB_OMAP_MANUAL_UPDATE
48 static int manual_update = 1;
49 #else
50 static int manual_update;
51 #endif
53 static struct platform_device *fbdev_pdev;
54 static struct lcd_panel *fbdev_panel;
55 static struct omapfb_device *omapfb_dev;
57 struct caps_table_struct {
58 unsigned long flag;
59 const char *name;
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" },
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", },
86 static void omapdss_release(struct device *dev)
90 /* dummy device for clocks */
91 static struct platform_device omapdss_device = {
92 .name = "omapdss",
93 .id = -1,
94 .dev = {
95 .release = omapdss_release,
100 * ---------------------------------------------------------------------------
101 * LCD panel
102 * ---------------------------------------------------------------------------
104 extern struct lcd_ctrl hwa742_ctrl;
105 extern struct lcd_ctrl blizzard_ctrl;
107 static const struct lcd_ctrl *ctrls[] = {
108 #ifdef CONFIG_ARCH_OMAP1
109 &omap1_int_ctrl,
110 #else
111 &omap2_int_ctrl,
112 #endif
114 #ifdef CONFIG_FB_OMAP_LCDC_HWA742
115 &hwa742_ctrl,
116 #endif
117 #ifdef CONFIG_FB_OMAP_LCDC_BLIZZARD
118 &blizzard_ctrl,
119 #endif
122 #ifdef CONFIG_FB_OMAP_LCDC_EXTERNAL
123 #ifdef CONFIG_ARCH_OMAP1
124 extern struct lcd_ctrl_extif omap1_ext_if;
125 #else
126 extern struct lcd_ctrl_extif omap2_ext_if;
127 #endif
128 #endif
130 static void omapfb_rqueue_lock(struct omapfb_device *fbdev)
132 mutex_lock(&fbdev->rqueue_mutex);
135 static void omapfb_rqueue_unlock(struct omapfb_device *fbdev)
137 mutex_unlock(&fbdev->rqueue_mutex);
141 * ---------------------------------------------------------------------------
142 * LCD controller and LCD DMA
143 * ---------------------------------------------------------------------------
145 /* Lookup table to map elem size to elem type. */
146 static const int dma_elem_type[] = {
148 OMAP_DMA_DATA_TYPE_S8,
149 OMAP_DMA_DATA_TYPE_S16,
151 OMAP_DMA_DATA_TYPE_S32,
155 * Allocate resources needed for LCD controller and LCD DMA operations. Video
156 * memory is allocated from system memory according to the virtual display
157 * size, except if a bigger memory size is specified explicitly as a kernel
158 * parameter.
160 static int ctrl_init(struct omapfb_device *fbdev)
162 int r;
163 int i;
165 /* kernel/module vram parameters override boot tags/board config */
166 if (def_vram_cnt) {
167 for (i = 0; i < def_vram_cnt; i++)
168 fbdev->mem_desc.region[i].size =
169 PAGE_ALIGN(def_vram[i]);
170 fbdev->mem_desc.region_cnt = i;
171 } else {
172 struct omapfb_platform_data *conf;
174 conf = fbdev->dev->platform_data;
175 fbdev->mem_desc = conf->mem_desc;
178 if (!fbdev->mem_desc.region_cnt) {
179 struct lcd_panel *panel = fbdev->panel;
180 int def_size;
181 int bpp = panel->bpp;
183 /* 12 bpp is packed in 16 bits */
184 if (bpp == 12)
185 bpp = 16;
186 def_size = def_vxres * def_vyres * bpp / 8;
187 fbdev->mem_desc.region_cnt = 1;
188 fbdev->mem_desc.region[0].size = PAGE_ALIGN(def_size);
190 r = fbdev->ctrl->init(fbdev, 0, &fbdev->mem_desc);
191 if (r < 0) {
192 dev_err(fbdev->dev, "controller initialization failed (%d)\n",
194 return r;
197 #ifdef DEBUG
198 for (i = 0; i < fbdev->mem_desc.region_cnt; i++) {
199 dev_dbg(fbdev->dev, "region%d phys %08x virt %p size=%lu\n",
201 fbdev->mem_desc.region[i].paddr,
202 fbdev->mem_desc.region[i].vaddr,
203 fbdev->mem_desc.region[i].size);
205 #endif
206 return 0;
209 static void ctrl_cleanup(struct omapfb_device *fbdev)
211 fbdev->ctrl->cleanup();
214 /* Must be called with fbdev->rqueue_mutex held. */
215 static int ctrl_change_mode(struct fb_info *fbi)
217 int r;
218 unsigned long offset;
219 struct omapfb_plane_struct *plane = fbi->par;
220 struct omapfb_device *fbdev = plane->fbdev;
221 struct fb_var_screeninfo *var = &fbi->var;
223 offset = var->yoffset * fbi->fix.line_length +
224 var->xoffset * var->bits_per_pixel / 8;
226 if (fbdev->ctrl->sync)
227 fbdev->ctrl->sync();
228 r = fbdev->ctrl->setup_plane(plane->idx, plane->info.channel_out,
229 offset, var->xres_virtual,
230 plane->info.pos_x, plane->info.pos_y,
231 var->xres, var->yres, plane->color_mode);
232 if (r < 0)
233 return r;
235 if (fbdev->ctrl->set_rotate != NULL) {
236 r = fbdev->ctrl->set_rotate(var->rotate);
237 if (r < 0)
238 return r;
241 if (fbdev->ctrl->set_scale != NULL)
242 r = fbdev->ctrl->set_scale(plane->idx,
243 var->xres, var->yres,
244 plane->info.out_width,
245 plane->info.out_height);
247 return r;
251 * ---------------------------------------------------------------------------
252 * fbdev framework callbacks and the ioctl interface
253 * ---------------------------------------------------------------------------
255 /* Called each time the omapfb device is opened */
256 static int omapfb_open(struct fb_info *info, int user)
258 return 0;
261 static void omapfb_sync(struct fb_info *info);
263 /* Called when the omapfb device is closed. We make sure that any pending
264 * gfx DMA operations are ended, before we return. */
265 static int omapfb_release(struct fb_info *info, int user)
267 omapfb_sync(info);
268 return 0;
271 /* Store a single color palette entry into a pseudo palette or the hardware
272 * palette if one is available. For now we support only 16bpp and thus store
273 * the entry only to the pseudo palette.
275 static int _setcolreg(struct fb_info *info, u_int regno, u_int red, u_int green,
276 u_int blue, u_int transp, int update_hw_pal)
278 struct omapfb_plane_struct *plane = info->par;
279 struct omapfb_device *fbdev = plane->fbdev;
280 struct fb_var_screeninfo *var = &info->var;
281 int r = 0;
283 switch (plane->color_mode) {
284 case OMAPFB_COLOR_YUV422:
285 case OMAPFB_COLOR_YUV420:
286 case OMAPFB_COLOR_YUY422:
287 r = -EINVAL;
288 break;
289 case OMAPFB_COLOR_CLUT_8BPP:
290 case OMAPFB_COLOR_CLUT_4BPP:
291 case OMAPFB_COLOR_CLUT_2BPP:
292 case OMAPFB_COLOR_CLUT_1BPP:
293 if (fbdev->ctrl->setcolreg)
294 r = fbdev->ctrl->setcolreg(regno, red, green, blue,
295 transp, update_hw_pal);
296 /* Fallthrough */
297 case OMAPFB_COLOR_RGB565:
298 case OMAPFB_COLOR_RGB444:
299 if (r != 0)
300 break;
302 if (regno < 0) {
303 r = -EINVAL;
304 break;
307 if (regno < 16) {
308 u16 pal;
309 pal = ((red >> (16 - var->red.length)) <<
310 var->red.offset) |
311 ((green >> (16 - var->green.length)) <<
312 var->green.offset) |
313 (blue >> (16 - var->blue.length));
314 ((u32 *)(info->pseudo_palette))[regno] = pal;
316 break;
317 default:
318 BUG();
320 return r;
323 static int omapfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
324 u_int transp, struct fb_info *info)
326 return _setcolreg(info, regno, red, green, blue, transp, 1);
329 static int omapfb_setcmap(struct fb_cmap *cmap, struct fb_info *info)
331 int count, index, r;
332 u16 *red, *green, *blue, *transp;
333 u16 trans = 0xffff;
335 red = cmap->red;
336 green = cmap->green;
337 blue = cmap->blue;
338 transp = cmap->transp;
339 index = cmap->start;
341 for (count = 0; count < cmap->len; count++) {
342 if (transp)
343 trans = *transp++;
344 r = _setcolreg(info, index++, *red++, *green++, *blue++, trans,
345 count == cmap->len - 1);
346 if (r != 0)
347 return r;
350 return 0;
353 static int omapfb_update_full_screen(struct fb_info *fbi);
355 static int omapfb_blank(int blank, struct fb_info *fbi)
357 struct omapfb_plane_struct *plane = fbi->par;
358 struct omapfb_device *fbdev = plane->fbdev;
359 int do_update = 0;
360 int r = 0;
362 omapfb_rqueue_lock(fbdev);
363 switch (blank) {
364 case FB_BLANK_UNBLANK:
365 if (fbdev->state == OMAPFB_SUSPENDED) {
366 if (fbdev->ctrl->resume)
367 fbdev->ctrl->resume();
368 fbdev->panel->enable(fbdev->panel);
369 fbdev->state = OMAPFB_ACTIVE;
370 if (fbdev->ctrl->get_update_mode() ==
371 OMAPFB_MANUAL_UPDATE)
372 do_update = 1;
374 break;
375 case FB_BLANK_POWERDOWN:
376 if (fbdev->state == OMAPFB_ACTIVE) {
377 fbdev->panel->disable(fbdev->panel);
378 if (fbdev->ctrl->suspend)
379 fbdev->ctrl->suspend();
380 fbdev->state = OMAPFB_SUSPENDED;
382 break;
383 default:
384 r = -EINVAL;
386 omapfb_rqueue_unlock(fbdev);
388 if (r == 0 && do_update)
389 r = omapfb_update_full_screen(fbi);
391 return r;
394 static void omapfb_sync(struct fb_info *fbi)
396 struct omapfb_plane_struct *plane = fbi->par;
397 struct omapfb_device *fbdev = plane->fbdev;
399 omapfb_rqueue_lock(fbdev);
400 if (fbdev->ctrl->sync)
401 fbdev->ctrl->sync();
402 omapfb_rqueue_unlock(fbdev);
406 * Set fb_info.fix fields and also updates fbdev.
407 * When calling this fb_info.var must be set up already.
409 static void set_fb_fix(struct fb_info *fbi, int from_init)
411 struct fb_fix_screeninfo *fix = &fbi->fix;
412 struct fb_var_screeninfo *var = &fbi->var;
413 struct omapfb_plane_struct *plane = fbi->par;
414 struct omapfb_mem_region *rg;
415 int bpp;
417 rg = &plane->fbdev->mem_desc.region[plane->idx];
418 fbi->screen_base = rg->vaddr;
420 if (!from_init) {
421 mutex_lock(&fbi->mm_lock);
422 fix->smem_start = rg->paddr;
423 fix->smem_len = rg->size;
424 mutex_unlock(&fbi->mm_lock);
425 } else {
426 fix->smem_start = rg->paddr;
427 fix->smem_len = rg->size;
430 fix->type = FB_TYPE_PACKED_PIXELS;
431 bpp = var->bits_per_pixel;
432 if (var->nonstd)
433 fix->visual = FB_VISUAL_PSEUDOCOLOR;
434 else switch (var->bits_per_pixel) {
435 case 16:
436 case 12:
437 fix->visual = FB_VISUAL_TRUECOLOR;
438 /* 12bpp is stored in 16 bits */
439 bpp = 16;
440 break;
441 case 1:
442 case 2:
443 case 4:
444 case 8:
445 fix->visual = FB_VISUAL_PSEUDOCOLOR;
446 break;
448 fix->accel = FB_ACCEL_OMAP1610;
449 fix->line_length = var->xres_virtual * bpp / 8;
452 static int set_color_mode(struct omapfb_plane_struct *plane,
453 struct fb_var_screeninfo *var)
455 switch (var->nonstd) {
456 case 0:
457 break;
458 case OMAPFB_COLOR_YUV422:
459 var->bits_per_pixel = 16;
460 plane->color_mode = var->nonstd;
461 return 0;
462 case OMAPFB_COLOR_YUV420:
463 var->bits_per_pixel = 12;
464 plane->color_mode = var->nonstd;
465 return 0;
466 case OMAPFB_COLOR_YUY422:
467 var->bits_per_pixel = 16;
468 plane->color_mode = var->nonstd;
469 return 0;
470 default:
471 return -EINVAL;
474 switch (var->bits_per_pixel) {
475 case 1:
476 plane->color_mode = OMAPFB_COLOR_CLUT_1BPP;
477 return 0;
478 case 2:
479 plane->color_mode = OMAPFB_COLOR_CLUT_2BPP;
480 return 0;
481 case 4:
482 plane->color_mode = OMAPFB_COLOR_CLUT_4BPP;
483 return 0;
484 case 8:
485 plane->color_mode = OMAPFB_COLOR_CLUT_8BPP;
486 return 0;
487 case 12:
488 var->bits_per_pixel = 16;
489 case 16:
490 if (plane->fbdev->panel->bpp == 12)
491 plane->color_mode = OMAPFB_COLOR_RGB444;
492 else
493 plane->color_mode = OMAPFB_COLOR_RGB565;
494 return 0;
495 default:
496 return -EINVAL;
501 * Check the values in var against our capabilities and in case of out of
502 * bound values try to adjust them.
504 static int set_fb_var(struct fb_info *fbi,
505 struct fb_var_screeninfo *var)
507 int bpp;
508 unsigned long max_frame_size;
509 unsigned long line_size;
510 int xres_min, xres_max;
511 int yres_min, yres_max;
512 struct omapfb_plane_struct *plane = fbi->par;
513 struct omapfb_device *fbdev = plane->fbdev;
514 struct lcd_panel *panel = fbdev->panel;
516 if (set_color_mode(plane, var) < 0)
517 return -EINVAL;
519 bpp = var->bits_per_pixel;
520 if (plane->color_mode == OMAPFB_COLOR_RGB444)
521 bpp = 16;
523 switch (var->rotate) {
524 case 0:
525 case 180:
526 xres_min = OMAPFB_PLANE_XRES_MIN;
527 xres_max = panel->x_res;
528 yres_min = OMAPFB_PLANE_YRES_MIN;
529 yres_max = panel->y_res;
530 if (cpu_is_omap15xx()) {
531 var->xres = panel->x_res;
532 var->yres = panel->y_res;
534 break;
535 case 90:
536 case 270:
537 xres_min = OMAPFB_PLANE_YRES_MIN;
538 xres_max = panel->y_res;
539 yres_min = OMAPFB_PLANE_XRES_MIN;
540 yres_max = panel->x_res;
541 if (cpu_is_omap15xx()) {
542 var->xres = panel->y_res;
543 var->yres = panel->x_res;
545 break;
546 default:
547 return -EINVAL;
550 if (var->xres < xres_min)
551 var->xres = xres_min;
552 if (var->yres < yres_min)
553 var->yres = yres_min;
554 if (var->xres > xres_max)
555 var->xres = xres_max;
556 if (var->yres > yres_max)
557 var->yres = yres_max;
559 if (var->xres_virtual < var->xres)
560 var->xres_virtual = var->xres;
561 if (var->yres_virtual < var->yres)
562 var->yres_virtual = var->yres;
563 max_frame_size = fbdev->mem_desc.region[plane->idx].size;
564 line_size = var->xres_virtual * bpp / 8;
565 if (line_size * var->yres_virtual > max_frame_size) {
566 /* Try to keep yres_virtual first */
567 line_size = max_frame_size / var->yres_virtual;
568 var->xres_virtual = line_size * 8 / bpp;
569 if (var->xres_virtual < var->xres) {
570 /* Still doesn't fit. Shrink yres_virtual too */
571 var->xres_virtual = var->xres;
572 line_size = var->xres * bpp / 8;
573 var->yres_virtual = max_frame_size / line_size;
575 /* Recheck this, as the virtual size changed. */
576 if (var->xres_virtual < var->xres)
577 var->xres = var->xres_virtual;
578 if (var->yres_virtual < var->yres)
579 var->yres = var->yres_virtual;
580 if (var->xres < xres_min || var->yres < yres_min)
581 return -EINVAL;
583 if (var->xres + var->xoffset > var->xres_virtual)
584 var->xoffset = var->xres_virtual - var->xres;
585 if (var->yres + var->yoffset > var->yres_virtual)
586 var->yoffset = var->yres_virtual - var->yres;
588 if (plane->color_mode == OMAPFB_COLOR_RGB444) {
589 var->red.offset = 8; var->red.length = 4;
590 var->red.msb_right = 0;
591 var->green.offset = 4; var->green.length = 4;
592 var->green.msb_right = 0;
593 var->blue.offset = 0; var->blue.length = 4;
594 var->blue.msb_right = 0;
595 } else {
596 var->red.offset = 11; var->red.length = 5;
597 var->red.msb_right = 0;
598 var->green.offset = 5; var->green.length = 6;
599 var->green.msb_right = 0;
600 var->blue.offset = 0; var->blue.length = 5;
601 var->blue.msb_right = 0;
604 var->height = -1;
605 var->width = -1;
606 var->grayscale = 0;
608 /* pixclock in ps, the rest in pixclock */
609 var->pixclock = 10000000 / (panel->pixel_clock / 100);
610 var->left_margin = panel->hfp;
611 var->right_margin = panel->hbp;
612 var->upper_margin = panel->vfp;
613 var->lower_margin = panel->vbp;
614 var->hsync_len = panel->hsw;
615 var->vsync_len = panel->vsw;
617 /* TODO: get these from panel->config */
618 var->vmode = FB_VMODE_NONINTERLACED;
619 var->sync = 0;
621 return 0;
625 /* Set rotation (0, 90, 180, 270 degree), and switch to the new mode. */
626 static void omapfb_rotate(struct fb_info *fbi, int rotate)
628 struct omapfb_plane_struct *plane = fbi->par;
629 struct omapfb_device *fbdev = plane->fbdev;
631 omapfb_rqueue_lock(fbdev);
632 if (rotate != fbi->var.rotate) {
633 struct fb_var_screeninfo *new_var = &fbdev->new_var;
635 memcpy(new_var, &fbi->var, sizeof(*new_var));
636 new_var->rotate = rotate;
637 if (set_fb_var(fbi, new_var) == 0 &&
638 memcmp(new_var, &fbi->var, sizeof(*new_var))) {
639 memcpy(&fbi->var, new_var, sizeof(*new_var));
640 ctrl_change_mode(fbi);
643 omapfb_rqueue_unlock(fbdev);
647 * Set new x,y offsets in the virtual display for the visible area and switch
648 * to the new mode.
650 static int omapfb_pan_display(struct fb_var_screeninfo *var,
651 struct fb_info *fbi)
653 struct omapfb_plane_struct *plane = fbi->par;
654 struct omapfb_device *fbdev = plane->fbdev;
655 int r = 0;
657 omapfb_rqueue_lock(fbdev);
658 if (var->xoffset != fbi->var.xoffset ||
659 var->yoffset != fbi->var.yoffset) {
660 struct fb_var_screeninfo *new_var = &fbdev->new_var;
662 memcpy(new_var, &fbi->var, sizeof(*new_var));
663 new_var->xoffset = var->xoffset;
664 new_var->yoffset = var->yoffset;
665 if (set_fb_var(fbi, new_var))
666 r = -EINVAL;
667 else {
668 memcpy(&fbi->var, new_var, sizeof(*new_var));
669 ctrl_change_mode(fbi);
672 omapfb_rqueue_unlock(fbdev);
674 return r;
677 /* Set mirror to vertical axis and switch to the new mode. */
678 static int omapfb_mirror(struct fb_info *fbi, int mirror)
680 struct omapfb_plane_struct *plane = fbi->par;
681 struct omapfb_device *fbdev = plane->fbdev;
682 int r = 0;
684 omapfb_rqueue_lock(fbdev);
685 mirror = mirror ? 1 : 0;
686 if (cpu_is_omap15xx())
687 r = -EINVAL;
688 else if (mirror != plane->info.mirror) {
689 plane->info.mirror = mirror;
690 r = ctrl_change_mode(fbi);
692 omapfb_rqueue_unlock(fbdev);
694 return r;
698 * Check values in var, try to adjust them in case of out of bound values if
699 * possible, or return error.
701 static int omapfb_check_var(struct fb_var_screeninfo *var, struct fb_info *fbi)
703 struct omapfb_plane_struct *plane = fbi->par;
704 struct omapfb_device *fbdev = plane->fbdev;
705 int r;
707 omapfb_rqueue_lock(fbdev);
708 if (fbdev->ctrl->sync != NULL)
709 fbdev->ctrl->sync();
710 r = set_fb_var(fbi, var);
711 omapfb_rqueue_unlock(fbdev);
713 return r;
717 * Switch to a new mode. The parameters for it has been check already by
718 * omapfb_check_var.
720 static int omapfb_set_par(struct fb_info *fbi)
722 struct omapfb_plane_struct *plane = fbi->par;
723 struct omapfb_device *fbdev = plane->fbdev;
724 int r = 0;
726 omapfb_rqueue_lock(fbdev);
727 set_fb_fix(fbi, 0);
728 r = ctrl_change_mode(fbi);
729 omapfb_rqueue_unlock(fbdev);
731 return r;
734 int omapfb_update_window_async(struct fb_info *fbi,
735 struct omapfb_update_window *win,
736 void (*callback)(void *),
737 void *callback_data)
739 int xres, yres;
740 struct omapfb_plane_struct *plane = fbi->par;
741 struct omapfb_device *fbdev = plane->fbdev;
742 struct fb_var_screeninfo *var = &fbi->var;
744 switch (var->rotate) {
745 case 0:
746 case 180:
747 xres = fbdev->panel->x_res;
748 yres = fbdev->panel->y_res;
749 break;
750 case 90:
751 case 270:
752 xres = fbdev->panel->y_res;
753 yres = fbdev->panel->x_res;
754 break;
755 default:
756 return -EINVAL;
759 if (win->x >= xres || win->y >= yres ||
760 win->out_x > xres || win->out_y > yres)
761 return -EINVAL;
763 if (!fbdev->ctrl->update_window ||
764 fbdev->ctrl->get_update_mode() != OMAPFB_MANUAL_UPDATE)
765 return -ENODEV;
767 if (win->x + win->width > xres)
768 win->width = xres - win->x;
769 if (win->y + win->height > yres)
770 win->height = yres - win->y;
771 if (win->out_x + win->out_width > xres)
772 win->out_width = xres - win->out_x;
773 if (win->out_y + win->out_height > yres)
774 win->out_height = yres - win->out_y;
775 if (!win->width || !win->height || !win->out_width || !win->out_height)
776 return 0;
778 return fbdev->ctrl->update_window(fbi, win, callback, callback_data);
780 EXPORT_SYMBOL(omapfb_update_window_async);
782 static int omapfb_update_win(struct fb_info *fbi,
783 struct omapfb_update_window *win)
785 struct omapfb_plane_struct *plane = fbi->par;
786 int ret;
788 omapfb_rqueue_lock(plane->fbdev);
789 ret = omapfb_update_window_async(fbi, win, NULL, NULL);
790 omapfb_rqueue_unlock(plane->fbdev);
792 return ret;
795 static int omapfb_update_full_screen(struct fb_info *fbi)
797 struct omapfb_plane_struct *plane = fbi->par;
798 struct omapfb_device *fbdev = plane->fbdev;
799 struct omapfb_update_window win;
800 int r;
802 if (!fbdev->ctrl->update_window ||
803 fbdev->ctrl->get_update_mode() != OMAPFB_MANUAL_UPDATE)
804 return -ENODEV;
806 win.x = 0;
807 win.y = 0;
808 win.width = fbi->var.xres;
809 win.height = fbi->var.yres;
810 win.out_x = 0;
811 win.out_y = 0;
812 win.out_width = fbi->var.xres;
813 win.out_height = fbi->var.yres;
814 win.format = 0;
816 omapfb_rqueue_lock(fbdev);
817 r = fbdev->ctrl->update_window(fbi, &win, NULL, NULL);
818 omapfb_rqueue_unlock(fbdev);
820 return r;
823 static int omapfb_setup_plane(struct fb_info *fbi, struct omapfb_plane_info *pi)
825 struct omapfb_plane_struct *plane = fbi->par;
826 struct omapfb_device *fbdev = plane->fbdev;
827 struct lcd_panel *panel = fbdev->panel;
828 struct omapfb_plane_info old_info;
829 int r = 0;
831 if (pi->pos_x + pi->out_width > panel->x_res ||
832 pi->pos_y + pi->out_height > panel->y_res)
833 return -EINVAL;
835 omapfb_rqueue_lock(fbdev);
836 if (pi->enabled && !fbdev->mem_desc.region[plane->idx].size) {
838 * This plane's memory was freed, can't enable it
839 * until it's reallocated.
841 r = -EINVAL;
842 goto out;
844 old_info = plane->info;
845 plane->info = *pi;
846 if (pi->enabled) {
847 r = ctrl_change_mode(fbi);
848 if (r < 0) {
849 plane->info = old_info;
850 goto out;
853 r = fbdev->ctrl->enable_plane(plane->idx, pi->enabled);
854 if (r < 0) {
855 plane->info = old_info;
856 goto out;
858 out:
859 omapfb_rqueue_unlock(fbdev);
860 return r;
863 static int omapfb_query_plane(struct fb_info *fbi, struct omapfb_plane_info *pi)
865 struct omapfb_plane_struct *plane = fbi->par;
867 *pi = plane->info;
868 return 0;
871 static int omapfb_setup_mem(struct fb_info *fbi, struct omapfb_mem_info *mi)
873 struct omapfb_plane_struct *plane = fbi->par;
874 struct omapfb_device *fbdev = plane->fbdev;
875 struct omapfb_mem_region *rg = &fbdev->mem_desc.region[plane->idx];
876 size_t size;
877 int r = 0;
879 if (fbdev->ctrl->setup_mem == NULL)
880 return -ENODEV;
881 if (mi->type > OMAPFB_MEMTYPE_MAX)
882 return -EINVAL;
884 size = PAGE_ALIGN(mi->size);
885 omapfb_rqueue_lock(fbdev);
886 if (plane->info.enabled) {
887 r = -EBUSY;
888 goto out;
890 if (rg->size != size || rg->type != mi->type) {
891 struct fb_var_screeninfo *new_var = &fbdev->new_var;
892 unsigned long old_size = rg->size;
893 u8 old_type = rg->type;
894 unsigned long paddr;
896 rg->size = size;
897 rg->type = mi->type;
899 * size == 0 is a special case, for which we
900 * don't check / adjust the screen parameters.
901 * This isn't a problem since the plane can't
902 * be reenabled unless its size is > 0.
904 if (old_size != size && size) {
905 if (size) {
906 memcpy(new_var, &fbi->var, sizeof(*new_var));
907 r = set_fb_var(fbi, new_var);
908 if (r < 0)
909 goto out;
913 if (fbdev->ctrl->sync)
914 fbdev->ctrl->sync();
915 r = fbdev->ctrl->setup_mem(plane->idx, size, mi->type, &paddr);
916 if (r < 0) {
917 /* Revert changes. */
918 rg->size = old_size;
919 rg->type = old_type;
920 goto out;
922 rg->paddr = paddr;
924 if (old_size != size) {
925 if (size) {
926 memcpy(&fbi->var, new_var, sizeof(fbi->var));
927 set_fb_fix(fbi, 0);
928 } else {
930 * Set these explicitly to indicate that the
931 * plane memory is dealloce'd, the other
932 * screen parameters in var / fix are invalid.
934 mutex_lock(&fbi->mm_lock);
935 fbi->fix.smem_start = 0;
936 fbi->fix.smem_len = 0;
937 mutex_unlock(&fbi->mm_lock);
941 out:
942 omapfb_rqueue_unlock(fbdev);
944 return r;
947 static int omapfb_query_mem(struct fb_info *fbi, struct omapfb_mem_info *mi)
949 struct omapfb_plane_struct *plane = fbi->par;
950 struct omapfb_device *fbdev = plane->fbdev;
951 struct omapfb_mem_region *rg;
953 rg = &fbdev->mem_desc.region[plane->idx];
954 memset(mi, 0, sizeof(*mi));
955 mi->size = rg->size;
956 mi->type = rg->type;
958 return 0;
961 static int omapfb_set_color_key(struct omapfb_device *fbdev,
962 struct omapfb_color_key *ck)
964 int r;
966 if (!fbdev->ctrl->set_color_key)
967 return -ENODEV;
969 omapfb_rqueue_lock(fbdev);
970 r = fbdev->ctrl->set_color_key(ck);
971 omapfb_rqueue_unlock(fbdev);
973 return r;
976 static int omapfb_get_color_key(struct omapfb_device *fbdev,
977 struct omapfb_color_key *ck)
979 int r;
981 if (!fbdev->ctrl->get_color_key)
982 return -ENODEV;
984 omapfb_rqueue_lock(fbdev);
985 r = fbdev->ctrl->get_color_key(ck);
986 omapfb_rqueue_unlock(fbdev);
988 return r;
991 static struct blocking_notifier_head omapfb_client_list[OMAPFB_PLANE_NUM];
992 static int notifier_inited;
994 static void omapfb_init_notifier(void)
996 int i;
998 for (i = 0; i < OMAPFB_PLANE_NUM; i++)
999 BLOCKING_INIT_NOTIFIER_HEAD(&omapfb_client_list[i]);
1002 int omapfb_register_client(struct omapfb_notifier_block *omapfb_nb,
1003 omapfb_notifier_callback_t callback,
1004 void *callback_data)
1006 int r;
1008 if ((unsigned)omapfb_nb->plane_idx > OMAPFB_PLANE_NUM)
1009 return -EINVAL;
1011 if (!notifier_inited) {
1012 omapfb_init_notifier();
1013 notifier_inited = 1;
1016 omapfb_nb->nb.notifier_call = (int (*)(struct notifier_block *,
1017 unsigned long, void *))callback;
1018 omapfb_nb->data = callback_data;
1019 r = blocking_notifier_chain_register(
1020 &omapfb_client_list[omapfb_nb->plane_idx],
1021 &omapfb_nb->nb);
1022 if (r)
1023 return r;
1024 if (omapfb_dev != NULL &&
1025 omapfb_dev->ctrl && omapfb_dev->ctrl->bind_client) {
1026 omapfb_dev->ctrl->bind_client(omapfb_nb);
1029 return 0;
1031 EXPORT_SYMBOL(omapfb_register_client);
1033 int omapfb_unregister_client(struct omapfb_notifier_block *omapfb_nb)
1035 return blocking_notifier_chain_unregister(
1036 &omapfb_client_list[omapfb_nb->plane_idx], &omapfb_nb->nb);
1038 EXPORT_SYMBOL(omapfb_unregister_client);
1040 void omapfb_notify_clients(struct omapfb_device *fbdev, unsigned long event)
1042 int i;
1044 if (!notifier_inited)
1045 /* no client registered yet */
1046 return;
1048 for (i = 0; i < OMAPFB_PLANE_NUM; i++)
1049 blocking_notifier_call_chain(&omapfb_client_list[i], event,
1050 fbdev->fb_info[i]);
1052 EXPORT_SYMBOL(omapfb_notify_clients);
1054 static int omapfb_set_update_mode(struct omapfb_device *fbdev,
1055 enum omapfb_update_mode mode)
1057 int r;
1059 omapfb_rqueue_lock(fbdev);
1060 r = fbdev->ctrl->set_update_mode(mode);
1061 omapfb_rqueue_unlock(fbdev);
1063 return r;
1066 static enum omapfb_update_mode omapfb_get_update_mode(struct omapfb_device *fbdev)
1068 int r;
1070 omapfb_rqueue_lock(fbdev);
1071 r = fbdev->ctrl->get_update_mode();
1072 omapfb_rqueue_unlock(fbdev);
1074 return r;
1077 static void omapfb_get_caps(struct omapfb_device *fbdev, int plane,
1078 struct omapfb_caps *caps)
1080 memset(caps, 0, sizeof(*caps));
1081 fbdev->ctrl->get_caps(plane, caps);
1082 caps->ctrl |= fbdev->panel->get_caps(fbdev->panel);
1085 /* For lcd testing */
1086 void omapfb_write_first_pixel(struct omapfb_device *fbdev, u16 pixval)
1088 omapfb_rqueue_lock(fbdev);
1089 *(u16 *)fbdev->mem_desc.region[0].vaddr = pixval;
1090 if (fbdev->ctrl->get_update_mode() == OMAPFB_MANUAL_UPDATE) {
1091 struct omapfb_update_window win;
1093 memset(&win, 0, sizeof(win));
1094 win.width = 2;
1095 win.height = 2;
1096 win.out_width = 2;
1097 win.out_height = 2;
1098 fbdev->ctrl->update_window(fbdev->fb_info[0], &win, NULL, NULL);
1100 omapfb_rqueue_unlock(fbdev);
1102 EXPORT_SYMBOL(omapfb_write_first_pixel);
1105 * Ioctl interface. Part of the kernel mode frame buffer API is duplicated
1106 * here to be accessible by user mode code.
1108 static int omapfb_ioctl(struct fb_info *fbi, unsigned int cmd,
1109 unsigned long arg)
1111 struct omapfb_plane_struct *plane = fbi->par;
1112 struct omapfb_device *fbdev = plane->fbdev;
1113 struct fb_ops *ops = fbi->fbops;
1114 union {
1115 struct omapfb_update_window update_window;
1116 struct omapfb_plane_info plane_info;
1117 struct omapfb_mem_info mem_info;
1118 struct omapfb_color_key color_key;
1119 enum omapfb_update_mode update_mode;
1120 struct omapfb_caps caps;
1121 unsigned int mirror;
1122 int plane_out;
1123 int enable_plane;
1124 } p;
1125 int r = 0;
1127 BUG_ON(!ops);
1128 switch (cmd) {
1129 case OMAPFB_MIRROR:
1130 if (get_user(p.mirror, (int __user *)arg))
1131 r = -EFAULT;
1132 else
1133 omapfb_mirror(fbi, p.mirror);
1134 break;
1135 case OMAPFB_SYNC_GFX:
1136 omapfb_sync(fbi);
1137 break;
1138 case OMAPFB_VSYNC:
1139 break;
1140 case OMAPFB_SET_UPDATE_MODE:
1141 if (get_user(p.update_mode, (int __user *)arg))
1142 r = -EFAULT;
1143 else
1144 r = omapfb_set_update_mode(fbdev, p.update_mode);
1145 break;
1146 case OMAPFB_GET_UPDATE_MODE:
1147 p.update_mode = omapfb_get_update_mode(fbdev);
1148 if (put_user(p.update_mode,
1149 (enum omapfb_update_mode __user *)arg))
1150 r = -EFAULT;
1151 break;
1152 case OMAPFB_UPDATE_WINDOW_OLD:
1153 if (copy_from_user(&p.update_window, (void __user *)arg,
1154 sizeof(struct omapfb_update_window_old)))
1155 r = -EFAULT;
1156 else {
1157 struct omapfb_update_window *u = &p.update_window;
1158 u->out_x = u->x;
1159 u->out_y = u->y;
1160 u->out_width = u->width;
1161 u->out_height = u->height;
1162 memset(u->reserved, 0, sizeof(u->reserved));
1163 r = omapfb_update_win(fbi, u);
1165 break;
1166 case OMAPFB_UPDATE_WINDOW:
1167 if (copy_from_user(&p.update_window, (void __user *)arg,
1168 sizeof(p.update_window)))
1169 r = -EFAULT;
1170 else
1171 r = omapfb_update_win(fbi, &p.update_window);
1172 break;
1173 case OMAPFB_SETUP_PLANE:
1174 if (copy_from_user(&p.plane_info, (void __user *)arg,
1175 sizeof(p.plane_info)))
1176 r = -EFAULT;
1177 else
1178 r = omapfb_setup_plane(fbi, &p.plane_info);
1179 break;
1180 case OMAPFB_QUERY_PLANE:
1181 if ((r = omapfb_query_plane(fbi, &p.plane_info)) < 0)
1182 break;
1183 if (copy_to_user((void __user *)arg, &p.plane_info,
1184 sizeof(p.plane_info)))
1185 r = -EFAULT;
1186 break;
1187 case OMAPFB_SETUP_MEM:
1188 if (copy_from_user(&p.mem_info, (void __user *)arg,
1189 sizeof(p.mem_info)))
1190 r = -EFAULT;
1191 else
1192 r = omapfb_setup_mem(fbi, &p.mem_info);
1193 break;
1194 case OMAPFB_QUERY_MEM:
1195 if ((r = omapfb_query_mem(fbi, &p.mem_info)) < 0)
1196 break;
1197 if (copy_to_user((void __user *)arg, &p.mem_info,
1198 sizeof(p.mem_info)))
1199 r = -EFAULT;
1200 break;
1201 case OMAPFB_SET_COLOR_KEY:
1202 if (copy_from_user(&p.color_key, (void __user *)arg,
1203 sizeof(p.color_key)))
1204 r = -EFAULT;
1205 else
1206 r = omapfb_set_color_key(fbdev, &p.color_key);
1207 break;
1208 case OMAPFB_GET_COLOR_KEY:
1209 if ((r = omapfb_get_color_key(fbdev, &p.color_key)) < 0)
1210 break;
1211 if (copy_to_user((void __user *)arg, &p.color_key,
1212 sizeof(p.color_key)))
1213 r = -EFAULT;
1214 break;
1215 case OMAPFB_GET_CAPS:
1216 omapfb_get_caps(fbdev, plane->idx, &p.caps);
1217 if (copy_to_user((void __user *)arg, &p.caps, sizeof(p.caps)))
1218 r = -EFAULT;
1219 break;
1220 case OMAPFB_LCD_TEST:
1222 int test_num;
1224 if (get_user(test_num, (int __user *)arg)) {
1225 r = -EFAULT;
1226 break;
1228 if (!fbdev->panel->run_test) {
1229 r = -EINVAL;
1230 break;
1232 r = fbdev->panel->run_test(fbdev->panel, test_num);
1233 break;
1235 case OMAPFB_CTRL_TEST:
1237 int test_num;
1239 if (get_user(test_num, (int __user *)arg)) {
1240 r = -EFAULT;
1241 break;
1243 if (!fbdev->ctrl->run_test) {
1244 r = -EINVAL;
1245 break;
1247 r = fbdev->ctrl->run_test(test_num);
1248 break;
1250 default:
1251 r = -EINVAL;
1254 return r;
1257 static int omapfb_mmap(struct fb_info *info, struct vm_area_struct *vma)
1259 struct omapfb_plane_struct *plane = info->par;
1260 struct omapfb_device *fbdev = plane->fbdev;
1261 int r;
1263 omapfb_rqueue_lock(fbdev);
1264 r = fbdev->ctrl->mmap(info, vma);
1265 omapfb_rqueue_unlock(fbdev);
1267 return r;
1271 * Callback table for the frame buffer framework. Some of these pointers
1272 * will be changed according to the current setting of fb_info->accel_flags.
1274 static struct fb_ops omapfb_ops = {
1275 .owner = THIS_MODULE,
1276 .fb_open = omapfb_open,
1277 .fb_release = omapfb_release,
1278 .fb_setcolreg = omapfb_setcolreg,
1279 .fb_setcmap = omapfb_setcmap,
1280 .fb_fillrect = cfb_fillrect,
1281 .fb_copyarea = cfb_copyarea,
1282 .fb_imageblit = cfb_imageblit,
1283 .fb_blank = omapfb_blank,
1284 .fb_ioctl = omapfb_ioctl,
1285 .fb_check_var = omapfb_check_var,
1286 .fb_set_par = omapfb_set_par,
1287 .fb_rotate = omapfb_rotate,
1288 .fb_pan_display = omapfb_pan_display,
1292 * ---------------------------------------------------------------------------
1293 * Sysfs interface
1294 * ---------------------------------------------------------------------------
1296 /* omapfbX sysfs entries */
1297 static ssize_t omapfb_show_caps_num(struct device *dev,
1298 struct device_attribute *attr, char *buf)
1300 struct omapfb_device *fbdev = dev_get_drvdata(dev);
1301 int plane;
1302 size_t size;
1303 struct omapfb_caps caps;
1305 plane = 0;
1306 size = 0;
1307 while (size < PAGE_SIZE && plane < OMAPFB_PLANE_NUM) {
1308 omapfb_get_caps(fbdev, plane, &caps);
1309 size += snprintf(&buf[size], PAGE_SIZE - size,
1310 "plane#%d %#010x %#010x %#010x\n",
1311 plane, caps.ctrl, caps.plane_color, caps.wnd_color);
1312 plane++;
1314 return size;
1317 static ssize_t omapfb_show_caps_text(struct device *dev,
1318 struct device_attribute *attr, char *buf)
1320 struct omapfb_device *fbdev = dev_get_drvdata(dev);
1321 int i;
1322 struct omapfb_caps caps;
1323 int plane;
1324 size_t size;
1326 plane = 0;
1327 size = 0;
1328 while (size < PAGE_SIZE && plane < OMAPFB_PLANE_NUM) {
1329 omapfb_get_caps(fbdev, plane, &caps);
1330 size += snprintf(&buf[size], PAGE_SIZE - size,
1331 "plane#%d:\n", plane);
1332 for (i = 0; i < ARRAY_SIZE(ctrl_caps) &&
1333 size < PAGE_SIZE; i++) {
1334 if (ctrl_caps[i].flag & caps.ctrl)
1335 size += snprintf(&buf[size], PAGE_SIZE - size,
1336 " %s\n", ctrl_caps[i].name);
1338 size += snprintf(&buf[size], PAGE_SIZE - size,
1339 " plane colors:\n");
1340 for (i = 0; i < ARRAY_SIZE(color_caps) &&
1341 size < PAGE_SIZE; i++) {
1342 if (color_caps[i].flag & caps.plane_color)
1343 size += snprintf(&buf[size], PAGE_SIZE - size,
1344 " %s\n", color_caps[i].name);
1346 size += snprintf(&buf[size], PAGE_SIZE - size,
1347 " window colors:\n");
1348 for (i = 0; i < ARRAY_SIZE(color_caps) &&
1349 size < PAGE_SIZE; i++) {
1350 if (color_caps[i].flag & caps.wnd_color)
1351 size += snprintf(&buf[size], PAGE_SIZE - size,
1352 " %s\n", color_caps[i].name);
1355 plane++;
1357 return size;
1360 static DEVICE_ATTR(caps_num, 0444, omapfb_show_caps_num, NULL);
1361 static DEVICE_ATTR(caps_text, 0444, omapfb_show_caps_text, NULL);
1363 /* panel sysfs entries */
1364 static ssize_t omapfb_show_panel_name(struct device *dev,
1365 struct device_attribute *attr, char *buf)
1367 struct omapfb_device *fbdev = dev_get_drvdata(dev);
1369 return snprintf(buf, PAGE_SIZE, "%s\n", fbdev->panel->name);
1372 static ssize_t omapfb_show_bklight_level(struct device *dev,
1373 struct device_attribute *attr,
1374 char *buf)
1376 struct omapfb_device *fbdev = dev_get_drvdata(dev);
1377 int r;
1379 if (fbdev->panel->get_bklight_level) {
1380 r = snprintf(buf, PAGE_SIZE, "%d\n",
1381 fbdev->panel->get_bklight_level(fbdev->panel));
1382 } else
1383 r = -ENODEV;
1384 return r;
1387 static ssize_t omapfb_store_bklight_level(struct device *dev,
1388 struct device_attribute *attr,
1389 const char *buf, size_t size)
1391 struct omapfb_device *fbdev = dev_get_drvdata(dev);
1392 int r;
1394 if (fbdev->panel->set_bklight_level) {
1395 unsigned int level;
1397 if (sscanf(buf, "%10d", &level) == 1) {
1398 r = fbdev->panel->set_bklight_level(fbdev->panel,
1399 level);
1400 } else
1401 r = -EINVAL;
1402 } else
1403 r = -ENODEV;
1404 return r ? r : size;
1407 static ssize_t omapfb_show_bklight_max(struct device *dev,
1408 struct device_attribute *attr, char *buf)
1410 struct omapfb_device *fbdev = dev_get_drvdata(dev);
1411 int r;
1413 if (fbdev->panel->get_bklight_level) {
1414 r = snprintf(buf, PAGE_SIZE, "%d\n",
1415 fbdev->panel->get_bklight_max(fbdev->panel));
1416 } else
1417 r = -ENODEV;
1418 return r;
1421 static struct device_attribute dev_attr_panel_name =
1422 __ATTR(name, 0444, omapfb_show_panel_name, NULL);
1423 static DEVICE_ATTR(backlight_level, 0664,
1424 omapfb_show_bklight_level, omapfb_store_bklight_level);
1425 static DEVICE_ATTR(backlight_max, 0444, omapfb_show_bklight_max, NULL);
1427 static struct attribute *panel_attrs[] = {
1428 &dev_attr_panel_name.attr,
1429 &dev_attr_backlight_level.attr,
1430 &dev_attr_backlight_max.attr,
1431 NULL,
1434 static struct attribute_group panel_attr_grp = {
1435 .name = "panel",
1436 .attrs = panel_attrs,
1439 /* ctrl sysfs entries */
1440 static ssize_t omapfb_show_ctrl_name(struct device *dev,
1441 struct device_attribute *attr, char *buf)
1443 struct omapfb_device *fbdev = dev_get_drvdata(dev);
1445 return snprintf(buf, PAGE_SIZE, "%s\n", fbdev->ctrl->name);
1448 static struct device_attribute dev_attr_ctrl_name =
1449 __ATTR(name, 0444, omapfb_show_ctrl_name, NULL);
1451 static struct attribute *ctrl_attrs[] = {
1452 &dev_attr_ctrl_name.attr,
1453 NULL,
1456 static struct attribute_group ctrl_attr_grp = {
1457 .name = "ctrl",
1458 .attrs = ctrl_attrs,
1461 static int omapfb_register_sysfs(struct omapfb_device *fbdev)
1463 int r;
1465 if ((r = device_create_file(fbdev->dev, &dev_attr_caps_num)))
1466 goto fail0;
1468 if ((r = device_create_file(fbdev->dev, &dev_attr_caps_text)))
1469 goto fail1;
1471 if ((r = sysfs_create_group(&fbdev->dev->kobj, &panel_attr_grp)))
1472 goto fail2;
1474 if ((r = sysfs_create_group(&fbdev->dev->kobj, &ctrl_attr_grp)))
1475 goto fail3;
1477 return 0;
1478 fail3:
1479 sysfs_remove_group(&fbdev->dev->kobj, &panel_attr_grp);
1480 fail2:
1481 device_remove_file(fbdev->dev, &dev_attr_caps_text);
1482 fail1:
1483 device_remove_file(fbdev->dev, &dev_attr_caps_num);
1484 fail0:
1485 dev_err(fbdev->dev, "unable to register sysfs interface\n");
1486 return r;
1489 static void omapfb_unregister_sysfs(struct omapfb_device *fbdev)
1491 sysfs_remove_group(&fbdev->dev->kobj, &ctrl_attr_grp);
1492 sysfs_remove_group(&fbdev->dev->kobj, &panel_attr_grp);
1493 device_remove_file(fbdev->dev, &dev_attr_caps_num);
1494 device_remove_file(fbdev->dev, &dev_attr_caps_text);
1498 * ---------------------------------------------------------------------------
1499 * LDM callbacks
1500 * ---------------------------------------------------------------------------
1502 /* Initialize system fb_info object and set the default video mode.
1503 * The frame buffer memory already allocated by lcddma_init
1505 static int fbinfo_init(struct omapfb_device *fbdev, struct fb_info *info)
1507 struct fb_var_screeninfo *var = &info->var;
1508 struct fb_fix_screeninfo *fix = &info->fix;
1509 int r = 0;
1511 info->fbops = &omapfb_ops;
1512 info->flags = FBINFO_FLAG_DEFAULT;
1514 strncpy(fix->id, MODULE_NAME, sizeof(fix->id));
1516 info->pseudo_palette = fbdev->pseudo_palette;
1518 var->accel_flags = def_accel ? FB_ACCELF_TEXT : 0;
1519 var->xres = def_vxres;
1520 var->yres = def_vyres;
1521 var->xres_virtual = def_vxres;
1522 var->yres_virtual = def_vyres;
1523 var->rotate = def_rotate;
1524 var->bits_per_pixel = fbdev->panel->bpp;
1526 set_fb_var(info, var);
1527 set_fb_fix(info, 1);
1529 r = fb_alloc_cmap(&info->cmap, 16, 0);
1530 if (r != 0)
1531 dev_err(fbdev->dev, "unable to allocate color map memory\n");
1533 return r;
1536 /* Release the fb_info object */
1537 static void fbinfo_cleanup(struct omapfb_device *fbdev, struct fb_info *fbi)
1539 fb_dealloc_cmap(&fbi->cmap);
1542 static void planes_cleanup(struct omapfb_device *fbdev)
1544 int i;
1546 for (i = 0; i < fbdev->mem_desc.region_cnt; i++) {
1547 if (fbdev->fb_info[i] == NULL)
1548 break;
1549 fbinfo_cleanup(fbdev, fbdev->fb_info[i]);
1550 framebuffer_release(fbdev->fb_info[i]);
1554 static int planes_init(struct omapfb_device *fbdev)
1556 struct fb_info *fbi;
1557 int i;
1558 int r;
1560 for (i = 0; i < fbdev->mem_desc.region_cnt; i++) {
1561 struct omapfb_plane_struct *plane;
1562 fbi = framebuffer_alloc(sizeof(struct omapfb_plane_struct),
1563 fbdev->dev);
1564 if (fbi == NULL) {
1565 dev_err(fbdev->dev,
1566 "unable to allocate memory for plane info\n");
1567 planes_cleanup(fbdev);
1568 return -ENOMEM;
1570 plane = fbi->par;
1571 plane->idx = i;
1572 plane->fbdev = fbdev;
1573 plane->info.mirror = def_mirror;
1574 fbdev->fb_info[i] = fbi;
1576 if ((r = fbinfo_init(fbdev, fbi)) < 0) {
1577 framebuffer_release(fbi);
1578 planes_cleanup(fbdev);
1579 return r;
1581 plane->info.out_width = fbi->var.xres;
1582 plane->info.out_height = fbi->var.yres;
1584 return 0;
1588 * Free driver resources. Can be called to rollback an aborted initialization
1589 * sequence.
1591 static void omapfb_free_resources(struct omapfb_device *fbdev, int state)
1593 int i;
1595 switch (state) {
1596 case OMAPFB_ACTIVE:
1597 for (i = 0; i < fbdev->mem_desc.region_cnt; i++)
1598 unregister_framebuffer(fbdev->fb_info[i]);
1599 case 7:
1600 omapfb_unregister_sysfs(fbdev);
1601 case 6:
1602 fbdev->panel->disable(fbdev->panel);
1603 case 5:
1604 omapfb_set_update_mode(fbdev, OMAPFB_UPDATE_DISABLED);
1605 case 4:
1606 planes_cleanup(fbdev);
1607 case 3:
1608 ctrl_cleanup(fbdev);
1609 case 2:
1610 fbdev->panel->cleanup(fbdev->panel);
1611 case 1:
1612 dev_set_drvdata(fbdev->dev, NULL);
1613 kfree(fbdev);
1614 case 0:
1615 /* nothing to free */
1616 break;
1617 default:
1618 BUG();
1622 static int omapfb_find_ctrl(struct omapfb_device *fbdev)
1624 struct omapfb_platform_data *conf;
1625 char name[17];
1626 int i;
1628 conf = fbdev->dev->platform_data;
1630 fbdev->ctrl = NULL;
1632 strncpy(name, conf->lcd.ctrl_name, sizeof(name) - 1);
1633 name[sizeof(name) - 1] = '\0';
1635 if (strcmp(name, "internal") == 0) {
1636 fbdev->ctrl = fbdev->int_ctrl;
1637 return 0;
1640 for (i = 0; i < ARRAY_SIZE(ctrls); i++) {
1641 dev_dbg(fbdev->dev, "ctrl %s\n", ctrls[i]->name);
1642 if (strcmp(ctrls[i]->name, name) == 0) {
1643 fbdev->ctrl = ctrls[i];
1644 break;
1648 if (fbdev->ctrl == NULL) {
1649 dev_dbg(fbdev->dev, "ctrl %s not supported\n", name);
1650 return -1;
1653 return 0;
1656 static void check_required_callbacks(struct omapfb_device *fbdev)
1658 #define _C(x) (fbdev->ctrl->x != NULL)
1659 #define _P(x) (fbdev->panel->x != NULL)
1660 BUG_ON(fbdev->ctrl == NULL || fbdev->panel == NULL);
1661 BUG_ON(!(_C(init) && _C(cleanup) && _C(get_caps) &&
1662 _C(set_update_mode) && _C(setup_plane) && _C(enable_plane) &&
1663 _P(init) && _P(cleanup) && _P(enable) && _P(disable) &&
1664 _P(get_caps)));
1665 #undef _P
1666 #undef _C
1670 * Called by LDM binding to probe and attach a new device.
1671 * Initialization sequence:
1672 * 1. allocate system omapfb_device structure
1673 * 2. select controller type according to platform configuration
1674 * init LCD panel
1675 * 3. init LCD controller and LCD DMA
1676 * 4. init system fb_info structure for all planes
1677 * 5. setup video mode for first plane and enable it
1678 * 6. enable LCD panel
1679 * 7. register sysfs attributes
1680 * OMAPFB_ACTIVE: register system fb_info structure for all planes
1682 static int omapfb_do_probe(struct platform_device *pdev,
1683 struct lcd_panel *panel)
1685 struct omapfb_device *fbdev = NULL;
1686 int init_state;
1687 unsigned long phz, hhz, vhz;
1688 unsigned long vram;
1689 int i;
1690 int r = 0;
1692 init_state = 0;
1694 if (pdev->num_resources != 0) {
1695 dev_err(&pdev->dev, "probed for an unknown device\n");
1696 r = -ENODEV;
1697 goto cleanup;
1700 if (pdev->dev.platform_data == NULL) {
1701 dev_err(&pdev->dev, "missing platform data\n");
1702 r = -ENOENT;
1703 goto cleanup;
1706 fbdev = kzalloc(sizeof(struct omapfb_device), GFP_KERNEL);
1707 if (fbdev == NULL) {
1708 dev_err(&pdev->dev,
1709 "unable to allocate memory for device info\n");
1710 r = -ENOMEM;
1711 goto cleanup;
1713 init_state++;
1715 fbdev->dev = &pdev->dev;
1716 fbdev->panel = panel;
1717 fbdev->dssdev = &omapdss_device;
1718 platform_set_drvdata(pdev, fbdev);
1720 mutex_init(&fbdev->rqueue_mutex);
1722 #ifdef CONFIG_ARCH_OMAP1
1723 fbdev->int_ctrl = &omap1_int_ctrl;
1724 #ifdef CONFIG_FB_OMAP_LCDC_EXTERNAL
1725 fbdev->ext_if = &omap1_ext_if;
1726 #endif
1727 #else /* OMAP2 */
1728 fbdev->int_ctrl = &omap2_int_ctrl;
1729 #ifdef CONFIG_FB_OMAP_LCDC_EXTERNAL
1730 fbdev->ext_if = &omap2_ext_if;
1731 #endif
1732 #endif
1733 if (omapfb_find_ctrl(fbdev) < 0) {
1734 dev_err(fbdev->dev,
1735 "LCD controller not found, board not supported\n");
1736 r = -ENODEV;
1737 goto cleanup;
1740 r = fbdev->panel->init(fbdev->panel, fbdev);
1741 if (r)
1742 goto cleanup;
1744 pr_info("omapfb: configured for panel %s\n", fbdev->panel->name);
1746 def_vxres = def_vxres ? def_vxres : fbdev->panel->x_res;
1747 def_vyres = def_vyres ? def_vyres : fbdev->panel->y_res;
1749 init_state++;
1751 r = ctrl_init(fbdev);
1752 if (r)
1753 goto cleanup;
1754 if (fbdev->ctrl->mmap != NULL)
1755 omapfb_ops.fb_mmap = omapfb_mmap;
1756 init_state++;
1758 check_required_callbacks(fbdev);
1760 r = planes_init(fbdev);
1761 if (r)
1762 goto cleanup;
1763 init_state++;
1765 #ifdef CONFIG_FB_OMAP_DMA_TUNE
1766 /* Set DMA priority for EMIFF access to highest */
1767 if (cpu_class_is_omap1())
1768 omap_set_dma_priority(0, OMAP_DMA_PORT_EMIFF, 15);
1769 #endif
1771 r = ctrl_change_mode(fbdev->fb_info[0]);
1772 if (r) {
1773 dev_err(fbdev->dev, "mode setting failed\n");
1774 goto cleanup;
1777 /* GFX plane is enabled by default */
1778 r = fbdev->ctrl->enable_plane(OMAPFB_PLANE_GFX, 1);
1779 if (r)
1780 goto cleanup;
1782 omapfb_set_update_mode(fbdev, manual_update ?
1783 OMAPFB_MANUAL_UPDATE : OMAPFB_AUTO_UPDATE);
1784 init_state++;
1786 r = fbdev->panel->enable(fbdev->panel);
1787 if (r)
1788 goto cleanup;
1789 init_state++;
1791 r = omapfb_register_sysfs(fbdev);
1792 if (r)
1793 goto cleanup;
1794 init_state++;
1796 vram = 0;
1797 for (i = 0; i < fbdev->mem_desc.region_cnt; i++) {
1798 r = register_framebuffer(fbdev->fb_info[i]);
1799 if (r != 0) {
1800 dev_err(fbdev->dev,
1801 "registering framebuffer %d failed\n", i);
1802 goto cleanup;
1804 vram += fbdev->mem_desc.region[i].size;
1807 fbdev->state = OMAPFB_ACTIVE;
1809 panel = fbdev->panel;
1810 phz = panel->pixel_clock * 1000;
1811 hhz = phz * 10 / (panel->hfp + panel->x_res + panel->hbp + panel->hsw);
1812 vhz = hhz / (panel->vfp + panel->y_res + panel->vbp + panel->vsw);
1814 omapfb_dev = fbdev;
1816 pr_info("omapfb: Framebuffer initialized. Total vram %lu planes %d\n",
1817 vram, fbdev->mem_desc.region_cnt);
1818 pr_info("omapfb: Pixclock %lu kHz hfreq %lu.%lu kHz "
1819 "vfreq %lu.%lu Hz\n",
1820 phz / 1000, hhz / 10000, hhz % 10, vhz / 10, vhz % 10);
1822 return 0;
1824 cleanup:
1825 omapfb_free_resources(fbdev, init_state);
1827 return r;
1830 static int omapfb_probe(struct platform_device *pdev)
1832 int r;
1834 BUG_ON(fbdev_pdev != NULL);
1836 r = platform_device_register(&omapdss_device);
1837 if (r) {
1838 dev_err(&pdev->dev, "can't register omapdss device\n");
1839 return r;
1842 /* Delay actual initialization until the LCD is registered */
1843 fbdev_pdev = pdev;
1844 if (fbdev_panel != NULL)
1845 omapfb_do_probe(fbdev_pdev, fbdev_panel);
1846 return 0;
1849 void omapfb_register_panel(struct lcd_panel *panel)
1851 BUG_ON(fbdev_panel != NULL);
1853 fbdev_panel = panel;
1854 if (fbdev_pdev != NULL)
1855 omapfb_do_probe(fbdev_pdev, fbdev_panel);
1858 /* Called when the device is being detached from the driver */
1859 static int omapfb_remove(struct platform_device *pdev)
1861 struct omapfb_device *fbdev = platform_get_drvdata(pdev);
1862 enum omapfb_state saved_state = fbdev->state;
1864 /* FIXME: wait till completion of pending events */
1866 fbdev->state = OMAPFB_DISABLED;
1867 omapfb_free_resources(fbdev, saved_state);
1869 platform_device_unregister(&omapdss_device);
1870 fbdev->dssdev = NULL;
1872 return 0;
1875 /* PM suspend */
1876 static int omapfb_suspend(struct platform_device *pdev, pm_message_t mesg)
1878 struct omapfb_device *fbdev = platform_get_drvdata(pdev);
1880 if (fbdev != NULL)
1881 omapfb_blank(FB_BLANK_POWERDOWN, fbdev->fb_info[0]);
1882 return 0;
1885 /* PM resume */
1886 static int omapfb_resume(struct platform_device *pdev)
1888 struct omapfb_device *fbdev = platform_get_drvdata(pdev);
1890 if (fbdev != NULL)
1891 omapfb_blank(FB_BLANK_UNBLANK, fbdev->fb_info[0]);
1892 return 0;
1895 static struct platform_driver omapfb_driver = {
1896 .probe = omapfb_probe,
1897 .remove = omapfb_remove,
1898 .suspend = omapfb_suspend,
1899 .resume = omapfb_resume,
1900 .driver = {
1901 .name = MODULE_NAME,
1902 .owner = THIS_MODULE,
1906 #ifndef MODULE
1908 /* Process kernel command line parameters */
1909 static int __init omapfb_setup(char *options)
1911 char *this_opt = NULL;
1912 int r = 0;
1914 pr_debug("omapfb: options %s\n", options);
1916 if (!options || !*options)
1917 return 0;
1919 while (!r && (this_opt = strsep(&options, ",")) != NULL) {
1920 if (!strncmp(this_opt, "accel", 5))
1921 def_accel = 1;
1922 else if (!strncmp(this_opt, "vram:", 5)) {
1923 char *suffix;
1924 unsigned long vram;
1925 vram = (simple_strtoul(this_opt + 5, &suffix, 0));
1926 switch (suffix[0]) {
1927 case '\0':
1928 break;
1929 case 'm':
1930 case 'M':
1931 vram *= 1024;
1932 /* Fall through */
1933 case 'k':
1934 case 'K':
1935 vram *= 1024;
1936 break;
1937 default:
1938 pr_debug("omapfb: invalid vram suffix %c\n",
1939 suffix[0]);
1940 r = -1;
1942 def_vram[def_vram_cnt++] = vram;
1944 else if (!strncmp(this_opt, "vxres:", 6))
1945 def_vxres = simple_strtoul(this_opt + 6, NULL, 0);
1946 else if (!strncmp(this_opt, "vyres:", 6))
1947 def_vyres = simple_strtoul(this_opt + 6, NULL, 0);
1948 else if (!strncmp(this_opt, "rotate:", 7))
1949 def_rotate = (simple_strtoul(this_opt + 7, NULL, 0));
1950 else if (!strncmp(this_opt, "mirror:", 7))
1951 def_mirror = (simple_strtoul(this_opt + 7, NULL, 0));
1952 else if (!strncmp(this_opt, "manual_update", 13))
1953 manual_update = 1;
1954 else {
1955 pr_debug("omapfb: invalid option\n");
1956 r = -1;
1960 return r;
1963 #endif
1965 /* Register both the driver and the device */
1966 static int __init omapfb_init(void)
1968 #ifndef MODULE
1969 char *option;
1971 if (fb_get_options("omapfb", &option))
1972 return -ENODEV;
1973 omapfb_setup(option);
1974 #endif
1975 /* Register the driver with LDM */
1976 if (platform_driver_register(&omapfb_driver)) {
1977 pr_debug("failed to register omapfb driver\n");
1978 return -ENODEV;
1981 return 0;
1984 static void __exit omapfb_cleanup(void)
1986 platform_driver_unregister(&omapfb_driver);
1989 module_param_named(accel, def_accel, uint, 0664);
1990 module_param_array_named(vram, def_vram, ulong, &def_vram_cnt, 0664);
1991 module_param_named(vxres, def_vxres, long, 0664);
1992 module_param_named(vyres, def_vyres, long, 0664);
1993 module_param_named(rotate, def_rotate, uint, 0664);
1994 module_param_named(mirror, def_mirror, uint, 0664);
1995 module_param_named(manual_update, manual_update, bool, 0664);
1997 module_init(omapfb_init);
1998 module_exit(omapfb_cleanup);
2000 MODULE_DESCRIPTION("TI OMAP framebuffer driver");
2001 MODULE_AUTHOR("Imre Deak <imre.deak@nokia.com>");
2002 MODULE_LICENSE("GPL");