1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > Linux platform 设备驱动实验-基于正点原子IMX6ULL开发板

Linux platform 设备驱动实验-基于正点原子IMX6ULL开发板

时间:2019-09-11 08:31:47

相关推荐

Linux platform 设备驱动实验-基于正点原子IMX6ULL开发板

我们以前的设备驱动都非常的简单,都是对IO进行最简单的读写操作。像I2C、 SPI、LCD 这些复杂外设的驱动就不能这么去写了,Linux 系统要考虑到驱动的可重用性,因此提出了驱动的分离与分层这样的软件思路,在这个思路下诞生了我们将来最常打交道的platform 设备驱动,也叫做平台设备驱动。现在我们就来学习一下 Linux 下的驱动分离与分层,以及 platform 框架下的设备驱动该如何编写。

1 Linux 驱动的分离与分层

1.1 驱动的分隔与分离

对于 Linux 这样一个成熟、庞大、复杂的操作系统,代码的重用性非常重要,否则的话就会在 Linux 内核中存在大量无意义的重复代码。尤其是驱动程序,因为驱动程序占用了 Linux内核代码量的大头,如果不对驱动程序加以管理,任由重复的代码肆意增加,那么用不了多久Linux 内核的文件数量就庞大到无法接受的地步。

假如现在有三个平台 A、B 和 C,这三个平台(这里的平台说的是 SOC)上都有 MPU6050 这 个 I2C 接口的六轴传感器,按照我们写裸机 I2C 驱动的时候的思路,每个平台都有一个MPU6050的驱动,因此编写出来的最简单的驱动框架如图所示:

从上图 可以看出,每种平台下都有一个主机驱动和设备驱动,主机驱动肯定是必须要的,毕竟不同的平台其 I2C 控制器不同。但是右侧的设备驱动就没必要每个平台都写一个,因为不管对于那个 SOC 来说,MPU6050 都是一样,通过 I2C 接口读写数据就行了,只需要一个 MPU6050 的驱动程序即可。如果再来几个 I2C 设备,比如 AT24C02、FT5206(电容触摸屏)等,如果按照上图中的写法,那么设备端的驱动将会重复的编写好几次。显然在 Linux 驱动程序中这种写法是不推荐的,最好的做法就是每个平台的 I2C 控制器都提供一个统一的接口(也叫做主机驱动),每个设备的话也只提供一个驱动程序(设备驱动),每个设备通过统一的 I2C接口驱动来访问,这样就可以大大简化驱动文件,上图中三种平台下的 MPU6050 驱动框架就可以简化为下图所示:

实际的 I2C 驱动设备肯定有很多种,不止 MPU6050 这一个,那么实际的驱动架构如下图所示:

这个就是驱动的分隔,也就是将主机驱动和设备驱动分隔开来,比如 I2C、SPI 等等都会采用驱动分隔的方式来简化驱动的开发。在实际的驱动开发中,一般 I2C 主机控制器驱动已经由半导体厂家编写好了,而设备驱动一般也由设备器件的厂家编写好了,我们只需要提供设备信息即可,比如 I2C 设备的话提供设备连接到了哪个 I2C 接口上,I2C 的速度是多少等等。相当于将设备信息从设备驱动中剥离开来,驱动使用标准方法去获取到设备信息(比如从设备树中获取到设备信息),然后根据获取到的设备信息来初始化设备。 这样就相当于驱动只负责驱动,设备只负责设备,想办法将两者进行匹配即可。这个就是 Linux 中的总线(bus)、驱动(driver)和设备(device)模型,也就是常说的驱动分离。总线就是驱动和设备信息的月老,负责给两者牵线搭桥,如图所示:

当我们向系统注册一个驱动的时候,总线就会在右侧的设备中查找,看看有没有与之匹配的设备,如果有的话就将两者联系起来。同样的,当向系统中注册一个设备的时候,总线就会在左侧的驱动中查找看有没有与之匹配的设备,有的话也联系起来。Linux 内核中大量的驱动程序都采用总线、驱动和设备模式,我们一会要重点讲解的 platform 驱动就是这一思想下的产物。

1.2 驱动的分层

