s3c2410fb.c 27.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*
 * linux/drivers/video/s3c2410fb.c
 *	Copyright (c) Arnaud Patard, Ben Dooks
 *
 * This file is subject to the terms and conditions of the GNU General Public
 * License.  See the file COPYING in the main directory of this archive for
 * more details.
 *
 *	    S3C2410 LCD Controller Frame Buffer Driver
 *	    based on skeletonfb.c, sa1100fb.c and others
 *
 * ChangeLog
 * 2005-04-07: Arnaud Patard <arnaud.patard@rtp-net.org>
 *      - u32 state -> pm_message_t state
 *      - S3C2410_{VA,SZ}_LCD -> S3C24XX
 *
 * 2005-03-15: Arnaud Patard <arnaud.patard@rtp-net.org>
 *      - Removed the ioctl
 *      - use readl/writel instead of __raw_writel/__raw_readl
 *
 * 2004-12-04: Arnaud Patard <arnaud.patard@rtp-net.org>
 *      - Added the possibility to set on or off the
23
 *      debugging messages
24
25
26
27
28
29
30
31
32
33
 *      - Replaced 0 and 1 by on or off when reading the
 *      /sys files
 *
 * 2005-03-23: Ben Dooks <ben-linux@fluff.org>
 *	- added non 16bpp modes
 *	- updated platform information for range of x/y/bpp
 *	- add code to ensure palette is written correctly
 *	- add pixel clock divisor control
 *
 * 2004-11-11: Arnaud Patard <arnaud.patard@rtp-net.org>
34
 *	- Removed the use of currcon as it no more exists
Krzysztof Helt's avatar
Krzysztof Helt committed
35
 *	- Added LCD power sysfs interface
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
 *
 * 2004-11-03: Ben Dooks <ben-linux@fluff.org>
 *	- minor cleanups
 *	- add suspend/resume support
 *	- s3c2410fb_setcolreg() not valid in >8bpp modes
 *	- removed last CONFIG_FB_S3C2410_FIXED
 *	- ensure lcd controller stopped before cleanup
 *	- added sysfs interface for backlight power
 *	- added mask for gpio configuration
 *	- ensured IRQs disabled during GPIO configuration
 *	- disable TPAL before enabling video
 *
 * 2004-09-20: Arnaud Patard <arnaud.patard@rtp-net.org>
 *      - Suppress command line options
 *
 * 2004-09-15: Arnaud Patard <arnaud.patard@rtp-net.org>
Krzysztof Helt's avatar
Krzysztof Helt committed
52
 *	- code cleanup
53
54
 *
 * 2004-09-07: Arnaud Patard <arnaud.patard@rtp-net.org>
Krzysztof Helt's avatar
Krzysztof Helt committed
55
56
57
 *	- Renamed from h1940fb.c to s3c2410fb.c
 *	- Add support for different devices
 *	- Backlight support
58
 *
59
 * 2004-09-05: Herbert Pötzl <herbert@13thfloor.at>
60
61
62
63
64
65
66
 *	- added clock (de-)allocation code
 *	- added fixem fbmem option
 *
 * 2004-07-27: Arnaud Patard <arnaud.patard@rtp-net.org>
 *	- code cleanup
 *	- added a forgotten return in h1940fb_init
 *
67
 * 2004-07-19: Herbert Pötzl <herbert@13thfloor.at>
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
 *	- code cleanup and extended debugging
 *
 * 2004-07-15: Arnaud Patard <arnaud.patard@rtp-net.org>
 *	- First version
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/string.h>
#include <linux/mm.h>
#include <linux/slab.h>
#include <linux/delay.h>
#include <linux/fb.h>
#include <linux/init.h>
#include <linux/dma-mapping.h>
#include <linux/interrupt.h>
85
#include <linux/platform_device.h>
86
#include <linux/clk.h>
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103

#include <asm/io.h>
#include <asm/div64.h>

#include <asm/mach/map.h>
#include <asm/arch/regs-lcd.h>
#include <asm/arch/regs-gpio.h>
#include <asm/arch/fb.h>

#ifdef CONFIG_PM
#include <linux/pm.h>
#endif

#include "s3c2410fb.h"

/* Debugging stuff */
#ifdef CONFIG_FB_S3C2410_DEBUG
Krzysztof Helt's avatar
Krzysztof Helt committed
104
static int debug	= 1;
105
#else
Krzysztof Helt's avatar
Krzysztof Helt committed
106
static int debug	= 0;
107
108
109
110
111
112
#endif

#define dprintk(msg...)	if (debug) { printk(KERN_DEBUG "s3c2410fb: " msg); }

/* useful functions */

113
114
115
116
117
static int is_s3c2412(struct s3c2410fb_info *fbi)
{
	return (fbi->drv_type == DRV_S3C2412);
}

118
119
120
/* s3c2410fb_set_lcdaddr
 *
 * initialise lcd controller address pointers
Krzysztof Helt's avatar
Krzysztof Helt committed
121
 */
122
static void s3c2410fb_set_lcdaddr(struct fb_info *info)
123
124
{
	unsigned long saddr1, saddr2, saddr3;
125
126
	struct s3c2410fb_info *fbi = info->par;
	void __iomem *regs = fbi->io;
127

128
129
	saddr1  = info->fix.smem_start >> 1;
	saddr2  = info->fix.smem_start;
130
	saddr2 += info->fix.line_length * info->var.yres;
Krzysztof Helt's avatar
Krzysztof Helt committed
131
	saddr2 >>= 1;
132

Krzysztof Helt's avatar
Krzysztof Helt committed
133
	saddr3 = S3C2410_OFFSIZE(0) |
134
		 S3C2410_PAGEWIDTH((info->fix.line_length / 2) & 0x3ff);
135
136
137
138
139

	dprintk("LCDSADDR1 = 0x%08lx\n", saddr1);
	dprintk("LCDSADDR2 = 0x%08lx\n", saddr2);
	dprintk("LCDSADDR3 = 0x%08lx\n", saddr3);

140
141
142
	writel(saddr1, regs + S3C2410_LCDSADDR1);
	writel(saddr2, regs + S3C2410_LCDSADDR2);
	writel(saddr3, regs + S3C2410_LCDSADDR3);
143
144
145
146
147
}

