本文转载自:http://blog.csdn.net/kris_fei/article/details/71515020
Platform: Rockchip OS: Android 6.0 Kernel: 3.10.92
rk在驱动层做了一个通用i2c测试代码提供给上层快速测试i2c外设是否传输正常.
测试使用方法: #echo [0-5] > /dev/i2c_detect //0-5表示i2c number号,不过i2c5需要修改下驱动,默认只支持到i2c4. 例如我的i2c2接的是audio codec:
- &i2c2 {
- status = "okay";
- rt5631: rt5631@1a {
- compatible = "rt5631";
- reg = <0x1a>;
- };
- };
root@rk3288:/ # echo 2 > /dev/i2c_detect kernel log出打印: I2c2 slave list: 0x1a 而audio codec的地址就是0x1a. 驱动关键点说明: kernel/drivers/i2c/buses/i2c-rockchip.c:
- static ssize_t i2c_detect_write(struct file *file,
- const char __user *buf, size_t count, loff_t *offset)
- {
- char nr_buf[8];
- int nr = 0, ret;
-
- if (count > 4)
- return -EFAULT;
- ret = copy_from_user(nr_buf, buf, count);
- if (ret < 0)
- return -EFAULT;
-
- sscanf(nr_buf, "%d", &nr);
-
- if (nr >= 5 || nr < 0)
- return -EFAULT;
-
- slave_detect(nr);
-
- return count;
- }
-
- static void slave_detect(int nr)
- {
- int ret = 0;
- unsigned short addr;
- char val[8];
- char buf[6 * 0x80 + 20];
- struct i2c_client client;
-
- memset(buf, 0, 6 * 0x80 + 20);
-
- sprintf(buf, "I2c%d slave list: ", nr);
- do {
-
- for (addr = 0x01; addr < 0x80; addr++) {
- detect_set_client(&client, addr, nr);
-
- ret = detect_read(&client, val, 1);
- if (ret > 0)
- sprintf(buf, "%s 0x%02x", buf, addr);
- }
-
- printk("%s\n", buf);
- }
- while (0);
- }
-
- static int detect_read(struct i2c_client *client, char *buf, int len)
- {
- struct i2c_msg msg;
-
- msg.addr = client->addr;
- msg.flags = client->flags | I2C_M_RD;
- msg.buf = buf;
- msg.len = len;
-
- #ifdef CONFIG_I2C_ROCKCHIP_COMPAT
- msg.scl_rate = 100 * 1000;
- #endif
-
- return i2c_transfer(client->adapter, &msg, 1);
- }
|
请发表评论