Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Platform Devices and Device Tree Basics | Communicating with Hardware
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
C Device Drivers Basics

bookPlatform Devices and Device Tree Basics

Platform devices are a key concept in Linux device driver development, especially on embedded systems and SoCs. A platform device represents a device that is directly attached to the system board and is not discoverable by standard bus enumeration methods, such as PCI or USB. Instead, the kernel is informed about these devices through board-specific data, which can be provided statically in code or dynamically via the device tree. Platform devices are commonly used for on-chip peripherals, such as UARTs, timers, or GPIO controllers.

platform_device_example.c

platform_device_example.c

copy
1234567891011121314151617181920212223242526272829303132
// platform_device_example.c #include <linux/module.h> #include <linux/platform_device.h> static struct platform_device my_platform_device = { .name = "my_simple_device", .id = -1, }; static int __init my_platform_device_init(void) { int ret; ret = platform_device_register(&my_platform_device); if (ret) pr_err("Failed to register platform device\n"); else pr_info("Platform device registered\n"); return ret; } static void __exit my_platform_device_exit(void) { platform_device_unregister(&my_platform_device); pr_info("Platform device unregistered\n"); } module_init(my_platform_device_init); module_exit(my_platform_device_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("Your Name"); MODULE_DESCRIPTION("Example Platform Device Registration");

While the previous code demonstrates registering a platform device directly in C, modern Linux systemsβ€”especially on ARM and embedded platformsβ€”use a device tree to describe hardware. The device tree is a data structure for describing hardware layout to the kernel, decoupling hardware configuration from kernel code. This allows the same kernel binary to support multiple hardware variants without recompilation. Device drivers can then match their platform devices based on compatible strings or other properties defined in the device tree. In the context of the platform device code shown above, the device tree would provide the kernel with the information needed to instantiate a platform device for the driver, rather than requiring hardcoded registration in C.

my_simple_device.dts

my_simple_device.dts

copy
123456
// my_simple_device.dts snippet my_simple_device: my_simple_device@0x10000000 { compatible = "myvendor,my-simple-device"; reg = <0x10000000 0x1000>; status = "okay"; };
question mark

What is the main role of the device tree in Linux device driver development?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 5. ChapterΒ 3

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Can you explain how to write a device tree entry for a platform device?

What are the advantages of using a device tree over hardcoded device registration?

How does a driver match with a platform device using the device tree?

bookPlatform Devices and Device Tree Basics

Swipe to show menu

Platform devices are a key concept in Linux device driver development, especially on embedded systems and SoCs. A platform device represents a device that is directly attached to the system board and is not discoverable by standard bus enumeration methods, such as PCI or USB. Instead, the kernel is informed about these devices through board-specific data, which can be provided statically in code or dynamically via the device tree. Platform devices are commonly used for on-chip peripherals, such as UARTs, timers, or GPIO controllers.

platform_device_example.c

platform_device_example.c

copy
1234567891011121314151617181920212223242526272829303132
// platform_device_example.c #include <linux/module.h> #include <linux/platform_device.h> static struct platform_device my_platform_device = { .name = "my_simple_device", .id = -1, }; static int __init my_platform_device_init(void) { int ret; ret = platform_device_register(&my_platform_device); if (ret) pr_err("Failed to register platform device\n"); else pr_info("Platform device registered\n"); return ret; } static void __exit my_platform_device_exit(void) { platform_device_unregister(&my_platform_device); pr_info("Platform device unregistered\n"); } module_init(my_platform_device_init); module_exit(my_platform_device_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("Your Name"); MODULE_DESCRIPTION("Example Platform Device Registration");

While the previous code demonstrates registering a platform device directly in C, modern Linux systemsβ€”especially on ARM and embedded platformsβ€”use a device tree to describe hardware. The device tree is a data structure for describing hardware layout to the kernel, decoupling hardware configuration from kernel code. This allows the same kernel binary to support multiple hardware variants without recompilation. Device drivers can then match their platform devices based on compatible strings or other properties defined in the device tree. In the context of the platform device code shown above, the device tree would provide the kernel with the information needed to instantiate a platform device for the driver, rather than requiring hardcoded registration in C.

my_simple_device.dts

my_simple_device.dts

copy
123456
// my_simple_device.dts snippet my_simple_device: my_simple_device@0x10000000 { compatible = "myvendor,my-simple-device"; reg = <0x10000000 0x1000>; status = "okay"; };
question mark

What is the main role of the device tree in Linux device driver development?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 5. ChapterΒ 3
some-alt