LCD驅動程序編寫


學習目標:編寫LCD驅動程序,熟悉根據芯片手冊分析時序圖,配置寄存器,並測試LCD程序。

一、LCD驅動程序編寫

步驟:

1)分配fb_info結構體

2)設置fb_info結構體

  a. 固定參數

  b. 可變參數

  c. 操作函數

   --設置調色板

  d. fb_info的其它成員

   --設置顯存

3)硬件相關的操作

  --配置GPIO用於用於LCD

  --根據手冊設置LCD控制器寄存器

     --分配顯存,並把地址告訴LCD控制器

4)  注冊fb_info結構體

二、源碼:

  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 #include <asm/io.h>
 17 #include <asm/uaccess.h>
 18 #include <asm/div64.h>
 19 #include <asm/mach/map.h>
 20 #include <asm/arch/regs-lcd.h>
 21 #include <asm/arch/regs-gpio.h>
 22 #include <asm/arch/fb.h>
 23 
 24 //調色板
 25 static int s3c_lcdfb_setcolreg(unsigned int regno, unsigned int red,
 26                  unsigned int green, unsigned int blue,
 27                  unsigned int transp, struct fb_info *info);
 28 
 29 //lcd寄存器
 30 struct lcd_regs {
 31     unsigned long    lcdcon1;
 32     unsigned long    lcdcon2;
 33     unsigned long    lcdcon3;
 34     unsigned long    lcdcon4;
 35     unsigned long    lcdcon5;
 36     unsigned long    lcdsaddr1;
 37     unsigned long    lcdsaddr2;
 38     unsigned long    lcdsaddr3;
 39     unsigned long    redlut;
 40     unsigned long    greenlut;
 41     unsigned long    bluelut;
 42     unsigned long    reserved[9];
 43     unsigned long    dithmode;
 44     unsigned long    tpal;
 45     unsigned long    lcdintpnd;
 46     unsigned long    lcdsrcpnd;
 47     unsigned long    lcdintmsk;
 48     unsigned long    lpcsel;
 49 };
 50 
 51 static struct fb_ops s3c_lcdfb_ops = {
 52     .owner          = THIS_MODULE,
 53     .fb_setcolreg   = s3c_lcdfb_setcolreg, //設置調色板
 54     .fb_fillrect    = cfb_fillrect,
 55     .fb_copyarea    = cfb_copyarea,
 56     .fb_imageblit   = cfb_imageblit,
 57 };
 58 
 59 static struct fb_info *s3c_lcd;
 60 static volatile unsigned long *gpbcon;
 61 static volatile unsigned long *gpbdat;
 62 static volatile unsigned long *gpccon;
 63 static volatile unsigned long *gpdcon;
 64 static volatile unsigned long *gpgcon;
 65 static volatile struct lcd_regs* lcd_regs;
 66 static u32 pseudo_palette[16];
 67 
 68 /* from pxafb.c */
 69 static inline unsigned int chan_to_field(unsigned int chan, struct fb_bitfield *bf)
 70 {
 71     chan &= 0xffff;
 72     chan >>= 16 - bf->length;
 73     return chan << bf->offset;
 74 }
 75 
 76 //設置調色板,供內核使用
 77 static int s3c_lcdfb_setcolreg(unsigned int regno, unsigned int red,
 78                  unsigned int green, unsigned int blue,
 79                  unsigned int transp, struct fb_info *info)
 80 {
 81     unsigned int val;
 82     
 83     if (regno > 16)
 84         return 1;
 85 
 86     /* 用red,green,blue三原色構造出val,根據info結構體中設置的偏移,val最終是16位象素 */
 87     val  = chan_to_field(red,    &info->var.red);
 88     val |= chan_to_field(green, &info->var.green);
 89     val |= chan_to_field(blue,    &info->var.blue);
 90     
 91     //((u32 *)(info->pseudo_palette))[regno] = val;
 92     pseudo_palette[regno] = val;                     //val放到調色板數組中
 93     return 0;
 94 }
 95 
 96 static int lcd_init(void)
 97 {
 98     /* 1. 分配一個fb_info */
 99     s3c_lcd = framebuffer_alloc(0, NULL);
100 
101     /* 2. 設置 */
102     /* 2.1 設置固定的參數 */
103     strcpy(s3c_lcd->fix.id, "mylcd");
104     s3c_lcd->fix.smem_len = 240*320*16/8;                 //顯存的長度=分辨率*每象素字節數  每個像素用16位表示(5:6:5);
105     s3c_lcd->fix.type     = FB_TYPE_PACKED_PIXELS;     //默認  
106     s3c_lcd->fix.visual   = FB_VISUAL_TRUECOLOR; /* TFT 真彩色*/
107     s3c_lcd->fix.line_length = 240*2;  //每行的長度,以字節為單位 
108     
109     /* 2.2 設置可變的參數 */
110     s3c_lcd->var.xres           = 240;
111     s3c_lcd->var.yres           = 320;
112     s3c_lcd->var.xres_virtual   = 240;
113     s3c_lcd->var.yres_virtual   = 320;
114     s3c_lcd->var.bits_per_pixel = 16;  //每個象素使用多少位 16bit
115 
116     /* RGB:565 */
117     s3c_lcd->var.red.offset     = 11;
118     s3c_lcd->var.red.length     = 5;
119     
120     s3c_lcd->var.green.offset   = 5;
121     s3c_lcd->var.green.length   = 6;
122 
123     s3c_lcd->var.blue.offset    = 0;
124     s3c_lcd->var.blue.length    = 5;
125 
126     s3c_lcd->var.activate       = FB_ACTIVATE_NOW;//使設置的值立即生效
127     
128     
129     /* 2.3 設置操作函數 */
130     s3c_lcd->fbops              = &s3c_lcdfb_ops;
131     
132     /* 2.4 其他的設置 */
133     s3c_lcd->pseudo_palette = pseudo_palette;//存放調色板所調顏色的數組
134     //s3c_lcd->screen_base  = ;  /* 顯存的虛擬地址 */ 
135     s3c_lcd->screen_size   = 240*324*16/8;//顯存的大小
136 
137     /* 3. 硬件相關的操作 */
138     /* 3.1 配置GPIO用於LCD */
139     gpbcon = ioremap(0x56000010, 8);
140     gpbdat = gpbcon+1;
141     gpccon = ioremap(0x56000020, 4);
142     gpdcon = ioremap(0x56000030, 4);
143     gpgcon = ioremap(0x56000060, 4);
144 
145     *gpccon  = 0xaaaaaaaa;   /* GPIO管腳用於VD[7:0],LCDVF[2:0],VM,VFRAME,VLINE,VCLK,LEND */
146     *gpdcon  = 0xaaaaaaaa;   /* GPIO管腳用於VD[23:8] */
147     
148     *gpbcon &= ~(3);  /* GPB0設置為輸出引腳 */
149     *gpbcon |= 1;
150     *gpbdat &= ~1;     /* 輸出低電平,關閉背光* */
151 
152     *gpgcon |= (3<<8); /* GPG4用作LCD_PWREN信號*/
153     
154     /* 3.2 根據LCD手冊設置LCD控制器, 比如VCLK的頻率等 */
155     lcd_regs = ioremap(0x4D000000, sizeof(struct lcd_regs));
156 
157     /* bit[17:8]: VCLK = HCLK / [(CLKVAL+1) x 2], LCD手冊P14
158      *            10MHz(100ns) = 100MHz / [(CLKVAL+1) x 2]
159      *            CLKVAL = 4
160      * bit[6:5]: 0b11, TFT LCD
161      * bit[4:1]: 0b1100, 16 bpp for TFT
162      * bit[0]  : 0 = Disable the video output and the LCD control signal.
163      */
164     lcd_regs->lcdcon1  = (4<<8) | (3<<5) | (0x0c<<1);
165 
166 #if 1
167     /* 垂直方向的時間參數
168      * bit[31:24]: VBPD, VSYNC之后再過多長時間才能發出第1行數據
169      *             LCD手冊 T0-T2-T1=4
170      *             VBPD=3
171      * bit[23:14]: 多少行, 320, 所以LINEVAL=320-1=319
172      * bit[13:6] : VFPD, 發出最后一行數據之后,再過多長時間才發出VSYNC
173      *             LCD手冊T2-T5=322-320=2, 所以VFPD=2-1=1
174      * bit[5:0]  : VSPW, VSYNC信號的脈沖寬度, LCD手冊T1=1, 所以VSPW=1-1=0
175      */
176     lcd_regs->lcdcon2  = (3<<24) | (319<<14) | (1<<6) | (0<<0);
177 
178 
179     /* 水平方向的時間參數
180      * bit[25:19]: HBPD, VSYNC之后再過多長時間才能發出第1行數據
181      *             LCD手冊 T6-T7-T8=17
182      *             HBPD=16
183      * bit[18:8]: 多少列, 240, 所以HOZVAL=240-1=239
184      * bit[7:0] : HFPD, 發出最后一行里最后一個象素數據之后,再過多長時間才發出HSYNC
185      *             LCD手冊T8-T11=251-240=11, 所以HFPD=11-1=10
186      */
187     lcd_regs->lcdcon3 = (16<<19) | (239<<8) | (10<<0);
188 
189     /* 水平方向的同步信號
190      * bit[7:0]    : HSPW, HSYNC信號的脈沖寬度, LCD手冊T7=5, 所以HSPW=5-1=4
191      */    
192     lcd_regs->lcdcon4 = 4;
193 //lcd寄存器配置的另一種寫法
194 #else 
195 lcd_regs->lcdcon2 =    S3C2410_LCDCON2_VBPD(5) | \
196         S3C2410_LCDCON2_LINEVAL(319) | \
197         S3C2410_LCDCON2_VFPD(3) | \
198         S3C2410_LCDCON2_VSPW(1);
199 
200 lcd_regs->lcdcon3 =    S3C2410_LCDCON3_HBPD(10) | \
201         S3C2410_LCDCON3_HOZVAL(239) | \
202         S3C2410_LCDCON3_HFPD(1);
203 
204 lcd_regs->lcdcon4 =    S3C2410_LCDCON4_MVAL(13) | \
205         S3C2410_LCDCON4_HSPW(0);
206 
207 #endif
208     /* 信號的極性 
209      * bit[11]: 1=565 format
210      * bit[10]: 0 = The video data is fetched at VCLK falling edge
211      * bit[9] : 1 = HSYNC信號要反轉,即低電平有效 
212      * bit[8] : 1 = VSYNC信號要反轉,即低電平有效 
213      * bit[6] : 0 = VDEN不用反轉
214      * bit[3] : 0 = PWREN輸出0
215      * bit[1] : 0 = BSWP
216      * bit[0] : 1 = HWSWP 2440手冊P413
217      */
218     lcd_regs->lcdcon5 = (1<<11) | (0<<10) | (1<<9) | (1<<8) | (1<<0);
219     
220     /* 3.3 分配顯存(framebuffer), 並把地址告訴LCD控制器 */
221     s3c_lcd->screen_base = dma_alloc_writecombine(NULL, s3c_lcd->fix.smem_len, &s3c_lcd->fix.smem_start, GFP_KERNEL);
222     
223     lcd_regs->lcdsaddr1  = (s3c_lcd->fix.smem_start >> 1) & ~(3<<30);  //對應內存起始地址的 A[30:1] ,則起始地址右移1位,&0x3fffffff 共30位
224     lcd_regs->lcdsaddr2  = ((s3c_lcd->fix.smem_start + s3c_lcd->fix.smem_len) >> 1) & 0x1fffff;
225     lcd_regs->lcdsaddr3  = (240*16/16);  /* 一行的長度(單位: 2字節) */    
226     
227     //s3c_lcd->fix.smem_start = xxx;  /* 顯存的物理地址 */
228     /* 啟動LCD */
229     lcd_regs->lcdcon1 |= (1<<0); /* 使能LCD控制器 */
230     lcd_regs->lcdcon5 |= (1<<3); /* 使能LCD本身 */
231     *gpbdat |= 1;     /* 輸出高電平, 使能背光 */        
232 
233     /* 4. 注冊 */
234     register_framebuffer(s3c_lcd);    
235     return 0;
236 }
237 static void lcd_exit(void)
238 {
239     unregister_framebuffer(s3c_lcd);
240     lcd_regs->lcdcon1 &= ~(1<<0); /* 關閉LCD本身 */
241     *gpbdat &= ~1;     /* 關閉背光 */
242     dma_free_writecombine(NULL, s3c_lcd->fix.smem_len, s3c_lcd->screen_base, s3c_lcd->fix.smem_start);//3為虛擬地址
243     iounmap(lcd_regs);
244     iounmap(gpbcon);
245     iounmap(gpccon);
246     iounmap(gpdcon);
247     iounmap(gpgcon);
248     framebuffer_release(s3c_lcd);
249 }
250 module_init(lcd_init);
251 module_exit(lcd_exit);
252 MODULE_LICENSE("GPL");

