目录
在 Linux 内核中 I2C 的体系结构分为 3 个部分:
①I2C 核心: I2C 核心提供了 I2C 总线驱动和设备驱动的注册、 注销方法
②I2C 总线驱动: I2C 总线驱动是对 I2C 硬件体系结构中适配器端的实现, 适配器可由CPU 控制, 甚至可以直接集成在 CPU 内部。I2C 总线驱动就是 SOC 的 I2C 控制器驱动,也叫做 I2C 适配器驱动。
③I2C 设备驱动: I2C 设备驱动是对 I2C 硬件体系结构中设备端的实现, 设备一般挂接在受 CPU 控制的 I2C 适配器上, 通过 I2C 适配器与 CPU 交换数据。
I2C 总线和 platform 总线类似, 区别在于platform 总线是虚拟的一条总线, 而 I2C 总线是实际
存在的。 对于使用 I2C 通信的设备, 在驱动中直接使用 I2C 总结即可。 I2C 总线驱动的重点是 I2C 适配器驱动, 主要涉及到两个结构体: i2c_adapter 和 i2c_algorithm。 在 Linux 内核中用 i2c_adapter 结构体来表示 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; /* 总线访问算法 */
- 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;
- };
i2c_algorithm 类型的指针变量 algo, 对于一个 I2C 适配器, 要对外提供读写 API 函数, 设备驱动程序可以使用这些 API 函数来完成读写操作。 i2c_algorithm 是 I2C 适配器与 IIC 设备进行通信的方法。
i2c_algorithm 结构体定义在 include/linux/i2c.h 文件中
-
- struct i2c_algorithm
- {
- ......
- 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 *);
- ......
- };
master_xfer 是 I2C 适配器的传输函数, 可以通过此函数来完成与 IIC 设备之间的通信。smbus_xfer 是 SMBUS 总线的传输函数。
I2C 总线驱动(I2C 适配器驱动)的主要工作就是初始化 i2c_adapter 结构体变量,然后设置 i2c_algorithm 中的 master_xfer 函数。最后通过 i2c_add_numbered_adapter或 i2c_add_adapter 这两个函数向系统注册i2c_adapter。
- int i2c_add_adapter(struct i2c_adapter *adapter)
- int i2c_add_numbered_adapter(struct i2c_adapter *adap)
adapter 或 adap:要添加到 Linux 内核中的 i2c_adapter,即I2C 适配器
返回值: 0,成功;负值,失败
这两个函数的区别在于 i2c_add_adapter 使用动态的总线号,而 i2c_add_numbered_adapter
使用静态总线号。
如果要删除 I2C 适配器使用 i2c_del_adapter 函数即可
void i2c_del_adapter(struct i2c_adapter * adap) //adap:要删除的 I2C 适配器
一般 SOC 的 I2C 总线驱动都是由半导体厂商编写的,所以大多数只要专注于 I2C 设备驱动即可。
在 I2C 设备驱动中主要有两个重要的结构体: i2c_client 和 i2c_driver。 i2c_client 是描述设备信息的,i2c_driver 描述驱动内容。 i2c_client 结构体定义在include/linux/i2c.h 文件中
- struct i2c_client {
- unsigned short flags; /* 标志 */
- unsigned short addr; /* 芯片地址, 7 位,存在低 7 位*/
- ......
- char name[I2C_NAME_SIZE]; /* 名字 */
- struct i2c_adapter *adapter; /* 对应的 I2C 适配器 */
- struct device dev; /* 设备结构体 */
- int irq; /* 中断 */
- struct list_head detected;
- ......
- };
当驱动和设备匹配成功后,每检测到一个 I2C 设备就会给这个 I2C 设备分配一个i2c_client,这个ic_client存储着这个设备的所有信息,如芯片地址。
i2c_driver 类似 platform_driver,是编写 I2C 设备驱动重点要处理的内容, i2c_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 ("eventflag"). */
- 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;
- };
当 I2C 设备和驱动匹配成功以后 probe 函数就会执行。device_driver 驱动结构体,如果使用设备树,需要设置 device_driver 的of_match_table 成员变量,即驱动的兼容(compatible)性。 未使用设备树的设备需要设置id_table 设备匹配 ID 表。
在编写 I2C 设备的驱动时, 主要是创建 i2c_driver 结构体, 并实现里面的内容。 i2c_driver 结构体创建完成后, 使用i2c_register_driver 函数向 Linux 内核中注册 i2c 设备。
int i2c_register_driver(struct module *owner,struct i2c_driver *driver)
owner: 一般为 THIS_MODULEdriver:要注册的 i2c_driver
返回值: 0,成功;负值,失败
i2c_add_driver 也常常用于注册 i2c_driver, i2c_add_driver 是一个宏定义,
- #define i2c_add_driver(driver) \
- i2c_register_driver(THIS_MODULE, driver)
i2c_add_driver是对i2c_register_driver做了一个简单的封装,只有一个参数,要注册的i2c_driver。
注销 I2C 设备驱动的时需要用到i2c_del_driver 函数将前面注册的 i2c_driver 从 Linux 内核中注销掉
void i2c_del_driver(struct i2c_driver *driver) //driver:要注销的 i2c_driver
i2c_driver注册模板:
- /* i2c 驱动的 probe 函数 */
- static int xxx_probe(struct i2c_client *client,const struct i2c_device_id *id)
- {
- /* 函数具体程序,一般字符设备驱动框架*/
- return 0;
- }
-
- /* i2c 驱动的 remove 函数 */
- static int xxx_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);
- module_exit(xxx_exit);
在 I2C 核心层完成的是 I2C 设备和I2C 驱 动的匹配过程。I2C 核心部分的文件是drivers/i2c/i2c-core.c。
I2C 核心层提供了一些与硬件无关的 API 函数。
①i2c_adapter 注册/注销函数
int i2c_add_adapter(struct i2c_adapter *adapter)
int i2c_add_numbered_adapter(struct i2c_adapter *adap)
void i2c_del_adapter(struct i2c_adapter * adap)
②i2c_driver 注册/注销函数
int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
int i2c_add_driver (struct i2c_driver *driver)
void i2c_del_driver(struct i2c_driver *driver)
设备和驱动的匹配过程是在 I2C 总线完成的, I2C 总线的数据结构为 i2c_bus_type, 定义在
drivers/i2c/i2c-core.c 文件
- struct bus_type i2c_bus_type = {
- .name = "i2c",
- .match = i2c_device_match,
- .probe = i2c_device_probe,
- .remove = i2c_device_remove,
- .shutdown = i2c_device_shutdown,
- };
match 成员变量是 I2C 总线上设备和驱动匹配函数,即i2c_device_match函数。
- static int i2c_device_match(struct device *dev, struct device_driver *drv)
- {
- struct i2c_client *client = i2c_verify_client(dev);
- struct i2c_driver *driver;
-
- if (!client)
- return 0;
-
- /* Attempt an OF style match */
- if (of_driver_match_device(dev, drv))
- return 1;
-
- /* Then ACPI style match */
- if (acpi_driver_match_device(dev, drv))
- return 1;
-
- driver = to_i2c_driver(drv);
- /* match on an id table if there is one */
- if (driver->id_table)
- return i2c_match_id(driver->id_table, client) != NULL;
-
- return 0;
- }
of_driver_match_device 函数用于完成设备树设备和驱动匹配。比较 I2C 设备节点的 compatible 属性和 of_device_id 中的 compatible 属性是否相等,如果相当的话就表示 I2C设备和驱动匹配。
acpi_driver_match_device 函数用于 ACPI 形式的匹配。
i2c_match_id 函数用于无设备树的 I2C 设备和驱动匹配过程。比较 I2C设备名字和 i2c_device_id 的 name 字段是否相等,相等的话就说明 I2C 设备和驱动匹配。
I2C 总线驱动一般是不用用户自己编写的, 但是I2C 设备驱动需要用户编写。 每个设备的驱动都
不相同, 但是编写流程是一样的。
当开始编写 I2C 设备驱动时,首先要添加设备信息。在不使用设备树时, 使用平台文件时添加I2C 设备信息。
在平台文件中通过 i2c_board_info 结构体来描述一个具体的 I2C 设备
- struct i2c_board_info {
- char type[I2C_NAME_SIZE]; /* I2C 设备名字 */
- unsigned short flags; /* 标志 */
- unsigned short addr; /* I2C 器件地址 */
- void *platform_data;
- struct dev_archdata *archdata;
- struct device_node *of_node;
- struct fwnode_handle *fwnode;
- int irq;
- };
其中 type 和 addr 这两个成员变量是必须要设置的, 一个是 I2C 设备的名字, 一个是 I2C 设备的器件地址。
打开 arch/arm/mach-imx/mach-mx27_3ds.c 文件, 此文件中关于 OV2640 的 I2C 设备信息描述如下:
- static struct i2c_board_info mx27_3ds_i2c_camera = {
- I2C_BOARD_INFO("ov2640", 0x30),
- };
I2C_BOARD_INFO 来完成 mx27_3ds_i2c_camera 的初始化工作, I2C_BOARD_INFO 是一个宏
- #define I2C_BOARD_INFO(dev_type, dev_addr) \
- .type = dev_type, .addr = (dev_addr)
I2C_BOARD_INFO 宏是设置 i2c_board_info 的 type 和 addr 这两个成员变量。
在使用设备树文件时, 只需要在设备树文件中添加相应的 I2C 设备节点就可以了。NXP 官方的在 I2C1 上接了 mag3110 这个磁力计芯片, 因此必须在 i2c1 节点下创建mag3110 子节点, 然后在这个子节点内描述 mag3110 这个芯片的相关信息。
- &i2c1 {
- clock-frequency = <100000>;
- pinctrl-names = "default";
- pinctrl-0 = <&pinctrl_i2c1>;
- status = "okay";
-
- mag3110@0e {
- compatible = "fsl,mag3110";
- reg = <0x0e>;
- position = <2>;
- };
- ......
- };
向 i2c1 添加 mag3110 子节点,mag3110@0e是子节点名字, @后面的0e是I2C 器件地址。compatible 属性值为fsl,mag3110。reg属性也是设置I2C的器件地址的。I2C 设备节点的创建主要是是 compatible 属性和 reg属性的设置, 一个用于匹配驱动, 一个用于设置器件地址。
在 I2C 设备驱动中首先要完成 i2c_driver 结构体的创建、 初始化和注册, 当设备和驱动匹配成功后,就会执行 probe 函数, probe 函数中就是执行字符设备驱动的一套流程。
一般需要在probe函数里面初始化 I2C 设备,要初始化 I2C 设备需要使用 i2c_transfer 函数对 I2C 设备寄存器进行读写操作。 i2c_transfer 函数会调用 I2C 适配器中 i2c_algorithm里面的 master_xfer 函数, 对于 I.MX6U 而言是 i2c_imx_xfer 这个函数。
int i2c_transfer(struct i2c_adapter *adap,struct i2c_msg *msgs,int num)
adap: 所使用的 I2C 适配器, i2c_client 会保存其对应的 i2c_adapter
msgs: I2C 要发送的一个或多个消息
num: 消息数量,msgs 的数量
返回值: 负值,失败,其他非负值,发送的 msgs 数量
msgs参数是一个 i2c_msg 类型的指针参数,Linux 内 核 使 用 i2c_msg 结 构 体 来 描 述 一 个 消 息。
- struct i2c_msg {
- __u16 addr; /* 从机地址 */
- __u16 flags; /* 标志 */
- #define I2C_M_TEN 0x0010
- #define I2C_M_RD 0x0001
- #define I2C_M_STOP 0x8000
- #define I2C_M_NOSTART 0x4000
- #define I2C_M_REV_DIR_ADDR 0x2000
- #define I2C_M_IGNORE_NAK 0x1000
- #define I2C_M_NO_RD_ACK 0x0800
- #define I2C_M_RECV_LEN 0x0400
- __u16 len; /* 消息(本 msg)长度 */
- __u8 *buf; /* 消息数据 */
- };
使用i2c_transfer函数发送数据前,要构建好 i2c_msg,使用 i2c_transfer 进行 I2C 数据收发的模板:
- /* 设备结构体 */
- struct xxx_dev {
- ......
- void *private_data; /* 私有数据,一般会设置为 i2c_client */
- };
-
- /*
- * @description : 读取 I2C 设备多个寄存器数据
- * @param – dev : I2C 设备
- * @param – reg : 要读取的寄存器首地址
- * @param – val : 读取到的数据
- * @param – len : 要读取的数据长度
- * @return : 操作结果
- */
- static int xxx_read_regs(struct xxx_dev *dev, u8 reg, void *val,
- int len)
- {
- int ret;
- struct i2c_msg msg[2];
- struct i2c_client *client = (struct i2c_client *)
- dev->private_data;
-
- /* msg[0],第一条写消息,发送要读取的寄存器首地址 */
- msg[0].addr = client->addr; /* I2C 器件地址 */
- msg[0].flags = 0; /* 标记为发送数据 */
- msg[0].buf = ® /* 读取的首地址 */
- msg[0].len = 1; /* reg 长度 */
-
- /* msg[1],第二条读消息,读取寄存器数据 */
- msg[1].addr = client->addr; /* I2C 器件地址 */
- msg[1].flags = I2C_M_RD; /* 标记为读取数据 */
- msg[1].buf = val; /* 读取数据缓冲区 */
- msg[1].len = len; /* 要读取的数据长度 */
- ret = i2c_transfer(client->adapter, msg, 2);
- if(ret == 2)
- {
- ret = 0;
- }
- else
- {
- ret = -EREMOTEIO;
- }
- return ret;
- }
-
- /*
- * @description : 向 I2C 设备多个寄存器写入数据
- * @param – dev : 要写入的设备结构体
- * @param – reg : 要写入的寄存器首地址
- * @param – buf : 要写入的数据缓冲区
- * @param – len : 要写入的数据长度
- * @return : 操作结果
- */
- static s32 xxx_write_regs(struct xxx_dev *dev, u8 reg, u8 *buf,u8 len)
- {
- u8 b[256];
- struct i2c_msg msg;
- struct i2c_client *client = (struct i2c_client *)
- dev->private_data;
-
- b[0] = reg; /* 寄存器首地址 */
- memcpy(&b[1],buf,len); /* 将要发送的数据拷贝到数组 b 里面 */
-
- msg.addr = client->addr; /* I2C 器件地址 */
- msg.flags = 0; /* 标记为写数据 */
-
- msg.buf = b; /* 要发送的数据缓冲区 */
- msg.len = len + 1; /* 要发送的数据长度 */
-
- return i2c_transfer(client->adapter, &msg, 1);
- }
在设备结构体里面添加一个执行void的指针成员变量private_data,此成员变量用于保存设备的私有数据,在 I2C 设备驱动中一般将其指向 I2C 设备对应的i2c_client。

xxx_read_regs 函数用于读取 I2C 设备多个寄存器数据,然后定义了一个i2c_msg 数组, 2 个数组元素。如图,I2C写时序图,因为 I2C 读取数据的时候要先发送要读取的寄存器地址,然后再读取数据,所以需要准备两个 i2c_msg。一个用于发送寄存器地址,一个用于读取寄存器值。
对于 msg[0], 将 flags设置为 0, 表示写数据。 msg[0]的 addr 是 I2C 设备的器件地址, msg[0]的 buf 成员变量就是要读取的寄存器地址。 对于 msg[1], 将 flags 设置为 I2C_M_RD, 表示读取数据。 msg[1]的 buf 成员变量用于保存读取到的数据, len 成员变量就是要读取的数据长度。 调用 i2c_transfer 函数完成 I2C 数据读操作。
xxx_write_regs 函数用于向 I2C 设备多个寄存器写数据。 数组 b 用于存放寄存器首地址和要发送的数据,msg 的 addr设置为 I2C 器件地址。然后设置 msg 的 flags 为 0, 也就是写数据。设置要发送的数据, 也就是数组 b。设置 msg 的 len 为 len+1, 因为要加上一个字节的寄存器地址。 最后通过 i2c_transfer 函数完成向 I2C 设备的写操作。
另外还有两个API函数分别用于I2C数据的收发操作,这两个函数都会调用i2c_transfer。I2C 数据发送函数 i2c_master_send。
int i2c_master_send(const struct i2c_client *client,const char *buf,int count)
client: I2C 设备对应的 i2c_client
buf:要发送的数据
count: 要发送的数据字节数,要小于 64KB,以为 i2c_msg 的 len 成员变量是一个 u16(无
符号 16 位)类型的数据。
返回值: 负值,失败,其他非负值,发送的字节数
I2C 数据接收函数为 i2c_master_recv。
int i2c_master_recv(const struct i2c_client *client,char *buf,int count)
client: I2C 设备对应的 i2c_client
buf:要接收的数据count: 要接收的数据字节数,要小于 64KB,以为 i2c_msg 的 len 成员变量是一个 u16(无
符号 16 位)类型的数据
返回值: 负值,失败,其他非负值,发送的字节数