在上一节中我们了解了I2C框架分为I2C核心、I2C总线驱动和I2C设备驱动三部分。其中I2C总线驱动就是SOC的I2C控制器驱动,一般来说都是SOC厂家实现好的。而I2C设备驱动是用户根据自己不同的设备具体实现的。
在imx6ull平台下,NXP官方已经实现好了I2C总线驱动,下面简单分析一下。
首先要找到设备树中关于I2C控制器的设备节点。打开imx6ull.dtsi文件,有如下内容:
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属性值"fsl,imx6ul-i2c"和"fsl,imx21-i2c",我们就可以在内核中找到相应的驱动文件,在Linux内核源码中搜索这两个字符串,可以找到驱动文件为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