Heng30的博客
搜索 分类 关于 订阅

在Linux驱动中如何获取platform_device的资源?

2025-03-30

struct platform_device是用于描述硬件信息的驱动,其他驱动可以通过名称和它进行匹配。一旦匹配成功后,可以通过它来获取硬件的信息。下面的例子就演示了如何进行驱动匹配和获取硬件信息。

设备代码

// device.c

#include <linux/init.h>
#include <linux/module.h>
#include <linux/platform_device.h>

#define LED1_CTL 0
#define LED2_CTL 1
#define SIGNAL_USER 3

// 这里的地址需要根据嵌入式开发板或外部设备进行设置
#define ADDR_START 0x0
#define DEV_NAME "my_device_001"

struct resource my_resource[] = {
    [0] =
        {
            .start = ADDR_START,     // 需要根据实际情况填写
            .end = ADDR_START + 0x0, // 需要根据实际情况填写
            .name = "LED1",
            .flags = IORESOURCE_MEM,
            .desc = LED1_CTL,
        },
    [1] =
        {
            .start = ADDR_START + 0x0, // 需要根据实际情况填写
            .end = ADDR_START + 0x0,   // 需要根据实际情况填写
            .name = "LED2",
            .flags = IORESOURCE_MEM,
            .desc = LED2_CTL,
        },
    [2] =
        {
            .start = 67, // 需要根据实际情况填写
            .end = 67,   // 需要根据实际情况填写
            .name = "SIGNAL_USER",
            .flags = IORESOURCE_IRQ,
            .desc = SIGNAL_USER,
        },
};

static void _release(struct device *dev) {
    pr_info("platform_device_release\n");
}

struct platform_device my_platform_dev = {
    .name = DEV_NAME,
    .id = PLATFORM_DEVID_AUTO,
    .resource = my_resource,
    .num_resources = 3,
    .dev =
        {
            .release = _release,
        },
};

static int __init platform_device_init(void) {
    pr_info("platform_device_init\n");

    // 会在`/sys/bus/platform/devices/`目录下创建对应设备
    int ret = platform_device_register(&my_platform_dev);
    if (ret < 0) {
        pr_err("platform_device_register failed\n");
        return -1;
    }
    return 0;
}

static void __exit platform_device_exit(void) {
    pr_info("platform_device_exit\n");
    platform_device_unregister(&my_platform_dev);
}

module_init(platform_device_init);
module_exit(platform_device_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("heng30");
MODULE_VERSION("v0.0.1");
MODULE_DESCRIPTION("A simple platform_device kernel module");

驱动代码

// driver.c

#include <linux/init.h>
#include <linux/module.h>
#include <linux/platform_device.h>

// 这个名称要与`struct platform_device`定义的一致
#define PLATFORM_DEVICE_NAME "my_device_001"

// 当安装该驱动并名称匹配成功后,平台设备驱动会自动调用该函数
static int _probe(struct platform_device *device) {
    pr_info("platform_driver_probe\n");

    for (int i = 0; i < 3; i++) {
        pr_info("resource name: %s\n", device->resource[i].name);
    }

    // 第3个参数是指同类型组成的数组的索引。即使IRQ在资源中是下标为2,因为只 有1个IRQ类型,同类型的索引是0
    struct resource *res = platform_get_resource(device, IORESOURCE_IRQ, 0);
    if (res) {
        pr_info("IRQ number = %lld\n", res->start);
    } else {
        pr_info("platform_get_resource failed");
    }

    return 0;
}

// 当平台设备驱动卸载时,会自动调用该函数
static void _remove(struct platform_device *device) {
    pr_info("platform_driver_remove\n");
}

static struct platform_driver my_platform_driver = {
    .probe = _probe,
    .remove = _remove,

    .driver =
        {
            .name = PLATFORM_DEVICE_NAME,
        },
};

static int __init platform_driver_init(void) {
    pr_info("platform_driver_init\n");

    int ret = platform_driver_register(&my_platform_driver);
    if (ret < 0) {
        pr_info("platform_driver_register failed\n");
        return -1;
    }
    return 0;
}

static void __exit platform_driver_exit(void) {
    pr_info("platform_driver_exit\n");
    platform_driver_unregister(&my_platform_driver);
}

module_init(platform_driver_init);
module_exit(platform_driver_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("heng30");
MODULE_VERSION("v0.0.1");
MODULE_DESCRIPTION("A simple platform_driver kernel module");

编译脚本

#!/bin/sh

top-dir = $(shell pwd)
kernel-version = $(shell uname -r)
kernel-dir ?= /lib/modules/$(kernel-version)/build

obj-m += driver.o device.o

all:
        make -C $(kernel-dir) modules M=$(top-dir)

clean:
        rm -f *.o *.ko *.mod *.mod.c *.order *.symvers *.dtbo
        make -C $(kernel-dir) clean m=$(top-dir)

测试代码

  • 编译:make

  • 安装驱动:

insmod device.ko
insmod driver.ko
  • 卸载驱动:
rmmod driver.ko
rmmod device.ko
  • 查看输出:dmesg
[ 1946.172327] platform_device_init
[ 1952.259841] platform_driver_init
[ 1952.259913] platform_driver_probe
[ 1952.259914] resource name: LED1
[ 1952.259916] resource name: LED2
[ 1952.259917] resource name: SIGNAL_USER
[ 1952.259917] IRQ number = 67
[ 1968.774562] platform_driver_exit
[ 1968.774597] platform_driver_remove
[ 1972.078052] platform_device_exit
[ 1972.078160] platform_device_release