leds-gpio driver
阅读原文时间:2023年07月11日阅读:1

我们还是先看看platform device是如何define的

  • example1

在板级驱动中定义, 通过platform_add_devices()函数将这个platform device注册进系统.

arch\arm\plat-samsung\devs.c

struct platform_device s3c_device_hsmmc0 = {
    .name       = "s3c-sdhci",
    .id     = 0,
    .num_resources  = ARRAY_SIZE(s3c_hsmmc_resource),
    .resource   = s3c_hsmmc_resource,
    .dev        = {
        .dma_mask       = &samsung_device_dma_mask,
        .coherent_dma_mask  = DMA_BIT_MASK(32),
        .platform_data      = &s3c_hsmmc0_def_platdata,
    },
};

arch\arm\mach-s3c24xx\mach-smdk2416.c

static struct platform_device *smdk2416_devices[] __initdata = {
    &s3c_device_fb,
    &s3c_device_wdt,
    &s3c_device_ohci,
    &s3c_device_i2c0,
    &s3c_device_hsmmc0,
    &s3c_device_hsmmc1,
    &s3c_device_usb_hsudc,
    &s3c2443_device_dma,
};

并最终通过platform_add_devices()函数将这个platform device注册进系统。

arch\arm\mach-s3c24xx\mach-smdk2416.c

static void __init smdk2416_machine_init(void)
{
    //...

    platform_add_devices(smdk2416_devices, ARRAY_SIZE(smdk2416_devices));

    //...
}
  • example2

在在板级驱动中定义,通过platform_device_register()函数将这个platform device注册进系统.

arch\arm\mach-imx\mach-imx6sx.c

platform_device_register_simple("imx6q-cpufreq", -1, NULL, 0);

arch\arm\mach-imx\mach-imx6q.c

static struct platform_device imx6q_cpufreq_pdev = {
    .name = "imx6q-cpufreq",
};

static void __init imx6q_init_late(void)
{
    //...
        platform_device_register(&imx6q_cpufreq_pdev);
    // ...
}
  • example3

通过设备树定义.

在早期的ARM Linux Kernel中,板级细节使用代码形式,存放在arch/arm/目录。名字通常为:plat-xxxx或者mach-xxxx,内容多为特定板上细节信息,如platform, i2c_board_info, spi_board_info和各类资源数据。

但这类目录对Linux Kernel来说,意义非常有限,因为板级代码只对相应开发板有用。使用这种方法显然不是一个好办法。与此同时PowerPC等其它架构已经在使用一种新的方法:Flattened Device Tree(FDT)。Device Tree是一种描述硬件的数据结构。它包含板级硬件细节信息,这样,通过Device Tree,就可以把硬件信息传递给Kernel,而不需要再硬编码了。

arch\arm\boot\dts\bcm2710-rpi-3-b.dts

&leds {
    act_led: act {
        label = "led0";
        linux,default-trigger = "mmc0";
        gpios = <&virtgpio 0 0>;
    };

    pwr_led: pwr {
        label = "led1";
        linux,default-trigger = "input";
        gpios = <&expgpio 7 0>;
    };
};

至于设备树中定义的platform_device是如何被kernel解析并定义的, 参考

Device Tree(三):代码分析

machine初始化的代码可以沿着start_kernel->rest_init->kernel_init->kernel_init_freeable->do_basic_setup->do_initcalls路径寻找。在do_initcalls函数中,kernel会依次执行各个initcall函数,在这个过程中,会调用customize_machine,

在这个函数中,一般会调用machine描述符中的init_machine callback函数来把各种Device Tree中定义的platform device设备节点加入到系统(即platform bus的所有的子节点,对于device tree中其他的设备节点,需要在各自bus controller初始化的时候自行处理)。如果machine描述符中没有定义init_machine函数,那么直接调用of_platform_populate把所有的platform device加入到kernel中。

不过这里好像和arch\arm\mach-bcm\board_bcm2835.c对应不上

但是内核中处理解析dts的机制是一样的

of_platform_default_populate_init

of_platform_device_create

of_platform_device_create_pdata

of_device_add

static int __init of_platform_default_populate_init(void)
{
    struct device_node *node;
    if (!of_have_populated_dt())
        return -ENODEV;
    /*
     * Handle certain compatibles explicitly, since we don't want to create
     * platform_devices for every node in /reserved-memory with a
     * "compatible",
     */
    for_each_matching_node(node, reserved_mem_matches)
        of_platform_device_create(node, NULL, NULL);
    node = of_find_node_by_path("/firmware");
    if (node) {
        of_platform_populate(node, NULL, NULL, NULL);
        of_node_put(node);
    }
    /* Populate everything else. */
    of_platform_default_populate(NULL, NULL, NULL);
    return 0;
}