版权所有,转载请说明转自 http://my.csdn.net/weiqing1981127
一.LCD屏理论
1.1 LCD屏基本概念
我们知道,诸如PCI、I2C和USB等外围设备总线都来自于系统PC机的南桥,今天我们看到的视频控制器则来自于北桥。LCD主要由TN(扭转向列型)、STN(超扭转向列型)、DSTN(双层超扭曲向列阵)和TFT(薄膜式晶体管型)四种显示器,许多MCU内部直接集成了LCD控制器,通过LCD控制器可以方便地控制STN和TFT屏,其中TFT屏是目前嵌入式系统应用的主流。LCD常的接口类型有RGB、CPU、SPI、MIPI、MDDI、LVDS和VGA。
显示标准:VGA(视频图形阵列)是IBM早期提出的显示标准,VGA的分辨率是640x480,而更新标准的SVGA(高级视频图形阵列)和XGA(扩展图形阵列)则支持800x600和1024x768分辨率,嵌入式设备常用分辨率为320x240的QVGA面板。
视频标准:许多接口标准对视频控制器和显示设备的连接做了规定,视频电缆有如下标准,其一,模拟显示器;其二,数字平面显示器,如笔记本的TFT LCD,有LVDS(低电压差分信号)连接器;其三,与DVI(数字视频接口)规范标准兼容的显示器;其四,与HDTV(高清电视)规范兼容的显示器,它使用HDMI(高清多媒体接口)。
LCD常用参数:PPI是每平方英寸所拥有的像素数目,BPP是每个像素使用多少位来表示其颜色。
1.2帧缓冲的理解
FrameBuffer又叫帧缓冲,是Linux为操作显示设备提供的一个用户接口,用户应用程序可以通过帧缓冲透明地访问不同类型的显示设备。对于帧缓冲设备,只要在显示缓冲区与显示点对应区域写入颜色值,对应的颜色会自动在屏幕上显示,帧缓冲设备是标准的字符设备,主设备号是29,对应于/dev/fbn设备文件。
在Linux中,帧缓冲可以看成一个内存,既可以向这块内存中写数据,也可以向这个内存中读数据,用户不需要关心物理显存的位置和换页机制,这些都是由帧缓冲设备驱动完成的。帧缓冲区位于Linux内核态地址空间,所以Linux在文件操作file_operations结构中提供了mmap函数,可将缓冲区的物理地址映射到用户空间的一段虚拟地址中,之后用户就可以通过读写这段虚拟地址访问屏幕缓冲区了。帧缓冲驱动的功能就是分配一块内存作显存,然后设置LCD控制器的寄存器,LCD显示器就会不断从显存中获得数据,并显示在LCD屏上。
二.Mini2440的X35型LCD移植
先说明下像素时钟pixclock的概念
pixclock=1/dotclock 其中dotclock是视频硬件在显示器上绘制像素的速率
dotclock=(x向分辨率+左空边+右空边+HSYNC长度)* (y向分辨率+上空边+下空边+YSYNC长度)*整屏的刷新率
其中x向分辨率、左空边、右空边、HSYNC长度、y向分辨率、上空边、下空边和YSYNC长度可以在X35LCD说明文档中查到。
整屏的刷新率计算方法如下:
假如我们通过查X35LCD说明文档,知道fclk=6.34MHZ,那么画一个像素需要的时间就是1/6.34us,如果屏的大小是240*320,那么现实一行需要的时间就是240/6.34us,每条扫描线是240,但是水平回扫和水平同步也需要时间,如果水平回扫和水平同步需要29个像素时钟,因此,画一条扫描线完整的时间就是(240+29) /6.34us。完整的屏有320根线,但是垂直回扫和垂直同步也需要时间,如果垂直回扫和垂直同步需要13个像素时钟,那么画一个完整的屏需要(240+29)*(320+13)/6.34us,所以整屏的刷新率就是6.34/((240+29)*(320+13))MHZ
下面我们来看看怎么移植LCD驱动,我们的mini2440使用的是X35的LCD屏,根据X35的LCD说明文档,需要在BSP中X35LCD屏的一些参数。
在mach-mini2440.c中添加X35LCD的参数
#if defined(CONFIG_FB_S3C2410_X240320) //定义X35LCD参数
#define LCD_WIDTH 240 //屏宽
#define LCD_HEIGHT 320 //屏高
#define LCD_PIXCLOCK 170000 //时钟
#define LCD_RIGHT_MARGIN 25 //左边界
#define LCD_LEFT_MARGIN 0 //右边界
#define LCD_HSYNC_LEN 4 //行同步
#define LCD_UPPER_MARGIN 0 //上边界
#define LCD_LOWER_MARGIN 4 //下边界
#define LCD_VSYNC_LEN 9 //帧同步
#define LCD_CON5 (S3C2410_LCDCON5_FRM565 | S3C2410_LCDCON5_INVVDEN | S3C2410_LCDCON5_INVVFRAME | S3C2410_LCDCON5_INVVLINE | S3C2410_LCDCON5_INVVCLK | S3C2410_LCDCON5_HWSWP )
#elif //定义其他LCD屏参数
#endif
好了,我们现在发现要想上面定义的X35LCD的参数正在起作用,必须使得CONFIG_FB_S3C2410_X240320=y;我们需要在/driver/video/Kconfig中定义
config FB_S3C2410_X240320
boolean "3.5 inch 240X320 LCD(ACX502BMU)"
depends on FB_S3C2410
help
3.5 inch 240X320 LCD(ACX502BMU)
然后我们通过make menuconfig选中"3.5 inch 240X320 LCD(ACX502BMU)"这一选项。
根据我们的X35LCD屏的说明文档,我们已经定义了一些边界参数和同步参数,因为我们的LCD驱动是基于platform总线的,所以需要在这个BSP中添加LCD的平台设备。
struct platform_device s3c_device_lcd = { //添加LCD平台设备
.name = "s3c2410-lcd", //设备名
.id = -1,
.num_resources = ARRAY_SIZE(s3c_lcd_resource),
.resource = s3c_lcd_resource, //资源
.dev = {
.dma_mask = &s3c_device_lcd_dmamask,
.coherent_dma_mask = 0xffffffffUL
}
};
资源的定义如下
static struct resource s3c_lcd_resource[] = {
[0] = { //内存空间资源
.start = S3C24XX_PA_LCD,
.end = S3C24XX_PA_LCD + S3C24XX_SZ_LCD - 1,
.flags = IORESOURCE_MEM,
},
[1] = { //中断资源
.start = IRQ_LCD,
.end = IRQ_LCD,
.flags = IORESOURCE_IRQ,
}
};
然后我们把s3c_device_lcd放到mini2440_devices[]结构体中,接着调用platform_add_devices(mini2440_devices, ARRAY_SIZE(mini2440_devices))将LCD平台设备注册到内核。
对于我们的LCD,需要给这个平台设备添加平台设备数据,通过调用
s3c24xx_fb_set_platdata(&mini2440_fb_info);
static struct s3c2410fb_mach_info mini2440_fb_info __initdata = {
.displays = &mini2440_lcd_cfg, //定义s3c2410fb_display数据
.num_displays = 1,
.default_display = 0,
.gpccon = 0xaa955699, //GPC端口设置
.gpccon_mask = 0xffc003cc,
.gpcup = 0x0000ffff,
.gpcup_mask = 0xffffffff,
.gpdcon = 0xaa95aaa1, //GPD端口设置
.gpdcon_mask = 0xffc0fff0,
.gpdup = 0x0000faff,
.gpdup_mask = 0xffffffff,
.lpcsel = 0xf82,
};
继续看
static struct s3c2410fb_display mini2440_lcd_cfg __initdata = {
#if !defined (LCD_CON5)
.lcdcon5 = S3C2410_LCDCON5_FRM565 |
S3C2410_LCDCON5_INVVLINE |
S3C2410_LCDCON5_INVVFRAME |
S3C2410_LCDCON5_PWREN |
S3C2410_LCDCON5_HWSWP,
#else
.lcdcon5 = LCD_CON5,
#endif
.type = S3C2410_LCDCON1_TFT, //屏的类型
.width = LCD_WIDTH, //屏宽
.height = LCD_HEIGHT, //屏高
.pixclock = LCD_PIXCLOCK, //时钟
.xres = LCD_WIDTH, //水平分辨率
.yres = LCD_HEIGHT, //垂直分辨率
.bpp = 16, //每个像素的比特数
.left_margin = LCD_LEFT_MARGIN + 1, //左边界
.right_margin = LCD_RIGHT_MARGIN + 1, //右边界
.hsync_len = LCD_HSYNC_LEN + 1, //行同步
.upper_margin = LCD_UPPER_MARGIN + 1, //上边界
.lower_margin = LCD_LOWER_MARGIN + 1, //下边界
.vsync_len = LCD_VSYNC_LEN + 1, //帧同步
};
好了,这样我们就完成了LCD驱动的移植工作,接着我们通过make menuconfig选择相应的文件层、设备层和X35LCD屏这个三个选项,最后编译生成内核。
三.LCD文件层和驱动层设计思路
LCD驱动可以分为文件层和设备层,文件层又叫FrameBuffer设备驱动,对应的文件是fbmem.c,主要实现为用户提供file_operations接口,同时为设备层提供一些函数接口,这个帧缓冲设备驱动内核已经帮我们编写好,我们不需要编写。在设备层我们专门Mini2440的LCD编写的驱动在s3c2410fb.c中,该驱动叫LCD驱动,主要是填充一个fbinfo结构,然后用register_framebuffer注册到内核,对于fbinfo结构,最主要的是填充它的fs_ops成员。对于驱动工程师,第一件事就是学会根据LCD说明文档,移植LCD。第二件事就是会写设备层LCD驱动。
3.1 LCD驱动中几个重要的数据结构
在分析内核LCD驱动代码之前,我们先要熟悉几个结构体。
struct fb_info {
int node;
int flags;
struct mutex lock;
struct mutex mm_lock;
struct fb_var_screeninfo var; //当前缓冲区的可变参数
struct fb_fix_screeninfo fix; //当前缓冲区的固定参数
struct fb_monspecs monspecs;
struct work_struct queue;
struct fb_pixmap pixmap;
struct fb_pixmap sprite;
struct fb_cmap cmap; //当前的调试板
struct list_head modelist;
struct fb_videomode *mode;
#ifdef CONFIG_FB_BACKLIGHT //背光
struct backlight_device *bl_dev;
struct mutex bl_curve_mutex; //背光灯层次
u8 bl_curve[FB_BACKLIGHT_LEVELS]; //调整背光灯
#endif
#ifdef CONFIG_FB_DEFERRED_IO
struct delayed_work deferred_work;
struct fb_deferred_io *fbdefio;
#endif
struct fb_ops *fbops; //帧缓冲操作函数集合
struct device *device;
struct device *dev;
int class_flag;
#ifdef CONFIG_FB_TILEBLITTING
struct fb_tile_ops *tileops;
#endif
char __iomem *screen_base; //虚拟基地址
unsigned long screen_size; //虚拟内存大小
void *pseudo_palette;
#define FBINFO_STATE_RUNNING 0
#define FBINFO_STATE_SUSPENDED 1
u32 state;
void *fbcon_par;
void *par; //私有数据
resource_size_t aperture_base;
resource_size_t aperture_size;
};
为了清晰起见,对于fb_info结构体,我只注释了重点几个成员,每个帧设备都有一个fb_info,该结构体包含了驱动实现的底层函数和记录设备状态的数据。fb_info结构体主要包含fb_var_screeninfo、fb_fix_screeninfo、fb_cmap和fb_ops,
struct fb_var_screeninfo {
__u32 xres; //水平分辨率
__u32 yres; //垂直分辨率
__u32 xres_virtual;
__u32 yres_virtual;
__u32 xoffset;
__u32 yoffset;
__u32 bits_per_pixel; //每个像素所占的比特数
__u32 grayscale;
struct fb_bitfield red;
struct fb_bitfield green;
struct fb_bitfield blue;
struct fb_bitfield transp;
__u32 nonstd;
__u32 activate;
__u32 height; //屏高
__u32 width; //屏宽
__u32 accel_flags;
__u32 pixclock; //像素时钟
__u32 left_margin; //左边界
__u32 right_margin; //右边界
__u32 upper_margin; //上边界
__u32 lower_margin; //下边界
__u32 hsync_len; //水平同步长度
__u32 vsync_len; //垂直同步长度
__u32 sync;
__u32 vmode;
__u32 rotate;
__u32 reserved[5];
};
上面的fb_var_screeninfo结构体存放了用户可以修改的显示控制器参数,如分辨率,BPP等参数。
struct fb_fix_screeninfo {
char id[16];
unsigned long smem_start; //fb缓冲区开始的位置
__u32 smem_len; //fb缓冲区长度
__u32 type;
__u32 type_aux;
__u32 visual; //屏幕色彩模式
__u16 xpanstep;
__u16 ypanstep;
__u16 ywrapstep;
__u32 line_length;
unsigned long mmio_start; //内存映射开始位置
__u32 mmio_len; //内存映射长度
__u32 accel;
__u16 reserved[3];
};
上面这个fb_fix_screeninfo主要记录了用户不能修改的固定显示控制器参数,如缓冲区物理地址、缓冲区长度、显示色彩模式、内核映射的开始位置等,这些结构体程序都需要驱动程序初始化时设置。
struct fb_cmap {
__u32 start; //颜色板的第一个元素入口位置
__u32 len; //元素长度
__u16 *red; //红
__u16 *green; //绿
__u16 *blue; //蓝
__u16 *transp; //透明分量值
};
对于上面的fb_cmap,它主要记录了一个颜色板信息,用户空间可以使用ioctl函数的FBIOGETCMAP和FBIOPUTCMAP读取和设置颜色表的值。
struct fb_ops {
struct module *owner;
int (*fb_open)(struct fb_info *info, int user);
int (*fb_release)(struct fb_info *info, int user);
ssize_t (*fb_read)(struct fb_info *info, char __user *buf,
size_t count, loff_t *ppos);
ssize_t (*fb_write)(struct fb_info *info, const char __user *buf,
size_t count, loff_t *ppos);
int (*fb_check_var)(struct fb_var_screeninfo *var, struct fb_info *info);
int (*fb_set_par)(struct fb_info *info);
int (*fb_setcolreg)(unsigned regno, unsigned red, unsigned green,
unsigned blue, unsigned transp, struct fb_info *info);
int (*fb_setcmap)(struct fb_cmap *cmap, struct fb_info *info);
int (*fb_blank)(int blank, struct fb_info *info);
int (*fb_pan_display)(struct fb_var_screeninfo *var, struct fb_info *info);
void (*fb_fillrect) (struct fb_info *info, const struct fb_fillrect *rect);
void (*fb_copyarea) (struct fb_info *info, const struct fb_copyarea *region);
void (*fb_imageblit) (struct fb_info *info, const struct fb_image *image);
int (*fb_cursor) (struct fb_info *info, struct fb_cursor *cursor);
void (*fb_rotate)(struct fb_info *info, int angle);
int (*fb_sync)(struct fb_info *info);
int (*fb_ioctl)(struct fb_info *info, unsigned int cmd,
unsigned long arg);
int (*fb_compat_ioctl)(struct fb_info *info, unsigned cmd,
unsigned long arg);
int (*fb_mmap)(struct fb_info *info, struct vm_area_struct *vma);
void (*fb_get_caps)(struct fb_info *info, struct fb_blit_caps *caps,
struct fb_var_screeninfo *var);
void (*fb_destroy)(struct fb_info *info);
};
其中fb_ops就是用来实现对帧缓冲设备的操作。
3.2 LCD驱动层
好了,我们先看看驱动层代码s3c2410fb.c
static struct platform_driver s3c2410fb_driver = {
.probe = s3c2410fb_probe, //探测
.remove = s3c2410fb_remove, //移除
.suspend = s3c2410fb_suspend, //挂起
.resume = s3c2410fb_resume, //恢复
.driver = {
.name = "s3c2410-lcd", //驱动名
.owner = THIS_MODULE,
},
};
我们看看探测函数s3c2410fb_probe
static int __init s3c2410fb_probe(struct platform_device *pdev)
{
return s3c24xxfb_probe(pdev, DRV_S3C2410);
}
继续看
static int __init s3c24xxfb_probe(struct platform_device *pdev,
enum s3c_drv_type drv_type)
{
struct s3c2410fb_info *info; //该驱动的全局变量结构体
struct s3c2410fb_display *display; //LCD屏的配置信息
struct fb_info *fbinfo; //帧缓冲驱动中对应的fb_info结构体
struct s3c2410fb_mach_info *mach_info; //内核平台设备数据
struct resource *res; //LCD资源
int ret;
int irq;
int i;
int size;
u32 lcdcon1;
mach_info = pdev->dev.platform_data; //获得平台设备数据
if (mach_info == NULL) {
dev_err(&pdev->dev,
"no platform data for lcd, cannot attach\n");
return -EINVAL;
}
if (mach_info->default_display >= mach_info->num_displays) {
dev_err(&pdev->dev, "default is %d but only %d displays\n",
mach_info->default_display, mach_info->num_displays);
return -EINVAL;
}
//获得LCD配置信息结构体
display = mach_info->displays + mach_info->default_display;
irq = platform_get_irq(pdev, 0); //获得中断号
if (irq < 0) {
dev_err(&pdev->dev, "no irq for device\n");
return -ENOENT;
}
//给帧缓冲fb_info分配空间,并将struct s3c2410fb_info作为其私有数据
fbinfo = framebuffer_alloc(sizeof(struct s3c2410fb_info), &pdev->dev);
if (!fbinfo)
return -ENOMEM;
platform_set_drvdata(pdev, fbinfo); //把fb_info作为平台设备的私有数据
info = fbinfo->par; //获得fb_info的私有数据
info->dev = &pdev->dev;
info->drv_type = drv_type;
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);//获取资源
if (res == NULL) {
dev_err(&pdev->dev, "failed to get memory registers\n");
ret = -ENXIO;
goto dealloc_fb;
}
size = (res->end - res->start) + 1;
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;
}
info->irq_base = info->io + ((drv_type == DRV_S3C2412) ? S3C2412_LCDINTBASE : S3C2410_LCDINTBASE); //基地址
dprintk("devinit\n");
strcpy(fbinfo->fix.id, driver_name); //驱动名
lcdcon1 = readl(info->io + S3C2410_LCDCON1);
writel(lcdcon1 & ~S3C2410_LCDCON1_ENVID, info->io + S3C2410_LCDCON1); //禁止输出使能
fbinfo->fix.type = FB_TYPE_PACKED_PIXELS;
fbinfo->fix.type_aux = 0; //LCD屏固定参数设置
fbinfo->fix.xpanstep = 0;
fbinfo->fix.ypanstep = 0;
fbinfo->fix.ywrapstep = 0;
fbinfo->fix.accel = FB_ACCEL_NONE;
fbinfo->var.nonstd = 0; //LCD屏可变参数设置
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;//初始化调试板为空
ret = request_irq(irq, s3c2410fb_irq, IRQF_DISABLED, pdev->name, info);
if (ret) {
dev_err(&pdev->dev, "cannot get irq %d - err %d\n", irq, ret);
ret = -EBUSY;
goto release_regs;
}
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);
info->clk_rate = clk_get_rate(info->clk) //设置时钟;
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;
}
//申请fb_info的显示缓冲区空间,并将其地址写入fbinfo中
ret = s3c2410fb_map_video_memory(fbinfo);
if (ret) {
printk(KERN_ERR "Failed to allocate video RAM: %d\n", ret);
ret = -ENOMEM;
goto release_clock;
}
dprintk("got video memory\n");
fbinfo->var.xres = display->xres; //水平分辨率
fbinfo->var.yres = display->yres; //垂直分辨率
fbinfo->var.bits_per_pixel = display->bpp; //每个像素的比特数
s3c2410fb_init_registers(fbinfo); //初始化GPIO寄存器
//检查fb_info->var与fbinfo支持的哪一种分辨率、色彩模式匹配
s3c2410fb_check_var(&fbinfo->var, fbinfo);
ret = s3c2410fb_cpufreq_register(info);
if (ret < 0) {
dev_err(&pdev->dev, "Failed to register cpufreq\n");
goto free_video_memory;
}
ret = register_framebuffer(fbinfo);//注册帧缓冲设备fb_info到系统中
if (ret < 0) {
printk(KERN_ERR "Failed to register framebuffer device: %d\n",
ret);
goto free_cpufreq;
}
ret = device_create_file(&pdev->dev, &dev_attr_debug);
if (ret) {
printk(KERN_ERR "failed to add debug attribute\n");
}
printk(KERN_INFO "fb%d: %s frame buffer device\n",
fbinfo->node, fbinfo->fix.id);
return 0;
free_cpufreq:
s3c2410fb_cpufreq_deregister(info);
free_video_memory:
s3c2410fb_unmap_video_memory(fbinfo);
release_clock:
clk_disable(info->clk);
clk_put(info->clk);
release_irq:
free_irq(irq, info);
release_regs:
iounmap(info->io);
release_mem:
release_resource(info->mem);
kfree(info->mem);
dealloc_fb:
platform_set_drvdata(pdev, NULL);
framebuffer_release(fbinfo);
return ret;
}
上面这个探测函数中包含了几个重要的函数,如申请帧缓冲设备的显存区空间的函数s3c2410fb_map_video_memory(fbinfo);初始化GPIO寄存器的函数s3c2410fb_init_registers(fbinfo);检查fb_info->var与fbinfo支持的哪一种分辨率、色彩模式匹配,并据此填充var中其他参数的函数s3c2410fb_check_var(&fbinfo->var, fbinfo),下面我们依次对这三个函数进行分析。
首先看s3c2410fb_map_video_memory(fbinfo),即申请帧缓冲设备的显存区空间的函数
static int __init s3c2410fb_map_video_memory(struct fb_info *info)
{
struct s3c2410fb_info *fbi = info->par; //获得fb_info的私有数据
dma_addr_t map_dma; //保存DMA缓冲区总线地址
unsigned map_size = PAGE_ALIGN(info->fix.smem_len);
dprintk("map_video_memory(fbi=%p) map_size %u\n", fbi, map_size);
//将分配的一个写合并DMA缓存区设置为LCD屏幕的虚拟地址
info->screen_base = dma_alloc_writecombine(fbi->dev, map_size,
&map_dma, GFP_KERNEL);
if (info->screen_base) {
dprintk("map_video_memory: clear %p:%08x\n",
info->screen_base, map_size);
memset(info->screen_base, 0x00, map_size); //设置DMA缓存内容为空
//将DMA缓冲区总线地址设为fb_info不可变参数中缓存的开始位置
info->fix.smem_start = map_dma;
dprintk("map_video_memory: dma=%08lx cpu=%p size=%08x\n",
info->fix.smem_start, info->screen_base, map_size);
}
return info->screen_base ? 0 : -ENOMEM;
}
接着我们看看初始化GPIO寄存器的函数s3c2410fb_init_registers(fbinfo)
static int s3c2410fb_init_registers(struct fb_info *info)
{
struct s3c2410fb_info *fbi = info->par; //获得fb_info的私有数据
struct s3c2410fb_mach_info *mach_info = fbi->dev->platform_data;
unsigned long flags;
void __iomem *regs = fbi->io;
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;
}
local_irq_save(flags);
//把GPIO端口C和D配置成LCD模式
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);
writel(mach_info->lpcsel, lpcsel);
dprintk("replacing TPAL %08x\n", readl(tpal));
writel(0x00, tpal);
return 0;
}
最后看看检查fb_info->var与fbinfo支持的哪一种分辨率、色彩模式匹配,并据此填充var中其他参数的函数s3c2410fb_check_var(&fbinfo->var, fbinfo)
static int s3c2410fb_check_var(struct fb_var_screeninfo *var,
struct fb_info *info)
{
struct s3c2410fb_info *fbi = info->par;
struct s3c2410fb_mach_info *mach_info = fbi->dev->platform_data;
struct s3c2410fb_display *display = NULL;
struct s3c2410fb_display *default_display = mach_info->displays +
mach_info->default_display;
int type = default_display->type; //获取LCD类型,TFT
unsigned i;
dprintk("check_var(var=%p, info=%p)\n", var, info);
//验证x/y解析度
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;
}
if (!display) {
dprintk("wrong resolution or depth %dx%d at %d bpp\n",
var->xres, var->yres, var->bits_per_pixel);
return -EINVAL;
}
var->xres_virtual = display->xres; //配置屏的虚拟解析像素
var->yres_virtual = display->yres;
var->height = display->height; //配置屏的高度宽度
var->width = display->width;
var->pixclock = display->pixclock; //配置屏的时钟
var->left_margin = display->left_margin; //配置屏的行帧同步、水平垂直同步
var->right_margin = display->right_margin;
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; ///配置LCD寄存器
fbi->regs.lcdcon1 = display->type;
var->transp.offset = 0; //配置透明度
var->transp.length = 0;
//根据BBP来设置可变参数RGB的颜色位域
switch (var->bits_per_pixel) {
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:
if (display->type != S3C2410_LCDCON1_TFT) {
var->red.length = 3;
var->red.offset = 5;
var->green.length = 3;
var->green.offset = 2;
var->blue.length = 2;
var->blue.offset = 0;
} else {
var->red.offset = 0;
var->red.length = 8;
var->green = var->red;
var->blue = var->red;
}
break;
case 12:
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:
if (display->lcdcon5 & S3C2410_LCDCON5_FRM565) {
var->red.offset = 11; //偏移
var->green.offset = 5;
var->blue.offset = 0;
var->red.length = 5; //长度
var->green.length = 6;
var->blue.length = 5;
} else {
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;
case 32:
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;
}
return 0;
}
好了,我们已经分析完LCD驱动中probe探测函数了,该函数主要是分配fb_info结构体空间,然后填充fb_info,初始化GPIO控制器,检查并设置fb_info中可变参数,申请帧缓冲设备的显示缓冲区空间,最后调用register_framebuffer函数注册到内核。
下面我们把重点放在fb_info结构体的fb_ops成员上
static struct fb_ops s3c2410fb_ops = {
.owner = THIS_MODULE,
.fb_check_var = s3c2410fb_check_var, //检查参数
.fb_set_par = s3c2410fb_set_par, //激活fb_info参数配置
.fb_blank = s3c2410fb_blank, //显示空白
.fb_setcolreg = s3c2410fb_setcolreg, //设置颜色表
.fb_fillrect = cfb_fillrect, //可选
.fb_copyarea = cfb_copyarea, //可选
.fb_imageblit = cfb_imageblit, //可选
};
fp_ops是使得帧缓冲设备工作所需函数的集合,它们最终与LCD控制器打交道。
s3c2410fb_check_va用于调整可变参数,并修改为硬件所支持的值;s3c2410fb_set_par则根据屏幕参数设置具体读写LCD控制器的寄存器,使得LCD控制器进入相应的工作状态。对于fb_ops中的.fb_fillrect、fb_copyarea和fb_imageblit,我们通常使用通用的cfb_fillrect、cfb_copyarea和cfb_imageblit函数即可。s3c2410fb_setcolreg是用来实现伪颜色表和颜色表的填充。
对于fb_ops中的成员中s3c2410fb_check_va这个函数在上面probe探测函数中已经讲过了,剩下的任务就是分析下激活fb_info参数配置函数s3c2410fb_set_par和显示空白函数s3c2410fb_blank。
首先看看显示空白函数s3c2410fb_blank
static int s3c2410fb_blank(int blank_mode, struct fb_info *info)
{
struct s3c2410fb_info *fbi = info->par; //获得fb_info私有数据
void __iomem *tpal_reg = fbi->io; //获得内存指针
dprintk("blank(mode=%d, info=%p)\n", blank_mode, info);
tpal_reg += is_s3c2412(fbi) ? S3C2412_TPAL : S3C2410_TPAL;
//根据显示空白的模式设置LCD开启或停止
if (blank_mode == FB_BLANK_POWERDOWN) {
s3c2410fb_lcd_enable(fbi, 0); //停止LCD
} else {
s3c2410fb_lcd_enable(fbi, 1); //开启LCD
}
//根据显示空白的模式控制临时调色板是否有效
if (blank_mode == FB_BLANK_UNBLANK)
writel(0x0, tpal_reg); //调色板寄存器无效
else {
dprintk("setting TPAL to output 0x000000\n");
writel(S3C2410_TPAL_EN, tpal_reg); //调色板寄存器有效
}
return 0;
}
跟踪s3c2410fb_blank中的s3c2410fb_lcd_enable函数
static void s3c2410fb_lcd_enable(struct s3c2410fb_info *fbi, int enable)
{
unsigned long flags;
local_irq_save(flags);
if (enable)
fbi->regs.lcdcon1 |= S3C2410_LCDCON1_ENVID; //开启LCD
else
fbi->regs.lcdcon1 &= ~S3C2410_LCDCON1_ENVID; //关闭LCD
writel(fbi->regs.lcdcon1, fbi->io + S3C2410_LCDCON1);
local_irq_restore(flags);
}
接着看看这个根据fbinfo->var激活fb_info中的参数配置函数s3c2410fb_set_par
static int s3c2410fb_set_par(struct fb_info *info)
{
struct fb_var_screeninfo *var = &info->var;
switch (var->bits_per_pixel) {//根据色位模式设置色彩模式
case 32:
case 16:
case 12:
info->fix.visual = FB_VISUAL_TRUECOLOR;
break;
case 1:
info->fix.visual = FB_VISUAL_MONO01;
break;
default:
info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
break;
}
//设置fb_info中固定参数中一行的字节数
info->fix.line_length = (var->xres_virtual * var->bits_per_pixel) / 8;
s3c2410fb_activate_var(info); //激活fb_info参数配置
return 0;
}
我们看看s3c2410fb_set_par中激活fb_info参数配置函数s3c2410fb_activate_var
static void s3c2410fb_activate_var(struct fb_info *info)
{
struct s3c2410fb_info *fbi = info->par;
void __iomem *regs = fbi->io;
int type = fbi->regs.lcdcon1 & S3C2410_LCDCON1_TFT;
struct fb_var_screeninfo *var = &info->var;
int clkdiv;
//计算LCD控制器1中的CLKVAL值
clkdiv = DIV_ROUND_UP(s3c2410fb_calc_pixclk(fbi, var->pixclock), 2);
dprintk("%s: var->xres = %d\n", __func__, var->xres);
dprintk("%s: var->yres = %d\n", __func__, var->yres);
dprintk("%s: var->bpp = %d\n", __func__, var->bits_per_pixel);
if (type == S3C2410_LCDCON1_TFT) { //配置TFT屏LCD控制寄存器
s3c2410fb_calculate_tft_lcd_regs(info, &fbi->regs);
--clkdiv;
if (clkdiv < 0)
clkdiv = 0;
} else { //配置STN屏LCD控制寄存器
s3c2410fb_calculate_stn_lcd_regs(info, &fbi->regs);
if (clkdiv < 2)
clkdiv = 2;
}
//设置LCD控制器1中的CLKVAL值
fbi->regs.lcdcon1 |= S3C2410_LCDCON1_CLKVAL(clkdiv);
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);
//设置LCD控制器1-5的参数
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);
s3c2410fb_set_lcdaddr(info); //设置帧缓冲起始地址寄存器1-3
fbi->regs.lcdcon1 |= S3C2410_LCDCON1_ENVID,
writel(fbi->regs.lcdcon1, regs + S3C2410_LCDCON1);
}
下面我们主要关注s3c2410fb_calculate_tft_lcd_regs和s3c2410fb_set_lcdaddr函数
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;
switch (var->bits_per_pixel) {//根据色模式设置LCD控制器1和5
case 1:
regs->lcdcon1 |= S3C2410_LCDCON1_TFT1BPP;
break;
case 2:
regs->lcdcon1 |= S3C2410_LCDCON1_TFT2BPP;
break;
case 4:
regs->lcdcon1 |= S3C2410_LCDCON1_TFT4BPP;
break;
case 8:
regs->lcdcon1 |= S3C2410_LCDCON1_TFT8BPP;
regs->lcdcon5 |= S3C2410_LCDCON5_BSWP |
S3C2410_LCDCON5_FRM565;
regs->lcdcon5 &= ~S3C2410_LCDCON5_HWSWP;
break;
case 16:
regs->lcdcon1 |= S3C2410_LCDCON1_TFT16BPP;
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);
break;
default:
dev_err(fbi->dev, "invalid bpp %d\n",
var->bits_per_pixel);
}
dprintk("setting vert: up=%d, low=%d, sync=%d\n",
var->upper_margin, var->lower_margin, var->vsync_len);
dprintk("setting horz: lft=%d, rt=%d, sync=%d\n",
var->left_margin, var->right_margin, var->hsync_len);
//设置LCD控制器2、3、4
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);
regs->lcdcon3 = S3C2410_LCDCON3_HBPD(var->right_margin - 1) |
S3C2410_LCDCON3_HFPD(var->left_margin - 1) |
S3C2410_LCDCON3_HOZVAL(var->xres - 1);
regs->lcdcon4 = S3C2410_LCDCON4_HSPW(var->hsync_len - 1);
}
static void s3c2410fb_set_lcdaddr(struct fb_info *info)
{
unsigned long saddr1, saddr2, saddr3;
struct s3c2410fb_info *fbi = info->par;
void __iomem *regs = fbi->io;
saddr1 = info->fix.smem_start >> 1;
saddr2 = info->fix.smem_start;
saddr2 += info->fix.line_length * info->var.yres;
saddr2 >>= 1;
saddr3 = S3C2410_OFFSIZE(0) |
S3C2410_PAGEWIDTH((info->fix.line_length / 2) & 0x3ff);
dprintk("LCDSADDR1 = 0x%08lx\n", saddr1);
dprintk("LCDSADDR2 = 0x%08lx\n", saddr2);
dprintk("LCDSADDR3 = 0x%08lx\n", saddr3);
//初始化LCD控制器的地址指针
writel(saddr1, regs + S3C2410_LCDSADDR1);
writel(saddr2, regs + S3C2410_LCDSADDR2);
writel(saddr3, regs + S3C2410_LCDSADDR3);
}
归纳下我们分析的这个激活fb_info参数配置函数s3c2410fb_activate_var,该函数主要是计算clkval的值,计算LCD控制器1-5的值,然后设置LCD控制器1-5,并设置帧缓冲寄存器。