platform设备驱动框架详解

platform设备驱动框架详解


    前面我们讲了设备驱动的分离,并且引出了总线(bus)、驱动(driver)和设备(device)模型,比如 I2C、SPI、USB 等总线。但是在 SOC 中有些外设是没有总线这个概念的,但是又要使用总线、驱动和设备模型该怎么办呢?为了解决此问题,Linux 提出了 platform 这个虚拟总线,相应的就有 platform_driver 和 platform_device。

一、platform总线

1. struct bus_type 结构体

    Linux 系统内核使用 bus_type 结构体表示总线,此结构体定义在文件***include/linux/device.h***, bus_type 结构体内容如下:

	struct bus_type {
	const char *name; /* 总线名字 */
	const char *dev_name;
	struct device *dev_root;
	struct device_attribute *dev_attrs;
	const struct attribute_group **bus_groups;/* 总线属性 */
	const struct attribute_group **dev_groups;/* 设备属性 */
	const struct attribute_group **drv_groups;/* 驱动属性 */
	int (*match)(struct device *dev, struct device_driver *drv);
	int (*uevent)(struct device *dev, struct kobj_uevent_env *env);
	int (*probe)(struct device *dev);
	int (*remove)(struct device *dev);
	void (*shutdown)(struct device *dev);
	int (*online)(struct device *dev);
	int (*offline)(struct device *dev);
	int (*suspend)(struct device *dev, pm_message_t state);
	int (*resume)(struct device *dev);
	const struct dev_pm_ops *pm;
	const struct iommu_ops *iommu_ops;
	struct subsys_private *p;
	struct lock_class_key lock_key;
};

    match 函数,此函数很重要,单词 match 的意思就是“匹配、相配”,因此此函数就是完成设备和驱动之间匹配的,总线就是使用 match 函数来根据注册的设备来查找对应的驱动,或者根据注册的驱动来查找相应的设备,因此每一条总线都必须实现此函数。 match 函数有两个参数:dev 和 drv,这两个参数分别为 device 和 device_driver 类型,也就是设备和驱动。

2. struct bus_type 结构体

    platform 总线是 bus_type 的一个具体实例,定义在文件 drivers/base/platform.c, platform 总线定义如下:

struct bus_type platform_bus_type = {
	.name = "platform",
	.dev_groups = platform_dev_groups,
	.match = platform_match,
	.uevent = platform_uevent,
	.pm = &platform_dev_pm_ops,
};

    platform_bus_type 就是 platform 平台总线,其中 platform_match 就是匹配函数。

二、platform驱动

1. struct platform_driver 结构体

    platform_driver 结 构 体 表 示***platforminclude/linux/platform_device.h*** 中,内容如下:

struct platform_driver {
	int (*probe)(struct platform_device *);
	int (*remove)(struct platform_device *);
	void (*shutdown)(struct platform_device *);
	int (*suspend)(struct platform_device *, pm_message_t state);
	int (*resume)(struct platform_device *);
	struct device_driver driver;
	const struct platform_device_id *id_table;
	bool prevent_deferred_probe;
};

    probe 函数,当驱动与设备匹配成功以后 probe 函数就会执行,非常重要的函数!!一般驱动的提供者会编写,如果自己要编写一个全新的驱动,那么 probe 就需要自行实现。
    driver 成员,为 device_driver 结构体变量,Linux 内核里面大量使用到了面向对象的思维, device_driver 相当于基类,提供了最基础的驱动框架。 plaform_driver 继承了这个基类,然后在此基础上又添加了一些特有的成员变量。
    在编写 platform 驱动的时候,首先定义一个 platform_driver 结构体变量,然后实现结构体中的各个成员变量,重点是实现匹配方法以及 probe 函数。当驱动和设备匹配成功以后 probe函数就会执行,具体的驱动程序在 probe 函数里面编写,比如字符设备驱动等等。

