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