xilinxfb.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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.
  10. * 2007 (c) Secret Lab Technologies, Ltd.
  11. *
  12. * This file is licensed under the terms of the GNU General Public License
  13. * version 2. This program is licensed "as is" without any warranty of any
  14. * kind, whether express or implied.
  15. */
  16. /*
  17. * This driver was based on au1100fb.c by MontaVista rewritten for 2.6
  18. * by Embedded Alley Solutions <source@embeddedalley.com>, which in turn
  19. * was based on skeletonfb.c, Skeleton for a frame buffer device by
  20. * Geert Uytterhoeven.
  21. */
  22. #include <linux/device.h>
  23. #include <linux/module.h>
  24. #include <linux/kernel.h>
  25. #include <linux/version.h>
  26. #include <linux/errno.h>
  27. #include <linux/string.h>
  28. #include <linux/mm.h>
  29. #include <linux/fb.h>
  30. #include <linux/init.h>
  31. #include <linux/dma-mapping.h>
  32. #include <linux/platform_device.h>
  33. #if defined(CONFIG_OF)
  34. #include <linux/of_device.h>
  35. #include <linux/of_platform.h>
  36. #endif
  37. #include <asm/io.h>
  38. #include <linux/xilinxfb.h>
  39. #define DRIVER_NAME "xilinxfb"
  40. #define DRIVER_DESCRIPTION "Xilinx TFT LCD frame buffer driver"
  41. /*
  42. * Xilinx calls it "PLB TFT LCD Controller" though it can also be used for
  43. * the VGA port on the Xilinx ML40x board. This is a hardware display controller
  44. * for a 640x480 resolution TFT or VGA screen.
  45. *
  46. * The interface to the framebuffer is nice and simple. There are two
  47. * control registers. The first tells the LCD interface where in memory
  48. * the frame buffer is (only the 11 most significant bits are used, so
  49. * don't start thinking about scrolling). The second allows the LCD to
  50. * be turned on or off as well as rotated 180 degrees.
  51. */
  52. #define NUM_REGS 2
  53. #define REG_FB_ADDR 0
  54. #define REG_CTRL 1
  55. #define REG_CTRL_ENABLE 0x0001
  56. #define REG_CTRL_ROTATE 0x0002
  57. /*
  58. * The hardware only handles a single mode: 640x480 24 bit true
  59. * color. Each pixel gets a word (32 bits) of memory. Within each word,
  60. * the 8 most significant bits are ignored, the next 8 bits are the red
  61. * level, the next 8 bits are the green level and the 8 least
  62. * significant bits are the blue level. Each row of the LCD uses 1024
  63. * words, but only the first 640 pixels are displayed with the other 384
  64. * words being ignored. There are 480 rows.
  65. */
  66. #define BYTES_PER_PIXEL 4
  67. #define BITS_PER_PIXEL (BYTES_PER_PIXEL * 8)
  68. #define XRES 640
  69. #define YRES 480
  70. #define XRES_VIRTUAL 1024
  71. #define YRES_VIRTUAL YRES
  72. #define LINE_LENGTH (XRES_VIRTUAL * BYTES_PER_PIXEL)
  73. #define FB_SIZE (YRES_VIRTUAL * LINE_LENGTH)
  74. #define RED_SHIFT 16
  75. #define GREEN_SHIFT 8
  76. #define BLUE_SHIFT 0
  77. #define PALETTE_ENTRIES_NO 16 /* passed to fb_alloc_cmap() */
  78. /*
  79. * Here are the default fb_fix_screeninfo and fb_var_screeninfo structures
  80. */
  81. static struct fb_fix_screeninfo xilinx_fb_fix = {
  82. .id = "Xilinx",
  83. .type = FB_TYPE_PACKED_PIXELS,
  84. .visual = FB_VISUAL_TRUECOLOR,
  85. .smem_len = FB_SIZE,
  86. .line_length = LINE_LENGTH,
  87. .accel = FB_ACCEL_NONE
  88. };
  89. static struct fb_var_screeninfo xilinx_fb_var = {
  90. .xres = XRES,
  91. .yres = YRES,
  92. .xres_virtual = XRES_VIRTUAL,
  93. .yres_virtual = YRES_VIRTUAL,
  94. .bits_per_pixel = BITS_PER_PIXEL,
  95. .red = { RED_SHIFT, 8, 0 },
  96. .green = { GREEN_SHIFT, 8, 0 },
  97. .blue = { BLUE_SHIFT, 8, 0 },
  98. .transp = { 0, 0, 0 },
  99. .activate = FB_ACTIVATE_NOW
  100. };
  101. struct xilinxfb_drvdata {
  102. struct fb_info info; /* FB driver info record */
  103. u32 regs_phys; /* phys. address of the control registers */
  104. u32 __iomem *regs; /* virt. address of the control registers */
  105. void *fb_virt; /* virt. address of the frame buffer */
  106. dma_addr_t fb_phys; /* phys. address of the frame buffer */
  107. u32 reg_ctrl_default;
  108. u32 pseudo_palette[PALETTE_ENTRIES_NO];
  109. /* Fake palette of 16 colors */
  110. };
  111. #define to_xilinxfb_drvdata(_info) \
  112. container_of(_info, struct xilinxfb_drvdata, info)
  113. /*
  114. * The LCD controller has DCR interface to its registers, but all
  115. * the boards and configurations the driver has been tested with
  116. * use opb2dcr bridge. So the registers are seen as memory mapped.
  117. * This macro is to make it simple to add the direct DCR access
  118. * when it's needed.
  119. */
  120. #define xilinx_fb_out_be32(driverdata, offset, val) \
  121. out_be32(driverdata->regs + offset, val)
  122. static int
  123. xilinx_fb_setcolreg(unsigned regno, unsigned red, unsigned green, unsigned blue,
  124. unsigned transp, struct fb_info *fbi)
  125. {
  126. u32 *palette = fbi->pseudo_palette;
  127. if (regno >= PALETTE_ENTRIES_NO)
  128. return -EINVAL;
  129. if (fbi->var.grayscale) {
  130. /* Convert color to grayscale.
  131. * grayscale = 0.30*R + 0.59*G + 0.11*B */
  132. red = green = blue =
  133. (red * 77 + green * 151 + blue * 28 + 127) >> 8;
  134. }
  135. /* fbi->fix.visual is always FB_VISUAL_TRUECOLOR */
  136. /* We only handle 8 bits of each color. */
  137. red >>= 8;
  138. green >>= 8;
  139. blue >>= 8;
  140. palette[regno] = (red << RED_SHIFT) | (green << GREEN_SHIFT) |
  141. (blue << BLUE_SHIFT);
  142. return 0;
  143. }
  144. static int
  145. xilinx_fb_blank(int blank_mode, struct fb_info *fbi)
  146. {
  147. struct xilinxfb_drvdata *drvdata = to_xilinxfb_drvdata(fbi);
  148. switch (blank_mode) {
  149. case FB_BLANK_UNBLANK:
  150. /* turn on panel */
  151. xilinx_fb_out_be32(drvdata, REG_CTRL, drvdata->reg_ctrl_default);
  152. break;
  153. case FB_BLANK_NORMAL:
  154. case FB_BLANK_VSYNC_SUSPEND:
  155. case FB_BLANK_HSYNC_SUSPEND:
  156. case FB_BLANK_POWERDOWN:
  157. /* turn off panel */
  158. xilinx_fb_out_be32(drvdata, REG_CTRL, 0);
  159. default:
  160. break;
  161. }
  162. return 0; /* success */
  163. }
  164. static struct fb_ops xilinxfb_ops =
  165. {
  166. .owner = THIS_MODULE,
  167. .fb_setcolreg = xilinx_fb_setcolreg,
  168. .fb_blank = xilinx_fb_blank,
  169. .fb_fillrect = cfb_fillrect,
  170. .fb_copyarea = cfb_copyarea,
  171. .fb_imageblit = cfb_imageblit,
  172. };
  173. /* ---------------------------------------------------------------------
  174. * Bus independent setup/teardown
  175. */
  176. static int xilinxfb_assign(struct device *dev, unsigned long physaddr,
  177. int width_mm, int height_mm, int rotate)
  178. {
  179. struct xilinxfb_drvdata *drvdata;
  180. int rc;
  181. /* Allocate the driver data region */
  182. drvdata = kzalloc(sizeof(*drvdata), GFP_KERNEL);
  183. if (!drvdata) {
  184. dev_err(dev, "Couldn't allocate device private record\n");
  185. return -ENOMEM;
  186. }
  187. dev_set_drvdata(dev, drvdata);
  188. /* Map the control registers in */
  189. if (!request_mem_region(physaddr, 8, DRIVER_NAME)) {
  190. dev_err(dev, "Couldn't lock memory region at 0x%08lX\n",
  191. physaddr);
  192. rc = -ENODEV;
  193. goto err_region;
  194. }
  195. drvdata->regs_phys = physaddr;
  196. drvdata->regs = ioremap(physaddr, 8);
  197. if (!drvdata->regs) {
  198. dev_err(dev, "Couldn't lock memory region at 0x%08lX\n",
  199. physaddr);
  200. rc = -ENODEV;
  201. goto err_map;
  202. }
  203. /* Allocate the framebuffer memory */
  204. drvdata->fb_virt = dma_alloc_coherent(dev, PAGE_ALIGN(FB_SIZE),
  205. &drvdata->fb_phys, GFP_KERNEL);
  206. if (!drvdata->fb_virt) {
  207. dev_err(dev, "Could not allocate frame buffer memory\n");
  208. rc = -ENOMEM;
  209. goto err_fbmem;
  210. }
  211. /* Clear (turn to black) the framebuffer */
  212. memset_io((void __iomem *)drvdata->fb_virt, 0, FB_SIZE);
  213. /* Tell the hardware where the frame buffer is */
  214. xilinx_fb_out_be32(drvdata, REG_FB_ADDR, drvdata->fb_phys);
  215. /* Turn on the display */
  216. drvdata->reg_ctrl_default = REG_CTRL_ENABLE;
  217. if (rotate)
  218. drvdata->reg_ctrl_default |= REG_CTRL_ROTATE;
  219. xilinx_fb_out_be32(drvdata, REG_CTRL, drvdata->reg_ctrl_default);
  220. /* Fill struct fb_info */
  221. drvdata->info.device = dev;
  222. drvdata->info.screen_base = (void __iomem *)drvdata->fb_virt;
  223. drvdata->info.fbops = &xilinxfb_ops;
  224. drvdata->info.fix = xilinx_fb_fix;
  225. drvdata->info.fix.smem_start = drvdata->fb_phys;
  226. drvdata->info.pseudo_palette = drvdata->pseudo_palette;
  227. drvdata->info.flags = FBINFO_DEFAULT;
  228. drvdata->info.var = xilinx_fb_var;
  229. xilinx_fb_var.height = height_mm;
  230. xilinx_fb_var.width = width_mm;
  231. /* Allocate a colour map */
  232. rc = fb_alloc_cmap(&drvdata->info.cmap, PALETTE_ENTRIES_NO, 0);
  233. if (rc) {
  234. dev_err(dev, "Fail to allocate colormap (%d entries)\n",
  235. PALETTE_ENTRIES_NO);
  236. goto err_cmap;
  237. }
  238. /* Register new frame buffer */
  239. rc = register_framebuffer(&drvdata->info);
  240. if (rc) {
  241. dev_err(dev, "Could not register frame buffer\n");
  242. goto err_regfb;
  243. }
  244. /* Put a banner in the log (for DEBUG) */
  245. dev_dbg(dev, "regs: phys=%lx, virt=%p\n", physaddr, drvdata->regs);
  246. dev_dbg(dev, "fb: phys=%p, virt=%p, size=%x\n",
  247. (void*)drvdata->fb_phys, drvdata->fb_virt, FB_SIZE);
  248. return 0; /* success */
  249. err_regfb:
  250. fb_dealloc_cmap(&drvdata->info.cmap);
  251. err_cmap:
  252. dma_free_coherent(dev, PAGE_ALIGN(FB_SIZE), drvdata->fb_virt,
  253. drvdata->fb_phys);
  254. /* Turn off the display */
  255. xilinx_fb_out_be32(drvdata, REG_CTRL, 0);
  256. err_fbmem:
  257. iounmap(drvdata->regs);
  258. err_map:
  259. release_mem_region(physaddr, 8);
  260. err_region:
  261. kfree(drvdata);
  262. dev_set_drvdata(dev, NULL);
  263. return rc;
  264. }
  265. static int xilinxfb_release(struct device *dev)
  266. {
  267. struct xilinxfb_drvdata *drvdata = dev_get_drvdata(dev);
  268. #if !defined(CONFIG_FRAMEBUFFER_CONSOLE) && defined(CONFIG_LOGO)
  269. xilinx_fb_blank(VESA_POWERDOWN, &drvdata->info);
  270. #endif
  271. unregister_framebuffer(&drvdata->info);
  272. fb_dealloc_cmap(&drvdata->info.cmap);
  273. dma_free_coherent(dev, PAGE_ALIGN(FB_SIZE), drvdata->fb_virt,
  274. drvdata->fb_phys);
  275. /* Turn off the display */
  276. xilinx_fb_out_be32(drvdata, REG_CTRL, 0);
  277. iounmap(drvdata->regs);
  278. release_mem_region(drvdata->regs_phys, 8);
  279. kfree(drvdata);
  280. dev_set_drvdata(dev, NULL);
  281. return 0;
  282. }
  283. /* ---------------------------------------------------------------------
  284. * Platform bus binding
  285. */
  286. static int
  287. xilinxfb_platform_probe(struct platform_device *pdev)
  288. {
  289. struct xilinxfb_platform_data *pdata;
  290. struct resource *res;
  291. int width_mm = 0;
  292. int height_mm = 0;
  293. int rotate = 0;
  294. /* Find the registers address */
  295. res = platform_get_resource(pdev, IORESOURCE_IO, 0);
  296. if (!res) {
  297. dev_err(&pdev->dev, "Couldn't get registers resource\n");
  298. return -ENODEV;
  299. }
  300. /* If a pdata structure is provided, then extract the parameters */
  301. pdata = pdev->dev.platform_data;
  302. if (pdata) {
  303. height_mm = pdata->screen_height_mm;
  304. width_mm = pdata->screen_width_mm;
  305. rotate = pdata->rotate_screen ? 1 : 0;
  306. }
  307. return xilinxfb_assign(&pdev->dev, res->start, width_mm, height_mm,
  308. rotate);
  309. }
  310. static int
  311. xilinxfb_platform_remove(struct platform_device *pdev)
  312. {
  313. return xilinxfb_release(&pdev->dev);
  314. }
  315. static struct platform_driver xilinxfb_platform_driver = {
  316. .probe = xilinxfb_platform_probe,
  317. .remove = xilinxfb_platform_remove,
  318. .driver = {
  319. .owner = THIS_MODULE,
  320. .name = DRIVER_NAME,
  321. },
  322. };
  323. /* ---------------------------------------------------------------------
  324. * OF bus binding
  325. */
  326. #if defined(CONFIG_OF)
  327. static int __devinit
  328. xilinxfb_of_probe(struct of_device *op, const struct of_device_id *match)
  329. {
  330. struct resource res;
  331. const u32 *prop;
  332. int width = 0, height = 0, rotate = 0;
  333. int size, rc;
  334. dev_dbg(&op->dev, "xilinxfb_of_probe(%p, %p)\n", op, match);
  335. rc = of_address_to_resource(op->node, 0, &res);
  336. if (rc) {
  337. dev_err(&op->dev, "invalid address\n");
  338. return rc;
  339. }
  340. prop = of_get_property(op->node, "display-number", &size);
  341. if ((prop) && (size >= sizeof(u32)*2)) {
  342. width = prop[0];
  343. height = prop[1];
  344. }
  345. if (of_find_property(op->node, "rotate-display", NULL))
  346. rotate = 1;
  347. return xilinxfb_assign(&op->dev, res.start, width, height, rotate);
  348. }
  349. static int __devexit xilinxfb_of_remove(struct of_device *op)
  350. {
  351. return xilinxfb_release(&op->dev);
  352. }
  353. /* Match table for of_platform binding */
  354. static struct of_device_id __devinit xilinxfb_of_match[] = {
  355. { .compatible = "xilinx,ml300-fb", },
  356. {},
  357. };
  358. MODULE_DEVICE_TABLE(of, xilinxfb_of_match);
  359. static struct of_platform_driver xilinxfb_of_driver = {
  360. .owner = THIS_MODULE,
  361. .name = DRIVER_NAME,
  362. .match_table = xilinxfb_of_match,
  363. .probe = xilinxfb_of_probe,
  364. .remove = __devexit_p(xilinxfb_of_remove),
  365. .driver = {
  366. .name = DRIVER_NAME,
  367. },
  368. };
  369. /* Registration helpers to keep the number of #ifdefs to a minimum */
  370. static inline int __init xilinxfb_of_register(void)
  371. {
  372. pr_debug("xilinxfb: calling of_register_platform_driver()\n");
  373. return of_register_platform_driver(&xilinxfb_of_driver);
  374. }
  375. static inline void __exit xilinxfb_of_unregister(void)
  376. {
  377. of_unregister_platform_driver(&xilinxfb_of_driver);
  378. }
  379. #else /* CONFIG_OF */
  380. /* CONFIG_OF not enabled; do nothing helpers */
  381. static inline int __init xilinxfb_of_register(void) { return 0; }
  382. static inline void __exit xilinxfb_of_unregister(void) { }
  383. #endif /* CONFIG_OF */
  384. /* ---------------------------------------------------------------------
  385. * Module setup and teardown
  386. */
  387. static int __init
  388. xilinxfb_init(void)
  389. {
  390. int rc;
  391. rc = xilinxfb_of_register();
  392. if (rc)
  393. return rc;
  394. rc = platform_driver_register(&xilinxfb_platform_driver);
  395. if (rc)
  396. xilinxfb_of_unregister();
  397. return rc;
  398. }
  399. static void __exit
  400. xilinxfb_cleanup(void)
  401. {
  402. platform_driver_unregister(&xilinxfb_platform_driver);
  403. xilinxfb_of_unregister();
  404. }
  405. module_init(xilinxfb_init);
  406. module_exit(xilinxfb_cleanup);
  407. MODULE_AUTHOR("MontaVista Software, Inc. <source@mvista.com>");
  408. MODULE_DESCRIPTION(DRIVER_DESCRIPTION);
  409. MODULE_LICENSE("GPL");