[PATCH] hwmon: hwmon vs i2c, second round (03/11)
[linux-2.6] / drivers / video / geode / gx1fb_core.c
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
29 static char mode_option[32] = "640x480-16@60";
30 static int  crt_option = 1;
31 static char panel_option[32] = "";
32
33 static int gx1_line_delta(int xres, int bpp)
34 {
35         int line_delta = xres * (bpp >> 3);
36
37         if (line_delta > 2048)
38                 line_delta = 4096;
39         else if (line_delta > 1024)
40                 line_delta = 2048;
41         else
42                 line_delta = 1024;
43         return line_delta;
44 }
45
46 static int gx1fb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
47 {
48         struct geodefb_par *par = info->par;
49
50         printk(KERN_DEBUG "%s()\n", __FUNCTION__);
51
52         /* Maximum resolution is 1280x1024. */
53         if (var->xres > 1280 || var->yres > 1024)
54                 return -EINVAL;
55
56         if (par->panel_x && (var->xres > par->panel_x || var->yres > par->panel_y))
57                 return -EINVAL;
58
59         /* Only 16 bpp and 8 bpp is supported by the hardware. */
60         if (var->bits_per_pixel == 16) {
61                 var->red.offset   = 11; var->red.length   = 5;
62                 var->green.offset =  5; var->green.length = 6;
63                 var->blue.offset  =  0; var->blue.length  = 5;
64                 var->transp.offset = 0; var->transp.length = 0;
65         } else if (var->bits_per_pixel == 8) {
66                 var->red.offset   = 0; var->red.length   = 8;
67                 var->green.offset = 0; var->green.length = 8;
68                 var->blue.offset  = 0; var->blue.length  = 8;
69                 var->transp.offset = 0; var->transp.length = 0;
70         } else
71                 return -EINVAL;
72
73         /* Enough video memory? */
74         if (gx1_line_delta(var->xres, var->bits_per_pixel) * var->yres > info->fix.smem_len)
75                 return -EINVAL;
76
77         /* FIXME: Check timing parameters here? */
78
79         return 0;
80 }
81
82 static int gx1fb_set_par(struct fb_info *info)
83 {
84         struct geodefb_par *par = info->par;
85
86         if (info->var.bits_per_pixel == 16) {
87                 info->fix.visual = FB_VISUAL_TRUECOLOR;
88                 fb_dealloc_cmap(&info->cmap);
89         } else {
90                 info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
91                 fb_alloc_cmap(&info->cmap, 1<<info->var.bits_per_pixel, 0);
92         }
93
94         info->fix.line_length = gx1_line_delta(info->var.xres, info->var.bits_per_pixel);
95
96         par->dc_ops->set_mode(info);
97
98         return 0;
99 }
100
101 static inline u_int chan_to_field(u_int chan, struct fb_bitfield *bf)
102 {
103         chan &= 0xffff;
104         chan >>= 16 - bf->length;
105         return chan << bf->offset;
106 }
107
108 static int gx1fb_setcolreg(unsigned regno, unsigned red, unsigned green,
109                            unsigned blue, unsigned transp,
110                            struct fb_info *info)
111 {
112         struct geodefb_par *par = info->par;
113
114         if (info->var.grayscale) {
115                 /* grayscale = 0.30*R + 0.59*G + 0.11*B */
116                 red = green = blue = (red * 77 + green * 151 + blue * 28) >> 8;
117         }
118
119         /* Truecolor has hardware independent palette */
120         if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
121                 u32 *pal = info->pseudo_palette;
122                 u32 v;
123
124                 if (regno >= 16)
125                         return -EINVAL;
126
127                 v  = chan_to_field(red, &info->var.red);
128                 v |= chan_to_field(green, &info->var.green);
129                 v |= chan_to_field(blue, &info->var.blue);
130
131                 pal[regno] = v;
132         } else {
133                 if (regno >= 256)
134                         return -EINVAL;
135
136                 par->dc_ops->set_palette_reg(info, regno, red, green, blue);
137         }
138
139         return 0;
140 }
141
142 static int gx1fb_blank(int blank_mode, struct fb_info *info)
143 {
144         struct geodefb_par *par = info->par;
145
146         return par->vid_ops->blank_display(info, blank_mode);
147 }
148
149 static int __init gx1fb_map_video_memory(struct fb_info *info)
150 {
151         struct geodefb_par *par = info->par;
152         unsigned gx_base;
153         int fb_len;
154
155         gx_base = gx1_gx_base();
156         if (!gx_base)
157                 return -ENODEV;
158
159         par->vid_dev = pci_get_device(PCI_VENDOR_ID_CYRIX,
160                                       PCI_DEVICE_ID_CYRIX_5530_VIDEO, NULL);
161         if (!par->vid_dev)
162                 return -ENODEV;
163
164         par->vid_regs = ioremap(pci_resource_start(par->vid_dev, 1),
165                                 pci_resource_len(par->vid_dev, 1));
166         if (!par->vid_regs)
167                 return -ENOMEM;
168
169         par->dc_regs = ioremap(gx_base + 0x8300, 0x100);
170         if (!par->dc_regs)
171                 return -ENOMEM;
172
173         info->fix.smem_start = gx_base + 0x800000;
174         if ((fb_len = gx1_frame_buffer_size()) < 0)
175                 return -ENOMEM;
176         info->fix.smem_len = fb_len;
177         info->screen_base = ioremap(info->fix.smem_start, info->fix.smem_len);
178         if (!info->screen_base)
179                 return -ENOMEM;
180
181         printk(KERN_INFO "%s: %d Kibyte of video memory at 0x%lx\n",
182                info->fix.id, info->fix.smem_len / 1024, info->fix.smem_start);
183
184         return 0;
185 }
186
187 static int parse_panel_option(struct fb_info *info)
188 {
189         struct geodefb_par *par = info->par;
190
191         if (strcmp(panel_option, "") != 0) {
192                 int x, y;
193                 char *s;
194                 x = simple_strtol(panel_option, &s, 10);
195                 if (!x)
196                         return -EINVAL;
197                 y = simple_strtol(s + 1, NULL, 10);
198                 if (!y)
199                         return -EINVAL;
200                 par->panel_x = x;
201                 par->panel_y = y;
202         }
203         return 0;
204 }
205
206 static struct fb_ops gx1fb_ops = {
207         .owner          = THIS_MODULE,
208         .fb_check_var   = gx1fb_check_var,
209         .fb_set_par     = gx1fb_set_par,
210         .fb_setcolreg   = gx1fb_setcolreg,
211         .fb_blank       = gx1fb_blank,
212         /* No HW acceleration for now. */
213         .fb_fillrect    = cfb_fillrect,
214         .fb_copyarea    = cfb_copyarea,
215         .fb_imageblit   = cfb_imageblit,
216         .fb_cursor      = soft_cursor,
217 };
218
219 static struct fb_info * __init gx1fb_init_fbinfo(void)
220 {
221         struct fb_info *info;
222         struct geodefb_par *par;
223
224         /* Alloc enough space for the pseudo palette. */
225         info = framebuffer_alloc(sizeof(struct geodefb_par) + sizeof(u32) * 16, NULL);
226         if (!info)
227                 return NULL;
228
229         par = info->par;
230
231         strcpy(info->fix.id, "GX1");
232
233         info->fix.type          = FB_TYPE_PACKED_PIXELS;
234         info->fix.type_aux      = 0;
235         info->fix.xpanstep      = 0;
236         info->fix.ypanstep      = 0;
237         info->fix.ywrapstep     = 0;
238         info->fix.accel         = FB_ACCEL_NONE;
239
240         info->var.nonstd        = 0;
241         info->var.activate      = FB_ACTIVATE_NOW;
242         info->var.height        = -1;
243         info->var.width = -1;
244         info->var.accel_flags = 0;
245         info->var.vmode = FB_VMODE_NONINTERLACED;
246
247         info->fbops             = &gx1fb_ops;
248         info->flags             = FBINFO_DEFAULT;
249         info->node              = -1;
250
251         info->pseudo_palette    = (void *)par + sizeof(struct geodefb_par);
252
253         info->var.grayscale     = 0;
254
255         /* CRT and panel options */
256         par->enable_crt = crt_option;
257         if (parse_panel_option(info) < 0)
258                 printk(KERN_WARNING "%s: invalid 'panel' option -- disabling flat panel\n",
259                        info->fix.id);
260         if (!par->panel_x)
261                 par->enable_crt = 1; /* fall back to CRT if no panel is specified */
262
263         return info;
264 }
265
266
267 static struct fb_info *gx1fb_info;
268
269 static int __init gx1fb_init(void)
270 {
271         struct fb_info *info;
272         struct geodefb_par *par;
273         int ret;
274
275 #ifndef MODULE
276         if (fb_get_options("gx1fb", NULL))
277                 return -ENODEV;
278 #endif
279
280         info = gx1fb_init_fbinfo();
281         if (!info)
282                 return -ENOMEM;
283         gx1fb_info = info;
284
285         par = info->par;
286
287         /* GX1 display controller and CS5530 video device */
288         par->dc_ops  = &gx1_dc_ops;
289         par->vid_ops = &cs5530_vid_ops;
290
291         if ((ret = gx1fb_map_video_memory(info)) < 0) {
292                 printk(KERN_ERR "%s: gx1fb_map_video_memory() failed\n", info->fix.id);
293                 goto err;
294         }
295
296         ret = fb_find_mode(&info->var, info, mode_option, NULL, 0, NULL, 16);
297         if (ret == 0 || ret == 4) {
298                 printk(KERN_ERR "%s: could not find valid video mode\n", info->fix.id);
299                 ret = -EINVAL;
300                 goto err;
301         }
302
303         /* Clear the frame buffer of garbage. */
304         memset_io(info->screen_base, 0, info->fix.smem_len);
305
306         gx1fb_check_var(&info->var, info);
307         gx1fb_set_par(info);
308
309         if (register_framebuffer(info) < 0) {
310                 ret = -EINVAL;
311                 goto err;
312         }
313         printk(KERN_INFO "fb%d: %s frame buffer device\n", info->node, info->fix.id);
314         return 0;
315
316   err:
317         if (info->screen_base)
318                 iounmap(info->screen_base);
319         if (par->vid_regs)
320                 iounmap(par->vid_regs);
321         if (par->dc_regs)
322                 iounmap(par->dc_regs);
323         if (par->vid_dev)
324                 pci_dev_put(par->vid_dev);
325         if (info)
326                 framebuffer_release(info);
327         return ret;
328 }
329
330 static void __exit gx1fb_cleanup(void)
331 {
332         struct fb_info *info = gx1fb_info;
333         struct geodefb_par *par = gx1fb_info->par;
334
335         unregister_framebuffer(info);
336
337         iounmap((void __iomem *)info->screen_base);
338         iounmap(par->vid_regs);
339         iounmap(par->dc_regs);
340
341         pci_dev_put(par->vid_dev);
342
343         framebuffer_release(info);
344 }
345
346 module_init(gx1fb_init);
347 module_exit(gx1fb_cleanup);
348
349 module_param_string(mode, mode_option, sizeof(mode_option), 0444);
350 MODULE_PARM_DESC(mode, "video mode (<x>x<y>[-<bpp>][@<refr>])");
351
352 module_param_named(crt, crt_option, int, 0444);
353 MODULE_PARM_DESC(crt, "enable CRT output. 0 = off, 1 = on (default)");
354
355 module_param_string(panel, panel_option, sizeof(panel_option), 0444);
356 MODULE_PARM_DESC(panel, "size of attached flat panel (<x>x<y>)");
357
358 MODULE_DESCRIPTION("framebuffer driver for the AMD Geode GX1");
359 MODULE_LICENSE("GPL");