2 * SH7760/SH7763 LCDC Framebuffer driver.
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>
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.
12 * PLEASE HAVE A LOOK AT Documentation/fb/sh7760fb.txt!
14 * Thanks to Siegfried Schaefer <s.schaefer at schaefer-edv.de>
15 * for his original source and testing!
18 #include <linux/completion.h>
19 #include <linux/delay.h>
20 #include <linux/dma-mapping.h>
22 #include <linux/interrupt.h>
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/platform_device.h>
28 #include <asm/sh7760fb.h>
34 struct sh7760fb_platdata *pd; /* display information */
36 dma_addr_t fbdma; /* physical address */
38 int rot; /* rotation enabled? */
40 u32 pseudo_palette[16];
42 struct platform_device *dev;
43 struct resource *ioarea;
44 struct completion vsync; /* vsync irq event */
47 static irqreturn_t sh7760fb_irq(int irq, void *data)
49 struct completion *c = data;
56 static void sh7760fb_wait_vsync(struct fb_info *info)
58 struct sh7760fb_par *par = info->par;
63 iowrite16(ioread16(par->base + LDINTR) & ~VINT_CHECK,
67 /* poll for vert. retrace: status bit is sticky */
68 while (!(ioread16(par->base + LDINTR) & VINT_CHECK))
71 /* a "wait_for_irq_event(par->irq)" would be extremely nice */
72 init_completion(&par->vsync);
74 wait_for_completion(&par->vsync);
75 disable_irq_nosync(par->irq);
79 /* wait_for_lps - wait until power supply has reached a certain state. */
80 static int wait_for_lps(struct sh7760fb_par *par, int val)
83 while (--i && ((ioread16(par->base + LDPMMR) & 3) != val))
92 /* en/disable the LCDC */
93 static int sh7760fb_blank(int blank, struct fb_info *info)
95 struct sh7760fb_par *par = info->par;
96 struct sh7760fb_platdata *pd = par->pd;
97 unsigned short cntr = ioread16(par->base + LDCNTR);
98 unsigned short intr = ioread16(par->base + LDINTR);
101 if (blank == FB_BLANK_UNBLANK) {
103 cntr = LDCNTR_DON2 | LDCNTR_DON;
114 iowrite16(intr, par->base + LDINTR);
115 iowrite16(cntr, par->base + LDCNTR);
117 return wait_for_lps(par, lps);
120 /* set color registers */
121 static int sh7760fb_setcmap(struct fb_cmap *cmap, struct fb_info *info)
123 struct sh7760fb_par *par = info->par;
127 u16 *g = cmap->green;
134 sh7760fb_wait_vsync(info);
136 /* request palette access */
137 iowrite16(LDPALCR_PALEN, par->base + LDPALCR);
139 /* poll for access grant */
141 while (!(ioread16(par->base + LDPALCR) & LDPALCR_PALS) && (--tmo))
146 dev_dbg(info->dev, "no palette access!\n");
150 while (l && (s < 256)) {
151 col = ((*r) & 0xff) << 16;
152 col |= ((*g) & 0xff) << 8;
153 col |= ((*b) & 0xff);
154 col &= SH7760FB_PALETTE_MASK;
157 ((u32 *) (info->pseudo_palette))[s] = s;
166 iowrite16(0, par->base + LDPALCR);
170 static void encode_fix(struct fb_fix_screeninfo *fix, struct fb_info *info,
171 unsigned long stride)
173 memset(fix, 0, sizeof(struct fb_fix_screeninfo));
174 strcpy(fix->id, "sh7760-lcdc");
176 fix->smem_start = (unsigned long)info->screen_base;
177 fix->smem_len = info->screen_size;
179 fix->line_length = stride;
182 static int sh7760fb_get_color_info(struct device *dev,
183 u16 lddfr, int *bpp, int *gray)
189 switch (lddfr & LDDFR_COLOR_MASK) {
190 case LDDFR_1BPP_MONO:
194 case LDDFR_2BPP_MONO:
198 case LDDFR_4BPP_MONO:
203 case LDDFR_6BPP_MONO:
208 case LDDFR_16BPP_RGB555:
209 case LDDFR_16BPP_RGB565:
214 dev_dbg(dev, "unsupported LDDFR bit depth.\n");
226 static int sh7760fb_check_var(struct fb_var_screeninfo *var,
227 struct fb_info *info)
229 struct fb_fix_screeninfo *fix = &info->fix;
230 struct sh7760fb_par *par = info->par;
233 /* get color info from register value */
234 ret = sh7760fb_get_color_info(info->dev, par->pd->lddfr, &bpp, NULL);
238 var->bits_per_pixel = bpp;
240 if ((var->grayscale) && (var->bits_per_pixel == 1))
241 fix->visual = FB_VISUAL_MONO10;
242 else if (var->bits_per_pixel >= 15)
243 fix->visual = FB_VISUAL_TRUECOLOR;
245 fix->visual = FB_VISUAL_PSEUDOCOLOR;
247 /* TODO: add some more validation here */
252 * sh7760fb_set_par - set videomode.
254 * NOTE: The rotation, grayscale and DSTN codepaths are
257 static int sh7760fb_set_par(struct fb_info *info)
259 struct sh7760fb_par *par = info->par;
260 struct fb_videomode *vm = par->pd->def_mode;
261 unsigned long sbase, dstn_off, ldsarl, stride;
262 unsigned short hsynp, hsynw, htcn, hdcn;
263 unsigned short vsynp, vsynw, vtln, vdln;
264 unsigned short lddfr, ldmtr;
267 par->rot = par->pd->rotate;
269 /* rotate only works with xres <= 320 */
270 if (par->rot && (vm->xres > 320)) {
271 dev_dbg(info->dev, "rotation disabled due to display size\n");
275 /* calculate LCDC reg vals from display parameters */
276 hsynp = vm->right_margin + vm->xres;
277 hsynw = vm->hsync_len;
278 htcn = vm->left_margin + hsynp + hsynw;
280 vsynp = vm->lower_margin + vm->yres;
281 vsynw = vm->vsync_len;
282 vtln = vm->upper_margin + vsynp + vsynw;
285 /* get color info from register value */
286 ret = sh7760fb_get_color_info(info->dev, par->pd->lddfr, &bpp, &gray);
290 dev_dbg(info->dev, "%dx%d %dbpp %s (orientation %s)\n", hdcn,
291 vdln, bpp, gray ? "grayscale" : "color",
292 par->rot ? "rotated" : "normal");
294 #ifdef CONFIG_CPU_LITTLE_ENDIAN
295 lddfr = par->pd->lddfr | (1 << 8);
297 lddfr = par->pd->lddfr & ~(1 << 8);
300 ldmtr = par->pd->ldmtr;
302 if (!(vm->sync & FB_SYNC_HOR_HIGH_ACT))
303 ldmtr |= LDMTR_CL1POL;
304 if (!(vm->sync & FB_SYNC_VERT_HIGH_ACT))
305 ldmtr |= LDMTR_FLMPOL;
307 /* shut down LCDC before changing display parameters */
308 sh7760fb_blank(FB_BLANK_POWERDOWN, info);
310 iowrite16(par->pd->ldickr, par->base + LDICKR); /* pixclock */
311 iowrite16(ldmtr, par->base + LDMTR); /* polarities */
312 iowrite16(lddfr, par->base + LDDFR); /* color/depth */
313 iowrite16((par->rot ? 1 << 13 : 0), par->base + LDSMR); /* rotate */
314 iowrite16(par->pd->ldpmmr, par->base + LDPMMR); /* Power Management */
315 iowrite16(par->pd->ldpspr, par->base + LDPSPR); /* Power Supply Ctrl */
317 /* display resolution */
318 iowrite16(((htcn >> 3) - 1) | (((hdcn >> 3) - 1) << 8),
320 iowrite16(vdln - 1, par->base + LDVDLNR);
321 iowrite16(vtln - 1, par->base + LDVTLNR);
322 /* h/v sync signals */
323 iowrite16((vsynp - 1) | ((vsynw - 1) << 12), par->base + LDVSYNR);
324 iowrite16(((hsynp >> 3) - 1) | (((hsynw >> 3) - 1) << 12),
325 par->base + LDHSYNR);
326 /* AC modulation sig */
327 iowrite16(par->pd->ldaclnr, par->base + LDACLNR);
329 stride = (par->rot) ? vtln : hdcn;
331 stride *= (bpp + 7) >> 3;
342 /* if rotated, stride must be power of 2 */
344 unsigned long bit = 1 << 31;
351 stride = bit << 1; /* not P-o-2, round up */
353 iowrite16(stride, par->base + LDLAOR);
355 /* set display mem start address */
356 sbase = (unsigned long)par->fbdma;
358 sbase += (hdcn - 1) * stride;
360 iowrite32(sbase, par->base + LDSARU);
363 * for DSTN need to set address for lower half.
364 * I (mlau) don't know which address to set it to,
365 * so I guessed at (stride * yres/2).
367 if (((ldmtr & 0x003f) >= LDMTR_DSTN_MONO_8) &&
368 ((ldmtr & 0x003f) <= LDMTR_DSTN_COLOR_16)) {
370 dev_dbg(info->dev, " ***** DSTN untested! *****\n");
374 dstn_off *= hdcn >> 1;
376 dstn_off *= vdln >> 1;
378 ldsarl = sbase + dstn_off;
382 iowrite32(ldsarl, par->base + LDSARL); /* mem for lower half of DSTN */
384 encode_fix(&info->fix, info, stride);
385 sh7760fb_check_var(&info->var, info);
387 sh7760fb_blank(FB_BLANK_UNBLANK, info); /* panel on! */
389 dev_dbg(info->dev, "hdcn : %6d htcn : %6d\n", hdcn, htcn);
390 dev_dbg(info->dev, "hsynw : %6d hsynp : %6d\n", hsynw, hsynp);
391 dev_dbg(info->dev, "vdln : %6d vtln : %6d\n", vdln, vtln);
392 dev_dbg(info->dev, "vsynw : %6d vsynp : %6d\n", vsynw, vsynp);
393 dev_dbg(info->dev, "clksrc: %6d clkdiv: %6d\n",
394 (par->pd->ldickr >> 12) & 3, par->pd->ldickr & 0x1f);
395 dev_dbg(info->dev, "ldpmmr: 0x%04x ldpspr: 0x%04x\n", par->pd->ldpmmr,
397 dev_dbg(info->dev, "ldmtr : 0x%04x lddfr : 0x%04x\n", ldmtr, lddfr);
398 dev_dbg(info->dev, "ldlaor: %ld\n", stride);
399 dev_dbg(info->dev, "ldsaru: 0x%08lx ldsarl: 0x%08lx\n", sbase, ldsarl);
404 static struct fb_ops sh7760fb_ops = {
405 .owner = THIS_MODULE,
406 .fb_blank = sh7760fb_blank,
407 .fb_check_var = sh7760fb_check_var,
408 .fb_setcmap = sh7760fb_setcmap,
409 .fb_set_par = sh7760fb_set_par,
410 .fb_fillrect = cfb_fillrect,
411 .fb_copyarea = cfb_copyarea,
412 .fb_imageblit = cfb_imageblit,
415 static void sh7760fb_free_mem(struct fb_info *info)
417 struct sh7760fb_par *par = info->par;
419 if (!info->screen_base)
422 dma_free_coherent(info->dev, info->screen_size,
423 info->screen_base, par->fbdma);
426 info->screen_base = NULL;
427 info->screen_size = 0;
430 /* allocate the framebuffer memory. This memory must be in Area3,
431 * (dictated by the DMA engine) and contiguous, at a 512 byte boundary.
433 static int sh7760fb_alloc_mem(struct fb_info *info)
435 struct sh7760fb_par *par = info->par;
440 if (info->screen_base)
443 /* get color info from register value */
444 ret = sh7760fb_get_color_info(info->dev, par->pd->lddfr, &bpp, NULL);
446 printk(KERN_ERR "colinfo\n");
450 /* min VRAM: xres_min = 16, yres_min = 1, bpp = 1: 2byte -> 1 page
451 max VRAM: xres_max = 1024, yres_max = 1024, bpp = 16: 2MB */
453 vram = info->var.xres * info->var.yres;
454 if (info->var.grayscale) {
463 if ((vram < 1) || (vram > 1024 * 2048)) {
464 dev_dbg(info->dev, "too much VRAM required. Check settings\n");
468 if (vram < PAGE_SIZE)
471 fbmem = dma_alloc_coherent(info->dev, vram, &par->fbdma, GFP_KERNEL);
476 if ((par->fbdma & SH7760FB_DMA_MASK) != SH7760FB_DMA_MASK) {
477 sh7760fb_free_mem(info);
478 dev_err(info->dev, "kernel gave me memory at 0x%08lx, which is"
479 "unusable for the LCDC\n", (unsigned long)par->fbdma);
483 info->screen_base = fbmem;
484 info->screen_size = vram;
489 static int __devinit sh7760fb_probe(struct platform_device *pdev)
491 struct fb_info *info;
492 struct resource *res;
493 struct sh7760fb_par *par;
496 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
497 if (unlikely(res == NULL)) {
498 dev_err(&pdev->dev, "invalid resource\n");
502 info = framebuffer_alloc(sizeof(struct sh7760fb_par), &pdev->dev);
509 par->pd = pdev->dev.platform_data;
511 dev_dbg(info->dev, "no display setup data!\n");
516 par->ioarea = request_mem_region(res->start,
517 (res->end - res->start), pdev->name);
519 dev_err(&pdev->dev, "mmio area busy\n");
524 par->base = ioremap_nocache(res->start, res->end - res->start + 1);
526 dev_err(&pdev->dev, "cannot remap\n");
531 iowrite16(0, par->base + LDINTR); /* disable vsync irq */
532 par->irq = platform_get_irq(pdev, 0);
534 ret = request_irq(par->irq, sh7760fb_irq, 0,
535 "sh7760-lcdc", &par->vsync);
537 dev_err(&pdev->dev, "cannot grab IRQ\n");
540 disable_irq_nosync(par->irq);
543 fb_videomode_to_var(&info->var, par->pd->def_mode);
545 ret = sh7760fb_alloc_mem(info);
547 dev_dbg(info->dev, "framebuffer memory allocation failed!\n");
551 info->pseudo_palette = par->pseudo_palette;
553 /* fixup color register bitpositions. These are fixed by hardware */
554 info->var.red.offset = 11;
555 info->var.red.length = 5;
556 info->var.red.msb_right = 0;
558 info->var.green.offset = 5;
559 info->var.green.length = 6;
560 info->var.green.msb_right = 0;
562 info->var.blue.offset = 0;
563 info->var.blue.length = 5;
564 info->var.blue.msb_right = 0;
566 info->var.transp.offset = 0;
567 info->var.transp.length = 0;
568 info->var.transp.msb_right = 0;
570 /* set the DON2 bit now, before cmap allocation, as it will randomize
573 iowrite16(LDCNTR_DON2, par->base + LDCNTR);
574 info->fbops = &sh7760fb_ops;
576 ret = fb_alloc_cmap(&info->cmap, 256, 0);
578 dev_dbg(info->dev, "Unable to allocate cmap memory\n");
582 ret = register_framebuffer(info);
584 dev_dbg(info->dev, "cannot register fb!\n");
587 platform_set_drvdata(pdev, info);
589 printk(KERN_INFO "%s: memory at phys 0x%08lx-0x%08lx, size %ld KiB\n",
591 (unsigned long)par->fbdma,
592 (unsigned long)(par->fbdma + info->screen_size - 1),
593 info->screen_size >> 10);
598 sh7760fb_blank(FB_BLANK_POWERDOWN, info);
599 fb_dealloc_cmap(&info->cmap);
601 sh7760fb_free_mem(info);
604 free_irq(par->irq, &par->vsync);
607 release_resource(par->ioarea);
610 framebuffer_release(info);
614 static int __devexit sh7760fb_remove(struct platform_device *dev)
616 struct fb_info *info = platform_get_drvdata(dev);
617 struct sh7760fb_par *par = info->par;
619 sh7760fb_blank(FB_BLANK_POWERDOWN, info);
620 unregister_framebuffer(info);
621 fb_dealloc_cmap(&info->cmap);
622 sh7760fb_free_mem(info);
624 free_irq(par->irq, par);
626 release_resource(par->ioarea);
628 framebuffer_release(info);
629 platform_set_drvdata(dev, NULL);
634 static struct platform_driver sh7760_lcdc_driver = {
636 .name = "sh7760-lcdc",
637 .owner = THIS_MODULE,
639 .probe = sh7760fb_probe,
640 .remove = __devexit_p(sh7760fb_remove),
643 static int __init sh7760fb_init(void)
645 return platform_driver_register(&sh7760_lcdc_driver);
648 static void __exit sh7760fb_exit(void)
650 platform_driver_unregister(&sh7760_lcdc_driver);
653 module_init(sh7760fb_init);
654 module_exit(sh7760fb_exit);
656 MODULE_AUTHOR("Nobuhiro Iwamatsu, Manuel Lauss");
657 MODULE_DESCRIPTION("FBdev for SH7760/63 integrated LCD Controller");
658 MODULE_LICENSE("GPL");