Linux_I2C總線分析(主要是probe的方式)1_第1頁
Linux_I2C總線分析(主要是probe的方式)1_第2頁
Linux_I2C總線分析(主要是probe的方式)1_第3頁
Linux_I2C總線分析(主要是probe的方式)1_第4頁
Linux_I2C總線分析(主要是probe的方式)1_第5頁
已閱讀5頁,還剩6頁未讀 繼續免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、Linux I2C 總線淺析 Overview內核空間層次!i2c adapter 是一個struct, 用來抽象一個物理i2c bus ,而且還和linux 設備驅動架構柔和在一起.如果只說硬件的話,就是在CPU內部集成的一個I2C控制器(提供給用戶的就是那幾個register),硬件上并沒的所謂的adapter,client這些東東,adapter和client都是linux驅動軟件抽象出來的東西 資料帖子:struct i2c_algorithm /* If an adapter algorithm can't do I2C-level access, set master_xf

2、er to NULL. If an adapter algorithm can do SMBus access, set smbus_xfer. If set to NULL, the SMBus protocol is simulated using common I2C messages */* master_xfer should return the number of messages successfully processed, or a negative value on error */int (*master_xfer)(struct i2c_adapter *adap,

3、struct i2c_msg *msgs, int num);int (*smbus_xfer) (struct i2c_adapter *adap, u16 addr, unsigned short flags, char read_write, u8 command, int size, union i2c_smbus_data *data);/* To determine what the adapter supports */u32 (*functionality) (struct i2c_adapter *);/* * i2c_adapter is the structure use

4、d to identify a physical i2c bus along * with the access algorithms necessary to access it. */struct i2c_adapter struct module *owner;unsigned int id;unsigned int class; /* classes to allow probing for */const struct i2c_algorithm *algo; /* the algorithm to access the bus */void *algo_data;/* data f

5、ields that are valid for all devices*/u8 level; /* nesting level for lockdep */struct mutex bus_lock;int timeout;/* in jiffies */int retries;struct device dev;/* the adapter device */int nr;char name48;struct completion dev_released;Linux的I2C體系結構分為3個組成部分:1·I2C核心:I2C核心提供了I2C總線驅動和設備驅動的注冊、注銷方法,I2C

6、通信方法(即“algorithm”)上層的、與具體適配器無關的代碼以及探測設備、檢測設備地址的上層代碼等。這部分是與平臺無關的。2·I2C總線驅動:I2C總線驅動是對I2C硬件體系結構中適配器端的實現。I2C總線驅動主要包含了I2C適配器數據結構i2c_adapter、I2C適配器的algorithm數據結構i2c_algorithm和控制I2C適配器產生通信信號的函數。經由I2C總線驅動的代碼,我們可以控制I2C適配器以主控方式產生開始位、停止位、讀寫周期,以及以從設備方式被讀寫、產生ACK等。不同的CPU平臺對應著不同的I2C總線驅動。 總線驅動的職責,是為系統中每個I2C總線增

7、加相應的讀寫方法。但是總線驅動本身并不會進行任何的通訊,它只是存在在那里,等待設備驅動調用其函數。這部分在MTK 6516中是由MTK已經幫我們實現了的,不需要我們更改。3· I2C設備驅動:I2C設備驅動是對I2C硬件體系結構中設備端的實現。設備一般掛接在受CPU控制的I2C適配器上,通過I2C適配器與CPU交換數據。I2C設備驅動主要包含了數據結構i2c_driver和i2c_client,我們需要根據具體設備實現其中的成員函數。在Linux內核源代碼中的drivers目錄下的i2c_dev.c文件,實現了I2C適配器設備文件的功能,應用程序通過“i2c-%d”文件名并使用文件操

8、作接口open()、write()、read()、ioctl()和close()等來訪問這個設備。應用層可以借用這些接口訪問掛接在適配器上的I2C設備的存儲空間或寄存器并控制I2C設備的工作方式。設備驅動則是與掛在I2C總線上的具體的設備通訊的驅動。通過I2C總線驅動提供的函數,設備驅動可以忽略不同總線控制器的差異,不考慮其實現細節地與硬件設備通訊。這部分在MTK 6516中是由具體的設備實現的。(比如camera)struct i2c_client:代表一個掛載到i2c總線上的i2c從設備,該設備所需要的數據結構,其中包括該i2c從設備所依附的i2c主設備 struct i2c_adapte

9、r *adapter 該i2c從設備的驅動程序struct i2c_driver *driver 作為i2c從設備所通用的成員變量,比如addr, name等 該i2c從設備驅動所特有的數據,依附于dev->driver_data下struct i2c_adapter:代表主芯片所支持的一個i2c主設備。struct i2c_algorithm *algo:是該i2c主設備傳輸數據的一種算法,或者說是在i2c總線上完成主從設備間數據通信的一種能力。Linux的i2c子系統新、舊架構并存。主要分為舊架構(Legacy)也有人稱之為adapter方式,和新的架構new-style的方式。這倆

