Linux Drive (ix) platform driver model, and led driver based on platform drive model

Source: Internet
Author: User

Reference:

http://blog.csdn.net/qq_28992301/article/details/52385518

http://blog.csdn.net/zoe6553/article/details/6372445

Http://blog.chinaunix.net/uid-25014876-id-111745.html

1: What is the platform bus?
Platform bus is different from the physical bus USB, I²c, SPI, pic bus virtual bus, some USB device location needs to be addressed via the USB bus,

And some similar to the SOC internal peripherals such as the LED watchdog timer is directly addressed through the memory address space, the CPU communicates with these devices do not need the bus, the 2.6 kernel to

Unified management of all devices, through Kset, kobject to establish a hierarchical relationship, these directly through memory addressing the device virtual a bus-platform bus, on the hardware

Actually there is no this bus; platform kernel pure software bus, all the direct memory addressing devices are mapped to this bus;

Advantages of the 2:platform bus

A: Can be through the platform bus, you can traverse all the platform bus equipment, platform nature is Kset, kobject, with kobject characteristics

B: To achieve the separation between the device and the drive, through the platform bus, the device and the driver is separate registration, through the platform bus probe to detect and match the device at any time, such as matching on the device driver registration;

C: Due to the above advantages, a driver can be used for several similar devices;

3:platform Bus and platform bus device driver implementation process

A:platform Bus Registration

B:platform_device Registration

C:platform_driver Registration

D: Device-to-drive matching

E: Registration of the driver

The workflow of the platform bus is as follows:

--------------------------------------------------------------------------------------------------------------- ---------------------------------------------------

1: According to the above process, we will analyze the specific code:
Platform Bus registration: Platform registration is a Linux kernel engineer has been set up; focus on. match = Platform_match function; Platform_driver and PLATFORM_ Device is through this function

to match.

1 struct bus_type platform_bus_type = {2     . Name        "platform"  ,3     . Dev_attrs    = platform_dev_attrs,4     . Match        = Platform_match,5     . uevent        = platform_uevent,6     . PM        = &platform_dev_pm_ops,7 };
1 int__init Platform_bus_init (void)2 {3     interror;4 5 Early_platform_cleanup ();6 7Error = Device_register (&Platform_bus);8     if(Error)9         returnerror;TenError = Bus_register (&platform_bus_type); One     if(Error) ADevice_unregister (&Platform_bus); -     returnerror; -}

1 Static intPlatform_match (structDevice *dev,structDevice_driver *DRV)2 {3     structPlatform_device *pdev =To_platform_device (dev);4     structPlatform_driver *pdrv =To_platform_driver (DRV);5 6     /*match against the ID table first*/7     if(pdrv->id_table)8         returnPLATFORM_MATCH_ID (pdrv->id_table, pdev)! =NULL;9 Ten     /*Fall-back to driver name match*/ One     return(strcmp (pdev->name, drv->name) = =0); A}

By the PLATFORM_MATCH_ID function to match, if id_table is not empty, then through Id_table to Pdev_name match, if empty, then Drv->name and Pdev->name to match,

The probe function is executed after the match, and this function registers the driver of the device;

--------------------------------------------------------------------------------------------------------------- ------------------------------------------------

2:platform_device's Registration

In the arch/arm/mach-s3c2440/mach-mini2440.c file

Notice here. Name,. Dev.platform_data these two variables

Platform_driver and Platform_device are matched by name. Name is matched on the same;

. Dev.platform_data this element is in the content is name, Gpio flag Def_trigger four elements

1 Static structPlatform_device mini2440_led1 = {2. Name ="s3c24xx_led",3. ID =1,4. Dev = {5. Platform_data = &Mini2440_led1_pdata,6     },7 };8 9 Static structPlatform_device Mini2440_led2 = {Ten. Name ="s3c24xx_led", One. ID =2, A. Dev = { -. Platform_data = &Mini2440_led2_pdata, -     }, the};

After setting up the platform_device structure, we can register the Platform_device device and put the PLATFORM_DEVICE structure that we set up to mini2440 the structure array pointer.

1 Static structPlatform_device *mini2440_devices[] __initdata = {2&S3C_DEVICE_OHCI,3&S3C_DEVICE_WDT,4&s3c_device_i2c0,5&S3C_DEVICE_RTC,6&S3c_device_usbgadget,7&Mini2440_device_eth,8&mini2440_led1,9&Mini2440_led2,Ten&Mini2440_led3, One&Mini2440_led4, A&Mini2440_button_device, -&S3c_device_nand, -&S3C_DEVICE_SDI, the&S3c_device_iis, -&Mini2440_audio, -};

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

Under the Mini2440_init function

Platform_add_devices (Mini2440_devices, Array_size (mini2440_devices));

Use the Platform_add_devices function to register all mini2440 devices in the kernel, and the kernel will automatically find Platform_device linked lists and platform_driver linked lists. The probe function of Platform_driver is executed automatically after match.

In the process of tidying up the Platform_device:

1: Set Platform_device structure (key for LED driver is name, dev->platform_data two elements)

2: Initialize the good dev->platform_data structure, here is mainly related to the LED driver to use the GPIO,

Here we can see the Linux kernel Platform drive framework design idea: First device and drive are separate, similar devices have common parts, different parts, different parts in the initialization is set up, the common part of the kernel engineer and set up a good And then in a matching function if the kernel linked list device matches the drive list driver, the driver will be installed automatically, otherwise the driver will not be installed;

3: Add the Set Platform_device device to the Mini2440_devices

4: In the Mini2440_device initialization of the time through the Platform_add_devices function to register the platform device, after registration and then/sys/bus/platform/ Dev.name folder will be seen under the devices directory

--------------------------------------------------------------------------------------------------------------- ------------------------------------------

3:platform_driver's Registration

1 structPlatform_driver {2     int(*probe) (structPlatform_device *);3     int(*remove) (structPlatform_device *);4     void(*shutdown) (structPlatform_device *);5     int(*suspend) (structPlatform_device *, pm_message_t state);6     int(*resume) (structPlatform_device *);7     structDevice_driver driver;8     Const structPLATFORM_DEVICE_ID *id_table;9};
1 Static structPlatform_driver S3c24xx_led_driver = {2. Probe =S3c24xx_led_probe,3. remove =S3c24xx_led_remove,4. Driver = {5. Name ="s3c24xx_led",6. Owner =This_module,7     },8 };9 Ten Static int__init S3c24xx_led_init (void) One { A     returnPlatform_driver_register (&s3c24xx_led_driver); -}

Set up a good platform_driver structure, use platform_driver_register registration can, here is the key is probe, remove, driver.name three variables;

Platform_driver_register Use this function to register the folder that will see Dev.name in the/sys/bus/platform/drivers directory later

The kernel automatically detects matches and then executes the probe function automatically;

--------------------------------------------------------------------------------------------------------------- -----------------------------------------------------

The following analysis of the probe function

What should the probe function do?

--------------------------------------------------------------------------------------------------------------- -----------------------------------------------------

Code Combat:

Linux Drive (ix) platform driver model, and led driver based on platform drive model

Related Article

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.