fbdev: move FBIO_WAITFORVSYNC to linux/fb.h
[safe/jmp/linux-2.6] / drivers / video / s3c-fb.c
1 /* linux/drivers/video/s3c-fb.c
2  *
3  * Copyright 2008 Openmoko Inc.
4  * Copyright 2008 Simtec Electronics
5  *      Ben Dooks <ben@simtec.co.uk>
6  *      http://armlinux.simtec.co.uk/
7  *
8  * Samsung SoC Framebuffer driver
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License version 2 as
12  * published by the Free Software Foundation.
13 */
14
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/platform_device.h>
18 #include <linux/dma-mapping.h>
19 #include <linux/slab.h>
20 #include <linux/init.h>
21 #include <linux/clk.h>
22 #include <linux/fb.h>
23 #include <linux/io.h>
24
25 #include <mach/map.h>
26 #include <mach/regs-fb.h>
27 #include <plat/fb.h>
28
29 /* This driver will export a number of framebuffer interfaces depending
30  * on the configuration passed in via the platform data. Each fb instance
31  * maps to a hardware window. Currently there is no support for runtime
32  * setting of the alpha-blending functions that each window has, so only
33  * window 0 is actually useful.
34  *
35  * Window 0 is treated specially, it is used for the basis of the LCD
36  * output timings and as the control for the output power-down state.
37 */
38
39 /* note, some of the functions that get called are derived from including
40  * <mach/regs-fb.h> as they are specific to the architecture that the code
41  * is being built for.
42 */
43
44 #ifdef CONFIG_FB_S3C_DEBUG_REGWRITE
45 #undef writel
46 #define writel(v, r) do { \
47         printk(KERN_DEBUG "%s: %08x => %p\n", __func__, (unsigned int)v, r); \
48         __raw_writel(v, r); } while(0)
49 #endif /* FB_S3C_DEBUG_REGWRITE */
50
51 struct s3c_fb;
52
53 /**
54  * struct s3c_fb_win - per window private data for each framebuffer.
55  * @windata: The platform data supplied for the window configuration.
56  * @parent: The hardware that this window is part of.
57  * @fbinfo: Pointer pack to the framebuffer info for this window.
58  * @palette_buffer: Buffer/cache to hold palette entries.
59  * @pseudo_palette: For use in TRUECOLOUR modes for entries 0..15/
60  * @index: The window number of this window.
61  * @palette: The bitfields for changing r/g/b into a hardware palette entry.
62  */
63 struct s3c_fb_win {
64         struct s3c_fb_pd_win    *windata;
65         struct s3c_fb           *parent;
66         struct fb_info          *fbinfo;
67         struct s3c_fb_palette    palette;
68
69         u32                     *palette_buffer;
70         u32                      pseudo_palette[16];
71         unsigned int             index;
72 };
73
74 /**
75  * struct s3c_fb - overall hardware state of the hardware
76  * @dev: The device that we bound to, for printing, etc.
77  * @regs_res: The resource we claimed for the IO registers.
78  * @bus_clk: The clk (hclk) feeding our interface and possibly pixclk.
79  * @regs: The mapped hardware registers.
80  * @enabled: A bitmask of enabled hardware windows.
81  * @pdata: The platform configuration data passed with the device.
82  * @windows: The hardware windows that have been claimed.
83  */
84 struct s3c_fb {
85         struct device           *dev;
86         struct resource         *regs_res;
87         struct clk              *bus_clk;
88         void __iomem            *regs;
89
90         unsigned char            enabled;
91
92         struct s3c_fb_platdata  *pdata;
93         struct s3c_fb_win       *windows[S3C_FB_MAX_WIN];
94 };
95
96 /**
97  * s3c_fb_win_has_palette() - determine if a mode has a palette
98  * @win: The window number being queried.
99  * @bpp: The number of bits per pixel to test.
100  *
101  * Work out if the given window supports palletised data at the specified bpp.
102  */
103 static int s3c_fb_win_has_palette(unsigned int win, unsigned int bpp)
104 {
105         return s3c_fb_win_pal_size(win) <= (1 << bpp);
106 }
107
108 /**
109  * s3c_fb_check_var() - framebuffer layer request to verify a given mode.
110  * @var: The screen information to verify.
111  * @info: The framebuffer device.
112  *
113  * Framebuffer layer call to verify the given information and allow us to
114  * update various information depending on the hardware capabilities.
115  */
116 static int s3c_fb_check_var(struct fb_var_screeninfo *var,
117                             struct fb_info *info)
118 {
119         struct s3c_fb_win *win = info->par;
120         struct s3c_fb_pd_win *windata = win->windata;
121         struct s3c_fb *sfb = win->parent;
122
123         dev_dbg(sfb->dev, "checking parameters\n");
124
125         var->xres_virtual = max((unsigned int)windata->virtual_x, var->xres);
126         var->yres_virtual = max((unsigned int)windata->virtual_y, var->yres);
127
128         if (!s3c_fb_validate_win_bpp(win->index, var->bits_per_pixel)) {
129                 dev_dbg(sfb->dev, "win %d: unsupported bpp %d\n",
130                         win->index, var->bits_per_pixel);
131                 return -EINVAL;
132         }
133
134         /* always ensure these are zero, for drop through cases below */
135         var->transp.offset = 0;
136         var->transp.length = 0;
137
138         switch (var->bits_per_pixel) {
139         case 1:
140         case 2:
141         case 4:
142         case 8:
143                 if (!s3c_fb_win_has_palette(win->index, var->bits_per_pixel)) {
144                         /* non palletised, A:1,R:2,G:3,B:2 mode */
145                         var->red.offset         = 4;
146                         var->green.offset       = 2;
147                         var->blue.offset        = 0;
148                         var->red.length         = 5;
149                         var->green.length       = 3;
150                         var->blue.length        = 2;
151                         var->transp.offset      = 7;
152                         var->transp.length      = 1;
153                 } else {
154                         var->red.offset = 0;
155                         var->red.length = var->bits_per_pixel;
156                         var->green      = var->red;
157                         var->blue       = var->red;
158                 }
159                 break;
160
161         case 19:
162                 /* 666 with one bit alpha/transparency */
163                 var->transp.offset      = 18;
164                 var->transp.length      = 1;
165         case 18:
166                 var->bits_per_pixel     = 32;
167
168                 /* 666 format */
169                 var->red.offset         = 12;
170                 var->green.offset       = 6;
171                 var->blue.offset        = 0;
172                 var->red.length         = 6;
173                 var->green.length       = 6;
174                 var->blue.length        = 6;
175                 break;
176
177         case 16:
178                 /* 16 bpp, 565 format */
179                 var->red.offset         = 11;
180                 var->green.offset       = 5;
181                 var->blue.offset        = 0;
182                 var->red.length         = 5;
183                 var->green.length       = 6;
184                 var->blue.length        = 5;
185                 break;
186
187         case 28:
188         case 25:
189                 var->transp.length      = var->bits_per_pixel - 24;
190                 var->transp.offset      = 24;
191                 /* drop through */
192         case 24:
193                 /* our 24bpp is unpacked, so 32bpp */
194                 var->bits_per_pixel     = 32;
195         case 32:
196                 var->red.offset         = 16;
197                 var->red.length         = 8;
198                 var->green.offset       = 8;
199                 var->green.length       = 8;
200                 var->blue.offset        = 0;
201                 var->blue.length        = 8;
202                 break;
203
204         default:
205                 dev_err(sfb->dev, "invalid bpp\n");
206         }
207
208         dev_dbg(sfb->dev, "%s: verified parameters\n", __func__);
209         return 0;
210 }
211
212 /**
213  * s3c_fb_calc_pixclk() - calculate the divider to create the pixel clock.
214  * @sfb: The hardware state.
215  * @pixclock: The pixel clock wanted, in picoseconds.
216  *
217  * Given the specified pixel clock, work out the necessary divider to get
218  * close to the output frequency.
219  */
220 static int s3c_fb_calc_pixclk(struct s3c_fb *sfb, unsigned int pixclk)
221 {
222         unsigned long clk = clk_get_rate(sfb->bus_clk);
223         unsigned long long tmp;
224         unsigned int result;
225
226         tmp = (unsigned long long)clk;
227         tmp *= pixclk;
228
229         do_div(tmp, 1000000000UL);
230         result = (unsigned int)tmp / 1000;
231
232         dev_dbg(sfb->dev, "pixclk=%u, clk=%lu, div=%d (%lu)\n",
233                 pixclk, clk, result, clk / result);
234
235         return result;
236 }
237
238 /**
239  * s3c_fb_align_word() - align pixel count to word boundary
240  * @bpp: The number of bits per pixel
241  * @pix: The value to be aligned.
242  *
243  * Align the given pixel count so that it will start on an 32bit word
244  * boundary.
245  */
246 static int s3c_fb_align_word(unsigned int bpp, unsigned int pix)
247 {
248         int pix_per_word;
249
250         if (bpp > 16)
251                 return pix;
252
253         pix_per_word = (8 * 32) / bpp;
254         return ALIGN(pix, pix_per_word);
255 }
256
257 /**
258  * s3c_fb_set_par() - framebuffer request to set new framebuffer state.
259  * @info: The framebuffer to change.
260  *
261  * Framebuffer layer request to set a new mode for the specified framebuffer
262  */
263 static int s3c_fb_set_par(struct fb_info *info)
264 {
265         struct fb_var_screeninfo *var = &info->var;
266         struct s3c_fb_win *win = info->par;
267         struct s3c_fb *sfb = win->parent;
268         void __iomem *regs = sfb->regs;
269         int win_no = win->index;
270         u32 osdc_data = 0;
271         u32 data;
272         u32 pagewidth;
273         int clkdiv;
274
275         dev_dbg(sfb->dev, "setting framebuffer parameters\n");
276
277         switch (var->bits_per_pixel) {
278         case 32:
279         case 24:
280         case 16:
281         case 12:
282                 info->fix.visual = FB_VISUAL_TRUECOLOR;
283                 break;
284         case 8:
285                 if (s3c_fb_win_has_palette(win_no, 8))
286                         info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
287                 else
288                         info->fix.visual = FB_VISUAL_TRUECOLOR;
289                 break;
290         case 1:
291                 info->fix.visual = FB_VISUAL_MONO01;
292                 break;
293         default:
294                 info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
295                 break;
296         }
297
298         info->fix.line_length = (var->xres_virtual * var->bits_per_pixel) / 8;
299
300         /* disable the window whilst we update it */
301         writel(0, regs + WINCON(win_no));
302
303         /* use window 0 as the basis for the lcd output timings */
304
305         if (win_no == 0) {
306                 clkdiv = s3c_fb_calc_pixclk(sfb, var->pixclock);
307
308                 data = sfb->pdata->vidcon0;
309                 data &= ~(VIDCON0_CLKVAL_F_MASK | VIDCON0_CLKDIR);
310
311                 if (clkdiv > 1)
312                         data |= VIDCON0_CLKVAL_F(clkdiv-1) | VIDCON0_CLKDIR;
313                 else
314                         data &= ~VIDCON0_CLKDIR;        /* 1:1 clock */
315
316                 /* write the timing data to the panel */
317
318                 data |= VIDCON0_ENVID | VIDCON0_ENVID_F;
319                 writel(data, regs + VIDCON0);
320
321                 data = VIDTCON0_VBPD(var->upper_margin - 1) |
322                        VIDTCON0_VFPD(var->lower_margin - 1) |
323                        VIDTCON0_VSPW(var->vsync_len - 1);
324
325                 writel(data, regs + VIDTCON0);
326
327                 data = VIDTCON1_HBPD(var->left_margin - 1) |
328                        VIDTCON1_HFPD(var->right_margin - 1) |
329                        VIDTCON1_HSPW(var->hsync_len - 1);
330
331                 writel(data, regs + VIDTCON1);
332
333                 data = VIDTCON2_LINEVAL(var->yres - 1) |
334                        VIDTCON2_HOZVAL(var->xres - 1);
335                 writel(data, regs + VIDTCON2);
336         }
337
338         /* write the buffer address */
339
340         writel(info->fix.smem_start, regs + VIDW_BUF_START(win_no));
341
342         data = info->fix.smem_start + info->fix.line_length * var->yres;
343         writel(data, regs + VIDW_BUF_END(win_no));
344
345         pagewidth = (var->xres * var->bits_per_pixel) >> 3;
346         data = VIDW_BUF_SIZE_OFFSET(info->fix.line_length - pagewidth) |
347                VIDW_BUF_SIZE_PAGEWIDTH(pagewidth);
348         writel(data, regs + VIDW_BUF_SIZE(win_no));
349
350         /* write 'OSD' registers to control position of framebuffer */
351
352         data = VIDOSDxA_TOPLEFT_X(0) | VIDOSDxA_TOPLEFT_Y(0);
353         writel(data, regs + VIDOSD_A(win_no));
354
355         data = VIDOSDxB_BOTRIGHT_X(s3c_fb_align_word(var->bits_per_pixel,
356                                                      var->xres - 1)) |
357                VIDOSDxB_BOTRIGHT_Y(var->yres - 1);
358
359         writel(data, regs + VIDOSD_B(win_no));
360
361         data = var->xres * var->yres;
362
363         osdc_data = VIDISD14C_ALPHA1_R(0xf) |
364                 VIDISD14C_ALPHA1_G(0xf) |
365                 VIDISD14C_ALPHA1_B(0xf);
366
367         if (s3c_fb_has_osd_d(win_no)) {
368                 writel(data, regs + VIDOSD_D(win_no));
369                 writel(osdc_data, regs + VIDOSD_C(win_no));
370         } else
371                 writel(data, regs + VIDOSD_C(win_no));
372
373         data = WINCONx_ENWIN;
374
375         /* note, since we have to round up the bits-per-pixel, we end up
376          * relying on the bitfield information for r/g/b/a to work out
377          * exactly which mode of operation is intended. */
378
379         switch (var->bits_per_pixel) {
380         case 1:
381                 data |= WINCON0_BPPMODE_1BPP;
382                 data |= WINCONx_BITSWP;
383                 data |= WINCONx_BURSTLEN_4WORD;
384                 break;
385         case 2:
386                 data |= WINCON0_BPPMODE_2BPP;
387                 data |= WINCONx_BITSWP;
388                 data |= WINCONx_BURSTLEN_8WORD;
389                 break;
390         case 4:
391                 data |= WINCON0_BPPMODE_4BPP;
392                 data |= WINCONx_BITSWP;
393                 data |= WINCONx_BURSTLEN_8WORD;
394                 break;
395         case 8:
396                 if (var->transp.length != 0)
397                         data |= WINCON1_BPPMODE_8BPP_1232;
398                 else
399                         data |= WINCON0_BPPMODE_8BPP_PALETTE;
400                 data |= WINCONx_BURSTLEN_8WORD;
401                 data |= WINCONx_BYTSWP;
402                 break;
403         case 16:
404                 if (var->transp.length != 0)
405                         data |= WINCON1_BPPMODE_16BPP_A1555;
406                 else
407                         data |= WINCON0_BPPMODE_16BPP_565;
408                 data |= WINCONx_HAWSWP;
409                 data |= WINCONx_BURSTLEN_16WORD;
410                 break;
411         case 24:
412         case 32:
413                 if (var->red.length == 6) {
414                         if (var->transp.length != 0)
415                                 data |= WINCON1_BPPMODE_19BPP_A1666;
416                         else
417                                 data |= WINCON1_BPPMODE_18BPP_666;
418                 } else if (var->transp.length == 1)
419                         data |= WINCON1_BPPMODE_25BPP_A1888
420                                 | WINCON1_BLD_PIX;
421                 else if (var->transp.length == 4)
422                         data |= WINCON1_BPPMODE_28BPP_A4888
423                                 | WINCON1_BLD_PIX | WINCON1_ALPHA_SEL;
424                 else
425                         data |= WINCON0_BPPMODE_24BPP_888;
426
427                 data |= WINCONx_BURSTLEN_16WORD;
428                 break;
429         }
430
431         /* It has no color key control register for window0 */
432         if (win_no > 0) {
433                 u32 keycon0_data = 0, keycon1_data = 0;
434
435                 keycon0_data = ~(WxKEYCON0_KEYBL_EN |
436                                 WxKEYCON0_KEYEN_F |
437                                 WxKEYCON0_DIRCON) | WxKEYCON0_COMPKEY(0);
438
439                 keycon1_data = WxKEYCON1_COLVAL(0xffffff);
440
441                 writel(keycon0_data, regs + WxKEYCONy(win_no-1, 0));
442                 writel(keycon1_data, regs + WxKEYCONy(win_no-1, 1));
443         }
444
445         writel(data, regs + WINCON(win_no));
446         writel(0x0, regs + WINxMAP(win_no));
447
448         return 0;
449 }
450
451 /**
452  * s3c_fb_update_palette() - set or schedule a palette update.
453  * @sfb: The hardware information.
454  * @win: The window being updated.
455  * @reg: The palette index being changed.
456  * @value: The computed palette value.
457  *
458  * Change the value of a palette register, either by directly writing to
459  * the palette (this requires the palette RAM to be disconnected from the
460  * hardware whilst this is in progress) or schedule the update for later.
461  *
462  * At the moment, since we have no VSYNC interrupt support, we simply set
463  * the palette entry directly.
464  */
465 static void s3c_fb_update_palette(struct s3c_fb *sfb,
466                                   struct s3c_fb_win *win,
467                                   unsigned int reg,
468                                   u32 value)
469 {
470         void __iomem *palreg;
471         u32 palcon;
472
473         palreg = sfb->regs + s3c_fb_pal_reg(win->index, reg);
474
475         dev_dbg(sfb->dev, "%s: win %d, reg %d (%p): %08x\n",
476                 __func__, win->index, reg, palreg, value);
477
478         win->palette_buffer[reg] = value;
479
480         palcon = readl(sfb->regs + WPALCON);
481         writel(palcon | WPALCON_PAL_UPDATE, sfb->regs + WPALCON);
482
483         if (s3c_fb_pal_is16(win->index))
484                 writew(value, palreg);
485         else
486                 writel(value, palreg);
487
488         writel(palcon, sfb->regs + WPALCON);
489 }
490
491 static inline unsigned int chan_to_field(unsigned int chan,
492                                          struct fb_bitfield *bf)
493 {
494         chan &= 0xffff;
495         chan >>= 16 - bf->length;
496         return chan << bf->offset;
497 }
498
499 /**
500  * s3c_fb_setcolreg() - framebuffer layer request to change palette.
501  * @regno: The palette index to change.
502  * @red: The red field for the palette data.
503  * @green: The green field for the palette data.
504  * @blue: The blue field for the palette data.
505  * @trans: The transparency (alpha) field for the palette data.
506  * @info: The framebuffer being changed.
507  */
508 static int s3c_fb_setcolreg(unsigned regno,
509                             unsigned red, unsigned green, unsigned blue,
510                             unsigned transp, struct fb_info *info)
511 {
512         struct s3c_fb_win *win = info->par;
513         struct s3c_fb *sfb = win->parent;
514         unsigned int val;
515
516         dev_dbg(sfb->dev, "%s: win %d: %d => rgb=%d/%d/%d\n",
517                 __func__, win->index, regno, red, green, blue);
518
519         switch (info->fix.visual) {
520         case FB_VISUAL_TRUECOLOR:
521                 /* true-colour, use pseudo-palette */
522
523                 if (regno < 16) {
524                         u32 *pal = info->pseudo_palette;
525
526                         val  = chan_to_field(red,   &info->var.red);
527                         val |= chan_to_field(green, &info->var.green);
528                         val |= chan_to_field(blue,  &info->var.blue);
529
530                         pal[regno] = val;
531                 }
532                 break;
533
534         case FB_VISUAL_PSEUDOCOLOR:
535                 if (regno < s3c_fb_win_pal_size(win->index)) {
536                         val  = chan_to_field(red, &win->palette.r);
537                         val |= chan_to_field(green, &win->palette.g);
538                         val |= chan_to_field(blue, &win->palette.b);
539
540                         s3c_fb_update_palette(sfb, win, regno, val);
541                 }
542
543                 break;
544
545         default:
546                 return 1;       /* unknown type */
547         }
548
549         return 0;
550 }
551
552 /**
553  * s3c_fb_enable() - Set the state of the main LCD output
554  * @sfb: The main framebuffer state.
555  * @enable: The state to set.
556  */
557 static void s3c_fb_enable(struct s3c_fb *sfb, int enable)
558 {
559         u32 vidcon0 = readl(sfb->regs + VIDCON0);
560
561         if (enable)
562                 vidcon0 |= VIDCON0_ENVID | VIDCON0_ENVID_F;
563         else {
564                 /* see the note in the framebuffer datasheet about
565                  * why you cannot take both of these bits down at the
566                  * same time. */
567
568                 if (!(vidcon0 & VIDCON0_ENVID))
569                         return;
570
571                 vidcon0 |= VIDCON0_ENVID;
572                 vidcon0 &= ~VIDCON0_ENVID_F;
573         }
574
575         writel(vidcon0, sfb->regs + VIDCON0);
576 }
577
578 /**
579  * s3c_fb_blank() - blank or unblank the given window
580  * @blank_mode: The blank state from FB_BLANK_*
581  * @info: The framebuffer to blank.
582  *
583  * Framebuffer layer request to change the power state.
584  */
585 static int s3c_fb_blank(int blank_mode, struct fb_info *info)
586 {
587         struct s3c_fb_win *win = info->par;
588         struct s3c_fb *sfb = win->parent;
589         unsigned int index = win->index;
590         u32 wincon;
591
592         dev_dbg(sfb->dev, "blank mode %d\n", blank_mode);
593
594         wincon = readl(sfb->regs + WINCON(index));
595
596         switch (blank_mode) {
597         case FB_BLANK_POWERDOWN:
598                 wincon &= ~WINCONx_ENWIN;
599                 sfb->enabled &= ~(1 << index);
600                 /* fall through to FB_BLANK_NORMAL */
601
602         case FB_BLANK_NORMAL:
603                 /* disable the DMA and display 0x0 (black) */
604                 writel(WINxMAP_MAP | WINxMAP_MAP_COLOUR(0x0),
605                        sfb->regs + WINxMAP(index));
606                 break;
607
608         case FB_BLANK_UNBLANK:
609                 writel(0x0, sfb->regs + WINxMAP(index));
610                 wincon |= WINCONx_ENWIN;
611                 sfb->enabled |= (1 << index);
612                 break;
613
614         case FB_BLANK_VSYNC_SUSPEND:
615         case FB_BLANK_HSYNC_SUSPEND:
616         default:
617                 return 1;
618         }
619
620         writel(wincon, sfb->regs + WINCON(index));
621
622         /* Check the enabled state to see if we need to be running the
623          * main LCD interface, as if there are no active windows then
624          * it is highly likely that we also do not need to output
625          * anything.
626          */
627
628         /* We could do something like the following code, but the current
629          * system of using framebuffer events means that we cannot make
630          * the distinction between just window 0 being inactive and all
631          * the windows being down.
632          *
633          * s3c_fb_enable(sfb, sfb->enabled ? 1 : 0);
634         */
635
636         /* we're stuck with this until we can do something about overriding
637          * the power control using the blanking event for a single fb.
638          */
639         if (index == 0)
640                 s3c_fb_enable(sfb, blank_mode != FB_BLANK_POWERDOWN ? 1 : 0);
641
642         return 0;
643 }
644
645 static struct fb_ops s3c_fb_ops = {
646         .owner          = THIS_MODULE,
647         .fb_check_var   = s3c_fb_check_var,
648         .fb_set_par     = s3c_fb_set_par,
649         .fb_blank       = s3c_fb_blank,
650         .fb_setcolreg   = s3c_fb_setcolreg,
651         .fb_fillrect    = cfb_fillrect,
652         .fb_copyarea    = cfb_copyarea,
653         .fb_imageblit   = cfb_imageblit,
654 };
655
656 /**
657  * s3c_fb_alloc_memory() - allocate display memory for framebuffer window
658  * @sfb: The base resources for the hardware.
659  * @win: The window to initialise memory for.
660  *
661  * Allocate memory for the given framebuffer.
662  */
663 static int __devinit s3c_fb_alloc_memory(struct s3c_fb *sfb,
664                                          struct s3c_fb_win *win)
665 {
666         struct s3c_fb_pd_win *windata = win->windata;
667         unsigned int real_size, virt_size, size;
668         struct fb_info *fbi = win->fbinfo;
669         dma_addr_t map_dma;
670
671         dev_dbg(sfb->dev, "allocating memory for display\n");
672
673         real_size = windata->win_mode.xres * windata->win_mode.yres;
674         virt_size = windata->virtual_x * windata->virtual_y;
675
676         dev_dbg(sfb->dev, "real_size=%u (%u.%u), virt_size=%u (%u.%u)\n",
677                 real_size, windata->win_mode.xres, windata->win_mode.yres,
678                 virt_size, windata->virtual_x, windata->virtual_y);
679
680         size = (real_size > virt_size) ? real_size : virt_size;
681         size *= (windata->max_bpp > 16) ? 32 : windata->max_bpp;
682         size /= 8;
683
684         fbi->fix.smem_len = size;
685         size = PAGE_ALIGN(size);
686
687         dev_dbg(sfb->dev, "want %u bytes for window\n", size);
688
689         fbi->screen_base = dma_alloc_writecombine(sfb->dev, size,
690                                                   &map_dma, GFP_KERNEL);
691         if (!fbi->screen_base)
692                 return -ENOMEM;
693
694         dev_dbg(sfb->dev, "mapped %x to %p\n",
695                 (unsigned int)map_dma, fbi->screen_base);
696
697         memset(fbi->screen_base, 0x0, size);
698         fbi->fix.smem_start = map_dma;
699
700         return 0;
701 }
702
703 /**
704  * s3c_fb_free_memory() - free the display memory for the given window
705  * @sfb: The base resources for the hardware.
706  * @win: The window to free the display memory for.
707  *
708  * Free the display memory allocated by s3c_fb_alloc_memory().
709  */
710 static void s3c_fb_free_memory(struct s3c_fb *sfb, struct s3c_fb_win *win)
711 {
712         struct fb_info *fbi = win->fbinfo;
713
714         dma_free_writecombine(sfb->dev, PAGE_ALIGN(fbi->fix.smem_len),
715                               fbi->screen_base, fbi->fix.smem_start);
716 }
717
718 /**
719  * s3c_fb_release_win() - release resources for a framebuffer window.
720  * @win: The window to cleanup the resources for.
721  *
722  * Release the resources that where claimed for the hardware window,
723  * such as the framebuffer instance and any memory claimed for it.
724  */
725 static void s3c_fb_release_win(struct s3c_fb *sfb, struct s3c_fb_win *win)
726 {
727         if (win->fbinfo) {
728                 unregister_framebuffer(win->fbinfo);
729                 fb_dealloc_cmap(&win->fbinfo->cmap);
730                 s3c_fb_free_memory(sfb, win);
731                 framebuffer_release(win->fbinfo);
732         }
733 }
734
735 /**
736  * s3c_fb_probe_win() - register an hardware window
737  * @sfb: The base resources for the hardware
738  * @res: Pointer to where to place the resultant window.
739  *
740  * Allocate and do the basic initialisation for one of the hardware's graphics
741  * windows.
742  */
743 static int __devinit s3c_fb_probe_win(struct s3c_fb *sfb, unsigned int win_no,
744                                       struct s3c_fb_win **res)
745 {
746         struct fb_var_screeninfo *var;
747         struct fb_videomode *initmode;
748         struct s3c_fb_pd_win *windata;
749         struct s3c_fb_win *win;
750         struct fb_info *fbinfo;
751         int palette_size;
752         int ret;
753
754         dev_dbg(sfb->dev, "probing window %d\n", win_no);
755
756         palette_size = s3c_fb_win_pal_size(win_no);
757
758         fbinfo = framebuffer_alloc(sizeof(struct s3c_fb_win) +
759                                    palette_size * sizeof(u32), sfb->dev);
760         if (!fbinfo) {
761                 dev_err(sfb->dev, "failed to allocate framebuffer\n");
762                 return -ENOENT;
763         }
764
765         windata = sfb->pdata->win[win_no];
766         initmode = &windata->win_mode;
767
768         WARN_ON(windata->max_bpp == 0);
769         WARN_ON(windata->win_mode.xres == 0);
770         WARN_ON(windata->win_mode.yres == 0);
771
772         win = fbinfo->par;
773         var = &fbinfo->var;
774         win->fbinfo = fbinfo;
775         win->parent = sfb;
776         win->windata = windata;
777         win->index = win_no;
778         win->palette_buffer = (u32 *)(win + 1);
779
780         ret = s3c_fb_alloc_memory(sfb, win);
781         if (ret) {
782                 dev_err(sfb->dev, "failed to allocate display memory\n");
783                 return ret;
784         }
785
786         /* setup the r/b/g positions for the window's palette */
787         s3c_fb_init_palette(win_no, &win->palette);
788
789         /* setup the initial video mode from the window */
790         fb_videomode_to_var(&fbinfo->var, initmode);
791
792         fbinfo->fix.type        = FB_TYPE_PACKED_PIXELS;
793         fbinfo->fix.accel       = FB_ACCEL_NONE;
794         fbinfo->var.activate    = FB_ACTIVATE_NOW;
795         fbinfo->var.vmode       = FB_VMODE_NONINTERLACED;
796         fbinfo->var.bits_per_pixel = windata->default_bpp;
797         fbinfo->fbops           = &s3c_fb_ops;
798         fbinfo->flags           = FBINFO_FLAG_DEFAULT;
799         fbinfo->pseudo_palette  = &win->pseudo_palette;
800
801         /* prepare to actually start the framebuffer */
802
803         ret = s3c_fb_check_var(&fbinfo->var, fbinfo);
804         if (ret < 0) {
805                 dev_err(sfb->dev, "check_var failed on initial video params\n");
806                 return ret;
807         }
808
809         /* create initial colour map */
810
811         ret = fb_alloc_cmap(&fbinfo->cmap, s3c_fb_win_pal_size(win_no), 1);
812         if (ret == 0)
813                 fb_set_cmap(&fbinfo->cmap, fbinfo);
814         else
815                 dev_err(sfb->dev, "failed to allocate fb cmap\n");
816
817         s3c_fb_set_par(fbinfo);
818
819         dev_dbg(sfb->dev, "about to register framebuffer\n");
820
821         /* run the check_var and set_par on our configuration. */
822
823         ret = register_framebuffer(fbinfo);
824         if (ret < 0) {
825                 dev_err(sfb->dev, "failed to register framebuffer\n");
826                 return ret;
827         }
828
829         *res = win;
830         dev_info(sfb->dev, "window %d: fb %s\n", win_no, fbinfo->fix.id);
831
832         return 0;
833 }
834
835 /**
836  * s3c_fb_clear_win() - clear hardware window registers.
837  * @sfb: The base resources for the hardware.
838  * @win: The window to process.
839  *
840  * Reset the specific window registers to a known state.
841  */
842 static void s3c_fb_clear_win(struct s3c_fb *sfb, int win)
843 {
844         void __iomem *regs = sfb->regs;
845
846         writel(0, regs + WINCON(win));
847         writel(0xffffff, regs + WxKEYCONy(win, 0));
848         writel(0xffffff, regs + WxKEYCONy(win, 1));
849
850         writel(0, regs + VIDOSD_A(win));
851         writel(0, regs + VIDOSD_B(win));
852         writel(0, regs + VIDOSD_C(win));
853 }
854
855 static int __devinit s3c_fb_probe(struct platform_device *pdev)
856 {
857         struct device *dev = &pdev->dev;
858         struct s3c_fb_platdata *pd;
859         struct s3c_fb *sfb;
860         struct resource *res;
861         int win;
862         int ret = 0;
863
864         pd = pdev->dev.platform_data;
865         if (!pd) {
866                 dev_err(dev, "no platform data specified\n");
867                 return -EINVAL;
868         }
869
870         sfb = kzalloc(sizeof(struct s3c_fb), GFP_KERNEL);
871         if (!sfb) {
872                 dev_err(dev, "no memory for framebuffers\n");
873                 return -ENOMEM;
874         }
875
876         sfb->dev = dev;
877         sfb->pdata = pd;
878
879         sfb->bus_clk = clk_get(dev, "lcd");
880         if (IS_ERR(sfb->bus_clk)) {
881                 dev_err(dev, "failed to get bus clock\n");
882                 goto err_sfb;
883         }
884
885         clk_enable(sfb->bus_clk);
886
887         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
888         if (!res) {
889                 dev_err(dev, "failed to find registers\n");
890                 ret = -ENOENT;
891                 goto err_clk;
892         }
893
894         sfb->regs_res = request_mem_region(res->start, resource_size(res),
895                                            dev_name(dev));
896         if (!sfb->regs_res) {
897                 dev_err(dev, "failed to claim register region\n");
898                 ret = -ENOENT;
899                 goto err_clk;
900         }
901
902         sfb->regs = ioremap(res->start, resource_size(res));
903         if (!sfb->regs) {
904                 dev_err(dev, "failed to map registers\n");
905                 ret = -ENXIO;
906                 goto err_req_region;
907         }
908
909         dev_dbg(dev, "got resources (regs %p), probing windows\n", sfb->regs);
910
911         /* setup gpio and output polarity controls */
912
913         pd->setup_gpio();
914
915         writel(pd->vidcon1, sfb->regs + VIDCON1);
916
917         /* zero all windows before we do anything */
918
919         for (win = 0; win < S3C_FB_MAX_WIN; win++)
920                 s3c_fb_clear_win(sfb, win);
921
922         /* we have the register setup, start allocating framebuffers */
923
924         for (win = 0; win < S3C_FB_MAX_WIN; win++) {
925                 if (!pd->win[win])
926                         continue;
927
928                 ret = s3c_fb_probe_win(sfb, win, &sfb->windows[win]);
929                 if (ret < 0) {
930                         dev_err(dev, "failed to create window %d\n", win);
931                         for (; win >= 0; win--)
932                                 s3c_fb_release_win(sfb, sfb->windows[win]);
933                         goto err_ioremap;
934                 }
935         }
936
937         platform_set_drvdata(pdev, sfb);
938
939         return 0;
940
941 err_ioremap:
942         iounmap(sfb->regs);
943
944 err_req_region:
945         release_resource(sfb->regs_res);
946         kfree(sfb->regs_res);
947
948 err_clk:
949         clk_disable(sfb->bus_clk);
950         clk_put(sfb->bus_clk);
951
952 err_sfb:
953         kfree(sfb);
954         return ret;
955 }
956
957 /**
958  * s3c_fb_remove() - Cleanup on module finalisation
959  * @pdev: The platform device we are bound to.
960  *
961  * Shutdown and then release all the resources that the driver allocated
962  * on initialisation.
963  */
964 static int __devexit s3c_fb_remove(struct platform_device *pdev)
965 {
966         struct s3c_fb *sfb = platform_get_drvdata(pdev);
967         int win;
968
969         for (win = 0; win < S3C_FB_MAX_WIN; win++)
970                 if (sfb->windows[win])
971                         s3c_fb_release_win(sfb, sfb->windows[win]);
972
973         iounmap(sfb->regs);
974
975         clk_disable(sfb->bus_clk);
976         clk_put(sfb->bus_clk);
977
978         release_resource(sfb->regs_res);
979         kfree(sfb->regs_res);
980
981         kfree(sfb);
982
983         return 0;
984 }
985
986 #ifdef CONFIG_PM
987 static int s3c_fb_suspend(struct platform_device *pdev, pm_message_t state)
988 {
989         struct s3c_fb *sfb = platform_get_drvdata(pdev);
990         struct s3c_fb_win *win;
991         int win_no;
992
993         for (win_no = S3C_FB_MAX_WIN - 1; win_no >= 0; win_no--) {
994                 win = sfb->windows[win_no];
995                 if (!win)
996                         continue;
997
998                 /* use the blank function to push into power-down */
999                 s3c_fb_blank(FB_BLANK_POWERDOWN, win->fbinfo);
1000         }
1001
1002         clk_disable(sfb->bus_clk);
1003         return 0;
1004 }
1005
1006 static int s3c_fb_resume(struct platform_device *pdev)
1007 {
1008         struct s3c_fb *sfb = platform_get_drvdata(pdev);
1009         struct s3c_fb_platdata *pd = sfb->pdata;
1010         struct s3c_fb_win *win;
1011         int win_no;
1012
1013         clk_enable(sfb->bus_clk);
1014
1015         /* setup registers */
1016         writel(pd->vidcon1, sfb->regs + VIDCON1);
1017
1018         /* zero all windows before we do anything */
1019         for (win_no = 0; win_no < S3C_FB_MAX_WIN; win_no++)
1020                 s3c_fb_clear_win(sfb, win_no);
1021
1022         /* restore framebuffers */
1023         for (win_no = 0; win_no < S3C_FB_MAX_WIN; win_no++) {
1024                 win = sfb->windows[win_no];
1025                 if (!win)
1026                         continue;
1027
1028                 dev_dbg(&pdev->dev, "resuming window %d\n", win_no);
1029                 s3c_fb_set_par(win->fbinfo);
1030         }
1031
1032         return 0;
1033 }
1034 #else
1035 #define s3c_fb_suspend NULL
1036 #define s3c_fb_resume  NULL
1037 #endif
1038
1039 static struct platform_driver s3c_fb_driver = {
1040         .probe          = s3c_fb_probe,
1041         .remove         = __devexit_p(s3c_fb_remove),
1042         .suspend        = s3c_fb_suspend,
1043         .resume         = s3c_fb_resume,
1044         .driver         = {
1045                 .name   = "s3c-fb",
1046                 .owner  = THIS_MODULE,
1047         },
1048 };
1049
1050 static int __init s3c_fb_init(void)
1051 {
1052         return platform_driver_register(&s3c_fb_driver);
1053 }
1054
1055 static void __exit s3c_fb_cleanup(void)
1056 {
1057         platform_driver_unregister(&s3c_fb_driver);
1058 }
1059
1060 module_init(s3c_fb_init);
1061 module_exit(s3c_fb_cleanup);
1062
1063 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
1064 MODULE_DESCRIPTION("Samsung S3C SoC Framebuffer driver");
1065 MODULE_LICENSE("GPL");
1066 MODULE_ALIAS("platform:s3c-fb");