linux驱动开发学习笔记二十四:I2C驱动框架介绍

I2C 是很常用的一个串行通信接口,用于连接各种外设、传感器等器件,Linux 下的 I2C 驱动是有框架的,我们需要按照指定的框架去编写 I2C 设备驱动。

为了符合 Linux 的驱动分离与分层的思想,Linux内核将 I2C 驱动分为两部分:
①I2C 总线驱动,I2C 总线驱动就是 SOC 的 I2C 控制器驱动,也叫做 I2C 适配器驱动。
②I2C 设备驱动,I2C 设备驱动就是针对具体的 I2C 设备而编写的驱动。

一、I2C 总线驱动

首先来看一下 I2C 总线,在platform中总线是虚拟出来的,目的是实现总线、设备、驱动框架。对于 I2C 而言,不需要虚拟出一条总线,直接使用 I2C总线即可。I2C 总线驱动重点是 I2C 适配器(也就是 SOC 的 I2C 接口控制器)驱动,这里要用到两个重要的数据结构:i2c_adapter 和i2c_algorithm,Linux 内核将 SOC 的 I2C 适配器(控制器) 抽象成 i2c_adapter,i2c_adapter 结构体定义在include/linux/i2c.h 文件中,结构体内容如下:

498 struct i2c_adapter {
499 	struct module *owner;
500 	unsigned int class; /* classes to allow probing for */
501 	const struct i2c_algorithm *algo; /* 总线访问算法 */
502 	void *algo_data;
503     ..................
        ..................
520 };

第 501 行,i2c_algorithm 类型的指针变量 algo,对于一个 I2C 适配器,肯定要对外提供读写 API 函数,设备驱动程序可以使用这些 API 函数来完成读写操作。i2c_algorithm 就是 I2C 适配器与 IIC 设备进行通信的方法。

i2c_algorithm 结构体定义在 include/linux/i2c.h 文件中,内容如下(删除条件编译):

391 struct i2c_algorithm {
			......
398 		int (*master_xfer)(struct i2c_adapter *adap,struct i2c_msg *msgs,
399 		int num);
400 		int (*smbus_xfer) (struct i2c_adapter *adap, u16 addr,
401 		unsigned short flags, char read_write,
402 		u8 command, int size, union i2c_smbus_data *data);
403
404 		/* To determine what the adapter supports */
405 		u32 (*functionality) (struct i2c_adapter *);
			......
411 };

第 398 行,master_xfer 就是 I2C 适配器的传输函数,可以通过此函数来完成与 IIC 设备之间的通信。

第 400 行,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)

这两个函数的区别在于 i2c_add_adapter 使用动态的总线号,而 i2c_add_numbered_adapter使用静态总线号。

函数参数和返回值含义如下:
adapter 或 adap:要添加到 Linux 内核中的 i2c_adapter,也就是 I2C 适配器。
返回值:0,成功;负值,失败。

如果要删除 I2C 适配器的话使用 i2c_del_adapter 函数即可,函数原型如下:

扫描二维码关注公众号,回复: 11264309 查看本文章
void i2c_del_adapter(struct i2c_adapter * adap)

函数参数和返回值含义如下:
adap:要删除的 I2C 适配器。
返回值:无。

一般 SOC 的 I2C 总线驱动都是由半导体厂商编写的,比如 I.MX6U 的 I2C 适配器驱动 NXP 已经编写好了,这个不需要用户去编写。因此 I2C 总线驱动对我们这些 SOC 使用者来说是被屏蔽掉的,我们只要专注于 I2C 设备驱动即可。

二、I2C 设备驱动

I2C 设备驱动重点关注两个数据结构:i2c_client 和 i2c_driver。i2c_client 就是描述设备信息的,i2c_driver 描述驱动内容,类似于 platform_driver。

  • i2c_client 结构体:i2c_client 结构体定义在 include/linux/i2c.h 文件中,内容如下:
217 struct i2c_client {
218 		unsigned short flags; /* 标志 */
219 		unsigned short addr; /* 芯片地址,7 位,存在低 7 位*/
			......
222 		char name[I2C_NAME_SIZE]; /* 名字 */
223 		struct i2c_adapter *adapter; /* 对应的 I2C 适配器 */
224 		struct device dev; /* 设备结构体 */
225 		int irq; /* 中断 */
226 		struct list_head detected;
			......
230 };

一个设备对应一个 i2c_client,每检测到一个 I2C 设备就会给这个 I2C 设备分配一个i2c_client。

  • i2c_driver 结构体:i2c_driver 类似 platform_driver,是我们编写 I2C 设备驱动重点要处理的内容,i2c_driver 结构体定义在 include/linux/i2c.h 文件中,内容如下:
