LCD驱动程序详细讲解(三)

经过前面两篇文章的基础知识的讲解,现在我们开始写驱动程序,具体代码如下,文件名lcd.c

1. 分配一个fb_info结构体: 使用framebuffer_alloc
2. 设置:各种参数的设置
3. 注册: register_framebuffer
4. 硬件相关的操作;

#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>
#include <linux/workqueue.h>
#include <linux/wait.h>
#include <linux/platform_device.h>
#include <linux/clk.h>

#include <asm/io.h>
#include <asm/uaccess.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>

//定义LCD控制器结构体,这样很方便初始化LCD寄存器
static struct lcd_regs{
		unsigned long lcdcon1;
		unsigned long lcdcon2;
		unsigned long lcdcon3;
		unsigned long lcdcon4;
		unsigned long lcdcon5;
		unsigned long lcdsaddr1;
		unsigned long lcdsaddr2;
		unsigned long lcdsaddr3;
		unsigned long redlut;
		unsigned long greenlut;
		unsigned long bluelut;
		unsigned long reserved[9];
		unsigned long dithmode;
		unsigned long tpal;
		unsigned long lcdintpnd;
		unsigned long lcdsrcpnd;
		unsigned long lcdintmsk;
		unsigned long tconsel;
};

static struct fb_info *s3c_lcd;

static u32	pseudo_pal[16]; //假的调色板

/* from pxafb.c */
static inline unsigned int chan_to_field(unsigned int chan, struct fb_bitfield *bf)
{
	chan &= 0xffff;
	chan >>= 16 - bf->length;
	return chan << bf->offset;
}

static int s3c_lcdfb_setcolreg(unsigned int regno, unsigned int red,
			     unsigned int green, unsigned int blue,
			     unsigned int transp, struct fb_info *info)
{
		unsigned int val;
		
		if(regno>16) //调色板大于16
			return 1;
			
		/* 用red,green,blue三原色构造出val */
		val  = chan_to_field(red,   &info->var.red);
		val |= chan_to_field(green, &info->var.green);
		val |= chan_to_field(blue,  &info->var.blue);
		
		pseudo_pal[regno] = val;
		
		return 0;
}

//对显存的操作
static struct fb_ops s3c_lcd_fb_ops = {
	.owner		= THIS_MODULE,
	.fb_setcolreg	= s3c_lcdfb_setcolreg,
	.fb_fillrect	= cfb_fillrect,
	.fb_copyarea	= cfb_copyarea,
	.fb_imageblit	= cfb_imageblit,
};

//LCD背光使能引脚 address:0x56000010 GPB0输出
static volatile unsigned long *GPBCON = NULL;  
static volatile unsigned long *GPBDAT = NULL; 

//LCD电源电路  address:0x56000060 GPG4 输出
static volatile unsigned long *GPGCON = NULL;
static volatile unsigned long *GPGDAT = NULL;

//VD3,4,5,6,7 -> B -> GPC11,12,13,14,15引脚 address:0x56000020
//VLINE -> GPC2
//VFRAME -> GPC3
static volatile unsigned long *GPCCON = NULL;
static volatile unsigned long *GPCDAT = NULL;

//VD10,11,12,13,14,15 -> G -> GPD2,3,4,5,6,7引脚 address:0x56000030
//VD19,20,21,22,23 -> R -> GPD11,12,13,14,15引脚
static volatile unsigned long *GPDCON = NULL;
static volatile unsigned long *GPDDAT = NULL;

static struct lcd_regs *lcd_regs = NULL;

