1 /* linux/drivers/video/s3c-fb.c
3 * Copyright 2008 Openmoko Inc.
4 * Copyright 2008-2010 Simtec Electronics
5 * Ben Dooks <ben@simtec.co.uk>
6 * http://armlinux.simtec.co.uk/
8 * Samsung SoC Framebuffer driver
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.
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>
26 #include <plat/regs-fb-v4.h>
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.
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.
39 /* note, the previous use of <mach/regs-fb.h> to get platform specific data
40 * has been replaced by using the platform device name to pick the correct
41 * configuration data for the system.
44 #ifdef CONFIG_FB_S3C_DEBUG_REGWRITE
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 */
53 #define VALID_BPP(x) (1 << ((x) - 1))
55 #define OSD_BASE(win, variant) ((variant).osd + ((win) * (variant).osd_stride))
56 #define VIDOSD_A(win, variant) (OSD_BASE(win, variant) + 0x00)
57 #define VIDOSD_B(win, variant) (OSD_BASE(win, variant) + 0x04)
58 #define VIDOSD_C(win, variant) (OSD_BASE(win, variant) + 0x08)
59 #define VIDOSD_D(win, variant) (OSD_BASE(win, variant) + 0x0C)
62 * struct s3c_fb_variant - fb variant information
63 * @is_2443: Set if S3C2443/S3C2416 style hardware.
64 * @nr_windows: The number of windows.
65 * @vidtcon: The base for the VIDTCONx registers
66 * @wincon: The base for the WINxCON registers.
67 * @winmap: The base for the WINxMAP registers.
68 * @keycon: The abse for the WxKEYCON registers.
69 * @buf_start: Offset of buffer start registers.
70 * @buf_size: Offset of buffer size registers.
71 * @buf_end: Offset of buffer end registers.
72 * @osd: The base for the OSD registers.
73 * @palette: Address of palette memory, or 0 if none.
75 struct s3c_fb_variant {
76 unsigned int is_2443:1;
77 unsigned short nr_windows;
78 unsigned short vidtcon;
79 unsigned short wincon;
80 unsigned short winmap;
81 unsigned short keycon;
82 unsigned short buf_start;
83 unsigned short buf_end;
84 unsigned short buf_size;
86 unsigned short osd_stride;
87 unsigned short palette[S3C_FB_MAX_WIN];
91 * struct s3c_fb_win_variant
92 * @has_osd_c: Set if has OSD C register.
93 * @has_osd_d: Set if has OSD D register.
94 * @palette_sz: Size of palette in entries.
95 * @palette_16bpp: Set if palette is 16bits wide.
96 * @valid_bpp: 1 bit per BPP setting to show valid bits-per-pixel.
98 * valid_bpp bit x is set if (x+1)BPP is supported.
100 struct s3c_fb_win_variant {
101 unsigned int has_osd_c:1;
102 unsigned int has_osd_d:1;
103 unsigned int palette_16bpp:1;
104 unsigned short palette_sz;
109 * struct s3c_fb_driverdata - per-device type driver data for init time.
110 * @variant: The variant information for this driver.
111 * @win: The window information for each window.
113 struct s3c_fb_driverdata {
114 struct s3c_fb_variant variant;
115 struct s3c_fb_win_variant *win[S3C_FB_MAX_WIN];
119 * struct s3c_fb_palette - palette information
121 * @g: Green bitfield.
123 * @a: Alpha bitfield.
125 struct s3c_fb_palette {
126 struct fb_bitfield r;
127 struct fb_bitfield g;
128 struct fb_bitfield b;
129 struct fb_bitfield a;
133 * struct s3c_fb_win - per window private data for each framebuffer.
134 * @windata: The platform data supplied for the window configuration.
135 * @parent: The hardware that this window is part of.
136 * @fbinfo: Pointer pack to the framebuffer info for this window.
137 * @varint: The variant information for this window.
138 * @palette_buffer: Buffer/cache to hold palette entries.
139 * @pseudo_palette: For use in TRUECOLOUR modes for entries 0..15/
140 * @index: The window number of this window.
141 * @palette: The bitfields for changing r/g/b into a hardware palette entry.
144 struct s3c_fb_pd_win *windata;
145 struct s3c_fb *parent;
146 struct fb_info *fbinfo;
147 struct s3c_fb_palette palette;
148 struct s3c_fb_win_variant variant;
151 u32 pseudo_palette[16];
156 * struct s3c_fb - overall hardware state of the hardware
157 * @dev: The device that we bound to, for printing, etc.
158 * @regs_res: The resource we claimed for the IO registers.
159 * @bus_clk: The clk (hclk) feeding our interface and possibly pixclk.
160 * @regs: The mapped hardware registers.
161 * @variant: Variant information for this hardware.
162 * @enabled: A bitmask of enabled hardware windows.
163 * @pdata: The platform configuration data passed with the device.
164 * @windows: The hardware windows that have been claimed.
168 struct resource *regs_res;
171 struct s3c_fb_variant variant;
173 unsigned char enabled;
175 struct s3c_fb_platdata *pdata;
176 struct s3c_fb_win *windows[S3C_FB_MAX_WIN];
180 * s3c_fb_validate_win_bpp - validate the bits-per-pixel for this mode.
181 * @win: The device window.
182 * @bpp: The bit depth.
184 static bool s3c_fb_validate_win_bpp(struct s3c_fb_win *win, unsigned int bpp)
186 return win->variant.valid_bpp & VALID_BPP(bpp);
190 * s3c_fb_check_var() - framebuffer layer request to verify a given mode.
191 * @var: The screen information to verify.
192 * @info: The framebuffer device.
194 * Framebuffer layer call to verify the given information and allow us to
195 * update various information depending on the hardware capabilities.
197 static int s3c_fb_check_var(struct fb_var_screeninfo *var,
198 struct fb_info *info)
200 struct s3c_fb_win *win = info->par;
201 struct s3c_fb_pd_win *windata = win->windata;
202 struct s3c_fb *sfb = win->parent;
204 dev_dbg(sfb->dev, "checking parameters\n");
206 var->xres_virtual = max((unsigned int)windata->virtual_x, var->xres);
207 var->yres_virtual = max((unsigned int)windata->virtual_y, var->yres);
209 if (!s3c_fb_validate_win_bpp(win, var->bits_per_pixel)) {
210 dev_dbg(sfb->dev, "win %d: unsupported bpp %d\n",
211 win->index, var->bits_per_pixel);
215 /* always ensure these are zero, for drop through cases below */
216 var->transp.offset = 0;
217 var->transp.length = 0;
219 switch (var->bits_per_pixel) {
224 if (sfb->variant.palette[win->index] != 0) {
225 /* non palletised, A:1,R:2,G:3,B:2 mode */
227 var->green.offset = 2;
228 var->blue.offset = 0;
230 var->green.length = 3;
231 var->blue.length = 2;
232 var->transp.offset = 7;
233 var->transp.length = 1;
236 var->red.length = var->bits_per_pixel;
237 var->green = var->red;
238 var->blue = var->red;
243 /* 666 with one bit alpha/transparency */
244 var->transp.offset = 18;
245 var->transp.length = 1;
247 var->bits_per_pixel = 32;
250 var->red.offset = 12;
251 var->green.offset = 6;
252 var->blue.offset = 0;
254 var->green.length = 6;
255 var->blue.length = 6;
259 /* 16 bpp, 565 format */
260 var->red.offset = 11;
261 var->green.offset = 5;
262 var->blue.offset = 0;
264 var->green.length = 6;
265 var->blue.length = 5;
270 var->transp.length = var->bits_per_pixel - 24;
271 var->transp.offset = 24;
274 /* our 24bpp is unpacked, so 32bpp */
275 var->bits_per_pixel = 32;
277 var->red.offset = 16;
279 var->green.offset = 8;
280 var->green.length = 8;
281 var->blue.offset = 0;
282 var->blue.length = 8;
286 dev_err(sfb->dev, "invalid bpp\n");
289 dev_dbg(sfb->dev, "%s: verified parameters\n", __func__);
294 * s3c_fb_calc_pixclk() - calculate the divider to create the pixel clock.
295 * @sfb: The hardware state.
296 * @pixclock: The pixel clock wanted, in picoseconds.
298 * Given the specified pixel clock, work out the necessary divider to get
299 * close to the output frequency.
301 static int s3c_fb_calc_pixclk(struct s3c_fb *sfb, unsigned int pixclk)
303 unsigned long clk = clk_get_rate(sfb->bus_clk);
304 unsigned long long tmp;
307 tmp = (unsigned long long)clk;
310 do_div(tmp, 1000000000UL);
311 result = (unsigned int)tmp / 1000;
313 dev_dbg(sfb->dev, "pixclk=%u, clk=%lu, div=%d (%lu)\n",
314 pixclk, clk, result, clk / result);
320 * s3c_fb_align_word() - align pixel count to word boundary
321 * @bpp: The number of bits per pixel
322 * @pix: The value to be aligned.
324 * Align the given pixel count so that it will start on an 32bit word
327 static int s3c_fb_align_word(unsigned int bpp, unsigned int pix)
334 pix_per_word = (8 * 32) / bpp;
335 return ALIGN(pix, pix_per_word);
339 * s3c_fb_set_par() - framebuffer request to set new framebuffer state.
340 * @info: The framebuffer to change.
342 * Framebuffer layer request to set a new mode for the specified framebuffer
344 static int s3c_fb_set_par(struct fb_info *info)
346 struct fb_var_screeninfo *var = &info->var;
347 struct s3c_fb_win *win = info->par;
348 struct s3c_fb *sfb = win->parent;
349 void __iomem *regs = sfb->regs;
350 void __iomem *buf = regs;
351 int win_no = win->index;
357 dev_dbg(sfb->dev, "setting framebuffer parameters\n");
359 switch (var->bits_per_pixel) {
364 info->fix.visual = FB_VISUAL_TRUECOLOR;
367 if (win->variant.palette_sz >= 256)
368 info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
370 info->fix.visual = FB_VISUAL_TRUECOLOR;
373 info->fix.visual = FB_VISUAL_MONO01;
376 info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
380 info->fix.line_length = (var->xres_virtual * var->bits_per_pixel) / 8;
382 /* disable the window whilst we update it */
383 writel(0, regs + WINCON(win_no));
385 /* use platform specified window as the basis for the lcd timings */
387 if (win_no == sfb->pdata->default_win) {
388 clkdiv = s3c_fb_calc_pixclk(sfb, var->pixclock);
390 data = sfb->pdata->vidcon0;
391 data &= ~(VIDCON0_CLKVAL_F_MASK | VIDCON0_CLKDIR);
394 data |= VIDCON0_CLKVAL_F(clkdiv-1) | VIDCON0_CLKDIR;
396 data &= ~VIDCON0_CLKDIR; /* 1:1 clock */
398 /* write the timing data to the panel */
400 if (sfb->variant.is_2443)
403 data |= VIDCON0_ENVID | VIDCON0_ENVID_F;
404 writel(data, regs + VIDCON0);
406 data = VIDTCON0_VBPD(var->upper_margin - 1) |
407 VIDTCON0_VFPD(var->lower_margin - 1) |
408 VIDTCON0_VSPW(var->vsync_len - 1);
410 writel(data, regs + sfb->variant.vidtcon);
412 data = VIDTCON1_HBPD(var->left_margin - 1) |
413 VIDTCON1_HFPD(var->right_margin - 1) |
414 VIDTCON1_HSPW(var->hsync_len - 1);
417 writel(data, regs + sfb->variant.vidtcon + 4);
419 data = VIDTCON2_LINEVAL(var->yres - 1) |
420 VIDTCON2_HOZVAL(var->xres - 1);
421 writel(data, regs +sfb->variant.vidtcon + 8 );
424 /* write the buffer address */
426 /* start and end registers stride is 8 */
427 buf = regs + win_no * 8;
429 writel(info->fix.smem_start, buf + sfb->variant.buf_start);
431 data = info->fix.smem_start + info->fix.line_length * var->yres;
432 writel(data, buf + sfb->variant.buf_end);
434 pagewidth = (var->xres * var->bits_per_pixel) >> 3;
435 data = VIDW_BUF_SIZE_OFFSET(info->fix.line_length - pagewidth) |
436 VIDW_BUF_SIZE_PAGEWIDTH(pagewidth);
437 writel(data, regs + sfb->variant.buf_size + (win_no * 4));
439 /* write 'OSD' registers to control position of framebuffer */
441 data = VIDOSDxA_TOPLEFT_X(0) | VIDOSDxA_TOPLEFT_Y(0);
442 writel(data, regs + VIDOSD_A(win_no, sfb->variant));
444 data = VIDOSDxB_BOTRIGHT_X(s3c_fb_align_word(var->bits_per_pixel,
446 VIDOSDxB_BOTRIGHT_Y(var->yres - 1);
448 writel(data, regs + VIDOSD_B(win_no, sfb->variant));
450 data = var->xres * var->yres;
452 osdc_data = VIDISD14C_ALPHA1_R(0xf) |
453 VIDISD14C_ALPHA1_G(0xf) |
454 VIDISD14C_ALPHA1_B(0xf);
456 if (win->variant.has_osd_d) {
457 writel(data, regs + VIDOSD_D(win_no, sfb->variant));
458 writel(osdc_data, regs + VIDOSD_C(win_no, sfb->variant));
460 writel(data, regs + VIDOSD_C(win_no, sfb->variant));
462 data = WINCONx_ENWIN;
464 /* note, since we have to round up the bits-per-pixel, we end up
465 * relying on the bitfield information for r/g/b/a to work out
466 * exactly which mode of operation is intended. */
468 switch (var->bits_per_pixel) {
470 data |= WINCON0_BPPMODE_1BPP;
471 data |= WINCONx_BITSWP;
472 data |= WINCONx_BURSTLEN_4WORD;
475 data |= WINCON0_BPPMODE_2BPP;
476 data |= WINCONx_BITSWP;
477 data |= WINCONx_BURSTLEN_8WORD;
480 data |= WINCON0_BPPMODE_4BPP;
481 data |= WINCONx_BITSWP;
482 data |= WINCONx_BURSTLEN_8WORD;
485 if (var->transp.length != 0)
486 data |= WINCON1_BPPMODE_8BPP_1232;
488 data |= WINCON0_BPPMODE_8BPP_PALETTE;
489 data |= WINCONx_BURSTLEN_8WORD;
490 data |= WINCONx_BYTSWP;
493 if (var->transp.length != 0)
494 data |= WINCON1_BPPMODE_16BPP_A1555;
496 data |= WINCON0_BPPMODE_16BPP_565;
497 data |= WINCONx_HAWSWP;
498 data |= WINCONx_BURSTLEN_16WORD;
502 if (var->red.length == 6) {
503 if (var->transp.length != 0)
504 data |= WINCON1_BPPMODE_19BPP_A1666;
506 data |= WINCON1_BPPMODE_18BPP_666;
507 } else if (var->transp.length == 1)
508 data |= WINCON1_BPPMODE_25BPP_A1888
510 else if (var->transp.length == 4)
511 data |= WINCON1_BPPMODE_28BPP_A4888
512 | WINCON1_BLD_PIX | WINCON1_ALPHA_SEL;
514 data |= WINCON0_BPPMODE_24BPP_888;
516 data |= WINCONx_WSWP;
517 data |= WINCONx_BURSTLEN_16WORD;
521 /* Enable the colour keying for the window below this one */
523 u32 keycon0_data = 0, keycon1_data = 0;
524 void __iomem *keycon = regs + sfb->variant.keycon;
526 keycon0_data = ~(WxKEYCON0_KEYBL_EN |
528 WxKEYCON0_DIRCON) | WxKEYCON0_COMPKEY(0);
530 keycon1_data = WxKEYCON1_COLVAL(0xffffff);
532 keycon += (win_no - 1) * 8;
534 writel(keycon0_data, keycon + WKEYCON0);
535 writel(keycon1_data, keycon + WKEYCON1);
538 writel(data, regs + sfb->variant.wincon + (win_no * 4));
539 writel(0x0, regs + sfb->variant.winmap + (win_no * 4));
545 * s3c_fb_update_palette() - set or schedule a palette update.
546 * @sfb: The hardware information.
547 * @win: The window being updated.
548 * @reg: The palette index being changed.
549 * @value: The computed palette value.
551 * Change the value of a palette register, either by directly writing to
552 * the palette (this requires the palette RAM to be disconnected from the
553 * hardware whilst this is in progress) or schedule the update for later.
555 * At the moment, since we have no VSYNC interrupt support, we simply set
556 * the palette entry directly.
558 static void s3c_fb_update_palette(struct s3c_fb *sfb,
559 struct s3c_fb_win *win,
563 void __iomem *palreg;
566 palreg = sfb->regs + sfb->variant.palette[win->index];
568 dev_dbg(sfb->dev, "%s: win %d, reg %d (%p): %08x\n",
569 __func__, win->index, reg, palreg, value);
571 win->palette_buffer[reg] = value;
573 palcon = readl(sfb->regs + WPALCON);
574 writel(palcon | WPALCON_PAL_UPDATE, sfb->regs + WPALCON);
576 if (win->variant.palette_16bpp)
577 writew(value, palreg + (reg * 2));
579 writel(value, palreg + (reg * 4));
581 writel(palcon, sfb->regs + WPALCON);
584 static inline unsigned int chan_to_field(unsigned int chan,
585 struct fb_bitfield *bf)
588 chan >>= 16 - bf->length;
589 return chan << bf->offset;
593 * s3c_fb_setcolreg() - framebuffer layer request to change palette.
594 * @regno: The palette index to change.
595 * @red: The red field for the palette data.
596 * @green: The green field for the palette data.
597 * @blue: The blue field for the palette data.
598 * @trans: The transparency (alpha) field for the palette data.
599 * @info: The framebuffer being changed.
601 static int s3c_fb_setcolreg(unsigned regno,
602 unsigned red, unsigned green, unsigned blue,
603 unsigned transp, struct fb_info *info)
605 struct s3c_fb_win *win = info->par;
606 struct s3c_fb *sfb = win->parent;
609 dev_dbg(sfb->dev, "%s: win %d: %d => rgb=%d/%d/%d\n",
610 __func__, win->index, regno, red, green, blue);
612 switch (info->fix.visual) {
613 case FB_VISUAL_TRUECOLOR:
614 /* true-colour, use pseudo-palette */
617 u32 *pal = info->pseudo_palette;
619 val = chan_to_field(red, &info->var.red);
620 val |= chan_to_field(green, &info->var.green);
621 val |= chan_to_field(blue, &info->var.blue);
627 case FB_VISUAL_PSEUDOCOLOR:
628 if (regno < win->variant.palette_sz) {
629 val = chan_to_field(red, &win->palette.r);
630 val |= chan_to_field(green, &win->palette.g);
631 val |= chan_to_field(blue, &win->palette.b);
633 s3c_fb_update_palette(sfb, win, regno, val);
639 return 1; /* unknown type */
646 * s3c_fb_enable() - Set the state of the main LCD output
647 * @sfb: The main framebuffer state.
648 * @enable: The state to set.
650 static void s3c_fb_enable(struct s3c_fb *sfb, int enable)
652 u32 vidcon0 = readl(sfb->regs + VIDCON0);
655 vidcon0 |= VIDCON0_ENVID | VIDCON0_ENVID_F;
657 /* see the note in the framebuffer datasheet about
658 * why you cannot take both of these bits down at the
661 if (!(vidcon0 & VIDCON0_ENVID))
664 vidcon0 |= VIDCON0_ENVID;
665 vidcon0 &= ~VIDCON0_ENVID_F;
668 writel(vidcon0, sfb->regs + VIDCON0);
672 * s3c_fb_blank() - blank or unblank the given window
673 * @blank_mode: The blank state from FB_BLANK_*
674 * @info: The framebuffer to blank.
676 * Framebuffer layer request to change the power state.
678 static int s3c_fb_blank(int blank_mode, struct fb_info *info)
680 struct s3c_fb_win *win = info->par;
681 struct s3c_fb *sfb = win->parent;
682 unsigned int index = win->index;
685 dev_dbg(sfb->dev, "blank mode %d\n", blank_mode);
687 wincon = readl(sfb->regs + sfb->variant.wincon + (index * 4));
689 switch (blank_mode) {
690 case FB_BLANK_POWERDOWN:
691 wincon &= ~WINCONx_ENWIN;
692 sfb->enabled &= ~(1 << index);
693 /* fall through to FB_BLANK_NORMAL */
695 case FB_BLANK_NORMAL:
696 /* disable the DMA and display 0x0 (black) */
697 writel(WINxMAP_MAP | WINxMAP_MAP_COLOUR(0x0),
698 sfb->regs + sfb->variant.winmap + (index * 4));
701 case FB_BLANK_UNBLANK:
702 writel(0x0, sfb->regs + sfb->variant.winmap + (index * 4));
703 wincon |= WINCONx_ENWIN;
704 sfb->enabled |= (1 << index);
707 case FB_BLANK_VSYNC_SUSPEND:
708 case FB_BLANK_HSYNC_SUSPEND:
713 writel(wincon, sfb->regs + sfb->variant.wincon + (index * 4));
715 /* Check the enabled state to see if we need to be running the
716 * main LCD interface, as if there are no active windows then
717 * it is highly likely that we also do not need to output
721 /* We could do something like the following code, but the current
722 * system of using framebuffer events means that we cannot make
723 * the distinction between just window 0 being inactive and all
724 * the windows being down.
726 * s3c_fb_enable(sfb, sfb->enabled ? 1 : 0);
729 /* we're stuck with this until we can do something about overriding
730 * the power control using the blanking event for a single fb.
732 if (index == sfb->pdata->default_win)
733 s3c_fb_enable(sfb, blank_mode != FB_BLANK_POWERDOWN ? 1 : 0);
738 static struct fb_ops s3c_fb_ops = {
739 .owner = THIS_MODULE,
740 .fb_check_var = s3c_fb_check_var,
741 .fb_set_par = s3c_fb_set_par,
742 .fb_blank = s3c_fb_blank,
743 .fb_setcolreg = s3c_fb_setcolreg,
744 .fb_fillrect = cfb_fillrect,
745 .fb_copyarea = cfb_copyarea,
746 .fb_imageblit = cfb_imageblit,
750 * s3c_fb_alloc_memory() - allocate display memory for framebuffer window
751 * @sfb: The base resources for the hardware.
752 * @win: The window to initialise memory for.
754 * Allocate memory for the given framebuffer.
756 static int __devinit s3c_fb_alloc_memory(struct s3c_fb *sfb,
757 struct s3c_fb_win *win)
759 struct s3c_fb_pd_win *windata = win->windata;
760 unsigned int real_size, virt_size, size;
761 struct fb_info *fbi = win->fbinfo;
764 dev_dbg(sfb->dev, "allocating memory for display\n");
766 real_size = windata->win_mode.xres * windata->win_mode.yres;
767 virt_size = windata->virtual_x * windata->virtual_y;
769 dev_dbg(sfb->dev, "real_size=%u (%u.%u), virt_size=%u (%u.%u)\n",
770 real_size, windata->win_mode.xres, windata->win_mode.yres,
771 virt_size, windata->virtual_x, windata->virtual_y);
773 size = (real_size > virt_size) ? real_size : virt_size;
774 size *= (windata->max_bpp > 16) ? 32 : windata->max_bpp;
777 fbi->fix.smem_len = size;
778 size = PAGE_ALIGN(size);
780 dev_dbg(sfb->dev, "want %u bytes for window\n", size);
782 fbi->screen_base = dma_alloc_writecombine(sfb->dev, size,
783 &map_dma, GFP_KERNEL);
784 if (!fbi->screen_base)
787 dev_dbg(sfb->dev, "mapped %x to %p\n",
788 (unsigned int)map_dma, fbi->screen_base);
790 memset(fbi->screen_base, 0x0, size);
791 fbi->fix.smem_start = map_dma;
797 * s3c_fb_free_memory() - free the display memory for the given window
798 * @sfb: The base resources for the hardware.
799 * @win: The window to free the display memory for.
801 * Free the display memory allocated by s3c_fb_alloc_memory().
803 static void s3c_fb_free_memory(struct s3c_fb *sfb, struct s3c_fb_win *win)
805 struct fb_info *fbi = win->fbinfo;
807 dma_free_writecombine(sfb->dev, PAGE_ALIGN(fbi->fix.smem_len),
808 fbi->screen_base, fbi->fix.smem_start);
812 * s3c_fb_release_win() - release resources for a framebuffer window.
813 * @win: The window to cleanup the resources for.
815 * Release the resources that where claimed for the hardware window,
816 * such as the framebuffer instance and any memory claimed for it.
818 static void s3c_fb_release_win(struct s3c_fb *sfb, struct s3c_fb_win *win)
821 unregister_framebuffer(win->fbinfo);
822 fb_dealloc_cmap(&win->fbinfo->cmap);
823 s3c_fb_free_memory(sfb, win);
824 framebuffer_release(win->fbinfo);
829 * s3c_fb_probe_win() - register an hardware window
830 * @sfb: The base resources for the hardware
831 * @variant: The variant information for this window.
832 * @res: Pointer to where to place the resultant window.
834 * Allocate and do the basic initialisation for one of the hardware's graphics
837 static int __devinit s3c_fb_probe_win(struct s3c_fb *sfb, unsigned int win_no,
838 struct s3c_fb_win_variant *variant,
839 struct s3c_fb_win **res)
841 struct fb_var_screeninfo *var;
842 struct fb_videomode *initmode;
843 struct s3c_fb_pd_win *windata;
844 struct s3c_fb_win *win;
845 struct fb_info *fbinfo;
849 dev_dbg(sfb->dev, "probing window %d, variant %p\n", win_no, variant);
851 palette_size = variant->palette_sz * 4;
853 fbinfo = framebuffer_alloc(sizeof(struct s3c_fb_win) +
854 palette_size * sizeof(u32), sfb->dev);
856 dev_err(sfb->dev, "failed to allocate framebuffer\n");
860 windata = sfb->pdata->win[win_no];
861 initmode = &windata->win_mode;
863 WARN_ON(windata->max_bpp == 0);
864 WARN_ON(windata->win_mode.xres == 0);
865 WARN_ON(windata->win_mode.yres == 0);
869 win->variant = *variant;
870 win->fbinfo = fbinfo;
872 win->windata = windata;
874 win->palette_buffer = (u32 *)(win + 1);
876 ret = s3c_fb_alloc_memory(sfb, win);
878 dev_err(sfb->dev, "failed to allocate display memory\n");
882 /* setup the r/b/g positions for the window's palette */
883 if (win->variant.palette_16bpp) {
884 /* Set RGB 5:6:5 as default */
885 win->palette.r.offset = 11;
886 win->palette.r.length = 5;
887 win->palette.g.offset = 5;
888 win->palette.g.length = 6;
889 win->palette.b.offset = 0;
890 win->palette.b.length = 5;
893 /* Set 8bpp or 8bpp and 1bit alpha */
894 win->palette.r.offset = 16;
895 win->palette.r.length = 8;
896 win->palette.g.offset = 8;
897 win->palette.g.length = 8;
898 win->palette.b.offset = 0;
899 win->palette.b.length = 8;
902 /* setup the initial video mode from the window */
903 fb_videomode_to_var(&fbinfo->var, initmode);
905 fbinfo->fix.type = FB_TYPE_PACKED_PIXELS;
906 fbinfo->fix.accel = FB_ACCEL_NONE;
907 fbinfo->var.activate = FB_ACTIVATE_NOW;
908 fbinfo->var.vmode = FB_VMODE_NONINTERLACED;
909 fbinfo->var.bits_per_pixel = windata->default_bpp;
910 fbinfo->fbops = &s3c_fb_ops;
911 fbinfo->flags = FBINFO_FLAG_DEFAULT;
912 fbinfo->pseudo_palette = &win->pseudo_palette;
914 /* prepare to actually start the framebuffer */
916 ret = s3c_fb_check_var(&fbinfo->var, fbinfo);
918 dev_err(sfb->dev, "check_var failed on initial video params\n");
922 /* create initial colour map */
924 ret = fb_alloc_cmap(&fbinfo->cmap, win->variant.palette_sz, 1);
926 fb_set_cmap(&fbinfo->cmap, fbinfo);
928 dev_err(sfb->dev, "failed to allocate fb cmap\n");
930 s3c_fb_set_par(fbinfo);
932 dev_dbg(sfb->dev, "about to register framebuffer\n");
934 /* run the check_var and set_par on our configuration. */
936 ret = register_framebuffer(fbinfo);
938 dev_err(sfb->dev, "failed to register framebuffer\n");
943 dev_info(sfb->dev, "window %d: fb %s\n", win_no, fbinfo->fix.id);
949 * s3c_fb_clear_win() - clear hardware window registers.
950 * @sfb: The base resources for the hardware.
951 * @win: The window to process.
953 * Reset the specific window registers to a known state.
955 static void s3c_fb_clear_win(struct s3c_fb *sfb, int win)
957 void __iomem *regs = sfb->regs;
959 writel(0, regs + sfb->variant.wincon + (win * 4));
960 writel(0, regs + VIDOSD_A(win, sfb->variant));
961 writel(0, regs + VIDOSD_B(win, sfb->variant));
962 writel(0, regs + VIDOSD_C(win, sfb->variant));
965 static int __devinit s3c_fb_probe(struct platform_device *pdev)
967 struct s3c_fb_driverdata *fbdrv;
968 struct device *dev = &pdev->dev;
969 struct s3c_fb_platdata *pd;
971 struct resource *res;
975 fbdrv = (struct s3c_fb_driverdata *)platform_get_device_id(pdev)->driver_data;
977 if (fbdrv->variant.nr_windows > S3C_FB_MAX_WIN) {
978 dev_err(dev, "too many windows, cannot attach\n");
982 pd = pdev->dev.platform_data;
984 dev_err(dev, "no platform data specified\n");
988 sfb = kzalloc(sizeof(struct s3c_fb), GFP_KERNEL);
990 dev_err(dev, "no memory for framebuffers\n");
994 dev_dbg(dev, "allocate new framebuffer %p\n", sfb);
998 sfb->variant = fbdrv->variant;
1000 sfb->bus_clk = clk_get(dev, "lcd");
1001 if (IS_ERR(sfb->bus_clk)) {
1002 dev_err(dev, "failed to get bus clock\n");
1006 clk_enable(sfb->bus_clk);
1008 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1010 dev_err(dev, "failed to find registers\n");
1015 sfb->regs_res = request_mem_region(res->start, resource_size(res),
1017 if (!sfb->regs_res) {
1018 dev_err(dev, "failed to claim register region\n");
1023 sfb->regs = ioremap(res->start, resource_size(res));
1025 dev_err(dev, "failed to map registers\n");
1027 goto err_req_region;
1030 dev_dbg(dev, "got resources (regs %p), probing windows\n", sfb->regs);
1032 /* setup gpio and output polarity controls */
1036 writel(pd->vidcon1, sfb->regs + VIDCON1);
1038 /* zero all windows before we do anything */
1040 for (win = 0; win < fbdrv->variant.nr_windows; win++)
1041 s3c_fb_clear_win(sfb, win);
1043 /* initialise colour key controls */
1044 for (win = 0; win < (fbdrv->variant.nr_windows - 1); win++) {
1045 void __iomem *regs = sfb->regs + sfb->variant.keycon;
1048 writel(0xffffff, regs + WKEYCON0);
1049 writel(0xffffff, regs + WKEYCON1);
1052 /* we have the register setup, start allocating framebuffers */
1054 for (win = 0; win < fbdrv->variant.nr_windows; win++) {
1058 ret = s3c_fb_probe_win(sfb, win, fbdrv->win[win],
1059 &sfb->windows[win]);
1061 dev_err(dev, "failed to create window %d\n", win);
1062 for (; win >= 0; win--)
1063 s3c_fb_release_win(sfb, sfb->windows[win]);
1068 platform_set_drvdata(pdev, sfb);
1076 release_resource(sfb->regs_res);
1077 kfree(sfb->regs_res);
1080 clk_disable(sfb->bus_clk);
1081 clk_put(sfb->bus_clk);
1089 * s3c_fb_remove() - Cleanup on module finalisation
1090 * @pdev: The platform device we are bound to.
1092 * Shutdown and then release all the resources that the driver allocated
1093 * on initialisation.
1095 static int __devexit s3c_fb_remove(struct platform_device *pdev)
1097 struct s3c_fb *sfb = platform_get_drvdata(pdev);
1100 for (win = 0; win < S3C_FB_MAX_WIN; win++)
1101 if (sfb->windows[win])
1102 s3c_fb_release_win(sfb, sfb->windows[win]);
1106 clk_disable(sfb->bus_clk);
1107 clk_put(sfb->bus_clk);
1109 release_resource(sfb->regs_res);
1110 kfree(sfb->regs_res);
1118 static int s3c_fb_suspend(struct platform_device *pdev, pm_message_t state)
1120 struct s3c_fb *sfb = platform_get_drvdata(pdev);
1121 struct s3c_fb_win *win;
1124 for (win_no = S3C_FB_MAX_WIN - 1; win_no >= 0; win_no--) {
1125 win = sfb->windows[win_no];
1129 /* use the blank function to push into power-down */
1130 s3c_fb_blank(FB_BLANK_POWERDOWN, win->fbinfo);
1133 clk_disable(sfb->bus_clk);
1137 static int s3c_fb_resume(struct platform_device *pdev)
1139 struct s3c_fb *sfb = platform_get_drvdata(pdev);
1140 struct s3c_fb_platdata *pd = sfb->pdata;
1141 struct s3c_fb_win *win;
1144 clk_enable(sfb->bus_clk);
1146 /* setup registers */
1147 writel(pd->vidcon1, sfb->regs + VIDCON1);
1149 /* zero all windows before we do anything */
1150 for (win_no = 0; win_no < sfb->variant.nr_windows; win_no++)
1151 s3c_fb_clear_win(sfb, win_no);
1153 for (win_no = 0; win_no < sfb->variant.nr_windows - 1; win_no++) {
1154 void __iomem *regs = sfb->regs + sfb->variant.keycon;
1156 regs += (win_no * 8);
1157 writel(0xffffff, regs + WKEYCON0);
1158 writel(0xffffff, regs + WKEYCON1);
1161 /* restore framebuffers */
1162 for (win_no = 0; win_no < S3C_FB_MAX_WIN; win_no++) {
1163 win = sfb->windows[win_no];
1167 dev_dbg(&pdev->dev, "resuming window %d\n", win_no);
1168 s3c_fb_set_par(win->fbinfo);
1174 #define s3c_fb_suspend NULL
1175 #define s3c_fb_resume NULL
1179 #define VALID_BPP124 (VALID_BPP(1) | VALID_BPP(2) | VALID_BPP(4))
1180 #define VALID_BPP1248 (VALID_BPP124 | VALID_BPP(8))
1182 static struct s3c_fb_win_variant s3c_fb_data_64xx_wins[] __devinitdata = {
1186 .valid_bpp = VALID_BPP1248 | VALID_BPP(16) | VALID_BPP(24),
1192 .valid_bpp = (VALID_BPP1248 | VALID_BPP(16) |
1193 VALID_BPP(18) | VALID_BPP(19) |
1194 VALID_BPP(24) | VALID_BPP(25)),
1201 .valid_bpp = (VALID_BPP1248 | VALID_BPP(16) |
1202 VALID_BPP(18) | VALID_BPP(19) |
1203 VALID_BPP(24) | VALID_BPP(25)),
1210 .valid_bpp = (VALID_BPP124 | VALID_BPP(16) |
1211 VALID_BPP(18) | VALID_BPP(19) |
1212 VALID_BPP(24) | VALID_BPP(25)),
1218 .valid_bpp = (VALID_BPP(1) | VALID_BPP(2) |
1219 VALID_BPP(16) | VALID_BPP(18) |
1220 VALID_BPP(24) | VALID_BPP(25)),
1224 static struct s3c_fb_driverdata s3c_fb_data_64xx __devinitdata = {
1227 .vidtcon = VIDTCON0,
1228 .wincon = WINCON(0),
1229 .winmap = WINxMAP(0),
1233 .buf_start = VIDW_BUF_START(0),
1234 .buf_size = VIDW_BUF_SIZE(0),
1235 .buf_end = VIDW_BUF_END(0),
1245 .win[0] = &s3c_fb_data_64xx_wins[0],
1246 .win[1] = &s3c_fb_data_64xx_wins[1],
1247 .win[2] = &s3c_fb_data_64xx_wins[2],
1248 .win[3] = &s3c_fb_data_64xx_wins[3],
1249 .win[4] = &s3c_fb_data_64xx_wins[4],
1252 static struct s3c_fb_driverdata s3c_fb_data_s5p __devinitdata = {
1255 .vidtcon = VIDTCON0,
1256 .wincon = WINCON(0),
1257 .winmap = WINxMAP(0),
1261 .buf_start = VIDW_BUF_START(0),
1262 .buf_size = VIDW_BUF_SIZE(0),
1263 .buf_end = VIDW_BUF_END(0),
1273 .win[0] = &s3c_fb_data_64xx_wins[0],
1274 .win[1] = &s3c_fb_data_64xx_wins[1],
1275 .win[2] = &s3c_fb_data_64xx_wins[2],
1276 .win[3] = &s3c_fb_data_64xx_wins[3],
1277 .win[4] = &s3c_fb_data_64xx_wins[4],
1280 /* S3C2443/S3C2416 style hardware */
1281 static struct s3c_fb_driverdata s3c_fb_data_s3c2443 __devinitdata = {
1301 .win[0] = &(struct s3c_fb_win_variant) {
1303 .valid_bpp = VALID_BPP1248 | VALID_BPP(16) | VALID_BPP(24),
1305 .win[1] = &(struct s3c_fb_win_variant) {
1308 .valid_bpp = (VALID_BPP1248 | VALID_BPP(16) |
1309 VALID_BPP(18) | VALID_BPP(19) |
1310 VALID_BPP(24) | VALID_BPP(25) |
1315 static struct platform_device_id s3c_fb_driver_ids[] = {
1318 .driver_data = (unsigned long)&s3c_fb_data_64xx,
1321 .driver_data = (unsigned long)&s3c_fb_data_s5p,
1323 .name = "s3c2443-fb",
1324 .driver_data = (unsigned long)&s3c_fb_data_s3c2443,
1328 MODULE_DEVICE_TABLE(platform, s3c_fb_driver_ids);
1330 static struct platform_driver s3c_fb_driver = {
1331 .probe = s3c_fb_probe,
1332 .remove = __devexit_p(s3c_fb_remove),
1333 .suspend = s3c_fb_suspend,
1334 .resume = s3c_fb_resume,
1335 .id_table = s3c_fb_driver_ids,
1338 .owner = THIS_MODULE,
1342 static int __init s3c_fb_init(void)
1344 return platform_driver_register(&s3c_fb_driver);
1347 static void __exit s3c_fb_cleanup(void)
1349 platform_driver_unregister(&s3c_fb_driver);
1352 module_init(s3c_fb_init);
1353 module_exit(s3c_fb_cleanup);
1355 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
1356 MODULE_DESCRIPTION("Samsung S3C SoC Framebuffer driver");
1357 MODULE_LICENSE("GPL");
1358 MODULE_ALIAS("platform:s3c-fb");