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结构体:
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结构体
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 };