Linux Platform devices 平台设备驱动

设备总线驱动模型:http://blog.csdn.net/lizuobin2/article/details/51570196

    本文主要参考:http://www.wowotech.net/device_model/platform_device.html


    platform平台设备驱动是基于设备总线驱动模型的,它只不过是将 device 进一步封装成为 platform_device,将 device_driver 进一步封装成为 platform_device_driver,前面已经分析过设备总线驱动模型,关于device 与 device_driver 的注册过程以及它们在sysfs文件系统中的层次关系就不在分析,本文重点分析platform平台设备驱动与设备总线驱动模型相比较新增添的那些东西。


    在Linux设备模型的抽象中,存在着一类称作“Platform Device”的设备,内核是这样描述它们的(Documentation/driver-model/platform.txt):

    Platform devices are devices that typically appear as autonomous entities in the system. This includes legacy port-based devices and host bridges to peripheral buses, and most controllers integrated into system-on-chip platforms.  What they usually have in common is direct addressing from a CPU bus.  Rarely, a platform_device will be connected through a segment of some other kind of bus; but its registers will still be directly addressable.

   

     概括来说,Platform设备包括:基于端口的设备(已不推荐使用,保留下来只为兼容旧设备,legacy);连接物理总线的桥设备;集成在SOC平台上面的控制器;连接在其它bus上的设备(很少见)。等等。
    这些设备有一个基本的特征:可以通过CPU bus直接寻址(例如在嵌入式系统常见的“寄存器”)。因此,由于这个共性,内核在设备模型的基础上(device和device_driver),对这些设备进行了更进一步的封装,抽象出paltform bus、platform device和platform driver,以便驱动开发人员可以方便的开发这类设备的驱动。
    可以说,paltform设备对Linux驱动工程师是非常重要的,因为我们编写的大多数设备驱动,都是为了驱动plaftom设备。


platform_bus_type
    我们知道,在设备总线驱动模型的中,BUS像一个月老一样,通过它的match函数,将注册到bus中的device与driver进行配对,那么每一个不同的bus 都有自己的match函数,我们来看看platform_bus_type.

[cpp] view plain copy
  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. };  
[cpp] view plain copy
  1. static int platform_match(struct device *dev, struct device_driver *drv)  
  2. {  
  3.     struct platform_device *pdev = to_platform_device(dev);  
  4.     struct platform_driver *pdrv = to_platform_driver(drv);  
  5.   
  6.     /* match against the id table first */  
  7.     if (pdrv->id_table)  
  8.         return platform_match_id(pdrv->id_table, pdev) != NULL;  
  9.   
  10.     /* fall-back to driver name match */  
  11.     return (strcmp(pdev->name, drv->name) == 0);  
  12. }  

    如果platform_device_driver中定义了id_table,则调用 platform_match_id 进行匹配

    举个例子:

[cpp] view plain copy
  1. static struct platform_device_id s3c24xx_driver_ids[] = {  
  2.     {  
  3.         .name       = "s3c2410-i2c",  
  4.         .driver_data    = TYPE_S3C2410,  
  5.     }, {  
  6.         .name       = "s3c2440-i2c",  
  7.         .driver_data    = TYPE_S3C2440,  
  8.     }, { },  
  9. };  
[cpp] view plain copy
  1. struct platform_device s3c_device_i2c0 = {  
  2.     .name         = "s3c2410-i2c",  
  3. #ifdef CONFIG_S3C_DEV_I2C1  
  4.     .id       = 0,  
  5. #else  
  6.     .id       = -1,  
  7. #endif  
  8.     .num_resources    = ARRAY_SIZE(s3c_i2c_resource),  
  9.     .resource     = s3c_i2c_resource,  
  10. };  
[cpp] view plain copy
  1. static const struct platform_device_id *platform_match_id(struct platform_device_id *id, struct platform_device *pdev)  
  2. {  
  3.     while (id->name[0]) {  
  4.         if (strcmp(pdev->name, id->name) == 0) {  
  5.             pdev->id_entry = id;  
  6.             return id;  
  7.         }  
  8.         id++;  
  9.     }  
  10.     return NULL;  
  11. }  
    显然,platform_match_id 的作用就是遍历整个 Id_table 数组,寻找是否有与 platform_device->name 同名的,如果有,则返回这个 Platform_device_id ,使用Id_table 打破了原本设备总线驱动模型,一个 device 只能用与一个 device_driver 配对的局限性。现在一个platform_device_driver 可以与多个platform_device配对。

    如果没有,则只是根据 platform_device_driver->name 与 platform_device->name 进行比较,这也就是老师为啥在写平台设备驱动程序的时候经常说,“将驱动注册到内核中去,如果有同名设备,则调用driver->probe函数....”。


