一、linux I2C 驱动框架简介
I2C 驱动分为总线驱动和设备驱动。
总线驱动:SOC 的 I2C 控制器驱动,也叫 I2C 适配器驱动。
设备驱动:针对具体的 I2C 设备编写的驱动。
1、I2C总线驱动
I2C 总线驱动一般由 SOC 厂商编写。
1)i2c_adapter 结构体
I2C 总线驱动重点是 I2C 适配器(SOC的 I2C 接口控制器)驱动,内核将 I2C 适配器抽象成 i2c_adapter 结构体,结构体定义在 include/linux/i2c.h:
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;
struct i2c_bus_recovery_info *bus_recovery_info;
const struct i2c_adapter_quirks *quirks;
};
2)i2c_algorithm 结构体
第 4 行中 i2c_algorithm 就是 I2C 适配器和 I2C 设备进行通讯的方法。 i2c_algorithm 结构体 定义在 include/linux/i2c.h:
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);
/* To determine what the adapter supports */
u32 (*functionality) (struct i2c_adapter *);
#if IS_ENABLED(CONFIG_I2C_SLAVE)
int (*reg_slave)(struct i2c_client *client);
int (*unreg_slave)(struct i2c_client *client);
#endif
};
master_xfer 就是 I2C 适配器的传输函数,可以完成适配器和设备之间的通讯。
smbus_xfer 是 SMBUS 总线的传输函数。
I2C 总线驱动主要工作是初始化 i2c_adapter 结构体变量,然后设置 i2c_algorithm 中的 master_xfer 函数,然后向系统注册 i2c_adapter。
3)i2c_add_adapter 和 i2c_add_numbered_adapter 注册函数
这两个函数都是用于向内核注册 i2c_adapter,区别在于 i2c_add_adapter 使用动态的总线号,i2c_add_numbered_adapter 使用静态的总线号。函数原型如下:
int i2c_add_adapter(struct i2c_adapter *adapter)
int i2c_add_numbered_adapter(struct i2c_adapter *adap)
adapter 或 adap::要添加到 Linux 内核中的 i2c_adapter。
返回值:0,成功;负值,失败。
4)i2c_del_adapter 函数
i2c_del_adapter 函数用于删除 I2C 适配器,函数原型如下:
void i2c_del_adapter(struct i2c_adapter * adap)
adap::要删除的 i2c_adapter。
2、I2C设备驱动
1)i2c_client结构体
i2c_client 结构体用于描述设备信息,结构体定义在 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 device dev; /* the device structure */
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_client,每检测到一个 I2C 设备就会给这个 I2C 设备分配一个 i2c_client。
2)i2c_driver结构体
i2c_driver 类似 platform_driver,结构体定义在 include/linux/i2c.h:
struct i2c_driver {
unsigned int class;
/* Notifies the driver that a new bus has appeared. You should avoid
* using this, it will be removed in a near future.
*/
int (*attach_adapter)(struct i2c_adapter *) __deprecated;
/* Standard driver model interfaces */
int (*probe)(struct i2c_client *, const struct i2c_device_id *);
int (*remove)(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.
* The format and meaning of the data value depends on the protocol.
* For the SMBus alert protocol, there is a single bit of data passed
* as the alert response's low bit ("event flag").
*/
void (*alert)(struct i2c_client *, 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;
const struct i2c_device_id *id_table;
/* Device detection callback for automatic device creation */
int (*detect)(struct i2c_client *, struct i2c_board_info *);
const unsigned short *address_list;
struct list_head clients;
};
第 10 行,当 I2C 设备和驱动匹配成功以后 probe 函数就会执行。
第 28 行,如果使用设备树的话,需要设置 device_driver 的 of_match_table 成员变量。
第 29 行,id_table 是传统的、未使用设备树的设备匹配 ID 表。
3)i2c_register_driver函数
i2c_register_driver 函数用于向内核注册 i2c_driver,函数原型如下:
int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
owner:一般为 THIS_MODULE。
driver:要注册的 i2c_driver。
返回值:0,成功;负值,失败。
4)i2c_add_driver宏
i2c_add_driver 宏也用于注册 i2c_driver,定义如下:
#define i2c_add_driver(driver) \
i2c_register_driver(THIS_MODULE, driver) //driver:要注册的 i2c_driver
5)i2c_del_driver函数
注销 I2C 设备驱动的时候需要将前面注册的 i2c_driver 从 linux 内核中注销掉,需要用到 i2c_del_driver 函数,函数原型如下:
void i2c_del_driver(struct i2c_driver *driver)
driver:要注销的 i2c_driver。
6)i2c_driver注册示例代码
/* i2c驱动的probe函数 */
static int xxx_probe(struct i2c_client *client, const struct i2c_device_id *id)
{
/* 函数具体程序 */
return 0;
}
/* i2c驱动的remove函数 */
static int ap3216c_remove(struct i2c_client *client)
{
/* 函数具体程序 */
return 0;
}
/* 传统匹配方式ID列表 */
static const struct i2c_device_id xxx_id[] = {
{
"xxx", 0},
{
}
};
/* 设备树匹配列表 */
static const struct of_device_id xxx_of_match[] = {
{
.compatible = "xxx" },
{
/* Sentinel */ }
};
/* i2c驱动结构体 */
static struct i2c_driver xxx_driver = {
.probe = xxx_probe,
.remove = xxx_remove,
.driver = {
.owner = THIS_MODULE,
.name = "xxx",
.of_match_table = xxx_of_match,
},
.id_table = xxx_id,
};
/* 驱动入口函数 */
static int __init xxx_init(void)
{
int ret = 0;
ret = i2c_add_driver(&xxx_driver);
return ret;
}
/* 驱动出口函数 */
static void __exit xxx_exit(void)
{
i2c_del_driver(&xxx_driver);
}
module_init(xxx_init)