161 struct i2c_driver {
162 		unsigned int class;
163
164 		/* Notifies the driver that a new bus has appeared. You should 
165 		* avoid using this, it will be removed in a near future.
166 		*/
167 		int (*attach_adapter)(struct i2c_adapter *) __deprecated;
168
169 		/* Standard driver model interfaces */
170 		int (*probe)(struct i2c_client *, const struct i2c_device_id *);
171 		int (*remove)(struct i2c_client *);
172
173 		/* driver model interfaces that don't relate to enumeration */
174 		void (*shutdown)(struct i2c_client *);
175
176 		/* Alert callback, for example for the SMBus alert protocol.
177 		* The format and meaning of the data value depends on the 
178 		* protocol.For the SMBus alert protocol, there is a single bit 
179 		* of data passed as the alert response's low bit ("event 
180 		flag"). */
181 		void (*alert)(struct i2c_client *, unsigned int data);
182
183 		/* a ioctl like command that can be used to perform specific 
184 		* functions with the device.
185 		*/
186 		int (*command)(struct i2c_client *client, unsigned int cmd,void *arg);
187
188 		struct device_driver driver;
189 		const struct i2c_device_id *id_table;
190
191 		/* Device detection callback for automatic device creation */
192 		int (*detect)(struct i2c_client *, struct i2c_board_info *);
193 		const unsigned short *address_list;
194 		struct list_head clients;
195 };

第 170 行,当 I2C 设备和驱动匹配成功以后 probe 函数就会执行,和 platform 驱动一样。

第 188 行,device_driver 驱动结构体,如果使用设备树的话,需要设置 device_driver 的of_match_table 成员变量,也就是驱动的兼容(compatible)属性。

第 189 行,id_table 是传统的、未使用设备树的设备匹配 ID 表。

对于我们 I2C 设备驱动编写人来说,重点工作就是构建i2c_driver,构建完成以后需要向Linux 内核注册这个 i2c_driver。i2c_driver 注册函数为 int i2c_register_driver,此函数原型如下:

int i2c_register_driver(struct module *owner, 
 						struct i2c_driver *driver)

函数参数和返回值含义如下:
owner:一般为 THIS_MODULE。
driver:要注册的 i2c_driver。
返回值:0,成功;负值,失败。

另外 i2c_add_driver 也常常用于注册 i2c_driver,i2c_add_driver 是一个宏,定义如下:

587 #define i2c_add_driver(driver)  i2c_register_driver(THIS_MODULE, driver) 

i2c_add_driver 就是对 i2c_register_driver 做了一个简单的封装,只有一个参数,就是要注册的 i2c_driver。

注销 I2C 设备驱动的时候需要将前面注册的 i2c_driver 从 Linux 内核中注销掉,需要用到i2c_del_driver 函数,此函数原型如下:

void i2c_del_driver(struct i2c_driver *driver)

函数参数和返回值含义如下:
driver:要注销的 i2c_driver。
返回值:无。

i2c_driver 的注册示例代码如下:

1 /* i2c 驱动的 probe 函数 */
2 static int xxx_probe(struct i2c_client *client,
					   const struct i2c_device_id *id)
3 { 
4 		/* 函数具体程序 */
5 		return 0; 
6 }
7
8 /* i2c 驱动的 remove 函数 */
9 static int xxx_remove(struct i2c_client *client)
10 {
11 		/* 函数具体程序 */
12 		return 0;
13 }
14
15 		/* 传统匹配方式 ID 列表 */
16 		static const struct i2c_device_id xxx_id[] = {
17 		{"xxx", 0}, 
18 		{}
19 };
21 /* 设备树匹配列表 */
22 static const struct of_device_id xxx_of_match[] = {
23 		{ .compatible = "xxx" },
24 		{ /* Sentinel */ }
25 };
26
27 /* i2c 驱动结构体 */
28 static struct i2c_driver xxx_driver = {
29 		.probe = xxx_probe,
30 		.remove = xxx_remove,
31 		.driver = {
32 			.owner = THIS_MODULE,
33 			.name = "xxx",
34 			.of_match_table = xxx_of_match,
35		 },
36 		.id_table = xxx_id,
37 };
38 
39 /* 驱动入口函数 */
40 static int __init xxx_init(void)
41 {
42 		int ret = 0;
43
44 		ret = i2c_add_driver(&xxx_driver);
45 		return ret;
46 }
47
48 /* 驱动出口函数 */
49 static void __exit xxx_exit(void)
50 {
51 		i2c_del_driver(&xxx_driver);
52 }
53
54 module_init(xxx_init);
55 module_exit(xxx_exit);

第 16~19 行,i2c_device_id,无设备树的时候匹配 ID 表。

