linux驱动摸索
内核版本:linux-2.6.32.2
开发板:mini2440
TFT屏:统宝3.5寸屏
LCD内核框架:
内核自带的LCD驱动程序:drivers/video/S3c2410fb.c
这个驱动程序通过提供最底层的操作来被核心层调用,这个核心层提供了应用程序所需 open/close,read/write 其中drivers/video/fbmem.c提供了对LCD的所有抽象操作。
LCD硬件原理简介:
• VD3-VD23: video data引脚,lcd控制器发的数据在这几根线上
• VLINE: 能发出HSYNC信号,用于切换到下一行扫描
• VFRAME: 能发出VSYNC信号,用于返回到最开始行的扫描
• VCLK:控制器发给lcd的时钟引脚,根据lcd手册设置
• VM/VDEN: video data 使能引脚
LCD驱动编写步骤
1.分配一个fb_info结构
mini2440fb = framebuffer_alloc(0, NULL);
注意:每一个lcd驱动程序都要分配一个fb_info结构体,framebuffer_alloc的用法可以参考其他lcd驱动,第一个参数0表示只需要分配本结构体的长度,
第二个NULL,表示无device结构,返回值为fb_info结构体。
2.设置fb_info的fix,val等相关参数,例如:
mini2440fb->fix.smem_len = MINI2440_LCD_WIDTH*MINI2440_LCD_LENGTH*MINI2440_LCD_DEEPTH/8; /* Length of frame buffer mem */mini2440fb->fix.type = FB_TYPE_PACKED_PIXELS; /* see FB_TYPE_**/mini2440fb->fix.visual = FB_VISUAL_TRUECOLOR; /* see FB_VISUAL_**/ mini2440fb->fix.line_length = MINI2440_LCD_WIDTH*MINI2440_LCD_DEEPTH/8; /* length of a line in bytes */
注意:在设置操作函数中这个操作函数是必须的,具体可参考
S3c2410fb.c中的相关配置,还有在mini2440中LCD位宽是24,但是2440里会分配4字节即32位(浪费1字节)即设置
MINI2440_LCD_DEEPTH 为32.
3.硬件相关的操作:
参考s3c2440手册和lcd手册,配置相关参数,在mini2440开发板中。背光控制引脚是GPG4.
4.注册
register_framebuffer(mini2440fb);
mini2440 LCD驱动程序,源码如下:
#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/platform_device.h>
#include <linux/clk.h>
#include <linux/cpufreq.h>#include <asm/io.h>
#include <asm/div64.h>#include <asm/mach/map.h>
#include <mach/regs-lcd.h>
#include <mach/regs-gpio.h>
#include <mach/fb.h>#define MINI2440_LCD_WIDTH 240
#define MINI2440_LCD_LENGTH 320
#define MINI2440_LCD_DEEPTH 32struct 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 lpcsel;
};static volatile unsigned long *gpccon;
static volatile unsigned long *gpdcon;
static volatile unsigned long *gpgcon;
static volatile unsigned long *gpgdat;
static u32 pseudo_palette[16];static struct fb_info *mini2440fb;
static volatile struct lcd_regs *s3c_lcd_regs;/* 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 mini2440fb_setcolreg(unsigned regno,unsigned red, unsigned green, unsigned blue,unsigned transp, struct fb_info *info)
{unsigned int val;if (regno > 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);//((u32 *)(info->pseudo_palette))[regno] = val;pseudo_palette[regno] = val;return 0;}static struct fb_ops mini2440fb_ops = {.owner = THIS_MODULE,.fb_setcolreg = mini2440fb_setcolreg,.fb_fillrect = cfb_fillrect,.fb_copyarea = cfb_copyarea,.fb_imageblit = cfb_imageblit,
};static int mini2440_tft_t35_init(void)
{/*1.分配一个fb_info结构体*/mini2440fb = framebuffer_alloc(0, NULL); //不需要额外的私有空间,设为0if (!mini2440fb){printk("framebuffer_alloc filed\n");return -ENOMEM;}/*2.设置*//*set fix param*/strcpy(mini2440fb->fix.id, "mytft");mini2440fb->fix.smem_len = MINI2440_LCD_WIDTH*MINI2440_LCD_LENGTH*MINI2440_LCD_DEEPTH/8; /* Length of frame buffer mem */mini2440fb->fix.type = FB_TYPE_PACKED_PIXELS; /* see FB_TYPE_**/mini2440fb->fix.visual = FB_VISUAL_TRUECOLOR; /* see FB_VISUAL_**/ mini2440fb->fix.line_length = MINI2440_LCD_WIDTH*MINI2440_LCD_DEEPTH/8; /* length of a line in bytes *//*set var param*/mini2440fb->var.xres = MINI2440_LCD_WIDTH; /* visible resolution*/mini2440fb->var.yres = MINI2440_LCD_LENGTH;mini2440fb->var.xres_virtual = MINI2440_LCD_WIDTH; /* virtual resolution*/mini2440fb->var.yres_virtual = MINI2440_LCD_LENGTH;mini2440fb->var.bits_per_pixel = MINI2440_LCD_DEEPTH; /* guess what */mini2440fb->var.red.offset = 16; /* bitfield in fb mem if true color, */mini2440fb->var.red.length = 8;mini2440fb->var.green.offset = 8; mini2440fb->var.green.length = 8 ;mini2440fb->var.blue.offset = 0; mini2440fb->var.blue.length = 8; mini2440fb->var.activate = FB_ACTIVATE_NOW; /* see FB_ACTIVATE_**/mini2440fb->fbops = &mini2440fb_ops;mini2440fb->pseudo_palette = pseudo_palette;mini2440fb->screen_size = MINI2440_LCD_WIDTH*MINI2440_LCD_LENGTH*MINI2440_LCD_DEEPTH/8;/*3.硬件相关的配置*/gpccon = ioremap(0x56000020,4);gpdcon = ioremap(0x56000030,4);gpgcon = ioremap(0x56000060,4);gpgdat = gpgcon + 1;/*3.1将GPIOC,GPIOD配成VD模式*/*gpccon = 0xaaaaaaaa;*gpdcon = 0xaaaaaaaa;/*3.2配置背光GPIOG4,低电平,关闭*/*gpgcon &= ~(3<<8);*gpgcon |= (1<<8);*gpgdat &= ~(1<<4);//*gpgcon |= (3<<8);/*3.3配置lcd相关寄存器*/s3c_lcd_regs = ioremap(0X4D000000,sizeof(struct lcd_regs));/* LCDCON1 : * CLKVAL [17:8]:VCLK = HCLK / [(CLKVAL+1) x 2] HCLK=100Mhz,VCLK=6400000* CLKVAL = 7* PNRMODE [6:5]:11 = TFT LCD panel* BPPMODE [4:1]:1100 = 16 bpp for TFT* ENVID [0]:0 = Disable the video output and the LCD control signal.* 1 = Enable the video output and the LCD control signal.*/s3c_lcd_regs->lcdcon1 = (7<<8)|(3<<5)|(0x0d<<1);/* LCDCON2 : * VBPD [31:24]:VBPD+1 = 4* LINEVAL [23:14] : LINEVAL+1=320* VFPD [13:6]:VFPD+1=2* VSPW [5:0]:VSPW+1 =2*/ s3c_lcd_regs->lcdcon2 = (3<<24)|(319<<14)|(1<<6)|(1<<0);/* LCDCON3 : * HBPD (TFT) [25:19]: HBPD+1=30* HOZVAL [18:8] : HOZVAL+1=240* HFPD (TFT) [7:0]:HFPD+1=10*/ s3c_lcd_regs->lcdcon3 = (29<<19)|(239<<8)|(9<<0);/* LCDCON4 : * HSPW(TFT) [7:0]:HSPW+1=10*/ s3c_lcd_regs->lcdcon4 = 0;/* LCDCON5 : * FRM565 [11]: 1 = 5:6:5 Format* INVVCLK [10]:1 = The video data is fetched at VCLK rising edge* INVVLINE [9] 1 = Inverted* INVVFRAME [8] 1 = Inverted* INVVDEN [6] 0 = normal* PWREN [3]: 0 = Disable PWREN signal 1 = Enable PWREN signal* BSWP [1]: 0 见2440手册 413页HWSWP [0]: 0*/ s3c_lcd_regs->lcdcon5 = (1<<10) | (1<<9) | (1<<8) | (0<<12) | (0<<1) | (0<<0);/*分配显存(framebuffer), 并把地址告诉LCD控制器*/mini2440fb->screen_base =dma_alloc_writecombine(NULL,mini2440fb->fix.smem_len,\&mini2440fb->fix.smem_start,GFP_KERNEL);s3c_lcd_regs->lcdsaddr1 = (mini2440fb->fix.smem_start>>1)&(~(3<<30));s3c_lcd_regs->lcdsaddr2 = ((mini2440fb->fix.smem_start+mini2440fb->fix.smem_len)>>1)&\(0x1fffff);s3c_lcd_regs->lcdsaddr3 =(240*MINI2440_LCD_DEEPTH/16); /* 一行的长度(单位: 2字节) */ /*使能LCD*/s3c_lcd_regs->lcdcon1 |= 1;s3c_lcd_regs->lcdcon5 |=(1<<3);*gpgdat |= (1<<4);/*注册*/register_framebuffer(mini2440fb);return 0;
}static void mini2440_tft_t35_exit(void)
{unregister_framebuffer(mini2440fb);s3c_lcd_regs->lcdcon1 &= ~1;s3c_lcd_regs->lcdcon5 &=~(1<<3);*gpgdat &= ~(1<<4);dma_free_writecombine(NULL, mini2440fb->fix.smem_len, mini2440fb->screen_base,\mini2440fb->fix.smem_start);iounmap(gpccon);iounmap(gpdcon);iounmap(gpgcon);iounmap(s3c_lcd_regs);framebuffer_release(mini2440fb);
}/* 这两行指定驱动程序的初始化函数和卸载函数 */
module_init(mini2440_tft_t35_init);
module_exit(mini2440_tft_t35_exit);/* 描述驱动程序的一些信息*/MODULE_LICENSE("GPL");
测试:
1. make menuconfig去掉原来的驱动程序,mini2440开发板提供的内核时,需要把相关的LCD驱动去掉。
-> Device Drivers
-> Graphics support
<M> S3C2410 LCD framebuffer support
2. make uImage
make modules
3. 使用新的uImage启动开发板:
4.
insmod cfbcopyarea.ko
insmod cfbfillrect.ko
insmod cfbimgblt.ko
insmod tft.ko
echo hello > /dev/tty1 // 可以在LCD上看见hello
linux驱动摸索
内核版本:linux-2.6.32.2
开发板:mini2440
TFT屏:统宝3.5寸屏
LCD内核框架:
内核自带的LCD驱动程序:drivers/video/S3c2410fb.c
这个驱动程序通过提供最底层的操作来被核心层调用,这个核心层提供了应用程序所需 open/close,read/write 其中drivers/video/fbmem.c提供了对LCD的所有抽象操作。
LCD硬件原理简介:
• VD3-VD23: video data引脚,lcd控制器发的数据在这几根线上
• VLINE: 能发出HSYNC信号,用于切换到下一行扫描
• VFRAME: 能发出VSYNC信号,用于返回到最开始行的扫描
• VCLK:控制器发给lcd的时钟引脚,根据lcd手册设置
• VM/VDEN: video data 使能引脚
LCD驱动编写步骤
1.分配一个fb_info结构
mini2440fb = framebuffer_alloc(0, NULL);
注意:每一个lcd驱动程序都要分配一个fb_info结构体,framebuffer_alloc的用法可以参考其他lcd驱动,第一个参数0表示只需要分配本结构体的长度,
第二个NULL,表示无device结构,返回值为fb_info结构体。
2.设置fb_info的fix,val等相关参数,例如:
mini2440fb->fix.smem_len = MINI2440_LCD_WIDTH*MINI2440_LCD_LENGTH*MINI2440_LCD_DEEPTH/8; /* Length of frame buffer mem */mini2440fb->fix.type = FB_TYPE_PACKED_PIXELS; /* see FB_TYPE_**/mini2440fb->fix.visual = FB_VISUAL_TRUECOLOR; /* see FB_VISUAL_**/ mini2440fb->fix.line_length = MINI2440_LCD_WIDTH*MINI2440_LCD_DEEPTH/8; /* length of a line in bytes */
注意:在设置操作函数中这个操作函数是必须的,具体可参考
S3c2410fb.c中的相关配置,还有在mini2440中LCD位宽是24,但是2440里会分配4字节即32位(浪费1字节)即设置
MINI2440_LCD_DEEPTH 为32.
3.硬件相关的操作:
参考s3c2440手册和lcd手册,配置相关参数,在mini2440开发板中。背光控制引脚是GPG4.
4.注册
register_framebuffer(mini2440fb);
mini2440 LCD驱动程序,源码如下:
#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/platform_device.h>
#include <linux/clk.h>
#include <linux/cpufreq.h>#include <asm/io.h>
#include <asm/div64.h>#include <asm/mach/map.h>
#include <mach/regs-lcd.h>
#include <mach/regs-gpio.h>
#include <mach/fb.h>#define MINI2440_LCD_WIDTH 240
#define MINI2440_LCD_LENGTH 320
#define MINI2440_LCD_DEEPTH 32struct 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 lpcsel;
};static volatile unsigned long *gpccon;
static volatile unsigned long *gpdcon;
static volatile unsigned long *gpgcon;
static volatile unsigned long *gpgdat;
static u32 pseudo_palette[16];static struct fb_info *mini2440fb;
static volatile struct lcd_regs *s3c_lcd_regs;/* 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 mini2440fb_setcolreg(unsigned regno,unsigned red, unsigned green, unsigned blue,unsigned transp, struct fb_info *info)
{unsigned int val;if (regno > 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);//((u32 *)(info->pseudo_palette))[regno] = val;pseudo_palette[regno] = val;return 0;}static struct fb_ops mini2440fb_ops = {.owner = THIS_MODULE,.fb_setcolreg = mini2440fb_setcolreg,.fb_fillrect = cfb_fillrect,.fb_copyarea = cfb_copyarea,.fb_imageblit = cfb_imageblit,
};static int mini2440_tft_t35_init(void)
{/*1.分配一个fb_info结构体*/mini2440fb = framebuffer_alloc(0, NULL); //不需要额外的私有空间,设为0if (!mini2440fb){printk("framebuffer_alloc filed\n");return -ENOMEM;}/*2.设置*//*set fix param*/strcpy(mini2440fb->fix.id, "mytft");mini2440fb->fix.smem_len = MINI2440_LCD_WIDTH*MINI2440_LCD_LENGTH*MINI2440_LCD_DEEPTH/8; /* Length of frame buffer mem */mini2440fb->fix.type = FB_TYPE_PACKED_PIXELS; /* see FB_TYPE_**/mini2440fb->fix.visual = FB_VISUAL_TRUECOLOR; /* see FB_VISUAL_**/ mini2440fb->fix.line_length = MINI2440_LCD_WIDTH*MINI2440_LCD_DEEPTH/8; /* length of a line in bytes *//*set var param*/mini2440fb->var.xres = MINI2440_LCD_WIDTH; /* visible resolution*/mini2440fb->var.yres = MINI2440_LCD_LENGTH;mini2440fb->var.xres_virtual = MINI2440_LCD_WIDTH; /* virtual resolution*/mini2440fb->var.yres_virtual = MINI2440_LCD_LENGTH;mini2440fb->var.bits_per_pixel = MINI2440_LCD_DEEPTH; /* guess what */mini2440fb->var.red.offset = 16; /* bitfield in fb mem if true color, */mini2440fb->var.red.length = 8;mini2440fb->var.green.offset = 8; mini2440fb->var.green.length = 8 ;mini2440fb->var.blue.offset = 0; mini2440fb->var.blue.length = 8; mini2440fb->var.activate = FB_ACTIVATE_NOW; /* see FB_ACTIVATE_**/mini2440fb->fbops = &mini2440fb_ops;mini2440fb->pseudo_palette = pseudo_palette;mini2440fb->screen_size = MINI2440_LCD_WIDTH*MINI2440_LCD_LENGTH*MINI2440_LCD_DEEPTH/8;/*3.硬件相关的配置*/gpccon = ioremap(0x56000020,4);gpdcon = ioremap(0x56000030,4);gpgcon = ioremap(0x56000060,4);gpgdat = gpgcon + 1;/*3.1将GPIOC,GPIOD配成VD模式*/*gpccon = 0xaaaaaaaa;*gpdcon = 0xaaaaaaaa;/*3.2配置背光GPIOG4,低电平,关闭*/*gpgcon &= ~(3<<8);*gpgcon |= (1<<8);*gpgdat &= ~(1<<4);//*gpgcon |= (3<<8);/*3.3配置lcd相关寄存器*/s3c_lcd_regs = ioremap(0X4D000000,sizeof(struct lcd_regs));/* LCDCON1 : * CLKVAL [17:8]:VCLK = HCLK / [(CLKVAL+1) x 2] HCLK=100Mhz,VCLK=6400000* CLKVAL = 7* PNRMODE [6:5]:11 = TFT LCD panel* BPPMODE [4:1]:1100 = 16 bpp for TFT* ENVID [0]:0 = Disable the video output and the LCD control signal.* 1 = Enable the video output and the LCD control signal.*/s3c_lcd_regs->lcdcon1 = (7<<8)|(3<<5)|(0x0d<<1);/* LCDCON2 : * VBPD [31:24]:VBPD+1 = 4* LINEVAL [23:14] : LINEVAL+1=320* VFPD [13:6]:VFPD+1=2* VSPW [5:0]:VSPW+1 =2*/ s3c_lcd_regs->lcdcon2 = (3<<24)|(319<<14)|(1<<6)|(1<<0);/* LCDCON3 : * HBPD (TFT) [25:19]: HBPD+1=30* HOZVAL [18:8] : HOZVAL+1=240* HFPD (TFT) [7:0]:HFPD+1=10*/ s3c_lcd_regs->lcdcon3 = (29<<19)|(239<<8)|(9<<0);/* LCDCON4 : * HSPW(TFT) [7:0]:HSPW+1=10*/ s3c_lcd_regs->lcdcon4 = 0;/* LCDCON5 : * FRM565 [11]: 1 = 5:6:5 Format* INVVCLK [10]:1 = The video data is fetched at VCLK rising edge* INVVLINE [9] 1 = Inverted* INVVFRAME [8] 1 = Inverted* INVVDEN [6] 0 = normal* PWREN [3]: 0 = Disable PWREN signal 1 = Enable PWREN signal* BSWP [1]: 0 见2440手册 413页HWSWP [0]: 0*/ s3c_lcd_regs->lcdcon5 = (1<<10) | (1<<9) | (1<<8) | (0<<12) | (0<<1) | (0<<0);/*分配显存(framebuffer), 并把地址告诉LCD控制器*/mini2440fb->screen_base =dma_alloc_writecombine(NULL,mini2440fb->fix.smem_len,\&mini2440fb->fix.smem_start,GFP_KERNEL);s3c_lcd_regs->lcdsaddr1 = (mini2440fb->fix.smem_start>>1)&(~(3<<30));s3c_lcd_regs->lcdsaddr2 = ((mini2440fb->fix.smem_start+mini2440fb->fix.smem_len)>>1)&\(0x1fffff);s3c_lcd_regs->lcdsaddr3 =(240*MINI2440_LCD_DEEPTH/16); /* 一行的长度(单位: 2字节) */ /*使能LCD*/s3c_lcd_regs->lcdcon1 |= 1;s3c_lcd_regs->lcdcon5 |=(1<<3);*gpgdat |= (1<<4);/*注册*/register_framebuffer(mini2440fb);return 0;
}static void mini2440_tft_t35_exit(void)
{unregister_framebuffer(mini2440fb);s3c_lcd_regs->lcdcon1 &= ~1;s3c_lcd_regs->lcdcon5 &=~(1<<3);*gpgdat &= ~(1<<4);dma_free_writecombine(NULL, mini2440fb->fix.smem_len, mini2440fb->screen_base,\mini2440fb->fix.smem_start);iounmap(gpccon);iounmap(gpdcon);iounmap(gpgcon);iounmap(s3c_lcd_regs);framebuffer_release(mini2440fb);
}/* 这两行指定驱动程序的初始化函数和卸载函数 */
module_init(mini2440_tft_t35_init);
module_exit(mini2440_tft_t35_exit);/* 描述驱动程序的一些信息*/MODULE_LICENSE("GPL");
测试:
1. make menuconfig去掉原来的驱动程序,mini2440开发板提供的内核时,需要把相关的LCD驱动去掉。
-> Device Drivers
-> Graphics support
<M> S3C2410 LCD framebuffer support
2. make uImage
make modules
3. 使用新的uImage启动开发板:
4.
insmod cfbcopyarea.ko
insmod cfbfillrect.ko
insmod cfbimgblt.ko
insmod tft.ko
echo hello > /dev/tty1 // 可以在LCD上看见hello