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