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_win - per window private data for each framebuffer.
120 * @windata: The platform data supplied for the window configuration.
121 * @parent: The hardware that this window is part of.
122 * @fbinfo: Pointer pack to the framebuffer info for this window.
123 * @varint: The variant information for this window.
124 * @palette_buffer: Buffer/cache to hold palette entries.
125 * @pseudo_palette: For use in TRUECOLOUR modes for entries 0..15/
126 * @index: The window number of this window.
127 * @palette: The bitfields for changing r/g/b into a hardware palette entry.
130 struct s3c_fb_pd_win *windata;
131 struct s3c_fb *parent;
132 struct fb_info *fbinfo;
133 struct s3c_fb_palette palette;
134 struct s3c_fb_win_variant variant;
137 u32 pseudo_palette[16];
142 * struct s3c_fb - overall hardware state of the hardware
143 * @dev: The device that we bound to, for printing, etc.
144 * @regs_res: The resource we claimed for the IO registers.
145 * @bus_clk: The clk (hclk) feeding our interface and possibly pixclk.
146 * @regs: The mapped hardware registers.
147 * @variant: Variant information for this hardware.
148 * @enabled: A bitmask of enabled hardware windows.
149 * @pdata: The platform configuration data passed with the device.
150 * @windows: The hardware windows that have been claimed.
154 struct resource *regs_res;
157 struct s3c_fb_variant variant;
159 unsigned char enabled;
161 struct s3c_fb_platdata *pdata;
162 struct s3c_fb_win *windows[S3C_FB_MAX_WIN];
166 * s3c_fb_validate_win_bpp - validate the bits-per-pixel for this mode.
167 * @win: The device window.
168 * @bpp: The bit depth.
170 static bool s3c_fb_validate_win_bpp(struct s3c_fb_win *win, unsigned int bpp)
172 return win->variant.valid_bpp & VALID_BPP(bpp);
176 * s3c_fb_check_var() - framebuffer layer request to verify a given mode.
177 * @var: The screen information to verify.
178 * @info: The framebuffer device.
180 * Framebuffer layer call to verify the given information and allow us to
181 * update various information depending on the hardware capabilities.
183 static int s3c_fb_check_var(struct fb_var_screeninfo *var,
184 struct fb_info *info)
186 struct s3c_fb_win *win = info->par;
187 struct s3c_fb_pd_win *windata = win->windata;
188 struct s3c_fb *sfb = win->parent;
190 dev_dbg(sfb->dev, "checking parameters\n");
192 var->xres_virtual = max((unsigned int)windata->virtual_x, var->xres);
193 var->yres_virtual = max((unsigned int)windata->virtual_y, var->yres);
195 if (!s3c_fb_validate_win_bpp(win, var->bits_per_pixel)) {
196 dev_dbg(sfb->dev, "win %d: unsupported bpp %d\n",
197 win->index, var->bits_per_pixel);
201 /* always ensure these are zero, for drop through cases below */
202 var->transp.offset = 0;
203 var->transp.length = 0;
205 switch (var->bits_per_pixel) {
210 if (sfb->variant.palette[win->index] != 0) {
211 /* non palletised, A:1,R:2,G:3,B:2 mode */
213 var->green.offset = 2;
214 var->blue.offset = 0;
216 var->green.length = 3;
217 var->blue.length = 2;
218 var->transp.offset = 7;
219 var->transp.length = 1;
222 var->red.length = var->bits_per_pixel;
223 var->green = var->red;
224 var->blue = var->red;
229 /* 666 with one bit alpha/transparency */
230 var->transp.offset = 18;
231 var->transp.length = 1;
233 var->bits_per_pixel = 32;
236 var->red.offset = 12;
237 var->green.offset = 6;
238 var->blue.offset = 0;
240 var->green.length = 6;
241 var->blue.length = 6;
245 /* 16 bpp, 565 format */
246 var->red.offset = 11;
247 var->green.offset = 5;
248 var->blue.offset = 0;
250 var->green.length = 6;
251 var->blue.length = 5;
256 var->transp.length = var->bits_per_pixel - 24;
257 var->transp.offset = 24;
260 /* our 24bpp is unpacked, so 32bpp */
261 var->bits_per_pixel = 32;
263 var->red.offset = 16;
265 var->green.offset = 8;
266 var->green.length = 8;
267 var->blue.offset = 0;
268 var->blue.length = 8;
272 dev_err(sfb->dev, "invalid bpp\n");
275 dev_dbg(sfb->dev, "%s: verified parameters\n", __func__);
280 * s3c_fb_calc_pixclk() - calculate the divider to create the pixel clock.
281 * @sfb: The hardware state.
282 * @pixclock: The pixel clock wanted, in picoseconds.
284 * Given the specified pixel clock, work out the necessary divider to get
285 * close to the output frequency.
287 static int s3c_fb_calc_pixclk(struct s3c_fb *sfb, unsigned int pixclk)
289 unsigned long clk = clk_get_rate(sfb->bus_clk);
290 unsigned long long tmp;
293 tmp = (unsigned long long)clk;
296 do_div(tmp, 1000000000UL);
297 result = (unsigned int)tmp / 1000;
299 dev_dbg(sfb->dev, "pixclk=%u, clk=%lu, div=%d (%lu)\n",
300 pixclk, clk, result, clk / result);
306 * s3c_fb_align_word() - align pixel count to word boundary
307 * @bpp: The number of bits per pixel
308 * @pix: The value to be aligned.
310 * Align the given pixel count so that it will start on an 32bit word
313 static int s3c_fb_align_word(unsigned int bpp, unsigned int pix)
320 pix_per_word = (8 * 32) / bpp;
321 return ALIGN(pix, pix_per_word);
325 * s3c_fb_set_par() - framebuffer request to set new framebuffer state.
326 * @info: The framebuffer to change.
328 * Framebuffer layer request to set a new mode for the specified framebuffer
330 static int s3c_fb_set_par(struct fb_info *info)
332 struct fb_var_screeninfo *var = &info->var;
333 struct s3c_fb_win *win = info->par;
334 struct s3c_fb *sfb = win->parent;
335 void __iomem *regs = sfb->regs;
336 void __iomem *buf = regs;
337 int win_no = win->index;
343 dev_dbg(sfb->dev, "setting framebuffer parameters\n");
345 switch (var->bits_per_pixel) {
350 info->fix.visual = FB_VISUAL_TRUECOLOR;
353 if (win->variant.palette_sz >= 256)
354 info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
356 info->fix.visual = FB_VISUAL_TRUECOLOR;
359 info->fix.visual = FB_VISUAL_MONO01;
362 info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
366 info->fix.line_length = (var->xres_virtual * var->bits_per_pixel) / 8;
368 /* disable the window whilst we update it */
369 writel(0, regs + WINCON(win_no));
371 /* use platform specified window as the basis for the lcd timings */
373 if (win_no == sfb->pdata->default_win) {
374 clkdiv = s3c_fb_calc_pixclk(sfb, var->pixclock);
376 data = sfb->pdata->vidcon0;
377 data &= ~(VIDCON0_CLKVAL_F_MASK | VIDCON0_CLKDIR);
380 data |= VIDCON0_CLKVAL_F(clkdiv-1) | VIDCON0_CLKDIR;
382 data &= ~VIDCON0_CLKDIR; /* 1:1 clock */
384 /* write the timing data to the panel */
386 if (sfb->variant.is_2443)
389 data |= VIDCON0_ENVID | VIDCON0_ENVID_F;
390 writel(data, regs + VIDCON0);
392 data = VIDTCON0_VBPD(var->upper_margin - 1) |
393 VIDTCON0_VFPD(var->lower_margin - 1) |
394 VIDTCON0_VSPW(var->vsync_len - 1);
396 writel(data, regs + sfb->variant.vidtcon);
398 data = VIDTCON1_HBPD(var->left_margin - 1) |
399 VIDTCON1_HFPD(var->right_margin - 1) |
400 VIDTCON1_HSPW(var->hsync_len - 1);
403 writel(data, regs + sfb->variant.vidtcon + 4);
405 data = VIDTCON2_LINEVAL(var->yres - 1) |
406 VIDTCON2_HOZVAL(var->xres - 1);
407 writel(data, regs +sfb->variant.vidtcon + 8 );
410 /* write the buffer address */
412 /* start and end registers stride is 8 */
413 buf = regs + win_no * 8;
415 writel(info->fix.smem_start, buf + sfb->variant.buf_start);
417 data = info->fix.smem_start + info->fix.line_length * var->yres;
418 writel(data, buf + sfb->variant.buf_end);
420 pagewidth = (var->xres * var->bits_per_pixel) >> 3;
421 data = VIDW_BUF_SIZE_OFFSET(info->fix.line_length - pagewidth) |
422 VIDW_BUF_SIZE_PAGEWIDTH(pagewidth);
423 writel(data, regs + sfb->variant.buf_size + (win_no * 4));
425 /* write 'OSD' registers to control position of framebuffer */
427 data = VIDOSDxA_TOPLEFT_X(0) | VIDOSDxA_TOPLEFT_Y(0);
428 writel(data, regs + VIDOSD_A(win_no, sfb->variant));
430 data = VIDOSDxB_BOTRIGHT_X(s3c_fb_align_word(var->bits_per_pixel,
432 VIDOSDxB_BOTRIGHT_Y(var->yres - 1);
434 writel(data, regs + VIDOSD_B(win_no, sfb->variant));
436 data = var->xres * var->yres;
438 osdc_data = VIDISD14C_ALPHA1_R(0xf) |
439 VIDISD14C_ALPHA1_G(0xf) |
440 VIDISD14C_ALPHA1_B(0xf);
442 if (win->variant.has_osd_d) {
443 writel(data, regs + VIDOSD_D(win_no, sfb->variant));
444 writel(osdc_data, regs + VIDOSD_C(win_no, sfb->variant));
446 writel(data, regs + VIDOSD_C(win_no, sfb->variant));
448 data = WINCONx_ENWIN;
450 /* note, since we have to round up the bits-per-pixel, we end up
451 * relying on the bitfield information for r/g/b/a to work out
452 * exactly which mode of operation is intended. */
454 switch (var->bits_per_pixel) {
456 data |= WINCON0_BPPMODE_1BPP;
457 data |= WINCONx_BITSWP;
458 data |= WINCONx_BURSTLEN_4WORD;
461 data |= WINCON0_BPPMODE_2BPP;
462 data |= WINCONx_BITSWP;
463 data |= WINCONx_BURSTLEN_8WORD;
466 data |= WINCON0_BPPMODE_4BPP;
467 data |= WINCONx_BITSWP;
468 data |= WINCONx_BURSTLEN_8WORD;
471 if (var->transp.length != 0)
472 data |= WINCON1_BPPMODE_8BPP_1232;
474 data |= WINCON0_BPPMODE_8BPP_PALETTE;
475 data |= WINCONx_BURSTLEN_8WORD;
476 data |= WINCONx_BYTSWP;
479 if (var->transp.length != 0)
480 data |= WINCON1_BPPMODE_16BPP_A1555;
482 data |= WINCON0_BPPMODE_16BPP_565;
483 data |= WINCONx_HAWSWP;
484 data |= WINCONx_BURSTLEN_16WORD;
488 if (var->red.length == 6) {
489 if (var->transp.length != 0)
490 data |= WINCON1_BPPMODE_19BPP_A1666;
492 data |= WINCON1_BPPMODE_18BPP_666;
493 } else if (var->transp.length == 1)
494 data |= WINCON1_BPPMODE_25BPP_A1888
496 else if (var->transp.length == 4)
497 data |= WINCON1_BPPMODE_28BPP_A4888
498 | WINCON1_BLD_PIX | WINCON1_ALPHA_SEL;
500 data |= WINCON0_BPPMODE_24BPP_888;
502 data |= WINCONx_WSWP;
503 data |= WINCONx_BURSTLEN_16WORD;
507 /* Enable the colour keying for the window below this one */
509 u32 keycon0_data = 0, keycon1_data = 0;
510 void __iomem *keycon = regs + sfb->variant.keycon;
512 keycon0_data = ~(WxKEYCON0_KEYBL_EN |
514 WxKEYCON0_DIRCON) | WxKEYCON0_COMPKEY(0);
516 keycon1_data = WxKEYCON1_COLVAL(0xffffff);
518 keycon += (win_no - 1) * 8;
520 writel(keycon0_data, keycon + WKEYCON0);
521 writel(keycon1_data, keycon + WKEYCON1);
524 writel(data, regs + sfb->variant.wincon + (win_no * 4));
525 writel(0x0, regs + sfb->variant.winmap + (win_no * 4));
531 * s3c_fb_update_palette() - set or schedule a palette update.
532 * @sfb: The hardware information.
533 * @win: The window being updated.
534 * @reg: The palette index being changed.
535 * @value: The computed palette value.
537 * Change the value of a palette register, either by directly writing to
538 * the palette (this requires the palette RAM to be disconnected from the
539 * hardware whilst this is in progress) or schedule the update for later.
541 * At the moment, since we have no VSYNC interrupt support, we simply set
542 * the palette entry directly.
544 static void s3c_fb_update_palette(struct s3c_fb *sfb,
545 struct s3c_fb_win *win,
549 void __iomem *palreg;
552 palreg = sfb->regs + sfb->variant.palette[win->index];
554 dev_dbg(sfb->dev, "%s: win %d, reg %d (%p): %08x\n",
555 __func__, win->index, reg, palreg, value);
557 win->palette_buffer[reg] = value;
559 palcon = readl(sfb->regs + WPALCON);
560 writel(palcon | WPALCON_PAL_UPDATE, sfb->regs + WPALCON);
562 if (win->variant.palette_16bpp)
563 writew(value, palreg + (reg * 2));
565 writel(value, palreg + (reg * 4));
567 writel(palcon, sfb->regs + WPALCON);
570 static inline unsigned int chan_to_field(unsigned int chan,
571 struct fb_bitfield *bf)
574 chan >>= 16 - bf->length;
575 return chan << bf->offset;
579 * s3c_fb_setcolreg() - framebuffer layer request to change palette.
580 * @regno: The palette index to change.
581 * @red: The red field for the palette data.
582 * @green: The green field for the palette data.
583 * @blue: The blue field for the palette data.
584 * @trans: The transparency (alpha) field for the palette data.
585 * @info: The framebuffer being changed.
587 static int s3c_fb_setcolreg(unsigned regno,
588 unsigned red, unsigned green, unsigned blue,
589 unsigned transp, struct fb_info *info)
591 struct s3c_fb_win *win = info->par;
592 struct s3c_fb *sfb = win->parent;
595 dev_dbg(sfb->dev, "%s: win %d: %d => rgb=%d/%d/%d\n",
596 __func__, win->index, regno, red, green, blue);
598 switch (info->fix.visual) {
599 case FB_VISUAL_TRUECOLOR:
600 /* true-colour, use pseudo-palette */
603 u32 *pal = info->pseudo_palette;
605 val = chan_to_field(red, &info->var.red);
606 val |= chan_to_field(green, &info->var.green);
607 val |= chan_to_field(blue, &info->var.blue);
613 case FB_VISUAL_PSEUDOCOLOR:
614 if (regno < win->variant.palette_sz) {
615 val = chan_to_field(red, &win->palette.r);
616 val |= chan_to_field(green, &win->palette.g);
617 val |= chan_to_field(blue, &win->palette.b);
619 s3c_fb_update_palette(sfb, win, regno, val);
625 return 1; /* unknown type */
632 * s3c_fb_enable() - Set the state of the main LCD output
633 * @sfb: The main framebuffer state.
634 * @enable: The state to set.
636 static void s3c_fb_enable(struct s3c_fb *sfb, int enable)
638 u32 vidcon0 = readl(sfb->regs + VIDCON0);
641 vidcon0 |= VIDCON0_ENVID | VIDCON0_ENVID_F;
643 /* see the note in the framebuffer datasheet about
644 * why you cannot take both of these bits down at the
647 if (!(vidcon0 & VIDCON0_ENVID))
650 vidcon0 |= VIDCON0_ENVID;
651 vidcon0 &= ~VIDCON0_ENVID_F;
654 writel(vidcon0, sfb->regs + VIDCON0);
658 * s3c_fb_blank() - blank or unblank the given window
659 * @blank_mode: The blank state from FB_BLANK_*
660 * @info: The framebuffer to blank.
662 * Framebuffer layer request to change the power state.
664 static int s3c_fb_blank(int blank_mode, struct fb_info *info)
666 struct s3c_fb_win *win = info->par;
667 struct s3c_fb *sfb = win->parent;
668 unsigned int index = win->index;
671 dev_dbg(sfb->dev, "blank mode %d\n", blank_mode);
673 wincon = readl(sfb->regs + sfb->variant.wincon + (index * 4));
675 switch (blank_mode) {
676 case FB_BLANK_POWERDOWN:
677 wincon &= ~WINCONx_ENWIN;
678 sfb->enabled &= ~(1 << index);
679 /* fall through to FB_BLANK_NORMAL */
681 case FB_BLANK_NORMAL:
682 /* disable the DMA and display 0x0 (black) */
683 writel(WINxMAP_MAP | WINxMAP_MAP_COLOUR(0x0),
684 sfb->regs + sfb->variant.winmap + (index * 4));
687 case FB_BLANK_UNBLANK:
688 writel(0x0, sfb->regs + sfb->variant.winmap + (index * 4));
689 wincon |= WINCONx_ENWIN;
690 sfb->enabled |= (1 << index);
693 case FB_BLANK_VSYNC_SUSPEND:
694 case FB_BLANK_HSYNC_SUSPEND:
699 writel(wincon, sfb->regs + sfb->variant.wincon + (index * 4));
701 /* Check the enabled state to see if we need to be running the
702 * main LCD interface, as if there are no active windows then
703 * it is highly likely that we also do not need to output
707 /* We could do something like the following code, but the current
708 * system of using framebuffer events means that we cannot make
709 * the distinction between just window 0 being inactive and all
710 * the windows being down.
712 * s3c_fb_enable(sfb, sfb->enabled ? 1 : 0);
715 /* we're stuck with this until we can do something about overriding
716 * the power control using the blanking event for a single fb.
718 if (index == sfb->pdata->default_win)
719 s3c_fb_enable(sfb, blank_mode != FB_BLANK_POWERDOWN ? 1 : 0);
724 static struct fb_ops s3c_fb_ops = {
725 .owner = THIS_MODULE,
726 .fb_check_var = s3c_fb_check_var,
727 .fb_set_par = s3c_fb_set_par,
728 .fb_blank = s3c_fb_blank,
729 .fb_setcolreg = s3c_fb_setcolreg,
730 .fb_fillrect = cfb_fillrect,
731 .fb_copyarea = cfb_copyarea,
732 .fb_imageblit = cfb_imageblit,
736 * s3c_fb_alloc_memory() - allocate display memory for framebuffer window
737 * @sfb: The base resources for the hardware.
738 * @win: The window to initialise memory for.
740 * Allocate memory for the given framebuffer.
742 static int __devinit s3c_fb_alloc_memory(struct s3c_fb *sfb,
743 struct s3c_fb_win *win)
745 struct s3c_fb_pd_win *windata = win->windata;
746 unsigned int real_size, virt_size, size;
747 struct fb_info *fbi = win->fbinfo;
750 dev_dbg(sfb->dev, "allocating memory for display\n");
752 real_size = windata->win_mode.xres * windata->win_mode.yres;
753 virt_size = windata->virtual_x * windata->virtual_y;
755 dev_dbg(sfb->dev, "real_size=%u (%u.%u), virt_size=%u (%u.%u)\n",
756 real_size, windata->win_mode.xres, windata->win_mode.yres,
757 virt_size, windata->virtual_x, windata->virtual_y);
759 size = (real_size > virt_size) ? real_size : virt_size;
760 size *= (windata->max_bpp > 16) ? 32 : windata->max_bpp;
763 fbi->fix.smem_len = size;
764 size = PAGE_ALIGN(size);
766 dev_dbg(sfb->dev, "want %u bytes for window\n", size);
768 fbi->screen_base = dma_alloc_writecombine(sfb->dev, size,
769 &map_dma, GFP_KERNEL);
770 if (!fbi->screen_base)
773 dev_dbg(sfb->dev, "mapped %x to %p\n",
774 (unsigned int)map_dma, fbi->screen_base);
776 memset(fbi->screen_base, 0x0, size);
777 fbi->fix.smem_start = map_dma;
783 * s3c_fb_free_memory() - free the display memory for the given window
784 * @sfb: The base resources for the hardware.
785 * @win: The window to free the display memory for.
787 * Free the display memory allocated by s3c_fb_alloc_memory().
789 static void s3c_fb_free_memory(struct s3c_fb *sfb, struct s3c_fb_win *win)
791 struct fb_info *fbi = win->fbinfo;
793 dma_free_writecombine(sfb->dev, PAGE_ALIGN(fbi->fix.smem_len),
794 fbi->screen_base, fbi->fix.smem_start);
798 * s3c_fb_release_win() - release resources for a framebuffer window.
799 * @win: The window to cleanup the resources for.
801 * Release the resources that where claimed for the hardware window,
802 * such as the framebuffer instance and any memory claimed for it.
804 static void s3c_fb_release_win(struct s3c_fb *sfb, struct s3c_fb_win *win)
807 unregister_framebuffer(win->fbinfo);
808 fb_dealloc_cmap(&win->fbinfo->cmap);
809 s3c_fb_free_memory(sfb, win);
810 framebuffer_release(win->fbinfo);
815 * s3c_fb_probe_win() - register an hardware window
816 * @sfb: The base resources for the hardware
817 * @variant: The variant information for this window.
818 * @res: Pointer to where to place the resultant window.
820 * Allocate and do the basic initialisation for one of the hardware's graphics
823 static int __devinit s3c_fb_probe_win(struct s3c_fb *sfb, unsigned int win_no,
824 struct s3c_fb_win_variant *variant,
825 struct s3c_fb_win **res)
827 struct fb_var_screeninfo *var;
828 struct fb_videomode *initmode;
829 struct s3c_fb_pd_win *windata;
830 struct s3c_fb_win *win;
831 struct fb_info *fbinfo;
835 dev_dbg(sfb->dev, "probing window %d, variant %p\n", win_no, variant);
837 palette_size = variant->palette_sz * 4;
839 fbinfo = framebuffer_alloc(sizeof(struct s3c_fb_win) +
840 palette_size * sizeof(u32), sfb->dev);
842 dev_err(sfb->dev, "failed to allocate framebuffer\n");
846 windata = sfb->pdata->win[win_no];
847 initmode = &windata->win_mode;
849 WARN_ON(windata->max_bpp == 0);
850 WARN_ON(windata->win_mode.xres == 0);
851 WARN_ON(windata->win_mode.yres == 0);
855 win->variant = *variant;
856 win->fbinfo = fbinfo;
858 win->windata = windata;
860 win->palette_buffer = (u32 *)(win + 1);
862 ret = s3c_fb_alloc_memory(sfb, win);
864 dev_err(sfb->dev, "failed to allocate display memory\n");
868 /* setup the r/b/g positions for the window's palette */
869 s3c_fb_init_palette(win_no, &win->palette);
871 /* setup the initial video mode from the window */
872 fb_videomode_to_var(&fbinfo->var, initmode);
874 fbinfo->fix.type = FB_TYPE_PACKED_PIXELS;
875 fbinfo->fix.accel = FB_ACCEL_NONE;
876 fbinfo->var.activate = FB_ACTIVATE_NOW;
877 fbinfo->var.vmode = FB_VMODE_NONINTERLACED;
878 fbinfo->var.bits_per_pixel = windata->default_bpp;
879 fbinfo->fbops = &s3c_fb_ops;
880 fbinfo->flags = FBINFO_FLAG_DEFAULT;
881 fbinfo->pseudo_palette = &win->pseudo_palette;
883 /* prepare to actually start the framebuffer */
885 ret = s3c_fb_check_var(&fbinfo->var, fbinfo);
887 dev_err(sfb->dev, "check_var failed on initial video params\n");
891 /* create initial colour map */
893 ret = fb_alloc_cmap(&fbinfo->cmap, win->variant.palette_sz, 1);
895 fb_set_cmap(&fbinfo->cmap, fbinfo);
897 dev_err(sfb->dev, "failed to allocate fb cmap\n");
899 s3c_fb_set_par(fbinfo);
901 dev_dbg(sfb->dev, "about to register framebuffer\n");
903 /* run the check_var and set_par on our configuration. */
905 ret = register_framebuffer(fbinfo);
907 dev_err(sfb->dev, "failed to register framebuffer\n");
912 dev_info(sfb->dev, "window %d: fb %s\n", win_no, fbinfo->fix.id);
918 * s3c_fb_clear_win() - clear hardware window registers.
919 * @sfb: The base resources for the hardware.
920 * @win: The window to process.
922 * Reset the specific window registers to a known state.
924 static void s3c_fb_clear_win(struct s3c_fb *sfb, int win)
926 void __iomem *regs = sfb->regs;
928 writel(0, regs + sfb->variant.wincon + (win * 4));
929 writel(0, regs + VIDOSD_A(win, sfb->variant));
930 writel(0, regs + VIDOSD_B(win, sfb->variant));
931 writel(0, regs + VIDOSD_C(win, sfb->variant));
934 static int __devinit s3c_fb_probe(struct platform_device *pdev)
936 struct s3c_fb_driverdata *fbdrv;
937 struct device *dev = &pdev->dev;
938 struct s3c_fb_platdata *pd;
940 struct resource *res;
944 fbdrv = (struct s3c_fb_driverdata *)platform_get_device_id(pdev)->driver_data;
946 if (fbdrv->variant.nr_windows > S3C_FB_MAX_WIN) {
947 dev_err(dev, "too many windows, cannot attach\n");
951 pd = pdev->dev.platform_data;
953 dev_err(dev, "no platform data specified\n");
957 sfb = kzalloc(sizeof(struct s3c_fb), GFP_KERNEL);
959 dev_err(dev, "no memory for framebuffers\n");
963 dev_dbg(dev, "allocate new framebuffer %p\n", sfb);
967 sfb->variant = fbdrv->variant;
969 sfb->bus_clk = clk_get(dev, "lcd");
970 if (IS_ERR(sfb->bus_clk)) {
971 dev_err(dev, "failed to get bus clock\n");
975 clk_enable(sfb->bus_clk);
977 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
979 dev_err(dev, "failed to find registers\n");
984 sfb->regs_res = request_mem_region(res->start, resource_size(res),
986 if (!sfb->regs_res) {
987 dev_err(dev, "failed to claim register region\n");
992 sfb->regs = ioremap(res->start, resource_size(res));
994 dev_err(dev, "failed to map registers\n");
999 dev_dbg(dev, "got resources (regs %p), probing windows\n", sfb->regs);
1001 /* setup gpio and output polarity controls */
1005 writel(pd->vidcon1, sfb->regs + VIDCON1);
1007 /* zero all windows before we do anything */
1009 for (win = 0; win < fbdrv->variant.nr_windows; win++)
1010 s3c_fb_clear_win(sfb, win);
1012 /* initialise colour key controls */
1013 for (win = 0; win < (fbdrv->variant.nr_windows - 1); win++) {
1014 void __iomem *regs = sfb->regs + sfb->variant.keycon;
1017 writel(0xffffff, regs + WKEYCON0);
1018 writel(0xffffff, regs + WKEYCON1);
1021 /* we have the register setup, start allocating framebuffers */
1023 for (win = 0; win < fbdrv->variant.nr_windows; win++) {
1027 ret = s3c_fb_probe_win(sfb, win, fbdrv->win[win],
1028 &sfb->windows[win]);
1030 dev_err(dev, "failed to create window %d\n", win);
1031 for (; win >= 0; win--)
1032 s3c_fb_release_win(sfb, sfb->windows[win]);
1037 platform_set_drvdata(pdev, sfb);
1045 release_resource(sfb->regs_res);
1046 kfree(sfb->regs_res);
1049 clk_disable(sfb->bus_clk);
1050 clk_put(sfb->bus_clk);
1058 * s3c_fb_remove() - Cleanup on module finalisation
1059 * @pdev: The platform device we are bound to.
1061 * Shutdown and then release all the resources that the driver allocated
1062 * on initialisation.
1064 static int __devexit s3c_fb_remove(struct platform_device *pdev)
1066 struct s3c_fb *sfb = platform_get_drvdata(pdev);
1069 for (win = 0; win < S3C_FB_MAX_WIN; win++)
1070 if (sfb->windows[win])
1071 s3c_fb_release_win(sfb, sfb->windows[win]);
1075 clk_disable(sfb->bus_clk);
1076 clk_put(sfb->bus_clk);
1078 release_resource(sfb->regs_res);
1079 kfree(sfb->regs_res);
1087 static int s3c_fb_suspend(struct platform_device *pdev, pm_message_t state)
1089 struct s3c_fb *sfb = platform_get_drvdata(pdev);
1090 struct s3c_fb_win *win;
1093 for (win_no = S3C_FB_MAX_WIN - 1; win_no >= 0; win_no--) {
1094 win = sfb->windows[win_no];
1098 /* use the blank function to push into power-down */
1099 s3c_fb_blank(FB_BLANK_POWERDOWN, win->fbinfo);
1102 clk_disable(sfb->bus_clk);
1106 static int s3c_fb_resume(struct platform_device *pdev)
1108 struct s3c_fb *sfb = platform_get_drvdata(pdev);
1109 struct s3c_fb_platdata *pd = sfb->pdata;
1110 struct s3c_fb_win *win;
1113 clk_enable(sfb->bus_clk);
1115 /* setup registers */
1116 writel(pd->vidcon1, sfb->regs + VIDCON1);
1118 /* zero all windows before we do anything */
1119 for (win_no = 0; win_no < sfb->variant.nr_windows; win_no++)
1120 s3c_fb_clear_win(sfb, win_no);
1122 for (win_no = 0; win_no < sfb->variant.nr_windows - 1; win_no++) {
1123 void __iomem *regs = sfb->regs + sfb->variant.keycon;
1125 regs += (win_no * 8);
1126 writel(0xffffff, regs + WKEYCON0);
1127 writel(0xffffff, regs + WKEYCON1);
1130 /* restore framebuffers */
1131 for (win_no = 0; win_no < S3C_FB_MAX_WIN; win_no++) {
1132 win = sfb->windows[win_no];
1136 dev_dbg(&pdev->dev, "resuming window %d\n", win_no);
1137 s3c_fb_set_par(win->fbinfo);
1143 #define s3c_fb_suspend NULL
1144 #define s3c_fb_resume NULL
1148 #define VALID_BPP124 (VALID_BPP(1) | VALID_BPP(2) | VALID_BPP(4))
1149 #define VALID_BPP1248 (VALID_BPP124 | VALID_BPP(8))
1151 static struct s3c_fb_win_variant s3c_fb_data_64xx_wins[] __devinitdata = {
1155 .valid_bpp = VALID_BPP1248 | VALID_BPP(16) | VALID_BPP(24),
1161 .valid_bpp = (VALID_BPP1248 | VALID_BPP(16) |
1162 VALID_BPP(18) | VALID_BPP(19) |
1163 VALID_BPP(24) | VALID_BPP(25)),
1170 .valid_bpp = (VALID_BPP1248 | VALID_BPP(16) |
1171 VALID_BPP(18) | VALID_BPP(19) |
1172 VALID_BPP(24) | VALID_BPP(25)),
1179 .valid_bpp = (VALID_BPP124 | VALID_BPP(16) |
1180 VALID_BPP(18) | VALID_BPP(19) |
1181 VALID_BPP(24) | VALID_BPP(25)),
1187 .valid_bpp = (VALID_BPP(1) | VALID_BPP(2) |
1188 VALID_BPP(16) | VALID_BPP(18) |
1189 VALID_BPP(24) | VALID_BPP(25)),
1193 static struct s3c_fb_driverdata s3c_fb_data_64xx __devinitdata = {
1196 .vidtcon = VIDTCON0,
1197 .wincon = WINCON(0),
1198 .winmap = WINxMAP(0),
1202 .buf_start = VIDW_BUF_START(0),
1203 .buf_size = VIDW_BUF_SIZE(0),
1204 .buf_end = VIDW_BUF_END(0),
1214 .win[0] = &s3c_fb_data_64xx_wins[0],
1215 .win[1] = &s3c_fb_data_64xx_wins[1],
1216 .win[2] = &s3c_fb_data_64xx_wins[2],
1217 .win[3] = &s3c_fb_data_64xx_wins[3],
1218 .win[4] = &s3c_fb_data_64xx_wins[4],
1221 static struct s3c_fb_driverdata s3c_fb_data_s5p __devinitdata = {
1224 .vidtcon = VIDTCON0,
1225 .wincon = WINCON(0),
1226 .winmap = WINxMAP(0),
1230 .buf_start = VIDW_BUF_START(0),
1231 .buf_size = VIDW_BUF_SIZE(0),
1232 .buf_end = VIDW_BUF_END(0),
1242 .win[0] = &s3c_fb_data_64xx_wins[0],
1243 .win[1] = &s3c_fb_data_64xx_wins[1],
1244 .win[2] = &s3c_fb_data_64xx_wins[2],
1245 .win[3] = &s3c_fb_data_64xx_wins[3],
1246 .win[4] = &s3c_fb_data_64xx_wins[4],
1249 /* S3C2443/S3C2416 style hardware */
1250 static struct s3c_fb_driverdata s3c_fb_data_s3c2443 __devinitdata = {
1270 .win[0] = &(struct s3c_fb_win_variant) {
1272 .valid_bpp = VALID_BPP1248 | VALID_BPP(16) | VALID_BPP(24),
1274 .win[1] = &(struct s3c_fb_win_variant) {
1277 .valid_bpp = (VALID_BPP1248 | VALID_BPP(16) |
1278 VALID_BPP(18) | VALID_BPP(19) |
1279 VALID_BPP(24) | VALID_BPP(25) |
1284 static struct platform_device_id s3c_fb_driver_ids[] = {
1287 .driver_data = (unsigned long)&s3c_fb_data_64xx,
1290 .driver_data = (unsigned long)&s3c_fb_data_s5p,
1292 .name = "s3c2443-fb",
1293 .driver_data = (unsigned long)&s3c_fb_data_s3c2443,
1297 MODULE_DEVICE_TABLE(platform, s3c_fb_driver_ids);
1299 static struct platform_driver s3c_fb_driver = {
1300 .probe = s3c_fb_probe,
1301 .remove = __devexit_p(s3c_fb_remove),
1302 .suspend = s3c_fb_suspend,
1303 .resume = s3c_fb_resume,
1304 .id_table = s3c_fb_driver_ids,
1307 .owner = THIS_MODULE,
1311 static int __init s3c_fb_init(void)
1313 return platform_driver_register(&s3c_fb_driver);
1316 static void __exit s3c_fb_cleanup(void)
1318 platform_driver_unregister(&s3c_fb_driver);
1321 module_init(s3c_fb_init);
1322 module_exit(s3c_fb_cleanup);
1324 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
1325 MODULE_DESCRIPTION("Samsung S3C SoC Framebuffer driver");
1326 MODULE_LICENSE("GPL");
1327 MODULE_ALIAS("platform:s3c-fb");