amba-clcd.c 14 KB

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