xilinxfb.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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. * Default xilinxfb configuration
  80. */
  81. static struct xilinxfb_platform_data xilinx_fb_default_pdata = {
  82. };
  83. /*
  84. * Here are the default fb_fix_screeninfo and fb_var_screeninfo structures
  85. */
  86. static struct fb_fix_screeninfo xilinx_fb_fix = {
  87. .id = "Xilinx",
  88. .type = FB_TYPE_PACKED_PIXELS,
  89. .visual = FB_VISUAL_TRUECOLOR,
  90. .smem_len = FB_SIZE,
  91. .line_length = LINE_LENGTH,
  92. .accel = FB_ACCEL_NONE
  93. };
  94. static struct fb_var_screeninfo xilinx_fb_var = {
  95. .xres = XRES,
  96. .yres = YRES,
  97. .xres_virtual = XRES_VIRTUAL,
  98. .yres_virtual = YRES_VIRTUAL,
  99. .bits_per_pixel = BITS_PER_PIXEL,
  100. .red = { RED_SHIFT, 8, 0 },
  101. .green = { GREEN_SHIFT, 8, 0 },
  102. .blue = { BLUE_SHIFT, 8, 0 },
  103. .transp = { 0, 0, 0 },
  104. .activate = FB_ACTIVATE_NOW
  105. };
  106. struct xilinxfb_drvdata {
  107. struct fb_info info; /* FB driver info record */
  108. u32 regs_phys; /* phys. address of the control registers */
  109. u32 __iomem *regs; /* virt. address of the control registers */
  110. void *fb_virt; /* virt. address of the frame buffer */
  111. dma_addr_t fb_phys; /* phys. address of the frame buffer */
  112. u32 reg_ctrl_default;
  113. u32 pseudo_palette[PALETTE_ENTRIES_NO];
  114. /* Fake palette of 16 colors */
  115. };
  116. #define to_xilinxfb_drvdata(_info) \
  117. container_of(_info, struct xilinxfb_drvdata, info)
  118. /*
  119. * The LCD controller has DCR interface to its registers, but all
  120. * the boards and configurations the driver has been tested with
  121. * use opb2dcr bridge. So the registers are seen as memory mapped.
  122. * This macro is to make it simple to add the direct DCR access
  123. * when it's needed.
  124. */
  125. #define xilinx_fb_out_be32(driverdata, offset, val) \
  126. out_be32(driverdata->regs + offset, val)
  127. static int
  128. xilinx_fb_setcolreg(unsigned regno, unsigned red, unsigned green, unsigned blue,
  129. unsigned transp, struct fb_info *fbi)
  130. {
  131. u32 *palette = fbi->pseudo_palette;
  132. if (regno >= PALETTE_ENTRIES_NO)
  133. return -EINVAL;
  134. if (fbi->var.grayscale) {
  135. /* Convert color to grayscale.
  136. * grayscale = 0.30*R + 0.59*G + 0.11*B */
  137. red = green = blue =
  138. (red * 77 + green * 151 + blue * 28 + 127) >> 8;
  139. }
  140. /* fbi->fix.visual is always FB_VISUAL_TRUECOLOR */
  141. /* We only handle 8 bits of each color. */
  142. red >>= 8;
  143. green >>= 8;
  144. blue >>= 8;
  145. palette[regno] = (red << RED_SHIFT) | (green << GREEN_SHIFT) |
  146. (blue << BLUE_SHIFT);
  147. return 0;
  148. }
  149. static int
  150. xilinx_fb_blank(int blank_mode, struct fb_info *fbi)
  151. {
  152. struct xilinxfb_drvdata *drvdata = to_xilinxfb_drvdata(fbi);
  153. switch (blank_mode) {
  154. case FB_BLANK_UNBLANK:
  155. /* turn on panel */
  156. xilinx_fb_out_be32(drvdata, REG_CTRL, drvdata->reg_ctrl_default);
  157. break;
  158. case FB_BLANK_NORMAL:
  159. case FB_BLANK_VSYNC_SUSPEND:
  160. case FB_BLANK_HSYNC_SUSPEND:
  161. case FB_BLANK_POWERDOWN:
  162. /* turn off panel */
  163. xilinx_fb_out_be32(drvdata, REG_CTRL, 0);
  164. default:
  165. break;
  166. }
  167. return 0; /* success */
  168. }
  169. static struct fb_ops xilinxfb_ops =
  170. {
  171. .owner = THIS_MODULE,
  172. .fb_setcolreg = xilinx_fb_setcolreg,
  173. .fb_blank = xilinx_fb_blank,
  174. .fb_fillrect = cfb_fillrect,
  175. .fb_copyarea = cfb_copyarea,
  176. .fb_imageblit = cfb_imageblit,
  177. };
  178. /* ---------------------------------------------------------------------
  179. * Bus independent setup/teardown
  180. */
  181. static int xilinxfb_assign(struct device *dev, unsigned long physaddr,
  182. struct xilinxfb_platform_data *pdata)
  183. {
  184. struct xilinxfb_drvdata *drvdata;
  185. int rc;
  186. /* Allocate the driver data region */
  187. drvdata = kzalloc(sizeof(*drvdata), GFP_KERNEL);
  188. if (!drvdata) {
  189. dev_err(dev, "Couldn't allocate device private record\n");
  190. return -ENOMEM;
  191. }
  192. dev_set_drvdata(dev, drvdata);
  193. /* Map the control registers in */
  194. if (!request_mem_region(physaddr, 8, DRIVER_NAME)) {
  195. dev_err(dev, "Couldn't lock memory region at 0x%08lX\n",
  196. physaddr);
  197. rc = -ENODEV;
  198. goto err_region;
  199. }
  200. drvdata->regs_phys = physaddr;
  201. drvdata->regs = ioremap(physaddr, 8);
  202. if (!drvdata->regs) {
  203. dev_err(dev, "Couldn't lock memory region at 0x%08lX\n",
  204. physaddr);
  205. rc = -ENODEV;
  206. goto err_map;
  207. }
  208. /* Allocate the framebuffer memory */
  209. drvdata->fb_virt = dma_alloc_coherent(dev, PAGE_ALIGN(FB_SIZE),
  210. &drvdata->fb_phys, GFP_KERNEL);
  211. if (!drvdata->fb_virt) {
  212. dev_err(dev, "Could not allocate frame buffer memory\n");
  213. rc = -ENOMEM;
  214. goto err_fbmem;
  215. }
  216. /* Clear (turn to black) the framebuffer */
  217. memset_io((void __iomem *)drvdata->fb_virt, 0, FB_SIZE);
  218. /* Tell the hardware where the frame buffer is */
  219. xilinx_fb_out_be32(drvdata, REG_FB_ADDR, drvdata->fb_phys);
  220. /* Turn on the display */
  221. drvdata->reg_ctrl_default = REG_CTRL_ENABLE;
  222. if (pdata->rotate_screen)
  223. drvdata->reg_ctrl_default |= REG_CTRL_ROTATE;
  224. xilinx_fb_out_be32(drvdata, REG_CTRL, drvdata->reg_ctrl_default);
  225. /* Fill struct fb_info */
  226. drvdata->info.device = dev;
  227. drvdata->info.screen_base = (void __iomem *)drvdata->fb_virt;
  228. drvdata->info.fbops = &xilinxfb_ops;
  229. drvdata->info.fix = xilinx_fb_fix;
  230. drvdata->info.fix.smem_start = drvdata->fb_phys;
  231. drvdata->info.pseudo_palette = drvdata->pseudo_palette;
  232. drvdata->info.flags = FBINFO_DEFAULT;
  233. drvdata->info.var = xilinx_fb_var;
  234. xilinx_fb_var.height = pdata->screen_height_mm;
  235. xilinx_fb_var.width = pdata->screen_width_mm;
  236. /* Allocate a colour map */
  237. rc = fb_alloc_cmap(&drvdata->info.cmap, PALETTE_ENTRIES_NO, 0);
  238. if (rc) {
  239. dev_err(dev, "Fail to allocate colormap (%d entries)\n",
  240. PALETTE_ENTRIES_NO);
  241. goto err_cmap;
  242. }
  243. /* Register new frame buffer */
  244. rc = register_framebuffer(&drvdata->info);
  245. if (rc) {
  246. dev_err(dev, "Could not register frame buffer\n");
  247. goto err_regfb;
  248. }
  249. /* Put a banner in the log (for DEBUG) */
  250. dev_dbg(dev, "regs: phys=%lx, virt=%p\n", physaddr, drvdata->regs);
  251. dev_dbg(dev, "fb: phys=%p, virt=%p, size=%x\n",
  252. (void*)drvdata->fb_phys, drvdata->fb_virt, FB_SIZE);
  253. return 0; /* success */
  254. err_regfb:
  255. fb_dealloc_cmap(&drvdata->info.cmap);
  256. err_cmap:
  257. dma_free_coherent(dev, PAGE_ALIGN(FB_SIZE), drvdata->fb_virt,
  258. drvdata->fb_phys);
  259. /* Turn off the display */
  260. xilinx_fb_out_be32(drvdata, REG_CTRL, 0);
  261. err_fbmem:
  262. iounmap(drvdata->regs);
  263. err_map:
  264. release_mem_region(physaddr, 8);
  265. err_region:
  266. kfree(drvdata);
  267. dev_set_drvdata(dev, NULL);
  268. return rc;
  269. }
  270. static int xilinxfb_release(struct device *dev)
  271. {
  272. struct xilinxfb_drvdata *drvdata = dev_get_drvdata(dev);
  273. #if !defined(CONFIG_FRAMEBUFFER_CONSOLE) && defined(CONFIG_LOGO)
  274. xilinx_fb_blank(VESA_POWERDOWN, &drvdata->info);
  275. #endif
  276. unregister_framebuffer(&drvdata->info);
  277. fb_dealloc_cmap(&drvdata->info.cmap);
  278. dma_free_coherent(dev, PAGE_ALIGN(FB_SIZE), drvdata->fb_virt,
  279. drvdata->fb_phys);
  280. /* Turn off the display */
  281. xilinx_fb_out_be32(drvdata, REG_CTRL, 0);
  282. iounmap(drvdata->regs);
  283. release_mem_region(drvdata->regs_phys, 8);
  284. kfree(drvdata);
  285. dev_set_drvdata(dev, NULL);
  286. return 0;
  287. }
  288. /* ---------------------------------------------------------------------
  289. * Platform bus binding
  290. */
  291. static int
  292. xilinxfb_platform_probe(struct platform_device *pdev)
  293. {
  294. struct xilinxfb_platform_data *pdata;
  295. struct resource *res;
  296. /* Find the registers address */
  297. res = platform_get_resource(pdev, IORESOURCE_IO, 0);
  298. if (!res) {
  299. dev_err(&pdev->dev, "Couldn't get registers resource\n");
  300. return -ENODEV;
  301. }
  302. /* If a pdata structure is provided, then extract the parameters */
  303. if (pdev->dev.platform_data)
  304. pdata = pdev->dev.platform_data;
  305. else
  306. pdata = &xilinx_fb_default_pdata;
  307. return xilinxfb_assign(&pdev->dev, res->start, pdata);
  308. }
  309. static int
  310. xilinxfb_platform_remove(struct platform_device *pdev)
  311. {
  312. return xilinxfb_release(&pdev->dev);
  313. }
  314. static struct platform_driver xilinxfb_platform_driver = {
  315. .probe = xilinxfb_platform_probe,
  316. .remove = xilinxfb_platform_remove,
  317. .driver = {
  318. .owner = THIS_MODULE,
  319. .name = DRIVER_NAME,
  320. },
  321. };
  322. /* ---------------------------------------------------------------------
  323. * OF bus binding
  324. */
  325. #if defined(CONFIG_OF)
  326. static int __devinit
  327. xilinxfb_of_probe(struct of_device *op, const struct of_device_id *match)
  328. {
  329. struct resource res;
  330. const u32 *prop;
  331. struct xilinxfb_platform_data pdata;
  332. int size, rc;
  333. /* Copy with the default pdata (not a ptr reference!) */
  334. pdata = xilinx_fb_default_pdata;
  335. dev_dbg(&op->dev, "xilinxfb_of_probe(%p, %p)\n", op, match);
  336. rc = of_address_to_resource(op->node, 0, &res);
  337. if (rc) {
  338. dev_err(&op->dev, "invalid address\n");
  339. return rc;
  340. }
  341. prop = of_get_property(op->node, "display-number", &size);
  342. if ((prop) && (size >= sizeof(u32)*2)) {
  343. pdata.screen_width_mm = prop[0];
  344. pdata.screen_height_mm = prop[1];
  345. }
  346. if (of_find_property(op->node, "rotate-display", NULL))
  347. pdata.rotate_screen = 1;
  348. return xilinxfb_assign(&op->dev, res.start, &pdata);
  349. }
  350. static int __devexit xilinxfb_of_remove(struct of_device *op)
  351. {
  352. return xilinxfb_release(&op->dev);
  353. }
  354. /* Match table for of_platform binding */
  355. static struct of_device_id __devinit xilinxfb_of_match[] = {
  356. { .compatible = "xilinx,ml300-fb", },
  357. {},
  358. };
  359. MODULE_DEVICE_TABLE(of, xilinxfb_of_match);
  360. static struct of_platform_driver xilinxfb_of_driver = {
  361. .owner = THIS_MODULE,
  362. .name = DRIVER_NAME,
  363. .match_table = xilinxfb_of_match,
  364. .probe = xilinxfb_of_probe,
  365. .remove = __devexit_p(xilinxfb_of_remove),
  366. .driver = {
  367. .name = DRIVER_NAME,
  368. },
  369. };
  370. /* Registration helpers to keep the number of #ifdefs to a minimum */
  371. static inline int __init xilinxfb_of_register(void)
  372. {
  373. pr_debug("xilinxfb: calling of_register_platform_driver()\n");
  374. return of_register_platform_driver(&xilinxfb_of_driver);
  375. }
  376. static inline void __exit xilinxfb_of_unregister(void)
  377. {
  378. of_unregister_platform_driver(&xilinxfb_of_driver);
  379. }
  380. #else /* CONFIG_OF */
  381. /* CONFIG_OF not enabled; do nothing helpers */
  382. static inline int __init xilinxfb_of_register(void) { return 0; }
  383. static inline void __exit xilinxfb_of_unregister(void) { }
  384. #endif /* CONFIG_OF */
  385. /* ---------------------------------------------------------------------
  386. * Module setup and teardown
  387. */
  388. static int __init
  389. xilinxfb_init(void)
  390. {
  391. int rc;
  392. rc = xilinxfb_of_register();
  393. if (rc)
  394. return rc;
  395. rc = platform_driver_register(&xilinxfb_platform_driver);
  396. if (rc)
  397. xilinxfb_of_unregister();
  398. return rc;
  399. }
  400. static void __exit
  401. xilinxfb_cleanup(void)
  402. {
  403. platform_driver_unregister(&xilinxfb_platform_driver);
  404. xilinxfb_of_unregister();
  405. }
  406. module_init(xilinxfb_init);
  407. module_exit(xilinxfb_cleanup);
  408. MODULE_AUTHOR("MontaVista Software, Inc. <source@mvista.com>");
  409. MODULE_DESCRIPTION(DRIVER_DESCRIPTION);
  410. MODULE_LICENSE("GPL");