LCD寄存器配置分析:

1、lcdcon1 寄存器配置

1  /* bit[17:8]: VCLK = HCLK / [(CLKVAL+1) x 2], LCD手冊P14
2   *            10MHz(100ns) = 100MHz / [(CLKVAL+1) x 2]
3   *            CLKVAL = 4
4   * bit[6:5]: 0b11, TFT LCD
5   * bit[4:1]: 0b1100, 16 bpp for TFT
6   * bit[0]  : 0 = Disable the video output and the LCD control signal.
7   */
8  lcd_regs->lcdcon1  = (4<<8) | (3<<5) | (0x0c<<1);

查看2440說明書,找到lcdcon1 寄存器,確定每一位的取值:

1)bit[17:8]  VCLK = HCLK / [(CLKVAL+1) x 2]計算CLKVAL的值,首先使用# dmesg命令查看內核打印信息,獲取HCLK為100MHz。

查看液晶屏的芯片手冊Clock cycle time 可知:VCLK = 100ns ==> 10MHz,從而可知 10=100/[(CLKVAL+1) x 2]==> CLKVAL=4   bit[17:8]就是4;

2)bit[6:5]: 0b11, TFT LCD;

3)bit[0]  : 0  LCD信號輸出使能位禁止。

2、lcdcon2 

 1    /* 垂直方向的時間參數
 2       * bit[31:24]: VBPD, VSYNC之后再過多長時間才能發出第1行數據
 3       *             LCD手冊 T0-T2-T1=4
 4       *             VBPD=3
 5       * bit[23:14]: 多少行, 320, 所以LINEVAL=320-1=319
 6       * bit[13:6] : VFPD, 發出最后一行數據之后,再過多長時間才發出VSYNC
 7       *             LCD手冊T2-T5=322-320=2, 所以VFPD=2-1=1
 8       * bit[5:0]  : VSPW, VSYNC信號的脈沖寬度, LCD手冊T1=1, 所以VSPW=1-1=0
 9 */
