欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 手游 > LINUX内核驱动-总线-设备模型

LINUX内核驱动-总线-设备模型

2024/10/24 0:09:02 来源:https://blog.csdn.net/weixin_44651073/article/details/143110165  浏览:    关键词:LINUX内核驱动-总线-设备模型

一、Linux驱动的分离与分层
       

1、驱动的分离  

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

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

从图 54.1.1.1 可以看出,每种平台下都有一个主机驱动和设备驱动,主机驱动肯定是必须
要的,毕竟不同的平台其 I2C 控制器不同。但是右侧的设备驱动就没必要每个平台都写一个,
因为不管对于那个 SOC 来说, MPU6050 都是一样,通过 I2C 接口读写数据就行了,只需要一
MPU6050 的驱动程序即可。如果再来几个 I2C 设备,比如 AT24C02 FT5206( 电容触摸屏 )
等,如果按照图 54.1.1 中的写法,那么设备端的驱动将会重复的编写好几次。显然在 Linux
动程序中这种写法是不推荐的,最好的做法就是每个平台的 I2C 控制器都提供一个统一的接口
( 也叫做主机驱动 ) ,每个设备的话也只提供一个驱动程序 ( 设备驱动 ) ,每个设备通过统一的 I2C
接口驱动来访问,这样就可以大大简化驱动文件,比如 54.1.1 中三种平台下的 MPU6050 驱动
框架就可以简化为图 54.1.1.2 所示:
实际的 I2C 驱动设备肯定有很多种,不止 MPU6050 这一个,那么实际的驱动架构如图
54.1.1.3 所示
这个就是驱动的分隔,也就是将主机驱动和设备驱动分隔开来,比如 I2C SPI 等等都会采
用驱动分隔的方式来简化驱动的开发。在实际的驱动开发中,一般 I2C 主机控制器驱动已经由
半导体厂家编写好了,而设备驱动一般也由设备器件的厂家编写好了,我们只需要提供设备信
息即可,比如 I2C 设备的话提供设备连接到了哪个 I2C 接口上, I2C 的速度是多少等等。相当
于将设备信息从设备驱动中剥离开来,驱动使用标准方法去获取到设备信息 ( 比如从设备树中获
取到设备信息 ) ,然后根据获取到的设备信息来初始化设备。 这样就相当于驱动只负责驱动,
设备只负责设备,想办法将两者进行匹配即可。这个就是 Linux 中的总线 (bus) 、驱动 (driver)
设备 (device) 模型,也就是常说的驱动分离。总线就是驱动和设备信息的月老,负责给两者牵线
搭桥,如图 54.1.1.4 所示:

当我们向系统注册一个驱动的时候,总线就会在右侧的设备中查找,看看有没有与之匹配
的设备,如果有的话就将两者联系起来。同样的,当向系统中注册一个设备的时候,总线就会
在左侧的驱动中查找看有没有与之匹配的设备,有的话也联系起来。Linux 内核中大量的驱动
程序都采用总线、驱动和设备模式。 
        2、驱动的分层


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

 二、平台驱动模型


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

         1、platform总线


 Linux系统内核使用bus_type 结构体表示总线,此结构体bus_type 内容如下:

/* 定义在文件 include/linux/device.h */ 
struct bus_type {const char        *name;                        /* 总线名字 */const char        *dev_name;struct device        *dev_root;struct device_attribute    *dev_attrs;    /* use dev_groups instead */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 类型,也就是设备和驱动。
        platform 总线是 bus_type 的一个具体实例,platform总线定义如下:

/* 定义在文件 drivers/base/platform.c */ 
struct bus_type platform_bus_type = {.name        = "platform",.dev_groups    = platform_dev_groups,.match        = platform_match,.uevent        = platform_uevent,.pm        = &platform_dev_pm_ops,
};
EXPORT_SYMBOL_GPL(platform_bus_type);


        platform_bus_type 就是 platform 平台总线,其中 platform_match 就是匹配函数。我们来看一下驱动和设备是如何匹配的, platform_match 函数定义在文件 drivers/base/platform.c 中,函数内容如下所示:

        在platform平台总线匹配函数中,有四种匹配方式:

