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