xilinxfb.c 10 KB

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