amba-clcd.c 13 KB

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