pletform_device 中的 id 的作用:

    if (pdev->id != -1)      /* 如果不是-1 对name编号 */  
        dev_set_name(&pdev->dev, "%s.%d", pdev->name,  pdev->id);  
    else                             /* -1时直接是名字 */
        dev_set_name(&pdev->dev, pdev->name); 


从device封装而来的platform_device

[cpp] view plain copy
  1. struct platform_device {  
  2.     const char  * name;  
  3.     int     id;  
  4.     struct device   dev;  
  5.     u32     num_resources;  
  6.     struct resource * resource;  
  7.   
  8.     struct platform_device_id   *id_entry;  
  9.   
  10.     /* arch specific additions */  
  11.     struct pdev_archdata    archdata;  
  12. };    
    name,设备的名称,该名称在设备注册时,会拷贝到dev.init_name中。
    dev,真正的设备,通过 container_of ,就能找到整个platform_device ,访问其它成员,如后面要提到的 resource 
    num_resources、resource,该设备的资源描述,由struct resource(include/linux/ioport.h)结构抽象。 
    在Linux中,系统资源包括I/O、Memory、Register、IRQ、DMA、Bus等多种类型。这些资源大多具有独占性,不允许多个设备同时使用,因此Linux内核提供了一些API,用于分配、管理这些资源。 
    当某个设备需要使用某些资源时,只需利用struct resource组织这些资源(如名称、类型、起始、结束地址等),并保存在该设备的resource指针中即可。然后在设备probe时,设备需求会调用资源管理接口,分配、使用这些资源。而内核的资源管理逻辑,可以判断这些资源是否已被使用、是否可被使用等等。

[cpp] view plain copy
  1. struct resource {  
  2.     resource_size_t start;  
  3.     resource_size_t end;  
  4.     const char *name;  
  5.     unsigned long flags;  
  6.     struct resource *parent, *sibling, *child;  
  7. };  
[cpp] view plain copy
  1. static struct resource led_resource[] = {   //jz2440的参数,驱动未测试   
  2.     [0] = {  
  3.         .start = 0x56000010,  
  4.         .end   = 0x56000010 + 8 - 1,  
  5.         .flags = IORESOURCE_MEM,  
  6.     },  
  7.     [1] = {  
  8.         .start = 5,  
  9.         .end   = 5,  
  10.         .flags = IORESOURCE_IRQ,  
  11.     },  
  12. };  
  13. static struct platform_device led_dev = {  
  14.     .name = "myled",    //设备名字 与 驱动相匹配  
  15.     .id   = -1,  
  16.     .num_resources = ARRAY_SIZE(led_resource),  
  17.     .resource = led_resource,  
  18.       
  19.     .dev = {  
  20.         .release = led_release,  
  21.         //.devt = MKDEV(252, 1),  
  22.     },  
  23. };  


从 device_driver 封装而来的platform_device_dirver

[cpp] view plain copy
  1. struct platform_driver {  
  2.     int (*probe)(struct platform_device *);  
  3.     int (*remove)(struct platform_device *);  
  4.     void (*shutdown)(struct platform_device *);  
  5.     int (*suspend)(struct platform_device *, pm_message_t state);  
  6.     int (*resume)(struct platform_device *);  
  7.     struct device_driver driver;  
  8.     struct platform_device_id *id_table;  
  9. };  
[cpp] view plain copy
  1. int platform_driver_register(struct platform_driver *drv)  
  2. {  
  3.     drv->driver.bus = &platform_bus_type;  
  4.     if (drv->probe)  
  5.         drv->driver.probe = platform_drv_probe;  
  6.     if (drv->remove)  
  7.         drv->driver.remove = platform_drv_remove;  
  8.     if (drv->shutdown)  
  9.         drv->driver.shutdown = platform_drv_shutdown;  
  10.   
  11.     return driver_register(&drv->driver);  
  12. }  
    struct platform_driver结构和struct device_driver非常类似,上边的platform_drv_probe、platform_drv_remove、platform_drv_shutdown,只不过稍作转换调用platform_driver中的probe、remove、shutdown函数,举个例子稍微看一下

[cpp] view plain copy
  1. static int platform_drv_probe(struct device *_dev)  
  2. {  
  3.     struct platform_driver *drv = to_platform_driver(_dev->driver);  
  4.     struct platform_device *dev = to_platform_device(_dev);  
  5.   
  6.     return drv->probe(dev);  
  7. }  