上一小节讲了驱动的分隔与分离,本节我们来简单看一下驱动的分层,大家应该听说过网络的 7 层模型,不同的层负责不同的内容。同样的,Linux 下的驱动往往也是分层的,分层的目的也是为了在不同的层处理不同的内容。以其他书籍或者资料常常使用到的input(输入子系统)为例,简单介绍一下驱动的分层。input 子系统负责管理所有跟输入有关的驱动,包括键盘、鼠标、触摸等,最底层的就是设备原始驱动,负责获取输入设备的原始值,获取到的输入事件上报给 input 核心层。input 核心层会处理各种 IO 模型,并且提供 file_operations 操作集合。我们在编写输入设备驱动的时候只需要处理好输入事件的上报即可,至于如何处理这些上报的输入事件那是上层去考虑的,我们不用管。可以看出借助分层模型可以极大的简化我们的驱动编写,对于驱动编写来说非常的友好。

2 platform 平台驱动模型简介

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

2.1 platform 总线

Linux系统内核使用bus_type结构体表示总线,此结构体定义在文件include/linux/device.h,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 总线定义如下:

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 中,函数内容如下所示:

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;1314 /* Then try ACPI style match */15 if (acpi_driver_match_device(dev, drv))16 return 1;1718 /* Then try to match against the id table */19 if (pdrv->id_table)20 return platform_match_id(pdrv->id_table, pdev) != NULL;2122 /* 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 字段,毕竟这种方式最简单了。

2.2 platform 驱动

platform_driver 结 构 体 表 示 platform 驱动,此结构体定义在文件include/linux/platform_device.h 中,内容如下:

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 结构体内容如下:

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 结构体内容如下:

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;1213 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;1920 const struct dev_pm_ops *pm;2122 struct driver_private *p;23 };

第 10 行,of_match_table 就是采用设备树的时候驱动使用的匹配表,同样是数组,每个匹配项都为 of_device_id 结构体类型,此结构体定义在文件 include/linux/mod_devicetable.h 中,内容如下:

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 驱动框架如下所示:

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 }1314 static ssize_t xxx_write(struct file *filp, const char __user *buf,size_t cnt, loff_t *offt)15 {16 /* 函数具体内容 */17 return 0;18 }1920 /*21 * 字符设备驱动操作集22 */23 static struct file_operations xxx_fops = {24 .owner = THIS_MODULE,25 .open = xxx_open,26 .write = xxx_write,27 };2829 /*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 }4041 static int xxx_remove(struct platform_device *dev)42 {43 ......44 cdev_del(&xxxdev.cdev);/* 删除 cdev */45 /* 函数具体内容 */46 return 0;47 }4849 /* 匹配列表 */50 static const struct of_device_id xxx_of_match[] = {51 {.compatible = "xxx-gpio" },52 {/* Sentinel */ }53 };5455 /* 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 }7273 /* 驱动模块卸载 */74 static void __exit xxxdriver_exit(void)75 {76 platform_driver_unregister(&xxx_driver);77 }7879 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 成员变量,当关闭 platform设备驱动的时候此函数就会执行,以前在驱动卸载 exit 函数里面要做的事情就放到此函数中来。比如,使用 iounmap 释放内存、删除 cdev,注销设备号等等。

第 50~53 行,xxx_of_match 匹配表,如果使用设备树的话将通过此匹配表进行驱动和设备的匹配。第 51 行设置了一个匹配项,此匹配项的 compatible 值为“xxx-gpio”,因此当设备树中设备节点的 compatible 属性值为“xxx-gpio”的时候此设备就会与此驱动匹配。第 52 行是一个标记,of_device_id 表最后一个匹配项必须是空的。

第 58~65 行,定义一个 platform_driver 结构体变量 xxx_driver,表示 platform 驱动,第 59~62行设置 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”的皮,目的是为了使用总线、驱动和设备这个驱动模型来实现驱动的分离与分层。

2.3 platform 设备

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

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;2930 const struct platform_device_id *id_entry;31 char *driver_override; /* Driver name to force a match */3233 /* MFD cell pointer */34 struct mfd_cell *mfd_cell;3536 /* 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 结构体内容如下:

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 里面,如下所示:

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 0x000004 #define IORESOURCE_REG 0x00000300 /* Register offsets */35 #define IORESOURCE_IRQ 0x0000040036 #define IORESOURCE_DMA 0x0000080037 #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 设备。

