高通DRM图形显示框架devm_regmap_init_i2c的使用方法
时间: 2024-01-21 17:20:20 浏览: 155
devm_regmap_init_i2c是高通设备驱动中用于初始化RegMap结构体的函数,它会自动管理设备资源的分配和释放。以下是使用方法:
1. 定义i2c设备
首先需要定义一个i2c设备结构体,并填写好i2c_adapter、i2c地址等信息。例如:
```
static struct i2c_device_id my_device_id[] = {
{ "my_device", 0 },
{ }
};
MODULE_DEVICE_TABLE(i2c, my_device_id);
static struct i2c_driver my_driver = {
.driver = {
.name = "my_device",
.owner = THIS_MODULE,
},
.probe = my_probe,
.remove = my_remove,
.id_table = my_device_id,
};
static struct i2c_board_info my_i2c_board_info = {
I2C_BOARD_INFO("my_device", 0x50),
};
static struct i2c_client *my_i2c_client;
```
2. 注册i2c设备
在probe函数中调用i2c_new_device函数注册i2c设备,例如:
```
static int my_probe(struct i2c_client *client, const struct i2c_device_id *id)
{
struct regmap *map;
my_i2c_client = i2c_new_device(client->adapter, &my_i2c_board_info);
...
}
```
3. 初始化RegMap
在probe函数中调用devm_regmap_init_i2c函数初始化RegMap结构体,例如:
```
static int my_probe(struct i2c_client *client, const struct i2c_device_id *id)
{
struct regmap *map;
my_i2c_client = i2c_new_device(client->adapter, &my_i2c_board_info);
map = devm_regmap_init_i2c(my_i2c_client, &my_regmap_config);
...
}
```
其中my_regmap_config是RegMap配置结构体,需要填写好寄存器地址宽度、寄存器值宽度、i2c地址等信息,例如:
```
static const struct regmap_config my_regmap_config = {
.name = "my_device",
.reg_bits = 16,
.val_bits = 8,
.max_register = 0xFFFF,
.write_flag_mask = 0x80,
.cache_type = REGCACHE_FLAT,
.reg_defaults = my_reg_defaults,
.num_reg_defaults = ARRAY_SIZE(my_reg_defaults),
.cache_size = 32,
.use_single_read = true,
.use_single_write = true,
.fast_io = true,
.max_raw_read = 4096,
.max_raw_write = 4096,
.write_through = false,
.read_modify_write = false,
.disable_locking = false,
.lock_free = false,
.use_hw_lock = false,
.hw_lock = NULL,
};
```
4. 使用RegMap
初始化完成后,可以使用RegMap提供的函数读写寄存器,例如:
```
static int my_probe(struct i2c_client *client, const struct i2c_device_id *id)
{
struct regmap *map;
u32 value;
my_i2c_client = i2c_new_device(client->adapter, &my_i2c_board_info);
map = devm_regmap_init_i2c(my_i2c_client, &my_regmap_config);
regmap_read(map, MY_REG, &value);
dev_info(&client->dev, "MY_REG value is 0x%x\n", value);
regmap_write(map, MY_REG, 0x1234);
...
}
```
其中MY_REG是需要读写的寄存器地址。
阅读全文