]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/video/geode/gx1fb_core.c
[PATCH] gx1fb: (try to) play nicer with various BIOSes
[net-next-2.6.git] / drivers / video / geode / gx1fb_core.c
CommitLineData
1da177e4
LT
1/*
2 * drivers/video/geode/gx1fb_core.c
3 * -- Geode GX1 framebuffer driver
4 *
5 * Copyright (C) 2005 Arcom Control Systems Ltd.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
13#include <linux/module.h>
14#include <linux/kernel.h>
15#include <linux/errno.h>
16#include <linux/string.h>
17#include <linux/mm.h>
18#include <linux/tty.h>
19#include <linux/slab.h>
20#include <linux/delay.h>
21#include <linux/fb.h>
22#include <linux/init.h>
23#include <linux/pci.h>
24
25#include "geodefb.h"
26#include "display_gx1.h"
27#include "video_cs5530.h"
28
29static char mode_option[32] = "640x480-16@60";
30static int crt_option = 1;
31static char panel_option[32] = "";
32
53eed4ec
DV
33/* Modes relevant to the GX1 (taken from modedb.c) */
34static const struct fb_videomode __initdata gx1_modedb[] = {
35 /* 640x480-60 VESA */
36 { NULL, 60, 640, 480, 39682, 48, 16, 33, 10, 96, 2,
37 0, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
38 /* 640x480-75 VESA */
39 { NULL, 75, 640, 480, 31746, 120, 16, 16, 01, 64, 3,
40 0, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
41 /* 640x480-85 VESA */
42 { NULL, 85, 640, 480, 27777, 80, 56, 25, 01, 56, 3,
43 0, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
44 /* 800x600-60 VESA */
45 { NULL, 60, 800, 600, 25000, 88, 40, 23, 01, 128, 4,
46 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
47 FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
48 /* 800x600-75 VESA */
49 { NULL, 75, 800, 600, 20202, 160, 16, 21, 01, 80, 3,
50 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
51 FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
52 /* 800x600-85 VESA */
53 { NULL, 85, 800, 600, 17761, 152, 32, 27, 01, 64, 3,
54 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
55 FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
56 /* 1024x768-60 VESA */
57 { NULL, 60, 1024, 768, 15384, 160, 24, 29, 3, 136, 6,
58 0, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
59 /* 1024x768-75 VESA */
60 { NULL, 75, 1024, 768, 12690, 176, 16, 28, 1, 96, 3,
61 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
62 FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
63 /* 1024x768-85 VESA */
64 { NULL, 85, 1024, 768, 10582, 208, 48, 36, 1, 96, 3,
65 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
66 FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
67 /* 1280x960-60 VESA */
68 { NULL, 60, 1280, 960, 9259, 312, 96, 36, 1, 112, 3,
69 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
70 FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
71 /* 1280x960-85 VESA */
72 { NULL, 85, 1280, 960, 6734, 224, 64, 47, 1, 160, 3,
73 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
74 FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
75 /* 1280x1024-60 VESA */
76 { NULL, 60, 1280, 1024, 9259, 248, 48, 38, 1, 112, 3,
77 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
78 FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
79 /* 1280x1024-75 VESA */
80 { NULL, 75, 1280, 1024, 7407, 248, 16, 38, 1, 144, 3,
81 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
82 FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
83 /* 1280x1024-85 VESA */
84 { NULL, 85, 1280, 1024, 6349, 224, 64, 44, 1, 160, 3,
85 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
86 FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
87};
88
1da177e4
LT
89static int gx1_line_delta(int xres, int bpp)
90{
91 int line_delta = xres * (bpp >> 3);
92
93 if (line_delta > 2048)
94 line_delta = 4096;
95 else if (line_delta > 1024)
96 line_delta = 2048;
97 else
98 line_delta = 1024;
99 return line_delta;
100}
101
102static int gx1fb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
103{
104 struct geodefb_par *par = info->par;
105
1da177e4
LT
106 /* Maximum resolution is 1280x1024. */
107 if (var->xres > 1280 || var->yres > 1024)
108 return -EINVAL;
109
110 if (par->panel_x && (var->xres > par->panel_x || var->yres > par->panel_y))
111 return -EINVAL;
112
113 /* Only 16 bpp and 8 bpp is supported by the hardware. */
114 if (var->bits_per_pixel == 16) {
115 var->red.offset = 11; var->red.length = 5;
116 var->green.offset = 5; var->green.length = 6;
117 var->blue.offset = 0; var->blue.length = 5;
118 var->transp.offset = 0; var->transp.length = 0;
119 } else if (var->bits_per_pixel == 8) {
120 var->red.offset = 0; var->red.length = 8;
121 var->green.offset = 0; var->green.length = 8;
122 var->blue.offset = 0; var->blue.length = 8;
123 var->transp.offset = 0; var->transp.length = 0;
124 } else
125 return -EINVAL;
126
127 /* Enough video memory? */
128 if (gx1_line_delta(var->xres, var->bits_per_pixel) * var->yres > info->fix.smem_len)
129 return -EINVAL;
130
131 /* FIXME: Check timing parameters here? */
132
133 return 0;
134}
135
136static int gx1fb_set_par(struct fb_info *info)
137{
138 struct geodefb_par *par = info->par;
139
140 if (info->var.bits_per_pixel == 16) {
141 info->fix.visual = FB_VISUAL_TRUECOLOR;
142 fb_dealloc_cmap(&info->cmap);
143 } else {
144 info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
145 fb_alloc_cmap(&info->cmap, 1<<info->var.bits_per_pixel, 0);
146 }
147
148 info->fix.line_length = gx1_line_delta(info->var.xres, info->var.bits_per_pixel);
149
150 par->dc_ops->set_mode(info);
151
152 return 0;
153}
154
155static inline u_int chan_to_field(u_int chan, struct fb_bitfield *bf)
156{
157 chan &= 0xffff;
158 chan >>= 16 - bf->length;
159 return chan << bf->offset;
160}
161
162static int gx1fb_setcolreg(unsigned regno, unsigned red, unsigned green,
163 unsigned blue, unsigned transp,
164 struct fb_info *info)
165{
166 struct geodefb_par *par = info->par;
167
168 if (info->var.grayscale) {
169 /* grayscale = 0.30*R + 0.59*G + 0.11*B */
170 red = green = blue = (red * 77 + green * 151 + blue * 28) >> 8;
171 }
172
173 /* Truecolor has hardware independent palette */
174 if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
175 u32 *pal = info->pseudo_palette;
176 u32 v;
177
178 if (regno >= 16)
179 return -EINVAL;
180
181 v = chan_to_field(red, &info->var.red);
182 v |= chan_to_field(green, &info->var.green);
183 v |= chan_to_field(blue, &info->var.blue);
184
185 pal[regno] = v;
186 } else {
187 if (regno >= 256)
188 return -EINVAL;
189
190 par->dc_ops->set_palette_reg(info, regno, red, green, blue);
191 }
192
193 return 0;
194}
195
196static int gx1fb_blank(int blank_mode, struct fb_info *info)
197{
198 struct geodefb_par *par = info->par;
199
200 return par->vid_ops->blank_display(info, blank_mode);
201}
202
53eed4ec 203static int __init gx1fb_map_video_memory(struct fb_info *info, struct pci_dev *dev)
1da177e4
LT
204{
205 struct geodefb_par *par = info->par;
206 unsigned gx_base;
207 int fb_len;
53eed4ec 208 int ret;
1da177e4
LT
209
210 gx_base = gx1_gx_base();
211 if (!gx_base)
212 return -ENODEV;
213
53eed4ec
DV
214 ret = pci_enable_device(dev);
215 if (ret < 0)
216 return ret;
1da177e4 217
a80da738 218 ret = pci_request_region(dev, 0, "gx1fb (video)");
53eed4ec
DV
219 if (ret < 0)
220 return ret;
a80da738
DV
221 par->vid_regs = ioremap(pci_resource_start(dev, 0),
222 pci_resource_len(dev, 0));
1da177e4
LT
223 if (!par->vid_regs)
224 return -ENOMEM;
225
53eed4ec
DV
226 if (!request_mem_region(gx_base + 0x8300, 0x100, "gx1fb (display controller)"))
227 return -EBUSY;
1da177e4
LT
228 par->dc_regs = ioremap(gx_base + 0x8300, 0x100);
229 if (!par->dc_regs)
230 return -ENOMEM;
231
1da177e4
LT
232 if ((fb_len = gx1_frame_buffer_size()) < 0)
233 return -ENOMEM;
a80da738 234 info->fix.smem_start = gx_base + 0x800000;
1da177e4
LT
235 info->fix.smem_len = fb_len;
236 info->screen_base = ioremap(info->fix.smem_start, info->fix.smem_len);
237 if (!info->screen_base)
238 return -ENOMEM;
239
53eed4ec
DV
240 dev_info(&dev->dev, "%d Kibyte of video memory at 0x%lx\n",
241 info->fix.smem_len / 1024, info->fix.smem_start);
1da177e4
LT
242
243 return 0;
244}
245
246static int parse_panel_option(struct fb_info *info)
247{
248 struct geodefb_par *par = info->par;
249
250 if (strcmp(panel_option, "") != 0) {
251 int x, y;
252 char *s;
253 x = simple_strtol(panel_option, &s, 10);
254 if (!x)
255 return -EINVAL;
256 y = simple_strtol(s + 1, NULL, 10);
257 if (!y)
258 return -EINVAL;
259 par->panel_x = x;
260 par->panel_y = y;
261 }
262 return 0;
263}
264
265static struct fb_ops gx1fb_ops = {
266 .owner = THIS_MODULE,
267 .fb_check_var = gx1fb_check_var,
268 .fb_set_par = gx1fb_set_par,
269 .fb_setcolreg = gx1fb_setcolreg,
270 .fb_blank = gx1fb_blank,
271 /* No HW acceleration for now. */
272 .fb_fillrect = cfb_fillrect,
273 .fb_copyarea = cfb_copyarea,
274 .fb_imageblit = cfb_imageblit,
1da177e4
LT
275};
276
53eed4ec 277static struct fb_info * __init gx1fb_init_fbinfo(struct device *dev)
1da177e4 278{
1da177e4 279 struct geodefb_par *par;
53eed4ec 280 struct fb_info *info;
1da177e4
LT
281
282 /* Alloc enough space for the pseudo palette. */
53eed4ec 283 info = framebuffer_alloc(sizeof(struct geodefb_par) + sizeof(u32) * 16, dev);
1da177e4
LT
284 if (!info)
285 return NULL;
286
287 par = info->par;
288
289 strcpy(info->fix.id, "GX1");
290
291 info->fix.type = FB_TYPE_PACKED_PIXELS;
292 info->fix.type_aux = 0;
293 info->fix.xpanstep = 0;
294 info->fix.ypanstep = 0;
295 info->fix.ywrapstep = 0;
296 info->fix.accel = FB_ACCEL_NONE;
297
298 info->var.nonstd = 0;
299 info->var.activate = FB_ACTIVATE_NOW;
300 info->var.height = -1;
301 info->var.width = -1;
302 info->var.accel_flags = 0;
303 info->var.vmode = FB_VMODE_NONINTERLACED;
304
305 info->fbops = &gx1fb_ops;
306 info->flags = FBINFO_DEFAULT;
307 info->node = -1;
308
309 info->pseudo_palette = (void *)par + sizeof(struct geodefb_par);
310
311 info->var.grayscale = 0;
312
313 /* CRT and panel options */
314 par->enable_crt = crt_option;
315 if (parse_panel_option(info) < 0)
53eed4ec 316 printk(KERN_WARNING "gx1fb: invalid 'panel' option -- disabling flat panel\n");
1da177e4
LT
317 if (!par->panel_x)
318 par->enable_crt = 1; /* fall back to CRT if no panel is specified */
319
320 return info;
321}
322
53eed4ec 323static int __init gx1fb_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1da177e4 324{
53eed4ec 325 struct geodefb_par *par;
1da177e4 326 struct fb_info *info;
1da177e4
LT
327 int ret;
328
53eed4ec 329 info = gx1fb_init_fbinfo(&pdev->dev);
1da177e4
LT
330 if (!info)
331 return -ENOMEM;
1da177e4
LT
332 par = info->par;
333
334 /* GX1 display controller and CS5530 video device */
335 par->dc_ops = &gx1_dc_ops;
336 par->vid_ops = &cs5530_vid_ops;
337
53eed4ec
DV
338 if ((ret = gx1fb_map_video_memory(info, pdev)) < 0) {
339 dev_err(&pdev->dev, "failed to map frame buffer or controller registers\n");
1da177e4
LT
340 goto err;
341 }
342
53eed4ec
DV
343 ret = fb_find_mode(&info->var, info, mode_option,
344 gx1_modedb, ARRAY_SIZE(gx1_modedb), NULL, 16);
1da177e4 345 if (ret == 0 || ret == 4) {
53eed4ec 346 dev_err(&pdev->dev, "could not find valid video mode\n");
1da177e4
LT
347 ret = -EINVAL;
348 goto err;
349 }
350
351 /* Clear the frame buffer of garbage. */
352 memset_io(info->screen_base, 0, info->fix.smem_len);
353
354 gx1fb_check_var(&info->var, info);
355 gx1fb_set_par(info);
356
357 if (register_framebuffer(info) < 0) {
358 ret = -EINVAL;
359 goto err;
360 }
53eed4ec 361 pci_set_drvdata(pdev, info);
1da177e4
LT
362 printk(KERN_INFO "fb%d: %s frame buffer device\n", info->node, info->fix.id);
363 return 0;
364
365 err:
53eed4ec 366 if (info->screen_base) {
1da177e4 367 iounmap(info->screen_base);
53eed4ec
DV
368 pci_release_region(pdev, 0);
369 }
370 if (par->vid_regs) {
1da177e4 371 iounmap(par->vid_regs);
53eed4ec
DV
372 pci_release_region(pdev, 1);
373 }
374 if (par->dc_regs) {
1da177e4 375 iounmap(par->dc_regs);
53eed4ec
DV
376 release_mem_region(gx1_gx_base() + 0x8300, 0x100);
377 }
378
379 pci_disable_device(pdev);
380
1da177e4
LT
381 if (info)
382 framebuffer_release(info);
383 return ret;
384}
385
53eed4ec 386static void gx1fb_remove(struct pci_dev *pdev)
1da177e4 387{
53eed4ec
DV
388 struct fb_info *info = pci_get_drvdata(pdev);
389 struct geodefb_par *par = info->par;
1da177e4
LT
390
391 unregister_framebuffer(info);
392
393 iounmap((void __iomem *)info->screen_base);
53eed4ec
DV
394 pci_release_region(pdev, 0);
395
1da177e4 396 iounmap(par->vid_regs);
53eed4ec
DV
397 pci_release_region(pdev, 1);
398
1da177e4 399 iounmap(par->dc_regs);
53eed4ec 400 release_mem_region(gx1_gx_base() + 0x8300, 0x100);
1da177e4 401
53eed4ec
DV
402 pci_disable_device(pdev);
403 pci_set_drvdata(pdev, NULL);
1da177e4
LT
404
405 framebuffer_release(info);
406}
407
53eed4ec
DV
408static struct pci_device_id gx1fb_id_table[] = {
409 { PCI_VENDOR_ID_CYRIX, PCI_DEVICE_ID_CYRIX_5530_VIDEO,
410 PCI_ANY_ID, PCI_ANY_ID, PCI_BASE_CLASS_DISPLAY << 16,
411 0xff0000, 0 },
412 { 0, }
413};
414
415MODULE_DEVICE_TABLE(pci, gx1fb_id_table);
416
417static struct pci_driver gx1fb_driver = {
418 .name = "gx1fb",
419 .id_table = gx1fb_id_table,
420 .probe = gx1fb_probe,
421 .remove = gx1fb_remove,
422};
423
424static int __init gx1fb_init(void)
425{
426#ifndef MODULE
427 if (fb_get_options("gx1fb", NULL))
428 return -ENODEV;
429#endif
430 return pci_register_driver(&gx1fb_driver);
431}
432
433static void __exit gx1fb_cleanup(void)
434{
435 pci_unregister_driver(&gx1fb_driver);
436}
437
1da177e4
LT
438module_init(gx1fb_init);
439module_exit(gx1fb_cleanup);
440
441module_param_string(mode, mode_option, sizeof(mode_option), 0444);
442MODULE_PARM_DESC(mode, "video mode (<x>x<y>[-<bpp>][@<refr>])");
443
444module_param_named(crt, crt_option, int, 0444);
445MODULE_PARM_DESC(crt, "enable CRT output. 0 = off, 1 = on (default)");
446
447module_param_string(panel, panel_option, sizeof(panel_option), 0444);
448MODULE_PARM_DESC(panel, "size of attached flat panel (<x>x<y>)");
449
450MODULE_DESCRIPTION("framebuffer driver for the AMD Geode GX1");
451MODULE_LICENSE("GPL");