VS-RK3399 开发板上有 9 个片上 I2C 控制器,各个 I2C 的使用情况如下表:
Port | Pin name | Device | I2C0 | GPIO1_B7/SPI3_RXD/I2C0_SDA
GPIO1_C0/SPI3_TXD/I2C0_SCL | RK808 | I2C1 | GPIO4_A1/I2C1_SDA
GPIO4_A2/I2C1_SCL | ALC5640
CAMERA | I2C2 | GPIO2_A0/VOP_D0/CIF_D0/I2C2_SDA
GPIO2_A1/VOP_D1/CIF_D1/I2C2_SCL | 复用为其他功能 | I2C3 | GPIO4_C0/I2C3_SDA/UART2B_RX
GPIO4_C1/I2C3_SCL/UART2B_TX | HDMI | I2C4 | GPIO1_B3/I2C4_SDA
GPIO1_B4/I2C4_SCL | FUSB302B
TP | I2C5 | GPIO3_B2/MAC_RXER/I2C5_SDA
GPIO3_B3/MAC_CLK/I2C5_SCL | 复用为其他功能 | I2C6 | GPIO2_B1/SPI2_RXD/CIF_HREF/I2C6_SDA
GPIO2_B2/SPI2_TXD/CIF_CLKIN/I2C6_SCL | 复用为其他功能 | I2C7 | GPIO2_A7/VOP_D7/CIF_D7/I2C7_SDA
GPIO2_B0/VOP_CLK/CIF_VSYNC/I2C7_SCL | 复用为其他功能 | I2C8 | GPIO1_C4/I2C8_SDA
GPIO1_C5/I2C8_SCL | 复用为其他功能
|
本文主要描述如何在该开发板上配置 I2C。 配置 I2C 可分为两大步骤: 下面以配置 GSL3680 为例。 定义和注册 I2C 设备在注册I2C设备时,需要结构体 i2c_client 来描述 I2C 设备。然而在标准Linux中,用户只需要提供相应的 I2C 设备信息,Linux就会根据所提供的信息构造 i2c_client 结构体。 用户所提供的 I2C 设备信息以节点的形式写到 dts 文件中,如下所示: kernel/arch/arm64/boot/dts/rockchip/rk3399-videostrong-board-mipi.dts&i2c4 { status = "okay";
gsl3680: gsl3680@41 {
compatible = "gslX680";
reg = <0x41>;
screen_max_x = <1536>;
screen_max_y = <2048>;
touch-gpio = <&gpio1 20 IRQ_TYPE_LEVEL_LOW>;
reset-gpio = <&gpio0 12 GPIO_ACTIVE_HIGH>;
}; };
定义和注册 I2C 驱动定义 I2C 驱动在定义 I2C 驱动之前,用户首先要定义变量 of_device_id 和 i2c_device_id 。 of_device_id 用于在驱动中调用dts文件中定义的设备信息,其定义如下所示: static struct of_device_id gsl_ts_ids[] = { {.compatible = "gslX680"}, {} };
定义变量 i2c_device_id: static const struct i2c_device_id gsl_ts_id[] = {
{GSLX680_I2C_NAME, 0},
{}
};
MODULE_DEVICE_TABLE(i2c, gsl_ts_id);
i2c_driver 如下所示: static struct i2c_driver gsl_ts_driver = {
.driver = {
.name = GSLX680_I2C_NAME,
.owner = THIS_MODULE,
.of_match_table = of_match_ptr(gsl_ts_ids),
},
.probe = gsl_ts_probe,
.remove = gsl_ts_remove,
.id_table = gsl_ts_id,
};
注:变量id_table指示该驱动所支持的设备。
注册 I2C 驱动使用i2c_add_driver函数注册 I2C 驱动。 i2c_add_driver(&gsl_ts_driver);在调用 i2c_add_driver 注册 I2C 驱动时,会遍历 I2C 设备,如果该驱动支持所遍历到的设备,则会调用该驱动的 probe 函数。 通过 I2C 收发数据在注册好 I2C 驱动后,即可进行 I2C 通讯。 int i2c_master_send(const struct i2c_client *client, const char *buf, int count) {
int ret; struct i2c_adapter *adap = client->adapter;
struct i2c_msg msg; msg.addr = client->addr;
msg.flags = client->flags & I2C_M_TEN;
msg.len = count; msg.buf = (char *)buf;
ret = i2c_transfer(adap, &msg, 1);
/* * If everything went ok (i.e. 1 msg transmitted), return #bytes * transmitted, else error code. */
return (ret == 1) ? count : ret;
}
int i2c_master_recv(const struct i2c_client *client, char *buf, int count) {
struct i2c_adapter *adap = client->adapter;
struct i2c_msg msg;
int ret;
msg.addr = client->addr;
msg.flags = client->flags & I2C_M_TEN;
msg.flags |= I2C_M_RD;
msg.len = count;
msg.buf = buf;
ret = i2c_transfer(adap, &msg, 1); /* * If everything went ok (i.e. 1 msg received), return #bytes received, * else error code. */ return (ret == 1) ? count : ret; }
EXPORT_SYMBOL(i2c_master_recv);
|