IIC设备驱动程序
IIC設(shè)備是一種通過IIC總線連接的設(shè)備,由于其簡(jiǎn)單性,被廣泛引用于電子系統(tǒng)中。在現(xiàn)代電子系統(tǒng)中,有很多的IIC設(shè)備需要進(jìn)行相互之間通信
IIC總線是由PHILIPS公司開發(fā)的兩線式串行總線,用于連接微處理器和外部IIC設(shè)備。IIC設(shè)備產(chǎn)生于20世紀(jì)80年代,最初專用與音頻和視頻設(shè)備,現(xiàn)在在各種電子設(shè)備中都廣泛應(yīng)用
IIC總線有兩條總線線路,一條是串行數(shù)據(jù)線(SDA),一條是串行時(shí)鐘線(SCL)。SDA負(fù)責(zé)數(shù)據(jù)傳輸,SCL負(fù)責(zé)數(shù)據(jù)傳輸?shù)臅r(shí)鐘同步。IIC設(shè)備通過這兩條總線連接到處理器的IIC總線控制器上。一種典型的設(shè)備連接如圖:
與其他總線相比,IIC總線有很多重要的特點(diǎn)。在選擇一種設(shè)備來完成特定功能時(shí),這些特點(diǎn)是選擇IIC設(shè)備的重要依據(jù)。
主要特點(diǎn):
1,每一個(gè)連接到總線的設(shè)備都可以通過唯一的設(shè)備地址單獨(dú)訪問
2,串行的8位雙向數(shù)據(jù)傳輸,位速率在標(biāo)準(zhǔn)模式下可達(dá)到100kb/s;快速模式下可以達(dá)到400kb/s;告訴模式下可以達(dá)到3.4Mb/s
3,總線長(zhǎng)度最長(zhǎng)7.6m左右
4,片上濾波器可以增加抗干擾能力,保證數(shù)據(jù)的完成傳輸
5,連接到一條IIC總線上的設(shè)備數(shù)量只受到最大電容400pF的限制
6,它是一個(gè)多主機(jī)系統(tǒng),在一條總線上可以同時(shí)有多個(gè)主機(jī)存在,通過沖突檢測(cè)方式和延時(shí)等待防止數(shù)據(jù)不被破壞。同一時(shí)間只能有一個(gè)主機(jī)占用總線
?
IIC總線在傳輸數(shù)據(jù)的過程中有3種類型的信號(hào):開始信號(hào)、結(jié)束信號(hào)、和應(yīng)答信號(hào)
>>開始信號(hào)(S): 當(dāng)SCL為高電平時(shí),SDA由高電平向低電平跳變,表示將要開始傳輸數(shù)據(jù)
>>結(jié)束信號(hào)(P):當(dāng)SCL為高電平時(shí),SDA由低電平向高電平跳變,表示結(jié)束傳輸數(shù)據(jù)
>>響應(yīng)信號(hào)(ACK): 從機(jī)接收到8位數(shù)據(jù)后,在第9個(gè)周期,拉低SDA電平,表示已經(jīng)收到數(shù)據(jù)。這個(gè)信號(hào)稱為應(yīng)答信號(hào)
開始信號(hào)和結(jié)束信號(hào)的波形如下圖:
主機(jī):IIC總線中發(fā)送命令的設(shè)備,對(duì)于ARM處理器來說,主機(jī)就是IIC控制器
從機(jī):接受命令的設(shè)備
?
主機(jī)向從機(jī)發(fā)送數(shù)據(jù):
主機(jī)通過數(shù)據(jù)線SDA向從機(jī)發(fā)送數(shù)據(jù)。當(dāng)總線空閑時(shí),SDA和SCL信號(hào)都處于高電平。主機(jī)向從機(jī)發(fā)送數(shù)據(jù)的過程:
1,當(dāng)主機(jī)檢測(cè)到總線空閑時(shí),主機(jī)發(fā)出開始信號(hào)
2,主機(jī)發(fā)送8位數(shù)據(jù)。這8位數(shù)據(jù)的前7位表示從機(jī)地址,第8位表示數(shù)據(jù)的傳輸方向。這時(shí),第8位為0,表示向從機(jī)發(fā)送數(shù)據(jù)
3,被選中的從機(jī)發(fā)出響應(yīng)信號(hào)ACK
4,從機(jī)傳輸一系列的字節(jié)和響應(yīng)位
5,主機(jī)接受這些數(shù)據(jù),并發(fā)出結(jié)束信號(hào)P,完成本次數(shù)據(jù)傳輸
?
由上圖可知,IIC控制器主要是由4個(gè)寄存器來完成所有的IIC操作的。
IICCON:控制是否發(fā)出ACK信號(hào),是否開啟IIC中斷
IICSTAT:
IICADD:掛載到總線上的從機(jī)地址。該寄存器的[7:1]表示從機(jī)地址。IICADD寄存器在串行輸出使能位IICSTAT[4]為0時(shí),才可以寫入;在任何時(shí)候可以讀出
IICDS:保存將要發(fā)送或者接收到的數(shù)據(jù)。IICCDS在串行輸出使能IICSTAT[4]為1時(shí),才可以寫入;在任何時(shí)間都可以讀出
?
因?yàn)镮IC設(shè)備種類太多,如果每一個(gè)IIC設(shè)備寫一個(gè)驅(qū)動(dòng)程序,那么顯得內(nèi)核非常大。不符合軟件工程代碼復(fù)用,所以對(duì)其層次話:
這里簡(jiǎn)單的將IIC設(shè)備驅(qū)動(dòng)分為設(shè)備層、總線層。理解這兩個(gè)層次的重點(diǎn)是理解4個(gè)數(shù)據(jù)結(jié)構(gòu),這4個(gè)數(shù)據(jù)結(jié)構(gòu)是i2c_driver、i2c_client、i2c_algorithm、i2c_adapter。i2c_driver、i2c_client屬于設(shè)備層;i2c_algorithm、i2c_adapter屬于總線型。如下圖:
設(shè)備層關(guān)系到實(shí)際的IIC設(shè)備,如芯片AT24C08就是一個(gè)IIC設(shè)備。總線層包括CPU中的IIC總線控制器和控制總線通信的方法。
值得注意的是:一個(gè)系統(tǒng)中可能有很多個(gè)總線層,也就是包含多個(gè)總線控制器;也可能有多個(gè)設(shè)備層,包含不同的IIC設(shè)備
由IIC總線規(guī)范可知,IIC總線由兩條物理線路組成,這兩條物理線路是SDA和SCL。只要連接到SDA和SCL總線上的設(shè)備都可以叫做IIC設(shè)備。一個(gè)IIC設(shè)備由i2c_client數(shù)據(jù)結(jié)構(gòu)進(jìn)行描述:
struct? i2c_client
{
unsigned short? flags;??????????????????????? //標(biāo)志位
????? unsigned short? addr; ? //設(shè)備的地址,低7位為芯片地址
char name[I2C_NAME_SIZE]; ?? //設(shè)備的名稱,最大為20個(gè)字節(jié)
struct? i2c_adapter *adapter; //依附的適配器i2c_adapter,適配器指明所屬的總線
struct? i2c_driver *driver; //指向設(shè)備對(duì)應(yīng)的驅(qū)動(dòng)程序
struct device? dev; //設(shè)備結(jié)構(gòu)體
int irq; //設(shè)備申請(qǐng)的中斷號(hào)
struct list_head? list; //連接到總線上的所有設(shè)備
struct list_head detected; //已經(jīng)被發(fā)現(xiàn)的設(shè)備鏈表
struct completion released; //是否已經(jīng)釋放的完成量
};
?
設(shè)備結(jié)構(gòu)體i2c_client中addr的低8位表示設(shè)備地址。設(shè)備地址由讀寫位、器件類型和自定義地址組成,如下圖:
第7位是R/W位,0表示寫,2表示讀,所以I2C設(shè)備通常有兩個(gè)地址,即讀地址和寫地址
類型器件由中間4位組成,這是由半導(dǎo)體公司生產(chǎn)的時(shí)候就已經(jīng)固化了。
自定義類型由低3位組成。由用戶自己設(shè)置,通常的做法如EEPROM這些器件是由外部I芯片的3個(gè)引腳所組合電平?jīng)Q定的(A0,A1,A2)。A0,A1,A2 就是自定義的地址碼。自定義的地址碼只能表示8個(gè)地址,所以同一IIC總線上同一型號(hào)的芯片最多只能掛載8個(gè)。
AT24C08的自定義地址碼如圖:A0,A1,A2接低電平,所以自定義地址碼為0;
如果在兩個(gè)不同IIC總線上掛接了兩塊類型和地址相同的芯片,那么這兩塊芯片的地址相同。這顯然是地址沖突,解決的辦法是為總線適配器指定一個(gè)ID號(hào),那么新的芯片地址就由總線適配器的ID和設(shè)備地址組成
除了地址之外,IIC設(shè)備還有一些重要的注意事項(xiàng):
1,i2c_client數(shù)據(jù)結(jié)構(gòu)是描述IIC設(shè)備的“模板”,驅(qū)動(dòng)程序的設(shè)備結(jié)構(gòu)中應(yīng)包含該結(jié)構(gòu)
2,adapter指向設(shè)備連接的總線適配器,系統(tǒng)可能有多個(gè)總線適配器。內(nèi)核中靜態(tài)指針數(shù)組adapters記錄所有已經(jīng)注冊(cè)的總線適配器設(shè)備
3,driver是指向設(shè)備驅(qū)動(dòng)程序,這個(gè)驅(qū)動(dòng)程序是在系統(tǒng)檢測(cè)到設(shè)備存在時(shí)賦值的
?
IIC設(shè)備驅(qū)動(dòng)???? i2c_driver:
struct? i2c_driver
{
int id;???????????????????????? //驅(qū)動(dòng)標(biāo)識(shí)ID
unsigned int class;?????????????? //驅(qū)動(dòng)的類型
int (*attach_adapter)(struct i2c_adapter *);???????????? //當(dāng)檢測(cè)到適配器時(shí)調(diào)用的函數(shù)
?????? int (*detach_adapter)(struct i2c_adapter*);????????????? //卸載適配器時(shí)調(diào)用的函數(shù)
int (*detach_client)(struct i2c_client *)?? __deprecated;???????????? //卸載設(shè)備時(shí)調(diào)用的函數(shù)
?????? //以下是一種新類型驅(qū)動(dòng)需要的函數(shù),這些函數(shù)支持IIC設(shè)備動(dòng)態(tài)插入和拔出。如果不想支持只實(shí)現(xiàn)上面3個(gè)。要不實(shí)現(xiàn)上面3個(gè)。要么實(shí)現(xiàn)下面5個(gè)。不能同時(shí)定義
?????? int? (*probe)(struct i2c_client *,const struct? i2c_device_id *);????????????? //新類型設(shè)備探測(cè)函數(shù)
?????? int (*remove)(struct i2c_client *);?????????????????? //新類型設(shè)備的移除函數(shù)
?????? void (*shutdown)(struct i2c_client *);????????????? //關(guān)閉IIC設(shè)備
??????? int (*suspend)(struct? i2c_client *,pm_messge_t mesg);?????????? //掛起IIC設(shè)備
??????? int (*resume)(struct? i2c_client *);?????????????????????????????? //恢復(fù)IIC設(shè)備
??????? int (*command)(struct i2c_client *client,unsigned int cmd,void *arg);??????? //使用命令使設(shè)備完成特殊的功能。類似ioctl()函數(shù)
??????? struct devcie_driver? driver;???????????????????????? //設(shè)備驅(qū)動(dòng)結(jié)構(gòu)體
???????? const struct? i2c_device_id *id_table;?????????????????????? //設(shè)備ID表
????????? int (*detect)(struct i2c_client *,int? kind,struct? i2c_board_info *);????????? //自動(dòng)探測(cè)設(shè)備的回調(diào)函數(shù)
???????? const? struct i2c_client_address_data????????? *address_data;???????????????? //設(shè)備所在的地址范圍
???????? struct? list_head??? clients;??????????????????? //指向驅(qū)動(dòng)支持的設(shè)備
};
結(jié)構(gòu)體i2c_driver和i2c_client的關(guān)系較為簡(jiǎn)單,其中i2c_driver表示一個(gè)IIC設(shè)備驅(qū)動(dòng),i2c_client表示一個(gè)IIC設(shè)備。關(guān)系如下圖:
IIC總線適配器就是一個(gè)IIC總線控制器,在物理上連接若干個(gè)IIC設(shè)備。IIC總線適配器本質(zhì)上是一個(gè)物理設(shè)備,其主要功能是完成IIC總線控制器相關(guān)的數(shù)據(jù)通信:
struct i2c_adapter
{
??????? struct module *owner;??????????????????????? //模塊計(jì)數(shù)
??????? unsigned? int id;????????????????????????????????? //alogorithm的類型,定義于i2c_id.h中
??????? unsigned?? int? class;?????????????????????????? //允許探測(cè)的驅(qū)動(dòng)類型
??????? const struct i2c_algorithm *algo;???????? //指向適配器的驅(qū)動(dòng)程序
??????? void *algo_data;????????????????????????????????? //指向適配器的私有數(shù)據(jù),根據(jù)不同的情況使用方法不同
??????? int (*client_register)(struct? i2c_client *);????????? //設(shè)備client注冊(cè)時(shí)調(diào)用
??????? int (*client_unregister(struct? i2c_client *);?????? //設(shè)備client注銷時(shí)調(diào)用
??????? u8 level;?????????????????????????????????????????????????????????
??????? struct? mutex? bus_lock;???????????????????????????? //對(duì)總線進(jìn)行操作時(shí),將獲得總線鎖
??????? struct? mutex? clist_lock ;??????????????????????????? //鏈表操作的互斥鎖
??????? int timeout;????????????????????????????????????????????????? //超時(shí)
int retries;???????????????????????????????????????????????????? //重試次數(shù)
?????? struct device dev;????????????????????????????????????????? //指向 適配器的設(shè)備結(jié)構(gòu)體
?????? int? nr ;??????????????????????????????????????????????????????????
?????? struct? list_head????? clients;??????????????????????????? //連接總線上的設(shè)備的鏈表
??????? char name[48];????????????????????????????????????????????? //適配器名稱
??????? struct completion???? dev_released;?????????????? //用于同步的完成量
};
每一個(gè)適配器對(duì)應(yīng)一個(gè)驅(qū)動(dòng)程序,該驅(qū)動(dòng)程序描述了適配器與設(shè)備之間的通信方法:
struct? i2c_algorithm
{
???????? int? (*master_xfer)(struct? i2c_adapter *adap,? struct? i2c_msg *msg, int num);????????????? //傳輸函數(shù)指針,指向?qū)崿F(xiàn)IIC總線通信協(xié)議的函數(shù),用來確定適配器支持那些傳輸類型
???????? int? (*smbus_xfer)(struct? i2c_adapter *adap, u16? addr, unsigned? short flags, char? read_write, u8 command, int size, union? i2c_smbus_data? *data);??? //smbus方式傳輸函數(shù)指針,指向?qū)崿F(xiàn)SMBus總線通信協(xié)議的函數(shù)。SMBus和IIC之間可以通過軟件方式兼容,所以這里提供了一個(gè)函數(shù),但是一般都賦值為NULL
???????? u32? (*functionality)(struct? i2c_adapter *);?????????????????? //返回適配器支持的功能
};
?
IIC設(shè)備驅(qū)動(dòng)程序大致可以分為設(shè)備層和總線層。設(shè)備層包括一個(gè)重要的數(shù)據(jù)結(jié)構(gòu),i2c_client。總線層包括兩個(gè)重要的數(shù)據(jù)結(jié)構(gòu),分別是i2c_adapter和i2c_algorithm。一個(gè)i2c_algorithm結(jié)構(gòu)表示適配器對(duì)應(yīng)的傳輸數(shù)據(jù)方法。3個(gè)數(shù)據(jù)結(jié)構(gòu)關(guān)系:
?
IIC設(shè)備層次結(jié)構(gòu)較為簡(jiǎn)單,但是寫IIC設(shè)備驅(qū)動(dòng)程序卻相當(dāng)復(fù)雜。
IIC設(shè)備驅(qū)動(dòng)程序的步驟:
IIC子系統(tǒng):
IIC子系統(tǒng)是作為模塊加載到系統(tǒng)中的。
初始化函數(shù):
static int __init i2c_init(void)
{
?? ?int retval;??????????? //返回值,成功0,錯(cuò)誤返回負(fù)值
?? ?retval = bus_register(&i2c_bus_type);?????? //注冊(cè)一條IIC的BUS總線
?? ?if (retval)
?? ??? ?return retval;
?? ?retval = class_register(&i2c_adapter_class);?????? //注冊(cè)適配器類,用于實(shí)現(xiàn)sys文件系統(tǒng)的部分功能
?? ?if (retval)
?? ??? ?goto bus_err;
?? ?retval = i2c_add_driver(&dummy_driver);?????????????? //將一個(gè)空驅(qū)動(dòng)程序注冊(cè)到IIC總線中
?? ?if (retval)
?? ??? ?goto class_err;
?? ?return 0;
class_err:
?? ?class_unregister(&i2c_adapter_class);??????????????????????????????? //類注銷
bus_err:
?? ?bus_unregister(&i2c_bus_type);????????????????????????????????????????? //總線注銷
?? ?return retval;
}
?
struct bus_type i2c_bus_type = {
?? ?.name?? ??? ?= "i2c",
?? ?.dev_attrs?? ?= i2c_dev_attrs,
?? ?.match?? ??? ?= i2c_device_match,
?? ?.uevent?? ??? ?= i2c_device_uevent,
?? ?.probe?? ??? ?= i2c_device_probe,
?? ?.remove?? ??? ?= i2c_device_remove,
?? ?.shutdown?? ?= i2c_device_shutdown,
?? ?.suspend?? ?= i2c_device_suspend,
?? ?.resume?? ??? ?= i2c_device_resume,
};
static struct class i2c_adapter_class = {
?? ?.owner?? ??? ??? ?= THIS_MODULE,
?? ?.name?? ??? ??? ?= "i2c-adapter",
?? ?.dev_attrs?? ??? ?= i2c_adapter_attrs,
};
static struct i2c_driver dummy_driver = {
?? ?.driver.name?? ?= "dummy",
?? ?.probe?? ??? ?= dummy_probe,
?? ?.remove?? ??? ?= dummy_remove,
?? ?.id_table?? ?= dummy_id,
};
?
IIC子系統(tǒng)退出函數(shù):
static void __exit i2c_exit(void)
{
?? ?i2c_del_driver(&dummy_driver);??????? //注銷IIC設(shè)備驅(qū)動(dòng)程序,主要功能是去掉總線中的該設(shè)備驅(qū)動(dòng)程序
?? ?class_unregister(&i2c_adapter_class);????????????? //注銷適配器類
?? ?bus_unregister(&i2c_bus_type);?????????????????????? //注銷I2C總線
}
?
適配器驅(qū)動(dòng)程序是IIC設(shè)備驅(qū)動(dòng)程序需要實(shí)現(xiàn)的主要驅(qū)動(dòng)程序,這個(gè)驅(qū)動(dòng)程序需要根據(jù)具體的適配器硬件來編寫。
I2c_adapter結(jié)構(gòu)體為描述各種IIC適配器提供了“模板",它定義了注冊(cè)總線上所有設(shè)備的clients鏈表、指向具體IIC適配器的總線通信方法I2c_algorithm的algo指針、實(shí)現(xiàn)i2c總線的操作原子性的lock信號(hào)量。但i2c_adapter結(jié)構(gòu)體只是所有適配器的共有屬性,并不能代表所有類型的適配器
?
s3c2440對(duì)應(yīng)的適配器為:
struct s3c24xx_i2c {
?? ?spinlock_t?? ??? ?lock;?????????? //lock自旋鎖
?? ?wait_queue_head_t?? ?wait;?????? //等待隊(duì)列頭。由于IIC設(shè)備是低速設(shè)備,所以可以采取“阻塞-中斷”的驅(qū)動(dòng)模型,即讀寫i2c設(shè)備的用戶程序在IIC設(shè)備操作期間進(jìn)入阻塞狀態(tài),待IIC操作完成后,總線適配器將引發(fā)中斷,再將相應(yīng)的中斷處理函數(shù)中喚醒受阻的用戶進(jìn)程。該隊(duì)列用來放阻塞的進(jìn)程
?? ?unsigned int?? ??? ?suspended:1;???????? //設(shè)備是否掛起
?? ?struct i2c_msg?? ??? ?*msg;?????? //從適配器到設(shè)備一次傳輸?shù)膯挝?#xff0c;用這個(gè)結(jié)構(gòu)體將數(shù)據(jù)包裝起來便于操作 ,
?? ?unsigned int?? ??? ?msg_num;?????? //表示消息的個(gè)數(shù)
?? ?unsigned int?? ??? ?msg_idx;??????????? //表示第幾個(gè)消息。當(dāng)完成一個(gè)消息后,該值增加
?? ?unsigned int?? ??? ?msg_ptr;??????????? //總是指向當(dāng)前交互中要傳送、接受的下一個(gè)字節(jié),在i2c_msg.buf中的偏移量位置
?? ?unsigned int?? ??? ?tx_setup;?????????? //表示寫IIC設(shè)備寄存器的一個(gè)時(shí)間,這里被設(shè)置為50ms
?? ?unsigned int?? ??? ?irq;???????????????????????? //適配器申請(qǐng)的中斷號(hào)
?? ?enum s3c24xx_i2c_state?? ?state;????? //表示IIC設(shè)備目前的狀態(tài)
?? ?unsigned long?? ??? ?clkrate;???????????? //時(shí)鐘速率
?? ?void __iomem?? ??? ?*regs;??????????? //IIC設(shè)備寄存器地址
?? ?struct clk?? ??? ?*clk;?????????????????????? //對(duì)應(yīng)的時(shí)鐘
?? ?struct device?? ??? ?*dev;???????????????? //適配器對(duì)應(yīng)的設(shè)備結(jié)構(gòu)體
?? ?struct resource?? ??? ?*ioarea;??????????? //適配器的資源
?? ?struct i2c_adapter?? ?adap;????????????????? //適配器主體結(jié)構(gòu)體
#ifdef CONFIG_CPU_FREQ
?? ?struct notifier_block?? ?freq_transition;
#endif
};
enum s3c24xx_i2c_state {
?? ?STATE_IDLE,
?? ?STATE_START,
?? ?STATE_READ,
?? ?STATE_WRITE,
?? ?STATE_STOP
};
?
struct? i2c_msg
{
???????? __u16?? addr;??????????????????????????????? //IIC設(shè)備地址。 這個(gè)字段說明一個(gè)適配器在獲得總線控制權(quán)后,可以與多個(gè)IIC設(shè)備進(jìn)行交互。
???????? __u16?? flags;??????????????????????????????? //消息類型標(biāo)志 。??????
#define? I2C_M_TEN???????? 0x0010??????? //這是有10位地址芯片
#define? I2C_M_RD??????????? 0x0001?????? //表示從 從機(jī)到主機(jī)讀數(shù)據(jù)
#define? I2C_M_NOSTART?????? 0x4000????????????????? // FUNC_PROTOCOL_MANLING協(xié)議的相關(guān)標(biāo)志
#define? I2C_M_REV_DIR_ADDR?? 0x2000????????? //FUNC_PROTOCOL_MANLING協(xié)議的相關(guān)標(biāo)志
#define? I2C_M_IGNORE_NAK???????? 0x1000???????? //FUNC_PROTOCOL_MANLING協(xié)議的相關(guān)標(biāo)志
#define? I2C_M_NO_RD_ACK?????????? 0x0800???????? //FUNC_PROTOCOL_MANLING協(xié)議的相關(guān)標(biāo)志
#define? I2C_M_RECV_LEN??????????? 0x0400????????? //第一次接收的字節(jié)長(zhǎng)度
??????? __u16??? len;????????????????????????????????????????????????? //消息字節(jié)長(zhǎng)度
??????? __u8?????? * buf;?????????????????????????????????????????????? //指向消息數(shù)據(jù)的緩沖區(qū)
};
?
當(dāng)拿到一塊新的電路板,并研究了響應(yīng)的IIC適配器之后,就應(yīng)該使用內(nèi)核提供的框架函數(shù)向IIC子系統(tǒng)添加一個(gè)新的適配器
過程:
1,分配一個(gè)IIC適配器,并初始化相應(yīng)的變量
2,使用i2c_add_adapter()函數(shù)向IIC子系統(tǒng)添加適配器結(jié)構(gòu)體i2c_adapter。這個(gè)結(jié)構(gòu)體已經(jīng)在第一步初始化了:
int i2c_add_adapter(struct i2c_adapter *adapter)
{
?? ?int?? ?id, res = 0;
retry:
?? ?if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)?????????? //存放分配ID號(hào)的內(nèi)存
?? ??? ?return -ENOMEM;?????????????????????????? //內(nèi)存分配失敗
?? ?mutex_lock(&core_lock);?????????? //鎖定內(nèi)核鎖
?? ?/* "above" here means "above or equal to", sigh */
?? ?res = idr_get_new_above(&i2c_adapter_idr, adapter,
?? ??? ??? ??? ?__i2c_first_dynamic_bus_num, &id);??????????? //分配ID號(hào),并將ID號(hào)和指針關(guān)聯(lián)
?? ?mutex_unlock(&core_lock);?????????? //釋放內(nèi)核鎖
?? ?if (res < 0) {
?? ??? ?if (res == -EAGAIN)
?? ??? ??? ?goto retry;???????????????????????????????????????????????????????????????????? //分配失敗,重試
?? ??? ?return res;
?? ?}
?? ?adapter->nr = id;
?? ?return i2c_register_adapter(adapter);?????????????????????? // 注冊(cè)適配器設(shè)備
}
?
關(guān)于IDR機(jī)制,請(qǐng)參考:http://www.cnblogs.com/lfsblack/archive/2012/09/15/2686557.html
static DEFINE_IDR(i2c_adapter_idr);
通過ID號(hào)獲得適配器指針:
struct i2c_adapter* i2c_get_adapter(int id)
{
?? ?struct i2c_adapter *adapter;????????????????? //適配器指針
?? ?mutex_lock(&core_lock);?????????? //鎖定內(nèi)核鎖
?? ?adapter = (struct i2c_adapter *)idr_find(&i2c_adapter_idr, id);????????? //通過ID號(hào),查詢適配器指針
?? ?if (adapter && !try_module_get(adapter->owner))????????????? //適配器引用計(jì)數(shù)+1
?? ??? ?adapter = NULL;
?? ?mutex_unlock(&core_lock);??????????????????????????? //釋放內(nèi)核鎖
?? ?return adapter;
}
適配器卸載函數(shù):
主要任務(wù):注銷適配器的數(shù)據(jù)結(jié)構(gòu),刪除總線上的所有設(shè)備的I2c_client數(shù)據(jù)結(jié)構(gòu)和對(duì)應(yīng)的i2c_driver驅(qū)動(dòng)程序,并減少其代表總線上所有設(shè)備的相應(yīng)驅(qū)動(dòng)程序數(shù)據(jù)結(jié)構(gòu)的引用計(jì)數(shù)(如果到達(dá)0,則卸載設(shè)備驅(qū)動(dòng)程序):
int i2c_del_adapter(struct i2c_adapter *adap)
{
?? ?struct i2c_client *client, *_n;
?? ?int res = 0;
?? ?mutex_lock(&core_lock);
?? ?/* First make sure that this adapter was ever added */
?? ?if (idr_find(&i2c_adapter_idr, adap->nr) != adap) {????????????????????????? //查找要卸載的適配器
?? ??? ?pr_debug("i2c-core: attempting to delete unregistered "
?? ??? ??? ? "adapter [%s]\n", adap->name);
?? ??? ?res = -EINVAL;
?? ??? ?goto out_unlock;
?? ?}
?? ?/* Tell drivers about this removal */
?? ?res = bus_for_each_drv(&i2c_bus_type, NULL, adap,
?? ??? ??? ??????? i2c_do_del_adapter);
?? ?if (res)
?? ??? ?goto out_unlock;
?? ?/* detach any active clients. This must be done first, because
?? ? * it can fail; in which case we give up. */
?? ?list_for_each_entry_safe_reverse(client, _n, &adap->clients, list) {
?? ??? ?struct i2c_driver?? ?*driver;
?? ??? ?driver = client->driver;
?? ??? ?/* new style, follow standard driver model */
?? ??? ?if (!driver || is_newstyle_driver(driver)) {
?? ??? ??? ?i2c_unregister_device(client);
?? ??? ??? ?continue;
?? ??? ?}
?? ??? ?/* legacy drivers create and remove clients themselves */
?? ??? ?if ((res = driver->detach_client(client))) {
?? ??? ??? ?dev_err(&adap->dev, "detach_client failed for client "
?? ??? ??? ??? ?"[%s] at address 0x%02x\n", client->name,
?? ??? ??? ??? ?client->addr);
?? ??? ??? ?goto out_unlock;
?? ??? ?}
?? ?}
?? ?/* clean up the sysfs representation */
?? ?init_completion(&adap->dev_released);
?? ?device_unregister(&adap->dev);???????????????????????????? 設(shè)備注銷
?? ?/* wait for sysfs to drop all references */
?? ?wait_for_completion(&adap->dev_released);
?? ?/* free bus id */
?? ?idr_remove(&i2c_adapter_idr, adap->nr);????????????????????? //刪除IDR,ID號(hào)
?? ?dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
?? ?/* Clear the device structure in case this adapter is ever going to be
?? ??? added again */
?? ?memset(&adap->dev, 0, sizeof(adap->dev));
?out_unlock:
?? ?mutex_unlock(&core_lock);
?? ?return res;
}
IIC總線通信方法s3c24xx_i2c_algorithm結(jié)構(gòu)體:
static const struct i2c_algorithm s3c24xx_i2c_algorithm = {
?? ?.master_xfer?? ??? ?= s3c24xx_i2c_xfer,
?? ?.functionality?? ??? ?= s3c24xx_i2c_func,
};
這里只實(shí)現(xiàn)了IIC總線通信協(xié)議
通信方法因不同的適配器有所不同,要跟據(jù)具體的硬件來實(shí)現(xiàn)
協(xié)議支持函數(shù)s3c24xx_i2c_func()
該函數(shù)返回總線支持的協(xié)議,如I2C_FUNC_I2C、I2C_FUNC_SMBUS_EMUL、I2C_FUNC_PROTOCOL_MANGLING協(xié)議:
static u32 s3c24xx_i2c_func(struct i2c_adapter *adap)
{
?? ?return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_PROTOCOL_MANGLING;
}
傳輸函數(shù)s3c24xx_i2c_xfer():
static int s3c24xx_i2c_xfer(struct i2c_adapter *adap,
?? ??? ??? ?struct i2c_msg *msgs, int num)
{
?? ?struct s3c24xx_i2c *i2c = (struct s3c24xx_i2c *)adap->algo_data;??? //從適配器的私有數(shù)據(jù)中獲得適配器s3c24xx_i2c結(jié)構(gòu)體
?? ?int retry;???????????????????????????????????????????????????????????????????????????????????????????? //傳輸錯(cuò)誤重發(fā)次數(shù)
?? ?int ret;??????????????????????????????????????????????????????????????????????????????????????????????? //返回值
?? ?for (retry = 0; retry < adap->retries; retry++) {
?? ??? ?ret = s3c24xx_i2c_doxfer(i2c, msgs, num);?????????????????????????????????????? //傳輸?shù)絀IC設(shè)備的具體函數(shù)
?? ??? ?if (ret != -EAGAIN)
?? ??? ??? ?return ret;
?? ??? ?dev_dbg(i2c->dev, "Retrying transmission (%d)\n", retry);??????????????? //重試信息
?? ??? ?udelay(100);??????????????????????????????????????????????????????????????????????????????????? //延時(shí)100us
?? ?}
?? ?return -EREMOTEIO;?????????????????????????????????????????????????????????????????????? // I/O錯(cuò)誤
}
真正的傳輸函數(shù):
static int s3c24xx_i2c_doxfer(struct s3c24xx_i2c *i2c,
?? ??? ??? ?????? struct i2c_msg *msgs, int num)
{
?? ?unsigned long timeout;??????????????????????????????????????????????????????????????????????????????? //定義一個(gè)傳輸超時(shí)時(shí)間
?? ?int ret;????????????????????????????????????????????????????????????????????????????????????????????????????????? //返回值,傳輸消息的個(gè)數(shù)
?? ?if (i2c->suspended)????????????????????????????????????????????????????????????????????????????????????? //如果適配器處于掛起省電狀態(tài),則返回
?? ??? ?return -EIO;
?? ?ret = s3c24xx_i2c_set_master(i2c);????????????????????????????????????????????????????????????? //將適配器設(shè)為主機(jī)發(fā)送狀態(tài),判斷總線忙閑狀態(tài)
?? ?if (ret != 0) {????????????????????????????????????????????????????????????????????????????????????????????????? //如果總線繁忙,則傳輸失敗
?? ??? ?dev_err(i2c->dev, "cannot get bus (error %d)\n", ret);
?? ??? ?ret = -EAGAIN;
?? ??? ?goto out;
?? ?}
?? ?spin_lock_irq(&i2c->lock);???????????????????????????????????????????? //操作適配器的自旋鎖鎖定,每次只允許一個(gè)進(jìn)程傳輸數(shù)據(jù),其他進(jìn)程無法獲得總線
?? ?i2c->msg???? = msgs;???????????????????????????????????????????????????? //傳輸?shù)南⒅羔?br />?? ?i2c->msg_num = num;???????????????????????????????????????????????? //傳輸?shù)南€(gè)數(shù)
?? ?i2c->msg_ptr = 0;???????????????????????????????????????????????????????? //當(dāng)前要傳輸?shù)淖止?jié)在消息中的偏移
?? ?i2c->msg_idx = 0;???????????????????????????????????????????????????????? //消息數(shù)組的索引
?? ?i2c->state?? = STATE_START;
?? ?s3c24xx_i2c_enable_irq(i2c);????????????????????????????????????? //啟動(dòng)適配器中斷信號(hào),允許適配器發(fā)出中斷
?? ?s3c24xx_i2c_message_start(i2c, msgs);???????????????????? //當(dāng)調(diào)用該函數(shù)啟動(dòng) 數(shù)據(jù)發(fā)送后,當(dāng)前進(jìn)程進(jìn)入睡眠狀態(tài),等待中斷到來,所以通過wait_event_timeout()函數(shù)將自己掛起到s3c24xx_i2c.wait等待隊(duì)列上,直到等待的條件"i2c->msg_num == 0"為真,或者5s超時(shí)后才能喚醒。注意一次i2c操作可能要涉及多個(gè)字節(jié),只有第一個(gè)字節(jié)發(fā)送是在當(dāng)前進(jìn)程的文件系統(tǒng)操作執(zhí)行流中進(jìn)行的,該字節(jié)操作的完成及后繼字節(jié)的寫入都由中斷處理程序來完成。在此期間當(dāng)前進(jìn)程掛起在s3c24xx_i2c.wait等待隊(duì)列上
?? ?spin_unlock_irq(&i2c->lock);
?? ?timeout = wait_event_timeout(i2c->wait, i2c->msg_num == 0, HZ * 5);
?? ?ret = i2c->msg_idx;
?? ?/* having these next two as dev_err() makes life very
?? ? * noisy when doing an i2cdetect */
?? ?if (timeout == 0)????????????????????????????????????????????????????????????????? //在規(guī)定的時(shí)間內(nèi),沒有成功的寫入數(shù)據(jù)
?? ??? ?dev_dbg(i2c->dev, "timeout\n");
?? ?else if (ret != num)?????????????????????????????????????????????????????????????? //未寫完規(guī)定的消息個(gè)數(shù),則失敗
?? ??? ?dev_dbg(i2c->dev, "incomplete xfer (%d)\n", ret);
?? ?/* ensure the stop has been through the bus */
?? ?msleep(1);????????????????????????????????????????????????????????????????????????? //睡眠1ms,使總線停止
?out:
?? ?return ret;
}
?
enum s3c24xx_i2c_state {
?? ?STATE_IDLE,??????????????????? //總線空閑狀態(tài)
?? ?STATE_START,??????????????? //總線開始狀態(tài)
?? ?STATE_READ,????????????????? //總線寫數(shù)據(jù)狀態(tài)
?? ?STATE_WRITE,??????????????? //總線讀書據(jù)狀態(tài)
?? ?STATE_STOP?????????????????? //總線停止?fàn)顟B(tài)
};
?
判斷總線閑忙狀態(tài)s3c24xx_i2c_set_master():
在適配器發(fā)送數(shù)據(jù)以前,需要判斷總線的忙閑狀態(tài)。讀取IICSTAT寄存器的[5]位,可以判斷總線的忙閑狀態(tài)。當(dāng)為0時(shí),總線空閑;當(dāng)為1時(shí)總線繁忙:
static int s3c24xx_i2c_set_master(struct s3c24xx_i2c *i2c)
{
?? ?unsigned long iicstat;?????????????????????????????????? //用于存儲(chǔ)IICSTAT的狀態(tài)
?? ?int timeout = 400;???????????????????????????????????????? //嘗試400次,獲得總線
?? ?while (timeout-- > 0) {
?? ??? ?iicstat = readl(i2c->regs + S3C2410_IICSTAT);??????????????????? //讀取寄存器IICSTAT的值
?? ??? ?if (!(iicstat & S3C2410_IICSTAT_BUSBUSY))???????????????????????????????? //檢查第5位是否為0
?? ??? ??? ?return 0;
?? ??? ?msleep(1);???????????????????????????????????????????????????????????????????????????????? //等待1ms
?? ?}
?? ?return -ETIMEDOUT;
}
?
適配器使能函數(shù)s3c24xx_i2c_enable_irq()
IIC設(shè)備是一種慢速設(shè)備,所以在讀寫數(shù)據(jù)的過程中,內(nèi)核進(jìn)程需要睡眠等待。當(dāng)數(shù)據(jù)發(fā)送完后,會(huì)從總線發(fā)送一個(gè)中斷信號(hào),喚醒睡眠中的進(jìn)程,所以適配器應(yīng)該使能中斷。中斷使能由IICCON寄存器的[5]位設(shè)置,該位為0表示Tx/Rx中斷禁止;該位為1表示Tx/Rx中斷使能。s3c24xx_i2c_enable_irq()函數(shù)用來使中斷使能。所以向IICCON寄存器的位[5]寫1:
static inline void s3c24xx_i2c_enable_irq(struct s3c24xx_i2c *i2c)
{
?? ?unsigned long tmp;?????????????????????? //寄存器緩存變量
?? ?tmp = readl(i2c->regs + S3C2410_IICCON);???????????? //讀IICCON寄存器
?? ?writel(tmp | S3C2410_IICCON_IRQEN, i2c->regs + S3C2410_IICCON);??????? //將IICCON的第5位置1
}
?
啟動(dòng)適配器消息傳輸函數(shù)s3c24xx_i2c_message_start():
s3c24xx_i2c_message_start()函數(shù)寫s3c2440適配器對(duì)應(yīng)的寄存器,向IIC設(shè)備傳遞開始位和IIC設(shè)備地址。主要功能:
1,s3c2440的適配器對(duì)應(yīng)的IICON和IICSTAT寄存器
2,寫從設(shè)備地址,并發(fā)出開始信號(hào)S
static void s3c24xx_i2c_message_start(struct s3c24xx_i2c *i2c,
?? ??? ??? ??? ?????? struct i2c_msg *msg)
{
?? ?unsigned int addr = (msg->addr & 0x7f) << 1;???????????????? //取從設(shè)備的低7位地址,并向前移動(dòng)一位。設(shè)置設(shè)備地址,前7位表示設(shè)備地址,最后一位表示讀寫,0寫1讀
?? ?unsigned long stat;?????????????????????????????????????????????????????????? //緩存IICSTAT寄存器
?? ?unsigned long iiccon;??????????????????????????????????????????????????????? //緩存IICCO寄存器
?? ?stat = 0;???????????????????????????????????????????????????????????????????????????? //狀態(tài)初始化為0
?? ?stat |=? S3C2410_IICSTAT_TXRXEN;?????????????????????????????? //使能接收和發(fā)送功能,是適配器可以收發(fā)數(shù)據(jù)
?? ?if (msg->flags & I2C_M_RD) {????????????????????????????????????????? //如果消息類型是從IIC設(shè)備到適配器讀數(shù)據(jù)
?? ??? ?stat |= S3C2410_IICSTAT_MASTER_RX;?????????????????? //將適配器設(shè)置為主機(jī)接收器
?? ??? ?addr |= 1;??????????????????????????????????????????????????????????????????? //將地址的最低位置1表示讀操作
?? ?} else??????????????????????????????????????????????????????????????? //否則
?? ??? ?stat |= S3C2410_IICSTAT_MASTER_TX;?????????????????? //將適配器設(shè)置為主機(jī)發(fā)送器
?? ?if (msg->flags & I2C_M_REV_DIR_ADDR)??????????????????? 一種新的擴(kuò)展協(xié)議,沒有設(shè)置該標(biāo)志
?? ??? ?addr ^= 1;
?? ?/* todo - check for wether ack wanted or not */
?? ?s3c24xx_i2c_enable_ack(i2c);?????????????????????????????????? //使能ACK響應(yīng)信號(hào)
?? ?iiccon = readl(i2c->regs + S3C2410_IICCON);????????????? //讀出IICCON寄存器的值
?? ?writel(stat, i2c->regs + S3C2410_IICSTAT);????????????????????????????? //設(shè)置IICSTAT的值,使其為主機(jī)發(fā)送器,接收使能
?? ?dev_dbg(i2c->dev, "START: %08lx to IICSTAT, %02x to DS\n", stat, addr);????????? //打印調(diào)試信息
?? ?writeb(addr, i2c->regs + S3C2410_IICDS);????????????????????????????????????????????????? //寫地址寄存器的值。將IIC設(shè)備地址寫入IICDS寄存器中,寄存器值[7:1]表示設(shè)備地址。IICADD寄存器必須在輸出使能為IICSTAT[4]為0時(shí),才可以寫入,所以上面的writel函數(shù)設(shè)置使能為輸出使能為IISTAT[4]。
?? ?/* delay here to ensure the data byte has gotten onto the bus
?? ? * before the transaction is started */
?? ?ndelay(i2c->tx_setup);???????????????????????????????????????????????????? //延時(shí),以使數(shù)據(jù)寫入寄存器中
?? ?dev_dbg(i2c->dev, "iiccon, %08lx\n", iiccon);
?? ?writel(iiccon, i2c->regs + S3C2410_IICCON);????????????????????????????????? //寫IICCON寄存器的值
?? ?stat |= S3C2410_IICSTAT_START;??????????????????????????????? //設(shè)置為啟動(dòng)狀態(tài)
?? ?writel(stat, i2c->regs + S3C2410_IICSTAT);????????????????????? //發(fā)出S開始信號(hào),當(dāng)S信號(hào)發(fā)出后,IICDS寄存器的數(shù)據(jù)將自動(dòng)發(fā)出到總線上
}
static inline void s3c24xx_i2c_enable_ack(struct s3c24xx_i2c *i2c)??? //使能ACK響應(yīng)信號(hào)
{
?? ?unsigned long tmp;???????????????????????????? //暫存IICCON寄存器
?? ?tmp = readl(i2c->regs + S3C2410_IICCON);???????????? //取出IICCON寄存器的值
?? ?writel(tmp | S3C2410_IICCON_ACKEN, i2c->regs + S3C2410_IICCON);? 寫IICCON,使能ACK
}
適配器中斷處理函數(shù):s3c24xx_i2c_irq()
順著通信函數(shù)s3c24xx_i2c_xfer()的執(zhí)行流程分析,函數(shù)最終會(huì)返回,但并沒有傳輸數(shù)據(jù)。傳輸數(shù)據(jù)的過程被交到了中斷處理函數(shù)中。這是因?yàn)镮IC設(shè)備的讀寫是非常慢的,需要使用中斷的方法提高處理器的效率,這在操作系統(tǒng)的過程中非常常見。
通過s3c24xx_i2c_algorithm通信方法中函數(shù)的調(diào)用關(guān)系,數(shù)據(jù)通信的過程如下:
1,傳輸數(shù)據(jù)時(shí),調(diào)用s3c24xx_i2c_algorithm結(jié)構(gòu)體中的數(shù)據(jù)傳輸函數(shù)s3c24xx_i2c_xfer()
2,s3c24xx_i2c_xfer()中會(huì)調(diào)用s3c24xx_i2c_doxfer()進(jìn)行數(shù)據(jù)的傳輸
3,s3c24xx_i2c_doxfer()中向總線 發(fā)送IIC設(shè)備地址和開始信號(hào)S后,便會(huì)調(diào)用wati_event_timeout()函數(shù)進(jìn)入等待狀態(tài)
4,將數(shù)據(jù)準(zhǔn)備好發(fā)送時(shí),將產(chǎn)生中斷,并調(diào)用實(shí)現(xiàn)注冊(cè)的中斷處理函數(shù)s3c24xx_i2c_irq()
5,s3c24xx_i2c_irq()調(diào)用下一個(gè)字節(jié)傳輸函數(shù)i2s_s3c_irq_nextbyte()來傳輸數(shù)據(jù)
6,當(dāng)數(shù)據(jù)傳輸完成后,會(huì)調(diào)用 s3c24xx_i2c_stop().
7,最后調(diào)用wake_up()喚醒等待隊(duì)列,完成數(shù)據(jù)的傳輸過程
?
當(dāng)s3c2440的IIC適配器處于主機(jī)模式時(shí),IIC操作的第一步總是向IIC總線寫入設(shè)備的地址及開始信號(hào)。這步由s3c24xx_i2c_set_master()和s3c24xx_i2c_message_start()完成。而收發(fā)數(shù)據(jù)的后繼操作在IIC中斷處理程序s3c24xx_i2c_irq()中完成的
中斷處理函數(shù):
IIC中斷的產(chǎn)生有3種情況:
1,當(dāng)總線仲裁失敗時(shí)產(chǎn)生中斷
2,當(dāng)發(fā)送/接受完一個(gè)字節(jié)的數(shù)據(jù)(包括響應(yīng)位)時(shí)產(chǎn)生中斷
3,當(dāng)發(fā)出地址信息或接收到一個(gè)IIC設(shè)備地址并且吻合時(shí)產(chǎn)生中斷
在這3種情況下都觸發(fā)中斷,由于當(dāng)發(fā)送/接收完一個(gè)字節(jié)后會(huì)產(chǎn)生中斷,所以可以在中斷處理函數(shù)中處理數(shù)據(jù)的傳輸:
static irqreturn_t s3c24xx_i2c_irq(int irqno, void *dev_id)
{
?? ?struct s3c24xx_i2c *i2c = dev_id;???????????????????????????????
?? ?unsigned long status;????????????????????????????????? //緩存IICSTAT
?? ?unsigned long tmp;???????????????????????????????????? //緩存寄存器
?? ?status = readl(i2c->regs + S3C2410_IICSTAT);???????????????????? //讀取IICSTAT的值
?? ?if (status & S3C2410_IICSTAT_ARBITR) {?????????????? //因仲裁失敗引發(fā)的中斷,IICSTAT[3]為0,表示仲裁成功,為1,表示失敗
?? ??? ?/* deal with arbitration loss */
?? ??? ?dev_err(i2c->dev, "deal with arbitration loss\n");
?? ?}
?? ?if (i2c->state == STATE_IDLE) {?????? 當(dāng)總線為空閑狀態(tài)時(shí),由于非讀寫引起的中斷,將會(huì)執(zhí)行下面的分支清除中斷信號(hào),繼續(xù)傳輸數(shù)據(jù)。這種中斷一般由總線仲裁引起,不會(huì)涉及數(shù)據(jù)的發(fā)送,所以清除中斷標(biāo)志后,直接跳出。IICCON[4]為1表示發(fā)生中斷,總線上的數(shù)據(jù)傳輸停止。要使繼續(xù)傳輸數(shù)據(jù),需要寫入0清除
?? ??? ?dev_dbg(i2c->dev, "IRQ: error i2c->state == IDLE\n");
?? ??? ?tmp = readl(i2c->regs + S3C2410_IICCON);?????????????????????? //讀IICCON寄存器
?? ??? ?tmp &= ~S3C2410_IICCON_IRQPEND;???????????????????????????? //將 IICCON的位[4]清零,表示清除中斷
?? ??? ?writel(tmp, i2c->regs +? S3C2410_IICCON);???????????????????????? //寫IICCON寄存器
?? ??? ?goto out;?????????????????????????????????????????????????????????????????????????????? //跳到退出直接返回
?? ?}
?? ?/* pretty much this leaves us with the fact that we've
?? ? * transmitted or received whatever byte we last sent */
?? ?i2s_s3c_irq_nextbyte(i2c, status);????????????????????? //傳輸或者接收下一個(gè)字節(jié)
?out:
?? ?return IRQ_HANDLED;
}
字節(jié)傳輸函數(shù):i2s_s3c_irq_nextbyte():
static int i2s_s3c_irq_nextbyte(struct s3c24xx_i2c *i2c, unsigned long iicstat)
{
?? ?unsigned long tmp;????????????????????????????????????????? //寄存器緩存
?? ?unsigned char byte;???????????????????????????????????????? //寄存器緩存
?? ?int ret = 0;
?? ?switch (i2c->state) {??????????????????????????????????
?? ?case STATE_IDLE:???????????????????????????????????????????? //總線上沒有數(shù)據(jù)傳輸,則立即返回
?? ??? ?dev_err(i2c->dev, "%s: called in STATE_IDLE\n", __func__);
?? ??? ?goto out;
?? ??? ?break;
?? ?case STATE_STOP:???????????????????????????????????????????? //發(fā)出停止信號(hào)P ,IIC設(shè)備處于停止?fàn)顟B(tài),發(fā)送一個(gè)停止信號(hào)給IIC適配器。這是即使有數(shù)據(jù)產(chǎn)生,也不會(huì)產(chǎn)生中斷信號(hào)
?? ??? ?dev_err(i2c->dev, "%s: called in STATE_STOP\n", __func__);
?? ??? ?s3c24xx_i2c_disable_irq(i2c);????????????????????????? //接收和發(fā)送數(shù)據(jù)時(shí),將不會(huì)產(chǎn)生中斷
?? ??? ?goto out_ack;
?? ?case STATE_START:??????????????????????????????????????????? //發(fā)出開始信號(hào)S
?? ??? ?/* last thing we did was send a start condition on the
?? ??? ? * bus, or started a new i2c message
?? ??? ? */
?? ??? ?if (iicstat & S3C2410_IICSTAT_LASTBIT &&
?? ??? ???? !(i2c->msg->flags & I2C_M_IGNORE_NAK)) {??? //當(dāng)沒有接收到IIC設(shè)備的應(yīng)答ACK信號(hào),說明對(duì)應(yīng)地址的IIC設(shè)備不存在,停止總線工作
?? ??? ??? ?/* ack was not received... */
?? ??? ??? ?dev_dbg(i2c->dev, "ack was not received\n");
?? ??? ??? ?s3c24xx_i2c_stop(i2c, -ENXIO);?????????????????? //停止總線工作,發(fā)出P信號(hào)
?? ??? ??? ?goto out_ack;
?? ??? ?}
?? ??? ?if (i2c->msg->flags & I2C_M_RD)????????????????????? //一個(gè)讀信息
?? ??? ??? ?i2c->state = STATE_READ;
?? ??? ?else
?? ??? ??? ?i2c->state = STATE_WRITE;???????????????????????? 一個(gè)寫消息
?? ??? ?/* terminate the transfer if there is nothing to do
?? ??? ? * as this is used by the i2c probe to find devices. */
?? ??? ?if (is_lastmsg(i2c) && i2c->msg->len == 0) {????????? //is_lastmsg()判斷是否只有一條消息,如果這條消息為0字節(jié),那么發(fā)送停止信號(hào)P。0長(zhǎng)度信息用于設(shè)備探測(cè)probe()時(shí)檢測(cè)設(shè)備
?? ??? ??? ?s3c24xx_i2c_stop(i2c, 0);
?? ??? ??? ?goto out_ack;
?? ??? ?}
?? ??? ?if (i2c->state == STATE_READ)
?? ??? ??? ?goto prepare_read;???????????????????????????? //直接跳到讀命令去
?? ??? ?/* fall through to the write state, as we will need to
?? ??? ? * send a byte as well */
?? ?case STATE_WRITE:
?? ??? ?/* we are writing data to the device... check for the
?? ??? ? * end of the message, and if so, work out what to do
?? ??? ? */
?? ??? ?if (!(i2c->msg->flags & I2C_M_IGNORE_NAK)) {????? //沒有接收到IIC設(shè)備的ACK信號(hào),表示出錯(cuò),停止總線傳輸
?? ??? ??? ?if (iicstat & S3C2410_IICSTAT_LASTBIT) {??????????? //
?? ??? ??? ??? ?dev_dbg(i2c->dev, "WRITE: No Ack\n");
?? ??? ??? ??? ?s3c24xx_i2c_stop(i2c, -ECONNREFUSED);
?? ??? ??? ??? ?goto out_ack;
?? ??? ??? ?}
?? ??? ?}
?retry_write:
判斷一個(gè)消息是否結(jié)束,如果沒有,則執(zhí)行下面的分支
?? ??? ?if (!is_msgend(i2c)) {
?? ??? ??? ?byte = i2c->msg->buf[i2c->msg_ptr++];???????? //讀出緩沖區(qū)中的數(shù)據(jù),并增加偏移
?? ??? ??? ?writeb(byte, i2c->regs + S3C2410_IICDS);??? //將一個(gè)字節(jié)的數(shù)據(jù)寫到IICDS中
?? ??? ??? ?/* delay after writing the byte to allow the
?? ??? ??? ? * data setup time on the bus, as writing the
?? ??? ??? ? * data to the register causes the first bit
?? ??? ??? ? * to appear on SDA, and SCL will change as
?? ??? ??? ? * soon as the interrupt is acknowledged */
?? ??? ??? ?ndelay(i2c->tx_setup);??????????? //等待數(shù)據(jù)發(fā)送到總線
?? ??? ?} else if (!is_lastmsg(i2c)) {?????????????????? //如果不是最后一個(gè)消息,則移向下一個(gè)消息
?? ??? ??? ?/* we need to go to the next i2c message */
?? ??? ??? ?dev_dbg(i2c->dev, "WRITE: Next Message\n");
?? ??? ??? ?i2c->msg_ptr = 0;
?? ??? ??? ?i2c->msg_idx++;
?? ??? ??? ?i2c->msg++;
?? ??? ??? ?/* check to see if we need to do another message */
?? ??? ??? ?if (i2c->msg->flags & I2C_M_NOSTART) {????? //不處理這種新類型的消息,直接停止
?? ??? ??? ??? ?if (i2c->msg->flags & I2C_M_RD) {
?? ??? ??? ??? ??? ?/* cannot do this, the controller
?? ??? ??? ??? ??? ? * forces us to send a new START
?? ??? ??? ??? ??? ? * when we change direction */
?? ??? ??? ??? ??? ?s3c24xx_i2c_stop(i2c, -EINVAL);
?? ??? ??? ??? ?}
?? ??? ??? ??? ?goto retry_write;
?? ??? ??? ?} else {???????????? //開始傳輸消息,將IICDS的數(shù)據(jù)發(fā)到總線上
?? ??? ??? ??? ?/* send the new start */
?? ??? ??? ??? ?s3c24xx_i2c_message_start(i2c, i2c->msg);??????????
?? ??? ??? ??? ?i2c->state = STATE_START;?????????????? //置開始狀態(tài)
?? ??? ??? ?}
?? ??? ?} else {
?? ??? ??? ?/* send stop */
?? ??? ??? ?s3c24xx_i2c_stop(i2c, 0);????????????? //所有消息傳遞結(jié)束,停止總線
?? ??? ?}
?? ??? ?break;
?? ?case STATE_READ:????????????????????????????????? //讀數(shù)據(jù)
?? ??? ?/* we have a byte of data in the data register, do
?? ??? ? * something with it, and then work out wether we are
?? ??? ? * going to do any more read/write
?? ??? ? */
?? ??? ?byte = readb(i2c->regs + S3C2410_IICDS);?????????????? //從數(shù)據(jù)寄存器讀出數(shù)據(jù)
?? ??? ?i2c->msg->buf[i2c->msg_ptr++] = byte;?????????????????????? //放到緩沖區(qū)
?prepare_read:
?? ??? ?if (is_msglast(i2c)) {???????????????????????????????? //一個(gè)消息的最后一個(gè)字節(jié)
?? ??? ??? ?/* last byte of buffer */
?? ??? ??? ?if (is_lastmsg(i2c))?????????????????????????????? //最后一個(gè)消息
?? ??? ??? ??? ?s3c24xx_i2c_disable_ack(i2c);???????????? //禁止ACK信號(hào)
?? ??? ?} else if (is_msgend(i2c)) {?????????????????????????????? //讀完一個(gè)消息
?? ??? ??? ?/* ok, we've read the entire buffer, see if there
?? ??? ??? ? * is anything else we need to do */
?? ??? ??? ?if (is_lastmsg(i2c)) {?????????????????????????????? //最后一個(gè)消息
?? ??? ??? ??? ?/* last message, send stop and complete */
?? ??? ??? ??? ?dev_dbg(i2c->dev, "READ: Send Stop\n");
?? ??? ??? ??? ?s3c24xx_i2c_stop(i2c, 0);??????? //發(fā)出停止信號(hào),并喚醒對(duì)立
?? ??? ??? ?} else {????????? //傳輸下一個(gè)消息
?? ??? ??? ??? ?/* go to the next transfer */
?? ??? ??? ??? ?dev_dbg(i2c->dev, "READ: Next Transfer\n");
?? ??? ??? ??? ?i2c->msg_ptr = 0;
?? ??? ??? ??? ?i2c->msg_idx++;???????????????????????????? //移到下一個(gè)消息索引
?? ??? ??? ??? ?i2c->msg++;?????????????????????????????????? //移到下一個(gè)消息
?? ??? ??? ?}
?? ??? ?}
?? ??? ?break;
?? ?}
?? ?/* acknowlegde the IRQ and get back on with the work */
?out_ack:?????????????????????? //清除中斷,不然重復(fù)執(zhí)行該中斷函數(shù)
?? ?tmp = readl(i2c->regs + S3C2410_IICCON);
?? ?tmp &= ~S3C2410_IICCON_IRQPEND;
?? ?writel(tmp, i2c->regs + S3C2410_IICCON);
?out:
?? ?return ret;
}
適配器傳輸停止函數(shù):s3c24xx_i2c_stop()
主要完成以下功能:
1,向總線發(fā)出結(jié)束P信號(hào)
2,喚醒等待在隊(duì)列s3c24xx_i2c->wait中的進(jìn)程,一次傳輸完畢
3,禁止中斷 ,適配器中不產(chǎn)生中斷信號(hào)
static inline void s3c24xx_i2c_stop(struct s3c24xx_i2c *i2c, int ret)
{
?? ?unsigned long iicstat = readl(i2c->regs + S3C2410_IICSTAT);???????????????? //讀IICSTAT寄存器
?? ?dev_dbg(i2c->dev, "STOP\n");
?? ?/* stop the transfer */
?? ?iicstat &= ~S3C2410_IICSTAT_START;??????????????????????????????????????? //寫IICSTAT[5]為0,則放出P信號(hào)
?? ?writel(iicstat, i2c->regs + S3C2410_IICSTAT);
?? ?i2c->state = STATE_STOP;???????????????????????????????????????????????????????? //設(shè)置適配器為停止?fàn)顟B(tài)
?? ?s3c24xx_i2c_master_complete(i2c, ret);?????????????????????????????????????? //喚醒傳輸?shù)却?duì)列中的進(jìn)程
?? ?s3c24xx_i2c_disable_irq(i2c);?????????????????????????????????????????????????????? //禁止中斷
}
?
static inline void s3c24xx_i2c_master_complete(struct s3c24xx_i2c *i2c, int ret)
{
?? ?dev_dbg(i2c->dev, "master_complete %d\n", ret);
?? ?i2c->msg_ptr = 0;
?? ?i2c->msg = NULL;
?? ?i2c->msg_idx++;
?? ?i2c->msg_num = 0;??????????? //表示適配器中已經(jīng)沒有待傳輸?shù)南?br />?? ?if (ret)
?? ??? ?i2c->msg_idx = ret;
?? ?wake_up(&i2c->wait);???????????? //喚醒等待隊(duì)列中的進(jìn)程
}
幾個(gè)小函數(shù):
static inline int is_lastmsg(struct s3c24xx_i2c *i2c)???????????????? //用來判斷當(dāng)前處理的消息是否為最后一個(gè)消息
{
?? ?return i2c->msg_idx >= (i2c->msg_num - 1);
}
static inline int is_msgend(struct s3c24xx_i2c *i2c)???????? //判斷當(dāng)前消息是否已經(jīng)傳輸完所有字節(jié)
{
?? ?return i2c->msg_ptr >= i2c->msg->len;
}
static inline int is_msglast(struct s3c24xx_i2c *i2c)???????????? //判斷當(dāng)前是否正在處理當(dāng)前消息的最后一個(gè)字節(jié)
{
?? ?return i2c->msg_ptr == i2c->msg->len-1;
}
static inline void s3c24xx_i2c_disable_ack(struct s3c24xx_i2c *i2c)???????????? //禁止適配器發(fā)出應(yīng)答信號(hào)
{
?? ?unsigned long tmp;
?? ?tmp = readl(i2c->regs + S3C2410_IICCON);????????????????? //?? IICCON[7]為0,表示不發(fā)出ACK信號(hào)
?? ?writel(tmp & ~S3C2410_IICCON_ACKEN, i2c->regs + S3C2410_IICCON);
}
?
IIC設(shè)備層驅(qū)動(dòng)程序:
IIC設(shè)備驅(qū)動(dòng)被作為一個(gè)單獨(dú)的模塊加入進(jìn)內(nèi)核,在模塊的加載和卸載函數(shù)中需要注冊(cè)和注銷一個(gè)平臺(tái)驅(qū)動(dòng)結(jié)構(gòu)體platform_driver。
static int __init i2c_adap_s3c_init(void)
{
?? ?int ret;???????????????????????????????????????????????????????????????? //返回值
?? ?ret = platform_driver_register(&s3c2410_i2c_driver);??? //注冊(cè)驅(qū)動(dòng)程序 。該函數(shù)將平臺(tái)驅(qū)動(dòng)添加到虛擬的總線上,以便與設(shè)備進(jìn)行關(guān)聯(lián)。platform_driver_register()函數(shù)中會(huì)調(diào)用s3c2410_i2c_driver中定義的s3c24xx_i2c_probe()函數(shù)進(jìn)行設(shè)備探測(cè),從而將驅(qū)動(dòng)和設(shè)備都加入總線中 ?????????
?? ?if (ret == 0) {
?? ??? ?ret = platform_driver_register(&s3c2440_i2c_driver);?????????????? //再次注冊(cè)
?? ??? ?if (ret)
?? ??? ??? ?platform_driver_unregister(&s3c2410_i2c_driver);?????????????? //注銷驅(qū)動(dòng)程序
?? ?}
?? ?return ret;
}
初始化函數(shù)為什么兩次調(diào)用platform_driver_register()函數(shù),這是因?yàn)榈谝粋€(gè)返回0,表示驅(qū)動(dòng)注冊(cè)成功,但并不表示探測(cè)函數(shù)s3c24xx_i2c_probe()探測(cè)IIC設(shè)備成功,有可能第一次注冊(cè)時(shí)因?yàn)橛布徽加枚綔y(cè)函數(shù)失敗,所以為了保證探測(cè)的成功率,又一次注冊(cè)并探測(cè)了一次設(shè)備。同樣卸載也要兩次
static void __exit i2c_adap_s3c_exit(void)
{
?? ?platform_driver_unregister(&s3c2410_i2c_driver);??????????? //注銷平臺(tái)驅(qū)動(dòng)
?? ?platform_driver_unregister(&s3c2440_i2c_driver);
}
static struct platform_driver s3c2440_i2c_driver = {
?? ?.probe?? ??? ?= s3c24xx_i2c_probe,
?? ?.remove?? ??? ?= s3c24xx_i2c_remove,
?? ?.suspend_late?? ?= s3c24xx_i2c_suspend_late,
?? ?.resume?? ??? ?= s3c24xx_i2c_resume,
?? ?.driver?? ??? ?= {
?? ??? ?.owner?? ?= THIS_MODULE,
?? ??? ?.name?? ?= "s3c2440-i2c",
?? ?},
};
?
探測(cè)函數(shù):s3c24xx_i2c_probe()
在該函數(shù)中將初始化適配器、IIC等硬件設(shè)備。主要完成如下功能:
1,申請(qǐng)一個(gè)適配器結(jié)構(gòu)體I2c,并對(duì)其賦初值
2,獲得I2c時(shí)鐘資源
3,將適配器的寄存器資源映射到虛擬內(nèi)存中
4,申請(qǐng)中斷處理函數(shù)
5,初始化IIC控制器
6,添加適配器I2c到內(nèi)核
static int s3c24xx_i2c_probe(struct platform_device *pdev)
{
?? ?struct s3c24xx_i2c *i2c;????????????????? //適配器指針
?? ?struct s3c2410_platform_i2c *pdata;?????????????????????????????? //IIC平臺(tái)設(shè)備相關(guān)的數(shù)據(jù)
?? ?struct resource *res;????????????????????????????????? //指向資源
?? ?int ret;??????????????????????????????????????????????????????????????????? //返回值
?? ?pdata = pdev->dev.platform_data;??????????????????????????????? //獲得平臺(tái)設(shè)備數(shù)據(jù)結(jié)構(gòu)指針
?? ?if (!pdata) {???????????????????????????????????????????????? //如果沒有數(shù)據(jù),則出錯(cuò)返回
?? ??? ?dev_err(&pdev->dev, "no platform data\n");
?? ??? ?return -EINVAL;
?? ?}
?? ?i2c = kzalloc(sizeof(struct s3c24xx_i2c), GFP_KERNEL);?????????????? //動(dòng)態(tài)分配一個(gè)適配器數(shù)據(jù)結(jié)構(gòu),并對(duì)其動(dòng)態(tài)賦值
?? ?if (!i2c) {?????????????????????????????????????????????????????????????????????????????????????????????? //內(nèi)存不足,失敗
?? ??? ?dev_err(&pdev->dev, "no memory for state\n");
?? ??? ?return -ENOMEM;
?? ?}
?? ?strlcpy(i2c->adap.name, "s3c2410-i2c", sizeof(i2c->adap.name));??????????????????? //給適配器起名為s3c2410-i2c
?? ?i2c->adap.owner?? = THIS_MODULE;???????????????????????????????????????????????????? //模塊指針
?? ?i2c->adap.algo??? = &s3c24xx_i2c_algorithm;??????????????????????????? //給適配器的一個(gè)通信方法
?? ?i2c->adap.retries = 2;???????????????????????????? //2次總線仲裁嘗試
?? ?i2c->adap.class?? = I2C_CLASS_HWMON | I2C_CLASS_SPD;???????????????????????????? //定義適配器類
?? ?i2c->tx_setup???? = 50;???????????????????? //數(shù)據(jù)從適配器傳輸?shù)娇偩€的時(shí)間為50ns
?? ?spin_lock_init(&i2c->lock);?????????????????????? //初始化自旋鎖
?? ?init_waitqueue_head(&i2c->wait);????????????????????? //初始化等待隊(duì)列頭部
?? ?/* find the clock and enable it */????? //以下代碼找到i2c的時(shí)鐘,并且調(diào)用clk_enable()函數(shù)啟動(dòng)它
?? ?i2c->dev = &pdev->dev;?????????????
?? ?i2c->clk = clk_get(&pdev->dev, "i2c");
?? ?if (IS_ERR(i2c->clk)) {
?? ??? ?dev_err(&pdev->dev, "cannot get clock\n");
?? ??? ?ret = -ENOENT;
?? ??? ?goto err_noclk;
?? ?}
?? ?dev_dbg(&pdev->dev, "clock source %p\n", i2c->clk);
?? ?clk_enable(i2c->clk);????????????????????????????? //啟動(dòng)時(shí)鐘
?? ?/* map the registers */
?? ?res = platform_get_resource(pdev, IORESOURCE_MEM, 0);??????????????????????????????? //獲得適配器的寄存器資源
?? ?if (res == NULL) {??????????????????????????????????????????????????????????????????????????????????????????????????? //獲取資源失敗則退出
?? ??? ?dev_err(&pdev->dev, "cannot find IO resource\n");
?? ??? ?ret = -ENOENT;
?? ??? ?goto err_clk;
?? ?}
?? ?i2c->ioarea = request_mem_region(res->start, (res->end-res->start)+1,
?? ??? ??? ??? ??? ? pdev->name);?????????????????????????????????????????????????????????????????????????????????????????????????????????????? //申請(qǐng)一塊I/O內(nèi)存,對(duì)應(yīng)適配器的幾個(gè)寄存器
?? ?if (i2c->ioarea == NULL) {???????????????????????????????????????????????????????????????????????????????????????????????????????? //? I/O內(nèi)存獲取失敗則退出
?? ??? ?dev_err(&pdev->dev, "cannot request IO\n");
?? ??? ?ret = -ENXIO;
?? ??? ?goto err_clk;
?? ?}
?? ?i2c->regs = ioremap(res->start, (res->end-res->start)+1);????????????????????????????????? //將設(shè)備內(nèi)存映射到虛擬地址空間,這樣可以使用函數(shù)訪問
?? ?if (i2c->regs == NULL) {?????????????????????????????????????????????????????????????????????????????????????? //映射內(nèi)存失敗則退出
?? ??? ?dev_err(&pdev->dev, "cannot map IO\n");
?? ??? ?ret = -ENXIO;
?? ??? ?goto err_ioarea;
?? ?}
?? ?dev_dbg(&pdev->dev, "registers %p (%p, %p)\n",
?? ??? ?i2c->regs, i2c->ioarea, res);???????????????????????????????????????????????????????????????????????????????? //輸出映射基地址,調(diào)試時(shí)用
?? ?/* setup info block for the i2c core */
?? ?i2c->adap.algo_data = i2c;??????????????????????????????????????????????????????????????????????????????????????????? //將私有數(shù)據(jù)指向適配器結(jié)構(gòu)體
?? ?i2c->adap.dev.parent = &pdev->dev;???????????????????????????????????????????????????????????????????????????? //組織設(shè)備模型
?? ?/* initialise the i2c controller */
?? ?ret = s3c24xx_i2c_init(i2c);??????????????????????????????????????????????? //初始化IIC控制器
?? ?if (ret != 0)???????????????????????????? //初始化失敗
?? ??? ?goto err_iomap;
?? ?/* find the IRQ for this unit (note, this relies on the init call to
?? ? * ensure no current IRQs pending
?? ? */
?? ?i2c->irq = ret = platform_get_irq(pdev, 0);??????????????????????????????????? //獲得平臺(tái)設(shè)備的第一個(gè)中斷號(hào)
?? ?if (ret <= 0) {
?? ??? ?dev_err(&pdev->dev, "cannot find IRQ\n");
?? ??? ?goto err_iomap;
?? ?}
?? ?ret = request_irq(i2c->irq, s3c24xx_i2c_irq, IRQF_DISABLED,
?? ??? ??? ?? dev_name(&pdev->dev), i2c);???????????????????????????? //申請(qǐng)一個(gè)中斷處理函數(shù),前面介紹過這個(gè)函數(shù)
?? ?if (ret != 0) {
?? ??? ?dev_err(&pdev->dev, "cannot claim IRQ %d\n", i2c->irq);
?? ??? ?goto err_iomap;
?? ?}
?? ?ret = s3c24xx_i2c_register_cpufreq(i2c);?????????????????????????????????? //在內(nèi)核中注冊(cè)一個(gè)適配器使用的時(shí)鐘
?? ?if (ret < 0) {
?? ??? ?dev_err(&pdev->dev, "failed to register cpufreq notifier\n");
?? ??? ?goto err_irq;
?? ?}
?? ?/* Note, previous versions of the driver used i2c_add_adapter()
?? ? * to add the bus at any number. We now pass the bus number via
?? ? * the platform data, so if unset it will now default to always
?? ? * being bus 0.
?? ? */
?? ?i2c->adap.nr = pdata->bus_num;????????????????????????? //適配器的總線編號(hào)
?? ?ret = i2c_add_numbered_adapter(&i2c->adap);?????????????? //指定一個(gè)最好總線編號(hào),向內(nèi)核添加該適配器
?? ?if (ret < 0) {
?? ??? ?dev_err(&pdev->dev, "failed to add bus to i2c core\n");
?? ??? ?goto err_cpufreq;
?? ?}
?? ?platform_set_drvdata(pdev, i2c);?????????????? //設(shè)置平臺(tái)設(shè)備的私有數(shù)據(jù)為i2c適配器
?? ?dev_info(&pdev->dev, "%s: S3C I2C adapter\n", dev_name(&i2c->adap.dev));
?? ?return 0;????????????????????? //成功返回0
?err_cpufreq:
?? ?s3c24xx_i2c_deregister_cpufreq(i2c);??????????????????????????? //頻率注冊(cè)失敗
?err_irq:
?? ?free_irq(i2c->irq, i2c);?????????????????????????????????????????????????????? //中斷申請(qǐng)失敗
?err_iomap:
?? ?iounmap(i2c->regs);????????????????????????????????????????????????????????? //內(nèi)存映射失敗
?err_ioarea:
?? ?release_resource(i2c->ioarea);????????????????????????????????????????? //清除資源
?? ?kfree(i2c->ioarea);
?err_clk:
?? ?clk_disable(i2c->clk);
?? ?clk_put(i2c->clk);
?err_noclk:
?? ?kfree(i2c);????????????????????????????????????????????????????????????????? //釋放i2c適配器結(jié)構(gòu)體資源
?? ?return ret;
}
與s3c24xx_i2c_probe()函數(shù)相反功能的函數(shù)是移除函數(shù):s3c24xx_i2c_remove()。它在模塊卸載函數(shù)調(diào)用platform_driver_unregister()函數(shù)時(shí)通過platform_driver的remove指針被調(diào)用。
static int s3c24xx_i2c_remove(struct platform_device *pdev)
{
?? ?struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev);?????????????????????????????????? //得到適配器結(jié)構(gòu)體指針
?? ?s3c24xx_i2c_deregister_cpufreq(i2c);???????????????????????????????????????????????????????????? //刪除內(nèi)核維護(hù)的與適配器時(shí)鐘頻率有關(guān)的數(shù)據(jù)結(jié)構(gòu)
?? ?i2c_del_adapter(&i2c->adap);???????????????????????????????????????????????????????????????????????????? //將適配器從系統(tǒng)中刪除
?? ?free_irq(i2c->irq, i2c);????????????????????????????????????????????????????????????????????????????????????? //關(guān)閉中斷
?? ?clk_disable(i2c->clk);???????????????????????????????????????????????????????????????????????????????????????? //關(guān)閉時(shí)鐘
?? ?clk_put(i2c->clk);??????????????????????????????????????????????????????????????????????????????????????????? //減少時(shí)鐘引用計(jì)數(shù)
?? ?iounmap(i2c->regs);????????????????????????????????????????????????????????????????????????????????????????? //關(guān)閉內(nèi)存映射
?? ?release_resource(i2c->ioarea);????????????????????????????????????????????????????????????????????????? //釋放I/O資源
?? ?kfree(i2c->ioarea);?????????????????????????????????????????????????????????????????????????????????????????????? //釋放資源所占用的內(nèi)存
?? ?kfree(i2c);???????????????????????????????????????????????????????????????????????????????????????????????????????? //釋放適配器的內(nèi)存
?? ?return 0;
}
?
控制器初始化函數(shù):s3c24xx_i2c_init()
static int s3c24xx_i2c_init(struct s3c24xx_i2c *i2c)
{
?? ?unsigned long iicon = S3C2410_IICCON_IRQEN | S3C2410_IICCON_ACKEN;? //設(shè)置IICCON[5]為1,表示發(fā)送和接收數(shù)據(jù)時(shí),會(huì)引發(fā)中斷。設(shè)置[7]為1,表示需要發(fā)出ACK信號(hào)
?? ?struct s3c2410_platform_i2c *pdata;?????????? //平臺(tái)設(shè)備數(shù)據(jù)指針
?? ?unsigned int freq;???????????????????????????????????????? //控制器工作的頻率
?? ?/* get the plafrom data */
?? ?pdata = i2c->dev->platform_data;????????????????????? //得到平臺(tái)設(shè)備的數(shù)據(jù)
?? ?/* inititalise the gpio */
?? ?if (pdata->cfg_gpio)????????????????????????????????????????????? //初始化gpio引腳
?? ??? ?pdata->cfg_gpio(to_platform_device(i2c->dev));
?? ?/* write slave address */
?? ?writeb(pdata->slave_addr, i2c->regs + S3C2410_IICADD);??????????????? //向IICADD寫入IIC設(shè)備地址,IICADD的位[7:1]表示IIC設(shè)備地址
?? ?dev_info(i2c->dev, "slave address 0x%02x\n", pdata->slave_addr);????????????? //打印地址信息
?? ?writel(iicon, i2c->regs + S3C2410_IICCON);?????????????????????????????????????? //初始化IICCON寄存器,只允許ACK信號(hào)和中斷使能,其他為0
?? ?/* we need to work out the divisors for the clock... */
?? ?if (s3c24xx_i2c_clockrate(i2c, &freq) != 0) {????????????????????????????????????????? //設(shè)置時(shí)鐘源和時(shí)鐘頻率
?? ??? ?writel(0, i2c->regs + S3C2410_IICCON);??????????????????????????????????????? //失敗,則設(shè)置為0
?? ??? ?dev_err(i2c->dev, "cannot meet bus frequency required\n");
?? ??? ?return -EINVAL;
?? ?}
?? ?/* todo - check that the i2c lines aren't being dragged anywhere */
?? ?dev_info(i2c->dev, "bus frequency set to %d KHz\n", freq);????????????????????????? //打印頻率信息
?? ?dev_dbg(i2c->dev, "S3C2410_IICCON=0x%02lx\n", iicon);????????????????????????? //打印IICCON寄存器
?? ?/* check for s3c2440 i2c controller? */
?? ?if (s3c24xx_i2c_is2440(i2c)) {???????????????????????????????????????????????????????????????? //如果處理器是s3c2440,則設(shè)置IICLC寄存器為SDA延時(shí)時(shí)間
?? ??? ?dev_dbg(i2c->dev, "S3C2440_IICLC=%08x\n", pdata->sda_delay);
?? ??? ?writel(pdata->sda_delay, i2c->regs + S3C2440_IICLC);
?? ?}
?? ?return 0;
}
設(shè)置控制器數(shù)據(jù)發(fā)送頻率函數(shù)s3c24xx_i2c_clockrate()
在控制器初始化函數(shù)s3c24xx_i2c_init(),調(diào)用s3c24xx_i2c_clockrate()函數(shù)設(shè)置數(shù)據(jù)發(fā)送頻率。此發(fā)送頻率由IICCON寄存器控制。發(fā)送頻率可以由一個(gè)公式得到:
發(fā)送頻率 =? IICCLK? /? (IICCON[3:0] + 1)
IICCLK = PCLK / 16?? (當(dāng)IICCON[6] == 0)
活I(lǐng)ICCLK = PCLK / 512? (當(dāng)IICCON[6] == 1)
PCLK是由clk_get_rate()函數(shù)獲得適配器的時(shí)鐘頻率。
第一個(gè)參數(shù)是適配器指針,第二個(gè)參數(shù)是返回的發(fā)送頻率:
static int s3c24xx_i2c_clockrate(struct s3c24xx_i2c *i2c, unsigned int *got)
{
?? ?struct s3c2410_platform_i2c *pdata = i2c->dev->platform_data;?????????????????? //得到平臺(tái)設(shè)備數(shù)據(jù)
?? ?unsigned long clkin = clk_get_rate(i2c->clk);????????????????????????????????????????????????? //獲得PCLK時(shí)鐘頻率
?? ?unsigned int divs, div1;
?? ?u32 iiccon;??????????????????????????????????????????????????????????????????????????????????????????????????????? //緩存IICCON
?? ?int freq;???????????????????????????????????????????????????????????????????????????????????????????????????????????? //計(jì)算的頻率
?? ?int start, end;???????????????????????????? //開始和結(jié)束頻率,用于尋找一個(gè)合適的頻率
?? ?i2c->clkrate = clkin;???????????????????????
?? ?clkin /= 1000;?? ??? ?/* clkin now in KHz */???????????? //將單位轉(zhuǎn)化為KH
?? ?dev_dbg(i2c->dev, "pdata %p, freq %lu %lu..%lu\n",
?? ??? ? pdata, pdata->bus_freq, pdata->min_freq, pdata->max_freq);????????????????????????? //打印總線,最大、最小頻率
?? ?if (pdata->bus_freq != 0) {
?? ??? ?freq = s3c24xx_i2c_calcdivisor(clkin, pdata->bus_freq/1000,
?? ??? ??? ??? ??? ??????? &div1, &divs);
?? ??? ?if (freq_acceptable(freq, pdata->bus_freq/1000))
?? ??? ??? ?goto found;
?? ?}
?? ?/* ok, we may have to search for something suitable... */
?? ?start = (pdata->max_freq == 0) ? pdata->bus_freq : pdata->max_freq;
?? ?end = pdata->min_freq;
?? ?start /= 1000;
?? ?end /= 1000;
?? ?/* search loop... */
?? ?for (; start > end; start--) {
?? ??? ?freq = s3c24xx_i2c_calcdivisor(clkin, start, &div1, &divs);
?? ??? ?if (freq_acceptable(freq, start))
?? ??? ??? ?goto found;
?? ?}
?? ?/* cannot find frequency spec */
?? ?return -EINVAL;?????????????????????????????? //不能找到一個(gè)合適的分配方式,返回錯(cuò)誤
?found:??????????????????????????????????????????????? //找到一個(gè)合適的發(fā)送頻率,則寫IICCON寄存器中與時(shí)鐘相關(guān)的位
?? ?*got = freq;????????????????????????????????????? //got為從參數(shù)返回的頻率值
?? ?iiccon = readl(i2c->regs + S3C2410_IICCON);???????????????????????????????????? //讀出IICCON的值
?? ?iiccon &= ~(S3C2410_IICCON_SCALEMASK | S3C2410_IICCON_TXDIV_512);?????????????????? //將IICCON的[6]和[3:0]清零,以避免以前分頻系數(shù)的影響
?? ?iiccon |= (divs-1);????????????????????????????????????????????????????????????????????????????????????????????????????????????? //設(shè)置位[3:0]的分頻系數(shù),divs的值 < 16
?? ?if (div1 == 512)??????????????????????????????????????????????????????????????????????????????????????????????????????????? //如果IICCLK為PCLK / 512 ,那么設(shè)置位[6]為1
?? ??? ?iiccon |= S3C2410_IICCON_TXDIV_512;
?? ?writel(iiccon, i2c->regs + S3C2410_IICCON);??????????????????????????????????????????????????? //重新寫IICCON寄存器的值
?? ?return 0;
}
?
static int s3c24xx_i2c_calcdivisor(unsigned long clkin, unsigned int wanted,
?? ??? ??? ??? ??? unsigned int *div1, unsigned int *divs)??????????????????????? //用來計(jì)算分頻系數(shù)
{
?? ?unsigned int calc_divs = clkin / wanted;????????????? //clkin表示輸入頻率,wanted表示想要分頻的系數(shù)
?? ?unsigned int calc_div1;
?? ?if (calc_divs > (16*16))??????????????????????????? //如果分頻系數(shù)大于256,那么就設(shè)置為512,為了2的冪次數(shù)
?? ??? ?calc_div1 = 512;
?? ?else
?? ??? ?calc_div1 = 16;
?? ?calc_divs += calc_div1-1;???????????????????? ? //按前面公式計(jì)算分頻系數(shù)
?? ?calc_divs /= calc_div1;
?? ?if (calc_divs == 0)????????????? //如果分頻系數(shù)不合法,調(diào)整合法
?? ??? ?calc_divs = 1;
?? ?if (calc_divs > 17)
?? ??? ?calc_divs = 17;
?? ?*divs = calc_divs;?????????????????????????????? //計(jì)算兩個(gè)分頻數(shù)
?? ?*div1 = calc_div1;
?? ?return clkin / (calc_divs * calc_div1);?????????????????????????? 得到最終的分頻系數(shù),這個(gè)系數(shù)將寫入寄存器
}
轉(zhuǎn)載于:https://www.cnblogs.com/wanghuaijun/p/7189741.html
總結(jié)
- 上一篇: android 自己定义水平和圆形pro
- 下一篇: SimpliciTI简介