static int __init lcd_init(void) //入口函数
{
	/* 1.分配一个fb_info结构体 */
	
	/* 原型: struct fb_info *framebuffer_alloc(size_t size, struct device *dev)       
	 * 其中的size表示大小,内核在分配结构体的同时会额外的分配一些内存空间 原来       
	 * 分配的空间中有一个指针,指向这个额外的内存空间,里面存放私有数据。这里我       
	 * 们不需要,所以size为0,指针为空
	*/
	s3c_lcd = framebuffer_alloc(0, NULL);
	/* 2. 设置 */
	/* 2.1 设置固定参数fix*/
	strcpy(s3c_lcd->fix.id, "mylcd"); //设置名字
	s3c_lcd->fix.smem_len = 480*272*16/8;  //设置framebuffer显存的长度
	s3c_lcd->fix.type = FB_TYPE_PACKED_PIXELS;  //设置显示格式
	s3c_lcd->fix.visual = FB_VISUAL_TRUECOLOR;  //TFT是真彩色
	s3c_lcd->fix.line_length = 480*2;    //行长度,单位是字节
	
	/* 2.2 设置可变参数 */
	s3c_lcd->var.xres = 480; //x方向的分辨率
	s3c_lcd->var.yres = 272; //y方向的分辨率
	s3c_lcd->var.xres_virtual = 480; //x方向的虚拟分辨率
	s3c_lcd->var.yres_virtual = 272; //y方向的虚拟分辨率
	s3c_lcd->var.bits_per_pixel = 16;  //每个像素多少位: 16位
	
	/* RGB:565 */
	//设置fb缓存的R位域
	s3c_lcd->var.red.offset = 11;
	s3c_lcd->var.red.length = 5;
	
	//设置fb缓存的G位域
	s3c_lcd->var.green.offset = 5;
	s3c_lcd->var.green.length = 6;
	
	//设置fb缓存的B位域
	s3c_lcd->var.blue.offset = 0;
	s3c_lcd->var.blue.length = 5;
	
	//使用默认值
	s3c_lcd->var.activate = FB_ACTIVATE_NOW;
	
	/* 2.3 设置操作函数 */
	s3c_lcd->fbops = &s3c_lcd_fb_ops;
	
	/* 其他设置 */
	s3c_lcd->pseudo_palette = pseudo_pal;  //设置调色板
	s3c_lcd->screen_size =  480*272*16/8;  //虚拟显存的长度
	
	/* 硬件相关的操作 */
	/* 配置GPIO用于LCD */
	GPBCON = ioremap(0x56000010,8);
	GPBDAT = GPBCON + 1;
	
	GPCCON = ioremap(0x56000020,4);
	GPDCON = ioremap(0x56000030,4);
	GPGCON = ioremap(0x56000060,4);
	GPGDAT = GPGCON + 1;
	
	//GPIO管脚用于VD[7:0],LCDVF[2:0],VM,VFRAME,VLINE,VCLK,LEND
	*GPCCON = 0xaaaaaaaa;
	
	//GPIO管脚用于VD[23:8]
	*GPDCON = 0xaaaaaaaa;
	
	*GPBCON &= ~(0x03); //GPB0设置为输出引脚 :KEYBOARD
	*GPBCON |= (0x01);
	*GPBDAT &= ~(0x01); //输出低电平,先不要开启背光
	
	*GPGCON |= (0x03 << 8); //GPG4设置LCD_PWREN,电源使能引脚
	*GPGDAT |= (0x01 << 4); //GPG4引脚输出高电平
	
	/* 根据LCD手册设置LCD控制器,比如VCLK的频率等 */
	lcd_regs = ioremap(0X4D000000,sizeof(struct lcd_regs));
	
	/* 设置频率等参数
	 * CLKVAL : bit[17:8] VCLK = HCLK / [(CLKVAL+1) x 2
	 * 					HCLK:dmesg查看内核信息,100MHz
	 * 					VCLK = 100MHz / [(CLKVAL+1) x 2
	 * 					VCLK:查看LCD手册,5MHz (P11页)
	 * 					5MHz = 100MHz / [(CLKVAL+1) x 2
	 * 					CLKVAL = 9MHz 
	 * PNRMODE : bit[6:5] 设置LCD显示模式
	 *					 0b11 = TFT LCD panel
	 * BPPMODE : bit[4:1] 设置BPP模式
	 *					 0b1100 = 16 bpp for TFT
	 * ENVID : bit[0] 设置是否输出视频格式
	 *				 1 = Enable the video output and the LCD control signal
	 * 				 0 = Disable the video output and the LCD control signal
	*/
	lcd_regs -> lcdcon1 = (0x09<<8)|(0x03<<5)|(0x0c<<1);
	
	/* 垂直方向的时间参数;可以结合LCD手册和主控芯片手册的LCD控制器章节计算以下参数;
	 * VBPD : bit[31:24],VSYNC时钟之后过多长时间才发出第一行数据,设置为1;
	 * LINEVAL : bit[23:14],多少行,设置为271;
	 * VFPD : bit[13:6],发出最后一行之后,再过多长时间才发出VSYNC信号,设置为1;
	 * VSPW : bit[5:0],VSYNC有效信号的保持时间,设置为9;
	*/
	lcd_regs -> lcdcon2 = (0x01<<24)|(0x10f<<14)|(0x01<<6)|(0x09<<0);
	
	/* 水平方向的时间参数
	 * HBPD:bit[25:19],HSYNC时钟信号之后过多长时间才发出第一个有效像素数据,设置为1;
	 * HOZVAL:bit[18:8],一行有多少个有效像素,设置为479;
	 * HFPD:bit[7:0],发出最后一个有效像素之后,再过多长时间才发出HSYNC信号,设置为1;
	*/
	lcd_regs -> lcdcon3 = (0x01<<19)|(0x1DF<<8)|(0x01<<0);
	
	/* 水平方向的时间参数
	 * HSPW: bit[7:0],HSYNC时钟信号的宽度,设置为40;
	*/
	lcd_regs -> lcdcon4 = (0x28<<0);
	
	/* 信号的极性设置
	 * FRM565:bit[11],设置为 1 = 5:6:5 Format
	 * INVVCLK:bit[10],设置为 0 = The video data is fetched at VCLK falling edge
	 * INVVLINE:bit[9],This bit indicates the VLINE/HSYNC pulse polarity,设置为1 = Inverted
	 * INVVFRAME:bit[8],This bit indicates the VFRAME/VSYNC pulse polarity,设置为1 = Inverted
	 * INVVD:bit[7],This bit indicates the VD (video data) pulse polarity,设置为0 = Normal
	 * INVVDEN:bit[6],This bit indicates the VDEN signal polarity,设置为0 = normal
	 * INVPWREN:bit[5],This bit indicates the PWREN signal polarity,设置为0 = normal
	 * PWREN:bit[3], LCD_PWREN output signal enable/disable,设置为0 = Disable PWREN signal
	 * BSWP:bit[1],Byte swap control bit,设置为0 = Swap Disable
	 * HWSWP:bit[0],Half-Word swap control bit,设置为1 = Swap Enable
	*/
	lcd_regs -> lcdcon5 = (1<<11)|(0<<10)|(1<<9)|(1<<8)|(1<<0);
	
	/* 分配(framebuffer)显存,告诉LCD控制器显存地址 */
	//返回显存的虚拟地址
	s3c_lcd->screen_base = dma_alloc_writecombine(NULL,s3c_lcd->fix.smem_len,&s3c_lcd->fix.smem_start,GFP_KERNEL);
	
	/* Frame buffer start address 1 register
	*/
	lcd_regs -> lcdsaddr1 = (s3c_lcd->fix.smem_start>>1) & ~(3<<30) ;
	
	//Frame buffer start address 2 register
	lcd_regs -> lcdsaddr2 = ((s3c_lcd->fix.smem_start+s3c_lcd->fix.smem_len)>>1) & 0x1fffff;
	
	//Virtual screen address set
	lcd_regs -> lcdsaddr3 = 480*16/16; //一行的长度,单位half words,2字节 有疑问
	
	/* 启动LCD */
	lcd_regs -> lcdcon1 |= (0x01<<0); //使能LCD控制器
	lcd_regs -> lcdcon5 |= (1<<3); //使能LCD本身
	*GPBDAT |= (0x01); //输出高电平,开启LCD背光灯

	/* 注册 */
	register_framebuffer(s3c_lcd);
	return 0;
}

