⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 s3c2410fb.c

📁 嵌入式Linux C语言应用程序设计
💻 C
字号:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/string.h>
#include <linux/mm.h>
#include <linux/tty.h>
#include <linux/slab.h>
#include <linux/delay.h>
#include <linux/fb.h>
#include <linux/init.h>
#include <linux/dma-mapping.h>
#include <linux/string.h>
#include <asm/io.h>
#include <asm/uaccess.h>
#include <asm/mach/map.h>
#include <asm/arch/regs-lcd.h>
#include <asm/arch/regs-gpio.h>
#include <asm-arm/arch-s3c2410/s3c2410fb.h>
#include <asm/hardware/clock.h>

#include "s3c2410fb.h"

static struct s3c2410fb_info info;
static struct s3c2410fb_mach_info *mach_info;

/* backlight and power control functions */
static int lcd_power	   = 1;

/* Debugging stuff */
#ifdef CONFIG_FB_S3C2410_DEBUG
static int debug	   = 1;
#else
static int debug	   = 0;
#endif

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



static inline void s3c2410fb_lcd_power(int to)
{
	if (mach_info == NULL)
		return;
	lcd_power = to;
	if (mach_info->lcd_power)
		(mach_info->lcd_power)(to);
}

/*
 *	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)
{
	dprintk("check_var(var=%p, info=%p)\n", var, info);
	var->red.offset		= 11;
	var->green.offset	= 5;
	var->blue.offset	= 0;
	var->red.length		= 5;
	var->green.length	= 6;
	var->blue.length	= 5;
	return 0;
}



/*
 *      s3c2410fb_set_par - Optional function. Alters the hardware state.
 *      @info: frame buffer structure that represents a single frame buffer
 *
 */

static int s3c2410fb_set_par(struct fb_info *info)
{
	struct s3c2410fb_info *fbi = (struct s3c2410fb_info *)info;
	struct fb_var_screeninfo *var = &info->var;

	/* We support only 16BPP true color */
	fbi->fb.fix.visual	    = FB_VISUAL_TRUECOLOR;
	fbi->fb.fix.line_length     = (var->width*var->bits_per_pixel)/8;
	return 0;
}

static int s3c2410fb_setcolreg(unsigned regno,
			       unsigned red, unsigned green, unsigned blue,
			       unsigned transp, struct fb_info *info)
{
	struct s3c2410fb_info *fbi = (struct s3c2410fb_info *)info;
	int bpp, m = 0;
	bpp = fbi->fb.var.bits_per_pixel;
	m = 1 << bpp;
	if (regno >= m) {
		return -EINVAL;
	}
	switch (bpp) {
	case 16:
		/* RGB 565 */
    	fbi->pseudo_pal[regno] = ((red & 0xF800)
			| ((green & 0xFC00) >> 5)
			| ((blue & 0xF800) >> 11));
		break;
	}
	return 0;
}


/**
 *	s3c2410fb_pan_display
 *	@var: frame buffer variable screen structure
 *	@info: frame buffer structure that represents a single frame buffer
 *
 *	Pan (or wrap, depending on the `vmode' field) the display using the
 *	`xoffset' and `yoffset' fields of the `var' structure.
 *	If the values don't fit, return -EINVAL.
 *
 *	Returns negative errno on error, or zero on success.
 */
static int s3c2410fb_pan_display(struct fb_var_screeninfo *var,
			     struct fb_info *info)
{
	dprintk("pan_display(var=%p, info=%p)\n", var, info);
	return 0;
}

#if 0
/**
 *      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)
{
	dprintk("blank(mode=%d, info=%p)\n", blank_mode, info);
	if (mach_info == NULL)
		return -EINVAL;
	switch (blank_mode) {
	case VESA_NO_BLANKING:	/* lcd on, backlight on */
		s3c2410fb_lcd_power(1);
		s3c2410fb_backlight_power(1);
		break;
	case VESA_VSYNC_SUSPEND: /* lcd on, backlight off */
	case VESA_HSYNC_SUSPEND:
		s3c2410fb_lcd_power(1);
		s3c2410fb_backlight_power(0);
		break;
	case VESA_POWERDOWN: /* lcd and backlight off */
		s3c2410fb_lcd_power(0);
		s3c2410fb_backlight_power(0);
		break;
	default:
		return -EINVAL;
	}
	return 0;
}
#endif

