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