        1)、OF 类型的匹配


        也就是设备树采用的匹配方式of_driver_match_device 函数。 device_driver 结构体(表示设备驱动)中有个名为of_match_table的成员变量

/* 定义在include/linux/of_device.h  */
struct device_driver {const char        *name;struct bus_type        *bus;struct module        *owner;const char        *mod_name;    /* used for built-in modules */bool suppress_bind_attrs;    /* disables bind/unbind via sysfs */const struct of_device_id    *of_match_table;const struct acpi_device_id    *acpi_match_table;int (*probe) (struct device *dev);int (*remove) (struct device *dev);void (*shutdown) (struct device *dev);int (*suspend) (struct device *dev, pm_message_t state);int (*resume) (struct device *dev);const struct attribute_group **groups;const struct dev_pm_ops *pm;struct driver_private *p;
};
struct of_device_id {char    name[32];char    type[32];char    compatible[128];const void *data;
};

        此成员变量保存着驱动的compatible匹配表,设备树中的每个设备节点的 compatible 属性会和 of_match_table 表中的所有成员比较,查看是否有相同的条目,如果有的话就表示设备和此驱动匹配,设备和驱动匹配成功以后,驱动的 probe 函数就会执行。

        2)、ACPI匹配

        3)、id_table匹配


        每个 platform_driver 结构体有一个 id_table成员变量,如下所示

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;
};


        顾名思义,保存了很多 id 信息。这些 id 信息存放着这个 platformd 驱动所支持的驱动类型。

        4)、 strcmp比较名字


                将struct platform_device设备结构体中的name和struct platform_driver 驱动结构体中的driver中的name对比,如果name相等表示设备和此驱动匹配

struct platform_device设备结构体如下:

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;
};

struct platform_driver结构体如下:
 
 

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 char        *name;struct bus_type        *bus;struct module        *owner;const char        *mod_name;    /* used for built-in modules */bool suppress_bind_attrs;    /* disables bind/unbind via sysfs */const struct of_device_id    *of_match_table;const struct acpi_device_id    *acpi_match_table;int (*probe) (struct device *dev);int (*remove) (struct device *dev);void (*shutdown) (struct device *dev);int (*suspend) (struct device *dev, pm_message_t state);int (*resume) (struct device *dev);const struct attribute_group **groups;const struct dev_pm_ops *pm;struct driver_private *p;}const struct platform_device_id *id_table;bool prevent_deferred_probe;
};
        2、 platform驱动

总线数据类型为:bus_type。向内核注册总线使用bus_register。

struct bus_type {
const char *name;
const char *dev_name;
struct device *dev_root;
struct device_attribute *dev_attrs; /* use dev_groups instead */
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;
};

向linux内核注册总线,使用bus_register函数。bus_unregister函数卸载。

总线主要工作就是完成总线下的设备和驱动之间的匹配。

驱动数据类型为device_driver,驱动程序向内核注册驱动采用driver_register。

struct device_driver {
const char *name;
struct bus_type *bus;
struct module *owner;
const char *mod_name; /* used for built-in modules */
bool suppress_bind_attrs; /* disables bind/unbind via sysfs */
const struct of_device_id *of_match_table;
const struct acpi_device_id *acpi_match_table;
int (*probe) (struct device *dev);
int (*remove) (struct device *dev);
void (*shutdown) (struct device *dev);
int (*suspend) (struct device *dev, pm_message_t state);
int (*resume) (struct device *dev);
const struct attribute_group **groups;
const struct dev_pm_ops *pm;
struct driver_private *p;
};

驱动和设备匹配以后驱动里面的probe函数就会执行。使用driver_register注册驱动。

driver_register-> bus_add_driver-> driver_attach		//查找bus下所有设备,找预期匹配的。->bus_for_each_dev(drv->bus, NULL, drv, __driver_attach); ->__driver_attach	//每个设备都调用此函数,//查看每个设备是否与驱动匹配-> driver_match_device  //检查是否匹配。-> driver_probe_device  ->really_probe-> drv->probe(dev);  //执行driver的probe函数。