static int s3c2410fb_debug_show(struct device *dev, char *buf)
{
	return snprintf(buf, PAGE_SIZE, "%s\n", debug ? "on" : "off");
}

static int s3c2410fb_debug_store(struct device *dev,
					   const char *buf, size_t len)
{
	if (mach_info == NULL)
		return -EINVAL;
	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;
}

/* sysfs export of baclight control */
static int s3c2410fb_lcd_power_show(struct device *dev, char *buf)
{
	return snprintf(buf, PAGE_SIZE, "%s\n", lcd_power ? "on" : "off");
}

static int s3c2410fb_lcd_power_store(struct device *dev,
					   const char *buf, size_t len)
{
	if (mach_info == NULL)
		return -EINVAL;
	if (len < 1)
		return -EINVAL;
	if (strnicmp(buf, "on", 2) == 0 ||
	    strnicmp(buf, "1", 1) == 0) {
		s3c2410fb_lcd_power(1);
	} else if (strnicmp(buf, "off", 3) == 0 ||
		   strnicmp(buf, "0", 1) == 0) {
		s3c2410fb_lcd_power(0);
	} else {
		return -EINVAL;
	}
	return len;
}



static DEVICE_ATTR(debug, 0666,
		   s3c2410fb_debug_show,
		   s3c2410fb_debug_store);

static DEVICE_ATTR(lcd_power, 0644,
		   s3c2410fb_lcd_power_show,
		   s3c2410fb_lcd_power_store);

static struct fb_ops s3c2410fb_ops = {
	.owner		= THIS_MODULE,
	.fb_check_var	= s3c2410fb_check_var,
	.fb_set_par	= s3c2410fb_set_par,
#if 0
	.fb_blank	= s3c2410fb_blank,
#endif
	.fb_pan_display	= s3c2410fb_pan_display,
	.fb_setcolreg	= s3c2410fb_setcolreg,
	.fb_fillrect	= cfb_fillrect,
	.fb_copyarea	= cfb_copyarea,
	.fb_imageblit	= cfb_imageblit,
	.fb_cursor	=  soft_cursor,
};

/* Fake monspecs to fill in fbinfo structure */
/* Don't know if the values are important    */
static struct fb_monspecs monspecs __initdata = {
	.hfmin	= 30000,
	.hfmax	= 70000,
	.vfmin	= 50,
	.vfmax	= 65,
};

/*
 * 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.
 */
static int __init s3c2410fb_map_video_memory(struct s3c2410fb_info *fbi)
{
	dprintk("map_video_memory(fbi=%p)\n", fbi);
	fbi->map_size = PAGE_ALIGN(fbi->fb.fix.smem_len + PAGE_SIZE);
	fbi->map_cpu = dma_alloc_writecombine(fbi->dev, fbi->map_size,
					      &fbi->map_dma, GFP_KERNEL);
	fbi->map_size = fbi->fb.fix.smem_len;
	if (fbi->map_cpu) {
		/* prevent initial garbage on screen */
		dprintk("map_video_memory: clear %p:%08x\n",
			fbi->map_cpu, fbi->map_size);
		memset(fbi->map_cpu, 0xf0, fbi->map_size);
		fbi->screen_dma = fbi->map_dma;
		fbi->fb.screen_base = fbi->map_cpu;
		fbi->fb.fix.smem_start = fbi->screen_dma;
		dprintk("map_video_memory: dma=%08x cpu=%p size=%08x\n",
			fbi->map_dma, fbi->map_cpu, fbi->fb.fix.smem_len);
	}

	return fbi->map_cpu ? 0 : -ENOMEM;
}


static inline void modify_gpio(unsigned long reg,
			       unsigned long set, unsigned long mask)
{
	unsigned long tmp;
	tmp = readl(reg) & ~mask;
	writel(tmp | set, reg);
	dprintk("%08lx set to %08x\n", reg, readl(reg));
}



/*
 * s3c2410fb_init_registers - Initialise all LCD-related registers
 */

