IIC 设备Dump 寄存器方法 (kernel 4.4)

废话不多说,直接上代码:

1.添加IIC读写寄存器方法:


static struct class *cls = NULL;

static struct i2c_client *rt5640_i2c_g = NULL;


#define
XYP_IIC_ADDR_LENGTH 1 s32 xyp_i2c_read(struct i2c_client *client, u8 *buf, s32 len) { struct i2c_msg msgs[2]; s32 ret=-1; s32 retries = 0; msgs[0].flags = !I2C_M_RD; msgs[0].addr = client->addr; msgs[0].len = XYP_IIC_ADDR_LENGTH; msgs[0].buf = &buf[0]; #ifdef CONFIG_I2C_ROCKCHIP_COMPAT msgs[0].scl_rate=200 * 1000; #endif msgs[1].flags = I2C_M_RD; msgs[1].addr = client->addr; msgs[1].len = len - XYP_IIC_ADDR_LENGTH; msgs[1].buf = &buf[XYP_IIC_ADDR_LENGTH]; #ifdef CONFIG_I2C_ROCKCHIP_COMPAT msgs[1].scl_rate=200 * 1000; #endif while(retries < 5) { ret = i2c_transfer(client->adapter, msgs, 2); if(ret == 2)break; retries++; } return ret; } s32 xyp_i2c_write(struct i2c_client *client,u8 *buf,s32 len) { struct i2c_msg msg; s32 ret = -1; s32 retries = 0; msg.flags = !I2C_M_RD; msg.addr = client->addr; msg.len = len; msg.buf = buf; #ifdef CONFIG_I2C_ROCKCHIP_COMPAT msg.scl_rate=200 * 1000; #endif while(retries < 5) { ret = i2c_transfer(client->adapter, &msg, 1); if (ret == 1)break; retries++; } return ret; } static ssize_t show_rt5640_reg( struct device *dev, struct device_attribute *attr, char *buf) { PAGE_SIZE; int i = 0, count = 0; u8 t_buf[4] = {0}; for(i = 0; i < 0xff; i++) { t_buf[0] = i; xyp_i2c_read(rt5640_i2c_g, t_buf, 3); printk("XYP_DEBUG read 0x%02x = 0x%02x \n", i, t_buf[1]); count += sprintf(buf + count, "0x%02x = 0x%04x\n", i, t_buf[1] << 8 | t_buf[2]); } return count; } static ssize_t store_rt5640_reg(struct class *dev, struct class_attribute *attr, const char *buf, size_t count) { int reg, value, ret; u8 t_buf[4] = {0}; sscanf(buf, "0x%x = 0x%x", &reg, &value); printk("XYP_DEBUG reg = 0x%02x, value = 0x%02x \n", reg, value); t_buf[0] = reg; t_buf[1] = (value & 0xff00) >> 8; t_buf[2] = value & 0xff; xyp_i2c_write(rt5640_i2c_g, t_buf, 3); return count; } static struct class_attribute attr[] = { __ATTR(rt5640_debug_reg, 0770, show_rt5640_reg, store_rt5640_reg), __ATTR_NULL, };

2、在probe函数中创建/sys/class下的节点,并且创建相应的属性文件。

cls = class_create(THIS_MODULE, "XYP_DEBUG_CODEC");
class_create_file(cls, attr);
rt5640_i2c_g = i2c;

 

posted on 2018-05-30 15:23  黑大米  阅读(1504)  评论(0编辑  收藏  举报

导航