锐单电子商城 , 一站式电子元器件采购平台!
  • 电话:400-990-0325

Linux I2C 驱动实验

时间:2023-06-05 08:37:00 991b传感器传感器1130

目录

  • Linux I2C 驱动框架简介
    • I2C 总线驱动
    • I2C 设备驱动
    • I2C 设备与驱动匹配过程
  • I.MX6U 的I2C 驱动分析适配器
  • I2C 设备驱动编写过程
    • I2C 设备信息描述
    • I2C 设备数据收发处理流程
  • 硬件原理图分析
  • 编写实验程序
    • 修改设备树
    • AP3216C 驱动编写
    • 编写测试APP
  • 运行测试
    • 编译驱动程序和测试APP
    • 运行测试

I2C 它是连接各种外设、传感器等设备的常用串行通信接口,在裸机部分已经对齐I.MX6U 的I2C 接口做了详细的讲解。让我们学习如何学习这一章。Linux 下开发I2C 接口设备驱动的重点是学习Linux 下的I2C 驱动框架,按指定框架编写I2C 设备驱动。本章同样以I.MX6U-ALPHA 开发板上的AP3216C 以三合一环境光传感器为例AP3216C 解释怎么写Linux 下的I2C 设备驱动程序。

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 由设备编写的驱动。

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 结构体内容如下:

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 文件中,内容如下(删除条件编译):

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 适配器驱动。

I2C 设备驱动

I2C 设备驱动重点关注两个数据结构:i2c_client 和i2c_driver,根据总线、设备和驱动模型,I2C 总线上一小节已经讲了。还剩下设备和驱动,i2c_client 就是描述设备信息的,i2c_driver 描述驱动内容,类似于platform_driver。
1、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。
2、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) \
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 的注册示例代码如下:

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 函数里面基本就是标准的字符设备驱动那一套了。

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 设备和驱动匹配。

I.MX6U 的I2C 适配器驱动分析

上一小节我们讲解了Linux 下的I2C 驱动框架,重点分为I2C 适配器驱动和I2C 设备驱动,其中I2C 适配器驱动就是SOC 的I2C 控制器驱动。I2C 设备驱动是需要用户根据不同的I2C 设备去编写,而I2C 适配器驱动一般都是SOC 厂商去编写的,比如NXP 就编写好了I.MX6U 的I2C 适配器驱动。在imx6ull.dtsi 文件中找到I.MX6U 的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,在此文件中有如下内容:

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 函数内容如下所示(有省略):

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 

相关文章