返回值:无。

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,17 },18 };1920 /* platform 设备结构体 */21 static struct platform_device xxxdevice = {22 .name = "xxx-gpio",23 .id = -1,24 .num_resources = ARRAY_SIZE(xxx_resources),25 .resource = xxx_resources,26 };27 28 /* 设备模块加载 */29 static int __init xxxdevice_init(void)30 {31 return platform_device_register(&xxxdevice);32 }3334 /* 设备模块注销 */35 static void __exit xxx_resourcesdevice_exit(void)36 {37 platform_device_unregister(&xxxdevice);38 }3940 module_init(xxxdevice_init);41 module_exit(xxxdevice_exit);42 MODULE_LICENSE("GPL");43 MODULE_AUTHOR("supersmart");

第 7~18 行,数组 xxx_resources 表示设备资源,一共有两个资源,分别为设备外设 1 和外设 2 的寄存器信息。因此 flags 都为 IORESOURCE_MEM,表示资源为内存类型的。

第 21~26 行,platform 设备结构体变量,注意 name 字段要和所使用的驱动中的 name 字段一致,否则驱动和设备无法匹配成功。num_resources 表示资源大小,其实就是数组 xxx_resources的元素数量,这里用 ARRAY_SIZE 来测量一个数组的元素个数。

第 29~32 行,设备模块加载函数,在此函数中调用 platform_device_register 向 Linux 内核注册 platform 设备。

第 35~38 行,设备模块卸载函数,在此函数中调用 platform_device_unregister 从 Linux 内核中卸载 platform 设备。

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

关于 platform 下的总线、驱动和设备就讲解到这里,我们接下来就使用 platform 驱动框架来编写一个 LED 灯驱动,本章我们不使用设备树来描述设备信息,我们采用自定义platform_device这种“古老”方式来编写LED的设备信息。下一个实验我们来编写设备树下的platform驱动,这样我们就掌握了无设备树和有设备树这两种 platform 驱动的开发方式。

3 硬件原理图分析

本次实验我们只使用到 IMX6U-ALPHA 开发板上的 LED 灯,因此实验硬件原理图同其它led实验一样。

4 试验程序编写

本次实验我们需要编写一个驱动模块和一个设备模块,其中驱动模块是 platform 驱动程序,设备模块是 platform 的设备信息。当这两个模块都加载成功以后就会匹配成功,然后 platform驱动模块中的 probe 函数就会执行,probe 函数中就是传统的字符设备驱动那一套。

4.1 platform 设备与驱动程序编写

leddevice.c 内容如下

#include <linux/module.h>#include <linux/kernel.h>#include <linux/init.h>#include <linux/fs.h>#include <linux/slab.h>#include <linux/uaccess.h>#include <linux/io.h>#include <linux/cdev.h>#include <linux/device.h>#include <linux/of.h>#include <linux/of_address.h>#include <linux/of_irq.h>#include <linux/gpio.h>#include <linux/of_gpio.h>#include <linux/string.h>#include <linux/irq.h>#include <asm/mach/map.h>#include <asm/uaccess.h>#include <asm/io.h>#include <linux/interrupt.h>#include <linux/poll.h>#include <linux/fcntl.h>#include <linux/ide.h>#include <linux/platform_device.h>/* 寄存器物理地址 */#define CCM_CCGR1_BASE (0X020C406C)#define SW_MUX_GPIO1_IO03_BASE (0X020E0068)#define SW_PAD_GPIO1_IO03_BASE (0X020E02F4)#define GPIO1_DR_BASE (0X0209C000)#define GPIO1_GDIR_BASE (0X0209C004)#define REGISTER_LENGTH 4void leddevice_release(struct device *dev){printk("leddevice release\r\n");}static struct resource led_resources[] = {[0] = {.start = CCM_CCGR1_BASE,.end = CCM_CCGR1_BASE + REGISTER_LENGTH - 1,.flags = IORESOURCE_MEM,},[1] = {.start = SW_MUX_GPIO1_IO03_BASE,.end = SW_MUX_GPIO1_IO03_BASE + REGISTER_LENGTH - 1,.flags = IORESOURCE_MEM,},[2] = {.start = SW_PAD_GPIO1_IO03_BASE,.end = SW_PAD_GPIO1_IO03_BASE + REGISTER_LENGTH - 1,.flags = IORESOURCE_MEM,},[3] = {.start = GPIO1_DR_BASE,.end = GPIO1_DR_BASE + REGISTER_LENGTH - 1,.flags = IORESOURCE_MEM,},[4] = {.start = GPIO1_GDIR_BASE,.end = GPIO1_GDIR_BASE + REGISTER_LENGTH - 1,.flags = IORESOURCE_MEM,}};static struct platform_device leddevice = {.name = "imx6ull-led",.id = -1, //-1 表示此设备无ID.dev = {.release = leddevice_release,},.num_resources = ARRAY_SIZE(led_resources),.resource = led_resources,};/*设备加载*/static int __init leddevice_init(void){/*注册platform设备*/return platform_device_register(&leddevice);}/*设备卸载*/static void __exit leddevice_exit(void){platform_device_unregister(&leddevice);}module_init(leddevice_init);module_exit(leddevice_exit);MODULE_LICENSE("GPL");MODULE_AUTHOR("supersmart");

