10、Regmap 介绍
在Linux内核中,原本通过API函数直接操作I2C设备寄存器时,会导致代码里出现大量重复和冗余的部分。虽然这些操作本质上都是对寄存器的读写,但零散的代码管理起来很麻烦。为此,内核引入了Regmap子系统,它能帮助开发者更方便、统一地操作I2C设备的寄存器。接下来,我们就来学习如何用Regmap的API函数来读写寄存器。
一、什么是Regmap
Linux系统中,大部分硬件设备的驱动开发都离不开对芯片内部寄存器的控制。比如I2C或SPI设备,本质上都是通过对应的接口读写芯片内部寄存器来工作的。无论是RK3568这类处理器的PWM定时器等外设,最终都要通过设置寄存器来完成初始化。
过去,开发者需要针对不同接口(如I2C用i2c_transfer函数,SPI用spi_write/spi_read函数)单独编写代码,导致大量重复代码出现,而且代码难以复用。为解决这个问题,Linux内核引入了regmap机制。
regmap的作用是:
- 统一接口:开发者只需用regmap提供的函数操作寄存器,无需关心是I2C还是SPI接口
- 自动优化:内置缓存功能,减少对硬件的实际读写次数(比如合并连续操作)
- 提升效率:通过减少低速总线的通信次数,加快整体操作速度
- 保证兼容:无论是芯片外设还是处理器内部寄存器,都能用同一套接口操作
这样做的好处是:
- 驱动代码更简洁,减少重复劳动
- 新设备驱动开发更方便
- 代码更容易在不同硬件平台间移植
需要注意的是,虽然regmap提升了效率,但因为使用了缓存机制,可能会让寄存器操作的实时性稍有降低。不过这对大多数普通设备来说影响不大。
二、Regmap的适用场景
当需要通过I2C、SPI等接口读写设备内部寄存器,或者操作芯片内部寄存器时,就会用到regmap。
使用它的主要好处是:
- 统一操作方式:让不同硬件的寄存器访问代码更规范,方便复用和维护,开发更简单。
- 提升效率:自动合并多次寄存器操作,减少对硬件的实际读写次数,加快访问速度。
三、Regmap驱动框架
regmap 驱动框架如下图所示:
regmap框架分为三个简单层级:
- 底层总线层:regmap统一处理不同硬件连接方式,目前支持I2C、SPI、MMIO等十多种通信总线类型。
- 核心处理层:这是regmap的中间核心,负责协调不同总线的通信,开发者无需深入理解其内部原理。
- 接口层:regmap为开发者提供的简单易用接口,通过这些标准化函数就能直接操作芯片寄存器,是开发者实际编写驱动时需要重点掌握的部分。
四、Regmap相关结构体
4.1、regmap 结 构 体
Linux 内 核 将 regmap 框 架 抽 象 为 regmap 结 构 体 , 这 个 结 构 体 定 义 在 文 件 drivers/base/regmap/internal.h 中
struct regmap {
union {
struct mutex mutex;
struct {
spinlock_t spinlock;
unsigned long spinlock_flags;
};
};
regmap_lock lock;
regmap_unlock unlock;
void *lock_arg; /* This is passed to lock/unlock functions */
gfp_t alloc_flags;
struct device *dev; /* Device we do I/O on */
void *work_buf; /* Scratch buffer used to format I/O */
struct regmap_format format; /* Buffer format */
const struct regmap_bus *bus;
void *bus_context;
const char *name;
bool async;
spinlock_t async_lock;
wait_queue_head_t async_waitq;
struct list_head async_list;
struct list_head async_free;
int async_ret;
#ifdef CONFIG_DEBUG_FS
bool debugfs_disable;
struct dentry *debugfs;
const char *debugfs_name;
unsigned int debugfs_reg_len;
unsigned int debugfs_val_len;
unsigned int debugfs_tot_len;
struct list_head debugfs_off_cache;
struct mutex cache_lock;
#endif
unsigned int max_register;
bool (*writeable_reg)(struct device *dev, unsigned int reg);
bool (*readable_reg)(struct device *dev, unsigned int reg);
bool (*volatile_reg)(struct device *dev, unsigned int reg);
bool (*precious_reg)(struct device *dev, unsigned int reg);
bool (*writeable_noinc_reg)(struct device *dev, unsigned int reg);
bool (*readable_noinc_reg)(struct device *dev, unsigned int reg);
const struct regmap_access_table *wr_table;
const struct regmap_access_table *rd_table;
const struct regmap_access_table *volatile_table;
const struct regmap_access_table *precious_table;
const struct regmap_access_table *wr_noinc_table;
const struct regmap_access_table *rd_noinc_table;
int (*reg_read)(void *context, unsigned int reg, unsigned int *val);
int (*reg_write)(void *context, unsigned int reg, unsigned int val);
int (*reg_update_bits)(void *context, unsigned int reg,
unsigned int mask, unsigned int val);
bool defer_caching;
unsigned long read_flag_mask;
unsigned long write_flag_mask;
/* number of bits to (left) shift the reg value when formatting*/
int reg_shift;
int reg_stride;
int reg_stride_order;
/* regcache specific members */
const struct regcache_ops *cache_ops;
enum regcache_type cache_type;
/* number of bytes in reg_defaults_raw */
unsigned int cache_size_raw;
/* number of bytes per word in reg_defaults_raw */
unsigned int cache_word_size;
/* number of entries in reg_defaults */
unsigned int num_reg_defaults;
/* number of entries in reg_defaults_raw */
unsigned int num_reg_defaults_raw;
/* if set, only the cache is modified not the HW */
bool cache_only;
/* if set, only the HW is modified not the cache */
bool cache_bypass;
/* if set, remember to free reg_defaults_raw */
bool cache_free;
struct reg_default *reg_defaults;
const void *reg_defaults_raw;
void *cache;
/* if set, the cache contains newer data than the HW */
bool cache_dirty;
/* if set, the HW registers are known to match map->reg_defaults */
bool no_sync_defaults;
struct reg_sequence *patch;
int patch_regs;
/* if set, converts bulk read to single read */
bool use_single_read;
/* if set, converts bulk write to single write */
bool use_single_write;
/* if set, the device supports multi write mode */
bool can_multi_write;
/* if set, raw reads/writes are limited to this size */
size_t max_raw_read;
size_t max_raw_write;
struct rb_root range_tree;
void *selector_work_buf; /* Scratch buffer used for selector */
struct hwspinlock *hwlock;
/* if set, the regmap core can sleep */
bool can_sleep;
};
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
4.2、regmap_config 结构体
regmap_config 结构体就是用来初始化 regmap 的,这个结构体定义在include/linux/regmap.h 文件中,结构体内容如下:
struct regmap_config {
const char *name;
int reg_bits;
int reg_stride;
int pad_bits;
int val_bits;
bool (*writeable_reg)(struct device *dev, unsigned int reg);
bool (*readable_reg)(struct device *dev, unsigned int reg);
bool (*volatile_reg)(struct device *dev, unsigned int reg);
bool (*precious_reg)(struct device *dev, unsigned int reg);
bool (*writeable_noinc_reg)(struct device *dev, unsigned int reg);
bool (*readable_noinc_reg)(struct device *dev, unsigned int reg);
bool disable_locking;
regmap_lock lock;
regmap_unlock unlock;
void *lock_arg;
int (*reg_read)(void *context, unsigned int reg, unsigned int *val);
int (*reg_write)(void *context, unsigned int reg, unsigned int val);
bool fast_io;
unsigned int max_register;
const struct regmap_access_table *wr_table;
const struct regmap_access_table *rd_table;
const struct regmap_access_table *volatile_table;
const struct regmap_access_table *precious_table;
const struct regmap_access_table *wr_noinc_table;
const struct regmap_access_table *rd_noinc_table;
const struct reg_default *reg_defaults;
unsigned int num_reg_defaults;
enum regcache_type cache_type;
const void *reg_defaults_raw;
unsigned int num_reg_defaults_raw;
unsigned long read_flag_mask;
unsigned long write_flag_mask;
bool zero_flag_mask;
bool use_single_read;
bool use_single_write;
bool can_multi_write;
enum regmap_endian reg_format_endian;
enum regmap_endian val_format_endian;
const struct regmap_range_cfg *ranges;
unsigned int num_ranges;
bool use_hwlock;
unsigned int hwlock_id;
unsigned int hwlock_mode;
bool can_sleep;
};
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
五、Regmap操作函数
5.1、regmap的申请与初始化
regmap能通过I2C、SPI等多种总线操作设备寄存器。以I2C为例,初始化时需要调用专用函数。
Linux内核提供了一个I2C专用的regmap初始化函数:devm_regmap_init_i2c
。它的作用是:
- 根据I2C设备信息和寄存器配置,创建并管理regmap对象
- 自动处理资源释放(无需手动注销)
函数使用方法:
struct regmap *map = devm_regmap_init_i2c(i2c_client, config);
参数说明:
i2c_client
:I2C设备的客户端信息config
:寄存器配置参数(如寄存器宽度、地址偏移等)
5.2、寄存器读写核心函数
不管设备是通过I2C/SPI连接,还是内部寄存器,regmap都提供了两个最基础的操作函数:
1. 读寄存器:regmap_read()
int regmap_read(struct regmap *map, unsigned int reg_addr, unsigned int *value);
作用:从指定寄存器地址读取数据到value
变量 参数:
map
:之前创建的regmap对象reg_addr
:要读取的寄存器地址value
:存放读取结果的变量 返回值:0表示成功,非0表示失败
2. 写寄存器:regmap_write()
int regmap_write(struct regmap *map, unsigned int reg_addr, unsigned int value);
作用:将value
的值写入指定寄存器地址 参数:
map
:regmap对象reg_addr
:要写入的寄存器地址value
:要写入的寄存器值 返回值:0表示成功,非0表示失败
使用流程总结:
- 用
devm_regmap_init_i2c
创建regmap对象 - 通过
regmap_read
/regmap_write
直接操作设备寄存器 - 内核会自动处理总线通信细节(如I2C协议、数据打包等)
除此之外,还衍生了一些其他的API函数,具体可以参考文件include/linux/regmap.h
int regmap_mmio_attach_clk(struct regmap *map, struct clk *clk);
void regmap_mmio_detach_clk(struct regmap *map);
void regmap_exit(struct regmap *map);
int regmap_reinit_cache(struct regmap *map,
const struct regmap_config *config);
struct regmap *dev_get_regmap(struct device *dev, const char *name);
struct device *regmap_get_device(struct regmap *map);
int regmap_write(struct regmap *map, unsigned int reg, unsigned int val);
int regmap_write_async(struct regmap *map, unsigned int reg, unsigned int val);
int regmap_raw_write(struct regmap *map, unsigned int reg,
const void *val, size_t val_len);
int regmap_noinc_write(struct regmap *map, unsigned int reg,
const void *val, size_t val_len);
int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
size_t val_count);
int regmap_multi_reg_write(struct regmap *map, const struct reg_sequence *regs,
int num_regs);
int regmap_multi_reg_write_bypassed(struct regmap *map,
const struct reg_sequence *regs,
int num_regs);
int regmap_raw_write_async(struct regmap *map, unsigned int reg,
const void *val, size_t val_len);
int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val);
int regmap_raw_read(struct regmap *map, unsigned int reg,
void *val, size_t val_len);
int regmap_noinc_read(struct regmap *map, unsigned int reg,
void *val, size_t val_len);
int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
size_t val_count);
int regmap_update_bits_base(struct regmap *map, unsigned int reg,
unsigned int mask, unsigned int val,
bool *change, bool async, bool force);
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32