2. 驱动注册相关API

    当我们定义并初始化好 platform_driver 结构体变量以后,需要在驱动入口函数里面调用***platform_driver_register*** 函数向 Linux 内核注册一个 platform 驱动,platform_driver_register 函数原型如下所示:

int platform_driver_register (struct platform_driver *driver)

函数参数和返回值含义如下:
driver: 要注册的 platform 驱动。
返回值: 负数,失败;0,成功。
还 需 要 在 驱 动 卸 载 函 数 中 通 过 platform_driver_unregister 函 数 卸 载platform 驱 动 ,platform_driver_unregister 函数原型如下:

void platform_driver_unregister(struct platform_driver *drv)

函数参数和返回值含义如下:
drv: 要卸载的 platform 驱动。
返回值: 无。

3. platfrom驱动框架

platform 驱动框架如下所示:

/* 设备结构体 */
struct xxx_dev{
	struct cdev cdev;
	/* 设备结构体其他具体内容 */
};
struct xxx_dev xxxdev;

static int xxx_open(struct inode *inode, struct file *filp)
{
	/* 函数具体内容 */
	return 0;
}
static ssize_t xxx_write(struct file *filp, const char __user *buf,size_t cnt, loff_t *offt)
{
	/* 函数具体内容 */
	return 0;
}

/*
 * 字符设备驱动操作集
 */
static struct file_operations xxx_fops = {
	.owner = THIS_MODULE,
	.open = xxx_open,
	.write = xxx_write,
};

/*
 * platform 驱动的 probe 函数
 * 驱动与设备匹配成功以后此函数就会执行
 */
static int xxx_probe(struct platform_device *dev)
{
	...
	cdev_init(&xxxdev.cdev, &xxx_fops); /* 注册字符设备驱动 */
	/* 函数具体内容 */
	return 0;
}
static int xxx_remove(struct platform_device *dev)
{
	...
	cdev_del(&xxxdev.cdev);/*删除 cdev */
	/* 函数具体内容 */
	return 0;
}
/* 匹配列表 */
static const struct of_device_id xxx_of_match[] = {
	{ .compatible = "xxx-gpio" },
	{ /* Sentinel */ }
};
/*
 * platform 平台驱动结构体
 */
static struct platform_driver xxx_driver = {
	.driver = {
		.name = "xxx",
		.of_match_table = xxx_of_match,
	}, 
	.probe = xxx_probe,
	.remove = xxx_remove,
};

/* 驱动模块加载 */
static int __init xxxdriver_init(void)
{
	return platform_driver_register(&xxx_driver);
}

