xilinxfb.c 14 KB

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