zoukankan      html  css  js  c++  java
  • Linux-2.6_LCD驱动学习

     内核自带的驱动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驱动程序,使用分层分离所写,再来学习!

  • 相关阅读:
    刷脸背后:人脸检测人脸识别人脸检索_张重生资料整理
    webpack工具
    js精度缺失和最大安全整数
    在线文档预览(干货篇)
    讨论js比较两个数组(对象)是否相等的范围
    js不同数据类型中==与===的对比
    js中this的指向
    前后端数据类型
    js网页节点操作
    圆角渐变边框实现
  • 原文地址:https://www.cnblogs.com/jason-linux/p/10294650.html
Copyright © 2011-2022 走看看