Linux驱动开发8 platform驱动分隔、分离与分层
驱动分隔
驱动分离
驱动分层(学linux驱动就是学linux框架)
驱动-总线-设备
platform 平台驱动模型 (虚拟总线平台)
platform_bus_type 就是 platform 平台总线,其中 platform_match 就是匹配函数。
platform_match 函数定义在文件 drivers/base/platform.c 中
对于platform平台而言,platform_match函数就是月老,负责驱动和设备的匹配
platform 驱动
platform_driver_register 函数
无设备树platform设备注册实验(以led点灯为例:寄存器版)
#include <linux/types.h>#include <linux/kernel.h>#include <linux/delay.h>#include <linux/ide.h>#include <linux/init.h>#include <linux/module.h>#include <linux/errno.h>#include <linux/gpio.h>#include <linux/cdev.h>#include <linux/device.h>#include <linux/of_gpio.h>#include <linux/semaphore.h>#include <linux/timer.h>#include <linux/irq.h>#include <linux/wait.h>#include <linux/poll.h>#include <linux/fs.h>#include <linux/fcntl.h>#include <linux/platform_device.h>#include <asm/mach/map.h>#include <asm/uaccess.h>#include <asm/io.h>/***************************************************************Copyright © ALIENTEK Co., Ltd. 1998-2029. All rights reserved.文件名 : leddevice.c作者 : 左忠凯版本 : V1.0描述 : platform设备其他 : 无论坛 : www.openedv.com日志 : 初版V1.0 2019/8/13 左忠凯创建***************************************************************//* * 寄存器地址定义*/#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 4/* @description : 释放flatform设备模块的时候此函数会执行 * @param - dev : 要释放的设备 * @return : 无*/static void led_release(struct device *dev){printk("led device released!\r\n"); }/* * 设备资源信息,也就是LED0所使用的所有寄存器*/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,},};/** platform设备结构体 */static struct platform_device leddevice = {.name = "imx6ul-led", /* 驱动名字,用于和设备匹配 名字必须保持一致*/.id = -1,.dev = {.release = &led_release,},// 与具体资源有关.num_resources = ARRAY_SIZE(led_resources),//资源数量.resource = led_resources,//资源(设备信息)};// /** @description : 设备模块加载 * @param : 无* @return : 无*/static int __init leddevice_init(void){//注册platform设备return platform_device_register(&leddevice);}/** @description : 设备模块注销* @param : 无* @return : 无*/static void __exit leddevice_exit(void){platform_device_unregister(&leddevice);}module_init(leddevice_init);module_exit(leddevice_exit);MODULE_LICENSE("GPL");MODULE_AUTHOR("zuozhongkai");
#include <linux/types.h>#include <linux/kernel.h>#include <linux/delay.h>#include <linux/ide.h>#include <linux/init.h>#include <linux/module.h>#include <linux/errno.h>#include <linux/gpio.h>#include <linux/cdev.h>#include <linux/device.h>#include <linux/of_gpio.h>#include <linux/semaphore.h>#include <linux/timer.h>#include <linux/irq.h>#include <linux/wait.h>#include <linux/poll.h>#include <linux/fs.h>#include <linux/fcntl.h>#include <linux/platform_device.h>#include <asm/mach/map.h>#include <asm/uaccess.h>#include <asm/io.h>/***************************************************************Copyright © ALIENTEK Co., Ltd. 1998-2029. All rights reserved.文件名 : leddriver.c作者 : 左忠凯版本 : V1.0描述 : platform驱动实际内容与led点灯完全不相同,是无设备树的platform平台注册其他 : 无论坛 : www.openedv.com日志 : 初版V1.0 2019/8/13 左忠凯创建***************************************************************/#define LEDDEV_CNT 1 /* 设备号长度 */#define LEDDEV_NAME "platled" /* 设备名字 */#define LEDOFF 0#define LEDON 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;/* leddev设备结构体 */struct leddev_dev{dev_t devid; /* 设备号 */struct cdev cdev; /* cdev */struct class *class; /* 类 */struct device *device; /* 设备 */int major; /* 主设备号 */ };struct leddev_dev leddev; /* led设备 *//** @description : LED打开/关闭* @param - sta : LEDON(0) 打开LED,LEDOFF(1) 关闭LED* @return : 无*/void led0_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 = &leddev; /* 设置私有数据 */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) {return -EFAULT;}ledstat = databuf[0]; /* 获取状态值 */if(ledstat == LEDON) {led0_switch(LEDON); /* 打开LED灯 */}else if(ledstat == LEDOFF) {led0_switch(LEDOFF); /* 关闭LED灯 */}return 0;}//* 设备操作函数 */static struct file_operations led_fops = {.owner = THIS_MODULE,.open = led_open,.write = led_write,};//** @description : flatform驱动的probe函数,当驱动与设备匹配以后此函数就会执行* * @param - dev : platform设备* @return : 0,成功;其他负值,失败*/static int led_probe(struct platform_device *dev){ int i = 0;int ressize[5];u32 val = 0;struct resource *ledsource[5];printk("led driver and device has matched!\r\n");/* 1、获取资源 */for (i = 0; i < 5; i++) {// platform_get_resource 从设备中获取资源// 一共有五个 循环获取资源ledsource[i] = platform_get_resource(dev, IORESOURCE_MEM, i); /* 依次MEM类型资源 */if (!ledsource[i]) {dev_err(&dev->dev, "No MEM resource for always on\n");return -ENXIO;}ressize[i] = resource_size(ledsource[i]); } /* 2、初始化LED *//* 寄存器地址映射 */IMX6U_CCM_CCGR1 = ioremap(ledsource[0]->start, ressize[0]);SW_MUX_GPIO1_IO03 = ioremap(ledsource[1]->start, ressize[1]);SW_PAD_GPIO1_IO03 = ioremap(ledsource[2]->start, ressize[2]);GPIO1_DR = ioremap(ledsource[3]->start, ressize[3]);GPIO1_GDIR = ioremap(ledsource[4]->start, ressize[4]);val = readl(IMX6U_CCM_CCGR1);val &= ~(3 << 26); /* 清除以前的设置 */val |= (3 << 26); /* 设置新值 */writel(val, IMX6U_CCM_CCGR1);/* 设置GPIO1_IO03复用功能,将其复用为GPIO1_IO03 */writel(5, SW_MUX_GPIO1_IO03);writel(0x10B0, SW_PAD_GPIO1_IO03);/* 设置GPIO1_IO03为输出功能 */val = readl(GPIO1_GDIR);val &= ~(1 << 3); /* 清除以前的设置 */val |= (1 << 3); /* 设置为输出 */writel(val, GPIO1_GDIR);/* 默认关闭LED1 */val = readl(GPIO1_DR);val |= (1 << 3) ; writel(val, GPIO1_DR);/* 注册字符设备驱动 *//*1、创建设备号 */if (leddev.major) { /* 定义了设备号 */leddev.devid = MKDEV(leddev.major, 0);register_chrdev_region(leddev.devid, LEDDEV_CNT, LEDDEV_NAME);} else { /* 没有定义设备号 */alloc_chrdev_region(&leddev.devid, 0, LEDDEV_CNT, LEDDEV_NAME); /* 申请设备号 */leddev.major = MAJOR(leddev.devid); /* 获取分配号的主设备号 */}/* 2、初始化cdev */leddev.cdev.owner = THIS_MODULE;cdev_init(&leddev.cdev, &led_fops);/* 3、添加一个cdev */cdev_add(&leddev.cdev, leddev.devid, LEDDEV_CNT);/* 4、创建类 */leddev.class = class_create(THIS_MODULE, LEDDEV_NAME);if (IS_ERR(leddev.class)) {return PTR_ERR(leddev.class);}/* 5、创建设备 */leddev.device = device_create(leddev.class, NULL, leddev.devid, NULL, LEDDEV_NAME);if (IS_ERR(leddev.device)) {return PTR_ERR(leddev.device);}return 0;}//** @description : platform驱动的remove函数,移除platform驱动的时候此函数会执行* @param - dev : platform设备* @return : 0,成功;其他负值,失败*/static int led_remove(struct platform_device *dev){iounmap(IMX6U_CCM_CCGR1);iounmap(SW_MUX_GPIO1_IO03);iounmap(SW_PAD_GPIO1_IO03);iounmap(GPIO1_DR);iounmap(GPIO1_GDIR);cdev_del(&leddev.cdev);/* 删除cdev */unregister_chrdev_region(leddev.devid, LEDDEV_CNT); /* 注销设备号 */device_destroy(leddev.class, leddev.devid);class_destroy(leddev.class);return 0;}//* platform驱动结构体 */static struct platform_driver led_driver = {.driver = {.name = "imx6ul-led", /* 驱动名字,用于和设备匹配 名字必须保持一致*/},.probe = led_probe,//注册函数名.remove = led_remove,//卸载函数名};/ /** @description : 驱动模块加载函数* @param : 无* @return : 无*/static int __init leddriver_init(void){//注册platform设备return platform_driver_register(&led_driver);}/** @description : 驱动模块卸载函数* @param : 无* @return : 无*/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");
有设备树实验(只需要修改设备树和编写驱动)
platform提供了很多API函数去获取设备相关信息,platform可以直接获取设备信息了,可以不需要获取设备节点那一套了
//设备树gpioled {#address-cells = <1>;#size-cells = <1>;compatible = "atkalpha-gpioled";pinctrl-names = "default";pinctrl-0 = <&pinctrl_led>;led-gpio = <&gpio1 3 GPIO_ACTIVE_LOW>;status = "okay";};
#include <linux/types.h>#include <linux/kernel.h>#include <linux/delay.h>#include <linux/ide.h>#include <linux/init.h>#include <linux/module.h>#include <linux/errno.h>#include <linux/gpio.h>#include <linux/cdev.h>#include <linux/device.h>#include <linux/of_gpio.h>#include <linux/semaphore.h>#include <linux/timer.h>#include <linux/irq.h>#include <linux/wait.h>#include <linux/poll.h>#include <linux/fs.h>#include <linux/fcntl.h>#include <linux/platform_device.h>#include <asm/mach/map.h>#include <asm/uaccess.h>#include <asm/io.h>/***************************************************************Copyright © ALIENTEK Co., Ltd. 1998-2029. All rights reserved.文件名 : leddriver.c作者 : 左忠凯版本 : V1.0描述 : 设备树下的platform驱动其他 : 无论坛 : www.openedv.com日志 : 初版V1.0 2019/8/13 左忠凯创建***************************************************************/#define LEDDEV_CNT 1 /* 设备号长度 */#define LEDDEV_NAME "dtsplatled" /* 设备名字 */#define LEDOFF 0#define LEDON 1/* leddev设备结构体 */struct leddev_dev{dev_t devid; /* 设备号 */struct cdev cdev; /* cdev */struct class *class; /* 类 */struct device *device; /* 设备 */int major; /* 主设备号 */ struct device_node *node; /* LED设备节点 *///int led0; /* LED灯GPIO标号 *///};struct leddev_dev leddev; /* led设备 *//** @description : LED打开/关闭* @param - sta : LEDON(0) 打开LED,LEDOFF(1) 关闭LED* @return : 无*/void led0_switch(u8 sta){if (sta == LEDON )gpio_set_value(leddev.led0, 0);else if (sta == LEDOFF)gpio_set_value(leddev.led0, 1); }/** @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 = &leddev; /* 设置私有数据 */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[2];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) {led0_switch(LEDON);} else if (ledstat == LEDOFF) {led0_switch(LEDOFF);}return 0;}/* 设备操作函数 */static struct file_operations led_fops = {.owner = THIS_MODULE,.open = led_open,.write = led_write,};/** @description : flatform驱动的probe函数,当驱动与* 设备匹配以后此函数就会执行* @param - dev : platform设备* @return : 0,成功;其他负值,失败* ××××××××××××××××××××××××××××××××××××××××××××××××* 之所以与无设备树platform驱动程序不同,* 是因为一个是直接调用寄存器操作 即 寄存器led* 现在这个是寄存器定义在了设备树里* 然后提取设备树的信息操作 即 gpioled* 其实相比以前的 多了一层壳*/static int led_probe(struct platform_device *dev){ printk("led driver and device was matched!\r\n");/* 1、设置设备号 */if (leddev.major) {leddev.devid = MKDEV(leddev.major, 0);register_chrdev_region(leddev.devid, LEDDEV_CNT, LEDDEV_NAME);} else {alloc_chrdev_region(&leddev.devid, 0, LEDDEV_CNT, LEDDEV_NAME);leddev.major = MAJOR(leddev.devid);}/* 2、注册设备 */cdev_init(&leddev.cdev, &led_fops);cdev_add(&leddev.cdev, leddev.devid, LEDDEV_CNT);/* 3、创建类 */leddev.class = class_create(THIS_MODULE, LEDDEV_NAME);if (IS_ERR(leddev.class)) {return PTR_ERR(leddev.class);}/* 4、创建设备 */leddev.device = device_create(leddev.class, NULL, leddev.devid, NULL, LEDDEV_NAME);if (IS_ERR(leddev.device)) {return PTR_ERR(leddev.device);}#if 0/* 5、初始化IO */ leddev.node = of_find_node_by_path("/gpioled");if (leddev.node == NULL){printk("gpioled node nost find!\r\n");return -EINVAL;} #endif// dev.of_node代替 of_find_node_by_path 获取设备节点gpioled.nd = dev->dev.of_node; leddev.led0 = of_get_named_gpio(leddev.node, "led-gpio", 0);if (leddev.led0 < 0) {printk("can't get led-gpio\r\n");return -EINVAL;}gpio_request(leddev.led0, "led0");gpio_direction_output(leddev.led0, 1); /* led0 IO设置为输出,默认高电平 */return 0;}/** @description : platform驱动的remove函数,移除platform驱动的时候此函数会执行* @param - dev : platform设备* @return : 0,成功;其他负值,失败*/static int led_remove(struct platform_device *dev){gpio_set_value(leddev.led0, 1); /* 卸载驱动的时候关闭LED */gpio_free(leddev.led0); /* 释放IO */cdev_del(&leddev.cdev); /* 删除cdev */unregister_chrdev_region(leddev.devid, LEDDEV_CNT); /* 注销设备号 */device_destroy(leddev.class, leddev.devid);class_destroy(leddev.class);return 0;}/* 匹配列表只要设备树的compatible与这两个中的一个匹配,即建立联系*/static const struct of_device_id led_of_match[] = {{ .compatible = "atkalpha-gpioled" },{ .compatible = "xxx-gpioled" },{ /* Sentinel */ } //用它结尾,没有意义,但别人都这么做};/* platform驱动结构体 */static struct platform_driver led_driver = {.driver = {.name = "imx6ul-led", /* 驱动名字,用于和设备匹配 */.of_match_table = led_of_match, /* 设备树匹配表 */},.probe = led_probe,.remove = led_remove,};/// /** @description : 驱动模块加载函数* @param : 无* @return : 无*/static int __init leddriver_init(void){return platform_driver_register(&led_driver);}/** @description : 驱动模块卸载函数* @param : 无* @return : 无*/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");
Linux驱动开发8 platform驱动分隔、分离与分层相关推荐
- <Linux开发>驱动开发 -之-platform 驱动
<Linux开发>驱动开发 -之-platform 驱动 交叉编译环境搭建: <Linux开发> linux开发工具-之-交叉编译环境搭建 uboot移植可参考以下: < ...
- linux驱动开发 - 12_platform 平台驱动模型
文章目录 platform 平台驱动模型 1 platform 总线 platform匹配过程 2 platform 驱动 platform 驱动框架如下所示: 3 platform 设备 platf ...
- Linux嵌入式驱动开发07——GPIO驱动过程记录(飞凌开发板)
文章目录 全系列传送门 1. 在/arch/arm/boot/dts/imx6q-pinfunc.h查找 2. 在设备树配置文件中添加设备节点定义以及其引脚定义 3. 修改设备树文件添加配置 4. d ...
- 驱动开发中platform设备驱动架构详解
1.什么是platform总线 从Linux2.6开始Linux加入了一套驱动管理和注册机制-platform总线驱动模型.platform总线是一条虚拟总线(只有一条),这类总线没有对应的硬件结构. ...
- <Linux开发>--驱动开发-- 字符设备驱动(3) 过程详细记录
<Linux开发>–驱动开发-- 字符设备驱动(3) 过程详细记录 驱动开发是建立再系统之上的,前面作者也记录了系统移植的过程记录,如果有兴趣,可进入博主的主页查看相关文章,这里就不添加链 ...
- Linux驱动开发1:驱动开发与裸机开发的区别
Linux驱动开发1:驱动开发与裸机开发的区别 1.裸机驱动开发回顾: 裸机驱动开发是非常底层的,跟寄存器打交道,有些MCU为了方便我们开发,提供了一些库,让我们通过调用API函数来间接的实现利用寄存 ...
- Linux驱动开发—内核I2C驱动详解
Linux驱动开发--内核I2C驱动 I2C驱动文件结构 I2C数据传输过程 i2c_transfer i2c_msg I2C通讯常用的接口函数(老版本) 快速读写接口函数:(连续读写) 常用的读操作 ...
- I.MX6ULL ARM驱动开发---platfrom设备驱动
引言 对IO进行最简单的读写操作,设备驱动都非常的简单.像I2C.SPI.LCD 等这些复杂外设的驱动就不能这么去写了,Linux 系统要考虑到驱动的可重用性,因此提出了驱动的分离与分层这样的软件 ...
- STM32MP157驱动开发——4G通信模块驱动
STM32MP157驱动开发--4G通信模块驱动 一.简介 二.驱动开发 1.高新兴 ME3630 驱动开发 驱动修改 添加 ECM 支持程序 配置 Linux 内核 ppp拨号功能测试 ECM 联网 ...
最新文章
- Postman请求linux服务器报错:503Forwarding failure,由于服务器端口未开放;服务器端口开放与关闭
- 用一个二维码做下载地址,自动区分是 ios 还是 android
- apache 提示You don't have permission to access /test.php on this server.怎样解决
- 未处理异常和C++异常——Windows核心编程学习手札之二十五
- Java 中初始化 List 集合的 6 种方式!
- 减少模型方差的方法借鉴
- 如何设置 jqplot 图表插件的标题图例和直线
- BZOJ1179 Atm //缩点+spfa
- Spring Boot入门到牛X
- java字符串拆分成数组_Java StringUtils字符串分割转数组的实现
- C++---模板特化
- python复制csv数据_如何使用Python将CSV数据复制到现有xlsx文件
- 电信光猫破解 (打开无线wifi及路由功能)
- 计算机组成原理读写周期波形图,计算机组成原理_第八章
- PMOS的电流方向以及工作区理解
- 开发愤怒的小鸟的Lua语言:Wax框架详解
- 三角函数中的和差化积公式编辑方法
- vue使用高德地图第一次进去点标记有数据,第二次进去就没有了
- 【札记】Python处理TSV文件以及144790个英语单词的注音、释义、例句的.sql和.tsv文件下载
- 《数据结构与算法之红黑树(Java实现)》