Platform Device提供的API
[cpp] view plain copy
  1. /* include/linux/platform_device.h */  
  2. extern int platform_device_register(struct platform_device *);  
  3. extern void platform_device_unregister(struct platform_device *);  
  4.    
  5. extern void arch_setup_pdev_archdata(struct platform_device *);  
  6. extern struct resource *platform_get_resource(struct platform_device *, unsigned int, unsigned int);  
  7. extern int platform_get_irq(struct platform_device *, unsigned int);  
  8. extern struct resource *platform_get_resource_byname(struct platform_device *, unsigned intconst char *);  
  9. extern int platform_get_irq_byname(struct platform_device *, const char *);  
  10. extern int platform_add_devices(struct platform_device **, int);  
  11.    
  12. extern struct platform_device *platform_device_register_full(const struct platform_device_info *pdevinfo);  
  13.    
  14. static inline struct platform_device *platform_device_register_resndata(  
  15.                 struct device *parent, const char *name, int id,  
  16.                 const struct resource *res, unsigned int num,  
  17.                 const void *data, size_t size)  
  18.    
  19. static inline struct platform_device *platform_device_register_simple(  
  20.                 const char *name, int id,  
  21.                 const struct resource *res, unsigned int num)  
  22.    
  23. static inline struct platform_device *platform_device_register_data(  
  24.                 struct device *parent, const char *name, int id,  
  25.                 const void *data, size_t size)  
  26.    
  27. extern struct platform_device *platform_device_alloc(const char *name, int id);  
  28. extern int platform_device_add_resources(struct platform_device *pdev,  
  29.                                          const struct resource *res,  
  30.                                          unsigned int num);  
  31. extern int platform_device_add_data(struct platform_device *pdev,  
  32.                                     const void *data, size_t size);  
  33. extern int platform_device_add(struct platform_device *pdev);  
  34. extern void platform_device_del(struct platform_device *pdev);  
  35. extern void platform_device_put(struct platform_device *pdev);  
    platform_device_register、platform_device_unregister,Platform设备的注册/注销接口,和底层的device_register等接口类似。
    arch_setup_pdev_archdata,设置platform_device变量中的archdata指针。
    platform_get_resource、platform_get_irq、platform_get_resource_byname、platform_get_irq_byname,通过这些接口,可以获取platform_device变量中的resource信息,以及直接获取IRQ的number等等。
    platform_device_register_full、platform_device_register_resndata、platform_device_register_simple、platform_device_register_data,其它形式的设备注册。调用者只需要提供一些必要的信息,如name、ID、resource等,Platform模块就会自动分配一个struct platform_device变量,填充内容后,注册到内核中。
    platform_device_alloc,以name和id为参数,动态分配一个struct platform_device变量。
    platform_device_add_resources,向platform device中增加资源描述。
    platform_device_add_data,向platform device中添加自定义的数据(保存在pdev->dev.platform_data指针中)。
    platform_device_add、platform_device_del、platform_device_put,其它操作接口。

Platform Driver提供的API
    platform_driver_registe、platform_driver_unregister,platform driver的注册、注销接口。
    platform_driver_probe,主动执行probe动作。
    platform_set_drvdata、platform_get_drvdata,设置或者获取driver保存在device变量中的私有数据。

懒人API
[cpp] view plain copy
  1. extern struct platform_device *platform_create_bundle(  
  2.       struct platform_driver *driver, int (*probe)(struct platform_device *),  
  3.       struct resource *res, unsigned int n_res,  
  4.       const void *data, size_t size);  
    只要提供一个platform_driver(要把driver的probe接口显式的传入),并告知该设备占用的资源信息,platform模块就会帮忙分配资源,并执行probe操作。对于那些不需要热拔插的设备来说,这种方式是最省事的了。  


简单一例:

    开发板:Mini2440

    内核版本:2.6.32.2

[cpp] view plain copy
  1. #include <linux/module.h>  
  2. #include <linux/kernel.h>  
  3. #include <linux/fs.h>  
  4. #include <linux/init.h>  
  5. #include <linux/device.h>  
  6. #include <linux/interrupt.h>  
  7. #include <linux/sched.h>   
  8. #include <linux/irq.h>  
  9. #include <asm/uaccess.h>  
  10. #include <linux/input.h>  
  11. #include <linux/platform_device.h>  
  12. // 设备资源  
  13. static struct resource led_resource[] = {   //jz2440的参数,驱动未测试   
  14.     [0] = {  
  15.         .start = 0x56000010,  
  16.         .end   = 0x56000010 + 8 - 1,  
  17.         .flags = IORESOURCE_MEM,  
  18.     },  
  19.     [1] = {  
  20.         .start = 5,  
  21.         .end   = 5,  
  22.         .flags = IORESOURCE_IRQ,  
  23.     },  
  24. };  
  25.   
  26. static void led_release(struct device *dev){  
  27.   
  28. }  
  29.   
  30. // 创建一个设备  
  31. static struct platform_device led_dev = {  
  32.     .name = "myled",    //设备名字 与 驱动相匹配  
  33.     .id   = -1,  
  34.     .num_resources = ARRAY_SIZE(led_resource),  
  35.     .resource = led_resource,  
  36.       
  37.     .dev = {  
  38.         .release = led_release,  
  39.         //.devt = MKDEV(252, 1),  
  40.     },  
  41. };  
  42.   
  43. static int led_dev_init(void){  
  44.   
  45.     //向bus注册led_dev match drv链表进行配对  
  46.     platform_device_register(&led_dev);  
  47.     return 0;  
  48. }  
  49.   
  50. static void led_dev_exit(void){  
  51.     platform_device_unregister(&led_dev);  
  52. }  
  53.   
  54. module_init(led_dev_init);  
  55. module_exit(led_dev_exit);  
  56. MODULE_LICENSE("GPL");  
  57.       

