platform是Linux内核抽象出来的软件代码,用于设备与驱动的连接,设备与驱动通过总线进行匹配;匹配成功后会执行驱动中的probe函数,在probe函数中可以获取到设备的信息;
设备与驱动的信息分别通过链表进行管理。
编写驱动和设备端的代码----搭建platform框架
设备与硬件有三种匹配方式:名字匹配,id_table匹配,设备树匹配
名字匹配,设备树匹配都在设备结构体pdrv里的driver结构体里面,
首先定义一个驱动结构体并赋值,。probe函数,.remove函数,.driver匹配选项设置有两种,使用一键注册函数module_platform_driver(结构体名)
驱动代码
- #include <linux/init.h>
- #include <linux/module.h>
- #include <linux/platform_device.h>
- struct resource *res;
- int irqno;
- //定义一个probe函数
- int pdrv_probe(struct platform_device *pdev)
- {
-
- //获取MEM类型的资源
-
- res=platform_get_resource(pdev,IORESOURCE_MEM,0);
- if(res==NULL)
- {
- printk("获取MEM资源失败\n");
- return ENODATA;
- }
- //获取中断类型的资源
- irqno=platform_get_irq(pdev,0);
- if(irqno<0)
- {
- printk("获取中断资源失败\n");
- return ENODATA;
- }
-
- printk("addr:%#llx ,irqno:%d\n",res->start,irqno);
- return 0;
- }
-
- int pdrv_remove(struct platform_device *pdev)
- {
-
- printk("%s:%d\n",__func__,__LINE__);
- return 0;
- }
-
- //热插拔宏,可以在插入硬件时,自动安装驱动
- MODULE_DEVICE_TABLE(platform,idtable);
- //对象初始化
- struct platform_driver pdrv={
- .probe=pdrv_probe,
- .remove=pdrv_remove,
- .driver={
- .name="aaaaa", //按名字匹配,只能匹配一个设备
- },
-
- };
-
- module_platform_driver(pdrv); //一键注册驱动
- MODULE_LICENSE("GPL");
设备代码
1.定义一个设备结构体并赋值,struct platform_driver pdrv;
2.


- #include <linux/init.h>
- #include <linux/module.h>
- #include <linux/platform_device.h>
-
- //对设备信息进行填充
- struct resource res[]={
- [0]={
- .start=0x12345678,
- .end=0x12345678+49,
- .flags= IORESOURCE_MEM,
- },
- [1]={
- .start=71,
- .end=71,
- .flags= IORESOURCE_IRQ,
- },
- };
-
- //定义一个relese函数
- void pdev_release(struct device *dev)
- {
- printk("%s:%d\n",__func__,__LINE__);
- }
-
- //给对象赋值
- struct platform_device pdev={
- .name="aaaaa",
- .id=PLATFORM_DEVID_AUTO,
- .dev={
- .release=pdev_release,
- },
- .resource=res,
- .num_resources=ARRAY_SIZE(res),
- };
-
- static int __init mycdev_init(void)
- {
- //注册device
- return platform_device_register(&pdev);
- }
- static void __exit mycdev_exit(void)
- {
- //注销device
- platform_device_unregister(&pdev);
-
- }
- module_init(mycdev_init);
- module_exit(mycdev_exit);
- MODULE_LICENSE("GPL");
- #include <linux/init.h>
- #include <linux/module.h>
- #include <linux/platform_device.h>
- #include <linux/mod_devicetable.h>
- struct resource *res;
- int irqno;
- //定义一个probe函数
- int pdrv_probe(struct platform_device *pdev)
- {
-
- //获取MEM类型的资源
-
- res=platform_get_resource(pdev,IORESOURCE_MEM,0);
- if(res==NULL)
- {
- printk("获取MEM资源失败\n");
- return ENODATA;
- }
- //获取中断类型的资源
- irqno=platform_get_irq(pdev,0);
- if(irqno<0)
- {
- printk("获取中断资源失败\n");
- return ENODATA;
- }
-
- printk("addr:%#llx ,irqno:%d\n",res->start,irqno);
- return 0;
- }
-
- int pdrv_remove(struct platform_device *pdev)
- {
-
- printk("%s:%d\n",__func__,__LINE__);
- return 0;
- }
- //id_table创建
- struct platform_device_id idtable[]={
- {"hi1",0},
- {"hi2",1},
- {"hi3",2},
- {},//防止越界
- };
- //热插拔宏,可以在插入硬件时,自动安装驱动
- MODULE_DEVICE_TABLE(platform,idtable);
- //对象初始化
- struct platform_driver pdrv={
- .probe=pdrv_probe,
- .remove=pdrv_remove,
- .driver={
- .name="aaaaa", //按名字匹配,只能匹配一个设备
- },
- .id_table=idtable, //设置名字表匹配,可以匹配多个硬件设备
- };
-
- module_platform_driver(pdrv);
- MODULE_LICENSE("GPL");
1、编写自己的设备树节点---编译设备树,开发板上电加载
2、驱动对象中,匹配方式-----设备树匹配
3、构建comptible表
struct of_device_id oftable[]={
{.compatible="hqyj,platform",},
};设备初始化结构体中,添加设备树匹配
.of_match_table=oftable,

驱动编译后拷贝到~/nfs/rootfs
- #include <linux/init.h>
- #include <linux/module.h>
- #include <linux/platform_device.h>
- #include <linux/mod_devicetable.h>
- #include <linux/of.h>
- #include <linux/of_gpio.h>
- struct resource *res;
- int irqno;
- struct gpio_desc *gpiono; //存放gpio编号
- //定义一个probe函数
- int pdrv_probe(struct platform_device *pdev)
- {
-
- //获取MEM类型的资源
-
- res=platform_get_resource(pdev,IORESOURCE_MEM,0);
- if(res==NULL)
- {
- printk("获取MEM资源失败\n");
- return ENODATA;
- }
- //获取中断类型的资源
- irqno=platform_get_irq(pdev,0);
- if(irqno<0)
- {
- printk("获取中断资源失败\n");
- return ENODATA;
- }
-
- printk("addr:%#x ,irqno:%d\n",res->start,irqno);
- //解析设备树节点信息
- gpiono=gpiod_get_from_of_node(pdev->dev.of_node,"myled1",0,GPIOD_OUT_HIGH,NULL);
- if(IS_ERR(gpiono))
- {
- printk("获取gpio编号失败\n");
- return PTR_ERR(gpiono);
- }
- printk("点亮led1\n");
- gpiod_set_value(gpiono,1);
- return 0;
- }
-
- int pdrv_remove(struct platform_device *pdev)
- {
-
- gpiod_set_value(gpiono,0);
- gpiod_put(gpiono);
- return 0;
- }
- struct of_device_id oftable[]={
- {
- .compatible="hqyj,platform",
- },
- {},
- };
- //热插拔宏,可以在插入硬件时,自动安装驱动
- MODULE_DEVICE_TABLE(of,oftable);
- //对象初始化
- struct platform_driver pdrv={
- .probe=pdrv_probe,
- .remove=pdrv_remove,
- .driver={
- .name="aaaaa", //按名字匹配,只能匹配一个设备
- .of_match_table=oftable,//设备树匹配
- },
- };
-
- module_platform_driver(pdrv);
- MODULE_LICENSE("GPL");
现象:

如何让设备安装时,驱动自动加载,就像插入u盘,系统会自动识别硬件安装驱动
MODULE_DEVICE_TABLE(platform,idtable);添加此行即可
