sh7760fb.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. /*
  2. * SH7760/SH7763 LCDC Framebuffer driver.
  3. *
  4. * (c) 2006-2008 MSC Vertriebsges.m.b.H.,
  5. * Manuel Lauss <mano@roarinelk.homelinux.net>
  6. * (c) 2008 Nobuhiro Iwamatsu <iwamatsu.nobuhiro@renesas.com>
  7. *
  8. * This file is subject to the terms and conditions of the GNU General
  9. * Public License. See the file COPYING in the main directory of this
  10. * archive for more details.
  11. *
  12. * PLEASE HAVE A LOOK AT Documentation/fb/sh7760fb.txt!
  13. *
  14. * Thanks to Siegfried Schaefer <s.schaefer at schaefer-edv.de>
  15. * for his original source and testing!
  16. *
  17. * sh7760_setcolreg get from drivers/video/sh_mobile_lcdcfb.c
  18. */
  19. #include <linux/completion.h>
  20. #include <linux/delay.h>
  21. #include <linux/dma-mapping.h>
  22. #include <linux/fb.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/io.h>
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/slab.h>
  29. #include <asm/sh7760fb.h>
  30. struct sh7760fb_par {
  31. void __iomem *base;
  32. int irq;
  33. struct sh7760fb_platdata *pd; /* display information */
  34. dma_addr_t fbdma; /* physical address */
  35. int rot; /* rotation enabled? */
  36. u32 pseudo_palette[16];
  37. struct platform_device *dev;
  38. struct resource *ioarea;
  39. struct completion vsync; /* vsync irq event */
  40. };
  41. static irqreturn_t sh7760fb_irq(int irq, void *data)
  42. {
  43. struct completion *c = data;
  44. complete(c);
  45. return IRQ_HANDLED;
  46. }
  47. /* wait_for_lps - wait until power supply has reached a certain state. */
  48. static int wait_for_lps(struct sh7760fb_par *par, int val)
  49. {
  50. int i = 100;
  51. while (--i && ((ioread16(par->base + LDPMMR) & 3) != val))
  52. msleep(1);
  53. if (i <= 0)
  54. return -ETIMEDOUT;
  55. return 0;
  56. }
  57. /* en/disable the LCDC */
  58. static int sh7760fb_blank(int blank, struct fb_info *info)
  59. {
  60. struct sh7760fb_par *par = info->par;
  61. struct sh7760fb_platdata *pd = par->pd;
  62. unsigned short cntr = ioread16(par->base + LDCNTR);
  63. unsigned short intr = ioread16(par->base + LDINTR);
  64. int lps;
  65. if (blank == FB_BLANK_UNBLANK) {
  66. intr |= VINT_START;
  67. cntr = LDCNTR_DON2 | LDCNTR_DON;
  68. lps = 3;
  69. } else {
  70. intr &= ~VINT_START;
  71. cntr = LDCNTR_DON2;
  72. lps = 0;
  73. }
  74. if (pd->blank)
  75. pd->blank(blank);
  76. iowrite16(intr, par->base + LDINTR);
  77. iowrite16(cntr, par->base + LDCNTR);
  78. return wait_for_lps(par, lps);
  79. }
  80. static int sh7760_setcolreg (u_int regno,
  81. u_int red, u_int green, u_int blue,
  82. u_int transp, struct fb_info *info)
  83. {
  84. u32 *palette = info->pseudo_palette;
  85. if (regno >= 16)
  86. return -EINVAL;
  87. /* only FB_VISUAL_TRUECOLOR supported */
  88. red >>= 16 - info->var.red.length;
  89. green >>= 16 - info->var.green.length;
  90. blue >>= 16 - info->var.blue.length;
  91. transp >>= 16 - info->var.transp.length;
  92. palette[regno] = (red << info->var.red.offset) |
  93. (green << info->var.green.offset) |
  94. (blue << info->var.blue.offset) |
  95. (transp << info->var.transp.offset);
  96. return 0;
  97. }
  98. static int sh7760fb_get_color_info(struct device *dev,
  99. u16 lddfr, int *bpp, int *gray)
  100. {
  101. int lbpp, lgray;
  102. lgray = lbpp = 0;
  103. switch (lddfr & LDDFR_COLOR_MASK) {
  104. case LDDFR_1BPP_MONO:
  105. lgray = 1;
  106. lbpp = 1;
  107. break;
  108. case LDDFR_2BPP_MONO:
  109. lgray = 1;
  110. lbpp = 2;
  111. break;
  112. case LDDFR_4BPP_MONO:
  113. lgray = 1;
  114. case LDDFR_4BPP:
  115. lbpp = 4;
  116. break;
  117. case LDDFR_6BPP_MONO:
  118. lgray = 1;
  119. case LDDFR_8BPP:
  120. lbpp = 8;
  121. break;
  122. case LDDFR_16BPP_RGB555:
  123. case LDDFR_16BPP_RGB565:
  124. lbpp = 16;
  125. lgray = 0;
  126. break;
  127. default:
  128. dev_dbg(dev, "unsupported LDDFR bit depth.\n");
  129. return -EINVAL;
  130. }
  131. if (bpp)
  132. *bpp = lbpp;
  133. if (gray)
  134. *gray = lgray;
  135. return 0;
  136. }
  137. static int sh7760fb_check_var(struct fb_var_screeninfo *var,
  138. struct fb_info *info)
  139. {
  140. struct fb_fix_screeninfo *fix = &info->fix;
  141. struct sh7760fb_par *par = info->par;
  142. int ret, bpp;
  143. /* get color info from register value */
  144. ret = sh7760fb_get_color_info(info->dev, par->pd->lddfr, &bpp, NULL);
  145. if (ret)
  146. return ret;
  147. var->bits_per_pixel = bpp;
  148. if ((var->grayscale) && (var->bits_per_pixel == 1))
  149. fix->visual = FB_VISUAL_MONO10;
  150. else if (var->bits_per_pixel >= 15)
  151. fix->visual = FB_VISUAL_TRUECOLOR;
  152. else
  153. fix->visual = FB_VISUAL_PSEUDOCOLOR;
  154. /* TODO: add some more validation here */
  155. return 0;
  156. }
  157. /*
  158. * sh7760fb_set_par - set videomode.
  159. *
  160. * NOTE: The rotation, grayscale and DSTN codepaths are
  161. * totally untested!
  162. */
  163. static int sh7760fb_set_par(struct fb_info *info)
  164. {
  165. struct sh7760fb_par *par = info->par;
  166. struct fb_videomode *vm = par->pd->def_mode;
  167. unsigned long sbase, dstn_off, ldsarl, stride;
  168. unsigned short hsynp, hsynw, htcn, hdcn;
  169. unsigned short vsynp, vsynw, vtln, vdln;
  170. unsigned short lddfr, ldmtr;
  171. int ret, bpp, gray;
  172. par->rot = par->pd->rotate;
  173. /* rotate only works with xres <= 320 */
  174. if (par->rot && (vm->xres > 320)) {
  175. dev_dbg(info->dev, "rotation disabled due to display size\n");
  176. par->rot = 0;
  177. }
  178. /* calculate LCDC reg vals from display parameters */
  179. hsynp = vm->right_margin + vm->xres;
  180. hsynw = vm->hsync_len;
  181. htcn = vm->left_margin + hsynp + hsynw;
  182. hdcn = vm->xres;
  183. vsynp = vm->lower_margin + vm->yres;
  184. vsynw = vm->vsync_len;
  185. vtln = vm->upper_margin + vsynp + vsynw;
  186. vdln = vm->yres;
  187. /* get color info from register value */
  188. ret = sh7760fb_get_color_info(info->dev, par->pd->lddfr, &bpp, &gray);
  189. if (ret)
  190. return ret;
  191. dev_dbg(info->dev, "%dx%d %dbpp %s (orientation %s)\n", hdcn,
  192. vdln, bpp, gray ? "grayscale" : "color",
  193. par->rot ? "rotated" : "normal");
  194. #ifdef CONFIG_CPU_LITTLE_ENDIAN
  195. lddfr = par->pd->lddfr | (1 << 8);
  196. #else
  197. lddfr = par->pd->lddfr & ~(1 << 8);
  198. #endif
  199. ldmtr = par->pd->ldmtr;
  200. if (!(vm->sync & FB_SYNC_HOR_HIGH_ACT))
  201. ldmtr |= LDMTR_CL1POL;
  202. if (!(vm->sync & FB_SYNC_VERT_HIGH_ACT))
  203. ldmtr |= LDMTR_FLMPOL;
  204. /* shut down LCDC before changing display parameters */
  205. sh7760fb_blank(FB_BLANK_POWERDOWN, info);
  206. iowrite16(par->pd->ldickr, par->base + LDICKR); /* pixclock */
  207. iowrite16(ldmtr, par->base + LDMTR); /* polarities */
  208. iowrite16(lddfr, par->base + LDDFR); /* color/depth */
  209. iowrite16((par->rot ? 1 << 13 : 0), par->base + LDSMR); /* rotate */
  210. iowrite16(par->pd->ldpmmr, par->base + LDPMMR); /* Power Management */
  211. iowrite16(par->pd->ldpspr, par->base + LDPSPR); /* Power Supply Ctrl */
  212. /* display resolution */
  213. iowrite16(((htcn >> 3) - 1) | (((hdcn >> 3) - 1) << 8),
  214. par->base + LDHCNR);
  215. iowrite16(vdln - 1, par->base + LDVDLNR);
  216. iowrite16(vtln - 1, par->base + LDVTLNR);
  217. /* h/v sync signals */
  218. iowrite16((vsynp - 1) | ((vsynw - 1) << 12), par->base + LDVSYNR);
  219. iowrite16(((hsynp >> 3) - 1) | (((hsynw >> 3) - 1) << 12),
  220. par->base + LDHSYNR);
  221. /* AC modulation sig */
  222. iowrite16(par->pd->ldaclnr, par->base + LDACLNR);
  223. stride = (par->rot) ? vtln : hdcn;
  224. if (!gray)
  225. stride *= (bpp + 7) >> 3;
  226. else {
  227. if (bpp == 1)
  228. stride >>= 3;
  229. else if (bpp == 2)
  230. stride >>= 2;
  231. else if (bpp == 4)
  232. stride >>= 1;
  233. /* 6 bpp == 8 bpp */
  234. }
  235. /* if rotated, stride must be power of 2 */
  236. if (par->rot) {
  237. unsigned long bit = 1 << 31;
  238. while (bit) {
  239. if (stride & bit)
  240. break;
  241. bit >>= 1;
  242. }
  243. if (stride & ~bit)
  244. stride = bit << 1; /* not P-o-2, round up */
  245. }
  246. iowrite16(stride, par->base + LDLAOR);
  247. /* set display mem start address */
  248. sbase = (unsigned long)par->fbdma;
  249. if (par->rot)
  250. sbase += (hdcn - 1) * stride;
  251. iowrite32(sbase, par->base + LDSARU);
  252. /*
  253. * for DSTN need to set address for lower half.
  254. * I (mlau) don't know which address to set it to,
  255. * so I guessed at (stride * yres/2).
  256. */
  257. if (((ldmtr & 0x003f) >= LDMTR_DSTN_MONO_8) &&
  258. ((ldmtr & 0x003f) <= LDMTR_DSTN_COLOR_16)) {
  259. dev_dbg(info->dev, " ***** DSTN untested! *****\n");
  260. dstn_off = stride;
  261. if (par->rot)
  262. dstn_off *= hdcn >> 1;
  263. else
  264. dstn_off *= vdln >> 1;
  265. ldsarl = sbase + dstn_off;
  266. } else
  267. ldsarl = 0;
  268. iowrite32(ldsarl, par->base + LDSARL); /* mem for lower half of DSTN */
  269. info->fix.line_length = stride;
  270. sh7760fb_check_var(&info->var, info);
  271. sh7760fb_blank(FB_BLANK_UNBLANK, info); /* panel on! */
  272. dev_dbg(info->dev, "hdcn : %6d htcn : %6d\n", hdcn, htcn);
  273. dev_dbg(info->dev, "hsynw : %6d hsynp : %6d\n", hsynw, hsynp);
  274. dev_dbg(info->dev, "vdln : %6d vtln : %6d\n", vdln, vtln);
  275. dev_dbg(info->dev, "vsynw : %6d vsynp : %6d\n", vsynw, vsynp);
  276. dev_dbg(info->dev, "clksrc: %6d clkdiv: %6d\n",
  277. (par->pd->ldickr >> 12) & 3, par->pd->ldickr & 0x1f);
  278. dev_dbg(info->dev, "ldpmmr: 0x%04x ldpspr: 0x%04x\n", par->pd->ldpmmr,
  279. par->pd->ldpspr);
  280. dev_dbg(info->dev, "ldmtr : 0x%04x lddfr : 0x%04x\n", ldmtr, lddfr);
  281. dev_dbg(info->dev, "ldlaor: %ld\n", stride);
  282. dev_dbg(info->dev, "ldsaru: 0x%08lx ldsarl: 0x%08lx\n", sbase, ldsarl);
  283. return 0;
  284. }
  285. static struct fb_ops sh7760fb_ops = {
  286. .owner = THIS_MODULE,
  287. .fb_blank = sh7760fb_blank,
  288. .fb_check_var = sh7760fb_check_var,
  289. .fb_setcolreg = sh7760_setcolreg,
  290. .fb_set_par = sh7760fb_set_par,
  291. .fb_fillrect = cfb_fillrect,
  292. .fb_copyarea = cfb_copyarea,
  293. .fb_imageblit = cfb_imageblit,
  294. };
  295. static void sh7760fb_free_mem(struct fb_info *info)
  296. {
  297. struct sh7760fb_par *par = info->par;
  298. if (!info->screen_base)
  299. return;
  300. dma_free_coherent(info->dev, info->screen_size,
  301. info->screen_base, par->fbdma);
  302. par->fbdma = 0;
  303. info->screen_base = NULL;
  304. info->screen_size = 0;
  305. }
  306. /* allocate the framebuffer memory. This memory must be in Area3,
  307. * (dictated by the DMA engine) and contiguous, at a 512 byte boundary.
  308. */
  309. static int sh7760fb_alloc_mem(struct fb_info *info)
  310. {
  311. struct sh7760fb_par *par = info->par;
  312. void *fbmem;
  313. unsigned long vram;
  314. int ret, bpp;
  315. if (info->screen_base)
  316. return 0;
  317. /* get color info from register value */
  318. ret = sh7760fb_get_color_info(info->dev, par->pd->lddfr, &bpp, NULL);
  319. if (ret) {
  320. printk(KERN_ERR "colinfo\n");
  321. return ret;
  322. }
  323. /* min VRAM: xres_min = 16, yres_min = 1, bpp = 1: 2byte -> 1 page
  324. max VRAM: xres_max = 1024, yres_max = 1024, bpp = 16: 2MB */
  325. vram = info->var.xres * info->var.yres;
  326. if (info->var.grayscale) {
  327. if (bpp == 1)
  328. vram >>= 3;
  329. else if (bpp == 2)
  330. vram >>= 2;
  331. else if (bpp == 4)
  332. vram >>= 1;
  333. } else if (bpp > 8)
  334. vram *= 2;
  335. if ((vram < 1) || (vram > 1024 * 2048)) {
  336. dev_dbg(info->dev, "too much VRAM required. Check settings\n");
  337. return -ENODEV;
  338. }
  339. if (vram < PAGE_SIZE)
  340. vram = PAGE_SIZE;
  341. fbmem = dma_alloc_coherent(info->dev, vram, &par->fbdma, GFP_KERNEL);
  342. if (!fbmem)
  343. return -ENOMEM;
  344. if ((par->fbdma & SH7760FB_DMA_MASK) != SH7760FB_DMA_MASK) {
  345. sh7760fb_free_mem(info);
  346. dev_err(info->dev, "kernel gave me memory at 0x%08lx, which is"
  347. "unusable for the LCDC\n", (unsigned long)par->fbdma);
  348. return -ENOMEM;
  349. }
  350. info->screen_base = fbmem;
  351. info->screen_size = vram;
  352. info->fix.smem_start = (unsigned long)info->screen_base;
  353. info->fix.smem_len = info->screen_size;
  354. return 0;
  355. }
  356. static int sh7760fb_probe(struct platform_device *pdev)
  357. {
  358. struct fb_info *info;
  359. struct resource *res;
  360. struct sh7760fb_par *par;
  361. int ret;
  362. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  363. if (unlikely(res == NULL)) {
  364. dev_err(&pdev->dev, "invalid resource\n");
  365. return -EINVAL;
  366. }
  367. info = framebuffer_alloc(sizeof(struct sh7760fb_par), &pdev->dev);
  368. if (!info)
  369. return -ENOMEM;
  370. par = info->par;
  371. par->dev = pdev;
  372. par->pd = pdev->dev.platform_data;
  373. if (!par->pd) {
  374. dev_dbg(info->dev, "no display setup data!\n");
  375. ret = -ENODEV;
  376. goto out_fb;
  377. }
  378. par->ioarea = request_mem_region(res->start,
  379. resource_size(res), pdev->name);
  380. if (!par->ioarea) {
  381. dev_err(&pdev->dev, "mmio area busy\n");
  382. ret = -EBUSY;
  383. goto out_fb;
  384. }
  385. par->base = ioremap_nocache(res->start, resource_size(res));
  386. if (!par->base) {
  387. dev_err(&pdev->dev, "cannot remap\n");
  388. ret = -ENODEV;
  389. goto out_res;
  390. }
  391. iowrite16(0, par->base + LDINTR); /* disable vsync irq */
  392. par->irq = platform_get_irq(pdev, 0);
  393. if (par->irq >= 0) {
  394. ret = request_irq(par->irq, sh7760fb_irq, 0,
  395. "sh7760-lcdc", &par->vsync);
  396. if (ret) {
  397. dev_err(&pdev->dev, "cannot grab IRQ\n");
  398. par->irq = -ENXIO;
  399. } else
  400. disable_irq_nosync(par->irq);
  401. }
  402. fb_videomode_to_var(&info->var, par->pd->def_mode);
  403. ret = sh7760fb_alloc_mem(info);
  404. if (ret) {
  405. dev_dbg(info->dev, "framebuffer memory allocation failed!\n");
  406. goto out_unmap;
  407. }
  408. info->pseudo_palette = par->pseudo_palette;
  409. /* fixup color register bitpositions. These are fixed by hardware */
  410. info->var.red.offset = 11;
  411. info->var.red.length = 5;
  412. info->var.red.msb_right = 0;
  413. info->var.green.offset = 5;
  414. info->var.green.length = 6;
  415. info->var.green.msb_right = 0;
  416. info->var.blue.offset = 0;
  417. info->var.blue.length = 5;
  418. info->var.blue.msb_right = 0;
  419. info->var.transp.offset = 0;
  420. info->var.transp.length = 0;
  421. info->var.transp.msb_right = 0;
  422. strcpy(info->fix.id, "sh7760-lcdc");
  423. /* set the DON2 bit now, before cmap allocation, as it will randomize
  424. * palette memory.
  425. */
  426. iowrite16(LDCNTR_DON2, par->base + LDCNTR);
  427. info->fbops = &sh7760fb_ops;
  428. ret = fb_alloc_cmap(&info->cmap, 256, 0);
  429. if (ret) {
  430. dev_dbg(info->dev, "Unable to allocate cmap memory\n");
  431. goto out_mem;
  432. }
  433. ret = register_framebuffer(info);
  434. if (ret < 0) {
  435. dev_dbg(info->dev, "cannot register fb!\n");
  436. goto out_cmap;
  437. }
  438. platform_set_drvdata(pdev, info);
  439. printk(KERN_INFO "%s: memory at phys 0x%08lx-0x%08lx, size %ld KiB\n",
  440. pdev->name,
  441. (unsigned long)par->fbdma,
  442. (unsigned long)(par->fbdma + info->screen_size - 1),
  443. info->screen_size >> 10);
  444. return 0;
  445. out_cmap:
  446. sh7760fb_blank(FB_BLANK_POWERDOWN, info);
  447. fb_dealloc_cmap(&info->cmap);
  448. out_mem:
  449. sh7760fb_free_mem(info);
  450. out_unmap:
  451. if (par->irq >= 0)
  452. free_irq(par->irq, &par->vsync);
  453. iounmap(par->base);
  454. out_res:
  455. release_mem_region(res->start, resource_size(res));
  456. out_fb:
  457. framebuffer_release(info);
  458. return ret;
  459. }
  460. static int sh7760fb_remove(struct platform_device *dev)
  461. {
  462. struct fb_info *info = platform_get_drvdata(dev);
  463. struct sh7760fb_par *par = info->par;
  464. sh7760fb_blank(FB_BLANK_POWERDOWN, info);
  465. unregister_framebuffer(info);
  466. fb_dealloc_cmap(&info->cmap);
  467. sh7760fb_free_mem(info);
  468. if (par->irq >= 0)
  469. free_irq(par->irq, &par->vsync);
  470. iounmap(par->base);
  471. release_mem_region(par->ioarea->start, resource_size(par->ioarea));
  472. framebuffer_release(info);
  473. return 0;
  474. }
  475. static struct platform_driver sh7760_lcdc_driver = {
  476. .driver = {
  477. .name = "sh7760-lcdc",
  478. },
  479. .probe = sh7760fb_probe,
  480. .remove = sh7760fb_remove,
  481. };
  482. module_platform_driver(sh7760_lcdc_driver);
  483. MODULE_AUTHOR("Nobuhiro Iwamatsu, Manuel Lauss");
  484. MODULE_DESCRIPTION("FBdev for SH7760/63 integrated LCD Controller");
  485. MODULE_LICENSE("GPL");