一、粉絲提問
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結構體時,會從對應的鏈表中查找節(jié)點,并通過i2c_device_match函數(shù)比較,如果匹配成功,則調用i2c_drive中定義的probe函數(shù),即ov5640的ov5640_probe()函數(shù);remove:如果卸載i2c_drive或者i2c_client結構體,會調用該函數(shù)卸載對應的資源;shutdown、pm是電源管理的接口,在此不討論。
該結構體變量在函數(shù)i2c_init()中初始化:
static int __init i2c_init(void)
{
…………
retval = bus_register(&i2c_bus_type);
…………
}
i2c架構是通用架構,可支持多種不同的i2c控制器驅動。
2. i2c架構如何如何管理硬件信息和驅動?
不論哪一種總線,一定會維護兩個鏈表,一個是驅動鏈表,一個是硬件信息鏈表。鏈表如下:
i2c總線的兩個節(jié)點信息如下:
「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()函數(shù);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結構需要我們自己定義,然后通過函數(shù)i2c_register_driver()注冊,將該結構體變量注冊到i2c_driver鏈表,同時從i2c_client鏈表中查找是否有匹配的節(jié)點:
設備樹情況下,會比較i2c_drive->driver->of_match_table->compatible和i2c_client->name,對應例子中的of_ov5640_id:
非設備樹比較i2c_drive->id_table->name和i2c_client->name,對應例子中的ov5640_id:
代碼中并沒有直接調用函數(shù)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內核就是個寶庫,里面有大量的優(yōu)秀的代碼,想提高自己的編程能力,就一定要多看代碼,代碼讀百遍,其義自見。
一口君認為,如果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節(jié)可知,驅動程序中 module_i2c_drive()這個宏其實最終是調用 i2c_add_driver(&(ov5640_i2c_driver));注冊ov5640_i2c_driver結構體;當我們insmod加載驅動模塊文件時,會調用i2c_add_driver()。
該函數(shù)定義如下:
#define i2c_add_driver(driver)
i2c_register_driver(THIS_MODULE, driver)
下面我們來追蹤i2c_register_driver()這個函數(shù):
其中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,
};
下面我們來追一追這兩個函數(shù)
2. i2c_device_match()
i2c_device_match3. i2c_device_probe
如下圖所示,通過driver->probe()調用到我們定義的struct i2c_driver ov5640_i2c_driver結構體變量中的ov5640_probe()函數(shù):
i2c_device_probe
【注意】內核代碼中大量使用到driver = to_i2c_driver(dev->driver);通過通用的結構體變量成員struct device_driver *driver來查找自己注冊的xx_driver地址。
-
驅動
+關注
關注
12文章
1916瀏覽量
86906 -
I2C
+關注
關注
28文章
1541瀏覽量
127787
發(fā)布評論請先 登錄
嵌入式學習-飛凌嵌入式ElfBoard ELF 1板卡-I2C設備驅動之I2C驅動構建流程
飛凌嵌入式ElfBoard ELF 1板卡-I2C設備驅動之I2C驅動之溫濕度傳感器
飛凌嵌入式ElfBoard ELF 1板卡-I2C設備驅動之I2C驅動構建流程
I2C總線通信原理 如何設計I2C總線電路
I2C總線與Arduino的接口示例
I2C總線的工作模式介紹
I2C總線協(xié)議詳細解析
I2C總線設備地址設置方法
I2C總線應用實例分析
I2C總線上拉電阻阻值如何確定?

RISC V的I2C操作
飛凌嵌入式ElfBoard ELF 1板卡-i2c與從設備通訊編程示例之i2c編寫程序
I2C協(xié)議的基礎知識

評論