一、最简
做完后只是部分GPIO可以用,只放了GPIO的基地址和复用的基地址,后面控制GPIO通用化可解决这问题
.driver = {
.name = "bbcen plat driver" ,
.owner = THIS_MODULE,
.of_match_table = of_plat_match,
},
//根据这些名字让驱动driver匹配设备device,优先匹配of_match_table(设备树里的),没有就匹配name(自己写的设备device文件)
设备树:描述设备的硬件信息
//hardware/nvidia/platform/t210/porg/kernel-dts/tegra210-p3448-0000-p3449-0000-b00.dts
添加:
yhai-plat {
compatible = "bbcen-plat";
status = "okay";
reg = <0 0x6000D200 0 0xff>,<0 0x70003150 0 0X10>; /*注意上层的
#address-cells = <2>; //设置子节点"reg"属性的 首地址 占2个单位(1个cell单位,占32位)
#size-cells = <2>; //设置子节点"reg"属性的 地址长度 占2个单位
*/
};
*驱动
- of_plat_match //注意这个要和设备树里的一样
- platform_get_resource //从平台设备里获取资源
//plat_driver.c
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/of.h>
struct resource *res1;
struct resource *res2;
static int plat_probe(struct platform_device *pdev)
{
printk("match ok,plat_probe go\n");
res1 = platform_get_resource(pdev, IORESOURCE_MEM, 0); /*从平台设备里获取资源<0 0x6000D200 0 0xff>
pdev: 指向平台设备的指针
IORESOURCE_MEM:资源类型
0: 资源序号
*/
if (res1 == NULL) {
printk("res1 platform_get_resource fail \n");
return -ENODEV;
}
printk("res1->start=%llx\n",res1->start);
res2 = platform_get_resource(pdev, IORESOURCE_MEM, 1);//<0 0x70003150 0 0X10>
if (res2 == NULL) {
printk("res2 platform_get_resource fail\n");
return -ENODEV;
}
printk("res2->start=%llx\n",res2->start);
return 0;
}
static int plat_remove(struct platform_device *pdev)
{
printk("plat_remove go\n");
return 0;
}
static const struct of_device_id of_plat_match[] = {
//与驱动匹配的平台设备id列表
{
.compatible = "bbcen-plat", },
{
},
};
MODULE_DEVICE_TABLE(of, of_plat_match);
static struct platform_driver plat_driver = {
.driver = {
.name = "bbcen plat driver" ,
.owner = THIS_MODULE,
.of_match_table = of_plat_match,
},
.probe = plat_probe, //当在设备树里找到对应的平台设备,才会调用probe函数
.remove = plat_remove, //删除设备时,会自动调用
};
module_platform_driver(plat_driver); //声明平台设备驱动的入口
编译文件
ifeq ($(KERNELRELEASE),)
KERNELDIR ?= ~/kernel-4.9
PWD := $(shell pwd)
all:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
clean:
rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions Module* modules* a.out
else
obj-m := plat_driver.o
endif