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显示终端控制信息:

转载于:https://www.cnblogs.com/lxl-lennie/p/10250623.html

  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值