1)实验平台:正原子阿尔法Linux开发板 2)平台采购地址:https://item.taobao.com/item.htm?id=603672744434 2)全套实验源码 手册 视频下载地址:http://www.openedv.com/thread-300792-1-1.html 3)正点原子Linux感兴趣的学生可以加入小组讨论:935446741 4)关注正点原子微信官方账号,获取最新信息更新
第六十一章 Linux I2C驱动实验
I2C它是连接各种外设、传感器等设备的常用串行通信接口,在裸机部分已经对齐I.MX6U的I2C接口详细说明。让我们学习如何学习这一章。Linux下开发I2C接口设备驱动的重点是学习Linux下的I2C驱动框架,按指定框架编写I2C设备驱动。本章同样以I.MX6U-ALPHA开发板上的AP3216C以三合一环境光传感器为例AP3216C解释怎么写Linux下的I2C设备驱动程序。
61.1 Linux I2C驱动框架简介 回想一下我们是如何在裸机中编写的AP3216C我们编写了四份驱动文件:bsp_i2c.c、bsp_i2c.h、bsp_ap3216c.c和bsp_ap3216c.h。前两个是I.MX6U的IIC接口驱动,后两个文件是AP3216C这个I2C设备驱动文件。驱动相当于两部分: ①、I2C主机驱动。 ②、I2C设备驱动。 对于I2C主机驱动,一旦编写完成,就不需要修改,其他的I2C设备直接调用主机驱动提供的设备API函数可以完成读写操作。这正好符合Linux的驱动分离与分层的思想,因此Linux内核也将I2C驱动分为两部分: ①、I2C总线驱动,I2C总线驱动是SOC的I2C又称控制器驱动I2C适配器驱动。 ②、I2C设备驱动,I2C设备驱动是针对特定的I2C由设备编写的驱动。 61.1.1 I2C总线驱动 先来看看I2C总线,在讲platform说的时候,platform为了实现总线、设备和驱动框架,虚拟总线。对于I2C直接使用不需要虚拟总线I2C总线即可。I2C总线驱动的重点是I2C适配器(即SOC的I2C两个重要的数据结构用于接口控制器驱动:i2c_adapter和i2c_algorithm,Linux内核将SOC的I2C适配器(控制器)抽象成i2c_adapter,i2c_adapter结构体定义在include/linux/i2c.h结构体内容如下:
示例代码61.1.1.1 i2c_adapter结构体 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 504 /* data fields that are valid for all devices */ 505 struct rt_mutex bus_lock; 506 507 int timeout; /* in jiffies */ 508 int retries; 509 struct device dev; /* the adapter device */ 510 511 int nr; 512 char name[48]; 513 struct completion dev_released; 514 515 struct mutex userspace_clients_lock; 516 struct list_head userspace_clients; 517 518 struct i2c_bus_recovery_info *bus_recovery_info;
519 const struct i2c_adapter_quirks *quirks;
520 };
第501行,i2c_algorithm类型的指针变量algo,对于一个I2C适配器,肯定要对外提供读写API函数,设备驱动程序可以使用这些API函数来完成读写操作。i2c_algorithm就是I2C适配器与IIC设备进行通信的方法。
i2c_algorithm结构体定义在include/linux/i2c.h文件中,内容如下(删除条件编译):
示例代码61.1.1.2 i2c_algorithm结构体
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函数即可,函数原型如下: void i2c_del_adapter(struct i2c_adapter * adap) 函数参数和返回值含义如下: adap:要删除的I2C适配器。 返回值:无。 关于I2C的总线(控制器或适配器)驱动就讲解到这里,一般SOC的I2C总线驱动都是由半导体厂商编写的,比如I.MX6U的I2C适配器驱动NXP已经编写好了,这个不需要用户去编写。因此I2C总线驱动对我们这些SOC使用者来说是被屏蔽掉的,我们只要专注于I2C设备驱动即可。除非你是在半导体公司上班,工作内容就是写I2C适配器驱动。 61.1.2 I2C设备驱动 I2C设备驱动重点关注两个数据结构:i2c_client和i2c_driver,根据总线、设备和驱动模型,I2C总线上一小节已经讲了。还剩下设备和驱动,i2c_client就是描述设备信息的,i2c_driver描述驱动内容,类似于platform_driver。 1、i2c_client结构体 i2c_client结构体定义在include/linux/i2c.h文件中,内容如下:
示例代码61.1.2.1 i2c_client结构体
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。
2、i2c_driver结构体
i2c_driver类似platform_driver,是我们编写I2C设备驱动重点要处理的内容,i2c_driver结构体定义在include/linux/i2c.h文件中,内容如下:
```c
示例代码61.1.2.2 i2c_driver结构体
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是一个宏,定义如下:
```c
示例代码61.1.2.3 i2c_add_driver宏
587 #define i2c_add_driver(driver) \
588 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的注册示例代码如下:
示例代码61.1.2.4 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 };
20
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函数就会执行,这些和platform驱动一样,probe函数里面基本就是标准的字符设备驱动那一套了。
61.1.3 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内容如下:
示例代码61.1.2.5 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这个函数,此函数内容如下:
示例代码61.1.2.6 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设备和驱动匹配。
61.2 I.MX6U的I2C适配器驱动分析 上一小节我们讲解了Linux下的I2C驱动框架,重点分为I2C适配器驱动和I2C设备驱动,其中I2C适配器驱动就是SOC的I2C控制器驱动。I2C设备驱动是需要用户根据不同的I2C设备去编写,而I2C适配器驱动一般都是SOC厂商去编写的,比如NXP就编写好了I.MX6U的I2C适配器驱动。在imx6ull.dtsi文件中找到I.MX6U的I2C1控制器节点,节点内容如下所示:
示例代码61.2.1 I2C1控制器节点
1 i2c1: i2c@021a0000 {
2 #address-cells = <1>;
3 #size-cells = <0>;
4 compatible = "fsl,imx6ul-i2c", "fsl,imx21-i2c";
5 reg = <0x021a0000 0x4000>;
6 interrupts = <GIC_SPI 36 IRQ_TYPE_LEVEL_HIGH>;
7 clocks = <&clks IMX6UL_CLK_I2C1>;
8 status = "disabled";
9 };
重点关注i2c1节点的compatible属性值,因为通过compatible属性值可以在Linux源码里面找到对应的驱动文件。这里i2c1节点的compatible属性值有两个:“fsl,imx6ul-i2c”和“fsl,imx21-i2c”,在Linux源码中搜索这两个字符串即可找到对应的驱动文件。I.MX6U的I2C适配器驱动驱动文件为drivers/i2c/busses/i2c-imx.c,在此文件中有如下内容:
示例代码61.2.2 i2c-imx.c文件代码段
244 static struct platform_device_id imx_i2c_devtype[] = {
245 {
246 .name = "imx1-i2c",
247 .driver_data = (kernel_ulong_t)&imx1_i2c_hwdata,
248 }, {
249 .name = "imx21-i2c",
250 .driver_data = (kernel_ulong_t)&imx21_i2c_hwdata,
251 }, {
252 /* sentinel */
253 }
254 };
255 MODULE_DEVICE_TABLE(platform, imx_i2c_devtype);
256
257 static const struct of_device_id i2c_imx_dt_ids[] = {
258 {
.compatible = "fsl,imx1-i2c", .data = &imx1_i2c_hwdata, },
259 {
.compatible = "fsl,imx21-i2c", .data = &imx21_i2c_hwdata, },
260 {
.compatible = "fsl,vf610-i2c", .data = &vf610_i2c_hwdata, },
261 {
/* sentinel */ }
262 };
263 MODULE_DEVICE_TABLE(of, i2c_imx_dt_ids);
......
1119 static struct platform_driver i2c_imx_driver = {
1120 .probe = i2c_imx_probe,
1121 .remove = i2c_imx_remove,
1122 .driver = {
1123 .name = DRIVER_NAME,
1124 .owner = THIS_MODULE,
1125 .of_match_table = i2c_imx_dt_ids,
1126 .pm = IMX_I2C_PM,
1127 },
1128 .id_table = imx_i2c_devtype,
1129 };
1130
1131 static int __init i2c_adap_imx_init(void)
1132 {
1133 return platform_driver_register(&i2c_imx_driver);
1134 }
1135 subsys_initcall(i2c_adap_imx_init);
1136
1137 static void __exit i2c_adap_imx_exit(void)
1138 {
1139 platform_driver_unregister(&i2c_imx_driver);
1140 }
1141 module_exit(i2c_adap_imx_exit);
从示例代码61.2.2可以看出,I.MX6U的I2C适配器驱动是个标准的platform驱动,由此可以看出,虽然I2C总线为别的设备提供了一种总线驱动框架,但是I2C适配器却是platform驱动。就像你的部门老大是你的领导,你是他的下属,但是放到整个公司,你的部门老大却也是老板的下属。
第259行,“fsl,imx21-i2c”属性值,设备树中 i2c1节点的compatible属性值就是与此匹配上的。因此i2c-imx.c文件就是I.MX6U的I2C适配器驱动文件。
第1120行,当设备和驱动匹配成功以后i2c_imx_probe函数就会执行,i2c_imx_probe函数就会完成I2C适配器初始化工作。
i2c_imx_probe函数内容如下所示(有省略):
示例代码61.2.3 i2c_imx_probe函数代码段
971 static int i2c_imx_probe(struct platform_device *pdev)
972 {
973 const struct of_device_id *of_id =
974 of_match_device(i2c_imx_dt_ids, &pdev->dev);
975 struct imx_i2c_struct *i2c_imx;
976 struct resource *res;
977 struct imxi2c_platform_data *pdata =
dev_get_platdata(&pdev->dev);
978 void __iomem *base;
979 int irq, ret;
980 dma_addr_t phy_addr;
981
982 dev_dbg(&pdev->dev, "<%s>\n", __func__);
983
984 irq = platform_get_irq(pdev, 0);
......
990 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
991 base = devm_ioremap_resource(&pdev->dev, res);
992 if (IS_ERR(base))
993 return PTR_ERR(base);
994
995 phy_addr = (dma_addr_t)res->start;
996 i2c_imx = devm_kzalloc(&pdev->dev, sizeof(*i2c_imx),
GFP_KERNEL);
997 if (!i2c_imx)
998 return -ENOMEM;
999
1000 if (of_id)
1001 i2c_imx->hwdata = of_id->data;
1002 else
1003 i2c_imx->hwdata = (struct imx_i2c_hwdata *)
1004 platform_get_device_id(pdev)->driver_data;
1005
1006 /* Setup i2c_imx driver structure */
1007 strlcpy(i2c_imx->adapter.name, pdev->name,
sizeof(i2c_imx->adapter.name));
1008 i2c_imx->adapter.owner = THIS_MODULE;
1009 i2c_imx->adapter.algo = &i2c_imx_algo;
1010 i2c_imx->adapter.dev.parent = &pdev->dev;
1011 i2c_imx->adapter.nr = pdev->id;
1012 i2c_imx->adapter.dev.of_node = pdev->dev.of_node;
1013 i2c_imx->base = base;
1014
1015 /* Get I2C clock */
1016 i2c_imx->clk =