/* s3c2410fb_calc_pixclk()
 *
 * calculate divisor for clk->pixclk
Krzysztof Helt's avatar
Krzysztof Helt committed
148
 */
149
150
151
152
153
154
static unsigned int s3c2410fb_calc_pixclk(struct s3c2410fb_info *fbi,
					  unsigned long pixclk)
{
	unsigned long clk = clk_get_rate(fbi->clk);
	unsigned long long div;

155
	/* pixclk is in picoseconds, our clock is in Hz
156
157
158
159
160
	 *
	 * Hz -> picoseconds is / 10^-12
	 */

	div = (unsigned long long)clk * pixclk;
161
162
	div >>= 12;			/* div / 2^12 */
	do_div(div, 625 * 625UL * 625); /* div / 5^12 */
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177

	dprintk("pixclk %ld, divisor is %ld\n", pixclk, (long)div);
	return div;
}

/*
 *	s3c2410fb_check_var():
 *	Get the video params out of 'var'. If a value doesn't fit, round it up,
 *	if it's too big, return -EINVAL.
 *
 */
static int s3c2410fb_check_var(struct fb_var_screeninfo *var,
			       struct fb_info *info)
{
	struct s3c2410fb_info *fbi = info->par;
178
	struct s3c2410fb_mach_info *mach_info = fbi->dev->platform_data;
179
	struct s3c2410fb_display *display = NULL;
180
181
182
	struct s3c2410fb_display *default_display = mach_info->displays +
						    mach_info->default_display;
	int type = default_display->type;
183
	unsigned i;
184
185
186
187

	dprintk("check_var(var=%p, info=%p)\n", var, info);

	/* validate x/y resolution */
188
189
190
191
192
193
194
195
196
197
198
199
200
201
	/* choose default mode if possible */
	if (var->yres == default_display->yres &&
	    var->xres == default_display->xres &&
	    var->bits_per_pixel == default_display->bpp)
		display = default_display;
	else
		for (i = 0; i < mach_info->num_displays; i++)
			if (type == mach_info->displays[i].type &&
			    var->yres == mach_info->displays[i].yres &&
			    var->xres == mach_info->displays[i].xres &&
			    var->bits_per_pixel == mach_info->displays[i].bpp) {
				display = mach_info->displays + i;
				break;
			}
202

203
204
205
206
207
	if (!display) {
		dprintk("wrong resolution or depth %dx%d at %d bpp\n",
			var->xres, var->yres, var->bits_per_pixel);
		return -EINVAL;
	}
208

209
210
211
	/* it is always the size as the display */
	var->xres_virtual = display->xres;
	var->yres_virtual = display->yres;
212
213
	var->height = display->height;
	var->width = display->width;
214
215

	/* copy lcd settings */
216
	var->pixclock = display->pixclock;
217
218
	var->left_margin = display->left_margin;
	var->right_margin = display->right_margin;
219
220
221
222
223
224
225
	var->upper_margin = display->upper_margin;
	var->lower_margin = display->lower_margin;
	var->vsync_len = display->vsync_len;
	var->hsync_len = display->hsync_len;

	fbi->regs.lcdcon5 = display->lcdcon5;
	/* set display type */
226
	fbi->regs.lcdcon1 = display->type;
227

Krzysztof Helt's avatar
Krzysztof Helt committed
228
229
	var->transp.offset = 0;
	var->transp.length = 0;
230
	/* set r/g/b positions */
231
	switch (var->bits_per_pixel) {
Krzysztof Helt's avatar
Krzysztof Helt committed
232
233
234
235
236
237
238
239
240
	case 1:
	case 2:
	case 4:
		var->red.offset	= 0;
		var->red.length	= var->bits_per_pixel;
		var->green	= var->red;
		var->blue	= var->red;
		break;
	case 8:
241
		if (display->type != S3C2410_LCDCON1_TFT) {
Krzysztof Helt's avatar
Krzysztof Helt committed
242
243
244
245
246
247
			/* 8 bpp 332 */
			var->red.length		= 3;
			var->red.offset		= 5;
			var->green.length	= 3;
			var->green.offset	= 2;
			var->blue.length	= 2;
248
			var->blue.offset	= 0;
Krzysztof Helt's avatar
Krzysztof Helt committed
249
250
		} else {
			var->red.offset		= 0;
251
			var->red.length		= 8;
Krzysztof Helt's avatar
Krzysztof Helt committed
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
			var->green		= var->red;
			var->blue		= var->red;
		}
		break;
	case 12:
		/* 12 bpp 444 */
		var->red.length		= 4;
		var->red.offset		= 8;
		var->green.length	= 4;
		var->green.offset	= 4;
		var->blue.length	= 4;
		var->blue.offset	= 0;
		break;

	default:
	case 16:
268
		if (display->lcdcon5 & S3C2410_LCDCON5_FRM565) {
Krzysztof Helt's avatar
Krzysztof Helt committed
269
270
271
			/* 16 bpp, 565 format */
			var->red.offset		= 11;
			var->green.offset	= 5;
272
			var->blue.offset	= 0;
Krzysztof Helt's avatar
Krzysztof Helt committed
273
274
275
276
277
278
279
280
281
282
283
284
285
			var->red.length		= 5;
			var->green.length	= 6;
			var->blue.length	= 5;
		} else {
			/* 16 bpp, 5551 format */
			var->red.offset		= 11;
			var->green.offset	= 6;
			var->blue.offset	= 1;
			var->red.length		= 5;
			var->green.length	= 5;
			var->blue.length	= 5;
		}
		break;
286
287
	case 32:
		/* 24 bpp 888 and 8 dummy */
Krzysztof Helt's avatar
Krzysztof Helt committed
288
289
290
291
292
293
294
		var->red.length		= 8;
		var->red.offset		= 16;
		var->green.length	= 8;
		var->green.offset	= 8;
		var->blue.length	= 8;
		var->blue.offset	= 0;
		break;
295
	}
296
297
298
	return 0;
}

