Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/roland...
[pandora-kernel.git] / drivers / video / sh7760fb.c
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
18 #include <linux/completion.h>
19 #include <linux/delay.h>
20 #include <linux/dma-mapping.h>
21 #include <linux/fb.h>
22 #include <linux/interrupt.h>
23 #include <linux/io.h>
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/platform_device.h>
27
28 #include <asm/sh7760fb.h>
29
30 struct sh7760fb_par {
31         void __iomem *base;
32         int irq;
33
34         struct sh7760fb_platdata *pd;   /* display information */
35
36         dma_addr_t fbdma;       /* physical address */
37
38         int rot;                /* rotation enabled? */
39
40         u32 pseudo_palette[16];
41
42         struct platform_device *dev;
43         struct resource *ioarea;
44         struct completion vsync;        /* vsync irq event */
45 };
46
47 static irqreturn_t sh7760fb_irq(int irq, void *data)
48 {
49         struct completion *c = data;
50
51         complete(c);
52
53         return IRQ_HANDLED;
54 }
55
56 static void sh7760fb_wait_vsync(struct fb_info *info)
57 {
58         struct sh7760fb_par *par = info->par;
59
60         if (par->pd->novsync)
61                 return;
62
63         iowrite16(ioread16(par->base + LDINTR) & ~VINT_CHECK,
64                   par->base + LDINTR);
65
66         if (par->irq < 0) {
67                 /* poll for vert. retrace: status bit is sticky */
68                 while (!(ioread16(par->base + LDINTR) & VINT_CHECK))
69                         cpu_relax();
70         } else {
71                 /* a "wait_for_irq_event(par->irq)" would be extremely nice */
72                 init_completion(&par->vsync);
73                 enable_irq(par->irq);
74                 wait_for_completion(&par->vsync);
75                 disable_irq_nosync(par->irq);
76         }
77 }
78
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)
81 {
82         int i = 100;
83         while (--i && ((ioread16(par->base + LDPMMR) & 3) != val))
84                 msleep(1);
85
86         if (i <= 0)
87                 return -ETIMEDOUT;
88
89         return 0;
90 }
91
92 /* en/disable the LCDC */
93 static int sh7760fb_blank(int blank, struct fb_info *info)
94 {
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);
99         int lps;
100
101         if (blank == FB_BLANK_UNBLANK) {
102                 intr |= VINT_START;
103                 cntr = LDCNTR_DON2 | LDCNTR_DON;
104                 lps = 3;
105         } else {
106                 intr &= ~VINT_START;
107                 cntr = LDCNTR_DON2;
108                 lps = 0;
109         }
110
111         if (pd->blank)
112                 pd->blank(blank);
113
114         iowrite16(intr, par->base + LDINTR);
115         iowrite16(cntr, par->base + LDCNTR);
116
117         return wait_for_lps(par, lps);
118 }
119
120 /* set color registers */
121 static int sh7760fb_setcmap(struct fb_cmap *cmap, struct fb_info *info)
122 {
123         struct sh7760fb_par *par = info->par;
124         u32 s = cmap->start;
125         u32 l = cmap->len;
126         u16 *r = cmap->red;
127         u16 *g = cmap->green;
128         u16 *b = cmap->blue;
129         u32 col, tmo;
130         int ret;
131
132         ret = 0;
133
134         sh7760fb_wait_vsync(info);
135
136         /* request palette access */
137         iowrite16(LDPALCR_PALEN, par->base + LDPALCR);
138
139         /* poll for access grant */
140         tmo = 100;
141         while (!(ioread16(par->base + LDPALCR) & LDPALCR_PALS) && (--tmo))
142                 cpu_relax();
143
144         if (!tmo) {
145                 ret = 1;
146                 dev_dbg(info->dev, "no palette access!\n");
147                 goto out;
148         }
149
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
156                 if (s < 16)
157                         ((u32 *) (info->pseudo_palette))[s] = s;
158
159                 s++;
160                 l--;
161                 r++;
162                 g++;
163                 b++;
164         }
165 out:
166         iowrite16(0, par->base + LDPALCR);
167         return ret;
168 }
169
170 static void encode_fix(struct fb_fix_screeninfo *fix, struct fb_info *info,
171                        unsigned long stride)
172 {
173         memset(fix, 0, sizeof(struct fb_fix_screeninfo));
174         strcpy(fix->id, "sh7760-lcdc");
175
176         fix->smem_start = (unsigned long)info->screen_base;
177         fix->smem_len = info->screen_size;
178
179         fix->line_length = stride;
180 }
181
182 static int sh7760fb_get_color_info(struct device *dev,
183                                    u16 lddfr, int *bpp, int *gray)
184 {
185         int lbpp, lgray;
186
187         lgray = lbpp = 0;
188
189         switch (lddfr & LDDFR_COLOR_MASK) {
190         case LDDFR_1BPP_MONO:
191                 lgray = 1;
192                 lbpp = 1;
193                 break;
194         case LDDFR_2BPP_MONO:
195                 lgray = 1;
196                 lbpp = 2;
197                 break;
198         case LDDFR_4BPP_MONO:
199                 lgray = 1;
200         case LDDFR_4BPP:
201                 lbpp = 4;
202                 break;
203         case LDDFR_6BPP_MONO:
204                 lgray = 1;
205         case LDDFR_8BPP:
206                 lbpp = 8;
207                 break;
208         case LDDFR_16BPP_RGB555:
209         case LDDFR_16BPP_RGB565:
210                 lbpp = 16;
211                 lgray = 0;
212                 break;
213         default:
214                 dev_dbg(dev, "unsupported LDDFR bit depth.\n");
215                 return -EINVAL;
216         }
217
218         if (bpp)
219                 *bpp = lbpp;
220         if (gray)
221                 *gray = lgray;
222
223         return 0;
224 }
225
226 static int sh7760fb_check_var(struct fb_var_screeninfo *var,
227                               struct fb_info *info)
228 {
229         struct fb_fix_screeninfo *fix = &info->fix;
230         struct sh7760fb_par *par = info->par;
231         int ret, bpp;
232
233         /* get color info from register value */
234         ret = sh7760fb_get_color_info(info->dev, par->pd->lddfr, &bpp, NULL);
235         if (ret)
236                 return ret;
237
238         var->bits_per_pixel = bpp;
239
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;
244         else
245                 fix->visual = FB_VISUAL_PSEUDOCOLOR;
246
247         /* TODO: add some more validation here */
248         return 0;
249 }
250
251 /*
252  * sh7760fb_set_par - set videomode.
253  *
254  * NOTE: The rotation, grayscale and DSTN codepaths are
255  *     totally untested!
256  */
257 static int sh7760fb_set_par(struct fb_info *info)
258 {
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;
265         int ret, bpp, gray;
266
267         par->rot = par->pd->rotate;
268
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");
272                 par->rot = 0;
273         }
274
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;
279         hdcn = vm->xres;
280         vsynp = vm->lower_margin + vm->yres;
281         vsynw = vm->vsync_len;
282         vtln = vm->upper_margin + vsynp + vsynw;
283         vdln = vm->yres;
284
285         /* get color info from register value */
286         ret = sh7760fb_get_color_info(info->dev, par->pd->lddfr, &bpp, &gray);
287         if (ret)
288                 return ret;
289
290         dev_dbg(info->dev, "%dx%d %dbpp %s (orientation %s)\n", hdcn,
291                 vdln, bpp, gray ? "grayscale" : "color",
292                 par->rot ? "rotated" : "normal");
293
294 #ifdef CONFIG_CPU_LITTLE_ENDIAN
295         lddfr = par->pd->lddfr | (1 << 8);
296 #else
297         lddfr = par->pd->lddfr & ~(1 << 8);
298 #endif
299
300         ldmtr = par->pd->ldmtr;
301
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;
306
307         /* shut down LCDC before changing display parameters */
308         sh7760fb_blank(FB_BLANK_POWERDOWN, info);
309
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 */
316
317         /* display resolution */
318         iowrite16(((htcn >> 3) - 1) | (((hdcn >> 3) - 1) << 8),
319                   par->base + LDHCNR);
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);
328
329         stride = (par->rot) ? vtln : hdcn;
330         if (!gray)
331                 stride *= (bpp + 7) >> 3;
332         else {
333                 if (bpp == 1)
334                         stride >>= 3;
335                 else if (bpp == 2)
336                         stride >>= 2;
337                 else if (bpp == 4)
338                         stride >>= 1;
339                 /* 6 bpp == 8 bpp */
340         }
341
342         /* if rotated, stride must be power of 2 */
343         if (par->rot) {
344                 unsigned long bit = 1 << 31;
345                 while (bit) {
346                         if (stride & bit)
347                                 break;
348                         bit >>= 1;
349                 }
350                 if (stride & ~bit)
351                         stride = bit << 1;      /* not P-o-2, round up */
352         }
353         iowrite16(stride, par->base + LDLAOR);
354
355         /* set display mem start address */
356         sbase = (unsigned long)par->fbdma;
357         if (par->rot)
358                 sbase += (hdcn - 1) * stride;
359
360         iowrite32(sbase, par->base + LDSARU);
361
362         /*
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).
366          */
367         if (((ldmtr & 0x003f) >= LDMTR_DSTN_MONO_8) &&
368             ((ldmtr & 0x003f) <= LDMTR_DSTN_COLOR_16)) {
369
370                 dev_dbg(info->dev, " ***** DSTN untested! *****\n");
371
372                 dstn_off = stride;
373                 if (par->rot)
374                         dstn_off *= hdcn >> 1;
375                 else
376                         dstn_off *= vdln >> 1;
377
378                 ldsarl = sbase + dstn_off;
379         } else
380                 ldsarl = 0;
381
382         iowrite32(ldsarl, par->base + LDSARL);  /* mem for lower half of DSTN */
383
384         encode_fix(&info->fix, info, stride);
385         sh7760fb_check_var(&info->var, info);
386
387         sh7760fb_blank(FB_BLANK_UNBLANK, info); /* panel on! */
388
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,
396                 par->pd->ldpspr);
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);
400
401         return 0;
402 }
403
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,
413 };
414
415 static void sh7760fb_free_mem(struct fb_info *info)
416 {
417         struct sh7760fb_par *par = info->par;
418
419         if (!info->screen_base)
420                 return;
421
422         dma_free_coherent(info->dev, info->screen_size,
423                           info->screen_base, par->fbdma);
424
425         par->fbdma = 0;
426         info->screen_base = NULL;
427         info->screen_size = 0;
428 }
429
430 /* allocate the framebuffer memory. This memory must be in Area3,
431  * (dictated by the DMA engine) and contiguous, at a 512 byte boundary.
432  */
433 static int sh7760fb_alloc_mem(struct fb_info *info)
434 {
435         struct sh7760fb_par *par = info->par;
436         void *fbmem;
437         unsigned long vram;
438         int ret, bpp;
439
440         if (info->screen_base)
441                 return 0;
442
443         /* get color info from register value */
444         ret = sh7760fb_get_color_info(info->dev, par->pd->lddfr, &bpp, NULL);
445         if (ret) {
446                 printk(KERN_ERR "colinfo\n");
447                 return ret;
448         }
449
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 */
452
453         vram = info->var.xres * info->var.yres;
454         if (info->var.grayscale) {
455                 if (bpp == 1)
456                         vram >>= 3;
457                 else if (bpp == 2)
458                         vram >>= 2;
459                 else if (bpp == 4)
460                         vram >>= 1;
461         } else if (bpp > 8)
462                 vram *= 2;
463         if ((vram < 1) || (vram > 1024 * 2048)) {
464                 dev_dbg(info->dev, "too much VRAM required. Check settings\n");
465                 return -ENODEV;
466         }
467
468         if (vram < PAGE_SIZE)
469                 vram = PAGE_SIZE;
470
471         fbmem = dma_alloc_coherent(info->dev, vram, &par->fbdma, GFP_KERNEL);
472
473         if (!fbmem)
474                 return -ENOMEM;
475
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);
480                 return -ENOMEM;
481         }
482
483         info->screen_base = fbmem;
484         info->screen_size = vram;
485
486         return 0;
487 }
488
489 static int __devinit sh7760fb_probe(struct platform_device *pdev)
490 {
491         struct fb_info *info;
492         struct resource *res;
493         struct sh7760fb_par *par;
494         int ret;
495
496         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
497         if (unlikely(res == NULL)) {
498                 dev_err(&pdev->dev, "invalid resource\n");
499                 return -EINVAL;
500         }
501
502         info = framebuffer_alloc(sizeof(struct sh7760fb_par), &pdev->dev);
503         if (!info)
504                 return -ENOMEM;
505
506         par = info->par;
507         par->dev = pdev;
508
509         par->pd = pdev->dev.platform_data;
510         if (!par->pd) {
511                 dev_dbg(info->dev, "no display setup data!\n");
512                 ret = -ENODEV;
513                 goto out_fb;
514         }
515
516         par->ioarea = request_mem_region(res->start,
517                                          (res->end - res->start), pdev->name);
518         if (!par->ioarea) {
519                 dev_err(&pdev->dev, "mmio area busy\n");
520                 ret = -EBUSY;
521                 goto out_fb;
522         }
523
524         par->base = ioremap_nocache(res->start, res->end - res->start + 1);
525         if (!par->base) {
526                 dev_err(&pdev->dev, "cannot remap\n");
527                 ret = -ENODEV;
528                 goto out_res;
529         }
530
531         iowrite16(0, par->base + LDINTR);       /* disable vsync irq */
532         par->irq = platform_get_irq(pdev, 0);
533         if (par->irq >= 0) {
534                 ret = request_irq(par->irq, sh7760fb_irq, 0,
535                                   "sh7760-lcdc", &par->vsync);
536                 if (ret) {
537                         dev_err(&pdev->dev, "cannot grab IRQ\n");
538                         par->irq = -ENXIO;
539                 } else
540                         disable_irq_nosync(par->irq);
541         }
542
543         fb_videomode_to_var(&info->var, par->pd->def_mode);
544
545         ret = sh7760fb_alloc_mem(info);
546         if (ret) {
547                 dev_dbg(info->dev, "framebuffer memory allocation failed!\n");
548                 goto out_unmap;
549         }
550
551         info->pseudo_palette = par->pseudo_palette;
552
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;
557
558         info->var.green.offset = 5;
559         info->var.green.length = 6;
560         info->var.green.msb_right = 0;
561
562         info->var.blue.offset = 0;
563         info->var.blue.length = 5;
564         info->var.blue.msb_right = 0;
565
566         info->var.transp.offset = 0;
567         info->var.transp.length = 0;
568         info->var.transp.msb_right = 0;
569
570         /* set the DON2 bit now, before cmap allocation, as it will randomize
571          * palette memory.
572          */
573         iowrite16(LDCNTR_DON2, par->base + LDCNTR);
574         info->fbops = &sh7760fb_ops;
575
576         ret = fb_alloc_cmap(&info->cmap, 256, 0);
577         if (ret) {
578                 dev_dbg(info->dev, "Unable to allocate cmap memory\n");
579                 goto out_mem;
580         }
581
582         ret = register_framebuffer(info);
583         if (ret < 0) {
584                 dev_dbg(info->dev, "cannot register fb!\n");
585                 goto out_cmap;
586         }
587         platform_set_drvdata(pdev, info);
588
589         printk(KERN_INFO "%s: memory at phys 0x%08lx-0x%08lx, size %ld KiB\n",
590                pdev->name,
591                (unsigned long)par->fbdma,
592                (unsigned long)(par->fbdma + info->screen_size - 1),
593                info->screen_size >> 10);
594
595         return 0;
596
597 out_cmap:
598         sh7760fb_blank(FB_BLANK_POWERDOWN, info);
599         fb_dealloc_cmap(&info->cmap);
600 out_mem:
601         sh7760fb_free_mem(info);
602 out_unmap:
603         if (par->irq >= 0)
604                 free_irq(par->irq, &par->vsync);
605         iounmap(par->base);
606 out_res:
607         release_resource(par->ioarea);
608         kfree(par->ioarea);
609 out_fb:
610         framebuffer_release(info);
611         return ret;
612 }
613
614 static int __devexit sh7760fb_remove(struct platform_device *dev)
615 {
616         struct fb_info *info = platform_get_drvdata(dev);
617         struct sh7760fb_par *par = info->par;
618
619         sh7760fb_blank(FB_BLANK_POWERDOWN, info);
620         unregister_framebuffer(info);
621         fb_dealloc_cmap(&info->cmap);
622         sh7760fb_free_mem(info);
623         if (par->irq >= 0)
624                 free_irq(par->irq, par);
625         iounmap(par->base);
626         release_resource(par->ioarea);
627         kfree(par->ioarea);
628         framebuffer_release(info);
629         platform_set_drvdata(dev, NULL);
630
631         return 0;
632 }
633
634 static struct platform_driver sh7760_lcdc_driver = {
635         .driver = {
636                    .name = "sh7760-lcdc",
637                    .owner = THIS_MODULE,
638                    },
639         .probe = sh7760fb_probe,
640         .remove = __devexit_p(sh7760fb_remove),
641 };
642
643 static int __init sh7760fb_init(void)
644 {
645         return platform_driver_register(&sh7760_lcdc_driver);
646 }
647
648 static void __exit sh7760fb_exit(void)
649 {
650         platform_driver_unregister(&sh7760_lcdc_driver);
651 }
652
653 module_init(sh7760fb_init);
654 module_exit(sh7760fb_exit);
655
656 MODULE_AUTHOR("Nobuhiro Iwamatsu, Manuel Lauss");
657 MODULE_DESCRIPTION("FBdev for SH7760/63 integrated LCD Controller");
658 MODULE_LICENSE("GPL");