Linux Platform Device and Driver

本文深入探讨了Linux Platform_driver机制,介绍了如何使用Platform_device和Platform_driver进行设备驱动开发。通过实例详细展示了资源注册、设备注册及驱动注册的过程,并讨论了其优势。

从Linux 2.6起引入了一套新的驱动管理和注册机制:Platform_device和Platform_driver。

Linux中大部分的设备驱动,都可以使用这套机制, 设备用Platform_device表示,驱动用Platform_driver进行注册。

 

Linux platform driver机制和传统的device driver 机制(通过driver_register函数进行注册)相比,一个十分明显的优势在于platform机制将设备本身的资源注册进内核,由内核统一管理,在驱动程序中使用这些资源时通过platform device提供的标准接口进行申请并使用。这样提高了驱动和资源管理的独立性,并且拥有较好的可移植性和安全性(这些标准接口是安全的)。

 

Platform机制的本身使用并不复杂,由两部分组成:platform_device和platfrom_driver。

通过Platform机制开发发底层驱动的大致流程为:  定义platform_device -> 注册platform_device -> 定义platform_driver -> 注册platform_driver。

 

首先要确认的就是设备的资源信息,例如设备的地址,中断号等。

在2.6内核中platform设备用结构体platform_device来描述,该结构体定义在kernel/include/linux/platform_device.h中,

struct platform_device {

 const char * name;

 u32  id;

 struct device dev;

 u32  num_resources;

 struct resource * resource;

};

 

该结构一个重要的元素是resource,该元素存入了最为重要的设备资源信息,定义在kernel/include/linux/ioport.h中,

struct resource {

 const char *name;

 unsigned long start, end;

 unsigned long flags;

 struct resource *parent, *sibling, *child;

};

 

下面举s3c2410平台的i2c驱动作为例子来说明:

 

/* arch/arm/mach-s3c2410/devs.c */
/* I2C */
static struct resource s3c_i2c_resource[]= {
         [0]= {
                   .start = S3C24XX_PA_IIC,
                   .end = S3C24XX_PA_IIC + S3C24XX_SZ_IIC - 1,
                   .flags = IORESOURCE_MEM,
         },
         [1]= {
                   .start = IRQ_IIC, //S3C2410_IRQ(27)
                   .end = IRQ_IIC,
                   .flags = IORESOURCE_IRQ,
         }
};

 

这里定义了两组resource,它描述了一个I2C设备的资源,第1组描述了这个I2C设备所占用的总线地址范围,IORESOURCE_MEM表示第1组描述的是内存类型的资源信息,第2组描述了这个I2C设备的中断号,IORESOURCE_IRQ表示第2组描述的是中断资源信息。设备驱动会根据flags来获取相应的资源信息。

 

有了resource信息,就可以定义platform_device了:

 

 

 

struct platform_device s3c_device_i2c= {
         .name ="s3c2410-i2c",
         .id =-1,
         .num_resources = ARRAY_SIZE(s3c_i2c_resource),
         .resource = s3c_i2c_resource,
};

定义好了platform_device结构体后就可以调用函数platform_add_devices向系统中添加该设备了,之后可以调用platform_driver_register()进行设备注册。要注意的是,这里的platform_device设备的注册过程必须在相应设备驱动加载之前被调用,即执行platform_driver_register之前,原因是因为驱动注册时需要匹配内核中所以已注册的设备名。

 

s3c2410-i2c的platform_device是在系统启动时,在cpu.c里的s3c_arch_init()函数里进行注册的,这个函数申明为arch_initcall(s3c_arch_init);会在系统初始化阶段被调用。

arch_initcall的优先级高于module_init。所以会在Platform驱动注册之前调用。(详细参考include/linux/init.h)

 

s3c_arch_init函数如下:

