amba-clcd.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  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/dma-mapping.h>
  14. #include <linux/module.h>
  15. #include <linux/kernel.h>
  16. #include <linux/errno.h>
  17. #include <linux/string.h>
  18. #include <linux/slab.h>
  19. #include <linux/delay.h>
  20. #include <linux/mm.h>
  21. #include <linux/fb.h>
  22. #include <linux/init.h>
  23. #include <linux/ioport.h>
  24. #include <linux/list.h>
  25. #include <linux/amba/bus.h>
  26. #include <linux/amba/clcd.h>
  27. #include <linux/clk.h>
  28. #include <linux/hardirq.h>
  29. #include <asm/sizes.h>
  30. #define to_clcd(info) container_of(info, struct clcd_fb, fb)
  31. /* This is limited to 16 characters when displayed by X startup */
  32. static const char *clcd_name = "CLCD FB";
  33. /*
  34. * Unfortunately, the enable/disable functions may be called either from
  35. * process or IRQ context, and we _need_ to delay. This is _not_ good.
  36. */
  37. static inline void clcdfb_sleep(unsigned int ms)
  38. {
  39. if (in_atomic()) {
  40. mdelay(ms);
  41. } else {
  42. msleep(ms);
  43. }
  44. }
  45. static inline void clcdfb_set_start(struct clcd_fb *fb)
  46. {
  47. unsigned long ustart = fb->fb.fix.smem_start;
  48. unsigned long lstart;
  49. ustart += fb->fb.var.yoffset * fb->fb.fix.line_length;
  50. lstart = ustart + fb->fb.var.yres * fb->fb.fix.line_length / 2;
  51. writel(ustart, fb->regs + CLCD_UBAS);
  52. writel(lstart, fb->regs + CLCD_LBAS);
  53. }
  54. static void clcdfb_disable(struct clcd_fb *fb)
  55. {
  56. u32 val;
  57. if (fb->board->disable)
  58. fb->board->disable(fb);
  59. val = readl(fb->regs + fb->off_cntl);
  60. if (val & CNTL_LCDPWR) {
  61. val &= ~CNTL_LCDPWR;
  62. writel(val, fb->regs + fb->off_cntl);
  63. clcdfb_sleep(20);
  64. }
  65. if (val & CNTL_LCDEN) {
  66. val &= ~CNTL_LCDEN;
  67. writel(val, fb->regs + fb->off_cntl);
  68. }
  69. /*
  70. * Disable CLCD clock source.
  71. */
  72. if (fb->clk_enabled) {
  73. fb->clk_enabled = false;
  74. clk_disable(fb->clk);
  75. }
  76. }
  77. static void clcdfb_enable(struct clcd_fb *fb, u32 cntl)
  78. {
  79. /*
  80. * Enable the CLCD clock source.
  81. */
  82. if (!fb->clk_enabled) {
  83. fb->clk_enabled = true;
  84. clk_enable(fb->clk);
  85. }
  86. /*
  87. * Bring up by first enabling..
  88. */
  89. cntl |= CNTL_LCDEN;
  90. writel(cntl, fb->regs + fb->off_cntl);
  91. clcdfb_sleep(20);
  92. /*
  93. * and now apply power.
  94. */
  95. cntl |= CNTL_LCDPWR;
  96. writel(cntl, fb->regs + fb->off_cntl);
  97. /*
  98. * finally, enable the interface.
  99. */
  100. if (fb->board->enable)
  101. fb->board->enable(fb);
  102. }
  103. static int
  104. clcdfb_set_bitfields(struct clcd_fb *fb, struct fb_var_screeninfo *var)
  105. {
  106. u32 caps;
  107. int ret = 0;
  108. if (fb->panel->caps && fb->board->caps)
  109. caps = fb->panel->caps & fb->board->caps;
  110. else {
  111. /* Old way of specifying what can be used */
  112. caps = fb->panel->cntl & CNTL_BGR ?
  113. CLCD_CAP_BGR : CLCD_CAP_RGB;
  114. /* But mask out 444 modes as they weren't supported */
  115. caps &= ~CLCD_CAP_444;
  116. }
  117. /* Only TFT panels can do RGB888/BGR888 */
  118. if (!(fb->panel->cntl & CNTL_LCDTFT))
  119. caps &= ~CLCD_CAP_888;
  120. memset(&var->transp, 0, sizeof(var->transp));
  121. var->red.msb_right = 0;
  122. var->green.msb_right = 0;
  123. var->blue.msb_right = 0;
  124. switch (var->bits_per_pixel) {
  125. case 1:
  126. case 2:
  127. case 4:
  128. case 8:
  129. /* If we can't do 5551, reject */
  130. caps &= CLCD_CAP_5551;
  131. if (!caps) {
  132. ret = -EINVAL;
  133. break;
  134. }
  135. var->red.length = var->bits_per_pixel;
  136. var->red.offset = 0;
  137. var->green.length = var->bits_per_pixel;
  138. var->green.offset = 0;
  139. var->blue.length = var->bits_per_pixel;
  140. var->blue.offset = 0;
  141. break;
  142. case 16:
  143. /* If we can't do 444, 5551 or 565, reject */
  144. if (!(caps & (CLCD_CAP_444 | CLCD_CAP_5551 | CLCD_CAP_565))) {
  145. ret = -EINVAL;
  146. break;
  147. }
  148. /*
  149. * Green length can be 4, 5 or 6 depending whether
  150. * we're operating in 444, 5551 or 565 mode.
  151. */
  152. if (var->green.length == 4 && caps & CLCD_CAP_444)
  153. caps &= CLCD_CAP_444;
  154. if (var->green.length == 5 && caps & CLCD_CAP_5551)
  155. caps &= CLCD_CAP_5551;
  156. else if (var->green.length == 6 && caps & CLCD_CAP_565)
  157. caps &= CLCD_CAP_565;
  158. else {
  159. /*
  160. * PL110 officially only supports RGB555,
  161. * but may be wired up to allow RGB565.
  162. */
  163. if (caps & CLCD_CAP_565) {
  164. var->green.length = 6;
  165. caps &= CLCD_CAP_565;
  166. } else if (caps & CLCD_CAP_5551) {
  167. var->green.length = 5;
  168. caps &= CLCD_CAP_5551;
  169. } else {
  170. var->green.length = 4;
  171. caps &= CLCD_CAP_444;
  172. }
  173. }
  174. if (var->green.length >= 5) {
  175. var->red.length = 5;
  176. var->blue.length = 5;
  177. } else {
  178. var->red.length = 4;
  179. var->blue.length = 4;
  180. }
  181. break;
  182. case 32:
  183. /* If we can't do 888, reject */
  184. caps &= CLCD_CAP_888;
  185. if (!caps) {
  186. ret = -EINVAL;
  187. break;
  188. }
  189. var->red.length = 8;
  190. var->green.length = 8;
  191. var->blue.length = 8;
  192. break;
  193. default:
  194. ret = -EINVAL;
  195. break;
  196. }
  197. /*
  198. * >= 16bpp displays have separate colour component bitfields
  199. * encoded in the pixel data. Calculate their position from
  200. * the bitfield length defined above.
  201. */
  202. if (ret == 0 && var->bits_per_pixel >= 16) {
  203. bool bgr, rgb;
  204. bgr = caps & CLCD_CAP_BGR && var->blue.offset == 0;
  205. rgb = caps & CLCD_CAP_RGB && var->red.offset == 0;
  206. if (!bgr && !rgb)
  207. /*
  208. * The requested format was not possible, try just
  209. * our capabilities. One of BGR or RGB must be
  210. * supported.
  211. */
  212. bgr = caps & CLCD_CAP_BGR;
  213. if (bgr) {
  214. var->blue.offset = 0;
  215. var->green.offset = var->blue.offset + var->blue.length;
  216. var->red.offset = var->green.offset + var->green.length;
  217. } else {
  218. var->red.offset = 0;
  219. var->green.offset = var->red.offset + var->red.length;
  220. var->blue.offset = var->green.offset + var->green.length;
  221. }
  222. }
  223. return ret;
  224. }
  225. static int clcdfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
  226. {
  227. struct clcd_fb *fb = to_clcd(info);
  228. int ret = -EINVAL;
  229. if (fb->board->check)
  230. ret = fb->board->check(fb, var);
  231. if (ret == 0 &&
  232. var->xres_virtual * var->bits_per_pixel / 8 *
  233. var->yres_virtual > fb->fb.fix.smem_len)
  234. ret = -EINVAL;
  235. if (ret == 0)
  236. ret = clcdfb_set_bitfields(fb, var);
  237. return ret;
  238. }
  239. static int clcdfb_set_par(struct fb_info *info)
  240. {
  241. struct clcd_fb *fb = to_clcd(info);
  242. struct clcd_regs regs;
  243. fb->fb.fix.line_length = fb->fb.var.xres_virtual *
  244. fb->fb.var.bits_per_pixel / 8;
  245. if (fb->fb.var.bits_per_pixel <= 8)
  246. fb->fb.fix.visual = FB_VISUAL_PSEUDOCOLOR;
  247. else
  248. fb->fb.fix.visual = FB_VISUAL_TRUECOLOR;
  249. fb->board->decode(fb, &regs);
  250. clcdfb_disable(fb);
  251. writel(regs.tim0, fb->regs + CLCD_TIM0);
  252. writel(regs.tim1, fb->regs + CLCD_TIM1);
  253. writel(regs.tim2, fb->regs + CLCD_TIM2);
  254. writel(regs.tim3, fb->regs + CLCD_TIM3);
  255. clcdfb_set_start(fb);
  256. clk_set_rate(fb->clk, (1000000000 / regs.pixclock) * 1000);
  257. fb->clcd_cntl = regs.cntl;
  258. clcdfb_enable(fb, regs.cntl);
  259. #ifdef DEBUG
  260. printk(KERN_INFO
  261. "CLCD: Registers set to\n"
  262. " %08x %08x %08x %08x\n"
  263. " %08x %08x %08x %08x\n",
  264. readl(fb->regs + CLCD_TIM0), readl(fb->regs + CLCD_TIM1),
  265. readl(fb->regs + CLCD_TIM2), readl(fb->regs + CLCD_TIM3),
  266. readl(fb->regs + CLCD_UBAS), readl(fb->regs + CLCD_LBAS),
  267. readl(fb->regs + fb->off_ienb), readl(fb->regs + fb->off_cntl));
  268. #endif
  269. return 0;
  270. }
  271. static inline u32 convert_bitfield(int val, struct fb_bitfield *bf)
  272. {
  273. unsigned int mask = (1 << bf->length) - 1;
  274. return (val >> (16 - bf->length) & mask) << bf->offset;
  275. }
  276. /*
  277. * Set a single color register. The values supplied have a 16 bit
  278. * magnitude. Return != 0 for invalid regno.
  279. */
  280. static int
  281. clcdfb_setcolreg(unsigned int regno, unsigned int red, unsigned int green,
  282. unsigned int blue, unsigned int transp, struct fb_info *info)
  283. {
  284. struct clcd_fb *fb = to_clcd(info);
  285. if (regno < 16)
  286. fb->cmap[regno] = convert_bitfield(transp, &fb->fb.var.transp) |
  287. convert_bitfield(blue, &fb->fb.var.blue) |
  288. convert_bitfield(green, &fb->fb.var.green) |
  289. convert_bitfield(red, &fb->fb.var.red);
  290. if (fb->fb.fix.visual == FB_VISUAL_PSEUDOCOLOR && regno < 256) {
  291. int hw_reg = CLCD_PALETTE + ((regno * 2) & ~3);
  292. u32 val, mask, newval;
  293. newval = (red >> 11) & 0x001f;
  294. newval |= (green >> 6) & 0x03e0;
  295. newval |= (blue >> 1) & 0x7c00;
  296. /*
  297. * 3.2.11: if we're configured for big endian
  298. * byte order, the palette entries are swapped.
  299. */
  300. if (fb->clcd_cntl & CNTL_BEBO)
  301. regno ^= 1;
  302. if (regno & 1) {
  303. newval <<= 16;
  304. mask = 0x0000ffff;
  305. } else {
  306. mask = 0xffff0000;
  307. }
  308. val = readl(fb->regs + hw_reg) & mask;
  309. writel(val | newval, fb->regs + hw_reg);
  310. }
  311. return regno > 255;
  312. }
  313. /*
  314. * Blank the screen if blank_mode != 0, else unblank. If blank == NULL
  315. * then the caller blanks by setting the CLUT (Color Look Up Table) to all
  316. * black. Return 0 if blanking succeeded, != 0 if un-/blanking failed due
  317. * to e.g. a video mode which doesn't support it. Implements VESA suspend
  318. * and powerdown modes on hardware that supports disabling hsync/vsync:
  319. * blank_mode == 2: suspend vsync
  320. * blank_mode == 3: suspend hsync
  321. * blank_mode == 4: powerdown
  322. */
  323. static int clcdfb_blank(int blank_mode, struct fb_info *info)
  324. {
  325. struct clcd_fb *fb = to_clcd(info);
  326. if (blank_mode != 0) {
  327. clcdfb_disable(fb);
  328. } else {
  329. clcdfb_enable(fb, fb->clcd_cntl);
  330. }
  331. return 0;
  332. }
  333. static int clcdfb_mmap(struct fb_info *info,
  334. struct vm_area_struct *vma)
  335. {
  336. struct clcd_fb *fb = to_clcd(info);
  337. unsigned long len, off = vma->vm_pgoff << PAGE_SHIFT;
  338. int ret = -EINVAL;
  339. len = info->fix.smem_len;
  340. if (off <= len && vma->vm_end - vma->vm_start <= len - off &&
  341. fb->board->mmap)
  342. ret = fb->board->mmap(fb, vma);
  343. return ret;
  344. }
  345. static struct fb_ops clcdfb_ops = {
  346. .owner = THIS_MODULE,
  347. .fb_check_var = clcdfb_check_var,
  348. .fb_set_par = clcdfb_set_par,
  349. .fb_setcolreg = clcdfb_setcolreg,
  350. .fb_blank = clcdfb_blank,
  351. .fb_fillrect = cfb_fillrect,
  352. .fb_copyarea = cfb_copyarea,
  353. .fb_imageblit = cfb_imageblit,
  354. .fb_mmap = clcdfb_mmap,
  355. };
  356. static int clcdfb_register(struct clcd_fb *fb)
  357. {
  358. int ret;
  359. /*
  360. * ARM PL111 always has IENB at 0x1c; it's only PL110
  361. * which is reversed on some platforms.
  362. */
  363. if (amba_manf(fb->dev) == 0x41 && amba_part(fb->dev) == 0x111) {
  364. fb->off_ienb = CLCD_PL111_IENB;
  365. fb->off_cntl = CLCD_PL111_CNTL;
  366. } else {
  367. #ifdef CONFIG_ARCH_VERSATILE
  368. fb->off_ienb = CLCD_PL111_IENB;
  369. fb->off_cntl = CLCD_PL111_CNTL;
  370. #else
  371. fb->off_ienb = CLCD_PL110_IENB;
  372. fb->off_cntl = CLCD_PL110_CNTL;
  373. #endif
  374. }
  375. fb->clk = clk_get(&fb->dev->dev, NULL);
  376. if (IS_ERR(fb->clk)) {
  377. ret = PTR_ERR(fb->clk);
  378. goto out;
  379. }
  380. ret = clk_prepare(fb->clk);
  381. if (ret)
  382. goto free_clk;
  383. fb->fb.device = &fb->dev->dev;
  384. fb->fb.fix.mmio_start = fb->dev->res.start;
  385. fb->fb.fix.mmio_len = resource_size(&fb->dev->res);
  386. fb->regs = ioremap(fb->fb.fix.mmio_start, fb->fb.fix.mmio_len);
  387. if (!fb->regs) {
  388. printk(KERN_ERR "CLCD: unable to remap registers\n");
  389. ret = -ENOMEM;
  390. goto clk_unprep;
  391. }
  392. fb->fb.fbops = &clcdfb_ops;
  393. fb->fb.flags = FBINFO_FLAG_DEFAULT;
  394. fb->fb.pseudo_palette = fb->cmap;
  395. strncpy(fb->fb.fix.id, clcd_name, sizeof(fb->fb.fix.id));
  396. fb->fb.fix.type = FB_TYPE_PACKED_PIXELS;
  397. fb->fb.fix.type_aux = 0;
  398. fb->fb.fix.xpanstep = 0;
  399. fb->fb.fix.ypanstep = 0;
  400. fb->fb.fix.ywrapstep = 0;
  401. fb->fb.fix.accel = FB_ACCEL_NONE;
  402. fb->fb.var.xres = fb->panel->mode.xres;
  403. fb->fb.var.yres = fb->panel->mode.yres;
  404. fb->fb.var.xres_virtual = fb->panel->mode.xres;
  405. fb->fb.var.yres_virtual = fb->panel->mode.yres;
  406. fb->fb.var.bits_per_pixel = fb->panel->bpp;
  407. fb->fb.var.grayscale = fb->panel->grayscale;
  408. fb->fb.var.pixclock = fb->panel->mode.pixclock;
  409. fb->fb.var.left_margin = fb->panel->mode.left_margin;
  410. fb->fb.var.right_margin = fb->panel->mode.right_margin;
  411. fb->fb.var.upper_margin = fb->panel->mode.upper_margin;
  412. fb->fb.var.lower_margin = fb->panel->mode.lower_margin;
  413. fb->fb.var.hsync_len = fb->panel->mode.hsync_len;
  414. fb->fb.var.vsync_len = fb->panel->mode.vsync_len;
  415. fb->fb.var.sync = fb->panel->mode.sync;
  416. fb->fb.var.vmode = fb->panel->mode.vmode;
  417. fb->fb.var.activate = FB_ACTIVATE_NOW;
  418. fb->fb.var.nonstd = 0;
  419. fb->fb.var.height = fb->panel->height;
  420. fb->fb.var.width = fb->panel->width;
  421. fb->fb.var.accel_flags = 0;
  422. fb->fb.monspecs.hfmin = 0;
  423. fb->fb.monspecs.hfmax = 100000;
  424. fb->fb.monspecs.vfmin = 0;
  425. fb->fb.monspecs.vfmax = 400;
  426. fb->fb.monspecs.dclkmin = 1000000;
  427. fb->fb.monspecs.dclkmax = 100000000;
  428. /*
  429. * Make sure that the bitfields are set appropriately.
  430. */
  431. clcdfb_set_bitfields(fb, &fb->fb.var);
  432. /*
  433. * Allocate colourmap.
  434. */
  435. ret = fb_alloc_cmap(&fb->fb.cmap, 256, 0);
  436. if (ret)
  437. goto unmap;
  438. /*
  439. * Ensure interrupts are disabled.
  440. */
  441. writel(0, fb->regs + fb->off_ienb);
  442. fb_set_var(&fb->fb, &fb->fb.var);
  443. dev_info(&fb->dev->dev, "%s hardware, %s display\n",
  444. fb->board->name, fb->panel->mode.name);
  445. ret = register_framebuffer(&fb->fb);
  446. if (ret == 0)
  447. goto out;
  448. printk(KERN_ERR "CLCD: cannot register framebuffer (%d)\n", ret);
  449. fb_dealloc_cmap(&fb->fb.cmap);
  450. unmap:
  451. iounmap(fb->regs);
  452. clk_unprep:
  453. clk_unprepare(fb->clk);
  454. free_clk:
  455. clk_put(fb->clk);
  456. out:
  457. return ret;
  458. }
  459. static int clcdfb_probe(struct amba_device *dev, const struct amba_id *id)
  460. {
  461. struct clcd_board *board = dev_get_platdata(&dev->dev);
  462. struct clcd_fb *fb;
  463. int ret;
  464. if (!board)
  465. return -EINVAL;
  466. ret = dma_set_mask_and_coherent(&dev->dev, DMA_BIT_MASK(32));
  467. if (ret)
  468. goto out;
  469. ret = amba_request_regions(dev, NULL);
  470. if (ret) {
  471. printk(KERN_ERR "CLCD: unable to reserve regs region\n");
  472. goto out;
  473. }
  474. fb = kzalloc(sizeof(struct clcd_fb), GFP_KERNEL);
  475. if (!fb) {
  476. printk(KERN_INFO "CLCD: could not allocate new clcd_fb struct\n");
  477. ret = -ENOMEM;
  478. goto free_region;
  479. }
  480. fb->dev = dev;
  481. fb->board = board;
  482. dev_info(&fb->dev->dev, "PL%03x rev%u at 0x%08llx\n",
  483. amba_part(dev), amba_rev(dev),
  484. (unsigned long long)dev->res.start);
  485. ret = fb->board->setup(fb);
  486. if (ret)
  487. goto free_fb;
  488. ret = clcdfb_register(fb);
  489. if (ret == 0) {
  490. amba_set_drvdata(dev, fb);
  491. goto out;
  492. }
  493. fb->board->remove(fb);
  494. free_fb:
  495. kfree(fb);
  496. free_region:
  497. amba_release_regions(dev);
  498. out:
  499. return ret;
  500. }
  501. static int clcdfb_remove(struct amba_device *dev)
  502. {
  503. struct clcd_fb *fb = amba_get_drvdata(dev);
  504. clcdfb_disable(fb);
  505. unregister_framebuffer(&fb->fb);
  506. if (fb->fb.cmap.len)
  507. fb_dealloc_cmap(&fb->fb.cmap);
  508. iounmap(fb->regs);
  509. clk_unprepare(fb->clk);
  510. clk_put(fb->clk);
  511. fb->board->remove(fb);
  512. kfree(fb);
  513. amba_release_regions(dev);
  514. return 0;
  515. }
  516. static struct amba_id clcdfb_id_table[] = {
  517. {
  518. .id = 0x00041110,
  519. .mask = 0x000ffffe,
  520. },
  521. { 0, 0 },
  522. };
  523. MODULE_DEVICE_TABLE(amba, clcdfb_id_table);
  524. static struct amba_driver clcd_driver = {
  525. .drv = {
  526. .name = "clcd-pl11x",
  527. },
  528. .probe = clcdfb_probe,
  529. .remove = clcdfb_remove,
  530. .id_table = clcdfb_id_table,
  531. };
  532. static int __init amba_clcdfb_init(void)
  533. {
  534. if (fb_get_options("ambafb", NULL))
  535. return -ENODEV;
  536. return amba_driver_register(&clcd_driver);
  537. }
  538. module_init(amba_clcdfb_init);
  539. static void __exit amba_clcdfb_exit(void)
  540. {
  541. amba_driver_unregister(&clcd_driver);
  542. }
  543. module_exit(amba_clcdfb_exit);
  544. MODULE_DESCRIPTION("ARM PrimeCell PL110 CLCD core driver");
  545. MODULE_LICENSE("GPL");