[cpp] view plain copy
  1. #include <linux/module.h>  
  2. #include <linux/kernel.h>  
  3. #include <linux/fs.h>  
  4. #include <linux/init.h>  
  5. #include <linux/device.h>  
  6. #include <linux/interrupt.h>  
  7. #include <linux/sched.h>   
  8. #include <linux/irq.h>  
  9. #include <asm/uaccess.h>  
  10.   
  11. #include <linux/platform_device.h>  
  12. #include <linux/io.h>  
  13.   
  14. static int major;  
  15.   
  16. static struct class *cls;  
  17. static struct device *dev;  
  18.   
  19. static volatile unsigned long *gpio_con;  
  20. static volatile unsigned long *gpio_dat;  
  21. static int pin;  
  22.   
  23. static int led_open(struct inode *inode, struct file *file){  
  24.   
  25.     *gpio_con &= ~(0x03 << (pin*2));  
  26.     *gpio_con |=  (0x01 << (pin*2));  
  27.     return 0;  
  28. }  
  29.   
  30. static ssize_t led_write(struct file *file, const char __user *buf,  
  31.     size_t count, loff_t *ppos){  
  32.   
  33.     int val;  
  34.     copy_from_user(&val, buf, count);  
  35.   
  36.     if(val == 1){  
  37.           
  38.         *gpio_dat &= ~(1<<pin);  
  39.     }else{  
  40.       
  41.         *gpio_dat &=  (1<<pin);  
  42.     }  
  43.   
  44.     return 0;  
  45. }  
  46.   
  47. static struct file_operations led_fops = {  
  48.   
  49.     .owner = THIS_MODULE,  
  50.     .open  = led_open,  
  51.     .write = led_write,  
  52. };  
  53.   
  54. static int led_probe(struct platform_device *pdev){  
  55.   
  56.     struct resource *res;  
  57.     // 最后一个参数 0 表示第1个该类型的资源  
  58.     res = platform_get_resource(pdev, IORESOURCE_MEM, 0);  
  59.     gpio_con = ioremap(res->start, res->end - res->start + 1);  
  60.     gpio_dat = gpio_con + 1;  
  61.   
  62.     res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);  
  63.     pin = res->start;  
  64.   
  65.     printk("led_probe, found led ");  
  66.   
  67.     // 注册设备驱动 创建设备节点  
  68.     major = register_chrdev(0, "myled", &led_fops);  
  69.     // 创建类  
  70.     cls = class_create(THIS_MODULE, "myled");  
  71.     // 创建设备节点  
  72.     dev = device_create(cls, NULL, MKDEV(major, 0), NULL, "led");  
  73.   
  74.     return 0;  
  75. }  
  76.   
  77. static int led_remove(struct platform_device *pdev){  
  78.   
  79.     printk("led_remove, remove led ");   
  80.     // 删除设备节点  
  81.     device_unregister(dev);  
  82.     // 销毁类  
  83.     class_destroy(cls);  
  84.     // 取消注册设备驱动  
  85.     unregister_chrdev(major, "myled");  
  86.     // 取消内存映射  
  87.     iounmap(gpio_con);  
  88.   
  89.     return 0;  
  90. }  
  91.   
  92. struct platform_driver led_drv = {  
  93.   
  94.     .probe  = led_probe,    //匹配到dev之后调用probe  
  95.     .remove = led_remove,  
  96.     .driver = {  
  97.         .name = "myled",  
  98.     },  
  99. };  
  100.   
  101. static int led_drv_init(void){  
  102.   
  103.     platform_driver_register(&led_drv);  
  104.     return 0;  
  105. }  
  106.   
  107. static void led_drv_exit(void){  
  108.       
  109.     platform_driver_unregister(&led_drv);  
  110. }  
  111.   
  112. module_init(led_drv_init);  
  113. module_exit(led_drv_exit);  
  114. MODULE_LICENSE("GPL");  


转自:http://blog.csdn.net/lizuobin2/article/details/51607813

原文地址:https://www.cnblogs.com/alan666/p/8311851.html