i2c體繫結構分為3個組成部分:
(1)i2c核心
(2)i2c匯流排驅動
(3)i2c裝置驅動
i2c核心:提供了i2c匯流排驅動和裝置驅動的註冊,登出方法,i2c通訊方法的上層的,與具體適配器無關的代碼以及探測裝置,檢測裝置地址的上層代碼等。
i2c匯流排驅動:對i2c硬體體繫結構中適配器的實現,適配器可由CPU控制,甚至可以直接整合在CPU內部。主要包含了i2c適配器資料結構i2c_adapter,i2c適配器的algorithm資料結構i2c_algorithm和控制i2c適配器產生通訊訊號的函數。
i2c裝置驅動:對i2c硬體體繫結構中裝置端的實現,裝置一般掛接在受CPU控制的i2c適配器上,通過i2c適配器與CPU交換資料。
接下來主要描述一下i2c裝置驅動的幾個關鍵函數。
1.在核心的i2c.h檔案中有這樣四個結構體:
i2c_adapter結構體:
333 struct i2c_adapter {
334 struct module *owner;
335 unsigned int id;
336 unsigned int class; /* classes to allow probing for */
337 const struct i2c_algorithm *algo; /* the algorithm to access the bus */
338 void *algo_data;
339
340 /* data fields that are valid for all devices */
341 u8 level; /* nesting level for lockdep */
342 struct mutex bus_lock;
343
344 int timeout; /* in jiffies */
345 int retries;
346 struct device dev; /* the adapter device */
347
348 int nr;
349 char name[48];
350 struct completion dev_released;
351 };
i2c_algorithm結構體
struct i2c_algorithm {
313 /* If an adapter algorithm can't do I2C-level access, set master_xfer
314 to NULL. If an adapter algorithm can do SMBus access, set
315 smbus_xfer. If set to NULL, the SMBus protocol is simulated
316 using common I2C messages */
317 /* master_xfer should return the number of messages successfully
318 processed, or a negative value on error */
319 int (*master_xfer)(struct i2c_adapter *adap, struct i2c_msg *msgs,
320 int num);
321 int (*smbus_xfer) (struct i2c_adapter *adap, u16 addr,
322 unsigned short flags, char read_write,
323 u8 command, int size, union i2c_smbus_data *data);
324
325 /* To determine what the adapter supports */
326 u32 (*functionality) (struct i2c_adapter *);
327 };
i2c_driver結構體
136 struct i2c_driver {
137 unsigned int class;
138
139 /* Notifies the driver that a new bus has appeared or is about to be
140 * removed. You should avoid using this if you can, it will probably
141 * be removed in a near future.
142 */
143 int (*attach_adapter)(struct i2c_adapter *);
144 int (*detach_adapter)(struct i2c_adapter *);
145
146 /* Standard driver model interfaces */
147 int (*probe)(struct i2c_client *, const struct i2c_device_id *);
148 int (*remove)(struct i2c_client *);
149
150 /* driver model interfaces that don't relate to enumeration */
151 void (*shutdown)(struct i2c_client *);
152 int (*suspend)(struct i2c_client *, pm_message_t mesg);
153 int (*resume)(struct i2c_client *);
154
155 /* a ioctl like command that can be used to perform specific functions
156 * with the device.
157 */
158 int (*command)(struct i2c_client *client, unsigned int cmd, void *arg);
159
160 struct device_driver driver;
161 const struct i2c_device_id *id_table;
162
163 /* Device detection callback for automatic device creation */
164 int (*detect)(struct i2c_client *, int kind, struct i2c_board_info *);
165 const struct i2c_client_address_data *address_data;
166 struct list_head clients;
167 };
i2c_client結構體
188 struct i2c_client {
189 unsigned short flags; /* div., see below */
190 unsigned short addr; /* chip address - NOTE: 7bit */
191 /* addresses are stored in the */
192 /* _LOWER_ 7 bits */
193 char name[I2C_NAME_SIZE];
194 struct i2c_adapter *adapter; /* the adapter we sit on */
195 struct i2c_driver *driver; /* and our access routines */
196 struct device dev; /* the device structure */
197 int irq; /* irq issued by device */
198 struct list_head detected;
199 };