299
/* s3c2410fb_calculate_stn_lcd_regs
300
 *
301
 * calculate register values from var settings
Krzysztof Helt's avatar
Krzysztof Helt committed
302
 */
303
304
static void s3c2410fb_calculate_stn_lcd_regs(const struct fb_info *info,
					     struct s3c2410fb_hw *regs)
305
{
306
307
308
309
310
	const struct s3c2410fb_info *fbi = info->par;
	const struct fb_var_screeninfo *var = &info->var;
	int type = regs->lcdcon1 & ~S3C2410_LCDCON1_TFT;
	int hs = var->xres >> 2;
	unsigned wdly = (var->left_margin >> 4) - 1;
311
	unsigned wlh = (var->hsync_len >> 4) - 1;
312

313
314
	if (type != S3C2410_LCDCON1_STN4)
		hs >>= 1;
315

316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
	switch (var->bits_per_pixel) {
	case 1:
		regs->lcdcon1 |= S3C2410_LCDCON1_STN1BPP;
		break;
	case 2:
		regs->lcdcon1 |= S3C2410_LCDCON1_STN2GREY;
		break;
	case 4:
		regs->lcdcon1 |= S3C2410_LCDCON1_STN4GREY;
		break;
	case 8:
		regs->lcdcon1 |= S3C2410_LCDCON1_STN8BPP;
		hs *= 3;
		break;
	case 12:
		regs->lcdcon1 |= S3C2410_LCDCON1_STN12BPP;
		hs *= 3;
		break;
334

335
336
337
338
339
340
341
342
	default:
		/* invalid pixel depth */
		dev_err(fbi->dev, "invalid bpp %d\n",
			var->bits_per_pixel);
	}
	/* update X/Y info */
	dprintk("setting horz: lft=%d, rt=%d, sync=%d\n",
		var->left_margin, var->right_margin, var->hsync_len);
343

344
	regs->lcdcon2 = S3C2410_LCDCON2_LINEVAL(var->yres - 1);
345

346
347
	if (wdly > 3)
		wdly = 3;
348

349
350
351
	if (wlh > 3)
		wlh = 3;

352
353
354
	regs->lcdcon3 =	S3C2410_LCDCON3_WDLY(wdly) |
			S3C2410_LCDCON3_LINEBLANK(var->right_margin / 8) |
			S3C2410_LCDCON3_HOZVAL(hs - 1);
355

356
	regs->lcdcon4 = S3C2410_LCDCON4_WLH(wlh);
357
}
358

359
360
361
362
363
364
365
366
367
/* s3c2410fb_calculate_tft_lcd_regs
 *
 * calculate register values from var settings
 */
static void s3c2410fb_calculate_tft_lcd_regs(const struct fb_info *info,
					     struct s3c2410fb_hw *regs)
{
	const struct s3c2410fb_info *fbi = info->par;
	const struct fb_var_screeninfo *var = &info->var;
368

369
370
371
372
373
374
	switch (var->bits_per_pixel) {
	case 1:
		regs->lcdcon1 |= S3C2410_LCDCON1_TFT1BPP;
		break;
	case 2:
		regs->lcdcon1 |= S3C2410_LCDCON1_TFT2BPP;
Krzysztof Helt's avatar
Krzysztof Helt committed
375
		break;
376
377
	case 4:
		regs->lcdcon1 |= S3C2410_LCDCON1_TFT4BPP;
Krzysztof Helt's avatar
Krzysztof Helt committed
378
		break;
379
380
	case 8:
		regs->lcdcon1 |= S3C2410_LCDCON1_TFT8BPP;
381
382
383
		regs->lcdcon5 |= S3C2410_LCDCON5_BSWP |
				 S3C2410_LCDCON5_FRM565;
		regs->lcdcon5 &= ~S3C2410_LCDCON5_HWSWP;
384
385
386
		break;
	case 16:
		regs->lcdcon1 |= S3C2410_LCDCON1_TFT16BPP;
387
388
389
390
391
392
393
394
		regs->lcdcon5 &= ~S3C2410_LCDCON5_BSWP;
		regs->lcdcon5 |= S3C2410_LCDCON5_HWSWP;
		break;
	case 32:
		regs->lcdcon1 |= S3C2410_LCDCON1_TFT24BPP;
		regs->lcdcon5 &= ~(S3C2410_LCDCON5_BSWP |
				   S3C2410_LCDCON5_HWSWP |
				   S3C2410_LCDCON5_BPP24BL);
Krzysztof Helt's avatar
Krzysztof Helt committed
395
		break;
396
397
398
399
	default:
		/* invalid pixel depth */
		dev_err(fbi->dev, "invalid bpp %d\n",
			var->bits_per_pixel);
400
	}
401
402
403
	/* update X/Y info */
	dprintk("setting vert: up=%d, low=%d, sync=%d\n",
		var->upper_margin, var->lower_margin, var->vsync_len);
404

405
406
	dprintk("setting horz: lft=%d, rt=%d, sync=%d\n",
		var->left_margin, var->right_margin, var->hsync_len);
407

408
409
410
411
	regs->lcdcon2 = S3C2410_LCDCON2_LINEVAL(var->yres - 1) |
			S3C2410_LCDCON2_VBPD(var->upper_margin - 1) |
			S3C2410_LCDCON2_VFPD(var->lower_margin - 1) |
			S3C2410_LCDCON2_VSPW(var->vsync_len - 1);
412
413
414
415

	regs->lcdcon3 = S3C2410_LCDCON3_HBPD(var->right_margin - 1) |
			S3C2410_LCDCON3_HFPD(var->left_margin - 1) |
			S3C2410_LCDCON3_HOZVAL(var->xres - 1);
416

417
	regs->lcdcon4 = S3C2410_LCDCON4_HSPW(var->hsync_len - 1);
418
419
420
421
422
423
424
425
426
427
}