int s3c2410fb_init_registers(struct s3c2410fb_info *fbi)
{
	unsigned long saddr1, saddr2, saddr3;
	unsigned long flags;
	/* 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);
	writel(mach_info->regs.lcdcon1, S3C2410_LCDCON1);
	writel(mach_info->regs.lcdcon2, S3C2410_LCDCON2);
	writel(mach_info->regs.lcdcon3, S3C2410_LCDCON3);
	writel(mach_info->regs.lcdcon4, S3C2410_LCDCON4);
	writel(mach_info->regs.lcdcon5, S3C2410_LCDCON5);
	saddr1 =  S3C2410_LCDBANK(fbi->fb.fix.smem_start >> 22)
		| S3C2410_LCDBASEU(fbi->fb.fix.smem_start >> 1);
	saddr2 = (fbi->fb.fix.smem_start+(mach_info->width*mach_info->height*2)) >> 1;
	saddr3 =  S3C2410_OFFSIZE(0) | S3C2410_PAGEWIDTH(mach_info->width);
	dprintk("LCDSADDR1 = 0x%08lx\n", saddr1);
	writel(saddr1, S3C2410_LCDSADDR1);
	dprintk("LCDSADDR2 = 0x%08lx\n", saddr2);
	writel(saddr2, S3C2410_LCDSADDR2);
	dprintk("LCDSADDR3 = 0x%08lx\n", saddr3);
	writel(saddr3, S3C2410_LCDSADDR3);
	dprintk("LPCSEL    = 0x%08lx\n", mach_info->lpcsel);
	writel(mach_info->lpcsel, S3C2410_LPCSEL);
	dprintk("replacing TPAL %08x\n", readl(S3C2410_TPAL));
	/* ensure temporary palette disabled */
	writel(0x00, S3C2410_TPAL);
	/* probably not required */
	msleep(10);
	/* Enable video by setting the ENVID bit to 1 */
	writel(mach_info->regs.lcdcon1|S3C2410_LCDCON1_ENVID, S3C2410_LCDCON1);
	return 0;
}


static struct clk      *lcd_clock;

int __init s3c2410fb_probe(struct device *dev)
{
	char driver_name[]="s3c2410fb";
	int ret;
	mach_info = dev->platform_data;
	if (mach_info == NULL)
	{
		printk(KERN_ERR "no platform data for lcd, cannot attach\n");
		return -EINVAL;
	}
	s3c2410fb_lcd_power(1);
	dprintk("devinit\n");
	strcpy(info.fb.fix.id, driver_name);
	info.fb.fix.type	    = FB_TYPE_PACKED_PIXELS;
	info.fb.fix.type_aux	    = 0;
	info.fb.fix.xpanstep	    = 0;
	info.fb.fix.ypanstep	    = 0;
	info.fb.fix.ywrapstep	    = 0;
	info.fb.fix.accel	    = FB_ACCEL_NONE;
	info.fb.var.nonstd	    = 0;
	info.fb.var.activate	    = FB_ACTIVATE_NOW;
	info.fb.var.height	    = mach_info->height;
	info.fb.var.width	    = mach_info->width;
	info.fb.var.accel_flags     = 0;
	info.fb.var.vmode	    = FB_VMODE_NONINTERLACED;
	info.fb.fbops		    = &s3c2410fb_ops;
	info.fb.flags		    = FBINFO_FLAG_DEFAULT;
	info.fb.monspecs	    = monspecs;
	info.fb.pseudo_palette      = &info.pseudo_pal;
	info.fb.var.xres	   = mach_info->xres.defval;
	info.fb.var.xres_virtual    = mach_info->xres.defval;
	info.fb.var.yres  	    = mach_info->yres.defval;
	info.fb.var.yres_virtual    = mach_info->yres.defval;
	info.fb.var.bits_per_pixel  = mach_info->bpp.defval;
	info.fb.var.red.offset      = 11;
	info.fb.var.green.offset    = 5;
	info.fb.var.blue.offset     = 0;
	info.fb.var.transp.offset   = 0;
	info.fb.var.red.length      = 5;
	info.fb.var.green.length    = 6;
	info.fb.var.blue.length     = 5;
	info.fb.var.transp.length   = 0;
	info.fb.fix.smem_len	    = info.fb.var.xres * info.fb.var.yres *
				      info.fb.var.bits_per_pixel / 8;
	if (!request_mem_region(S3C2410_VA_LCD, SZ_1M, "s3c2410-lcd"))
		return -EBUSY;
	dprintk("got LCD region\n");
	lcd_clock = clk_get(NULL, "lcd");
	if (!lcd_clock) {
		printk(KERN_ERR "failed to get lcd clock source\n");
		return -ENOENT;
	}
	clk_use(lcd_clock);
	clk_enable(lcd_clock);
	dprintk("got and enabled clock\n");
	/* maybe not required */
	msleep(10);
	/* Initialize video memory */
	ret = s3c2410fb_map_video_memory(&info);
	if (ret) {
		printk( KERN_ERR "Failed to allocate video RAM: %d\n", ret);
		ret = -ENOMEM;
		goto failed;
	}
	dprintk("got video memory\n");
	ret = s3c2410fb_init_registers(&info);
	s3c2410fb_lcd_power(1);
	ret = s3c2410fb_check_var(&info.fb.var, &info.fb);
	ret = register_framebuffer(&info.fb);
	if (ret < 0) {
		printk(KERN_ERR "Failed to register framebuffer device: %d\n", ret);
		goto failed;
	}

	/* create device files */
	device_create_file(dev, &dev_attr_debug);
	device_create_file(dev, &dev_attr_lcd_power);
	printk(KERN_INFO "fb%d: %s frame buffer device\n",
		info.fb.node, info.fb.fix.id);
	return 0;
failed:
	release_mem_region(S3C2410_VA_LCD, S3C2410_SZ_LCD);
	return ret;
}


