ID匹配和设备树匹配
一、ID匹配之框架代码
id匹配(可想象成八字匹配):一个驱动可以对应多个设备 ------优先级次低
注意事项:
- device模块中,id的name成员必须与struct platform_device中的name成员内容一致,因此device模块中,struct platform_device中的name成员必须指定
- driver模块中,struct platform_driver成员driver的name成员必须指定,但与device模块中name可以不相同
hello_device.c
/*platform device框架*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/platform_device.h>
//定义资源数组
static void device_release(struct device *dev)
{
printk("platform: device release\n");
}
struct platform_device_id test_id = {
.name = "test_device",
};
struct platform_device test_device = {
.name = "test_device",//必须初始化
.dev.release = device_release,
.id_entry = &test_id,
};
static int __init platform_device_init(void)
{
platform_device_register(&test_device);
return 0;
}
static void __exit platform_device_exit(void)
{
platform_device_unregister(&test_device);
}
module_init(platform_device_init);
module_exit(platform_device_exit);
MODULE_LICENSE("Dual BSD/GPL");
hello_driver.c
/*platform driver框架*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/platform_device.h>
static int driver_probe(struct platform_device *dev)
{
printk("platform: match ok!\n");
return 0;
}
static int driver_remove(struct platform_device *dev)
{
printk("platform: driver remove\n");
return 0;
}
struct platform_device_id testdrv_ids[] =
{
[0] = {.name = "test_device"},
[1] = {.name = "abcxyz"},
[2] = {}, //means ending
};
struct platform_driver test_driver = {
.probe = driver_probe,
.remove = driver_remove,
.driver = {
.name = "xxxxx", //必须初始化
},
.id_table = testdrv_ids,
};
static int __init platform_driver_init(void)
{
platform_driver_register(&test_driver);
return 0;
}
static void __exit platform_driver_exit(void)
{
platform_driver_unregister(&test_driver);
}
module_init(platform_driver_init);
module_exit(platform_driver_exit);
MODULE_LICENSE("Dual BSD/GPL");
用到结构体数组,一般不指定大小,初始化时最后加{}表示数组结束
设备中增加资源,驱动中访问资源
二、ID匹配之led驱动
led_device.c
/*platform device框架*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/platform_device.h>
//定义资源数组
static void device_release(struct device *dev)
{
printk("platform: device release\n");
}
struct resource hello_dev_res[] =
{
[0] = {.start = 0x1000,.end = 0x1003,.name = "reg1",.flags = IORESOURCE_MEM},
[1] = {.start = 0x2000,.end = 0x2003,.name = "reg2",.flags = IORESOURCE_MEM},
[2] = {.start = 10,.end = 10,.name = "irq1",.flags = IORESOURCE_IRQ},
[3] = {.start = 0x3000,.end = 0x3003,.name = "reg3",.flags = IORESOURCE_MEM},
[4] = {.start = 100,.end = 100,.name = "irq2",.flags = IORESOURCE_IRQ},
[5] = {.start = 62,.end = 62,.name = "irq3",.flags = IORESOURCE_IRQ},
};
struct platform_device_id hello_id = { //id匹配结构体
.name = "hello",
};
struct platform_device test_device = {
.id = -1,
.name = "hello",//必须初始化
.dev.release = device_release,
.resource = hello_dev_res,
.num_resources = ARRAY_SIZE(hello_dev_res),
.id_entry = &hello_id , //id访问
};
static int __init platform_device_init(void)
{
platform_device_register(&test_device);
return 0;
}
static void __exit platform_device_exit(void)
{
platform_device_unregister(&test_device);
}
module_init(platform_device_init);
module_exit(platform_device_exit);
MODULE_LICENSE("Dual BSD/GPL");
led_driver.c
/*platform driver框架*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/platform_device.h>
static int driver_probe(struct platform_device *dev) //设备和驱动匹配成功之后调用该函数
{
struct resource *pres = NULL;
printk("platform: match ok!\n");
pres = platform_get_resource(p_pltdev,IORESOURCE_MEM,2); //2不是下标是第几个MEM资源
printk("res.start = 0x%x\n",(unsigned int)pres->start);
pres = platform_get_resource(p_pltdev,IORESOURCE_IRQ,2); //从dev里面获取信息设备信息
printk("res.start = %d\n",(int)pres->start);
return 0;
}
static int driver_remove(struct platform_device *dev) //设备卸载了调用该函数
{
printk("platform: driver remove\n");
return 0;
}
struct platform_device_id hellodrv_ids[] //id匹配结构体
{
[0] = {.name = "hello"},
[1] = {.name = "xyz"},
[2] = {},
};
struct platform_driver test_driver = //提供一些函数指针指向用户自己编写的函数
{
.driver.name = "test_device", //必须初始化
.probe = driver_probe,
.remove = driver_remove,
.id_table = hellodrv_ids, //只要是上面那个结构体的字符串都能匹配
};
static int __init platform_driver_init(void)
{
platform_driver_register(&test_driver);
return 0;
}
static void __exit platform_driver_exit(void)
{
platform_driver_unregister(&test_driver);
}
module_init(platform_driver_init);
module_exit(platform_driver_exit);
MODULE_LICENSE("Dual BSD/GPL");
三、设备树匹配
设备树匹配:内核启动时根据设备树自动产生的设备 ------ 优先级最高
注意事项:
- 无需编写device模块,只需编写driver模块
- 使用compatible属性进行匹配,注意设备树中compatible属性值不要包含空白字符
- id_table可不设置,但struct platform_driver成员driver的name成员必须设置
leddrv.c
/*platform driver框架*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/platform_device.h>
static int driver_probe(struct platform_device *dev)
{
printk("platform: match ok!\n");
return 0;
}
static int driver_remove(struct platform_device *dev)
{
printk("platform: driver remove\n");
return 0;
}
struct platform_device_id testdrv_ids[] =
{
[0] = {.name = "test_device"},
[1] = {.name = "abcxyz"},
[2] = {}, //means ending
};
struct of_device_id test_of_ids[] =
{
[0] = {.compatible = "xyz,abc"},
[1] = {.compatible = "qwe,opq"},
[2] = {},
};
struct platform_driver test_driver = {
.probe = driver_probe,
.remove = driver_remove,
.driver = {
.name = "xxxxx", //必须初始化
.of_match_table = test_of_ids,
},
};
static int __init platform_driver_init(void)
{
platform_driver_register(&test_driver);
return 0;
}
static void __exit platform_driver_exit(void)
{
platform_driver_unregister(&test_driver);
}
module_init(platform_driver_init);
module_exit(platform_driver_exit);
MODULE_LICENSE("Dual BSD/GPL");
四、设备树匹配之led驱动
leddrv.c
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/platform_device.h>
#include <linux/fs.h>
#include <linux/gpio.h>
#include <linux/of_gpio.h>
#include <linux/cdev.h>
#include <linux/wait.h>
#include <linux/sched.h>
#include <linux/poll.h>
#include <linux/slab.h>
#include <linux/mm.h>
#include <linux/io.h>
#include <linux/of.h>
#include <asm/uaccess.h>
#include <asm/atomic.h>
#include "led_dev.h"
int major = 11, minor = 0, num = 1;
struct myled_dev
{
struct cdev mydev;
unsigned int led2gpio;
unsigned int led3gpio;
unsigned int led4gpio;
unsigned int led5gpio;
};
struct myled_dev *pgmydev = NULL;
int myopen(struct inode *pnode, struct file * pfile)
{
pfile->private_data = (void *) (container_of(pnode->i_cdev, struct myled_dev, mydev));
return 0;
}
int myclose(struct inode *pnode, struct file * pfile)
{
return 0;
}
void led_on(struct myled_dev *pmydev,int ledno)
{
switch(ledno)
{
case 2:
gpio_set_value(pmydev->led2gpio, 1);
break;
case 3:
gpio_set_value(pmydev->led3gpio, 1);
break;
case 4:
gpio_set_value(pmydev->led4gpio, 1);
break;
case 5:
gpio_set_value(pmydev->led5gpio, 1);
break;
}
}
void led_off(struct myled_dev *pmydev,int ledno)
{
switch(ledno)
{
case 2:
gpio_set_value(pmydev->led2gpio, 0);
break;
case 3:
gpio_set_value(pmydev->led3gpio, 0);
break;
case 4:
gpio_set_value(pmydev->led4gpio, 0);
break;
case 5:
gpio_set_value(pmydev->led5gpio, 0);
break;
}
}
long myled_ioctl(struct file *pfile, unsigned int cmd, unsigned long arg)
{
struct myled_dev *pmydev = (struct myled_dev *) pfile->private_data;
if(arg < 2 || arg > 5)
{
return -1;
}
switch (cmd)
{
case MY_LED_ON:
led_on(pmydev,arg);
break;
case MY_LED_OFF:
led_off(pmydev,arg);
break;
default:
return -1;
}
return 0;
}
void request_leds_gpio(struct myled_dev *pmydev, struct device_node *pnode)
{
pmydev->led2gpio = of_get_named_gpio(pnode, "led2-gpio", 0);
gpio_request(pmydev->led2gpio, "led2");
pmydev->led3gpio = of_get_named_gpio(pnode, "led3-gpio", 0);
gpio_request(pmydev->led3gpio, "led3");
pmydev->led4gpio = of_get_named_gpio(pnode, "led4-gpio", 0);
gpio_request(pmydev->led4gpio, "led4");
pmydev->led5gpio = of_get_named_gpio(pnode, "led5-gpio", 0);
gpio_request(pmydev->led5gpio, "led5");
}
void free_leds_gpio(struct myled_dev *pmydev)
{
gpio_free(pmydev->led2gpio);
gpio_free(pmydev->led3gpio);
gpio_free(pmydev->led4gpio);
gpio_free(pmydev->led5gpio);
}
void set_leds_gpio_output(struct myled_dev *pmydev)
{
gpio_direction_output(pmydev->led2gpio, 0);
gpio_direction_output(pmydev->led3gpio, 0);
gpio_direction_output(pmydev->led4gpio, 0);
gpio_direction_output(pmydev->led5gpio, 0);
}
struct file_operations myops = {
.owner = THIS_MODULE,
.open = myopen,
.release = myclose,
.unlocked_ioctl = myled_ioctl,
};
int leds_probe(struct platform_device *pdev)
{
int ret = 0;
dev_t devno = MKDEV(major,minor);
struct device_node *pnode = NULL;
ret = register_chrdev_region(devno, num, "myled");
if(ret)
{
ret = alloc_chrdev_region(&devno, minor, num, "myled");
if(ret)
{
printk("devno failed.\n");
return -1;
}
major = MAJOR(devno);
minor = MINOR(devno);
}
pgmydev = (struct myled_dev *) kmalloc(sizeof(struct myled_dev), GFP_KERNEL);
if(pgmydev == NULL)
{
unregister_chrdev_region(devno, num);
printk("kmalloc failed.\n");
return -1;
}
memset(pgmydev,0,sizeof(struct myled_dev));
cdev_init(&pgmydev->mydev,&myops);
pgmydev->mydev.owner = THIS_MODULE;
cdev_add(&pgmydev->mydev,devno,num);
pnode = pdev->dev.of_node;
/*ioremap*/
request_leds_gpio(pgmydev,pnode);
/*set gpio output*/
set_leds_gpio_output(pgmydev);
return 0;
}
int leds_remove(struct platform_device *pdev)
{
dev_t devno = MKDEV(major,minor);
free_leds_gpio(pgmydev);
cdev_del(&pgmydev->mydev);
unregister_chrdev_region(devno, num);
kfree(pgmydev);
pgmydev = NULL;
}
struct of_device_id leds_of_dev[] =
{
[0] = {.compatible = "fs4412,led2-5"},
[1] = {},
};
struct platform_driver leds_drv = {
.probe = leds_probe,
.remove = leds_remove,
.driver = {
.name = "xyz",
.of_match_table = leds_of_dev,
},
};
int __init myled_init(void)
{
platform_driver_register(&leds_drv);
return 0;
}
void __exit myled_exit(void)
{
platform_driver_unregister(&leds_drv);
}
MODULE_LICENSE("GPL");
MODULE_AUTHOR("imysy_22");
module_init(myled_init);
module_exit(myled_exit);
五、一个编写驱动用的宏
struct platform_driver xxx = {
...
};
module_platform_driver(xxx);
//最终展开后就是如下形式:
static int __init xxx_init(void)
{
return platform_driver_register(&xxx);
}
module_init(xxx_init);
static void __exit xxx_init(void)
{
return platform_driver_unregister(&xxx);
}
module_exit(xxx_exit)
;