xilinxfb.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /*
  2. * xilinxfb.c
  3. *
  4. * Xilinx TFT LCD frame buffer driver
  5. *
  6. * Author: MontaVista Software, Inc.
  7. * source@mvista.com
  8. *
  9. * 2002-2007 (c) MontaVista Software, Inc. This file is licensed under the
  10. * terms of the GNU General Public License version 2. This program is licensed
  11. * "as is" without any warranty of any kind, whether express or implied.
  12. */
  13. /*
  14. * This driver was based on au1100fb.c by MontaVista rewritten for 2.6
  15. * by Embedded Alley Solutions <source@embeddedalley.com>, which in turn
  16. * was based on skeletonfb.c, Skeleton for a frame buffer device by
  17. * Geert Uytterhoeven.
  18. */
  19. #include <linux/module.h>
  20. #include <linux/kernel.h>
  21. #include <linux/version.h>
  22. #include <linux/errno.h>
  23. #include <linux/string.h>
  24. #include <linux/mm.h>
  25. #include <linux/fb.h>
  26. #include <linux/init.h>
  27. #include <linux/dma-mapping.h>
  28. #include <linux/platform_device.h>
  29. #include <asm/io.h>
  30. #include <syslib/virtex_devices.h>
  31. #define DRIVER_NAME "xilinxfb"
  32. #define DRIVER_DESCRIPTION "Xilinx TFT LCD frame buffer driver"
  33. /*
  34. * Xilinx calls it "PLB TFT LCD Controller" though it can also be used for
  35. * the VGA port on the Xilinx ML40x board. This is a hardware display controller
  36. * for a 640x480 resolution TFT or VGA screen.
  37. *
  38. * The interface to the framebuffer is nice and simple. There are two
  39. * control registers. The first tells the LCD interface where in memory
  40. * the frame buffer is (only the 11 most significant bits are used, so
  41. * don't start thinking about scrolling). The second allows the LCD to
  42. * be turned on or off as well as rotated 180 degrees.
  43. */
  44. #define NUM_REGS 2
  45. #define REG_FB_ADDR 0
  46. #define REG_CTRL 1
  47. #define REG_CTRL_ENABLE 0x0001
  48. #define REG_CTRL_ROTATE 0x0002
  49. /*
  50. * The hardware only handles a single mode: 640x480 24 bit true
  51. * color. Each pixel gets a word (32 bits) of memory. Within each word,
  52. * the 8 most significant bits are ignored, the next 8 bits are the red
  53. * level, the next 8 bits are the green level and the 8 least
  54. * significant bits are the blue level. Each row of the LCD uses 1024
  55. * words, but only the first 640 pixels are displayed with the other 384
  56. * words being ignored. There are 480 rows.
  57. */
  58. #define BYTES_PER_PIXEL 4
  59. #define BITS_PER_PIXEL (BYTES_PER_PIXEL * 8)
  60. #define XRES 640
  61. #define YRES 480
  62. #define XRES_VIRTUAL 1024
  63. #define YRES_VIRTUAL YRES
  64. #define LINE_LENGTH (XRES_VIRTUAL * BYTES_PER_PIXEL)
  65. #define FB_SIZE (YRES_VIRTUAL * LINE_LENGTH)
  66. #define RED_SHIFT 16
  67. #define GREEN_SHIFT 8
  68. #define BLUE_SHIFT 0
  69. #define PALETTE_ENTRIES_NO 16 /* passed to fb_alloc_cmap() */
  70. /*
  71. * Here are the default fb_fix_screeninfo and fb_var_screeninfo structures
  72. */
  73. static struct fb_fix_screeninfo xilinx_fb_fix __initdata = {
  74. .id = "Xilinx",
  75. .type = FB_TYPE_PACKED_PIXELS,
  76. .visual = FB_VISUAL_TRUECOLOR,
  77. .smem_len = FB_SIZE,
  78. .line_length = LINE_LENGTH,
  79. .accel = FB_ACCEL_NONE
  80. };
  81. static struct fb_var_screeninfo xilinx_fb_var __initdata = {
  82. .xres = XRES,
  83. .yres = YRES,
  84. .xres_virtual = XRES_VIRTUAL,
  85. .yres_virtual = YRES_VIRTUAL,
  86. .bits_per_pixel = BITS_PER_PIXEL,
  87. .red = { RED_SHIFT, 8, 0 },
  88. .green = { GREEN_SHIFT, 8, 0 },
  89. .blue = { BLUE_SHIFT, 8, 0 },
  90. .transp = { 0, 0, 0 },
  91. .activate = FB_ACTIVATE_NOW
  92. };
  93. struct xilinxfb_drvdata {
  94. struct fb_info info; /* FB driver info record */
  95. u32 regs_phys; /* phys. address of the control registers */
  96. u32 __iomem *regs; /* virt. address of the control registers */
  97. unsigned char __iomem *fb_virt; /* virt. address of the frame buffer */
  98. dma_addr_t fb_phys; /* phys. address of the frame buffer */
  99. u32 reg_ctrl_default;
  100. u32 pseudo_palette[PALETTE_ENTRIES_NO];
  101. /* Fake palette of 16 colors */
  102. };
  103. #define to_xilinxfb_drvdata(_info) \
  104. container_of(_info, struct xilinxfb_drvdata, info)
  105. /*
  106. * The LCD controller has DCR interface to its registers, but all
  107. * the boards and configurations the driver has been tested with
  108. * use opb2dcr bridge. So the registers are seen as memory mapped.
  109. * This macro is to make it simple to add the direct DCR access
  110. * when it's needed.
  111. */
  112. #define xilinx_fb_out_be32(driverdata, offset, val) \
  113. out_be32(driverdata->regs + offset, val)
  114. static int
  115. xilinx_fb_setcolreg(unsigned regno, unsigned red, unsigned green, unsigned blue,
  116. unsigned transp, struct fb_info *fbi)
  117. {
  118. u32 *palette = fbi->pseudo_palette;
  119. if (regno >= PALETTE_ENTRIES_NO)
  120. return -EINVAL;
  121. if (fbi->var.grayscale) {
  122. /* Convert color to grayscale.
  123. * grayscale = 0.30*R + 0.59*G + 0.11*B */
  124. red = green = blue =
  125. (red * 77 + green * 151 + blue * 28 + 127) >> 8;
  126. }
  127. /* fbi->fix.visual is always FB_VISUAL_TRUECOLOR */
  128. /* We only handle 8 bits of each color. */
  129. red >>= 8;
  130. green >>= 8;
  131. blue >>= 8;
  132. palette[regno] = (red << RED_SHIFT) | (green << GREEN_SHIFT) |
  133. (blue << BLUE_SHIFT);
  134. return 0;
  135. }
  136. static int
  137. xilinx_fb_blank(int blank_mode, struct fb_info *fbi)
  138. {
  139. struct xilinxfb_drvdata *drvdata = to_xilinxfb_drvdata(fbi);
  140. switch (blank_mode) {
  141. case FB_BLANK_UNBLANK:
  142. /* turn on panel */
  143. xilinx_fb_out_be32(drvdata, REG_CTRL, drvdata->reg_ctrl_default);
  144. break;
  145. case FB_BLANK_NORMAL:
  146. case FB_BLANK_VSYNC_SUSPEND:
  147. case FB_BLANK_HSYNC_SUSPEND:
  148. case FB_BLANK_POWERDOWN:
  149. /* turn off panel */
  150. xilinx_fb_out_be32(drvdata, REG_CTRL, 0);
  151. default:
  152. break;
  153. }
  154. return 0; /* success */
  155. }
  156. static struct fb_ops xilinxfb_ops =
  157. {
  158. .owner = THIS_MODULE,
  159. .fb_setcolreg = xilinx_fb_setcolreg,
  160. .fb_blank = xilinx_fb_blank,
  161. .fb_fillrect = cfb_fillrect,
  162. .fb_copyarea = cfb_copyarea,
  163. .fb_imageblit = cfb_imageblit,
  164. };
  165. /* === The device driver === */
  166. static int
  167. xilinxfb_drv_probe(struct device *dev)
  168. {
  169. struct platform_device *pdev;
  170. struct xilinxfb_platform_data *pdata;
  171. struct xilinxfb_drvdata *drvdata;
  172. struct resource *regs_res;
  173. int retval;
  174. if (!dev)
  175. return -EINVAL;
  176. pdev = to_platform_device(dev);
  177. pdata = pdev->dev.platform_data;
  178. if (pdata == NULL) {
  179. printk(KERN_ERR "Couldn't find platform data.\n");
  180. return -EFAULT;
  181. }
  182. drvdata = kzalloc(sizeof(*drvdata), GFP_KERNEL);
  183. if (!drvdata) {
  184. printk(KERN_ERR "Couldn't allocate device private record\n");
  185. return -ENOMEM;
  186. }
  187. dev_set_drvdata(dev, drvdata);
  188. /* Map the control registers in */
  189. regs_res = platform_get_resource(pdev, IORESOURCE_IO, 0);
  190. if (!regs_res || (regs_res->end - regs_res->start + 1 < 8)) {
  191. printk(KERN_ERR "Couldn't get registers resource\n");
  192. retval = -EFAULT;
  193. goto failed1;
  194. }
  195. if (!request_mem_region(regs_res->start, 8, DRIVER_NAME)) {
  196. printk(KERN_ERR
  197. "Couldn't lock memory region at 0x%08X\n",
  198. regs_res->start);
  199. retval = -EBUSY;
  200. goto failed1;
  201. }
  202. drvdata->regs = (u32 __iomem*) ioremap(regs_res->start, 8);
  203. drvdata->regs_phys = regs_res->start;
  204. /* Allocate the framebuffer memory */
  205. drvdata->fb_virt = dma_alloc_coherent(dev, PAGE_ALIGN(FB_SIZE),
  206. &drvdata->fb_phys, GFP_KERNEL);
  207. if (!drvdata->fb_virt) {
  208. printk(KERN_ERR "Could not allocate frame buffer memory\n");
  209. retval = -ENOMEM;
  210. goto failed2;
  211. }
  212. /* Clear (turn to black) the framebuffer */
  213. memset_io((void *) drvdata->fb_virt, 0, FB_SIZE);
  214. /* Tell the hardware where the frame buffer is */
  215. xilinx_fb_out_be32(drvdata, REG_FB_ADDR, drvdata->fb_phys);
  216. /* Turn on the display */
  217. if (pdata->rotate_screen) {
  218. drvdata->reg_ctrl_default = REG_CTRL_ENABLE | REG_CTRL_ROTATE;
  219. } else {
  220. drvdata->reg_ctrl_default = REG_CTRL_ENABLE;
  221. }
  222. xilinx_fb_out_be32(drvdata, REG_CTRL, drvdata->reg_ctrl_default);
  223. /* Fill struct fb_info */
  224. drvdata->info.device = dev;
  225. drvdata->info.screen_base = drvdata->fb_virt;
  226. drvdata->info.fbops = &xilinxfb_ops;
  227. drvdata->info.fix = xilinx_fb_fix;
  228. drvdata->info.fix.smem_start = drvdata->fb_phys;
  229. drvdata->info.pseudo_palette = drvdata->pseudo_palette;
  230. if (fb_alloc_cmap(&drvdata->info.cmap, PALETTE_ENTRIES_NO, 0) < 0) {
  231. printk(KERN_ERR "Fail to allocate colormap (%d entries)\n",
  232. PALETTE_ENTRIES_NO);
  233. retval = -EFAULT;
  234. goto failed3;
  235. }
  236. drvdata->info.flags = FBINFO_DEFAULT;
  237. xilinx_fb_var.height = pdata->screen_height_mm;
  238. xilinx_fb_var.width = pdata->screen_width_mm;
  239. drvdata->info.var = xilinx_fb_var;
  240. /* Register new frame buffer */
  241. if (register_framebuffer(&drvdata->info) < 0) {
  242. printk(KERN_ERR "Could not register frame buffer\n");
  243. retval = -EINVAL;
  244. goto failed4;
  245. }
  246. return 0; /* success */
  247. failed4:
  248. fb_dealloc_cmap(&drvdata->info.cmap);
  249. failed3:
  250. dma_free_coherent(dev, PAGE_ALIGN(FB_SIZE), drvdata->fb_virt,
  251. drvdata->fb_phys);
  252. /* Turn off the display */
  253. xilinx_fb_out_be32(drvdata, REG_CTRL, 0);
  254. iounmap(drvdata->regs);
  255. failed2:
  256. release_mem_region(regs_res->start, 8);
  257. failed1:
  258. kfree(drvdata);
  259. dev_set_drvdata(dev, NULL);
  260. return retval;
  261. }
  262. static int
  263. xilinxfb_drv_remove(struct device *dev)
  264. {
  265. struct xilinxfb_drvdata *drvdata;
  266. if (!dev)
  267. return -ENODEV;
  268. drvdata = (struct xilinxfb_drvdata *) dev_get_drvdata(dev);
  269. #if !defined(CONFIG_FRAMEBUFFER_CONSOLE) && defined(CONFIG_LOGO)
  270. xilinx_fb_blank(VESA_POWERDOWN, &drvdata->info);
  271. #endif
  272. unregister_framebuffer(&drvdata->info);
  273. fb_dealloc_cmap(&drvdata->info.cmap);
  274. dma_free_coherent(dev, PAGE_ALIGN(FB_SIZE), drvdata->fb_virt,
  275. drvdata->fb_phys);
  276. /* Turn off the display */
  277. xilinx_fb_out_be32(drvdata, REG_CTRL, 0);
  278. iounmap(drvdata->regs);
  279. release_mem_region(drvdata->regs_phys, 8);
  280. kfree(drvdata);
  281. dev_set_drvdata(dev, NULL);
  282. return 0;
  283. }
  284. static struct device_driver xilinxfb_driver = {
  285. .name = DRIVER_NAME,
  286. .bus = &platform_bus_type,
  287. .probe = xilinxfb_drv_probe,
  288. .remove = xilinxfb_drv_remove
  289. };
  290. static int __init
  291. xilinxfb_init(void)
  292. {
  293. /*
  294. * No kernel boot options used,
  295. * so we just need to register the driver
  296. */
  297. return driver_register(&xilinxfb_driver);
  298. }
  299. static void __exit
  300. xilinxfb_cleanup(void)
  301. {
  302. driver_unregister(&xilinxfb_driver);
  303. }
  304. module_init(xilinxfb_init);
  305. module_exit(xilinxfb_cleanup);
  306. MODULE_AUTHOR("MontaVista Software, Inc. <source@mvista.com>");
  307. MODULE_DESCRIPTION(DRIVER_DESCRIPTION);
  308. MODULE_LICENSE("GPL");