blob: b00c064beddf600c0c11a53b9fa65d70650e37ee [file] [log] [blame]
Ben Dooksec549a02009-03-31 15:25:39 -07001/* linux/drivers/video/s3c-fb.c
2 *
3 * Copyright 2008 Openmoko Inc.
Ben Dooks50a55032010-08-10 18:02:33 -07004 * Copyright 2008-2010 Simtec Electronics
Ben Dooksec549a02009-03-31 15:25:39 -07005 * 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
Ben Dooksc4bb6ff2010-08-10 18:02:34 -070012 * published by the Free Software FoundatIon.
Ben Dooksec549a02009-03-31 15:25:39 -070013*/
14
15#include <linux/kernel.h>
16#include <linux/module.h>
17#include <linux/platform_device.h>
18#include <linux/dma-mapping.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Ben Dooksec549a02009-03-31 15:25:39 -070020#include <linux/init.h>
Ben Dooksec549a02009-03-31 15:25:39 -070021#include <linux/clk.h>
22#include <linux/fb.h>
23#include <linux/io.h>
24
25#include <mach/map.h>
Ben Dooksc4bb6ff2010-08-10 18:02:34 -070026#include <plat/regs-fb-v4.h>
Ben Dooksec549a02009-03-31 15:25:39 -070027#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
Ben Dooks50a55032010-08-10 18:02:33 -070039/* 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.
Ben Dooksec549a02009-03-31 15:25:39 -070042*/
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
51struct s3c_fb;
52
Ben Dooks50a55032010-08-10 18:02:33 -070053#define VALID_BPP(x) (1 << ((x) - 1))
54
Ben Dooksc4bb6ff2010-08-10 18:02:34 -070055#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)
60
Ben Dooks50a55032010-08-10 18:02:33 -070061/**
62 * struct s3c_fb_variant - fb variant information
Ben Dooksc4bb6ff2010-08-10 18:02:34 -070063 * @is_2443: Set if S3C2443/S3C2416 style hardware.
Ben Dooks50a55032010-08-10 18:02:33 -070064 * @nr_windows: The number of windows.
Ben Dooksc4bb6ff2010-08-10 18:02:34 -070065 * @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.
Ben Dooks50a55032010-08-10 18:02:33 -070073 * @palette: Address of palette memory, or 0 if none.
74 */
75struct s3c_fb_variant {
Ben Dooksc4bb6ff2010-08-10 18:02:34 -070076 unsigned int is_2443:1;
Ben Dooks50a55032010-08-10 18:02:33 -070077 unsigned short nr_windows;
Ben Dooksc4bb6ff2010-08-10 18:02:34 -070078 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;
85 unsigned short osd;
86 unsigned short osd_stride;
Ben Dooks50a55032010-08-10 18:02:33 -070087 unsigned short palette[S3C_FB_MAX_WIN];
88};
89
90/**
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.
97 *
98 * valid_bpp bit x is set if (x+1)BPP is supported.
99 */
100struct 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;
105 u32 valid_bpp;
106};
107
108/**
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.
112 */
113struct s3c_fb_driverdata {
114 struct s3c_fb_variant variant;
115 struct s3c_fb_win_variant *win[S3C_FB_MAX_WIN];
116};
117
Ben Dooksec549a02009-03-31 15:25:39 -0700118/**
Ben Dooksbc2da1b2010-08-10 18:02:34 -0700119 * struct s3c_fb_palette - palette information
120 * @r: Red bitfield.
121 * @g: Green bitfield.
122 * @b: Blue bitfield.
123 * @a: Alpha bitfield.
124 */
125struct s3c_fb_palette {
126 struct fb_bitfield r;
127 struct fb_bitfield g;
128 struct fb_bitfield b;
129 struct fb_bitfield a;
130};
131
132/**
Ben Dooksec549a02009-03-31 15:25:39 -0700133 * 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.
Ben Dooks50a55032010-08-10 18:02:33 -0700137 * @varint: The variant information for this window.
Ben Dooksec549a02009-03-31 15:25:39 -0700138 * @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.
142 */
143struct s3c_fb_win {
144 struct s3c_fb_pd_win *windata;
145 struct s3c_fb *parent;
146 struct fb_info *fbinfo;
147 struct s3c_fb_palette palette;
Ben Dooks50a55032010-08-10 18:02:33 -0700148 struct s3c_fb_win_variant variant;
Ben Dooksec549a02009-03-31 15:25:39 -0700149
150 u32 *palette_buffer;
151 u32 pseudo_palette[16];
152 unsigned int index;
153};
154
155/**
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.
Ben Dooks50a55032010-08-10 18:02:33 -0700161 * @variant: Variant information for this hardware.
Ben Dooksec549a02009-03-31 15:25:39 -0700162 * @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.
165 */
166struct s3c_fb {
167 struct device *dev;
168 struct resource *regs_res;
169 struct clk *bus_clk;
170 void __iomem *regs;
Ben Dooks50a55032010-08-10 18:02:33 -0700171 struct s3c_fb_variant variant;
Ben Dooksec549a02009-03-31 15:25:39 -0700172
173 unsigned char enabled;
174
175 struct s3c_fb_platdata *pdata;
176 struct s3c_fb_win *windows[S3C_FB_MAX_WIN];
177};
178
179/**
Ben Dooks50a55032010-08-10 18:02:33 -0700180 * s3c_fb_validate_win_bpp - validate the bits-per-pixel for this mode.
181 * @win: The device window.
182 * @bpp: The bit depth.
Ben Dooksec549a02009-03-31 15:25:39 -0700183 */
Ben Dooks50a55032010-08-10 18:02:33 -0700184static bool s3c_fb_validate_win_bpp(struct s3c_fb_win *win, unsigned int bpp)
Ben Dooksec549a02009-03-31 15:25:39 -0700185{
Ben Dooks50a55032010-08-10 18:02:33 -0700186 return win->variant.valid_bpp & VALID_BPP(bpp);
Ben Dooksec549a02009-03-31 15:25:39 -0700187}
188
189/**
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.
193 *
194 * Framebuffer layer call to verify the given information and allow us to
195 * update various information depending on the hardware capabilities.
196 */
197static int s3c_fb_check_var(struct fb_var_screeninfo *var,
198 struct fb_info *info)
199{
200 struct s3c_fb_win *win = info->par;
201 struct s3c_fb_pd_win *windata = win->windata;
202 struct s3c_fb *sfb = win->parent;
203
204 dev_dbg(sfb->dev, "checking parameters\n");
205
206 var->xres_virtual = max((unsigned int)windata->virtual_x, var->xres);
207 var->yres_virtual = max((unsigned int)windata->virtual_y, var->yres);
208
Ben Dooks50a55032010-08-10 18:02:33 -0700209 if (!s3c_fb_validate_win_bpp(win, var->bits_per_pixel)) {
Ben Dooksec549a02009-03-31 15:25:39 -0700210 dev_dbg(sfb->dev, "win %d: unsupported bpp %d\n",
211 win->index, var->bits_per_pixel);
212 return -EINVAL;
213 }
214
215 /* always ensure these are zero, for drop through cases below */
216 var->transp.offset = 0;
217 var->transp.length = 0;
218
219 switch (var->bits_per_pixel) {
220 case 1:
221 case 2:
222 case 4:
223 case 8:
Ben Dooks50a55032010-08-10 18:02:33 -0700224 if (sfb->variant.palette[win->index] != 0) {
Ben Dooksec549a02009-03-31 15:25:39 -0700225 /* non palletised, A:1,R:2,G:3,B:2 mode */
226 var->red.offset = 4;
227 var->green.offset = 2;
228 var->blue.offset = 0;
229 var->red.length = 5;
230 var->green.length = 3;
231 var->blue.length = 2;
232 var->transp.offset = 7;
233 var->transp.length = 1;
234 } else {
235 var->red.offset = 0;
236 var->red.length = var->bits_per_pixel;
237 var->green = var->red;
238 var->blue = var->red;
239 }
240 break;
241
242 case 19:
243 /* 666 with one bit alpha/transparency */
244 var->transp.offset = 18;
245 var->transp.length = 1;
246 case 18:
247 var->bits_per_pixel = 32;
248
249 /* 666 format */
250 var->red.offset = 12;
251 var->green.offset = 6;
252 var->blue.offset = 0;
253 var->red.length = 6;
254 var->green.length = 6;
255 var->blue.length = 6;
256 break;
257
258 case 16:
259 /* 16 bpp, 565 format */
260 var->red.offset = 11;
261 var->green.offset = 5;
262 var->blue.offset = 0;
263 var->red.length = 5;
264 var->green.length = 6;
265 var->blue.length = 5;
266 break;
267
268 case 28:
269 case 25:
270 var->transp.length = var->bits_per_pixel - 24;
271 var->transp.offset = 24;
272 /* drop through */
273 case 24:
274 /* our 24bpp is unpacked, so 32bpp */
275 var->bits_per_pixel = 32;
276 case 32:
277 var->red.offset = 16;
278 var->red.length = 8;
279 var->green.offset = 8;
280 var->green.length = 8;
281 var->blue.offset = 0;
282 var->blue.length = 8;
283 break;
284
285 default:
286 dev_err(sfb->dev, "invalid bpp\n");
287 }
288
289 dev_dbg(sfb->dev, "%s: verified parameters\n", __func__);
290 return 0;
291}
292
293/**
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.
297 *
298 * Given the specified pixel clock, work out the necessary divider to get
299 * close to the output frequency.
300 */
Mark Browneb29a5c2010-01-15 17:01:40 -0800301static int s3c_fb_calc_pixclk(struct s3c_fb *sfb, unsigned int pixclk)
Ben Dooksec549a02009-03-31 15:25:39 -0700302{
303 unsigned long clk = clk_get_rate(sfb->bus_clk);
Mark Browneb29a5c2010-01-15 17:01:40 -0800304 unsigned long long tmp;
Ben Dooksec549a02009-03-31 15:25:39 -0700305 unsigned int result;
306
Mark Browneb29a5c2010-01-15 17:01:40 -0800307 tmp = (unsigned long long)clk;
308 tmp *= pixclk;
309
310 do_div(tmp, 1000000000UL);
311 result = (unsigned int)tmp / 1000;
Ben Dooksec549a02009-03-31 15:25:39 -0700312
313 dev_dbg(sfb->dev, "pixclk=%u, clk=%lu, div=%d (%lu)\n",
314 pixclk, clk, result, clk / result);
315
316 return result;
317}
318
319/**
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.
323 *
324 * Align the given pixel count so that it will start on an 32bit word
325 * boundary.
326 */
327static int s3c_fb_align_word(unsigned int bpp, unsigned int pix)
328{
329 int pix_per_word;
330
331 if (bpp > 16)
332 return pix;
333
334 pix_per_word = (8 * 32) / bpp;
335 return ALIGN(pix, pix_per_word);
336}
337
338/**
339 * s3c_fb_set_par() - framebuffer request to set new framebuffer state.
340 * @info: The framebuffer to change.
341 *
342 * Framebuffer layer request to set a new mode for the specified framebuffer
343 */
344static int s3c_fb_set_par(struct fb_info *info)
345{
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;
Ben Dooksc4bb6ff2010-08-10 18:02:34 -0700350 void __iomem *buf = regs;
Ben Dooksec549a02009-03-31 15:25:39 -0700351 int win_no = win->index;
InKi Dae600ce1a2009-07-05 12:08:21 -0700352 u32 osdc_data = 0;
Ben Dooksec549a02009-03-31 15:25:39 -0700353 u32 data;
354 u32 pagewidth;
355 int clkdiv;
356
357 dev_dbg(sfb->dev, "setting framebuffer parameters\n");
358
359 switch (var->bits_per_pixel) {
360 case 32:
361 case 24:
362 case 16:
363 case 12:
364 info->fix.visual = FB_VISUAL_TRUECOLOR;
365 break;
366 case 8:
Ben Dooks50a55032010-08-10 18:02:33 -0700367 if (win->variant.palette_sz >= 256)
Ben Dooksec549a02009-03-31 15:25:39 -0700368 info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
369 else
370 info->fix.visual = FB_VISUAL_TRUECOLOR;
371 break;
372 case 1:
373 info->fix.visual = FB_VISUAL_MONO01;
374 break;
375 default:
376 info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
377 break;
378 }
379
380 info->fix.line_length = (var->xres_virtual * var->bits_per_pixel) / 8;
381
382 /* disable the window whilst we update it */
383 writel(0, regs + WINCON(win_no));
384
InKi Daead044902010-08-10 18:02:31 -0700385 /* use platform specified window as the basis for the lcd timings */
Ben Dooksec549a02009-03-31 15:25:39 -0700386
InKi Daead044902010-08-10 18:02:31 -0700387 if (win_no == sfb->pdata->default_win) {
Mark Browneb29a5c2010-01-15 17:01:40 -0800388 clkdiv = s3c_fb_calc_pixclk(sfb, var->pixclock);
Ben Dooksec549a02009-03-31 15:25:39 -0700389
390 data = sfb->pdata->vidcon0;
391 data &= ~(VIDCON0_CLKVAL_F_MASK | VIDCON0_CLKDIR);
392
393 if (clkdiv > 1)
394 data |= VIDCON0_CLKVAL_F(clkdiv-1) | VIDCON0_CLKDIR;
395 else
396 data &= ~VIDCON0_CLKDIR; /* 1:1 clock */
397
398 /* write the timing data to the panel */
399
Ben Dooksc4bb6ff2010-08-10 18:02:34 -0700400 if (sfb->variant.is_2443)
401 data |= (1 << 5);
402
Ben Dooksec549a02009-03-31 15:25:39 -0700403 data |= VIDCON0_ENVID | VIDCON0_ENVID_F;
404 writel(data, regs + VIDCON0);
405
406 data = VIDTCON0_VBPD(var->upper_margin - 1) |
407 VIDTCON0_VFPD(var->lower_margin - 1) |
408 VIDTCON0_VSPW(var->vsync_len - 1);
409
Ben Dooksc4bb6ff2010-08-10 18:02:34 -0700410 writel(data, regs + sfb->variant.vidtcon);
Ben Dooksec549a02009-03-31 15:25:39 -0700411
412 data = VIDTCON1_HBPD(var->left_margin - 1) |
413 VIDTCON1_HFPD(var->right_margin - 1) |
414 VIDTCON1_HSPW(var->hsync_len - 1);
415
Ben Dooksc4bb6ff2010-08-10 18:02:34 -0700416 /* VIDTCON1 */
417 writel(data, regs + sfb->variant.vidtcon + 4);
Ben Dooksec549a02009-03-31 15:25:39 -0700418
419 data = VIDTCON2_LINEVAL(var->yres - 1) |
420 VIDTCON2_HOZVAL(var->xres - 1);
Ben Dooksc4bb6ff2010-08-10 18:02:34 -0700421 writel(data, regs +sfb->variant.vidtcon + 8 );
Ben Dooksec549a02009-03-31 15:25:39 -0700422 }
423
424 /* write the buffer address */
425
Ben Dooksc4bb6ff2010-08-10 18:02:34 -0700426 /* start and end registers stride is 8 */
427 buf = regs + win_no * 8;
428
429 writel(info->fix.smem_start, buf + sfb->variant.buf_start);
Ben Dooksec549a02009-03-31 15:25:39 -0700430
431 data = info->fix.smem_start + info->fix.line_length * var->yres;
Ben Dooksc4bb6ff2010-08-10 18:02:34 -0700432 writel(data, buf + sfb->variant.buf_end);
Ben Dooksec549a02009-03-31 15:25:39 -0700433
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);
Ben Dooksc4bb6ff2010-08-10 18:02:34 -0700437 writel(data, regs + sfb->variant.buf_size + (win_no * 4));
Ben Dooksec549a02009-03-31 15:25:39 -0700438
439 /* write 'OSD' registers to control position of framebuffer */
440
441 data = VIDOSDxA_TOPLEFT_X(0) | VIDOSDxA_TOPLEFT_Y(0);
Ben Dooksc4bb6ff2010-08-10 18:02:34 -0700442 writel(data, regs + VIDOSD_A(win_no, sfb->variant));
Ben Dooksec549a02009-03-31 15:25:39 -0700443
444 data = VIDOSDxB_BOTRIGHT_X(s3c_fb_align_word(var->bits_per_pixel,
445 var->xres - 1)) |
446 VIDOSDxB_BOTRIGHT_Y(var->yres - 1);
447
Ben Dooksc4bb6ff2010-08-10 18:02:34 -0700448 writel(data, regs + VIDOSD_B(win_no, sfb->variant));
Ben Dooksec549a02009-03-31 15:25:39 -0700449
450 data = var->xres * var->yres;
InKi Dae39000d62009-06-16 15:34:27 -0700451
InKi Dae39000d62009-06-16 15:34:27 -0700452 osdc_data = VIDISD14C_ALPHA1_R(0xf) |
453 VIDISD14C_ALPHA1_G(0xf) |
454 VIDISD14C_ALPHA1_B(0xf);
455
Ben Dooks50a55032010-08-10 18:02:33 -0700456 if (win->variant.has_osd_d) {
Ben Dooksc4bb6ff2010-08-10 18:02:34 -0700457 writel(data, regs + VIDOSD_D(win_no, sfb->variant));
458 writel(osdc_data, regs + VIDOSD_C(win_no, sfb->variant));
Ben Dooksec549a02009-03-31 15:25:39 -0700459 } else
Ben Dooksc4bb6ff2010-08-10 18:02:34 -0700460 writel(data, regs + VIDOSD_C(win_no, sfb->variant));
Ben Dooksec549a02009-03-31 15:25:39 -0700461
462 data = WINCONx_ENWIN;
463
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. */
467
468 switch (var->bits_per_pixel) {
469 case 1:
470 data |= WINCON0_BPPMODE_1BPP;
471 data |= WINCONx_BITSWP;
472 data |= WINCONx_BURSTLEN_4WORD;
473 break;
474 case 2:
475 data |= WINCON0_BPPMODE_2BPP;
476 data |= WINCONx_BITSWP;
477 data |= WINCONx_BURSTLEN_8WORD;
478 break;
479 case 4:
480 data |= WINCON0_BPPMODE_4BPP;
481 data |= WINCONx_BITSWP;
482 data |= WINCONx_BURSTLEN_8WORD;
483 break;
484 case 8:
485 if (var->transp.length != 0)
486 data |= WINCON1_BPPMODE_8BPP_1232;
487 else
488 data |= WINCON0_BPPMODE_8BPP_PALETTE;
489 data |= WINCONx_BURSTLEN_8WORD;
490 data |= WINCONx_BYTSWP;
491 break;
492 case 16:
493 if (var->transp.length != 0)
494 data |= WINCON1_BPPMODE_16BPP_A1555;
495 else
496 data |= WINCON0_BPPMODE_16BPP_565;
497 data |= WINCONx_HAWSWP;
498 data |= WINCONx_BURSTLEN_16WORD;
499 break;
500 case 24:
501 case 32:
502 if (var->red.length == 6) {
503 if (var->transp.length != 0)
504 data |= WINCON1_BPPMODE_19BPP_A1666;
505 else
506 data |= WINCON1_BPPMODE_18BPP_666;
InKi Dae39000d62009-06-16 15:34:27 -0700507 } else if (var->transp.length == 1)
508 data |= WINCON1_BPPMODE_25BPP_A1888
509 | WINCON1_BLD_PIX;
510 else if (var->transp.length == 4)
511 data |= WINCON1_BPPMODE_28BPP_A4888
512 | WINCON1_BLD_PIX | WINCON1_ALPHA_SEL;
Ben Dooksec549a02009-03-31 15:25:39 -0700513 else
514 data |= WINCON0_BPPMODE_24BPP_888;
515
InKi Daedc8498c2010-08-10 18:02:32 -0700516 data |= WINCONx_WSWP;
Ben Dooksec549a02009-03-31 15:25:39 -0700517 data |= WINCONx_BURSTLEN_16WORD;
518 break;
519 }
520
Ben Dooksc4bb6ff2010-08-10 18:02:34 -0700521 /* Enable the colour keying for the window below this one */
InKi Dae39000d62009-06-16 15:34:27 -0700522 if (win_no > 0) {
523 u32 keycon0_data = 0, keycon1_data = 0;
Ben Dooksc4bb6ff2010-08-10 18:02:34 -0700524 void __iomem *keycon = regs + sfb->variant.keycon;
InKi Dae39000d62009-06-16 15:34:27 -0700525
526 keycon0_data = ~(WxKEYCON0_KEYBL_EN |
527 WxKEYCON0_KEYEN_F |
528 WxKEYCON0_DIRCON) | WxKEYCON0_COMPKEY(0);
529
530 keycon1_data = WxKEYCON1_COLVAL(0xffffff);
531
Ben Dooksc4bb6ff2010-08-10 18:02:34 -0700532 keycon += (win_no - 1) * 8;
533
534 writel(keycon0_data, keycon + WKEYCON0);
535 writel(keycon1_data, keycon + WKEYCON1);
InKi Dae39000d62009-06-16 15:34:27 -0700536 }
537
Ben Dooksc4bb6ff2010-08-10 18:02:34 -0700538 writel(data, regs + sfb->variant.wincon + (win_no * 4));
539 writel(0x0, regs + sfb->variant.winmap + (win_no * 4));
Ben Dooksec549a02009-03-31 15:25:39 -0700540
541 return 0;
542}
543
544/**
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.
550 *
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.
554 *
555 * At the moment, since we have no VSYNC interrupt support, we simply set
556 * the palette entry directly.
557 */
558static void s3c_fb_update_palette(struct s3c_fb *sfb,
559 struct s3c_fb_win *win,
560 unsigned int reg,
561 u32 value)
562{
563 void __iomem *palreg;
564 u32 palcon;
565
Ben Dooks50a55032010-08-10 18:02:33 -0700566 palreg = sfb->regs + sfb->variant.palette[win->index];
Ben Dooksec549a02009-03-31 15:25:39 -0700567
568 dev_dbg(sfb->dev, "%s: win %d, reg %d (%p): %08x\n",
569 __func__, win->index, reg, palreg, value);
570
571 win->palette_buffer[reg] = value;
572
573 palcon = readl(sfb->regs + WPALCON);
574 writel(palcon | WPALCON_PAL_UPDATE, sfb->regs + WPALCON);
575
Ben Dooks50a55032010-08-10 18:02:33 -0700576 if (win->variant.palette_16bpp)
577 writew(value, palreg + (reg * 2));
Ben Dooksec549a02009-03-31 15:25:39 -0700578 else
Ben Dooks50a55032010-08-10 18:02:33 -0700579 writel(value, palreg + (reg * 4));
Ben Dooksec549a02009-03-31 15:25:39 -0700580
581 writel(palcon, sfb->regs + WPALCON);
582}
583
584static inline unsigned int chan_to_field(unsigned int chan,
585 struct fb_bitfield *bf)
586{
587 chan &= 0xffff;
588 chan >>= 16 - bf->length;
589 return chan << bf->offset;
590}
591
592/**
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.
600 */
601static int s3c_fb_setcolreg(unsigned regno,
602 unsigned red, unsigned green, unsigned blue,
603 unsigned transp, struct fb_info *info)
604{
605 struct s3c_fb_win *win = info->par;
606 struct s3c_fb *sfb = win->parent;
607 unsigned int val;
608
609 dev_dbg(sfb->dev, "%s: win %d: %d => rgb=%d/%d/%d\n",
610 __func__, win->index, regno, red, green, blue);
611
612 switch (info->fix.visual) {
613 case FB_VISUAL_TRUECOLOR:
614 /* true-colour, use pseudo-palette */
615
616 if (regno < 16) {
617 u32 *pal = info->pseudo_palette;
618
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);
622
623 pal[regno] = val;
624 }
625 break;
626
627 case FB_VISUAL_PSEUDOCOLOR:
Ben Dooks50a55032010-08-10 18:02:33 -0700628 if (regno < win->variant.palette_sz) {
Ben Dooksec549a02009-03-31 15:25:39 -0700629 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);
632
633 s3c_fb_update_palette(sfb, win, regno, val);
634 }
635
636 break;
637
638 default:
639 return 1; /* unknown type */
640 }
641
642 return 0;
643}
644
645/**
646 * s3c_fb_enable() - Set the state of the main LCD output
647 * @sfb: The main framebuffer state.
648 * @enable: The state to set.
649 */
650static void s3c_fb_enable(struct s3c_fb *sfb, int enable)
651{
652 u32 vidcon0 = readl(sfb->regs + VIDCON0);
653
654 if (enable)
655 vidcon0 |= VIDCON0_ENVID | VIDCON0_ENVID_F;
656 else {
657 /* see the note in the framebuffer datasheet about
658 * why you cannot take both of these bits down at the
659 * same time. */
660
661 if (!(vidcon0 & VIDCON0_ENVID))
662 return;
663
664 vidcon0 |= VIDCON0_ENVID;
665 vidcon0 &= ~VIDCON0_ENVID_F;
666 }
667
668 writel(vidcon0, sfb->regs + VIDCON0);
669}
670
671/**
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.
675 *
676 * Framebuffer layer request to change the power state.
677 */
678static int s3c_fb_blank(int blank_mode, struct fb_info *info)
679{
680 struct s3c_fb_win *win = info->par;
681 struct s3c_fb *sfb = win->parent;
682 unsigned int index = win->index;
683 u32 wincon;
684
685 dev_dbg(sfb->dev, "blank mode %d\n", blank_mode);
686
Ben Dooksc4bb6ff2010-08-10 18:02:34 -0700687 wincon = readl(sfb->regs + sfb->variant.wincon + (index * 4));
Ben Dooksec549a02009-03-31 15:25:39 -0700688
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 */
694
695 case FB_BLANK_NORMAL:
696 /* disable the DMA and display 0x0 (black) */
697 writel(WINxMAP_MAP | WINxMAP_MAP_COLOUR(0x0),
Ben Dooksc4bb6ff2010-08-10 18:02:34 -0700698 sfb->regs + sfb->variant.winmap + (index * 4));
Ben Dooksec549a02009-03-31 15:25:39 -0700699 break;
700
701 case FB_BLANK_UNBLANK:
Ben Dooksc4bb6ff2010-08-10 18:02:34 -0700702 writel(0x0, sfb->regs + sfb->variant.winmap + (index * 4));
Ben Dooksec549a02009-03-31 15:25:39 -0700703 wincon |= WINCONx_ENWIN;
704 sfb->enabled |= (1 << index);
705 break;
706
707 case FB_BLANK_VSYNC_SUSPEND:
708 case FB_BLANK_HSYNC_SUSPEND:
709 default:
710 return 1;
711 }
712
Ben Dooksc4bb6ff2010-08-10 18:02:34 -0700713 writel(wincon, sfb->regs + sfb->variant.wincon + (index * 4));
Ben Dooksec549a02009-03-31 15:25:39 -0700714
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
718 * anything.
719 */
720
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.
725 *
726 * s3c_fb_enable(sfb, sfb->enabled ? 1 : 0);
727 */
728
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.
731 */
InKi Daead044902010-08-10 18:02:31 -0700732 if (index == sfb->pdata->default_win)
Ben Dooksec549a02009-03-31 15:25:39 -0700733 s3c_fb_enable(sfb, blank_mode != FB_BLANK_POWERDOWN ? 1 : 0);
734
735 return 0;
736}
737
738static 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,
747};
748
749/**
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.
753 *
754 * Allocate memory for the given framebuffer.
755 */
756static int __devinit s3c_fb_alloc_memory(struct s3c_fb *sfb,
757 struct s3c_fb_win *win)
758{
759 struct s3c_fb_pd_win *windata = win->windata;
760 unsigned int real_size, virt_size, size;
761 struct fb_info *fbi = win->fbinfo;
762 dma_addr_t map_dma;
763
764 dev_dbg(sfb->dev, "allocating memory for display\n");
765
766 real_size = windata->win_mode.xres * windata->win_mode.yres;
767 virt_size = windata->virtual_x * windata->virtual_y;
768
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);
772
773 size = (real_size > virt_size) ? real_size : virt_size;
774 size *= (windata->max_bpp > 16) ? 32 : windata->max_bpp;
775 size /= 8;
776
777 fbi->fix.smem_len = size;
778 size = PAGE_ALIGN(size);
779
780 dev_dbg(sfb->dev, "want %u bytes for window\n", size);
781
782 fbi->screen_base = dma_alloc_writecombine(sfb->dev, size,
783 &map_dma, GFP_KERNEL);
784 if (!fbi->screen_base)
785 return -ENOMEM;
786
787 dev_dbg(sfb->dev, "mapped %x to %p\n",
788 (unsigned int)map_dma, fbi->screen_base);
789
790 memset(fbi->screen_base, 0x0, size);
791 fbi->fix.smem_start = map_dma;
792
793 return 0;
794}
795
796/**
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.
800 *
801 * Free the display memory allocated by s3c_fb_alloc_memory().
802 */
803static void s3c_fb_free_memory(struct s3c_fb *sfb, struct s3c_fb_win *win)
804{
805 struct fb_info *fbi = win->fbinfo;
806
807 dma_free_writecombine(sfb->dev, PAGE_ALIGN(fbi->fix.smem_len),
808 fbi->screen_base, fbi->fix.smem_start);
809}
810
811/**
812 * s3c_fb_release_win() - release resources for a framebuffer window.
813 * @win: The window to cleanup the resources for.
814 *
815 * Release the resources that where claimed for the hardware window,
816 * such as the framebuffer instance and any memory claimed for it.
817 */
818static void s3c_fb_release_win(struct s3c_fb *sfb, struct s3c_fb_win *win)
819{
Krzysztof Heltddc518d2009-06-16 15:34:33 -0700820 if (win->fbinfo) {
821 unregister_framebuffer(win->fbinfo);
822 fb_dealloc_cmap(&win->fbinfo->cmap);
823 s3c_fb_free_memory(sfb, win);
824 framebuffer_release(win->fbinfo);
825 }
Ben Dooksec549a02009-03-31 15:25:39 -0700826}
827
828/**
829 * s3c_fb_probe_win() - register an hardware window
830 * @sfb: The base resources for the hardware
Ben Dooks50a55032010-08-10 18:02:33 -0700831 * @variant: The variant information for this window.
Ben Dooksec549a02009-03-31 15:25:39 -0700832 * @res: Pointer to where to place the resultant window.
833 *
834 * Allocate and do the basic initialisation for one of the hardware's graphics
835 * windows.
836 */
837static int __devinit s3c_fb_probe_win(struct s3c_fb *sfb, unsigned int win_no,
Ben Dooks50a55032010-08-10 18:02:33 -0700838 struct s3c_fb_win_variant *variant,
Ben Dooksec549a02009-03-31 15:25:39 -0700839 struct s3c_fb_win **res)
840{
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;
846 int palette_size;
847 int ret;
848
Ben Dooksc4bb6ff2010-08-10 18:02:34 -0700849 dev_dbg(sfb->dev, "probing window %d, variant %p\n", win_no, variant);
Ben Dooksec549a02009-03-31 15:25:39 -0700850
Ben Dooks50a55032010-08-10 18:02:33 -0700851 palette_size = variant->palette_sz * 4;
Ben Dooksec549a02009-03-31 15:25:39 -0700852
853 fbinfo = framebuffer_alloc(sizeof(struct s3c_fb_win) +
854 palette_size * sizeof(u32), sfb->dev);
855 if (!fbinfo) {
856 dev_err(sfb->dev, "failed to allocate framebuffer\n");
857 return -ENOENT;
858 }
859
860 windata = sfb->pdata->win[win_no];
861 initmode = &windata->win_mode;
862
863 WARN_ON(windata->max_bpp == 0);
864 WARN_ON(windata->win_mode.xres == 0);
865 WARN_ON(windata->win_mode.yres == 0);
866
867 win = fbinfo->par;
868 var = &fbinfo->var;
Ben Dooks50a55032010-08-10 18:02:33 -0700869 win->variant = *variant;
Ben Dooksec549a02009-03-31 15:25:39 -0700870 win->fbinfo = fbinfo;
871 win->parent = sfb;
872 win->windata = windata;
873 win->index = win_no;
874 win->palette_buffer = (u32 *)(win + 1);
875
876 ret = s3c_fb_alloc_memory(sfb, win);
877 if (ret) {
878 dev_err(sfb->dev, "failed to allocate display memory\n");
Krzysztof Heltddc518d2009-06-16 15:34:33 -0700879 return ret;
Ben Dooksec549a02009-03-31 15:25:39 -0700880 }
881
882 /* setup the r/b/g positions for the window's palette */
Ben Dooksbc2da1b2010-08-10 18:02:34 -0700883 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;
891
892 } else {
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;
900 }
Ben Dooksec549a02009-03-31 15:25:39 -0700901
902 /* setup the initial video mode from the window */
903 fb_videomode_to_var(&fbinfo->var, initmode);
904
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;
913
914 /* prepare to actually start the framebuffer */
915
916 ret = s3c_fb_check_var(&fbinfo->var, fbinfo);
917 if (ret < 0) {
918 dev_err(sfb->dev, "check_var failed on initial video params\n");
Krzysztof Heltddc518d2009-06-16 15:34:33 -0700919 return ret;
Ben Dooksec549a02009-03-31 15:25:39 -0700920 }
921
922 /* create initial colour map */
923
Ben Dooks50a55032010-08-10 18:02:33 -0700924 ret = fb_alloc_cmap(&fbinfo->cmap, win->variant.palette_sz, 1);
Ben Dooksec549a02009-03-31 15:25:39 -0700925 if (ret == 0)
926 fb_set_cmap(&fbinfo->cmap, fbinfo);
927 else
928 dev_err(sfb->dev, "failed to allocate fb cmap\n");
929
930 s3c_fb_set_par(fbinfo);
931
932 dev_dbg(sfb->dev, "about to register framebuffer\n");
933
934 /* run the check_var and set_par on our configuration. */
935
936 ret = register_framebuffer(fbinfo);
937 if (ret < 0) {
938 dev_err(sfb->dev, "failed to register framebuffer\n");
Krzysztof Heltddc518d2009-06-16 15:34:33 -0700939 return ret;
Ben Dooksec549a02009-03-31 15:25:39 -0700940 }
941
942 *res = win;
943 dev_info(sfb->dev, "window %d: fb %s\n", win_no, fbinfo->fix.id);
944
945 return 0;
Ben Dooksec549a02009-03-31 15:25:39 -0700946}
947
948/**
949 * s3c_fb_clear_win() - clear hardware window registers.
950 * @sfb: The base resources for the hardware.
951 * @win: The window to process.
952 *
953 * Reset the specific window registers to a known state.
954 */
955static void s3c_fb_clear_win(struct s3c_fb *sfb, int win)
956{
957 void __iomem *regs = sfb->regs;
958
Ben Dooksc4bb6ff2010-08-10 18:02:34 -0700959 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));
Ben Dooksec549a02009-03-31 15:25:39 -0700963}
964
965static int __devinit s3c_fb_probe(struct platform_device *pdev)
966{
Ben Dooks50a55032010-08-10 18:02:33 -0700967 struct s3c_fb_driverdata *fbdrv;
Ben Dooksec549a02009-03-31 15:25:39 -0700968 struct device *dev = &pdev->dev;
969 struct s3c_fb_platdata *pd;
970 struct s3c_fb *sfb;
971 struct resource *res;
972 int win;
973 int ret = 0;
974
Ben Dooks50a55032010-08-10 18:02:33 -0700975 fbdrv = (struct s3c_fb_driverdata *)platform_get_device_id(pdev)->driver_data;
976
977 if (fbdrv->variant.nr_windows > S3C_FB_MAX_WIN) {
978 dev_err(dev, "too many windows, cannot attach\n");
979 return -EINVAL;
980 }
981
Ben Dooksec549a02009-03-31 15:25:39 -0700982 pd = pdev->dev.platform_data;
983 if (!pd) {
984 dev_err(dev, "no platform data specified\n");
985 return -EINVAL;
986 }
987
988 sfb = kzalloc(sizeof(struct s3c_fb), GFP_KERNEL);
989 if (!sfb) {
990 dev_err(dev, "no memory for framebuffers\n");
991 return -ENOMEM;
992 }
993
Ben Dooksc4bb6ff2010-08-10 18:02:34 -0700994 dev_dbg(dev, "allocate new framebuffer %p\n", sfb);
995
Ben Dooksec549a02009-03-31 15:25:39 -0700996 sfb->dev = dev;
997 sfb->pdata = pd;
Ben Dooks50a55032010-08-10 18:02:33 -0700998 sfb->variant = fbdrv->variant;
Ben Dooksec549a02009-03-31 15:25:39 -0700999
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");
1003 goto err_sfb;
1004 }
1005
1006 clk_enable(sfb->bus_clk);
1007
1008 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1009 if (!res) {
1010 dev_err(dev, "failed to find registers\n");
1011 ret = -ENOENT;
1012 goto err_clk;
1013 }
1014
1015 sfb->regs_res = request_mem_region(res->start, resource_size(res),
1016 dev_name(dev));
1017 if (!sfb->regs_res) {
1018 dev_err(dev, "failed to claim register region\n");
1019 ret = -ENOENT;
1020 goto err_clk;
1021 }
1022
1023 sfb->regs = ioremap(res->start, resource_size(res));
1024 if (!sfb->regs) {
1025 dev_err(dev, "failed to map registers\n");
1026 ret = -ENXIO;
1027 goto err_req_region;
1028 }
1029
1030 dev_dbg(dev, "got resources (regs %p), probing windows\n", sfb->regs);
1031
1032 /* setup gpio and output polarity controls */
1033
1034 pd->setup_gpio();
1035
1036 writel(pd->vidcon1, sfb->regs + VIDCON1);
1037
1038 /* zero all windows before we do anything */
1039
Ben Dooks50a55032010-08-10 18:02:33 -07001040 for (win = 0; win < fbdrv->variant.nr_windows; win++)
Ben Dooksec549a02009-03-31 15:25:39 -07001041 s3c_fb_clear_win(sfb, win);
1042
Ben Dooks94947032010-08-10 18:02:32 -07001043 /* initialise colour key controls */
Ben Dooks50a55032010-08-10 18:02:33 -07001044 for (win = 0; win < (fbdrv->variant.nr_windows - 1); win++) {
Ben Dooksc4bb6ff2010-08-10 18:02:34 -07001045 void __iomem *regs = sfb->regs + sfb->variant.keycon;
1046
1047 regs += (win * 8);
1048 writel(0xffffff, regs + WKEYCON0);
1049 writel(0xffffff, regs + WKEYCON1);
Ben Dooks94947032010-08-10 18:02:32 -07001050 }
1051
Ben Dooksec549a02009-03-31 15:25:39 -07001052 /* we have the register setup, start allocating framebuffers */
1053
Ben Dooks50a55032010-08-10 18:02:33 -07001054 for (win = 0; win < fbdrv->variant.nr_windows; win++) {
Ben Dooksec549a02009-03-31 15:25:39 -07001055 if (!pd->win[win])
1056 continue;
1057
Ben Dooks50a55032010-08-10 18:02:33 -07001058 ret = s3c_fb_probe_win(sfb, win, fbdrv->win[win],
1059 &sfb->windows[win]);
Ben Dooksec549a02009-03-31 15:25:39 -07001060 if (ret < 0) {
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]);
1064 goto err_ioremap;
1065 }
1066 }
1067
1068 platform_set_drvdata(pdev, sfb);
1069
1070 return 0;
1071
1072err_ioremap:
1073 iounmap(sfb->regs);
1074
1075err_req_region:
1076 release_resource(sfb->regs_res);
1077 kfree(sfb->regs_res);
1078
1079err_clk:
1080 clk_disable(sfb->bus_clk);
1081 clk_put(sfb->bus_clk);
1082
1083err_sfb:
1084 kfree(sfb);
1085 return ret;
1086}
1087
1088/**
1089 * s3c_fb_remove() - Cleanup on module finalisation
1090 * @pdev: The platform device we are bound to.
1091 *
1092 * Shutdown and then release all the resources that the driver allocated
1093 * on initialisation.
1094 */
1095static int __devexit s3c_fb_remove(struct platform_device *pdev)
1096{
1097 struct s3c_fb *sfb = platform_get_drvdata(pdev);
1098 int win;
1099
Pawel Osciakc42b1102009-07-29 15:02:10 -07001100 for (win = 0; win < S3C_FB_MAX_WIN; win++)
Marek Szyprowski17663e52009-05-28 14:34:35 -07001101 if (sfb->windows[win])
1102 s3c_fb_release_win(sfb, sfb->windows[win]);
Ben Dooksec549a02009-03-31 15:25:39 -07001103
1104 iounmap(sfb->regs);
1105
1106 clk_disable(sfb->bus_clk);
1107 clk_put(sfb->bus_clk);
1108
1109 release_resource(sfb->regs_res);
1110 kfree(sfb->regs_res);
1111
1112 kfree(sfb);
1113
1114 return 0;
1115}
1116
1117#ifdef CONFIG_PM
1118static int s3c_fb_suspend(struct platform_device *pdev, pm_message_t state)
1119{
1120 struct s3c_fb *sfb = platform_get_drvdata(pdev);
1121 struct s3c_fb_win *win;
1122 int win_no;
1123
Pawel Osciakc42b1102009-07-29 15:02:10 -07001124 for (win_no = S3C_FB_MAX_WIN - 1; win_no >= 0; win_no--) {
Ben Dooksec549a02009-03-31 15:25:39 -07001125 win = sfb->windows[win_no];
1126 if (!win)
1127 continue;
1128
1129 /* use the blank function to push into power-down */
1130 s3c_fb_blank(FB_BLANK_POWERDOWN, win->fbinfo);
1131 }
1132
1133 clk_disable(sfb->bus_clk);
1134 return 0;
1135}
1136
1137static int s3c_fb_resume(struct platform_device *pdev)
1138{
1139 struct s3c_fb *sfb = platform_get_drvdata(pdev);
Marek Szyprowski17663e52009-05-28 14:34:35 -07001140 struct s3c_fb_platdata *pd = sfb->pdata;
Ben Dooksec549a02009-03-31 15:25:39 -07001141 struct s3c_fb_win *win;
1142 int win_no;
1143
1144 clk_enable(sfb->bus_clk);
1145
Marek Szyprowski17663e52009-05-28 14:34:35 -07001146 /* setup registers */
1147 writel(pd->vidcon1, sfb->regs + VIDCON1);
1148
1149 /* zero all windows before we do anything */
Ben Dooks50a55032010-08-10 18:02:33 -07001150 for (win_no = 0; win_no < sfb->variant.nr_windows; win_no++)
Marek Szyprowski17663e52009-05-28 14:34:35 -07001151 s3c_fb_clear_win(sfb, win_no);
1152
Ben Dooks50a55032010-08-10 18:02:33 -07001153 for (win_no = 0; win_no < sfb->variant.nr_windows - 1; win_no++) {
Ben Dooksc4bb6ff2010-08-10 18:02:34 -07001154 void __iomem *regs = sfb->regs + sfb->variant.keycon;
1155
1156 regs += (win_no * 8);
1157 writel(0xffffff, regs + WKEYCON0);
1158 writel(0xffffff, regs + WKEYCON1);
Ben Dooks94947032010-08-10 18:02:32 -07001159 }
1160
Marek Szyprowski17663e52009-05-28 14:34:35 -07001161 /* restore framebuffers */
Ben Dooksec549a02009-03-31 15:25:39 -07001162 for (win_no = 0; win_no < S3C_FB_MAX_WIN; win_no++) {
1163 win = sfb->windows[win_no];
1164 if (!win)
1165 continue;
1166
1167 dev_dbg(&pdev->dev, "resuming window %d\n", win_no);
1168 s3c_fb_set_par(win->fbinfo);
1169 }
1170
1171 return 0;
1172}
1173#else
1174#define s3c_fb_suspend NULL
1175#define s3c_fb_resume NULL
1176#endif
1177
Ben Dooks50a55032010-08-10 18:02:33 -07001178
1179#define VALID_BPP124 (VALID_BPP(1) | VALID_BPP(2) | VALID_BPP(4))
1180#define VALID_BPP1248 (VALID_BPP124 | VALID_BPP(8))
1181
1182static struct s3c_fb_win_variant s3c_fb_data_64xx_wins[] __devinitdata = {
1183 [0] = {
1184 .has_osd_c = 1,
1185 .palette_sz = 256,
1186 .valid_bpp = VALID_BPP1248 | VALID_BPP(16) | VALID_BPP(24),
1187 },
1188 [1] = {
1189 .has_osd_c = 1,
1190 .has_osd_d = 1,
1191 .palette_sz = 256,
1192 .valid_bpp = (VALID_BPP1248 | VALID_BPP(16) |
1193 VALID_BPP(18) | VALID_BPP(19) |
1194 VALID_BPP(24) | VALID_BPP(25)),
1195 },
1196 [2] = {
1197 .has_osd_c = 1,
1198 .has_osd_d = 1,
1199 .palette_sz = 16,
1200 .palette_16bpp = 1,
1201 .valid_bpp = (VALID_BPP1248 | VALID_BPP(16) |
1202 VALID_BPP(18) | VALID_BPP(19) |
1203 VALID_BPP(24) | VALID_BPP(25)),
1204 },
1205 [3] = {
1206 .has_osd_c = 1,
1207 .has_osd_d = 1,
1208 .palette_sz = 16,
1209 .palette_16bpp = 1,
1210 .valid_bpp = (VALID_BPP124 | VALID_BPP(16) |
1211 VALID_BPP(18) | VALID_BPP(19) |
1212 VALID_BPP(24) | VALID_BPP(25)),
1213 },
1214 [4] = {
1215 .has_osd_c = 1,
1216 .palette_sz = 4,
1217 .palette_16bpp = 1,
1218 .valid_bpp = (VALID_BPP(1) | VALID_BPP(2) |
1219 VALID_BPP(16) | VALID_BPP(18) |
1220 VALID_BPP(24) | VALID_BPP(25)),
1221 },
1222};
1223
1224static struct s3c_fb_driverdata s3c_fb_data_64xx __devinitdata = {
1225 .variant = {
1226 .nr_windows = 5,
Ben Dooksc4bb6ff2010-08-10 18:02:34 -07001227 .vidtcon = VIDTCON0,
1228 .wincon = WINCON(0),
1229 .winmap = WINxMAP(0),
1230 .keycon = WKEYCON,
1231 .osd = VIDOSD_BASE,
1232 .osd_stride = 16,
1233 .buf_start = VIDW_BUF_START(0),
1234 .buf_size = VIDW_BUF_SIZE(0),
1235 .buf_end = VIDW_BUF_END(0),
Ben Dooks50a55032010-08-10 18:02:33 -07001236
1237 .palette = {
1238 [0] = 0x400,
1239 [1] = 0x800,
1240 [2] = 0x300,
1241 [3] = 0x320,
1242 [4] = 0x340,
1243 },
1244 },
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],
1250};
1251
1252static struct s3c_fb_driverdata s3c_fb_data_s5p __devinitdata = {
1253 .variant = {
1254 .nr_windows = 5,
Ben Dooksc4bb6ff2010-08-10 18:02:34 -07001255 .vidtcon = VIDTCON0,
1256 .wincon = WINCON(0),
1257 .winmap = WINxMAP(0),
1258 .keycon = WKEYCON,
1259 .osd = VIDOSD_BASE,
1260 .osd_stride = 16,
1261 .buf_start = VIDW_BUF_START(0),
1262 .buf_size = VIDW_BUF_SIZE(0),
1263 .buf_end = VIDW_BUF_END(0),
Ben Dooks50a55032010-08-10 18:02:33 -07001264
1265 .palette = {
1266 [0] = 0x2400,
1267 [1] = 0x2800,
1268 [2] = 0x2c00,
1269 [3] = 0x3000,
1270 [4] = 0x3400,
1271 },
1272 },
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],
1278};
1279
Ben Dooksc4bb6ff2010-08-10 18:02:34 -07001280/* S3C2443/S3C2416 style hardware */
1281static struct s3c_fb_driverdata s3c_fb_data_s3c2443 __devinitdata = {
1282 .variant = {
1283 .nr_windows = 2,
1284 .is_2443 = 1,
1285
1286 .vidtcon = 0x08,
1287 .wincon = 0x14,
1288 .winmap = 0xd0,
1289 .keycon = 0xb0,
1290 .osd = 0x28,
1291 .osd_stride = 12,
1292 .buf_start = 0x64,
1293 .buf_size = 0x94,
1294 .buf_end = 0x7c,
1295
1296 .palette = {
1297 [0] = 0x400,
1298 [1] = 0x800,
1299 },
1300 },
1301 .win[0] = &(struct s3c_fb_win_variant) {
1302 .palette_sz = 256,
1303 .valid_bpp = VALID_BPP1248 | VALID_BPP(16) | VALID_BPP(24),
1304 },
1305 .win[1] = &(struct s3c_fb_win_variant) {
1306 .has_osd_c = 1,
1307 .palette_sz = 256,
1308 .valid_bpp = (VALID_BPP1248 | VALID_BPP(16) |
1309 VALID_BPP(18) | VALID_BPP(19) |
1310 VALID_BPP(24) | VALID_BPP(25) |
1311 VALID_BPP(28)),
1312 },
1313};
1314
Ben Dooks50a55032010-08-10 18:02:33 -07001315static struct platform_device_id s3c_fb_driver_ids[] = {
1316 {
1317 .name = "s3c-fb",
1318 .driver_data = (unsigned long)&s3c_fb_data_64xx,
1319 }, {
1320 .name = "s5p-fb",
1321 .driver_data = (unsigned long)&s3c_fb_data_s5p,
Ben Dooksc4bb6ff2010-08-10 18:02:34 -07001322 }, {
1323 .name = "s3c2443-fb",
1324 .driver_data = (unsigned long)&s3c_fb_data_s3c2443,
Ben Dooks50a55032010-08-10 18:02:33 -07001325 },
1326 {},
1327};
1328MODULE_DEVICE_TABLE(platform, s3c_fb_driver_ids);
1329
Ben Dooksec549a02009-03-31 15:25:39 -07001330static struct platform_driver s3c_fb_driver = {
1331 .probe = s3c_fb_probe,
Peter Korsgaard3163eaba2009-09-22 16:47:55 -07001332 .remove = __devexit_p(s3c_fb_remove),
Ben Dooksec549a02009-03-31 15:25:39 -07001333 .suspend = s3c_fb_suspend,
1334 .resume = s3c_fb_resume,
Ben Dooks50a55032010-08-10 18:02:33 -07001335 .id_table = s3c_fb_driver_ids,
Ben Dooksec549a02009-03-31 15:25:39 -07001336 .driver = {
1337 .name = "s3c-fb",
1338 .owner = THIS_MODULE,
1339 },
1340};
1341
1342static int __init s3c_fb_init(void)
1343{
1344 return platform_driver_register(&s3c_fb_driver);
1345}
1346
1347static void __exit s3c_fb_cleanup(void)
1348{
1349 platform_driver_unregister(&s3c_fb_driver);
1350}
1351
1352module_init(s3c_fb_init);
1353module_exit(s3c_fb_cleanup);
1354
1355MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
1356MODULE_DESCRIPTION("Samsung S3C SoC Framebuffer driver");
1357MODULE_LICENSE("GPL");
1358MODULE_ALIAS("platform:s3c-fb");