10、者的區別主要在于設備注冊和驅動注冊的不同。對于Legacy的設備注冊是在驅動運行的時候動態的創建,而新式的new-style則是采用靜態定義的方式。注:MTK在Android2.1版上用的是Legacy的架構,而在Android2.2版上用的是new-style的架構。(在這里我就只說明Android2.2的new-style的實現方法)要完成I2C設備的驅動,我們可以分三步走:第一步:完成適配器的注冊(總線);第二步:完成I2C client的設備注冊(設備);第三步:完成I2C client驅動的注冊(驅動);我們分別給予介紹:(I2C-mt6516.c)就總線而言,其本質只需要我們填充倆

11、個結構體就可以了:i2c_adapter;i2c_algorithm;i2c_add_adapter(i2c->adap); 往總線上添加對應的適配器;struct i2c_adapter     struct module *owner;    unsigned int id;    unsigned int class;    /* classes to allow probing for */   const struct i2c_algori

12、thm *algo; /* the algorithm to access the bus */   void *algo_data;    /* - administration stuff. */   int (*client_register)(struct i2c_client *);    int (*client_unregister)(struct i2c_client *);      /* data fields that are val

13、id for all devices */   u8 level;    /* nesting level for lockdep */   struct mutex bus_lock;    struct mutex clist_lock;      int timeout;   /* in jiffies */   int retries;    struct device dev;

14、  /* the adapter device */     int nr; /*該成員描述了總線號*/   struct list_head clients; /* i2c_client結構鏈表,該結構包含device,driver和  adapter結構*/   char name48;    struct completion dev_released;   ; static struct i2c_algorithm mt6516_i2c_

15、algorithm = .master_xfer = mt6516_i2c_transfer,.smbus_xfer = NULL,.functionality = mt6516_i2c_functionality,;2、設備注冊第一步:記得以前的i2c設備驅動,設備部分喜歡驅動運行的時候動態創建,新式的驅動傾向于向傳統的linux下設備驅動看齊,采用靜態定義的方式來注冊設備,使用接口為:int _init i2c_register_board_info(int busnum, struct i2c_board_info const *info, unsigned len) int statu

