1)实验平台:正原子阿尔法Linux开发板 2)平台采购地址:https://item.taobao.com/item.htm?id=603672744434 2)全套实验源码 手册 视频下载地址:http://www.openedv.com/thread-300792-1-1.html 3)正点原子Linux感兴趣的学生可以加入小组讨论:935446741 4)关注正点原子微信官方账号,获取最新信息更新 第五十四章 platform设备驱动试验
前几章编写的设备驱动非常简单,都是对的IO最简单的读写操作。像I2C、SPI、LCD这些复杂外设的驱动就不能这样写了,Linux考虑到驱动的可重用性,系统提出了驱动的分离和分层等软件理念,在这个理念下诞生了我们未来最常处理的理念platform设备驱动,也叫做平台设备驱动。让我们学习这一章。Linux以及下驱动分离和分层platform如何编写框架下的设备驱动。
54.1 Linux驱动的分离和分层 54.1.1 驱动的分离和分离 对于Linux这样一个成熟、庞大、复杂的操作系统,代码的重用性非常重要,否则就会出现Linux核心中有大量无意义的重复代码。特别是驱动程序,因为驱动程序占用了Linux如果不管理驱动程序,允许重复代码肆意增加,内核代码量的大头不会持续太久Linux内核文件的数量如此之大,以至于无法接受。 假设现在有三个平台A、B和C,这三个平台(这里的平台是指SOC)上都有MPU6050这个I2C六轴传感器的接口,根据我们写裸机I2C每个平台在驱动时都有一个想法MPU6050的驱动,因此编写出来的最简单的驱动框架如图54.1.1所示:
图54.1.1.1 传统的I2C设备驱动 从图54.1.1.1.可以看出,每个平台下都有主机驱动和设备驱动,必须有主机驱动。毕竟,不同的平台是必要的I2C不同的控制器。但是,没有必要在每个平台上写一个右侧的设备驱动,因为无论是哪一个SOC来说,MPU6050是一样的,通过I2C接口读写数据,只需要一个MPU可以使用6050驱动程序。若再来几个I2C设备,比如AT24C02、FT5206(电容触摸屏)等,如果按照图54.1.1中的写法,那么设备端的驱动就会重复几次。显然在Linux驱动程序中这种写法是不推荐的,最好的做法就是每个平台的I2C控制器提供统一的接口(也称为主机驱动),每个设备只提供一个驱动程序(设备驱动),每个设备通过统一I2C接口驱动访问可以大大简化驱动文件,比如54.1.1.在三种平台下MPU6050驱动框架可以简化为图54.1.1.2所示: 插入图片描述
图54.1.1.2 改进后的设备驱动 实际的I2C一定有很多种驱动设备。MPU6050这实际驱动架构如图54所示.1.1.3所示:
图54.1.1.3 分隔后的驱动框架 这是驱动的分隔,即将主机驱动与设备驱动分开,如I2C、SPI驱动分隔将用于简化驱动的发展。在实际的驱动开发中,一般I2C主控制器驱动已由半导体制造商编写,设备驱动一般由设备制造商编写。我们只需要提供设备信息,例如I2C设备连接到哪个设备?I2C接口上,I2C速度等等。相当于从设备驱动中剥离设备信息,驱动使用标准方法获取设备信息(如从设备树获取设备信息),然后根据获得的设备信息初始化设备。 这相当于驱动只负责驱动,设备只负责设备,想办法匹配两者。这个就是Linux中的总线(bus)、驱动(driver)和设备(device)模型,通常被称为驱动分离。总线是驱动和设备信息的老年人,负责为两者搭桥,如图54所示.1.1.4所示:
图54.1.1.4 Linux总线、驱动和设备模式 当我们向系统注册驱动器时,总线会在右侧的设备中找到,看看是否有匹配的设备。如果是这样,我们将连接两者。同样,当在系统中注册设备时,总线会在左驱动器中查看是否有匹配的设备,如果有,则会联系起来。Linux内核中的大量驱动程序采用总线、驱动和设备模式,我们稍后将重点关注platform驱动是这种思想的产物。 54.1.2 驱动的分层 在最后一节中,我们讨论了驱动程序的分离和分离。在本节中,让我们简要看看驱动程序的分层。您应该听说过网络的7层模型,不同的层负责不同的内容。同样的,Linux下驱动通常是分层的,分层的目的是处理不同层的不同内容。常用于其他书籍或资料input以输入子系统为例,简要介绍驱动分层。input子系统负责管理所有与输入相关的驱动程序,包括键盘、鼠标、触摸等。底部是设备的原始驱动程序,负责获取输入设备的原始值,并向输入事件报告input核心层。input各种核心层都会处理IO并提供模型file_operations操作集合。当我们编写输入设备驱动程序时,我们只需要处理输入事件的报告。至于如何处理这些报告的输入事件,我们不必担心。可见,借助分层模型,我们的驱动编写可以大大简化,对驱动编写非常友好。 54.2 platform介绍平台驱动模型 我们谈到了设备驱动的分离,并引出了总线(bus)、驱动(driver)和设备(device)模型,比如I2C、SPI、USB等总线。但是在SOC有些外设没有总线的概念,但是如何使用总线、驱动和设备模型呢?要解决这个问题,Linux提出了platform这个虚拟总线,相应的platform_driver和platform_device。 54.2.1 platform总线 Linux使用系统内核bus_type结构体表示总线,此结构体定义在文件include/linux/device.h,bus_type结构内容如下:
示例代码54.2.1.1 bus_type结构体代码段 1 struct bus_type {
2 const char *name; /* 总线名字 */ 3 const char *dev_name; 4 struct device *dev_root;
5 struct device_attribute *dev_attrs;
6 const struct attribute_group **bus_groups; /* 总线属性 */
7 const struct attribute_group **dev_groups; /* 设备属性 */
8 const struct attribute_group **drv_groups; /* 驱动属性 */
9
10 int (*match)(struct device *dev, struct device_driver *drv);
11 int (*uevent)(struct device *dev, struct kobj_uevent_env *env);
12 int (*probe)(struct device *dev);
13 int (*remove)(struct device *dev);
14 void (*shutdown)(struct device *dev);
15
16 int (*online)(struct device *dev);
17 int (*offline)(struct device *dev);
18 int (*suspend)(struct device *dev, pm_message_t state);
19 int (*resume)(struct device *dev);
20 const struct dev_pm_ops *pm;
21 const struct iommu_ops *iommu_ops;
22 struct subsys_private *p;
23 struct lock_class_key lock_key;
24 };
第10行,match函数,此函数很重要,单词match的意思就是“匹配、相配”,因此此函数就是完成设备和驱动之间匹配的,总线就是使用match函数来根据注册的设备来查找对应的驱动,或者根据注册的驱动来查找相应的设备,因此每一条总线都必须实现此函数。match函数有两个参数:dev和drv,这两个参数分别为device和device_driver类型,也就是设备和驱动。
platform总线是bus_type的一个具体实例,定义在文件drivers/base/platform.c,platform总线定义如下:
示例代码54.2.1.2 platform总线实例
1 struct bus_type platform_bus_type = {
2 .name = "platform",
3 .dev_groups = platform_dev_groups,
4 .match = platform_match,
5 .uevent = platform_uevent,
6 .pm = &platform_dev_pm_ops,
7 };
platform_bus_type就是platform平台总线,其中platform_match就是匹配函数。我们来看一下驱动和设备是如何匹配的,platform_match函数定义在文件drivers/base/platform.c中,函数内容如下所示:
示例代码54.2.1.3 platform总线实例
1 static int platform_match(struct device *dev,
struct device_driver *drv)
2 {
3 struct platform_device *pdev = to_platform_device(dev);
4 struct platform_driver *pdrv = to_platform_driver(drv);
5
6 /*When driver_override is set,only bind to the matching driver*/
7 if (pdev->driver_override)
8 return !strcmp(pdev->driver_override, drv->name);
9
10 /* Attempt an OF style match first */
11 if (of_driver_match_device(dev, drv))
12 return 1;
13
14 /* Then try ACPI style match */
15 if (acpi_driver_match_device(dev, drv))
16 return 1;
17
18 /* Then try to match against the id table */
19 if (pdrv->id_table)
20 return platform_match_id(pdrv->id_table, pdev) != NULL;
21
22 /* fall-back to driver name match */
23 return (strcmp(pdev->name, drv->name) == 0);
24 }
驱动和设备的匹配有四种方法,我们依次来看一下: 第11~12行,第一种匹配方式, OF类型的匹配,也就是设备树采用的匹配方式,of_driver_match_device函数定义在文件include/linux/of_device.h中。device_driver结构体(表示设备驱动)中有个名为of_match_table的成员变量,此成员变量保存着驱动的compatible匹配表,设备树中的每个设备节点的compatible属性会和of_match_table表中的所有成员比较,查看是否有相同的条目,如果有的话就表示设备和此驱动匹配,设备和驱动匹配成功以后probe函数就会执行。 第15~16行,第二种匹配方式,ACPI匹配方式。 第19~20行,第三种匹配方式,id_table匹配,每个platform_driver结构体有一个id_table成员变量,顾名思义,保存了很多id信息。这些id信息存放着这个platformd驱动所支持的驱动类型。 第23行,第四种匹配方式,如果第三种匹配方式的id_table不存在的话就直接比较驱动和设备的name字段,看看是不是相等,如果相等的话就匹配成功。 对于支持设备树的Linux版本号,一般设备驱动为了兼容性都支持设备树和无设备树两种匹配方式。也就是第一种匹配方式一般都会存在,第三种和第四种只要存在一种就可以,一般用的最多的还是第四种,也就是直接比较驱动和设备的name字段,毕竟这种方式最简单了。 54.2.2 platform驱动 platform_driver结构体表示platform驱动,此结构体定义在文件include/linux/platform_device.h中,内容如下:
示例代码54.2.2.1 platform_driver结构体
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 bool prevent_deferred_probe;
10 };
第2行,probe函数,当驱动与设备匹配成功以后probe函数就会执行,非常重要的函数!!一般驱动的提供者会编写,如果自己要编写一个全新的驱动,那么probe就需要自行实现。 第7行,driver成员,为device_driver结构体变量,Linux内核里面大量使用到了面向对象的思维,device_driver相当于基类,提供了最基础的驱动框架。plaform_driver继承了这个基类,然后在此基础上又添加了一些特有的成员变量。 第8行,id_table表,也就是我们上一小节讲解platform总线匹配驱动和设备的时候采用的第三种方法,id_table是个表(也就是数组),每个元素的类型为platform_device_id,platform_device_id结构体内容如下:
示例代码54.2.2.2 platform_device_id结构体
1 struct platform_device_id {
2 char name[PLATFORM_NAME_SIZE];
3 kernel_ulong_t driver_data;
4 };
device_driver结构体定义在include/linux/device.h,device_driver结构体内容如下:
示例代码54.2.2.3 device_driver结构体
1 struct device_driver {
2 const char *name;
3 struct bus_type *bus;
4
5 struct module *owner;
6 const char *mod_name; /* used for built-in modules */
7
8 bool suppress_bind_attrs; /* disables bind/unbind via sysfs */
9
10 const struct of_device_id *of_match_table;
11 const struct acpi_device_id *acpi_match_table;
12
13 int (*probe) (struct device *dev);
14 int (*remove) (struct device *dev);
15 void (*shutdown) (struct device *dev);
16 int (*suspend) (struct device *dev, pm_message_t state);
17 int (*resume) (struct device *dev);
18 const struct attribute_group **groups;
19
20 const struct dev_pm_ops *pm;
21
22 struct driver_private *p;
23 };
第10行,of_match_table就是采用设备树的时候驱动使用的匹配表,同样是数组,每个匹配项都为of_device_id结构体类型,此结构体定义在文件include/linux/mod_devicetable.h中,内容如下:
示例代码54.2.2.4 of_device_id结构体
1 struct of_device_id {
2 char name[32];
3 char type[32];
4 char compatible[128];
5 const void *data;
6 };
第4行的compatible非常重要,因为对于设备树而言,就是通过设备节点的compatible属性值和of_match_table中每个项目的compatible成员变量进行比较,如果有相等的就表示设备和此驱动匹配成功。 在编写platform驱动的时候,首先定义一个platform_driver结构体变量,然后实现结构体中的各个成员变量,重点是实现匹配方法以及probe函数。当驱动和设备匹配成功以后probe函数就会执行,具体的驱动程序在probe函数里面编写,比如字符设备驱动等等。 当我们定义并初始化好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驱动。 返回值:无。 platform驱动框架如下所示:
示例代码54.2.2.5 platform驱动框架
/* 设备结构体 */
1 struct xxx_dev{
2 struct cdev cdev;
3 /* 设备结构体其他具体内容 */
4 };
5
6 struct xxx_dev xxxdev; /* 定义个设备结构体变量 */
7
8 static int xxx_open(struct inode *inode, struct file *filp)
9 {
10 /* 函数具体内容 */
11 return 0;
12 }
13
14 static ssize_t xxx_write(struct file *filp, const char __user *buf,
size_t cnt, loff_t *offt)
15 {
16 /* 函数具体内容 */
17 return 0;
18 }
19
20 /* 21 * 字符设备驱动操作集 22 */
23 static struct file_operations xxx_fops = {
24 .owner = THIS_MODULE,
25 .open = xxx_open,
26 .write = xxx_write,
27 };
28
29 /* 30 * platform驱动的probe函数 31 * 驱动与设备匹配成功以后此函数就会执行 32 */
33 static int xxx_probe(struct platform_device *dev)
34 {
35 ......
36 cdev_init(&xxxdev.cdev, &xxx_fops); /* 注册字符设备驱动 */
37 /* 函数具体内容 */
38 return 0;
39 }
40
41 static int xxx_remove(struct platform_device *dev)
42 {
43 ......
44 cdev_del(&xxxdev.cdev);/* 删除cdev */
45 /* 函数具体内容 */
46 return 0;
47 }
48
49 /* 匹配列表 */
50 static const struct of_device_id xxx_of_match[] = {
51 {
.compatible = "xxx-gpio" },
52 {
/* Sentinel */ }
53 };
54
55 /* 56 * platform平台驱动结构体 57 */
58 static struct platform_driver xxx_driver = {
59 .driver = {
60 .name = "xxx",
61 .of_match_table = xxx_of_match,
62 },
63 .probe = xxx_probe,
64 .remove = xxx_remove,
65 };
66
67 /* 驱动模块加载 */
68 static int __init xxxdriver_init(void)
69 {
70 return platform_driver_register(&xxx_driver);
71 }
72
73 /* 驱动模块卸载 */
74 static void __exit xxxdriver_exit(void)
75 {
76 platform_driver_unregister(&xxx_driver);
77 }
78
79 module_init(xxxdriver_init);
80 module_exit(xxxdriver_exit);
81 MODULE_LICENSE("GPL");
82 MODULE_AUTHOR("zuozhongkai");
第1~27行,传统的字符设备驱动,所谓的platform驱动并不是独立于字符设备驱动、块设备驱动和网络设备驱动之外的其他种类的驱动。platform只是为了驱动的分离与分层而提出来的一种框架,其驱动的具体实现还是需要字符设备驱动、块设备驱动或网络设备驱动。
第33~39行,xxx_probe函数,当驱动和设备匹配成功以后此函数就会执行,以前在驱动入口init函数里面编写的字符设备驱动程序就全部放到此probe函数里面。比如注册字符设备驱动、添加cdev、创建类等等。 第41~47行,xxx_remove函数,platform_driver结构体中的remove成员变量,当关闭platfor备驱动的时候此函数就会执行,以前在驱动卸载exit函数里面要做的事情就放到此函数中来。比如,使用iounmap释放内存、删除cdev,注销设备号等等。 第50~53行,xxx_of_match匹配表,如果使用设备树的话将通过此匹配表进行驱动和设备的匹配。第51行设置了一个匹配项,此匹配项的compatible值为“xxx-gpio”,因此当设备树中设备节点的compatible属性值为“xxx-gpio”的时候此设备就会与此驱动匹配。第52行是一个标记,of_device_id表最后一个匹配项必须是空的。 第5865行,定义一个platform_driver结构体变量xxx_driver,表示platform驱动,第5962行设置paltform_driver中的device_driver成员变量的name和of_match_table这两个属性。其中name属性用于传统的驱动与设备匹配,也就是检查驱动和设备的name字段是不是相同。of_match_table属性就是用于设备树下的驱动与设备检查。对于一个完整的驱动程序,必须提供有设备树和无设备树两种匹配方法。最后63和64这两行设置probe和remove这两成员变量。 第68~71行,驱动入口函数,调用platform_driver_register函数向Linux内核注册一个platform驱动,也就是上面定义的xxx_driver结构体变量。 第74~77行,驱动出口函数,调用platform_driver_unregister函数卸载前面注册的platform驱动。 总体来说,platform驱动还是传统的字符设备驱动、块设备驱动或网络设备驱动,只是套上了一张“platform”的皮,目的是为了使用总线、驱动和设备这个驱动模型来实现驱动的分离与分层。 54.2.3 platform设备 platform驱动已经准备好了,我们还需要platform设备,否则的话单单一个驱动也做不了什么。platform_device这个结构体表示platform设备,这里我们要注意,如果内核支持设备树的话就不要再使用platform_device来描述设备了,因为改用设备树去描述了。当然了,你如果一定要用platform_device来描述设备信息的话也是可以的。platform_device结构体定义在文件include/linux/platform_device.h中,结构体内容如下:
示例代码54.2.3.1 platform_device结构体代码段
22 struct platform_device {
23 const char *name;
24 int id;
25 bool id_auto;
26 struct device dev;
27 u32 num_resources;
28 struct resource *resource;
29
30 const struct platform_device_id *id_entry;
31 char *driver_override; /* Driver name to force a match */
32
33 /* MFD cell pointer */
34 struct mfd_cell *mfd_cell;
35
36 /* arch specific additions */
37 struct pdev_archdata archdata;
38 };
第23行,name表示设备名字,要和所使用的platform驱动的name字段相同,否则的话设备就无法匹配到对应的驱动。比如对应的platform驱动的name字段为“xxx-gpio”,那么此name字段也要设置为“xxx-gpio”。
第27行,num_resources表示资源数量,一般为第28行resource资源的大小。
第28行,resource表示资源,也就是设备信息,比如外设寄存器等。Linux内核使用resource结构体表示资源,resource结构体内容如下:
示例代码54.2.3.2 resource结构体代码段
18 struct resource {
19 resource_size_t start;
20 resource_size_t end;
21 const char *name;
22 unsigned long flags;
23 struct resource *parent, *sibling, *child;
24 };
start和end分别表示资源的起始和终止信息,对于内存类的资源,就表示内存起始和终止地址,name表示资源名字,flags表示资源类型,可选的资源类型都定义在了文件include/linux/ioport.h里面,如下所示:
示例代码54.2.3.3 资源类型
29 #define IORESOURCE_BITS 0x000000ff /* Bus-specific bits */
30
31 #define IORESOURCE_TYPE_BITS 0x00001f00 /* Resource type */
32 #define IORESOURCE_IO 0x00000100 /* PCI/ISA I/O ports */
33 #define IORESOURCE_MEM 0x00000200
34 #define IORESOURCE_REG 0x00000300 /* Register offsets */
35 #define IORESOURCE_IRQ 0x00000400
36 #define IORESOURCE_DMA 0x00000800
37 #define IORESOURCE_BUS 0x00001000
......
104 /* PCI control bits. Shares IORESOURCE_BITS with above PCI ROM. */
105 #define IORESOURCE_PCI_FIXED (1<<4) /* Do not move resource */
在以前不支持设备树的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设备。 返回值:无。 platform设备信息框架如下所示:
示例代码54.2.3.4 platform设备框架
1 /* 寄存器地址定义*/
2 #define PERIPH1_REGISTER_BASE (0X20000000) /* 外设1寄存器首地址 */
3 #define PERIPH2_REGISTER_BASE (0X020E0068) /* 外设2寄存器首地址 */
4 #define REGISTER_LENGTH 4
5
6 /* 资源 */
7 static struct resource xxx_resources[] = {
8 [0] = {
9 .start = PERIPH1_REGISTER_BASE,
10 .end = (PERIPH1_REGISTER_BASE + REGISTER_LENGTH - 1),
11 .flags = IORESOURCE_MEM,
12 },
13 [1] = {
14 .start = PERIPH2_REGISTER_BASE,
15 .end = (PERIPH2_REGISTER_BASE + REGISTER_LENGTH - 1),
16 .flags = IORESOURCE_MEM,