i2c的设备树和驱动是如何匹配以及何时调用probe的?粉丝手里的I2C外设是ov5640,一个摄像头。
设备树信息如下:
ov5640:ov5640@3c{
compatible="ovti,ov5640";
reg=<0x3c>;
pinctrl-names="default";
pinctrl-0=<&pinctrl_csi1
&csi_pwn_rst>;
clocks=<&clks IMX6UL_CLK_CSI>;
clock-names="csi_mclk";
pwn-gpios=<&gpio1 41>;
rst-gpios=<&gpio1 20>;
csi_id=<0>;
mclk=<24000000>;
mclk_source=<0>;
status="okay";
port{
ov5640_ep:endpoint{
remote-endpoint=<&csi1_ep>;
};
};
};
驱动最重要的结构体如下:
![7b357dc6-45ba-11eb-8b86-12bb97331649.png](https://file.elecfans.com/web1/M00/D6/9E/o4YBAF_kWJGAbKmtAACc3V8GFwk316.png)
要搞懂这个问题,我们需要有一些基础知识:
1.内核如何维护i2c总线
Linux内核维护很多总线,platform、usb、i2c、spi、pci等等,这个总线的架构在内核中都支持的很完善,内核通过以下结构体来维护总线:
structbus_type{
constchar*name;
constchar*dev_name;
structdevice*dev_root;
structdevice_attribute*dev_attrs;/*usedev_groupsinstead*/
conststructattribute_group**bus_groups;
conststructattribute_group**dev_groups;
conststructattribute_group**drv_groups;
int(*match)(structdevice*dev,structdevice_driver*drv);
int(*uevent)(structdevice*dev,structkobj_uevent_env*env);
int(*probe)(structdevice*dev);
int(*remove)(structdevice*dev);
void(*shutdown)(structdevice*dev);
int(*online)(structdevice*dev);
int(*offline)(structdevice*dev);
int(*suspend)(structdevice*dev,pm_message_tstate);
int(*resume)(structdevice*dev);
conststructdev_pm_ops*pm;
structiommu_ops*iommu_ops;
structsubsys_private*p;
structlock_class_keylock_key;
};
i2c对应总线结构体变量为i2c_bus_type,定义如下:
drivers/i2c/I2c-core.c
structbus_typei2c_bus_type={
.name="i2c",
.match=i2c_device_match,
.probe=i2c_device_probe,
.remove=i2c_device_remove,
.shutdown=i2c_device_shutdown,
.pm=&i2c_device_pm_ops,
};
其中:
- i2c_device_match(),匹配总线维护的驱动链表和设备信息链表,如果其中名字完全相同,则返回true,否则false;
- i2c_device_probe(),当我们注册一个i2c_drive或者i2c_client结构体时,会从对应的链表中查找节点,并通过i2c_device_match函数比较,如果匹配成功,则调用i2c_drive中定义的probe函数,即ov5640的ov5640_probe()函数;
- remove:如果卸载i2c_drive或者i2c_client结构体,会调用该函数卸载对应的资源;
- shutdown、pm是电源管理的接口,在此不讨论。
该结构体变量在函数i2c_init()中初始化:
staticint__initi2c_init(void)
{
…………
retval=bus_register(&i2c_bus_type);
…………
}
i2c架构是通用架构,可支持多种不同的i2c控制器驱动。
2. i2c架构如何如何管理硬件信息和驱动?
不论哪一种总线,一定会维护两个链表,一个是驱动链表,一个是硬件信息链表。链表如下:
i2c总线的两个节点信息如下:
「struct i2c_driver」
structi2c_driver{
unsignedintclass;
/*Notifiesthedriverthatanewbushasappeared.Youshouldavoid
*usingthis,itwillberemovedinanearfuture.
*/
int(*attach_adapter)(structi2c_adapter*)__deprecated;
/*Standarddrivermodelinterfaces*/
int(*probe)(structi2c_client*,conststructi2c_device_id*);
int(*remove)(structi2c_client*);
/*drivermodelinterfacesthatdon'trelatetoenumeration*/
void(*shutdown)(structi2c_client*);
int(*suspend)(structi2c_client*,pm_message_tmesg);
int(*resume)(structi2c_client*);
/*Alertcallback,forexamplefortheSMBusalertprotocol.
*Theformatandmeaningofthedatavaluedependsontheprotocol.
*FortheSMBusalertprotocol,thereisasinglebitofdatapassed
*asthealertresponse'slowbit("eventflag").
*/
void(*alert)(structi2c_client*,unsignedintdata);
/*aioctllikecommandthatcanbeusedtoperformspecificfunctions
*withthedevice.
*/
int(*command)(structi2c_client*client,unsignedintcmd,void*arg);
structdevice_driverdriver;
conststructi2c_device_id*id_table;
/*Devicedetectioncallbackforautomaticdevicecreation*/
int(*detect)(structi2c_client*,structi2c_board_info*);
constunsignedshort*address_list;
structlist_headclients;
};
- 当总线匹配驱动和硬件信息成功后就会调用其中的probe()函数;
- struct device_driver driver,内核中注册的驱动模块,必须包含该类型的结构体成员。
「struct i2c_client」
成员 | 含义 |
---|---|
unsigned short flags | 从设备地址长度 |
unsigned short addr | 从设备地址 |
char name[I2C_NAME_SIZE] | 从设备地址名称 |
struct i2c_adapter *adapter | 从设备地址对应的控制器驱动地址 |
struct device dev | 注册到内核的每一个设备模块都需要先定义一个该结构体变量,对应struct device_driver driver |
int irq | 从设备地址往往会有一根中断线连接到SOC的中断控制器 |
struct list_head detected | 链表 |
3. i2c_driver和i2c_client
1) i2c_driver如何注册
i2c_driver结构需要我们自己定义,然后通过函数i2c_register_driver()注册,将该结构体变量注册到i2c_driver链表,同时从i2c_client链表中查找是否有匹配的节点:
设备树情况下,会比较i2c_drive->driver->of_match_table->compatible和i2c_client->name,对应例子中的of_ov5640_id:
非设备树比较i2c_drive->id_table->name和i2c_client->name,对应例子中的ov5640_id:
代码中并没有直接调用函数i2c_register_driver()注册,而是使用了下面的这个宏:
该宏定义如下:
include/linux/I2c.h
该宏其实自动帮我生成了insmod和rmmod会用到宏module_init和module_exit,以及注册和注销i2c_driver结构体的代码。
如果看不明白宏,可以编写测试文件:test.c
#definemodule_i2c_driver(__i2c_driver)
module_driver(__i2c_driver,i2c_add_driver,
i2c_del_driver)
#definemodule_driver(__driver,__register,__unregister,...)
staticint__init__driver##_init(void)
{
return__register(&(__driver),##__VA_ARGS__);
}
module_init(__driver##_init);
staticvoid__exit__driver##_exit(void)
{
__unregister(&(__driver),##__VA_ARGS__);
}
module_exit(__driver##_exit);
module_i2c_drive(ov5640_i2c_driver);
预编译:
gcc-Etest.c
得到宏替换后的结果:
staticint__initov5640_i2c_driver_init(void)
{
returni2c_add_driver(&(ov5640_i2c_driver));
}
module_init(ov5640_i2c_driver_init);
staticvoid__exitov5640_i2c_driver_exit(void)
{
i2c_del_driver(&(ov5640_i2c_driver));
}
module_exit(ov5640_i2c_driver_exit);;
内核中有大量的高效简洁的宏定义,Linux内核就是个宝库,里面有大量的优秀的代码,想提高自己的编程能力,就一定要多看代码,代码读百遍,其义自见。
一口君认为,如果Linux代码都看不太明白,就不要自称精通C语言,充其量是会用C语言。
2)i2c_client如何生成(只讨论有设备树的情况)
在有设备树的情况下,i2c_client的生成是要在控制器驱动adapter注册情况下从设备树中枚举出来的。
i2c控制器有很多种,不同的厂家都会设计自己特有的i2c控制器,但是不论哪一个控制器,最终都会调用 i2c_register_adapter()注册控制器驱动。
i2c_client生成流程如下:
![7ca2c8d0-45ba-11eb-8b86-12bb97331649.png](https://file.elecfans.com/web1/M00/D6/9E/o4YBAF_kWJKACuZcAAUbVFIky7Y101.png)
三、 i2c的设备树和驱动是如何匹配以及何时调用probe?
1. i2c的设备树和驱动是如何match,何时调用probe?
从第二章第3节可知,驱动程序中 module_i2c_drive()这个宏其实最终是调用 i2c_add_driver(&(ov5640_i2c_driver));注册ov5640_i2c_driver结构体;当我们insmod加载驱动模块文件时,会调用i2c_add_driver()。
该函数定义如下:
#definei2c_add_driver(driver)
i2c_register_driver(THIS_MODULE,driver)
下面我们来追踪i2c_register_driver()这个函数:
其中drv->bus就是我们之前所说的i2c_bus_type,上图中,分别调用了.match、.probe:
structbus_typei2c_bus_type={
.name="i2c",
.match=i2c_device_match,
.probe=i2c_device_probe,
.remove=i2c_device_remove,
.shutdown=i2c_device_shutdown,
.pm=&i2c_device_pm_ops,
};
下面我们来追一追这两个函数
2. i2c_device_match()
![7d5979a4-45ba-11eb-8b86-12bb97331649.png](https://file.elecfans.com/web1/M00/D6/9E/o4YBAF_kWJKAb40rAAMQFZGc1zo290.png)
3. i2c_device_probe
如下图所示,通过driver->probe()调用到我们定义的struct i2c_driver ov5640_i2c_driver
结构体变量中的ov5640_probe()函数:
![7da22546-45ba-11eb-8b86-12bb97331649.png](https://file.elecfans.com/web1/M00/D6/9E/o4YBAF_kWJKAQ-igAACfX75oWcQ693.png)
【注意】 内核代码中大量使用到driver = to_i2c_driver(dev->driver);
通过通用的结构体变量成员struct device_driver *driver
来查找自己注册的xx_driver地址。
-
I2C
+关注
关注
28文章
1495浏览量
124807 -
Probes
+关注
关注
0文章
7浏览量
7413
原文标题:i2c的设备树和驱动是如何匹配以及何时调用probe的?
文章出处:【微信号:gh_c472c2199c88,微信公众号:嵌入式微处理器】欢迎添加关注!文章转载请注明出处。
发布评论请先 登录
相关推荐
如何选择I2C总线拉电阻
I2C总线与Arduino的接口示例
I2C总线的工作模式介绍
I2C总线设备地址设置方法
I2C总线应用实例分析
RISC V的I2C操作
飞凌嵌入式ElfBoard ELF 1板卡-i2c与从设备通讯编程示例之i2c编写程序
stm32mp135 i2c3无法正常probe怎么解决?
SPI和I2C通信协议:应用与区别
什么是I2C协议 I2C总线的控制逻辑
![什么是<b class='flag-5'>I2C</b>协议 <b class='flag-5'>I2C</b>总线的控制逻辑](https://file1.elecfans.com/web2/M00/C3/E6/wKgZomXvre-AWsW5AABL2e5FJAM091.png)
评论