leddriver.c 内容如下

#include <linux/module.h>#include <linux/kernel.h>#include <linux/init.h>#include <linux/fs.h>#include <linux/slab.h>#include <linux/uaccess.h>#include <linux/io.h>#include <linux/cdev.h>#include <linux/device.h>#include <linux/of.h>#include <linux/of_address.h>#include <linux/of_irq.h>#include <linux/gpio.h>#include <linux/of_gpio.h>#include <linux/string.h>#include <linux/irq.h>#include <asm/mach/map.h>#include <asm/uaccess.h>#include <asm/io.h>#include <linux/interrupt.h>#include <linux/poll.h>#include <linux/fcntl.h>#include <linux/ide.h>#include <linux/platform_device.h>#define PLATFORM_CNT 1/* 设备号个数 */#define PLATFORM_NAME "platled" /* 名字 *//* 映射后的寄存器虚拟地址指针 */static void __iomem *IMX6U_CCM_CCGR1;static void __iomem *SW_MUX_GPIO1_IO03;static void __iomem *SW_PAD_GPIO1_IO03;static void __iomem *GPIO1_DR;static void __iomem *GPIO1_GDIR;#define LEDOFF 0 /* 关灯 */#define LEDON 1 /* 开灯 *//* newchrled设备结构体 */struct newchrled_dev{dev_t devid; /* 设备号 */struct cdev cdev;/* cdev */struct class *class; /* 类 */struct device *device; /* 设备 */int major; /* 主设备号 */int minor; /* 次设备号 */};struct newchrled_dev newchrled; /* led设备 *//** @description: LED打开/关闭* @param - sta : LEDON(0) 打开LED,LEDOFF(1) 关闭LED* @return : 无*/void led_switch(u8 sta){u32 val = 0;if (sta == LEDON){val = readl(GPIO1_DR);val &= ~(1 << 3);writel(val, GPIO1_DR);}else if (sta == LEDOFF){val = readl(GPIO1_DR);val |= (1 << 3);writel(val, GPIO1_DR);}}/** @description: 打开设备* @param - inode : 传递给驱动的inode* @param - filp : 设备文件,file结构体有个叫做private_data的成员变量* 一般在open的时候将private_data指向设备结构体。* @return : 0 成功;其他 失败*/static int led_open(struct inode *inode, struct file *filp){filp->private_data = &newchrled; /* 设置私有数据 */return 0;}/** @description: 从设备读取数据 * @param - filp : 要打开的设备文件(文件描述符)* @param - buf : 返回给用户空间的数据缓冲区* @param - cnt : 要读取的数据长度* @param - offt : 相对于文件首地址的偏移* @return : 读取的字节数,如果为负值,表示读取失败*/static ssize_t led_read(struct file *filp, char __user *buf, size_t cnt, loff_t *offt){return 0;}/** @description: 向设备写数据 * @param - filp : 设备文件,表示打开的文件描述符* @param - buf : 要写给设备写入的数据* @param - cnt : 要写入的数据长度* @param - offt : 相对于文件首地址的偏移* @return : 写入的字节数,如果为负值,表示写入失败*/static ssize_t led_write(struct file *filp, const char __user *buf, size_t cnt, loff_t *offt){int retvalue;unsigned char databuf[1];unsigned char ledstat;retvalue = copy_from_user(databuf, buf, cnt);if (retvalue < 0){printk("kernel write failed!\r\n");return -EFAULT;}ledstat = databuf[0]; /* 获取状态值 */if (ledstat == LEDON){led_switch(LEDON); /* 打开LED灯 */}else if (ledstat == LEDOFF){led_switch(LEDOFF); /* 关闭LED灯 */}return 0;}/** @description: 关闭/释放设备* @param - filp : 要关闭的设备文件(文件描述符)* @return : 0 成功;其他 失败*/static int led_release(struct inode *inode, struct file *filp){return 0;}/* 设备操作函数 */static struct file_operations newchrled_fops = {.owner = THIS_MODULE,.open = led_open,.read = led_read,.write = led_write,.release = led_release,};static int led_probe(struct platform_device *dev){int i = 0;struct resource *ledsource[5];unsigned int val = 0;// printk("led_driver probe\r\n");/* 初始化LED,字符设备驱动 *//* 1.从设备中获取资源 */for (i = 0; i < 5; i++){ledsource[i] = platform_get_resource(dev, IORESOURCE_MEM, i);if (ledsource[i] == NULL)return -EINVAL;}// ledsource[0]->end - ledsource[0]->start +1/* 内存映射 *//* 初始化LED *//* 1、寄存器地址映射 */IMX6U_CCM_CCGR1 = ioremap(ledsource[0]->start, resource_size(ledsource[0]));SW_MUX_GPIO1_IO03 = ioremap(ledsource[1]->start, resource_size(ledsource[1]));SW_PAD_GPIO1_IO03 = ioremap(ledsource[2]->start, resource_size(ledsource[2]));GPIO1_DR = ioremap(ledsource[3]->start, resource_size(ledsource[3]));GPIO1_GDIR = ioremap(ledsource[4]->start, resource_size(ledsource[4]));/* 2、使能GPIO1时钟 */val = readl(IMX6U_CCM_CCGR1);val &= ~(3 << 26); /* 清楚以前的设置 */val |= (3 << 26); /* 设置新值 */writel(val, IMX6U_CCM_CCGR1);/* 3、设置GPIO1_IO03的复用功能,将其复用为* GPIO1_IO03,最后设置IO属性。*/writel(5, SW_MUX_GPIO1_IO03);/*寄存器SW_PAD_GPIO1_IO03设置IO属性*bit 16:0 HYS关闭*bit [15:14]: 00 默认下拉*bit [13]: 0 kepper功能*bit [12]: 1 pull/keeper使能*bit [11]: 0 关闭开路输出*bit [7:6]: 10 速度100Mhz*bit [5:3]: 110 R0/6驱动能力*bit [0]: 0 低转换率*/writel(0x10B0, SW_PAD_GPIO1_IO03);/* 4、设置GPIO1_IO03为输出功能 */val = readl(GPIO1_GDIR);val &= ~(1 << 3); /* 清除以前的设置 */val |= (1 << 3); /* 设置为输出 */writel(val, GPIO1_GDIR);/* 5、默认关闭LED */val = readl(GPIO1_DR);val |= (1 << 3);writel(val, GPIO1_DR);/* 注册字符设备驱动 *//* 1、创建设备号 */if (newchrled.major){/* 定义了设备号 */newchrled.devid = MKDEV(newchrled.major, 0);register_chrdev_region(newchrled.devid, PLATFORM_CNT, PLATFORM_NAME);}else{/* 没有定义设备号 */alloc_chrdev_region(&newchrled.devid, 0, PLATFORM_CNT, PLATFORM_NAME); /* 申请设备号 */newchrled.major = MAJOR(newchrled.devid);/* 获取分配号的主设备号 */newchrled.minor = MINOR(newchrled.devid);/* 获取分配号的次设备号 */}printk("newcheled major=%d,minor=%d\r\n", newchrled.major, newchrled.minor);/* 2、初始化cdev */newchrled.cdev.owner = THIS_MODULE;cdev_init(&newchrled.cdev, &newchrled_fops);/* 3、添加一个cdev */cdev_add(&newchrled.cdev, newchrled.devid, PLATFORM_CNT);/* 4、创建类 */newchrled.class = class_create(THIS_MODULE, PLATFORM_NAME);if (IS_ERR(newchrled.class)){return PTR_ERR(newchrled.class);}/* 5、创建设备 */newchrled.device = device_create(newchrled.class, NULL, newchrled.devid, NULL, PLATFORM_NAME);if (IS_ERR(newchrled.device)){return PTR_ERR(newchrled.device);}return 0;}static int led_remove(struct platform_device *dev){printk("led_driver remove\r\n");/* 取消映射 */iounmap(IMX6U_CCM_CCGR1);iounmap(SW_MUX_GPIO1_IO03);iounmap(SW_PAD_GPIO1_IO03);iounmap(GPIO1_DR);iounmap(GPIO1_GDIR);/* 注销字符设备驱动 */cdev_del(&newchrled.cdev); /* 删除cdev */unregister_chrdev_region(newchrled.devid, PLATFORM_CNT); /* 注销设备号 */device_destroy(newchrled.class, newchrled.devid);class_destroy(newchrled.class);return 0;}/*platform 驱动结构体*/static struct platform_driver led_driver = {.driver = {.name = "imx6ull-led", /*驱动名字,用于和设备匹配*/},.probe = led_probe,.remove = led_remove,};/*驱动加载*/static int __init leddriver_init(void){/*注册platform驱动*/return platform_driver_register(&led_driver);}/*驱动卸载*/static void __exit leddriver_exit(void){platform_driver_unregister(&led_driver);}module_init(leddriver_init);module_exit(leddriver_exit);MODULE_LICENSE("GPL");MODULE_AUTHOR("supersmart");

