一、i2c_driver, i2c_client, i2c_adapter, i2c_algorithm 這4個資料結構的作用及關係
1.i2c_adapter 與 i2c_algorithm
i2c_adapter 對應物理上一個適配器,而i2c_algorithm對應一套通訊演算法。
I2C適配器需要i2c_algorithm中提供的通訊函數,來控制適配器上產生特定的訪問周期,缺少i2c_algorithm的i2c_adapter什麼也做不了.
i2c_adapter中包含了其使用的i2c_algorithm的指標。
i2c_algorithm中關鍵函數是 master_xfer(),用於產生I2C訪問周期的需要的訊號,以i2c_msg為單位。
/** * struct i2c_msg - an I2C transaction segment beginning with START * @addr: Slave address, either seven or ten bits. When this is a ten * bit address, I2C_M_TEN must be set in @flags and the adapter * must support I2C_FUNC_10BIT_ADDR. * @flags: I2C_M_RD is handled by all adapters. No other flags may be * provided unless the adapter exported the relevant I2C_FUNC_* * flags through i2c_check_functionality(). * @len: Number of data bytes in @buf being read from or written to the * I2C slave address. For read transactions where I2C_M_RECV_LEN * is set, the caller guarantees that this buffer can hold up to * 32 bytes in addition to the initial length byte sent by the * slave (plus, if used, the SMBus PEC); and this value will be * incremented by the number of block data bytes received. * @buf: The buffer into which data is read, or from which it's written. * * An i2c_msg is the low level representation of one segment of an I2C * transaction. It is visible to drivers in the @i2c_transfer() procedure, * to userspace from i2c-dev, and to I2C adapter drivers through the * @i2c_adapter.@master_xfer() method. * * Except when I2C "protocol mangling" is used, all I2C adapters implement * the standard rules for I2C transactions. Each transaction begins with a * START. That is followed by the slave address, and a bit encoding read * versus write. Then follow all the data bytes, possibly including a byte * with SMBus PEC. The transfer terminates with a NAK, or when all those * bytes have been transferred and ACKed. If this is the last message in a * group, it is followed by a STOP. Otherwise it is followed by the next * @i2c_msg transaction segment, beginning with a (repeated) START. * * Alternatively, when the adapter supports I2C_FUNC_PROTOCOL_MANGLING then * passing certain @flags may have changed those standard protocol behaviors. * Those flags are only for use with broken/nonconforming slaves, and with * adapters which are known to support the specific mangling options they * need (one or more of IGNORE_NAK, NO_RD_ACK, NOSTART, and REV_DIR_ADDR). */struct i2c_msg { __u16 addr; /* slave address */ //裝置地址 __u16 flags;#define I2C_M_TEN 0x0010 /* this is a ten bit chip address */#define I2C_M_RD 0x0001 /* read data, from slave to master */#define I2C_M_NOSTART 0x4000 /* if I2C_FUNC_PROTOCOL_MANGLING */#define I2C_M_REV_DIR_ADDR 0x2000 /* if I2C_FUNC_PROTOCOL_MANGLING */#define I2C_M_IGNORE_NAK 0x1000 /* if I2C_FUNC_PROTOCOL_MANGLING */#define I2C_M_NO_RD_ACK 0x0800 /* if I2C_FUNC_PROTOCOL_MANGLING */#define I2C_M_RECV_LEN 0x0400 /* length will be first received byte */ __u16 len; /* msg length */ __u8 *buf; /* pointer to msg data */ //訊息資料};
2. i2c_driver and i2c_client.
i2c_driver是一套驅動方法,用於輔助作用的資料結構,不對應任何物理實體。
i2c_client對應真實的物理裝置,每個I2C裝置都需要一個i2c_client來描述。
i2c_client一般被包含在I2C字元裝置的私人資訊結構體中,private_data.
i2c_driver的attach_adapter()函數被運行時,i2c_adapter()會探測物理裝置,當確定一個client存在時,把該client使用的i2c_client資料結構的adapter指標指向對應的i2c_adapter, driver指標指向i2c_driver,並會調用i2c_adapter的client_register()函數。
相反的過程發生在i2c_driver的detach_client()函數被調用的時候。
3.i2c_adapter 與 i2c_client
i2c_adapter 與 i2c_client 關係相當於適配器和裝置的關係,i2c_client 依附於 i2c_adapter.
一個適配器可以串連多個I2C裝置,所以一個 i2c_adapter 可以被多個 i2c_client 依附。
i2c_adapter 中有依附於它的 i2c_client 的鏈表。
二、I2C模組中工程師需要實現的工作:
1.
寫I2C適配器的驅動: 探測、初始化I2C適配器(如申請I2C的I/O地址,中斷號)、驅動I2C適配器在硬體上產生各種訊號,及處理I2C中斷等。
2.
寫I2C適配器的algorithm:用具體適配器的xxx_xfer函數填充i2c_algorithm的master_xfer指標,並把i2c_algorithm指標賦值給i2c_adapter的algo指標。
3.
寫I2C裝置驅動:實現I2C裝置驅動與i2c_driver的介面,用具體裝置yyy的yyy_attach_adapter()函數指標,yyy_detach_client()函數指標,和yyy_commond()函數指標,賦值給i2c_driver的attach_adater,detach_adapter,和detach_client指標。
4.實現I2C裝置驅動的檔案操作介面: 即實現具體裝置yyy的 yyy_read(),yyy_write(),yyy_ioctl().
前兩個工作屬於I2C匯流排驅動,後兩個屬於I2C裝置驅動。
來源:http://blog.chinaunix.net/space.php?uid=20748774&do=blog&id=1747412