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;

 

转载于:https://www.cnblogs.com/hei-da-mi/p/9111112.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值