/* s3c2410fb_activate_var
 *
 * activate (set) the controller from the given framebuffer
 * information
 */
static void s3c2410fb_activate_var(struct fb_info *info)
{
	struct s3c2410fb_info *fbi = info->par;
428
	void __iomem *regs = fbi->io;
429
	int type = fbi->regs.lcdcon1 & S3C2410_LCDCON1_TFT;
430
	struct fb_var_screeninfo *var = &info->var;
431
	int clkdiv = s3c2410fb_calc_pixclk(fbi, var->pixclock) / 2;
432

433
434
435
	dprintk("%s: var->xres  = %d\n", __FUNCTION__, var->xres);
	dprintk("%s: var->yres  = %d\n", __FUNCTION__, var->yres);
	dprintk("%s: var->bpp   = %d\n", __FUNCTION__, var->bits_per_pixel);
436

437
	if (type == S3C2410_LCDCON1_TFT) {
438
		s3c2410fb_calculate_tft_lcd_regs(info, &fbi->regs);
439
440
441
442
		--clkdiv;
		if (clkdiv < 0)
			clkdiv = 0;
	} else {
443
		s3c2410fb_calculate_stn_lcd_regs(info, &fbi->regs);
444
445
446
447
448
		if (clkdiv < 2)
			clkdiv = 2;
	}

	fbi->regs.lcdcon1 |=  S3C2410_LCDCON1_CLKVAL(clkdiv);
449

450
451
452
453
454
455
456
457
458
	/* write new registers */

	dprintk("new register set:\n");
	dprintk("lcdcon[1] = 0x%08lx\n", fbi->regs.lcdcon1);
	dprintk("lcdcon[2] = 0x%08lx\n", fbi->regs.lcdcon2);
	dprintk("lcdcon[3] = 0x%08lx\n", fbi->regs.lcdcon3);
	dprintk("lcdcon[4] = 0x%08lx\n", fbi->regs.lcdcon4);
	dprintk("lcdcon[5] = 0x%08lx\n", fbi->regs.lcdcon5);

459
460
461
462
463
464
	writel(fbi->regs.lcdcon1 & ~S3C2410_LCDCON1_ENVID,
		regs + S3C2410_LCDCON1);
	writel(fbi->regs.lcdcon2, regs + S3C2410_LCDCON2);
	writel(fbi->regs.lcdcon3, regs + S3C2410_LCDCON3);
	writel(fbi->regs.lcdcon4, regs + S3C2410_LCDCON4);
	writel(fbi->regs.lcdcon5, regs + S3C2410_LCDCON5);
465
466

	/* set lcd address pointers */
467
	s3c2410fb_set_lcdaddr(info);
468

469
	fbi->regs.lcdcon1 |= S3C2410_LCDCON1_ENVID,
470
	writel(fbi->regs.lcdcon1, regs + S3C2410_LCDCON1);
471
472
473
}

/*
Krzysztof Helt's avatar
Krzysztof Helt committed
474
 *      s3c2410fb_set_par - Alters the hardware state.
475
476
477
478
479
480
481
 *      @info: frame buffer structure that represents a single frame buffer
 *
 */
static int s3c2410fb_set_par(struct fb_info *info)
{
	struct fb_var_screeninfo *var = &info->var;

Krzysztof Helt's avatar
Krzysztof Helt committed
482
	switch (var->bits_per_pixel) {
483
	case 32:
Krzysztof Helt's avatar
Krzysztof Helt committed
484
	case 16:
485
	case 12:
Krzysztof Helt's avatar
Krzysztof Helt committed
486
487
488
489
490
491
492
493
		info->fix.visual = FB_VISUAL_TRUECOLOR;
		break;
	case 1:
		info->fix.visual = FB_VISUAL_MONO01;
		break;
	default:
		info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
		break;
494
	}
495

496
	info->fix.line_length = (var->xres_virtual * var->bits_per_pixel) / 8;
497
498
499

	/* activate this new configuration */

500
	s3c2410fb_activate_var(info);
501
502
503
504
505
506
507
508
	return 0;
}

static void schedule_palette_update(struct s3c2410fb_info *fbi,
				    unsigned int regno, unsigned int val)
{
	unsigned long flags;
	unsigned long irqen;
509
	void __iomem *irq_base = fbi->irq_base;
510
511
512
513
514
515
516
517
518

	local_irq_save(flags);

	fbi->palette_buffer[regno] = val;

	if (!fbi->palette_ready) {
		fbi->palette_ready = 1;

		/* enable IRQ */
519
		irqen = readl(irq_base + S3C24XX_LCDINTMSK);
520
		irqen &= ~S3C2410_LCDINT_FRSYNC;
521
		writel(irqen, irq_base + S3C24XX_LCDINTMSK);
522
523
524
525
526
527
	}

	local_irq_restore(flags);
}

/* from pxafb.c */
Krzysztof Helt's avatar
Krzysztof Helt committed
528
529
static inline unsigned int chan_to_field(unsigned int chan,
					 struct fb_bitfield *bf)
530
531
532
533
534
535
536
537
538
539
540
{
	chan &= 0xffff;
	chan >>= 16 - bf->length;
	return chan << bf->offset;
}

