diff options
author | Masahiro Yamada | 2018-04-19 12:14:01 +0900 |
---|---|---|
committer | Tom Rini | 2018-05-07 15:15:27 -0400 |
commit | 8c1de5e08b845eb71c9a247d16bc81ebdab0598d (patch) | |
tree | 3cfa1a137c58f01b0a658bc53d0efec5c1734aca /drivers/core | |
parent | 9ce751a6f5d6683b0fabd3cfc62da7e9f30bc57f (diff) |
regmap: clean up regmap allocation
Putting zero length array at the end of struct is a common technique
to embed arbitrary length of members. There is no good reason to let
regmap_alloc_count() branch by "if (count <= 1)".
As far as I understood the code, regmap->base is an alias of
regmap->ranges[0].start, but it is not helpful but make the code
just ugly.
Rename regmap_alloc_count() to regmap_alloc() because the _count
suffix seems pointless.
Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
Reviewed-by: Simon Glass <sjg@chromium.org>
[trini: fixup cpu_info-rcar.c]
Signed-off-by: Tom Rini <trini@konsulko.com>
Diffstat (limited to 'drivers/core')
-rw-r--r-- | drivers/core/regmap.c | 31 |
1 files changed, 9 insertions, 22 deletions
diff --git a/drivers/core/regmap.c b/drivers/core/regmap.c index a5e4e748256..0f787ab4e13 100644 --- a/drivers/core/regmap.c +++ b/drivers/core/regmap.c @@ -17,22 +17,13 @@ DECLARE_GLOBAL_DATA_PTR; -static struct regmap *regmap_alloc_count(int count) +static struct regmap *regmap_alloc(int count) { struct regmap *map; - map = malloc(sizeof(struct regmap)); + map = malloc(sizeof(*map) + sizeof(map->ranges[0]) * count); if (!map) return NULL; - if (count <= 1) { - map->range = &map->base_range; - } else { - map->range = malloc(count * sizeof(struct regmap_range)); - if (!map->range) { - free(map); - return NULL; - } - } map->range_count = count; return map; @@ -45,12 +36,11 @@ int regmap_init_mem_platdata(struct udevice *dev, fdt_val_t *reg, int count, struct regmap_range *range; struct regmap *map; - map = regmap_alloc_count(count); + map = regmap_alloc(count); if (!map) return -ENOMEM; - map->base = *reg; - for (range = map->range; count > 0; reg += 2, range++, count--) { + for (range = map->ranges; count > 0; reg += 2, range++, count--) { range->start = *reg; range->size = reg[1]; } @@ -83,11 +73,11 @@ int regmap_init_mem(struct udevice *dev, struct regmap **mapp) if (!count) return -EINVAL; - map = regmap_alloc_count(count); + map = regmap_alloc(count); if (!map) return -ENOMEM; - for (range = map->range, index = 0; count > 0; + for (range = map->ranges, index = 0; count > 0; count--, range++, index++) { fdt_size_t sz; if (of_live_active()) { @@ -101,7 +91,6 @@ int regmap_init_mem(struct udevice *dev, struct regmap **mapp) range->size = sz; } } - map->base = map->range[0].start; *mapp = map; @@ -115,15 +104,13 @@ void *regmap_get_range(struct regmap *map, unsigned int range_num) if (range_num >= map->range_count) return NULL; - range = &map->range[range_num]; + range = &map->ranges[range_num]; return map_sysmem(range->start, range->size); } int regmap_uninit(struct regmap *map) { - if (map->range_count > 1) - free(map->range); free(map); return 0; @@ -131,7 +118,7 @@ int regmap_uninit(struct regmap *map) int regmap_read(struct regmap *map, uint offset, uint *valp) { - uint32_t *ptr = map_physmem(map->base + offset, 4, MAP_NOCACHE); + u32 *ptr = map_physmem(map->ranges[0].start + offset, 4, MAP_NOCACHE); *valp = le32_to_cpu(readl(ptr)); @@ -140,7 +127,7 @@ int regmap_read(struct regmap *map, uint offset, uint *valp) int regmap_write(struct regmap *map, uint offset, uint val) { - uint32_t *ptr = map_physmem(map->base + offset, 4, MAP_NOCACHE); + u32 *ptr = map_physmem(map->ranges[0].start + offset, 4, MAP_NOCACHE); writel(cpu_to_le32(val), ptr); |