[RK3288][Android6.0] 调试笔记 --- 测试I2C设备正常传输方法

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;
    /*只支持到i2c4, 如果要支持i2c5,那么要改成5.*/
    if (count > 4)
        return -EFAULT;
    ret = copy_from_user(nr_buf, buf, count);
    if (ret < 0)
        return -EFAULT;

    sscanf(nr_buf, "%d", &nr);
    /*这里得改成6. */
    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 {
        /*扫描0x01~0x80地址范围的设备.*/
        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;
    /*以100kHz的速率读取*/
#ifdef CONFIG_I2C_ROCKCHIP_COMPAT
    msg.scl_rate = 100 * 1000;
#endif

    return i2c_transfer(client->adapter, &msg, 1);
}

你可能感兴趣的:(子类__I2C)