linux/drivers/staging/sm750fb/sm750.c
<<
>>
Prefs
   1#include <linux/kernel.h>
   2#include <linux/module.h>
   3#include <linux/errno.h>
   4#include <linux/string.h>
   5#include <linux/mm.h>
   6#include <linux/slab.h>
   7#include <linux/delay.h>
   8#include <linux/fb.h>
   9#include <linux/ioport.h>
  10#include <linux/init.h>
  11#include <linux/pci.h>
  12#include <linux/mm_types.h>
  13#include <linux/vmalloc.h>
  14#include <linux/pagemap.h>
  15#include <linux/screen_info.h>
  16#include <linux/console.h>
  17#include <asm/fb.h>
  18#include "sm750.h"
  19#include "sm750_accel.h"
  20#include "sm750_cursor.h"
  21
  22/*
  23 * #ifdef __BIG_ENDIAN
  24 * ssize_t lynxfb_ops_write(struct fb_info *info, const char __user *buf,
  25 * size_t count, loff_t *ppos);
  26 * ssize_t lynxfb_ops_read(struct fb_info *info, char __user *buf,
  27 * size_t count, loff_t *ppos);
  28 * #endif
  29 */
  30
  31/* common var for all device */
  32static int g_hwcursor = 1;
  33static int g_noaccel;
  34static int g_nomtrr;
  35static const char *g_fbmode[] = {NULL, NULL};
  36static const char *g_def_fbmode = "800x600-16@60";
  37static char *g_settings;
  38static int g_dualview;
  39static char *g_option;
  40
  41static const struct fb_videomode lynx750_ext[] = {
  42        /*      1024x600-60 VESA        [1.71:1] */
  43        {NULL,  60, 1024, 600, 20423, 144,  40, 18, 1, 104, 3,
  44         FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
  45         FB_VMODE_NONINTERLACED},
  46
  47        /*      1024x600-70 VESA */
  48        {NULL,  70, 1024, 600, 17211, 152,  48, 21, 1, 104, 3,
  49         FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
  50         FB_VMODE_NONINTERLACED},
  51
  52        /*      1024x600-75 VESA */
  53        {NULL,  75, 1024, 600, 15822, 160,  56, 23, 1, 104, 3,
  54         FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
  55         FB_VMODE_NONINTERLACED},
  56
  57        /*      1024x600-85 VESA */
  58        {NULL,  85, 1024, 600, 13730, 168,  56, 26, 1, 112, 3,
  59         FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
  60         FB_VMODE_NONINTERLACED},
  61
  62        /*      720x480 */
  63        {NULL, 60,  720,  480,  37427, 88,   16, 13, 1,   72,  3,
  64         FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
  65         FB_VMODE_NONINTERLACED},
  66
  67        /*      1280x720                [1.78:1]        */
  68        {NULL, 60,  1280,  720,  13426, 162, 86, 22, 1,  136, 3,
  69         FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
  70         FB_VMODE_NONINTERLACED},
  71
  72        /*      1280x768@60 */
  73        {NULL, 60, 1280, 768, 12579, 192, 64, 20, 3, 128, 7,
  74         FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
  75         FB_VMODE_NONINTERLACED},
  76
  77        /*      1360 x 768      [1.77083:1]     */
  78        {NULL,  60, 1360, 768, 11804, 208,  64, 23, 1, 144, 3,
  79         FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
  80         FB_VMODE_NONINTERLACED},
  81
  82        /*      1368 x 768      [1.78:1]        */
  83        {NULL, 60,  1368,  768,  11647, 216, 72, 23, 1,  144, 3,
  84         FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
  85         FB_VMODE_NONINTERLACED},
  86
  87        /*      1440 x 900              [16:10] */
  88        {NULL, 60, 1440, 900, 9392, 232, 80, 28, 1, 152, 3,
  89         FB_SYNC_VERT_HIGH_ACT,
  90         FB_VMODE_NONINTERLACED},
  91
  92        /*      1440x960                [15:10] */
  93        {NULL, 60, 1440, 960, 8733, 240, 88, 30, 1, 152, 3,
  94         FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
  95         FB_VMODE_NONINTERLACED},
  96
  97        /*      1920x1080       [16:9]  */
  98        {NULL, 60, 1920, 1080, 6734, 148, 88, 41, 1, 44, 3,
  99         FB_SYNC_VERT_HIGH_ACT,
 100         FB_VMODE_NONINTERLACED},
 101};
 102
 103/* no hardware cursor supported under version 2.6.10, kernel bug */
 104static int lynxfb_ops_cursor(struct fb_info *info, struct fb_cursor *fbcursor)
 105{
 106        struct lynxfb_par *par;
 107        struct lynxfb_crtc *crtc;
 108        struct lynx_cursor *cursor;
 109
 110        par = info->par;
 111        crtc = &par->crtc;
 112        cursor = &crtc->cursor;
 113
 114        if (fbcursor->image.width > cursor->maxW ||
 115           fbcursor->image.height > cursor->maxH ||
 116           fbcursor->image.depth > 1) {
 117                return -ENXIO;
 118        }
 119
 120        sm750_hw_cursor_disable(cursor);
 121        if (fbcursor->set & FB_CUR_SETSIZE)
 122                sm750_hw_cursor_setSize(cursor,
 123                                  fbcursor->image.width,
 124                                  fbcursor->image.height);
 125
 126        if (fbcursor->set & FB_CUR_SETPOS)
 127                sm750_hw_cursor_setPos(cursor,
 128                                 fbcursor->image.dx - info->var.xoffset,
 129                                 fbcursor->image.dy - info->var.yoffset);
 130
 131        if (fbcursor->set & FB_CUR_SETCMAP) {
 132                /* get the 16bit color of kernel means */
 133                u16 fg, bg;
 134
 135                fg = ((info->cmap.red[fbcursor->image.fg_color] & 0xf800)) |
 136                      ((info->cmap.green[fbcursor->image.fg_color] & 0xfc00) >> 5) |
 137                      ((info->cmap.blue[fbcursor->image.fg_color] & 0xf800) >> 11);
 138
 139                bg = ((info->cmap.red[fbcursor->image.bg_color] & 0xf800)) |
 140                      ((info->cmap.green[fbcursor->image.bg_color] & 0xfc00) >> 5) |
 141                      ((info->cmap.blue[fbcursor->image.bg_color] & 0xf800) >> 11);
 142
 143                sm750_hw_cursor_setColor(cursor, fg, bg);
 144        }
 145
 146        if (fbcursor->set & (FB_CUR_SETSHAPE | FB_CUR_SETIMAGE)) {
 147                sm750_hw_cursor_setData(cursor,
 148                                  fbcursor->rop,
 149                                  fbcursor->image.data,
 150                                  fbcursor->mask);
 151        }
 152
 153        if (fbcursor->enable)
 154                sm750_hw_cursor_enable(cursor);
 155
 156        return 0;
 157}
 158
 159static void lynxfb_ops_fillrect(struct fb_info *info,
 160                                const struct fb_fillrect *region)
 161{
 162        struct lynxfb_par *par;
 163        struct sm750_dev *sm750_dev;
 164        unsigned int base, pitch, Bpp, rop;
 165        u32 color;
 166
 167        if (info->state != FBINFO_STATE_RUNNING)
 168                return;
 169
 170        par = info->par;
 171        sm750_dev = par->dev;
 172
 173        /*
 174         * each time 2d function begin to work,below three variable always need
 175         * be set, seems we can put them together in some place
 176         */
 177        base = par->crtc.oScreen;
 178        pitch = info->fix.line_length;
 179        Bpp = info->var.bits_per_pixel >> 3;
 180
 181        color = (Bpp == 1) ? region->color :
 182                ((u32 *)info->pseudo_palette)[region->color];
 183        rop = (region->rop != ROP_COPY) ? HW_ROP2_XOR : HW_ROP2_COPY;
 184
 185        /*
 186         * If not use spin_lock,system will die if user load driver
 187         * and immediately unload driver frequently (dual)
 188         */
 189        if (sm750_dev->fb_count > 1)
 190                spin_lock(&sm750_dev->slock);
 191
 192        sm750_dev->accel.de_fillrect(&sm750_dev->accel,
 193                                     base, pitch, Bpp,
 194                                     region->dx, region->dy,
 195                                     region->width, region->height,
 196                                     color, rop);
 197        if (sm750_dev->fb_count > 1)
 198                spin_unlock(&sm750_dev->slock);
 199}
 200
 201static void lynxfb_ops_copyarea(struct fb_info *info,
 202                                const struct fb_copyarea *region)
 203{
 204        struct lynxfb_par *par;
 205        struct sm750_dev *sm750_dev;
 206        unsigned int base, pitch, Bpp;
 207
 208        par = info->par;
 209        sm750_dev = par->dev;
 210
 211        /*
 212         * each time 2d function begin to work,below three variable always need
 213         * be set, seems we can put them together in some place
 214         */
 215        base = par->crtc.oScreen;
 216        pitch = info->fix.line_length;
 217        Bpp = info->var.bits_per_pixel >> 3;
 218
 219        /*
 220         * If not use spin_lock, system will die if user load driver
 221         * and immediately unload driver frequently (dual)
 222         */
 223        if (sm750_dev->fb_count > 1)
 224                spin_lock(&sm750_dev->slock);
 225
 226        sm750_dev->accel.de_copyarea(&sm750_dev->accel,
 227                                     base, pitch, region->sx, region->sy,
 228                                     base, pitch, Bpp, region->dx, region->dy,
 229                                     region->width, region->height,
 230                                     HW_ROP2_COPY);
 231        if (sm750_dev->fb_count > 1)
 232                spin_unlock(&sm750_dev->slock);
 233}
 234
 235static void lynxfb_ops_imageblit(struct fb_info *info,
 236                                 const struct fb_image *image)
 237{
 238        unsigned int base, pitch, Bpp;
 239        unsigned int fgcol, bgcol;
 240        struct lynxfb_par *par;
 241        struct sm750_dev *sm750_dev;
 242
 243        par = info->par;
 244        sm750_dev = par->dev;
 245        /*
 246         * each time 2d function begin to work,below three variable always need
 247         * be set, seems we can put them together in some place
 248         */
 249        base = par->crtc.oScreen;
 250        pitch = info->fix.line_length;
 251        Bpp = info->var.bits_per_pixel >> 3;
 252
 253        /* TODO: Implement hardware acceleration for image->depth > 1 */
 254        if (image->depth != 1) {
 255                cfb_imageblit(info, image);
 256                return;
 257        }
 258
 259        if (info->fix.visual == FB_VISUAL_TRUECOLOR ||
 260            info->fix.visual == FB_VISUAL_DIRECTCOLOR) {
 261                fgcol = ((u32 *)info->pseudo_palette)[image->fg_color];
 262                bgcol = ((u32 *)info->pseudo_palette)[image->bg_color];
 263        } else {
 264                fgcol = image->fg_color;
 265                bgcol = image->bg_color;
 266        }
 267
 268        /*
 269         * If not use spin_lock, system will die if user load driver
 270         * and immediately unload driver frequently (dual)
 271         */
 272        if (sm750_dev->fb_count > 1)
 273                spin_lock(&sm750_dev->slock);
 274
 275        sm750_dev->accel.de_imageblit(&sm750_dev->accel,
 276                                      image->data, image->width >> 3, 0,
 277                                      base, pitch, Bpp,
 278                                      image->dx, image->dy,
 279                                      image->width, image->height,
 280                                      fgcol, bgcol, HW_ROP2_COPY);
 281        if (sm750_dev->fb_count > 1)
 282                spin_unlock(&sm750_dev->slock);
 283}
 284
 285static int lynxfb_ops_pan_display(struct fb_var_screeninfo *var,
 286                                  struct fb_info *info)
 287{
 288        struct lynxfb_par *par;
 289        struct lynxfb_crtc *crtc;
 290
 291        if (!info)
 292                return -EINVAL;
 293
 294        par = info->par;
 295        crtc = &par->crtc;
 296        return hw_sm750_pan_display(crtc, var, info);
 297}
 298
 299static int lynxfb_ops_set_par(struct fb_info *info)
 300{
 301        struct lynxfb_par *par;
 302        struct lynxfb_crtc *crtc;
 303        struct lynxfb_output *output;
 304        struct fb_var_screeninfo *var;
 305        struct fb_fix_screeninfo *fix;
 306        int ret;
 307        unsigned int line_length;
 308
 309        if (!info)
 310                return -EINVAL;
 311
 312        ret = 0;
 313        par = info->par;
 314        crtc = &par->crtc;
 315        output = &par->output;
 316        var = &info->var;
 317        fix = &info->fix;
 318
 319        /* fix structure is not so FIX ... */
 320        line_length = var->xres_virtual * var->bits_per_pixel / 8;
 321        line_length = ALIGN(line_length, crtc->line_pad);
 322        fix->line_length = line_length;
 323        pr_info("fix->line_length = %d\n", fix->line_length);
 324
 325        /*
 326         * var->red,green,blue,transp are need to be set by driver
 327         * and these data should be set before setcolreg routine
 328         */
 329
 330        switch (var->bits_per_pixel) {
 331        case 8:
 332                fix->visual = FB_VISUAL_PSEUDOCOLOR;
 333                var->red.offset = 0;
 334                var->red.length = 8;
 335                var->green.offset = 0;
 336                var->green.length = 8;
 337                var->blue.offset = 0;
 338                var->blue.length = 8;
 339                var->transp.length = 0;
 340                var->transp.offset = 0;
 341                break;
 342        case 16:
 343                var->red.offset = 11;
 344                var->red.length = 5;
 345                var->green.offset = 5;
 346                var->green.length = 6;
 347                var->blue.offset = 0;
 348                var->blue.length = 5;
 349                var->transp.length = 0;
 350                var->transp.offset = 0;
 351                fix->visual = FB_VISUAL_TRUECOLOR;
 352                break;
 353        case 24:
 354        case 32:
 355                var->red.offset = 16;
 356                var->red.length = 8;
 357                var->green.offset = 8;
 358                var->green.length = 8;
 359                var->blue.offset = 0;
 360                var->blue.length = 8;
 361                fix->visual = FB_VISUAL_TRUECOLOR;
 362                break;
 363        default:
 364                ret = -EINVAL;
 365                break;
 366        }
 367        var->height = var->width = -1;
 368        var->accel_flags = 0;/*FB_ACCELF_TEXT;*/
 369
 370        if (ret) {
 371                pr_err("pixel bpp format not satisfied\n.");
 372                return ret;
 373        }
 374        ret = hw_sm750_crtc_setMode(crtc, var, fix);
 375        if (!ret)
 376                ret = hw_sm750_output_setMode(output, var, fix);
 377        return ret;
 378}
 379
 380static inline unsigned int chan_to_field(unsigned int chan,
 381                                         struct fb_bitfield *bf)
 382{
 383        chan &= 0xffff;
 384        chan >>= 16 - bf->length;
 385        return chan << bf->offset;
 386}
 387
 388#ifdef CONFIG_PM
 389static int lynxfb_suspend(struct pci_dev *pdev, pm_message_t mesg)
 390{
 391        struct fb_info *info;
 392        struct sm750_dev *sm750_dev;
 393        int ret;
 394
 395        if (mesg.event == pdev->dev.power.power_state.event)
 396                return 0;
 397
 398        ret = 0;
 399        sm750_dev = pci_get_drvdata(pdev);
 400        switch (mesg.event) {
 401        case PM_EVENT_FREEZE:
 402        case PM_EVENT_PRETHAW:
 403                pdev->dev.power.power_state = mesg;
 404                return 0;
 405        }
 406
 407        console_lock();
 408        if (mesg.event & PM_EVENT_SLEEP) {
 409                info = sm750_dev->fbinfo[0];
 410                if (info)
 411                        /* 1 means do suspend */
 412                        fb_set_suspend(info, 1);
 413                info = sm750_dev->fbinfo[1];
 414                if (info)
 415                        /* 1 means do suspend */
 416                        fb_set_suspend(info, 1);
 417
 418                ret = pci_save_state(pdev);
 419                if (ret) {
 420                        dev_err(&pdev->dev,
 421                                "error:%d occurred in pci_save_state\n", ret);
 422                        return ret;
 423                }
 424
 425                ret = pci_set_power_state(pdev, pci_choose_state(pdev, mesg));
 426                if (ret) {
 427                        dev_err(&pdev->dev,
 428                                "error:%d occurred in pci_set_power_state\n",
 429                                ret);
 430                        return ret;
 431                }
 432        }
 433
 434        pdev->dev.power.power_state = mesg;
 435        console_unlock();
 436        return ret;
 437}
 438
 439static int lynxfb_resume(struct pci_dev *pdev)
 440{
 441        struct fb_info *info;
 442        struct sm750_dev *sm750_dev;
 443
 444        struct lynxfb_par *par;
 445        struct lynxfb_crtc *crtc;
 446        struct lynx_cursor *cursor;
 447
 448        int ret;
 449
 450        ret = 0;
 451        sm750_dev = pci_get_drvdata(pdev);
 452
 453        console_lock();
 454
 455        ret = pci_set_power_state(pdev, PCI_D0);
 456        if (ret) {
 457                dev_err(&pdev->dev,
 458                        "error:%d occurred in pci_set_power_state\n", ret);
 459                return ret;
 460        }
 461
 462        if (pdev->dev.power.power_state.event != PM_EVENT_FREEZE) {
 463                pci_restore_state(pdev);
 464                ret = pci_enable_device(pdev);
 465                if (ret) {
 466                        dev_err(&pdev->dev,
 467                                "error:%d occurred in pci_enable_device\n",
 468                                ret);
 469                        return ret;
 470                }
 471                pci_set_master(pdev);
 472        }
 473
 474        hw_sm750_inithw(sm750_dev, pdev);
 475
 476        info = sm750_dev->fbinfo[0];
 477
 478        if (info) {
 479                par = info->par;
 480                crtc = &par->crtc;
 481                cursor = &crtc->cursor;
 482                memset_io(cursor->vstart, 0x0, cursor->size);
 483                memset_io(crtc->vScreen, 0x0, crtc->vidmem_size);
 484                lynxfb_ops_set_par(info);
 485                fb_set_suspend(info, 0);
 486        }
 487
 488        info = sm750_dev->fbinfo[1];
 489
 490        if (info) {
 491                par = info->par;
 492                crtc = &par->crtc;
 493                cursor = &crtc->cursor;
 494                memset_io(cursor->vstart, 0x0, cursor->size);
 495                memset_io(crtc->vScreen, 0x0, crtc->vidmem_size);
 496                lynxfb_ops_set_par(info);
 497                fb_set_suspend(info, 0);
 498        }
 499
 500        pdev->dev.power.power_state.event = PM_EVENT_RESUME;
 501        console_unlock();
 502        return ret;
 503}
 504#endif
 505
 506static int lynxfb_ops_check_var(struct fb_var_screeninfo *var,
 507                                struct fb_info *info)
 508{
 509        struct lynxfb_par *par;
 510        struct lynxfb_crtc *crtc;
 511        struct lynxfb_output *output;
 512        resource_size_t request;
 513
 514        par = info->par;
 515        crtc = &par->crtc;
 516        output = &par->output;
 517
 518        pr_debug("check var:%dx%d-%d\n",
 519                 var->xres,
 520                 var->yres,
 521                 var->bits_per_pixel);
 522
 523        switch (var->bits_per_pixel) {
 524        case 8:
 525                info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
 526                var->red.offset = 0;
 527                var->red.length = 8;
 528                var->green.offset = 0;
 529                var->green.length = 8;
 530                var->blue.offset = 0;
 531                var->blue.length = 8;
 532                var->transp.length = 0;
 533                var->transp.offset = 0;
 534                break;
 535        case 16:
 536                var->red.offset = 11;
 537                var->red.length = 5;
 538                var->green.offset = 5;
 539                var->green.length = 6;
 540                var->blue.offset = 0;
 541                var->blue.length = 5;
 542                var->transp.length = 0;
 543                var->transp.offset = 0;
 544                info->fix.visual = FB_VISUAL_TRUECOLOR;
 545                break;
 546        case 24:
 547        case 32:
 548                var->red.offset = 16;
 549                var->red.length = 8;
 550                var->green.offset = 8;
 551                var->green.length = 8;
 552                var->blue.offset = 0;
 553                var->blue.length = 8;
 554                info->fix.visual = FB_VISUAL_TRUECOLOR;
 555                break;
 556        default:
 557                pr_err("bpp %d not supported\n", var->bits_per_pixel);
 558                return -EINVAL;
 559        }
 560        var->height = var->width = -1;
 561        var->accel_flags = 0;/* FB_ACCELF_TEXT; */
 562
 563        /* check if current fb's video memory big enought to hold the onscreen*/
 564        request = var->xres_virtual * (var->bits_per_pixel >> 3);
 565        /* defaulty crtc->channel go with par->index */
 566
 567        request = ALIGN(request, crtc->line_pad);
 568        request = request * var->yres_virtual;
 569        if (crtc->vidmem_size < request) {
 570                pr_err("not enough video memory for mode\n");
 571                return -ENOMEM;
 572        }
 573
 574        return hw_sm750_crtc_checkMode(crtc, var);
 575}
 576
 577static int lynxfb_ops_setcolreg(unsigned int regno,
 578                                unsigned int red,
 579                                unsigned int green,
 580                                unsigned int blue,
 581                                unsigned int transp,
 582                                struct fb_info *info)
 583{
 584        struct lynxfb_par *par;
 585        struct lynxfb_crtc *crtc;
 586        struct fb_var_screeninfo *var;
 587        int ret;
 588
 589        par = info->par;
 590        crtc = &par->crtc;
 591        var = &info->var;
 592        ret = 0;
 593
 594        if (regno > 256) {
 595                pr_err("regno = %d\n", regno);
 596                return -EINVAL;
 597        }
 598
 599        if (info->var.grayscale)
 600                red = green = blue = (red * 77 + green * 151 + blue * 28) >> 8;
 601
 602        if (var->bits_per_pixel == 8 &&
 603            info->fix.visual == FB_VISUAL_PSEUDOCOLOR) {
 604                red >>= 8;
 605                green >>= 8;
 606                blue >>= 8;
 607                ret = hw_sm750_setColReg(crtc, regno, red, green, blue);
 608                goto exit;
 609        }
 610
 611        if (info->fix.visual == FB_VISUAL_TRUECOLOR && regno < 256) {
 612                u32 val;
 613
 614                if (var->bits_per_pixel == 16 ||
 615                    var->bits_per_pixel == 32 ||
 616                    var->bits_per_pixel == 24) {
 617                        val = chan_to_field(red, &var->red);
 618                        val |= chan_to_field(green, &var->green);
 619                        val |= chan_to_field(blue, &var->blue);
 620                        par->pseudo_palette[regno] = val;
 621                        goto exit;
 622                }
 623        }
 624
 625        ret = -EINVAL;
 626
 627exit:
 628        return ret;
 629}
 630
 631static int lynxfb_ops_blank(int blank, struct fb_info *info)
 632{
 633        struct lynxfb_par *par;
 634        struct lynxfb_output *output;
 635
 636        pr_debug("blank = %d.\n", blank);
 637        par = info->par;
 638        output = &par->output;
 639        return output->proc_setBLANK(output, blank);
 640}
 641
 642static int sm750fb_set_drv(struct lynxfb_par *par)
 643{
 644        int ret;
 645        struct sm750_dev *sm750_dev;
 646        struct lynxfb_output *output;
 647        struct lynxfb_crtc *crtc;
 648
 649        ret = 0;
 650
 651        sm750_dev = par->dev;
 652        output = &par->output;
 653        crtc = &par->crtc;
 654
 655        crtc->vidmem_size = sm750_dev->vidmem_size;
 656        if (sm750_dev->fb_count > 1)
 657                crtc->vidmem_size >>= 1;
 658
 659        /* setup crtc and output member */
 660        sm750_dev->hwCursor = g_hwcursor;
 661
 662        crtc->line_pad = 16;
 663        crtc->xpanstep = 8;
 664        crtc->ypanstep = 1;
 665        crtc->ywrapstep = 0;
 666
 667        output->proc_setBLANK = (sm750_dev->revid == SM750LE_REVISION_ID) ?
 668                                 hw_sm750le_setBLANK : hw_sm750_setBLANK;
 669        /* chip specific phase */
 670        sm750_dev->accel.de_wait = (sm750_dev->revid == SM750LE_REVISION_ID) ?
 671                                    hw_sm750le_deWait : hw_sm750_deWait;
 672        switch (sm750_dev->dataflow) {
 673        case sm750_simul_pri:
 674                output->paths = sm750_pnc;
 675                crtc->channel = sm750_primary;
 676                crtc->oScreen = 0;
 677                crtc->vScreen = sm750_dev->pvMem;
 678                pr_info("use simul primary mode\n");
 679                break;
 680        case sm750_simul_sec:
 681                output->paths = sm750_pnc;
 682                crtc->channel = sm750_secondary;
 683                crtc->oScreen = 0;
 684                crtc->vScreen = sm750_dev->pvMem;
 685                break;
 686        case sm750_dual_normal:
 687                if (par->index == 0) {
 688                        output->paths = sm750_panel;
 689                        crtc->channel = sm750_primary;
 690                        crtc->oScreen = 0;
 691                        crtc->vScreen = sm750_dev->pvMem;
 692                } else {
 693                        output->paths = sm750_crt;
 694                        crtc->channel = sm750_secondary;
 695                        /* not consider of padding stuffs for oScreen,need fix */
 696                        crtc->oScreen = (sm750_dev->vidmem_size >> 1);
 697                        crtc->vScreen = sm750_dev->pvMem + crtc->oScreen;
 698                }
 699                break;
 700        case sm750_dual_swap:
 701                if (par->index == 0) {
 702                        output->paths = sm750_panel;
 703                        crtc->channel = sm750_secondary;
 704                        crtc->oScreen = 0;
 705                        crtc->vScreen = sm750_dev->pvMem;
 706                } else {
 707                        output->paths = sm750_crt;
 708                        crtc->channel = sm750_primary;
 709                        /* not consider of padding stuffs for oScreen,need fix */
 710                        crtc->oScreen = (sm750_dev->vidmem_size >> 1);
 711                        crtc->vScreen = sm750_dev->pvMem + crtc->oScreen;
 712                }
 713                break;
 714        default:
 715                ret = -EINVAL;
 716        }
 717
 718        return ret;
 719}
 720
 721static struct fb_ops lynxfb_ops = {
 722        .owner = THIS_MODULE,
 723        .fb_check_var =  lynxfb_ops_check_var,
 724        .fb_set_par = lynxfb_ops_set_par,
 725        .fb_setcolreg = lynxfb_ops_setcolreg,
 726        .fb_blank = lynxfb_ops_blank,
 727        .fb_fillrect = cfb_fillrect,
 728        .fb_imageblit = cfb_imageblit,
 729        .fb_copyarea = cfb_copyarea,
 730        /* cursor */
 731        .fb_cursor = lynxfb_ops_cursor,
 732};
 733
 734static int lynxfb_set_fbinfo(struct fb_info *info, int index)
 735{
 736        int i;
 737        struct lynxfb_par *par;
 738        struct sm750_dev *sm750_dev;
 739        struct lynxfb_crtc *crtc;
 740        struct lynxfb_output *output;
 741        struct fb_var_screeninfo *var;
 742        struct fb_fix_screeninfo *fix;
 743
 744        const struct fb_videomode *pdb[] = {
 745                lynx750_ext, NULL, vesa_modes,
 746        };
 747        int cdb[] = {ARRAY_SIZE(lynx750_ext), 0, VESA_MODEDB_SIZE};
 748        static const char *mdb_desc[] = {
 749                "driver prepared modes",
 750                "kernel prepared default modedb",
 751                "kernel HELPERS prepared vesa_modes",
 752        };
 753
 754        static const char *fixId[2] = {
 755                "sm750_fb1", "sm750_fb2",
 756        };
 757
 758        int ret, line_length;
 759
 760        ret = 0;
 761        par = (struct lynxfb_par *)info->par;
 762        sm750_dev = par->dev;
 763        crtc = &par->crtc;
 764        output = &par->output;
 765        var = &info->var;
 766        fix = &info->fix;
 767
 768        /* set index */
 769        par->index = index;
 770        output->channel = &crtc->channel;
 771        sm750fb_set_drv(par);
 772        lynxfb_ops.fb_pan_display = lynxfb_ops_pan_display;
 773
 774        /*
 775         * set current cursor variable and proc pointer,
 776         * must be set after crtc member initialized
 777         */
 778        crtc->cursor.offset = crtc->oScreen + crtc->vidmem_size - 1024;
 779        crtc->cursor.mmio = sm750_dev->pvReg +
 780                0x800f0 + (int)crtc->channel * 0x140;
 781
 782        pr_info("crtc->cursor.mmio = %p\n", crtc->cursor.mmio);
 783        crtc->cursor.maxH = crtc->cursor.maxW = 64;
 784        crtc->cursor.size = crtc->cursor.maxH * crtc->cursor.maxW * 2 / 8;
 785        crtc->cursor.vstart = sm750_dev->pvMem + crtc->cursor.offset;
 786
 787        memset_io(crtc->cursor.vstart, 0, crtc->cursor.size);
 788        if (!g_hwcursor) {
 789                lynxfb_ops.fb_cursor = NULL;
 790                sm750_hw_cursor_disable(&crtc->cursor);
 791        }
 792
 793        /* set info->fbops, must be set before fb_find_mode */
 794        if (!sm750_dev->accel_off) {
 795                /* use 2d acceleration */
 796                lynxfb_ops.fb_fillrect = lynxfb_ops_fillrect;
 797                lynxfb_ops.fb_copyarea = lynxfb_ops_copyarea;
 798                lynxfb_ops.fb_imageblit = lynxfb_ops_imageblit;
 799        }
 800        info->fbops = &lynxfb_ops;
 801
 802        if (!g_fbmode[index]) {
 803                g_fbmode[index] = g_def_fbmode;
 804                if (index)
 805                        g_fbmode[index] = g_fbmode[0];
 806        }
 807
 808        for (i = 0; i < 3; i++) {
 809
 810                ret = fb_find_mode(var, info, g_fbmode[index],
 811                                   pdb[i], cdb[i], NULL, 8);
 812
 813                if (ret == 1) {
 814                        pr_info("success! use specified mode:%s in %s\n",
 815                                g_fbmode[index],
 816                                mdb_desc[i]);
 817                        break;
 818                } else if (ret == 2) {
 819                        pr_warn("use specified mode:%s in %s,with an ignored refresh rate\n",
 820                                g_fbmode[index],
 821                                mdb_desc[i]);
 822                        break;
 823                } else if (ret == 3) {
 824                        pr_warn("wanna use default mode\n");
 825                        /*break;*/
 826                } else if (ret == 4) {
 827                        pr_warn("fall back to any valid mode\n");
 828                } else {
 829                        pr_warn("ret = %d,fb_find_mode failed,with %s\n",
 830                                ret,
 831                                mdb_desc[i]);
 832                }
 833        }
 834
 835        /* some member of info->var had been set by fb_find_mode */
 836
 837        pr_info("Member of info->var is :\n\
 838                xres=%d\n\
 839                yres=%d\n\
 840                xres_virtual=%d\n\
 841                yres_virtual=%d\n\
 842                xoffset=%d\n\
 843                yoffset=%d\n\
 844                bits_per_pixel=%d\n \
 845                ...\n",
 846                var->xres,
 847                var->yres,
 848                var->xres_virtual,
 849                var->yres_virtual,
 850                var->xoffset,
 851                var->yoffset,
 852                var->bits_per_pixel);
 853
 854        /* set par */
 855        par->info = info;
 856
 857        /* set info */
 858        line_length = ALIGN((var->xres_virtual * var->bits_per_pixel / 8),
 859                            crtc->line_pad);
 860
 861        info->pseudo_palette = &par->pseudo_palette[0];
 862        info->screen_base = crtc->vScreen;
 863        pr_debug("screen_base vaddr = %p\n", info->screen_base);
 864        info->screen_size = line_length * var->yres_virtual;
 865        info->flags = FBINFO_FLAG_DEFAULT | 0;
 866
 867        /* set info->fix */
 868        fix->type = FB_TYPE_PACKED_PIXELS;
 869        fix->type_aux = 0;
 870        fix->xpanstep = crtc->xpanstep;
 871        fix->ypanstep = crtc->ypanstep;
 872        fix->ywrapstep = crtc->ywrapstep;
 873        fix->accel = FB_ACCEL_SMI;
 874
 875        strlcpy(fix->id, fixId[index], sizeof(fix->id));
 876
 877        fix->smem_start = crtc->oScreen + sm750_dev->vidmem_start;
 878        pr_info("fix->smem_start = %lx\n", fix->smem_start);
 879        /*
 880         * according to mmap experiment from user space application,
 881         * fix->mmio_len should not larger than virtual size
 882         * (xres_virtual x yres_virtual x ByPP)
 883         * Below line maybe buggy when user mmap fb dev node and write
 884         * data into the bound over virtual size
 885         */
 886        fix->smem_len = crtc->vidmem_size;
 887        pr_info("fix->smem_len = %x\n", fix->smem_len);
 888        info->screen_size = fix->smem_len;
 889        fix->line_length = line_length;
 890        fix->mmio_start = sm750_dev->vidreg_start;
 891        pr_info("fix->mmio_start = %lx\n", fix->mmio_start);
 892        fix->mmio_len = sm750_dev->vidreg_size;
 893        pr_info("fix->mmio_len = %x\n", fix->mmio_len);
 894        switch (var->bits_per_pixel) {
 895        case 8:
 896                fix->visual = FB_VISUAL_PSEUDOCOLOR;
 897                break;
 898        case 16:
 899        case 32:
 900                fix->visual = FB_VISUAL_TRUECOLOR;
 901                break;
 902        }
 903
 904        /* set var */
 905        var->activate = FB_ACTIVATE_NOW;
 906        var->accel_flags = 0;
 907        var->vmode = FB_VMODE_NONINTERLACED;
 908
 909        pr_debug("#1 show info->cmap :\nstart=%d,len=%d,red=%p,green=%p,blue=%p,transp=%p\n",
 910                 info->cmap.start, info->cmap.len,
 911                 info->cmap.red, info->cmap.green, info->cmap.blue,
 912                 info->cmap.transp);
 913
 914        ret = fb_alloc_cmap(&info->cmap, 256, 0);
 915        if (ret < 0) {
 916                pr_err("Could not allocate memory for cmap.\n");
 917                goto exit;
 918        }
 919
 920        pr_debug("#2 show info->cmap :\nstart=%d,len=%d,red=%p,green=%p,blue=%p,transp=%p\n",
 921                 info->cmap.start, info->cmap.len,
 922                 info->cmap.red, info->cmap.green, info->cmap.blue,
 923                 info->cmap.transp);
 924
 925exit:
 926        lynxfb_ops_check_var(var, info);
 927        return ret;
 928}
 929
 930/*      chip specific g_option configuration routine */
 931static void sm750fb_setup(struct sm750_dev *sm750_dev, char *src)
 932{
 933        char *opt;
 934        int swap;
 935
 936        swap = 0;
 937
 938        sm750_dev->initParm.chip_clk = 0;
 939        sm750_dev->initParm.mem_clk = 0;
 940        sm750_dev->initParm.master_clk = 0;
 941        sm750_dev->initParm.powerMode = 0;
 942        sm750_dev->initParm.setAllEngOff = 0;
 943        sm750_dev->initParm.resetMemory = 1;
 944
 945        /* defaultly turn g_hwcursor on for both view */
 946        g_hwcursor = 3;
 947
 948        if (!src || !*src) {
 949                dev_warn(&sm750_dev->pdev->dev, "no specific g_option.\n");
 950                goto NO_PARAM;
 951        }
 952
 953        while ((opt = strsep(&src, ":")) != NULL && *opt != 0) {
 954                dev_info(&sm750_dev->pdev->dev, "opt=%s\n", opt);
 955                dev_info(&sm750_dev->pdev->dev, "src=%s\n", src);
 956
 957                if (!strncmp(opt, "swap", strlen("swap")))
 958                        swap = 1;
 959                else if (!strncmp(opt, "nocrt", strlen("nocrt")))
 960                        sm750_dev->nocrt = 1;
 961                else if (!strncmp(opt, "36bit", strlen("36bit")))
 962                        sm750_dev->pnltype = sm750_doubleTFT;
 963                else if (!strncmp(opt, "18bit", strlen("18bit")))
 964                        sm750_dev->pnltype = sm750_dualTFT;
 965                else if (!strncmp(opt, "24bit", strlen("24bit")))
 966                        sm750_dev->pnltype = sm750_24TFT;
 967                else if (!strncmp(opt, "nohwc0", strlen("nohwc0")))
 968                        g_hwcursor &= ~0x1;
 969                else if (!strncmp(opt, "nohwc1", strlen("nohwc1")))
 970                        g_hwcursor &= ~0x2;
 971                else if (!strncmp(opt, "nohwc", strlen("nohwc")))
 972                        g_hwcursor = 0;
 973                else {
 974                        if (!g_fbmode[0]) {
 975                                g_fbmode[0] = opt;
 976                                dev_info(&sm750_dev->pdev->dev,
 977                                         "find fbmode0 : %s\n", g_fbmode[0]);
 978                        } else if (!g_fbmode[1]) {
 979                                g_fbmode[1] = opt;
 980                                dev_info(&sm750_dev->pdev->dev,
 981                                         "find fbmode1 : %s\n", g_fbmode[1]);
 982                        } else {
 983                                dev_warn(&sm750_dev->pdev->dev, "How many view you wann set?\n");
 984                        }
 985                }
 986        }
 987
 988NO_PARAM:
 989        if (sm750_dev->revid != SM750LE_REVISION_ID) {
 990                if (sm750_dev->fb_count > 1) {
 991                        if (swap)
 992                                sm750_dev->dataflow = sm750_dual_swap;
 993                        else
 994                                sm750_dev->dataflow = sm750_dual_normal;
 995                } else {
 996                        if (swap)
 997                                sm750_dev->dataflow = sm750_simul_sec;
 998                        else
 999                                sm750_dev->dataflow = sm750_simul_pri;
1000                }
1001        } else {
1002                /* SM750LE only have one crt channel */
1003                sm750_dev->dataflow = sm750_simul_sec;
1004                /* sm750le do not have complex attributes */
1005                sm750_dev->nocrt = 0;
1006        }
1007}
1008
1009static void sm750fb_frambuffer_release(struct sm750_dev *sm750_dev)
1010{
1011        struct fb_info *fb_info;
1012
1013        while (sm750_dev->fb_count) {
1014                fb_info = sm750_dev->fbinfo[sm750_dev->fb_count - 1];
1015                unregister_framebuffer(fb_info);
1016                framebuffer_release(fb_info);
1017                sm750_dev->fb_count--;
1018        }
1019}
1020
1021static int sm750fb_frambuffer_alloc(struct sm750_dev *sm750_dev, int fbidx)
1022{
1023        struct fb_info *fb_info;
1024        struct lynxfb_par *par;
1025        int err;
1026
1027        fb_info = framebuffer_alloc(sizeof(struct lynxfb_par),
1028                                    &sm750_dev->pdev->dev);
1029        if (!fb_info)
1030                return -ENOMEM;
1031
1032        sm750_dev->fbinfo[fbidx] = fb_info;
1033        par = fb_info->par;
1034        par->dev = sm750_dev;
1035
1036        err = lynxfb_set_fbinfo(fb_info, fbidx);
1037        if (err)
1038                goto release_fb;
1039
1040        err = register_framebuffer(fb_info);
1041        if (err < 0)
1042                goto release_fb;
1043
1044        sm750_dev->fb_count++;
1045
1046        return 0;
1047
1048release_fb:
1049        framebuffer_release(fb_info);
1050        return err;
1051}
1052
1053static int lynxfb_pci_probe(struct pci_dev *pdev,
1054                            const struct pci_device_id *ent)
1055{
1056        struct sm750_dev *sm750_dev = NULL;
1057        int max_fb;
1058        int fbidx;
1059        int err;
1060
1061        /* enable device */
1062        err = pcim_enable_device(pdev);
1063        if (err)
1064                return err;
1065
1066        err = -ENOMEM;
1067        sm750_dev = devm_kzalloc(&pdev->dev, sizeof(*sm750_dev), GFP_KERNEL);
1068        if (!sm750_dev)
1069                return err;
1070
1071        sm750_dev->fbinfo[0] = sm750_dev->fbinfo[1] = NULL;
1072        sm750_dev->devid = pdev->device;
1073        sm750_dev->revid = pdev->revision;
1074        sm750_dev->pdev = pdev;
1075        sm750_dev->mtrr_off = g_nomtrr;
1076        sm750_dev->mtrr.vram = 0;
1077        sm750_dev->accel_off = g_noaccel;
1078        spin_lock_init(&sm750_dev->slock);
1079
1080        if (!sm750_dev->accel_off) {
1081                /*
1082                 * hook deInit and 2d routines, notes that below hw_xxx
1083                 * routine can work on most of lynx chips
1084                 * if some chip need specific function,
1085                 * please hook it in smXXX_set_drv routine
1086                 */
1087                sm750_dev->accel.de_init = sm750_hw_de_init;
1088                sm750_dev->accel.de_fillrect = sm750_hw_fillrect;
1089                sm750_dev->accel.de_copyarea = sm750_hw_copyarea;
1090                sm750_dev->accel.de_imageblit = sm750_hw_imageblit;
1091        }
1092
1093        /* call chip specific setup routine  */
1094        sm750fb_setup(sm750_dev, g_settings);
1095
1096        /* call chip specific mmap routine */
1097        err = hw_sm750_map(sm750_dev, pdev);
1098        if (err)
1099                return err;
1100
1101        if (!sm750_dev->mtrr_off)
1102                sm750_dev->mtrr.vram = arch_phys_wc_add(sm750_dev->vidmem_start,
1103                                                        sm750_dev->vidmem_size);
1104
1105        memset_io(sm750_dev->pvMem, 0, sm750_dev->vidmem_size);
1106
1107        pci_set_drvdata(pdev, sm750_dev);
1108
1109        /* call chipInit routine */
1110        hw_sm750_inithw(sm750_dev, pdev);
1111
1112        /* allocate frame buffer info structures according to g_dualview */
1113        max_fb = g_dualview ? 2 : 1;
1114        for (fbidx = 0; fbidx < max_fb; fbidx++) {
1115                err = sm750fb_frambuffer_alloc(sm750_dev, fbidx);
1116                if (err)
1117                        goto release_fb;
1118        }
1119
1120        return 0;
1121
1122release_fb:
1123        sm750fb_frambuffer_release(sm750_dev);
1124        return err;
1125}
1126
1127static void lynxfb_pci_remove(struct pci_dev *pdev)
1128{
1129        struct sm750_dev *sm750_dev;
1130
1131        sm750_dev = pci_get_drvdata(pdev);
1132
1133        sm750fb_frambuffer_release(sm750_dev);
1134        arch_phys_wc_del(sm750_dev->mtrr.vram);
1135
1136        iounmap(sm750_dev->pvReg);
1137        iounmap(sm750_dev->pvMem);
1138        kfree(g_settings);
1139}
1140
1141static int __init lynxfb_setup(char *options)
1142{
1143        int len;
1144        char *opt, *tmp;
1145
1146        if (!options || !*options) {
1147                pr_warn("no options.\n");
1148                return 0;
1149        }
1150
1151        pr_info("options:%s\n", options);
1152
1153        len = strlen(options) + 1;
1154        g_settings = kzalloc(len, GFP_KERNEL);
1155        if (!g_settings)
1156                return -ENOMEM;
1157
1158        tmp = g_settings;
1159
1160        /*
1161         * Notes:
1162         * char * strsep(char **s,const char * ct);
1163         * @s: the string to be searched
1164         * @ct :the characters to search for
1165         *
1166         * strsep() updates @options to pointer after the first found token
1167         * it also returns the pointer ahead the token.
1168         */
1169        while ((opt = strsep(&options, ":")) != NULL) {
1170                /* options that mean for any lynx chips are configured here */
1171                if (!strncmp(opt, "noaccel", strlen("noaccel")))
1172                        g_noaccel = 1;
1173                else if (!strncmp(opt, "nomtrr", strlen("nomtrr")))
1174                        g_nomtrr = 1;
1175                else if (!strncmp(opt, "dual", strlen("dual")))
1176                        g_dualview = 1;
1177                else {
1178                        strcat(tmp, opt);
1179                        tmp += strlen(opt);
1180                        if (options)
1181                                *tmp++ = ':';
1182                        else
1183                                *tmp++ = 0;
1184                }
1185        }
1186
1187        /* misc g_settings are transport to chip specific routines */
1188        pr_info("parameter left for chip specific analysis:%s\n", g_settings);
1189        return 0;
1190}
1191
1192static const struct pci_device_id smi_pci_table[] = {
1193        { PCI_DEVICE(0x126f, 0x0750), },
1194        {0,}
1195};
1196
1197MODULE_DEVICE_TABLE(pci, smi_pci_table);
1198
1199static struct pci_driver lynxfb_driver = {
1200        .name =         "sm750fb",
1201        .id_table =     smi_pci_table,
1202        .probe =        lynxfb_pci_probe,
1203        .remove =       lynxfb_pci_remove,
1204#ifdef CONFIG_PM
1205        .suspend = lynxfb_suspend,
1206        .resume = lynxfb_resume,
1207#endif
1208};
1209
1210static int __init lynxfb_init(void)
1211{
1212        char *option;
1213
1214#ifdef MODULE
1215        option = g_option;
1216#else
1217        if (fb_get_options("sm750fb", &option))
1218                return -ENODEV;
1219#endif
1220
1221        lynxfb_setup(option);
1222        return pci_register_driver(&lynxfb_driver);
1223}
1224module_init(lynxfb_init);
1225
1226static void __exit lynxfb_exit(void)
1227{
1228        pci_unregister_driver(&lynxfb_driver);
1229}
1230module_exit(lynxfb_exit);
1231
1232module_param(g_option, charp, 0444);
1233
1234MODULE_PARM_DESC(g_option,
1235                 "\n\t\tCommon options:\n"
1236                 "\t\tnoaccel:disable 2d capabilities\n"
1237                 "\t\tnomtrr:disable MTRR attribute for video memory\n"
1238                 "\t\tdualview:dual frame buffer feature enabled\n"
1239                 "\t\tnohwc:disable hardware cursor\n"
1240                 "\t\tUsual example:\n"
1241                 "\t\tinsmod ./sm750fb.ko g_option=\"noaccel,nohwc,1280x1024-8@60\"\n"
1242                 );
1243
1244MODULE_AUTHOR("monk liu <monk.liu@siliconmotion.com>");
1245MODULE_AUTHOR("Sudip Mukherjee <sudip@vectorindia.org>");
1246MODULE_DESCRIPTION("Frame buffer driver for SM750 chipset");
1247MODULE_LICENSE("Dual BSD/GPL");
1248