16、s; mutex_lock(&_i2c_board_lock); /* dynamic bus numbers will be assigned after the last static one */ if (busnum >= _i2c_first_dynamic_bus_num) _i2c_first_dynamic_bus_num = busnum + 1; for (status = 0; len; len-, info+) struct i2c_devinfo *devinfo; devinfo = kzalloc(sizeof(*devinfo), GFP_KERN

17、EL);/申請表示i2c設備的結構體空間 if (!devinfo) pr_debug("i2c-core: can't register boardinfo!n"); status = -ENOMEM; break; /* 填寫i2c設備描述結構 */ devinfo->busnum = busnum; devinfo->board_info = *info; list_add_tail(&devinfo->list, &_i2c_board_list);/添加到全局鏈表_i2c_board_list中 mutex_unlock(

18、&_i2c_board_lock); return status;在系統初始化的過程中,我們可以通過 i2c_register_board_info,將所需要的I2C從設備加入一個名為_i2c_board_list雙向循環鏈表,系統在成功加載I2C主設備adapt后,就會對這張鏈表里所有I2C從設備逐一地完成 i2c_client的注冊。第二步: 系統初始化的時候,會根據板級i2c設備配置信息,創建i2c客戶端設備(i2c_client),添加到i2c子系統中:static void i2c_scan_static_board_info (struct i2c_adapter *ada

19、pter) struct i2c_devinfo *devinfo; mutex_lock(&_i2c_board_lock); list_for_each_entry(devinfo, &_i2c_board_list, list) /遍歷全局鏈表_i2c_board_list if (devinfo->busnum = adapter->nr && !i2c_new_device(adapter, &devinfo->board_info) printk(KERN_ERR "i2c-core: can't crea

20、te i2c%d-%04xn", i2c_adapter_id(adapter), devinfo->board_info.addr); mutex_unlock(&_i2c_board_lock);struct i2c_client *i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)struct i2c_client*client;intstatus;client = kzalloc(sizeof *client, GFP_KERNEL);if (!client)re

21、turn NULL;client->adapter = adap;client->dev.platform_data = info->platform_data;if (info->archdata)client->dev.archdata = *info->archdata;client->flags = info->flags;client->addr = info->addr;client->irq = info->irq;strlcpy(client->name, info->type, sizeof(

22、client->name);/* Check for address business */status = i2c_check_addr(adap, client->addr);if (status)goto out_err;client->dev.parent = &client->adapter->dev;client->dev.bus = &i2c_bus_type;client->dev.type = &i2c_client_type;dev_set_name(&client->dev, "%d

23、-%04x", i2c_adapter_id(adap), client->addr);status = device_register(&client->dev);if (status)goto out_err;dev_dbg(&adap->dev, "client %s registered with bus id %sn",client->name, dev_name(&client->dev);return client;out_err:dev_err(&adap->dev, "

24、Failed to register i2c client %s at 0x%02x ""(%d)n", client->name, client->addr, status);kfree(client);return NULL;IDR機制:完成的是設備ID和結構體的關聯。_i2c_first_dynamic_bus_num:當前系統允許的動態總線的最大值。i2c_scan_static_board_info(adap);/*完成新類型i2c設備的注冊,一般只在主板初始化時*/ 此函數為整個I2C子系統的核心,它會去遍歷一個由I2C從設備組成

25、的雙向循環鏈表,并完成所有I2C從設備的i2c_client的注冊。struct i2c_devinfo *devinfo;     /已經建立好了的I2C從設備鏈表status = i2c_check_addr(adap, client->addr);注: 特別要提一下的是這個“i2c_check_addr”,引用<<i2c 源代碼情景分析>>里的話:“i2c 設備的7 位地址是就當前i2c 總線而言的,是“相對地址”。不同的i2c 總線上的設備可以使用相同的7 位地址,但是它們所在的i2c 總線不同。所以在系統中一個i2

26、c 設備的“絕對地址”由二元組(i2c 適配器的ID 和設備在該總線上的7 位地址)表示。”,所以這個函數的作用主要是排除同一i2c總線上出現多個地址相同的設備。3、I2C驅動注冊:第一步:static inline int i2c_add_driver(struct i2c_driver *driver) return i2c_register_driver(THIS_MODULE, driver);int i2c_register_driver(struct module *owner, struct i2c_driver *driver)int res;/* Can't regi

27、ster until after driver model init */if (unlikely(WARN_ON(!i2c_bus_type.p)return -EAGAIN;/* add the driver to the list of i2c drivers in the driver core */driver->driver.owner = owner;driver->driver.bus = &i2c_bus_type;/* When registration returns, the driver core * will have called probe(

28、) for all matching-but-unbound devices. */res = driver_register(&driver->driver);if (res)return res;pr_debug("i2c-core: driver %s registeredn", driver->);INIT_LIST_HEAD(&driver->clients);/* Walk the adapters that are already present */mutex_lock(&core_lock);

29、bus_for_each_dev(&i2c_bus_type, NULL, driver, _attach_adapter);mutex_unlock(&core_lock);return 0;設備和驅動的關聯過程:首先當I2C從設備和I2C驅動如果處于同一條總線上,那么其在設備和驅動注冊之后,將會促使I2C_bus_type中的match獲得調用;()如下:struct bus_type i2c_bus_type = .name= "i2c",.match= i2c_device_match,.probe= i2c_device_probe,.remove

30、= i2c_device_remove,.shutdown= i2c_device_shutdown,.suspend= i2c_device_suspend,.resume= i2c_device_resume,;繼續跟進i2c_device_match;i2c_match_id(driver->id_table, client) != NULL;我們回到i2c_device_probe;這個函數的關鍵是:status = driver->probe(client, i2c_match_id(driver->id_table, client);它將函數的流程交回到了driv

31、er->probe的手中;流程圖:過程分享:1、設備和驅動的關聯大家知道,對于一個驅動程序有兩個元素不可或缺,即設備和驅動,一般驅動都是通過設備名和驅動名的匹配建立關系的,最開始我從代碼中只能發現驅動的注冊,卻不見設備注冊的蹤影,令人疑惑,跟蹤發現,在i2c adapter注冊時會遍歷i2c_board_info這樣一個結構,而這個結構在29以前或更早的內核里是不存在的,它會完成驅動與設備的匹配問題, 2、名字匹配一個i2c驅動是可以有多個名字的,即一個驅動程序可以支持多個設備,該機制是通過 struct i2c_device_id實現的,驅動中建立這么一個結構體數組,i2c架構層便會掃

32、描該數組,與設備名去匹配,匹配成功的都會進入相應probe函數。3、進入probe該過程困惑了我一段時間,其實要進入自己驅動的probe首先需要進入總線的probe,而進入總線probe的前提是與總線的match成功。待解決的困惑:1、I2C從設備名; Legacy 的相關知識:(一) Linux的I2C驅動框架中的主要數據結構及其關系 Linux的I2C驅動框架中的主要數據結構包括:i2c_driver、i2c_client、i2c_adapter和i2c_algorithm。i2c_adapter對應于物理上的一個適配器,這個適配器是基于不同的平臺的,一個I2C適配器需要i2c_algor

33、ithm中提供的通信函數來控制適配器,因此i2c_adapter中包含其使用的i2c_algorithm的指針。i2c_algorithm中的關鍵函數master_xfer()以i2c_msg為單位產生I2C訪問需要的信號。不同的平臺所對應的master_xfer()是不同的,開發人員需要根據所用平臺的硬件特性實現自己的XXX_xfer()方法以填充i2c_algorithm的master_xfer指針。i2c_ driver對應一套驅動方法,不對應于任何的物理實體。i2c_client對應于真實的物理設備,每個I2C設備都需要一個i2c_client來描述。i2c_client依附于i2c_adpater,這與I2C硬件體系中適配器和設備的關系一致。i2c_driver提供了i2c-client與i2c-adapter產生聯系的函數。當attach a_dapter()函數探測物理設備時,如果確定存在一個client,則把該client使用的i2c_client數據結構的

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論