Linux驱动开发(4)——总线驱动

一、platform总线

Platform:虚拟总线平台,Linux专门用来管理外设(LED、按键、GPIO、I2C、SPI 等)。platform 总线架构将驱动系统抽象为以下三部分:

  • 设备(Device) :描述硬件设备的资源信息,如寄存器地址、GPIO 编号等,通常由设备树定义,内核启动时将.compatible自动转换为 platform_device 结构。
  • 总线(Bus):负责设备与驱动的匹配,platform 总线内置匹配规则,自动将合适的驱动与设备绑定。
  • 驱动(Driver) :包含对设备的操作方法,如 proberemove 等回调函数,实现具体硬件控制逻辑,对应 platform_driver 结构。

1、设备(device):设备树节点

cs 复制代码
设备节点{
    .compatible = "标识,设备名称";
};

platform_device{  //内核启动后将设备节点转换为此结构体
    struct device dev; //设备信息,eg:of_node存放设备节点
};

2、驱动(driver):platfrom_deriver结构

cs 复制代码
struct platform_driver drvname{
    .probe = probe,    //驱动与设备匹配成功执行
    .remove = remove,  //驱动与设备分离执行
    .driver = {        //驱动核心信息
        .name =  "设备名",
        .of_match_table =  ,//可多个设备匹配一个驱动
    },
    id_table = ,
};


int probe(struct platform_device *);
int remove(struct platform_device *);
struct device_driver driver;
struct of_device_id of_match_table[] = {
    {.compatible = "标签,设备名"},      //与设备节点中的compatible匹配
    {},         //空结尾,防越界
};
struct platform_driver_id id_table[]={
    {.name = "设备名"},
    {},
};

3、总线(bus):platform_bus_type匹配结构

.match = 设备与驱动匹配规则:

  • platform_device中的override与驱动名一致
  • 设备节点中的.compatible选项与驱动of_match_table一致
  • 设备树中的设备名与驱动中的id_tables一致
  • 设备名和驱动名一致

二、platform常用API函数

1).函数:platform_driver_register(&drv);

  • 功能:向内核注册一个平台驱动,将其绑定到platform虚拟总线上,并触发匹配设备的功能。
  • 参数:@drv --- 驱动platform_driver结构体

2).函数:platform_driver_unregister(&drv);

  • 功能:分离驱动与设备

2).函数:int of_get_named_gpio(struct device_node *np, const char *propname, int index);

  • 功能:获得设备树中的gpio节点
  • 参数:@np --- 设备树节点(pdevice->dev.of_node),@propname --- 属性名,@index --- reg数组下标
相关推荐
世微 如初17 天前
AP5125大功率LED恒流驱动实战:地摊灯项目从原理图到调试笔记
驱动开发·芯片·led电源驱动·降压恒流ic
ScilogyHunter17 天前
Zephyr串口驱动开发及构建完全指南
驱动开发·uart·zephyr
_Emma_17 天前
【DRM&Graphic】Linux图形与显示框架
linux·驱动开发·图形渲染·显示器
董厂长17 天前
Loop Engineering:停止手动提示,开始设计自动提示的系统
大数据·人工智能·驱动开发·llm
Saniffer_SH18 天前
【高清视频】Gen6 服务器还没到,Gen6 SSD 怎么测?Emily 现场演示三种测试环境
人工智能·驱动开发·测试工具·缓存·fpga开发·计算机外设·压力测试
暮云星影18 天前
全志linux开发屏幕适配(二)`HDMI`驱动适配说明
linux·arm开发·驱动开发
charlie11451419118 天前
嵌入式Linux驱动开发——从轮询到中断
linux·开发语言·驱动开发·嵌入式
暮云星影18 天前
瑞芯微rk3566开发FIT Secure Boot
linux·arm开发·驱动开发·安全
暮云星影18 天前
全志linux开发 USB接口设置
linux·arm开发·驱动开发
sukalot18 天前
windows显示驱动开发-CCD DDI的其它技术
windows·驱动开发