Skip to content

Commit d939fb9

Browse files
marc-cpdesignbroonie
authored andcommitted
regmap: Use pad_bits and reg_bits when determining register format.
This change combines any padding bits into the register address bits when determining register format handlers to use the next byte-divisible register size. A reg_shift member is introduced to the regmap struct to enable fixup of the reg format. Format handlers now take an extra parameter specifying the number of bits to shift the value by. Signed-off-by: Marc Reilly <marc@cpdesign.com.au> Signed-off-by: Mark Brown <broonie@opensource.wolfsonmicro.com>
1 parent ea279fc commit d939fb9

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

drivers/base/regmap/internal.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ struct regmap_format {
2626
size_t val_bytes;
2727
void (*format_write)(struct regmap *map,
2828
unsigned int reg, unsigned int val);
29-
void (*format_reg)(void *buf, unsigned int reg);
30-
void (*format_val)(void *buf, unsigned int val);
29+
void (*format_reg)(void *buf, unsigned int reg, unsigned int shift);
30+
void (*format_val)(void *buf, unsigned int val, unsigned int shift);
3131
unsigned int (*parse_val)(void *buf);
3232
};
3333

@@ -52,6 +52,9 @@ struct regmap {
5252
u8 read_flag_mask;
5353
u8 write_flag_mask;
5454

55+
/* number of bits to (left) shift the reg value when formatting*/
56+
int reg_shift;
57+
5558
/* regcache specific members */
5659
const struct regcache_ops *cache_ops;
5760
enum regcache_type cache_type;

drivers/base/regmap/regmap.c

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -112,34 +112,36 @@ static void regmap_format_10_14_write(struct regmap *map,
112112
out[0] = reg >> 2;
113113
}
114114

115-
static void regmap_format_8(void *buf, unsigned int val)
115+
static void regmap_format_8(void *buf, unsigned int val, unsigned int shift)
116116
{
117117
u8 *b = buf;
118118

119-
b[0] = val;
119+
b[0] = val << shift;
120120
}
121121

122-
static void regmap_format_16(void *buf, unsigned int val)
122+
static void regmap_format_16(void *buf, unsigned int val, unsigned int shift)
123123
{
124124
__be16 *b = buf;
125125

126-
b[0] = cpu_to_be16(val);
126+
b[0] = cpu_to_be16(val << shift);
127127
}
128128

129-
static void regmap_format_24(void *buf, unsigned int val)
129+
static void regmap_format_24(void *buf, unsigned int val, unsigned int shift)
130130
{
131131
u8 *b = buf;
132132

133+
val <<= shift;
134+
133135
b[0] = val >> 16;
134136
b[1] = val >> 8;
135137
b[2] = val;
136138
}
137139

138-
static void regmap_format_32(void *buf, unsigned int val)
140+
static void regmap_format_32(void *buf, unsigned int val, unsigned int shift)
139141
{
140142
__be32 *b = buf;
141143

142-
b[0] = cpu_to_be32(val);
144+
b[0] = cpu_to_be32(val << shift);
143145
}
144146

145147
static unsigned int regmap_parse_8(void *buf)
@@ -210,6 +212,7 @@ struct regmap *regmap_init(struct device *dev,
210212
map->format.pad_bytes = config->pad_bits / 8;
211213
map->format.val_bytes = DIV_ROUND_UP(config->val_bits, 8);
212214
map->format.buf_size += map->format.pad_bytes;
215+
map->reg_shift = config->pad_bits % 8;
213216
map->dev = dev;
214217
map->bus = bus;
215218
map->max_register = config->max_register;
@@ -226,7 +229,7 @@ struct regmap *regmap_init(struct device *dev,
226229
map->read_flag_mask = bus->read_flag_mask;
227230
}
228231

229-
switch (config->reg_bits) {
232+
switch (config->reg_bits + map->reg_shift) {
230233
case 2:
231234
switch (config->val_bits) {
232235
case 6:
@@ -454,7 +457,7 @@ static int _regmap_raw_write(struct regmap *map, unsigned int reg,
454457
}
455458
}
456459

457-
map->format.format_reg(map->work_buf, reg);
460+
map->format.format_reg(map->work_buf, reg, map->reg_shift);
458461

459462
u8[0] |= map->write_flag_mask;
460463

@@ -529,7 +532,7 @@ int _regmap_write(struct regmap *map, unsigned int reg,
529532
return ret;
530533
} else {
531534
map->format.format_val(map->work_buf + map->format.reg_bytes
532-
+ map->format.pad_bytes, val);
535+
+ map->format.pad_bytes, val, 0);
533536
return _regmap_raw_write(map, reg,
534537
map->work_buf +
535538
map->format.reg_bytes +
@@ -649,7 +652,7 @@ static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
649652
u8 *u8 = map->work_buf;
650653
int ret;
651654

652-
map->format.format_reg(map->work_buf, reg);
655+
map->format.format_reg(map->work_buf, reg, map->reg_shift);
653656

654657
/*
655658
* Some buses or devices flag reads by setting the high bits in the
@@ -757,7 +760,7 @@ int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
757760
if (ret != 0)
758761
goto out;
759762

760-
map->format.format_val(val + (i * val_bytes), v);
763+
map->format.format_val(val + (i * val_bytes), v, 0);
761764
}
762765
}
763766

0 commit comments

Comments
 (0)