#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/platform_device.h>
#include <linux/of.h>
//设备节点类型
static struct device_node *devnode;
//节点属性类型
static struct property *prop;
static int len = 0;
u32 value[2];
int beep_probe(struct platform_device *pdev)
{
int ret = 0;
printk("beep_probe.....\n");
// 直接获取设备节点
printk("name: %s %s\n", pdev->name, pdev->resource[0].name);
// 根据节点获取资源
//根据节点路径寻找节点函数
devnode = of_find_node_by_path("/test1");
if (NULL == devnode)
{
printk("of_find_node_by_path failed....\n");
return 1;
}
printk("name: %s full_name: %s\n", devnode->name, devnode->full_name);
//获取节点属性
prop = of_find_property(devnode, "compatible", &len);
if (NULL == prop)
{
printk("of_find_property failed....\n");
return 1;
}
printk("property name: %s value: %s\n", prop->name, (char *)prop->value);
//获取reg的值
ret = of_property_read_u32_array(devnode, "reg", value, 2);
if (ret < 0)
{
printk("of_property_read_u32_array failed...\n");
return 1;
}
printk("value[0]: 0x%8x value[1]: 0x%8x\n", value[0], value[1]);
return 0;
}
int beep_remove(struct platform_device *pdev)
{
printk("beep_remove.....\n");
return 0;
}
//与设备树compatible匹配
const struct of_device_id of_match_table_test[] = {
{
.compatible = "test123456"
},
{},
};
const struct platform_device_id beep_idtable = {
.name = "beep_test",
};
struct platform_driver beep_driver = {
.probe = beep_probe,
.remove = beep_remove,
.driver = {
.owner = THIS_MODULE,
.name = "beep_test",
//设备树匹配
.of_match_table = of_match_table_test
},
.id_table = &beep_idtable
};
//设备模块加载
static int __init test_init(void)
{
int ret = 0;
printk("test_init.....\n");
//平台驱动注册到内核
ret = platform_driver_register(&beep_driver);
if (ret < 0)
{
printk("platform_driver_register error\n");
return -1;
}
printk("platform_driver_register OK\n");
return 0;
}
static void __exit test_exit(void)
{
platform_driver_unregister(&beep_driver);
printk("test_exit......\n");
}
module_init(test_init);
module_exit(test_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Deng");
MODULE_INFO(intree, "Y");
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
一般对于I2C、SPI、USB这些常见类型的物理总线来说,Linux内核会自动创建与之相应的驱动总线,因此I2C设备、SPI设备、 USB设备自然是注册挂载在相应的总线上。但是,实际项目开发中还有很多结构简单的设备,对它们进行控制并不需要特殊的时序。 它们也就没有相应的物理总线,比如led、rtc时钟、蜂鸣器、按键等等,Linux内核将不会为它们创建相应的驱动总线。 为了使这部分设备的驱动开发也能够遵循设备驱动模型,Linux内核引入了一种虚拟的总线——平台总线(platform bus)。 平台总线用于管理、挂载那些没有相应物理总线的设备,这些设备被称为平台设备,对应的设备驱动则被称为平台驱动。 平台设备驱动的核心依然是Linux设备驱动模型,平台设备使用platform_device结构体来进行表示,其继承了设备驱动模型中的device结构体。 而平台驱动使用platform_driver结构体来进行表示,其则是继承了设备驱动模型中的device_driver结构体。 ————————————————
资源推荐
资源详情
资源评论
收起资源包目录
6platform.rar (2个子文件)
6platform
Makefile 206B
driver.c 2KB
共 2 条
- 1
资源评论
沧海一笑-dj
- 粉丝: 3w+
- 资源: 337
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功