內核自帶的驅動LCD,drivers/video/Fbmem.c LCD驅動程式 假設app: open("/dev/fb0", ...) 主設備號: 29, 次設備號: 0 kernel: fb_open int fbidx = iminor(inode); struct fb_info *inf ...
內核自帶的驅動LCD,drivers/video/Fbmem.c
LCD驅動程式
假設
app: open("/dev/fb0", ...) 主設備號: 29, 次設備號: 0
--------------------------------------------------------------
kernel:
fb_open
int fbidx = iminor(inode);
struct fb_info *info = = registered_fb[0];
以次設備號為下標。
app: read()
---------------------------------------------------------------
kernel:
fb_read
int fbidx = iminor(inode);
struct fb_info *info = registered_fb[fbidx];
if (info->fbops->fb_read)
return info->fbops->fb_read(info, buf, count, ppos);
src = (u32 __iomem *) (info->screen_base + p);
dst = buffer;
*dst++ = fb_readl(src++);
copy_to_user(buf, buffer, c)
1. registered_fb在哪裡被設置?
答. register_framebuffer
怎麼寫LCD驅動程式?
1. 分配一個fb_info結構體: framebuffer_alloc
2. 設置
3. 註冊: register_framebuffer
4. 硬體相關的操作
fbmem.c都是抽象出來的,最終都得依賴於底層的驅動。
VCLK 看手冊,給合適的頻率
VLINE--HYSNC 行同步信號
VFRAM--VSYNC 幀同步信號
VDEN(video date enable) 顏色
硬體操作:根據LCD手冊,設置LCD控制器;
分配顯存,並把地址告訴LCD控制器
配置引腳用於LCD
初始化:
/* 1. 分配一個fb_info */
/* 2. 設置 */
/* 2.1 設置固定的參數 */
/* 2.2 設置可變的參數 */
/* 2.3 設置操作函數 */
/* 2.4 其他的設置 */
/* 3. 硬體相關的操作 */
/* 3.1 配置GPIO用於LCD */
/* 3.2 根據LCD手冊設置LCD控制器, 比如VCLK的頻率等 */
/* 3.3 分配顯存(framebuffer), 並把地址告訴LCD控制器 */
/* 4. 註冊 */
我們看到的圖像是從左到右,從上到下
測試:配置內核
1. make menuconfig去掉原來的驅動程式
-> Device Drivers
-> Graphics support
<M> S3C2410 LCD framebuffer support
2. make uImage
make modules
3. 使用新的uImage啟動開發板:
具體驅動程式代碼:
1 #include <linux/module.h> 2 #include <linux/kernel.h> 3 #include <linux/errno.h> 4 #include <linux/string.h> 5 #include <linux/mm.h> 6 #include <linux/slab.h> 7 #include <linux/delay.h> 8 #include <linux/fb.h> 9 #include <linux/init.h> 10 #include <linux/dma-mapping.h> 11 #include <linux/interrupt.h> 12 #include <linux/workqueue.h> 13 #include <linux/wait.h> 14 #include <linux/platform_device.h> 15 #include <linux/clk.h> 16 17 #include <asm/io.h> 18 #include <asm/uaccess.h> 19 #include <asm/div64.h> 20 21 #include <asm/mach/map.h> 22 #include <asm/arch/regs-lcd.h> 23 #include <asm/arch/regs-gpio.h> 24 #include <asm/arch/fb.h> 25 26 static int s3c_lcdfb_setcolreg(unsigned int regno, unsigned int red, 27 unsigned int green, unsigned int blue, 28 unsigned int transp, struct fb_info *info); 29 30 31 struct lcd_regs { 32 unsigned long lcdcon1; 33 unsigned long lcdcon2; 34 unsigned long lcdcon3; 35 unsigned long lcdcon4; 36 unsigned long lcdcon5; 37 unsigned long lcdsaddr1; 38 unsigned long lcdsaddr2; 39 unsigned long lcdsaddr3; 40 unsigned long redlut; 41 unsigned long greenlut; 42 unsigned long bluelut; 43 unsigned long reserved[9]; 44 unsigned long dithmode; 45 unsigned long tpal; 46 unsigned long lcdintpnd; 47 unsigned long lcdsrcpnd; 48 unsigned long lcdintmsk; 49 unsigned long lpcsel; 50 }; 51 52 static struct fb_ops s3c_lcdfb_ops = { 53 .owner = THIS_MODULE, 54 .fb_setcolreg = s3c_lcdfb_setcolreg, 55 .fb_fillrect = cfb_fillrect, 56 .fb_copyarea = cfb_copyarea, 57 .fb_imageblit = cfb_imageblit, 58 }; 59 60 61 static struct fb_info *s3c_lcd; 62 static volatile unsigned long *gpbcon; 63 static volatile unsigned long *gpbdat; 64 static volatile unsigned long *gpccon; 65 static volatile unsigned long *gpdcon; 66 static volatile unsigned long *gpgcon; 67 static volatile struct lcd_regs* lcd_regs; 68 static u32 pseudo_palette[16]; 69 70 71 /* from pxafb.c */ 72 static inline unsigned int chan_to_field(unsigned int chan, struct fb_bitfield *bf) 73 { 74 chan &= 0xffff; 75 chan >>= 16 - bf->length; 76 return chan << bf->offset; 77 } 78 79 80 static int s3c_lcdfb_setcolreg(unsigned int regno, unsigned int red, 81 unsigned int green, unsigned int blue, 82 unsigned int transp, struct fb_info *info) 83 { 84 unsigned int val; 85 86 if (regno > 16) 87 return 1; 88 89 /* 用red,green,blue三原色構造出val */ 90 val = chan_to_field(red, &info->var.red); 91 val |= chan_to_field(green, &info->var.green); 92 val |= chan_to_field(blue, &info->var.blue); 93 94 //((u32 *)(info->pseudo_palette))[regno] = val; 95 pseudo_palette[regno] = val; 96 return 0; 97 } 98 99 static int lcd_init(void) 100 { 101 /* 1. 分配一個fb_info */ 102 s3c_lcd = framebuffer_alloc(0, NULL); 103 104 /* 2. 設置 */ 105 /* 2.1 設置固定的參數 */ 106 strcpy(s3c_lcd->fix.id, "mylcd"); 107 s3c_lcd->fix.smem_len = 240*320*16/8; 108 s3c_lcd->fix.type = FB_TYPE_PACKED_PIXELS; 109 s3c_lcd->fix.visual = FB_VISUAL_TRUECOLOR; /* TFT */ 110 s3c_lcd->fix.line_length = 240*2; 111 112 /* 2.2 設置可變的參數 */ 113 s3c_lcd->var.xres = 240; 114 s3c_lcd->var.yres = 320; 115 s3c_lcd->var.xres_virtual = 240; 116 s3c_lcd->var.yres_virtual = 320; 117 s3c_lcd->var.bits_per_pixel = 16; 118 119 /* RGB:565 */ 120 s3c_lcd->var.red.offset = 11; 121 s3c_lcd->var.red.length = 5; 122 123 s3c_lcd->var.green.offset = 5; 124 s3c_lcd->var.green.length = 6; 125 126 s3c_lcd->var.blue.offset = 0; 127 s3c_lcd->var.blue.length = 5; 128 129 s3c_lcd->var.activate = FB_ACTIVATE_NOW; 130 131 132 /* 2.3 設置操作函數 */ 133 s3c_lcd->fbops = &s3c_lcdfb_ops; 134 135 /* 2.4 其他的設置 */ 136 s3c_lcd->pseudo_palette = pseudo_palette; 137 //s3c_lcd->screen_base = ; /* 顯存的虛擬地址 */ 138 s3c_lcd->screen_size = 240*324*16/8; 139 140 /* 3. 硬體相關的操作 */ 141 /* 3.1 配置GPIO用於LCD */ 142 gpbcon = ioremap(0x56000010, 8); 143 gpbdat = gpbcon+1; 144 gpccon = ioremap(0x56000020, 4); 145 gpdcon = ioremap(0x56000030, 4); 146 gpgcon = ioremap(0x56000060, 4); 147 148 *gpccon = 0xaaaaaaaa; /* GPIO管腳用於VD[7:0],LCDVF[2:0],VM,VFRAME,VLINE,VCLK,LEND */ 149 *gpdcon = 0xaaaaaaaa; /* GPIO管腳用於VD[23:8] */ 150 151 *gpbcon &= ~(3); /* GPB0設置為輸出引腳 */ 152 *gpbcon |= 1; 153 *gpbdat &= ~1; /* 輸出低電平 */ 154 155 *gpgcon |= (3<<8); /* GPG4用作LCD_PWREN */ 156 157 /* 3.2 根據LCD手冊設置LCD控制器, 比如VCLK的頻率等 */ 158 lcd_regs = ioremap(0x4D000000, sizeof(struct lcd_regs)); 159 160 /* bit[17:8]: VCLK = HCLK / [(CLKVAL+1) x 2], LCD手冊P14 161 * 10MHz(100ns) = 100MHz / [(CLKVAL+1) x 2] 162 * CLKVAL = 4 163 * bit[6:5]: 0b11, TFT LCD 164 * bit[4:1]: 0b1100, 16 bpp for TFT 165 * bit[0] : 0 = Disable the video output and the LCD control signal. 166 */ 167 lcd_regs->lcdcon1 = (4<<8) | (3<<5) | (0x0c<<1); 168 169 #if 1 170 /* 垂直方向的時間參數 171 * bit[31:24]: VBPD, VSYNC之後再過多長時間才能發出第1行數據 172 * LCD手冊 T0-T2-T1=4 173 * VBPD=3 174 * bit[23:14]: 多少行, 320, 所以LINEVAL=320-1=319 175 * bit[13:6] : VFPD, 發出最後一行數據之後,再過多長時間才發出VSYNC 176 * LCD手冊T2-T5=322-320=2, 所以VFPD=2-1=1 177 * bit[5:0] : VSPW, VSYNC信號的脈衝寬度, LCD手冊T1=1, 所以VSPW=1-1=0 178 */ 179 lcd_regs->lcdcon2 = (3<<24) | (319<<14) | (1<<6) | (0<<0); 180 181 182 /* 水平方向的時間參數 183 * bit[25:19]: HBPD, VSYNC之後再過多長時間才能發出第1行數據 184 * LCD手冊 T6-T7-T8=17 185 * HBPD=16 186 * bit[18:8]: 多少列, 240, 所以HOZVAL=240-1=239 187 * bit[7:0] : HFPD, 發出最後一行里最後一個象素數據之後,再過多長時間才發出HSYNC 188 * LCD手冊T8-T11=251-240=11, 所以HFPD=11-1=10 189 */ 190 lcd_regs->lcdcon3 = (16<<19) | (239<<8) | (10<<0); 191 192 /* 水平方向的同步信號 193 * bit[7:0] : HSPW, HSYNC信號的脈衝寬度, LCD手冊T7=5, 所以HSPW=5-1=4 194 */ 195 lcd_regs->lcdcon4 = 4; 196 197 #else 198 lcd_regs->lcdcon2 = S3C2410_LCDCON2_VBPD(5) | \ 199 S3C2410_LCDCON2_LINEVAL(319) | \ 200 S3C2410_LCDCON2_VFPD(3) | \ 201 S3C2410_LCDCON2_VSPW(1); 202 203 lcd_regs->lcdcon3 = S3C2410_LCDCON3_HBPD(10) | \ 204 S3C2410_LCDCON3_HOZVAL(239) | \ 205 S3C2410_LCDCON3_HFPD(1); 206 207 lcd_regs->lcdcon4 = S3C2410_LCDCON4_MVAL(13) | \ 208 S3C2410_LCDCON4_HSPW(0); 209 210 #endif 211 /* 信號的極性 212 * bit[11]: 1=565 format 213 * bit[10]: 0 = The video data is fetched at VCLK falling edge 214 * bit[9] : 1 = HSYNC信號要反轉,即低電平有效 215 * bit[8] : 1 = VSYNC信號要反轉,即低電平有效 216 * bit[6] : 0 = VDEN不用反轉 217 * bit[3] : 0 = PWREN輸出0 218 * bit[1] : 0 = BSWP 219 * bit[0] : 1 = HWSWP 2440手冊P413 220 */ 221 lcd_regs->lcdcon5 = (1<<11) | (0<<10) | (1<<9) | (1<<8) | (1<<0); 222 223 /* 3.3 分配顯存(framebuffer), 並把地址告訴LCD控制器 */ 224 s3c_lcd->screen_base = dma_alloc_writecombine(NULL, s3c_lcd->fix.smem_len, &s3c_lcd->fix.smem_start, GFP_KERNEL); 225 226 lcd_regs->lcdsaddr1 = (s3c_lcd->fix.smem_start >> 1) & ~(3<<30); 227 lcd_regs->lcdsaddr2 = ((s3c_lcd->fix.smem_start + s3c_lcd->fix.smem_len) >> 1) & 0x1fffff; 228 lcd_regs->lcdsaddr3 = (240*16/16); /* 一行的長度(單位: 2位元組) */ 229 230 //s3c_lcd->fix.smem_start = xxx; /* 顯存的物理地址 */ 231 /* 啟動LCD */ 232 lcd_regs->lcdcon1 |= (1<<0); /* 使能LCD控制器 */ 233 lcd_regs->lcdcon5 |= (1<<3); /* 使能LCD本身 */ 234 *gpbdat |= 1; /* 輸出高電平, 使能背光 */ 235 236 /* 4. 註冊 */ 237 register_framebuffer(s3c_lcd); 238 239 return 0; 240 } 241 242 static void lcd_exit(void) 243 { 244 unregister_framebuffer(s3c_lcd); 245 lcd_regs->lcdcon1 &= ~(1<<0); /* 關閉LCD本身 */ 246 *gpbdat &= ~1; /* 關閉背光 */ 247 dma_free_writecombine(NULL, s3c_lcd->fix.smem_len, s3c_lcd->screen_base, s3c_lcd->fix.smem_start); 248 iounmap(lcd_regs); 249 iounmap(gpbcon); 250 iounmap(gpccon); 251 iounmap(gpdcon); 252 iounmap(gpgcon); 253 framebuffer_release(s3c_lcd); 254 } 255 256 module_init(lcd_init); 257 module_exit(lcd_exit); 258 259 MODULE_LICENSE("GPL");LCD Code
insmod cfbcopyarea.ko
insmod cfbfillrect.ko
insmod cfbimgblt.ko
insmod lcd.ko
還有一種是系統自帶的LCD驅動程式,使用分層分離所寫,再來學習!