13 #include <linux/device.h>
14 #include <linux/slab.h>
15 #include <linux/export.h>
18 #include <linux/rbtree.h>
20 #define CREATE_TRACE_POINTS
33 static int _regmap_update_bits(
struct regmap *
map,
unsigned int reg,
34 unsigned int mask,
unsigned int val,
53 if (map->
format.format_write)
84 static bool regmap_volatile_range(
struct regmap *
map,
unsigned int reg,
89 for (i = 0; i < num; i++)
96 static void regmap_format_2_6_write(
struct regmap *map,
97 unsigned int reg,
unsigned int val)
101 *out = (reg << 6) | val;
104 static void regmap_format_4_12_write(
struct regmap *map,
105 unsigned int reg,
unsigned int val)
111 static void regmap_format_7_9_write(
struct regmap *map,
112 unsigned int reg,
unsigned int val)
118 static void regmap_format_10_14_write(
struct regmap *map,
119 unsigned int reg,
unsigned int val)
124 out[1] = (val >> 8) | (reg << 6);
128 static void regmap_format_8(
void *
buf,
unsigned int val,
unsigned int shift)
135 static void regmap_format_16_be(
void *
buf,
unsigned int val,
unsigned int shift)
142 static void regmap_format_16_native(
void *
buf,
unsigned int val,
145 *(
u16 *)buf = val << shift;
148 static void regmap_format_24(
void *
buf,
unsigned int val,
unsigned int shift)
159 static void regmap_format_32_be(
void *
buf,
unsigned int val,
unsigned int shift)
166 static void regmap_format_32_native(
void *
buf,
unsigned int val,
169 *(
u32 *)buf = val << shift;
172 static unsigned int regmap_parse_8(
void *
buf)
179 static unsigned int regmap_parse_16_be(
void *
buf)
188 static unsigned int regmap_parse_16_native(
void *
buf)
193 static unsigned int regmap_parse_24(
void *
buf)
196 unsigned int ret = b[2];
197 ret |= ((
unsigned int)b[1]) << 8;
198 ret |= ((
unsigned int)b[0]) << 16;
203 static unsigned int regmap_parse_32_be(
void *
buf)
212 static unsigned int regmap_parse_32_native(
void *
buf)
217 static void regmap_lock_mutex(
struct regmap *map)
222 static void regmap_unlock_mutex(
struct regmap *map)
227 static void regmap_lock_spinlock(
struct regmap *map)
232 static void regmap_unlock_spinlock(
struct regmap *map)
237 static void dev_get_regmap_release(
struct device *
dev,
void *
res)
246 static bool _regmap_range_add(
struct regmap *map,
258 new = &((*new)->rb_left);
259 else if (data->
range_min > this->range_max)
260 new = &((*new)->rb_right);
265 rb_link_node(&data->
node, parent,
new);
291 static void regmap_range_exit(
struct regmap *map)
320 const struct regmap_bus *
bus,
322 const struct regmap_config *
config)
326 enum regmap_endian reg_endian, val_endian;
340 map->
lock = regmap_lock_spinlock;
341 map->
unlock = regmap_unlock_spinlock;
344 map->
lock = regmap_lock_mutex;
345 map->
unlock = regmap_unlock_mutex;
348 map->
format.pad_bytes = config->pad_bits / 8;
351 config->val_bits + config->pad_bits, 8);
353 if (config->reg_stride)
367 map->
name = config->name;
369 if (config->read_flag_mask || config->write_flag_mask) {
376 reg_endian = config->reg_format_endian;
377 if (reg_endian == REGMAP_ENDIAN_DEFAULT)
378 reg_endian = bus->reg_format_endian_default;
379 if (reg_endian == REGMAP_ENDIAN_DEFAULT)
380 reg_endian = REGMAP_ENDIAN_BIG;
382 val_endian = config->val_format_endian;
383 if (val_endian == REGMAP_ENDIAN_DEFAULT)
384 val_endian = bus->val_format_endian_default;
385 if (val_endian == REGMAP_ENDIAN_DEFAULT)
386 val_endian = REGMAP_ENDIAN_BIG;
388 switch (config->reg_bits + map->
reg_shift) {
390 switch (config->val_bits) {
392 map->
format.format_write = regmap_format_2_6_write;
400 switch (config->val_bits) {
402 map->
format.format_write = regmap_format_4_12_write;
410 switch (config->val_bits) {
412 map->
format.format_write = regmap_format_7_9_write;
420 switch (config->val_bits) {
422 map->
format.format_write = regmap_format_10_14_write;
430 map->
format.format_reg = regmap_format_8;
434 switch (reg_endian) {
435 case REGMAP_ENDIAN_BIG:
436 map->
format.format_reg = regmap_format_16_be;
438 case REGMAP_ENDIAN_NATIVE:
439 map->
format.format_reg = regmap_format_16_native;
447 switch (reg_endian) {
448 case REGMAP_ENDIAN_BIG:
449 map->
format.format_reg = regmap_format_32_be;
451 case REGMAP_ENDIAN_NATIVE:
452 map->
format.format_reg = regmap_format_32_native;
463 switch (config->val_bits) {
465 map->
format.format_val = regmap_format_8;
466 map->
format.parse_val = regmap_parse_8;
469 switch (val_endian) {
470 case REGMAP_ENDIAN_BIG:
471 map->
format.format_val = regmap_format_16_be;
472 map->
format.parse_val = regmap_parse_16_be;
474 case REGMAP_ENDIAN_NATIVE:
475 map->
format.format_val = regmap_format_16_native;
476 map->
format.parse_val = regmap_parse_16_native;
483 if (val_endian != REGMAP_ENDIAN_BIG)
485 map->
format.format_val = regmap_format_24;
486 map->
format.parse_val = regmap_parse_24;
489 switch (val_endian) {
490 case REGMAP_ENDIAN_BIG:
491 map->
format.format_val = regmap_format_32_be;
492 map->
format.parse_val = regmap_parse_32_be;
494 case REGMAP_ENDIAN_NATIVE:
495 map->
format.format_val = regmap_format_32_native;
496 map->
format.parse_val = regmap_parse_32_native;
504 if (map->
format.format_write) {
505 if ((reg_endian != REGMAP_ENDIAN_BIG) ||
506 (val_endian != REGMAP_ENDIAN_BIG))
511 if (!map->
format.format_write &&
522 for (i = 0; i < config->n_ranges; i++) {
523 const struct regmap_range_cfg *range_cfg = &config->ranges[
i];
527 if (range_cfg->range_max < range_cfg->range_min ||
530 range_cfg->window_len == 0)
535 for (j = 0; j < config->n_ranges; j++) {
537 unsigned win_min = config->ranges[
j].window_start;
538 unsigned win_max = win_min +
539 config->ranges[
j].window_len - 1;
541 if (range_cfg->range_min <= sel_reg &&
547 win_min > range_cfg->range_max)) {
558 new->range_min = range_cfg->range_min;
559 new->range_max = range_cfg->range_max;
560 new->selector_reg = range_cfg->selector_reg;
561 new->selector_mask = range_cfg->selector_mask;
562 new->selector_shift = range_cfg->selector_shift;
563 new->window_start = range_cfg->window_start;
564 new->window_len = range_cfg->window_len;
566 if (_regmap_range_add(map,
new) ==
false) {
602 regmap_range_exit(map);
611 static void devm_regmap_release(
struct device *
dev,
void *
res)
630 const struct regmap_bus *
bus,
632 const struct regmap_config *
config)
640 regmap =
regmap_init(dev, bus, bus_context, config);
641 if (!IS_ERR(regmap)) {
694 regmap_range_exit(map);
695 if (map->
bus->free_context)
702 static int dev_get_regmap_match(
struct device *dev,
void *res,
void *data)
712 return (*r)->name == data;
732 dev_get_regmap_match, (
void *)name);
740 static int _regmap_select_page(
struct regmap *map,
unsigned int *reg,
741 unsigned int val_num)
745 unsigned int win_offset;
746 unsigned int win_page;
750 range = _regmap_range_lookup(map, *reg);
757 if (*reg + val_num - 1 > range->
range_max)
791 static int _regmap_raw_write(
struct regmap *map,
unsigned int reg,
792 const void *val,
size_t val_len)
802 for (i = 0; i < val_len / map->
format.val_bytes; i++)
809 int val_bytes = map->
format.val_bytes;
810 for (i = 0; i < val_len / val_bytes; i++) {
817 "Error in caching of register: %u ret: %d\n",
828 ret = _regmap_select_page(map, ®, val_len / map->
format.val_bytes);
836 trace_regmap_hw_write_start(map->
dev, reg,
837 val_len / map->
format.val_bytes);
849 else if (map->
bus->gather_write)
857 len = map->
format.reg_bytes + map->
format.pad_bytes + val_len;
870 trace_regmap_hw_write_done(map->
dev, reg,
871 val_len / map->
format.val_bytes);
893 if (
strcmp(dev_name(map->
dev), LOG_DEVICE) == 0)
897 trace_regmap_reg_write(map->
dev, reg, val);
899 if (map->
format.format_write) {
900 ret = _regmap_select_page(map, ®, 1);
904 map->
format.format_write(map, reg, val);
906 trace_regmap_hw_write_start(map->
dev, reg, 1);
911 trace_regmap_hw_write_done(map->
dev, reg, 1);
916 + map->
format.pad_bytes, val, 0);
917 return _regmap_raw_write(map, reg,
969 const void *val,
size_t val_len)
973 if (val_len % map->
format.val_bytes)
980 ret = _regmap_raw_write(map, reg, val, val_len);
1006 size_t val_bytes = map->
format.val_bytes;
1009 if (!map->
format.parse_val)
1017 if (val_bytes == 1) {
1023 dev_err(map->
dev,
"Error in memory allocation\n");
1026 for (i = 0; i < val_count * val_bytes; i += val_bytes)
1027 map->
format.parse_val(wval + i);
1034 for (i = 0; i < val_count; i++) {
1037 val + (i * val_bytes),
1043 ret = _regmap_raw_write(map, reg, wval, val_bytes * val_count);
1055 static int _regmap_raw_read(
struct regmap *map,
unsigned int reg,
void *val,
1056 unsigned int val_len)
1061 ret = _regmap_select_page(map, ®, val_len / map->
format.val_bytes);
1075 trace_regmap_hw_read_start(map->
dev, reg,
1076 val_len / map->
format.val_bytes);
1082 trace_regmap_hw_read_done(map->
dev, reg,
1083 val_len / map->
format.val_bytes);
1088 static int _regmap_read(
struct regmap *map,
unsigned int reg,
1099 if (!map->
format.parse_val)
1105 ret = _regmap_raw_read(map, reg, map->
work_buf, map->
format.val_bytes);
1110 if (
strcmp(dev_name(map->
dev), LOG_DEVICE) == 0)
1114 trace_regmap_reg_read(map->
dev, reg, *val);
1142 ret = _regmap_read(map, reg, val);
1164 size_t val_bytes = map->
format.val_bytes;
1165 size_t val_count = val_len / val_bytes;
1169 if (val_len % map->
format.val_bytes)
1176 if (regmap_volatile_range(map, reg, val_count) || map->
cache_bypass ||
1179 ret = _regmap_raw_read(map, reg, val, val_len);
1185 for (i = 0; i < val_count; i++) {
1186 ret = _regmap_read(map, reg + (i * map->
reg_stride),
1191 map->
format.format_val(val + (i * val_bytes), v, 0);
1217 size_t val_bytes = map->
format.val_bytes;
1218 bool vol = regmap_volatile_range(map, reg, val_count);
1220 if (!map->
format.parse_val)
1231 for (i = 0; i < val_count; i++) {
1234 val + (i * val_bytes),
1241 val_bytes * val_count);
1246 for (i = 0; i < val_count * val_bytes; i += val_bytes)
1247 map->
format.parse_val(val + i);
1249 for (i = 0; i < val_count; i++) {
1255 memcpy(val + (i * val_bytes), &ival, val_bytes);
1263 static int _regmap_update_bits(
struct regmap *map,
unsigned int reg,
1264 unsigned int mask,
unsigned int val,
1268 unsigned int tmp, orig;
1270 ret = _regmap_read(map, reg, &orig);
1298 unsigned int mask,
unsigned int val)
1304 ret = _regmap_update_bits(map, reg, mask, val, &change);
1324 unsigned int mask,
unsigned int val,
1330 ret = _regmap_update_bits(map, reg, mask, val, change);
1367 for (i = 0; i < num_regs; i++) {
1370 dev_err(map->
dev,
"Failed to write %x = %x: %d\n",
1371 regs[i].
reg, regs[i].
def, ret);
1402 if (map->
format.format_write)
1405 return map->
format.val_bytes;
1409 static int __init regmap_initcall(
void)