10      lcd_regs->lcdcon2  = (3<<24) | (319<<14) | (1<<6) | (0<<0);

lcdcon2 寄存器,確定每一位的取值:

1) * bit[31:24]: 垂直方向的時間參數 VBPD, 即VSYNC之后再過多長時間才能發出第1行數據。

查看2440 LCD控制器的手冊:

 根據LCD的芯片手冊:
 時間表:

由上圖可知:
VSPW+1=T1 ;  VBPD+1=T0-T2-T1;  則VSPW=0,VBPD=3;
2) bit[23:14]: 多少行, 320, 所以LINEVAL=320-1=319
3) bit[13:6] : VFPD, 發出最后一行數據之后,再過多長時間才發出VSYNC
                    VFPD+1=T2-T5=322-320, VFPD=1
4) bit[5:0]  : VSPW, VSYNC信號的脈沖寬度, VSPW=0。
 
3、lcdcon3
 /* 水平方向的時間參數
寄存器lcdcon3:
時序圖:
 
LCD芯片手冊時序圖:
 
1) bit[25:19]: HBPD, VSYNC之后再過多長時間才能發出第1行數據
  HSPW+1=T7;則HSPW=4;
  HBPD+1= T6-T7-T8=17,HBPD=16;
2)  bit[18:8]: 多少列, 240, 所以HOZVAL=240-1=239
3)  bit[7:0] : HFPD, 發出最后一行里最后一個象素數據之后,再過多長時間才發出HSYNC
  HFPD+1=T8-T11=251-240=11, 所以HFPD=10
4、lcdcon4
寄存器:
  /* 水平方向的同步信號
  * bit[7:0] : HSPW, HSYNC信號的脈沖寬度,HSPW+1=T7,HSPW=4
  */ 

 三、編譯測試

 第一種方法:

