int platform_driver_register(struct platform_driver *drv)
注册平台driver,将platform_driver结构体挂入bus总线的drv链表。
int platform_device_register(struct platform_device * pdev)
注册平台device,将platform_device结构体挂入bus总线的dev链表。
它们的总线是平台总线drv->driver.bus = &platform_bus_type。这是一个虚拟总线,里面有.match函数.match = platform_match。
怎么决定左右两边相互支持呢?
通过比较平台dev和drv的名字,如果名字相同就能够匹配,就会调用platform_driver里的.probe函数,在.probe函数中可以做很多自己的事情。这只不过是两边建立连接的机制。
static int platform_match(struct device * dev, struct device_driver * drv){
struct platform_device *pdev = container_of(dev, struct platform_device, dev);
return (strncmp(pdev->name, drv->name, BUS_ID_SIZE) == 0); //比较两边名字
}
以点灯驱动程序为例,强制分为左右两边。左边device表示某一盏灯,想修改某一盏灯只需要修改左边,右边不变。
分配/设置/注册一个platform_device
static struct platform_device led_dev = {
.name = "led",
.id = -1,
.num_resources = ARRAY_SIZE(led_resource),
.resource = led_resource,
.dev = { .release = led_release, },
}
驱动程序里定义了平台设备,平台设备里面还有很多资源.resource,有寄存器起始地址、哪一位引脚,以后换一盏灯改这个位,换寄存器就改寄存器就好了。在入口函数里面注册一个平台设备platform_device_register(&led_dev),出口函数里卸载这个平台设备platform_device_unregister(&led_dev)。注册这个设备会做某些初始化,最终会device_add,放到平台总线里的设备链表。
int platform_device_register(struct platform_device * pdev){
device_initialize(&pdev->dev);
return platform_device_add(pdev); //将dev加入总线dev链表
}
分配/设置/注册一个platform_driver
struct platform_driver led_drv = {
.probe = led_probe,
.remove = led_remove,
.driver = {.name = "led",}
}
定义一个平台drv结构体,.name要与上面dev的.name一致,因为平台总线里面的.match函数比较的是它们的名字,如果名字相同的话就认为这个drv能够支持这个dev,就能够调用里面的probe函数,我们关心.probe函数。要写出led_probe函数,想做什么做什么。可以根据platform_device的资源进行ioremap,注册字符设备驱动程序,.remove做的相反。
程序
platform_device.c
static struct resource led_resource[] = {
[0] = {
.start = 0x56000050, //寄存器起始地址
.end = 0x56000050 + 8 - 1, //寄存器结束地址
.flags = IORESOURCE_MEM, //资源类型flag
},
[1] = {
.start = 5,
.end = 5,
.flags = IORESOURCE_IRQ,
}
};
static void led_release(struct device * dev) {}
//分配设置platform_device
static struct platform_device led_dev = {
.name = "myled", //名字
.id = -1,
.num_resources = ARRAY_SIZE(led_resource), //资源个数
.resource = led_resource, //资源
.dev = {
.release = led_release, //空的release函数
},
};
static int led_dev_init(void){
platform_device_register(&led_dev); //注册platform_device
return 0;
}
static void led_dev_exit(void){
platform_device_unregister(&led_dev); //卸载platform_device
}
module_init(led_dev_init);
module_exit(led_dev_exit);
MODULE_LICENSE("GPL");
platform_driver.c
static int major;
static struct class *cls;
static volatile unsigned long *gpio_con;
static volatile unsigned long *gpio_dat;
static int pin;
static int led_open(struct inode *inode, struct file *file){
/* 引脚配置为输出 */
*gpio_con &= ~(0x3<<(pin*2));
*gpio_con |= (0x1<<(pin*2));
return 0;
}
static ssize_t led_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos){
int val;
copy_from_user(&val, buf, count); //从用户空间取值
if (val == 1){
*gpio_dat &= ~(1<<pin); // 点灯
}
else{
*gpio_dat |= (1<<pin); // 灭灯
}
return 0;
}
static struct file_operations led_fops = {
.owner = THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
.open = led_open,
.write = led_write,
};
static int led_probe(struct platform_device *pdev){
struct resource *res;
/* 根据platform_device的资源进行ioremap */
//参数: platform_device ,资源类型 ,该类资源的第几个
res = platform_get_resource(pdev, IORESOURCE_MEM, 0); //获取平台资源IORESOURCE_MEM
gpio_con = ioremap(res->start, res->end - res->start + 1);
gpio_dat = gpio_con + 1;
res = platform_get_resource(pdev, IORESOURCE_IRQ, 0); //获取平台资源IORESOURCE_IRQ
pin = res->start;
/* 注册字符设备驱动程序 */
major = register_chrdev(0, "myled", &led_fops);
cls = class_create(THIS_MODULE, "myled");
class_device_create(cls, NULL, MKDEV(major, 0), NULL, "led"); /* /dev/led */
return 0;
}
static int led_remove(struct platform_device *pdev){
/* 卸载字符设备驱动程序 , iounmap */
class_device_destroy(cls, MKDEV(major, 0));
class_destroy(cls);
unregister_chrdev(major, "myled");
iounmap(gpio_con);
return 0;
}
/* 分配/设置platform_driver */
struct platform_driver led_drv = {
.probe = led_probe,
.remove = led_remove,
.driver = {
.name = "myled", //与platform_device的名字一致时调用.probe函数
}
};
static int led_drv_init(void){
platform_driver_register(&led_drv); //注册platform_driver
return 0;
}
static void led_drv_exit(void){
platform_driver_unregister(&led_drv); //卸载platform_driver
}
module_init(led_drv_init);
module_exit(led_drv_exit);
MODULE_LICENSE("GPL");