Staging: merge staging patches into Linus's main branch
[sfrench/cifs-2.6.git] / drivers / staging / sm7xx / smtcfb.c
1 /*
2  * Silicon Motion SM7XX frame buffer device
3  *
4  * Copyright (C) 2006 Silicon Motion Technology Corp.
5  * Authors: Ge Wang, gewang@siliconmotion.com
6  *          Boyod boyod.yang@siliconmotion.com.cn
7  *
8  * Copyright (C) 2009 Lemote, Inc.
9  * Author: Wu Zhangjin, wuzhangjin@gmail.com
10  *
11  *  This file is subject to the terms and conditions of the GNU General Public
12  *  License. See the file COPYING in the main directory of this archive for
13  *  more details.
14  *
15  * Version 0.10.26192.21.01
16  *      - Add PowerPC/Big endian support
17  *      - Add 2D support for Lynx
18  *      - Verified on2.6.19.2  Boyod.yang <boyod.yang@siliconmotion.com.cn>
19  *
20  * Version 0.09.2621.00.01
21  *      - Only support Linux Kernel's version 2.6.21.
22  *      Boyod.yang  <boyod.yang@siliconmotion.com.cn>
23  *
24  * Version 0.09
25  *      - Only support Linux Kernel's version 2.6.12.
26  *      Boyod.yang <boyod.yang@siliconmotion.com.cn>
27  */
28
29 #ifndef __KERNEL__
30 #define __KERNEL__
31 #endif
32
33 #include <linux/io.h>
34 #include <linux/fb.h>
35 #include <linux/pci.h>
36 #include <linux/init.h>
37 #include <linux/uaccess.h>
38 #include <linux/console.h>
39 #include <linux/screen_info.h>
40
41 #ifdef CONFIG_PM
42 #include <linux/pm.h>
43 #endif
44
45 struct screen_info smtc_screen_info;
46
47 #include "smtcfb.h"
48
49 #ifdef DEBUG
50 #define smdbg(format, arg...)   printk(KERN_DEBUG format , ## arg)
51 #else
52 #define smdbg(format, arg...)
53 #endif
54
55 /*
56 * Private structure
57 */
58 struct smtcfb_info {
59         /*
60          * The following is a pointer to be passed into the
61          * functions below.  The modules outside the main
62          * voyager.c driver have no knowledge as to what
63          * is within this structure.
64          */
65         struct fb_info fb;
66         struct display_switch *dispsw;
67         struct pci_dev *dev;
68         signed int currcon;
69
70         struct {
71                 u8 red, green, blue;
72         } palette[NR_RGB];
73
74         u_int palette_size;
75 };
76
77 struct par_info {
78         /*
79          * Hardware
80          */
81         u16 chipID;
82         unsigned char __iomem *m_pMMIO;
83         char __iomem *m_pLFB;
84         char *m_pDPR;
85         char *m_pVPR;
86         char *m_pCPR;
87
88         u_int width;
89         u_int height;
90         u_int hz;
91         u_long BaseAddressInVRAM;
92         u8 chipRevID;
93 };
94
95 struct vesa_mode_table  {
96         char mode_index[6];
97         u16 lfb_width;
98         u16 lfb_height;
99         u16 lfb_depth;
100 };
101
102 static struct vesa_mode_table vesa_mode[] = {
103         {"0x301", 640,  480,  8},
104         {"0x303", 800,  600,  8},
105         {"0x305", 1024, 768,    8},
106         {"0x307", 1280, 1024, 8},
107
108         {"0x311", 640,  480,  16},
109         {"0x314", 800,  600,  16},
110         {"0x317", 1024, 768,    16},
111         {"0x31A", 1280, 1024, 16},
112
113         {"0x312", 640,  480,  24},
114         {"0x315", 800,  600,  24},
115         {"0x318", 1024, 768,    24},
116         {"0x31B", 1280, 1024, 24},
117 };
118
119 char __iomem *smtc_RegBaseAddress;      /* Memory Map IO starting address */
120 char __iomem *smtc_VRAMBaseAddress;     /* video memory starting address */
121
122 static u32 colreg[17];
123 static struct par_info hw;      /* hardware information */
124
125 u16 smtc_ChipIDs[] = {
126         0x710,
127         0x712,
128         0x720
129 };
130
131 #define numSMTCchipIDs (sizeof(smtc_ChipIDs) / sizeof(u16))
132
133 static void sm712_set_timing(struct smtcfb_info *sfb,
134                              struct par_info *ppar_info)
135 {
136         int i = 0, j = 0;
137         u32 m_nScreenStride;
138
139         smdbg("\nppar_info->width = %d ppar_info->height = %d"
140                         "sfb->fb.var.bits_per_pixel = %d ppar_info->hz = %d\n",
141                         ppar_info->width, ppar_info->height,
142                         sfb->fb.var.bits_per_pixel, ppar_info->hz);
143
144         for (j = 0; j < numVGAModes; j++) {
145                 if (VGAMode[j].mmSizeX == ppar_info->width &&
146                     VGAMode[j].mmSizeY == ppar_info->height &&
147                     VGAMode[j].bpp == sfb->fb.var.bits_per_pixel &&
148                     VGAMode[j].hz == ppar_info->hz) {
149
150                         smdbg("\nVGAMode[j].mmSizeX  = %d VGAMode[j].mmSizeY ="
151                                         "%d VGAMode[j].bpp = %d"
152                                         "VGAMode[j].hz=%d\n",
153                                         VGAMode[j].mmSizeX, VGAMode[j].mmSizeY,
154                                         VGAMode[j].bpp, VGAMode[j].hz);
155
156                         smdbg("VGAMode index=%d\n", j);
157
158                         smtc_mmiowb(0x0, 0x3c6);
159
160                         smtc_seqw(0, 0x1);
161
162                         smtc_mmiowb(VGAMode[j].Init_MISC, 0x3c2);
163
164                         /* init SEQ register SR00 - SR04 */
165                         for (i = 0; i < SIZE_SR00_SR04; i++)
166                                 smtc_seqw(i, VGAMode[j].Init_SR00_SR04[i]);
167
168                         /* init SEQ register SR10 - SR24 */
169                         for (i = 0; i < SIZE_SR10_SR24; i++)
170                                 smtc_seqw(i + 0x10,
171                                           VGAMode[j].Init_SR10_SR24[i]);
172
173                         /* init SEQ register SR30 - SR75 */
174                         for (i = 0; i < SIZE_SR30_SR75; i++)
175                                 if (((i + 0x30) != 0x62) \
176                                         && ((i + 0x30) != 0x6a) \
177                                         && ((i + 0x30) != 0x6b))
178                                         smtc_seqw(i + 0x30,
179                                                 VGAMode[j].Init_SR30_SR75[i]);
180
181                         /* init SEQ register SR80 - SR93 */
182                         for (i = 0; i < SIZE_SR80_SR93; i++)
183                                 smtc_seqw(i + 0x80,
184                                           VGAMode[j].Init_SR80_SR93[i]);
185
186                         /* init SEQ register SRA0 - SRAF */
187                         for (i = 0; i < SIZE_SRA0_SRAF; i++)
188                                 smtc_seqw(i + 0xa0,
189                                           VGAMode[j].Init_SRA0_SRAF[i]);
190
191                         /* init Graphic register GR00 - GR08 */
192                         for (i = 0; i < SIZE_GR00_GR08; i++)
193                                 smtc_grphw(i, VGAMode[j].Init_GR00_GR08[i]);
194
195                         /* init Attribute register AR00 - AR14 */
196                         for (i = 0; i < SIZE_AR00_AR14; i++)
197                                 smtc_attrw(i, VGAMode[j].Init_AR00_AR14[i]);
198
199                         /* init CRTC register CR00 - CR18 */
200                         for (i = 0; i < SIZE_CR00_CR18; i++)
201                                 smtc_crtcw(i, VGAMode[j].Init_CR00_CR18[i]);
202
203                         /* init CRTC register CR30 - CR4D */
204                         for (i = 0; i < SIZE_CR30_CR4D; i++)
205                                 smtc_crtcw(i + 0x30,
206                                            VGAMode[j].Init_CR30_CR4D[i]);
207
208                         /* init CRTC register CR90 - CRA7 */
209                         for (i = 0; i < SIZE_CR90_CRA7; i++)
210                                 smtc_crtcw(i + 0x90,
211                                            VGAMode[j].Init_CR90_CRA7[i]);
212                 }
213         }
214         smtc_mmiowb(0x67, 0x3c2);
215
216         /* set VPR registers */
217         writel(0x0, ppar_info->m_pVPR + 0x0C);
218         writel(0x0, ppar_info->m_pVPR + 0x40);
219
220         /* set data width */
221         m_nScreenStride =
222                 (ppar_info->width * sfb->fb.var.bits_per_pixel) / 64;
223         switch (sfb->fb.var.bits_per_pixel) {
224         case 8:
225                 writel(0x0, ppar_info->m_pVPR + 0x0);
226                 break;
227         case 16:
228                 writel(0x00020000, ppar_info->m_pVPR + 0x0);
229                 break;
230         case 24:
231                 writel(0x00040000, ppar_info->m_pVPR + 0x0);
232                 break;
233         case 32:
234                 writel(0x00030000, ppar_info->m_pVPR + 0x0);
235                 break;
236         }
237         writel((u32) (((m_nScreenStride + 2) << 16) | m_nScreenStride),
238                ppar_info->m_pVPR + 0x10);
239
240 }
241
242 static void sm712_setpalette(int regno, unsigned red, unsigned green,
243                              unsigned blue, struct fb_info *info)
244 {
245         struct par_info *cur_par = (struct par_info *)info->par;
246
247         if (cur_par->BaseAddressInVRAM)
248                 /*
249                  * second display palette for dual head. Enable CRT RAM, 6-bit
250                  * RAM
251                  */
252                 smtc_seqw(0x66, (smtc_seqr(0x66) & 0xC3) | 0x20);
253         else
254                 /* primary display palette. Enable LCD RAM only, 6-bit RAM */
255                 smtc_seqw(0x66, (smtc_seqr(0x66) & 0xC3) | 0x10);
256         smtc_mmiowb(regno, dac_reg);
257         smtc_mmiowb(red >> 10, dac_val);
258         smtc_mmiowb(green >> 10, dac_val);
259         smtc_mmiowb(blue >> 10, dac_val);
260 }
261
262 static void smtc_set_timing(struct smtcfb_info *sfb, struct par_info
263                 *ppar_info)
264 {
265         switch (ppar_info->chipID) {
266         case 0x710:
267         case 0x712:
268         case 0x720:
269                 sm712_set_timing(sfb, ppar_info);
270                 break;
271         }
272 }
273
274 static struct fb_var_screeninfo smtcfb_var = {
275         .xres = 1024,
276         .yres = 600,
277         .xres_virtual = 1024,
278         .yres_virtual = 600,
279         .bits_per_pixel = 16,
280         .red = {16, 8, 0},
281         .green = {8, 8, 0},
282         .blue = {0, 8, 0},
283         .activate = FB_ACTIVATE_NOW,
284         .height = -1,
285         .width = -1,
286         .vmode = FB_VMODE_NONINTERLACED,
287 };
288
289 static struct fb_fix_screeninfo smtcfb_fix = {
290         .id = "sm712fb",
291         .type = FB_TYPE_PACKED_PIXELS,
292         .visual = FB_VISUAL_TRUECOLOR,
293         .line_length = 800 * 3,
294         .accel = FB_ACCEL_SMI_LYNX,
295 };
296
297 /* chan_to_field
298  *
299  * convert a colour value into a field position
300  *
301  * from pxafb.c
302  */
303
304 static inline unsigned int chan_to_field(unsigned int chan,
305                                          struct fb_bitfield *bf)
306 {
307         chan &= 0xffff;
308         chan >>= 16 - bf->length;
309         return chan << bf->offset;
310 }
311
312 static int cfb_blank(int blank_mode, struct fb_info *info)
313 {
314         /* clear DPMS setting */
315         switch (blank_mode) {
316         case FB_BLANK_UNBLANK:
317                 /* Screen On: HSync: On, VSync : On */
318                 smtc_seqw(0x01, (smtc_seqr(0x01) & (~0x20)));
319                 smtc_seqw(0x6a, 0x16);
320                 smtc_seqw(0x6b, 0x02);
321                 smtc_seqw(0x21, (smtc_seqr(0x21) & 0x77));
322                 smtc_seqw(0x22, (smtc_seqr(0x22) & (~0x30)));
323                 smtc_seqw(0x23, (smtc_seqr(0x23) & (~0xc0)));
324                 smtc_seqw(0x24, (smtc_seqr(0x24) | 0x01));
325                 smtc_seqw(0x31, (smtc_seqr(0x31) | 0x03));
326                 break;
327         case FB_BLANK_NORMAL:
328                 /* Screen Off: HSync: On, VSync : On   Soft blank */
329                 smtc_seqw(0x01, (smtc_seqr(0x01) & (~0x20)));
330                 smtc_seqw(0x6a, 0x16);
331                 smtc_seqw(0x6b, 0x02);
332                 smtc_seqw(0x22, (smtc_seqr(0x22) & (~0x30)));
333                 smtc_seqw(0x23, (smtc_seqr(0x23) & (~0xc0)));
334                 smtc_seqw(0x24, (smtc_seqr(0x24) | 0x01));
335                 smtc_seqw(0x31, ((smtc_seqr(0x31) & (~0x07)) | 0x00));
336                 break;
337         case FB_BLANK_VSYNC_SUSPEND:
338                 /* Screen On: HSync: On, VSync : Off */
339                 smtc_seqw(0x01, (smtc_seqr(0x01) | 0x20));
340                 smtc_seqw(0x20, (smtc_seqr(0x20) & (~0xB0)));
341                 smtc_seqw(0x6a, 0x0c);
342                 smtc_seqw(0x6b, 0x02);
343                 smtc_seqw(0x21, (smtc_seqr(0x21) | 0x88));
344                 smtc_seqw(0x22, ((smtc_seqr(0x22) & (~0x30)) | 0x20));
345                 smtc_seqw(0x23, ((smtc_seqr(0x23) & (~0xc0)) | 0x20));
346                 smtc_seqw(0x24, (smtc_seqr(0x24) & (~0x01)));
347                 smtc_seqw(0x31, ((smtc_seqr(0x31) & (~0x07)) | 0x00));
348                 smtc_seqw(0x34, (smtc_seqr(0x34) | 0x80));
349                 break;
350         case FB_BLANK_HSYNC_SUSPEND:
351                 /* Screen On: HSync: Off, VSync : On */
352                 smtc_seqw(0x01, (smtc_seqr(0x01) | 0x20));
353                 smtc_seqw(0x20, (smtc_seqr(0x20) & (~0xB0)));
354                 smtc_seqw(0x6a, 0x0c);
355                 smtc_seqw(0x6b, 0x02);
356                 smtc_seqw(0x21, (smtc_seqr(0x21) | 0x88));
357                 smtc_seqw(0x22, ((smtc_seqr(0x22) & (~0x30)) | 0x10));
358                 smtc_seqw(0x23, ((smtc_seqr(0x23) & (~0xc0)) | 0xD8));
359                 smtc_seqw(0x24, (smtc_seqr(0x24) & (~0x01)));
360                 smtc_seqw(0x31, ((smtc_seqr(0x31) & (~0x07)) | 0x00));
361                 smtc_seqw(0x34, (smtc_seqr(0x34) | 0x80));
362                 break;
363         case FB_BLANK_POWERDOWN:
364                 /* Screen On: HSync: Off, VSync : Off */
365                 smtc_seqw(0x01, (smtc_seqr(0x01) | 0x20));
366                 smtc_seqw(0x20, (smtc_seqr(0x20) & (~0xB0)));
367                 smtc_seqw(0x6a, 0x0c);
368                 smtc_seqw(0x6b, 0x02);
369                 smtc_seqw(0x21, (smtc_seqr(0x21) | 0x88));
370                 smtc_seqw(0x22, ((smtc_seqr(0x22) & (~0x30)) | 0x30));
371                 smtc_seqw(0x23, ((smtc_seqr(0x23) & (~0xc0)) | 0xD8));
372                 smtc_seqw(0x24, (smtc_seqr(0x24) & (~0x01)));
373                 smtc_seqw(0x31, ((smtc_seqr(0x31) & (~0x07)) | 0x00));
374                 smtc_seqw(0x34, (smtc_seqr(0x34) | 0x80));
375                 break;
376         default:
377                 return -EINVAL;
378         }
379
380         return 0;
381 }
382
383 static int smtc_setcolreg(unsigned regno, unsigned red, unsigned green,
384                           unsigned blue, unsigned trans, struct fb_info *info)
385 {
386         struct smtcfb_info *sfb = (struct smtcfb_info *)info;
387         u32 val;
388
389         if (regno > 255)
390                 return 1;
391
392         switch (sfb->fb.fix.visual) {
393         case FB_VISUAL_DIRECTCOLOR:
394         case FB_VISUAL_TRUECOLOR:
395                 /*
396                  * 16/32 bit true-colour, use pseuo-palette for 16 base color
397                  */
398                 if (regno < 16) {
399                         if (sfb->fb.var.bits_per_pixel == 16) {
400                                 u32 *pal = sfb->fb.pseudo_palette;
401                                 val = chan_to_field(red, &sfb->fb.var.red);
402                                 val |= chan_to_field(green, \
403                                                 &sfb->fb.var.green);
404                                 val |= chan_to_field(blue, &sfb->fb.var.blue);
405 #ifdef __BIG_ENDIAN
406                                 pal[regno] =
407                                     ((red & 0xf800) >> 8) |
408                                     ((green & 0xe000) >> 13) |
409                                     ((green & 0x1c00) << 3) |
410                                     ((blue & 0xf800) >> 3);
411 #else
412                                 pal[regno] = val;
413 #endif
414                         } else {
415                                 u32 *pal = sfb->fb.pseudo_palette;
416                                 val = chan_to_field(red, &sfb->fb.var.red);
417                                 val |= chan_to_field(green, \
418                                                 &sfb->fb.var.green);
419                                 val |= chan_to_field(blue, &sfb->fb.var.blue);
420 #ifdef __BIG_ENDIAN
421                                 val =
422                                     (val & 0xff00ff00 >> 8) |
423                                     (val & 0x00ff00ff << 8);
424 #endif
425                                 pal[regno] = val;
426                         }
427                 }
428                 break;
429
430         case FB_VISUAL_PSEUDOCOLOR:
431                 /* color depth 8 bit */
432                 sm712_setpalette(regno, red, green, blue, info);
433                 break;
434
435         default:
436                 return 1;       /* unknown type */
437         }
438
439         return 0;
440
441 }
442
443 #ifdef __BIG_ENDIAN
444 static ssize_t smtcfb_read(struct fb_info *info, char __user * buf, size_t
445                                 count, loff_t *ppos)
446 {
447         unsigned long p = *ppos;
448
449         u32 *buffer, *dst;
450         u32 __iomem *src;
451         int c, i, cnt = 0, err = 0;
452         unsigned long total_size;
453
454         if (!info || !info->screen_base)
455                 return -ENODEV;
456
457         if (info->state != FBINFO_STATE_RUNNING)
458                 return -EPERM;
459
460         total_size = info->screen_size;
461
462         if (total_size == 0)
463                 total_size = info->fix.smem_len;
464
465         if (p >= total_size)
466                 return 0;
467
468         if (count >= total_size)
469                 count = total_size;
470
471         if (count + p > total_size)
472                 count = total_size - p;
473
474         buffer = kmalloc((count > PAGE_SIZE) ? PAGE_SIZE : count, GFP_KERNEL);
475         if (!buffer)
476                 return -ENOMEM;
477
478         src = (u32 __iomem *) (info->screen_base + p);
479
480         if (info->fbops->fb_sync)
481                 info->fbops->fb_sync(info);
482
483         while (count) {
484                 c = (count > PAGE_SIZE) ? PAGE_SIZE : count;
485                 dst = buffer;
486                 for (i = c >> 2; i--;) {
487                         *dst = fb_readl(src++);
488                         *dst =
489                             (*dst & 0xff00ff00 >> 8) |
490                             (*dst & 0x00ff00ff << 8);
491                         dst++;
492                 }
493                 if (c & 3) {
494                         u8 *dst8 = (u8 *) dst;
495                         u8 __iomem *src8 = (u8 __iomem *) src;
496
497                         for (i = c & 3; i--;) {
498                                 if (i & 1) {
499                                         *dst8++ = fb_readb(++src8);
500                                 } else {
501                                         *dst8++ = fb_readb(--src8);
502                                         src8 += 2;
503                                 }
504                         }
505                         src = (u32 __iomem *) src8;
506                 }
507
508                 if (copy_to_user(buf, buffer, c)) {
509                         err = -EFAULT;
510                         break;
511                 }
512                 *ppos += c;
513                 buf += c;
514                 cnt += c;
515                 count -= c;
516         }
517
518         kfree(buffer);
519
520         return (err) ? err : cnt;
521 }
522
523 static ssize_t
524 smtcfb_write(struct fb_info *info, const char __user *buf, size_t count,
525              loff_t *ppos)
526 {
527         unsigned long p = *ppos;
528
529         u32 *buffer, *src;
530         u32 __iomem *dst;
531         int c, i, cnt = 0, err = 0;
532         unsigned long total_size;
533
534         if (!info || !info->screen_base)
535                 return -ENODEV;
536
537         if (info->state != FBINFO_STATE_RUNNING)
538                 return -EPERM;
539
540         total_size = info->screen_size;
541
542         if (total_size == 0)
543                 total_size = info->fix.smem_len;
544
545         if (p > total_size)
546                 return -EFBIG;
547
548         if (count > total_size) {
549                 err = -EFBIG;
550                 count = total_size;
551         }
552
553         if (count + p > total_size) {
554                 if (!err)
555                         err = -ENOSPC;
556
557                 count = total_size - p;
558         }
559
560         buffer = kmalloc((count > PAGE_SIZE) ? PAGE_SIZE : count, GFP_KERNEL);
561         if (!buffer)
562                 return -ENOMEM;
563
564         dst = (u32 __iomem *) (info->screen_base + p);
565
566         if (info->fbops->fb_sync)
567                 info->fbops->fb_sync(info);
568
569         while (count) {
570                 c = (count > PAGE_SIZE) ? PAGE_SIZE : count;
571                 src = buffer;
572
573                 if (copy_from_user(src, buf, c)) {
574                         err = -EFAULT;
575                         break;
576                 }
577
578                 for (i = c >> 2; i--;) {
579                         fb_writel((*src & 0xff00ff00 >> 8) |
580                                   (*src & 0x00ff00ff << 8), dst++);
581                         src++;
582                 }
583                 if (c & 3) {
584                         u8 *src8 = (u8 *) src;
585                         u8 __iomem *dst8 = (u8 __iomem *) dst;
586
587                         for (i = c & 3; i--;) {
588                                 if (i & 1) {
589                                         fb_writeb(*src8++, ++dst8);
590                                 } else {
591                                         fb_writeb(*src8++, --dst8);
592                                         dst8 += 2;
593                                 }
594                         }
595                         dst = (u32 __iomem *) dst8;
596                 }
597
598                 *ppos += c;
599                 buf += c;
600                 cnt += c;
601                 count -= c;
602         }
603
604         kfree(buffer);
605
606         return (cnt) ? cnt : err;
607 }
608 #endif  /* ! __BIG_ENDIAN */
609
610 static struct fb_ops smtcfb_ops = {
611         .owner = THIS_MODULE,
612         .fb_setcolreg = smtc_setcolreg,
613         .fb_blank = cfb_blank,
614         .fb_fillrect = cfb_fillrect,
615         .fb_imageblit = cfb_imageblit,
616         .fb_copyarea = cfb_copyarea,
617 #ifdef __BIG_ENDIAN
618         .fb_read = smtcfb_read,
619         .fb_write = smtcfb_write,
620 #endif
621
622 };
623
624 void smtcfb_setmode(struct smtcfb_info *sfb)
625 {
626         switch (sfb->fb.var.bits_per_pixel) {
627         case 32:
628                 sfb->fb.fix.visual = FB_VISUAL_TRUECOLOR;
629                 sfb->fb.fix.line_length = sfb->fb.var.xres * 4;
630                 sfb->fb.var.red.length = 8;
631                 sfb->fb.var.green.length = 8;
632                 sfb->fb.var.blue.length = 8;
633                 sfb->fb.var.red.offset = 16;
634                 sfb->fb.var.green.offset = 8;
635                 sfb->fb.var.blue.offset = 0;
636
637                 break;
638         case 8:
639                 sfb->fb.fix.visual = FB_VISUAL_PSEUDOCOLOR;
640                 sfb->fb.fix.line_length = sfb->fb.var.xres;
641                 sfb->fb.var.red.offset = 5;
642                 sfb->fb.var.red.length = 3;
643                 sfb->fb.var.green.offset = 2;
644                 sfb->fb.var.green.length = 3;
645                 sfb->fb.var.blue.offset = 0;
646                 sfb->fb.var.blue.length = 2;
647                 break;
648         case 24:
649                 sfb->fb.fix.visual = FB_VISUAL_TRUECOLOR;
650                 sfb->fb.fix.line_length = sfb->fb.var.xres * 3;
651                 sfb->fb.var.red.length = 8;
652                 sfb->fb.var.green.length = 8;
653                 sfb->fb.var.blue.length = 8;
654
655                 sfb->fb.var.red.offset = 16;
656                 sfb->fb.var.green.offset = 8;
657                 sfb->fb.var.blue.offset = 0;
658
659                 break;
660         case 16:
661         default:
662                 sfb->fb.fix.visual = FB_VISUAL_TRUECOLOR;
663                 sfb->fb.fix.line_length = sfb->fb.var.xres * 2;
664
665                 sfb->fb.var.red.length = 5;
666                 sfb->fb.var.green.length = 6;
667                 sfb->fb.var.blue.length = 5;
668
669                 sfb->fb.var.red.offset = 11;
670                 sfb->fb.var.green.offset = 5;
671                 sfb->fb.var.blue.offset = 0;
672
673                 break;
674         }
675
676         hw.width = sfb->fb.var.xres;
677         hw.height = sfb->fb.var.yres;
678         hw.hz = 60;
679         smtc_set_timing(sfb, &hw);
680 }
681
682 /*
683  * Alloc struct smtcfb_info and assign the default value
684  */
685 static struct smtcfb_info *smtc_alloc_fb_info(struct pci_dev *dev,
686                                                         char *name)
687 {
688         struct smtcfb_info *sfb;
689
690         sfb = kmalloc(sizeof(struct smtcfb_info), GFP_KERNEL);
691
692         if (!sfb)
693                 return NULL;
694
695         memset(sfb, 0, sizeof(struct smtcfb_info));
696
697         sfb->currcon = -1;
698         sfb->dev = dev;
699
700         /*** Init sfb->fb with default value ***/
701         sfb->fb.flags = FBINFO_FLAG_DEFAULT;
702         sfb->fb.fbops = &smtcfb_ops;
703         sfb->fb.var = smtcfb_var;
704         sfb->fb.fix = smtcfb_fix;
705
706         strcpy(sfb->fb.fix.id, name);
707
708         sfb->fb.fix.type = FB_TYPE_PACKED_PIXELS;
709         sfb->fb.fix.type_aux = 0;
710         sfb->fb.fix.xpanstep = 0;
711         sfb->fb.fix.ypanstep = 0;
712         sfb->fb.fix.ywrapstep = 0;
713         sfb->fb.fix.accel = FB_ACCEL_SMI_LYNX;
714
715         sfb->fb.var.nonstd = 0;
716         sfb->fb.var.activate = FB_ACTIVATE_NOW;
717         sfb->fb.var.height = -1;
718         sfb->fb.var.width = -1;
719         /* text mode acceleration */
720         sfb->fb.var.accel_flags = FB_ACCELF_TEXT;
721         sfb->fb.var.vmode = FB_VMODE_NONINTERLACED;
722         sfb->fb.par = &hw;
723         sfb->fb.pseudo_palette = colreg;
724
725         return sfb;
726 }
727
728 /*
729  * Unmap in the memory mapped IO registers
730  */
731
732 static void smtc_unmap_mmio(struct smtcfb_info *sfb)
733 {
734         if (sfb && smtc_RegBaseAddress)
735                 smtc_RegBaseAddress = NULL;
736 }
737
738 /*
739  * Map in the screen memory
740  */
741
742 static int smtc_map_smem(struct smtcfb_info *sfb,
743                 struct pci_dev *dev, u_long smem_len)
744 {
745         if (sfb->fb.var.bits_per_pixel == 32) {
746 #ifdef __BIG_ENDIAN
747                 sfb->fb.fix.smem_start = pci_resource_start(dev, 0)
748                         + 0x800000;
749 #else
750                 sfb->fb.fix.smem_start = pci_resource_start(dev, 0);
751 #endif
752         } else {
753                 sfb->fb.fix.smem_start = pci_resource_start(dev, 0);
754         }
755
756         sfb->fb.fix.smem_len = smem_len;
757
758         sfb->fb.screen_base = smtc_VRAMBaseAddress;
759
760         if (!sfb->fb.screen_base) {
761                 printk(KERN_INFO "%s: unable to map screen memory\n",
762                                 sfb->fb.fix.id);
763                 return -ENOMEM;
764         }
765
766         return 0;
767 }
768
769 /*
770  * Unmap in the screen memory
771  *
772  */
773 static void smtc_unmap_smem(struct smtcfb_info *sfb)
774 {
775         if (sfb && sfb->fb.screen_base) {
776                 iounmap(sfb->fb.screen_base);
777                 sfb->fb.screen_base = NULL;
778         }
779 }
780
781 /*
782  * We need to wake up the LynxEM+, and make sure its in linear memory mode.
783  */
784 static inline void sm7xx_init_hw(void)
785 {
786         outb_p(0x18, 0x3c4);
787         outb_p(0x11, 0x3c5);
788 }
789
790 static void smtc_free_fb_info(struct smtcfb_info *sfb)
791 {
792         if (sfb) {
793                 fb_alloc_cmap(&sfb->fb.cmap, 0, 0);
794                 kfree(sfb);
795         }
796 }
797
798 /*
799  *      sm712vga_setup - process command line options, get vga parameter
800  *      @options: string of options
801  *      Returns zero.
802  *
803  */
804 static int __init __maybe_unused sm712vga_setup(char *options)
805 {
806         int index;
807
808         if (!options || !*options) {
809                 smdbg("\n No vga parameter\n");
810                 return -EINVAL;
811         }
812
813         smtc_screen_info.lfb_width = 0;
814         smtc_screen_info.lfb_height = 0;
815         smtc_screen_info.lfb_depth = 0;
816
817         smdbg("\nsm712vga_setup = %s\n", options);
818
819         for (index = 0;
820              index < (sizeof(vesa_mode) / sizeof(struct vesa_mode_table));
821              index++) {
822                 if (strstr(options, vesa_mode[index].mode_index)) {
823                         smtc_screen_info.lfb_width = vesa_mode[index].lfb_width;
824                         smtc_screen_info.lfb_height =
825                                         vesa_mode[index].lfb_height;
826                         smtc_screen_info.lfb_depth = vesa_mode[index].lfb_depth;
827                         return 0;
828                 }
829         }
830
831         return -1;
832 }
833 __setup("vga=", sm712vga_setup);
834
835 /* Jason (08/13/2009)
836  * Original init function changed to probe method to be used by pci_drv
837  * process used to detect chips replaced with kernel process in pci_drv
838  */
839 static int __init smtcfb_pci_probe(struct pci_dev *pdev,
840                                    const struct pci_device_id *ent)
841 {
842         struct smtcfb_info *sfb;
843         u_long smem_size = 0x00800000;  /* default 8MB */
844         char name[16];
845         int err;
846         unsigned long pFramebufferPhysical;
847
848         printk(KERN_INFO
849                 "Silicon Motion display driver " SMTC_LINUX_FB_VERSION "\n");
850
851         err = pci_enable_device(pdev);  /* enable SMTC chip */
852
853         if (err)
854                 return err;
855         err = -ENOMEM;
856
857         hw.chipID = ent->device;
858         sprintf(name, "sm%Xfb", hw.chipID);
859
860         sfb = smtc_alloc_fb_info(pdev, name);
861
862         if (!sfb)
863                 goto failed;
864         /* Jason (08/13/2009)
865          * Store fb_info to be further used when suspending and resuming
866          */
867         pci_set_drvdata(pdev, sfb);
868
869         sm7xx_init_hw();
870
871         /*get mode parameter from smtc_screen_info */
872         if (smtc_screen_info.lfb_width != 0) {
873                 sfb->fb.var.xres = smtc_screen_info.lfb_width;
874                 sfb->fb.var.yres = smtc_screen_info.lfb_height;
875                 sfb->fb.var.bits_per_pixel = smtc_screen_info.lfb_depth;
876         } else {
877                 /* default resolution 1024x600 16bit mode */
878                 sfb->fb.var.xres = SCREEN_X_RES;
879                 sfb->fb.var.yres = SCREEN_Y_RES;
880                 sfb->fb.var.bits_per_pixel = SCREEN_BPP;
881         }
882
883 #ifdef __BIG_ENDIAN
884         if (sfb->fb.var.bits_per_pixel == 24)
885                 sfb->fb.var.bits_per_pixel = (smtc_screen_info.lfb_depth = 32);
886 #endif
887         /* Map address and memory detection */
888         pFramebufferPhysical = pci_resource_start(pdev, 0);
889         pci_read_config_byte(pdev, PCI_REVISION_ID, &hw.chipRevID);
890
891         switch (hw.chipID) {
892         case 0x710:
893         case 0x712:
894                 sfb->fb.fix.mmio_start = pFramebufferPhysical + 0x00400000;
895                 sfb->fb.fix.mmio_len = 0x00400000;
896                 smem_size = SM712_VIDEOMEMORYSIZE;
897 #ifdef __BIG_ENDIAN
898                 hw.m_pLFB = (smtc_VRAMBaseAddress =
899                     ioremap(pFramebufferPhysical, 0x00c00000));
900 #else
901                 hw.m_pLFB = (smtc_VRAMBaseAddress =
902                     ioremap(pFramebufferPhysical, 0x00800000));
903 #endif
904                 hw.m_pMMIO = (smtc_RegBaseAddress =
905                     smtc_VRAMBaseAddress + 0x00700000);
906                 hw.m_pDPR = smtc_VRAMBaseAddress + 0x00408000;
907                 hw.m_pVPR = hw.m_pLFB + 0x0040c000;
908 #ifdef __BIG_ENDIAN
909                 if (sfb->fb.var.bits_per_pixel == 32) {
910                         smtc_VRAMBaseAddress += 0x800000;
911                         hw.m_pLFB += 0x800000;
912                         printk(KERN_INFO
913                                 "\nsmtc_VRAMBaseAddress=%p hw.m_pLFB=%p\n",
914                                         smtc_VRAMBaseAddress, hw.m_pLFB);
915                 }
916 #endif
917                 if (!smtc_RegBaseAddress) {
918                         printk(KERN_INFO
919                                 "%s: unable to map memory mapped IO\n",
920                                 sfb->fb.fix.id);
921                         return -ENOMEM;
922                 }
923
924                 /* set MCLK = 14.31818 * (0x16 / 0x2) */
925                 smtc_seqw(0x6a, 0x16);
926                 smtc_seqw(0x6b, 0x02);
927                 smtc_seqw(0x62, 0x3e);
928                 /* enable PCI burst */
929                 smtc_seqw(0x17, 0x20);
930                 /* enable word swap */
931 #ifdef __BIG_ENDIAN
932                 if (sfb->fb.var.bits_per_pixel == 32)
933                         smtc_seqw(0x17, 0x30);
934 #endif
935                 break;
936         case 0x720:
937                 sfb->fb.fix.mmio_start = pFramebufferPhysical;
938                 sfb->fb.fix.mmio_len = 0x00200000;
939                 smem_size = SM722_VIDEOMEMORYSIZE;
940                 hw.m_pDPR = ioremap(pFramebufferPhysical, 0x00a00000);
941                 hw.m_pLFB = (smtc_VRAMBaseAddress =
942                     hw.m_pDPR + 0x00200000);
943                 hw.m_pMMIO = (smtc_RegBaseAddress =
944                     hw.m_pDPR + 0x000c0000);
945                 hw.m_pVPR = hw.m_pDPR + 0x800;
946
947                 smtc_seqw(0x62, 0xff);
948                 smtc_seqw(0x6a, 0x0d);
949                 smtc_seqw(0x6b, 0x02);
950                 break;
951         default:
952                 printk(KERN_INFO
953                 "No valid Silicon Motion display chip was detected!\n");
954
955                 smtc_free_fb_info(sfb);
956                 return err;
957         }
958
959         /* can support 32 bpp */
960         if (15 == sfb->fb.var.bits_per_pixel)
961                 sfb->fb.var.bits_per_pixel = 16;
962
963         sfb->fb.var.xres_virtual = sfb->fb.var.xres;
964         sfb->fb.var.yres_virtual = sfb->fb.var.yres;
965         err = smtc_map_smem(sfb, pdev, smem_size);
966         if (err)
967                 goto failed;
968
969         smtcfb_setmode(sfb);
970         /* Primary display starting from 0 postion */
971         hw.BaseAddressInVRAM = 0;
972         sfb->fb.par = &hw;
973
974         err = register_framebuffer(&sfb->fb);
975         if (err < 0)
976                 goto failed;
977
978         printk(KERN_INFO "Silicon Motion SM%X Rev%X primary display mode"
979                         "%dx%d-%d Init Complete.\n", hw.chipID, hw.chipRevID,
980                         sfb->fb.var.xres, sfb->fb.var.yres,
981                         sfb->fb.var.bits_per_pixel);
982
983         return 0;
984
985  failed:
986         printk(KERN_INFO "Silicon Motion, Inc.  primary display init fail\n");
987
988         smtc_unmap_smem(sfb);
989         smtc_unmap_mmio(sfb);
990         smtc_free_fb_info(sfb);
991
992         return err;
993 }
994
995
996 /* Jason (08/11/2009) PCI_DRV wrapper essential structs */
997 static const struct pci_device_id smtcfb_pci_table[] = {
998         {0x126f, 0x710, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
999         {0x126f, 0x712, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
1000         {0x126f, 0x720, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
1001         {0,}
1002 };
1003
1004
1005 /* Jason (08/14/2009)
1006  * do some clean up when the driver module is removed
1007  */
1008 static void __devexit smtcfb_pci_remove(struct pci_dev *pdev)
1009 {
1010         struct smtcfb_info *sfb;
1011
1012         sfb = pci_get_drvdata(pdev);
1013         pci_set_drvdata(pdev, NULL);
1014         smtc_unmap_smem(sfb);
1015         smtc_unmap_mmio(sfb);
1016         unregister_framebuffer(&sfb->fb);
1017         smtc_free_fb_info(sfb);
1018 }
1019
1020 /* Jason (08/14/2009)
1021  * suspend function, called when the suspend event is triggered
1022  */
1023 static int __maybe_unused smtcfb_suspend(struct pci_dev *pdev, pm_message_t msg)
1024 {
1025         struct smtcfb_info *sfb;
1026         int retv;
1027
1028         sfb = pci_get_drvdata(pdev);
1029
1030         /* set the hw in sleep mode use externel clock and self memory refresh
1031          * so that we can turn off internal PLLs later on
1032          */
1033         smtc_seqw(0x20, (smtc_seqr(0x20) | 0xc0));
1034         smtc_seqw(0x69, (smtc_seqr(0x69) & 0xf7));
1035
1036         switch (msg.event) {
1037         case PM_EVENT_FREEZE:
1038         case PM_EVENT_PRETHAW:
1039                 pdev->dev.power.power_state = msg;
1040                 return 0;
1041         }
1042
1043         /* when doing suspend, call fb apis and pci apis */
1044         if (msg.event == PM_EVENT_SUSPEND) {
1045                 acquire_console_sem();
1046                 fb_set_suspend(&sfb->fb, 1);
1047                 release_console_sem();
1048                 retv = pci_save_state(pdev);
1049                 pci_disable_device(pdev);
1050                 retv = pci_choose_state(pdev, msg);
1051                 retv = pci_set_power_state(pdev, retv);
1052         }
1053
1054         pdev->dev.power.power_state = msg;
1055
1056         /* additionaly turn off all function blocks including internal PLLs */
1057         smtc_seqw(0x21, 0xff);
1058
1059         return 0;
1060 }
1061
1062 static int __maybe_unused smtcfb_resume(struct pci_dev *pdev)
1063 {
1064         struct smtcfb_info *sfb;
1065         int retv;
1066
1067         sfb = pci_get_drvdata(pdev);
1068
1069         /* when resuming, restore pci data and fb cursor */
1070         if (pdev->dev.power.power_state.event != PM_EVENT_FREEZE) {
1071                 retv = pci_set_power_state(pdev, PCI_D0);
1072                 retv = pci_restore_state(pdev);
1073                 if (pci_enable_device(pdev))
1074                         return -1;
1075                 pci_set_master(pdev);
1076         }
1077
1078         /* reinit hardware */
1079         sm7xx_init_hw();
1080         switch (hw.chipID) {
1081         case 0x710:
1082         case 0x712:
1083                 /* set MCLK = 14.31818 *  (0x16 / 0x2) */
1084                 smtc_seqw(0x6a, 0x16);
1085                 smtc_seqw(0x6b, 0x02);
1086                 smtc_seqw(0x62, 0x3e);
1087                 /* enable PCI burst */
1088                 smtc_seqw(0x17, 0x20);
1089 #ifdef __BIG_ENDIAN
1090                 if (sfb->fb.var.bits_per_pixel == 32)
1091                         smtc_seqw(0x17, 0x30);
1092 #endif
1093                 break;
1094         case 0x720:
1095                 smtc_seqw(0x62, 0xff);
1096                 smtc_seqw(0x6a, 0x0d);
1097                 smtc_seqw(0x6b, 0x02);
1098                 break;
1099         }
1100
1101         smtc_seqw(0x34, (smtc_seqr(0x34) | 0xc0));
1102         smtc_seqw(0x33, ((smtc_seqr(0x33) | 0x08) & 0xfb));
1103
1104         smtcfb_setmode(sfb);
1105
1106         acquire_console_sem();
1107         fb_set_suspend(&sfb->fb, 0);
1108         release_console_sem();
1109
1110         return 0;
1111 }
1112
1113 /* Jason (08/13/2009)
1114  * pci_driver struct used to wrap the original driver
1115  * so that it can be registered into the kernel and
1116  * the proper method would be called when suspending and resuming
1117  */
1118 static struct pci_driver smtcfb_driver = {
1119         .name = "smtcfb",
1120         .id_table = smtcfb_pci_table,
1121         .probe = smtcfb_pci_probe,
1122         .remove = __devexit_p(smtcfb_pci_remove),
1123 #ifdef CONFIG_PM
1124         .suspend = smtcfb_suspend,
1125         .resume = smtcfb_resume,
1126 #endif
1127 };
1128
1129 static int __init smtcfb_init(void)
1130 {
1131         return pci_register_driver(&smtcfb_driver);
1132 }
1133
1134 static void __exit smtcfb_exit(void)
1135 {
1136         pci_unregister_driver(&smtcfb_driver);
1137 }
1138
1139 module_init(smtcfb_init);
1140 module_exit(smtcfb_exit);
1141
1142 MODULE_AUTHOR("Siliconmotion ");
1143 MODULE_DESCRIPTION("Framebuffer driver for SMI Graphic Cards");
1144 MODULE_LICENSE("GPL");