借助platform理总线设备驱动架构

soc中集成的独立的外设控制器、挂接在soc内存空间的外设等却不依赖此类总线,基于这一背景linux发明了一种虚拟总线platform总线,比较喜欢把soc的iic、rtc、lcd、看门狗等归纳为platform_device,以下模拟了platform_drive和platform_device设备的对应情况
设备1:


```c
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/cdev.h>
#include <linux/fs.h>
#include <linux/platform_device.h>

MODULE_LICENSE("GPL");

struct resource res[2] = {
	[0] = {
		.start = 0x10004000,
		.end = 0x10004003,
		.flags = IORESOURCE_MEM,
	},
	[1] = {
		.start = 0x20004000,
		.end = 0x20008000,
		.flags = IORESOURCE_MEM,
	},
};

void demo_release(struct device *dev)
{
	printk("%s,%d\n", __func__, __LINE__);   
}

struct platform_device  pdevice = {
	.name = "demo0",
	.num_resources = ARRAY_SIZE(res),
	.resource = res,
	.dev = {
		.release = demo_release,
	},
};

static int __init demo_init(void)
{
	printk("%s,%d\n", __func__, __LINE__);   
	platform_device_register(&pdevice);
	return 0;
}

static void __exit demo_exit(void)
{
	printk("%s,%d\n", __func__, __LINE__);
	platform_device_unregister(&pdevice);
}

module_init(demo_init);
module_exit(demo_exit);






设备2
	

```c
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/cdev.h>
#include <linux/fs.h>
#include <linux/platform_device.h>

MODULE_LICENSE("GPL");

struct resource res[2] = {
	[0] = {
		.start = 0x30004000,
		.end = 0x30004003,
		.flags = IORESOURCE_MEM,
	},
	[1] = {
		.start = 0x40004000,
		.end = 0x40008000,
		.flags = IORESOURCE_MEM,
	},
};

void demo_release(struct device *dev)
{
	printk("%s,%d\n", __func__, __LINE__);   
}

struct platform_device  pdevice = {
	.name = "demo1",
	.num_resources = ARRAY_SIZE(res),
	.resource = res,
	.dev = {
		.release = demo_release,
	},
};

static int __init demo_init(void)
{
	printk("%s,%d\n", __func__, __LINE__);   
	platform_device_register(&pdevice);

	return 0;
}

static void __exit demo_exit(void)
{
	printk("%s,%d\n", __func__, __LINE__);
	platform_device_unregister(&pdevice);
}

module_init(demo_init);
module_exit(demo_exit);

设备3

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/cdev.h>
#include <linux/fs.h>
#include <linux/platform_device.h>

MODULE_LICENSE("GPL");

struct resource * res0;
struct resource * res1;

int demo_probe(struct platform_device * pdevice)  // 探测函数,设备与驱动匹配成功被回调
{
	printk("probe ok!\n");

	res0 = platform_get_resource(pdevice, IORESOURCE_MEM, 0);
	if(res0 == NULL)
	{
		printk("Failed to platform_get_resource.\n");
		return -1;
	}
	printk("res0:%#x , %#x\n", (unsigned int)res0->start, (unsigned int )res0->end);

	res1 = platform_get_resource(pdevice, IORESOURCE_MEM, 1);
	if(res1 == NULL)
	{
		printk("Failed to platform_get_resource.\n");
		return -1;
	}
	printk("res1:%#x , %#x\n", (unsigned int)res1->start, (unsigned int )res1->end);

	return 0;
}

int demo_remove(struct platform_device * pdevice)  // 移除函数,当设备与驱动,有一个移除时,被调用
{
	printk("%s,%d\n", __func__, __LINE__);

	return 0;
}

struct platform_device_id  idts[] = {
	{.name = "demo0", },
	{.name = "demo1", },
	{/*Nothing to be done.*/},
};

struct platform_driver  pdriver = {
	.probe = demo_probe,
	.remove = demo_remove,
	.driver  = {
		.owner = THIS_MODULE,
		.name = "xxx",
	},
	.id_table = idts,
};

static int __init demo_init(void)
{
	printk("%s,%d\n", __func__, __LINE__);   
	platform_driver_register(&pdriver);

	return 0;
}

static void __exit demo_exit(void)
{
	printk("%s,%d\n", __func__, __LINE__);
	platform_driver_unregister(&pdriver);
}

module_init(demo_init);
module_exit(demo_exit);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值