[PATCH] fix drivers/video/imacfb.c compilation
[linux-2.6.git] / drivers / video / imacfb.c
1 /*
2  * framebuffer driver for Intel Based Mac's
3  *
4  * (c) 2006 Edgar Hucek <gimli@dark-green.com>
5  * Original imac driver written by Gerd Knorr <kraxel@goldbach.in-berlin.de>
6  *
7  */
8
9 #include <linux/delay.h>
10 #include <linux/errno.h>
11 #include <linux/fb.h>
12 #include <linux/kernel.h>
13 #include <linux/init.h>
14 #include <linux/ioport.h>
15 #include <linux/mm.h>
16 #include <linux/module.h>
17 #include <linux/platform_device.h>
18 #include <linux/slab.h>
19 #include <linux/string.h>
20 #include <linux/tty.h>
21
22 #include <asm/io.h>
23
24 #include <video/vga.h>
25
26 typedef enum _MAC_TYPE {
27         M_I17,
28         M_I20,
29         M_MINI,
30         M_MACBOOK,
31         M_NEW
32 } MAC_TYPE;
33
34 /* --------------------------------------------------------------------- */
35
36 static struct fb_var_screeninfo imacfb_defined __initdata = {
37         .activate               = FB_ACTIVATE_NOW,
38         .height                 = -1,
39         .width                  = -1,
40         .right_margin           = 32,
41         .upper_margin           = 16,
42         .lower_margin           = 4,
43         .vsync_len              = 4,
44         .vmode                  = FB_VMODE_NONINTERLACED,
45 };
46
47 static struct fb_fix_screeninfo imacfb_fix __initdata = {
48         .id                     = "IMAC VGA",
49         .type                   = FB_TYPE_PACKED_PIXELS,
50         .accel                  = FB_ACCEL_NONE,
51         .visual                 = FB_VISUAL_TRUECOLOR,
52 };
53
54 static int inverse;
55 static int model                = M_NEW;
56 static int manual_height;
57 static int manual_width;
58
59 #define DEFAULT_FB_MEM  1024*1024*16
60
61 /* --------------------------------------------------------------------- */
62
63 static int imacfb_setcolreg(unsigned regno, unsigned red, unsigned green,
64                             unsigned blue, unsigned transp,
65                             struct fb_info *info)
66 {
67         /*
68          *  Set a single color register. The values supplied are
69          *  already rounded down to the hardware's capabilities
70          *  (according to the entries in the `var' structure). Return
71          *  != 0 for invalid regno.
72          */
73
74         if (regno >= info->cmap.len)
75                 return 1;
76
77         if (regno < 16) {
78                 red   >>= 8;
79                 green >>= 8;
80                 blue  >>= 8;
81                 ((u32 *)(info->pseudo_palette))[regno] =
82                         (red   << info->var.red.offset)   |
83                         (green << info->var.green.offset) |
84                         (blue  << info->var.blue.offset);
85         }
86         return 0;
87 }
88
89 static struct fb_ops imacfb_ops = {
90         .owner          = THIS_MODULE,
91         .fb_setcolreg   = imacfb_setcolreg,
92         .fb_fillrect    = cfb_fillrect,
93         .fb_copyarea    = cfb_copyarea,
94         .fb_imageblit   = cfb_imageblit,
95 };
96
97 static int __init imacfb_setup(char *options)
98 {
99         char *this_opt;
100
101         if (!options || !*options)
102                 return 0;
103
104         while ((this_opt = strsep(&options, ",")) != NULL) {
105                 if (!*this_opt) continue;
106
107                 if (!strcmp(this_opt, "inverse"))
108                         inverse = 1;
109                 else if (!strcmp(this_opt, "i17"))
110                         model = M_I17;
111                 else if (!strcmp(this_opt, "i20"))
112                         model = M_I20;
113                 else if (!strcmp(this_opt, "mini"))
114                         model = M_MINI;
115                 else if (!strcmp(this_opt, "macbook"))
116                         model = M_MACBOOK;
117                 else if (!strncmp(this_opt, "height:", 7))
118                         manual_height = simple_strtoul(this_opt+7, NULL, 0);
119                 else if (!strncmp(this_opt, "width:", 6))
120                         manual_width = simple_strtoul(this_opt+6, NULL, 0);
121         }
122         return 0;
123 }
124
125 static int __init imacfb_probe(struct platform_device *dev)
126 {
127         struct fb_info *info;
128         int err;
129         unsigned int size_vmode;
130         unsigned int size_remap;
131         unsigned int size_total;
132
133         screen_info.lfb_depth = 32;
134         screen_info.lfb_size = DEFAULT_FB_MEM / 0x10000;
135         screen_info.pages=1;
136         screen_info.blue_size = 8;
137         screen_info.blue_pos = 0;
138         screen_info.green_size = 8;
139         screen_info.green_pos = 8;
140         screen_info.red_size = 8;
141         screen_info.red_pos = 16;
142         screen_info.rsvd_size = 8;
143         screen_info.rsvd_pos = 24;
144
145         switch (model) {
146         case M_I17:
147                 screen_info.lfb_width = 1440;
148                 screen_info.lfb_height = 900;
149                 screen_info.lfb_linelength = 1472 * 4;
150                 screen_info.lfb_base = 0x80010000;
151                 break;
152         case M_NEW:
153         case M_I20:
154                 screen_info.lfb_width = 1680;
155                 screen_info.lfb_height = 1050;
156                 screen_info.lfb_linelength = 1728 * 4;
157                 screen_info.lfb_base = 0x80010000;
158                 break;
159         case M_MINI:
160                 screen_info.lfb_width = 1024;
161                 screen_info.lfb_height = 768;
162                 screen_info.lfb_linelength = 2048 * 4;
163                 screen_info.lfb_base = 0x80000000;
164                 break;
165         case M_MACBOOK:
166                 screen_info.lfb_width = 1280;
167                 screen_info.lfb_height = 800;
168                 screen_info.lfb_linelength = 2048 * 4;
169                 screen_info.lfb_base = 0x80000000;
170                 break;
171         }
172
173         /* if the user wants to manually specify height/width,
174            we will override the defaults */
175         /* TODO: eventually get auto-detection working */
176         if (manual_height > 0)
177                 screen_info.lfb_height = manual_height;
178         if (manual_width > 0)
179                 screen_info.lfb_width = manual_width;
180
181         imacfb_fix.smem_start = screen_info.lfb_base;
182         imacfb_defined.bits_per_pixel = screen_info.lfb_depth;
183         imacfb_defined.xres = screen_info.lfb_width;
184         imacfb_defined.yres = screen_info.lfb_height;
185         imacfb_fix.line_length = screen_info.lfb_linelength;
186
187         /*   size_vmode -- that is the amount of memory needed for the
188          *                 used video mode, i.e. the minimum amount of
189          *                 memory we need. */
190         size_vmode = imacfb_defined.yres * imacfb_fix.line_length;
191
192         /*   size_total -- all video memory we have. Used for
193          *                 entries, ressource allocation and bounds
194          *                 checking. */
195         size_total = screen_info.lfb_size * 65536;
196         if (size_total < size_vmode)
197                 size_total = size_vmode;
198
199         /*   size_remap -- the amount of video memory we are going to
200          *                 use for imacfb.  With modern cards it is no
201          *                 option to simply use size_total as that
202          *                 wastes plenty of kernel address space. */
203         size_remap  = size_vmode * 2;
204         if (size_remap < size_vmode)
205                 size_remap = size_vmode;
206         if (size_remap > size_total)
207                 size_remap = size_total;
208         imacfb_fix.smem_len = size_remap;
209
210         if (!request_mem_region(imacfb_fix.smem_start, size_total, "imacfb")) {
211                 printk(KERN_WARNING
212                        "imacfb: cannot reserve video memory at 0x%lx\n",
213                         imacfb_fix.smem_start);
214                 /* We cannot make this fatal. Sometimes this comes from magic
215                    spaces our resource handlers simply don't know about */
216         }
217
218         info = framebuffer_alloc(sizeof(u32) * 16, &dev->dev);
219         if (!info) {
220                 err = -ENOMEM;
221                 goto err_release_mem;
222         }
223         info->pseudo_palette = info->par;
224         info->par = NULL;
225
226         info->screen_base = ioremap(imacfb_fix.smem_start, imacfb_fix.smem_len);
227         if (!info->screen_base) {
228                 printk(KERN_ERR "imacfb: abort, cannot ioremap video memory "
229                                 "0x%x @ 0x%lx\n",
230                         imacfb_fix.smem_len, imacfb_fix.smem_start);
231                 err = -EIO;
232                 goto err_unmap;
233         }
234
235         printk(KERN_INFO "imacfb: framebuffer at 0x%lx, mapped to 0x%p, "
236                "using %dk, total %dk\n",
237                imacfb_fix.smem_start, info->screen_base,
238                size_remap/1024, size_total/1024);
239         printk(KERN_INFO "imacfb: mode is %dx%dx%d, linelength=%d, pages=%d\n",
240                imacfb_defined.xres, imacfb_defined.yres,
241                imacfb_defined.bits_per_pixel, imacfb_fix.line_length,
242                screen_info.pages);
243
244         imacfb_defined.xres_virtual = imacfb_defined.xres;
245         imacfb_defined.yres_virtual = imacfb_fix.smem_len /
246                                         imacfb_fix.line_length;
247         printk(KERN_INFO "imacfb: scrolling: redraw\n");
248         imacfb_defined.yres_virtual = imacfb_defined.yres;
249
250         /* some dummy values for timing to make fbset happy */
251         imacfb_defined.pixclock     = 10000000 / imacfb_defined.xres *
252                                         1000 / imacfb_defined.yres;
253         imacfb_defined.left_margin  = (imacfb_defined.xres / 8) & 0xf8;
254         imacfb_defined.hsync_len    = (imacfb_defined.xres / 8) & 0xf8;
255
256         imacfb_defined.red.offset    = screen_info.red_pos;
257         imacfb_defined.red.length    = screen_info.red_size;
258         imacfb_defined.green.offset  = screen_info.green_pos;
259         imacfb_defined.green.length  = screen_info.green_size;
260         imacfb_defined.blue.offset   = screen_info.blue_pos;
261         imacfb_defined.blue.length   = screen_info.blue_size;
262         imacfb_defined.transp.offset = screen_info.rsvd_pos;
263         imacfb_defined.transp.length = screen_info.rsvd_size;
264
265         printk(KERN_INFO "imacfb: %s: "
266                "size=%d:%d:%d:%d, shift=%d:%d:%d:%d\n",
267                "Truecolor",
268                screen_info.rsvd_size,
269                screen_info.red_size,
270                screen_info.green_size,
271                screen_info.blue_size,
272                screen_info.rsvd_pos,
273                screen_info.red_pos,
274                screen_info.green_pos,
275                screen_info.blue_pos);
276
277         imacfb_fix.ypanstep  = 0;
278         imacfb_fix.ywrapstep = 0;
279
280         /* request failure does not faze us, as vgacon probably has this
281          * region already (FIXME) */
282         request_region(0x3c0, 32, "imacfb");
283
284         info->fbops = &imacfb_ops;
285         info->var = imacfb_defined;
286         info->fix = imacfb_fix;
287         info->flags = FBINFO_FLAG_DEFAULT;
288
289         if (fb_alloc_cmap(&info->cmap, 256, 0) < 0) {
290                 err = -ENOMEM;
291                 goto err_unmap;
292         }
293         if (register_framebuffer(info)<0) {
294                 err = -EINVAL;
295                 goto err_fb_dealoc;
296         }
297         printk(KERN_INFO "fb%d: %s frame buffer device\n",
298                info->node, info->fix.id);
299         return 0;
300
301 err_fb_dealoc:
302         fb_dealloc_cmap(&info->cmap);
303 err_unmap:
304         iounmap(info->screen_base);
305         framebuffer_release(info);
306 err_release_mem:
307         release_mem_region(imacfb_fix.smem_start, size_total);
308         return err;
309 }
310
311 static struct platform_driver imacfb_driver = {
312         .probe  = imacfb_probe,
313         .driver = {
314                 .name   = "imacfb",
315         },
316 };
317
318 static struct platform_device imacfb_device = {
319         .name   = "imacfb",
320 };
321
322 static int __init imacfb_init(void)
323 {
324         int ret;
325         char *option = NULL;
326
327         /* ignore error return of fb_get_options */
328         fb_get_options("imacfb", &option);
329         imacfb_setup(option);
330         ret = platform_driver_register(&imacfb_driver);
331
332         if (!ret) {
333                 ret = platform_device_register(&imacfb_device);
334                 if (ret)
335                         platform_driver_unregister(&imacfb_driver);
336         }
337         return ret;
338 }
339 module_init(imacfb_init);
340
341 MODULE_LICENSE("GPL");