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

编译及开发板测试

相关推荐
楼兰公子5 小时前
# RK3588 Linux 驱动开发完整学习指南RK3588_Linux_Driver_Development.md
linux·驱动开发
念何架构之路1 天前
GoFrameMap转换详解
驱动开发
charlie1145141911 天前
嵌入式Linux嵌入式Linux驱动开发:设备树驱动改造——从硬编码到设备树的实战之旅
linux·运维·驱动开发
国产芯片设计1 天前
小家电单段码屏项目实战|YL1621 LCD驱动开发与调试心得
驱动开发·stm32·单片机·mcu·51单片机
小此方1 天前
Re:Linux系统篇(十六) 进程篇 · 一:深入理解操作系统:从软硬件架构到“先描述,再组织”的管理哲学
linux·驱动开发·硬件架构
济6171 天前
I.MX6U Linux 驱动开发篇---异步通知(信号)实验--- Ubuntu20.04
linux·驱动开发·嵌入式·嵌入式linux驱动开发
charlie1145141911 天前
嵌入式Linux嵌入式Linux驱动开发:板级DTS实操与完整实战演练——从修改设备树到点亮LED的完整闭环
linux·运维·驱动开发
智者知已应修善业2 天前
【74ls138+74ls00传送带故障报警】2024-1-9
驱动开发·经验分享·笔记·硬件架构·硬件工程
不怕犯错,就怕不做2 天前
linux的notifier_block内核通知链
linux·驱动开发·嵌入式硬件
Szime2 天前
CS57167半桥驱动H桥方案,国产替代BOM配单选型
驱动开发