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
要搞懂這個問題,我們需要有一些基礎知識:
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_init()中初始化:
static int __init i2c_init(void){ ………… retval = bus_register(&i2c_bus_type); …………}
i2c架構是通用架構,可支持多種不同的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;};
「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 鍊表
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語言。
在有設備樹的情況下,i2c_client的生成是要在控制器驅動adapter註冊情況下從設備樹中枚舉出來的。
i2c控制器有很多種,不同的廠家都會設計自己特有的i2c控制器,但是不論哪一個控制器,最終都會調用 i2c_register_adapter()註冊控制器驅動。
i2c_client生成流程如下:
i2c_client
從第二章第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,};
下面我們來追一追這兩個函數
i2c_device_match
如下圖所示,通過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地址。
往期問答匯總:
更多關於Linux arm的知識,請關注 一口Linux