Skip to content

Day72 - Platform Bus Internals

Objective

Understand how the Linux Platform Bus is built on top of the Linux Driver Model and how Device Tree creates platform devices that are matched against platform drivers.

Topics covered:

  • platform_bus_type
  • platform_device
  • platform_driver
  • Device Tree integration
  • of_match_table
  • Name matching
  • Compatible matching
  • MODULE_DEVICE_TABLE(of)
  • Platform Bus sysfs hierarchy

Lab 1 - Observe Real Platform Bus Devices

Inspect Platform Bus

ls /sys/bus/platform

Example output:

devices
drivers
drivers_autoprobe
drivers_probe
uevent

Inspect Platform Devices

ls /sys/bus/platform/devices | head

Example output:

1000005100.iommu
1000010000.dma
1000120000.pcie
...

Inspect Device Binding

readlink /sys/bus/platform/devices/1000120000.pcie/driver

Example output:

../../../../bus/platform/drivers/brcm-pcie

Inspect Driver Side

ls -l /sys/bus/platform/drivers/brcm-pcie

Example output:

1000120000.pcie -> ../../../../devices/platform/axi/1000120000.pcie
bind
unbind
uevent

Inspect Device Tree Node

ls -l /sys/bus/platform/devices/1000120000.pcie/of_node

Example output:

../../../firmware/devicetree/base/axi/pcie@1000120000

Inspect Compatible String

cat \
/sys/bus/platform/devices/1000120000.pcie/of_node/compatible \
| tr '\0' '\n'

Example output:

brcm,bcm2712-pcie

Lab 2 - Minimal Platform Driver

Goal

Register a platform driver without creating any platform device.


Driver Registration

static struct platform_driver demo_driver = {
    .probe = demo_probe,
    .remove = demo_remove,

    .driver = {
        .name = "demo_platform",
    },
};
platform_driver_register(&demo_driver);

Verify Driver Registration

sudo insmod platform_driver_demo.ko

Expected log:

[DEMO] init

No probe should occur.


Verify Sysfs

ls -l /sys/bus/platform/drivers/demo_platform

Expected:

bind
unbind
module
uevent

No device symlink exists.


Lab 3 - Name Matching

Goal

Create a platform device manually and verify name-based matching.


Create Platform Device

demo_pdev =
    platform_device_register_simple(
        "demo_platform",
        -1,
        NULL,
        0);

Register Driver

.driver = {
    .name = "demo_platform",
}

Verify Probe

Expected log:

[DEMO] probe

Verify Binding

Driver side:

ls -l /sys/bus/platform/drivers/demo_platform

Device side:

readlink \
/sys/bus/platform/devices/demo_platform/driver

Registration Order Test

Driver First

platform_driver_register()
platform_device_register_simple()
probe()

Device First

platform_device_register_simple()
platform_driver_register()
probe()

Result:

Registration order does not affect matching.

Lab 4 - Device Tree Compatible Matching

Goal

Verify Device Tree based matching using of_match_table.


Device Tree Overlay

/dts-v1/;
/plugin/;

/ {
    compatible = "brcm,bcm2712";

    fragment@0 {
        target-path = "/";

        __overlay__ {

            demo_platform: demo-platform {
                compatible = "demo,platform-bus";
                status = "okay";
            };
        };
    };
};

Driver Match Table

static const struct of_device_id demo_of_match[] = {
    { .compatible = "demo,platform-bus" },
    {}
};
MODULE_DEVICE_TABLE(of, demo_of_match);

Driver Definition

.driver = {
    .name = "demo_platform",
    .of_match_table = demo_of_match,
}

Load Driver First

sudo insmod platform_device_driver_demo.ko

Expected:

[DEMO] init begin
[DEMO] init end

Apply Overlay

sudo dtoverlay demo-platform.dtbo

Expected:

[DEMO] probe

Remove Overlay

sudo dtoverlay -r demo-platform

Expected:

[DEMO] remove

Verify Device Tree Node

ls -l \
/sys/bus/platform/devices/demo-platform/of_node

Expected:

../../../firmware/devicetree/base/demo-platform

Verify Compatible

cat \
/sys/bus/platform/devices/demo-platform/of_node/compatible \
| tr '\0' '\n'

Expected:

demo,platform-bus

Lab 5 - Compatible Match Failure

Goal

Verify that matching fails when compatible strings differ.


Modify Driver

static const struct of_device_id demo_of_match[] = {
    { .compatible = "demo,wrong-device" },
    {}
};

Load Driver

sudo insmod platform_device_driver_demo.ko

Apply Overlay

sudo dtoverlay demo-platform.dtbo

Expected:

[DEMO] init begin
[DEMO] init end

No probe should occur.


Conclusion

Successful registration does not imply successful binding.


Lab 6 - MODULE_DEVICE_TABLE(of)

Goal

Inspect generated OF aliases.


Inspect Module Metadata

modinfo platform_device_driver_demo.ko

Example output:

alias: of:N*T*Cdemo,platform-busC*
alias: of:N*T*Cdemo,platform-bus

Conclusion

of_match_table
Kernel runtime matching

MODULE_DEVICE_TABLE(of)
Module alias generation

Result

Verified:

  • Real Raspberry Pi 5 Platform Bus hierarchy
  • Platform driver registration
  • Name-based matching
  • Device Tree compatible matching
  • Match failure behavior
  • Registration order independence
  • Platform Bus sysfs hierarchy
  • Device Tree integration
  • MODULE_DEVICE_TABLE(of) behavior