/* arch/arm/mach-3sc2410/cpu.c */
static int __init s3c_arch_init(void)
{
    int ret;
    ……
/* 这里board指针指向在mach-smdk2410.c里的定义的smdk2410_board,里面包含了预先定义的I2C Platform_device等. */
    if (board!= NULL) {
        struct platform_device **ptr = board->devices;
        int i;

        for (i= 0; i < board->devices_count; i++, ptr++){
            ret = platform_device_register(*ptr);    
//在这里进行注册

            if (ret){
                printk(KERN_ERR "s3c24xx: failed to add board device %s (%d) @%p/n",(*ptr)->name,
ret, *ptr);
            }
        }
        /* mask any error, we may not need all these board
         * devices */

        ret = 0;
    }
    return ret;
}

 

同时被注册还有很多其他平台的platform_device,详细查看arch/arm/mach-s3c2410/mach-smdk2410.c里的smdk2410_devices结构体。

 

 

驱动程序需要实现结构体struct platform_driver,参考drivers/i2c/busses

/* device driver for platform bus bits */


static struct platform_driver s3c2410_i2c_driver= {
         .probe = s3c24xx_i2c_probe,
         .remove= s3c24xx_i2c_remove,
         .resume = s3c24xx_i2c_resume,
         .driver ={
                   .owner = THIS_MODULE,
                   .name = "s3c2410-i2c",
         },
};

 

在驱动初始化函数中调用函数platform_driver_register()注册platform_driver,需要注意的是s3c_device_i2c结构中name元素和s3c2410_i2c_driver结构中driver.name必须是相同的,这样在platform_driver_register()注册时会对所有已注册的所有platform_device中的name和当前注册的platform_driver的driver.name进行比较,只有找到相同的名称的platfomr_device才能注册成功,当注册成功时会调用platform_driver结构元素probe函数指针,这里就是s3c24xx_i2c_probe,当进入probe函数后,需要获取设备的资源信息,常用获取资源的函数主要是:

struct resource * platform_get_resource(struct platform_device *dev, unsigned int type, unsigned int num);

根据参数type所指定类型,例如IORESOURCE_MEM,来获取指定的资源。

 

struct int platform_get_irq(struct platform_device *dev, unsigned int num);

获取资源中的中断号。

 

 

 

下面举s3c24xx_i2c_probe函数分析,看看这些接口是怎么用的。

前面已经讲了,s3c2410_i2c_driver注册成功后会调用s3c24xx_i2c_probe执行,下面看代码:

 

 

/* drivers/i2c/busses/i2c-s3c2410.c */

static int s3c24xx_i2c_probe(struct platform_device*pdev)
{
    struct s3c24xx_i2c *i2c = &s3c24xx_i2c;
    struct resource *res;
    int ret;
 
    /* find the clock and enable it */
 
    i2c->dev= &pdev->dev;
    i2c->clk= clk_get(&pdev->dev,"i2c");
    if (IS_ERR(i2c->clk)){
     dev_err(&pdev->dev,"cannot get clock/n");
     ret = -ENOENT;
     goto out;
    }

    dev_dbg(&pdev->dev,"clock source %p/n", i2c->clk);
    clk_enable(i2c->clk);


    /* map the registers */
    res = platform_get_resource(pdev, IORESOURCE_MEM, 0);/* 获取设备的IO资源地址 */
    if (res== NULL) {
     dev_err(&pdev->dev,"cannot find IO resource/n");
     ret = -ENOENT;
     goto out;
    }
    
    i2c->ioarea= request_mem_region(res->start,(res->end-res->start)+1, pdev->name);/* 申请这块IO Region */
    
    if (i2c->ioarea== NULL) {
     dev_err(&pdev->dev,"cannot request IO/n");
     ret = -ENXIO;
     goto out;
    }
    
    i2c->regs= ioremap(res->start,(res->end-res->start)+1);/* 映射至内核虚拟空间 */
    
    if (i2c->regs== NULL) {
     dev_err(&pdev->dev,"cannot map IO/n");
     ret = -ENXIO;
     goto out;
    }
    
    dev_dbg(&pdev->dev,"registers %p (%p, %p)/n", i2c->regs, i2c->ioarea, res);
    
    /* setup info block for the i2c core */
    i2c->adap.algo_data= i2c;
    i2c->adap.dev.parent= &pdev->dev;
    
    /* initialise the i2c controller */
    ret = s3c24xx_i2c_init(i2c);
    if (ret!= 0)
     goto out;

    /* find the IRQ for this unit (note, this relies on the init call to ensure no current IRQs pending */
    
    res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);/* 获取设备IRQ中断号 */

    if (res== NULL) {
     dev_err(&pdev->dev,"cannot find IRQ/n");
     ret = -ENOENT;
     goto out;
    }
    
    ret = request_irq(res->start, s3c24xx_i2c_irq, IRQF_DISABLED, /* 申请IRQ */
     pdev->name, i2c);
    
    ……

    return ret;
    
}

 