static int s3c2410fb_setcolreg(unsigned regno,
			       unsigned red, unsigned green, unsigned blue,
			       unsigned transp, struct fb_info *info)
{
	struct s3c2410fb_info *fbi = info->par;
541
	void __iomem *regs = fbi->io;
542
543
	unsigned int val;

Krzysztof Helt's avatar
Krzysztof Helt committed
544
545
	/* dprintk("setcol: regno=%d, rgb=%d,%d,%d\n",
		   regno, red, green, blue); */
546

Krzysztof Helt's avatar
Krzysztof Helt committed
547
	switch (info->fix.visual) {
548
	case FB_VISUAL_TRUECOLOR:
Krzysztof Helt's avatar
Krzysztof Helt committed
549
		/* true-colour, use pseudo-palette */
550
551

		if (regno < 16) {
Krzysztof Helt's avatar
Krzysztof Helt committed
552
			u32 *pal = info->pseudo_palette;
553

Krzysztof Helt's avatar
Krzysztof Helt committed
554
555
556
			val  = chan_to_field(red,   &info->var.red);
			val |= chan_to_field(green, &info->var.green);
			val |= chan_to_field(blue,  &info->var.blue);
557
558
559
560
561
562
563
564
565

			pal[regno] = val;
		}
		break;

	case FB_VISUAL_PSEUDOCOLOR:
		if (regno < 256) {
			/* currently assume RGB 5-6-5 mode */

566
567
568
			val  = (red   >>  0) & 0xf800;
			val |= (green >>  5) & 0x07e0;
			val |= (blue  >> 11) & 0x001f;
569

570
			writel(val, regs + S3C2410_TFTPAL(regno));
571
572
573
574
575
576
			schedule_palette_update(fbi, regno, val);
		}

		break;

	default:
Krzysztof Helt's avatar
Krzysztof Helt committed
577
		return 1;	/* unknown type */
578
579
580
581
582
	}

	return 0;
}

Krzysztof Helt's avatar
Krzysztof Helt committed
583
/*
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
 *      s3c2410fb_blank
 *	@blank_mode: the blank mode we want.
 *	@info: frame buffer structure that represents a single frame buffer
 *
 *	Blank the screen if blank_mode != 0, else unblank. Return 0 if
 *	blanking succeeded, != 0 if un-/blanking failed due to e.g. a
 *	video mode which doesn't support it. Implements VESA suspend
 *	and powerdown modes on hardware that supports disabling hsync/vsync:
 *	blank_mode == 2: suspend vsync
 *	blank_mode == 3: suspend hsync
 *	blank_mode == 4: powerdown
 *
 *	Returns negative errno on error, or zero on success.
 *
 */
static int s3c2410fb_blank(int blank_mode, struct fb_info *info)
{
601
	struct s3c2410fb_info *fbi = info->par;
602
	void __iomem *tpal_reg = fbi->io;
603

604
605
	dprintk("blank(mode=%d, info=%p)\n", blank_mode, info);

606
607
	tpal_reg += is_s3c2412(fbi) ? S3C2412_TPAL : S3C2410_TPAL;

608
	if (blank_mode == FB_BLANK_UNBLANK)
609
		writel(0x0, tpal_reg);
610
611
	else {
		dprintk("setting TPAL to output 0x000000\n");
612
		writel(S3C2410_TPAL_EN, tpal_reg);
613
614
615
616
617
	}

	return 0;
}

Krzysztof Helt's avatar
Krzysztof Helt committed
618
619
static int s3c2410fb_debug_show(struct device *dev,
				struct device_attribute *attr, char *buf)
620
621
622
{
	return snprintf(buf, PAGE_SIZE, "%s\n", debug ? "on" : "off");
}
623

Krzysztof Helt's avatar
Krzysztof Helt committed
624
625
626
static int s3c2410fb_debug_store(struct device *dev,
				 struct device_attribute *attr,
				 const char *buf, size_t len)
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
{
	if (len < 1)
		return -EINVAL;

	if (strnicmp(buf, "on", 2) == 0 ||
	    strnicmp(buf, "1", 1) == 0) {
		debug = 1;
		printk(KERN_DEBUG "s3c2410fb: Debug On");
	} else if (strnicmp(buf, "off", 3) == 0 ||
		   strnicmp(buf, "0", 1) == 0) {
		debug = 0;
		printk(KERN_DEBUG "s3c2410fb: Debug Off");
	} else {
		return -EINVAL;
	}

	return len;
}

Krzysztof Helt's avatar
Krzysztof Helt committed
646
static DEVICE_ATTR(debug, 0666, s3c2410fb_debug_show, s3c2410fb_debug_store);
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666

static struct fb_ops s3c2410fb_ops = {
	.owner		= THIS_MODULE,
	.fb_check_var	= s3c2410fb_check_var,
	.fb_set_par	= s3c2410fb_set_par,
	.fb_blank	= s3c2410fb_blank,
	.fb_setcolreg	= s3c2410fb_setcolreg,
	.fb_fillrect	= cfb_fillrect,
	.fb_copyarea	= cfb_copyarea,
	.fb_imageblit	= cfb_imageblit,
};

/*
 * s3c2410fb_map_video_memory():
 *	Allocates the DRAM memory for the frame buffer.  This buffer is
 *	remapped into a non-cached, non-buffered, memory region to
 *	allow palette and pixel writes to occur without flushing the
 *	cache.  Once this area is remapped, all virtual memory
 *	access to the video memory should occur at the new region.
 */
667
static int __init s3c2410fb_map_video_memory(struct fb_info *info)
668
{
669
	struct s3c2410fb_info *fbi = info->par;
670
671
	dma_addr_t map_dma;
	unsigned map_size = PAGE_ALIGN(info->fix.smem_len);
672

673
	dprintk("map_video_memory(fbi=%p) map_size %u\n", fbi, map_size);
674

675
676
	info->screen_base = dma_alloc_writecombine(fbi->dev, map_size,
						   &map_dma, GFP_KERNEL);
677

678
	if (info->screen_base) {
679
680
		/* prevent initial garbage on screen */
		dprintk("map_video_memory: clear %p:%08x\n",
681
			info->screen_base, map_size);
682
		memset(info->screen_base, 0x00, map_size);
683

684
		info->fix.smem_start = map_dma;
685

686
687
		dprintk("map_video_memory: dma=%08lx cpu=%p size=%08x\n",
			info->fix.smem_start, info->screen_base, map_size);
688
689
	}

690
	return info->screen_base ? 0 : -ENOMEM;
691
692
}

693
static inline void s3c2410fb_unmap_video_memory(struct fb_info *info)
694
{
695
696
697
698
	struct s3c2410fb_info *fbi = info->par;

	dma_free_writecombine(fbi->dev, PAGE_ALIGN(info->fix.smem_len),
			      info->screen_base, info->fix.smem_start);
699
700
701
702
703
704
705
706
707
708
709
710
711
712
}

static inline void modify_gpio(void __iomem *reg,
			       unsigned long set, unsigned long mask)
{
	unsigned long tmp;

	tmp = readl(reg) & ~mask;
	writel(tmp | set, reg);
}

/*
 * s3c2410fb_init_registers - Initialise all LCD-related registers
 */
713
static int s3c2410fb_init_registers(struct fb_info *info)
714
{
715
	struct s3c2410fb_info *fbi = info->par;
716
	struct s3c2410fb_mach_info *mach_info = fbi->dev->platform_data;
717
	unsigned long flags;
Ben Dooks's avatar
Ben Dooks committed
718
	void __iomem *regs = fbi->io;
719
720
721
722
723
724
725
726
727
728
	void __iomem *tpal;
	void __iomem *lpcsel;

	if (is_s3c2412(fbi)) {
		tpal = regs + S3C2412_TPAL;
		lpcsel = regs + S3C2412_TCONSEL;
	} else {
		tpal = regs + S3C2410_TPAL;
		lpcsel = regs + S3C2410_LPCSEL;
	}
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743

	/* Initialise LCD with values from haret */

	local_irq_save(flags);

	/* modify the gpio(s) with interrupts set (bjd) */

	modify_gpio(S3C2410_GPCUP,  mach_info->gpcup,  mach_info->gpcup_mask);
	modify_gpio(S3C2410_GPCCON, mach_info->gpccon, mach_info->gpccon_mask);
	modify_gpio(S3C2410_GPDUP,  mach_info->gpdup,  mach_info->gpdup_mask);
	modify_gpio(S3C2410_GPDCON, mach_info->gpdcon, mach_info->gpdcon_mask);

	local_irq_restore(flags);

	dprintk("LPCSEL    = 0x%08lx\n", mach_info->lpcsel);
744
	writel(mach_info->lpcsel, lpcsel);
745

746
	dprintk("replacing TPAL %08x\n", readl(tpal));
747
748

	/* ensure temporary palette disabled */
749
	writel(0x00, tpal);
750
751
752
753
754
755
756

	return 0;
}

static void s3c2410fb_write_palette(struct s3c2410fb_info *fbi)
{
	unsigned int i;
Ben Dooks's avatar
Ben Dooks committed
757
	void __iomem *regs = fbi->io;
758
759
760
761

	fbi->palette_ready = 0;

	for (i = 0; i < 256; i++) {
Krzysztof Helt's avatar
Krzysztof Helt committed
762
763
		unsigned long ent = fbi->palette_buffer[i];
		if (ent == PALETTE_BUFF_CLEAR)
764
765
			continue;

Ben Dooks's avatar
Ben Dooks committed
766
		writel(ent, regs + S3C2410_TFTPAL(i));
767
768
769
770
771
772

		/* it seems the only way to know exactly
		 * if the palette wrote ok, is to check
		 * to see if the value verifies ok
		 */

Ben Dooks's avatar
Ben Dooks committed
773
		if (readw(regs + S3C2410_TFTPAL(i)) == ent)
774
775
776
777
778
779
			fbi->palette_buffer[i] = PALETTE_BUFF_CLEAR;
		else
			fbi->palette_ready = 1;   /* retry */
	}
}

780
static irqreturn_t s3c2410fb_irq(int irq, void *dev_id)
781
782
{
	struct s3c2410fb_info *fbi = dev_id;
783
784
	void __iomem *irq_base = fbi->irq_base;
	unsigned long lcdirq = readl(irq_base + S3C24XX_LCDINTPND);
785
786
787
788
789

	if (lcdirq & S3C2410_LCDINT_FRSYNC) {
		if (fbi->palette_ready)
			s3c2410fb_write_palette(fbi);

790
791
		writel(S3C2410_LCDINT_FRSYNC, irq_base + S3C24XX_LCDINTPND);
		writel(S3C2410_LCDINT_FRSYNC, irq_base + S3C24XX_LCDSRCPND);
792
793
794
795
796
	}

	return IRQ_HANDLED;
}

Krzysztof Helt's avatar
Krzysztof Helt committed
797
static char driver_name[] = "s3c2410fb";
798

799
800
static int __init s3c24xxfb_probe(struct platform_device *pdev,
				  enum s3c_drv_type drv_type)
801
802
{
	struct s3c2410fb_info *info;
803
	struct s3c2410fb_display *display;
Krzysztof Helt's avatar
Krzysztof Helt committed
804
	struct fb_info *fbinfo;
805
	struct s3c2410fb_mach_info *mach_info;
Ben Dooks's avatar
Ben Dooks committed
806
	struct resource *res;
807
808
809
	int ret;
	int irq;
	int i;
Ben Dooks's avatar
Ben Dooks committed
810
	int size;
Arnaud Patard's avatar
Arnaud Patard committed
811
	u32 lcdcon1;
812

813
	mach_info = pdev->dev.platform_data;
814
	if (mach_info == NULL) {
Krzysztof Helt's avatar
Krzysztof Helt committed
815
816
		dev_err(&pdev->dev,
			"no platform data for lcd, cannot attach\n");
817
818
819
		return -EINVAL;
	}

820
	display = mach_info->displays + mach_info->default_display;
821
822
823

	irq = platform_get_irq(pdev, 0);
	if (irq < 0) {
824
		dev_err(&pdev->dev, "no irq for device\n");
825
826
827
		return -ENOENT;
	}

828
	fbinfo = framebuffer_alloc(sizeof(struct s3c2410fb_info), &pdev->dev);
Krzysztof Helt's avatar
Krzysztof Helt committed
829
	if (!fbinfo)
830
831
		return -ENOMEM;

832
833
	platform_set_drvdata(pdev, fbinfo);

834
	info = fbinfo->par;
835
	info->dev = &pdev->dev;
836
	info->drv_type = drv_type;
837

Ben Dooks's avatar
Ben Dooks committed
838
839
	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (res == NULL) {
Krzysztof Helt's avatar
Krzysztof Helt committed
840
		dev_err(&pdev->dev, "failed to get memory registers\n");
Ben Dooks's avatar
Ben Dooks committed
841
842
843
844
		ret = -ENXIO;
		goto dealloc_fb;
	}

Krzysztof Helt's avatar
Krzysztof Helt committed
845
	size = (res->end - res->start) + 1;
Ben Dooks's avatar
Ben Dooks committed
846
847
848
849
850
851
852
853
854
855
856
857
858
859
	info->mem = request_mem_region(res->start, size, pdev->name);
	if (info->mem == NULL) {
		dev_err(&pdev->dev, "failed to get memory region\n");
		ret = -ENOENT;
		goto dealloc_fb;
	}

	info->io = ioremap(res->start, size);
	if (info->io == NULL) {
		dev_err(&pdev->dev, "ioremap() of registers failed\n");
		ret = -ENXIO;
		goto release_mem;
	}

860
861
	info->irq_base = info->io + ((drv_type == DRV_S3C2412) ? S3C2412_LCDINTBASE : S3C2410_LCDINTBASE);

862
863
864
865
	dprintk("devinit\n");

	strcpy(fbinfo->fix.id, driver_name);

866
	/* Stop the video */
Ben Dooks's avatar
Ben Dooks committed
867
868
	lcdcon1 = readl(info->io + S3C2410_LCDCON1);
	writel(lcdcon1 & ~S3C2410_LCDCON1_ENVID, info->io + S3C2410_LCDCON1);
Arnaud Patard's avatar
Arnaud Patard committed
869

870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
	fbinfo->fix.type	    = FB_TYPE_PACKED_PIXELS;
	fbinfo->fix.type_aux	    = 0;
	fbinfo->fix.xpanstep	    = 0;
	fbinfo->fix.ypanstep	    = 0;
	fbinfo->fix.ywrapstep	    = 0;
	fbinfo->fix.accel	    = FB_ACCEL_NONE;

	fbinfo->var.nonstd	    = 0;
	fbinfo->var.activate	    = FB_ACTIVATE_NOW;
	fbinfo->var.accel_flags     = 0;
	fbinfo->var.vmode	    = FB_VMODE_NONINTERLACED;

	fbinfo->fbops		    = &s3c2410fb_ops;
	fbinfo->flags		    = FBINFO_FLAG_DEFAULT;
	fbinfo->pseudo_palette      = &info->pseudo_pal;

	for (i = 0; i < 256; i++)
		info->palette_buffer[i] = PALETTE_BUFF_CLEAR;

889
	ret = request_irq(irq, s3c2410fb_irq, IRQF_DISABLED, pdev->name, info);
890
	if (ret) {
891
		dev_err(&pdev->dev, "cannot get irq %d - err %d\n", irq, ret);
892
		ret = -EBUSY;
Ben Dooks's avatar
Ben Dooks committed
893
		goto release_regs;
894
895
896
897
898
899
900
901
902
903
904
905
906
907
	}

	info->clk = clk_get(NULL, "lcd");
	if (!info->clk || IS_ERR(info->clk)) {
		printk(KERN_ERR "failed to get lcd clock source\n");
		ret = -ENOENT;
		goto release_irq;
	}

	clk_enable(info->clk);
	dprintk("got and enabled clock\n");

	msleep(1);

908
909
910
911
912
913
914
915
916
917
918
	/* find maximum required memory size for display */
	for (i = 0; i < mach_info->num_displays; i++) {
		unsigned long smem_len = mach_info->displays[i].xres;

		smem_len *= mach_info->displays[i].yres;
		smem_len *= mach_info->displays[i].bpp;
		smem_len >>= 3;
		if (fbinfo->fix.smem_len < smem_len)
			fbinfo->fix.smem_len = smem_len;
	}

919
	/* Initialize video memory */
920
	ret = s3c2410fb_map_video_memory(fbinfo);
921
	if (ret) {
Krzysztof Helt's avatar
Krzysztof Helt committed
922
		printk(KERN_ERR "Failed to allocate video RAM: %d\n", ret);
923
924
925
		ret = -ENOMEM;
		goto release_clock;
	}
Ben Dooks's avatar
Ben Dooks committed
926

927
928
	dprintk("got video memory\n");

929
930
931
932
	fbinfo->var.xres = display->xres;
	fbinfo->var.yres = display->yres;
	fbinfo->var.bits_per_pixel = display->bpp;

933
	s3c2410fb_init_registers(fbinfo);
934

Krzysztof Helt's avatar
Krzysztof Helt committed
935
	s3c2410fb_check_var(&fbinfo->var, fbinfo);
936
937
938

	ret = register_framebuffer(fbinfo);
	if (ret < 0) {
Krzysztof Helt's avatar
Krzysztof Helt committed
939
940
		printk(KERN_ERR "Failed to register framebuffer device: %d\n",
			ret);
941
942
943
944
		goto free_video_memory;
	}

	/* create device files */
945
	device_create_file(&pdev->dev, &dev_attr_debug);
946
947
948
949
950
951
952

	printk(KERN_INFO "fb%d: %s frame buffer device\n",
		fbinfo->node, fbinfo->fix.id);

	return 0;

free_video_memory:
953
	s3c2410fb_unmap_video_memory(fbinfo);
954
955
956
957
release_clock:
	clk_disable(info->clk);
	clk_put(info->clk);
release_irq:
Krzysztof Helt's avatar
Krzysztof Helt committed
958
	free_irq(irq, info);
Ben Dooks's avatar
Ben Dooks committed
959
960
release_regs:
	iounmap(info->io);
961
release_mem:
Ben Dooks's avatar
Ben Dooks committed
962
963
	release_resource(info->mem);
	kfree(info->mem);
964
dealloc_fb:
965
	platform_set_drvdata(pdev, NULL);
966
967
968
969
	framebuffer_release(fbinfo);
	return ret;
}

