2025年Linux Platform devices 平台设备驱动

Linux Platform devices 平台设备驱动设备总线驱动模型 http blog csdn net lizuobin2 article details 51570196 本文主要参考 http www wowotech net device model platform device html platform 平台设备驱动是基于设备总线驱动模型的 它只不过是将 device 进一步封装成为 platform device 将

设备总线驱动模型:http://blog.csdn.net/lizuobin2/article/details/51570196

本文主要参考:http://www.wowotech.net/device_model/platform_device.html

platform平台设备驱动是基于设备总线驱动模型的,它只不过是将 device 进一步封装成为 platform_device,将 device_driver 进一步封装成为 platform_device_driver,前面已经分析过设备总线驱动模型,关于device 与 device_driver 的注册过程以及它们在sysfs文件系统中的层次关系就不在分析,本文重点分析platform平台设备驱动与设备总线驱动模型相比较新增添的那些东西。

在Linux设备模型的抽象中,存在着一类称作“Platform Device”的设备,内核是这样描述它们的(Documentation/driver-model/platform.txt):

Platform devices are devices that typically appear as autonomous entities in the system. This includes legacy port-based devices and host bridges to peripheral buses, and most controllers integrated into system-on-chip platforms. What they usually have in common is direct addressing from a CPU bus. Rarely, a platform_device will be connected through a segment of some other kind of bus; but its registers will still be directly addressable.

概括来说,Platform设备包括:基于端口的设备(已不推荐使用,保留下来只为兼容旧设备,legacy);连接物理总线的桥设备;集成在SOC平台上面的控制器;连接在其它bus上的设备(很少见)。等等。
这些设备有一个基本的特征:可以通过CPU bus直接寻址(例如在嵌入式系统常见的“寄存器”)。因此,由于这个共性,内核在设备模型的基础上(device和device_driver),对这些设备进行了更进一步的封装,抽象出paltform bus、platform device和platform driver,以便驱动开发人员可以方便的开发这类设备的驱动。
可以说,paltform设备对Linux驱动工程师是非常重要的,因为我们编写的大多数设备驱动,都是为了驱动plaftom设备。

platform_bus_type
我们知道,在设备总线驱动模型的中,BUS像一个月老一样,通过它的match函数,将注册到bus中的device与driver进行配对,那么每一个不同的bus 都有自己的match函数,我们来看看platform_bus_type.

struct bus_type platform_bus_type = {
.name = "platform",
.dev_attrs = platform_dev_attrs,
.match = platform_match,
.uevent = platform_uevent,
.pm = &platform_dev_pm_ops,
};
static int platform_match(struct device *dev, struct device_driver *drv)
{
struct platform_device *pdev = to_platform_device(dev);
struct platform_driver *pdrv = to_platform_driver(drv);

/* match against the id table first */
if (pdrv->id_table)
return platform_match_id(pdrv->id_table, pdev) != NULL;

/* fall-back to driver name match */
return (strcmp(pdev->name, drv->name) == 0);
}

如果platform_device_driver中定义了id_table,则调用 platform_match_id 进行匹配

举个例子:

static struct platform_device_id s3c24xx_driver_ids[] = {
{
.name = "s3c2410-i2c",
.driver_data = TYPE_S3C2410,
}, {
.name = "s3c2440-i2c",
.driver_data = TYPE_S3C2440,
}, { },
};
struct platform_device s3c_device_i2c0 = {
.name = "s3c2410-i2c",
#ifdef CONFIG_S3C_DEV_I2C1
.id = 0,
#else
.id = -1,
#endif
.num_resources = ARRAY_SIZE(s3c_i2c_resource),
.resource = s3c_i2c_resource,
};
static const struct platform_device_id *platform_match_id(struct platform_device_id *id, struct platform_device *pdev)
{
while (id->name[0]) {
if (strcmp(pdev->name, id->name) == 0) {
pdev->id_entry = id;
return id;
}
id++;
}
return NULL;
}


显然,platform_match_id 的作用就是遍历整个 Id_table 数组,寻找是否有与 platform_device->name 同名的,如果有,则返回这个 Platform_device_id ,使用Id_table 打破了原本设备总线驱动模型,一个 device 只能用与一个 device_driver 配对的局限性。现在一个platform_device_driver 可以与多个platform_device配对。

如果没有,则只是根据 platform_device_driver->name 与 platform_device->name 进行比较,这也就是老师为啥在写平台设备驱动程序的时候经常说,“将驱动注册到内核中去,如果有同名设备,则调用driver->probe函数….”。

pletform_device 中的 id 的作用:

if (pdev->id != -1) /* 如果不是-1 对name编号 */
dev_set_name(&pdev->dev, “%s.%d”, pdev->name, pdev->id);
else /* -1时直接是名字 */
dev_set_name(&pdev->dev, pdev->name);


从device封装而来的platform_device

struct platform_device {
const char * name;
int id;
struct device dev;
u32 num_resources;
struct resource * resource;

struct platform_device_id *id_entry;

/* arch specific additions */
struct pdev_archdata archdata;
};

name,设备的名称,该名称在设备注册时,会拷贝到dev.init_name中。

dev,真正的设备,通过 container_of ,就能找到整个platform_device ,访问其它成员,如后面要提到的 resource

num_resources、resource,该设备的资源描述,由struct resource(include/linux/ioport.h)结构抽象。

在Linux中,
系统资源包括I/O、Memory、Register、IRQ、DMA、Bus等多种类型。这些资源大多具有独占性,不允许多个设备同时使用,因此Linux内核提供了一些API,用于分配、管理这些资源。


当某个设备需要使用某些资源时,只需利用struct resource组织这些资源(如名称、类型、起始、结束地址等),并保存在该设备的resource指针中即可。然后在设备probe时,设备需求会调用资源管理接口,分配、使用这些资源。而内核的资源管理逻辑,可以判断这些资源是否已被使用、是否可被使用等等。

struct resource {
resource_size_t start;
resource_size_t end;
const char *name;
unsigned long flags;
struct resource *parent, *sibling, *child;
};
static struct resource led_resource[] = {	//jz2440的参数,驱动未测试 
[0] = {
.start = 0x56000010,
.end = 0x56000010 + 8 - 1,
.flags = IORESOURCE_MEM,
},
[1] = {
.start = 5,
.end = 5,
.flags = IORESOURCE_IRQ,
},
};
static struct platform_device led_dev = {
.name = "myled", //设备名字 与 驱动相匹配
.id = -1,
.num_resources = ARRAY_SIZE(led_resource),
.resource = led_resource,

.dev = {
.release = led_release,
//.devt = MKDEV(252, 1),
},
};

从 device_driver 封装而来的platform_device_dirver

struct platform_driver {
int (*probe)(struct platform_device *);
int (*remove)(struct platform_device *);
void (*shutdown)(struct platform_device *);
int (*suspend)(struct platform_device *, pm_message_t state);
int (*resume)(struct platform_device *);
struct device_driver driver;
struct platform_device_id *id_table;
};
int platform_driver_register(struct platform_driver *drv)
{
drv->driver.bus = &platform_bus_type;
if (drv->probe)
drv->driver.probe = platform_drv_probe;
if (drv->remove)
drv->driver.remove = platform_drv_remove;
if (drv->shutdown)
drv->driver.shutdown = platform_drv_shutdown;

return driver_register(&drv->driver);
}

struct platform_driver结构和struct device_driver非常类似,上边的platform_drv_probe、platform_drv_remove、platform_drv_shutdown,只不过稍作转换调用platform_driver中的probe、remove、shutdown函数,举个例子稍微看一下

static int platform_drv_probe(struct device *_dev)
{
struct platform_driver *drv = to_platform_driver(_dev->driver);
struct platform_device *dev = to_platform_device(_dev);

return drv->probe(dev);
}

Platform Device提供的API

/* include/linux/platform_device.h */
extern int platform_device_register(struct platform_device *);
extern void platform_device_unregister(struct platform_device *);

extern void arch_setup_pdev_archdata(struct platform_device *);
extern struct resource *platform_get_resource(struct platform_device *, unsigned int, unsigned int);
extern int platform_get_irq(struct platform_device *, unsigned int);
extern struct resource *platform_get_resource_byname(struct platform_device *, unsigned int, const char *);
extern int platform_get_irq_byname(struct platform_device *, const char *);
extern int platform_add_devices(struct platform_device **, int);

extern struct platform_device *platform_device_register_full(const struct platform_device_info *pdevinfo);

static inline struct platform_device *platform_device_register_resndata(
struct device *parent, const char *name, int id,
const struct resource *res, unsigned int num,
const void *data, size_t size)

static inline struct platform_device *platform_device_register_simple(
const char *name, int id,
const struct resource *res, unsigned int num)

static inline struct platform_device *platform_device_register_data(
struct device *parent, const char *name, int id,
const void *data, size_t size)

extern struct platform_device *platform_device_alloc(const char *name, int id);
extern int platform_device_add_resources(struct platform_device *pdev,
const struct resource *res,
unsigned int num);
extern int platform_device_add_data(struct platform_device *pdev,
const void *data, size_t size);
extern int platform_device_add(struct platform_device *pdev);
extern void platform_device_del(struct platform_device *pdev);
extern void platform_device_put(struct platform_device *pdev);

platform_device_register、platform_device_unregister,Platform设备的注册/注销接口,和底层的device_register等接口类似。

arch_setup_pdev_archdata,设置platform_device变量中的archdata指针。

platform_get_resource、platform_get_irq、platform_get_resource_byname、platform_get_irq_byname,通过这些接口,可以获取platform_device变量中的resource信息,以及直接获取IRQ的number等等。

platform_device_register_full、platform_device_register_resndata、platform_device_register_simple、platform_device_register_data,其它形式的设备注册。调用者只需要提供一些必要的信息,如name、ID、resource等,Platform模块就会自动分配一个struct platform_device变量,填充内容后,注册到内核中。

platform_device_alloc,以name和id为参数,动态分配一个struct platform_device变量。

platform_device_add_resources,向platform device中增加资源描述。

platform_device_add_data,向platform device中添加自定义的数据(保存在pdev->dev.platform_data指针中)。

platform_device_add、platform_device_del、platform_device_put,其它操作接口。

Platform Driver提供的API

platform_driver_registe、platform_driver_unregister,platform driver的注册、注销接口。

platform_driver_probe,主动执行probe动作。

platform_set_drvdata、platform_get_drvdata,设置或者获取driver保存在device变量中的私有数据。

懒人API

 extern struct platform_device *platform_create_bundle(
struct platform_driver *driver, int (*probe)(struct platform_device *),
struct resource *res, unsigned int n_res,
const void *data, size_t size);

只要提供一个platform_driver(要把driver的probe接口显式的传入),并告知该设备占用的资源信息,platform模块就会帮忙分配资源,并执行probe操作。对于那些不需要热拔插的设备来说,这种方式是最省事的了。

简单一例:

开发板:Mini2440

内核版本:2.6.32.2

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
// 设备资源
static struct resource led_resource[] = { //jz2440的参数,驱动未测试
[0] = {
.start = 0x56000010,
.end = 0x56000010 + 8 - 1,
.flags = IORESOURCE_MEM,
},
[1] = {
.start = 5,
.end = 5,
.flags = IORESOURCE_IRQ,
},
};

static void led_release(struct device *dev){

}

// 创建一个设备
static struct platform_device led_dev = {
.name = "myled", //设备名字 与 驱动相匹配
.id = -1,
.num_resources = ARRAY_SIZE(led_resource),
.resource = led_resource,

.dev = {
.release = led_release,
//.devt = MKDEV(252, 1),
},
};

static int led_dev_init(void){

//向bus注册led_dev match drv链表进行配对
platform_device_register(&led_dev);
return 0;
}

static void led_dev_exit(void){
platform_device_unregister(&led_dev);
}

module_init(led_dev_init);
module_exit(led_dev_exit);
MODULE_LICENSE("GPL");
#include 
#include
#include
#include
#include
#include
#include
#include
#include

#include
#include

static int major;

static struct class *cls;
static struct device *dev;

static volatile unsigned long *gpio_con;
static volatile unsigned long *gpio_dat;
static int pin;

static int led_open(struct inode *inode, struct file *file){

*gpio_con &= ~(0x03 << (pin*2));
*gpio_con |= (0x01 << (pin*2));
return 0;
}

static ssize_t led_write(struct file *file, const char __user *buf,
size_t count, loff_t *ppos){

int val;
copy_from_user(&val, buf, count);

if(val == 1){

*gpio_dat &= ~(1<}else{

*gpio_dat &= (1<}

return 0;
}

static struct file_operations led_fops = {

.owner = THIS_MODULE,
.open = led_open,
.write = led_write,
};

static int led_probe(struct platform_device *pdev){

struct resource *res;
// 最后一个参数 0 表示第1个该类型的资源
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
gpio_con = ioremap(res->start, res->end - res->start + 1);
gpio_dat = gpio_con + 1;

res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
pin = res->start;

printk("led_probe, found led\n");

// 注册设备驱动 创建设备节点
major = register_chrdev(0, "myled", &led_fops);
// 创建类
cls = class_create(THIS_MODULE, "myled");
// 创建设备节点
dev = device_create(cls, NULL, MKDEV(major, 0), NULL, "led");

return 0;
}

static int led_remove(struct platform_device *pdev){

printk("led_remove, remove led\n");
// 删除设备节点
device_unregister(dev);
// 销毁类
class_destroy(cls);
// 取消注册设备驱动
unregister_chrdev(major, "myled");
// 取消内存映射
iounmap(gpio_con);

return 0;
}

struct platform_driver led_drv = {

.probe = led_probe, //匹配到dev之后调用probe
.remove = led_remove,
.driver = {
.name = "myled",
},
};

static int led_drv_init(void){

platform_driver_register(&led_drv);
return 0;
}

static void led_drv_exit(void){

platform_driver_unregister(&led_drv);
}

module_init(led_drv_init);
module_exit(led_drv_exit);
MODULE_LICENSE("GPL");
编程小号
上一篇 2025-03-08 16:51
下一篇 2025-03-08 07:46

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/hz/139914.html