1) make menuconfig去掉原來的驅動程序

  -->Device Drivers

       -->Graphics support

         <M>S3C2410 LCD framebuffer support

 2) 編譯內核和模塊 

    # make uImage

    # make modules //把用到的程序編譯成模塊運行

3)編譯lcd驅動為模塊,將生成的lcd.ko和driver/video目錄下用到的cfbcopyarea.ko、cfbfillrect.ko、 cfbimgblt.ko (lcd驅動程序中的fb_ops會用到)拷貝到/work/nfs_root/first_fs/lcd_test目錄下。

4) 燒寫uImage並啟動開發板,並加載模塊:

# nfs 30000000 10.70.12.103:/work/nfs_root/uImage_nolcd  

# bootm 30000000
掛載:
(# ifconfig eth0 10.70.12.168 配置網卡eth0的ip)
# mount -t nfs -o nolock,vers=2 10.70.12.103:/work/nfs_root/ /mnt
# cd /mnt/first_fs/lcd_test/
加載驅動: 

# insmod cfbcopyarea.ko 

# insmod cfbfillrect.ko

# insmod cfbimgblt.ko

# insmod lcd.ko

使用# ls /dev/fb* ,可以看到設備節點:/dev/fb0.

5) echo hello > /dev/tty1  // 可以在LCD上看見hello

    cat lcd.ko > /dev/fb0   // 花屏 

第二種測試方法:

1)修改/etc/inittab (添加:tty1::askfirst:-/bin/sh) 輸出到tty1(對應為顯示屏)//將/bin/sh信息輸出到tty1中;

類似於:s3c2410_serial0::askfirst:-/bin/sh 輸出到串口。

用新內核重啟開發板

insmod cfbcopyarea.ko

insmod cfbfillrect.ko

insmod cfbimgblt.ko

insmod lcd.ko

insmod buttons.ko

可以看到Please。。。。。。。。。等一行字符

接着,將以前的USB鍵盤驅動,加載:# insmod ../usb_keyboard.ko

 插上鍵盤后,可以在LCD顯示終端控制信息:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM