linux/drivers/video/fbdev/omap2/omapfb/omapfb-main.c
<<
>>
Prefs
   1/*
   2 * linux/drivers/video/omap2/omapfb-main.c
   3 *
   4 * Copyright (C) 2008 Nokia Corporation
   5 * Author: Tomi Valkeinen <tomi.valkeinen@nokia.com>
   6 *
   7 * Some code and ideas taken from drivers/video/omap/ driver
   8 * by Imre Deak.
   9 *
  10 * This program is free software; you can redistribute it and/or modify it
  11 * under the terms of the GNU General Public License version 2 as published by
  12 * the Free Software Foundation.
  13 *
  14 * This program is distributed in the hope that it will be useful, but WITHOUT
  15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  16 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  17 * more details.
  18 *
  19 * You should have received a copy of the GNU General Public License along with
  20 * this program.  If not, see <http://www.gnu.org/licenses/>.
  21 */
  22
  23#include <linux/module.h>
  24#include <linux/delay.h>
  25#include <linux/slab.h>
  26#include <linux/fb.h>
  27#include <linux/dma-mapping.h>
  28#include <linux/vmalloc.h>
  29#include <linux/device.h>
  30#include <linux/platform_device.h>
  31#include <linux/omapfb.h>
  32
  33#include <video/omapfb_dss.h>
  34#include <video/omapvrfb.h>
  35
  36#include "omapfb.h"
  37
  38#define MODULE_NAME     "omapfb"
  39
  40#define OMAPFB_PLANE_XRES_MIN           8
  41#define OMAPFB_PLANE_YRES_MIN           8
  42
  43static char *def_mode;
  44static char *def_vram;
  45static bool def_vrfb;
  46static int def_rotate;
  47static bool def_mirror;
  48static bool auto_update;
  49static unsigned int auto_update_freq;
  50module_param(auto_update, bool, 0);
  51module_param(auto_update_freq, uint, 0644);
  52
  53#ifdef DEBUG
  54bool omapfb_debug;
  55module_param_named(debug, omapfb_debug, bool, 0644);
  56static bool omapfb_test_pattern;
  57module_param_named(test, omapfb_test_pattern, bool, 0644);
  58#endif
  59
  60static int omapfb_fb_init(struct omapfb2_device *fbdev, struct fb_info *fbi);
  61static int omapfb_get_recommended_bpp(struct omapfb2_device *fbdev,
  62                struct omap_dss_device *dssdev);
  63
  64#ifdef DEBUG
  65static void draw_pixel(struct fb_info *fbi, int x, int y, unsigned color)
  66{
  67        struct fb_var_screeninfo *var = &fbi->var;
  68        struct fb_fix_screeninfo *fix = &fbi->fix;
  69        void __iomem *addr = fbi->screen_base;
  70        const unsigned bytespp = var->bits_per_pixel >> 3;
  71        const unsigned line_len = fix->line_length / bytespp;
  72
  73        int r = (color >> 16) & 0xff;
  74        int g = (color >> 8) & 0xff;
  75        int b = (color >> 0) & 0xff;
  76
  77        if (var->bits_per_pixel == 16) {
  78                u16 __iomem *p = (u16 __iomem *)addr;
  79                p += y * line_len + x;
  80
  81                r = r * 32 / 256;
  82                g = g * 64 / 256;
  83                b = b * 32 / 256;
  84
  85                __raw_writew((r << 11) | (g << 5) | (b << 0), p);
  86        } else if (var->bits_per_pixel == 24) {
  87                u8 __iomem *p = (u8 __iomem *)addr;
  88                p += (y * line_len + x) * 3;
  89
  90                __raw_writeb(b, p + 0);
  91                __raw_writeb(g, p + 1);
  92                __raw_writeb(r, p + 2);
  93        } else if (var->bits_per_pixel == 32) {
  94                u32 __iomem *p = (u32 __iomem *)addr;
  95                p += y * line_len + x;
  96                __raw_writel(color, p);
  97        }
  98}
  99
 100static void fill_fb(struct fb_info *fbi)
 101{
 102        struct fb_var_screeninfo *var = &fbi->var;
 103        const short w = var->xres_virtual;
 104        const short h = var->yres_virtual;
 105        void __iomem *addr = fbi->screen_base;
 106        int y, x;
 107
 108        if (!addr)
 109                return;
 110
 111        DBG("fill_fb %dx%d, line_len %d bytes\n", w, h, fbi->fix.line_length);
 112
 113        for (y = 0; y < h; y++) {
 114                for (x = 0; x < w; x++) {
 115                        if (x < 20 && y < 20)
 116                                draw_pixel(fbi, x, y, 0xffffff);
 117                        else if (x < 20 && (y > 20 && y < h - 20))
 118                                draw_pixel(fbi, x, y, 0xff);
 119                        else if (y < 20 && (x > 20 && x < w - 20))
 120                                draw_pixel(fbi, x, y, 0xff00);
 121                        else if (x > w - 20 && (y > 20 && y < h - 20))
 122                                draw_pixel(fbi, x, y, 0xff0000);
 123                        else if (y > h - 20 && (x > 20 && x < w - 20))
 124                                draw_pixel(fbi, x, y, 0xffff00);
 125                        else if (x == 20 || x == w - 20 ||
 126                                        y == 20 || y == h - 20)
 127                                draw_pixel(fbi, x, y, 0xffffff);
 128                        else if (x == y || w - x == h - y)
 129                                draw_pixel(fbi, x, y, 0xff00ff);
 130                        else if (w - x == y || x == h - y)
 131                                draw_pixel(fbi, x, y, 0x00ffff);
 132                        else if (x > 20 && y > 20 && x < w - 20 && y < h - 20) {
 133                                int t = x * 3 / w;
 134                                unsigned r = 0, g = 0, b = 0;
 135                                unsigned c;
 136                                if (var->bits_per_pixel == 16) {
 137                                        if (t == 0)
 138                                                b = (y % 32) * 256 / 32;
 139                                        else if (t == 1)
 140                                                g = (y % 64) * 256 / 64;
 141                                        else if (t == 2)
 142                                                r = (y % 32) * 256 / 32;
 143                                } else {
 144                                        if (t == 0)
 145                                                b = (y % 256);
 146                                        else if (t == 1)
 147                                                g = (y % 256);
 148                                        else if (t == 2)
 149                                                r = (y % 256);
 150                                }
 151                                c = (r << 16) | (g << 8) | (b << 0);
 152                                draw_pixel(fbi, x, y, c);
 153                        } else {
 154                                draw_pixel(fbi, x, y, 0);
 155                        }
 156                }
 157        }
 158}
 159#endif
 160
 161static unsigned omapfb_get_vrfb_offset(const struct omapfb_info *ofbi, int rot)
 162{
 163        const struct vrfb *vrfb = &ofbi->region->vrfb;
 164        unsigned offset;
 165
 166        switch (rot) {
 167        case FB_ROTATE_UR:
 168                offset = 0;
 169                break;
 170        case FB_ROTATE_CW:
 171                offset = vrfb->yoffset;
 172                break;
 173        case FB_ROTATE_UD:
 174                offset = vrfb->yoffset * OMAP_VRFB_LINE_LEN + vrfb->xoffset;
 175                break;
 176        case FB_ROTATE_CCW:
 177                offset = vrfb->xoffset * OMAP_VRFB_LINE_LEN;
 178                break;
 179        default:
 180                BUG();
 181                return 0;
 182        }
 183
 184        offset *= vrfb->bytespp;
 185
 186        return offset;
 187}
 188
 189static u32 omapfb_get_region_rot_paddr(const struct omapfb_info *ofbi, int rot)
 190{
 191        if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB) {
 192                return ofbi->region->vrfb.paddr[rot]
 193                        + omapfb_get_vrfb_offset(ofbi, rot);
 194        } else {
 195                return ofbi->region->paddr;
 196        }
 197}
 198
 199static u32 omapfb_get_region_paddr(const struct omapfb_info *ofbi)
 200{
 201        if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB)
 202                return ofbi->region->vrfb.paddr[0];
 203        else
 204                return ofbi->region->paddr;
 205}
 206
 207static void __iomem *omapfb_get_region_vaddr(const struct omapfb_info *ofbi)
 208{
 209        if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB)
 210                return ofbi->region->vrfb.vaddr[0];
 211        else
 212                return ofbi->region->vaddr;
 213}
 214
 215static struct omapfb_colormode omapfb_colormodes[] = {
 216        {
 217                .dssmode = OMAP_DSS_COLOR_UYVY,
 218                .bits_per_pixel = 16,
 219                .nonstd = OMAPFB_COLOR_YUV422,
 220        }, {
 221                .dssmode = OMAP_DSS_COLOR_YUV2,
 222                .bits_per_pixel = 16,
 223                .nonstd = OMAPFB_COLOR_YUY422,
 224        }, {
 225                .dssmode = OMAP_DSS_COLOR_ARGB16,
 226                .bits_per_pixel = 16,
 227                .red    = { .length = 4, .offset = 8, .msb_right = 0 },
 228                .green  = { .length = 4, .offset = 4, .msb_right = 0 },
 229                .blue   = { .length = 4, .offset = 0, .msb_right = 0 },
 230                .transp = { .length = 4, .offset = 12, .msb_right = 0 },
 231        }, {
 232                .dssmode = OMAP_DSS_COLOR_RGB16,
 233                .bits_per_pixel = 16,
 234                .red    = { .length = 5, .offset = 11, .msb_right = 0 },
 235                .green  = { .length = 6, .offset = 5, .msb_right = 0 },
 236                .blue   = { .length = 5, .offset = 0, .msb_right = 0 },
 237                .transp = { .length = 0, .offset = 0, .msb_right = 0 },
 238        }, {
 239                .dssmode = OMAP_DSS_COLOR_RGB24P,
 240                .bits_per_pixel = 24,
 241                .red    = { .length = 8, .offset = 16, .msb_right = 0 },
 242                .green  = { .length = 8, .offset = 8, .msb_right = 0 },
 243                .blue   = { .length = 8, .offset = 0, .msb_right = 0 },
 244                .transp = { .length = 0, .offset = 0, .msb_right = 0 },
 245        }, {
 246                .dssmode = OMAP_DSS_COLOR_RGB24U,
 247                .bits_per_pixel = 32,
 248                .red    = { .length = 8, .offset = 16, .msb_right = 0 },
 249                .green  = { .length = 8, .offset = 8, .msb_right = 0 },
 250                .blue   = { .length = 8, .offset = 0, .msb_right = 0 },
 251                .transp = { .length = 0, .offset = 0, .msb_right = 0 },
 252        }, {
 253                .dssmode = OMAP_DSS_COLOR_ARGB32,
 254                .bits_per_pixel = 32,
 255                .red    = { .length = 8, .offset = 16, .msb_right = 0 },
 256                .green  = { .length = 8, .offset = 8, .msb_right = 0 },
 257                .blue   = { .length = 8, .offset = 0, .msb_right = 0 },
 258                .transp = { .length = 8, .offset = 24, .msb_right = 0 },
 259        }, {
 260                .dssmode = OMAP_DSS_COLOR_RGBA32,
 261                .bits_per_pixel = 32,
 262                .red    = { .length = 8, .offset = 24, .msb_right = 0 },
 263                .green  = { .length = 8, .offset = 16, .msb_right = 0 },
 264                .blue   = { .length = 8, .offset = 8, .msb_right = 0 },
 265                .transp = { .length = 8, .offset = 0, .msb_right = 0 },
 266        }, {
 267                .dssmode = OMAP_DSS_COLOR_RGBX32,
 268                .bits_per_pixel = 32,
 269                .red    = { .length = 8, .offset = 24, .msb_right = 0 },
 270                .green  = { .length = 8, .offset = 16, .msb_right = 0 },
 271                .blue   = { .length = 8, .offset = 8, .msb_right = 0 },
 272                .transp = { .length = 0, .offset = 0, .msb_right = 0 },
 273        },
 274};
 275
 276static bool cmp_component(struct fb_bitfield *f1, struct fb_bitfield *f2)
 277{
 278        return f1->length == f2->length &&
 279                f1->offset == f2->offset &&
 280                f1->msb_right == f2->msb_right;
 281}
 282
 283static bool cmp_var_to_colormode(struct fb_var_screeninfo *var,
 284                struct omapfb_colormode *color)
 285{
 286        if (var->bits_per_pixel == 0 ||
 287                        var->red.length == 0 ||
 288                        var->blue.length == 0 ||
 289                        var->green.length == 0)
 290                return false;
 291
 292        return var->bits_per_pixel == color->bits_per_pixel &&
 293                cmp_component(&var->red, &color->red) &&
 294                cmp_component(&var->green, &color->green) &&
 295                cmp_component(&var->blue, &color->blue) &&
 296                cmp_component(&var->transp, &color->transp);
 297}
 298
 299static void assign_colormode_to_var(struct fb_var_screeninfo *var,
 300                struct omapfb_colormode *color)
 301{
 302        var->bits_per_pixel = color->bits_per_pixel;
 303        var->nonstd = color->nonstd;
 304        var->red = color->red;
 305        var->green = color->green;
 306        var->blue = color->blue;
 307        var->transp = color->transp;
 308}
 309
 310static int fb_mode_to_dss_mode(struct fb_var_screeninfo *var,
 311                enum omap_color_mode *mode)
 312{
 313        enum omap_color_mode dssmode;
 314        int i;
 315
 316        /* first match with nonstd field */
 317        if (var->nonstd) {
 318                for (i = 0; i < ARRAY_SIZE(omapfb_colormodes); ++i) {
 319                        struct omapfb_colormode *m = &omapfb_colormodes[i];
 320                        if (var->nonstd == m->nonstd) {
 321                                assign_colormode_to_var(var, m);
 322                                *mode = m->dssmode;
 323                                return 0;
 324                        }
 325                }
 326
 327                return -EINVAL;
 328        }
 329
 330        /* then try exact match of bpp and colors */
 331        for (i = 0; i < ARRAY_SIZE(omapfb_colormodes); ++i) {
 332                struct omapfb_colormode *m = &omapfb_colormodes[i];
 333                if (cmp_var_to_colormode(var, m)) {
 334                        assign_colormode_to_var(var, m);
 335                        *mode = m->dssmode;
 336                        return 0;
 337                }
 338        }
 339
 340        /* match with bpp if user has not filled color fields
 341         * properly */
 342        switch (var->bits_per_pixel) {
 343        case 1:
 344                dssmode = OMAP_DSS_COLOR_CLUT1;
 345                break;
 346        case 2:
 347                dssmode = OMAP_DSS_COLOR_CLUT2;
 348                break;
 349        case 4:
 350                dssmode = OMAP_DSS_COLOR_CLUT4;
 351                break;
 352        case 8:
 353                dssmode = OMAP_DSS_COLOR_CLUT8;
 354                break;
 355        case 12:
 356                dssmode = OMAP_DSS_COLOR_RGB12U;
 357                break;
 358        case 16:
 359                dssmode = OMAP_DSS_COLOR_RGB16;
 360                break;
 361        case 24:
 362                dssmode = OMAP_DSS_COLOR_RGB24P;
 363                break;
 364        case 32:
 365                dssmode = OMAP_DSS_COLOR_RGB24U;
 366                break;
 367        default:
 368                return -EINVAL;
 369        }
 370
 371        for (i = 0; i < ARRAY_SIZE(omapfb_colormodes); ++i) {
 372                struct omapfb_colormode *m = &omapfb_colormodes[i];
 373                if (dssmode == m->dssmode) {
 374                        assign_colormode_to_var(var, m);
 375                        *mode = m->dssmode;
 376                        return 0;
 377                }
 378        }
 379
 380        return -EINVAL;
 381}
 382
 383static int check_fb_res_bounds(struct fb_var_screeninfo *var)
 384{
 385        int xres_min = OMAPFB_PLANE_XRES_MIN;
 386        int xres_max = 2048;
 387        int yres_min = OMAPFB_PLANE_YRES_MIN;
 388        int yres_max = 2048;
 389
 390        /* XXX: some applications seem to set virtual res to 0. */
 391        if (var->xres_virtual == 0)
 392                var->xres_virtual = var->xres;
 393
 394        if (var->yres_virtual == 0)
 395                var->yres_virtual = var->yres;
 396
 397        if (var->xres_virtual < xres_min || var->yres_virtual < yres_min)
 398                return -EINVAL;
 399
 400        if (var->xres < xres_min)
 401                var->xres = xres_min;
 402        if (var->yres < yres_min)
 403                var->yres = yres_min;
 404        if (var->xres > xres_max)
 405                var->xres = xres_max;
 406        if (var->yres > yres_max)
 407                var->yres = yres_max;
 408
 409        if (var->xres > var->xres_virtual)
 410                var->xres = var->xres_virtual;
 411        if (var->yres > var->yres_virtual)
 412                var->yres = var->yres_virtual;
 413
 414        return 0;
 415}
 416
 417static void shrink_height(unsigned long max_frame_size,
 418                struct fb_var_screeninfo *var)
 419{
 420        DBG("can't fit FB into memory, reducing y\n");
 421        var->yres_virtual = max_frame_size /
 422                (var->xres_virtual * var->bits_per_pixel >> 3);
 423
 424        if (var->yres_virtual < OMAPFB_PLANE_YRES_MIN)
 425                var->yres_virtual = OMAPFB_PLANE_YRES_MIN;
 426
 427        if (var->yres > var->yres_virtual)
 428                var->yres = var->yres_virtual;
 429}
 430
 431static void shrink_width(unsigned long max_frame_size,
 432                struct fb_var_screeninfo *var)
 433{
 434        DBG("can't fit FB into memory, reducing x\n");
 435        var->xres_virtual = max_frame_size / var->yres_virtual /
 436                (var->bits_per_pixel >> 3);
 437
 438        if (var->xres_virtual < OMAPFB_PLANE_XRES_MIN)
 439                var->xres_virtual = OMAPFB_PLANE_XRES_MIN;
 440
 441        if (var->xres > var->xres_virtual)
 442                var->xres = var->xres_virtual;
 443}
 444
 445static int check_vrfb_fb_size(unsigned long region_size,
 446                const struct fb_var_screeninfo *var)
 447{
 448        unsigned long min_phys_size = omap_vrfb_min_phys_size(var->xres_virtual,
 449                var->yres_virtual, var->bits_per_pixel >> 3);
 450
 451        return min_phys_size > region_size ? -EINVAL : 0;
 452}
 453
 454static int check_fb_size(const struct omapfb_info *ofbi,
 455                struct fb_var_screeninfo *var)
 456{
 457        unsigned long max_frame_size = ofbi->region->size;
 458        int bytespp = var->bits_per_pixel >> 3;
 459        unsigned long line_size = var->xres_virtual * bytespp;
 460
 461        if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB) {
 462                /* One needs to check for both VRFB and OMAPFB limitations. */
 463                if (check_vrfb_fb_size(max_frame_size, var))
 464                        shrink_height(omap_vrfb_max_height(
 465                                max_frame_size, var->xres_virtual, bytespp) *
 466                                line_size, var);
 467
 468                if (check_vrfb_fb_size(max_frame_size, var)) {
 469                        DBG("cannot fit FB to memory\n");
 470                        return -EINVAL;
 471                }
 472
 473                return 0;
 474        }
 475
 476        DBG("max frame size %lu, line size %lu\n", max_frame_size, line_size);
 477
 478        if (line_size * var->yres_virtual > max_frame_size)
 479                shrink_height(max_frame_size, var);
 480
 481        if (line_size * var->yres_virtual > max_frame_size) {
 482                shrink_width(max_frame_size, var);
 483                line_size = var->xres_virtual * bytespp;
 484        }
 485
 486        if (line_size * var->yres_virtual > max_frame_size) {
 487                DBG("cannot fit FB to memory\n");
 488                return -EINVAL;
 489        }
 490
 491        return 0;
 492}
 493
 494/*
 495 * Consider if VRFB assisted rotation is in use and if the virtual space for
 496 * the zero degree view needs to be mapped. The need for mapping also acts as
 497 * the trigger for setting up the hardware on the context in question. This
 498 * ensures that one does not attempt to access the virtual view before the
 499 * hardware is serving the address translations.
 500 */
 501static int setup_vrfb_rotation(struct fb_info *fbi)
 502{
 503        struct omapfb_info *ofbi = FB2OFB(fbi);
 504        struct omapfb2_mem_region *rg = ofbi->region;
 505        struct vrfb *vrfb = &rg->vrfb;
 506        struct fb_var_screeninfo *var = &fbi->var;
 507        struct fb_fix_screeninfo *fix = &fbi->fix;
 508        unsigned bytespp;
 509        bool yuv_mode;
 510        enum omap_color_mode mode;
 511        int r;
 512        bool reconf;
 513
 514        if (!rg->size || ofbi->rotation_type != OMAP_DSS_ROT_VRFB)
 515                return 0;
 516
 517        DBG("setup_vrfb_rotation\n");
 518
 519        r = fb_mode_to_dss_mode(var, &mode);
 520        if (r)
 521                return r;
 522
 523        bytespp = var->bits_per_pixel >> 3;
 524
 525        yuv_mode = mode == OMAP_DSS_COLOR_YUV2 || mode == OMAP_DSS_COLOR_UYVY;
 526
 527        /* We need to reconfigure VRFB if the resolution changes, if yuv mode
 528         * is enabled/disabled, or if bytes per pixel changes */
 529
 530        /* XXX we shouldn't allow this when framebuffer is mmapped */
 531
 532        reconf = false;
 533
 534        if (yuv_mode != vrfb->yuv_mode)
 535                reconf = true;
 536        else if (bytespp != vrfb->bytespp)
 537                reconf = true;
 538        else if (vrfb->xres != var->xres_virtual ||
 539                        vrfb->yres != var->yres_virtual)
 540                reconf = true;
 541
 542        if (vrfb->vaddr[0] && reconf) {
 543                fbi->screen_base = NULL;
 544                fix->smem_start = 0;
 545                fix->smem_len = 0;
 546                iounmap(vrfb->vaddr[0]);
 547                vrfb->vaddr[0] = NULL;
 548                DBG("setup_vrfb_rotation: reset fb\n");
 549        }
 550
 551        if (vrfb->vaddr[0])
 552                return 0;
 553
 554        omap_vrfb_setup(&rg->vrfb, rg->paddr,
 555                        var->xres_virtual,
 556                        var->yres_virtual,
 557                        bytespp, yuv_mode);
 558
 559        /* Now one can ioremap the 0 angle view */
 560        r = omap_vrfb_map_angle(vrfb, var->yres_virtual, 0);
 561        if (r)
 562                return r;
 563
 564        /* used by open/write in fbmem.c */
 565        fbi->screen_base = ofbi->region->vrfb.vaddr[0];
 566
 567        fix->smem_start = ofbi->region->vrfb.paddr[0];
 568
 569        switch (var->nonstd) {
 570        case OMAPFB_COLOR_YUV422:
 571        case OMAPFB_COLOR_YUY422:
 572                fix->line_length =
 573                        (OMAP_VRFB_LINE_LEN * var->bits_per_pixel) >> 2;
 574                break;
 575        default:
 576                fix->line_length =
 577                        (OMAP_VRFB_LINE_LEN * var->bits_per_pixel) >> 3;
 578                break;
 579        }
 580
 581        fix->smem_len = var->yres_virtual * fix->line_length;
 582
 583        return 0;
 584}
 585
 586int dss_mode_to_fb_mode(enum omap_color_mode dssmode,
 587                        struct fb_var_screeninfo *var)
 588{
 589        int i;
 590
 591        for (i = 0; i < ARRAY_SIZE(omapfb_colormodes); ++i) {
 592                struct omapfb_colormode *mode = &omapfb_colormodes[i];
 593                if (dssmode == mode->dssmode) {
 594                        assign_colormode_to_var(var, mode);
 595                        return 0;
 596                }
 597        }
 598        return -ENOENT;
 599}
 600
 601void set_fb_fix(struct fb_info *fbi)
 602{
 603        struct fb_fix_screeninfo *fix = &fbi->fix;
 604        struct fb_var_screeninfo *var = &fbi->var;
 605        struct omapfb_info *ofbi = FB2OFB(fbi);
 606        struct omapfb2_mem_region *rg = ofbi->region;
 607
 608        DBG("set_fb_fix\n");
 609
 610        /* used by open/write in fbmem.c */
 611        fbi->screen_base = (char __iomem *)omapfb_get_region_vaddr(ofbi);
 612
 613        /* used by mmap in fbmem.c */
 614        if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB) {
 615                switch (var->nonstd) {
 616                case OMAPFB_COLOR_YUV422:
 617                case OMAPFB_COLOR_YUY422:
 618                        fix->line_length =
 619                                (OMAP_VRFB_LINE_LEN * var->bits_per_pixel) >> 2;
 620                        break;
 621                default:
 622                        fix->line_length =
 623                                (OMAP_VRFB_LINE_LEN * var->bits_per_pixel) >> 3;
 624                        break;
 625                }
 626
 627                fix->smem_len = var->yres_virtual * fix->line_length;
 628        } else {
 629                fix->line_length =
 630                        (var->xres_virtual * var->bits_per_pixel) >> 3;
 631                fix->smem_len = rg->size;
 632        }
 633
 634        fix->smem_start = omapfb_get_region_paddr(ofbi);
 635
 636        fix->type = FB_TYPE_PACKED_PIXELS;
 637
 638        if (var->nonstd)
 639                fix->visual = FB_VISUAL_PSEUDOCOLOR;
 640        else {
 641                switch (var->bits_per_pixel) {
 642                case 32:
 643                case 24:
 644                case 16:
 645                case 12:
 646                        fix->visual = FB_VISUAL_TRUECOLOR;
 647                        /* 12bpp is stored in 16 bits */
 648                        break;
 649                case 1:
 650                case 2:
 651                case 4:
 652                case 8:
 653                        fix->visual = FB_VISUAL_PSEUDOCOLOR;
 654                        break;
 655                }
 656        }
 657
 658        fix->accel = FB_ACCEL_NONE;
 659
 660        fix->xpanstep = 1;
 661        fix->ypanstep = 1;
 662}
 663
 664/* check new var and possibly modify it to be ok */
 665int check_fb_var(struct fb_info *fbi, struct fb_var_screeninfo *var)
 666{
 667        struct omapfb_info *ofbi = FB2OFB(fbi);
 668        struct omap_dss_device *display = fb2display(fbi);
 669        enum omap_color_mode mode = 0;
 670        int i;
 671        int r;
 672
 673        DBG("check_fb_var %d\n", ofbi->id);
 674
 675        WARN_ON(!atomic_read(&ofbi->region->lock_count));
 676
 677        r = fb_mode_to_dss_mode(var, &mode);
 678        if (r) {
 679                DBG("cannot convert var to omap dss mode\n");
 680                return r;
 681        }
 682
 683        for (i = 0; i < ofbi->num_overlays; ++i) {
 684                if ((ofbi->overlays[i]->supported_modes & mode) == 0) {
 685                        DBG("invalid mode\n");
 686                        return -EINVAL;
 687                }
 688        }
 689
 690        if (var->rotate > 3)
 691                return -EINVAL;
 692
 693        if (check_fb_res_bounds(var))
 694                return -EINVAL;
 695
 696        /* When no memory is allocated ignore the size check */
 697        if (ofbi->region->size != 0 && check_fb_size(ofbi, var))
 698                return -EINVAL;
 699
 700        if (var->xres + var->xoffset > var->xres_virtual)
 701                var->xoffset = var->xres_virtual - var->xres;
 702        if (var->yres + var->yoffset > var->yres_virtual)
 703                var->yoffset = var->yres_virtual - var->yres;
 704
 705        DBG("xres = %d, yres = %d, vxres = %d, vyres = %d\n",
 706                        var->xres, var->yres,
 707                        var->xres_virtual, var->yres_virtual);
 708
 709        if (display && display->driver->get_dimensions) {
 710                u32 w, h;
 711                display->driver->get_dimensions(display, &w, &h);
 712                var->width = DIV_ROUND_CLOSEST(w, 1000);
 713                var->height = DIV_ROUND_CLOSEST(h, 1000);
 714        } else {
 715                var->height = -1;
 716                var->width = -1;
 717        }
 718
 719        var->grayscale          = 0;
 720
 721        if (display && display->driver->get_timings) {
 722                struct omap_video_timings timings;
 723                display->driver->get_timings(display, &timings);
 724
 725                /* pixclock in ps, the rest in pixclock */
 726                var->pixclock = timings.pixelclock != 0 ?
 727                        KHZ2PICOS(timings.pixelclock / 1000) :
 728                        0;
 729                var->left_margin = timings.hbp;
 730                var->right_margin = timings.hfp;
 731                var->upper_margin = timings.vbp;
 732                var->lower_margin = timings.vfp;
 733                var->hsync_len = timings.hsw;
 734                var->vsync_len = timings.vsw;
 735                var->sync |= timings.hsync_level == OMAPDSS_SIG_ACTIVE_HIGH ?
 736                                FB_SYNC_HOR_HIGH_ACT : 0;
 737                var->sync |= timings.vsync_level == OMAPDSS_SIG_ACTIVE_HIGH ?
 738                                FB_SYNC_VERT_HIGH_ACT : 0;
 739                var->vmode = timings.interlace ?
 740                                FB_VMODE_INTERLACED : FB_VMODE_NONINTERLACED;
 741        } else {
 742                var->pixclock = 0;
 743                var->left_margin = 0;
 744                var->right_margin = 0;
 745                var->upper_margin = 0;
 746                var->lower_margin = 0;
 747                var->hsync_len = 0;
 748                var->vsync_len = 0;
 749                var->sync = 0;
 750                var->vmode = FB_VMODE_NONINTERLACED;
 751        }
 752
 753        return 0;
 754}
 755
 756/*
 757 * ---------------------------------------------------------------------------
 758 * fbdev framework callbacks
 759 * ---------------------------------------------------------------------------
 760 */
 761static int omapfb_open(struct fb_info *fbi, int user)
 762{
 763        return 0;
 764}
 765
 766static int omapfb_release(struct fb_info *fbi, int user)
 767{
 768        return 0;
 769}
 770
 771static unsigned calc_rotation_offset_dma(const struct fb_var_screeninfo *var,
 772                const struct fb_fix_screeninfo *fix, int rotation)
 773{
 774        unsigned offset;
 775
 776        offset = var->yoffset * fix->line_length +
 777                var->xoffset * (var->bits_per_pixel >> 3);
 778
 779        return offset;
 780}
 781
 782static unsigned calc_rotation_offset_vrfb(const struct fb_var_screeninfo *var,
 783                const struct fb_fix_screeninfo *fix, int rotation)
 784{
 785        unsigned offset;
 786
 787        if (rotation == FB_ROTATE_UD)
 788                offset = (var->yres_virtual - var->yres) *
 789                        fix->line_length;
 790        else if (rotation == FB_ROTATE_CW)
 791                offset = (var->yres_virtual - var->yres) *
 792                        (var->bits_per_pixel >> 3);
 793        else
 794                offset = 0;
 795
 796        if (rotation == FB_ROTATE_UR)
 797                offset += var->yoffset * fix->line_length +
 798                        var->xoffset * (var->bits_per_pixel >> 3);
 799        else if (rotation == FB_ROTATE_UD)
 800                offset -= var->yoffset * fix->line_length +
 801                        var->xoffset * (var->bits_per_pixel >> 3);
 802        else if (rotation == FB_ROTATE_CW)
 803                offset -= var->xoffset * fix->line_length +
 804                        var->yoffset * (var->bits_per_pixel >> 3);
 805        else if (rotation == FB_ROTATE_CCW)
 806                offset += var->xoffset * fix->line_length +
 807                        var->yoffset * (var->bits_per_pixel >> 3);
 808
 809        return offset;
 810}
 811
 812static void omapfb_calc_addr(const struct omapfb_info *ofbi,
 813                             const struct fb_var_screeninfo *var,
 814                             const struct fb_fix_screeninfo *fix,
 815                             int rotation, u32 *paddr)
 816{
 817        u32 data_start_p;
 818        int offset;
 819
 820        if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB)
 821                data_start_p = omapfb_get_region_rot_paddr(ofbi, rotation);
 822        else
 823                data_start_p = omapfb_get_region_paddr(ofbi);
 824
 825        if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB)
 826                offset = calc_rotation_offset_vrfb(var, fix, rotation);
 827        else
 828                offset = calc_rotation_offset_dma(var, fix, rotation);
 829
 830        data_start_p += offset;
 831
 832        if (offset)
 833                DBG("offset %d, %d = %d\n",
 834                    var->xoffset, var->yoffset, offset);
 835
 836        DBG("paddr %x\n", data_start_p);
 837
 838        *paddr = data_start_p;
 839}
 840
 841/* setup overlay according to the fb */
 842int omapfb_setup_overlay(struct fb_info *fbi, struct omap_overlay *ovl,
 843                u16 posx, u16 posy, u16 outw, u16 outh)
 844{
 845        int r = 0;
 846        struct omapfb_info *ofbi = FB2OFB(fbi);
 847        struct fb_var_screeninfo *var = &fbi->var;
 848        struct fb_fix_screeninfo *fix = &fbi->fix;
 849        enum omap_color_mode mode = 0;
 850        u32 data_start_p = 0;
 851        struct omap_overlay_info info;
 852        int xres, yres;
 853        int screen_width;
 854        int mirror;
 855        int rotation = var->rotate;
 856        int i;
 857
 858        WARN_ON(!atomic_read(&ofbi->region->lock_count));
 859
 860        for (i = 0; i < ofbi->num_overlays; i++) {
 861                if (ovl != ofbi->overlays[i])
 862                        continue;
 863
 864                rotation = (rotation + ofbi->rotation[i]) % 4;
 865                break;
 866        }
 867
 868        DBG("setup_overlay %d, posx %d, posy %d, outw %d, outh %d\n", ofbi->id,
 869                        posx, posy, outw, outh);
 870
 871        if (rotation == FB_ROTATE_CW || rotation == FB_ROTATE_CCW) {
 872                xres = var->yres;
 873                yres = var->xres;
 874        } else {
 875                xres = var->xres;
 876                yres = var->yres;
 877        }
 878
 879        if (ofbi->region->size)
 880                omapfb_calc_addr(ofbi, var, fix, rotation, &data_start_p);
 881
 882        r = fb_mode_to_dss_mode(var, &mode);
 883        if (r) {
 884                DBG("fb_mode_to_dss_mode failed");
 885                goto err;
 886        }
 887
 888        switch (var->nonstd) {
 889        case OMAPFB_COLOR_YUV422:
 890        case OMAPFB_COLOR_YUY422:
 891                if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB) {
 892                        screen_width = fix->line_length
 893                                / (var->bits_per_pixel >> 2);
 894                        break;
 895                }
 896                /* fall through */
 897        default:
 898                screen_width = fix->line_length / (var->bits_per_pixel >> 3);
 899                break;
 900        }
 901
 902        ovl->get_overlay_info(ovl, &info);
 903
 904        if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB)
 905                mirror = 0;
 906        else
 907                mirror = ofbi->mirror;
 908
 909        info.paddr = data_start_p;
 910        info.screen_width = screen_width;
 911        info.width = xres;
 912        info.height = yres;
 913        info.color_mode = mode;
 914        info.rotation_type = ofbi->rotation_type;
 915        info.rotation = rotation;
 916        info.mirror = mirror;
 917
 918        info.pos_x = posx;
 919        info.pos_y = posy;
 920        info.out_width = outw;
 921        info.out_height = outh;
 922
 923        r = ovl->set_overlay_info(ovl, &info);
 924        if (r) {
 925                DBG("ovl->setup_overlay_info failed\n");
 926                goto err;
 927        }
 928
 929        return 0;
 930
 931err:
 932        DBG("setup_overlay failed\n");
 933        return r;
 934}
 935
 936/* apply var to the overlay */
 937int omapfb_apply_changes(struct fb_info *fbi, int init)
 938{
 939        int r = 0;
 940        struct omapfb_info *ofbi = FB2OFB(fbi);
 941        struct fb_var_screeninfo *var = &fbi->var;
 942        struct omap_overlay *ovl;
 943        u16 posx, posy;
 944        u16 outw, outh;
 945        int i;
 946
 947#ifdef DEBUG
 948        if (omapfb_test_pattern)
 949                fill_fb(fbi);
 950#endif
 951
 952        WARN_ON(!atomic_read(&ofbi->region->lock_count));
 953
 954        for (i = 0; i < ofbi->num_overlays; i++) {
 955                ovl = ofbi->overlays[i];
 956
 957                DBG("apply_changes, fb %d, ovl %d\n", ofbi->id, ovl->id);
 958
 959                if (ofbi->region->size == 0) {
 960                        /* the fb is not available. disable the overlay */
 961                        omapfb_overlay_enable(ovl, 0);
 962                        if (!init && ovl->manager)
 963                                ovl->manager->apply(ovl->manager);
 964                        continue;
 965                }
 966
 967                if (init || (ovl->caps & OMAP_DSS_OVL_CAP_SCALE) == 0) {
 968                        int rotation = (var->rotate + ofbi->rotation[i]) % 4;
 969                        if (rotation == FB_ROTATE_CW ||
 970                                        rotation == FB_ROTATE_CCW) {
 971                                outw = var->yres;
 972                                outh = var->xres;
 973                        } else {
 974                                outw = var->xres;
 975                                outh = var->yres;
 976                        }
 977                } else {
 978                        struct omap_overlay_info info;
 979                        ovl->get_overlay_info(ovl, &info);
 980                        outw = info.out_width;
 981                        outh = info.out_height;
 982                }
 983
 984                if (init) {
 985                        posx = 0;
 986                        posy = 0;
 987                } else {
 988                        struct omap_overlay_info info;
 989                        ovl->get_overlay_info(ovl, &info);
 990                        posx = info.pos_x;
 991                        posy = info.pos_y;
 992                }
 993
 994                r = omapfb_setup_overlay(fbi, ovl, posx, posy, outw, outh);
 995                if (r)
 996                        goto err;
 997
 998                if (!init && ovl->manager)
 999                        ovl->manager->apply(ovl->manager);
1000        }
1001        return 0;
1002err:
1003        DBG("apply_changes failed\n");
1004        return r;
1005}
1006
1007/* checks var and eventually tweaks it to something supported,
1008 * DO NOT MODIFY PAR */
1009static int omapfb_check_var(struct fb_var_screeninfo *var, struct fb_info *fbi)
1010{
1011        struct omapfb_info *ofbi = FB2OFB(fbi);
1012        int r;
1013
1014        DBG("check_var(%d)\n", FB2OFB(fbi)->id);
1015
1016        omapfb_get_mem_region(ofbi->region);
1017
1018        r = check_fb_var(fbi, var);
1019
1020        omapfb_put_mem_region(ofbi->region);
1021
1022        return r;
1023}
1024
1025/* set the video mode according to info->var */
1026static int omapfb_set_par(struct fb_info *fbi)
1027{
1028        struct omapfb_info *ofbi = FB2OFB(fbi);
1029        int r;
1030
1031        DBG("set_par(%d)\n", FB2OFB(fbi)->id);
1032
1033        omapfb_get_mem_region(ofbi->region);
1034
1035        set_fb_fix(fbi);
1036
1037        r = setup_vrfb_rotation(fbi);
1038        if (r)
1039                goto out;
1040
1041        r = omapfb_apply_changes(fbi, 0);
1042
1043 out:
1044        omapfb_put_mem_region(ofbi->region);
1045
1046        return r;
1047}
1048
1049static int omapfb_pan_display(struct fb_var_screeninfo *var,
1050                struct fb_info *fbi)
1051{
1052        struct omapfb_info *ofbi = FB2OFB(fbi);
1053        struct fb_var_screeninfo new_var;
1054        int r;
1055
1056        DBG("pan_display(%d)\n", FB2OFB(fbi)->id);
1057
1058        if (var->xoffset == fbi->var.xoffset &&
1059            var->yoffset == fbi->var.yoffset)
1060                return 0;
1061
1062        new_var = fbi->var;
1063        new_var.xoffset = var->xoffset;
1064        new_var.yoffset = var->yoffset;
1065
1066        fbi->var = new_var;
1067
1068        omapfb_get_mem_region(ofbi->region);
1069
1070        r = omapfb_apply_changes(fbi, 0);
1071
1072        omapfb_put_mem_region(ofbi->region);
1073
1074        return r;
1075}
1076
1077static void mmap_user_open(struct vm_area_struct *vma)
1078{
1079        struct omapfb2_mem_region *rg = vma->vm_private_data;
1080
1081        omapfb_get_mem_region(rg);
1082        atomic_inc(&rg->map_count);
1083        omapfb_put_mem_region(rg);
1084}
1085
1086static void mmap_user_close(struct vm_area_struct *vma)
1087{
1088        struct omapfb2_mem_region *rg = vma->vm_private_data;
1089
1090        omapfb_get_mem_region(rg);
1091        atomic_dec(&rg->map_count);
1092        omapfb_put_mem_region(rg);
1093}
1094
1095static const struct vm_operations_struct mmap_user_ops = {
1096        .open = mmap_user_open,
1097        .close = mmap_user_close,
1098};
1099
1100static int omapfb_mmap(struct fb_info *fbi, struct vm_area_struct *vma)
1101{
1102        struct omapfb_info *ofbi = FB2OFB(fbi);
1103        struct fb_fix_screeninfo *fix = &fbi->fix;
1104        struct omapfb2_mem_region *rg;
1105        unsigned long start;
1106        u32 len;
1107        int r;
1108
1109        rg = omapfb_get_mem_region(ofbi->region);
1110
1111        start = omapfb_get_region_paddr(ofbi);
1112        len = fix->smem_len;
1113
1114        DBG("user mmap region start %lx, len %d, off %lx\n", start, len,
1115                        vma->vm_pgoff << PAGE_SHIFT);
1116
1117        vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
1118        vma->vm_ops = &mmap_user_ops;
1119        vma->vm_private_data = rg;
1120
1121        r = vm_iomap_memory(vma, start, len);
1122        if (r)
1123                goto error;
1124
1125        /* vm_ops.open won't be called for mmap itself. */
1126        atomic_inc(&rg->map_count);
1127
1128        omapfb_put_mem_region(rg);
1129
1130        return 0;
1131
1132error:
1133        omapfb_put_mem_region(ofbi->region);
1134
1135        return r;
1136}
1137
1138/* Store a single color palette entry into a pseudo palette or the hardware
1139 * palette if one is available. For now we support only 16bpp and thus store
1140 * the entry only to the pseudo palette.
1141 */
1142static int _setcolreg(struct fb_info *fbi, u_int regno, u_int red, u_int green,
1143                u_int blue, u_int transp, int update_hw_pal)
1144{
1145        /*struct omapfb_info *ofbi = FB2OFB(fbi);*/
1146        /*struct omapfb2_device *fbdev = ofbi->fbdev;*/
1147        struct fb_var_screeninfo *var = &fbi->var;
1148        int r = 0;
1149
1150        enum omapfb_color_format mode = OMAPFB_COLOR_RGB24U; /* XXX */
1151
1152        /*switch (plane->color_mode) {*/
1153        switch (mode) {
1154        case OMAPFB_COLOR_YUV422:
1155        case OMAPFB_COLOR_YUV420:
1156        case OMAPFB_COLOR_YUY422:
1157                r = -EINVAL;
1158                break;
1159        case OMAPFB_COLOR_CLUT_8BPP:
1160        case OMAPFB_COLOR_CLUT_4BPP:
1161        case OMAPFB_COLOR_CLUT_2BPP:
1162        case OMAPFB_COLOR_CLUT_1BPP:
1163                /*
1164                   if (fbdev->ctrl->setcolreg)
1165                   r = fbdev->ctrl->setcolreg(regno, red, green, blue,
1166                   transp, update_hw_pal);
1167                   */
1168                /* Fallthrough */
1169                r = -EINVAL;
1170                break;
1171        case OMAPFB_COLOR_RGB565:
1172        case OMAPFB_COLOR_RGB444:
1173        case OMAPFB_COLOR_RGB24P:
1174        case OMAPFB_COLOR_RGB24U:
1175                if (r != 0)
1176                        break;
1177
1178                if (regno < 16) {
1179                        u32 pal;
1180                        pal = ((red >> (16 - var->red.length)) <<
1181                                        var->red.offset) |
1182                                ((green >> (16 - var->green.length)) <<
1183                                 var->green.offset) |
1184                                (blue >> (16 - var->blue.length));
1185                        ((u32 *)(fbi->pseudo_palette))[regno] = pal;
1186                }
1187                break;
1188        default:
1189                BUG();
1190        }
1191        return r;
1192}
1193
1194static int omapfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
1195                u_int transp, struct fb_info *info)
1196{
1197        DBG("setcolreg\n");
1198
1199        return _setcolreg(info, regno, red, green, blue, transp, 1);
1200}
1201
1202static int omapfb_setcmap(struct fb_cmap *cmap, struct fb_info *info)
1203{
1204        int count, index, r;
1205        u16 *red, *green, *blue, *transp;
1206        u16 trans = 0xffff;
1207
1208        DBG("setcmap\n");
1209
1210        red     = cmap->red;
1211        green   = cmap->green;
1212        blue    = cmap->blue;
1213        transp  = cmap->transp;
1214        index   = cmap->start;
1215
1216        for (count = 0; count < cmap->len; count++) {
1217                if (transp)
1218                        trans = *transp++;
1219                r = _setcolreg(info, index++, *red++, *green++, *blue++, trans,
1220                                count == cmap->len - 1);
1221                if (r != 0)
1222                        return r;
1223        }
1224
1225        return 0;
1226}
1227
1228static int omapfb_blank(int blank, struct fb_info *fbi)
1229{
1230        struct omapfb_info *ofbi = FB2OFB(fbi);
1231        struct omapfb2_device *fbdev = ofbi->fbdev;
1232        struct omap_dss_device *display = fb2display(fbi);
1233        struct omapfb_display_data *d;
1234        int r = 0;
1235
1236        if (!display)
1237                return -EINVAL;
1238
1239        omapfb_lock(fbdev);
1240
1241        d = get_display_data(fbdev, display);
1242
1243        switch (blank) {
1244        case FB_BLANK_UNBLANK:
1245                if (display->state == OMAP_DSS_DISPLAY_ACTIVE)
1246                        goto exit;
1247
1248                r = display->driver->enable(display);
1249
1250                if ((display->caps & OMAP_DSS_DISPLAY_CAP_MANUAL_UPDATE) &&
1251                                d->update_mode == OMAPFB_AUTO_UPDATE &&
1252                                !d->auto_update_work_enabled)
1253                        omapfb_start_auto_update(fbdev, display);
1254
1255                break;
1256
1257        case FB_BLANK_NORMAL:
1258                /* FB_BLANK_NORMAL could be implemented.
1259                 * Needs DSS additions. */
1260        case FB_BLANK_VSYNC_SUSPEND:
1261        case FB_BLANK_HSYNC_SUSPEND:
1262        case FB_BLANK_POWERDOWN:
1263                if (display->state != OMAP_DSS_DISPLAY_ACTIVE)
1264                        goto exit;
1265
1266                if (d->auto_update_work_enabled)
1267                        omapfb_stop_auto_update(fbdev, display);
1268
1269                display->driver->disable(display);
1270
1271                break;
1272
1273        default:
1274                r = -EINVAL;
1275        }
1276
1277exit:
1278        omapfb_unlock(fbdev);
1279
1280        return r;
1281}
1282
1283#if 0
1284/* XXX fb_read and fb_write are needed for VRFB */
1285ssize_t omapfb_write(struct fb_info *info, const char __user *buf,
1286                size_t count, loff_t *ppos)
1287{
1288        DBG("omapfb_write %d, %lu\n", count, (unsigned long)*ppos);
1289        /* XXX needed for VRFB */
1290        return count;
1291}
1292#endif
1293
1294static struct fb_ops omapfb_ops = {
1295        .owner          = THIS_MODULE,
1296        .fb_open        = omapfb_open,
1297        .fb_release     = omapfb_release,
1298        .fb_fillrect    = cfb_fillrect,
1299        .fb_copyarea    = cfb_copyarea,
1300        .fb_imageblit   = cfb_imageblit,
1301        .fb_blank       = omapfb_blank,
1302        .fb_ioctl       = omapfb_ioctl,
1303        .fb_check_var   = omapfb_check_var,
1304        .fb_set_par     = omapfb_set_par,
1305        .fb_pan_display = omapfb_pan_display,
1306        .fb_mmap        = omapfb_mmap,
1307        .fb_setcolreg   = omapfb_setcolreg,
1308        .fb_setcmap     = omapfb_setcmap,
1309        /*.fb_write     = omapfb_write,*/
1310};
1311
1312static void omapfb_free_fbmem(struct fb_info *fbi)
1313{
1314        struct omapfb_info *ofbi = FB2OFB(fbi);
1315        struct omapfb2_device *fbdev = ofbi->fbdev;
1316        struct omapfb2_mem_region *rg;
1317
1318        rg = ofbi->region;
1319
1320        if (rg->token == NULL)
1321                return;
1322
1323        WARN_ON(atomic_read(&rg->map_count));
1324
1325        if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB) {
1326                /* unmap the 0 angle rotation */
1327                if (rg->vrfb.vaddr[0]) {
1328                        iounmap(rg->vrfb.vaddr[0]);
1329                        rg->vrfb.vaddr[0] = NULL;
1330                }
1331
1332                omap_vrfb_release_ctx(&rg->vrfb);
1333        }
1334
1335        dma_free_attrs(fbdev->dev, rg->size, rg->token, rg->dma_handle,
1336                        rg->attrs);
1337
1338        rg->token = NULL;
1339        rg->vaddr = NULL;
1340        rg->paddr = 0;
1341        rg->alloc = 0;
1342        rg->size = 0;
1343}
1344
1345static void clear_fb_info(struct fb_info *fbi)
1346{
1347        memset(&fbi->var, 0, sizeof(fbi->var));
1348        memset(&fbi->fix, 0, sizeof(fbi->fix));
1349        strlcpy(fbi->fix.id, MODULE_NAME, sizeof(fbi->fix.id));
1350}
1351
1352static int omapfb_free_all_fbmem(struct omapfb2_device *fbdev)
1353{
1354        int i;
1355
1356        DBG("free all fbmem\n");
1357
1358        for (i = 0; i < fbdev->num_fbs; i++) {
1359                struct fb_info *fbi = fbdev->fbs[i];
1360                omapfb_free_fbmem(fbi);
1361                clear_fb_info(fbi);
1362        }
1363
1364        return 0;
1365}
1366
1367static int omapfb_alloc_fbmem(struct fb_info *fbi, unsigned long size,
1368                unsigned long paddr)
1369{
1370        struct omapfb_info *ofbi = FB2OFB(fbi);
1371        struct omapfb2_device *fbdev = ofbi->fbdev;
1372        struct omapfb2_mem_region *rg;
1373        void *token;
1374        unsigned long attrs;
1375        dma_addr_t dma_handle;
1376        int r;
1377
1378        rg = ofbi->region;
1379
1380        rg->paddr = 0;
1381        rg->vaddr = NULL;
1382        memset(&rg->vrfb, 0, sizeof rg->vrfb);
1383        rg->size = 0;
1384        rg->type = 0;
1385        rg->alloc = false;
1386        rg->map = false;
1387
1388        size = PAGE_ALIGN(size);
1389
1390        attrs = DMA_ATTR_WRITE_COMBINE;
1391
1392        if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB)
1393                attrs |= DMA_ATTR_NO_KERNEL_MAPPING;
1394
1395        DBG("allocating %lu bytes for fb %d\n", size, ofbi->id);
1396
1397        token = dma_alloc_attrs(fbdev->dev, size, &dma_handle,
1398                        GFP_KERNEL, attrs);
1399
1400        if (token == NULL) {
1401                dev_err(fbdev->dev, "failed to allocate framebuffer\n");
1402                return -ENOMEM;
1403        }
1404
1405        DBG("allocated VRAM paddr %lx, vaddr %p\n",
1406                        (unsigned long)dma_handle, token);
1407
1408        if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB) {
1409                r = omap_vrfb_request_ctx(&rg->vrfb);
1410                if (r) {
1411                        dma_free_attrs(fbdev->dev, size, token, dma_handle,
1412                                        attrs);
1413                        dev_err(fbdev->dev, "vrfb create ctx failed\n");
1414                        return r;
1415                }
1416        }
1417
1418        rg->attrs = attrs;
1419        rg->token = token;
1420        rg->dma_handle = dma_handle;
1421
1422        rg->paddr = (unsigned long)dma_handle;
1423        rg->vaddr = (void __iomem *)token;
1424        rg->size = size;
1425        rg->alloc = 1;
1426
1427        return 0;
1428}
1429
1430/* allocate fbmem using display resolution as reference */
1431static int omapfb_alloc_fbmem_display(struct fb_info *fbi, unsigned long size,
1432                unsigned long paddr)
1433{
1434        struct omapfb_info *ofbi = FB2OFB(fbi);
1435        struct omapfb2_device *fbdev = ofbi->fbdev;
1436        struct omap_dss_device *display;
1437        int bytespp;
1438
1439        display =  fb2display(fbi);
1440
1441        if (!display)
1442                return 0;
1443
1444        switch (omapfb_get_recommended_bpp(fbdev, display)) {
1445        case 16:
1446                bytespp = 2;
1447                break;
1448        case 24:
1449                bytespp = 4;
1450                break;
1451        default:
1452                bytespp = 4;
1453                break;
1454        }
1455
1456        if (!size) {
1457                u16 w, h;
1458
1459                display->driver->get_resolution(display, &w, &h);
1460
1461                if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB) {
1462                        size = max(omap_vrfb_min_phys_size(w, h, bytespp),
1463                                        omap_vrfb_min_phys_size(h, w, bytespp));
1464
1465                        DBG("adjusting fb mem size for VRFB, %u -> %lu\n",
1466                                        w * h * bytespp, size);
1467                } else {
1468                        size = w * h * bytespp;
1469                }
1470        }
1471
1472        if (!size)
1473                return 0;
1474
1475        return omapfb_alloc_fbmem(fbi, size, paddr);
1476}
1477
1478static int omapfb_parse_vram_param(const char *param, int max_entries,
1479                unsigned long *sizes, unsigned long *paddrs)
1480{
1481        unsigned int fbnum;
1482        unsigned long size;
1483        unsigned long paddr = 0;
1484        char *p, *start;
1485
1486        start = (char *)param;
1487
1488        while (1) {
1489                p = start;
1490
1491                fbnum = simple_strtoul(p, &p, 10);
1492
1493                if (p == start)
1494                        return -EINVAL;
1495
1496                if (*p != ':')
1497                        return -EINVAL;
1498
1499                if (fbnum >= max_entries)
1500                        return -EINVAL;
1501
1502                size = memparse(p + 1, &p);
1503
1504                if (!size)
1505                        return -EINVAL;
1506
1507                paddr = 0;
1508
1509                if (*p == '@') {
1510                        paddr = simple_strtoul(p + 1, &p, 16);
1511
1512                        if (!paddr)
1513                                return -EINVAL;
1514
1515                }
1516
1517                WARN_ONCE(paddr,
1518                        "reserving memory at predefined address not supported\n");
1519
1520                paddrs[fbnum] = paddr;
1521                sizes[fbnum] = size;
1522
1523                if (*p == 0)
1524                        break;
1525
1526                if (*p != ',')
1527                        return -EINVAL;
1528
1529                ++p;
1530
1531                start = p;
1532        }
1533
1534        return 0;
1535}
1536
1537static int omapfb_allocate_all_fbs(struct omapfb2_device *fbdev)
1538{
1539        int i, r;
1540        unsigned long vram_sizes[10];
1541        unsigned long vram_paddrs[10];
1542
1543        memset(&vram_sizes, 0, sizeof(vram_sizes));
1544        memset(&vram_paddrs, 0, sizeof(vram_paddrs));
1545
1546        if (def_vram && omapfb_parse_vram_param(def_vram, 10,
1547                                vram_sizes, vram_paddrs)) {
1548                dev_err(fbdev->dev, "failed to parse vram parameter\n");
1549
1550                memset(&vram_sizes, 0, sizeof(vram_sizes));
1551                memset(&vram_paddrs, 0, sizeof(vram_paddrs));
1552        }
1553
1554        for (i = 0; i < fbdev->num_fbs; i++) {
1555                /* allocate memory automatically only for fb0, or if
1556                 * excplicitly defined with vram or plat data option */
1557                if (i == 0 || vram_sizes[i] != 0) {
1558                        r = omapfb_alloc_fbmem_display(fbdev->fbs[i],
1559                                        vram_sizes[i], vram_paddrs[i]);
1560
1561                        if (r)
1562                                return r;
1563                }
1564        }
1565
1566        for (i = 0; i < fbdev->num_fbs; i++) {
1567                struct omapfb_info *ofbi = FB2OFB(fbdev->fbs[i]);
1568                struct omapfb2_mem_region *rg;
1569                rg = ofbi->region;
1570
1571                DBG("region%d phys %08x virt %p size=%lu\n",
1572                                i,
1573                                rg->paddr,
1574                                rg->vaddr,
1575                                rg->size);
1576        }
1577
1578        return 0;
1579}
1580
1581static void omapfb_clear_fb(struct fb_info *fbi)
1582{
1583        const struct fb_fillrect rect = {
1584                .dx = 0,
1585                .dy = 0,
1586                .width = fbi->var.xres_virtual,
1587                .height = fbi->var.yres_virtual,
1588                .color = 0,
1589                .rop = ROP_COPY,
1590        };
1591
1592        cfb_fillrect(fbi, &rect);
1593}
1594
1595int omapfb_realloc_fbmem(struct fb_info *fbi, unsigned long size, int type)
1596{
1597        struct omapfb_info *ofbi = FB2OFB(fbi);
1598        struct omapfb2_device *fbdev = ofbi->fbdev;
1599        struct omapfb2_mem_region *rg = ofbi->region;
1600        unsigned long old_size = rg->size;
1601        unsigned long old_paddr = rg->paddr;
1602        int old_type = rg->type;
1603        int r;
1604
1605        if (type != OMAPFB_MEMTYPE_SDRAM)
1606                return -EINVAL;
1607
1608        size = PAGE_ALIGN(size);
1609
1610        if (old_size == size && old_type == type)
1611                return 0;
1612
1613        omapfb_free_fbmem(fbi);
1614
1615        if (size == 0) {
1616                clear_fb_info(fbi);
1617                return 0;
1618        }
1619
1620        r = omapfb_alloc_fbmem(fbi, size, 0);
1621
1622        if (r) {
1623                if (old_size)
1624                        omapfb_alloc_fbmem(fbi, old_size, old_paddr);
1625
1626                if (rg->size == 0)
1627                        clear_fb_info(fbi);
1628
1629                return r;
1630        }
1631
1632        if (old_size == size)
1633                return 0;
1634
1635        if (old_size == 0) {
1636                DBG("initializing fb %d\n", ofbi->id);
1637                r = omapfb_fb_init(fbdev, fbi);
1638                if (r) {
1639                        DBG("omapfb_fb_init failed\n");
1640                        goto err;
1641                }
1642                r = omapfb_apply_changes(fbi, 1);
1643                if (r) {
1644                        DBG("omapfb_apply_changes failed\n");
1645                        goto err;
1646                }
1647        } else {
1648                struct fb_var_screeninfo new_var;
1649                memcpy(&new_var, &fbi->var, sizeof(new_var));
1650                r = check_fb_var(fbi, &new_var);
1651                if (r)
1652                        goto err;
1653                memcpy(&fbi->var, &new_var, sizeof(fbi->var));
1654                set_fb_fix(fbi);
1655                r = setup_vrfb_rotation(fbi);
1656                if (r)
1657                        goto err;
1658        }
1659
1660        omapfb_clear_fb(fbi);
1661
1662        return 0;
1663err:
1664        omapfb_free_fbmem(fbi);
1665        clear_fb_info(fbi);
1666        return r;
1667}
1668
1669static void omapfb_auto_update_work(struct work_struct *work)
1670{
1671        struct omap_dss_device *dssdev;
1672        struct omap_dss_driver *dssdrv;
1673        struct omapfb_display_data *d;
1674        u16 w, h;
1675        unsigned int freq;
1676        struct omapfb2_device *fbdev;
1677
1678        d = container_of(work, struct omapfb_display_data,
1679                        auto_update_work.work);
1680
1681        dssdev = d->dssdev;
1682        dssdrv = dssdev->driver;
1683        fbdev = d->fbdev;
1684
1685        if (!dssdrv || !dssdrv->update)
1686                return;
1687
1688        if (dssdrv->sync)
1689                dssdrv->sync(dssdev);
1690
1691        dssdrv->get_resolution(dssdev, &w, &h);
1692        dssdrv->update(dssdev, 0, 0, w, h);
1693
1694        freq = auto_update_freq;
1695        if (freq == 0)
1696                freq = 20;
1697        queue_delayed_work(fbdev->auto_update_wq,
1698                        &d->auto_update_work, HZ / freq);
1699}
1700
1701void omapfb_start_auto_update(struct omapfb2_device *fbdev,
1702                struct omap_dss_device *display)
1703{
1704        struct omapfb_display_data *d;
1705
1706        if (fbdev->auto_update_wq == NULL) {
1707                struct workqueue_struct *wq;
1708
1709                wq = create_singlethread_workqueue("omapfb_auto_update");
1710
1711                if (wq == NULL) {
1712                        dev_err(fbdev->dev, "Failed to create workqueue for "
1713                                        "auto-update\n");
1714                        return;
1715                }
1716
1717                fbdev->auto_update_wq = wq;
1718        }
1719
1720        d = get_display_data(fbdev, display);
1721
1722        INIT_DELAYED_WORK(&d->auto_update_work, omapfb_auto_update_work);
1723
1724        d->auto_update_work_enabled = true;
1725
1726        omapfb_auto_update_work(&d->auto_update_work.work);
1727}
1728
1729void omapfb_stop_auto_update(struct omapfb2_device *fbdev,
1730                struct omap_dss_device *display)
1731{
1732        struct omapfb_display_data *d;
1733
1734        d = get_display_data(fbdev, display);
1735
1736        cancel_delayed_work_sync(&d->auto_update_work);
1737
1738        d->auto_update_work_enabled = false;
1739}
1740
1741/* initialize fb_info, var, fix to something sane based on the display */
1742static int omapfb_fb_init(struct omapfb2_device *fbdev, struct fb_info *fbi)
1743{
1744        struct fb_var_screeninfo *var = &fbi->var;
1745        struct omap_dss_device *display = fb2display(fbi);
1746        struct omapfb_info *ofbi = FB2OFB(fbi);
1747        int r = 0;
1748
1749        fbi->fbops = &omapfb_ops;
1750        fbi->flags = FBINFO_FLAG_DEFAULT;
1751        fbi->pseudo_palette = fbdev->pseudo_palette;
1752
1753        if (ofbi->region->size == 0) {
1754                clear_fb_info(fbi);
1755                return 0;
1756        }
1757
1758        var->nonstd = 0;
1759        var->bits_per_pixel = 0;
1760
1761        var->rotate = def_rotate;
1762
1763        if (display) {
1764                u16 w, h;
1765                int rotation = (var->rotate + ofbi->rotation[0]) % 4;
1766
1767                display->driver->get_resolution(display, &w, &h);
1768
1769                if (rotation == FB_ROTATE_CW ||
1770                                rotation == FB_ROTATE_CCW) {
1771                        var->xres = h;
1772                        var->yres = w;
1773                } else {
1774                        var->xres = w;
1775                        var->yres = h;
1776                }
1777
1778                var->xres_virtual = var->xres;
1779                var->yres_virtual = var->yres;
1780
1781                if (!var->bits_per_pixel) {
1782                        switch (omapfb_get_recommended_bpp(fbdev, display)) {
1783                        case 16:
1784                                var->bits_per_pixel = 16;
1785                                break;
1786                        case 24:
1787                                var->bits_per_pixel = 32;
1788                                break;
1789                        default:
1790                                dev_err(fbdev->dev, "illegal display "
1791                                                "bpp\n");
1792                                return -EINVAL;
1793                        }
1794                }
1795        } else {
1796                /* if there's no display, let's just guess some basic values */
1797                var->xres = 320;
1798                var->yres = 240;
1799                var->xres_virtual = var->xres;
1800                var->yres_virtual = var->yres;
1801                if (!var->bits_per_pixel)
1802                        var->bits_per_pixel = 16;
1803        }
1804
1805        r = check_fb_var(fbi, var);
1806        if (r)
1807                goto err;
1808
1809        set_fb_fix(fbi);
1810        r = setup_vrfb_rotation(fbi);
1811        if (r)
1812                goto err;
1813
1814        r = fb_alloc_cmap(&fbi->cmap, 256, 0);
1815        if (r)
1816                dev_err(fbdev->dev, "unable to allocate color map memory\n");
1817
1818err:
1819        return r;
1820}
1821
1822static void fbinfo_cleanup(struct omapfb2_device *fbdev, struct fb_info *fbi)
1823{
1824        fb_dealloc_cmap(&fbi->cmap);
1825}
1826
1827
1828static void omapfb_free_resources(struct omapfb2_device *fbdev)
1829{
1830        int i;
1831
1832        DBG("free_resources\n");
1833
1834        if (fbdev == NULL)
1835                return;
1836
1837        for (i = 0; i < fbdev->num_overlays; i++) {
1838                struct omap_overlay *ovl = fbdev->overlays[i];
1839
1840                ovl->disable(ovl);
1841
1842                if (ovl->manager)
1843                        ovl->unset_manager(ovl);
1844        }
1845
1846        for (i = 0; i < fbdev->num_fbs; i++)
1847                unregister_framebuffer(fbdev->fbs[i]);
1848
1849        /* free the reserved fbmem */
1850        omapfb_free_all_fbmem(fbdev);
1851
1852        for (i = 0; i < fbdev->num_fbs; i++) {
1853                fbinfo_cleanup(fbdev, fbdev->fbs[i]);
1854                framebuffer_release(fbdev->fbs[i]);
1855        }
1856
1857        for (i = 0; i < fbdev->num_displays; i++) {
1858                struct omap_dss_device *dssdev = fbdev->displays[i].dssdev;
1859
1860                if (fbdev->displays[i].auto_update_work_enabled)
1861                        omapfb_stop_auto_update(fbdev, dssdev);
1862
1863                if (dssdev->state != OMAP_DSS_DISPLAY_DISABLED)
1864                        dssdev->driver->disable(dssdev);
1865
1866                dssdev->driver->disconnect(dssdev);
1867
1868                omap_dss_put_device(dssdev);
1869        }
1870
1871        if (fbdev->auto_update_wq != NULL) {
1872                flush_workqueue(fbdev->auto_update_wq);
1873                destroy_workqueue(fbdev->auto_update_wq);
1874                fbdev->auto_update_wq = NULL;
1875        }
1876
1877        dev_set_drvdata(fbdev->dev, NULL);
1878}
1879
1880static int omapfb_create_framebuffers(struct omapfb2_device *fbdev)
1881{
1882        int r, i;
1883
1884        fbdev->num_fbs = 0;
1885
1886        DBG("create %d framebuffers\n", CONFIG_FB_OMAP2_NUM_FBS);
1887
1888        /* allocate fb_infos */
1889        for (i = 0; i < CONFIG_FB_OMAP2_NUM_FBS; i++) {
1890                struct fb_info *fbi;
1891                struct omapfb_info *ofbi;
1892
1893                fbi = framebuffer_alloc(sizeof(struct omapfb_info),
1894                                fbdev->dev);
1895
1896                if (fbi == NULL) {
1897                        dev_err(fbdev->dev,
1898                                "unable to allocate memory for plane info\n");
1899                        return -ENOMEM;
1900                }
1901
1902                clear_fb_info(fbi);
1903
1904                fbdev->fbs[i] = fbi;
1905
1906                ofbi = FB2OFB(fbi);
1907                ofbi->fbdev = fbdev;
1908                ofbi->id = i;
1909
1910                ofbi->region = &fbdev->regions[i];
1911                ofbi->region->id = i;
1912                init_rwsem(&ofbi->region->lock);
1913
1914                /* assign these early, so that fb alloc can use them */
1915                ofbi->rotation_type = def_vrfb ? OMAP_DSS_ROT_VRFB :
1916                        OMAP_DSS_ROT_DMA;
1917                ofbi->mirror = def_mirror;
1918
1919                fbdev->num_fbs++;
1920        }
1921
1922        DBG("fb_infos allocated\n");
1923
1924        /* assign overlays for the fbs */
1925        for (i = 0; i < min(fbdev->num_fbs, fbdev->num_overlays); i++) {
1926                struct omapfb_info *ofbi = FB2OFB(fbdev->fbs[i]);
1927
1928                ofbi->overlays[0] = fbdev->overlays[i];
1929                ofbi->num_overlays = 1;
1930        }
1931
1932        /* allocate fb memories */
1933        r = omapfb_allocate_all_fbs(fbdev);
1934        if (r) {
1935                dev_err(fbdev->dev, "failed to allocate fbmem\n");
1936                return r;
1937        }
1938
1939        DBG("fbmems allocated\n");
1940
1941        /* setup fb_infos */
1942        for (i = 0; i < fbdev->num_fbs; i++) {
1943                struct fb_info *fbi = fbdev->fbs[i];
1944                struct omapfb_info *ofbi = FB2OFB(fbi);
1945
1946                omapfb_get_mem_region(ofbi->region);
1947                r = omapfb_fb_init(fbdev, fbi);
1948                omapfb_put_mem_region(ofbi->region);
1949
1950                if (r) {
1951                        dev_err(fbdev->dev, "failed to setup fb_info\n");
1952                        return r;
1953                }
1954        }
1955
1956        for (i = 0; i < fbdev->num_fbs; i++) {
1957                struct fb_info *fbi = fbdev->fbs[i];
1958                struct omapfb_info *ofbi = FB2OFB(fbi);
1959
1960                if (ofbi->region->size == 0)
1961                        continue;
1962
1963                omapfb_clear_fb(fbi);
1964        }
1965
1966        DBG("fb_infos initialized\n");
1967
1968        for (i = 0; i < fbdev->num_fbs; i++) {
1969                r = register_framebuffer(fbdev->fbs[i]);
1970                if (r != 0) {
1971                        dev_err(fbdev->dev,
1972                                "registering framebuffer %d failed\n", i);
1973                        return r;
1974                }
1975        }
1976
1977        DBG("framebuffers registered\n");
1978
1979        for (i = 0; i < fbdev->num_fbs; i++) {
1980                struct fb_info *fbi = fbdev->fbs[i];
1981                struct omapfb_info *ofbi = FB2OFB(fbi);
1982
1983                omapfb_get_mem_region(ofbi->region);
1984                r = omapfb_apply_changes(fbi, 1);
1985                omapfb_put_mem_region(ofbi->region);
1986
1987                if (r) {
1988                        dev_err(fbdev->dev, "failed to change mode\n");
1989                        return r;
1990                }
1991        }
1992
1993        /* Enable fb0 */
1994        if (fbdev->num_fbs > 0) {
1995                struct omapfb_info *ofbi = FB2OFB(fbdev->fbs[0]);
1996
1997                if (ofbi->num_overlays > 0) {
1998                        struct omap_overlay *ovl = ofbi->overlays[0];
1999
2000                        ovl->manager->apply(ovl->manager);
2001
2002                        r = omapfb_overlay_enable(ovl, 1);
2003
2004                        if (r) {
2005                                dev_err(fbdev->dev,
2006                                                "failed to enable overlay\n");
2007                                return r;
2008                        }
2009                }
2010        }
2011
2012        DBG("create_framebuffers done\n");
2013
2014        return 0;
2015}
2016
2017static int omapfb_mode_to_timings(const char *mode_str,
2018                struct omap_dss_device *display,
2019                struct omap_video_timings *timings, u8 *bpp)
2020{
2021        struct fb_info *fbi;
2022        struct fb_var_screeninfo *var;
2023        struct fb_ops *fbops;
2024        int r;
2025
2026#ifdef CONFIG_OMAP2_DSS_VENC
2027        if (strcmp(mode_str, "pal") == 0) {
2028                *timings = omap_dss_pal_timings;
2029                *bpp = 24;
2030                return 0;
2031        } else if (strcmp(mode_str, "ntsc") == 0) {
2032                *timings = omap_dss_ntsc_timings;
2033                *bpp = 24;
2034                return 0;
2035        }
2036#endif
2037
2038        /* this is quite a hack, but I wanted to use the modedb and for
2039         * that we need fb_info and var, so we create dummy ones */
2040
2041        *bpp = 0;
2042        fbi = NULL;
2043        var = NULL;
2044        fbops = NULL;
2045
2046        fbi = kzalloc(sizeof(*fbi), GFP_KERNEL);
2047        if (fbi == NULL) {
2048                r = -ENOMEM;
2049                goto err;
2050        }
2051
2052        var = kzalloc(sizeof(*var), GFP_KERNEL);
2053        if (var == NULL) {
2054                r = -ENOMEM;
2055                goto err;
2056        }
2057
2058        fbops = kzalloc(sizeof(*fbops), GFP_KERNEL);
2059        if (fbops == NULL) {
2060                r = -ENOMEM;
2061                goto err;
2062        }
2063
2064        fbi->fbops = fbops;
2065
2066        r = fb_find_mode(var, fbi, mode_str, NULL, 0, NULL, 24);
2067        if (r == 0) {
2068                r = -EINVAL;
2069                goto err;
2070        }
2071
2072        if (display->driver->get_timings) {
2073                display->driver->get_timings(display, timings);
2074        } else {
2075                timings->data_pclk_edge = OMAPDSS_DRIVE_SIG_RISING_EDGE;
2076                timings->de_level = OMAPDSS_SIG_ACTIVE_HIGH;
2077                timings->sync_pclk_edge = OMAPDSS_DRIVE_SIG_FALLING_EDGE;
2078        }
2079
2080        timings->pixelclock = PICOS2KHZ(var->pixclock) * 1000;
2081        timings->hbp = var->left_margin;
2082        timings->hfp = var->right_margin;
2083        timings->vbp = var->upper_margin;
2084        timings->vfp = var->lower_margin;
2085        timings->hsw = var->hsync_len;
2086        timings->vsw = var->vsync_len;
2087        timings->x_res = var->xres;
2088        timings->y_res = var->yres;
2089        timings->hsync_level = var->sync & FB_SYNC_HOR_HIGH_ACT ?
2090                                OMAPDSS_SIG_ACTIVE_HIGH :
2091                                OMAPDSS_SIG_ACTIVE_LOW;
2092        timings->vsync_level = var->sync & FB_SYNC_VERT_HIGH_ACT ?
2093                                OMAPDSS_SIG_ACTIVE_HIGH :
2094                                OMAPDSS_SIG_ACTIVE_LOW;
2095        timings->interlace = var->vmode & FB_VMODE_INTERLACED;
2096
2097        switch (var->bits_per_pixel) {
2098        case 16:
2099                *bpp = 16;
2100                break;
2101        case 24:
2102        case 32:
2103        default:
2104                *bpp = 24;
2105                break;
2106        }
2107
2108        r = 0;
2109
2110err:
2111        kfree(fbi);
2112        kfree(var);
2113        kfree(fbops);
2114
2115        return r;
2116}
2117
2118static int omapfb_set_def_mode(struct omapfb2_device *fbdev,
2119                struct omap_dss_device *display, char *mode_str)
2120{
2121        int r;
2122        u8 bpp;
2123        struct omap_video_timings timings, temp_timings;
2124        struct omapfb_display_data *d;
2125
2126        r = omapfb_mode_to_timings(mode_str, display, &timings, &bpp);
2127        if (r)
2128                return r;
2129
2130        d = get_display_data(fbdev, display);
2131        d->bpp_override = bpp;
2132
2133        if (display->driver->check_timings) {
2134                r = display->driver->check_timings(display, &timings);
2135                if (r)
2136                        return r;
2137        } else {
2138                /* If check_timings is not present compare xres and yres */
2139                if (display->driver->get_timings) {
2140                        display->driver->get_timings(display, &temp_timings);
2141
2142                        if (temp_timings.x_res != timings.x_res ||
2143                                temp_timings.y_res != timings.y_res)
2144                                return -EINVAL;
2145                }
2146        }
2147
2148        if (display->driver->set_timings)
2149                        display->driver->set_timings(display, &timings);
2150
2151        return 0;
2152}
2153
2154static int omapfb_get_recommended_bpp(struct omapfb2_device *fbdev,
2155                struct omap_dss_device *dssdev)
2156{
2157        struct omapfb_display_data *d;
2158
2159        BUG_ON(dssdev->driver->get_recommended_bpp == NULL);
2160
2161        d = get_display_data(fbdev, dssdev);
2162
2163        if (d->bpp_override != 0)
2164                return d->bpp_override;
2165
2166        return dssdev->driver->get_recommended_bpp(dssdev);
2167}
2168
2169static int omapfb_parse_def_modes(struct omapfb2_device *fbdev)
2170{
2171        char *str, *options, *this_opt;
2172        int r = 0;
2173
2174        str = kstrdup(def_mode, GFP_KERNEL);
2175        if (!str)
2176                return -ENOMEM;
2177        options = str;
2178
2179        while (!r && (this_opt = strsep(&options, ",")) != NULL) {
2180                char *p, *display_str, *mode_str;
2181                struct omap_dss_device *display;
2182                int i;
2183
2184                p = strchr(this_opt, ':');
2185                if (!p) {
2186                        r = -EINVAL;
2187                        break;
2188                }
2189
2190                *p = 0;
2191                display_str = this_opt;
2192                mode_str = p + 1;
2193
2194                display = NULL;
2195                for (i = 0; i < fbdev->num_displays; ++i) {
2196                        if (strcmp(fbdev->displays[i].dssdev->name,
2197                                                display_str) == 0) {
2198                                display = fbdev->displays[i].dssdev;
2199                                break;
2200                        }
2201                }
2202
2203                if (!display) {
2204                        r = -EINVAL;
2205                        break;
2206                }
2207
2208                r = omapfb_set_def_mode(fbdev, display, mode_str);
2209                if (r)
2210                        break;
2211        }
2212
2213        kfree(str);
2214
2215        return r;
2216}
2217
2218static void fb_videomode_to_omap_timings(struct fb_videomode *m,
2219                struct omap_dss_device *display,
2220                struct omap_video_timings *t)
2221{
2222        if (display->driver->get_timings) {
2223                display->driver->get_timings(display, t);
2224        } else {
2225                t->data_pclk_edge = OMAPDSS_DRIVE_SIG_RISING_EDGE;
2226                t->de_level = OMAPDSS_SIG_ACTIVE_HIGH;
2227                t->sync_pclk_edge = OMAPDSS_DRIVE_SIG_FALLING_EDGE;
2228        }
2229
2230        t->x_res = m->xres;
2231        t->y_res = m->yres;
2232        t->pixelclock = PICOS2KHZ(m->pixclock) * 1000;
2233        t->hsw = m->hsync_len;
2234        t->hfp = m->right_margin;
2235        t->hbp = m->left_margin;
2236        t->vsw = m->vsync_len;
2237        t->vfp = m->lower_margin;
2238        t->vbp = m->upper_margin;
2239        t->hsync_level = m->sync & FB_SYNC_HOR_HIGH_ACT ?
2240                                OMAPDSS_SIG_ACTIVE_HIGH :
2241                                OMAPDSS_SIG_ACTIVE_LOW;
2242        t->vsync_level = m->sync & FB_SYNC_VERT_HIGH_ACT ?
2243                                OMAPDSS_SIG_ACTIVE_HIGH :
2244                                OMAPDSS_SIG_ACTIVE_LOW;
2245        t->interlace = m->vmode & FB_VMODE_INTERLACED;
2246}
2247
2248static int omapfb_find_best_mode(struct omap_dss_device *display,
2249                struct omap_video_timings *timings)
2250{
2251        struct fb_monspecs *specs;
2252        u8 *edid;
2253        int r, i, best_idx, len;
2254
2255        if (!display->driver->read_edid)
2256                return -ENODEV;
2257
2258        len = 0x80 * 2;
2259        edid = kmalloc(len, GFP_KERNEL);
2260        if (edid == NULL)
2261                return -ENOMEM;
2262
2263        r = display->driver->read_edid(display, edid, len);
2264        if (r < 0)
2265                goto err1;
2266
2267        specs = kzalloc(sizeof(*specs), GFP_KERNEL);
2268        if (specs == NULL) {
2269                r = -ENOMEM;
2270                goto err1;
2271        }
2272
2273        fb_edid_to_monspecs(edid, specs);
2274
2275        best_idx = -1;
2276
2277        for (i = 0; i < specs->modedb_len; ++i) {
2278                struct fb_videomode *m;
2279                struct omap_video_timings t;
2280
2281                m = &specs->modedb[i];
2282
2283                if (m->pixclock == 0)
2284                        continue;
2285
2286                /* skip repeated pixel modes */
2287                if (m->xres == 2880 || m->xres == 1440)
2288                        continue;
2289
2290                if (m->vmode & FB_VMODE_INTERLACED ||
2291                                m->vmode & FB_VMODE_DOUBLE)
2292                        continue;
2293
2294                fb_videomode_to_omap_timings(m, display, &t);
2295
2296                r = display->driver->check_timings(display, &t);
2297                if (r == 0) {
2298                        best_idx = i;
2299                        break;
2300                }
2301        }
2302
2303        if (best_idx == -1) {
2304                r = -ENOENT;
2305                goto err2;
2306        }
2307
2308        fb_videomode_to_omap_timings(&specs->modedb[best_idx], display,
2309                timings);
2310
2311        r = 0;
2312
2313err2:
2314        fb_destroy_modedb(specs->modedb);
2315        kfree(specs);
2316err1:
2317        kfree(edid);
2318
2319        return r;
2320}
2321
2322static int omapfb_init_display(struct omapfb2_device *fbdev,
2323                struct omap_dss_device *dssdev)
2324{
2325        struct omap_dss_driver *dssdrv = dssdev->driver;
2326        struct omapfb_display_data *d;
2327        int r;
2328
2329        r = dssdrv->enable(dssdev);
2330        if (r) {
2331                dev_warn(fbdev->dev, "Failed to enable display '%s'\n",
2332                                dssdev->name);
2333                return r;
2334        }
2335
2336        d = get_display_data(fbdev, dssdev);
2337
2338        d->fbdev = fbdev;
2339
2340        if (dssdev->caps & OMAP_DSS_DISPLAY_CAP_MANUAL_UPDATE) {
2341                u16 w, h;
2342
2343                if (auto_update) {
2344                        omapfb_start_auto_update(fbdev, dssdev);
2345                        d->update_mode = OMAPFB_AUTO_UPDATE;
2346                } else {
2347                        d->update_mode = OMAPFB_MANUAL_UPDATE;
2348                }
2349
2350                if (dssdrv->enable_te) {
2351                        r = dssdrv->enable_te(dssdev, 1);
2352                        if (r) {
2353                                dev_err(fbdev->dev, "Failed to set TE\n");
2354                                return r;
2355                        }
2356                }
2357
2358                dssdrv->get_resolution(dssdev, &w, &h);
2359                r = dssdrv->update(dssdev, 0, 0, w, h);
2360                if (r) {
2361                        dev_err(fbdev->dev,
2362                                        "Failed to update display\n");
2363                        return r;
2364                }
2365        } else {
2366                d->update_mode = OMAPFB_AUTO_UPDATE;
2367        }
2368
2369        return 0;
2370}
2371
2372static int omapfb_init_connections(struct omapfb2_device *fbdev,
2373                struct omap_dss_device *def_dssdev)
2374{
2375        int i, r;
2376        struct omap_overlay_manager *mgr;
2377
2378        r = def_dssdev->driver->connect(def_dssdev);
2379        if (r) {
2380                dev_err(fbdev->dev, "failed to connect default display\n");
2381                return r;
2382        }
2383
2384        for (i = 0; i < fbdev->num_displays; ++i) {
2385                struct omap_dss_device *dssdev = fbdev->displays[i].dssdev;
2386
2387                if (dssdev == def_dssdev)
2388                        continue;
2389
2390                /*
2391                 * We don't care if the connect succeeds or not. We just want to
2392                 * connect as many displays as possible.
2393                 */
2394                dssdev->driver->connect(dssdev);
2395        }
2396
2397        mgr = omapdss_find_mgr_from_display(def_dssdev);
2398
2399        if (!mgr) {
2400                dev_err(fbdev->dev, "no ovl manager for the default display\n");
2401                return -EINVAL;
2402        }
2403
2404        for (i = 0; i < fbdev->num_overlays; i++) {
2405                struct omap_overlay *ovl = fbdev->overlays[i];
2406
2407                if (ovl->manager)
2408                        ovl->unset_manager(ovl);
2409
2410                r = ovl->set_manager(ovl, mgr);
2411                if (r)
2412                        dev_warn(fbdev->dev,
2413                                        "failed to connect overlay %s to manager %s\n",
2414                                        ovl->name, mgr->name);
2415        }
2416
2417        return 0;
2418}
2419
2420static struct omap_dss_device *
2421omapfb_find_default_display(struct omapfb2_device *fbdev)
2422{
2423        const char *def_name;
2424        int i;
2425
2426        /*
2427         * Search with the display name from the user or the board file,
2428         * comparing to display names and aliases
2429         */
2430
2431        def_name = omapdss_get_default_display_name();
2432
2433        if (def_name) {
2434                for (i = 0; i < fbdev->num_displays; ++i) {
2435                        struct omap_dss_device *dssdev;
2436
2437                        dssdev = fbdev->displays[i].dssdev;
2438
2439                        if (dssdev->name && strcmp(def_name, dssdev->name) == 0)
2440                                return dssdev;
2441
2442                        if (strcmp(def_name, dssdev->alias) == 0)
2443                                return dssdev;
2444                }
2445
2446                /* def_name given but not found */
2447                return NULL;
2448        }
2449
2450        /* then look for DT alias display0 */
2451        for (i = 0; i < fbdev->num_displays; ++i) {
2452                struct omap_dss_device *dssdev;
2453                int id;
2454
2455                dssdev = fbdev->displays[i].dssdev;
2456
2457                if (dssdev->dev->of_node == NULL)
2458                        continue;
2459
2460                id = of_alias_get_id(dssdev->dev->of_node, "display");
2461                if (id == 0)
2462                        return dssdev;
2463        }
2464
2465        /* return the first display we have in the list */
2466        return fbdev->displays[0].dssdev;
2467}
2468
2469static int omapfb_probe(struct platform_device *pdev)
2470{
2471        struct omapfb2_device *fbdev = NULL;
2472        int r = 0;
2473        int i;
2474        struct omap_dss_device *def_display;
2475        struct omap_dss_device *dssdev;
2476
2477        DBG("omapfb_probe\n");
2478
2479        if (omapdss_is_initialized() == false)
2480                return -EPROBE_DEFER;
2481
2482        if (pdev->num_resources != 0) {
2483                dev_err(&pdev->dev, "probed for an unknown device\n");
2484                r = -ENODEV;
2485                goto err0;
2486        }
2487
2488        fbdev = devm_kzalloc(&pdev->dev, sizeof(struct omapfb2_device),
2489                        GFP_KERNEL);
2490        if (fbdev == NULL) {
2491                r = -ENOMEM;
2492                goto err0;
2493        }
2494
2495        if (def_vrfb && !omap_vrfb_supported()) {
2496                def_vrfb = 0;
2497                dev_warn(&pdev->dev, "VRFB is not supported on this hardware, "
2498                                "ignoring the module parameter vrfb=y\n");
2499        }
2500
2501        r = omapdss_compat_init();
2502        if (r)
2503                goto err0;
2504
2505        mutex_init(&fbdev->mtx);
2506
2507        fbdev->dev = &pdev->dev;
2508        platform_set_drvdata(pdev, fbdev);
2509
2510        fbdev->num_displays = 0;
2511        dssdev = NULL;
2512        for_each_dss_dev(dssdev) {
2513                struct omapfb_display_data *d;
2514
2515                omap_dss_get_device(dssdev);
2516
2517                if (!dssdev->driver) {
2518                        dev_warn(&pdev->dev, "no driver for display: %s\n",
2519                                dssdev->name);
2520                        omap_dss_put_device(dssdev);
2521                        continue;
2522                }
2523
2524                d = &fbdev->displays[fbdev->num_displays++];
2525                d->dssdev = dssdev;
2526                if (dssdev->caps & OMAP_DSS_DISPLAY_CAP_MANUAL_UPDATE)
2527                        d->update_mode = OMAPFB_MANUAL_UPDATE;
2528                else
2529                        d->update_mode = OMAPFB_AUTO_UPDATE;
2530        }
2531
2532        if (fbdev->num_displays == 0) {
2533                dev_err(&pdev->dev, "no displays\n");
2534                r = -EPROBE_DEFER;
2535                goto cleanup;
2536        }
2537
2538        fbdev->num_overlays = omap_dss_get_num_overlays();
2539        for (i = 0; i < fbdev->num_overlays; i++)
2540                fbdev->overlays[i] = omap_dss_get_overlay(i);
2541
2542        fbdev->num_managers = omap_dss_get_num_overlay_managers();
2543        for (i = 0; i < fbdev->num_managers; i++)
2544                fbdev->managers[i] = omap_dss_get_overlay_manager(i);
2545
2546        def_display = omapfb_find_default_display(fbdev);
2547        if (def_display == NULL) {
2548                dev_err(fbdev->dev, "failed to find default display\n");
2549                r = -EPROBE_DEFER;
2550                goto cleanup;
2551        }
2552
2553        r = omapfb_init_connections(fbdev, def_display);
2554        if (r) {
2555                dev_err(fbdev->dev, "failed to init overlay connections\n");
2556                goto cleanup;
2557        }
2558
2559        if (def_mode && strlen(def_mode) > 0) {
2560                if (omapfb_parse_def_modes(fbdev))
2561                        dev_warn(&pdev->dev, "cannot parse default modes\n");
2562        } else if (def_display && def_display->driver->set_timings &&
2563                        def_display->driver->check_timings) {
2564                struct omap_video_timings t;
2565
2566                r = omapfb_find_best_mode(def_display, &t);
2567
2568                if (r == 0)
2569                        def_display->driver->set_timings(def_display, &t);
2570        }
2571
2572        r = omapfb_create_framebuffers(fbdev);
2573        if (r)
2574                goto cleanup;
2575
2576        for (i = 0; i < fbdev->num_managers; i++) {
2577                struct omap_overlay_manager *mgr;
2578                mgr = fbdev->managers[i];
2579                r = mgr->apply(mgr);
2580                if (r)
2581                        dev_warn(fbdev->dev, "failed to apply dispc config\n");
2582        }
2583
2584        DBG("mgr->apply'ed\n");
2585
2586        if (def_display) {
2587                r = omapfb_init_display(fbdev, def_display);
2588                if (r) {
2589                        dev_err(fbdev->dev,
2590                                        "failed to initialize default "
2591                                        "display\n");
2592                        goto cleanup;
2593                }
2594        }
2595
2596        DBG("create sysfs for fbs\n");
2597        r = omapfb_create_sysfs(fbdev);
2598        if (r) {
2599                dev_err(fbdev->dev, "failed to create sysfs entries\n");
2600                goto cleanup;
2601        }
2602
2603        if (def_display) {
2604                u16 w, h;
2605
2606                def_display->driver->get_resolution(def_display, &w, &h);
2607
2608                dev_info(fbdev->dev, "using display '%s' mode %dx%d\n",
2609                        def_display->name, w, h);
2610        }
2611
2612        return 0;
2613
2614cleanup:
2615        omapfb_free_resources(fbdev);
2616        omapdss_compat_uninit();
2617err0:
2618        dev_err(&pdev->dev, "failed to setup omapfb\n");
2619        return r;
2620}
2621
2622static int omapfb_remove(struct platform_device *pdev)
2623{
2624        struct omapfb2_device *fbdev = platform_get_drvdata(pdev);
2625
2626        /* FIXME: wait till completion of pending events */
2627
2628        omapfb_remove_sysfs(fbdev);
2629
2630        omapfb_free_resources(fbdev);
2631
2632        omapdss_compat_uninit();
2633
2634        return 0;
2635}
2636
2637static struct platform_driver omapfb_driver = {
2638        .probe          = omapfb_probe,
2639        .remove         = omapfb_remove,
2640        .driver         = {
2641                .name   = "omapfb",
2642        },
2643};
2644
2645module_param_named(mode, def_mode, charp, 0);
2646module_param_named(vram, def_vram, charp, 0);
2647module_param_named(rotate, def_rotate, int, 0);
2648module_param_named(vrfb, def_vrfb, bool, 0);
2649module_param_named(mirror, def_mirror, bool, 0);
2650
2651module_platform_driver(omapfb_driver);
2652
2653MODULE_ALIAS("platform:omapfb");
2654MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@nokia.com>");
2655MODULE_DESCRIPTION("OMAP2/3 Framebuffer");
2656MODULE_LICENSE("GPL v2");
2657