amba-clcd.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932
  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/bitops.h>
  28. #include <linux/clk.h>
  29. #include <linux/hardirq.h>
  30. #include <linux/of.h>
  31. #include <linux/of_address.h>
  32. #include <linux/of_graph.h>
  33. #include <video/display_timing.h>
  34. #include <video/of_display_timing.h>
  35. #include <video/videomode.h>
  36. #include <asm/sizes.h>
  37. #define to_clcd(info) container_of(info, struct clcd_fb, fb)
  38. /* This is limited to 16 characters when displayed by X startup */
  39. static const char *clcd_name = "CLCD FB";
  40. /*
  41. * Unfortunately, the enable/disable functions may be called either from
  42. * process or IRQ context, and we _need_ to delay. This is _not_ good.
  43. */
  44. static inline void clcdfb_sleep(unsigned int ms)
  45. {
  46. if (in_atomic()) {
  47. mdelay(ms);
  48. } else {
  49. msleep(ms);
  50. }
  51. }
  52. static inline void clcdfb_set_start(struct clcd_fb *fb)
  53. {
  54. unsigned long ustart = fb->fb.fix.smem_start;
  55. unsigned long lstart;
  56. ustart += fb->fb.var.yoffset * fb->fb.fix.line_length;
  57. lstart = ustart + fb->fb.var.yres * fb->fb.fix.line_length / 2;
  58. writel(ustart, fb->regs + CLCD_UBAS);
  59. writel(lstart, fb->regs + CLCD_LBAS);
  60. }
  61. static void clcdfb_disable(struct clcd_fb *fb)
  62. {
  63. u32 val;
  64. if (fb->board->disable)
  65. fb->board->disable(fb);
  66. val = readl(fb->regs + fb->off_cntl);
  67. if (val & CNTL_LCDPWR) {
  68. val &= ~CNTL_LCDPWR;
  69. writel(val, fb->regs + fb->off_cntl);
  70. clcdfb_sleep(20);
  71. }
  72. if (val & CNTL_LCDEN) {
  73. val &= ~CNTL_LCDEN;
  74. writel(val, fb->regs + fb->off_cntl);
  75. }
  76. /*
  77. * Disable CLCD clock source.
  78. */
  79. if (fb->clk_enabled) {
  80. fb->clk_enabled = false;
  81. clk_disable(fb->clk);
  82. }
  83. }
  84. static void clcdfb_enable(struct clcd_fb *fb, u32 cntl)
  85. {
  86. /*
  87. * Enable the CLCD clock source.
  88. */
  89. if (!fb->clk_enabled) {
  90. fb->clk_enabled = true;
  91. clk_enable(fb->clk);
  92. }
  93. /*
  94. * Bring up by first enabling..
  95. */
  96. cntl |= CNTL_LCDEN;
  97. writel(cntl, fb->regs + fb->off_cntl);
  98. clcdfb_sleep(20);
  99. /*
  100. * and now apply power.
  101. */
  102. cntl |= CNTL_LCDPWR;
  103. writel(cntl, fb->regs + fb->off_cntl);
  104. /*
  105. * finally, enable the interface.
  106. */
  107. if (fb->board->enable)
  108. fb->board->enable(fb);
  109. }
  110. static int
  111. clcdfb_set_bitfields(struct clcd_fb *fb, struct fb_var_screeninfo *var)
  112. {
  113. u32 caps;
  114. int ret = 0;
  115. if (fb->panel->caps && fb->board->caps)
  116. caps = fb->panel->caps & fb->board->caps;
  117. else {
  118. /* Old way of specifying what can be used */
  119. caps = fb->panel->cntl & CNTL_BGR ?
  120. CLCD_CAP_BGR : CLCD_CAP_RGB;
  121. /* But mask out 444 modes as they weren't supported */
  122. caps &= ~CLCD_CAP_444;
  123. }
  124. /* Only TFT panels can do RGB888/BGR888 */
  125. if (!(fb->panel->cntl & CNTL_LCDTFT))
  126. caps &= ~CLCD_CAP_888;
  127. memset(&var->transp, 0, sizeof(var->transp));
  128. var->red.msb_right = 0;
  129. var->green.msb_right = 0;
  130. var->blue.msb_right = 0;
  131. switch (var->bits_per_pixel) {
  132. case 1:
  133. case 2:
  134. case 4:
  135. case 8:
  136. /* If we can't do 5551, reject */
  137. caps &= CLCD_CAP_5551;
  138. if (!caps) {
  139. ret = -EINVAL;
  140. break;
  141. }
  142. var->red.length = var->bits_per_pixel;
  143. var->red.offset = 0;
  144. var->green.length = var->bits_per_pixel;
  145. var->green.offset = 0;
  146. var->blue.length = var->bits_per_pixel;
  147. var->blue.offset = 0;
  148. break;
  149. case 16:
  150. /* If we can't do 444, 5551 or 565, reject */
  151. if (!(caps & (CLCD_CAP_444 | CLCD_CAP_5551 | CLCD_CAP_565))) {
  152. ret = -EINVAL;
  153. break;
  154. }
  155. /*
  156. * Green length can be 4, 5 or 6 depending whether
  157. * we're operating in 444, 5551 or 565 mode.
  158. */
  159. if (var->green.length == 4 && caps & CLCD_CAP_444)
  160. caps &= CLCD_CAP_444;
  161. if (var->green.length == 5 && caps & CLCD_CAP_5551)
  162. caps &= CLCD_CAP_5551;
  163. else if (var->green.length == 6 && caps & CLCD_CAP_565)
  164. caps &= CLCD_CAP_565;
  165. else {
  166. /*
  167. * PL110 officially only supports RGB555,
  168. * but may be wired up to allow RGB565.
  169. */
  170. if (caps & CLCD_CAP_565) {
  171. var->green.length = 6;
  172. caps &= CLCD_CAP_565;
  173. } else if (caps & CLCD_CAP_5551) {
  174. var->green.length = 5;
  175. caps &= CLCD_CAP_5551;
  176. } else {
  177. var->green.length = 4;
  178. caps &= CLCD_CAP_444;
  179. }
  180. }
  181. if (var->green.length >= 5) {
  182. var->red.length = 5;
  183. var->blue.length = 5;
  184. } else {
  185. var->red.length = 4;
  186. var->blue.length = 4;
  187. }
  188. break;
  189. case 32:
  190. /* If we can't do 888, reject */
  191. caps &= CLCD_CAP_888;
  192. if (!caps) {
  193. ret = -EINVAL;
  194. break;
  195. }
  196. var->red.length = 8;
  197. var->green.length = 8;
  198. var->blue.length = 8;
  199. break;
  200. default:
  201. ret = -EINVAL;
  202. break;
  203. }
  204. /*
  205. * >= 16bpp displays have separate colour component bitfields
  206. * encoded in the pixel data. Calculate their position from
  207. * the bitfield length defined above.
  208. */
  209. if (ret == 0 && var->bits_per_pixel >= 16) {
  210. bool bgr, rgb;
  211. bgr = caps & CLCD_CAP_BGR && var->blue.offset == 0;
  212. rgb = caps & CLCD_CAP_RGB && var->red.offset == 0;
  213. if (!bgr && !rgb)
  214. /*
  215. * The requested format was not possible, try just
  216. * our capabilities. One of BGR or RGB must be
  217. * supported.
  218. */
  219. bgr = caps & CLCD_CAP_BGR;
  220. if (bgr) {
  221. var->blue.offset = 0;
  222. var->green.offset = var->blue.offset + var->blue.length;
  223. var->red.offset = var->green.offset + var->green.length;
  224. } else {
  225. var->red.offset = 0;
  226. var->green.offset = var->red.offset + var->red.length;
  227. var->blue.offset = var->green.offset + var->green.length;
  228. }
  229. }
  230. return ret;
  231. }
  232. static int clcdfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
  233. {
  234. struct clcd_fb *fb = to_clcd(info);
  235. int ret = -EINVAL;
  236. if (fb->board->check)
  237. ret = fb->board->check(fb, var);
  238. if (ret == 0 &&
  239. var->xres_virtual * var->bits_per_pixel / 8 *
  240. var->yres_virtual > fb->fb.fix.smem_len)
  241. ret = -EINVAL;
  242. if (ret == 0)
  243. ret = clcdfb_set_bitfields(fb, var);
  244. return ret;
  245. }
  246. static int clcdfb_set_par(struct fb_info *info)
  247. {
  248. struct clcd_fb *fb = to_clcd(info);
  249. struct clcd_regs regs;
  250. fb->fb.fix.line_length = fb->fb.var.xres_virtual *
  251. fb->fb.var.bits_per_pixel / 8;
  252. if (fb->fb.var.bits_per_pixel <= 8)
  253. fb->fb.fix.visual = FB_VISUAL_PSEUDOCOLOR;
  254. else
  255. fb->fb.fix.visual = FB_VISUAL_TRUECOLOR;
  256. fb->board->decode(fb, &regs);
  257. clcdfb_disable(fb);
  258. writel(regs.tim0, fb->regs + CLCD_TIM0);
  259. writel(regs.tim1, fb->regs + CLCD_TIM1);
  260. writel(regs.tim2, fb->regs + CLCD_TIM2);
  261. writel(regs.tim3, fb->regs + CLCD_TIM3);
  262. clcdfb_set_start(fb);
  263. clk_set_rate(fb->clk, (1000000000 / regs.pixclock) * 1000);
  264. fb->clcd_cntl = regs.cntl;
  265. clcdfb_enable(fb, regs.cntl);
  266. #ifdef DEBUG
  267. printk(KERN_INFO
  268. "CLCD: Registers set to\n"
  269. " %08x %08x %08x %08x\n"
  270. " %08x %08x %08x %08x\n",
  271. readl(fb->regs + CLCD_TIM0), readl(fb->regs + CLCD_TIM1),
  272. readl(fb->regs + CLCD_TIM2), readl(fb->regs + CLCD_TIM3),
  273. readl(fb->regs + CLCD_UBAS), readl(fb->regs + CLCD_LBAS),
  274. readl(fb->regs + fb->off_ienb), readl(fb->regs + fb->off_cntl));
  275. #endif
  276. return 0;
  277. }
  278. static inline u32 convert_bitfield(int val, struct fb_bitfield *bf)
  279. {
  280. unsigned int mask = (1 << bf->length) - 1;
  281. return (val >> (16 - bf->length) & mask) << bf->offset;
  282. }
  283. /*
  284. * Set a single color register. The values supplied have a 16 bit
  285. * magnitude. Return != 0 for invalid regno.
  286. */
  287. static int
  288. clcdfb_setcolreg(unsigned int regno, unsigned int red, unsigned int green,
  289. unsigned int blue, unsigned int transp, struct fb_info *info)
  290. {
  291. struct clcd_fb *fb = to_clcd(info);
  292. if (regno < 16)
  293. fb->cmap[regno] = convert_bitfield(transp, &fb->fb.var.transp) |
  294. convert_bitfield(blue, &fb->fb.var.blue) |
  295. convert_bitfield(green, &fb->fb.var.green) |
  296. convert_bitfield(red, &fb->fb.var.red);
  297. if (fb->fb.fix.visual == FB_VISUAL_PSEUDOCOLOR && regno < 256) {
  298. int hw_reg = CLCD_PALETTE + ((regno * 2) & ~3);
  299. u32 val, mask, newval;
  300. newval = (red >> 11) & 0x001f;
  301. newval |= (green >> 6) & 0x03e0;
  302. newval |= (blue >> 1) & 0x7c00;
  303. /*
  304. * 3.2.11: if we're configured for big endian
  305. * byte order, the palette entries are swapped.
  306. */
  307. if (fb->clcd_cntl & CNTL_BEBO)
  308. regno ^= 1;
  309. if (regno & 1) {
  310. newval <<= 16;
  311. mask = 0x0000ffff;
  312. } else {
  313. mask = 0xffff0000;
  314. }
  315. val = readl(fb->regs + hw_reg) & mask;
  316. writel(val | newval, fb->regs + hw_reg);
  317. }
  318. return regno > 255;
  319. }
  320. /*
  321. * Blank the screen if blank_mode != 0, else unblank. If blank == NULL
  322. * then the caller blanks by setting the CLUT (Color Look Up Table) to all
  323. * black. Return 0 if blanking succeeded, != 0 if un-/blanking failed due
  324. * to e.g. a video mode which doesn't support it. Implements VESA suspend
  325. * and powerdown modes on hardware that supports disabling hsync/vsync:
  326. * blank_mode == 2: suspend vsync
  327. * blank_mode == 3: suspend hsync
  328. * blank_mode == 4: powerdown
  329. */
  330. static int clcdfb_blank(int blank_mode, struct fb_info *info)
  331. {
  332. struct clcd_fb *fb = to_clcd(info);
  333. if (blank_mode != 0) {
  334. clcdfb_disable(fb);
  335. } else {
  336. clcdfb_enable(fb, fb->clcd_cntl);
  337. }
  338. return 0;
  339. }
  340. static int clcdfb_mmap(struct fb_info *info,
  341. struct vm_area_struct *vma)
  342. {
  343. struct clcd_fb *fb = to_clcd(info);
  344. unsigned long len, off = vma->vm_pgoff << PAGE_SHIFT;
  345. int ret = -EINVAL;
  346. len = info->fix.smem_len;
  347. if (off <= len && vma->vm_end - vma->vm_start <= len - off &&
  348. fb->board->mmap)
  349. ret = fb->board->mmap(fb, vma);
  350. return ret;
  351. }
  352. static struct fb_ops clcdfb_ops = {
  353. .owner = THIS_MODULE,
  354. .fb_check_var = clcdfb_check_var,
  355. .fb_set_par = clcdfb_set_par,
  356. .fb_setcolreg = clcdfb_setcolreg,
  357. .fb_blank = clcdfb_blank,
  358. .fb_fillrect = cfb_fillrect,
  359. .fb_copyarea = cfb_copyarea,
  360. .fb_imageblit = cfb_imageblit,
  361. .fb_mmap = clcdfb_mmap,
  362. };
  363. static int clcdfb_register(struct clcd_fb *fb)
  364. {
  365. int ret;
  366. /*
  367. * ARM PL111 always has IENB at 0x1c; it's only PL110
  368. * which is reversed on some platforms.
  369. */
  370. if (amba_manf(fb->dev) == 0x41 && amba_part(fb->dev) == 0x111) {
  371. fb->off_ienb = CLCD_PL111_IENB;
  372. fb->off_cntl = CLCD_PL111_CNTL;
  373. } else {
  374. #ifdef CONFIG_ARCH_VERSATILE
  375. fb->off_ienb = CLCD_PL111_IENB;
  376. fb->off_cntl = CLCD_PL111_CNTL;
  377. #else
  378. fb->off_ienb = CLCD_PL110_IENB;
  379. fb->off_cntl = CLCD_PL110_CNTL;
  380. #endif
  381. }
  382. fb->clk = clk_get(&fb->dev->dev, NULL);
  383. if (IS_ERR(fb->clk)) {
  384. ret = PTR_ERR(fb->clk);
  385. goto out;
  386. }
  387. ret = clk_prepare(fb->clk);
  388. if (ret)
  389. goto free_clk;
  390. fb->fb.device = &fb->dev->dev;
  391. fb->fb.fix.mmio_start = fb->dev->res.start;
  392. fb->fb.fix.mmio_len = resource_size(&fb->dev->res);
  393. fb->regs = ioremap(fb->fb.fix.mmio_start, fb->fb.fix.mmio_len);
  394. if (!fb->regs) {
  395. printk(KERN_ERR "CLCD: unable to remap registers\n");
  396. ret = -ENOMEM;
  397. goto clk_unprep;
  398. }
  399. fb->fb.fbops = &clcdfb_ops;
  400. fb->fb.flags = FBINFO_FLAG_DEFAULT;
  401. fb->fb.pseudo_palette = fb->cmap;
  402. strncpy(fb->fb.fix.id, clcd_name, sizeof(fb->fb.fix.id));
  403. fb->fb.fix.type = FB_TYPE_PACKED_PIXELS;
  404. fb->fb.fix.type_aux = 0;
  405. fb->fb.fix.xpanstep = 0;
  406. fb->fb.fix.ypanstep = 0;
  407. fb->fb.fix.ywrapstep = 0;
  408. fb->fb.fix.accel = FB_ACCEL_NONE;
  409. fb->fb.var.xres = fb->panel->mode.xres;
  410. fb->fb.var.yres = fb->panel->mode.yres;
  411. fb->fb.var.xres_virtual = fb->panel->mode.xres;
  412. fb->fb.var.yres_virtual = fb->panel->mode.yres;
  413. fb->fb.var.bits_per_pixel = fb->panel->bpp;
  414. fb->fb.var.grayscale = fb->panel->grayscale;
  415. fb->fb.var.pixclock = fb->panel->mode.pixclock;
  416. fb->fb.var.left_margin = fb->panel->mode.left_margin;
  417. fb->fb.var.right_margin = fb->panel->mode.right_margin;
  418. fb->fb.var.upper_margin = fb->panel->mode.upper_margin;
  419. fb->fb.var.lower_margin = fb->panel->mode.lower_margin;
  420. fb->fb.var.hsync_len = fb->panel->mode.hsync_len;
  421. fb->fb.var.vsync_len = fb->panel->mode.vsync_len;
  422. fb->fb.var.sync = fb->panel->mode.sync;
  423. fb->fb.var.vmode = fb->panel->mode.vmode;
  424. fb->fb.var.activate = FB_ACTIVATE_NOW;
  425. fb->fb.var.nonstd = 0;
  426. fb->fb.var.height = fb->panel->height;
  427. fb->fb.var.width = fb->panel->width;
  428. fb->fb.var.accel_flags = 0;
  429. fb->fb.monspecs.hfmin = 0;
  430. fb->fb.monspecs.hfmax = 100000;
  431. fb->fb.monspecs.vfmin = 0;
  432. fb->fb.monspecs.vfmax = 400;
  433. fb->fb.monspecs.dclkmin = 1000000;
  434. fb->fb.monspecs.dclkmax = 100000000;
  435. /*
  436. * Make sure that the bitfields are set appropriately.
  437. */
  438. clcdfb_set_bitfields(fb, &fb->fb.var);
  439. /*
  440. * Allocate colourmap.
  441. */
  442. ret = fb_alloc_cmap(&fb->fb.cmap, 256, 0);
  443. if (ret)
  444. goto unmap;
  445. /*
  446. * Ensure interrupts are disabled.
  447. */
  448. writel(0, fb->regs + fb->off_ienb);
  449. fb_set_var(&fb->fb, &fb->fb.var);
  450. dev_info(&fb->dev->dev, "%s hardware, %s display\n",
  451. fb->board->name, fb->panel->mode.name);
  452. ret = register_framebuffer(&fb->fb);
  453. if (ret == 0)
  454. goto out;
  455. printk(KERN_ERR "CLCD: cannot register framebuffer (%d)\n", ret);
  456. fb_dealloc_cmap(&fb->fb.cmap);
  457. unmap:
  458. iounmap(fb->regs);
  459. clk_unprep:
  460. clk_unprepare(fb->clk);
  461. free_clk:
  462. clk_put(fb->clk);
  463. out:
  464. return ret;
  465. }
  466. #ifdef CONFIG_OF
  467. static int clcdfb_of_get_dpi_panel_mode(struct device_node *node,
  468. struct fb_videomode *mode)
  469. {
  470. int err;
  471. struct display_timing timing;
  472. struct videomode video;
  473. err = of_get_display_timing(node, "panel-timing", &timing);
  474. if (err)
  475. return err;
  476. videomode_from_timing(&timing, &video);
  477. err = fb_videomode_from_videomode(&video, mode);
  478. if (err)
  479. return err;
  480. return 0;
  481. }
  482. static int clcdfb_snprintf_mode(char *buf, int size, struct fb_videomode *mode)
  483. {
  484. return snprintf(buf, size, "%ux%u@%u", mode->xres, mode->yres,
  485. mode->refresh);
  486. }
  487. static int clcdfb_of_get_mode(struct device *dev, struct device_node *endpoint,
  488. struct fb_videomode *mode)
  489. {
  490. int err;
  491. struct device_node *panel;
  492. char *name;
  493. int len;
  494. panel = of_graph_get_remote_port_parent(endpoint);
  495. if (!panel)
  496. return -ENODEV;
  497. /* Only directly connected DPI panels supported for now */
  498. if (of_device_is_compatible(panel, "panel-dpi"))
  499. err = clcdfb_of_get_dpi_panel_mode(panel, mode);
  500. else
  501. err = -ENOENT;
  502. if (err)
  503. return err;
  504. len = clcdfb_snprintf_mode(NULL, 0, mode);
  505. name = devm_kzalloc(dev, len + 1, GFP_KERNEL);
  506. if (!name)
  507. return -ENOMEM;
  508. clcdfb_snprintf_mode(name, len + 1, mode);
  509. mode->name = name;
  510. return 0;
  511. }
  512. static int clcdfb_of_init_tft_panel(struct clcd_fb *fb, u32 r0, u32 g0, u32 b0)
  513. {
  514. static struct {
  515. unsigned int part;
  516. u32 r0, g0, b0;
  517. u32 caps;
  518. } panels[] = {
  519. { 0x110, 1, 7, 13, CLCD_CAP_5551 },
  520. { 0x110, 0, 8, 16, CLCD_CAP_888 },
  521. { 0x111, 4, 14, 20, CLCD_CAP_444 },
  522. { 0x111, 3, 11, 19, CLCD_CAP_444 | CLCD_CAP_5551 },
  523. { 0x111, 3, 10, 19, CLCD_CAP_444 | CLCD_CAP_5551 |
  524. CLCD_CAP_565 },
  525. { 0x111, 0, 8, 16, CLCD_CAP_444 | CLCD_CAP_5551 |
  526. CLCD_CAP_565 | CLCD_CAP_888 },
  527. };
  528. int i;
  529. /* Bypass pixel clock divider, data output on the falling edge */
  530. fb->panel->tim2 = TIM2_BCD | TIM2_IPC;
  531. /* TFT display, vert. comp. interrupt at the start of the back porch */
  532. fb->panel->cntl |= CNTL_LCDTFT | CNTL_LCDVCOMP(1);
  533. fb->panel->caps = 0;
  534. /* Match the setup with known variants */
  535. for (i = 0; i < ARRAY_SIZE(panels) && !fb->panel->caps; i++) {
  536. if (amba_part(fb->dev) != panels[i].part)
  537. continue;
  538. if (g0 != panels[i].g0)
  539. continue;
  540. if (r0 == panels[i].r0 && b0 == panels[i].b0)
  541. fb->panel->caps = panels[i].caps;
  542. }
  543. return fb->panel->caps ? 0 : -EINVAL;
  544. }
  545. static int clcdfb_of_init_display(struct clcd_fb *fb)
  546. {
  547. struct device_node *endpoint;
  548. int err;
  549. unsigned int bpp;
  550. u32 max_bandwidth;
  551. u32 tft_r0b0g0[3];
  552. fb->panel = devm_kzalloc(&fb->dev->dev, sizeof(*fb->panel), GFP_KERNEL);
  553. if (!fb->panel)
  554. return -ENOMEM;
  555. endpoint = of_graph_get_next_endpoint(fb->dev->dev.of_node, NULL);
  556. if (!endpoint)
  557. return -ENODEV;
  558. err = clcdfb_of_get_mode(&fb->dev->dev, endpoint, &fb->panel->mode);
  559. if (err)
  560. return err;
  561. err = of_property_read_u32(fb->dev->dev.of_node, "max-memory-bandwidth",
  562. &max_bandwidth);
  563. if (!err) {
  564. /*
  565. * max_bandwidth is in bytes per second and pixclock in
  566. * pico-seconds, so the maximum allowed bits per pixel is
  567. * 8 * max_bandwidth / (PICOS2KHZ(pixclock) * 1000)
  568. * Rearrange this calculation to avoid overflow and then ensure
  569. * result is a valid format.
  570. */
  571. bpp = max_bandwidth / (1000 / 8)
  572. / PICOS2KHZ(fb->panel->mode.pixclock);
  573. bpp = rounddown_pow_of_two(bpp);
  574. if (bpp > 32)
  575. bpp = 32;
  576. } else
  577. bpp = 32;
  578. fb->panel->bpp = bpp;
  579. #ifdef CONFIG_CPU_BIG_ENDIAN
  580. fb->panel->cntl |= CNTL_BEBO;
  581. #endif
  582. fb->panel->width = -1;
  583. fb->panel->height = -1;
  584. if (of_property_read_u32_array(endpoint,
  585. "arm,pl11x,tft-r0g0b0-pads",
  586. tft_r0b0g0, ARRAY_SIZE(tft_r0b0g0)) == 0)
  587. return clcdfb_of_init_tft_panel(fb, tft_r0b0g0[0],
  588. tft_r0b0g0[1], tft_r0b0g0[2]);
  589. return -ENOENT;
  590. }
  591. static int clcdfb_of_vram_setup(struct clcd_fb *fb)
  592. {
  593. int err;
  594. struct device_node *memory;
  595. u64 size;
  596. err = clcdfb_of_init_display(fb);
  597. if (err)
  598. return err;
  599. memory = of_parse_phandle(fb->dev->dev.of_node, "memory-region", 0);
  600. if (!memory)
  601. return -ENODEV;
  602. fb->fb.screen_base = of_iomap(memory, 0);
  603. if (!fb->fb.screen_base)
  604. return -ENOMEM;
  605. fb->fb.fix.smem_start = of_translate_address(memory,
  606. of_get_address(memory, 0, &size, NULL));
  607. fb->fb.fix.smem_len = size;
  608. return 0;
  609. }
  610. static int clcdfb_of_vram_mmap(struct clcd_fb *fb, struct vm_area_struct *vma)
  611. {
  612. unsigned long off, user_size, kernel_size;
  613. off = vma->vm_pgoff << PAGE_SHIFT;
  614. user_size = vma->vm_end - vma->vm_start;
  615. kernel_size = fb->fb.fix.smem_len;
  616. if (off >= kernel_size || user_size > (kernel_size - off))
  617. return -ENXIO;
  618. return remap_pfn_range(vma, vma->vm_start,
  619. __phys_to_pfn(fb->fb.fix.smem_start) + vma->vm_pgoff,
  620. user_size,
  621. pgprot_writecombine(vma->vm_page_prot));
  622. }
  623. static void clcdfb_of_vram_remove(struct clcd_fb *fb)
  624. {
  625. iounmap(fb->fb.screen_base);
  626. }
  627. static int clcdfb_of_dma_setup(struct clcd_fb *fb)
  628. {
  629. unsigned long framesize;
  630. dma_addr_t dma;
  631. int err;
  632. err = clcdfb_of_init_display(fb);
  633. if (err)
  634. return err;
  635. framesize = PAGE_ALIGN(fb->panel->mode.xres * fb->panel->mode.yres *
  636. fb->panel->bpp / 8);
  637. fb->fb.screen_base = dma_alloc_coherent(&fb->dev->dev, framesize,
  638. &dma, GFP_KERNEL);
  639. if (!fb->fb.screen_base)
  640. return -ENOMEM;
  641. fb->fb.fix.smem_start = dma;
  642. fb->fb.fix.smem_len = framesize;
  643. return 0;
  644. }
  645. static int clcdfb_of_dma_mmap(struct clcd_fb *fb, struct vm_area_struct *vma)
  646. {
  647. return dma_mmap_writecombine(&fb->dev->dev, vma, fb->fb.screen_base,
  648. fb->fb.fix.smem_start, fb->fb.fix.smem_len);
  649. }
  650. static void clcdfb_of_dma_remove(struct clcd_fb *fb)
  651. {
  652. dma_free_coherent(&fb->dev->dev, fb->fb.fix.smem_len,
  653. fb->fb.screen_base, fb->fb.fix.smem_start);
  654. }
  655. static struct clcd_board *clcdfb_of_get_board(struct amba_device *dev)
  656. {
  657. struct clcd_board *board = devm_kzalloc(&dev->dev, sizeof(*board),
  658. GFP_KERNEL);
  659. struct device_node *node = dev->dev.of_node;
  660. if (!board)
  661. return NULL;
  662. board->name = of_node_full_name(node);
  663. board->caps = CLCD_CAP_ALL;
  664. board->check = clcdfb_check;
  665. board->decode = clcdfb_decode;
  666. if (of_find_property(node, "memory-region", NULL)) {
  667. board->setup = clcdfb_of_vram_setup;
  668. board->mmap = clcdfb_of_vram_mmap;
  669. board->remove = clcdfb_of_vram_remove;
  670. } else {
  671. board->setup = clcdfb_of_dma_setup;
  672. board->mmap = clcdfb_of_dma_mmap;
  673. board->remove = clcdfb_of_dma_remove;
  674. }
  675. return board;
  676. }
  677. #else
  678. static struct clcd_board *clcdfb_of_get_board(struct amba_device *dev)
  679. {
  680. return NULL;
  681. }
  682. #endif
  683. static int clcdfb_probe(struct amba_device *dev, const struct amba_id *id)
  684. {
  685. struct clcd_board *board = dev_get_platdata(&dev->dev);
  686. struct clcd_fb *fb;
  687. int ret;
  688. if (!board)
  689. board = clcdfb_of_get_board(dev);
  690. if (!board)
  691. return -EINVAL;
  692. ret = dma_set_mask_and_coherent(&dev->dev, DMA_BIT_MASK(32));
  693. if (ret)
  694. goto out;
  695. ret = amba_request_regions(dev, NULL);
  696. if (ret) {
  697. printk(KERN_ERR "CLCD: unable to reserve regs region\n");
  698. goto out;
  699. }
  700. fb = kzalloc(sizeof(struct clcd_fb), GFP_KERNEL);
  701. if (!fb) {
  702. printk(KERN_INFO "CLCD: could not allocate new clcd_fb struct\n");
  703. ret = -ENOMEM;
  704. goto free_region;
  705. }
  706. fb->dev = dev;
  707. fb->board = board;
  708. dev_info(&fb->dev->dev, "PL%03x rev%u at 0x%08llx\n",
  709. amba_part(dev), amba_rev(dev),
  710. (unsigned long long)dev->res.start);
  711. ret = fb->board->setup(fb);
  712. if (ret)
  713. goto free_fb;
  714. ret = clcdfb_register(fb);
  715. if (ret == 0) {
  716. amba_set_drvdata(dev, fb);
  717. goto out;
  718. }
  719. fb->board->remove(fb);
  720. free_fb:
  721. kfree(fb);
  722. free_region:
  723. amba_release_regions(dev);
  724. out:
  725. return ret;
  726. }
  727. static int clcdfb_remove(struct amba_device *dev)
  728. {
  729. struct clcd_fb *fb = amba_get_drvdata(dev);
  730. clcdfb_disable(fb);
  731. unregister_framebuffer(&fb->fb);
  732. if (fb->fb.cmap.len)
  733. fb_dealloc_cmap(&fb->fb.cmap);
  734. iounmap(fb->regs);
  735. clk_unprepare(fb->clk);
  736. clk_put(fb->clk);
  737. fb->board->remove(fb);
  738. kfree(fb);
  739. amba_release_regions(dev);
  740. return 0;
  741. }
  742. static struct amba_id clcdfb_id_table[] = {
  743. {
  744. .id = 0x00041110,
  745. .mask = 0x000ffffe,
  746. },
  747. { 0, 0 },
  748. };
  749. MODULE_DEVICE_TABLE(amba, clcdfb_id_table);
  750. static struct amba_driver clcd_driver = {
  751. .drv = {
  752. .name = "clcd-pl11x",
  753. },
  754. .probe = clcdfb_probe,
  755. .remove = clcdfb_remove,
  756. .id_table = clcdfb_id_table,
  757. };
  758. static int __init amba_clcdfb_init(void)
  759. {
  760. if (fb_get_options("ambafb", NULL))
  761. return -ENODEV;
  762. return amba_driver_register(&clcd_driver);
  763. }
  764. module_init(amba_clcdfb_init);
  765. static void __exit amba_clcdfb_exit(void)
  766. {
  767. amba_driver_unregister(&clcd_driver);
  768. }
  769. module_exit(amba_clcdfb_exit);
  770. MODULE_DESCRIPTION("ARM PrimeCell PL110 CLCD core driver");
  771. MODULE_LICENSE("GPL");