/* 驱动模块卸载 */
static void __exit xxxdriver_exit(void)
{
	platform_driver_unregister(&xxx_driver);
}
module_init(xxxdriver_init);
module_exit(xxxdriver_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("eurphan");

三、platform设备

1. struct platform_device 结构体

    platform 驱动已经准备好了,我们还需要 platform 设备,否则的话单单一个驱动也做不了什么。platform_device 这个结构体表示 platform 设备,这里我们要注意,如果内核支持设备树的话就不要再使用 platform_device 来描述设备了,因为改用设备树去描述了。当然了,你如果一定要用 platform_device 来描述设备信息的话也是可以的。platform_device 结构体定义在文件***include/linux/platform_device.h*** 中,结构体内容如下:

struct platform_device {
	const char *name;
	int id;
	bool id_auto;
	struct device dev;
	u32 num_resources;
	struct resource *resource;
	const struct platform_device_id *id_entry;
	char *driver_override; /* Driver name to force a match */
	/* MFD cell pointer */
	struct mfd_cell *mfd_cell;
	/* arch specific additions */
	struct pdev_archdata archdata;
};

name 表示设备名字,要和所使用的 platform 驱动的 name 字段相同,否则的话设备就无法匹配到对应的驱动。比如对应的 platform 驱动的 name 字段为“xxx-gpio”,那么此 name字段也要设置为“xxx-gpio”。
num_resources 表示资源数量,一般为第 28 行 resource 资源的大小。
resource 表示资源,也就是设备信息,比如外设寄存器等。

2. struct resource 结构体

Linux 内核使用 resource结构体表示资源。resource 结构体内容如下:

struct resource {
	resource_size_t start;
	resource_size_t end;
	const char *name;
	unsigned long flags;
	struct resource *parent, *sibling, *child;
};

start 和 end 分别表示资源的起始和终止信息,对于内存类的资源,就表示内存起始和终止地 址 ,
name 表 示 资 源 名 字 ,
flags 表 示 资 源 类 型 , 可 选 的 资 源 类 型 都 定 义 在 了 文 件***include/linux/ioport.h*** 里面,如下所示:

#define IORESOURCE_BITS 		0x000000ff /* Bus-specific bits */
#define IORESOURCE_TYPE_BITS 	0x00001f00 /* Resource type */
#define IORESOURCE_IO 			0x00000100 /* PCI/ISA I/O ports */
#define IORESOURCE_MEM 			0x00000200 
#define IORESOURCE_REG 			0x00000300 /* Register offsets */
#define IORESOURCE_IRQ 			0x00000400 
#define IORESOURCE_DMA 			0x00000800
#define IORESOURCE_BUS 			0x00001000
......
/* PCI control bits.Shares IORESOURCE_BITS with above PCI ROM.*/
#define IORESOURCE_PCI_FIXED	(1<<4)
/* Do not move resource */
3.platform设备注册相关API

    在以前不支持设备树的 Linux 版本中,用户需要编写 platform_device 变量来描述设备信息,
然后使用 platform_device_register 函数将设备信息注册到 Linux 内核中,此函数原型如下所示:

int platform_device_register(struct platform_device *pdev)

函数参数和返回值含义如下:
pdev: 要注册的 platform 设备。
返回值: 负数,失败;0,成功。
    如果不再使用 platform 的话可以通过 platform_device_unregister 函数注销掉相应的 platform
设备,platform_device_unregister 函数原型如下:

void platform_device_unregister(struct platform_device *pdev)

函数参数和返回值含义如下:
pdev: 要注销的 platform 设备。
返回值: 无。

4.platform设备信息框架

platform 设备信息框架如下所示:

/* 寄存器地址定义*/ 
#define PERIPH1_REGISTER_BASE (0X20000000) /* 外设 1 寄存器首地址 */

#define PERIPH2_REGISTER_BASE (0X020E0068) /* 外设 2 寄存器首地址 */
#define REGISTER_LENGTH 	  4

/* 资源 */
static struct resource xxx_resources[] = {
	[0] = {
		.start = PERIPH1_REGISTER_BASE,
		.end = (PERIPH1_REGISTER_BASE + REGISTER_LENGTH - 1),
		.flags = IORESOURCE_MEM,
	},
	[1] = {
		.start = PERIPH2_REGISTER_BASE,
		.end = (PERIPH2_REGISTER_BASE + REGISTER_LENGTH - 1),
		.flags = IORESOURCE_MEM,
	},
};

/* platform 设备结构体 */
static struct platform_device xxxdevice = {
	.name = "xxx-gpio",
	.id = -1,
	.num_resources = ARRAY_SIZE(xxx_resources),
	.resource = xxx_resources,
 };

/* 设备模块加载 */
static int __init xxxdevice_init(void)
{
	return platform_device_register(&xxxdevice);
}

/* 设备模块注销 */
static void __exit xxx_resourcesdevice_exit(void)
{
	platform_device_unregister(&xxxdevice);
}

    上述示例代码主要是在不支持设备树的 Linux 版本中使用的,当 Linux 内核支持了设备树以后就不需要用户手动去注册 platform 设备了。因为设备信息都放到了设备树中去描述,Linux 内核启动的时候会从设备树中读取设备信息,然后将其组织成 platform_device 形式,至于设备树到platform_device 的具体过程就不去详细的追究了,感兴趣的可以去看一下,网上也有很多博客详细的讲解了整个过程。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章