第 22~25 行,of_device_id,设备树所使用的匹配表。

第 28~37 行,i2c_driver,当 I2C 设备和 I2C 驱动匹配成功以后 probe 函数就会执行,当注销xxx_driver的时候remove函数会执行。这些和 platform 驱动一样,probe 函数里面基本就是标准的字符设备驱动的初始化和其他一些前期设置。

三、I2C 设备和驱动匹配过程

I2C 设备和驱动的匹配过程是由 I2C 核心来完成的,drivers/i2c/i2c-core.c 就是 I2C 的核心部分,I2C 核心提供了一些与具体硬件无关的 API 函数,比如前面讲过的:

  • 1、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)
  • 2、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 文件,i2c_bus_type 内容如下:

736 struct bus_type i2c_bus_type = {
737 		.name = "i2c",
738 		.match = i2c_device_match,
739 		.probe = i2c_device_probe,
740 		.remove = i2c_device_remove,
741 		.shutdown = i2c_device_shutdown,
742 };

.match 就是 I2C 总线的设备和驱动匹配函数,在这里就是 i2c_device_match 这个函数,此函数内容如下:

457 static int i2c_device_match(struct device *dev, struct device_driver *drv)
458 {
459 		struct i2c_client *client = i2c_verify_client(dev);
460 		struct i2c_driver *driver;
461
462 		if (!client)
463 			return 0;
464
465 		/* Attempt an OF style match */
466 		if (of_driver_match_device(dev, drv))
467 			return 1;
468
469 		/* Then ACPI style match */
470 		if (acpi_driver_match_device(dev, drv))
471 			return 1;
472
473 		driver = to_i2c_driver(drv);
474 		/* match on an id table if there is one */
475 		if (driver->id_table)
476 			return i2c_match_id(driver->id_table, client) != NULL;
477
478 		return 0;
479 }

第 466 行,of_driver_match_device 函数用于完成设备树设备和驱动匹配。比较 I2C 设备节点的 compatible 属性和 of_device_id 中的 compatible 属性是否相等,如果相当的话就表示 I2C设备和驱动匹配。

第 470 行,acpi_driver_match_device 函数用于 ACPI 形式的匹配。

第 476 行,i2c_match_id 函数用于传统的、无设备树的 I2C 设备和驱动匹配过程。比较 I2C设备名字和 i2c_device_id 的 name 字段是否相等,相等的话就说明 I2C 设备和驱动匹配。

当然这一部分是不需要我们去编写的,只是看一下设备和驱动的匹配过程

四、I2C 设备驱动编写流程

1、 设备信息描述

使用设备树的时候 I2C 设备信息通过创建相应的节点就行了,假如开发板在 I2C1 上接了 mag3110 磁力计芯片,因此必须在 i2c1 节点下创建 mag3110 子节点,然后在这个子节点内描述 mag3110 这个芯片的相关信息。如下所示:

1 &i2c1 {
2 		clock-frequency = <100000>;
3 		pinctrl-names = "default"; 
4 		pinctrl-0 = <&pinctrl_i2c1>;
5 		status = "okay"; 
6 
7 		mag3110@0e { 
8 			compatible = "fsl,mag3110"; 
9 			reg = <0x0e>;
10 			position = <2>;
11 		};
......
20 };

第 7~11 行,向 i2c1 添加 mag3110 子节点,第 7 行“mag3110@0e”是子节点名字,“@”后面的“0e”就是 mag3110 的 I2C 器件地址。第 8 行设置 compatible 属性值为“fsl,mag3110”。第 9 行的 reg 属性也是设置 mag3110 的器件地址的,因此值为 0x0e。I2C 设备节点的创建重点是 compatible 属性和 reg 属性的设置,一个用于匹配驱动,一个用于设置器件地址。

2、 设备数据收发处理流程

I2C 设备驱动首先要做的就是初始化 i2c_driver 并向 Linux 内核注册。当设备和驱动匹配以后 i2c_driver 里面的 probe 函数就会执行,probe 函数里面所做的就是字符设备驱动那一套了。一般需要在 probe 函数里面初始化 I2C 设备,要初始化 I2C 设备就必须能够对 I2C 设备寄存器进行读写操作,这里就要用到 i2c_transfer 函数了。i2c_transfer 函数最终会调用 I2C 适配器中 i2c_algorithm 里面的 master_xfer 函数,i2c_transfer 函数原型如下:

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 类型的指针参数,I2C 进行数据收发说白了就是消息的传递,Linux 内核使用 i2c_msg 结构体来描述一个消息。i2c_msg 结构体定义在 include/uapi/linux/i2c.h 文件中,结构体内容如下:

68 struct i2c_msg {
69 		__u16 addr; /* 从机地址 */
70 		__u16 flags; /* 标志 */
71 		#define I2C_M_TEN 0x0010
72		#define I2C_M_RD 0x0001
73 		#define I2C_M_STOP 0x8000
74 		#define I2C_M_NOSTART 0x4000
75 		#define I2C_M_REV_DIR_ADDR 0x2000 
76 		#define I2C_M_IGNORE_NAK 0x1000 
77 		#define I2C_M_NO_RD_ACK 0x0800
78 		#define I2C_M_RECV_LEN 0x0400
79 		__u16 len; /* 消息(本 msg)长度 */
80 		__u8 *buf; /* 消息数据 */
81 };

使用 i2c_transfer 函数发送数据之前要先构建好 i2c_msg,使用 i2c_transfer 进行 I2C 数据收发的示例代码如下:

1 /* 设备结构体 */
2 struct xxx_dev { 
3 		......
4 		void *private_data; /* 私有数据,一般会设置为 i2c_client */
5 };
6 
7 /*
8 * @description : 读取 I2C 设备多个寄存器数据
9 * @param – dev : I2C 设备
10 * @param – reg : 要读取的寄存器首地址
11 * @param – val : 读取到的数据
12 * @param – len : 要读取的数据长度
13 * @return : 操作结果
14 */
15 static int xxx_read_regs(struct xxx_dev *dev, u8 reg, void *val, int len)
16 {
17 		int ret;
18 		struct i2c_msg msg[2];
19 		struct i2c_client *client = (struct i2c_client *) dev->private_data;
20
21 		/* msg[0],第一条写消息,发送要读取的寄存器首地址 */
22 		msg[0].addr = client->addr; /* I2C 器件地址 */
23 		msg[0].flags = 0; /* 标记为发送数据 */
24 		msg[0].buf = &reg; /* 读取的首地址 */
25 		msg[0].len = 1; /* reg 长度 */
26
27 		/* msg[1],第二条读消息,读取寄存器数据 */
28 		msg[1].addr = client->addr; /* I2C 器件地址 */
29 		msg[1].flags = I2C_M_RD; /* 标记为读取数据 */
30 		msg[1].buf = val; /* 读取数据缓冲区 */
31 		msg[1].len = len; /* 要读取的数据长度 */
32
33 	    ret = i2c_transfer(client->adapter, msg, 2);
34 		if(ret == 2) {
35 			ret = 0;
36 		} else {
37 			ret = -EREMOTEIO;
38		}
39 		return ret;
40 }

第2-5行,设备结构体,在设备结构体里面添加一个执行void的指针成员变量private_data,此成员变量用于保存设备的私有数据。在 I2C 设备驱动中我们一般将其指向 I2C 设备对应的i2c_client。 第 15~40 行,xxx_read_regs 函数用于读取 I2C 设备多个寄存器数据。第 18 行定义了一个i2c_msg 数组,2 个数组元素,因为 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 数据读操作。

42 /*
43 * @description : 向 I2C 设备多个寄存器写入数据
44 * @param – dev : 要写入的设备结构体
45 * @param – reg : 要写入的寄存器首地址
46 * @param – val : 要写入的数据缓冲区
47 * @param – len : 要写入的数据长度
48 * @return : 操作结果
49 */
50 static s32 xxx_write_regs(struct xxx_dev *dev, u8 reg, u8 *buf, u8 len)
51 {
52 		u8 b[256];
53 		struct i2c_msg msg;
54 		struct i2c_client *client = (struct i2c_client *) dev->private_data;
55 
56 		b[0] = reg; /* 寄存器首地址 */
57 		memcpy(&b[1],buf,len); /* 将要发送的数据拷贝到数组 b 里面 */
58 
59 		msg.addr = client->addr; /* I2C 器件地址 */
60 		msg.flags = 0; /* 标记为写数据 */
61
62 		msg.buf = b; /* 要发送的数据缓冲区 */
63 		msg.len = len + 1; /* 要发送的数据长度 */
64
65 		return i2c_transfer(client->adapter, &msg, 1);
66 }

第 50~66 行,xxx_write_regs 函数用于向 I2C 设备多个寄存器写数据,I2C 写操作要比读操作简单一点,因此一个 i2c_msg 即可。数组 b 用于存放寄存器首地址和要发送的数据,第 59 行设置 msg 的 addr 为 I2C 器件地址。第 60 行设置 msg 的 flags 为 0,也就是写数据。第 62 行设置要发送的数据,也就是数组 b。第 63 行设置 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 位)类型的数据。
返回值:负值,失败,其他非负值,发送的字节数

猜你喜欢

转载自blog.csdn.net/qq_39507748/article/details/106076776