一、粉絲提問
i2c的設(shè)備樹和驅(qū)動是如何匹配以及何時調(diào)用probe的?粉絲手里的I2C外設(shè)是ov5640,一個攝像頭。粉絲提問,一口君必須安排。
二、問題分析
設(shè)備樹信息如下:
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>;
};
};
};
驅(qū)動最重要的結(jié)構(gòu)體如下:
ov5640_i2c_driver
要搞懂這個問題,我們需要有一些基礎(chǔ)知識:
1.內(nèi)核如何維護i2c總線
Linux內(nèi)核維護很多總線,platform、usb、i2c、spi、pci等等,這個總線的架構(gòu)在內(nèi)核中都支持的很完善,內(nèi)核通過以下結(jié)構(gòu)體來維護總線:
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對應(yīng)總線結(jié)構(gòu)體變量為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(),匹配總線維護的驅(qū)動鏈表和設(shè)備信息鏈表,如果其中名字完全相同,則返回true,否則false;i2c_device_probe(),當我們注冊一個i2c_drive或者i2c_client結(jié)構(gòu)體時,會從對應(yīng)的鏈表中查找節(jié)點,并通過i2c_device_match函數(shù)比較,如果匹配成功,則調(diào)用i2c_drive中定義的probe函數(shù),即ov5640的ov5640_probe()函數(shù);remove:如果卸載i2c_drive或者i2c_client結(jié)構(gòu)體,會調(diào)用該函數(shù)卸載對應(yīng)的資源;shutdown、pm是電源管理的接口,在此不討論。
該結(jié)構(gòu)體變量在函數(shù)i2c_init()中初始化:
static int __init i2c_init(void)
{
…………
retval = bus_register(&i2c_bus_type);
…………
}
i2c架構(gòu)是通用架構(gòu),可支持多種不同的i2c控制器驅(qū)動。
2. i2c架構(gòu)如何如何管理硬件信息和驅(qū)動?
不論哪一種總線,一定會維護兩個鏈表,一個是驅(qū)動鏈表,一個是硬件信息鏈表。鏈表如下:
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;
};
當總線匹配驅(qū)動和硬件信息成功后就會調(diào)用其中的probe()函數(shù);struct device_driver driver,內(nèi)核中注冊的驅(qū)動模塊,必須包含該類型的結(jié)構(gòu)體成員。
「struct i2c_client」
成員含義unsigned short flags從設(shè)備地址長度unsigned short addr從設(shè)備地址char name[I2C_NAME_SIZE]從設(shè)備地址名稱struct i2c_adapter *adapter從設(shè)備地址對應(yīng)的控制器驅(qū)動地址struct device dev注冊到內(nèi)核的每一個設(shè)備模塊都需要先定義一個該結(jié)構(gòu)體變量,對應(yīng)struct device_driver driverint irq從設(shè)備地址往往會有一根中斷線連接到SOC的中斷控制器struct list_head detected鏈表3. i2c_driver和i2c_client1) i2c_driver如何注冊
i2c_driver結(jié)構(gòu)需要我們自己定義,然后通過函數(shù)i2c_register_driver()注冊,將該結(jié)構(gòu)體變量注冊到i2c_driver鏈表,同時從i2c_client鏈表中查找是否有匹配的節(jié)點:
設(shè)備樹情況下,會比較i2c_drive->driver->of_match_table->compatible和i2c_client->name,對應(yīng)例子中的of_ov5640_id:
非設(shè)備樹比較i2c_drive->id_table->name和i2c_client->name,對應(yīng)例子中的ov5640_id:
代碼中并沒有直接調(diào)用函數(shù)i2c_register_driver()注冊,而是使用了下面的這個宏:
該宏定義如下:
include/linux/I2c.h
該宏其實自動幫我生成了insmod和rmmod會用到宏module_init和module_exit,以及注冊和注銷i2c_driver結(jié)構(gòu)體的代碼。
如果看不明白宏,可以編寫測試文件: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);
預(yù)編譯:
gcc -E test.c
得到宏替換后的結(jié)果:
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);;
內(nèi)核中有大量的高效簡潔的宏定義,Linux內(nèi)核就是個寶庫,里面有大量的優(yōu)秀的代碼,想提高自己的編程能力,就一定要多看代碼,代碼讀百遍,其義自見。
一口君認為,如果Linux代碼都看不太明白,就不要自稱精通C語言,充其量是會用C語言。
2)i2c_client如何生成(只討論有設(shè)備樹的情況)
在有設(shè)備樹的情況下,i2c_client的生成是要在控制器驅(qū)動adapter注冊情況下從設(shè)備樹中枚舉出來的。
i2c控制器有很多種,不同的廠家都會設(shè)計自己特有的i2c控制器,但是不論哪一個控制器,最終都會調(diào)用i2c_register_adapter()注冊控制器驅(qū)動。
i2c_client生成流程如下:
i2c_client三、 i2c的設(shè)備樹和驅(qū)動是如何匹配以及何時調(diào)用probe?1. i2c的設(shè)備樹和驅(qū)動是如何match,何時調(diào)用probe?
從第二章第3節(jié)可知,驅(qū)動程序中 module_i2c_drive()這個宏其實最終是調(diào)用 i2c_add_driver(&(ov5640_i2c_driver));注冊ov5640_i2c_driver結(jié)構(gòu)體;當我們insmod加載驅(qū)動模塊文件時,會調(diào)用i2c_add_driver()。
該函數(shù)定義如下:
#define i2c_add_driver(driver)
i2c_register_driver(THIS_MODULE, driver)
下面我們來追蹤i2c_register_driver()這個函數(shù):
其中drv->bus就是我們之前所說的i2c_bus_type,上圖中,分別調(diào)用了.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()調(diào)用到我們定義的struct i2c_driver ov5640_i2c_driver結(jié)構(gòu)體變量中的ov5640_probe()函數(shù):
i2c_device_probe
【注意】內(nèi)核代碼中大量使用到driver = to_i2c_driver(dev->driver);通過通用的結(jié)構(gòu)體變量成員struct device_driver *driver來查找自己注冊的xx_driver地址。
-
驅(qū)動
+關(guān)注
關(guān)注
12文章
1961瀏覽量
88587 -
I2C
+關(guān)注
關(guān)注
28文章
1556瀏覽量
131314
發(fā)布評論請先 登錄
RK3576平臺PCA9548 I2C開關(guān)設(shè)備樹配置與生效全解析
I2C死鎖的問題
I2C的缺點介紹
第十八章 I2C通信測試
基于RT-Thread的I2C(軟件) 實踐 | 技術(shù)集結(jié)
嵌入式學習-飛凌嵌入式ElfBoard ELF 1板卡-I2C設(shè)備驅(qū)動之I2C驅(qū)動之溫濕度傳感器
嵌入式學習-飛凌嵌入式ElfBoard ELF 1板卡-I2C設(shè)備驅(qū)動之Linux下的I2C驅(qū)動簡介
嵌入式學習-飛凌嵌入式ElfBoard ELF 1板卡-I2C設(shè)備驅(qū)動之I2C驅(qū)動構(gòu)建流程
飛凌嵌入式ElfBoard ELF 1板卡-I2C設(shè)備驅(qū)動之I2C驅(qū)動之溫濕度傳感器
飛凌嵌入式ElfBoard ELF 1板卡-I2C設(shè)備驅(qū)動之I2C驅(qū)動構(gòu)建流程
飛凌嵌入式ElfBoard ELF 1板卡-I2C設(shè)備驅(qū)動之Linux下的I2C驅(qū)動簡介
嵌入式教育科普|I2C接口全面解析
i2c的設(shè)備樹是如何匹配以及何時調(diào)用probe的
評論