Linux驱动开发--平台总线id和设备树匹配

本文详细介绍了Linux内核中ID匹配和设备树匹配在平台设备驱动开发中的应用。从框架代码示例到LED驱动的实现,展示了如何进行驱动与设备的绑定,以及如何利用设备树自动化匹配。同时,提到了在不同匹配方式下的注意事项,如driver的name成员设定和compatible属性的使用。
摘要由CSDN通过智能技术生成

目录

一、ID匹配之框架代码

二、ID匹配之led驱动

三、设备树匹配

四、设备树匹配之led驱动

五、一个编写驱动用的宏


一、ID匹配之框架代码

id匹配(可想象成八字匹配):一个驱动可以对应多个设备 ------优先级次低

注意事项:

  1. device模块中,id的name成员必须与struct platform_device中的name成员内容一致,因此device模块中,struct platform_device中的name成员必须指定
  2. driver模块中,struct platform_driver成员driver的name成员必须指定,但与device模块中name可以不相同
/*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");
/*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驱动


#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/platform_device.h>
 
#define GPX1CON 0x11000C20
#define GPX1DAT 0x11000C24
 
#define GPX2CON 0x11000C40
#define GPX2DAT 0x11000C44
 
#define GPF3CON 0x114001E0
#define GPF3DAT 0x114001E4
void fs4412leds_dev_release(struct device *pdev)
{
	printk("fs4412leds_dev_release is called\n");
}
 
struct resource fs4412leds_dev_res [] =
{
	[0] = {.start = GPX1CON,.end=GPX1CON + 3,.name="GPX1CON",.flags = IORESOURCE_MEM},
	[1] = {.start = GPX1DAT,.end=GPX1DAT + 3,.name="GPX1DAT",.flags = IORESOURCE_MEM},
 
	[2] = {.start = GPX2CON,.end=GPX2CON + 3,.name="GPX2CON",.flags = IORESOURCE_MEM},
	[3] = {.start = GPX2DAT,.end=GPX2DAT + 3,.name="GPX2DAT",.flags = IORESOURCE_MEM},
	
	[4] = {.start = GPF3CON,.end=GPF3CON + 3,.name="GPF3CON",.flags = IORESOURCE_MEM},
	[5] = {.start = GPF3DAT,.end=GPF3DAT + 3,.name="GPF3DAT",.flags = IORESOURCE_MEM},
};
 
struct platform_device_id led_id = {
	.name = "fs4412leds",
};
 
struct platform_device fs4412leds_device = 
{
	.name = "fs4412leds",
	.dev.release = fs4412leds_dev_release,
	.resource = fs4412leds_dev_res,
	.num_resources = ARRAY_SIZE(fs4412leds_dev_res),
	.id_entry = &led_id,
};
 
int __init fs4412leds_device_init(void)
{
	platform_device_register(&fs4412leds_device);
	return 0;
}
 
void __exit fs4412leds_device_exit(void)
{
	platform_device_unregister(&fs4412leds_device);
}
 
MODULE_LICENSE("GPL");
module_init(fs4412leds_device_init);
module_exit(fs4412leds_device_exit);
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/platform_device.h>
#include <linux/fs.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 <asm/uaccess.h>
#include <asm/atomic.h>
 
#include "leddrv.h"
 
 
 
int major = 11;
int minor = 0;
int myled_num  = 1;
 
struct myled_dev
{
	struct cdev mydev;
 
	volatile unsigned long *pled2_con;
	volatile unsigned long *pled2_dat;
	
	volatile unsigned long *pled3_con;
	volatile unsigned long *pled3_dat;
 
	volatile unsigned long *pled4_con;
	volatile unsigned long *pled4_dat;
 
	volatile unsigned long *pled5_con;
	volatile unsigned long *pled5_dat;
};
 
struct myled_dev *pgmydev = NULL;
 
 
int myled_open(struct inode *pnode,struct file *pfile)
{
	pfile->private_data =(void *) (container_of(pnode->i_cdev,struct myled_dev,mydev));
	
	return 0;
}
 
int myled_close(struct inode *pnode,struct file *pfile)
{
	return 0;
}
 
void led_on(struct myled_dev *pmydev,int ledno)
{
	switch(ledno)
	{
		case 2:
			writel(readl(pmydev->pled2_dat) | (0x1 << 7),pmydev->pled2_dat);
			break;
		case 3:
			writel(readl(pmydev->pled3_dat) | (0x1),pmydev->pled3_dat);
			break;
		case 4:
			writel(readl(pmydev->pled4_dat) | (0x1 << 4),pmydev->pled4_dat);
			break;
		case 5:
			writel(readl(pmydev->pled5_dat) | (0x1 << 5),pmydev->pled5_dat);
			break;
	}
}
 
void led_off(struct myled_dev *pmydev,int ledno)
{
	switch(ledno)
	{
		case 2:
			writel(readl(pmydev->pled2_dat) & (~(0x1 << 7)),pmydev->pled2_dat);
			break;
		case 3:
			writel(readl(pmydev->pled3_dat) & (~(0x1)),pmydev->pled3_dat);
			break;
		case 4:
			writel(readl(pmydev->pled4_dat) & (~(0x1 << 4)),pmydev->pled4_dat);
			break;
		case 5:
			writel(readl(pmydev->pled5_dat) & (~(0x1 << 5)),pmydev->pled5_dat);
			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;
}
 
struct file_operations myops = {
	.owner = THIS_MODULE,
	.open = myled_open,
	.release = myled_close,
	.unlocked_ioctl = myled_ioctl,
};
 
void ioremap_ledreg(struct myled_dev *pmydev,struct platform_device *p_pltdev)
{
	struct resource *pres = NULL;
 
 
	pres = platform_get_resource(p_pltdev,IORESOURCE_MEM,2);
	pmydev->pled2_con = ioremap(pres->start,4);
 
	pres = platform_get_resource(p_pltdev,IORESOURCE_MEM,3);
	pmydev->pled2_dat = ioremap(pres->start,4);
 
 
	pres = platform_get_resource(p_pltdev,IORESOURCE_MEM,0);
	pmydev->pled3_con = ioremap(pres->start,4);
 
	pres = platform_get_resource(p_pltdev,IORESOURCE_MEM,1);
	pmydev->pled3_dat = ioremap(pres->start,4);
	
 
	pres = platform_get_resource(p_pltdev,IORESOURCE_MEM,4);
	pmydev->pled4_con = ioremap(pres->start,4);
 
	pres = platform_get_resource(p_pltdev,IORESOURCE_MEM,5);
	pmydev->pled4_dat = ioremap(pres->start,4);
	
	pmydev->pled5_con = pmydev->pled4_con;
	pmydev->pled5_dat = pmydev->pled4_dat;
}
 
void set_output_ledconreg(struct myled_dev *pmydev)
{
	writel((readl(pmydev->pled2_con) & (~(0xF << 28))) | (0x1 << 28),pmydev->pled2_con);
	writel((readl(pmydev->pled3_con) & (~(0xF))) | (0x1),pmydev->pled3_con);
	writel((readl(pmydev->pled4_con) & (~(0xF << 16))) | (0x1 << 16),pmydev->pled4_con);
	writel((readl(pmydev->pled5_con) & (~(0xF << 20))) | (0x1 << 20),pmydev->pled5_con);
 
	writel(readl(pmydev->pled2_dat) & (~(0x1 << 7)),pmydev->pled2_dat);
	writel(readl(pmydev->pled3_dat) & (~(0x1)),pmydev->pled3_dat);
	writel(readl(pmydev->pled4_dat) & (~(0x1 << 4)),pmydev->pled4_dat);
	writel(readl(pmydev->pled5_dat) & (~(0x1 << 5)),pmydev->pled5_dat);
}
 
void iounmap_ledreg(struct myled_dev *pmydev)
{
	iounmap(pmydev->pled2_con);
	pmydev->pled2_con = NULL;
	iounmap(pmydev->pled2_dat);
	pmydev->pled2_dat = NULL;
 
	iounmap(pmydev->pled3_con);
	pmydev->pled3_con = NULL;
	iounmap(pmydev->pled3_dat);
	pmydev->pled3_dat = NULL;
	
	iounmap(pmydev->pled4_con);
	pmydev->pled4_con = NULL;
	iounmap(pmydev->pled4_dat);
	pmydev->pled4_dat = NULL;
	
	pmydev->pled5_con = NULL;
	pmydev->pled5_dat = NULL;
}
 
int fs4412leds_driver_probe(struct platform_device *p_pltdev)
{
	int ret = 0;
	dev_t devno = MKDEV(major,minor);
 
	/*申请设备号*/
	ret = register_chrdev_region(devno,myled_num,"myled");
	if(ret)
	{
		ret = alloc_chrdev_region(&devno,minor,myled_num,"myled");
		if(ret)
		{
			printk("get devno failed\n");
			return -1;
		}
		major = MAJOR(devno);//容易遗漏,注意
	}
 
	pgmydev = (struct myled_dev *)kmalloc(sizeof(struct myled_dev),GFP_KERNEL);
	if(NULL == pgmydev)
	{
		unregister_chrdev_region(devno,myled_num);
		printk("kmalloc failed\n");
		return -1;
	}
	memset(pgmydev,0,sizeof(struct myled_dev));
 
	/*给struct cdev对象指定操作函数集*/	
	cdev_init(&pgmydev->mydev,&myops);
 
	/*将struct cdev对象添加到内核对应的数据结构里*/
	pgmydev->mydev.owner = THIS_MODULE;
	cdev_add(&pgmydev->mydev,devno,myled_num);
 
	/*ioremap*/
	ioremap_ledreg(pgmydev,p_pltdev);
 
	/*con-register set output*/
	set_output_ledconreg(pgmydev);
 
	return 0;
}
 
