[PATCH] vt: Remove VT-specific declarations and definitions from tty.h
[safe/jmp/linux-2.6] / drivers / video / chipsfb.c
1 /*
2  *  drivers/video/chipsfb.c -- frame buffer device for
3  *  Chips & Technologies 65550 chip.
4  *
5  *  Copyright (C) 1998-2002 Paul Mackerras
6  *
7  *  This file is derived from the Powermac "chips" driver:
8  *  Copyright (C) 1997 Fabio Riccardi.
9  *  And from the frame buffer device for Open Firmware-initialized devices:
10  *  Copyright (C) 1997 Geert Uytterhoeven.
11  *
12  *  This file is subject to the terms and conditions of the GNU General Public
13  *  License. See the file COPYING in the main directory of this archive for
14  *  more details.
15  */
16
17 #include <linux/module.h>
18 #include <linux/kernel.h>
19 #include <linux/errno.h>
20 #include <linux/string.h>
21 #include <linux/mm.h>
22 #include <linux/slab.h>
23 #include <linux/vmalloc.h>
24 #include <linux/delay.h>
25 #include <linux/interrupt.h>
26 #include <linux/fb.h>
27 #include <linux/init.h>
28 #include <linux/pci.h>
29 #include <linux/console.h>
30 #include <asm/io.h>
31
32 #ifdef CONFIG_PMAC_BACKLIGHT
33 #include <asm/backlight.h>
34 #endif
35
36 /*
37  * Since we access the display with inb/outb to fixed port numbers,
38  * we can only handle one 6555x chip.  -- paulus
39  */
40 #define write_ind(num, val, ap, dp)     do { \
41         outb((num), (ap)); outb((val), (dp)); \
42 } while (0)
43 #define read_ind(num, var, ap, dp)      do { \
44         outb((num), (ap)); var = inb((dp)); \
45 } while (0)
46
47 /* extension registers */
48 #define write_xr(num, val)      write_ind(num, val, 0x3d6, 0x3d7)
49 #define read_xr(num, var)       read_ind(num, var, 0x3d6, 0x3d7)
50 /* flat panel registers */
51 #define write_fr(num, val)      write_ind(num, val, 0x3d0, 0x3d1)
52 #define read_fr(num, var)       read_ind(num, var, 0x3d0, 0x3d1)
53 /* CRTC registers */
54 #define write_cr(num, val)      write_ind(num, val, 0x3d4, 0x3d5)
55 #define read_cr(num, var)       read_ind(num, var, 0x3d4, 0x3d5)
56 /* graphics registers */
57 #define write_gr(num, val)      write_ind(num, val, 0x3ce, 0x3cf)
58 #define read_gr(num, var)       read_ind(num, var, 0x3ce, 0x3cf)
59 /* sequencer registers */
60 #define write_sr(num, val)      write_ind(num, val, 0x3c4, 0x3c5)
61 #define read_sr(num, var)       read_ind(num, var, 0x3c4, 0x3c5)
62 /* attribute registers - slightly strange */
63 #define write_ar(num, val)      do { \
64         inb(0x3da); write_ind(num, val, 0x3c0, 0x3c0); \
65 } while (0)
66 #define read_ar(num, var)       do { \
67         inb(0x3da); read_ind(num, var, 0x3c0, 0x3c1); \
68 } while (0)
69
70 /*
71  * Exported functions
72  */
73 int chips_init(void);
74
75 static int chipsfb_pci_init(struct pci_dev *dp, const struct pci_device_id *);
76 static int chipsfb_check_var(struct fb_var_screeninfo *var,
77                              struct fb_info *info);
78 static int chipsfb_set_par(struct fb_info *info);
79 static int chipsfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
80                              u_int transp, struct fb_info *info);
81 static int chipsfb_blank(int blank, struct fb_info *info);
82
83 static struct fb_ops chipsfb_ops = {
84         .owner          = THIS_MODULE,
85         .fb_check_var   = chipsfb_check_var,
86         .fb_set_par     = chipsfb_set_par,
87         .fb_setcolreg   = chipsfb_setcolreg,
88         .fb_blank       = chipsfb_blank,
89         .fb_fillrect    = cfb_fillrect,
90         .fb_copyarea    = cfb_copyarea,
91         .fb_imageblit   = cfb_imageblit,
92 };
93
94 static int chipsfb_check_var(struct fb_var_screeninfo *var,
95                              struct fb_info *info)
96 {
97         if (var->xres > 800 || var->yres > 600
98             || var->xres_virtual > 800 || var->yres_virtual > 600
99             || (var->bits_per_pixel != 8 && var->bits_per_pixel != 16)
100             || var->nonstd
101             || (var->vmode & FB_VMODE_MASK) != FB_VMODE_NONINTERLACED)
102                 return -EINVAL;
103
104         var->xres = var->xres_virtual = 800;
105         var->yres = var->yres_virtual = 600;
106
107         return 0;
108 }
109
110 static int chipsfb_set_par(struct fb_info *info)
111 {
112         if (info->var.bits_per_pixel == 16) {
113                 write_cr(0x13, 200);            // Set line length (doublewords)
114                 write_xr(0x81, 0x14);           // 15 bit (555) color mode
115                 write_xr(0x82, 0x00);           // Disable palettes
116                 write_xr(0x20, 0x10);           // 16 bit blitter mode
117
118                 info->fix.line_length = 800*2;
119                 info->fix.visual = FB_VISUAL_TRUECOLOR;
120
121                 info->var.red.offset = 10;
122                 info->var.green.offset = 5;
123                 info->var.blue.offset = 0;
124                 info->var.red.length = info->var.green.length =
125                         info->var.blue.length = 5;
126                 
127         } else {
128                 /* p->var.bits_per_pixel == 8 */
129                 write_cr(0x13, 100);            // Set line length (doublewords)
130                 write_xr(0x81, 0x12);           // 8 bit color mode
131                 write_xr(0x82, 0x08);           // Graphics gamma enable
132                 write_xr(0x20, 0x00);           // 8 bit blitter mode
133
134                 info->fix.line_length = 800;
135                 info->fix.visual = FB_VISUAL_PSEUDOCOLOR;               
136
137                 info->var.red.offset = info->var.green.offset =
138                         info->var.blue.offset = 0;
139                 info->var.red.length = info->var.green.length =
140                         info->var.blue.length = 8;
141                 
142         }
143         return 0;
144 }
145
146 static int chipsfb_blank(int blank, struct fb_info *info)
147 {
148 #ifdef CONFIG_PMAC_BACKLIGHT
149         mutex_lock(&pmac_backlight_mutex);
150
151         if (pmac_backlight) {
152                 down(&pmac_backlight->sem);
153
154                 /* used to disable backlight only for blank > 1, but it seems
155                  * useful at blank = 1 too (saves battery, extends backlight
156                  * life)
157                  */
158                 if (blank)
159                         pmac_backlight->props->power = FB_BLANK_POWERDOWN;
160                 else
161                         pmac_backlight->props->power = FB_BLANK_UNBLANK;
162                 pmac_backlight->props->update_status(pmac_backlight);
163                 up(&pmac_backlight->sem);
164         }
165
166         mutex_unlock(&pmac_backlight_mutex);
167 #endif /* CONFIG_PMAC_BACKLIGHT */
168
169         return 1;       /* get fb_blank to set the colormap to all black */
170 }
171
172 static int chipsfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
173                              u_int transp, struct fb_info *info)
174 {
175         if (regno > 255)
176                 return 1;
177         red >>= 8;
178         green >>= 8;
179         blue >>= 8;
180         outb(regno, 0x3c8);
181         udelay(1);
182         outb(red, 0x3c9);
183         outb(green, 0x3c9);
184         outb(blue, 0x3c9);
185
186         return 0;
187 }
188
189 struct chips_init_reg {
190         unsigned char addr;
191         unsigned char data;
192 };
193
194 static struct chips_init_reg chips_init_sr[] = {
195         { 0x00, 0x03 },
196         { 0x01, 0x01 },
197         { 0x02, 0x0f },
198         { 0x04, 0x0e }
199 };
200
201 static struct chips_init_reg chips_init_gr[] = {
202         { 0x05, 0x00 },
203         { 0x06, 0x0d },
204         { 0x08, 0xff }
205 };
206
207 static struct chips_init_reg chips_init_ar[] = {
208         { 0x10, 0x01 },
209         { 0x12, 0x0f },
210         { 0x13, 0x00 }
211 };
212
213 static struct chips_init_reg chips_init_cr[] = {
214         { 0x00, 0x7f },
215         { 0x01, 0x63 },
216         { 0x02, 0x63 },
217         { 0x03, 0x83 },
218         { 0x04, 0x66 },
219         { 0x05, 0x10 },
220         { 0x06, 0x72 },
221         { 0x07, 0x3e },
222         { 0x08, 0x00 },
223         { 0x09, 0x40 },
224         { 0x0c, 0x00 },
225         { 0x0d, 0x00 },
226         { 0x10, 0x59 },
227         { 0x11, 0x0d },
228         { 0x12, 0x57 },
229         { 0x13, 0x64 },
230         { 0x14, 0x00 },
231         { 0x15, 0x57 },
232         { 0x16, 0x73 },
233         { 0x17, 0xe3 },
234         { 0x18, 0xff },
235         { 0x30, 0x02 },
236         { 0x31, 0x02 },
237         { 0x32, 0x02 },
238         { 0x33, 0x02 },
239         { 0x40, 0x00 },
240         { 0x41, 0x00 },
241         { 0x40, 0x80 }
242 };
243
244 static struct chips_init_reg chips_init_fr[] = {
245         { 0x01, 0x02 },
246         { 0x03, 0x08 },
247         { 0x04, 0x81 },
248         { 0x05, 0x21 },
249         { 0x08, 0x0c },
250         { 0x0a, 0x74 },
251         { 0x0b, 0x11 },
252         { 0x10, 0x0c },
253         { 0x11, 0xe0 },
254         /* { 0x12, 0x40 }, -- 3400 needs 40, 2400 needs 48, no way to tell */
255         { 0x20, 0x63 },
256         { 0x21, 0x68 },
257         { 0x22, 0x19 },
258         { 0x23, 0x7f },
259         { 0x24, 0x68 },
260         { 0x26, 0x00 },
261         { 0x27, 0x0f },
262         { 0x30, 0x57 },
263         { 0x31, 0x58 },
264         { 0x32, 0x0d },
265         { 0x33, 0x72 },
266         { 0x34, 0x02 },
267         { 0x35, 0x22 },
268         { 0x36, 0x02 },
269         { 0x37, 0x00 }
270 };
271
272 static struct chips_init_reg chips_init_xr[] = {
273         { 0xce, 0x00 },         /* set default memory clock */
274         { 0xcc, 0x43 },         /* memory clock ratio */
275         { 0xcd, 0x18 },
276         { 0xce, 0xa1 },
277         { 0xc8, 0x84 },
278         { 0xc9, 0x0a },
279         { 0xca, 0x00 },
280         { 0xcb, 0x20 },
281         { 0xcf, 0x06 },
282         { 0xd0, 0x0e },
283         { 0x09, 0x01 },
284         { 0x0a, 0x02 },
285         { 0x0b, 0x01 },
286         { 0x20, 0x00 },
287         { 0x40, 0x03 },
288         { 0x41, 0x01 },
289         { 0x42, 0x00 },
290         { 0x80, 0x82 },
291         { 0x81, 0x12 },
292         { 0x82, 0x08 },
293         { 0xa0, 0x00 },
294         { 0xa8, 0x00 }
295 };
296
297 static void __init chips_hw_init(void)
298 {
299         int i;
300
301         for (i = 0; i < ARRAY_SIZE(chips_init_xr); ++i)
302                 write_xr(chips_init_xr[i].addr, chips_init_xr[i].data);
303         outb(0x29, 0x3c2); /* set misc output reg */
304         for (i = 0; i < ARRAY_SIZE(chips_init_sr); ++i)
305                 write_sr(chips_init_sr[i].addr, chips_init_sr[i].data);
306         for (i = 0; i < ARRAY_SIZE(chips_init_gr); ++i)
307                 write_gr(chips_init_gr[i].addr, chips_init_gr[i].data);
308         for (i = 0; i < ARRAY_SIZE(chips_init_ar); ++i)
309                 write_ar(chips_init_ar[i].addr, chips_init_ar[i].data);
310         for (i = 0; i < ARRAY_SIZE(chips_init_cr); ++i)
311                 write_cr(chips_init_cr[i].addr, chips_init_cr[i].data);
312         for (i = 0; i < ARRAY_SIZE(chips_init_fr); ++i)
313                 write_fr(chips_init_fr[i].addr, chips_init_fr[i].data);
314 }
315
316 static struct fb_fix_screeninfo chipsfb_fix __initdata = {
317         .id =           "C&T 65550",
318         .type =         FB_TYPE_PACKED_PIXELS,
319         .visual =       FB_VISUAL_PSEUDOCOLOR,
320         .accel =        FB_ACCEL_NONE,
321         .line_length =  800,
322
323 // FIXME: Assumes 1MB frame buffer, but 65550 supports 1MB or 2MB.
324 // * "3500" PowerBook G3 (the original PB G3) has 2MB.
325 // * 2400 has 1MB composed of 2 Mitsubishi M5M4V4265CTP DRAM chips.
326 //   Motherboard actually supports 2MB -- there are two blank locations
327 //   for a second pair of DRAMs.  (Thanks, Apple!)
328 // * 3400 has 1MB (I think).  Don't know if it's expandable.
329 // -- Tim Seufert
330         .smem_len =     0x100000,       /* 1MB */
331 };
332
333 static struct fb_var_screeninfo chipsfb_var __initdata = {
334         .xres = 800,
335         .yres = 600,
336         .xres_virtual = 800,
337         .yres_virtual = 600,
338         .bits_per_pixel = 8,
339         .red = { .length = 8 },
340         .green = { .length = 8 },
341         .blue = { .length = 8 },
342         .height = -1,
343         .width = -1,
344         .vmode = FB_VMODE_NONINTERLACED,
345         .pixclock = 10000,
346         .left_margin = 16,
347         .right_margin = 16,
348         .upper_margin = 16,
349         .lower_margin = 16,
350         .hsync_len = 8,
351         .vsync_len = 8,
352 };
353
354 static void __init init_chips(struct fb_info *p, unsigned long addr)
355 {
356         memset(p->screen_base, 0, 0x100000);
357
358         p->fix = chipsfb_fix;
359         p->fix.smem_start = addr;
360
361         p->var = chipsfb_var;
362
363         p->fbops = &chipsfb_ops;
364         p->flags = FBINFO_DEFAULT;
365
366         fb_alloc_cmap(&p->cmap, 256, 0);
367
368         chips_hw_init();
369 }
370
371 static int __devinit
372 chipsfb_pci_init(struct pci_dev *dp, const struct pci_device_id *ent)
373 {
374         struct fb_info *p;
375         unsigned long addr, size;
376         unsigned short cmd;
377         int rc = -ENODEV;
378
379         if (pci_enable_device(dp) < 0) {
380                 dev_err(&dp->dev, "Cannot enable PCI device\n");
381                 goto err_out;
382         }
383
384         if ((dp->resource[0].flags & IORESOURCE_MEM) == 0)
385                 goto err_disable;
386         addr = pci_resource_start(dp, 0);
387         size = pci_resource_len(dp, 0);
388         if (addr == 0)
389                 goto err_disable;
390
391         p = framebuffer_alloc(0, &dp->dev);
392         if (p == NULL) {
393                 dev_err(&dp->dev, "Cannot allocate framebuffer structure\n");
394                 rc = -ENOMEM;
395                 goto err_disable;
396         }
397
398         if (pci_request_region(dp, 0, "chipsfb") != 0) {
399                 dev_err(&dp->dev, "Cannot request framebuffer\n");
400                 rc = -EBUSY;
401                 goto err_release_fb;
402         }
403
404 #ifdef __BIG_ENDIAN
405         addr += 0x800000;       // Use big-endian aperture
406 #endif
407
408         /* we should use pci_enable_device here, but,
409            the device doesn't declare its I/O ports in its BARs
410            so pci_enable_device won't turn on I/O responses */
411         pci_read_config_word(dp, PCI_COMMAND, &cmd);
412         cmd |= 3;       /* enable memory and IO space */
413         pci_write_config_word(dp, PCI_COMMAND, cmd);
414
415 #ifdef CONFIG_PMAC_BACKLIGHT
416         /* turn on the backlight */
417         mutex_lock(&pmac_backlight_mutex);
418         if (pmac_backlight) {
419                 down(&pmac_backlight->sem);
420                 pmac_backlight->props->power = FB_BLANK_UNBLANK;
421                 pmac_backlight->props->update_status(pmac_backlight);
422                 up(&pmac_backlight->sem);
423         }
424         mutex_unlock(&pmac_backlight_mutex);
425 #endif /* CONFIG_PMAC_BACKLIGHT */
426
427 #ifdef CONFIG_PPC
428         p->screen_base = __ioremap(addr, 0x200000, _PAGE_NO_CACHE);
429 #else
430         p->screen_base = ioremap(addr, 0x200000);
431 #endif
432         if (p->screen_base == NULL) {
433                 dev_err(&dp->dev, "Cannot map framebuffer\n");
434                 rc = -ENOMEM;
435                 goto err_release_pci;
436         }
437
438         pci_set_drvdata(dp, p);
439         p->device = &dp->dev;
440
441         init_chips(p, addr);
442
443         if (register_framebuffer(p) < 0) {
444                 dev_err(&dp->dev,"C&T 65550 framebuffer failed to register\n");
445                 goto err_unmap;
446         }
447
448         dev_info(&dp->dev,"fb%d: Chips 65550 frame buffer"
449                  " (%dK RAM detected)\n",
450                  p->node, p->fix.smem_len / 1024);
451
452         return 0;
453
454  err_unmap:
455         iounmap(p->screen_base);
456  err_release_pci:
457         pci_release_region(dp, 0);
458  err_release_fb:
459         framebuffer_release(p);
460  err_disable:
461  err_out:
462         return rc;
463 }
464
465 static void __devexit chipsfb_remove(struct pci_dev *dp)
466 {
467         struct fb_info *p = pci_get_drvdata(dp);
468
469         if (p->screen_base == NULL)
470                 return;
471         unregister_framebuffer(p);
472         iounmap(p->screen_base);
473         p->screen_base = NULL;
474         pci_release_region(dp, 0);
475 }
476
477 #ifdef CONFIG_PM
478 static int chipsfb_pci_suspend(struct pci_dev *pdev, pm_message_t state)
479 {
480         struct fb_info *p = pci_get_drvdata(pdev);
481
482         if (state.event == pdev->dev.power.power_state.event)
483                 return 0;
484         if (state.event != PM_SUSPEND_MEM)
485                 goto done;
486
487         acquire_console_sem();
488         chipsfb_blank(1, p);
489         fb_set_suspend(p, 1);
490         release_console_sem();
491  done:
492         pdev->dev.power.power_state = state;
493         return 0;
494 }
495
496 static int chipsfb_pci_resume(struct pci_dev *pdev)
497 {
498         struct fb_info *p = pci_get_drvdata(pdev);
499
500         acquire_console_sem();
501         fb_set_suspend(p, 0);
502         chipsfb_blank(0, p);
503         release_console_sem();
504
505         pdev->dev.power.power_state = PMSG_ON;
506         return 0;
507 }
508 #endif /* CONFIG_PM */
509
510
511 static struct pci_device_id chipsfb_pci_tbl[] = {
512         { PCI_VENDOR_ID_CT, PCI_DEVICE_ID_CT_65550, PCI_ANY_ID, PCI_ANY_ID },
513         { 0 }
514 };
515
516 MODULE_DEVICE_TABLE(pci, chipsfb_pci_tbl);
517
518 static struct pci_driver chipsfb_driver = {
519         .name =         "chipsfb",
520         .id_table =     chipsfb_pci_tbl,
521         .probe =        chipsfb_pci_init,
522         .remove =       __devexit_p(chipsfb_remove),
523 #ifdef CONFIG_PM
524         .suspend =      chipsfb_pci_suspend,
525         .resume =       chipsfb_pci_resume,
526 #endif
527 };
528
529 int __init chips_init(void)
530 {
531         if (fb_get_options("chipsfb", NULL))
532                 return -ENODEV;
533
534         return pci_register_driver(&chipsfb_driver);
535 }
536
537 module_init(chips_init);
538
539 static void __exit chipsfb_exit(void)
540 {
541         pci_unregister_driver(&chipsfb_driver);
542 }
543
544 MODULE_LICENSE("GPL");