向总线注册驱动的时候,会检查当前总线下的所有设备,有没有与此驱动匹配的设备,如果有的话就执行驱动里面的probe函数。


        1)、platform驱动表示


        platform_driver结构体表示 platform 驱 动 , 内容如下:

/* 定义在include/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 继承了这个基类,然后在此基础上又添加了一些特有的成员变量。

/* 定义在linux/device.h */struct device_driver {const char        *name;struct bus_type        *bus;struct module        *owner;const char        *mod_name;    /* used for built-in modules */bool suppress_bind_attrs;    /* disables bind/unbind via sysfs */ const struct of_device_id    *of_match_table;const struct acpi_device_id    *acpi_match_table;int (*probe) (struct device *dev);int (*remove) (struct device *dev);void (*shutdown) (struct device *dev);int (*suspend) (struct device *dev, pm_message_t state);int (*resume) (struct device *dev);const struct attribute_group **groups;const struct dev_pm_ops *pm;struct driver_private *p;
};

        id_table 表,也就是我们前面讲解 platform总线匹配驱动和设备的时候采用的第三种方法,id_table是个表(也就是数组) 每个元素的类型为platform_device_id,platform_device_id结构体内容如下:

/* 定义在linux/mod_devicerable.h */
struct platform_device_id {char name[PLATFORM_NAME_SIZE];kernel_ulong_t driver_data;
};


        在struct device_driver结构体中of_match_table 就是采用设备树的时候驱动使用的匹配表,同样是数组,每个匹配项都为 of_device_id 结构体类型,内容如下:

/* 定义在linux/mod_devicetable.h中 */
struct of_device_id {char    name[32];char    type[32];char    compatible[128];const void *data;
};


        compatible 非常重要,因为对于设备树而言,就是通过设备节点的 compatible 属性值和 of_match_table 中每个项目的 compatible 成员变量进行比较,如果有相等的就表示设备和此驱动匹配成功。

        2)、platform驱动框架


        在编写 platform 驱动的时候,首先定义一个 platform_driver 结构体变量,然后实现结构体中的各个成员变量,重点是实现匹配方法以及 probe 函数。当驱动和设备匹配成功以后 probe函数就会执行,具体的驱动程序在 probe 函数里面编写,比如字符设备驱动等等。

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

/* 定义在linux/platform_device.h */int platform_driver_register (struct platform_driver *driver)driver:要注册的 platform 驱动。
返回值:负数,失败; 0,成功。

        还需要在驱动卸载函数中通过platform_driver_unregister函数卸载 platform 驱动,platform_driver_unregister 函数原型如下:

/* 定义在linux/platform_device.h */
void platform_driver_unregister (struct platform_driver *driver)
driver:要卸载的 platform 驱动。

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("zhangxueguo");
        3、platform设备


        platform_device 这个结构体表示 platform 设备,这里我们要注意,如果内核支持设备树的话就不要再使用 platform_device 来描述设备了,因为改用设备树去描述了。当然了,你如果一定要用 platform_device 来描述设备信息的话也是可以的。

        1)、无设备树情况

根据前面的分析,驱动和设备匹配是通过bus->match函数,platform总线下的match函数就是:platform_match。

platform_match-> of_driver_match_device,设备树-> acpi_driver_match_device ACPI类型的-> platform_match_id 根据platform_driver-> id_table-> strcmp(pdev->name, drv->name)  
//最终的就是比较字符串,就是platform_device->name,和platform_driver->driver->name。
//无设备树情况下使用。


      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 表示资源数量,一般为 resource 资源的大小。

        resource 表示资源,也就是设备信息,比如外设寄存器等。 Linux 内核使用 resource结构体表示资源, resource 结构体内容如下:

/* 定义在 linux/ioport.h */ 
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 */ 
/** IO resources have these defined flags.*/
#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 
#define IORESOURCE_PREFETCH    0x00002000    /* No side effects */
#define IORESOURCE_READONLY    0x00004000
#define IORESOURCE_CACHEABLE    0x00008000
#define IORESOURCE_RANGELENGTH    0x00010000
#define IORESOURCE_SHADOWABLE    0x00020000
#define IORESOURCE_SIZEALIGN    0x00040000    /* size indicates alignment */
#define IORESOURCE_STARTALIGN    0x00080000    /* start field is alignment */
#define IORESOURCE_MEM_64    0x00100000
#define IORESOURCE_WINDOW    0x00200000    /* forwarded by bridge */
#define IORESOURCE_MUXED    0x00400000    /* Resource is software muxed */
#define IORESOURCE_EXCLUSIVE    0x08000000    /* Userland may not map this resource */
#define IORESOURCE_DISABLED    0x10000000
#define IORESOURCE_UNSET    0x20000000    /* No address assigned yet */
#define IORESOURCE_AUTO        0x40000000
#define IORESOURCE_BUSY        0x80000000    /* Driver has marked this resource busy */


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

/* 定义在linux/platform_device.h */
int platform_device_register(struct platform_device *pdev)pdev:要注册的 platform 设备。返回值: 负数,失败; 0,成功。


        如果不再使用 platform 的话可以通过platform_device_unregister函数注销掉相应的platform设备, platform_device_unregister 函数原型如下:

/* 定义在linux/platform_device.h */ 
void platform_device_unregister(struct platform_device *pdev)pdev:要注销的 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);
}module_init(xxxdevice_init);
module_exit(xxxdevice_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("zhangxueguo");
        2)、有设备树情况

有设备树的时候:

of_driver_match_device-> of_match_device(drv->of_match_table, dev)  //of_match_table非常重要,
类型为of_device_id。
//compatible属性


        在使用设备树的时候,设备的描述被放到了设备树中,因此 platform_device 就不需要我们去编写了,我们只需要实现 platform_driver 即可。

        ①、在设备数中创建设备节点
        毫无疑问,肯定要先在设备树中创建设备节点来描述设备信息,重点是要设置好 compatible属性的值,因为 platform 总线需要通过设备节点的 compatible 属性值来匹配驱动!这点要切记。比如,我们可以编写如下所示的设备节点来描述 LED 这个设备:


        ②、编写 platform 驱动的时候要注意兼容属性
        在使用设备树的时候 platform 驱动会通过 of_match_table 来保存兼容性值,也就是表明此驱动兼容哪些设备。所以, of_match_table 将会尤为重要,比如我们要编写led灯的platform 驱动,platform_driver 就可以按照如下所示设置:

static const struct of_device_id leds_of_match[] = {{ .compatible = "atkalpha-gpioled" }, /* 兼容属性 */{ /* Sentinel */ }
}; 
/* 通过 MODULE_DEVICE_TABLE 声明一下 leds_of_match 这个设备匹配表 */
MODULE_DEVICE_TABLE(of, leds_of_match);static struct platform_driver leds_platform_driver = {.driver = {.name = "imx6ul-led",.of_match_table = leds_of_match,},.probe = xxx_probe,        /* 自己实现 */.remove = xxx_remove,      /* 自己实现 */
};

========================

点灯设备代码leddevice.c编写

#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,.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("zuozhongkai");

===============================