static void __exit lcd_exit(void)
{
	unregister_framebuffer(s3c_lcd);
	/* 关闭LCD */
	lcd_regs -> lcdcon1 &= ~(0x01>>0); //关闭LCD本身
	*GPBDAT &= ~(0x01); //输出低电平,关闭LCD背光灯
	
	//释放显存
	dma_alloc_writecombine(NULL,s3c_lcd->fix.smem_len,s3c_lcd->screen_base,s3c_lcd->fix.smem_start);
	
	//取消映射端口
	iounmap(lcd_regs);
	iounmap(GPBCON);
	iounmap(GPCCON);
	iounmap(GPDCON);
	
	iounmap(GPGDAT);
	iounmap(GPGCON);
	
	framebuffer_release(s3c_lcd);
}

module_init(lcd_init);
module_exit(lcd_exit);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("LCD drivers by Cai haitao");


测试:

1.make menuconfig去掉原来的LCD驱动程序;
  Device Drivers
     Graphics support
         <M> S3C2410 LCD framebuffer support

2. make uImage

uImage 文件里面已经不包含LCD驱动;

make modules

将cfbcopyarea.ko,cfbfillrect.ko,cfbimgblt.ko三个文件拷贝开发板;

3. 使用新的uImage启动开发板
   下载uImage的方法:
   reboot-->按任意键进入菜单模式,输入:
   nfs 30000000 192.168.1.123:/work/nfs_root/first_fs/uImage
   然后bootm 30000000启动开发板

4. 加载驱动程序
insmod cfbcopyarea.ko 
insmod cfbfillrect.ko 
insmod cfbimgblt.ko 
insmod lcd.ko

5.

echo hello > /dev/tty1  // 可以在LCD上看见hello
cat lcd.ko > /dev/fb0   // 花屏

转载于:https://my.oschina.net/cht2000/blog/1021808

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值