/* s3c2410fb_stop_lcd
 *
 * shutdown the lcd controller
*/
static void s3c2410fb_stop_lcd(void)
{
	unsigned long flags;
	unsigned long tmp;
	local_irq_save(flags);
	tmp = readl(S3C2410_LCDCON1);
	writel(tmp & ~S3C2410_LCDCON1_ENVID, S3C2410_LCDCON1);
	local_irq_restore(flags);
}


/*
 *  Cleanup
 */
static void __exit s3c2410fb_cleanup(void)
{
	s3c2410fb_stop_lcd();
	msleep(1);
	if (lcd_clock) {
		clk_disable(lcd_clock);
		clk_unuse(lcd_clock);
		clk_put(lcd_clock);
		lcd_clock = NULL;
	}
	unregister_framebuffer(&info.fb);
	release_mem_region(S3C2410_VA_LCD, S3C2410_SZ_LCD);
}

#ifdef CONFIG_PM
/* suspend and resume support for the lcd controller */
static int s3c2410fb_suspend(struct device *dev, u32 state, u32 level)
{
	if (level == SUSPEND_DISABLE || level == SUSPEND_POWER_DOWN) {
		s3c2410fb_stop_lcd();
		/* 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) */
		if (mach_info != NULL) {
			/* don't use our helper functions in this file,
			   becuase we want to save the original values for
			   when the system wakes up
			*/
			if (mach_info->lcd_power)
				(mach_info->lcd_power)(0);
		}
		msleep(1);
		clk_disable(lcd_clock);
	}
	return 0;
}

static int s3c2410fb_resume(struct device *dev, u32 level)
{
	if (level == RESUME_ENABLE) {
		clk_enable(lcd_clock);
		msleep(1);
		s3c2410fb_init_registers(&info);
	}
	return 0;
}

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

static struct device_driver s3c2410fb_driver = {
	.name		= "s3c2410-lcd",
	.bus		= &platform_bus_type,
	.probe		= s3c2410fb_probe,
	.suspend	= s3c2410fb_suspend,
	.resume		= s3c2410fb_resume,
};

int __devinit s3c2410fb_init(void)
{
	return driver_register(&s3c2410fb_driver);
}

module_init(s3c2410fb_init);
module_exit(s3c2410fb_cleanup);

MODULE_AUTHOR("Arnaud Patard <arnaud.patard@rtp-net.org>");
MODULE_DESCRIPTION("Framebuffer driver for the s3c2410");
MODULE_LICENSE("GPL");

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -