主要的数据结构体:
// include/linux/i2c.h
struct i2c_client {
unsigned short flags; /* div., see below */
unsigned short addr; /* chip address - NOTE: 7bit */
/* addresses are stored in the */
/* _LOWER_ 7 bits */
char name[I2C_NAME_SIZE];
struct i2c_adapter *adapter; /* the adapter we sit on */
struct i2c_driver *driver; /* and our access routines */
struct device dev; /* the device structure */
int irq; /* irq issued by device */
struct list_head detected;
};
struct i2c_adapter {
struct module *owner;
unsigned int class; /* classes to allow probing for */
const struct i2c_algorithm *algo; /* the algorithm to access the bus */
void *algo_data;
/* data fields that are valid for all devices */
struct rt_mutex bus_lock;
int timeout; /* in jiffies */
int retries;
struct device dev; /* the adapter device */
int nr;
char name[48];
struct completion dev_released;
struct mutex userspace_clients_lock;
struct list_head userspace_clients;
};
驱动编写:
i2c控制器的驱动一般在platform_device注册列表中注册。(driver/i2c/busses/i2c.c)
从机驱动:
- 系统初始化时添加以i2c_board_info为结构体的从设备信息,然后在板级系统初始化的时候通过i2c_register_board_info函数,生成一个对应的i2c_client。
- 在I2C从设备驱动程序里使用i2c_adapter里所提供的算法
- 将i2c从设备的特有数据结构挂载在i2c_client.dev下