int fs4412leds_driver_remove(struct platform_device *p_pltdev)
{
	dev_t devno = MKDEV(major,minor);
 
	/*iounmap*/
	iounmap_ledreg(pgmydev);
 
	cdev_del(&pgmydev->mydev);
 
	unregister_chrdev_region(devno,myled_num);
 
	kfree(pgmydev);
	pgmydev = NULL;
 
	return 0;
}
 
struct platform_device_id leddrv_ids[] =
{
	[0] = {.name = "fs4412leds"},
	[1] = {.name = "xyz"},
	[2] = {}
};
 
struct platform_driver fs4412leds_driver = 
{
	.driver.name = "leds",
	.probe = fs4412leds_driver_probe,
	.remove = fs4412leds_driver_remove,
	.id_table = leddrv_ids,
};
 
int __init fs4412leds_driver_init(void)
{
	platform_driver_register(&fs4412leds_driver);
	return 0;
}
 
void __exit fs4412leds_driver_exit(void)
{
	platform_driver_unregister(&fs4412leds_driver);
}
 
MODULE_LICENSE("GPL");
 
module_init(fs4412leds_driver_init);
module_exit(fs4412leds_driver_exit);
#ifndef LED_DRIVER_H
#define LED_DRIVER_H
 
#define LED_DEV_MAGIC 'g'
 