970
971
972
973
974
975
976
977
978
979
static int __init s3c2410fb_probe(struct platform_device *pdev)
{
	return s3c24xxfb_probe(pdev, DRV_S3C2410);
}

static int __init s3c2412fb_probe(struct platform_device *pdev)
{
	return s3c24xxfb_probe(pdev, DRV_S3C2412);
}

980
981
982
/* s3c2410fb_stop_lcd
 *
 * shutdown the lcd controller
Krzysztof Helt's avatar
Krzysztof Helt committed
983
 */
Arnaud Patard's avatar
Arnaud Patard committed
984
static void s3c2410fb_stop_lcd(struct s3c2410fb_info *fbi)
985
986
987
988
989
{
	unsigned long flags;

	local_irq_save(flags);

Arnaud Patard's avatar
Arnaud Patard committed
990
	fbi->regs.lcdcon1 &= ~S3C2410_LCDCON1_ENVID;
Ben Dooks's avatar
Ben Dooks committed
991
	writel(fbi->regs.lcdcon1, fbi->io + S3C2410_LCDCON1);
992
993
994
995
996
997
998

	local_irq_restore(flags);
}

/*
 *  Cleanup
 */
999
static int s3c2410fb_remove(struct platform_device *pdev)
1000
{
Krzysztof Helt's avatar
Krzysztof Helt committed
1001
	struct fb_info *fbinfo = platform_get_drvdata(pdev);
1002
1003
1004
	struct s3c2410fb_info *info = fbinfo->par;
	int irq;

1005
1006
	unregister_framebuffer(fbinfo);

Arnaud Patard's avatar
Arnaud Patard committed
1007
	s3c2410fb_stop_lcd(info);
1008
1009
	msleep(1);

1010
	s3c2410fb_unmap_video_memory(fbinfo);
1011

Krzysztof Helt's avatar
Krzysztof Helt committed
1012
1013
1014
1015
	if (info->clk) {
		clk_disable(info->clk);
		clk_put(info->clk);
		info->clk = NULL;
1016
1017
1018
	}

	irq = platform_get_irq(pdev, 0);
Krzysztof Helt's avatar
Krzysztof Helt committed
1019
	free_irq(irq, info);
Ben Dooks's avatar
Ben Dooks committed
1020

1021
1022
	iounmap(info->io);

Ben Dooks's avatar
Ben Dooks committed
1023
1024
	release_resource(info->mem);
	kfree(info->mem);
1025
1026
1027

	platform_set_drvdata(pdev, NULL);
	framebuffer_release(fbinfo);
1028
1029
1030
1031
1032
1033
1034

	return 0;
}

#ifdef CONFIG_PM

/* suspend and resume support for the lcd controller */
1035
static int s3c2410fb_suspend(struct platform_device *dev, pm_message_t state)
1036
{
1037
	struct fb_info	   *fbinfo = platform_get_drvdata(dev);
1038
1039
	struct s3c2410fb_info *info = fbinfo->par;

Arnaud Patard's avatar
Arnaud Patard committed
1040
	s3c2410fb_stop_lcd(info);
1041

1042
1043
1044
	/* sleep before disabling the clock, we need to ensure
	 * the LCD DMA engine is not going to get back on the bus
	 * before the clock goes off again (bjd) */
1045

1046
1047
	msleep(1);
	clk_disable(info->clk);
1048
1049
1050
1051

	return 0;
}

1052
static int s3c2410fb_resume(struct platform_device *dev)
1053
{
1054
	struct fb_info	   *fbinfo = platform_get_drvdata(dev);
1055
1056
	struct s3c2410fb_info *info = fbinfo->par;

1057
1058
	clk_enable(info->clk);
	msleep(1);
1059

1060
	s3c2410fb_init_registers(fbinfo);
1061
1062
1063
1064
1065
1066
1067
1068
1069

	return 0;
}

#else
#define s3c2410fb_suspend NULL
#define s3c2410fb_resume  NULL
#endif

1070
static struct platform_driver s3c2410fb_driver = {
1071
	.probe		= s3c2410fb_probe,
1072
	.remove		= s3c2410fb_remove,
1073
1074
	.suspend	= s3c2410fb_suspend,
	.resume		= s3c2410fb_resume,
1075
1076
1077
1078
	.driver		= {
		.name	= "s3c2410-lcd",
		.owner	= THIS_MODULE,
	},
1079
1080
};

1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
static struct platform_driver s3c2412fb_driver = {
	.probe		= s3c2412fb_probe,
	.remove		= s3c2410fb_remove,
	.suspend	= s3c2410fb_suspend,
	.resume		= s3c2410fb_resume,
	.driver		= {
		.name	= "s3c2412-lcd",
		.owner	= THIS_MODULE,
	},
};

1092
int __init s3c2410fb_init(void)
1093
{
1094
1095
1096
1097
1098
1099
	int ret = platform_driver_register(&s3c2410fb_driver);

	if (ret == 0)
		ret = platform_driver_register(&s3c2412fb_driver);;

	return ret;
1100
1101
1102
1103
}

static void __exit s3c2410fb_cleanup(void)
{
1104
	platform_driver_unregister(&s3c2410fb_driver);
1105
	platform_driver_unregister(&s3c2412fb_driver);
1106
1107
1108
1109
1110
}

module_init(s3c2410fb_init);
module_exit(s3c2410fb_cleanup);

Krzysztof Helt's avatar
Krzysztof Helt committed
1111
1112
MODULE_AUTHOR("Arnaud Patard <arnaud.patard@rtp-net.org>, "
	      "Ben Dooks <ben-linux@fluff.org>");
1113
1114
MODULE_DESCRIPTION("Framebuffer driver for the s3c2410");
MODULE_LICENSE("GPL");