gx1fb_core.c 12 KB

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