小思考:

那什么情况可以使用platform driver机制编写驱动呢?

我的理解是只要和内核本身运行依赖性不大的外围设备(换句话说只要不在内核运行所需的一个最小系统之内的设备),相对独立的,拥有各自独自的资源(addresses and IRQs),都可以用platform_driver实现。如:lcd,usb,uart等,都可以用platfrom_driver写,而timer,irq等最小系统之内的设备则最好不用platfrom_driver机制,实际上内核实现也是这样的。

<think>嗯,用户正在准备嵌入式Linux驱动开发的面试,特别关注PlatformDevice/Driver相关的知识点。从用户引用的资料来看,ta已经掌握了设备树(DTS)、字符设备驱动框架等基础概念,现在需要针对性更强的平台设备驱动专题复习。用户提供的参考资料很有价值,引用[1]提到驱动开发流程,引用[2]强调了设备树的作用,引用[3][4]则涉及底层寄存器和驱动函数操作。这说明用户需要理论结合实践的面试题,尤其是平台设备驱动特有的实现机制。考虑到平台设备驱动的核心是设备树匹配和资源管理,应该重点覆盖以下维度:首先是设备树与platformdriver的绑定原理,这是面试常考点;其次是platform_get_resource等关键API的使用场景;最后是platformdevicedriver结构体成员的意义。用户可能还容易混淆platformdriver和标准字符设备驱动的区别,这个对比点很实用。在组织回复时,特意将问题分为基础概念、设备树应用、驱动实现和进阶思考四个板块,这样用户可以从浅入深复习。第14题关于probe函数作用的题目特别重要,因为90%的平台驱动面试都会涉及。还加入了实际外设的案例题,比如第8题的GPIO控制,这样用户能检验知识应用能力。安全方面完全遵守技术讨论规范,所有示例代码都是Linux内核标准实现。回复末尾的复习建议特别提醒用户关注probe/remove流程和设备树语法,这两个是实际开发中最容易出错的地方。补充说明里提到probe时机问题也是资深工程师常踩的坑。</think> 以下是关于**Platform Device/Driver**的嵌入式Linux驱动开发面试题汇总,结合设备树(DTS)、驱动框架和实际应用场景,帮助你系统复习: --- ### **一、基础概念题** 1. **Platform Device/Driver 是什么?** - Platform Device 描述**与CPU直连的片上外设**(如GPIO控制器、I²C控制器等),无需总线枚举,其资源由内核直接管理。 - Platform Driver 通过 `platform_driver` 结构体注册,实现与 Platform Device 的匹配和操作函数[^2][^4]。 2. **Platform Driver 和标准字符设备驱动的区别?** - **Platform Driver** 需通过 `platform_driver_register()` 注册,绑定设备树或ACPI描述的硬件资源(如寄存器地址、中断号)。 - **字符设备驱动** 通过 `cdev_init()` 注册,需自行实现 `file_operations` 接口(`open/read/write`等)[^4]。 3. **简述 Platform Driver 的注册流程** ```c static struct platform_driver my_driver = { .probe = my_probe, // 设备匹配时调用 .remove = my_remove, // 设备移除时调用 .driver = { .name = "my_device", // 匹配名称或设备树compatible值 .of_match_table = my_of_match, }, }; module_platform_driver(my_driver); // 自动注册 + 注销 ``` --- ### **二、设备树(DTS)相关题** 4. **设备树如何描述 Platform Device?** 在 `.dts` 文件中定义节点: ```dts my_device: my_device@0x10000000 { compatible = "vendor,my-device"; // 驱动匹配关键字 reg = <0x10000000 0x1000>; // 寄存器基地址+长度 interrupt-parent = <&gic>; interrupts = <0 10 IRQ_TYPE_LEVEL_HIGH>; // 中断号 }; ``` 驱动通过 `of_match_table` 匹配 `compatible` 值[^2]。 5. **驱动如何解析设备树资源?** ```c // 获取寄存器地址 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); base = devm_ioremap_resource(&pdev->dev, res); // 获取中断号 irq = platform_get_irq(pdev, 0); request_irq(irq, my_handler, IRQF_SHARED, "my_irq", NULL); ``` --- ### **三、驱动实现题** 6. **`probe()` 函数的作用?需要实现哪些操作?** - 初始化硬件(映射寄存器、申请中断) - 注册字符设备(`alloc_chrdev_region()`, `cdev_init()`) - 暴露设备节点(`device_create()`生成 `/dev/my_device`) - 关联 `file_operations` 函数[^4]: ```c static const struct file_operations fops = { .owner = THIS_MODULE, .read = my_read, .write = my_write, .open = my_open, .release = my_release, }; ``` 7. **为什么常用 `devm_` 系列函数(如 `devm_ioremap_resource()`)?** **自动释放资源**:当驱动卸载或 `probe()` 失败时,内核自动释放内存/中断,避免资源泄漏[^3]。 --- ### **四、综合应用题** 8. **为GPIO控制器编写Platform Driver框架** ```c // 设备树匹配表 static const struct of_device_id gpio_dt_ids[] = { { .compatible = "vendor,gpio-ctrl" }, {} }; // 驱动结构体 static struct platform_driver gpio_driver = { .driver.name = "vendor_gpio", .driver.of_match_table = gpio_dt_ids, .probe = gpio_probe, .remove = gpio_remove, }; module_platform_driver(gpio_driver); ``` 9. **用户层如何访问Platform Driver?** 通过 `sysfs` 节点或 `/dev` 下设备文件: ```bash echo 1 > /sys/class/gpio/gpio17/value # 控制GPIO电平 cat /dev/my_sensor # 读取传感器数据 ``` --- ### **五、进阶思考题** 10. **Platform Device 如何支持热插拔?** 需配合热插拔框架(如 `MODULE_DEVICE_TABLE()`)动态响应设备添加/移除,重新调用 `probe()`/`remove()`[^4]。 11. **设备树 vs 传统板级文件(Board File)的优劣?** **设备树优势**:硬件变动无需重编译内核,只需修改 `.dts`;支持硬件动态配置[^2]。 **板级文件劣势**:代码耦合度高,修改需重新编译内核。 --- ### **复习建议** - **调试工具**:掌握 `dmesg` 查看驱动日志,`devmem` 直接读写寄存器。 - **内核文档**:阅读 `Documentation/devicetree/bindings/` 中的设备树绑定规范。 - **实战**:尝试为虚拟设备(如LED控制器)编写完整 Platform Driver。 > 引用注意事项:设备树分离硬件与驱动[^2],`devm_`函数简化资源管理[^3],`probe/remove`实现驱动生命周期[^4]。 --- ### 相关问题 1. **Platform DeviceDevice Tree 的关系是什么?** 2. **如何调试 Platform Driver 的 probe 失败问题?** 3. **Platform Driver 如何处理多个相同外设?** 4. **设备树中的 `reg` 属性如何映射到驱动中的物理地址?**
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值