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
;
155 iowrite32(col
, par
->base
+ LDPR(s
));
158 ((u32
*) (info
->pseudo_palette
))[s
] = s
;
167 iowrite16(0, par
->base
+ LDPALCR
);
171 static void encode_fix(struct fb_fix_screeninfo
*fix
, struct fb_info
*info
,
172 unsigned long stride
)
174 memset(fix
, 0, sizeof(struct fb_fix_screeninfo
));
175 strcpy(fix
->id
, "sh7760-lcdc");
177 fix
->smem_start
= (unsigned long)info
->screen_base
;
178 fix
->smem_len
= info
->screen_size
;
180 fix
->line_length
= stride
;
183 static int sh7760fb_get_color_info(struct device
*dev
,
184 u16 lddfr
, int *bpp
, int *gray
)
190 switch (lddfr
& LDDFR_COLOR_MASK
) {
191 case LDDFR_1BPP_MONO
:
195 case LDDFR_2BPP_MONO
:
199 case LDDFR_4BPP_MONO
:
204 case LDDFR_6BPP_MONO
:
209 case LDDFR_16BPP_RGB555
:
210 case LDDFR_16BPP_RGB565
:
215 dev_dbg(dev
, "unsupported LDDFR bit depth.\n");
227 static int sh7760fb_check_var(struct fb_var_screeninfo
*var
,
228 struct fb_info
*info
)
230 struct fb_fix_screeninfo
*fix
= &info
->fix
;
231 struct sh7760fb_par
*par
= info
->par
;
234 /* get color info from register value */
235 ret
= sh7760fb_get_color_info(info
->dev
, par
->pd
->lddfr
, &bpp
, NULL
);
239 var
->bits_per_pixel
= bpp
;
241 if ((var
->grayscale
) && (var
->bits_per_pixel
== 1))
242 fix
->visual
= FB_VISUAL_MONO10
;
243 else if (var
->bits_per_pixel
>= 15)
244 fix
->visual
= FB_VISUAL_TRUECOLOR
;
246 fix
->visual
= FB_VISUAL_PSEUDOCOLOR
;
248 /* TODO: add some more validation here */
253 * sh7760fb_set_par - set videomode.
255 * NOTE: The rotation, grayscale and DSTN codepaths are
258 static int sh7760fb_set_par(struct fb_info
*info
)
260 struct sh7760fb_par
*par
= info
->par
;
261 struct fb_videomode
*vm
= par
->pd
->def_mode
;
262 unsigned long sbase
, dstn_off
, ldsarl
, stride
;
263 unsigned short hsynp
, hsynw
, htcn
, hdcn
;
264 unsigned short vsynp
, vsynw
, vtln
, vdln
;
265 unsigned short lddfr
, ldmtr
;
268 par
->rot
= par
->pd
->rotate
;
270 /* rotate only works with xres <= 320 */
271 if (par
->rot
&& (vm
->xres
> 320)) {
272 dev_dbg(info
->dev
, "rotation disabled due to display size\n");
276 /* calculate LCDC reg vals from display parameters */
277 hsynp
= vm
->right_margin
+ vm
->xres
;
278 hsynw
= vm
->hsync_len
;
279 htcn
= vm
->left_margin
+ hsynp
+ hsynw
;
281 vsynp
= vm
->lower_margin
+ vm
->yres
;
282 vsynw
= vm
->vsync_len
;
283 vtln
= vm
->upper_margin
+ vsynp
+ vsynw
;
286 /* get color info from register value */
287 ret
= sh7760fb_get_color_info(info
->dev
, par
->pd
->lddfr
, &bpp
, &gray
);
291 dev_dbg(info
->dev
, "%dx%d %dbpp %s (orientation %s)\n", hdcn
,
292 vdln
, bpp
, gray
? "grayscale" : "color",
293 par
->rot
? "rotated" : "normal");
295 #ifdef CONFIG_CPU_LITTLE_ENDIAN
296 lddfr
= par
->pd
->lddfr
| (1 << 8);
298 lddfr
= par
->pd
->lddfr
& ~(1 << 8);
301 ldmtr
= par
->pd
->ldmtr
;
303 if (!(vm
->sync
& FB_SYNC_HOR_HIGH_ACT
))
304 ldmtr
|= LDMTR_CL1POL
;
305 if (!(vm
->sync
& FB_SYNC_VERT_HIGH_ACT
))
306 ldmtr
|= LDMTR_FLMPOL
;
308 /* shut down LCDC before changing display parameters */
309 sh7760fb_blank(FB_BLANK_POWERDOWN
, info
);
311 iowrite16(par
->pd
->ldickr
, par
->base
+ LDICKR
); /* pixclock */
312 iowrite16(ldmtr
, par
->base
+ LDMTR
); /* polarities */
313 iowrite16(lddfr
, par
->base
+ LDDFR
); /* color/depth */
314 iowrite16((par
->rot
? 1 << 13 : 0), par
->base
+ LDSMR
); /* rotate */
315 iowrite16(par
->pd
->ldpmmr
, par
->base
+ LDPMMR
); /* Power Management */
316 iowrite16(par
->pd
->ldpspr
, par
->base
+ LDPSPR
); /* Power Supply Ctrl */
318 /* display resolution */
319 iowrite16(((htcn
>> 3) - 1) | (((hdcn
>> 3) - 1) << 8),
321 iowrite16(vdln
- 1, par
->base
+ LDVDLNR
);
322 iowrite16(vtln
- 1, par
->base
+ LDVTLNR
);
323 /* h/v sync signals */
324 iowrite16((vsynp
- 1) | ((vsynw
- 1) << 12), par
->base
+ LDVSYNR
);
325 iowrite16(((hsynp
>> 3) - 1) | (((hsynw
>> 3) - 1) << 12),
326 par
->base
+ LDHSYNR
);
327 /* AC modulation sig */
328 iowrite16(par
->pd
->ldaclnr
, par
->base
+ LDACLNR
);
330 stride
= (par
->rot
) ? vtln
: hdcn
;
332 stride
*= (bpp
+ 7) >> 3;
343 /* if rotated, stride must be power of 2 */
345 unsigned long bit
= 1 << 31;
352 stride
= bit
<< 1; /* not P-o-2, round up */
354 iowrite16(stride
, par
->base
+ LDLAOR
);
356 /* set display mem start address */
357 sbase
= (unsigned long)par
->fbdma
;
359 sbase
+= (hdcn
- 1) * stride
;
361 iowrite32(sbase
, par
->base
+ LDSARU
);
364 * for DSTN need to set address for lower half.
365 * I (mlau) don't know which address to set it to,
366 * so I guessed at (stride * yres/2).
368 if (((ldmtr
& 0x003f) >= LDMTR_DSTN_MONO_8
) &&
369 ((ldmtr
& 0x003f) <= LDMTR_DSTN_COLOR_16
)) {
371 dev_dbg(info
->dev
, " ***** DSTN untested! *****\n");
375 dstn_off
*= hdcn
>> 1;
377 dstn_off
*= vdln
>> 1;
379 ldsarl
= sbase
+ dstn_off
;
383 iowrite32(ldsarl
, par
->base
+ LDSARL
); /* mem for lower half of DSTN */
385 encode_fix(&info
->fix
, info
, stride
);
386 sh7760fb_check_var(&info
->var
, info
);
388 sh7760fb_blank(FB_BLANK_UNBLANK
, info
); /* panel on! */
390 dev_dbg(info
->dev
, "hdcn : %6d htcn : %6d\n", hdcn
, htcn
);
391 dev_dbg(info
->dev
, "hsynw : %6d hsynp : %6d\n", hsynw
, hsynp
);
392 dev_dbg(info
->dev
, "vdln : %6d vtln : %6d\n", vdln
, vtln
);
393 dev_dbg(info
->dev
, "vsynw : %6d vsynp : %6d\n", vsynw
, vsynp
);
394 dev_dbg(info
->dev
, "clksrc: %6d clkdiv: %6d\n",
395 (par
->pd
->ldickr
>> 12) & 3, par
->pd
->ldickr
& 0x1f);
396 dev_dbg(info
->dev
, "ldpmmr: 0x%04x ldpspr: 0x%04x\n", par
->pd
->ldpmmr
,
398 dev_dbg(info
->dev
, "ldmtr : 0x%04x lddfr : 0x%04x\n", ldmtr
, lddfr
);
399 dev_dbg(info
->dev
, "ldlaor: %ld\n", stride
);
400 dev_dbg(info
->dev
, "ldsaru: 0x%08lx ldsarl: 0x%08lx\n", sbase
, ldsarl
);
405 static struct fb_ops sh7760fb_ops
= {
406 .owner
= THIS_MODULE
,
407 .fb_blank
= sh7760fb_blank
,
408 .fb_check_var
= sh7760fb_check_var
,
409 .fb_setcmap
= sh7760fb_setcmap
,
410 .fb_set_par
= sh7760fb_set_par
,
411 .fb_fillrect
= cfb_fillrect
,
412 .fb_copyarea
= cfb_copyarea
,
413 .fb_imageblit
= cfb_imageblit
,
416 static void sh7760fb_free_mem(struct fb_info
*info
)
418 struct sh7760fb_par
*par
= info
->par
;
420 if (!info
->screen_base
)
423 dma_free_coherent(info
->dev
, info
->screen_size
,
424 info
->screen_base
, par
->fbdma
);
427 info
->screen_base
= NULL
;
428 info
->screen_size
= 0;
431 /* allocate the framebuffer memory. This memory must be in Area3,
432 * (dictated by the DMA engine) and contiguous, at a 512 byte boundary.
434 static int sh7760fb_alloc_mem(struct fb_info
*info
)
436 struct sh7760fb_par
*par
= info
->par
;
441 if (info
->screen_base
)
444 /* get color info from register value */
445 ret
= sh7760fb_get_color_info(info
->dev
, par
->pd
->lddfr
, &bpp
, NULL
);
447 printk(KERN_ERR
"colinfo\n");
451 /* min VRAM: xres_min = 16, yres_min = 1, bpp = 1: 2byte -> 1 page
452 max VRAM: xres_max = 1024, yres_max = 1024, bpp = 16: 2MB */
454 vram
= info
->var
.xres
* info
->var
.yres
;
455 if (info
->var
.grayscale
) {
464 if ((vram
< 1) || (vram
> 1024 * 2048)) {
465 dev_dbg(info
->dev
, "too much VRAM required. Check settings\n");
469 if (vram
< PAGE_SIZE
)
472 fbmem
= dma_alloc_coherent(info
->dev
, vram
, &par
->fbdma
, GFP_KERNEL
);
477 if ((par
->fbdma
& SH7760FB_DMA_MASK
) != SH7760FB_DMA_MASK
) {
478 sh7760fb_free_mem(info
);
479 dev_err(info
->dev
, "kernel gave me memory at 0x%08lx, which is"
480 "unusable for the LCDC\n", (unsigned long)par
->fbdma
);
484 info
->screen_base
= fbmem
;
485 info
->screen_size
= vram
;
490 static int __devinit
sh7760fb_probe(struct platform_device
*pdev
)
492 struct fb_info
*info
;
493 struct resource
*res
;
494 struct sh7760fb_par
*par
;
497 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
498 if (unlikely(res
== NULL
)) {
499 dev_err(&pdev
->dev
, "invalid resource\n");
503 info
= framebuffer_alloc(sizeof(struct sh7760fb_par
), &pdev
->dev
);
510 par
->pd
= pdev
->dev
.platform_data
;
512 dev_dbg(info
->dev
, "no display setup data!\n");
517 par
->ioarea
= request_mem_region(res
->start
,
518 (res
->end
- res
->start
), pdev
->name
);
520 dev_err(&pdev
->dev
, "mmio area busy\n");
525 par
->base
= ioremap_nocache(res
->start
, res
->end
- res
->start
+ 1);
527 dev_err(&pdev
->dev
, "cannot remap\n");
532 iowrite16(0, par
->base
+ LDINTR
); /* disable vsync irq */
533 par
->irq
= platform_get_irq(pdev
, 0);
535 ret
= request_irq(par
->irq
, sh7760fb_irq
, 0,
536 "sh7760-lcdc", &par
->vsync
);
538 dev_err(&pdev
->dev
, "cannot grab IRQ\n");
541 disable_irq_nosync(par
->irq
);
544 fb_videomode_to_var(&info
->var
, par
->pd
->def_mode
);
546 ret
= sh7760fb_alloc_mem(info
);
548 dev_dbg(info
->dev
, "framebuffer memory allocation failed!\n");
552 info
->pseudo_palette
= par
->pseudo_palette
;
554 /* fixup color register bitpositions. These are fixed by hardware */
555 info
->var
.red
.offset
= 11;
556 info
->var
.red
.length
= 5;
557 info
->var
.red
.msb_right
= 0;
559 info
->var
.green
.offset
= 5;
560 info
->var
.green
.length
= 6;
561 info
->var
.green
.msb_right
= 0;
563 info
->var
.blue
.offset
= 0;
564 info
->var
.blue
.length
= 5;
565 info
->var
.blue
.msb_right
= 0;
567 info
->var
.transp
.offset
= 0;
568 info
->var
.transp
.length
= 0;
569 info
->var
.transp
.msb_right
= 0;
571 /* set the DON2 bit now, before cmap allocation, as it will randomize
574 iowrite16(LDCNTR_DON2
, par
->base
+ LDCNTR
);
575 info
->fbops
= &sh7760fb_ops
;
577 ret
= fb_alloc_cmap(&info
->cmap
, 256, 0);
579 dev_dbg(info
->dev
, "Unable to allocate cmap memory\n");
583 ret
= register_framebuffer(info
);
585 dev_dbg(info
->dev
, "cannot register fb!\n");
588 platform_set_drvdata(pdev
, info
);
590 printk(KERN_INFO
"%s: memory at phys 0x%08lx-0x%08lx, size %ld KiB\n",
592 (unsigned long)par
->fbdma
,
593 (unsigned long)(par
->fbdma
+ info
->screen_size
- 1),
594 info
->screen_size
>> 10);
599 sh7760fb_blank(FB_BLANK_POWERDOWN
, info
);
600 fb_dealloc_cmap(&info
->cmap
);
602 sh7760fb_free_mem(info
);
605 free_irq(par
->irq
, &par
->vsync
);
608 release_resource(par
->ioarea
);
611 framebuffer_release(info
);
615 static int __devexit
sh7760fb_remove(struct platform_device
*dev
)
617 struct fb_info
*info
= platform_get_drvdata(dev
);
618 struct sh7760fb_par
*par
= info
->par
;
620 sh7760fb_blank(FB_BLANK_POWERDOWN
, info
);
621 unregister_framebuffer(info
);
622 fb_dealloc_cmap(&info
->cmap
);
623 sh7760fb_free_mem(info
);
625 free_irq(par
->irq
, par
);
627 release_resource(par
->ioarea
);
629 framebuffer_release(info
);
630 platform_set_drvdata(dev
, NULL
);
635 static struct platform_driver sh7760_lcdc_driver
= {
637 .name
= "sh7760-lcdc",
638 .owner
= THIS_MODULE
,
640 .probe
= sh7760fb_probe
,
641 .remove
= __devexit_p(sh7760fb_remove
),
644 static int __init
sh7760fb_init(void)
646 return platform_driver_register(&sh7760_lcdc_driver
);
649 static void __exit
sh7760fb_exit(void)
651 platform_driver_unregister(&sh7760_lcdc_driver
);
654 module_init(sh7760fb_init
);
655 module_exit(sh7760fb_exit
);
657 MODULE_AUTHOR("Nobuhiro Iwamatsu, Manuel Lauss");
658 MODULE_DESCRIPTION("FBdev for SH7760/63 integrated LCD Controller");
659 MODULE_LICENSE("GPL");