amba-clcd.c 12 KB

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