VDEN 使能信號 HSYNC 水平方向的同步信號 VSYNC 垂直方向的同步信號 LED-/LED+ 背光信號 VCLK 時鐘信號 VD0~VD23 數字信號 GPB0輸出為1 驅動程式中: lcd_init: static struct fb_info *s3c_lcd; //定義一個結構體 / ...
VDEN 使能信號 HSYNC 水平方向的同步信號 VSYNC 垂直方向的同步信號 LED-/LED+ 背光信號 VCLK 時鐘信號 VD0~VD23 數字信號 GPB0輸出為1
驅動程式中: lcd_init: --------------------------------------------------------- static struct fb_info *s3c_lcd; //定義一個結構體 /* 1. 分配一個fb_info */ s3c_lcd = framebuffer_alloc(0, NULL); //不需要額外的空間
--------------------3.4.2內核中的函數----------------------------------------- struct fb_info { struct fb_var_screeninfo var; /* Current var可變參數 */ struct fb_fix_screeninfo fix; /* Current fix 固定參數*/ struct fb_ops *fbops; /*操作參數*/ void *pseudo_palette; /* Fake palette of 16 colors 其他設置*/ unsigned long screen_size; /* Amount of ioremapped VRAM or 0顯存的大小 */ } ----------------------------------------------------------------------------- /* 2. 設置 */ /* 2.1 設置固定的參數 */ strcpy(s3c_lcd->fix.id, "mylcd"); //名字 s3c_lcd->fix.smem_len = 480*272*16/8; //顯存的長度 s3c_lcd->fix.type = FB_TYPE_PACKED_PIXELS; //看不懂預設0 s3c_lcd->fix.visual = FB_VISUAL_TRUECOLOR; /* TFT (真彩色)*/ s3c_lcd->fix.line_length = 480*2; //長度*2個位元組
--------------------3.4.2內核中的函數-----------------------------------------
struct fb_fix_screeninfo { char id[16]; /* identification string eg "TT Builtin" */ unsigned long smem_start; /* Start of frame buffer mem */ /* (physical address) */ __u32 smem_len; /* Length of frame buffer mem */ __u32 type; /* see FB_TYPE_* */ __u32 type_aux; /* Interleave for interleaved Planes */ __u32 visual; /* see FB_VISUAL_* */ __u16 xpanstep; /* zero if no hardware panning */ __u16 ypanstep; /* zero if no hardware panning */ __u16 ywrapstep; /* zero if no hardware ywrap */ __u32 line_length; /* length of a line in bytes */ unsigned long mmio_start; /* Start of Memory Mapped I/O */ /* (physical address) */ __u32 mmio_len; /* Length of Memory Mapped I/O */ __u32 accel; /* Indicate to driver which */ /* specific chip/card we have */ __u16 capabilities; /* see FB_CAP_* */ __u16 reserved[2]; /* Reserved for future compatibility */ }; --------------------------------------------------------------------- /* 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; //每個像素用多少位 /* RGB:565 */ s3c_lcd->var.red.offset = 11; //從那位開始 s3c_lcd->var.red.length = 5; //長度 s3c_lcd->var.green.offset = 5; s3c_lcd->var.green.length = 6; s3c_lcd->var.blue.offset = 0; s3c_lcd->var.blue.length = 5; s3c_lcd->var.activate = FB_ACTIVATE_NOW;
--------------------3.4.2內核中的函數-----------------------------------------
struct fb_var_screeninfo { __u32 xres; /* visible resolution */ __u32 yres; __u32 xres_virtual; /* virtual resolution */ __u32 yres_virtual; __u32 xoffset; /* offset from virtual to visible */ __u32 yoffset; /* resolution */ __u32 bits_per_pixel; /* guess what */ __u32 grayscale; /* 0 = color, 1 = grayscale, */ /* >1 = FOURCC */ struct fb_bitfield red; /* bitfield in fb mem if true color, */ struct fb_bitfield green; /* else only length is significant */ struct fb_bitfield blue; struct fb_bitfield transp; /* transparency */ __u32 nonstd; /* != 0 Non standard pixel format */ __u32 activate; /* see FB_ACTIVATE_* */ __u32 height; /* height of picture in mm */ __u32 width; /* width of picture in mm */ __u32 accel_flags; /* (OBSOLETE) see fb_info.flags */ /* Timing: All values in pixclocks, except pixclock (of course) */ __u32 pixclock; /* pixel clock in ps (pico seconds) */ __u32 left_margin; /* time from sync to picture */ __u32 right_margin; /* time from picture to sync */ __u32 upper_margin; /* time from sync to picture */ __u32 lower_margin; __u32 hsync_len; /* length of horizontal sync */ __u32 vsync_len; /* length of vertical sync */ __u32 sync; /* see FB_SYNC_* */ __u32 vmode; /* see FB_VMODE_* */ __u32 rotate; /* angle we rotate counter clockwise */ __u32 colorspace; /* colorspace for FOURCC-based modes */ __u32 reserved[4]; /* Reserved for future compatibility */ }; --------------------------------------------------------------------- /* 2.3 設置操作函數 */ s3c_lcd->fbops = &s3c_lcdfb_ops; static struct fb_ops s3c_lcdfb_ops = { .owner = THIS_MODULE, .fb_setcolreg = s3c_lcdfb_setcolreg, .fb_fillrect = cfb_fillrect, .fb_copyarea = cfb_copyarea, .fb_imageblit = cfb_imageblit, }; /* 2.4 其他的設置 */ static u32 pseudo_palette[16]; //定義 s3c_lcd->pseudo_palette = pseudo_palette; //調色板 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) 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; } /* 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; } //s3c_lcd->screen_base = ; /* 顯存的虛擬地址 */ s3c_lcd->screen_size = 480*272*16/8; //顯存的大小 /* 3. 硬體相關的操作 */ /* 3.1 配置GPIO用於LCD */ /*定義*/ static volatile unsigned long *gpbcon; static volatile unsigned long *gpbdat; static volatile unsigned long *gpccon; static volatile unsigned long *gpdcon; static volatile unsigned long *gpgcon; gpbcon = ioremap(0x56000010, 8); gpbdat = gpbcon+1; gpccon = ioremap(0x56000020, 4); gpdcon = ioremap(0x56000030, 4); gpgcon = ioremap(0x56000060, 4); *gpccon = 0xaaaaaaaa; /* GPIO管腳用於VD[7:0],LCDVF[2:0],VM,VFRAME,VLINE,VCLK,LEND */ *gpdcon = 0xaaaaaaaa; /* GPIO管腳用於VD[23:8] */ *gpbcon &= ~(3); /* GPB0設置為輸出引腳 */ *gpbcon |= 1; *gpbdat &= ~1; /* 輸出低電平 */ *gpgcon |= (3<<8); /* GPG4用作LCD_PWREN */ /* 3.2 根據LCD手冊設置LCD控制器, 比如VCLK的頻率等 */ static volatile struct lcd_regs* lcd_regs; //定義 lcd_regs = ioremap(0x4D000000, sizeof(struct lcd_regs)); /*****LCD寄存器*****/ 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 lpcsel; }; /* bit[17:8]: VCLK = HCLK / [(CLKVAL+1) x 2], LCD手冊P14 * 10MHz(100ns) = 100MHz / [(CLKVAL+1) x 2] * CLKVAL = 4 * bit[6:5]: 0b11, TFT LCD * bit[4:1]: 0b1100, 16 bpp for TFT * bit[0] : 0 = Disable the video output and the LCD control signal. */ lcd_regs->lcdcon1 = (4<<8) | (3<<5) | (0x0c<<1); /* 垂直方向的時間參數 * bit[31:24]: VBPD, VSYNC之後再過多長時間才能發出第1行數據 * 4.3寸液晶屏手冊 * VBPD=1 tvb=2 * bit[23:14]: 多少行, 272, 所以LINEVAL=272-1=271 tvd=272 * bit[13:6] : VFPD, 發出最後一行數據之後,再過多長時間才發出VSYNC * 4.3寸液晶屏手冊 VFPD=2-1=1 tvf=2 * bit[5:0] : VSPW, VSYNC信號的脈衝寬度, VSPW=10-1=9 tvp=10 */ lcd_regs->lcdcon2 = (1<<24) | (271<<14) | (1<<6) | (9); /* 水平方向的時間參數 * bit[25:19]: HBPD, VSYNC之後再過多長時間才能發出第1行數據 * 4.3寸液晶屏手冊 * HBPD=1 thb=2 * bit[18:8]: 多少列,480, 所以HOZVAL=480-1=479 thd=480 * bit[7:0] : HFPD, 發出最後一行里最後一個象素數據之後,再過多長時間才發出HSYNC * 4.3寸液晶屏手冊 HFPD=2-1=1 thf=2 */ lcd_regs->lcdcon3 = (1<<19) | (479<<8) | (1); /* 水平方向的同步信號 * bit[7:0] : HSPW, HSYNC信號的脈衝寬度, LCD手冊T7=5, 所以HSPW=41-1=40 */ lcd_regs->lcdcon4 = 40; /* 信號的極性 * bit[11]: 1=565 format * bit[10]: 0 = The video data is fetched at VCLK falling edge * bit[9] : 1 = HSYNC信號要反轉,即低電平有效 * bit[8] : 1 = VSYNC信號要反轉,即低電平有效 * bit[6] : 0 = VDEN不用反轉 * bit[3] : 0 = PWREN輸出0 * bit[1] : 0 = BSWP * bit[0] : 1 = HWSWP 2440手冊P413 */ lcd_regs->lcdcon5 = (1<<11) | (0<<10) | (1<<9) | (1<<8) | (1<<0); /* 3.3 分配顯存(framebuffer), 並把地址告訴LCD控制器 */ //s3c_lcd->screen_base /* 顯存的虛擬地址 */ //s3c_lcd->fix.smem_start = xxx; /* 顯存的物理地址 */ s3c_lcd->screen_base = dma_alloc_writecombine(NULL, s3c_lcd->fix.smem_len, &s3c_lcd->fix.smem_start, GFP_KERNEL); /*幀緩衝開始地址1寄存器 * */ lcd_regs->lcdsaddr1 = (s3c_lcd->fix.smem_start >> 1) & ~(3<<30); //1~30,開始地址 lcd_regs->lcdsaddr2 = ((s3c_lcd->fix.smem_start + s3c_lcd->fix.smem_len) >> 1) & 0x1fffff; //從1開始,結束地址 lcd_regs->lcdsaddr3 = (480*16/16); /* 一行的長度(單位: 2位元組) */ /* 啟動LCD */ lcd_regs->lcdcon1 |= (1<<0); /* 使能LCD控制器 */ lcd_regs->lcdcon5 |= (1<<3); /* 使能LCD本身 */ *gpbdat |= 1; /* 輸出高電平, 使能背光 */ /* 4. 註冊 */ register_framebuffer(s3c_lcd); ------------------------------------------------------------------- lcd_exit: unregister_framebuffer(s3c_lcd); lcd_regs->lcdcon1 &= ~(1<<0); /* 關閉LCD本身 */ *gpbdat &= ~1; /* 關閉背光 */ dma_free_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(gpgcon); framebuffer_release(s3c_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在哪裡被設置?
答1. register_framebuffer
怎麼寫LCD驅動程式?
1. 分配一個fb_info結構體: framebuffer_alloc
2. 設置
3. 註冊: register_framebuffer
4. 硬體相關的操作
測試:
1. make menuconfig去掉原來的驅動程式
-> 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 lcd.ko
echo hello > /dev/tty1 // 可以在LCD上看見hello
cat lcd.ko > /dev/fb0 // 花屏
5. 修改 /etc/inittab
tty1::askfirst:-/bin/sh
用新內核重啟開發板
insmod cfbcopyarea.ko
insmod cfbfillrect.ko
insmod cfbimgblt.ko
insmod lcd.ko
insmod buttons.ko