相关结构体
I2C驱动
struct i2c_driver
路径: include/linux/i2c.h
c
struct i2c_driver {
unsigned int class; //I2C设备的类型
/* Standard driver model interfaces 标准驱动模型接口 */
int (*probe)(struct i2c_client *, const struct i2c_device_id *);
int (*remove)(struct i2c_client *);
/* New driver model interface to aid the seamless removal of the
* current probe()'s, more commonly unused than used second parameter.
*/
int (*probe_new)(struct i2c_client *);
/* driver model interfaces that don't relate to enumeration */
void (*shutdown)(struct i2c_client *);
/* Alert callback, for example for the SMBus alert protocol. */
void (*alert)(struct i2c_client *, enum i2c_alert_protocol protocol,
unsigned int data);
/* a ioctl like command that can be used to perform specific functions
* with the device.
*/
int (*command)(struct i2c_client *client, unsigned int cmd, void *arg);
struct device_driver driver; /* Device driver model driver */
const struct i2c_device_id *id_table; /* 这个驱动所支持的I2C设备列表 */
/* Device detection callback for automatic device creation */
int (*detect)(struct i2c_client *, struct i2c_board_info *);
const unsigned short *address_list; /* 在detect时的地址范围 */
struct list_head clients; /* 探测到的从设备列表 */
bool disable_i2c_core_irq_mapping; /* Tell the i2c-core to not do irq-mapping */
};
I2C客户端
struct i2c_client
路径: include/linux/i2c.h
c
struct i2c_client {
unsigned short flags; /* div., see below */
unsigned short addr; /* chip address - NOTE: 7bit 连接到I2C控制器上的设备地址 */
/* addresses are stored in the */
/* _LOWER_ 7 bits */
char name[I2C_NAME_SIZE]; /* 设备的名字,通常指示着设备的类型 */
struct i2c_adapter *adapter; /* the adapter we sit on */
struct device dev; /* the device structure */
int init_irq; /* irq set at initialization 初始的中断号 */
int irq; /* irq issued by device 映射之后的中断号 */
struct list_head detected;
#if IS_ENABLED(CONFIG_I2C_SLAVE)
i2c_slave_cb_t slave_cb; /* callback for slave mode */
#endif
};
I2C控制器
struct i2c_adapter
路径: include/linux/i2c.h
c
struct i2c_adapter {
struct module *owner; /* THIS_MODULE指向当前模块 */
unsigned int class; /* classes to allow probing for 支持的设备类型 */
const struct i2c_algorithm *algo; /* the algorithm to access the bus 控制器的传输方法*/
void *algo_data; /* algorithm私有数据 */
/* data fields that are valid for all devices */
const struct i2c_lock_operations *lock_ops; /* 特殊锁定行为支持 */
struct rt_mutex bus_lock; /* 多设备同时访问总线锁 */
struct rt_mutex mux_lock; /* 包含I2C总线多路复用器的同步访问 */
int timeout; /* 超时时间 in jiffies */
int retries; /* 重试次数 */
struct device dev; /* the adapter device */
int nr; /* 控制器编号 /dev/i2c-x (x=nr) */
char name[48]; /* 控制器名称 /sys/bus/i2c/devices/i2c-x/name */
struct completion dev_released; /* 用于同步释放控制器资源的完成机制 */
struct mutex userspace_clients_lock; /* 保护并发操作 */
struct list_head userspace_clients; /* 维护一个I2C客户端链表 */
struct i2c_bus_recovery_info *bus_recovery_info; /* 总线死锁时恢复手段 */
const struct i2c_adapter_quirks *quirks; /* 控制器某些特殊限制或特性 */
struct irq_domain *host_notify_domain; /* 中断路由 */
};
I2C传输方法
struct i2c_algorithm
路径: include/linux/i2c.h
c
struct i2c_algorithm {
/* If an adapter algorithm can't do I2C-level access, set master_xfer
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, 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); /* SMBus 传输函数 */
/* To determine what the adapter supports 功能标志函数 */
u32 (*functionality) (struct i2c_adapter *);
#if IS_ENABLED(CONFIG_I2C_SLAVE)
/* Register given client to I2C slave mode of this adapter */
int (*reg_slave)(struct i2c_client *client);
/* Unregister given client from I2C slave mode of this adapter */
int (*unreg_slave)(struct i2c_client *client);
#endif
};
总结
上述各项结构体与I2C总线之间的关系如下图所示:

- i2c_adapter在物理层面代表一个I2C控制器,i2c_algorithm代表该控制器支持的传输方法;
- i2c_client代表每一个I2C设备,i2c_driver为一套驱动方法,两者关系为多对1;
- i2c_adapter和i2c_client与物理连接一致,即i2c_client表示连着在这个控制器下面的设备,而i2c_adapter代表为设备所连接的控制器;