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

编译及开发板测试

相关推荐
TangDuoduo00054 天前
【Linux SPI驱动开发】
驱动开发
The️4 天前
Linux驱动开发之Read_Write函数
linux·运维·服务器·驱动开发·ubuntu·交互
FserSuN4 天前
AI编程 - 规范驱动开发(SDD)学习
驱动开发·学习·ai编程
TangDuoduo00055 天前
【Linux I2C设备驱动】
linux·驱动开发
The️5 天前
Linux驱动开发之Open_Close函数
linux·运维·驱动开发·mcu·ubuntu
LCG元5 天前
嵌入式GUI设计:STM32F429+LVGL,智能仪表盘界面开发指南
驱动开发·stm32·嵌入式硬件
小龙报6 天前
【51单片机】 给单片机加 “安全锁”!看门狗 WDT:原理 + 配置 + 复位验证全拆解,让程序稳定不跑飞
驱动开发·stm32·单片机·嵌入式硬件·物联网·51单片机·硬件工程
码农编程录6 天前
【notes12】kbuild,内核模块化,字符设备驱动,设备树,platform总线,设备驱动模型
驱动开发
乔碧萝成都分萝6 天前
二十六、IIO子系统 + SPI子系统 + ICM20608
linux·驱动开发·嵌入式
A星空1236 天前
二、交叉编译工具链(arm-linux-gnueabihf-gcc)安装与验证,搭建 TFTP+NFS 服务,调试开发板网络连通性;
linux·c++·驱动开发·单片机·嵌入式硬件