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