《韦东山视频第二期》——LCD驱动 一、LCD驱动程序框架分析

一、LCD驱动程序框架分析


app: open("/dev/fb0", ...)     主设备号:29, 次设备号:0

————————————————————————————————————————————————————

kernel:(核心文件/drivers/video/fbmem.c)

                fb_open

                          int fbidx = iminor(inode);
                          struct fb_info *info;

                          info = registered_fb[fbidx];      //根据次设备号获得从底层注册的struct fb_info结构体中

                         file->private_data = info;
                         if (info->fbops->fb_open) {                         //调用底层struct fb_info结构体中的fb_open函数
                                 res = info->fbops->fb_open(info,1);
                                 if (res)
                                 module_put(info->fbops->owner);
                         }

———————————————————————————————————————————————————

app:    read()

_____________________________________________________________________________________

kernel:

                fb_read

                            struct inode *inode = file->f_path.dentry->d_inode;
                            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)          


怎么写LCD驱动程序?

1.分配一个fb_info结构体:framebuffer_alloc()

2.设置

3.注册:register_framebuffer

4.硬件相关的操作  

  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. struct lcd_regs {
  24.      unsigned long    lcdcon1;
  25.      unsigned long    lcdcon2;
  26.      unsigned long    lcdcon3;
  27.      unsigned long    lcdcon4;
  28.      unsigned long    lcdcon5;
  29.     unsigned long    lcdsaddr1;
  30.     unsigned long    lcdsaddr2;
  31.     unsigned long    lcdsaddr3;
  32.     unsigned long    redlut;
  33.     unsigned long    greenlut;
  34.     unsigned long    bluelut;
  35.     unsigned long    reserved[ 9];
  36.     unsigned long    dithmode;
  37.     unsigned long    tpal;
  38.     unsigned long    lcdintpnd;
  39.     unsigned long    lcdsrcpnd;
  40.     unsigned long    lcdintmsk;
  41.     unsigned long    lpcsel;
  42. };
  43. static struct fb_ops* s3c_lcdfb_ops = {
  44.     .owner          = THIS_MODULE,
  45. //  .fb_setcplreg = atmel_lcdfb_setolreg,
  46.     .fb_fillrect  = cfb_fillrect,
  47.     .fb_copyarea  = cfb_copyarea,
  48.     .fb_imageblit = cfb_imageblit,
  49. };
  50. static struct fb_info* s3c_lcd;
  51. static volatile unsigned long *gpbcon;
  52. static volatile unsigned long *gpbdat;
  53. static volatile unsigned long *gpccon;
  54. static volatile unsigned long *gpdcon;
  55. static volatile unsigned long *gpgcon;
  56. static volatile struct lcd_regs* lcd_regs;
  57. static u32 pseudo_palette[ 16];     //为了兼容,设置假的调色板
  58. /* from pxafb.c */
  59. static inline unsigned int chan_to_field(unsigned int chan, struct fb_bitfield *bf)
  60. {
  61.     chan &= 0xffff;
  62.     chan >>= 16 - bf->length;
  63.      return chan << bf->offset;
  64. }
  65. static int s3c_lcdfb_setcolreg(unsigned int regno, unsigned int red,
  66.                  unsigned int green, unsigned int blue,
  67.                  unsigned int transp, struct fb_info *info)
  68. {
  69.      unsigned int val;
  70.     
  71.      if (regno > 16)
  72.          return 1;
  73.      /* 用red,green,blue三原色构造出val */
  74.     val  = chan_to_field(red,    &info->var.red);
  75.     val |= chan_to_field(green, &info->var.green);
  76.     val |= chan_to_field(blue,    &info->var.blue);
  77.     
  78.      //((u32 *)(info->pseudo_palette))[regno] = val;
  79.     pseudo_palette[regno] = val;
  80.      return 0;
  81. }
  82. static int lcd_init(void)
  83. {
  84.      /* 1. 分配一个fb_info结构体 */
  85.     s3c_lcd = framebuffer_alloc( 0, NULL);
  86.      /* 2. 设置 */
  87.      /* 2.1 设置固定的参数 */
  88.      strcpy(s3c_lcd->fix.id, "mylcd");
  89.     s3c_lcd->fix.smem_len    = 320* 240* 32/ 8; /* MINI2440的LCD位宽是24,但是2440里会分配4字节即32位(浪费1字节) */
  90.     s3c_lcd->fix.type        = FB_TYPE_PACKED_PIXELS;
  91.     s3c_lcd->fix.visual      = FB_VISUAL_TRUECOLOR;      /* 真彩色 */
  92.     s3c_lcd->fix.line_length = 320 * 4;     /* 1行的字节数 */
  93.     
  94.      /* 2.2 设置可变的参数 */
  95.     s3c_lcd->var.xres          = 320;
  96.     s3c_lcd->var.yres          = 240;
  97.     s3c_lcd->var.xres_virtual  = 320;
  98.     s3c_lcd->var.yres_virtual  = 240;
  99.     s3c_lcd->var.bits_per_pixel = 32;     /* 每个像素所占的位数 */
  100.      /* RGB: 565 */
  101.     s3c_lcd->var.red.offset    = 16;
  102.     s3c_lcd->var.red.length    = 8;
  103.     s3c_lcd->var.green.offset  = 8;
  104.     s3c_lcd->var.green.length  = 8;
  105.     s3c_lcd->var.blue.offset   = 0;
  106.     s3c_lcd->var.blue.length   = 0;
  107.     s3c_lcd->var.activate      = FB_ACTIVATE_NOW;
  108.     
  109.     
  110.      /* 2.3 设置操作函数 */
  111.     s3c_lcd->fops   = &s3c_lcdfb_ops;
  112.         
  113.      /* 2.4 其他设置 */
  114.      //s3c_lcd->pseudo_palette =; //
  115.      //s3c_lcd->screen_base  = ;  /* 显存的虚拟地址 */
  116.     s3c_lcd->screen_size   = 320* 240* 32/ 8;
  117.      /* 3. 硬件相关的操作 */
  118.      /* 3.1 配置GPIO用于LCD */
  119.     gpbcon = ioremap( 0x56000010, 8);
  120.     gpbdat = gpbcon+ 1;
  121.     gpccon = ioremap( 0x56000020, 4);
  122.     gpdcon = ioremap( 0x56000030, 4);
  123.     gpgcon = ioremap( 0x56000060, 4);
  124.     *gpccon  = 0xaaaaaaaa;   /* GPIO管脚用于VD[7:0],LCDVF[2:0],VM,VFRAME,VLINE,VCLK,LEND */
  125.     *gpdcon  = 0xaaaaaaaa;   /* GPIO管脚用于VD[23:8] */
  126.     
  127. //    *gpbcon &= ~(3);  /* GPB0设置为输出引脚 */
  128. //    *gpbcon |= 1;
  129. //    *gpbdat &= ~1;     /* 输出低电平 */
  130.     *gpgcon |= ( 3<< 8); /* GPG4用作LCD_PWREN */
  131.      /* 3.2 根据LCD手册设置LCD控制器,比如VCLK的频率等 */
  132.     lcd_regs = ioremap( 0x4D000000, sizeof(struct lcd_regs));
  133.     
  134.      /*
  135.      * MINI2440 LCD 3.5英寸 ZQ3506_V0 SPEC.pdf 第11、12页
  136.      *
  137.      * LCD手册11,12页和2440手册"Figure 15-6. TFT LCD Timing Example"一对比就知道参数含义了
  138.      */
  139.      /* bit[17:8]: VCLK = HCLK / [(CLKVAL+1) x 2], LCD手册11 (Dclk=6.4MHz~11MHz)
  140.      *            7.1MHz = 100MHz / [(CLKVAL+1) x 2]
  141.      *            CLKVAL = 6
  142.      * bit[6:5]: 0b11, TFT LCD
  143.      * bit[4:1]: 0b1101, 24 bpp for TFT
  144.      * bit[0]  : 0 = Disable the video output and the LCD control signal.
  145.      */
  146.     lcd_regs->lcdcon1  = ( 6<< 8) | ( 3<< 5) | ( 0x0d<< 1);
  147.      /* 垂直方向的时间参数
  148.      * 根据数据手册
  149.      * bit[31:24]: VBPD, VSYNC之后再过多长时间才能发出第1行数据
  150.      *             LCD手册 tvb=18
  151.      *             VBPD=17
  152.      * bit[23:14]: 多少行, 240, 所以LINEVAL=240-1=239
  153.      * bit[13:6] : VFPD, 发出最后一行数据之后,再过多长时间才发出VSYNC
  154.      *             LCD手册tvf=4, 所以VFPD=4-1=3
  155.      * bit[5:0]  : VSPW, VSYNC信号的脉冲宽度, LCD手册tvp=1, 所以VSPW=1-1=0
  156.      */
  157.     
  158.     /* 使用这些数值, 图像有下移的现象, 应该是数据手册过时了
  159.      * 自己微调一下, 上下移动调VBPD和VFPD
  160.      * 保持(VBPD+VFPD)不变, 减小VBPD图像上移, 取VBPD=11, VFPD=9
  161.      * 多试几次, 我试了10多次
  162.      */
  163.   //lcd_regs->lcdcon2  = (17<<24) | (239<<14) | (3<<6) | (0<<0);
  164.     lcd_regs->lcdcon2  = ( 11<< 24) | ( 239<< 14) | ( 9<< 6) | ( 0<< 0);
  165.      /* 水平方向的时间参数
  166.      * bit[25:19]: HBPD, VSYNC之后再过多长时间才能发出第1行数据
  167.      *             LCD手册 thb=38
  168.      *             HBPD=37
  169.      * bit[18:8]: 多少列, 320, 所以HOZVAL=320-1=319
  170.      * bit[7:0] : HFPD, 发出最后一行里最后一个象素数据之后,再过多长时间才发出HSYNC
  171.      *             LCD手册thf>=2, th=408=thp+thb+320+thf, thf=49, HFPD=49-1=48
  172.      */
  173.     /* 使用这些数值, 图像有左移的现象, 应该是数据手册过时了
  174.      * 自己微调一下, 上下移动调HBPD和HFPD
  175.      * 保持(VBPD+VFPD)不变, 增加HBPD图像右移, 取HBPD=69, HFPD=16
  176.      * 多试几次, 我试了10多次
  177.      */
  178. //    lcd_regs->lcdcon3 = (37<<19) | (319<<8) | (48<<0);
  179.     lcd_regs->lcdcon3 = ( 69<< 19) | ( 319<< 8) | ( 16<< 0);
  180.      /* 水平方向的同步信号
  181.      * bit[7:0]    : HSPW, HSYNC信号的脉冲宽度, LCD手册Thp=1, 所以HSPW=1-1=0
  182.      */    
  183.     lcd_regs->lcdcon4 = 0;
  184.      /* 信号的极性
  185.      * bit[11]: 1=565 format, 对于24bpp这个不用设
  186.      * bit[10]: 0 = The video data is fetched at VCLK falling edge
  187.      * bit[9] : 1 = HSYNC信号要反转,即低电平有效
  188.      * bit[8] : 1 = VSYNC信号要反转,即低电平有效
  189.      * bit[6] : 0 = VDEN不用反转
  190.      * bit[3] : 0 = PWREN输出0
  191.      *
  192.      * BSWP = 0, HWSWP = 0, BPP24BL = 0 : 当bpp=24时,2440会给每一个象素分配32位即4字节,哪一个字节是不使用的? 看2440手册P412
  193.          * bit[12]: 0, LSB valid, 即最高字节不使用
  194.      * bit[1] : 0 = BSWP
  195.      * bit[0] : 0 = HWSWP
  196.      */
  197.     lcd_regs->lcdcon5 = ( 0<< 10) | ( 1<< 9) | ( 1<< 8) | ( 0<< 12) | ( 0<< 1) | ( 0<< 0);
  198.      /* 3.3 分配显存(framebuffer),并把地址告诉LCD控制器 */
  199.     s3c_lcd->screen_base = dma_alloc_writecombine( NULL, s3c_lcd->fix.smem_len, &s3c_lcd->fix.smem_start, GFP_KERNEL);
  200.     
  201.     lcd_regs->lcdsaddr1  = (s3c_lcd->fix.smem_start >> 1) & ~( 3<< 30);
  202.     lcd_regs->lcdsaddr2  = ((s3c_lcd->fix.smem_start + s3c_lcd->fix.smem_len) >> 1) & 0x1fffff;
  203.     lcd_regs->lcdsaddr3  = ( 320* 32/ 16);  /* 一行的长度(单位: 2字节) */    
  204.     
  205.      //s3c_lcd->fix.smem_start = xxx;  /* 显存的物理地址 */
  206.      /* 启动LCD */
  207.     lcd_regs->lcdcon1 |= ( 1<< 0); /* 使能LCD控制器 */
  208.     lcd_regs->lcdcon5 |= ( 1<< 3); /* 使能LCD本身: LCD_PWREN */
  209. //    *gpbdat |= 1;     /* MINI2440的背光电路也是通过LCD_PWREN来控制的, 不需要单独的背光引脚 */
  210.     
  211.      /* 4. 注册 */
  212.     register_framebuffer(s3c_lcd);
  213.      return 0;
  214. }
  215. static void lcd_exit()
  216. {
  217.     unregister_framebuffer(s3c_lcd);
  218.     lcd_regs->lcdcon1 &= ~( 1<< 0); /* 关闭LCD控制器 */
  219.     lcd_regs->lcdcon1 &= ~( 1<< 3); /* 关闭LCD本身 */
  220. //    *gpbdat &= ~1;     /* 关闭背光 */
  221.     dma_free_writecombine( NULL, s3c_lcd->fix.smem_len, s3c_lcd->screen_base, s3c_lcd->fix.smem_start);
  222.     iounmap(lcd_regs);
  223.     iounmap(gpbcon);
  224.     iounmap(gpccon);
  225.     iounmap(gpdcon);
  226.     iounmap(gpgcon);
  227.     framebuffer_release(s3c_lcd);
  228. }
  229. module_init(lcd_init);
  230. module_exit(lcd_exit);
  231. MODULE_LICENSE( "GPL");

测试:
1. make menuconfig去掉原来的驱动程序
-> Device Drivers
  -> Graphics support
<M> S3C2410 LCD framebuffer support

2. make uImage
                cp arch/arm/boot/uImage /home/null/work/yaffs/mini2440_rootfs/rootfs
   make modules  

3. 使用新的uImage启动开发板:

4. null@ubuntu:~/work/linux-kernel/linux-2.6.32.2/drivers/video$ cp cfb*.ko /home/null/work/yaffs/mini2440_rootfs/rootfs/wds_2

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

猜你喜欢

转载自blog.csdn.net/special00/article/details/80928856