×
嵌入式 > 技术百科 > 详情

i2c的设备树是如何匹配以及何时调用probe的?

发布时间:2024-04-16 发布时间:
|

一、粉丝提问

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 4 1>;
 rst-gpios = <&gpio1 2 0>;
 csi_id = <0>;
 mclk = <24000000>;
 mclk_source = <0>;
 status = "okay";
 port {
  ov5640_ep: endpoint {
   remote-endpoint = <&csi1_ep>;
  };
 };
};

驱动最重要的结构体如下:

ov5640_i2c_driver

要搞懂这个问题,我们需要有一些基础知识:

1.内核如何维护i2c总线

Linux内核维护很多总线,platform、usb、i2c、spi、pci等等,这个总线的架构在内核中都支持的很完善,内核通过以下结构体来维护总线:

struct bus_type {
const char  *name;
const char  *dev_name;
struct device  *dev_root;
struct device_attribute *dev_attrs;  use dev_groups instead
const struct attribute_group **bus_groups;
const struct attribute_group **dev_groups;
const struct attribute_group **drv_groups;
int (*match)(struct device *dev, struct device_driver *drv);
int (*uevent)(struct device *dev, struct kobj_uevent_env *env);
int (*probe)(struct device *dev);
int (*remove)(struct device *dev);
void (*shutdown)(struct device *dev);
int (*online)(struct device *dev);
int (*offline)(struct device *dev);
int (*suspend)(struct device *dev, pm_message_t state);
int (*resume)(struct device *dev);
const struct dev_pm_ops *pm;
struct iommu_ops *iommu_ops;
struct subsys_private *p;
struct lock_class_key lock_key;
};

i2c对应总线结构体变量为i2c_bus_type,定义如下:

drivers/i2c/I2c-core.c
struct bus_type i2c_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()中初始化:

static int __init i2c_init(void)

…………
retval = bus_register(&i2c_bus_type);
…………

i2c架构是通用架构,可支持多种不同的i2c控制器驱动。

2. i2c架构如何如何管理硬件信息和驱动?

不论哪一种总线,一定会维护两个链表,一个是驱动链表,一个是硬件信息链表。链表如下:

i2c总线的两个节点信息如下:

「struct i2c_driver」

struct i2c_driver {
unsigned int class;
 Notifies the driver that a new bus has appeared. You should avoid
 * using this, it will be removed in a near future.
 
int (*attach_adapter)(struct i2c_adapter *) __deprecated;
 Standard driver model interfaces
int (*probe)(struct i2c_client *, const struct i2c_device_id *);
int (*remove)(struct i2c_client *);
 driver model interfaces that don't relate to enumeration  
void (*shutdown)(struct i2c_client *);
int (*suspend)(struct i2c_client *, pm_message_t mesg);
int (*resume)(struct i2c_client *);
 Alert callback, for example for the SMBus alert protocol.
 * The format and meaning of the data value depends on the protocol.
 * For the SMBus alert protocol, there is a single bit of data passed
 * as the alert response's low bit ("event flag").
 
void (*alert)(struct i2c_client *, unsigned int data);
 a ioctl like command that can be used to perform specific functions
 * with the device.
 
int (*command)(struct i2c_client *client, unsigned int cmd, void *arg);
struct device_driver driver;
const struct i2c_device_id *id_table;
 Device detection callback for automatic device creation
int (*detect)(struct i2c_client *, struct i2c_board_info *);
const unsigned short *address_list;
struct list_head clients;
};
当总线匹配驱动和硬件信息成功后就会调用其中的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 driverint irq从设备地址往往会有一根中断线连接到SOC的中断控制器struct list_head detected链表3. i2c_driver和i2c_client1) 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

#define module_i2c_driver(__i2c_driver)
module_driver(__i2c_driver, i2c_add_driver,
  i2c_del_driver)
 
#define module_driver(__driver, __register, __unregister, ...)
static int __init __driver##_init(void)

return __register(&(__driver) , ##__VA_ARGS__);

module_init(__driver##_init);
static void __exit __driver##_exit(void)

__unregister(&(__driver) , ##__VA_ARGS__);

module_exit(__driver##_exit);
module_i2c_drive(ov5640_i2c_driver);

预编译:

gcc -E test.c

得到宏替换后的结果:

static int __init ov5640_i2c_driver_init(void)

 return i2c_add_driver(&(ov5640_i2c_driver));

module_init(ov5640_i2c_driver_init);
static void __exit ov5640_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生成流程如下:

i2c_client三、 i2c的设备树和驱动是如何匹配以及何时调用probe?1.  i2c的设备树和驱动是如何match,何时调用probe?

从第二章第3节可知,驱动程序中 module_i2c_drive()这个宏其实最终是调用 i2c_add_driver(&(ov5640_i2c_driver));注册ov5640_i2c_driver结构体;当我们insmod加载驱动模块文件时,会调用i2c_add_driver()。

该函数定义如下:

#define i2c_add_driver(driver)
i2c_register_driver(THIS_MODULE, driver)

下面我们来追踪i2c_register_driver()这个函数:

其中drv->bus就是我们之前所说的i2c_bus_type,上图中,分别调用了.match、.probe:

struct bus_type i2c_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()

i2c_device_match3. i2c_device_probe

如下图所示,通过driver->probe()调用到我们定义的struct i2c_driver ov5640_i2c_driver结构体变量中的ov5640_probe()函数:

i2c_device_probe

【注意】内核代码中大量使用到driver = to_i2c_driver(dev->driver);通过通用的结构体变量成员struct device_driver *driver来查找自己注册的xx_driver地址。


『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
馈通滤波器的运用指南