blob: ae08d45870917e3fac1dc70dca5201c87ff147d3 [file] [log] [blame]
Arnaud Patard20fd5762005-09-09 13:10:07 -07001/*
2 * linux/drivers/video/s3c2410fb.c
3 * Copyright (c) Arnaud Patard, Ben Dooks
4 *
5 * This file is subject to the terms and conditions of the GNU General Public
6 * License. See the file COPYING in the main directory of this archive for
7 * more details.
8 *
9 * S3C2410 LCD Controller Frame Buffer Driver
10 * based on skeletonfb.c, sa1100fb.c and others
11 *
12 * ChangeLog
13 * 2005-04-07: Arnaud Patard <arnaud.patard@rtp-net.org>
14 * - u32 state -> pm_message_t state
15 * - S3C2410_{VA,SZ}_LCD -> S3C24XX
16 *
17 * 2005-03-15: Arnaud Patard <arnaud.patard@rtp-net.org>
18 * - Removed the ioctl
19 * - use readl/writel instead of __raw_writel/__raw_readl
20 *
21 * 2004-12-04: Arnaud Patard <arnaud.patard@rtp-net.org>
22 * - Added the possibility to set on or off the
Krzysztof Helt9fa7bc02007-10-16 01:29:05 -070023 * debugging messages
Arnaud Patard20fd5762005-09-09 13:10:07 -070024 * - Replaced 0 and 1 by on or off when reading the
25 * /sys files
26 *
27 * 2005-03-23: Ben Dooks <ben-linux@fluff.org>
28 * - added non 16bpp modes
29 * - updated platform information for range of x/y/bpp
30 * - add code to ensure palette is written correctly
31 * - add pixel clock divisor control
32 *
33 * 2004-11-11: Arnaud Patard <arnaud.patard@rtp-net.org>
Krzysztof Helt9fa7bc02007-10-16 01:29:05 -070034 * - Removed the use of currcon as it no more exists
Krzysztof Heltb0831942007-10-16 01:28:54 -070035 * - Added LCD power sysfs interface
Arnaud Patard20fd5762005-09-09 13:10:07 -070036 *
37 * 2004-11-03: Ben Dooks <ben-linux@fluff.org>
38 * - minor cleanups
39 * - add suspend/resume support
40 * - s3c2410fb_setcolreg() not valid in >8bpp modes
41 * - removed last CONFIG_FB_S3C2410_FIXED
42 * - ensure lcd controller stopped before cleanup
43 * - added sysfs interface for backlight power
44 * - added mask for gpio configuration
45 * - ensured IRQs disabled during GPIO configuration
46 * - disable TPAL before enabling video
47 *
48 * 2004-09-20: Arnaud Patard <arnaud.patard@rtp-net.org>
49 * - Suppress command line options
50 *
51 * 2004-09-15: Arnaud Patard <arnaud.patard@rtp-net.org>
Krzysztof Heltb0831942007-10-16 01:28:54 -070052 * - code cleanup
Arnaud Patard20fd5762005-09-09 13:10:07 -070053 *
54 * 2004-09-07: Arnaud Patard <arnaud.patard@rtp-net.org>
Krzysztof Heltb0831942007-10-16 01:28:54 -070055 * - Renamed from h1940fb.c to s3c2410fb.c
56 * - Add support for different devices
57 * - Backlight support
Arnaud Patard20fd5762005-09-09 13:10:07 -070058 *
59 * 2004-09-05: Herbert Pötzl <herbert@13thfloor.at>
60 * - added clock (de-)allocation code
61 * - added fixem fbmem option
62 *
63 * 2004-07-27: Arnaud Patard <arnaud.patard@rtp-net.org>
64 * - code cleanup
65 * - added a forgotten return in h1940fb_init
66 *
67 * 2004-07-19: Herbert Pötzl <herbert@13thfloor.at>
68 * - code cleanup and extended debugging
69 *
70 * 2004-07-15: Arnaud Patard <arnaud.patard@rtp-net.org>
71 * - First version
72 */
73
74#include <linux/module.h>
75#include <linux/kernel.h>
76#include <linux/errno.h>
77#include <linux/string.h>
78#include <linux/mm.h>
Arnaud Patard20fd5762005-09-09 13:10:07 -070079#include <linux/slab.h>
80#include <linux/delay.h>
81#include <linux/fb.h>
82#include <linux/init.h>
83#include <linux/dma-mapping.h>
Arnaud Patard20fd5762005-09-09 13:10:07 -070084#include <linux/interrupt.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010085#include <linux/platform_device.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000086#include <linux/clk.h>
Arnaud Patard20fd5762005-09-09 13:10:07 -070087
88#include <asm/io.h>
Arnaud Patard20fd5762005-09-09 13:10:07 -070089#include <asm/div64.h>
90
91#include <asm/mach/map.h>
92#include <asm/arch/regs-lcd.h>
93#include <asm/arch/regs-gpio.h>
94#include <asm/arch/fb.h>
Arnaud Patard20fd5762005-09-09 13:10:07 -070095
96#ifdef CONFIG_PM
97#include <linux/pm.h>
98#endif
99
100#include "s3c2410fb.h"
101
Arnaud Patard20fd5762005-09-09 13:10:07 -0700102/* Debugging stuff */
103#ifdef CONFIG_FB_S3C2410_DEBUG
Krzysztof Heltb0831942007-10-16 01:28:54 -0700104static int debug = 1;
Arnaud Patard20fd5762005-09-09 13:10:07 -0700105#else
Krzysztof Heltb0831942007-10-16 01:28:54 -0700106static int debug = 0;
Arnaud Patard20fd5762005-09-09 13:10:07 -0700107#endif
108
109#define dprintk(msg...) if (debug) { printk(KERN_DEBUG "s3c2410fb: " msg); }
110
111/* useful functions */
112
113/* s3c2410fb_set_lcdaddr
114 *
115 * initialise lcd controller address pointers
Krzysztof Heltb0831942007-10-16 01:28:54 -0700116 */
Krzysztof Helt110c1fa2007-10-16 01:28:55 -0700117static void s3c2410fb_set_lcdaddr(struct fb_info *info)
Arnaud Patard20fd5762005-09-09 13:10:07 -0700118{
Arnaud Patard20fd5762005-09-09 13:10:07 -0700119 unsigned long saddr1, saddr2, saddr3;
Krzysztof Helt7ee0fe42007-10-16 01:29:01 -0700120 struct s3c2410fb_info *fbi = info->par;
121 void __iomem *regs = fbi->io;
Arnaud Patard20fd5762005-09-09 13:10:07 -0700122
Krzysztof Helt110c1fa2007-10-16 01:28:55 -0700123 saddr1 = info->fix.smem_start >> 1;
124 saddr2 = info->fix.smem_start;
Krzysztof Helt9fa7bc02007-10-16 01:29:05 -0700125 saddr2 += info->fix.line_length * info->var.yres;
Krzysztof Heltb0831942007-10-16 01:28:54 -0700126 saddr2 >>= 1;
Arnaud Patard20fd5762005-09-09 13:10:07 -0700127
Krzysztof Heltb0831942007-10-16 01:28:54 -0700128 saddr3 = S3C2410_OFFSIZE(0) |
Krzysztof Helt9fa7bc02007-10-16 01:29:05 -0700129 S3C2410_PAGEWIDTH((info->fix.line_length / 2) & 0x3ff);
Arnaud Patard20fd5762005-09-09 13:10:07 -0700130
131 dprintk("LCDSADDR1 = 0x%08lx\n", saddr1);
132 dprintk("LCDSADDR2 = 0x%08lx\n", saddr2);
133 dprintk("LCDSADDR3 = 0x%08lx\n", saddr3);
134
Krzysztof Helt7ee0fe42007-10-16 01:29:01 -0700135 writel(saddr1, regs + S3C2410_LCDSADDR1);
136 writel(saddr2, regs + S3C2410_LCDSADDR2);
137 writel(saddr3, regs + S3C2410_LCDSADDR3);
Arnaud Patard20fd5762005-09-09 13:10:07 -0700138}
139
140/* s3c2410fb_calc_pixclk()
141 *
142 * calculate divisor for clk->pixclk
Krzysztof Heltb0831942007-10-16 01:28:54 -0700143 */
Arnaud Patard20fd5762005-09-09 13:10:07 -0700144static unsigned int s3c2410fb_calc_pixclk(struct s3c2410fb_info *fbi,
145 unsigned long pixclk)
146{
147 unsigned long clk = clk_get_rate(fbi->clk);
148 unsigned long long div;
149
Krzysztof Helt9fa7bc02007-10-16 01:29:05 -0700150 /* pixclk is in picoseconds, our clock is in Hz
Arnaud Patard20fd5762005-09-09 13:10:07 -0700151 *
152 * Hz -> picoseconds is / 10^-12
153 */
154
155 div = (unsigned long long)clk * pixclk;
Krzysztof Helt9fa7bc02007-10-16 01:29:05 -0700156 div >>= 12; /* div / 2^12 */
157 do_div(div, 625 * 625UL * 625); /* div / 5^12 */
Arnaud Patard20fd5762005-09-09 13:10:07 -0700158
159 dprintk("pixclk %ld, divisor is %ld\n", pixclk, (long)div);
160 return div;
161}
162
163/*
164 * s3c2410fb_check_var():
165 * Get the video params out of 'var'. If a value doesn't fit, round it up,
166 * if it's too big, return -EINVAL.
167 *
168 */
169static int s3c2410fb_check_var(struct fb_var_screeninfo *var,
170 struct fb_info *info)
171{
172 struct s3c2410fb_info *fbi = info->par;
Krzysztof Helt9fa7bc02007-10-16 01:29:05 -0700173 struct s3c2410fb_mach_info *mach_info = fbi->dev->platform_data;
Krzysztof Helt09fe75f2007-10-16 01:28:56 -0700174 struct s3c2410fb_display *display = NULL;
Krzysztof Helte7076382007-10-16 01:29:08 -0700175 struct s3c2410fb_display *default_display = mach_info->displays +
176 mach_info->default_display;
177 int type = default_display->type;
Krzysztof Helt09fe75f2007-10-16 01:28:56 -0700178 unsigned i;
Arnaud Patard20fd5762005-09-09 13:10:07 -0700179
180 dprintk("check_var(var=%p, info=%p)\n", var, info);
181
182 /* validate x/y resolution */
Krzysztof Helte7076382007-10-16 01:29:08 -0700183 /* choose default mode if possible */
184 if (var->yres == default_display->yres &&
185 var->xres == default_display->xres &&
186 var->bits_per_pixel == default_display->bpp)
187 display = default_display;
188 else
189 for (i = 0; i < mach_info->num_displays; i++)
190 if (type == mach_info->displays[i].type &&
191 var->yres == mach_info->displays[i].yres &&
192 var->xres == mach_info->displays[i].xres &&
193 var->bits_per_pixel == mach_info->displays[i].bpp) {
194 display = mach_info->displays + i;
195 break;
196 }
Arnaud Patard20fd5762005-09-09 13:10:07 -0700197
Krzysztof Helt09fe75f2007-10-16 01:28:56 -0700198 if (!display) {
199 dprintk("wrong resolution or depth %dx%d at %d bpp\n",
200 var->xres, var->yres, var->bits_per_pixel);
201 return -EINVAL;
202 }
Arnaud Patard20fd5762005-09-09 13:10:07 -0700203
Krzysztof Helt9939a482007-10-16 01:28:57 -0700204 /* it is always the size as the display */
205 var->xres_virtual = display->xres;
206 var->yres_virtual = display->yres;
Krzysztof Helt9fa7bc02007-10-16 01:29:05 -0700207 var->height = display->height;
208 var->width = display->width;
Krzysztof Helt9939a482007-10-16 01:28:57 -0700209
210 /* copy lcd settings */
Krzysztof Helt69816692007-10-16 01:29:06 -0700211 var->pixclock = display->pixclock;
Krzysztof Helt9939a482007-10-16 01:28:57 -0700212 var->left_margin = display->left_margin;
213 var->right_margin = display->right_margin;
Krzysztof Helt9fa7bc02007-10-16 01:29:05 -0700214 var->upper_margin = display->upper_margin;
215 var->lower_margin = display->lower_margin;
216 var->vsync_len = display->vsync_len;
217 var->hsync_len = display->hsync_len;
218
Krzysztof Helt9fa7bc02007-10-16 01:29:05 -0700219 fbi->regs.lcdcon5 = display->lcdcon5;
220 /* set display type */
Krzysztof Helt36f31a72007-10-16 01:29:07 -0700221 fbi->regs.lcdcon1 = display->type;
Krzysztof Helt9939a482007-10-16 01:28:57 -0700222
Krzysztof Heltb0831942007-10-16 01:28:54 -0700223 var->transp.offset = 0;
224 var->transp.length = 0;
Arnaud Patard20fd5762005-09-09 13:10:07 -0700225 /* set r/g/b positions */
Arnaud Patard (Rtp357b8192006-12-08 02:40:23 -0800226 switch (var->bits_per_pixel) {
Krzysztof Heltb0831942007-10-16 01:28:54 -0700227 case 1:
228 case 2:
229 case 4:
230 var->red.offset = 0;
231 var->red.length = var->bits_per_pixel;
232 var->green = var->red;
233 var->blue = var->red;
234 break;
235 case 8:
Krzysztof Helt09fe75f2007-10-16 01:28:56 -0700236 if (display->type != S3C2410_LCDCON1_TFT) {
Krzysztof Heltb0831942007-10-16 01:28:54 -0700237 /* 8 bpp 332 */
238 var->red.length = 3;
239 var->red.offset = 5;
240 var->green.length = 3;
241 var->green.offset = 2;
242 var->blue.length = 2;
Arnaud Patard (Rtp357b8192006-12-08 02:40:23 -0800243 var->blue.offset = 0;
Krzysztof Heltb0831942007-10-16 01:28:54 -0700244 } else {
245 var->red.offset = 0;
Arnaud Patard (Rtp357b8192006-12-08 02:40:23 -0800246 var->red.length = 8;
Krzysztof Heltb0831942007-10-16 01:28:54 -0700247 var->green = var->red;
248 var->blue = var->red;
249 }
250 break;
251 case 12:
252 /* 12 bpp 444 */
253 var->red.length = 4;
254 var->red.offset = 8;
255 var->green.length = 4;
256 var->green.offset = 4;
257 var->blue.length = 4;
258 var->blue.offset = 0;
259 break;
260
261 default:
262 case 16:
Krzysztof Heltf28ef572007-10-16 01:28:58 -0700263 if (display->lcdcon5 & S3C2410_LCDCON5_FRM565) {
Krzysztof Heltb0831942007-10-16 01:28:54 -0700264 /* 16 bpp, 565 format */
265 var->red.offset = 11;
266 var->green.offset = 5;
Arnaud Patard (Rtp357b8192006-12-08 02:40:23 -0800267 var->blue.offset = 0;
Krzysztof Heltb0831942007-10-16 01:28:54 -0700268 var->red.length = 5;
269 var->green.length = 6;
270 var->blue.length = 5;
271 } else {
272 /* 16 bpp, 5551 format */
273 var->red.offset = 11;
274 var->green.offset = 6;
275 var->blue.offset = 1;
276 var->red.length = 5;
277 var->green.length = 5;
278 var->blue.length = 5;
279 }
280 break;
Krzysztof Helt93613b92007-10-16 01:29:02 -0700281 case 32:
282 /* 24 bpp 888 and 8 dummy */
Krzysztof Heltb0831942007-10-16 01:28:54 -0700283 var->red.length = 8;
284 var->red.offset = 16;
285 var->green.length = 8;
286 var->green.offset = 8;
287 var->blue.length = 8;
288 var->blue.offset = 0;
289 break;
Arnaud Patard20fd5762005-09-09 13:10:07 -0700290 }
Arnaud Patard20fd5762005-09-09 13:10:07 -0700291 return 0;
292}
293
Krzysztof Helt9939a482007-10-16 01:28:57 -0700294/* s3c2410fb_calculate_stn_lcd_regs
Arnaud Patard20fd5762005-09-09 13:10:07 -0700295 *
Krzysztof Helt9939a482007-10-16 01:28:57 -0700296 * calculate register values from var settings
Krzysztof Heltb0831942007-10-16 01:28:54 -0700297 */
Krzysztof Helt9939a482007-10-16 01:28:57 -0700298static void s3c2410fb_calculate_stn_lcd_regs(const struct fb_info *info,
299 struct s3c2410fb_hw *regs)
Arnaud Patard20fd5762005-09-09 13:10:07 -0700300{
Krzysztof Helt9939a482007-10-16 01:28:57 -0700301 const struct s3c2410fb_info *fbi = info->par;
302 const struct fb_var_screeninfo *var = &info->var;
303 int type = regs->lcdcon1 & ~S3C2410_LCDCON1_TFT;
304 int hs = var->xres >> 2;
305 unsigned wdly = (var->left_margin >> 4) - 1;
Krzysztof Helt93d11f52007-10-16 01:29:00 -0700306 unsigned wlh = (var->hsync_len >> 4) - 1;
Arnaud Patard20fd5762005-09-09 13:10:07 -0700307
Krzysztof Helt9939a482007-10-16 01:28:57 -0700308 if (type != S3C2410_LCDCON1_STN4)
309 hs >>= 1;
310
Krzysztof Helt9939a482007-10-16 01:28:57 -0700311 switch (var->bits_per_pixel) {
312 case 1:
313 regs->lcdcon1 |= S3C2410_LCDCON1_STN1BPP;
314 break;
315 case 2:
316 regs->lcdcon1 |= S3C2410_LCDCON1_STN2GREY;
317 break;
318 case 4:
319 regs->lcdcon1 |= S3C2410_LCDCON1_STN4GREY;
320 break;
321 case 8:
322 regs->lcdcon1 |= S3C2410_LCDCON1_STN8BPP;
323 hs *= 3;
324 break;
325 case 12:
326 regs->lcdcon1 |= S3C2410_LCDCON1_STN12BPP;
327 hs *= 3;
328 break;
329
330 default:
331 /* invalid pixel depth */
332 dev_err(fbi->dev, "invalid bpp %d\n",
333 var->bits_per_pixel);
334 }
335 /* update X/Y info */
Krzysztof Helt9939a482007-10-16 01:28:57 -0700336 dprintk("setting horz: lft=%d, rt=%d, sync=%d\n",
337 var->left_margin, var->right_margin, var->hsync_len);
338
Krzysztof Helt3c9ffd02007-10-16 01:28:59 -0700339 regs->lcdcon2 = S3C2410_LCDCON2_LINEVAL(var->yres - 1);
Krzysztof Helt9939a482007-10-16 01:28:57 -0700340
341 if (wdly > 3)
342 wdly = 3;
343
Krzysztof Helt93d11f52007-10-16 01:29:00 -0700344 if (wlh > 3)
345 wlh = 3;
346
Krzysztof Helt9939a482007-10-16 01:28:57 -0700347 regs->lcdcon3 = S3C2410_LCDCON3_WDLY(wdly) |
348 S3C2410_LCDCON3_LINEBLANK(var->right_margin / 8) |
349 S3C2410_LCDCON3_HOZVAL(hs - 1);
Krzysztof Helt93d11f52007-10-16 01:29:00 -0700350
Krzysztof Helte92e7392007-10-16 01:29:01 -0700351 regs->lcdcon4 = S3C2410_LCDCON4_WLH(wlh);
Krzysztof Helt9939a482007-10-16 01:28:57 -0700352}
353
354/* s3c2410fb_calculate_tft_lcd_regs
355 *
356 * calculate register values from var settings
357 */
358static void s3c2410fb_calculate_tft_lcd_regs(const struct fb_info *info,
359 struct s3c2410fb_hw *regs)
360{
361 const struct s3c2410fb_info *fbi = info->par;
362 const struct fb_var_screeninfo *var = &info->var;
363
Krzysztof Helt9939a482007-10-16 01:28:57 -0700364 switch (var->bits_per_pixel) {
365 case 1:
366 regs->lcdcon1 |= S3C2410_LCDCON1_TFT1BPP;
367 break;
368 case 2:
369 regs->lcdcon1 |= S3C2410_LCDCON1_TFT2BPP;
370 break;
371 case 4:
372 regs->lcdcon1 |= S3C2410_LCDCON1_TFT4BPP;
373 break;
374 case 8:
375 regs->lcdcon1 |= S3C2410_LCDCON1_TFT8BPP;
Krzysztof Helt93613b92007-10-16 01:29:02 -0700376 regs->lcdcon5 |= S3C2410_LCDCON5_BSWP |
377 S3C2410_LCDCON5_FRM565;
378 regs->lcdcon5 &= ~S3C2410_LCDCON5_HWSWP;
Krzysztof Helt9939a482007-10-16 01:28:57 -0700379 break;
380 case 16:
381 regs->lcdcon1 |= S3C2410_LCDCON1_TFT16BPP;
Krzysztof Helt93613b92007-10-16 01:29:02 -0700382 regs->lcdcon5 &= ~S3C2410_LCDCON5_BSWP;
383 regs->lcdcon5 |= S3C2410_LCDCON5_HWSWP;
Krzysztof Helt9939a482007-10-16 01:28:57 -0700384 break;
Krzysztof Helt93613b92007-10-16 01:29:02 -0700385 case 32:
386 regs->lcdcon1 |= S3C2410_LCDCON1_TFT24BPP;
387 regs->lcdcon5 &= ~(S3C2410_LCDCON5_BSWP |
388 S3C2410_LCDCON5_HWSWP |
389 S3C2410_LCDCON5_BPP24BL);
390 break;
Krzysztof Helt9939a482007-10-16 01:28:57 -0700391 default:
392 /* invalid pixel depth */
393 dev_err(fbi->dev, "invalid bpp %d\n",
394 var->bits_per_pixel);
395 }
396 /* update X/Y info */
397 dprintk("setting vert: up=%d, low=%d, sync=%d\n",
398 var->upper_margin, var->lower_margin, var->vsync_len);
399
400 dprintk("setting horz: lft=%d, rt=%d, sync=%d\n",
401 var->left_margin, var->right_margin, var->hsync_len);
402
Krzysztof Helt93d11f52007-10-16 01:29:00 -0700403 regs->lcdcon2 = S3C2410_LCDCON2_LINEVAL(var->yres - 1) |
404 S3C2410_LCDCON2_VBPD(var->upper_margin - 1) |
405 S3C2410_LCDCON2_VFPD(var->lower_margin - 1) |
406 S3C2410_LCDCON2_VSPW(var->vsync_len - 1);
Krzysztof Helt9939a482007-10-16 01:28:57 -0700407
408 regs->lcdcon3 = S3C2410_LCDCON3_HBPD(var->right_margin - 1) |
409 S3C2410_LCDCON3_HFPD(var->left_margin - 1) |
410 S3C2410_LCDCON3_HOZVAL(var->xres - 1);
Krzysztof Helt93d11f52007-10-16 01:29:00 -0700411
Krzysztof Helte92e7392007-10-16 01:29:01 -0700412 regs->lcdcon4 = S3C2410_LCDCON4_HSPW(var->hsync_len - 1);
Krzysztof Helt9939a482007-10-16 01:28:57 -0700413}
414
415/* s3c2410fb_activate_var
416 *
417 * activate (set) the controller from the given framebuffer
418 * information
419 */
420static void s3c2410fb_activate_var(struct fb_info *info)
421{
422 struct s3c2410fb_info *fbi = info->par;
Krzysztof Helt7ee0fe42007-10-16 01:29:01 -0700423 void __iomem *regs = fbi->io;
Krzysztof Helt9fa7bc02007-10-16 01:29:05 -0700424 int type = fbi->regs.lcdcon1 & S3C2410_LCDCON1_TFT;
Krzysztof Helt9939a482007-10-16 01:28:57 -0700425 struct fb_var_screeninfo *var = &info->var;
Krzysztof Helt69816692007-10-16 01:29:06 -0700426 int clkdiv = s3c2410fb_calc_pixclk(fbi, var->pixclock) / 2;
Arnaud Patard (Rtp357b8192006-12-08 02:40:23 -0800427
Krzysztof Helt69816692007-10-16 01:29:06 -0700428 dprintk("%s: var->xres = %d\n", __FUNCTION__, var->xres);
429 dprintk("%s: var->yres = %d\n", __FUNCTION__, var->yres);
430 dprintk("%s: var->bpp = %d\n", __FUNCTION__, var->bits_per_pixel);
Arnaud Patard20fd5762005-09-09 13:10:07 -0700431
Krzysztof Helt69816692007-10-16 01:29:06 -0700432 if (type == S3C2410_LCDCON1_TFT) {
433 s3c2410fb_calculate_tft_lcd_regs(info, &fbi->regs);
434 --clkdiv;
435 if (clkdiv < 0)
436 clkdiv = 0;
437 } else {
438 s3c2410fb_calculate_stn_lcd_regs(info, &fbi->regs);
439 if (clkdiv < 2)
440 clkdiv = 2;
Arnaud Patard20fd5762005-09-09 13:10:07 -0700441 }
442
Krzysztof Helt69816692007-10-16 01:29:06 -0700443 fbi->regs.lcdcon1 |= S3C2410_LCDCON1_CLKVAL(clkdiv);
Krzysztof Helt9939a482007-10-16 01:28:57 -0700444
Arnaud Patard20fd5762005-09-09 13:10:07 -0700445 /* write new registers */
446
447 dprintk("new register set:\n");
448 dprintk("lcdcon[1] = 0x%08lx\n", fbi->regs.lcdcon1);
449 dprintk("lcdcon[2] = 0x%08lx\n", fbi->regs.lcdcon2);
450 dprintk("lcdcon[3] = 0x%08lx\n", fbi->regs.lcdcon3);
451 dprintk("lcdcon[4] = 0x%08lx\n", fbi->regs.lcdcon4);
452 dprintk("lcdcon[5] = 0x%08lx\n", fbi->regs.lcdcon5);
453
Krzysztof Helt7ee0fe42007-10-16 01:29:01 -0700454 writel(fbi->regs.lcdcon1 & ~S3C2410_LCDCON1_ENVID,
455 regs + S3C2410_LCDCON1);
456 writel(fbi->regs.lcdcon2, regs + S3C2410_LCDCON2);
457 writel(fbi->regs.lcdcon3, regs + S3C2410_LCDCON3);
458 writel(fbi->regs.lcdcon4, regs + S3C2410_LCDCON4);
459 writel(fbi->regs.lcdcon5, regs + S3C2410_LCDCON5);
Arnaud Patard20fd5762005-09-09 13:10:07 -0700460
461 /* set lcd address pointers */
Krzysztof Helt110c1fa2007-10-16 01:28:55 -0700462 s3c2410fb_set_lcdaddr(info);
Arnaud Patard20fd5762005-09-09 13:10:07 -0700463
Krzysztof Helt9fa7bc02007-10-16 01:29:05 -0700464 fbi->regs.lcdcon1 |= S3C2410_LCDCON1_ENVID,
Krzysztof Helt7ee0fe42007-10-16 01:29:01 -0700465 writel(fbi->regs.lcdcon1, regs + S3C2410_LCDCON1);
Arnaud Patard20fd5762005-09-09 13:10:07 -0700466}
467
Arnaud Patard20fd5762005-09-09 13:10:07 -0700468/*
Krzysztof Heltb0831942007-10-16 01:28:54 -0700469 * s3c2410fb_set_par - Alters the hardware state.
Arnaud Patard20fd5762005-09-09 13:10:07 -0700470 * @info: frame buffer structure that represents a single frame buffer
471 *
472 */
473static int s3c2410fb_set_par(struct fb_info *info)
474{
Arnaud Patard20fd5762005-09-09 13:10:07 -0700475 struct fb_var_screeninfo *var = &info->var;
476
Krzysztof Heltb0831942007-10-16 01:28:54 -0700477 switch (var->bits_per_pixel) {
Krzysztof Helt93613b92007-10-16 01:29:02 -0700478 case 32:
Krzysztof Heltb0831942007-10-16 01:28:54 -0700479 case 16:
Krzysztof Helt93613b92007-10-16 01:29:02 -0700480 case 12:
Krzysztof Heltb0831942007-10-16 01:28:54 -0700481 info->fix.visual = FB_VISUAL_TRUECOLOR;
482 break;
483 case 1:
484 info->fix.visual = FB_VISUAL_MONO01;
485 break;
486 default:
487 info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
488 break;
Arnaud Patard (Rtp357b8192006-12-08 02:40:23 -0800489 }
Arnaud Patard20fd5762005-09-09 13:10:07 -0700490
Krzysztof Heltb0831942007-10-16 01:28:54 -0700491 info->fix.line_length = (var->width * var->bits_per_pixel) / 8;
Arnaud Patard20fd5762005-09-09 13:10:07 -0700492
493 /* activate this new configuration */
494
Krzysztof Helt9939a482007-10-16 01:28:57 -0700495 s3c2410fb_activate_var(info);
Arnaud Patard20fd5762005-09-09 13:10:07 -0700496 return 0;
497}
498
499static void schedule_palette_update(struct s3c2410fb_info *fbi,
500 unsigned int regno, unsigned int val)
501{
502 unsigned long flags;
503 unsigned long irqen;
Ben Dooksaff39a82007-07-31 00:37:37 -0700504 void __iomem *regs = fbi->io;
Arnaud Patard20fd5762005-09-09 13:10:07 -0700505
506 local_irq_save(flags);
507
508 fbi->palette_buffer[regno] = val;
509
510 if (!fbi->palette_ready) {
511 fbi->palette_ready = 1;
512
513 /* enable IRQ */
Ben Dooksaff39a82007-07-31 00:37:37 -0700514 irqen = readl(regs + S3C2410_LCDINTMSK);
Arnaud Patard20fd5762005-09-09 13:10:07 -0700515 irqen &= ~S3C2410_LCDINT_FRSYNC;
Ben Dooksaff39a82007-07-31 00:37:37 -0700516 writel(irqen, regs + S3C2410_LCDINTMSK);
Arnaud Patard20fd5762005-09-09 13:10:07 -0700517 }
518
519 local_irq_restore(flags);
520}
521
522/* from pxafb.c */
Krzysztof Heltb0831942007-10-16 01:28:54 -0700523static inline unsigned int chan_to_field(unsigned int chan,
524 struct fb_bitfield *bf)
Arnaud Patard20fd5762005-09-09 13:10:07 -0700525{
526 chan &= 0xffff;
527 chan >>= 16 - bf->length;
528 return chan << bf->offset;
529}
530
531static int s3c2410fb_setcolreg(unsigned regno,
532 unsigned red, unsigned green, unsigned blue,
533 unsigned transp, struct fb_info *info)
534{
535 struct s3c2410fb_info *fbi = info->par;
Krzysztof Helt7ee0fe42007-10-16 01:29:01 -0700536 void __iomem *regs = fbi->io;
Arnaud Patard20fd5762005-09-09 13:10:07 -0700537 unsigned int val;
538
Krzysztof Heltb0831942007-10-16 01:28:54 -0700539 /* dprintk("setcol: regno=%d, rgb=%d,%d,%d\n",
540 regno, red, green, blue); */
Arnaud Patard20fd5762005-09-09 13:10:07 -0700541
Krzysztof Heltb0831942007-10-16 01:28:54 -0700542 switch (info->fix.visual) {
Arnaud Patard20fd5762005-09-09 13:10:07 -0700543 case FB_VISUAL_TRUECOLOR:
Krzysztof Heltb0831942007-10-16 01:28:54 -0700544 /* true-colour, use pseudo-palette */
Arnaud Patard20fd5762005-09-09 13:10:07 -0700545
546 if (regno < 16) {
Krzysztof Heltb0831942007-10-16 01:28:54 -0700547 u32 *pal = info->pseudo_palette;
Arnaud Patard20fd5762005-09-09 13:10:07 -0700548
Krzysztof Heltb0831942007-10-16 01:28:54 -0700549 val = chan_to_field(red, &info->var.red);
550 val |= chan_to_field(green, &info->var.green);
551 val |= chan_to_field(blue, &info->var.blue);
Arnaud Patard20fd5762005-09-09 13:10:07 -0700552
553 pal[regno] = val;
554 }
555 break;
556
557 case FB_VISUAL_PSEUDOCOLOR:
558 if (regno < 256) {
559 /* currently assume RGB 5-6-5 mode */
560
Krzysztof Helt9fa7bc02007-10-16 01:29:05 -0700561 val = (red >> 0) & 0xf800;
562 val |= (green >> 5) & 0x07e0;
563 val |= (blue >> 11) & 0x001f;
Arnaud Patard20fd5762005-09-09 13:10:07 -0700564
Krzysztof Helt7ee0fe42007-10-16 01:29:01 -0700565 writel(val, regs + S3C2410_TFTPAL(regno));
Arnaud Patard20fd5762005-09-09 13:10:07 -0700566 schedule_palette_update(fbi, regno, val);
567 }
568
569 break;
570
571 default:
Krzysztof Heltb0831942007-10-16 01:28:54 -0700572 return 1; /* unknown type */
Arnaud Patard20fd5762005-09-09 13:10:07 -0700573 }
574
575 return 0;
576}
577
Krzysztof Heltb0831942007-10-16 01:28:54 -0700578/*
Arnaud Patard20fd5762005-09-09 13:10:07 -0700579 * s3c2410fb_blank
580 * @blank_mode: the blank mode we want.
581 * @info: frame buffer structure that represents a single frame buffer
582 *
583 * Blank the screen if blank_mode != 0, else unblank. Return 0 if
584 * blanking succeeded, != 0 if un-/blanking failed due to e.g. a
585 * video mode which doesn't support it. Implements VESA suspend
586 * and powerdown modes on hardware that supports disabling hsync/vsync:
587 * blank_mode == 2: suspend vsync
588 * blank_mode == 3: suspend hsync
589 * blank_mode == 4: powerdown
590 *
591 * Returns negative errno on error, or zero on success.
592 *
593 */
594static int s3c2410fb_blank(int blank_mode, struct fb_info *info)
595{
Krzysztof Helt7ee0fe42007-10-16 01:29:01 -0700596 struct s3c2410fb_info *fbi = info->par;
597 void __iomem *regs = fbi->io;
598
Arnaud Patard20fd5762005-09-09 13:10:07 -0700599 dprintk("blank(mode=%d, info=%p)\n", blank_mode, info);
600
Arnaud Patard20fd5762005-09-09 13:10:07 -0700601 if (blank_mode == FB_BLANK_UNBLANK)
Krzysztof Helt7ee0fe42007-10-16 01:29:01 -0700602 writel(0x0, regs + S3C2410_TPAL);
Arnaud Patard20fd5762005-09-09 13:10:07 -0700603 else {
604 dprintk("setting TPAL to output 0x000000\n");
Krzysztof Helt7ee0fe42007-10-16 01:29:01 -0700605 writel(S3C2410_TPAL_EN, regs + S3C2410_TPAL);
Arnaud Patard20fd5762005-09-09 13:10:07 -0700606 }
607
608 return 0;
609}
610
Krzysztof Heltb0831942007-10-16 01:28:54 -0700611static int s3c2410fb_debug_show(struct device *dev,
612 struct device_attribute *attr, char *buf)
Arnaud Patard20fd5762005-09-09 13:10:07 -0700613{
614 return snprintf(buf, PAGE_SIZE, "%s\n", debug ? "on" : "off");
615}
Krzysztof Helt9fa7bc02007-10-16 01:29:05 -0700616
Krzysztof Heltb0831942007-10-16 01:28:54 -0700617static int s3c2410fb_debug_store(struct device *dev,
618 struct device_attribute *attr,
619 const char *buf, size_t len)
Arnaud Patard20fd5762005-09-09 13:10:07 -0700620{
Arnaud Patard20fd5762005-09-09 13:10:07 -0700621 if (len < 1)
622 return -EINVAL;
623
624 if (strnicmp(buf, "on", 2) == 0 ||
625 strnicmp(buf, "1", 1) == 0) {
626 debug = 1;
627 printk(KERN_DEBUG "s3c2410fb: Debug On");
628 } else if (strnicmp(buf, "off", 3) == 0 ||
629 strnicmp(buf, "0", 1) == 0) {
630 debug = 0;
631 printk(KERN_DEBUG "s3c2410fb: Debug Off");
632 } else {
633 return -EINVAL;
634 }
635
636 return len;
637}
638
Krzysztof Heltb0831942007-10-16 01:28:54 -0700639static DEVICE_ATTR(debug, 0666, s3c2410fb_debug_show, s3c2410fb_debug_store);
Arnaud Patard20fd5762005-09-09 13:10:07 -0700640
641static struct fb_ops s3c2410fb_ops = {
642 .owner = THIS_MODULE,
643 .fb_check_var = s3c2410fb_check_var,
644 .fb_set_par = s3c2410fb_set_par,
645 .fb_blank = s3c2410fb_blank,
646 .fb_setcolreg = s3c2410fb_setcolreg,
647 .fb_fillrect = cfb_fillrect,
648 .fb_copyarea = cfb_copyarea,
649 .fb_imageblit = cfb_imageblit,
Arnaud Patard20fd5762005-09-09 13:10:07 -0700650};
651
Arnaud Patard20fd5762005-09-09 13:10:07 -0700652/*
653 * s3c2410fb_map_video_memory():
654 * Allocates the DRAM memory for the frame buffer. This buffer is
655 * remapped into a non-cached, non-buffered, memory region to
656 * allow palette and pixel writes to occur without flushing the
657 * cache. Once this area is remapped, all virtual memory
658 * access to the video memory should occur at the new region.
659 */
Krzysztof Helt110c1fa2007-10-16 01:28:55 -0700660static int __init s3c2410fb_map_video_memory(struct fb_info *info)
Arnaud Patard20fd5762005-09-09 13:10:07 -0700661{
Krzysztof Helt110c1fa2007-10-16 01:28:55 -0700662 struct s3c2410fb_info *fbi = info->par;
Krzysztof Helt9fa7bc02007-10-16 01:29:05 -0700663 dma_addr_t map_dma;
664 unsigned map_size = PAGE_ALIGN(info->fix.smem_len);
Krzysztof Helt110c1fa2007-10-16 01:28:55 -0700665
Arnaud Patard20fd5762005-09-09 13:10:07 -0700666 dprintk("map_video_memory(fbi=%p)\n", fbi);
667
Krzysztof Helt9fa7bc02007-10-16 01:29:05 -0700668 info->screen_base = dma_alloc_writecombine(fbi->dev, map_size,
669 &map_dma, GFP_KERNEL);
Arnaud Patard20fd5762005-09-09 13:10:07 -0700670
Krzysztof Helt9fa7bc02007-10-16 01:29:05 -0700671 if (info->screen_base) {
Arnaud Patard20fd5762005-09-09 13:10:07 -0700672 /* prevent initial garbage on screen */
673 dprintk("map_video_memory: clear %p:%08x\n",
Krzysztof Helt9fa7bc02007-10-16 01:29:05 -0700674 info->screen_base, map_size);
675 memset(info->screen_base, 0xf0, map_size);
Arnaud Patard20fd5762005-09-09 13:10:07 -0700676
Krzysztof Helt9fa7bc02007-10-16 01:29:05 -0700677 info->fix.smem_start = map_dma;
Arnaud Patard20fd5762005-09-09 13:10:07 -0700678
Krzysztof Helt9fa7bc02007-10-16 01:29:05 -0700679 dprintk("map_video_memory: dma=%08lx cpu=%p size=%08x\n",
680 info->fix.smem_start, info->screen_base, map_size);
Arnaud Patard20fd5762005-09-09 13:10:07 -0700681 }
682
Krzysztof Helt9fa7bc02007-10-16 01:29:05 -0700683 return info->screen_base ? 0 : -ENOMEM;
Arnaud Patard20fd5762005-09-09 13:10:07 -0700684}
685
Krzysztof Helt9fa7bc02007-10-16 01:29:05 -0700686static inline void s3c2410fb_unmap_video_memory(struct fb_info *info)
Arnaud Patard20fd5762005-09-09 13:10:07 -0700687{
Krzysztof Helt9fa7bc02007-10-16 01:29:05 -0700688 struct s3c2410fb_info *fbi = info->par;
689
690 dma_free_writecombine(fbi->dev, PAGE_ALIGN(info->fix.smem_len),
691 info->screen_base, info->fix.smem_start);
Arnaud Patard20fd5762005-09-09 13:10:07 -0700692}
693
694static inline void modify_gpio(void __iomem *reg,
695 unsigned long set, unsigned long mask)
696{
697 unsigned long tmp;
698
699 tmp = readl(reg) & ~mask;
700 writel(tmp | set, reg);
701}
702
Arnaud Patard20fd5762005-09-09 13:10:07 -0700703/*
704 * s3c2410fb_init_registers - Initialise all LCD-related registers
705 */
Krzysztof Helt110c1fa2007-10-16 01:28:55 -0700706static int s3c2410fb_init_registers(struct fb_info *info)
Arnaud Patard20fd5762005-09-09 13:10:07 -0700707{
Krzysztof Helt110c1fa2007-10-16 01:28:55 -0700708 struct s3c2410fb_info *fbi = info->par;
Krzysztof Helt9fa7bc02007-10-16 01:29:05 -0700709 struct s3c2410fb_mach_info *mach_info = fbi->dev->platform_data;
Arnaud Patard20fd5762005-09-09 13:10:07 -0700710 unsigned long flags;
Ben Dooksaff39a82007-07-31 00:37:37 -0700711 void __iomem *regs = fbi->io;
Arnaud Patard20fd5762005-09-09 13:10:07 -0700712
713 /* Initialise LCD with values from haret */
714
715 local_irq_save(flags);
716
717 /* modify the gpio(s) with interrupts set (bjd) */
718
719 modify_gpio(S3C2410_GPCUP, mach_info->gpcup, mach_info->gpcup_mask);
720 modify_gpio(S3C2410_GPCCON, mach_info->gpccon, mach_info->gpccon_mask);
721 modify_gpio(S3C2410_GPDUP, mach_info->gpdup, mach_info->gpdup_mask);
722 modify_gpio(S3C2410_GPDCON, mach_info->gpdcon, mach_info->gpdcon_mask);
723
724 local_irq_restore(flags);
725
Arnaud Patard20fd5762005-09-09 13:10:07 -0700726 dprintk("LPCSEL = 0x%08lx\n", mach_info->lpcsel);
Ben Dooksaff39a82007-07-31 00:37:37 -0700727 writel(mach_info->lpcsel, regs + S3C2410_LPCSEL);
Arnaud Patard20fd5762005-09-09 13:10:07 -0700728
Ben Dooksaff39a82007-07-31 00:37:37 -0700729 dprintk("replacing TPAL %08x\n", readl(regs + S3C2410_TPAL));
Arnaud Patard20fd5762005-09-09 13:10:07 -0700730
731 /* ensure temporary palette disabled */
Ben Dooksaff39a82007-07-31 00:37:37 -0700732 writel(0x00, regs + S3C2410_TPAL);
Arnaud Patard20fd5762005-09-09 13:10:07 -0700733
Arnaud Patard20fd5762005-09-09 13:10:07 -0700734 return 0;
735}
736
737static void s3c2410fb_write_palette(struct s3c2410fb_info *fbi)
738{
739 unsigned int i;
Ben Dooksaff39a82007-07-31 00:37:37 -0700740 void __iomem *regs = fbi->io;
Arnaud Patard20fd5762005-09-09 13:10:07 -0700741
742 fbi->palette_ready = 0;
743
744 for (i = 0; i < 256; i++) {
Krzysztof Heltb0831942007-10-16 01:28:54 -0700745 unsigned long ent = fbi->palette_buffer[i];
746 if (ent == PALETTE_BUFF_CLEAR)
Arnaud Patard20fd5762005-09-09 13:10:07 -0700747 continue;
748
Ben Dooksaff39a82007-07-31 00:37:37 -0700749 writel(ent, regs + S3C2410_TFTPAL(i));
Arnaud Patard20fd5762005-09-09 13:10:07 -0700750
751 /* it seems the only way to know exactly
752 * if the palette wrote ok, is to check
753 * to see if the value verifies ok
754 */
755
Ben Dooksaff39a82007-07-31 00:37:37 -0700756 if (readw(regs + S3C2410_TFTPAL(i)) == ent)
Arnaud Patard20fd5762005-09-09 13:10:07 -0700757 fbi->palette_buffer[i] = PALETTE_BUFF_CLEAR;
758 else
759 fbi->palette_ready = 1; /* retry */
760 }
761}
762
David Howells7d12e782006-10-05 14:55:46 +0100763static irqreturn_t s3c2410fb_irq(int irq, void *dev_id)
Arnaud Patard20fd5762005-09-09 13:10:07 -0700764{
765 struct s3c2410fb_info *fbi = dev_id;
Ben Dooksaff39a82007-07-31 00:37:37 -0700766 void __iomem *regs = fbi->io;
767 unsigned long lcdirq = readl(regs + S3C2410_LCDINTPND);
Arnaud Patard20fd5762005-09-09 13:10:07 -0700768
769 if (lcdirq & S3C2410_LCDINT_FRSYNC) {
770 if (fbi->palette_ready)
771 s3c2410fb_write_palette(fbi);
772
Ben Dooksaff39a82007-07-31 00:37:37 -0700773 writel(S3C2410_LCDINT_FRSYNC, regs + S3C2410_LCDINTPND);
774 writel(S3C2410_LCDINT_FRSYNC, regs + S3C2410_LCDSRCPND);
Arnaud Patard20fd5762005-09-09 13:10:07 -0700775 }
776
777 return IRQ_HANDLED;
778}
779
Krzysztof Heltb0831942007-10-16 01:28:54 -0700780static char driver_name[] = "s3c2410fb";
Arnaud Patard20fd5762005-09-09 13:10:07 -0700781
Arnaud Patard740f14b2006-01-09 20:53:41 -0800782static int __init s3c2410fb_probe(struct platform_device *pdev)
Arnaud Patard20fd5762005-09-09 13:10:07 -0700783{
784 struct s3c2410fb_info *info;
Krzysztof Helt09fe75f2007-10-16 01:28:56 -0700785 struct s3c2410fb_display *display;
Krzysztof Heltb0831942007-10-16 01:28:54 -0700786 struct fb_info *fbinfo;
Krzysztof Helt9fa7bc02007-10-16 01:29:05 -0700787 struct s3c2410fb_mach_info *mach_info;
Ben Dooksaff39a82007-07-31 00:37:37 -0700788 struct resource *res;
Arnaud Patard20fd5762005-09-09 13:10:07 -0700789 int ret;
790 int irq;
791 int i;
Ben Dooksaff39a82007-07-31 00:37:37 -0700792 int size;
Arnaud Patard6931a762006-06-26 00:26:45 -0700793 u32 lcdcon1;
Arnaud Patard20fd5762005-09-09 13:10:07 -0700794
Russell King3ae5eae2005-11-09 22:32:44 +0000795 mach_info = pdev->dev.platform_data;
Arnaud Patard20fd5762005-09-09 13:10:07 -0700796 if (mach_info == NULL) {
Krzysztof Heltb0831942007-10-16 01:28:54 -0700797 dev_err(&pdev->dev,
798 "no platform data for lcd, cannot attach\n");
Arnaud Patard20fd5762005-09-09 13:10:07 -0700799 return -EINVAL;
800 }
801
Krzysztof Helt09fe75f2007-10-16 01:28:56 -0700802 display = mach_info->displays + mach_info->default_display;
Arnaud Patard20fd5762005-09-09 13:10:07 -0700803
804 irq = platform_get_irq(pdev, 0);
805 if (irq < 0) {
Russell King3ae5eae2005-11-09 22:32:44 +0000806 dev_err(&pdev->dev, "no irq for device\n");
Arnaud Patard20fd5762005-09-09 13:10:07 -0700807 return -ENOENT;
808 }
809
Russell King3ae5eae2005-11-09 22:32:44 +0000810 fbinfo = framebuffer_alloc(sizeof(struct s3c2410fb_info), &pdev->dev);
Krzysztof Heltb0831942007-10-16 01:28:54 -0700811 if (!fbinfo)
Arnaud Patard20fd5762005-09-09 13:10:07 -0700812 return -ENOMEM;
Arnaud Patard20fd5762005-09-09 13:10:07 -0700813
Krzysztof Helt9fa7bc02007-10-16 01:29:05 -0700814 platform_set_drvdata(pdev, fbinfo);
815
Arnaud Patard20fd5762005-09-09 13:10:07 -0700816 info = fbinfo->par;
Ben Dooks0187f222007-02-16 01:28:42 -0800817 info->dev = &pdev->dev;
818
Ben Dooksaff39a82007-07-31 00:37:37 -0700819 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
820 if (res == NULL) {
Krzysztof Heltb0831942007-10-16 01:28:54 -0700821 dev_err(&pdev->dev, "failed to get memory registers\n");
Ben Dooksaff39a82007-07-31 00:37:37 -0700822 ret = -ENXIO;
823 goto dealloc_fb;
824 }
825
Krzysztof Heltb0831942007-10-16 01:28:54 -0700826 size = (res->end - res->start) + 1;
Ben Dooksaff39a82007-07-31 00:37:37 -0700827 info->mem = request_mem_region(res->start, size, pdev->name);
828 if (info->mem == NULL) {
829 dev_err(&pdev->dev, "failed to get memory region\n");
830 ret = -ENOENT;
831 goto dealloc_fb;
832 }
833
834 info->io = ioremap(res->start, size);
835 if (info->io == NULL) {
836 dev_err(&pdev->dev, "ioremap() of registers failed\n");
837 ret = -ENXIO;
838 goto release_mem;
839 }
840
Arnaud Patard20fd5762005-09-09 13:10:07 -0700841 dprintk("devinit\n");
842
843 strcpy(fbinfo->fix.id, driver_name);
844
Krzysztof Helt9fa7bc02007-10-16 01:29:05 -0700845 /* Stop the video */
Ben Dooksaff39a82007-07-31 00:37:37 -0700846 lcdcon1 = readl(info->io + S3C2410_LCDCON1);
847 writel(lcdcon1 & ~S3C2410_LCDCON1_ENVID, info->io + S3C2410_LCDCON1);
Arnaud Patard6931a762006-06-26 00:26:45 -0700848
Arnaud Patard20fd5762005-09-09 13:10:07 -0700849 fbinfo->fix.type = FB_TYPE_PACKED_PIXELS;
850 fbinfo->fix.type_aux = 0;
851 fbinfo->fix.xpanstep = 0;
852 fbinfo->fix.ypanstep = 0;
853 fbinfo->fix.ywrapstep = 0;
854 fbinfo->fix.accel = FB_ACCEL_NONE;
855
856 fbinfo->var.nonstd = 0;
857 fbinfo->var.activate = FB_ACTIVATE_NOW;
Arnaud Patard20fd5762005-09-09 13:10:07 -0700858 fbinfo->var.accel_flags = 0;
859 fbinfo->var.vmode = FB_VMODE_NONINTERLACED;
860
861 fbinfo->fbops = &s3c2410fb_ops;
862 fbinfo->flags = FBINFO_FLAG_DEFAULT;
863 fbinfo->pseudo_palette = &info->pseudo_pal;
864
Arnaud Patard20fd5762005-09-09 13:10:07 -0700865 for (i = 0; i < 256; i++)
866 info->palette_buffer[i] = PALETTE_BUFF_CLEAR;
867
Thomas Gleixner63a43392006-07-01 19:29:45 -0700868 ret = request_irq(irq, s3c2410fb_irq, IRQF_DISABLED, pdev->name, info);
Arnaud Patard20fd5762005-09-09 13:10:07 -0700869 if (ret) {
Russell King3ae5eae2005-11-09 22:32:44 +0000870 dev_err(&pdev->dev, "cannot get irq %d - err %d\n", irq, ret);
Arnaud Patard20fd5762005-09-09 13:10:07 -0700871 ret = -EBUSY;
Ben Dooksaff39a82007-07-31 00:37:37 -0700872 goto release_regs;
Arnaud Patard20fd5762005-09-09 13:10:07 -0700873 }
874
875 info->clk = clk_get(NULL, "lcd");
876 if (!info->clk || IS_ERR(info->clk)) {
877 printk(KERN_ERR "failed to get lcd clock source\n");
878 ret = -ENOENT;
879 goto release_irq;
880 }
881
Arnaud Patard20fd5762005-09-09 13:10:07 -0700882 clk_enable(info->clk);
883 dprintk("got and enabled clock\n");
884
885 msleep(1);
886
Krzysztof Helt9fa7bc02007-10-16 01:29:05 -0700887 /* find maximum required memory size for display */
888 for (i = 0; i < mach_info->num_displays; i++) {
889 unsigned long smem_len = mach_info->displays[i].xres;
890
891 smem_len *= mach_info->displays[i].yres;
892 smem_len *= mach_info->displays[i].bpp;
893 smem_len >>= 3;
894 if (fbinfo->fix.smem_len < smem_len)
895 fbinfo->fix.smem_len = smem_len;
896 }
897
Arnaud Patard20fd5762005-09-09 13:10:07 -0700898 /* Initialize video memory */
Krzysztof Helt110c1fa2007-10-16 01:28:55 -0700899 ret = s3c2410fb_map_video_memory(fbinfo);
Arnaud Patard20fd5762005-09-09 13:10:07 -0700900 if (ret) {
Krzysztof Heltb0831942007-10-16 01:28:54 -0700901 printk(KERN_ERR "Failed to allocate video RAM: %d\n", ret);
Arnaud Patard20fd5762005-09-09 13:10:07 -0700902 ret = -ENOMEM;
903 goto release_clock;
904 }
Ben Dooksaff39a82007-07-31 00:37:37 -0700905
Arnaud Patard20fd5762005-09-09 13:10:07 -0700906 dprintk("got video memory\n");
907
Krzysztof Helt9fa7bc02007-10-16 01:29:05 -0700908 fbinfo->var.xres = display->xres;
909 fbinfo->var.yres = display->yres;
910 fbinfo->var.bits_per_pixel = display->bpp;
911
Krzysztof Helt110c1fa2007-10-16 01:28:55 -0700912 s3c2410fb_init_registers(fbinfo);
Arnaud Patard20fd5762005-09-09 13:10:07 -0700913
Krzysztof Heltb0831942007-10-16 01:28:54 -0700914 s3c2410fb_check_var(&fbinfo->var, fbinfo);
Arnaud Patard20fd5762005-09-09 13:10:07 -0700915
916 ret = register_framebuffer(fbinfo);
917 if (ret < 0) {
Krzysztof Heltb0831942007-10-16 01:28:54 -0700918 printk(KERN_ERR "Failed to register framebuffer device: %d\n",
919 ret);
Arnaud Patard20fd5762005-09-09 13:10:07 -0700920 goto free_video_memory;
921 }
922
923 /* create device files */
Russell King3ae5eae2005-11-09 22:32:44 +0000924 device_create_file(&pdev->dev, &dev_attr_debug);
Arnaud Patard20fd5762005-09-09 13:10:07 -0700925
926 printk(KERN_INFO "fb%d: %s frame buffer device\n",
927 fbinfo->node, fbinfo->fix.id);
928
929 return 0;
930
931free_video_memory:
Krzysztof Helt9fa7bc02007-10-16 01:29:05 -0700932 s3c2410fb_unmap_video_memory(fbinfo);
Arnaud Patard20fd5762005-09-09 13:10:07 -0700933release_clock:
934 clk_disable(info->clk);
Arnaud Patard20fd5762005-09-09 13:10:07 -0700935 clk_put(info->clk);
936release_irq:
Krzysztof Heltb0831942007-10-16 01:28:54 -0700937 free_irq(irq, info);
Ben Dooksaff39a82007-07-31 00:37:37 -0700938release_regs:
939 iounmap(info->io);
Arnaud Patard20fd5762005-09-09 13:10:07 -0700940release_mem:
Ben Dooksaff39a82007-07-31 00:37:37 -0700941 release_resource(info->mem);
942 kfree(info->mem);
Arnaud Patard20fd5762005-09-09 13:10:07 -0700943dealloc_fb:
Krzysztof Helt9fa7bc02007-10-16 01:29:05 -0700944 platform_set_drvdata(pdev, NULL);
Arnaud Patard20fd5762005-09-09 13:10:07 -0700945 framebuffer_release(fbinfo);
946 return ret;
947}
948
949/* s3c2410fb_stop_lcd
950 *
951 * shutdown the lcd controller
Krzysztof Heltb0831942007-10-16 01:28:54 -0700952 */
Arnaud Patard6931a762006-06-26 00:26:45 -0700953static void s3c2410fb_stop_lcd(struct s3c2410fb_info *fbi)
Arnaud Patard20fd5762005-09-09 13:10:07 -0700954{
955 unsigned long flags;
Arnaud Patard20fd5762005-09-09 13:10:07 -0700956
957 local_irq_save(flags);
958
Arnaud Patard6931a762006-06-26 00:26:45 -0700959 fbi->regs.lcdcon1 &= ~S3C2410_LCDCON1_ENVID;
Ben Dooksaff39a82007-07-31 00:37:37 -0700960 writel(fbi->regs.lcdcon1, fbi->io + S3C2410_LCDCON1);
Arnaud Patard20fd5762005-09-09 13:10:07 -0700961
962 local_irq_restore(flags);
963}
964
965/*
966 * Cleanup
967 */
Russell King3ae5eae2005-11-09 22:32:44 +0000968static int s3c2410fb_remove(struct platform_device *pdev)
Arnaud Patard20fd5762005-09-09 13:10:07 -0700969{
Krzysztof Heltb0831942007-10-16 01:28:54 -0700970 struct fb_info *fbinfo = platform_get_drvdata(pdev);
Arnaud Patard20fd5762005-09-09 13:10:07 -0700971 struct s3c2410fb_info *info = fbinfo->par;
972 int irq;
973
Krzysztof Helt9fa7bc02007-10-16 01:29:05 -0700974 unregister_framebuffer(fbinfo);
975
Arnaud Patard6931a762006-06-26 00:26:45 -0700976 s3c2410fb_stop_lcd(info);
Arnaud Patard20fd5762005-09-09 13:10:07 -0700977 msleep(1);
978
Krzysztof Helt9fa7bc02007-10-16 01:29:05 -0700979 s3c2410fb_unmap_video_memory(fbinfo);
Arnaud Patard20fd5762005-09-09 13:10:07 -0700980
Krzysztof Heltb0831942007-10-16 01:28:54 -0700981 if (info->clk) {
982 clk_disable(info->clk);
983 clk_put(info->clk);
984 info->clk = NULL;
Arnaud Patard20fd5762005-09-09 13:10:07 -0700985 }
986
987 irq = platform_get_irq(pdev, 0);
Krzysztof Heltb0831942007-10-16 01:28:54 -0700988 free_irq(irq, info);
Ben Dooksaff39a82007-07-31 00:37:37 -0700989
Krzysztof Helt9fa7bc02007-10-16 01:29:05 -0700990 iounmap(info->io);
991
Ben Dooksaff39a82007-07-31 00:37:37 -0700992 release_resource(info->mem);
993 kfree(info->mem);
Krzysztof Helt9fa7bc02007-10-16 01:29:05 -0700994
995 platform_set_drvdata(pdev, NULL);
996 framebuffer_release(fbinfo);
Arnaud Patard20fd5762005-09-09 13:10:07 -0700997
998 return 0;
999}
1000
1001#ifdef CONFIG_PM
1002
1003/* suspend and resume support for the lcd controller */
Russell King3ae5eae2005-11-09 22:32:44 +00001004static int s3c2410fb_suspend(struct platform_device *dev, pm_message_t state)
Arnaud Patard20fd5762005-09-09 13:10:07 -07001005{
Russell King3ae5eae2005-11-09 22:32:44 +00001006 struct fb_info *fbinfo = platform_get_drvdata(dev);
Arnaud Patard20fd5762005-09-09 13:10:07 -07001007 struct s3c2410fb_info *info = fbinfo->par;
1008
Arnaud Patard6931a762006-06-26 00:26:45 -07001009 s3c2410fb_stop_lcd(info);
Arnaud Patard20fd5762005-09-09 13:10:07 -07001010
Russell King9480e302005-10-28 09:52:56 -07001011 /* sleep before disabling the clock, we need to ensure
1012 * the LCD DMA engine is not going to get back on the bus
1013 * before the clock goes off again (bjd) */
Arnaud Patard20fd5762005-09-09 13:10:07 -07001014
Russell King9480e302005-10-28 09:52:56 -07001015 msleep(1);
1016 clk_disable(info->clk);
Arnaud Patard20fd5762005-09-09 13:10:07 -07001017
1018 return 0;
1019}
1020
Russell King3ae5eae2005-11-09 22:32:44 +00001021static int s3c2410fb_resume(struct platform_device *dev)
Arnaud Patard20fd5762005-09-09 13:10:07 -07001022{
Russell King3ae5eae2005-11-09 22:32:44 +00001023 struct fb_info *fbinfo = platform_get_drvdata(dev);
Arnaud Patard20fd5762005-09-09 13:10:07 -07001024 struct s3c2410fb_info *info = fbinfo->par;
1025
Russell King9480e302005-10-28 09:52:56 -07001026 clk_enable(info->clk);
1027 msleep(1);
Arnaud Patard20fd5762005-09-09 13:10:07 -07001028
Russell King9480e302005-10-28 09:52:56 -07001029 s3c2410fb_init_registers(info);
Arnaud Patard20fd5762005-09-09 13:10:07 -07001030
1031 return 0;
1032}
1033
1034#else
1035#define s3c2410fb_suspend NULL
1036#define s3c2410fb_resume NULL
1037#endif
1038
Russell King3ae5eae2005-11-09 22:32:44 +00001039static struct platform_driver s3c2410fb_driver = {
Arnaud Patard20fd5762005-09-09 13:10:07 -07001040 .probe = s3c2410fb_probe,
Russell King3ae5eae2005-11-09 22:32:44 +00001041 .remove = s3c2410fb_remove,
Arnaud Patard20fd5762005-09-09 13:10:07 -07001042 .suspend = s3c2410fb_suspend,
1043 .resume = s3c2410fb_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00001044 .driver = {
1045 .name = "s3c2410-lcd",
1046 .owner = THIS_MODULE,
1047 },
Arnaud Patard20fd5762005-09-09 13:10:07 -07001048};
1049
Krzysztof Helt9fa7bc02007-10-16 01:29:05 -07001050int __init s3c2410fb_init(void)
Arnaud Patard20fd5762005-09-09 13:10:07 -07001051{
Russell King3ae5eae2005-11-09 22:32:44 +00001052 return platform_driver_register(&s3c2410fb_driver);
Arnaud Patard20fd5762005-09-09 13:10:07 -07001053}
1054
1055static void __exit s3c2410fb_cleanup(void)
1056{
Russell King3ae5eae2005-11-09 22:32:44 +00001057 platform_driver_unregister(&s3c2410fb_driver);
Arnaud Patard20fd5762005-09-09 13:10:07 -07001058}
1059
Arnaud Patard20fd5762005-09-09 13:10:07 -07001060module_init(s3c2410fb_init);
1061module_exit(s3c2410fb_cleanup);
1062
Krzysztof Heltb0831942007-10-16 01:28:54 -07001063MODULE_AUTHOR("Arnaud Patard <arnaud.patard@rtp-net.org>, "
1064 "Ben Dooks <ben-linux@fluff.org>");
Arnaud Patard20fd5762005-09-09 13:10:07 -07001065MODULE_DESCRIPTION("Framebuffer driver for the s3c2410");
1066MODULE_LICENSE("GPL");