定义regmap结构体
struct regmap *regmap;
定义regmap_config结构体
struct regmap_config regmap_config;
定义字符设备操作集
static struct file_operations lm75_dev_fops = {
.owner = THIS_MODULE;
.open = lm75_open,
.read = lm75_read,
.write = lm75_wirte,
.release = lm75_release,
};
probe函数中初始化regmap、regmap_config
regmap_config.reg_bits = 8;
regmap_config.val_bits = 16;
regmap_config.use_single_rw = true;
regmap_config.max_register = 0x03,
regmap_config.val_format_endian = REGMAP_ENDIAN_BIG,
regmap_config.cache_type = REGCACHE_RBTREE,
regmap_config.use_single_rw = true,
regmap = regmap_init_i2c(client,®map_config);
由于lm75 temp寄存器数据为16位,val_bits等于16,八位以上寄存器数据需要指定大小端,val_format_endian=REGMAP_ENDIAN_BIG
字符操作集读取函数
static ssize_t lm75_read(struct file *filp,char __user *buf,size_t cnt,loff_t *off){
unsigned int read_data;
long temp_out;
s16 temp;
int ret;
int err = regmap_read(regmap,LM75_REG_TEMP, &read_data);
if (err < 0)
return err;
temp = (s16)read_data;
ret = copy_to_user(buf,&temp,sizeof(temp));
return 0;
}
读取LM75温度寄存器数据上报用户空间
总结
具体驱动程序可参考/driver/hwmon/lm75.c