amba-clcd.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. /*
  2. * linux/drivers/video/amba-clcd.c
  3. *
  4. * Copyright (C) 2001 ARM Limited, by David A Rusling
  5. * Updated to 2.5, Deep Blue Solutions Ltd.
  6. *
  7. * This file is subject to the terms and conditions of the GNU General Public
  8. * License. See the file COPYING in the main directory of this archive
  9. * for more details.
  10. *
  11. * ARM PrimeCell PL110 Color LCD Controller
  12. */
  13. #include <linux/module.h>
  14. #include <linux/kernel.h>
  15. #include <linux/errno.h>
  16. #include <linux/string.h>
  17. #include <linux/slab.h>
  18. #include <linux/delay.h>
  19. #include <linux/mm.h>
  20. #include <linux/fb.h>
  21. #include <linux/init.h>
  22. #include <linux/ioport.h>
  23. #include <linux/list.h>
  24. #include <linux/amba/bus.h>
  25. #include <linux/amba/clcd.h>
  26. #include <linux/clk.h>
  27. #include <linux/hardirq.h>
  28. #include <asm/sizes.h>
  29. #define to_clcd(info) container_of(info, struct clcd_fb, fb)
  30. /* This is limited to 16 characters when displayed by X startup */
  31. static const char *clcd_name = "CLCD FB";
  32. /*
  33. * Unfortunately, the enable/disable functions may be called either from
  34. * process or IRQ context, and we _need_ to delay. This is _not_ good.
  35. */
  36. static inline void clcdfb_sleep(unsigned int ms)
  37. {
  38. if (in_atomic()) {
  39. mdelay(ms);
  40. } else {
  41. msleep(ms);
  42. }
  43. }
  44. static inline void clcdfb_set_start(struct clcd_fb *fb)
  45. {
  46. unsigned long ustart = fb->fb.fix.smem_start;
  47. unsigned long lstart;
  48. ustart += fb->fb.var.yoffset * fb->fb.fix.line_length;
  49. lstart = ustart + fb->fb.var.yres * fb->fb.fix.line_length / 2;
  50. writel(ustart, fb->regs + CLCD_UBAS);
  51. writel(lstart, fb->regs + CLCD_LBAS);
  52. }
  53. static void clcdfb_disable(struct clcd_fb *fb)
  54. {
  55. u32 val;
  56. if (fb->board->disable)
  57. fb->board->disable(fb);
  58. val = readl(fb->regs + CLCD_CNTL);
  59. if (val & CNTL_LCDPWR) {
  60. val &= ~CNTL_LCDPWR;
  61. writel(val, fb->regs + CLCD_CNTL);
  62. clcdfb_sleep(20);
  63. }
  64. if (val & CNTL_LCDEN) {
  65. val &= ~CNTL_LCDEN;
  66. writel(val, fb->regs + CLCD_CNTL);
  67. }
  68. /*
  69. * Disable CLCD clock source.
  70. */
  71. clk_disable(fb->clk);
  72. }
  73. static void clcdfb_enable(struct clcd_fb *fb, u32 cntl)
  74. {
  75. /*
  76. * Enable the CLCD clock source.
  77. */
  78. clk_enable(fb->clk);
  79. /*
  80. * Bring up by first enabling..
  81. */
  82. cntl |= CNTL_LCDEN;
  83. writel(cntl, fb->regs + CLCD_CNTL);
  84. clcdfb_sleep(20);
  85. /*
  86. * and now apply power.
  87. */
  88. cntl |= CNTL_LCDPWR;
  89. writel(cntl, fb->regs + CLCD_CNTL);
  90. /*
  91. * finally, enable the interface.
  92. */
  93. if (fb->board->enable)
  94. fb->board->enable(fb);
  95. }
  96. static int
  97. clcdfb_set_bitfields(struct clcd_fb *fb, struct fb_var_screeninfo *var)
  98. {
  99. int ret = 0;
  100. memset(&var->transp, 0, sizeof(var->transp));
  101. var->red.msb_right = 0;
  102. var->green.msb_right = 0;
  103. var->blue.msb_right = 0;
  104. switch (var->bits_per_pixel) {
  105. case 1:
  106. case 2:
  107. case 4:
  108. case 8:
  109. var->red.length = var->bits_per_pixel;
  110. var->red.offset = 0;
  111. var->green.length = var->bits_per_pixel;
  112. var->green.offset = 0;
  113. var->blue.length = var->bits_per_pixel;
  114. var->blue.offset = 0;
  115. break;
  116. case 16:
  117. var->red.length = 5;
  118. var->blue.length = 5;
  119. /*
  120. * Green length can be 5 or 6 depending whether
  121. * we're operating in RGB555 or RGB565 mode.
  122. */
  123. if (var->green.length != 5 && var->green.length != 6)
  124. var->green.length = 6;
  125. break;
  126. case 32:
  127. if (fb->panel->cntl & CNTL_LCDTFT) {
  128. var->red.length = 8;
  129. var->green.length = 8;
  130. var->blue.length = 8;
  131. break;
  132. }
  133. default:
  134. ret = -EINVAL;
  135. break;
  136. }
  137. /*
  138. * >= 16bpp displays have separate colour component bitfields
  139. * encoded in the pixel data. Calculate their position from
  140. * the bitfield length defined above.
  141. */
  142. if (ret == 0 && var->bits_per_pixel >= 16) {
  143. if (fb->panel->cntl & CNTL_BGR) {
  144. var->blue.offset = 0;
  145. var->green.offset = var->blue.offset + var->blue.length;
  146. var->red.offset = var->green.offset + var->green.length;
  147. } else {
  148. var->red.offset = 0;
  149. var->green.offset = var->red.offset + var->red.length;
  150. var->blue.offset = var->green.offset + var->green.length;
  151. }
  152. }
  153. return ret;
  154. }
  155. static int clcdfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
  156. {
  157. struct clcd_fb *fb = to_clcd(info);
  158. int ret = -EINVAL;
  159. if (fb->board->check)
  160. ret = fb->board->check(fb, var);
  161. if (ret == 0 &&
  162. var->xres_virtual * var->bits_per_pixel / 8 *
  163. var->yres_virtual > fb->fb.fix.smem_len)
  164. ret = -EINVAL;
  165. if (ret == 0)
  166. ret = clcdfb_set_bitfields(fb, var);
  167. return ret;
  168. }
  169. static int clcdfb_set_par(struct fb_info *info)
  170. {
  171. struct clcd_fb *fb = to_clcd(info);
  172. struct clcd_regs regs;
  173. fb->fb.fix.line_length = fb->fb.var.xres_virtual *
  174. fb->fb.var.bits_per_pixel / 8;
  175. if (fb->fb.var.bits_per_pixel <= 8)
  176. fb->fb.fix.visual = FB_VISUAL_PSEUDOCOLOR;
  177. else
  178. fb->fb.fix.visual = FB_VISUAL_TRUECOLOR;
  179. fb->board->decode(fb, &regs);
  180. clcdfb_disable(fb);
  181. writel(regs.tim0, fb->regs + CLCD_TIM0);
  182. writel(regs.tim1, fb->regs + CLCD_TIM1);
  183. writel(regs.tim2, fb->regs + CLCD_TIM2);
  184. writel(regs.tim3, fb->regs + CLCD_TIM3);
  185. clcdfb_set_start(fb);
  186. clk_set_rate(fb->clk, (1000000000 / regs.pixclock) * 1000);
  187. fb->clcd_cntl = regs.cntl;
  188. clcdfb_enable(fb, regs.cntl);
  189. #ifdef DEBUG
  190. printk(KERN_INFO "CLCD: Registers set to\n"
  191. KERN_INFO " %08x %08x %08x %08x\n"
  192. KERN_INFO " %08x %08x %08x %08x\n",
  193. readl(fb->regs + CLCD_TIM0), readl(fb->regs + CLCD_TIM1),
  194. readl(fb->regs + CLCD_TIM2), readl(fb->regs + CLCD_TIM3),
  195. readl(fb->regs + CLCD_UBAS), readl(fb->regs + CLCD_LBAS),
  196. readl(fb->regs + CLCD_IENB), readl(fb->regs + CLCD_CNTL));
  197. #endif
  198. return 0;
  199. }
  200. static inline u32 convert_bitfield(int val, struct fb_bitfield *bf)
  201. {
  202. unsigned int mask = (1 << bf->length) - 1;
  203. return (val >> (16 - bf->length) & mask) << bf->offset;
  204. }
  205. /*
  206. * Set a single color register. The values supplied have a 16 bit
  207. * magnitude. Return != 0 for invalid regno.
  208. */
  209. static int
  210. clcdfb_setcolreg(unsigned int regno, unsigned int red, unsigned int green,
  211. unsigned int blue, unsigned int transp, struct fb_info *info)
  212. {
  213. struct clcd_fb *fb = to_clcd(info);
  214. if (regno < 16)
  215. fb->cmap[regno] = convert_bitfield(transp, &fb->fb.var.transp) |
  216. convert_bitfield(blue, &fb->fb.var.blue) |
  217. convert_bitfield(green, &fb->fb.var.green) |
  218. convert_bitfield(red, &fb->fb.var.red);
  219. if (fb->fb.fix.visual == FB_VISUAL_PSEUDOCOLOR && regno < 256) {
  220. int hw_reg = CLCD_PALETTE + ((regno * 2) & ~3);
  221. u32 val, mask, newval;
  222. newval = (red >> 11) & 0x001f;
  223. newval |= (green >> 6) & 0x03e0;
  224. newval |= (blue >> 1) & 0x7c00;
  225. /*
  226. * 3.2.11: if we're configured for big endian
  227. * byte order, the palette entries are swapped.
  228. */
  229. if (fb->clcd_cntl & CNTL_BEBO)
  230. regno ^= 1;
  231. if (regno & 1) {
  232. newval <<= 16;
  233. mask = 0x0000ffff;
  234. } else {
  235. mask = 0xffff0000;
  236. }
  237. val = readl(fb->regs + hw_reg) & mask;
  238. writel(val | newval, fb->regs + hw_reg);
  239. }
  240. return regno > 255;
  241. }
  242. /*
  243. * Blank the screen if blank_mode != 0, else unblank. If blank == NULL
  244. * then the caller blanks by setting the CLUT (Color Look Up Table) to all
  245. * black. Return 0 if blanking succeeded, != 0 if un-/blanking failed due
  246. * to e.g. a video mode which doesn't support it. Implements VESA suspend
  247. * and powerdown modes on hardware that supports disabling hsync/vsync:
  248. * blank_mode == 2: suspend vsync
  249. * blank_mode == 3: suspend hsync
  250. * blank_mode == 4: powerdown
  251. */
  252. static int clcdfb_blank(int blank_mode, struct fb_info *info)
  253. {
  254. struct clcd_fb *fb = to_clcd(info);
  255. if (blank_mode != 0) {
  256. clcdfb_disable(fb);
  257. } else {
  258. clcdfb_enable(fb, fb->clcd_cntl);
  259. }
  260. return 0;
  261. }
  262. static int clcdfb_mmap(struct fb_info *info,
  263. struct vm_area_struct *vma)
  264. {
  265. struct clcd_fb *fb = to_clcd(info);
  266. unsigned long len, off = vma->vm_pgoff << PAGE_SHIFT;
  267. int ret = -EINVAL;
  268. len = info->fix.smem_len;
  269. if (off <= len && vma->vm_end - vma->vm_start <= len - off &&
  270. fb->board->mmap)
  271. ret = fb->board->mmap(fb, vma);
  272. return ret;
  273. }
  274. static struct fb_ops clcdfb_ops = {
  275. .owner = THIS_MODULE,
  276. .fb_check_var = clcdfb_check_var,
  277. .fb_set_par = clcdfb_set_par,
  278. .fb_setcolreg = clcdfb_setcolreg,
  279. .fb_blank = clcdfb_blank,
  280. .fb_fillrect = cfb_fillrect,
  281. .fb_copyarea = cfb_copyarea,
  282. .fb_imageblit = cfb_imageblit,
  283. .fb_mmap = clcdfb_mmap,
  284. };
  285. static int clcdfb_register(struct clcd_fb *fb)
  286. {
  287. int ret;
  288. fb->clk = clk_get(&fb->dev->dev, NULL);
  289. if (IS_ERR(fb->clk)) {
  290. ret = PTR_ERR(fb->clk);
  291. goto out;
  292. }
  293. fb->fb.fix.mmio_start = fb->dev->res.start;
  294. fb->fb.fix.mmio_len = resource_size(&fb->dev->res);
  295. fb->regs = ioremap(fb->fb.fix.mmio_start, fb->fb.fix.mmio_len);
  296. if (!fb->regs) {
  297. printk(KERN_ERR "CLCD: unable to remap registers\n");
  298. ret = -ENOMEM;
  299. goto free_clk;
  300. }
  301. fb->fb.fbops = &clcdfb_ops;
  302. fb->fb.flags = FBINFO_FLAG_DEFAULT;
  303. fb->fb.pseudo_palette = fb->cmap;
  304. strncpy(fb->fb.fix.id, clcd_name, sizeof(fb->fb.fix.id));
  305. fb->fb.fix.type = FB_TYPE_PACKED_PIXELS;
  306. fb->fb.fix.type_aux = 0;
  307. fb->fb.fix.xpanstep = 0;
  308. fb->fb.fix.ypanstep = 0;
  309. fb->fb.fix.ywrapstep = 0;
  310. fb->fb.fix.accel = FB_ACCEL_NONE;
  311. fb->fb.var.xres = fb->panel->mode.xres;
  312. fb->fb.var.yres = fb->panel->mode.yres;
  313. fb->fb.var.xres_virtual = fb->panel->mode.xres;
  314. fb->fb.var.yres_virtual = fb->panel->mode.yres;
  315. fb->fb.var.bits_per_pixel = fb->panel->bpp;
  316. fb->fb.var.grayscale = fb->panel->grayscale;
  317. fb->fb.var.pixclock = fb->panel->mode.pixclock;
  318. fb->fb.var.left_margin = fb->panel->mode.left_margin;
  319. fb->fb.var.right_margin = fb->panel->mode.right_margin;
  320. fb->fb.var.upper_margin = fb->panel->mode.upper_margin;
  321. fb->fb.var.lower_margin = fb->panel->mode.lower_margin;
  322. fb->fb.var.hsync_len = fb->panel->mode.hsync_len;
  323. fb->fb.var.vsync_len = fb->panel->mode.vsync_len;
  324. fb->fb.var.sync = fb->panel->mode.sync;
  325. fb->fb.var.vmode = fb->panel->mode.vmode;
  326. fb->fb.var.activate = FB_ACTIVATE_NOW;
  327. fb->fb.var.nonstd = 0;
  328. fb->fb.var.height = fb->panel->height;
  329. fb->fb.var.width = fb->panel->width;
  330. fb->fb.var.accel_flags = 0;
  331. fb->fb.monspecs.hfmin = 0;
  332. fb->fb.monspecs.hfmax = 100000;
  333. fb->fb.monspecs.vfmin = 0;
  334. fb->fb.monspecs.vfmax = 400;
  335. fb->fb.monspecs.dclkmin = 1000000;
  336. fb->fb.monspecs.dclkmax = 100000000;
  337. /*
  338. * Make sure that the bitfields are set appropriately.
  339. */
  340. clcdfb_set_bitfields(fb, &fb->fb.var);
  341. /*
  342. * Allocate colourmap.
  343. */
  344. ret = fb_alloc_cmap(&fb->fb.cmap, 256, 0);
  345. if (ret)
  346. goto unmap;
  347. /*
  348. * Ensure interrupts are disabled.
  349. */
  350. writel(0, fb->regs + CLCD_IENB);
  351. fb_set_var(&fb->fb, &fb->fb.var);
  352. printk(KERN_INFO "CLCD: %s hardware, %s display\n",
  353. fb->board->name, fb->panel->mode.name);
  354. ret = register_framebuffer(&fb->fb);
  355. if (ret == 0)
  356. goto out;
  357. printk(KERN_ERR "CLCD: cannot register framebuffer (%d)\n", ret);
  358. fb_dealloc_cmap(&fb->fb.cmap);
  359. unmap:
  360. iounmap(fb->regs);
  361. free_clk:
  362. clk_put(fb->clk);
  363. out:
  364. return ret;
  365. }
  366. static int clcdfb_probe(struct amba_device *dev, struct amba_id *id)
  367. {
  368. struct clcd_board *board = dev->dev.platform_data;
  369. struct clcd_fb *fb;
  370. int ret;
  371. if (!board)
  372. return -EINVAL;
  373. ret = amba_request_regions(dev, NULL);
  374. if (ret) {
  375. printk(KERN_ERR "CLCD: unable to reserve regs region\n");
  376. goto out;
  377. }
  378. fb = kzalloc(sizeof(struct clcd_fb), GFP_KERNEL);
  379. if (!fb) {
  380. printk(KERN_INFO "CLCD: could not allocate new clcd_fb struct\n");
  381. ret = -ENOMEM;
  382. goto free_region;
  383. }
  384. fb->dev = dev;
  385. fb->board = board;
  386. ret = fb->board->setup(fb);
  387. if (ret)
  388. goto free_fb;
  389. ret = clcdfb_register(fb);
  390. if (ret == 0) {
  391. amba_set_drvdata(dev, fb);
  392. goto out;
  393. }
  394. fb->board->remove(fb);
  395. free_fb:
  396. kfree(fb);
  397. free_region:
  398. amba_release_regions(dev);
  399. out:
  400. return ret;
  401. }
  402. static int clcdfb_remove(struct amba_device *dev)
  403. {
  404. struct clcd_fb *fb = amba_get_drvdata(dev);
  405. amba_set_drvdata(dev, NULL);
  406. clcdfb_disable(fb);
  407. unregister_framebuffer(&fb->fb);
  408. if (fb->fb.cmap.len)
  409. fb_dealloc_cmap(&fb->fb.cmap);
  410. iounmap(fb->regs);
  411. clk_put(fb->clk);
  412. fb->board->remove(fb);
  413. kfree(fb);
  414. amba_release_regions(dev);
  415. return 0;
  416. }
  417. static struct amba_id clcdfb_id_table[] = {
  418. {
  419. .id = 0x00041110,
  420. .mask = 0x000ffffe,
  421. },
  422. { 0, 0 },
  423. };
  424. static struct amba_driver clcd_driver = {
  425. .drv = {
  426. .name = "clcd-pl11x",
  427. },
  428. .probe = clcdfb_probe,
  429. .remove = clcdfb_remove,
  430. .id_table = clcdfb_id_table,
  431. };
  432. static int __init amba_clcdfb_init(void)
  433. {
  434. if (fb_get_options("ambafb", NULL))
  435. return -ENODEV;
  436. return amba_driver_register(&clcd_driver);
  437. }
  438. module_init(amba_clcdfb_init);
  439. static void __exit amba_clcdfb_exit(void)
  440. {
  441. amba_driver_unregister(&clcd_driver);
  442. }
  443. module_exit(amba_clcdfb_exit);
  444. MODULE_DESCRIPTION("ARM PrimeCell PL110 CLCD core driver");
  445. MODULE_LICENSE("GPL");