Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Framebuffer driver for TI OMAP boards
0004  *
0005  * Copyright (C) 2004 Nokia Corporation
0006  * Author: Imre Deak <imre.deak@nokia.com>
0007  *
0008  * Acknowledgements:
0009  *   Alex McMains <aam@ridgerun.com>       - Original driver
0010  *   Juha Yrjola <juha.yrjola@nokia.com>   - Original driver and improvements
0011  *   Dirk Behme <dirk.behme@de.bosch.com>  - changes for 2.6 kernel API
0012  *   Texas Instruments                     - H3 support
0013  */
0014 #include <linux/platform_device.h>
0015 #include <linux/mm.h>
0016 #include <linux/slab.h>
0017 #include <linux/uaccess.h>
0018 #include <linux/module.h>
0019 #include <linux/sysfs.h>
0020 
0021 #include <linux/omap-dma.h>
0022 
0023 #include <linux/soc/ti/omap1-soc.h>
0024 #include "omapfb.h"
0025 #include "lcdc.h"
0026 
0027 #define MODULE_NAME "omapfb"
0028 
0029 static unsigned int def_accel;
0030 static unsigned long    def_vram[OMAPFB_PLANE_NUM];
0031 static unsigned int def_vram_cnt;
0032 static unsigned long    def_vxres;
0033 static unsigned long    def_vyres;
0034 static unsigned int def_rotate;
0035 static unsigned int def_mirror;
0036 
0037 static bool manual_update = IS_BUILTIN(CONFIG_FB_OMAP_MANUAL_UPDATE);
0038 
0039 static struct platform_device   *fbdev_pdev;
0040 static struct lcd_panel     *fbdev_panel;
0041 static struct omapfb_device *omapfb_dev;
0042 
0043 struct caps_table_struct {
0044     unsigned long flag;
0045     const char *name;
0046 };
0047 
0048 static const struct caps_table_struct ctrl_caps[] = {
0049     { OMAPFB_CAPS_MANUAL_UPDATE,  "manual update" },
0050     { OMAPFB_CAPS_TEARSYNC,       "tearing synchronization" },
0051     { OMAPFB_CAPS_PLANE_RELOCATE_MEM, "relocate plane memory" },
0052     { OMAPFB_CAPS_PLANE_SCALE,    "scale plane" },
0053     { OMAPFB_CAPS_WINDOW_PIXEL_DOUBLE, "pixel double window" },
0054     { OMAPFB_CAPS_WINDOW_SCALE,   "scale window" },
0055     { OMAPFB_CAPS_WINDOW_OVERLAY, "overlay window" },
0056     { OMAPFB_CAPS_WINDOW_ROTATE,  "rotate window" },
0057     { OMAPFB_CAPS_SET_BACKLIGHT,  "backlight setting" },
0058 };
0059 
0060 static const struct caps_table_struct color_caps[] = {
0061     { 1 << OMAPFB_COLOR_RGB565, "RGB565", },
0062     { 1 << OMAPFB_COLOR_YUV422, "YUV422", },
0063     { 1 << OMAPFB_COLOR_YUV420, "YUV420", },
0064     { 1 << OMAPFB_COLOR_CLUT_8BPP,  "CLUT8", },
0065     { 1 << OMAPFB_COLOR_CLUT_4BPP,  "CLUT4", },
0066     { 1 << OMAPFB_COLOR_CLUT_2BPP,  "CLUT2", },
0067     { 1 << OMAPFB_COLOR_CLUT_1BPP,  "CLUT1", },
0068     { 1 << OMAPFB_COLOR_RGB444, "RGB444", },
0069     { 1 << OMAPFB_COLOR_YUY422, "YUY422", },
0070 };
0071 
0072 static void omapdss_release(struct device *dev)
0073 {
0074 }
0075 
0076 /* dummy device for clocks */
0077 static struct platform_device omapdss_device = {
0078     .name       = "omapdss_dss",
0079     .id     = -1,
0080     .dev            = {
0081         .release = omapdss_release,
0082     },
0083 };
0084 
0085 /*
0086  * ---------------------------------------------------------------------------
0087  * LCD panel
0088  * ---------------------------------------------------------------------------
0089  */
0090 extern struct lcd_ctrl hwa742_ctrl;
0091 
0092 static const struct lcd_ctrl *ctrls[] = {
0093     &omap1_int_ctrl,
0094 
0095 #ifdef CONFIG_FB_OMAP_LCDC_HWA742
0096     &hwa742_ctrl,
0097 #endif
0098 };
0099 
0100 #ifdef CONFIG_FB_OMAP_LCDC_EXTERNAL
0101 extern struct lcd_ctrl_extif omap1_ext_if;
0102 #endif
0103 
0104 static void omapfb_rqueue_lock(struct omapfb_device *fbdev)
0105 {
0106     mutex_lock(&fbdev->rqueue_mutex);
0107 }
0108 
0109 static void omapfb_rqueue_unlock(struct omapfb_device *fbdev)
0110 {
0111     mutex_unlock(&fbdev->rqueue_mutex);
0112 }
0113 
0114 /*
0115  * ---------------------------------------------------------------------------
0116  * LCD controller and LCD DMA
0117  * ---------------------------------------------------------------------------
0118  */
0119 /*
0120  * Allocate resources needed for LCD controller and LCD DMA operations. Video
0121  * memory is allocated from system memory according to the virtual display
0122  * size, except if a bigger memory size is specified explicitly as a kernel
0123  * parameter.
0124  */
0125 static int ctrl_init(struct omapfb_device *fbdev)
0126 {
0127     int r;
0128     int i;
0129 
0130     /* kernel/module vram parameters override boot tags/board config */
0131     if (def_vram_cnt) {
0132         for (i = 0; i < def_vram_cnt; i++)
0133             fbdev->mem_desc.region[i].size =
0134                 PAGE_ALIGN(def_vram[i]);
0135         fbdev->mem_desc.region_cnt = i;
0136     }
0137 
0138     if (!fbdev->mem_desc.region_cnt) {
0139         struct lcd_panel *panel = fbdev->panel;
0140         int def_size;
0141         int bpp = panel->bpp;
0142 
0143         /* 12 bpp is packed in 16 bits */
0144         if (bpp == 12)
0145             bpp = 16;
0146         def_size = def_vxres * def_vyres * bpp / 8;
0147         fbdev->mem_desc.region_cnt = 1;
0148         fbdev->mem_desc.region[0].size = PAGE_ALIGN(def_size);
0149     }
0150     r = fbdev->ctrl->init(fbdev, 0, &fbdev->mem_desc);
0151     if (r < 0) {
0152         dev_err(fbdev->dev, "controller initialization failed (%d)\n",
0153             r);
0154         return r;
0155     }
0156 
0157 #ifdef DEBUG
0158     for (i = 0; i < fbdev->mem_desc.region_cnt; i++) {
0159         dev_dbg(fbdev->dev, "region%d phys %08x virt %p size=%lu\n",
0160              i,
0161              fbdev->mem_desc.region[i].paddr,
0162              fbdev->mem_desc.region[i].vaddr,
0163              fbdev->mem_desc.region[i].size);
0164     }
0165 #endif
0166     return 0;
0167 }
0168 
0169 static void ctrl_cleanup(struct omapfb_device *fbdev)
0170 {
0171     fbdev->ctrl->cleanup();
0172 }
0173 
0174 /* Must be called with fbdev->rqueue_mutex held. */
0175 static int ctrl_change_mode(struct fb_info *fbi)
0176 {
0177     int r;
0178     unsigned long offset;
0179     struct omapfb_plane_struct *plane = fbi->par;
0180     struct omapfb_device *fbdev = plane->fbdev;
0181     struct fb_var_screeninfo *var = &fbi->var;
0182 
0183     offset = var->yoffset * fbi->fix.line_length +
0184          var->xoffset * var->bits_per_pixel / 8;
0185 
0186     if (fbdev->ctrl->sync)
0187         fbdev->ctrl->sync();
0188     r = fbdev->ctrl->setup_plane(plane->idx, plane->info.channel_out,
0189                  offset, var->xres_virtual,
0190                  plane->info.pos_x, plane->info.pos_y,
0191                  var->xres, var->yres, plane->color_mode);
0192     if (r < 0)
0193         return r;
0194 
0195     if (fbdev->ctrl->set_rotate != NULL) {
0196         r = fbdev->ctrl->set_rotate(var->rotate);
0197         if (r < 0)
0198             return r;
0199     }
0200 
0201     if (fbdev->ctrl->set_scale != NULL)
0202         r = fbdev->ctrl->set_scale(plane->idx,
0203                    var->xres, var->yres,
0204                    plane->info.out_width,
0205                    plane->info.out_height);
0206 
0207     return r;
0208 }
0209 
0210 /*
0211  * ---------------------------------------------------------------------------
0212  * fbdev framework callbacks and the ioctl interface
0213  * ---------------------------------------------------------------------------
0214  */
0215 /* Called each time the omapfb device is opened */
0216 static int omapfb_open(struct fb_info *info, int user)
0217 {
0218     return 0;
0219 }
0220 
0221 static void omapfb_sync(struct fb_info *info);
0222 
0223 /* Called when the omapfb device is closed. We make sure that any pending
0224  * gfx DMA operations are ended, before we return. */
0225 static int omapfb_release(struct fb_info *info, int user)
0226 {
0227     omapfb_sync(info);
0228     return 0;
0229 }
0230 
0231 /* Store a single color palette entry into a pseudo palette or the hardware
0232  * palette if one is available. For now we support only 16bpp and thus store
0233  * the entry only to the pseudo palette.
0234  */
0235 static int _setcolreg(struct fb_info *info, u_int regno, u_int red, u_int green,
0236             u_int blue, u_int transp, int update_hw_pal)
0237 {
0238     struct omapfb_plane_struct *plane = info->par;
0239     struct omapfb_device *fbdev = plane->fbdev;
0240     struct fb_var_screeninfo *var = &info->var;
0241     int r = 0;
0242 
0243     switch (plane->color_mode) {
0244     case OMAPFB_COLOR_YUV422:
0245     case OMAPFB_COLOR_YUV420:
0246     case OMAPFB_COLOR_YUY422:
0247         r = -EINVAL;
0248         break;
0249     case OMAPFB_COLOR_CLUT_8BPP:
0250     case OMAPFB_COLOR_CLUT_4BPP:
0251     case OMAPFB_COLOR_CLUT_2BPP:
0252     case OMAPFB_COLOR_CLUT_1BPP:
0253         if (fbdev->ctrl->setcolreg)
0254             r = fbdev->ctrl->setcolreg(regno, red, green, blue,
0255                             transp, update_hw_pal);
0256         fallthrough;
0257     case OMAPFB_COLOR_RGB565:
0258     case OMAPFB_COLOR_RGB444:
0259         if (r != 0)
0260             break;
0261 
0262         if (regno < 16) {
0263             u16 pal;
0264             pal = ((red >> (16 - var->red.length)) <<
0265                     var->red.offset) |
0266                   ((green >> (16 - var->green.length)) <<
0267                     var->green.offset) |
0268                   (blue >> (16 - var->blue.length));
0269             ((u32 *)(info->pseudo_palette))[regno] = pal;
0270         }
0271         break;
0272     default:
0273         BUG();
0274     }
0275     return r;
0276 }
0277 
0278 static int omapfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
0279                 u_int transp, struct fb_info *info)
0280 {
0281     return _setcolreg(info, regno, red, green, blue, transp, 1);
0282 }
0283 
0284 static int omapfb_setcmap(struct fb_cmap *cmap, struct fb_info *info)
0285 {
0286     int count, index, r;
0287     u16 *red, *green, *blue, *transp;
0288     u16 trans = 0xffff;
0289 
0290     red     = cmap->red;
0291     green   = cmap->green;
0292     blue    = cmap->blue;
0293     transp  = cmap->transp;
0294     index   = cmap->start;
0295 
0296     for (count = 0; count < cmap->len; count++) {
0297         if (transp)
0298             trans = *transp++;
0299         r = _setcolreg(info, index++, *red++, *green++, *blue++, trans,
0300                 count == cmap->len - 1);
0301         if (r != 0)
0302             return r;
0303     }
0304 
0305     return 0;
0306 }
0307 
0308 static int omapfb_update_full_screen(struct fb_info *fbi);
0309 
0310 static int omapfb_blank(int blank, struct fb_info *fbi)
0311 {
0312     struct omapfb_plane_struct *plane = fbi->par;
0313     struct omapfb_device *fbdev = plane->fbdev;
0314     int do_update = 0;
0315     int r = 0;
0316 
0317     omapfb_rqueue_lock(fbdev);
0318     switch (blank) {
0319     case FB_BLANK_UNBLANK:
0320         if (fbdev->state == OMAPFB_SUSPENDED) {
0321             if (fbdev->ctrl->resume)
0322                 fbdev->ctrl->resume();
0323             if (fbdev->panel->enable)
0324                 fbdev->panel->enable(fbdev->panel);
0325             fbdev->state = OMAPFB_ACTIVE;
0326             if (fbdev->ctrl->get_update_mode() ==
0327                     OMAPFB_MANUAL_UPDATE)
0328                 do_update = 1;
0329         }
0330         break;
0331     case FB_BLANK_POWERDOWN:
0332         if (fbdev->state == OMAPFB_ACTIVE) {
0333             if (fbdev->panel->disable)
0334                 fbdev->panel->disable(fbdev->panel);
0335             if (fbdev->ctrl->suspend)
0336                 fbdev->ctrl->suspend();
0337             fbdev->state = OMAPFB_SUSPENDED;
0338         }
0339         break;
0340     default:
0341         r = -EINVAL;
0342     }
0343     omapfb_rqueue_unlock(fbdev);
0344 
0345     if (r == 0 && do_update)
0346         r = omapfb_update_full_screen(fbi);
0347 
0348     return r;
0349 }
0350 
0351 static void omapfb_sync(struct fb_info *fbi)
0352 {
0353     struct omapfb_plane_struct *plane = fbi->par;
0354     struct omapfb_device *fbdev = plane->fbdev;
0355 
0356     omapfb_rqueue_lock(fbdev);
0357     if (fbdev->ctrl->sync)
0358         fbdev->ctrl->sync();
0359     omapfb_rqueue_unlock(fbdev);
0360 }
0361 
0362 /*
0363  * Set fb_info.fix fields and also updates fbdev.
0364  * When calling this fb_info.var must be set up already.
0365  */
0366 static void set_fb_fix(struct fb_info *fbi, int from_init)
0367 {
0368     struct fb_fix_screeninfo *fix = &fbi->fix;
0369     struct fb_var_screeninfo *var = &fbi->var;
0370     struct omapfb_plane_struct *plane = fbi->par;
0371     struct omapfb_mem_region *rg;
0372     int bpp;
0373 
0374     rg = &plane->fbdev->mem_desc.region[plane->idx];
0375     fbi->screen_base    = rg->vaddr;
0376 
0377     if (!from_init) {
0378         mutex_lock(&fbi->mm_lock);
0379         fix->smem_start     = rg->paddr;
0380         fix->smem_len       = rg->size;
0381         mutex_unlock(&fbi->mm_lock);
0382     } else {
0383         fix->smem_start     = rg->paddr;
0384         fix->smem_len       = rg->size;
0385     }
0386 
0387     fix->type = FB_TYPE_PACKED_PIXELS;
0388     bpp = var->bits_per_pixel;
0389     if (var->nonstd)
0390         fix->visual = FB_VISUAL_PSEUDOCOLOR;
0391     else switch (var->bits_per_pixel) {
0392     case 16:
0393     case 12:
0394         fix->visual = FB_VISUAL_TRUECOLOR;
0395         /* 12bpp is stored in 16 bits */
0396         bpp = 16;
0397         break;
0398     case 1:
0399     case 2:
0400     case 4:
0401     case 8:
0402         fix->visual = FB_VISUAL_PSEUDOCOLOR;
0403         break;
0404     }
0405     fix->accel      = FB_ACCEL_OMAP1610;
0406     fix->line_length    = var->xres_virtual * bpp / 8;
0407 }
0408 
0409 static int set_color_mode(struct omapfb_plane_struct *plane,
0410               struct fb_var_screeninfo *var)
0411 {
0412     switch (var->nonstd) {
0413     case 0:
0414         break;
0415     case OMAPFB_COLOR_YUV422:
0416         var->bits_per_pixel = 16;
0417         plane->color_mode = var->nonstd;
0418         return 0;
0419     case OMAPFB_COLOR_YUV420:
0420         var->bits_per_pixel = 12;
0421         plane->color_mode = var->nonstd;
0422         return 0;
0423     case OMAPFB_COLOR_YUY422:
0424         var->bits_per_pixel = 16;
0425         plane->color_mode = var->nonstd;
0426         return 0;
0427     default:
0428         return -EINVAL;
0429     }
0430 
0431     switch (var->bits_per_pixel) {
0432     case 1:
0433         plane->color_mode = OMAPFB_COLOR_CLUT_1BPP;
0434         return 0;
0435     case 2:
0436         plane->color_mode = OMAPFB_COLOR_CLUT_2BPP;
0437         return 0;
0438     case 4:
0439         plane->color_mode = OMAPFB_COLOR_CLUT_4BPP;
0440         return 0;
0441     case 8:
0442         plane->color_mode = OMAPFB_COLOR_CLUT_8BPP;
0443         return 0;
0444     case 12:
0445         var->bits_per_pixel = 16;
0446         fallthrough;
0447     case 16:
0448         if (plane->fbdev->panel->bpp == 12)
0449             plane->color_mode = OMAPFB_COLOR_RGB444;
0450         else
0451             plane->color_mode = OMAPFB_COLOR_RGB565;
0452         return 0;
0453     default:
0454         return -EINVAL;
0455     }
0456 }
0457 
0458 /*
0459  * Check the values in var against our capabilities and in case of out of
0460  * bound values try to adjust them.
0461  */
0462 static int set_fb_var(struct fb_info *fbi,
0463               struct fb_var_screeninfo *var)
0464 {
0465     int     bpp;
0466     unsigned long   max_frame_size;
0467     unsigned long   line_size;
0468     int     xres_min, xres_max;
0469     int     yres_min, yres_max;
0470     struct omapfb_plane_struct *plane = fbi->par;
0471     struct omapfb_device *fbdev = plane->fbdev;
0472     struct lcd_panel *panel = fbdev->panel;
0473 
0474     if (set_color_mode(plane, var) < 0)
0475         return -EINVAL;
0476 
0477     bpp = var->bits_per_pixel;
0478     if (plane->color_mode == OMAPFB_COLOR_RGB444)
0479         bpp = 16;
0480 
0481     switch (var->rotate) {
0482     case 0:
0483     case 180:
0484         xres_min = OMAPFB_PLANE_XRES_MIN;
0485         xres_max = panel->x_res;
0486         yres_min = OMAPFB_PLANE_YRES_MIN;
0487         yres_max = panel->y_res;
0488         if (cpu_is_omap15xx()) {
0489             var->xres = panel->x_res;
0490             var->yres = panel->y_res;
0491         }
0492         break;
0493     case 90:
0494     case 270:
0495         xres_min = OMAPFB_PLANE_YRES_MIN;
0496         xres_max = panel->y_res;
0497         yres_min = OMAPFB_PLANE_XRES_MIN;
0498         yres_max = panel->x_res;
0499         if (cpu_is_omap15xx()) {
0500             var->xres = panel->y_res;
0501             var->yres = panel->x_res;
0502         }
0503         break;
0504     default:
0505         return -EINVAL;
0506     }
0507 
0508     if (var->xres < xres_min)
0509         var->xres = xres_min;
0510     if (var->yres < yres_min)
0511         var->yres = yres_min;
0512     if (var->xres > xres_max)
0513         var->xres = xres_max;
0514     if (var->yres > yres_max)
0515         var->yres = yres_max;
0516 
0517     if (var->xres_virtual < var->xres)
0518         var->xres_virtual = var->xres;
0519     if (var->yres_virtual < var->yres)
0520         var->yres_virtual = var->yres;
0521     max_frame_size = fbdev->mem_desc.region[plane->idx].size;
0522     line_size = var->xres_virtual * bpp / 8;
0523     if (line_size * var->yres_virtual > max_frame_size) {
0524         /* Try to keep yres_virtual first */
0525         line_size = max_frame_size / var->yres_virtual;
0526         var->xres_virtual = line_size * 8 / bpp;
0527         if (var->xres_virtual < var->xres) {
0528             /* Still doesn't fit. Shrink yres_virtual too */
0529             var->xres_virtual = var->xres;
0530             line_size = var->xres * bpp / 8;
0531             var->yres_virtual = max_frame_size / line_size;
0532         }
0533         /* Recheck this, as the virtual size changed. */
0534         if (var->xres_virtual < var->xres)
0535             var->xres = var->xres_virtual;
0536         if (var->yres_virtual < var->yres)
0537             var->yres = var->yres_virtual;
0538         if (var->xres < xres_min || var->yres < yres_min)
0539             return -EINVAL;
0540     }
0541     if (var->xres + var->xoffset > var->xres_virtual)
0542         var->xoffset = var->xres_virtual - var->xres;
0543     if (var->yres + var->yoffset > var->yres_virtual)
0544         var->yoffset = var->yres_virtual - var->yres;
0545 
0546     if (plane->color_mode == OMAPFB_COLOR_RGB444) {
0547         var->red.offset   = 8; var->red.length   = 4;
0548                         var->red.msb_right   = 0;
0549         var->green.offset = 4; var->green.length = 4;
0550                         var->green.msb_right = 0;
0551         var->blue.offset  = 0; var->blue.length  = 4;
0552                         var->blue.msb_right  = 0;
0553     } else {
0554         var->red.offset  = 11; var->red.length   = 5;
0555                         var->red.msb_right   = 0;
0556         var->green.offset = 5;  var->green.length = 6;
0557                         var->green.msb_right = 0;
0558         var->blue.offset = 0;  var->blue.length  = 5;
0559                         var->blue.msb_right  = 0;
0560     }
0561 
0562     var->height     = -1;
0563     var->width      = -1;
0564     var->grayscale      = 0;
0565 
0566     /* pixclock in ps, the rest in pixclock */
0567     var->pixclock       = 10000000 / (panel->pixel_clock / 100);
0568     var->left_margin    = panel->hfp;
0569     var->right_margin   = panel->hbp;
0570     var->upper_margin   = panel->vfp;
0571     var->lower_margin   = panel->vbp;
0572     var->hsync_len      = panel->hsw;
0573     var->vsync_len      = panel->vsw;
0574 
0575     /* TODO: get these from panel->config */
0576     var->vmode      = FB_VMODE_NONINTERLACED;
0577     var->sync       = 0;
0578 
0579     return 0;
0580 }
0581 
0582 
0583 /*
0584  * Set new x,y offsets in the virtual display for the visible area and switch
0585  * to the new mode.
0586  */
0587 static int omapfb_pan_display(struct fb_var_screeninfo *var,
0588                    struct fb_info *fbi)
0589 {
0590     struct omapfb_plane_struct *plane = fbi->par;
0591     struct omapfb_device *fbdev = plane->fbdev;
0592     int r = 0;
0593 
0594     omapfb_rqueue_lock(fbdev);
0595     if (var->xoffset != fbi->var.xoffset ||
0596         var->yoffset != fbi->var.yoffset) {
0597         struct fb_var_screeninfo *new_var = &fbdev->new_var;
0598 
0599         memcpy(new_var, &fbi->var, sizeof(*new_var));
0600         new_var->xoffset = var->xoffset;
0601         new_var->yoffset = var->yoffset;
0602         if (set_fb_var(fbi, new_var))
0603             r = -EINVAL;
0604         else {
0605             memcpy(&fbi->var, new_var, sizeof(*new_var));
0606             ctrl_change_mode(fbi);
0607         }
0608     }
0609     omapfb_rqueue_unlock(fbdev);
0610 
0611     return r;
0612 }
0613 
0614 /* Set mirror to vertical axis and switch to the new mode. */
0615 static int omapfb_mirror(struct fb_info *fbi, int mirror)
0616 {
0617     struct omapfb_plane_struct *plane = fbi->par;
0618     struct omapfb_device *fbdev = plane->fbdev;
0619     int r = 0;
0620 
0621     omapfb_rqueue_lock(fbdev);
0622     mirror = mirror ? 1 : 0;
0623     if (cpu_is_omap15xx())
0624         r = -EINVAL;
0625     else if (mirror != plane->info.mirror) {
0626         plane->info.mirror = mirror;
0627         r = ctrl_change_mode(fbi);
0628     }
0629     omapfb_rqueue_unlock(fbdev);
0630 
0631     return r;
0632 }
0633 
0634 /*
0635  * Check values in var, try to adjust them in case of out of bound values if
0636  * possible, or return error.
0637  */
0638 static int omapfb_check_var(struct fb_var_screeninfo *var, struct fb_info *fbi)
0639 {
0640     struct omapfb_plane_struct *plane = fbi->par;
0641     struct omapfb_device *fbdev = plane->fbdev;
0642     int r;
0643 
0644     omapfb_rqueue_lock(fbdev);
0645     if (fbdev->ctrl->sync != NULL)
0646         fbdev->ctrl->sync();
0647     r = set_fb_var(fbi, var);
0648     omapfb_rqueue_unlock(fbdev);
0649 
0650     return r;
0651 }
0652 
0653 /*
0654  * Switch to a new mode. The parameters for it has been check already by
0655  * omapfb_check_var.
0656  */
0657 static int omapfb_set_par(struct fb_info *fbi)
0658 {
0659     struct omapfb_plane_struct *plane = fbi->par;
0660     struct omapfb_device *fbdev = plane->fbdev;
0661     int r = 0;
0662 
0663     omapfb_rqueue_lock(fbdev);
0664     set_fb_fix(fbi, 0);
0665     r = ctrl_change_mode(fbi);
0666     omapfb_rqueue_unlock(fbdev);
0667 
0668     return r;
0669 }
0670 
0671 static int omapfb_update_window_async(struct fb_info *fbi,
0672                 struct omapfb_update_window *win,
0673                 void (*callback)(void *),
0674                 void *callback_data)
0675 {
0676     int xres, yres;
0677     struct omapfb_plane_struct *plane = fbi->par;
0678     struct omapfb_device *fbdev = plane->fbdev;
0679     struct fb_var_screeninfo *var = &fbi->var;
0680 
0681     switch (var->rotate) {
0682     case 0:
0683     case 180:
0684         xres = fbdev->panel->x_res;
0685         yres = fbdev->panel->y_res;
0686         break;
0687     case 90:
0688     case 270:
0689         xres = fbdev->panel->y_res;
0690         yres = fbdev->panel->x_res;
0691         break;
0692     default:
0693         return -EINVAL;
0694     }
0695 
0696     if (win->x >= xres || win->y >= yres ||
0697         win->out_x > xres || win->out_y > yres)
0698         return -EINVAL;
0699 
0700     if (!fbdev->ctrl->update_window ||
0701         fbdev->ctrl->get_update_mode() != OMAPFB_MANUAL_UPDATE)
0702         return -ENODEV;
0703 
0704     if (win->x + win->width > xres)
0705         win->width = xres - win->x;
0706     if (win->y + win->height > yres)
0707         win->height = yres - win->y;
0708     if (win->out_x + win->out_width > xres)
0709         win->out_width = xres - win->out_x;
0710     if (win->out_y + win->out_height > yres)
0711         win->out_height = yres - win->out_y;
0712     if (!win->width || !win->height || !win->out_width || !win->out_height)
0713         return 0;
0714 
0715     return fbdev->ctrl->update_window(fbi, win, callback, callback_data);
0716 }
0717 
0718 static int omapfb_update_win(struct fb_info *fbi,
0719                 struct omapfb_update_window *win)
0720 {
0721     struct omapfb_plane_struct *plane = fbi->par;
0722     int ret;
0723 
0724     omapfb_rqueue_lock(plane->fbdev);
0725     ret = omapfb_update_window_async(fbi, win, NULL, NULL);
0726     omapfb_rqueue_unlock(plane->fbdev);
0727 
0728     return ret;
0729 }
0730 
0731 static int omapfb_update_full_screen(struct fb_info *fbi)
0732 {
0733     struct omapfb_plane_struct *plane = fbi->par;
0734     struct omapfb_device *fbdev = plane->fbdev;
0735     struct omapfb_update_window win;
0736     int r;
0737 
0738     if (!fbdev->ctrl->update_window ||
0739         fbdev->ctrl->get_update_mode() != OMAPFB_MANUAL_UPDATE)
0740         return -ENODEV;
0741 
0742     win.x = 0;
0743     win.y = 0;
0744     win.width = fbi->var.xres;
0745     win.height = fbi->var.yres;
0746     win.out_x = 0;
0747     win.out_y = 0;
0748     win.out_width = fbi->var.xres;
0749     win.out_height = fbi->var.yres;
0750     win.format = 0;
0751 
0752     omapfb_rqueue_lock(fbdev);
0753     r = fbdev->ctrl->update_window(fbi, &win, NULL, NULL);
0754     omapfb_rqueue_unlock(fbdev);
0755 
0756     return r;
0757 }
0758 
0759 static int omapfb_setup_plane(struct fb_info *fbi, struct omapfb_plane_info *pi)
0760 {
0761     struct omapfb_plane_struct *plane = fbi->par;
0762     struct omapfb_device *fbdev = plane->fbdev;
0763     struct lcd_panel *panel = fbdev->panel;
0764     struct omapfb_plane_info old_info;
0765     int r = 0;
0766 
0767     if (pi->pos_x + pi->out_width > panel->x_res ||
0768         pi->pos_y + pi->out_height > panel->y_res)
0769         return -EINVAL;
0770 
0771     omapfb_rqueue_lock(fbdev);
0772     if (pi->enabled && !fbdev->mem_desc.region[plane->idx].size) {
0773         /*
0774          * This plane's memory was freed, can't enable it
0775          * until it's reallocated.
0776          */
0777         r = -EINVAL;
0778         goto out;
0779     }
0780     old_info = plane->info;
0781     plane->info = *pi;
0782     if (pi->enabled) {
0783         r = ctrl_change_mode(fbi);
0784         if (r < 0) {
0785             plane->info = old_info;
0786             goto out;
0787         }
0788     }
0789     r = fbdev->ctrl->enable_plane(plane->idx, pi->enabled);
0790     if (r < 0) {
0791         plane->info = old_info;
0792         goto out;
0793     }
0794 out:
0795     omapfb_rqueue_unlock(fbdev);
0796     return r;
0797 }
0798 
0799 static int omapfb_query_plane(struct fb_info *fbi, struct omapfb_plane_info *pi)
0800 {
0801     struct omapfb_plane_struct *plane = fbi->par;
0802 
0803     *pi = plane->info;
0804     return 0;
0805 }
0806 
0807 static int omapfb_setup_mem(struct fb_info *fbi, struct omapfb_mem_info *mi)
0808 {
0809     struct omapfb_plane_struct *plane = fbi->par;
0810     struct omapfb_device *fbdev = plane->fbdev;
0811     struct omapfb_mem_region *rg = &fbdev->mem_desc.region[plane->idx];
0812     size_t size;
0813     int r = 0;
0814 
0815     if (fbdev->ctrl->setup_mem == NULL)
0816         return -ENODEV;
0817     if (mi->type != OMAPFB_MEMTYPE_SDRAM)
0818         return -EINVAL;
0819 
0820     size = PAGE_ALIGN(mi->size);
0821     omapfb_rqueue_lock(fbdev);
0822     if (plane->info.enabled) {
0823         r = -EBUSY;
0824         goto out;
0825     }
0826     if (rg->size != size || rg->type != mi->type) {
0827         struct fb_var_screeninfo *new_var = &fbdev->new_var;
0828         unsigned long old_size = rg->size;
0829         u8        old_type = rg->type;
0830         unsigned long paddr;
0831 
0832         rg->size = size;
0833         rg->type = mi->type;
0834         /*
0835          * size == 0 is a special case, for which we
0836          * don't check / adjust the screen parameters.
0837          * This isn't a problem since the plane can't
0838          * be reenabled unless its size is > 0.
0839          */
0840         if (old_size != size && size) {
0841             if (size) {
0842                 memcpy(new_var, &fbi->var, sizeof(*new_var));
0843                 r = set_fb_var(fbi, new_var);
0844                 if (r < 0)
0845                     goto out;
0846             }
0847         }
0848 
0849         if (fbdev->ctrl->sync)
0850             fbdev->ctrl->sync();
0851         r = fbdev->ctrl->setup_mem(plane->idx, size, mi->type, &paddr);
0852         if (r < 0) {
0853             /* Revert changes. */
0854             rg->size = old_size;
0855             rg->type = old_type;
0856             goto out;
0857         }
0858         rg->paddr = paddr;
0859 
0860         if (old_size != size) {
0861             if (size) {
0862                 memcpy(&fbi->var, new_var, sizeof(fbi->var));
0863                 set_fb_fix(fbi, 0);
0864             } else {
0865                 /*
0866                  * Set these explicitly to indicate that the
0867                  * plane memory is dealloce'd, the other
0868                  * screen parameters in var / fix are invalid.
0869                  */
0870                 mutex_lock(&fbi->mm_lock);
0871                 fbi->fix.smem_start = 0;
0872                 fbi->fix.smem_len = 0;
0873                 mutex_unlock(&fbi->mm_lock);
0874             }
0875         }
0876     }
0877 out:
0878     omapfb_rqueue_unlock(fbdev);
0879 
0880     return r;
0881 }
0882 
0883 static int omapfb_query_mem(struct fb_info *fbi, struct omapfb_mem_info *mi)
0884 {
0885     struct omapfb_plane_struct *plane = fbi->par;
0886     struct omapfb_device *fbdev = plane->fbdev;
0887     struct omapfb_mem_region *rg;
0888 
0889     rg = &fbdev->mem_desc.region[plane->idx];
0890     memset(mi, 0, sizeof(*mi));
0891     mi->size = rg->size;
0892     mi->type = rg->type;
0893 
0894     return 0;
0895 }
0896 
0897 static int omapfb_set_color_key(struct omapfb_device *fbdev,
0898                 struct omapfb_color_key *ck)
0899 {
0900     int r;
0901 
0902     if (!fbdev->ctrl->set_color_key)
0903         return -ENODEV;
0904 
0905     omapfb_rqueue_lock(fbdev);
0906     r = fbdev->ctrl->set_color_key(ck);
0907     omapfb_rqueue_unlock(fbdev);
0908 
0909     return r;
0910 }
0911 
0912 static int omapfb_get_color_key(struct omapfb_device *fbdev,
0913                 struct omapfb_color_key *ck)
0914 {
0915     int r;
0916 
0917     if (!fbdev->ctrl->get_color_key)
0918         return -ENODEV;
0919 
0920     omapfb_rqueue_lock(fbdev);
0921     r = fbdev->ctrl->get_color_key(ck);
0922     omapfb_rqueue_unlock(fbdev);
0923 
0924     return r;
0925 }
0926 
0927 static struct blocking_notifier_head omapfb_client_list[OMAPFB_PLANE_NUM];
0928 static int notifier_inited;
0929 
0930 static void omapfb_init_notifier(void)
0931 {
0932     int i;
0933 
0934     for (i = 0; i < OMAPFB_PLANE_NUM; i++)
0935         BLOCKING_INIT_NOTIFIER_HEAD(&omapfb_client_list[i]);
0936 }
0937 
0938 int omapfb_register_client(struct omapfb_notifier_block *omapfb_nb,
0939                 omapfb_notifier_callback_t callback,
0940                 void *callback_data)
0941 {
0942     int r;
0943 
0944     if ((unsigned)omapfb_nb->plane_idx >= OMAPFB_PLANE_NUM)
0945         return -EINVAL;
0946 
0947     if (!notifier_inited) {
0948         omapfb_init_notifier();
0949         notifier_inited = 1;
0950     }
0951 
0952     omapfb_nb->nb.notifier_call = (int (*)(struct notifier_block *,
0953                     unsigned long, void *))callback;
0954     omapfb_nb->data = callback_data;
0955     r = blocking_notifier_chain_register(
0956                 &omapfb_client_list[omapfb_nb->plane_idx],
0957                 &omapfb_nb->nb);
0958     if (r)
0959         return r;
0960     if (omapfb_dev != NULL &&
0961         omapfb_dev->ctrl && omapfb_dev->ctrl->bind_client) {
0962         omapfb_dev->ctrl->bind_client(omapfb_nb);
0963     }
0964 
0965     return 0;
0966 }
0967 EXPORT_SYMBOL(omapfb_register_client);
0968 
0969 int omapfb_unregister_client(struct omapfb_notifier_block *omapfb_nb)
0970 {
0971     return blocking_notifier_chain_unregister(
0972         &omapfb_client_list[omapfb_nb->plane_idx], &omapfb_nb->nb);
0973 }
0974 EXPORT_SYMBOL(omapfb_unregister_client);
0975 
0976 void omapfb_notify_clients(struct omapfb_device *fbdev, unsigned long event)
0977 {
0978     int i;
0979 
0980     if (!notifier_inited)
0981         /* no client registered yet */
0982         return;
0983 
0984     for (i = 0; i < OMAPFB_PLANE_NUM; i++)
0985         blocking_notifier_call_chain(&omapfb_client_list[i], event,
0986                     fbdev->fb_info[i]);
0987 }
0988 EXPORT_SYMBOL(omapfb_notify_clients);
0989 
0990 static int omapfb_set_update_mode(struct omapfb_device *fbdev,
0991                    enum omapfb_update_mode mode)
0992 {
0993     int r;
0994 
0995     omapfb_rqueue_lock(fbdev);
0996     r = fbdev->ctrl->set_update_mode(mode);
0997     omapfb_rqueue_unlock(fbdev);
0998 
0999     return r;
1000 }
1001 
1002 static enum omapfb_update_mode omapfb_get_update_mode(struct omapfb_device *fbdev)
1003 {
1004     int r;
1005 
1006     omapfb_rqueue_lock(fbdev);
1007     r = fbdev->ctrl->get_update_mode();
1008     omapfb_rqueue_unlock(fbdev);
1009 
1010     return r;
1011 }
1012 
1013 static void omapfb_get_caps(struct omapfb_device *fbdev, int plane,
1014                      struct omapfb_caps *caps)
1015 {
1016     memset(caps, 0, sizeof(*caps));
1017     fbdev->ctrl->get_caps(plane, caps);
1018     if (fbdev->panel->get_caps)
1019         caps->ctrl |= fbdev->panel->get_caps(fbdev->panel);
1020 }
1021 
1022 /* For lcd testing */
1023 void omapfb_write_first_pixel(struct omapfb_device *fbdev, u16 pixval)
1024 {
1025     omapfb_rqueue_lock(fbdev);
1026     *(u16 *)fbdev->mem_desc.region[0].vaddr = pixval;
1027     if (fbdev->ctrl->get_update_mode() == OMAPFB_MANUAL_UPDATE) {
1028         struct omapfb_update_window win;
1029 
1030         memset(&win, 0, sizeof(win));
1031         win.width = 2;
1032         win.height = 2;
1033         win.out_width = 2;
1034         win.out_height = 2;
1035         fbdev->ctrl->update_window(fbdev->fb_info[0], &win, NULL, NULL);
1036     }
1037     omapfb_rqueue_unlock(fbdev);
1038 }
1039 EXPORT_SYMBOL(omapfb_write_first_pixel);
1040 
1041 /*
1042  * Ioctl interface. Part of the kernel mode frame buffer API is duplicated
1043  * here to be accessible by user mode code.
1044  */
1045 static int omapfb_ioctl(struct fb_info *fbi, unsigned int cmd,
1046             unsigned long arg)
1047 {
1048     struct omapfb_plane_struct *plane = fbi->par;
1049     struct omapfb_device    *fbdev = plane->fbdev;
1050     const struct fb_ops *ops = fbi->fbops;
1051     union {
1052         struct omapfb_update_window update_window;
1053         struct omapfb_plane_info    plane_info;
1054         struct omapfb_mem_info      mem_info;
1055         struct omapfb_color_key     color_key;
1056         enum omapfb_update_mode     update_mode;
1057         struct omapfb_caps      caps;
1058         unsigned int        mirror;
1059         int         plane_out;
1060         int         enable_plane;
1061     } p;
1062     int r = 0;
1063 
1064     BUG_ON(!ops);
1065     switch (cmd) {
1066     case OMAPFB_MIRROR:
1067         if (get_user(p.mirror, (int __user *)arg))
1068             r = -EFAULT;
1069         else
1070             omapfb_mirror(fbi, p.mirror);
1071         break;
1072     case OMAPFB_SYNC_GFX:
1073         omapfb_sync(fbi);
1074         break;
1075     case OMAPFB_VSYNC:
1076         break;
1077     case OMAPFB_SET_UPDATE_MODE:
1078         if (get_user(p.update_mode, (int __user *)arg))
1079             r = -EFAULT;
1080         else
1081             r = omapfb_set_update_mode(fbdev, p.update_mode);
1082         break;
1083     case OMAPFB_GET_UPDATE_MODE:
1084         p.update_mode = omapfb_get_update_mode(fbdev);
1085         if (put_user(p.update_mode,
1086                     (enum omapfb_update_mode __user *)arg))
1087             r = -EFAULT;
1088         break;
1089     case OMAPFB_UPDATE_WINDOW_OLD:
1090         if (copy_from_user(&p.update_window, (void __user *)arg,
1091                    sizeof(struct omapfb_update_window_old)))
1092             r = -EFAULT;
1093         else {
1094             struct omapfb_update_window *u = &p.update_window;
1095             u->out_x = u->x;
1096             u->out_y = u->y;
1097             u->out_width = u->width;
1098             u->out_height = u->height;
1099             memset(u->reserved, 0, sizeof(u->reserved));
1100             r = omapfb_update_win(fbi, u);
1101         }
1102         break;
1103     case OMAPFB_UPDATE_WINDOW:
1104         if (copy_from_user(&p.update_window, (void __user *)arg,
1105                    sizeof(p.update_window)))
1106             r = -EFAULT;
1107         else
1108             r = omapfb_update_win(fbi, &p.update_window);
1109         break;
1110     case OMAPFB_SETUP_PLANE:
1111         if (copy_from_user(&p.plane_info, (void __user *)arg,
1112                    sizeof(p.plane_info)))
1113             r = -EFAULT;
1114         else
1115             r = omapfb_setup_plane(fbi, &p.plane_info);
1116         break;
1117     case OMAPFB_QUERY_PLANE:
1118         if ((r = omapfb_query_plane(fbi, &p.plane_info)) < 0)
1119             break;
1120         if (copy_to_user((void __user *)arg, &p.plane_info,
1121                    sizeof(p.plane_info)))
1122             r = -EFAULT;
1123         break;
1124     case OMAPFB_SETUP_MEM:
1125         if (copy_from_user(&p.mem_info, (void __user *)arg,
1126                    sizeof(p.mem_info)))
1127             r = -EFAULT;
1128         else
1129             r = omapfb_setup_mem(fbi, &p.mem_info);
1130         break;
1131     case OMAPFB_QUERY_MEM:
1132         if ((r = omapfb_query_mem(fbi, &p.mem_info)) < 0)
1133             break;
1134         if (copy_to_user((void __user *)arg, &p.mem_info,
1135                    sizeof(p.mem_info)))
1136             r = -EFAULT;
1137         break;
1138     case OMAPFB_SET_COLOR_KEY:
1139         if (copy_from_user(&p.color_key, (void __user *)arg,
1140                    sizeof(p.color_key)))
1141             r = -EFAULT;
1142         else
1143             r = omapfb_set_color_key(fbdev, &p.color_key);
1144         break;
1145     case OMAPFB_GET_COLOR_KEY:
1146         if ((r = omapfb_get_color_key(fbdev, &p.color_key)) < 0)
1147             break;
1148         if (copy_to_user((void __user *)arg, &p.color_key,
1149                  sizeof(p.color_key)))
1150             r = -EFAULT;
1151         break;
1152     case OMAPFB_GET_CAPS:
1153         omapfb_get_caps(fbdev, plane->idx, &p.caps);
1154         if (copy_to_user((void __user *)arg, &p.caps, sizeof(p.caps)))
1155             r = -EFAULT;
1156         break;
1157     case OMAPFB_LCD_TEST:
1158         {
1159             int test_num;
1160 
1161             if (get_user(test_num, (int __user *)arg)) {
1162                 r = -EFAULT;
1163                 break;
1164             }
1165             if (!fbdev->panel->run_test) {
1166                 r = -EINVAL;
1167                 break;
1168             }
1169             r = fbdev->panel->run_test(fbdev->panel, test_num);
1170             break;
1171         }
1172     case OMAPFB_CTRL_TEST:
1173         {
1174             int test_num;
1175 
1176             if (get_user(test_num, (int __user *)arg)) {
1177                 r = -EFAULT;
1178                 break;
1179             }
1180             if (!fbdev->ctrl->run_test) {
1181                 r = -EINVAL;
1182                 break;
1183             }
1184             r = fbdev->ctrl->run_test(test_num);
1185             break;
1186         }
1187     default:
1188         r = -EINVAL;
1189     }
1190 
1191     return r;
1192 }
1193 
1194 static int omapfb_mmap(struct fb_info *info, struct vm_area_struct *vma)
1195 {
1196     struct omapfb_plane_struct *plane = info->par;
1197     struct omapfb_device *fbdev = plane->fbdev;
1198     int r;
1199 
1200     omapfb_rqueue_lock(fbdev);
1201     r = fbdev->ctrl->mmap(info, vma);
1202     omapfb_rqueue_unlock(fbdev);
1203 
1204     return r;
1205 }
1206 
1207 /*
1208  * Callback table for the frame buffer framework. Some of these pointers
1209  * will be changed according to the current setting of fb_info->accel_flags.
1210  */
1211 static struct fb_ops omapfb_ops = {
1212     .owner      = THIS_MODULE,
1213     .fb_open        = omapfb_open,
1214     .fb_release     = omapfb_release,
1215     .fb_setcolreg   = omapfb_setcolreg,
1216     .fb_setcmap = omapfb_setcmap,
1217     .fb_fillrect    = cfb_fillrect,
1218     .fb_copyarea    = cfb_copyarea,
1219     .fb_imageblit   = cfb_imageblit,
1220     .fb_blank       = omapfb_blank,
1221     .fb_ioctl   = omapfb_ioctl,
1222     .fb_check_var   = omapfb_check_var,
1223     .fb_set_par = omapfb_set_par,
1224     .fb_pan_display = omapfb_pan_display,
1225 };
1226 
1227 /*
1228  * ---------------------------------------------------------------------------
1229  * Sysfs interface
1230  * ---------------------------------------------------------------------------
1231  */
1232 /* omapfbX sysfs entries */
1233 static ssize_t omapfb_show_caps_num(struct device *dev,
1234                     struct device_attribute *attr, char *buf)
1235 {
1236     struct omapfb_device *fbdev = dev_get_drvdata(dev);
1237     int plane;
1238     size_t size;
1239     struct omapfb_caps caps;
1240 
1241     plane = 0;
1242     size = 0;
1243     while (size < PAGE_SIZE && plane < OMAPFB_PLANE_NUM) {
1244         omapfb_get_caps(fbdev, plane, &caps);
1245         size += scnprintf(&buf[size], PAGE_SIZE - size,
1246             "plane#%d %#010x %#010x %#010x\n",
1247             plane, caps.ctrl, caps.plane_color, caps.wnd_color);
1248         plane++;
1249     }
1250     return size;
1251 }
1252 
1253 static ssize_t omapfb_show_caps_text(struct device *dev,
1254                      struct device_attribute *attr, char *buf)
1255 {
1256     struct omapfb_device *fbdev = dev_get_drvdata(dev);
1257     int i;
1258     struct omapfb_caps caps;
1259     int plane;
1260     size_t size;
1261 
1262     plane = 0;
1263     size = 0;
1264     while (size < PAGE_SIZE && plane < OMAPFB_PLANE_NUM) {
1265         omapfb_get_caps(fbdev, plane, &caps);
1266         size += scnprintf(&buf[size], PAGE_SIZE - size,
1267                  "plane#%d:\n", plane);
1268         for (i = 0; i < ARRAY_SIZE(ctrl_caps) &&
1269              size < PAGE_SIZE; i++) {
1270             if (ctrl_caps[i].flag & caps.ctrl)
1271                 size += scnprintf(&buf[size], PAGE_SIZE - size,
1272                     " %s\n", ctrl_caps[i].name);
1273         }
1274         size += scnprintf(&buf[size], PAGE_SIZE - size,
1275                  " plane colors:\n");
1276         for (i = 0; i < ARRAY_SIZE(color_caps) &&
1277              size < PAGE_SIZE; i++) {
1278             if (color_caps[i].flag & caps.plane_color)
1279                 size += scnprintf(&buf[size], PAGE_SIZE - size,
1280                     "  %s\n", color_caps[i].name);
1281         }
1282         size += scnprintf(&buf[size], PAGE_SIZE - size,
1283                  " window colors:\n");
1284         for (i = 0; i < ARRAY_SIZE(color_caps) &&
1285              size < PAGE_SIZE; i++) {
1286             if (color_caps[i].flag & caps.wnd_color)
1287                 size += scnprintf(&buf[size], PAGE_SIZE - size,
1288                     "  %s\n", color_caps[i].name);
1289         }
1290 
1291         plane++;
1292     }
1293     return size;
1294 }
1295 
1296 static DEVICE_ATTR(caps_num, 0444, omapfb_show_caps_num, NULL);
1297 static DEVICE_ATTR(caps_text, 0444, omapfb_show_caps_text, NULL);
1298 
1299 /* panel sysfs entries */
1300 static ssize_t omapfb_show_panel_name(struct device *dev,
1301                       struct device_attribute *attr, char *buf)
1302 {
1303     struct omapfb_device *fbdev = dev_get_drvdata(dev);
1304 
1305     return sysfs_emit(buf, "%s\n", fbdev->panel->name);
1306 }
1307 
1308 static ssize_t omapfb_show_bklight_level(struct device *dev,
1309                      struct device_attribute *attr,
1310                      char *buf)
1311 {
1312     struct omapfb_device *fbdev = dev_get_drvdata(dev);
1313     int r;
1314 
1315     if (fbdev->panel->get_bklight_level) {
1316         r = sysfs_emit(buf, "%d\n",
1317                    fbdev->panel->get_bklight_level(fbdev->panel));
1318     } else
1319         r = -ENODEV;
1320     return r;
1321 }
1322 
1323 static ssize_t omapfb_store_bklight_level(struct device *dev,
1324                       struct device_attribute *attr,
1325                       const char *buf, size_t size)
1326 {
1327     struct omapfb_device *fbdev = dev_get_drvdata(dev);
1328     int r;
1329 
1330     if (fbdev->panel->set_bklight_level) {
1331         unsigned int level;
1332 
1333         if (sscanf(buf, "%10d", &level) == 1) {
1334             r = fbdev->panel->set_bklight_level(fbdev->panel,
1335                                 level);
1336         } else
1337             r = -EINVAL;
1338     } else
1339         r = -ENODEV;
1340     return r ? r : size;
1341 }
1342 
1343 static ssize_t omapfb_show_bklight_max(struct device *dev,
1344                        struct device_attribute *attr, char *buf)
1345 {
1346     struct omapfb_device *fbdev = dev_get_drvdata(dev);
1347     int r;
1348 
1349     if (fbdev->panel->get_bklight_level) {
1350         r = sysfs_emit(buf, "%d\n",
1351                    fbdev->panel->get_bklight_max(fbdev->panel));
1352     } else
1353         r = -ENODEV;
1354     return r;
1355 }
1356 
1357 static struct device_attribute dev_attr_panel_name =
1358     __ATTR(name, 0444, omapfb_show_panel_name, NULL);
1359 static DEVICE_ATTR(backlight_level, 0664,
1360            omapfb_show_bklight_level, omapfb_store_bklight_level);
1361 static DEVICE_ATTR(backlight_max, 0444, omapfb_show_bklight_max, NULL);
1362 
1363 static struct attribute *panel_attrs[] = {
1364     &dev_attr_panel_name.attr,
1365     &dev_attr_backlight_level.attr,
1366     &dev_attr_backlight_max.attr,
1367     NULL,
1368 };
1369 
1370 static const struct attribute_group panel_attr_grp = {
1371     .name  = "panel",
1372     .attrs = panel_attrs,
1373 };
1374 
1375 /* ctrl sysfs entries */
1376 static ssize_t omapfb_show_ctrl_name(struct device *dev,
1377                      struct device_attribute *attr, char *buf)
1378 {
1379     struct omapfb_device *fbdev = dev_get_drvdata(dev);
1380 
1381     return sysfs_emit(buf, "%s\n", fbdev->ctrl->name);
1382 }
1383 
1384 static struct device_attribute dev_attr_ctrl_name =
1385     __ATTR(name, 0444, omapfb_show_ctrl_name, NULL);
1386 
1387 static struct attribute *ctrl_attrs[] = {
1388     &dev_attr_ctrl_name.attr,
1389     NULL,
1390 };
1391 
1392 static const struct attribute_group ctrl_attr_grp = {
1393     .name  = "ctrl",
1394     .attrs = ctrl_attrs,
1395 };
1396 
1397 static int omapfb_register_sysfs(struct omapfb_device *fbdev)
1398 {
1399     int r;
1400 
1401     if ((r = device_create_file(fbdev->dev, &dev_attr_caps_num)))
1402         goto fail0;
1403 
1404     if ((r = device_create_file(fbdev->dev, &dev_attr_caps_text)))
1405         goto fail1;
1406 
1407     if ((r = sysfs_create_group(&fbdev->dev->kobj, &panel_attr_grp)))
1408         goto fail2;
1409 
1410     if ((r = sysfs_create_group(&fbdev->dev->kobj, &ctrl_attr_grp)))
1411         goto fail3;
1412 
1413     return 0;
1414 fail3:
1415     sysfs_remove_group(&fbdev->dev->kobj, &panel_attr_grp);
1416 fail2:
1417     device_remove_file(fbdev->dev, &dev_attr_caps_text);
1418 fail1:
1419     device_remove_file(fbdev->dev, &dev_attr_caps_num);
1420 fail0:
1421     dev_err(fbdev->dev, "unable to register sysfs interface\n");
1422     return r;
1423 }
1424 
1425 static void omapfb_unregister_sysfs(struct omapfb_device *fbdev)
1426 {
1427     sysfs_remove_group(&fbdev->dev->kobj, &ctrl_attr_grp);
1428     sysfs_remove_group(&fbdev->dev->kobj, &panel_attr_grp);
1429     device_remove_file(fbdev->dev, &dev_attr_caps_num);
1430     device_remove_file(fbdev->dev, &dev_attr_caps_text);
1431 }
1432 
1433 /*
1434  * ---------------------------------------------------------------------------
1435  * LDM callbacks
1436  * ---------------------------------------------------------------------------
1437  */
1438 /* Initialize system fb_info object and set the default video mode.
1439  * The frame buffer memory already allocated by lcddma_init
1440  */
1441 static int fbinfo_init(struct omapfb_device *fbdev, struct fb_info *info)
1442 {
1443     struct fb_var_screeninfo    *var = &info->var;
1444     struct fb_fix_screeninfo    *fix = &info->fix;
1445     int             r = 0;
1446 
1447     info->fbops = &omapfb_ops;
1448     info->flags = FBINFO_FLAG_DEFAULT;
1449 
1450     strncpy(fix->id, MODULE_NAME, sizeof(fix->id));
1451 
1452     info->pseudo_palette = fbdev->pseudo_palette;
1453 
1454     var->accel_flags  = def_accel ? FB_ACCELF_TEXT : 0;
1455     var->xres = def_vxres;
1456     var->yres = def_vyres;
1457     var->xres_virtual = def_vxres;
1458     var->yres_virtual = def_vyres;
1459     var->rotate   = def_rotate;
1460     var->bits_per_pixel = fbdev->panel->bpp;
1461 
1462     set_fb_var(info, var);
1463     set_fb_fix(info, 1);
1464 
1465     r = fb_alloc_cmap(&info->cmap, 16, 0);
1466     if (r != 0)
1467         dev_err(fbdev->dev, "unable to allocate color map memory\n");
1468 
1469     return r;
1470 }
1471 
1472 /* Release the fb_info object */
1473 static void fbinfo_cleanup(struct omapfb_device *fbdev, struct fb_info *fbi)
1474 {
1475     fb_dealloc_cmap(&fbi->cmap);
1476 }
1477 
1478 static void planes_cleanup(struct omapfb_device *fbdev)
1479 {
1480     int i;
1481 
1482     for (i = 0; i < fbdev->mem_desc.region_cnt; i++) {
1483         if (fbdev->fb_info[i] == NULL)
1484             break;
1485         fbinfo_cleanup(fbdev, fbdev->fb_info[i]);
1486         framebuffer_release(fbdev->fb_info[i]);
1487     }
1488 }
1489 
1490 static int planes_init(struct omapfb_device *fbdev)
1491 {
1492     struct fb_info *fbi;
1493     int i;
1494     int r;
1495 
1496     for (i = 0; i < fbdev->mem_desc.region_cnt; i++) {
1497         struct omapfb_plane_struct *plane;
1498         fbi = framebuffer_alloc(sizeof(struct omapfb_plane_struct),
1499                     fbdev->dev);
1500         if (fbi == NULL) {
1501             planes_cleanup(fbdev);
1502             return -ENOMEM;
1503         }
1504         plane = fbi->par;
1505         plane->idx = i;
1506         plane->fbdev = fbdev;
1507         plane->info.mirror = def_mirror;
1508         fbdev->fb_info[i] = fbi;
1509 
1510         if ((r = fbinfo_init(fbdev, fbi)) < 0) {
1511             framebuffer_release(fbi);
1512             planes_cleanup(fbdev);
1513             return r;
1514         }
1515         plane->info.out_width = fbi->var.xres;
1516         plane->info.out_height = fbi->var.yres;
1517     }
1518     return 0;
1519 }
1520 
1521 /*
1522  * Free driver resources. Can be called to rollback an aborted initialization
1523  * sequence.
1524  */
1525 static void omapfb_free_resources(struct omapfb_device *fbdev, int state)
1526 {
1527     int i;
1528 
1529     switch (state) {
1530     case OMAPFB_ACTIVE:
1531         for (i = 0; i < fbdev->mem_desc.region_cnt; i++)
1532             unregister_framebuffer(fbdev->fb_info[i]);
1533         fallthrough;
1534     case 7:
1535         omapfb_unregister_sysfs(fbdev);
1536         fallthrough;
1537     case 6:
1538         if (fbdev->panel->disable)
1539             fbdev->panel->disable(fbdev->panel);
1540         fallthrough;
1541     case 5:
1542         omapfb_set_update_mode(fbdev, OMAPFB_UPDATE_DISABLED);
1543         fallthrough;
1544     case 4:
1545         planes_cleanup(fbdev);
1546         fallthrough;
1547     case 3:
1548         ctrl_cleanup(fbdev);
1549         fallthrough;
1550     case 2:
1551         if (fbdev->panel->cleanup)
1552             fbdev->panel->cleanup(fbdev->panel);
1553         fallthrough;
1554     case 1:
1555         dev_set_drvdata(fbdev->dev, NULL);
1556         kfree(fbdev);
1557         break;
1558     case 0:
1559         /* nothing to free */
1560         break;
1561     default:
1562         BUG();
1563     }
1564 }
1565 
1566 static int omapfb_find_ctrl(struct omapfb_device *fbdev)
1567 {
1568     struct omapfb_platform_data *conf;
1569     char name[17];
1570     int i;
1571 
1572     conf = dev_get_platdata(fbdev->dev);
1573 
1574     fbdev->ctrl = NULL;
1575 
1576     strncpy(name, conf->lcd.ctrl_name, sizeof(name) - 1);
1577     name[sizeof(name) - 1] = '\0';
1578 
1579     if (strcmp(name, "internal") == 0) {
1580         fbdev->ctrl = fbdev->int_ctrl;
1581         return 0;
1582     }
1583 
1584     for (i = 0; i < ARRAY_SIZE(ctrls); i++) {
1585         dev_dbg(fbdev->dev, "ctrl %s\n", ctrls[i]->name);
1586         if (strcmp(ctrls[i]->name, name) == 0) {
1587             fbdev->ctrl = ctrls[i];
1588             break;
1589         }
1590     }
1591 
1592     if (fbdev->ctrl == NULL) {
1593         dev_dbg(fbdev->dev, "ctrl %s not supported\n", name);
1594         return -1;
1595     }
1596 
1597     return 0;
1598 }
1599 
1600 /*
1601  * Called by LDM binding to probe and attach a new device.
1602  * Initialization sequence:
1603  *   1. allocate system omapfb_device structure
1604  *   2. select controller type according to platform configuration
1605  *      init LCD panel
1606  *   3. init LCD controller and LCD DMA
1607  *   4. init system fb_info structure for all planes
1608  *   5. setup video mode for first plane and enable it
1609  *   6. enable LCD panel
1610  *   7. register sysfs attributes
1611  *   OMAPFB_ACTIVE: register system fb_info structure for all planes
1612  */
1613 static int omapfb_do_probe(struct platform_device *pdev,
1614                 struct lcd_panel *panel)
1615 {
1616     struct omapfb_device    *fbdev = NULL;
1617     int         init_state;
1618     unsigned long       phz, hhz, vhz;
1619     unsigned long       vram;
1620     int         i;
1621     int         r = 0;
1622 
1623     init_state = 0;
1624 
1625     if (pdev->num_resources != 2) {
1626         dev_err(&pdev->dev, "probed for an unknown device\n");
1627         r = -ENODEV;
1628         goto cleanup;
1629     }
1630 
1631     if (dev_get_platdata(&pdev->dev) == NULL) {
1632         dev_err(&pdev->dev, "missing platform data\n");
1633         r = -ENOENT;
1634         goto cleanup;
1635     }
1636 
1637     fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL);
1638     if (fbdev == NULL) {
1639         dev_err(&pdev->dev,
1640             "unable to allocate memory for device info\n");
1641         r = -ENOMEM;
1642         goto cleanup;
1643     }
1644     fbdev->int_irq = platform_get_irq(pdev, 0);
1645     if (fbdev->int_irq < 0) {
1646         r = ENXIO;
1647         goto cleanup;
1648     }
1649 
1650     fbdev->ext_irq = platform_get_irq(pdev, 1);
1651     if (fbdev->ext_irq < 0) {
1652         r = ENXIO;
1653         goto cleanup;
1654     }
1655 
1656     init_state++;
1657 
1658     fbdev->dev = &pdev->dev;
1659     fbdev->panel = panel;
1660     fbdev->dssdev = &omapdss_device;
1661     platform_set_drvdata(pdev, fbdev);
1662 
1663     mutex_init(&fbdev->rqueue_mutex);
1664 
1665     fbdev->int_ctrl = &omap1_int_ctrl;
1666 #ifdef CONFIG_FB_OMAP_LCDC_EXTERNAL
1667     fbdev->ext_if = &omap1_ext_if;
1668 #endif
1669     if (omapfb_find_ctrl(fbdev) < 0) {
1670         dev_err(fbdev->dev,
1671             "LCD controller not found, board not supported\n");
1672         r = -ENODEV;
1673         goto cleanup;
1674     }
1675 
1676     if (fbdev->panel->init) {
1677         r = fbdev->panel->init(fbdev->panel, fbdev);
1678         if (r)
1679             goto cleanup;
1680     }
1681 
1682     pr_info("omapfb: configured for panel %s\n", fbdev->panel->name);
1683 
1684     def_vxres = def_vxres ? def_vxres : fbdev->panel->x_res;
1685     def_vyres = def_vyres ? def_vyres : fbdev->panel->y_res;
1686 
1687     init_state++;
1688 
1689     r = ctrl_init(fbdev);
1690     if (r)
1691         goto cleanup;
1692     if (fbdev->ctrl->mmap != NULL)
1693         omapfb_ops.fb_mmap = omapfb_mmap;
1694     init_state++;
1695 
1696     r = planes_init(fbdev);
1697     if (r)
1698         goto cleanup;
1699     init_state++;
1700 
1701 #ifdef CONFIG_FB_OMAP_DMA_TUNE
1702     /* Set DMA priority for EMIFF access to highest */
1703     omap_set_dma_priority(0, OMAP_DMA_PORT_EMIFF, 15);
1704 #endif
1705 
1706     r = ctrl_change_mode(fbdev->fb_info[0]);
1707     if (r) {
1708         dev_err(fbdev->dev, "mode setting failed\n");
1709         goto cleanup;
1710     }
1711 
1712     /* GFX plane is enabled by default */
1713     r = fbdev->ctrl->enable_plane(OMAPFB_PLANE_GFX, 1);
1714     if (r)
1715         goto cleanup;
1716 
1717     omapfb_set_update_mode(fbdev, manual_update ?
1718                    OMAPFB_MANUAL_UPDATE : OMAPFB_AUTO_UPDATE);
1719     init_state++;
1720 
1721     if (fbdev->panel->enable) {
1722         r = fbdev->panel->enable(fbdev->panel);
1723         if (r)
1724             goto cleanup;
1725     }
1726     init_state++;
1727 
1728     r = omapfb_register_sysfs(fbdev);
1729     if (r)
1730         goto cleanup;
1731     init_state++;
1732 
1733     vram = 0;
1734     for (i = 0; i < fbdev->mem_desc.region_cnt; i++) {
1735         r = register_framebuffer(fbdev->fb_info[i]);
1736         if (r != 0) {
1737             dev_err(fbdev->dev,
1738                 "registering framebuffer %d failed\n", i);
1739             goto cleanup;
1740         }
1741         vram += fbdev->mem_desc.region[i].size;
1742     }
1743 
1744     fbdev->state = OMAPFB_ACTIVE;
1745 
1746     panel = fbdev->panel;
1747     phz = panel->pixel_clock * 1000;
1748     hhz = phz * 10 / (panel->hfp + panel->x_res + panel->hbp + panel->hsw);
1749     vhz = hhz / (panel->vfp + panel->y_res + panel->vbp + panel->vsw);
1750 
1751     omapfb_dev = fbdev;
1752 
1753     pr_info("omapfb: Framebuffer initialized. Total vram %lu planes %d\n",
1754             vram, fbdev->mem_desc.region_cnt);
1755     pr_info("omapfb: Pixclock %lu kHz hfreq %lu.%lu kHz "
1756             "vfreq %lu.%lu Hz\n",
1757             phz / 1000, hhz / 10000, hhz % 10, vhz / 10, vhz % 10);
1758 
1759     return 0;
1760 
1761 cleanup:
1762     omapfb_free_resources(fbdev, init_state);
1763 
1764     return r;
1765 }
1766 
1767 static int omapfb_probe(struct platform_device *pdev)
1768 {
1769     int r;
1770 
1771     BUG_ON(fbdev_pdev != NULL);
1772 
1773     r = platform_device_register(&omapdss_device);
1774     if (r) {
1775         dev_err(&pdev->dev, "can't register omapdss device\n");
1776         return r;
1777     }
1778 
1779     /* Delay actual initialization until the LCD is registered */
1780     fbdev_pdev = pdev;
1781     if (fbdev_panel != NULL)
1782         omapfb_do_probe(fbdev_pdev, fbdev_panel);
1783     return 0;
1784 }
1785 
1786 void omapfb_register_panel(struct lcd_panel *panel)
1787 {
1788     BUG_ON(fbdev_panel != NULL);
1789 
1790     fbdev_panel = panel;
1791     if (fbdev_pdev != NULL)
1792         omapfb_do_probe(fbdev_pdev, fbdev_panel);
1793 }
1794 EXPORT_SYMBOL_GPL(omapfb_register_panel);
1795 
1796 /* Called when the device is being detached from the driver */
1797 static int omapfb_remove(struct platform_device *pdev)
1798 {
1799     struct omapfb_device *fbdev = platform_get_drvdata(pdev);
1800     enum omapfb_state saved_state = fbdev->state;
1801 
1802     /* FIXME: wait till completion of pending events */
1803 
1804     fbdev->state = OMAPFB_DISABLED;
1805     omapfb_free_resources(fbdev, saved_state);
1806 
1807     platform_device_unregister(&omapdss_device);
1808     fbdev->dssdev = NULL;
1809 
1810     return 0;
1811 }
1812 
1813 /* PM suspend */
1814 static int omapfb_suspend(struct platform_device *pdev, pm_message_t mesg)
1815 {
1816     struct omapfb_device *fbdev = platform_get_drvdata(pdev);
1817 
1818     if (fbdev != NULL)
1819         omapfb_blank(FB_BLANK_POWERDOWN, fbdev->fb_info[0]);
1820     return 0;
1821 }
1822 
1823 /* PM resume */
1824 static int omapfb_resume(struct platform_device *pdev)
1825 {
1826     struct omapfb_device *fbdev = platform_get_drvdata(pdev);
1827 
1828     if (fbdev != NULL)
1829         omapfb_blank(FB_BLANK_UNBLANK, fbdev->fb_info[0]);
1830     return 0;
1831 }
1832 
1833 static struct platform_driver omapfb_driver = {
1834     .probe      = omapfb_probe,
1835     .remove     = omapfb_remove,
1836     .suspend    = omapfb_suspend,
1837     .resume     = omapfb_resume,
1838     .driver     = {
1839         .name   = MODULE_NAME,
1840     },
1841 };
1842 
1843 #ifndef MODULE
1844 
1845 /* Process kernel command line parameters */
1846 static int __init omapfb_setup(char *options)
1847 {
1848     char *this_opt = NULL;
1849     int r = 0;
1850 
1851     pr_debug("omapfb: options %s\n", options);
1852 
1853     if (!options || !*options)
1854         return 0;
1855 
1856     while (!r && (this_opt = strsep(&options, ",")) != NULL) {
1857         if (!strncmp(this_opt, "accel", 5))
1858             def_accel = 1;
1859         else if (!strncmp(this_opt, "vram:", 5)) {
1860             char *suffix;
1861             unsigned long vram;
1862             vram = (simple_strtoul(this_opt + 5, &suffix, 0));
1863             switch (suffix[0]) {
1864             case '\0':
1865                 break;
1866             case 'm':
1867             case 'M':
1868                 vram *= 1024;
1869                 fallthrough;
1870             case 'k':
1871             case 'K':
1872                 vram *= 1024;
1873                 break;
1874             default:
1875                 pr_debug("omapfb: invalid vram suffix %c\n",
1876                      suffix[0]);
1877                 r = -1;
1878             }
1879             def_vram[def_vram_cnt++] = vram;
1880         }
1881         else if (!strncmp(this_opt, "vxres:", 6))
1882             def_vxres = simple_strtoul(this_opt + 6, NULL, 0);
1883         else if (!strncmp(this_opt, "vyres:", 6))
1884             def_vyres = simple_strtoul(this_opt + 6, NULL, 0);
1885         else if (!strncmp(this_opt, "rotate:", 7))
1886             def_rotate = (simple_strtoul(this_opt + 7, NULL, 0));
1887         else if (!strncmp(this_opt, "mirror:", 7))
1888             def_mirror = (simple_strtoul(this_opt + 7, NULL, 0));
1889         else if (!strncmp(this_opt, "manual_update", 13))
1890             manual_update = 1;
1891         else {
1892             pr_debug("omapfb: invalid option\n");
1893             r = -1;
1894         }
1895     }
1896 
1897     return r;
1898 }
1899 
1900 #endif
1901 
1902 /* Register both the driver and the device */
1903 static int __init omapfb_init(void)
1904 {
1905 #ifndef MODULE
1906     char *option;
1907 
1908     if (fb_get_options("omapfb", &option))
1909         return -ENODEV;
1910     omapfb_setup(option);
1911 #endif
1912     /* Register the driver with LDM */
1913     if (platform_driver_register(&omapfb_driver)) {
1914         pr_debug("failed to register omapfb driver\n");
1915         return -ENODEV;
1916     }
1917 
1918     return 0;
1919 }
1920 
1921 static void __exit omapfb_cleanup(void)
1922 {
1923     platform_driver_unregister(&omapfb_driver);
1924 }
1925 
1926 module_param_named(accel, def_accel, uint, 0664);
1927 module_param_array_named(vram, def_vram, ulong, &def_vram_cnt, 0664);
1928 module_param_named(vxres, def_vxres, long, 0664);
1929 module_param_named(vyres, def_vyres, long, 0664);
1930 module_param_named(rotate, def_rotate, uint, 0664);
1931 module_param_named(mirror, def_mirror, uint, 0664);
1932 module_param_named(manual_update, manual_update, bool, 0664);
1933 
1934 module_init(omapfb_init);
1935 module_exit(omapfb_cleanup);
1936 
1937 MODULE_DESCRIPTION("TI OMAP framebuffer driver");
1938 MODULE_AUTHOR("Imre Deak <imre.deak@nokia.com>");
1939 MODULE_LICENSE("GPL");