平台设备注册:
1 int platform_device_register(struct platform_device *pdev);
platform_device_register()会对平台设备进行相应的初始化之后调用platform_device_register()函数把它添加到子系统中。
平台设备注销:
1 void platform_device_unregister(struct platform_device *pdev);
platform_device_unregister()函数释放设备资源之后从子系统中将其移除。
平台设备模板:
1staticstructresourcexxx_resource= 2{ 3[0]= 4{ 5.start=..., 6.end=..., 7.flags=..., 8}, 9[1]= 10{ 11... 12} 13... 14}; 15 16staticstructxxx_plat_dataxxx_data= 17{ 18... 19}; 20 21staticstructplatform_devicexxx_platform_device= 22{ 23.name=NAME, 24.num_resources=ARRAY_SIZE(xxx_resource), 25.resource=xxx_resource, 26.dev= 27{ 28.platform_data=&xxx_data, 29} 30}; 31 32staticint__initxxx_device_init(void) 33{ 34... 35/*注册平台设备*/ 36platform_device_register(&xxx_platform_device); 37... 38} 39 40staticvoid__exitxxx_device_exit(void) 41{ 42... 43/*注销平台设备*/ 44platform_device_unregister(&xxx_platform_device); 45... 46} |
二、平台驱动
平台驱动结构体:
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 const struct platform_device_id *id_table;
9 };
平台驱动结构体driver成员里面的name必须与平台设备结构体里面的name成员一致,在系统注册一个设备的时候,会通过设备结构体里面的name成员和平台驱动driver里面的name成员匹配,当匹配成功则调用平台驱动的probe函数,
通常在probe函数中获取平台设备的资源和私有数据并进行设备的初始化。
获取设备资源:
1 struct resource *platform_get_resource(struct platform_device *dev, unsigned int type, unsigned int num);
platform_get_resource()函数用于获取平台设备的资源,dev为要平台设备,type为平台设备资源类型,num为平台资源号(比如同一个资源有两个则资源号为0,1)。
平台驱动注册:
1 int platform_driver_register(struct platform_driver *drv);
platform_driver_register()函数完成平台驱动的注册,在驱动模块加载时调用。
平台驱动注销:
1 void platform_driver_unregister(struct platform_driver *drv);
platform_driver_unregister()函数完成平台驱动的注销,在驱动模块卸载时调用。
平台驱动模板:
1 static int __devinit xxx_probe(struct platform_device *pdev) 2 { 3 struct xxx_plat_data *pdata = pdev->dev.platform_data; /* 获取私有数据 */ 4 platform_get_resource(pdev,xxx,x); /* 获取设备资源 */ 5 ... 6 } 7 8 static struct platform_driver xxx_platform_driver = 9 { 10 .probe = xxx_probe, 11 .remove = __devexit_p(xxx_remove), 12 .driver = 13 { 14 .name = NAME, /* 跟平台设备名一致 */ 15 ... 16 }, 17 ... 18 }; 19 20 static int __init xxx_driver_init(void) 21 { 22 ... 23 /* 驱动注册 */ 24 platform_driver_register(&xxx_platform_driver); 25 ... 26 } 27 28 static void __exit xxx_driver_exit(void) 29 { 30 ... 31 /* 驱动注销 */ 32 platform_driver_unregister(&xxx_platform_driver); 33 ... 34 } |