platledAPP.c文件内容如下

#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <stdio.h>#include <unistd.h>#include <stdlib.h>#include <string.h>/**argc:应用程序参数个数*argv[]:具体的参数内容,字符串形式 *./platledAPP <filename> <0:1> 0表示关灯,1表示开灯* ./platledAPP /dev/platled 0 关灯* ./platledAPP /dev/platled 1 开灯*/#define LEDOFF 0#define LEDON 1int main(int argc, char *argv[]){int fd, retvalue;char *filename;unsigned char databuf[1];if(argc != 3) {printf("Error Usage!\r\n");return -1;}filename = argv[1];fd = open(filename, O_RDWR);if(fd < 0) {printf("file %s open failed!\r\n", filename);return -1;}databuf[0] = atoi(argv[2]); /* 将字符转换为数字 */retvalue = write(fd, databuf, sizeof(databuf));if(retvalue < 0) {printf("LED Control Failed!\r\n");close(fd);return -1;}close(fd);return 0;}

5 运行测试

5.1 编译驱动程序和测试 APP

1、Makefile 文件内容如下

KERNELDIR := /home/znn/linux/IMX6ULL/linux/linux-imx-rel_imx_4.1.15_2.1.0_gaCURRENT_PAHT := $(shell pwd)obj-m := leddevice.o leddriver.obuild :kernel_moduleskernel_modules:$(MAKE) -C $(KERNELDIR) M=$(CURRENT_PAHT) modulesclean:$(MAKE) -C $(KERNELDIR) M=$(CURRENT_PAHT) clean

2、编译驱动和测试APP

5.2 运行测试

将 编译出来 leddevice.ko 、 leddriver.ko 和 ledApp 这 两 个 文 件 拷 贝 到rootfs/lib/modules/4.1.15 目录中,重启开发板,进入到目录 lib/modules/4.1.15 中,加载驱动

根文件系统中/sys/bus/platform/目录下保存着当前板子 platform 总线下的设备和驱动,其中

devices 子目录为 platform 设备,drivers 子目录为 plartofm 驱动。

查看/sys/bus/platform/devices/目录,看看我们的设备是否存在,我们在 leddevice.c 中设置 leddevice(platform_device 类型)的name 字段为“imx6ull-led”。

同理,查看/sys/bus/platform/drivers/目录,看一下驱动是否存在,我们在 leddriver.c 中设置

led_driver (platform_driver 类型)的 name 字段为“imx6ull-led”,因此会在/sys/bus/platform/drivers/

目录下存在名为“imx6ull-led”这个文件,

驱动和设备匹配成功以后就可以测试 LED 灯驱动了,输入如下命令打开 LED 灯:

输入如下命令关闭 LED 灯:

卸载驱动

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。