#define MY_LED_OFF _IO(LED_DEV_MAGIC,0)
#define MY_LED_ON _IO(LED_DEV_MAGIC,1)
 
 
 
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>
 
#include <stdio.h>
 
#include "leddrv.h"
 
int main(int argc,char *argv[])
{
	int fd = -1;
	int onoff = 0;
	int no = 0;
 
	if(argc < 4)
	{
		printf("The argument is too few\n");
		return 1;
	}
 
	sscanf(argv[2],"%d",&onoff);
	sscanf(argv[3],"%d",&no);
 
	if(no < 2 || no > 5)
	{
		printf("len-no is invalid\n");
		return 2;
	}
 
	fd = open(argv[1],O_RDONLY);
	if(fd < 0)
	{
		printf("open %s failed\n",argv[1]);
		return 3;
	}
 
	if(onoff)
	{
		ioctl(fd,MY_LED_ON,no);
	}
	else
	{
		ioctl(fd,MY_LED_OFF,no);
	}
 
	close(fd);
	fd = -1;
	return 0;
}

三、设备树匹配

设备树匹配:内核启动时根据设备树自动产生的设备 ------ 优先级最高

注意事项:

  1. 无需编写device模块,只需编写driver模块
  2. 使用compatible属性进行匹配,注意设备树中compatible属性值不要包含空白字符
  3. id_table可不设置,但struct platform_driver成员driver的name成员必须设置
