mohitmishra786/low-level-dev-skills

platform-device-model

Platform device model skill for Linux driver binding.

View source
Original skill document

Rendered from the source repository. Headings, examples, code, tables, links, and referenced images are preserved.

Platform Device Model

Purpose

Explain the Linux driver model for platform (and similar) buses: struct device, device_driver, probe/remove, resource acquisition, sysfs, and deferred probe — the glue between device tree and driver code.

When to Use

  • Implementing or debugging platform_driver lifecycle
  • Exposing driver state via sysfs
  • Understanding uevent / udev device node creation
  • Before skills/kernel-dev/writing-char-drivers

Workflow

1. Object hierarchy

bus_type (platform, amba, pci, i2c, spi)
├── struct device        — hardware instance
└── struct device_driver — driver logic
        └── probe(dev) / remove(dev)

Platform devices often come from DT (of_platform) or legacy board files.

2. Platform driver skeleton

c
static int my_probe(struct platform_device *pdev)
{
    struct resource *res;
    void __iomem *base;

    res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
    base = devm_ioremap_resource(&pdev->dev, res);
    if (IS_ERR(base))
        return PTR_ERR(base);

    platform_set_drvdata(pdev, priv);
    return 0;
}

static void my_remove(struct platform_device *pdev)
{
    /* devm_* auto-cleanup on remove */
}

static struct platform_driver my_pdrv = {
    .probe  = my_probe,
    .remove = my_remove,
    .driver = {
        .name = "my-dev",
        .of_match_table = my_of_match,
    },
};
module_platform_driver(my_pdrv);

Prefer devm_* helpers for automatic unwind.

3. Device properties (DT / ACPI)

c
u32 speed;
device_property_read_u32(&pdev->dev, "clock-speed", &speed);
bool flag = device_property_present(&pdev->dev, "feature-x");

4. Sysfs (driver visibility)

c
static ssize_t status_show(struct device *dev,
                           struct device_attribute *attr, char *buf)
{
    return sysfs_emit(buf, "ok\n");
}
static DEVICE_ATTR_RO(status);

/* in probe */
device_create_file(&pdev->dev, &dev_attr_status);

5. Deferred probe

Return -EPROBE_DEFER when a clock, regulator, or bus is not ready — core retries later.

6. Debug

bash
ls /sys/bus/platform/devices/
ls /sys/bus/platform/drivers/
cat /sys/kernel/debug/devices_deferred  # if debugfs enabled
udevadm monitor

7. Agent usage

/platform-device-model Convert legacy board file driver to DT platform_driver with devm_ioremap

Common Problems

SymptomCauseFix
-EBUSY on probeResource claimed twiceCheck status / duplicate nodes
No /dev nodeChar device not registeredSee writing-char-drivers
Deferred foreverSupplier driver missingFix DT dependency chain
Remove crashManual free vs devm mismatchUse devm consistently
Name mismatch.name vs compatibleOF uses of_match_table

Related Skills

  • skills/kernel-dev/device-tree — hardware description
  • skills/kernel/device-drivers — IRQ, DMA, regmap depth
  • skills/kernel-dev/writing-char-drivers — userspace interface
  • skills/kernel-dev/bus-drivers-i2c-spi — other bus types
  • skills/low-level-programming/linux-kernel-modules — module loading
from this repository

More skills

All skills
mohitmishra786
Community

static-analysis

Static analysis skill for C/C++ codebases. Use when hardening code quality, triaging noisy builds, running clang-tidy, cppcheck, or scan-build, interpreting check categories, suppressing false positives, or integrating static analysis into CI. Activates on queries about clang-tidy checks, cppcheck, scan-build, compilecommands.json, code hardening, or static analysis warnings.

installs
1
GitHub stars
263
Updated
Jun 27
mohitmishra786
Community

assembly-x86

x86-64 assembly skill for reading, writing, and debugging assembly code. Use when reading GCC/Clang assembly output, writing inline asm in C/C++, understanding the System V AMD64 ABI calling convention, or debugging register and stack state. Activates on queries about x86-64 assembly, AT&T vs Intel syntax, inline asm, calling conventions, SIMD intrinsics, or reading disassembly output from objdump or GDB.

installs
1
GitHub stars
258
Updated
Jun 27
mohitmishra786
Community

cpp-modules

C++20 modules skill for modern C++ projects. Use when working with named modules, module partitions, header units, CMake MODULESOURCES, Clang -fmodules-ts, BMI caching issues, or migrating from headers to modules. Activates on queries about C++20 modules, import statements, module interface units, header units, or BMI files.

installs
1
GitHub stars
258
Updated
Jun 27
mohitmishra786
Community

gcc

GCC compiler skill for C/C++ projects. Use when selecting optimization levels, warning flags, debug builds, LTO, sanitizer instrumentation, or diagnosing compilation errors with GCC. Covers flag selection for debug vs release, ABI concerns, preprocessor macros, profile-guided optimization, and integration with build systems. Activates on queries about gcc flags, compilation errors, performance tuning, warning suppression, or cross-standard compilation.

installs
1
GitHub stars
258
Updated
Jun 27