驱动代码编写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_NAME  "platled"
#define PLATFORM_COUNT 1/* 地址映射后的虚拟地址指针 */
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       /* 打开 *//* LED设备结构体 */
struct newchrled_dev{struct cdev cdev;       /* 字符设备 */dev_t   devid;          /* 设备号 */struct class *class;    /* 类 */struct device *device;  /* 设备 */int major;              /* 主设备号 */int minor;              /* 次设备号 */};struct newchrled_dev newchrled; /* led设备 *//* LED灯打开/关闭 */
static void led_switch(u8 sta)
{u32 val = 0;if(sta == LEDON) {val = readl(GPIO1_DR);val &= ~(1 << 3);            /* bit3清零,打开LED灯 */writel(val, GPIO1_DR); } else if(sta == LEDOFF) {val = readl(GPIO1_DR);val |= (1 << 3);            /* bit3清零,打开LED灯 */writel(val, GPIO1_DR);}
}static int newchrled_open(struct inode *inode, struct file *filp)
{filp->private_data = &newchrled;return 0;
}static int newchrled_release(struct inode *inode, struct file *filp)
{struct newchrled_dev *dev = (struct newchrled_dev*)filp->private_data;return 0;
}static ssize_t newchrled_write(struct file *filp, const char __user *buf,size_t count, loff_t *ppos)
{int retvalue;unsigned char databuf[1];retvalue = copy_from_user(databuf, buf, count);if(retvalue < 0) {printk("kernel write failed!\r\n");return -EFAULT;}/* 判断是开灯还是关灯 */led_switch(databuf[0]);return 0;
}static const struct file_operations newchrled_fops = {.owner = THIS_MODULE,.write	= newchrled_write,.open	= newchrled_open,.release= newchrled_release,
};static int led_probe(struct platform_device *dev)
{int i = 0;struct resource *ledsource[5];int ret = 0;unsigned int val = 0;//printk("led driver proe\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;}/* 内存映射 *//* 1,初始化LED灯,地址映射 */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,初始化 */val = readl(IMX6U_CCM_CCGR1);val &= ~(3 << 26);  /* 先清除以前的配置bit26,27 */val |= 3 << 26;     /* bit26,27置1 */writel(val, IMX6U_CCM_CCGR1);writel(0x5, SW_MUX_GPIO1_IO03);     /* 设置复用 */writel(0X10B0, SW_PAD_GPIO1_IO03);  /* 设置电气属性 */val = readl(GPIO1_GDIR);val |= 1 << 3;              /* bit3置1,设置为输出 */writel(val, GPIO1_GDIR);val = readl(GPIO1_DR);val |= (1 << 3);            /* bit3置1,关闭LED灯 */writel(val, GPIO1_DR);newchrled.major = 0;    /* 设置为0,表示由系统申请设备号 *//* 2,注册字符设备 */if(newchrled.major){    /* 给定主设备号 */newchrled.devid = MKDEV(newchrled.major, 0);ret = register_chrdev_region(newchrled.devid, PLATFORM_COUNT, PLATFORM_NAME);} else {                /* 没有给定主设备号 */ret = alloc_chrdev_region(&newchrled.devid, 0, PLATFORM_COUNT, PLATFORM_NAME);newchrled.major = MAJOR(newchrled.devid);newchrled.minor = MINOR(newchrled.devid);}if(ret < 0) {printk("newchrled chrdev_region err!\r\n");goto fail_devid;}printk("newchrled major=%d, minor=%d\r\n", newchrled.major, newchrled.minor);/* 3,注册字符设备 */newchrled.cdev.owner = THIS_MODULE;cdev_init(&newchrled.cdev, &newchrled_fops);ret = cdev_add(&newchrled.cdev, newchrled.devid, PLATFORM_COUNT);if(ret < 0) {goto fail_cdev;}/* 4,自动创建设备节点 */newchrled.class = class_create(THIS_MODULE, PLATFORM_NAME);if (IS_ERR(newchrled.class)) {ret = PTR_ERR(newchrled.class);goto fail_class;}newchrled.device = device_create(newchrled.class, NULL,newchrled.devid, NULL, PLATFORM_NAME);if (IS_ERR(newchrled.device)) {ret = PTR_ERR(newchrled.device);goto fail_device;}return 0;fail_device:class_destroy(newchrled.class);
fail_class:cdev_del(&newchrled.cdev);
fail_cdev:unregister_chrdev_region(newchrled.devid, PLATFORM_COUNT);
fail_devid:return ret; 
}static int led_remove(struct platform_device *dev)
{unsigned int val = 0;printk("led driver remove\r\n");val = readl(GPIO1_DR);val |= (1 << 3);            /* bit3清零,打开LED灯 */writel(val, GPIO1_DR);/* 1,取消地址映射 */iounmap(IMX6U_CCM_CCGR1);iounmap(SW_MUX_GPIO1_IO03);iounmap(SW_PAD_GPIO1_IO03);iounmap(GPIO1_DR);iounmap(GPIO1_GDIR);/* 1,删除字符设备 */cdev_del(&newchrled.cdev);/* 2,注销设备号 */unregister_chrdev_region(newchrled.devid, PLATFORM_COUNT);/* 3,摧毁设备 */device_destroy(newchrled.class, newchrled.devid);/* 4,摧毁类 */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("zuozhongkai");

===========================

应用层代码编写
#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;

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com