/*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驱动

#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 <asm/uaccess.h>
#include <asm/atomic.h>
 
#include "leddrv.h"
 
int major = 11;
int minor = 0;
int myled_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 myled_open(struct inode *pnode,struct file *pfile)
{
	pfile->private_data =(void *) (container_of(pnode->i_cdev,struct myled_dev,mydev));
	
	return 0;
}
 
int myled_close(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;
}
 
struct file_operations myops = {
	.owner = THIS_MODULE,
	.open = myled_open,
	.release = myled_close,
	.unlocked_ioctl = myled_ioctl,
};
 
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 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);
}
 
void free_leds_gpio(struct myled_dev *pmydev)
{
	gpio_free(pmydev->led2gpio);
	gpio_free(pmydev->led3gpio);
	gpio_free(pmydev->led4gpio);
	gpio_free(pmydev->led5gpio);
}
 
int myled_probe(struct platform_device *p_pltdev)
{
	int ret = 0;
	dev_t devno = MKDEV(major,minor);
	struct device_node *pnode = NULL;
	
	/*申请设备号*/
	ret = register_chrdev_region(devno,myled_num,"myled");
	if(ret)
	{
		ret = alloc_chrdev_region(&devno,minor,myled_num,"myled");
		if(ret)
		{
			printk("get devno failed\n");
			return -1;
		}
		major = MAJOR(devno);//容易遗漏,注意
	}
 
	pgmydev = (struct myled_dev *)kmalloc(sizeof(struct myled_dev),GFP_KERNEL);
	if(NULL == pgmydev)
	{
		unregister_chrdev_region(devno,myled_num);
		printk("kmalloc failed\n");
		return -1;
	}
	memset(pgmydev,0,sizeof(struct myled_dev));
 
	/*给struct cdev对象指定操作函数集*/	
	cdev_init(&pgmydev->mydev,&myops);
 
	/*将struct cdev对象添加到内核对应的数据结构里*/
	pgmydev->mydev.owner = THIS_MODULE;
	cdev_add(&pgmydev->mydev,devno,myled_num);
 
	pnode = p_pltdev->dev.of_node;
 
	/*ioremap*/
	request_leds_gpio(pgmydev,pnode);
 
	/*con-register set output*/
	set_leds_gpio_output(pgmydev);
 
	return 0;
}
 
int myled_remove(struct platform_device *p_pltdev)
{
	dev_t devno = MKDEV(major,minor);
 
	/*iounmap*/
	free_leds_gpio(pgmydev);
 
	cdev_del(&pgmydev->mydev);
 
	unregister_chrdev_region(devno,myled_num);
 
	kfree(pgmydev);
	pgmydev = NULL;
 
	return 0;
}
 
struct of_device_id myleddrv_of_ids[] = 
{
	[0] = {.compatible = "fs4412,led2-5"},
	[1] = {.compatible = "origen4412,led6-9"},
	[2] = {},
};
 
struct platform_driver myled_driver = 
{
	.driver = {
		.name = "fs4412leds",
		.of_match_table = myleddrv_of_ids,
	},
	.probe = myled_probe,
	.remove = myled_remove,
};
 
int __init myled_init(void)
{
	platform_driver_register(&myled_driver);
 
	return 0;
}
 
void __exit myled_exit(void)
{
	platform_driver_unregister(&myled_driver);
}
 
 
MODULE_LICENSE("GPL");
 
module_init(myled_init);
module_exit(myled_exit);
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>
 
#include <stdio.h>
 
#include "leddrv.h"
 
int main(int argc,char *argv[])
{
	int fd = -1;
	int onoff = 0;
	int no = 0;
 
	if(argc < 4)
	{
		printf("The argument is too few\n");
		return 1;
	}
 
	sscanf(argv[2],"%d",&onoff);
	sscanf(argv[3],"%d",&no);
 
	if(no < 2 || no > 5)
	{
		printf("len-no is invalid\n");
		return 2;
	}
 
	fd = open(argv[1],O_RDONLY);
	if(fd < 0)
	{
		printf("open %s failed\n",argv[1]);
		return 3;
	}
 
	if(onoff)
	{
		ioctl(fd,MY_LED_ON,no);
	}
	else
	{
		ioctl(fd,MY_LED_OFF,no);
	}
 
	close(fd);
	fd = -1;
	return 0;
}

五、一个编写驱动用的宏

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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值