65.设备树下platform_device和platform_driver

在设备树根节点下添加这个节点

复制代码
/{
    quan {
        #address-cells = <1>;
        #size-cells = <1>;
        compatible = "simple-bus";
        myLed{
            compatible = "my devicetree";
            reg = <0xFDD60000 0x00000004>;
        };
    };
};

driver.c

cpp 复制代码
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/mod_devicetable.h>
// 平台设备的初始化函数
static int my_platform_probe(struct platform_device *pdev)
{
    printk(KERN_INFO "my_platform_probe: Probing platform device\n");
 
    // 添加设备特定的操作
    // ...
 
    return 0;
}
 
// 平台设备的移除函数
static void my_platform_remove(struct platform_device *pdev)
{
    printk(KERN_INFO "my_platform_remove: Removing platform device\n");
 
    // 清理设备特定的操作
    // ...
 
}
 
 
const struct of_device_id of_match_table_id[]  = {
    {.compatible="my devicetree"},
};
 
// 定义平台驱动结构体
static struct platform_driver my_platform_driver = {
    .probe = my_platform_probe,
    .remove = my_platform_remove,
    .driver = {
        .name = "my_platform_device",
        .owner = THIS_MODULE,
        .of_match_table =  of_match_table_id,
    },
};
 
// 模块初始化函数
static int __init my_platform_driver_init(void)
{
    int ret;
 
    // 注册平台驱动
    ret = platform_driver_register(&my_platform_driver);
    if (ret) {
        printk(KERN_ERR "Failed to register platform driver\n");
        return ret;
    }
 
    printk(KERN_INFO "my_platform_driver: Platform driver initialized\n");
 
    return 0;
}
 
// 模块退出函数
static void __exit my_platform_driver_exit(void)
{
    // 注销平台驱动
    platform_driver_unregister(&my_platform_driver);
 
    printk(KERN_INFO "my_platform_driver: Platform driver exited\n");
}
 
module_init(my_platform_driver_init);
module_exit(my_platform_driver_exit);
 
MODULE_LICENSE("GPL");
MODULE_AUTHOR("quan");

makefile

cpp 复制代码
obj-m += devicetree.o
KDIR:=/home/linux/samba-mount/linux-kernel/linux-6.17.5
PWD?=$(shell pwd)
all:
	make -C $(KDIR) M=$(PWD) modules
	echo $(PWD)
clean:
	rm -rf *.ko *.o *.mod *.mod.o *.mod.c *.symvers *.order

install:
	cp  *.ko ../../linux-kernel/linux-6.17.5/kmodules

编译及开发板测试

相关推荐
TE-茶叶蛋16 小时前
Spec-Driven Development(规范驱动开发)
驱动开发
云栖梦泽1 天前
Camera驱动开发与应用开发中的零拷贝与DMA
linux·驱动开发·嵌入式硬件
脱胎换骨-军哥2 天前
C++ 嵌入式编程实例:从寄存器操作到底层驱动开发
开发语言·c++·驱动开发
智者知已应修善业2 天前
【使用D触发器74HC175实现0001 0011 0111 1111 1110 1100 1000的循环彩灯电路。】2025-4-28
驱动开发·经验分享·笔记·硬件架构·硬件工程
新元代码3 天前
SDD+TDD 双驱动开发模式实战指南
驱动开发
mk0153 天前
20V输入,12V 3A输出 效率96%DCDC降压芯片
驱动开发·单片机·硬件工程·智能硬件·pcb工艺
mounter6254 天前
跨越鸿沟:从内核驱动开发到超大规模云端生产环境的思考
linux·驱动开发·linux kernel·kernel
Saniffer_SH4 天前
NAND技术(二):从 Channel、Die/LUN、P/E Cycle 到 LDPC,一次讲透 NAND 里那些最容易误解的概念
人工智能·驱动开发·嵌入式硬件·测试工具·fpga开发·计算机外设·压力测试
2401_854151554 天前
嵌入式传感器驱动开发深度解析——从 I2C/SPI 驱动到数据融合算法
驱动开发·算法
Bug退散师5 天前
多路IO复用[select版TCP服务器与poll版TCP服务器]与常用网络编程函数详解
linux·服务器·c语言·网络·驱动开发·tcp/ip