LINUX下的LCD子系统分析

转载地址:http://blog.csdn.net/tang_jin_chan/article/details/11727103

LINUX下的帧缓冲是LINUX视频系统的核心,其重大意义在于使得开发人员可以与平台无关的方式编写应用程序和较高内核层的程序.从而使得内核的帧缓冲接口允许应用程序与底层图形硬件的变化无关.也就是说,只要应用程序和显示器驱动程序遵循缓冲接口,应用程序可以不用改变就可以在不同类型的视频硬件上运行.其框架图如下:

   

 

    下面以MINI2440为实例进行分析.

 

    1.平台总线、平台设备、平台驱动:

        LINUX中用软件总线来组织设备与驱动.每一个设备或驱动隶属某一条总线.总线是设备找到其相应的驱动的一个桥梁.只是每一条总线都挂了多个设备多个驱动,某设备找到其相应的驱动的依据条件不同,比如平台总线有平台总线的匹配依据;IIC总线有IIC总线的匹配依据;USB总线有USB总线的匹配依据等.见博文中IIC子系统分析一文.

        1-1.平台设备:

        MINI2440的LCD向内核注册平台设备代码如下:

        arch/arm/mach-s3c2440/mach-mini2440.c:

  1. /*  
  2.  * mini2440_features string  
  3.  *  
  4.  * t = Touchscreen present  
  5.  * b = backlight control  
  6.  * c = camera [TODO]  
  7.  * 0-9 LCD configuration  
  8.  *  
  9.  */  
  10. static char mini2440_features_str[12] __initdata = "0tb";  

    此处把LCD的配置信息存放在段mini2440_features_str段数组里面,并初始化为"0tb".

  1. static void __init mini2440_init(void)  
  2. {  
  3.     ... ...;  
  4.     mini2440_parse_features(&features, mini2440_features_str);  
  5.     ... ...;  
  6.     if (features.count) /* the optional features */  
  7.         platform_add_devices(features.optional, features.count);  
  8. }  

    mini2440_init()函数会被系统启动过去调用,和LCD相关的函数有两个:mini2440_parse_features()和platform_add_devices().

  1. static void mini2440_parse_features(struct mini2440_features_t * features,const char * features_str )  
  2. {  
  3.     const char * fp = features_str;  
  4.   
  5.     features->count = 0;  
  6.     features->done = 0;  
  7.     features->lcd_index = -1;  
  8.   
  9.     while (*fp) {  
  10.         char f = *fp++;  
  11.   
  12.         switch (f) {  
  13.         case '0'...'9': /* tft screen */  
  14.             if (features->done & FEATURE_SCREEN) {  
  15.                 printk(KERN_INFO "MINI2440: '%c' ignored, "  
  16.                     "screen type already set\n", f);  
  17.             } else {  
  18.                 int li = f - '0';  
  19.                 if (li >= ARRAY_SIZE(mini2440_lcd_cfg))  
  20.                     printk(KERN_INFO "MINI2440: "  
  21.                         "'%c' out of range LCD mode\n", f);  
  22.                 else {  
  23.                     features->optional[features->count++] =  
  24.                             &s3c_device_lcd;  
  25.                     features->lcd_index = li;  
  26.                 }  
  27.             }  
  28.             features->done |= FEATURE_SCREEN;  
  29.             break;  
  30.         ... ...;  
  31.     }  
  32. }  

    看参数features,如下:

  1. struct mini2440_features_t {  
  2.     int count;  
  3.     int done;  
  4.     int lcd_index;  
  5.     struct platform_device *optional[8];  
  6. };  

    里面的optional域就是平台设备.上述函数mini2440_parse_features()把LCD平台相关信息s3c_device_lcd保存在此features->optional域:

  1. static struct resource s3c_lcd_resource[] = {  
  2.     [0] = {  
  3.         .start = S3C24XX_PA_LCD,  
  4.         .end   = S3C24XX_PA_LCD + S3C24XX_SZ_LCD - 1,  
  5.         .flags = IORESOURCE_MEM,  
  6.     },  
  7.     [1] = {  
  8.         .start = IRQ_LCD,  
  9.         .end   = IRQ_LCD,  
  10.         .flags = IORESOURCE_IRQ,  
  11.     }  
  12.   
  13. };  
  14.   
  15. static u64 s3c_device_lcd_dmamask = 0xffffffffUL;  
  16.   
  17. struct platform_device s3c_device_lcd = {  
  18.     .name         = "s3c2410-lcd",  
  19.     .id       = -1,  
  20.     .num_resources    = ARRAY_SIZE(s3c_lcd_resource),  
  21.     .resource     = s3c_lcd_resource,  
  22.     .dev              = {  
  23.         .dma_mask       = &s3c_device_lcd_dmamask,  
  24.         .coherent_dma_mask  = 0xffffffffUL  
  25.     }  
  26. };  

    至此,把mini2440平台相关的LCD信息打包进feature的optional域,并通过函数platform_add_devices()注册进内核的平台设备.见上述函数mini2440_init():

  1. if (features.count) /* the optional features */  
  2.     platform_add_devices(features.optional, features.count);  

    因此,就这样,MINI2440的LCD的作为一个平台设备注册进内核里面去了.

 

    1-2.平台驱动:

        MINI2440关于LCD的平台驱动的源码位于drivers/video/s3c2410fb.c:

  1. module_init(s3c2410fb_init);  

        展开函数s3c2410fb_init:

  1. int __init s3c2410fb_init(void)  
  2. {  
  3.     int ret = platform_driver_register(&s3c2410fb_driver);  
  4.   
  5.     if (ret == 0)  
  6.         ret = platform_driver_register(&s3c2412fb_driver);  
  7.   
  8.     return ret;  
  9. }  

        其中平台驱动s3c2410fb_driver定义如下:

  1. static struct platform_driver s3c2410fb_driver = {  
  2.     .probe      = s3c2410fb_probe,  
  3.     .remove     = s3c2410fb_remove,  
  4.     .suspend    = s3c2410fb_suspend,  
  5.     .resume     = s3c2410fb_resume,  
  6.     .driver     = {  
  7.         .name   = "s3c2410-lcd",  
  8.         .owner  = THIS_MODULE,  
  9.     },  
  10. };  

        通过函数platform_driver_register()把这个平台驱动注册进内核,它会detect到其所支持的设备.其detect的依据是驱动名和设备名是否一致(具体过程可参看博文的IIC子系统分析,不再累赘):

        平台驱动名:

  1. .driver     = {  
  2.     .name   = "s3c2410-lcd",  

        平台设备名:

  1. struct platform_device s3c_device_lcd = {  
  2.     .name         = "s3c2410-lcd",  
  3.     .id       = -1,  


    它们的match的成功,将会引发平台驱动probe函数的执行,即s3c2410fb_probe()函数被调用:

  1. static struct platform_driver s3c2410fb_driver = {  
  2.     .probe      = s3c2410fb_probe,  
  3.     .remove     = s3c2410fb_remove,  
  4.     .suspend    = s3c2410fb_suspend,  
  5.     .resume     = s3c2410fb_resume,  
  6.     .driver     = {  
  7.         .name   = "s3c2410-lcd",  
  8.         .owner  = THIS_MODULE,  
  9.     },  
  10. };  

    展开函数s3c2412fb_probe():

  1. static int __init s3c2412fb_probe(struct platform_device *pdev)  
  2. {  
  3.     return s3c24xxfb_probe(pdev, DRV_S3C2412);  
  4. }  

    这里的参数pdev即为:

  1. struct platform_device s3c_device_lcd = {  
  2.     .name         = "s3c2410-lcd",  
  3.     .id       = -1,  
  4.     .num_resources    = ARRAY_SIZE(s3c_lcd_resource),  
  5.     .resource     = s3c_lcd_resource,  
  6.     .dev              = {  
  7.         .dma_mask       = &s3c_device_lcd_dmamask,  
  8.         .coherent_dma_mask  = 0xffffffffUL  
  9.     }  
  10. };  

    展开函数s3c24xxfb_probe():

  1. static int __init s3c24xxfb_probe(struct platform_device *pdev,  
  2.                   enum s3c_drv_type drv_type)  
  3. {  
  4.     struct s3c2410fb_info *info;  
  5.     struct s3c2410fb_display *display;  
  6.     struct fb_info *fbinfo;  
  7.     struct s3c2410fb_mach_info *mach_info;  
  8.     struct resource *res;  
  9.     int ret;  
  10.     int irq;  
  11.     int i;  
  12.     int size;  
  13.     u32 lcdcon1;  
  14.   
  15.     mach_info = pdev->dev.platform_data;  
  16.     if (mach_info == NULL) {  
  17.         dev_err(&pdev->dev,  
  18.             "no platform data for lcd, cannot attach\n");  
  19.         return -EINVAL;  
  20.     }  
  21.   
  22.     if (mach_info->default_display >= mach_info->num_displays) {  
  23.         dev_err(&pdev->dev, "default is %d but only %d displays\n",  
  24.             mach_info->default_display, mach_info->num_displays);  
  25.         return -EINVAL;  
  26.     }  
  27.   
  28.     display = mach_info->displays + mach_info->default_display;  
  29.   
  30.     irq = platform_get_irq(pdev, 0);  
  31.     if (irq < 0) {  
  32.         dev_err(&pdev->dev, "no irq for device\n");  
  33.         return -ENOENT;  
  34.     }  
  35.   
  36.     fbinfo = framebuffer_alloc(sizeof(struct s3c2410fb_info), &pdev->dev);  
  37.     if (!fbinfo)  
  38.         return -ENOMEM;  
  39.   
  40.     platform_set_drvdata(pdev, fbinfo);  
  41.   
  42.     info = fbinfo->par;  
  43.     info->dev = &pdev->dev;  
  44.     info->drv_type = drv_type;  
  45.   
  46.     res = platform_get_resource(pdev, IORESOURCE_MEM, 0);  
  47.     if (res == NULL) {  
  48.         dev_err(&pdev->dev, "failed to get memory registers\n");  
  49.         ret = -ENXIO;  
  50.         goto dealloc_fb;  
  51.     }  
  52.   
  53.     size = (res->end - res->start) + 1;  
  54.     info->mem = request_mem_region(res->start, size, pdev->name);  
  55.     if (info->mem == NULL) {  
  56.         dev_err(&pdev->dev, "failed to get memory region\n");  
  57.         ret = -ENOENT;  
  58.         goto dealloc_fb;  
  59.     }  
  60.   
  61.     info->io = ioremap(res->start, size);  
  62.     if (info->io == NULL) {  
  63.         dev_err(&pdev->dev, "ioremap() of registers failed\n");  
  64.         ret = -ENXIO;  
  65.         goto release_mem;  
  66.     }  
  67.   
  68.     info->irq_base = info->io + ((drv_type == DRV_S3C2412) ? S3C2412_LCDINTBASE : S3C2410_LCDINTBASE);  
  69.   
  70.     dprintk("devinit\n");  
  71.   
  72.     strcpy(fbinfo->fix.id, driver_name);  
  73.   
  74.     /* Stop the video */  
  75.     lcdcon1 = readl(info->io + S3C2410_LCDCON1);  
  76.     writel(lcdcon1 & ~S3C2410_LCDCON1_ENVID, info->io + S3C2410_LCDCON1);  
  77.   
  78.     fbinfo->fix.type     = FB_TYPE_PACKED_PIXELS;  
  79.     fbinfo->fix.type_aux     = 0;  
  80.     fbinfo->fix.xpanstep     = 0;  
  81.     fbinfo->fix.ypanstep     = 0;  
  82.     fbinfo->fix.ywrapstep        = 0;  
  83.     fbinfo->fix.accel        = FB_ACCEL_NONE;  
  84.   
  85.     fbinfo->var.nonstd       = 0;  
  86.     fbinfo->var.activate     = FB_ACTIVATE_NOW;  
  87.     fbinfo->var.accel_flags     = 0;  
  88.     fbinfo->var.vmode        = FB_VMODE_NONINTERLACED;  
  89.   
  90.     fbinfo->fbops            = &s3c2410fb_ops;  
  91.     fbinfo->flags            = FBINFO_FLAG_DEFAULT;  
  92.     fbinfo->pseudo_palette      = &info->pseudo_pal;  
  93.   
  94.     for (i = 0; i < 256; i++)  
  95.         info->palette_buffer[i] = PALETTE_BUFF_CLEAR;  
  96.   
  97.     ret = request_irq(irq, s3c2410fb_irq, IRQF_DISABLED, pdev->name, info);  
  98.     if (ret) {  
  99.         dev_err(&pdev->dev, "cannot get irq %d - err %d\n", irq, ret);  
  100.         ret = -EBUSY;  
  101.         goto release_regs;  
  102.     }  
  103.   
  104.     info->clk = clk_get(NULL, "lcd");  
  105.     if (!info->clk || IS_ERR(info->clk)) {  
  106.         printk(KERN_ERR "failed to get lcd clock source\n");  
  107.         ret = -ENOENT;  
  108.         goto release_irq;  
  109.     }  
  110.   
  111.     clk_enable(info->clk);  
  112.     dprintk("got and enabled clock\n");  
  113.   
  114.     msleep(1);  
  115.   
  116.     info->clk_rate = clk_get_rate(info->clk);  
  117.   
  118.     /* find maximum required memory size for display */  
  119.     for (i = 0; i < mach_info->num_displays; i++) {  
  120.         unsigned long smem_len = mach_info->displays[i].xres;  
  121.   
  122.         smem_len *= mach_info->displays[i].yres;  
  123.         smem_len *= mach_info->displays[i].bpp;  
  124.         smem_len >>= 3;  
  125.         if (fbinfo->fix.smem_len < smem_len)  
  126.             fbinfo->fix.smem_len = smem_len;  
  127.     }  
  128.   
  129.     /* Initialize video memory */  
  130.     ret = s3c2410fb_map_video_memory(fbinfo);  
  131.     if (ret) {  
  132.         printk(KERN_ERR "Failed to allocate video RAM: %d\n", ret);  
  133.         ret = -ENOMEM;  
  134.         goto release_clock;  
  135.     }  
  136.   
  137.     dprintk("got video memory\n");  
  138.   
  139.     fbinfo->var.xres = display->xres;  
  140.     fbinfo->var.yres = display->yres;  
  141.     fbinfo->var.bits_per_pixel = display->bpp;  
  142.   
  143.     s3c2410fb_init_registers(fbinfo);  
  144.   
  145.     s3c2410fb_check_var(&fbinfo->var, fbinfo);  
  146.   
  147.     ret = s3c2410fb_cpufreq_register(info);  
  148.     if (ret < 0) {  
  149.         dev_err(&pdev->dev, "Failed to register cpufreq\n");  
  150.         goto free_video_memory;  
  151.     }  
  152.   
  153.     ret = register_framebuffer(fbinfo);  
  154.     if (ret < 0) {  
  155.         printk(KERN_ERR "Failed to register framebuffer device: %d\n",  
  156.             ret);  
  157.         goto free_cpufreq;  
  158.     }  
  159.   
  160.     /* create device files */  
  161.     ret = device_create_file(&pdev->dev, &dev_attr_debug);  
  162.     if (ret) {  
  163.         printk(KERN_ERR "failed to add debug attribute\n");  
  164.     }  
  165.   
  166.     printk(KERN_INFO "fb%d: %s frame buffer device\n",  
  167.         fbinfo->node, fbinfo->fix.id);  
  168.   
  169.     return 0;  
  170.   
  171.  free_cpufreq:  
  172.     s3c2410fb_cpufreq_deregister(info);  
  173. free_video_memory:  
  174.     s3c2410fb_unmap_video_memory(fbinfo);  
  175. release_clock:  
  176.     clk_disable(info->clk);  
  177.     clk_put(info->clk);  
  178. release_irq:  
  179.     free_irq(irq, info);  
  180. release_regs:  
  181.     iounmap(info->io);  
  182. release_mem:  
  183.     release_resource(info->mem);  
  184.     kfree(info->mem);  
  185. dealloc_fb:  
  186.     platform_set_drvdata(pdev, NULL);  
  187.     framebuffer_release(fbinfo);  
  188.     return ret;  
  189. }  

    对于LINUX LCD子系统,一个核心的数据结构就是struct fb_info.是和LINUX LCD核心层打交道的关键数据.也就是说,我们只需要把我们平台相关的LCD信息及操作方法打包成struct fb_info去和LINUX LCD核心层打交互即可.下面逐步分析这个结构体的相关项的重要意义.

 

    2.struct fb_info结构体:

  1. struct fb_info {  
  2.     int node;  
  3.     int flags;  
  4.     struct mutex lock;      /* Lock for open/release/ioctl funcs */  
  5.     struct mutex mm_lock;       /* Lock for fb_mmap and smem_* fields */  
  6.     struct fb_var_screeninfo var;   /* Current var */  
  7.     struct fb_fix_screeninfo fix;   /* Current fix */  
  8.     struct fb_monspecs monspecs;    /* Current Monitor specs */  
  9.     struct work_struct queue;   /* Framebuffer event queue */  
  10.     struct fb_pixmap pixmap;    /* Image hardware mapper */  
  11.     struct fb_pixmap sprite;    /* Cursor hardware mapper */  
  12.     struct fb_cmap cmap;        /* Current cmap */  
  13.     struct list_head modelist;      /* mode list */  
  14.     struct fb_videomode *mode;  /* current mode */  
  15.   
  16. #ifdef CONFIG_FB_BACKLIGHT  
  17.     /* assigned backlight device */  
  18.     /* set before framebuffer registration,   
  19.        remove after unregister */  
  20.     struct backlight_device *bl_dev;  
  21.   
  22.     /* Backlight level curve */  
  23.     struct mutex bl_curve_mutex;      
  24.     u8 bl_curve[FB_BACKLIGHT_LEVELS];  
  25. #endif  
  26. #ifdef CONFIG_FB_DEFERRED_IO  
  27.     struct delayed_work deferred_work;  
  28.     struct fb_deferred_io *fbdefio;  
  29. #endif  
  30.   
  31.     struct fb_ops *fbops;  
  32.     struct device *device;      /* This is the parent */  
  33.     struct device *dev;     /* This is this fb device */  
  34.     int class_flag;                    /* private sysfs flags */  
  35. #ifdef CONFIG_FB_TILEBLITTING  
  36.     struct fb_tile_ops *tileops;    /* Tile Blitting */  
  37. #endif  
  38.     char __iomem *screen_base;  /* Virtual address */  
  39.     unsigned long screen_size;  /* Amount of ioremapped VRAM or 0 */   
  40.     void *pseudo_palette;       /* Fake palette of 16 colors */   
  41. #define FBINFO_STATE_RUNNING    0  
  42. #define FBINFO_STATE_SUSPENDED  1  
  43.     u32 state;          /* Hardware state i.e suspend */  
  44.     void *fbcon_par;                /* fbcon use-only private area */  
  45.     /* From here on everything is device dependent */  
  46.     void *par;  
  47.     /* we need the PCI or similiar aperture base/size not  
  48.        smem_start/size as smem_start may just be an object  
  49.        allocated inside the aperture so may not actually overlap */  
  50.     resource_size_t aperture_base;  
  51.     resource_size_t aperture_size;  
  52. };  

    我们知道,struct fb_info是LINUX子系统的关键数据结构,是和LCD核心层交互的核心数据.特定平台需要封装特定平台的fb_info.因此,在函数s3c24xxfb_probe()里面:1.先分配这样的一个数据结构体;2.然后再根据我们的具体平台初始化这个结构体;3.最后通过函数register_framebuffer()注册进LINUX的LCD子系统.下面根据这三步来分析:

    2-1.分配fb_info结构体:

  1. fbinfo = framebuffer_alloc(sizeof(struct s3c2410fb_info), &pdev->dev);  

    其中第一个参数是和具体平台相关的:s3c2410fb_info;第二个参数pdev即为LCD的平台设备:s3c_device_lcd.

 

        2-1-1.struct platform_device s3c_device_lcd:

        这个结构体记录了S3C平台的LCD设备资源信息,比如说LCD控制器寄存器的物理地址、内存位置和中断号.

  1. struct platform_device s3c_device_lcd = {  
  2.     .name         = "s3c2410-lcd",  
  3.     .id       = -1,  
  4.     .num_resources    = ARRAY_SIZE(s3c_lcd_resource),  
  5.     .resource     = s3c_lcd_resource,  
  6.     .dev              = {  
  7.         .dma_mask       = &s3c_device_lcd_dmamask,  
  8.         .coherent_dma_mask  = 0xffffffffUL  
  9.     }  
  10. };  

        如上述结构体的resource域记录的便是平台相关的LCD信息:

  1. /* LCD Controller */  
  2.   
  3. static struct resource s3c_lcd_resource[] = {  
  4.     [0] = {  
  5.         .start = S3C24XX_PA_LCD,  
  6.         .end   = S3C24XX_PA_LCD + S3C24XX_SZ_LCD - 1,  
  7.         .flags = IORESOURCE_MEM,  
  8.     },  
  9.     [1] = {  
  10.         .start = IRQ_LCD,  
  11.         .end   = IRQ_LCD,  
  12.         .flags = IORESOURCE_IRQ,  
  13.     }  
  14.   
  15. };  

        如上述结构体的start域存在的便是S3C2410(S3C2440)LCD控制器组的起始物理地址:

  1. #define S3C24XX_PA_LCD      S3C2410_PA_LCD  
  2. #define S3C2410_PA_LCD     (0x4D000000)  

        对照S3C2440的数据手册:


        以后对LCD控制器的配置,直接配置此内在区域即可.此内存区域的大小为:

  1. #define S3C24XX_SZ_LCD     SZ_1M  
  2. #define SZ_1M                           0x00100000  

        具体可结构S3C2440的数据手册参看.

 

        2-1-2.struct s3c2410fb_info:

    明显,这个结构体是记录与平台相关的信息,它被记录在struct fb_info的par域,见函数framebuffer_alloc()源码:

  1. struct fb_info *framebuffer_alloc(size_t size, struct device *dev)  
  2. {  
  3. #define BYTES_PER_LONG (BITS_PER_LONG/8)  
  4. #define PADDING (BYTES_PER_LONG - (sizeof(struct fb_info) % BYTES_PER_LONG))  
  5.     int fb_info_size = sizeof(struct fb_info);  
  6.     struct fb_info *info;  
  7.     char *p;  
  8.   
  9.     if (size)  
  10.         fb_info_size += PADDING;  
  11.   
  12.     p = kzalloc(fb_info_size + size, GFP_KERNEL);  
  13.   
  14.     if (!p)  
  15.         return NULL;  
  16.   
  17.     info = (struct fb_info *) p;  
  18.   
  19.     if (size)  
  20.         info->par = p + fb_info_size;  
  21.   
  22.     info->device = dev;  
  23.   
  24. #ifdef CONFIG_FB_BACKLIGHT  
  25.     mutex_init(&info->bl_curve_mutex);  
  26. #endif  
  27.   
  28.     return info;  
  29. #undef PADDING  
  30. #undef BYTES_PER_LONG  
  31. }  

    中有语句:

  1. if (size)  
  2.     info->par = p + fb_info_size;  

    在结构体struct fb_info里面有如下注释:

  1. /* From here on everything is device dependent */  
  2. void *par;  

    这个指针域记录的是平台相关的信息,如这里记录的是struct s3c2410fb_info.具体平台的fb_info和LCD子系统的fb_info通过此域关联起来.

    至此,需要对平台相关的xxfb_info和LCD核心层的fb_info结构体进行初始化了.

 

        2-1-3.struct s3c2410fb_info *info的初始化:

  1. info = fbinfo->par;  
  2.     info->dev = &pdev->dev;  
  3.     info->drv_type = drv_type;  
  4.       
  5.     info->mem = request_mem_region(res->start, size, pdev->name);  
  6.   
  7.   info->io = ioremap(res->start, size);  
  8.     
  9.   info->irq_base = info->io + ((drv_type == DRV_S3C2412) ? S3C2412_LCDINTBASE : S3C2410_LCDINTBASE);  
  10.     
  11.   for (i = 0; i < 256; i++)  
  12.         info->palette_buffer[i] = PALETTE_BUFF_CLEAR;  
  13.           
  14.     info->clk = clk_get(NULL, "lcd");  
  15.       
  16.     clk_enable(info->clk);  
  17.       
  18.     info->clk_rate = clk_get_rate(info->clk);  
  19.   
  20.     ret = s3c2410fb_map_video_memory(fbinfo);  

    
        上述便是相关平台相关的xxfb_info的初始化.其中下面代码:

  1. info = fbinfo->par;  

         这代码便是平台相关的xxfb_info和LCD核心层的fb_info的关联.

  1. info->dev = &pdev->dev;  

        其中,pdev即为平台设备端的资源,这里是:s3c_device_lcd.其dev域如下:

  1. .dev              = {  
  2.     .dma_mask       = &s3c_device_lcd_dmamask,  
  3.     .coherent_dma_mask  = 0xffffffffUL  
  4. }  

        下面代码:

  1. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);  
  2. info->mem = request_mem_region(res->start, size, pdev->name);  
  3.         info->io = ioremap(res->start, size);  

         第一句是获取平台设备端(即s3c_dvice_lcd)的内存资源信息:

  1. struct resource *platform_get_resource(struct platform_device *dev,  
  2.                        unsigned int type, unsigned int num)  
  3. {  
  4.     int i;  
  5.   
  6.     for (i = 0; i < dev->num_resources; i++) {  
  7.         struct resource *r = &dev->resource[i];  
  8.   
  9.         if (type == resource_type(r) && num-- == 0)  
  10.             return r;  
  11.     }  
  12.     return NULL;  
  13. }  

        见s3c_device_lcd:

  1. /* LCD Controller */  
  2.   
  3. static struct resource s3c_lcd_resource[] = {  
  4.     [0] = {  
  5.         .start = S3C24XX_PA_LCD,  
  6.         .end   = S3C24XX_PA_LCD + S3C24XX_SZ_LCD - 1,  
  7.         .flags = IORESOURCE_MEM,  
  8.     },  
  9.     [1] = {  
  10.         .start = IRQ_LCD,  
  11.         .end   = IRQ_LCD,  
  12.         .flags = IORESOURCE_IRQ,  
  13.     }  
  14.   
  15. };  

        然后把这段内存通过函数ioremap()映射映射成"I/O端口"映射成内存地址,以后要操作某寄存器,变转化成操作这段内在空间即可.
        下面代码:

  1. ret = s3c2410fb_map_video_memory(fbinfo);  

        分配frame buffer.
        至此,平台相关的xxfb_info算是初始化完成.

 

        2-1-4.struct fb_info *fbinfo的初始化:

        struct fb_info *fbinfo是LCD子系统的关键数据,"打包"进LCD核心层,包括操作集及向用户空间暴露设备节点(如/dev/fbn,n = 0,1,2,...)均在此结构体里面体现.

  1. fbinfo->fix.type     = FB_TYPE_PACKED_PIXELS;  
  2. fbinfo->fix.type_aux     = 0;  
  3. fbinfo->fix.xpanstep     = 0;  
  4. fbinfo->fix.ypanstep     = 0;  
  5. fbinfo->fix.ywrapstep        = 0;  
  6. fbinfo->fix.accel        = FB_ACCEL_NONE;  
  7.   
  8. fbinfo->var.nonstd       = 0;  
  9. fbinfo->var.activate     = FB_ACTIVATE_NOW;  
  10. fbinfo->var.accel_flags     = 0;  
  11. fbinfo->var.vmode        = FB_VMODE_NONINTERLACED;  
  12.   
  13. fbinfo->fbops            = &s3c2410fb_ops;  
  14. fbinfo->flags            = FBINFO_FLAG_DEFAULT;  
  15. fbinfo->pseudo_palette      = &info->pseudo_pal;  
  16.   
  17.     /* find maximum required memory size for display */  
  18. for (i = 0; i < mach_info->num_displays; i++) {  
  19.     unsigned long smem_len = mach_info->displays[i].xres;  
  20.   
  21.     smem_len *= mach_info->displays[i].yres;  
  22.     smem_len *= mach_info->displays[i].bpp;  
  23.     smem_len >>= 3;  
  24.     if (fbinfo->fix.smem_len < smem_len)  
  25.         fbinfo->fix.smem_len = smem_len;  
  26. }  
  27.   
  28. ret = s3c2410fb_map_video_memory(fbinfo);  
  29.   
  30. fbinfo->var.xres = display->xres;  
  31. fbinfo->var.yres = display->yres;  
  32. fbinfo->var.bits_per_pixel = display->bpp;  
  33.   
  34. s3c2410fb_init_registers(fbinfo);  
  35.   
  36. s3c2410fb_check_var(&fbinfo->var, fbinfo);  

        上述便是对LCD子系统的关键数据结构fb_info的"打包(即初始化)".

        下面的代码:

  1. fbinfo->fbops            = &s3c2410fb_ops;  

        便是与平台相关的操作集,对应用户空间的系统调用:

  1. static struct fb_ops s3c2410fb_ops = {  
  2.     .owner      = THIS_MODULE,  
  3.     .fb_check_var   = s3c2410fb_check_var,  
  4.     .fb_set_par = s3c2410fb_set_par,  
  5.     .fb_blank   = s3c2410fb_blank,  
  6.     .fb_setcolreg   = s3c2410fb_setcolreg,  
  7.     .fb_fillrect    = cfb_fillrect,  
  8.     .fb_copyarea    = cfb_copyarea,  
  9.     .fb_imageblit   = cfb_imageblit,  
  10. };  

        接下来借助平台设备端的信息初始化fb_info:

  1. fbinfo->var.xres = display->xres;  
  2. fbinfo->var.yres = display->yres;  
  3. fbinfo->var.bits_per_pixel = display->bpp;  

        下面追踪一下display的由来:

  1. display = mach_info->displays + mach_info->default_display;  
  2. mach_info = pdev->dev.platform_data;  

        这里的pdev即为平台总线设备s3c_device_lcd.我们实际使用的过程中,比如屏的大小(如3.5寸、4.3寸等)是需要设置的,下面来看这些具体的屏设备如何设置.以内核自带的mini2440搭载的3.5寸屏为例,进行分析.看这些屏信息如何流窜到LCD子系统的.

        arch/arm/mach-s3c2440/mach-mini2440.c:

  1. static struct s3c2410fb_display mini2440_lcd_cfg[] __initdata = {  
  2.     [0] = { /* mini2440 + 3.5" TFT + touchscreen */  
  3.         _LCD_DECLARE(  
  4.             7,          /* The 3.5 is quite fast */  
  5.             240, 21, 38, 6,     /* x timing */  
  6.             320, 4, 4, 2,       /* y timing */  
  7.             60),            /* refresh rate */  
  8.         .lcdcon5    = (S3C2410_LCDCON5_FRM565 |  
  9.                    S3C2410_LCDCON5_INVVLINE |  
  10.                    S3C2410_LCDCON5_INVVFRAME |  
  11.                    S3C2410_LCDCON5_INVVDEN |  
  12.                    S3C2410_LCDCON5_PWREN),  
  13.     },  
  14.     [1] = { /* mini2440 + 7" TFT + touchscreen */  
  15.         _LCD_DECLARE(  
  16.             10,         /* the 7" runs slower */  
  17.             800, 40, 40, 48,    /* x timing */  
  18.             480, 29, 3, 3,      /* y timing */  
  19.             50),            /* refresh rate */  
  20.         .lcdcon5    = (S3C2410_LCDCON5_FRM565 |  
  21.                    S3C2410_LCDCON5_INVVLINE |  
  22.                    S3C2410_LCDCON5_INVVFRAME |  
  23.                    S3C2410_LCDCON5_PWREN),  
  24.     },  
  25.     /* The VGA shield can outout at several resolutions. All share   
  26.      * the same timings, however, anything smaller than 1024x768  
  27.      * will only be displayed in the top left corner of a 1024x768  
  28.      * XGA output unless you add optional dip switches to the shield.  
  29.      * Therefore timings for other resolutions have been ommited here.  
  30.      */  
  31.     [2] = {  
  32.         _LCD_DECLARE(  
  33.             10,  
  34.             1024, 1, 2, 2,      /* y timing */  
  35.             768, 200, 16, 16,   /* x timing */  
  36.             24),    /* refresh rate, maximum stable,  
  37.                  tested with the FPGA shield */  
  38.         .lcdcon5    = (S3C2410_LCDCON5_FRM565 |  
  39.                    S3C2410_LCDCON5_HWSWP),  
  40.     },  
  41. };  

        见上面的宏--_LCD_DECLARE,是具体屏的信息,如长、宽、边框等.如下:

  1. /* LCD timing and setup */  
  2.   
  3. /*  
  4.  * This macro simplifies the table bellow  
  5.  */  
  6. #define _LCD_DECLARE(_clock,_xres,margin_left,margin_right,hsync, \  
  7.             _yres,margin_top,margin_bottom,vsync, refresh) \  
  8.     .width = _xres, \  
  9.     .xres = _xres, \  
  10.     .height = _yres, \  
  11.     .yres = _yres, \  
  12.     .left_margin    = margin_left,  \  
  13.     .right_margin   = margin_right, \  
  14.     .upper_margin   = margin_top,   \  
  15.     .lower_margin   = margin_bottom,    \  
  16.     .hsync_len  = hsync,    \  
  17.     .vsync_len  = vsync,    \  
  18.     .pixclock   = ((_clock*100000000000LL) /    \  
  19.                ((refresh) * \  
  20.                (hsync + margin_left + _xres + margin_right) * \  
  21.                (vsync + margin_top + _yres + margin_bottom))), \  
  22.     .bpp        = 16,\  
  23.     .type       = (S3C2410_LCDCON1_TFT16BPP |\  
  24.                S3C2410_LCDCON1_TFT)  

        具体的LCD需要根据具体的LCD硬件信息配置这个宏,如长、宽、边框、时钟、显示格式.

        域lcdcon5是根据具体的LCD的硬件信息对LCD主控端的配置信息域.这些信息将会被写到S3C2440的寄存器才会实际生效.这些具体的屏的信息是如何和LCD子系统的关键数据fb_info关联起来的呢?下面继续分析.

        上述的结构体mini2440_lcd_cfg又被封装在结构体mini2440_fb_info当中的display域:

  1. static struct s3c2410fb_mach_info mini2440_fb_info __initdata = {  
  2.     .displays    = &mini2440_lcd_cfg[0], /* not constant! see init */  
  3.     .num_displays    = 1,  
  4.     .default_display = 0,  
  5.   
  6.     /* Enable VD[2..7], VD[10..15], VD[18..23] and VCLK, syncs, VDEN  
  7.      * and disable the pull down resistors on pins we are using for LCD  
  8.      * data. */  
  9.   
  10.     .gpcup      = (0xf << 1) | (0x3f << 10),  
  11.   
  12.     .gpccon     = (S3C2410_GPC1_VCLK   | S3C2410_GPC2_VLINE |  
  13.                S3C2410_GPC3_VFRAME | S3C2410_GPC4_VM |  
  14.                S3C2410_GPC10_VD2   | S3C2410_GPC11_VD3 |  
  15.                S3C2410_GPC12_VD4   | S3C2410_GPC13_VD5 |  
  16.                S3C2410_GPC14_VD6   | S3C2410_GPC15_VD7),  
  17.   
  18.     .gpccon_mask    = (S3C2410_GPCCON_MASK(1)  | S3C2410_GPCCON_MASK(2)  |  
  19.                S3C2410_GPCCON_MASK(3)  | S3C2410_GPCCON_MASK(4)  |  
  20.                S3C2410_GPCCON_MASK(10) | S3C2410_GPCCON_MASK(11) |  
  21.                S3C2410_GPCCON_MASK(12) | S3C2410_GPCCON_MASK(13) |  
  22.                S3C2410_GPCCON_MASK(14) | S3C2410_GPCCON_MASK(15)),  
  23.   
  24.     .gpdup      = (0x3f << 2) | (0x3f << 10),  
  25.   
  26.     .gpdcon     = (S3C2410_GPD2_VD10  | S3C2410_GPD3_VD11 |  
  27.                S3C2410_GPD4_VD12  | S3C2410_GPD5_VD13 |  
  28.                S3C2410_GPD6_VD14  | S3C2410_GPD7_VD15 |  
  29.                S3C2410_GPD10_VD18 | S3C2410_GPD11_VD19 |  
  30.                S3C2410_GPD12_VD20 | S3C2410_GPD13_VD21 |  
  31.                S3C2410_GPD14_VD22 | S3C2410_GPD15_VD23),  
  32.   
  33.     .gpdcon_mask    = (S3C2410_GPDCON_MASK(2)  | S3C2410_GPDCON_MASK(3) |  
  34.                S3C2410_GPDCON_MASK(4)  | S3C2410_GPDCON_MASK(5) |  
  35.                S3C2410_GPDCON_MASK(6)  | S3C2410_GPDCON_MASK(7) |  
  36.                S3C2410_GPDCON_MASK(10) | S3C2410_GPDCON_MASK(11)|  
  37.                S3C2410_GPDCON_MASK(12) | S3C2410_GPDCON_MASK(13)|  
  38.                S3C2410_GPDCON_MASK(14) | S3C2410_GPDCON_MASK(15)),  
  39. };  

        见上述代码:

  1. .displays    = &mini2440_lcd_cfg[0]  

        在函数mini2440_init()中:

  1. static void __init mini2440_init(void)  
  2. {  
  3.     mini2440_parse_features(&features, mini2440_features_str);  
  4.       
  5.     if (features.lcd_index != -1) {  
  6.         int li;  
  7.   
  8.         mini2440_fb_info.displays =  
  9.             &mini2440_lcd_cfg[features.lcd_index];  
  10.   
  11.         printk(KERN_INFO "MINI2440: LCD");  
  12.         for (li = 0; li < ARRAY_SIZE(mini2440_lcd_cfg); li++)  
  13.             if (li == features.lcd_index)  
  14.                 printk(" [%d:%dx%d]", li,  
  15.                     mini2440_lcd_cfg[li].width,  
  16.                     mini2440_lcd_cfg[li].height);  
  17.             else  
  18.                 printk(" %d:%dx%d", li,  
  19.                     mini2440_lcd_cfg[li].width,  
  20.                     mini2440_lcd_cfg[li].height);  
  21.         printk("\n");  
  22.         s3c24xx_fb_set_platdata(&mini2440_fb_info);  
  23.     }  
  24. }  

        展开函数s3c24xx_fb_set_platdata(&mini2440_fb_info):

  1. void __init s3c24xx_fb_set_platdata(struct s3c2410fb_mach_info *pd)  
  2. {  
  3.     struct s3c2410fb_mach_info *npd;  
  4.   
  5.     npd = kmalloc(sizeof(*npd), GFP_KERNEL);  
  6.     if (npd) {  
  7.         memcpy(npd, pd, sizeof(*npd));  
  8.         s3c_device_lcd.dev.platform_data = npd;  
  9.     } else {  
  10.         printk(KERN_ERR "no memory for LCD platform data\n");  
  11.     }  
  12. }  

        可见,平台设备端记录了具体LCD的配置信息:

  1. s3c_device_lcd.dev.platform_data = npd;  

        参数npd即为mini2440_fb_info.

        回到上面的2-1-4:

  1. display = mach_info->displays + mach_info->default_display;  
  2. mach_info = pdev->dev.platform_data;  

        因此,这里mach_info实际保存的是mini2440_fb_info.mini2440_fb_info中的displays域保存的是具体某一款LCD的硬件特性配置,如3.5寸屏.见函数mini2440_init()下面代码:

  1. mini2440_fb_info.displays =  
  2.     &mini2440_lcd_cfg[features.lcd_index];  

        保存的是结构体数组mini2440_lcd_cfg的某一元素,其元素成员即为保证某一款屏的硬件特性配置信息.回到2-1-4:

  1. fbinfo->var.xres = display->xres;  
  2. fbinfo->var.yres = display->yres;  
  3. fbinfo->var.bits_per_pixel = display->bpp;  

        LCD子系统的关键数据fbinfo这时获取了具体LCD硬件特性配置的信息,如长、宽、像素点的位数等.

        回到2-1-4,见函数s3c2410fb_init_registers():

  1. /*  
  2.  * s3c2410fb_init_registers - Initialise all LCD-related registers  
  3.  */  
  4. static int s3c2410fb_init_registers(struct fb_info *info)  
  5. {  
  6.     struct s3c2410fb_info *fbi = info->par;  
  7.     struct s3c2410fb_mach_info *mach_info = fbi->dev->platform_data;  
  8.     unsigned long flags;  
  9.     void __iomem *regs = fbi->io;  
  10.     void __iomem *tpal;  
  11.     void __iomem *lpcsel;  
  12.   
  13.     if (is_s3c2412(fbi)) {  
  14.         tpal = regs + S3C2412_TPAL;  
  15.         lpcsel = regs + S3C2412_TCONSEL;  
  16.     } else {  
  17.         tpal = regs + S3C2410_TPAL;  
  18.         lpcsel = regs + S3C2410_LPCSEL;  
  19.     }  
  20.   
  21.     /* Initialise LCD with values from haret */  
  22.   
  23.     local_irq_save(flags);  
  24.   
  25.     /* modify the gpio(s) with interrupts set (bjd) */  
  26.   
  27.     modify_gpio(S3C2410_GPCUP,  mach_info->gpcup,  mach_info->gpcup_mask);  
  28.     modify_gpio(S3C2410_GPCCON, mach_info->gpccon, mach_info->gpccon_mask);  
  29.     modify_gpio(S3C2410_GPDUP,  mach_info->gpdup,  mach_info->gpdup_mask);  
  30.     modify_gpio(S3C2410_GPDCON, mach_info->gpdcon, mach_info->gpdcon_mask);  
  31.   
  32.     local_irq_restore(flags);  
  33.   
  34.     dprintk("LPCSEL    = 0x%08lx\n", mach_info->lpcsel);  
  35.     writel(mach_info->lpcsel, lpcsel);  
  36.   
  37.     dprintk("replacing TPAL %08x\n", readl(tpal));  
  38.   
  39.     /* ensure temporary palette disabled */  
  40.     writel(0x00, tpal);  
  41.   
  42.     return 0;  
  43. }  

        这里对平台LCD控制器的初始化.比如使能LCD控制引脚的上拉电阻之类的.

        2-1-4接下来的函数 s3c2410fb_check_var(&fbinfo->var, fbinfo),此函数中涉及到另外一个重要的数据结构:struct fb_var_screeninfo.此结构体保存的是用户可修改的关于FBI的信息.比如X向分辨率、Y向分辨率、像素的位数、pixclock等.如下:

  1. struct fb_var_screeninfo {  
  2.     __u32 xres;         /* visible resolution       */  
  3.     __u32 yres;  
  4.     __u32 xres_virtual;     /* virtual resolution       */  
  5.     __u32 yres_virtual;  
  6.     __u32 xoffset;          /* offset from virtual to visible */  
  7.     __u32 yoffset;          /* resolution           */  
  8.   
  9.     __u32 bits_per_pixel;       /* guess what           */  
  10.     __u32 grayscale;        /* != 0 Graylevels instead of colors */  
  11.   
  12.     struct fb_bitfield red;     /* bitfield in fb mem if true color, */  
  13.     struct fb_bitfield green;   /* else only length is significant */  
  14.     struct fb_bitfield blue;  
  15.     struct fb_bitfield transp;  /* transparency         */    
  16.   
  17.     __u32 nonstd;           /* != 0 Non standard pixel format */  
  18.   
  19.     __u32 activate;         /* see FB_ACTIVATE_*        */  
  20.   
  21.     __u32 height;           /* height of picture in mm    */  
  22.     __u32 width;            /* width of picture in mm     */  
  23.   
  24.     __u32 accel_flags;      /* (OBSOLETE) see fb_info.flags */  
  25.   
  26.     /* Timing: All values in pixclocks, except pixclock (of course) */  
  27.     __u32 pixclock;         /* pixel clock in ps (pico seconds) */  
  28.     __u32 left_margin;      /* time from sync to picture    */  
  29.     __u32 right_margin;     /* time from picture to sync    */  
  30.     __u32 upper_margin;     /* time from sync to picture    */  
  31.     __u32 lower_margin;  
  32.     __u32 hsync_len;        /* length of horizontal sync    */  
  33.     __u32 vsync_len;        /* length of vertical sync  */  
  34.     __u32 sync;         /* see FB_SYNC_*        */  
  35.     __u32 vmode;            /* see FB_VMODE_*       */  
  36.     __u32 rotate;           /* angle we rotate counter clockwise */  
  37.     __u32 reserved[5];      /* Reserved for future compatibility */  
  38. };  

        用户空间的命令fbset抓取的信息便是对应此结构体:

  1. #fbset  
  2.   
  3. mode "240x320-106"  
  4.         # D: 9.709 MHz, H: 34.674 kHz, V: 106.362 Hz  
  5.         geometry 240 320 240 640 16  
  6.         timings 103000 20 10 2 2 10 2  
  7.         accel false  
  8.         rgba 5/11,6/5,5/0,0/0  
  9. endmode  
  10.   
  11. #     

        明显,结构体struct fb_var_screeninfo保存的信息和具体的LCD硬件参数有关.在函数s3c2410fb_check_var()完成此结构体的初始化:

  1. /*  
  2.  *  s3c2410fb_check_var():  
  3.  *  Get the video params out of 'var'. If a value doesn't fit, round it up,  
  4.  *  if it's too big, return -EINVAL.  
  5.  *  
  6.  */  
  7. static int s3c2410fb_check_var(struct fb_var_screeninfo *var,  
  8.                    struct fb_info *info)  
  9. {  
  10.     struct s3c2410fb_info *fbi = info->par;  
  11.     struct s3c2410fb_mach_info *mach_info = fbi->dev->platform_data;  
  12.     struct s3c2410fb_display *display = NULL;  
  13.     struct s3c2410fb_display *default_display = mach_info->displays +  
  14.                             mach_info->default_display;  
  15.     int type = default_display->type;  
  16.     unsigned i;  
  17.   
  18.     dprintk("check_var(var=%p, info=%p)\n", var, info);  
  19.   
  20.     /* validate x/y resolution */  
  21.     /* choose default mode if possible */  
  22.     if (var->yres == default_display->yres &&  
  23.         var->xres == default_display->xres &&  
  24.         var->bits_per_pixel == default_display->bpp)  
  25.         display = default_display;  
  26.     else  
  27.         for (i = 0; i < mach_info->num_displays; i++)  
  28.             if (type == mach_info->displays[i].type &&  
  29.                 var->yres == mach_info->displays[i].yres &&  
  30.                 var->xres == mach_info->displays[i].xres &&  
  31.                 var->bits_per_pixel == mach_info->displays[i].bpp) {  
  32.                 display = mach_info->displays + i;  
  33.                 break;  
  34.             }  
  35.   
  36.     if (!display) {  
  37.         dprintk("wrong resolution or depth %dx%d at %d bpp\n",  
  38.             var->xres, var->yres, var->bits_per_pixel);  
  39.         return -EINVAL;  
  40.     }  
  41.   
  42.     /* it is always the size as the display */  
  43.     var->xres_virtual = display->xres;  
  44.     var->yres_virtual = display->yres;  
  45.     var->height = display->height;  
  46.     var->width = display->width;  
  47.   
  48.     /* copy lcd settings */  
  49.     var->pixclock = display->pixclock;  
  50.     var->left_margin = display->left_margin;  
  51.     var->right_margin = display->right_margin;  
  52.     var->upper_margin = display->upper_margin;  
  53.     var->lower_margin = display->lower_margin;  
  54.     var->vsync_len = display->vsync_len;  
  55.     var->hsync_len = display->hsync_len;  
  56.   
  57.     fbi->regs.lcdcon5 = display->lcdcon5;  
  58.     /* set display type */  
  59.     fbi->regs.lcdcon1 = display->type;  
  60.   
  61.     var->transp.offset = 0;  
  62.     var->transp.length = 0;  
  63.     /* set r/g/b positions */  
  64.     switch (var->bits_per_pixel) {  
  65.     case 1:  
  66.     case 2:  
  67.     case 4:  
  68.         var->red.offset  = 0;  
  69.         var->red.length  = var->bits_per_pixel;  
  70.         var->green   = var->red;  
  71.         var->blue    = var->red;  
  72.         break;  
  73.     case 8:  
  74.         if (display->type != S3C2410_LCDCON1_TFT) {  
  75.             /* 8 bpp 332 */  
  76.             var->red.length      = 3;  
  77.             var->red.offset      = 5;  
  78.             var->green.length    = 3;  
  79.             var->green.offset    = 2;  
  80.             var->blue.length = 2;  
  81.             var->blue.offset = 0;  
  82.         } else {  
  83.             var->red.offset      = 0;  
  84.             var->red.length      = 8;  
  85.             var->green       = var->red;  
  86.             var->blue        = var->red;  
  87.         }  
  88.         break;  
  89.     case 12:  
  90.         /* 12 bpp 444 */  
  91.         var->red.length      = 4;  
  92.         var->red.offset      = 8;  
  93.         var->green.length    = 4;  
  94.         var->green.offset    = 4;  
  95.         var->blue.length = 4;  
  96.         var->blue.offset = 0;  
  97.         break;  
  98.   
  99.     default:  
  100.     case 16:  
  101.         if (display->lcdcon5 & S3C2410_LCDCON5_FRM565) {  
  102.             /* 16 bpp, 565 format */  
  103.             var->red.offset      = 11;  
  104.             var->green.offset    = 5;  
  105.             var->blue.offset = 0;  
  106.             var->red.length      = 5;  
  107.             var->green.length    = 6;  
  108.             var->blue.length = 5;  
  109.         } else {  
  110.             /* 16 bpp, 5551 format */  
  111.             var->red.offset      = 11;  
  112.             var->green.offset    = 6;  
  113.             var->blue.offset = 1;  
  114.             var->red.length      = 5;  
  115.             var->green.length    = 5;  
  116.             var->blue.length = 5;  
  117.         }  
  118.         break;  
  119.     case 32:  
  120.         /* 24 bpp 888 and 8 dummy */  
  121.         var->red.length      = 8;  
  122.         var->red.offset      = 16;  
  123.         var->green.length    = 8;  
  124.         var->green.offset    = 8;  
  125.         var->blue.length = 8;  
  126.         var->blue.offset = 0;  
  127.         break;  
  128.     }  
  129.     return 0;  
  130. }  

        因此,此处便是借助具体的LCD硬件信息去"打包"结构体struct fb_var_screeninfo.

       和上面的结构体struct fb_var_screeninfo对应的,有结构体struct fb_fix_screeninfo,此结构体保存的是用户无法修改的信息,比如帧缓冲在内存的起始地址和大小.回到2-1-4,关于fb_info的fix域的初始化代码如下:

       

  1. strcpy(fbinfo->fix.id, driver_name);  
  2.   
  3. fbinfo->fix.type     = FB_TYPE_PACKED_PIXELS;  
  4. fbinfo->fix.type_aux     = 0;  
  5. fbinfo->fix.xpanstep     = 0;  
  6. fbinfo->fix.ypanstep     = 0;  
  7. fbinfo->fix.ywrapstep        = 0;  
  8. fbinfo->fix.accel        = FB_ACCEL_NONE;  
  9.   
  10. /* find maximum required memory size for display */  
  11. for (i = 0; i < mach_info->num_displays; i++) {  
  12.     unsigned long smem_len = mach_info->displays[i].xres;  
  13.   
  14.     smem_len *= mach_info->displays[i].yres;  
  15.     smem_len *= mach_info->displays[i].bpp;  
  16.     smem_len >>= 3;  
  17.     if (fbinfo->fix.smem_len < smem_len)  
  18.         fbinfo->fix.smem_len = smem_len;  
  19. }  
  20.   
  21. static int __init s3c2410fb_map_video_memory(struct fb_info *info)  

        展开函数s3c2410fb_map_video_memory(fbinfo): 

  1. static int __init s3c2410fb_map_video_memory(struct fb_info *info)  
  2. {  
  3.     struct s3c2410fb_info *fbi = info->par;  
  4.     dma_addr_t map_dma;  
  5.     unsigned map_size = PAGE_ALIGN(info->fix.smem_len);  
  6.   
  7.     dprintk("map_video_memory(fbi=%p) map_size %u\n", fbi, map_size);  
  8.   
  9.     info->screen_base = dma_alloc_writecombine(fbi->dev, map_size,  
  10.                            &map_dma, GFP_KERNEL);  
  11.   
  12.     if (info->screen_base) {  
  13.         /* prevent initial garbage on screen */  
  14.         dprintk("map_video_memory: clear %p:%08x\n",  
  15.             info->screen_base, map_size);  
  16.         memset(info->screen_base, 0x00, map_size);  
  17.   
  18.         info->fix.smem_start = map_dma;  
  19.   
  20.         dprintk("map_video_memory: dma=%08lx cpu=%p size=%08x\n",  
  21.             info->fix.smem_start, info->screen_base, map_size);  
  22.     }  
  23.   
  24.     return info->screen_base ? 0 : -ENOMEM;  
  25. }  

        其中下面两语句:

  1. info->screen_base = dma_alloc_writecombine(fbi->dev, map_size,  
  2.                    &map_dma, GFP_KERNEL);  
  3. info->fix.smem_start = map_dma;  

        分配了一个DMA内存,并将其记录在fb_info->fix.smem_start中,这是帧缓冲(FB)的内存起始位置.但是,这里只是分配内存,并不指定这段内存从哪个位置开始分配,因为LCD的帧缓冲的地址是唯一的,这段内存空间是如何和LCD的帧缓冲关联起来的呢?在函数s3c24xxfb_probe()中,见下面代码:

  1. ret = s3c2410fb_cpufreq_register(info);  
  2. if (ret < 0) {  
  3.     dev_err(&pdev->dev, "Failed to register cpufreq\n");  
  4.     goto free_video_memory;  
  5. }  

        展开函数s3c2410fb_cpufreq_register():

  1. static inline int s3c2410fb_cpufreq_register(struct s3c2410fb_info *info)  
  2. {  
  3.     info->freq_transition.notifier_call = s3c2410fb_cpufreq_transition;  
  4.   
  5.     return cpufreq_register_notifier(&info->freq_transition,  
  6.                      CPUFREQ_TRANSITION_NOTIFIER);  
  7. }  

        回调函数s3c2410fb_cpufreq_transition保存在info->freq_transition.notifier_call.如何实现回调在此处就不展开.下面展开函数s3c2410fb_cpufreq_transition():

  1. static int s3c2410fb_cpufreq_transition(struct notifier_block *nb,unsigned long val, void *data)  
  2. {  
  3.         s3c2410fb_activate_var(fbinfo);  
  4. }  

        展开函数s3c2410fb_activate_var(fbinfo):

  1. /* s3c2410fb_activate_var  
  2.  *  
  3.  * activate (set) the controller from the given framebuffer  
  4.  * information  
  5.  */  
  6. static void s3c2410fb_activate_var(struct fb_info *info)  
  7. {  
  8.     struct s3c2410fb_info *fbi = info->par;  
  9.     void __iomem *regs = fbi->io;  
  10.     int type = fbi->regs.lcdcon1 & S3C2410_LCDCON1_TFT;  
  11.     struct fb_var_screeninfo *var = &info->var;  
  12.     int clkdiv;  
  13.   
  14.     clkdiv = DIV_ROUND_UP(s3c2410fb_calc_pixclk(fbi, var->pixclock), 2);  
  15.   
  16.     dprintk("%s: var->xres  = %d\n", __func__, var->xres);  
  17.     dprintk("%s: var->yres  = %d\n", __func__, var->yres);  
  18.     dprintk("%s: var->bpp   = %d\n", __func__, var->bits_per_pixel);  
  19.   
  20.     if (type == S3C2410_LCDCON1_TFT) {  
  21.         s3c2410fb_calculate_tft_lcd_regs(info, &fbi->regs);  
  22.         --clkdiv;  
  23.         if (clkdiv < 0)  
  24.             clkdiv = 0;  
  25.     } else {  
  26.         s3c2410fb_calculate_stn_lcd_regs(info, &fbi->regs);  
  27.         if (clkdiv < 2)  
  28.             clkdiv = 2;  
  29.     }  
  30.   
  31.     fbi->regs.lcdcon1 |=  S3C2410_LCDCON1_CLKVAL(clkdiv);  
  32.   
  33.     /* write new registers */  
  34.   
  35.     dprintk("new register set:\n");  
  36.     dprintk("lcdcon[1] = 0x%08lx\n", fbi->regs.lcdcon1);  
  37.     dprintk("lcdcon[2] = 0x%08lx\n", fbi->regs.lcdcon2);  
  38.     dprintk("lcdcon[3] = 0x%08lx\n", fbi->regs.lcdcon3);  
  39.     dprintk("lcdcon[4] = 0x%08lx\n", fbi->regs.lcdcon4);  
  40.     dprintk("lcdcon[5] = 0x%08lx\n", fbi->regs.lcdcon5);  
  41.   
  42.     writel(fbi->regs.lcdcon1 & ~S3C2410_LCDCON1_ENVID,  
  43.         regs + S3C2410_LCDCON1);  
  44.     writel(fbi->regs.lcdcon2, regs + S3C2410_LCDCON2);  
  45.     writel(fbi->regs.lcdcon3, regs + S3C2410_LCDCON3);  
  46.     writel(fbi->regs.lcdcon4, regs + S3C2410_LCDCON4);  
  47.     writel(fbi->regs.lcdcon5, regs + S3C2410_LCDCON5);  
  48.   
  49.     /* set lcd address pointers */  
  50.     s3c2410fb_set_lcdaddr(info);  
  51.   
  52.     fbi->regs.lcdcon1 |= S3C2410_LCDCON1_ENVID,  
  53.     writel(fbi->regs.lcdcon1, regs + S3C2410_LCDCON1);  
  54. }  

        展开函数s3c2410fb_set_lcdaddr():

  1. /* s3c2410fb_set_lcdaddr  
  2.  *  
  3.  * initialise lcd controller address pointers  
  4.  */  
  5. static void s3c2410fb_set_lcdaddr(struct fb_info *info)  
  6. {  
  7.     unsigned long saddr1, saddr2, saddr3;  
  8.     struct s3c2410fb_info *fbi = info->par;  
  9.     void __iomem *regs = fbi->io;  
  10.   
  11.     saddr1  = info->fix.smem_start >> 1;  
  12.     saddr2  = info->fix.smem_start;  
  13.     saddr2 += info->fix.line_length * info->var.yres;  
  14.     saddr2 >>= 1;  
  15.   
  16.     saddr3 = S3C2410_OFFSIZE(0) |  
  17.          S3C2410_PAGEWIDTH((info->fix.line_length / 2) & 0x3ff);  
  18.   
  19.     dprintk("LCDSADDR1 = 0x%08lx\n", saddr1);  
  20.     dprintk("LCDSADDR2 = 0x%08lx\n", saddr2);  
  21.     dprintk("LCDSADDR3 = 0x%08lx\n", saddr3);  
  22.   
  23.     writel(saddr1, regs + S3C2410_LCDSADDR1);  
  24.     writel(saddr2, regs + S3C2410_LCDSADDR2);  
  25.     writel(saddr3, regs + S3C2410_LCDSADDR3);  
  26. }  

        下面代码实现了DMA内存和S3C2440平台LCD控制器帧缓冲的关联:

  1. writel(saddr1, regs + S3C2410_LCDSADDR1);  
  2. writel(saddr2, regs + S3C2410_LCDSADDR2);  
  3. writel(saddr3, regs + S3C2410_LCDSADDR3);  

 

    3.向LCD核心层注册fb_info:

        经过上述第2步,完成了具体平台、具体LCD信息对LCD子系统关键数据fb_info的打包,接下来通过函数register_framebuffer()把xxfb_info注册进内核:

  1. /**  
  2.  *  register_framebuffer - registers a frame buffer device  
  3.  *  @fb_info: frame buffer info structure  
  4.  *  
  5.  *  Registers a frame buffer device @fb_info.  
  6.  *  
  7.  *  Returns negative errno on error, or zero for success.  
  8.  *  
  9.  */  
  10.   
  11. int  
  12. register_framebuffer(struct fb_info *fb_info)  
  13. {  
  14.     int i;  
  15.     struct fb_event event;  
  16.     struct fb_videomode mode;  
  17.   
  18.     if (num_registered_fb == FB_MAX)  
  19.         return -ENXIO;  
  20.   
  21.     if (fb_check_foreignness(fb_info))  
  22.         return -ENOSYS;  
  23.   
  24.     /* check all firmware fbs and kick off if the base addr overlaps */  
  25.     for (i = 0 ; i < FB_MAX; i++) {  
  26.         if (!registered_fb[i])  
  27.             continue;  
  28.   
  29.         if (registered_fb[i]->flags & FBINFO_MISC_FIRMWARE) {  
  30.             if (fb_do_apertures_overlap(registered_fb[i], fb_info)) {  
  31.                 printk(KERN_ERR "fb: conflicting fb hw usage "  
  32.                        "%s vs %s - removing generic driver\n",  
  33.                        fb_info->fix.id,  
  34.                        registered_fb[i]->fix.id);  
  35.                 unregister_framebuffer(registered_fb[i]);  
  36.                 break;  
  37.             }  
  38.         }  
  39.     }  
  40.   
  41.     num_registered_fb++;  
  42.     for (i = 0 ; i < FB_MAX; i++)  
  43.         if (!registered_fb[i])  
  44.             break;  
  45.     fb_info->node = i;  
  46.     mutex_init(&fb_info->lock);  
  47.     mutex_init(&fb_info->mm_lock);  
  48.   
  49.     fb_info->dev = device_create(fb_class, fb_info->device,  
  50.                      MKDEV(FB_MAJOR, i), NULL, "fb%d", i);  
  51.     if (IS_ERR(fb_info->dev)) {  
  52.         /* Not fatal */  
  53.         printk(KERN_WARNING "Unable to create device for framebuffer %d; errno = %ld\n", i, PTR_ERR(fb_info->dev));  
  54.         fb_info->dev = NULL;  
  55.     } else  
  56.         fb_init_device(fb_info);  
  57.   
  58.     if (fb_info->pixmap.addr == NULL) {  
  59.         fb_info->pixmap.addr = kmalloc(FBPIXMAPSIZE, GFP_KERNEL);  
  60.         if (fb_info->pixmap.addr) {  
  61.             fb_info->pixmap.size = FBPIXMAPSIZE;  
  62.             fb_info->pixmap.buf_align = 1;  
  63.             fb_info->pixmap.scan_align = 1;  
  64.             fb_info->pixmap.access_align = 32;  
  65.             fb_info->pixmap.flags = FB_PIXMAP_DEFAULT;  
  66.         }  
  67.     }     
  68.     fb_info->pixmap.offset = 0;  
  69.   
  70.     if (!fb_info->pixmap.blit_x)  
  71.         fb_info->pixmap.blit_x = ~(u32)0;  
  72.   
  73.     if (!fb_info->pixmap.blit_y)  
  74.         fb_info->pixmap.blit_y = ~(u32)0;  
  75.   
  76.     if (!fb_info->modelist.prev || !fb_info->modelist.next)  
  77.         INIT_LIST_HEAD(&fb_info->modelist);  
  78.   
  79.     fb_var_to_videomode(&mode, &fb_info->var);  
  80.     fb_add_videomode(&mode, &fb_info->modelist);  
  81.     registered_fb[i] = fb_info;  
  82.   
  83.     event.info = fb_info;  
  84.     if (!lock_fb_info(fb_info))  
  85.         return -ENODEV;  
  86.     fb_notifier_call_chain(FB_EVENT_FB_REGISTERED, &event);  
  87.     unlock_fb_info(fb_info);  
  88.     return 0;  
  89. }  

        下面的代码完成了/dev/fbn(n=0,1,2,...)设备节点的创建:

  1. fb_info->dev = device_create(fb_class, fb_info->device,  
  2.                  MKDEV(FB_MAJOR, i), NULL, "fb%d", i);  

        LINUX下一个驱动要实现设备节点的创建需要下面两个内核API:

  1. class_create(owner, name)  
  2. struct device *device_create(struct class *class, struct device *parent,dev_t devt, void *drvdata, const char *fmt, ...)  

        注意到上述代码:

  1. fb_info->dev = device_create(fb_class, fb_info->device,MKDEV(FB_MAJOR, i), NULL, "fb%d", i);  

        函数device_create()中有个参数fb_class是全局变量:

  1. struct class *fb_class;  
  2. EXPORT_SYMBOL(fb_class);  

        其初始化代码如下:

  1. /**  
  2.  *  fbmem_init - init frame buffer subsystem  
  3.  *  
  4.  *  Initialize the frame buffer subsystem.  
  5.  *  
  6.  *  NOTE: This function is _only_ to be called by drivers/char/mem.c.  
  7.  *  
  8.  */  
  9.   
  10. static int __init  
  11. fbmem_init(void)  
  12. {  
  13.     proc_create("fb", 0, NULL, &fb_proc_fops);  
  14.   
  15.     if (register_chrdev(FB_MAJOR,"fb",&fb_fops))  
  16.         printk("unable to get major %d for fb devs\n", FB_MAJOR);  
  17.   
  18.     fb_class = class_create(THIS_MODULE, "graphics");  
  19.     if (IS_ERR(fb_class)) {  
  20.         printk(KERN_WARNING "Unable to create fb class; errno = %ld\n", PTR_ERR(fb_class));  
  21.         fb_class = NULL;  
  22.     }  
  23.     return 0;  
  24. }  

        因此,语句 fb_class = class_create(THIS_MODULE, "graphics")和语句 fb_info->dev = device_create(fb_class, fb_info->device,MKDEV(FB_MAJOR, i), NULL, "fb%d", i);共同生成了设备节点/dev/fbn(n=0,1,2,...).在上述的函数fbmem_init()中,见语句:

  1. if (register_chrdev(FB_MAJOR,"fb",&fb_fops))  

        把LCD当作字符设备来看待.这部分代码是平台无关的,其中平台无关性的操作集fb_fops:

  1. static const struct file_operations fb_fops = {  
  2.     .owner =    THIS_MODULE,  
  3.     .read =     fb_read,  
  4.     .write =    fb_write,  
  5.     .unlocked_ioctl = fb_ioctl,  
  6. #ifdef CONFIG_COMPAT  
  7.     .compat_ioctl = fb_compat_ioctl,  
  8. #endif  
  9.     .mmap =     fb_mmap,  
  10.     .open =     fb_open,  
  11.     .release =  fb_release,  
  12. #ifdef HAVE_ARCH_FB_UNMAPPED_AREA  
  13.     .get_unmapped_area = get_fb_unmapped_area,  
  14. #endif  
  15. #ifdef CONFIG_FB_DEFERRED_IO  
  16.     .fsync =    fb_deferred_io_fsync,  
  17. #endif  
  18. };  

        这里是平台无关的代码,同时也是直接面向用户空间系统调用的操作集.但是实际我们的操作中,我们往往要作用在具体平台搭载的LCD硬件特性上.那么,LINUX下的LCD子系统是如何实现平台无关到平台相关的过渡的呢?下面以open()函数为例分析这一思想设计过程.当用户空间进行open进,对应的fb_open函数被调用:

       

  1. static int  
  2. fb_open(struct inode *inode, struct file *file)  
  3. __acquires(&info->lock)  
  4. __releases(&info->lock)  
  5. {  
  6.     int fbidx = iminor(inode);  
  7.     struct fb_info *info;  
  8.     int res = 0;  
  9.   
  10.     if (fbidx >= FB_MAX)  
  11.         return -ENODEV;  
  12.     info = registered_fb[fbidx];  
  13.     if (!info)  
  14.         request_module("fb%d", fbidx);  
  15.     info = registered_fb[fbidx];  
  16.     if (!info)  
  17.         return -ENODEV;  
  18.     mutex_lock(&info->lock);  
  19.     if (!try_module_get(info->fbops->owner)) {  
  20.         res = -ENODEV;  
  21.         goto out;  
  22.     }  
  23.     file->private_data = info;  
  24.     if (info->fbops->fb_open) {  
  25.         res = info->fbops->fb_open(info,1);  
  26.         if (res)  
  27.             module_put(info->fbops->owner);  
  28.     }  
  29. #ifdef CONFIG_FB_DEFERRED_IO  
  30.     if (info->fbdefio)  
  31.         fb_deferred_io_open(info, inode, file);  
  32. #endif  
  33. out:  
  34.     mutex_unlock(&info->lock);  
  35.     return res;  
  36. }  

        注意到这里会根据设备号/dev/fbn(n = 0,1,2,...)提取出我们相应的平台相关的fb_info,见下面代码:

  1. info = registered_fb[fbidx];  
  2. if (!info)  
  3.     request_module("fb%d", fbidx);  
  4. info = registered_fb[fbidx];  

        其中,registered_fb是一个全局数组:

  1. extern struct fb_info *registered_fb[FB_MAX];  

        当我们把我们具体平台相关的fb_info注册进LCD核心子系统时,我们平台相关的fb_info被记录在这个数组里面.见函数int register_framebuffer(struct fb_info *fb_info)下面代码:

  1. for (i = 0 ; i < FB_MAX; i++) {  
  2.     if (!registered_fb[i])  
  3.         continue;  
  4.   
  5.     if (registered_fb[i]->flags & FBINFO_MISC_FIRMWARE) {  
  6.         if (fb_do_apertures_overlap(registered_fb[i], fb_info)) {  
  7.             printk(KERN_ERR "fb: conflicting fb hw usage "  
  8.                    "%s vs %s - removing generic driver\n",  
  9.                    fb_info->fix.id,  
  10.                    registered_fb[i]->fix.id);  
  11.             unregister_framebuffer(registered_fb[i]);  
  12.             break;  
  13.         }  
  14.     }  
  15. }  
  16.   
  17. registered_fb[i] = fb_info;  

        因此,回到函数fb_open()中:

  1. info = registered_fb[fbidx];  

        提取的便是相应的平台相关的fb_info.见下面的代码:

  1. file->private_data = info;  
  2. if (info->fbops->fb_open) {  
  3.     res = info->fbops->fb_open(info,1);  
  4.     if (res)  
  5.         module_put(info->fbops->owner);  
  6. }  

        用户空间虽然直接面向的是fb_open函数,但是在此函数中.又重载了具体平台相关的fb_open()函数.因此,以S3C2440平台为例,在函数:

  1. static int __init s3c24xxfb_probe(struct platform_device *pdev,enum s3c_drv_type drv_type)  

        见下面代码:

  1. fbinfo->fbops            = &s3c2410fb_ops;  

        展开操作集s3c2410fb_ops:

  1. static struct fb_ops s3c2410fb_ops = {  
  2.     .owner      = THIS_MODULE,  
  3.     .fb_check_var   = s3c2410fb_check_var,  
  4.     .fb_set_par = s3c2410fb_set_par,  
  5.     .fb_blank   = s3c2410fb_blank,  
  6.     .fb_setcolreg   = s3c2410fb_setcolreg,  
  7.     .fb_fillrect    = cfb_fillrect,  
  8.     .fb_copyarea    = cfb_copyarea,  
  9.     .fb_imageblit   = cfb_imageblit,  
  10. };  

        只是这里没有重载open函数.

 

        3.系统调用:

        随着s3c24xxfb_probe()函数的执行完成,内核态的软件布署也宣布完成.接下来看是LCD子系统是如何跟用户空间交互的.在LINUX下的LCD子系统,由关键数据结构fb_info去承载.见函数s3c24xxfb_probe()中下面代码:

  1. fbinfo->fbops            = &s3c2410fb_ops;  

        fbops结构包含了底层帧缓冲驱动程序提供的所有函数指针.fbops中前几个方法是必须要实现的,剩下的是可选的(主要用于图形加速).比如当用户接口需要执行繁重的视频操作,像混合(blending)、拉伸(stretching)、移动位图、动态渐变(gradient)生成,这时候需要通过图形加速来获取可以接受的性能.比如某CPU平台支持图形加速,大体会进行下面的动作:

        fb_imageblit()方法在显示器画一幅图像,此函数为驱动提供了一个能利用视频控制器所拥有的某些特别能力的机会,加速这个操作.如果硬件不支持加速功能,cfb_imageblit()就是帧缓冲核心为加速提供的通用库函数,如在启动阶段向屏幕输出logo.fb_copyarea()将屏幕的一个矩形区域复制到另一个区域.如果图形控制器没有任何可加速这个操作能力,那么cfb_copyarea()就是为这个操作提供优化的方法.fb_fillrect()方法能快速用像素行填充矩形区.cfb_fillrect()则是通用的非加速的方法手段.

        [附:]DirectFB.DirectFB是一个建立在帧缓冲接口之上的库,它为硬件图形加速和可视化接口提供了一个简单的窗口管理框架和钩子.它同时也是一个虚拟接口,允许多个帧缓冲应用程序同时存在.某些关心图形性能的嵌入式设备采用DirectFB方案,在这种策略中,DirectFB与底层的加速的帧缓冲设备驱动程序与上层的支持DierctFB的渲染引擎.

 

        4.启动logo:

        如果要在机器启动过程中显示一个logo.需要在内核配置阶段启用CONFIG_LOGO选项并选择一个可用的logo.如果需要自定义logo,往目录drivers/video/logo增加即可.常用的LOGO的图片格式是CLUT224.其实现主要由底层帧缓冲驱动程序的fb_imageblit()函数完成.其他格式的logo(如16色的vga16和黑白的单色),可以借助script/目录下的脚本将标准的PPM转换成logo格式.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值