Merge branch 'master' of /home/sam/kernel/linux-2.6/
[linux-2.6] / drivers / video / s3c2410fb.c
1 /*
2  * linux/drivers/video/s3c2410fb.c
3  *      Copyright (c) Arnaud Patard, Ben Dooks
4  *
5  * This file is subject to the terms and conditions of the GNU General Public
6  * License.  See the file COPYING in the main directory of this archive for
7  * more details.
8  *
9  *          S3C2410 LCD Controller Frame Buffer Driver
10  *          based on skeletonfb.c, sa1100fb.c and others
11  *
12  * ChangeLog
13  * 2005-04-07: Arnaud Patard <arnaud.patard@rtp-net.org>
14  *      - u32 state -> pm_message_t state
15  *      - S3C2410_{VA,SZ}_LCD -> S3C24XX
16  *
17  * 2005-03-15: Arnaud Patard <arnaud.patard@rtp-net.org>
18  *      - Removed the ioctl
19  *      - use readl/writel instead of __raw_writel/__raw_readl
20  *
21  * 2004-12-04: Arnaud Patard <arnaud.patard@rtp-net.org>
22  *      - Added the possibility to set on or off the
23  *      debugging mesaages
24  *      - Replaced 0 and 1 by on or off when reading the
25  *      /sys files
26  *
27  * 2005-03-23: Ben Dooks <ben-linux@fluff.org>
28  *      - added non 16bpp modes
29  *      - updated platform information for range of x/y/bpp
30  *      - add code to ensure palette is written correctly
31  *      - add pixel clock divisor control
32  *
33  * 2004-11-11: Arnaud Patard <arnaud.patard@rtp-net.org>
34  *      - Removed the use of currcon as it no more exist
35  *      - Added LCD power sysfs interface
36  *
37  * 2004-11-03: Ben Dooks <ben-linux@fluff.org>
38  *      - minor cleanups
39  *      - add suspend/resume support
40  *      - s3c2410fb_setcolreg() not valid in >8bpp modes
41  *      - removed last CONFIG_FB_S3C2410_FIXED
42  *      - ensure lcd controller stopped before cleanup
43  *      - added sysfs interface for backlight power
44  *      - added mask for gpio configuration
45  *      - ensured IRQs disabled during GPIO configuration
46  *      - disable TPAL before enabling video
47  *
48  * 2004-09-20: Arnaud Patard <arnaud.patard@rtp-net.org>
49  *      - Suppress command line options
50  *
51  * 2004-09-15: Arnaud Patard <arnaud.patard@rtp-net.org>
52  *      - code cleanup
53  *
54  * 2004-09-07: Arnaud Patard <arnaud.patard@rtp-net.org>
55  *      - Renamed from h1940fb.c to s3c2410fb.c
56  *      - Add support for different devices
57  *      - Backlight support
58  *
59  * 2004-09-05: Herbert Pötzl <herbert@13thfloor.at>
60  *      - added clock (de-)allocation code
61  *      - added fixem fbmem option
62  *
63  * 2004-07-27: Arnaud Patard <arnaud.patard@rtp-net.org>
64  *      - code cleanup
65  *      - added a forgotten return in h1940fb_init
66  *
67  * 2004-07-19: Herbert Pötzl <herbert@13thfloor.at>
68  *      - code cleanup and extended debugging
69  *
70  * 2004-07-15: Arnaud Patard <arnaud.patard@rtp-net.org>
71  *      - First version
72  */
73
74 #include <linux/module.h>
75 #include <linux/kernel.h>
76 #include <linux/errno.h>
77 #include <linux/string.h>
78 #include <linux/mm.h>
79 #include <linux/tty.h>
80 #include <linux/slab.h>
81 #include <linux/delay.h>
82 #include <linux/fb.h>
83 #include <linux/init.h>
84 #include <linux/dma-mapping.h>
85 #include <linux/interrupt.h>
86 #include <linux/workqueue.h>
87 #include <linux/wait.h>
88 #include <linux/platform_device.h>
89 #include <linux/clk.h>
90
91 #include <asm/io.h>
92 #include <asm/uaccess.h>
93 #include <asm/div64.h>
94
95 #include <asm/mach/map.h>
96 #include <asm/arch/regs-lcd.h>
97 #include <asm/arch/regs-gpio.h>
98 #include <asm/arch/fb.h>
99
100 #ifdef CONFIG_PM
101 #include <linux/pm.h>
102 #endif
103
104 #include "s3c2410fb.h"
105
106
107 static struct s3c2410fb_mach_info *mach_info;
108
109 /* Debugging stuff */
110 #ifdef CONFIG_FB_S3C2410_DEBUG
111 static int debug           = 1;
112 #else
113 static int debug           = 0;
114 #endif
115
116 #define dprintk(msg...) if (debug) { printk(KERN_DEBUG "s3c2410fb: " msg); }
117
118 /* useful functions */
119
120 /* s3c2410fb_set_lcdaddr
121  *
122  * initialise lcd controller address pointers
123 */
124
125 static void s3c2410fb_set_lcdaddr(struct s3c2410fb_info *fbi)
126 {
127         struct fb_var_screeninfo *var = &fbi->fb->var;
128         unsigned long saddr1, saddr2, saddr3;
129
130         saddr1  = fbi->fb->fix.smem_start >> 1;
131         saddr2  = fbi->fb->fix.smem_start;
132         saddr2 += (var->xres * var->yres * var->bits_per_pixel)/8;
133         saddr2>>= 1;
134
135         saddr3 =  S3C2410_OFFSIZE(0) | S3C2410_PAGEWIDTH(var->xres);
136
137         dprintk("LCDSADDR1 = 0x%08lx\n", saddr1);
138         dprintk("LCDSADDR2 = 0x%08lx\n", saddr2);
139         dprintk("LCDSADDR3 = 0x%08lx\n", saddr3);
140
141         writel(saddr1, S3C2410_LCDSADDR1);
142         writel(saddr2, S3C2410_LCDSADDR2);
143         writel(saddr3, S3C2410_LCDSADDR3);
144 }
145
146 /* s3c2410fb_calc_pixclk()
147  *
148  * calculate divisor for clk->pixclk
149 */
150
151 static unsigned int s3c2410fb_calc_pixclk(struct s3c2410fb_info *fbi,
152                                           unsigned long pixclk)
153 {
154         unsigned long clk = clk_get_rate(fbi->clk);
155         unsigned long long div;
156
157         /* pixclk is in picoseoncds, our clock is in Hz
158          *
159          * Hz -> picoseconds is / 10^-12
160          */
161
162         div = (unsigned long long)clk * pixclk;
163         do_div(div,1000000UL);
164         do_div(div,1000000UL);
165
166         dprintk("pixclk %ld, divisor is %ld\n", pixclk, (long)div);
167         return div;
168 }
169
170 /*
171  *      s3c2410fb_check_var():
172  *      Get the video params out of 'var'. If a value doesn't fit, round it up,
173  *      if it's too big, return -EINVAL.
174  *
175  */
176 static int s3c2410fb_check_var(struct fb_var_screeninfo *var,
177                                struct fb_info *info)
178 {
179         struct s3c2410fb_info *fbi = info->par;
180
181         dprintk("check_var(var=%p, info=%p)\n", var, info);
182
183         /* validate x/y resolution */
184
185         if (var->yres > fbi->mach_info->yres.max)
186                 var->yres = fbi->mach_info->yres.max;
187         else if (var->yres < fbi->mach_info->yres.min)
188                 var->yres = fbi->mach_info->yres.min;
189
190         if (var->xres > fbi->mach_info->xres.max)
191                 var->yres = fbi->mach_info->xres.max;
192         else if (var->xres < fbi->mach_info->xres.min)
193                 var->xres = fbi->mach_info->xres.min;
194
195         /* validate bpp */
196
197         if (var->bits_per_pixel > fbi->mach_info->bpp.max)
198                 var->bits_per_pixel = fbi->mach_info->bpp.max;
199         else if (var->bits_per_pixel < fbi->mach_info->bpp.min)
200                 var->bits_per_pixel = fbi->mach_info->bpp.min;
201
202         /* set r/g/b positions */
203
204         if (var->bits_per_pixel == 16) {
205                 var->red.offset         = 11;
206                 var->green.offset       = 5;
207                 var->blue.offset        = 0;
208                 var->red.length         = 5;
209                 var->green.length       = 6;
210                 var->blue.length        = 5;
211                 var->transp.length      = 0;
212         } else {
213                 var->red.length         = var->bits_per_pixel;
214                 var->red.offset         = 0;
215                 var->green.length       = var->bits_per_pixel;
216                 var->green.offset       = 0;
217                 var->blue.length        = var->bits_per_pixel;
218                 var->blue.offset        = 0;
219                 var->transp.length      = 0;
220         }
221
222         return 0;
223 }
224
225 /* s3c2410fb_activate_var
226  *
227  * activate (set) the controller from the given framebuffer
228  * information
229 */
230
231 static void s3c2410fb_activate_var(struct s3c2410fb_info *fbi,
232                                    struct fb_var_screeninfo *var)
233 {
234         fbi->regs.lcdcon1 &= ~S3C2410_LCDCON1_MODEMASK;
235
236         dprintk("%s: var->xres  = %d\n", __FUNCTION__, var->xres);
237         dprintk("%s: var->yres  = %d\n", __FUNCTION__, var->yres);
238         dprintk("%s: var->bpp   = %d\n", __FUNCTION__, var->bits_per_pixel);
239
240         switch (var->bits_per_pixel) {
241         case 1:
242                 fbi->regs.lcdcon1 |= S3C2410_LCDCON1_TFT1BPP;
243                 break;
244         case 2:
245                 fbi->regs.lcdcon1 |= S3C2410_LCDCON1_TFT2BPP;
246                 break;
247         case 4:
248                 fbi->regs.lcdcon1 |= S3C2410_LCDCON1_TFT4BPP;
249                 break;
250         case 8:
251                 fbi->regs.lcdcon1 |= S3C2410_LCDCON1_TFT8BPP;
252                 break;
253         case 16:
254                 fbi->regs.lcdcon1 |= S3C2410_LCDCON1_TFT16BPP;
255                 break;
256         }
257
258         /* check to see if we need to update sync/borders */
259
260         if (!fbi->mach_info->fixed_syncs) {
261                 dprintk("setting vert: up=%d, low=%d, sync=%d\n",
262                         var->upper_margin, var->lower_margin,
263                         var->vsync_len);
264
265                 dprintk("setting horz: lft=%d, rt=%d, sync=%d\n",
266                         var->left_margin, var->right_margin,
267                         var->hsync_len);
268
269                 fbi->regs.lcdcon2 =
270                         S3C2410_LCDCON2_VBPD(var->upper_margin - 1) |
271                         S3C2410_LCDCON2_VFPD(var->lower_margin - 1) |
272                         S3C2410_LCDCON2_VSPW(var->vsync_len - 1);
273
274                 fbi->regs.lcdcon3 =
275                         S3C2410_LCDCON3_HBPD(var->right_margin - 1) |
276                         S3C2410_LCDCON3_HFPD(var->left_margin - 1);
277
278                 fbi->regs.lcdcon4 &= ~S3C2410_LCDCON4_HSPW(0xff);
279                 fbi->regs.lcdcon4 |=  S3C2410_LCDCON4_HSPW(var->hsync_len - 1);
280         }
281
282         /* update X/Y info */
283
284         fbi->regs.lcdcon2 &= ~S3C2410_LCDCON2_LINEVAL(0x3ff);
285         fbi->regs.lcdcon2 |=  S3C2410_LCDCON2_LINEVAL(var->yres - 1);
286
287         fbi->regs.lcdcon3 &= ~S3C2410_LCDCON3_HOZVAL(0x7ff);
288         fbi->regs.lcdcon3 |=  S3C2410_LCDCON3_HOZVAL(var->xres - 1);
289
290         if (var->pixclock > 0) {
291                 int clkdiv = s3c2410fb_calc_pixclk(fbi, var->pixclock);
292
293                 clkdiv = (clkdiv / 2) -1;
294                 if (clkdiv < 0)
295                         clkdiv = 0;
296
297                 fbi->regs.lcdcon1 &= ~S3C2410_LCDCON1_CLKVAL(0x3ff);
298                 fbi->regs.lcdcon1 |=  S3C2410_LCDCON1_CLKVAL(clkdiv);
299         }
300
301         /* write new registers */
302
303         dprintk("new register set:\n");
304         dprintk("lcdcon[1] = 0x%08lx\n", fbi->regs.lcdcon1);
305         dprintk("lcdcon[2] = 0x%08lx\n", fbi->regs.lcdcon2);
306         dprintk("lcdcon[3] = 0x%08lx\n", fbi->regs.lcdcon3);
307         dprintk("lcdcon[4] = 0x%08lx\n", fbi->regs.lcdcon4);
308         dprintk("lcdcon[5] = 0x%08lx\n", fbi->regs.lcdcon5);
309
310         writel(fbi->regs.lcdcon1 & ~S3C2410_LCDCON1_ENVID, S3C2410_LCDCON1);
311         writel(fbi->regs.lcdcon2, S3C2410_LCDCON2);
312         writel(fbi->regs.lcdcon3, S3C2410_LCDCON3);
313         writel(fbi->regs.lcdcon4, S3C2410_LCDCON4);
314         writel(fbi->regs.lcdcon5, S3C2410_LCDCON5);
315
316         /* set lcd address pointers */
317         s3c2410fb_set_lcdaddr(fbi);
318
319         writel(fbi->regs.lcdcon1, S3C2410_LCDCON1);
320 }
321
322
323 /*
324  *      s3c2410fb_set_par - Optional function. Alters the hardware state.
325  *      @info: frame buffer structure that represents a single frame buffer
326  *
327  */
328 static int s3c2410fb_set_par(struct fb_info *info)
329 {
330         struct s3c2410fb_info *fbi = info->par;
331         struct fb_var_screeninfo *var = &info->var;
332
333         if (var->bits_per_pixel == 16)
334                 fbi->fb->fix.visual = FB_VISUAL_TRUECOLOR;
335         else
336                 fbi->fb->fix.visual = FB_VISUAL_PSEUDOCOLOR;
337
338         fbi->fb->fix.line_length     = (var->width*var->bits_per_pixel)/8;
339
340         /* activate this new configuration */
341
342         s3c2410fb_activate_var(fbi, var);
343         return 0;
344 }
345
346 static void schedule_palette_update(struct s3c2410fb_info *fbi,
347                                     unsigned int regno, unsigned int val)
348 {
349         unsigned long flags;
350         unsigned long irqen;
351
352         local_irq_save(flags);
353
354         fbi->palette_buffer[regno] = val;
355
356         if (!fbi->palette_ready) {
357                 fbi->palette_ready = 1;
358
359                 /* enable IRQ */
360                 irqen = readl(S3C2410_LCDINTMSK);
361                 irqen &= ~S3C2410_LCDINT_FRSYNC;
362                 writel(irqen, S3C2410_LCDINTMSK);
363         }
364
365         local_irq_restore(flags);
366 }
367
368 /* from pxafb.c */
369 static inline unsigned int chan_to_field(unsigned int chan, struct fb_bitfield *bf)
370 {
371         chan &= 0xffff;
372         chan >>= 16 - bf->length;
373         return chan << bf->offset;
374 }
375
376 static int s3c2410fb_setcolreg(unsigned regno,
377                                unsigned red, unsigned green, unsigned blue,
378                                unsigned transp, struct fb_info *info)
379 {
380         struct s3c2410fb_info *fbi = info->par;
381         unsigned int val;
382
383         /* dprintk("setcol: regno=%d, rgb=%d,%d,%d\n", regno, red, green, blue); */
384
385         switch (fbi->fb->fix.visual) {
386         case FB_VISUAL_TRUECOLOR:
387                 /* true-colour, use pseuo-palette */
388
389                 if (regno < 16) {
390                         u32 *pal = fbi->fb->pseudo_palette;
391
392                         val  = chan_to_field(red,   &fbi->fb->var.red);
393                         val |= chan_to_field(green, &fbi->fb->var.green);
394                         val |= chan_to_field(blue,  &fbi->fb->var.blue);
395
396                         pal[regno] = val;
397                 }
398                 break;
399
400         case FB_VISUAL_PSEUDOCOLOR:
401                 if (regno < 256) {
402                         /* currently assume RGB 5-6-5 mode */
403
404                         val  = ((red   >>  0) & 0xf800);
405                         val |= ((green >>  5) & 0x07e0);
406                         val |= ((blue  >> 11) & 0x001f);
407
408                         writel(val, S3C2410_TFTPAL(regno));
409                         schedule_palette_update(fbi, regno, val);
410                 }
411
412                 break;
413
414         default:
415                 return 1;   /* unknown type */
416         }
417
418         return 0;
419 }
420
421
422 /**
423  *      s3c2410fb_blank
424  *      @blank_mode: the blank mode we want.
425  *      @info: frame buffer structure that represents a single frame buffer
426  *
427  *      Blank the screen if blank_mode != 0, else unblank. Return 0 if
428  *      blanking succeeded, != 0 if un-/blanking failed due to e.g. a
429  *      video mode which doesn't support it. Implements VESA suspend
430  *      and powerdown modes on hardware that supports disabling hsync/vsync:
431  *      blank_mode == 2: suspend vsync
432  *      blank_mode == 3: suspend hsync
433  *      blank_mode == 4: powerdown
434  *
435  *      Returns negative errno on error, or zero on success.
436  *
437  */
438 static int s3c2410fb_blank(int blank_mode, struct fb_info *info)
439 {
440         dprintk("blank(mode=%d, info=%p)\n", blank_mode, info);
441
442         if (mach_info == NULL)
443                 return -EINVAL;
444
445         if (blank_mode == FB_BLANK_UNBLANK)
446                 writel(0x0, S3C2410_TPAL);
447         else {
448                 dprintk("setting TPAL to output 0x000000\n");
449                 writel(S3C2410_TPAL_EN, S3C2410_TPAL);
450         }
451
452         return 0;
453 }
454
455 static int s3c2410fb_debug_show(struct device *dev, struct device_attribute *attr, char *buf)
456 {
457         return snprintf(buf, PAGE_SIZE, "%s\n", debug ? "on" : "off");
458 }
459 static int s3c2410fb_debug_store(struct device *dev, struct device_attribute *attr,
460                                            const char *buf, size_t len)
461 {
462         if (mach_info == NULL)
463                 return -EINVAL;
464
465         if (len < 1)
466                 return -EINVAL;
467
468         if (strnicmp(buf, "on", 2) == 0 ||
469             strnicmp(buf, "1", 1) == 0) {
470                 debug = 1;
471                 printk(KERN_DEBUG "s3c2410fb: Debug On");
472         } else if (strnicmp(buf, "off", 3) == 0 ||
473                    strnicmp(buf, "0", 1) == 0) {
474                 debug = 0;
475                 printk(KERN_DEBUG "s3c2410fb: Debug Off");
476         } else {
477                 return -EINVAL;
478         }
479
480         return len;
481 }
482
483
484 static DEVICE_ATTR(debug, 0666,
485                    s3c2410fb_debug_show,
486                    s3c2410fb_debug_store);
487
488 static struct fb_ops s3c2410fb_ops = {
489         .owner          = THIS_MODULE,
490         .fb_check_var   = s3c2410fb_check_var,
491         .fb_set_par     = s3c2410fb_set_par,
492         .fb_blank       = s3c2410fb_blank,
493         .fb_setcolreg   = s3c2410fb_setcolreg,
494         .fb_fillrect    = cfb_fillrect,
495         .fb_copyarea    = cfb_copyarea,
496         .fb_imageblit   = cfb_imageblit,
497 };
498
499
500 /*
501  * s3c2410fb_map_video_memory():
502  *      Allocates the DRAM memory for the frame buffer.  This buffer is
503  *      remapped into a non-cached, non-buffered, memory region to
504  *      allow palette and pixel writes to occur without flushing the
505  *      cache.  Once this area is remapped, all virtual memory
506  *      access to the video memory should occur at the new region.
507  */
508 static int __init s3c2410fb_map_video_memory(struct s3c2410fb_info *fbi)
509 {
510         dprintk("map_video_memory(fbi=%p)\n", fbi);
511
512         fbi->map_size = PAGE_ALIGN(fbi->fb->fix.smem_len + PAGE_SIZE);
513         fbi->map_cpu  = dma_alloc_writecombine(fbi->dev, fbi->map_size,
514                                                &fbi->map_dma, GFP_KERNEL);
515
516         fbi->map_size = fbi->fb->fix.smem_len;
517
518         if (fbi->map_cpu) {
519                 /* prevent initial garbage on screen */
520                 dprintk("map_video_memory: clear %p:%08x\n",
521                         fbi->map_cpu, fbi->map_size);
522                 memset(fbi->map_cpu, 0xf0, fbi->map_size);
523
524                 fbi->screen_dma         = fbi->map_dma;
525                 fbi->fb->screen_base    = fbi->map_cpu;
526                 fbi->fb->fix.smem_start  = fbi->screen_dma;
527
528                 dprintk("map_video_memory: dma=%08x cpu=%p size=%08x\n",
529                         fbi->map_dma, fbi->map_cpu, fbi->fb->fix.smem_len);
530         }
531
532         return fbi->map_cpu ? 0 : -ENOMEM;
533 }
534
535 static inline void s3c2410fb_unmap_video_memory(struct s3c2410fb_info *fbi)
536 {
537         dma_free_writecombine(fbi->dev,fbi->map_size,fbi->map_cpu, fbi->map_dma);
538 }
539
540 static inline void modify_gpio(void __iomem *reg,
541                                unsigned long set, unsigned long mask)
542 {
543         unsigned long tmp;
544
545         tmp = readl(reg) & ~mask;
546         writel(tmp | set, reg);
547 }
548
549
550 /*
551  * s3c2410fb_init_registers - Initialise all LCD-related registers
552  */
553
554 static int s3c2410fb_init_registers(struct s3c2410fb_info *fbi)
555 {
556         unsigned long flags;
557
558         /* Initialise LCD with values from haret */
559
560         local_irq_save(flags);
561
562         /* modify the gpio(s) with interrupts set (bjd) */
563
564         modify_gpio(S3C2410_GPCUP,  mach_info->gpcup,  mach_info->gpcup_mask);
565         modify_gpio(S3C2410_GPCCON, mach_info->gpccon, mach_info->gpccon_mask);
566         modify_gpio(S3C2410_GPDUP,  mach_info->gpdup,  mach_info->gpdup_mask);
567         modify_gpio(S3C2410_GPDCON, mach_info->gpdcon, mach_info->gpdcon_mask);
568
569         local_irq_restore(flags);
570
571         writel(fbi->regs.lcdcon1, S3C2410_LCDCON1);
572         writel(fbi->regs.lcdcon2, S3C2410_LCDCON2);
573         writel(fbi->regs.lcdcon3, S3C2410_LCDCON3);
574         writel(fbi->regs.lcdcon4, S3C2410_LCDCON4);
575         writel(fbi->regs.lcdcon5, S3C2410_LCDCON5);
576
577         s3c2410fb_set_lcdaddr(fbi);
578
579         dprintk("LPCSEL    = 0x%08lx\n", mach_info->lpcsel);
580         writel(mach_info->lpcsel, S3C2410_LPCSEL);
581
582         dprintk("replacing TPAL %08x\n", readl(S3C2410_TPAL));
583
584         /* ensure temporary palette disabled */
585         writel(0x00, S3C2410_TPAL);
586
587         /* Enable video by setting the ENVID bit to 1 */
588         fbi->regs.lcdcon1 |= S3C2410_LCDCON1_ENVID;
589         writel(fbi->regs.lcdcon1, S3C2410_LCDCON1);
590         return 0;
591 }
592
593 static void s3c2410fb_write_palette(struct s3c2410fb_info *fbi)
594 {
595         unsigned int i;
596         unsigned long ent;
597
598         fbi->palette_ready = 0;
599
600         for (i = 0; i < 256; i++) {
601                 if ((ent = fbi->palette_buffer[i]) == PALETTE_BUFF_CLEAR)
602                         continue;
603
604                 writel(ent, S3C2410_TFTPAL(i));
605
606                 /* it seems the only way to know exactly
607                  * if the palette wrote ok, is to check
608                  * to see if the value verifies ok
609                  */
610
611                 if (readw(S3C2410_TFTPAL(i)) == ent)
612                         fbi->palette_buffer[i] = PALETTE_BUFF_CLEAR;
613                 else
614                         fbi->palette_ready = 1;   /* retry */
615         }
616 }
617
618 static irqreturn_t s3c2410fb_irq(int irq, void *dev_id, struct pt_regs *r)
619 {
620         struct s3c2410fb_info *fbi = dev_id;
621         unsigned long lcdirq = readl(S3C2410_LCDINTPND);
622
623         if (lcdirq & S3C2410_LCDINT_FRSYNC) {
624                 if (fbi->palette_ready)
625                         s3c2410fb_write_palette(fbi);
626
627                 writel(S3C2410_LCDINT_FRSYNC, S3C2410_LCDINTPND);
628                 writel(S3C2410_LCDINT_FRSYNC, S3C2410_LCDSRCPND);
629         }
630
631         return IRQ_HANDLED;
632 }
633
634 static char driver_name[]="s3c2410fb";
635
636 static int __init s3c2410fb_probe(struct platform_device *pdev)
637 {
638         struct s3c2410fb_info *info;
639         struct fb_info     *fbinfo;
640         struct s3c2410fb_hw *mregs;
641         int ret;
642         int irq;
643         int i;
644         u32 lcdcon1;
645
646         mach_info = pdev->dev.platform_data;
647         if (mach_info == NULL) {
648                 dev_err(&pdev->dev,"no platform data for lcd, cannot attach\n");
649                 return -EINVAL;
650         }
651
652         mregs = &mach_info->regs;
653
654         irq = platform_get_irq(pdev, 0);
655         if (irq < 0) {
656                 dev_err(&pdev->dev, "no irq for device\n");
657                 return -ENOENT;
658         }
659
660         fbinfo = framebuffer_alloc(sizeof(struct s3c2410fb_info), &pdev->dev);
661         if (!fbinfo) {
662                 return -ENOMEM;
663         }
664
665
666         info = fbinfo->par;
667         info->fb = fbinfo;
668         platform_set_drvdata(pdev, fbinfo);
669
670         dprintk("devinit\n");
671
672         strcpy(fbinfo->fix.id, driver_name);
673
674         memcpy(&info->regs, &mach_info->regs, sizeof(info->regs));
675
676         /* Stop the video and unset ENVID if set */
677         info->regs.lcdcon1 &= ~S3C2410_LCDCON1_ENVID;
678         lcdcon1 = readl(S3C2410_LCDCON1);
679         writel(lcdcon1 & ~S3C2410_LCDCON1_ENVID, S3C2410_LCDCON1);
680
681         info->mach_info             = pdev->dev.platform_data;
682
683         fbinfo->fix.type            = FB_TYPE_PACKED_PIXELS;
684         fbinfo->fix.type_aux        = 0;
685         fbinfo->fix.xpanstep        = 0;
686         fbinfo->fix.ypanstep        = 0;
687         fbinfo->fix.ywrapstep       = 0;
688         fbinfo->fix.accel           = FB_ACCEL_NONE;
689
690         fbinfo->var.nonstd          = 0;
691         fbinfo->var.activate        = FB_ACTIVATE_NOW;
692         fbinfo->var.height          = mach_info->height;
693         fbinfo->var.width           = mach_info->width;
694         fbinfo->var.accel_flags     = 0;
695         fbinfo->var.vmode           = FB_VMODE_NONINTERLACED;
696
697         fbinfo->fbops               = &s3c2410fb_ops;
698         fbinfo->flags               = FBINFO_FLAG_DEFAULT;
699         fbinfo->pseudo_palette      = &info->pseudo_pal;
700
701         fbinfo->var.xres            = mach_info->xres.defval;
702         fbinfo->var.xres_virtual    = mach_info->xres.defval;
703         fbinfo->var.yres            = mach_info->yres.defval;
704         fbinfo->var.yres_virtual    = mach_info->yres.defval;
705         fbinfo->var.bits_per_pixel  = mach_info->bpp.defval;
706
707         fbinfo->var.upper_margin    = S3C2410_LCDCON2_GET_VBPD(mregs->lcdcon2) + 1;
708         fbinfo->var.lower_margin    = S3C2410_LCDCON2_GET_VFPD(mregs->lcdcon2) + 1;
709         fbinfo->var.vsync_len       = S3C2410_LCDCON2_GET_VSPW(mregs->lcdcon2) + 1;
710
711         fbinfo->var.left_margin     = S3C2410_LCDCON3_GET_HFPD(mregs->lcdcon3) + 1;
712         fbinfo->var.right_margin    = S3C2410_LCDCON3_GET_HBPD(mregs->lcdcon3) + 1;
713         fbinfo->var.hsync_len       = S3C2410_LCDCON4_GET_HSPW(mregs->lcdcon4) + 1;
714
715         fbinfo->var.red.offset      = 11;
716         fbinfo->var.green.offset    = 5;
717         fbinfo->var.blue.offset     = 0;
718         fbinfo->var.transp.offset   = 0;
719         fbinfo->var.red.length      = 5;
720         fbinfo->var.green.length    = 6;
721         fbinfo->var.blue.length     = 5;
722         fbinfo->var.transp.length   = 0;
723         fbinfo->fix.smem_len        =   mach_info->xres.max *
724                                         mach_info->yres.max *
725                                         mach_info->bpp.max / 8;
726
727         for (i = 0; i < 256; i++)
728                 info->palette_buffer[i] = PALETTE_BUFF_CLEAR;
729
730         if (!request_mem_region((unsigned long)S3C24XX_VA_LCD, SZ_1M, "s3c2410-lcd")) {
731                 ret = -EBUSY;
732                 goto dealloc_fb;
733         }
734
735
736         dprintk("got LCD region\n");
737
738         ret = request_irq(irq, s3c2410fb_irq, IRQF_DISABLED, pdev->name, info);
739         if (ret) {
740                 dev_err(&pdev->dev, "cannot get irq %d - err %d\n", irq, ret);
741                 ret = -EBUSY;
742                 goto release_mem;
743         }
744
745         info->clk = clk_get(NULL, "lcd");
746         if (!info->clk || IS_ERR(info->clk)) {
747                 printk(KERN_ERR "failed to get lcd clock source\n");
748                 ret = -ENOENT;
749                 goto release_irq;
750         }
751
752         clk_enable(info->clk);
753         dprintk("got and enabled clock\n");
754
755         msleep(1);
756
757         /* Initialize video memory */
758         ret = s3c2410fb_map_video_memory(info);
759         if (ret) {
760                 printk( KERN_ERR "Failed to allocate video RAM: %d\n", ret);
761                 ret = -ENOMEM;
762                 goto release_clock;
763         }
764         dprintk("got video memory\n");
765
766         ret = s3c2410fb_init_registers(info);
767
768         ret = s3c2410fb_check_var(&fbinfo->var, fbinfo);
769
770         ret = register_framebuffer(fbinfo);
771         if (ret < 0) {
772                 printk(KERN_ERR "Failed to register framebuffer device: %d\n", ret);
773                 goto free_video_memory;
774         }
775
776         /* create device files */
777         device_create_file(&pdev->dev, &dev_attr_debug);
778
779         printk(KERN_INFO "fb%d: %s frame buffer device\n",
780                 fbinfo->node, fbinfo->fix.id);
781
782         return 0;
783
784 free_video_memory:
785         s3c2410fb_unmap_video_memory(info);
786 release_clock:
787         clk_disable(info->clk);
788         clk_put(info->clk);
789 release_irq:
790         free_irq(irq,info);
791 release_mem:
792         release_mem_region((unsigned long)S3C24XX_VA_LCD, S3C24XX_SZ_LCD);
793 dealloc_fb:
794         framebuffer_release(fbinfo);
795         return ret;
796 }
797
798 /* s3c2410fb_stop_lcd
799  *
800  * shutdown the lcd controller
801 */
802
803 static void s3c2410fb_stop_lcd(struct s3c2410fb_info *fbi)
804 {
805         unsigned long flags;
806
807         local_irq_save(flags);
808
809         fbi->regs.lcdcon1 &= ~S3C2410_LCDCON1_ENVID;
810         writel(fbi->regs.lcdcon1, S3C2410_LCDCON1);
811
812         local_irq_restore(flags);
813 }
814
815 /*
816  *  Cleanup
817  */
818 static int s3c2410fb_remove(struct platform_device *pdev)
819 {
820         struct fb_info     *fbinfo = platform_get_drvdata(pdev);
821         struct s3c2410fb_info *info = fbinfo->par;
822         int irq;
823
824         s3c2410fb_stop_lcd(info);
825         msleep(1);
826
827         s3c2410fb_unmap_video_memory(info);
828
829         if (info->clk) {
830                 clk_disable(info->clk);
831                 clk_put(info->clk);
832                 info->clk = NULL;
833         }
834
835         irq = platform_get_irq(pdev, 0);
836         free_irq(irq,info);
837         release_mem_region((unsigned long)S3C24XX_VA_LCD, S3C24XX_SZ_LCD);
838         unregister_framebuffer(fbinfo);
839
840         return 0;
841 }
842
843 #ifdef CONFIG_PM
844
845 /* suspend and resume support for the lcd controller */
846
847 static int s3c2410fb_suspend(struct platform_device *dev, pm_message_t state)
848 {
849         struct fb_info     *fbinfo = platform_get_drvdata(dev);
850         struct s3c2410fb_info *info = fbinfo->par;
851
852         s3c2410fb_stop_lcd(info);
853
854         /* sleep before disabling the clock, we need to ensure
855          * the LCD DMA engine is not going to get back on the bus
856          * before the clock goes off again (bjd) */
857
858         msleep(1);
859         clk_disable(info->clk);
860
861         return 0;
862 }
863
864 static int s3c2410fb_resume(struct platform_device *dev)
865 {
866         struct fb_info     *fbinfo = platform_get_drvdata(dev);
867         struct s3c2410fb_info *info = fbinfo->par;
868
869         clk_enable(info->clk);
870         msleep(1);
871
872         s3c2410fb_init_registers(info);
873
874         return 0;
875 }
876
877 #else
878 #define s3c2410fb_suspend NULL
879 #define s3c2410fb_resume  NULL
880 #endif
881
882 static struct platform_driver s3c2410fb_driver = {
883         .probe          = s3c2410fb_probe,
884         .remove         = s3c2410fb_remove,
885         .suspend        = s3c2410fb_suspend,
886         .resume         = s3c2410fb_resume,
887         .driver         = {
888                 .name   = "s3c2410-lcd",
889                 .owner  = THIS_MODULE,
890         },
891 };
892
893 int __devinit s3c2410fb_init(void)
894 {
895         return platform_driver_register(&s3c2410fb_driver);
896 }
897
898 static void __exit s3c2410fb_cleanup(void)
899 {
900         platform_driver_unregister(&s3c2410fb_driver);
901 }
902
903
904 module_init(s3c2410fb_init);
905 module_exit(s3c2410fb_cleanup);
906
907 MODULE_AUTHOR("Arnaud Patard <arnaud.patard@rtp-net.org>, Ben Dooks <ben-linux@fluff.org>");
908 MODULE_DESCRIPTION("Framebuffer driver for the s3c2410");
909 MODULE_LICENSE("GPL");