blob: 2794ba11f33251c9b95a0a1099f3c68fcb89bbda [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/delay.h>
19#include <linux/fb.h>
20#include <linux/init.h>
21#include <linux/pci.h>
22
23#include "geodefb.h"
24#include "display_gx1.h"
25#include "video_cs5530.h"
26
27static char mode_option[32] = "640x480-16@60";
28static int crt_option = 1;
29static char panel_option[32] = "";
30
David Vrabel53eed4e2005-09-09 13:04:32 -070031/* Modes relevant to the GX1 (taken from modedb.c) */
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -080032static const struct fb_videomode gx1_modedb[] = {
David Vrabel53eed4e2005-09-09 13:04:32 -070033 /* 640x480-60 VESA */
34 { NULL, 60, 640, 480, 39682, 48, 16, 33, 10, 96, 2,
35 0, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
36 /* 640x480-75 VESA */
37 { NULL, 75, 640, 480, 31746, 120, 16, 16, 01, 64, 3,
38 0, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
39 /* 640x480-85 VESA */
40 { NULL, 85, 640, 480, 27777, 80, 56, 25, 01, 56, 3,
41 0, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
42 /* 800x600-60 VESA */
43 { NULL, 60, 800, 600, 25000, 88, 40, 23, 01, 128, 4,
44 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
45 FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
46 /* 800x600-75 VESA */
47 { NULL, 75, 800, 600, 20202, 160, 16, 21, 01, 80, 3,
48 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
49 FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
50 /* 800x600-85 VESA */
51 { NULL, 85, 800, 600, 17761, 152, 32, 27, 01, 64, 3,
52 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
53 FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
54 /* 1024x768-60 VESA */
55 { NULL, 60, 1024, 768, 15384, 160, 24, 29, 3, 136, 6,
56 0, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
57 /* 1024x768-75 VESA */
58 { NULL, 75, 1024, 768, 12690, 176, 16, 28, 1, 96, 3,
59 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
60 FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
61 /* 1024x768-85 VESA */
62 { NULL, 85, 1024, 768, 10582, 208, 48, 36, 1, 96, 3,
63 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
64 FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
65 /* 1280x960-60 VESA */
66 { NULL, 60, 1280, 960, 9259, 312, 96, 36, 1, 112, 3,
67 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
68 FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
69 /* 1280x960-85 VESA */
70 { NULL, 85, 1280, 960, 6734, 224, 64, 47, 1, 160, 3,
71 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
72 FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
73 /* 1280x1024-60 VESA */
74 { NULL, 60, 1280, 1024, 9259, 248, 48, 38, 1, 112, 3,
75 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
76 FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
77 /* 1280x1024-75 VESA */
78 { NULL, 75, 1280, 1024, 7407, 248, 16, 38, 1, 144, 3,
79 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
80 FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
81 /* 1280x1024-85 VESA */
82 { NULL, 85, 1280, 1024, 6349, 224, 64, 44, 1, 160, 3,
83 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
84 FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
85};
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087static int gx1_line_delta(int xres, int bpp)
88{
89 int line_delta = xres * (bpp >> 3);
90
91 if (line_delta > 2048)
92 line_delta = 4096;
93 else if (line_delta > 1024)
94 line_delta = 2048;
95 else
96 line_delta = 1024;
97 return line_delta;
98}
99
100static int gx1fb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
101{
102 struct geodefb_par *par = info->par;
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 /* Maximum resolution is 1280x1024. */
105 if (var->xres > 1280 || var->yres > 1024)
106 return -EINVAL;
107
108 if (par->panel_x && (var->xres > par->panel_x || var->yres > par->panel_y))
109 return -EINVAL;
110
111 /* Only 16 bpp and 8 bpp is supported by the hardware. */
112 if (var->bits_per_pixel == 16) {
113 var->red.offset = 11; var->red.length = 5;
114 var->green.offset = 5; var->green.length = 6;
115 var->blue.offset = 0; var->blue.length = 5;
116 var->transp.offset = 0; var->transp.length = 0;
117 } else if (var->bits_per_pixel == 8) {
118 var->red.offset = 0; var->red.length = 8;
119 var->green.offset = 0; var->green.length = 8;
120 var->blue.offset = 0; var->blue.length = 8;
121 var->transp.offset = 0; var->transp.length = 0;
122 } else
123 return -EINVAL;
124
125 /* Enough video memory? */
126 if (gx1_line_delta(var->xres, var->bits_per_pixel) * var->yres > info->fix.smem_len)
127 return -EINVAL;
128
129 /* FIXME: Check timing parameters here? */
130
131 return 0;
132}
133
134static int gx1fb_set_par(struct fb_info *info)
135{
136 struct geodefb_par *par = info->par;
137
Andres Salomon35887b12009-02-11 13:04:23 -0800138 if (info->var.bits_per_pixel == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 info->fix.visual = FB_VISUAL_TRUECOLOR;
Andres Salomon35887b12009-02-11 13:04:23 -0800140 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143 info->fix.line_length = gx1_line_delta(info->var.xres, info->var.bits_per_pixel);
144
145 par->dc_ops->set_mode(info);
146
147 return 0;
148}
149
150static inline u_int chan_to_field(u_int chan, struct fb_bitfield *bf)
151{
152 chan &= 0xffff;
153 chan >>= 16 - bf->length;
154 return chan << bf->offset;
155}
156
157static int gx1fb_setcolreg(unsigned regno, unsigned red, unsigned green,
158 unsigned blue, unsigned transp,
159 struct fb_info *info)
160{
161 struct geodefb_par *par = info->par;
162
163 if (info->var.grayscale) {
164 /* grayscale = 0.30*R + 0.59*G + 0.11*B */
165 red = green = blue = (red * 77 + green * 151 + blue * 28) >> 8;
166 }
167
168 /* Truecolor has hardware independent palette */
169 if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
170 u32 *pal = info->pseudo_palette;
171 u32 v;
172
173 if (regno >= 16)
174 return -EINVAL;
175
176 v = chan_to_field(red, &info->var.red);
177 v |= chan_to_field(green, &info->var.green);
178 v |= chan_to_field(blue, &info->var.blue);
179
180 pal[regno] = v;
181 } else {
182 if (regno >= 256)
183 return -EINVAL;
184
185 par->dc_ops->set_palette_reg(info, regno, red, green, blue);
186 }
187
188 return 0;
189}
190
191static int gx1fb_blank(int blank_mode, struct fb_info *info)
192{
193 struct geodefb_par *par = info->par;
194
195 return par->vid_ops->blank_display(info, blank_mode);
196}
197
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -0800198static int gx1fb_map_video_memory(struct fb_info *info, struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
200 struct geodefb_par *par = info->par;
201 unsigned gx_base;
202 int fb_len;
David Vrabel53eed4e2005-09-09 13:04:32 -0700203 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205 gx_base = gx1_gx_base();
206 if (!gx_base)
207 return -ENODEV;
208
David Vrabel53eed4e2005-09-09 13:04:32 -0700209 ret = pci_enable_device(dev);
210 if (ret < 0)
211 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
David Vrabela80da732006-01-14 13:21:23 -0800213 ret = pci_request_region(dev, 0, "gx1fb (video)");
David Vrabel53eed4e2005-09-09 13:04:32 -0700214 if (ret < 0)
215 return ret;
Arjan van de Ven3c36aa52009-01-06 14:42:28 -0800216 par->vid_regs = pci_ioremap_bar(dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 if (!par->vid_regs)
218 return -ENOMEM;
219
David Vrabel53eed4e2005-09-09 13:04:32 -0700220 if (!request_mem_region(gx_base + 0x8300, 0x100, "gx1fb (display controller)"))
221 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 par->dc_regs = ioremap(gx_base + 0x8300, 0x100);
223 if (!par->dc_regs)
224 return -ENOMEM;
225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 if ((fb_len = gx1_frame_buffer_size()) < 0)
227 return -ENOMEM;
David Vrabela80da732006-01-14 13:21:23 -0800228 info->fix.smem_start = gx_base + 0x800000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 info->fix.smem_len = fb_len;
230 info->screen_base = ioremap(info->fix.smem_start, info->fix.smem_len);
231 if (!info->screen_base)
232 return -ENOMEM;
233
David Vrabel53eed4e2005-09-09 13:04:32 -0700234 dev_info(&dev->dev, "%d Kibyte of video memory at 0x%lx\n",
235 info->fix.smem_len / 1024, info->fix.smem_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237 return 0;
238}
239
240static int parse_panel_option(struct fb_info *info)
241{
242 struct geodefb_par *par = info->par;
243
244 if (strcmp(panel_option, "") != 0) {
245 int x, y;
246 char *s;
247 x = simple_strtol(panel_option, &s, 10);
248 if (!x)
249 return -EINVAL;
250 y = simple_strtol(s + 1, NULL, 10);
251 if (!y)
252 return -EINVAL;
253 par->panel_x = x;
254 par->panel_y = y;
255 }
256 return 0;
257}
258
259static struct fb_ops gx1fb_ops = {
260 .owner = THIS_MODULE,
261 .fb_check_var = gx1fb_check_var,
262 .fb_set_par = gx1fb_set_par,
263 .fb_setcolreg = gx1fb_setcolreg,
264 .fb_blank = gx1fb_blank,
265 /* No HW acceleration for now. */
266 .fb_fillrect = cfb_fillrect,
267 .fb_copyarea = cfb_copyarea,
268 .fb_imageblit = cfb_imageblit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269};
270
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -0800271static struct fb_info *gx1fb_init_fbinfo(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 struct geodefb_par *par;
David Vrabel53eed4e2005-09-09 13:04:32 -0700274 struct fb_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276 /* Alloc enough space for the pseudo palette. */
David Vrabel53eed4e2005-09-09 13:04:32 -0700277 info = framebuffer_alloc(sizeof(struct geodefb_par) + sizeof(u32) * 16, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 if (!info)
279 return NULL;
280
281 par = info->par;
282
283 strcpy(info->fix.id, "GX1");
284
285 info->fix.type = FB_TYPE_PACKED_PIXELS;
286 info->fix.type_aux = 0;
287 info->fix.xpanstep = 0;
288 info->fix.ypanstep = 0;
289 info->fix.ywrapstep = 0;
290 info->fix.accel = FB_ACCEL_NONE;
291
292 info->var.nonstd = 0;
293 info->var.activate = FB_ACTIVATE_NOW;
294 info->var.height = -1;
295 info->var.width = -1;
296 info->var.accel_flags = 0;
297 info->var.vmode = FB_VMODE_NONINTERLACED;
298
299 info->fbops = &gx1fb_ops;
300 info->flags = FBINFO_DEFAULT;
301 info->node = -1;
302
303 info->pseudo_palette = (void *)par + sizeof(struct geodefb_par);
304
305 info->var.grayscale = 0;
306
307 /* CRT and panel options */
308 par->enable_crt = crt_option;
309 if (parse_panel_option(info) < 0)
David Vrabel53eed4e2005-09-09 13:04:32 -0700310 printk(KERN_WARNING "gx1fb: invalid 'panel' option -- disabling flat panel\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 if (!par->panel_x)
312 par->enable_crt = 1; /* fall back to CRT if no panel is specified */
313
Andres Salomon35887b12009-02-11 13:04:23 -0800314 if (fb_alloc_cmap(&info->cmap, 256, 0) < 0) {
315 framebuffer_release(info);
316 return NULL;
317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 return info;
319}
320
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -0800321static int gx1fb_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
David Vrabel53eed4e2005-09-09 13:04:32 -0700323 struct geodefb_par *par;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 struct fb_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 int ret;
326
David Vrabel53eed4e2005-09-09 13:04:32 -0700327 info = gx1fb_init_fbinfo(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 if (!info)
329 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 par = info->par;
331
332 /* GX1 display controller and CS5530 video device */
333 par->dc_ops = &gx1_dc_ops;
334 par->vid_ops = &cs5530_vid_ops;
335
David Vrabel53eed4e2005-09-09 13:04:32 -0700336 if ((ret = gx1fb_map_video_memory(info, pdev)) < 0) {
337 dev_err(&pdev->dev, "failed to map frame buffer or controller registers\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 goto err;
339 }
340
David Vrabel53eed4e2005-09-09 13:04:32 -0700341 ret = fb_find_mode(&info->var, info, mode_option,
342 gx1_modedb, ARRAY_SIZE(gx1_modedb), NULL, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 if (ret == 0 || ret == 4) {
David Vrabel53eed4e2005-09-09 13:04:32 -0700344 dev_err(&pdev->dev, "could not find valid video mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 ret = -EINVAL;
346 goto err;
347 }
348
349 /* Clear the frame buffer of garbage. */
350 memset_io(info->screen_base, 0, info->fix.smem_len);
351
352 gx1fb_check_var(&info->var, info);
353 gx1fb_set_par(info);
354
355 if (register_framebuffer(info) < 0) {
356 ret = -EINVAL;
357 goto err;
358 }
David Vrabel53eed4e2005-09-09 13:04:32 -0700359 pci_set_drvdata(pdev, info);
Joe Perches31b67802013-09-19 18:35:55 -0700360 fb_info(info, "%s frame buffer device\n", info->fix.id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 return 0;
362
363 err:
David Vrabel53eed4e2005-09-09 13:04:32 -0700364 if (info->screen_base) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 iounmap(info->screen_base);
David Vrabel53eed4e2005-09-09 13:04:32 -0700366 pci_release_region(pdev, 0);
367 }
368 if (par->vid_regs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 iounmap(par->vid_regs);
David Vrabel53eed4e2005-09-09 13:04:32 -0700370 pci_release_region(pdev, 1);
371 }
372 if (par->dc_regs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 iounmap(par->dc_regs);
David Vrabel53eed4e2005-09-09 13:04:32 -0700374 release_mem_region(gx1_gx_base() + 0x8300, 0x100);
375 }
376
Andres Salomon35887b12009-02-11 13:04:23 -0800377 if (info) {
378 fb_dealloc_cmap(&info->cmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 framebuffer_release(info);
Andres Salomon35887b12009-02-11 13:04:23 -0800380 }
381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 return ret;
383}
384
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -0800385static void gx1fb_remove(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
David Vrabel53eed4e2005-09-09 13:04:32 -0700387 struct fb_info *info = pci_get_drvdata(pdev);
388 struct geodefb_par *par = info->par;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
390 unregister_framebuffer(info);
391
392 iounmap((void __iomem *)info->screen_base);
David Vrabel53eed4e2005-09-09 13:04:32 -0700393 pci_release_region(pdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
David Vrabel53eed4e2005-09-09 13:04:32 -0700395 iounmap(par->vid_regs);
396 pci_release_region(pdev, 1);
397
398 iounmap(par->dc_regs);
399 release_mem_region(gx1_gx_base() + 0x8300, 0x100);
400
Andres Salomon35887b12009-02-11 13:04:23 -0800401 fb_dealloc_cmap(&info->cmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403 framebuffer_release(info);
404}
405
Alexey Dobriyanc7b54472007-02-12 00:55:02 -0800406#ifndef MODULE
407static void __init gx1fb_setup(char *options)
408{
409 char *this_opt;
410
411 if (!options || !*options)
412 return;
413
414 while ((this_opt = strsep(&options, ","))) {
415 if (!*this_opt)
416 continue;
417
418 if (!strncmp(this_opt, "mode:", 5))
419 strlcpy(mode_option, this_opt + 5, sizeof(mode_option));
420 else if (!strncmp(this_opt, "crt:", 4))
421 crt_option = !!simple_strtoul(this_opt + 4, NULL, 0);
422 else if (!strncmp(this_opt, "panel:", 6))
423 strlcpy(panel_option, this_opt + 6, sizeof(panel_option));
424 else
425 strlcpy(mode_option, this_opt, sizeof(mode_option));
426 }
427}
428#endif
429
David Vrabel53eed4e2005-09-09 13:04:32 -0700430static struct pci_device_id gx1fb_id_table[] = {
431 { PCI_VENDOR_ID_CYRIX, PCI_DEVICE_ID_CYRIX_5530_VIDEO,
432 PCI_ANY_ID, PCI_ANY_ID, PCI_BASE_CLASS_DISPLAY << 16,
433 0xff0000, 0 },
434 { 0, }
435};
436
437MODULE_DEVICE_TABLE(pci, gx1fb_id_table);
438
439static struct pci_driver gx1fb_driver = {
440 .name = "gx1fb",
441 .id_table = gx1fb_id_table,
442 .probe = gx1fb_probe,
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -0800443 .remove = gx1fb_remove,
David Vrabel53eed4e2005-09-09 13:04:32 -0700444};
445
446static int __init gx1fb_init(void)
447{
448#ifndef MODULE
Alexey Dobriyanc7b54472007-02-12 00:55:02 -0800449 char *option = NULL;
450
451 if (fb_get_options("gx1fb", &option))
David Vrabel53eed4e2005-09-09 13:04:32 -0700452 return -ENODEV;
Alexey Dobriyanc7b54472007-02-12 00:55:02 -0800453 gx1fb_setup(option);
David Vrabel53eed4e2005-09-09 13:04:32 -0700454#endif
455 return pci_register_driver(&gx1fb_driver);
456}
457
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -0800458static void gx1fb_cleanup(void)
David Vrabel53eed4e2005-09-09 13:04:32 -0700459{
460 pci_unregister_driver(&gx1fb_driver);
461}
462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463module_init(gx1fb_init);
464module_exit(gx1fb_cleanup);
465
466module_param_string(mode, mode_option, sizeof(mode_option), 0444);
467MODULE_PARM_DESC(mode, "video mode (<x>x<y>[-<bpp>][@<refr>])");
468
469module_param_named(crt, crt_option, int, 0444);
470MODULE_PARM_DESC(crt, "enable CRT output. 0 = off, 1 = on (default)");
471
472module_param_string(panel, panel_option, sizeof(panel_option), 0444);
473MODULE_PARM_DESC(panel, "size of attached flat panel (<x>x<y>)");
474
475MODULE_DESCRIPTION("framebuffer driver for the AMD Geode GX1");
476MODULE_LICENSE("GPL");