15.LINUX-LCD Hierarchy Analysis (detailed)

Source: Internet
Author: User

If our system is to use a GUI (graphical interface), then the LCD device driver should be written into the Frambuffer interface, rather than just write the operating layer of the LCD controller interface as before.

What is a frambuffer device?

The Frambuffer device layer is an abstraction of an image device that represents the frame cache of the video hardware, allowing the application to access the hardware through a defined interface. So the application does not need to consider the underlying (register-level) operations. Application access to device files is typically in the/dev directory, such as/dev/fb*.

Frambuffer in the kernel in DRIVERS/VIDEO/FBMEM.C (fb:frame buffer)

1. We enter FBMEM.C to find its entry function:

Static  int__init Fbmem_init (void) {create_proc_read_entry ("FB",0, NULL, FBMEM_READ_PROC, NULL); if(Register_chrdev (Fb_major,"FB", &fb_fops))//(1) Creating a character devicePRINTK ("unable to get major%d for FB devs\n", Fb_major); Fb_class= Class_create (This_module,"Graphics");//Create Class         if(Is_err (Fb_class)) {PRINTK (kern_warning"Unable to create FB class; errno =%ld\n", Ptr_err (Fb_class)); Fb_class=NULL; }         return 0;}

(1) Create the character device "FB", fb_major=29, the main device number is 29, we cat/proc/devices can also find this character device:

Like our previous drivers, but not using the Create device node, why?

Because of the need to register the LCD driver, there will be a device node, so there is no code, the following will be analyzed where there is.

2. Let's take a look at the. Open function and the. Read function of the registered file_operations structure Fb_fops, how the application layer opens the drive, reads the drive data

The 2.1 Fb_open function is as follows:

Static intFb_open (structInode *inode,structFile *file) {       intFbidx = Iminor (inode);//Gets the secondary device number of the device node       structFb_info *info;//define the FB_INFO structure body       intres =0; ... ...if(! (info = registered_fb[fbidx]))//(1) info= Registered_fb[fbidx], get the LCD driver information for this device numbertry_to_load (FBIDX); ... ...        if(info->fbops->Fb_open) {Res= Info->fbops->fb_open (Info,1);//Call Registered_fb[fbidx]->fbops->fb_open              if(RES) module_put (Info->fbops->owner); }       returnRes;}

(1) Registered_fb[fbidx] This array is also a fb_info struct, where fbidx equals the secondary device ID, obviously this array is the information that holds our various LCD drivers

The 2.2 Fb_read function is as follows:

Staticssize_t Fb_read (structFile *file,Char__user *buf, size_t count, loff_t *PPOs) {unsignedLongp = *PPOs; structInode *inode = file->f_path.dentry->D_inode; intFbidx = Iminor (inode);//get the secondary device number       structFb_info *info = Registered_fb[fbidx];//Get the LCD driver information for the secondary device numberu32*buffer, *DST; U32 __iomem*src; intc, I, CNT =0, err =0; unsignedLongtotal_size; ... ...       if(info->fbops->fb_read)returnInfo->fbops->Fb_read (Info, buf, count, PPOs); Total_size= info->screen_size;//Get screen length    
... buffer= Kmalloc ((Count > Page_size)? Page_size:count,gfp_kernel);//Allocating buffers if(!buffer)return-Enomem; SRC= (u32 __iomem *) (info->screen_base + p);//Get physical memory base address if(info->fbops->Fb_sync) Info->fbops->Fb_sync (info); while(count) {C= (Count > Page_size)? Page_size:count;//Get page addressDST=buffer; /*because SRC is 32 bits, one src equals 4 bytes, so page address C >> 2*/ for(i = C >>2; i--; ) *dst++ = Fb_readl (src++);//read the video memory every pixel data and put it on the DST address . if(C &3) {U8*DST8 = (U8 *) DST; U8 __iomem*src8 = (U8 __iomem *) src; for(i = C &3; i--;) *dst8++ = FB_READB (src8++); SRC= (u32 __iomem *) Src8; } if(Copy_to_user (buf, buffer, c)) {//upload data, length equals page address sizeErr= -Efault; Break; } *ppos + =C; BUF+=C; CNT+=C; Count-=C; } kfree (buffer); return(err)?err:cnt;}

As can be found from the. Open and. Write functions, all depend on the Fb_info frame buffer information structure, which is obtained from the REGISTERED_FB[FBIDX] array, which holds the information of our various LCD drivers

3. Let's find out where this array is registered, located in Register_framebuffer ():

intRegister_framebuffer (structFb_info *fb_info) { ... ... for(i =0; i < Fb_max; i++)//find an empty array        if(!Registered_fb[i]) Break; Fb_info->node =i; ... .../*Create device node, name is FDI, main device number is 29, secondary device number is I*/Fb_info->dev = Device_create (Fb_class, Fb_info->device,mkdev (Fb_major, i),"fb%d", i); ... registered_fb[i]=fb_info, ...}

This register_framebuffer () in addition to registering the Fb_info, it also creates a device node

So this is called when the driver is registered, as shown in:

4. Let's see how the/DRIVERS/VIDEO/S3C2410FB.C is driven.

4.1 Find the entry Exit Function first:

int __devinit s3c2410fb_init (void) {     return platform_driver_register (& s3c2410fb_driver);} Static void __exit s3c2410fb_cleanup (void) {     platform_driver_unregister (&  S3c2410fb_driver);}

Discovery is registration, logoff device platform DRV

4.2来 See how s3c2410fb_driver is defined

Static structPlatform_driver S3c2410fb_driver ={. Probe= S3c2410fb_probe,//detection functions, registering devices. Remove= S3c2410fb_remove,//Remove Device. Suspend= S3c2410fb_suspend,//Hibernate. Resume= S3c2410fb_resume,//Wake up. Driver={. Name="S3C2410-LCD",//DRV name. Owner=This_module,},};

As with the platform mechanism we analyzed in the previous section, when the device matches successfully, enter the probe function and initialize the drive device

4.3来 look at the. Probe function, how to implement the driver

Static int__init S3c2410fb_probe (structPlatform_device *Pdev) {       structS3c2410fb_info *info; structFb_info *Fbinfo; structS3C2410FB_HW *Mregs; intret; intIRQ; inti;        U32 Lcdcon1; Mach_info= pdev->dev.platform_data;//Get LCD device information (length, width, type, etc.)       if(Mach_info = =NULL) {Dev_err (&pdev->dev,"no platform data for LCD, cannot attach\n"); return-EINVAL; } Mregs= &mach_info->regs; IRQ= Platform_get_irq (Pdev,0); if(IRQ <0) {Dev_err (&pdev->dev,"no IRQ for device\n"); return-ENOENT; } fbinfo= Framebuffer_alloc (sizeof(structS3c2410fb_info), &pdev->dev);//1. Assigning a fb_info structure       if(!fbinfo) {              return-Enomem; }      /*2. Set Fb_info*/Info= fbinfo->par; Info-&GT;FB =Fbinfo; Info->dev = &pdev->Dev; ... ...    /*3. Hardware-related operation, set interrupt, LCD clock frequency, memory address, configuration pin ...*/ret= REQUEST_IRQ (IRQ, S3C2410FB_IRQ, irqf_disabled, pdev->name, info);//setting interruptsInfo-&GT;CLK = Clk_get (NULL,"LCD");//Get Clockclk_enable (Info-&GT;CLK);//Enable Clockret= S3c2410fb_map_video_memory (info);//memory Addressret= S3c2410fb_init_registers (info);//setting registers, configuring Pins       ... ...
RET= Register_framebuffer (Fbinfo);//4. Register a FB_INFO structure if(Ret <0) {PRINTK (Kern_err"Failed to register framebuffer device:%d\n", ret); Gotofree_video_memory; }... ...returnret;}

4.4 Obviously to write an LCD driver, you need the following 4 steps:

1) Assign a FB_INFO structure: Framebuffer_alloc ();

2) Set Fb_info

3) Hardware-related operations (set interrupt, LCD clock frequency, memory address, configuration pin ...)

4 Registered Fb_info:register_framebuffer ()

How to write the LCD driver in the next section

15.LINUX-LCD Hierarchy Analysis (detailed)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.