All of lore.kernel.org
 help / color / mirror / Atom feed
From: Masahiro Yamada <yamada.masahiro@socionext.com>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH 1/4] regmap: clean up regmap allocation
Date: Wed, 18 Apr 2018 11:38:42 +0900	[thread overview]
Message-ID: <1524019125-26287-2-git-send-email-yamada.masahiro@socionext.com> (raw)
In-Reply-To: <1524019125-26287-1-git-send-email-yamada.masahiro@socionext.com>

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>
---

 drivers/core/regmap.c | 31 +++++++++----------------------
 include/regmap.h      |  7 ++-----
 2 files changed, 11 insertions(+), 27 deletions(-)

diff --git a/drivers/core/regmap.c b/drivers/core/regmap.c
index 8a0e00f..6c3dbe9 100644
--- a/drivers/core/regmap.c
+++ b/drivers/core/regmap.c
@@ -18,22 +18,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;
@@ -46,12 +37,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];
 	}
@@ -84,11 +74,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()) {
@@ -102,7 +92,6 @@ int regmap_init_mem(struct udevice *dev, struct regmap **mapp)
 			range->size = sz;
 		}
 	}
-	map->base = map->range[0].start;
 
 	*mapp = map;
 
@@ -116,15 +105,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;
@@ -132,7 +119,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));
 
@@ -141,7 +128,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);
 
diff --git a/include/regmap.h b/include/regmap.h
index 493a5d8..858aa7e 100644
--- a/include/regmap.h
+++ b/include/regmap.h
@@ -22,15 +22,12 @@ struct regmap_range {
 /**
  * struct regmap - a way of accessing hardware/bus registers
  *
- * @base:	Base address of register map
  * @range_count: Number of ranges available within the map
- * @range:	Pointer to the list of ranges, allocated if @range_count > 1
- * @base_range:	If @range_count is <= 1, @range points here
+ * @ranges:	Array of ranges
  */
 struct regmap {
-	phys_addr_t base;
 	int range_count;
-	struct regmap_range *range, base_range;
+	struct regmap_range ranges[];
 };
 
 /*
-- 
2.7.4

  reply	other threads:[~2018-04-18  2:38 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-04-18  2:38 [U-Boot] [PATCH 0/4] Add Linux-compatible syscon_to_regmap API Masahiro Yamada
2018-04-18  2:38 ` Masahiro Yamada [this message]
2018-04-22 20:11   ` [U-Boot] [PATCH 1/4] regmap: clean up regmap allocation Simon Glass
2018-04-18  2:38 ` [U-Boot] [PATCH 2/4] dm: ofnode: add ofnode_device_is_compatible() helper Masahiro Yamada
2018-04-22 20:11   ` Simon Glass
2018-04-23  4:50     ` Masahiro Yamada
2018-04-25  5:01       ` Simon Glass
2018-04-18  2:38 ` [U-Boot] [PATCH 3/4] regmap: change regmap_init_mem() to take ofnode instead udevice Masahiro Yamada
2018-04-18 15:34   ` Neil Armstrong
2018-04-22 20:10   ` Simon Glass
2018-04-23  4:56     ` Masahiro Yamada
2018-04-25  5:01       ` Simon Glass
2018-04-18  2:38 ` [U-Boot] [PATCH 4/4] syscon: add Linux-compatible syscon API Masahiro Yamada
2018-04-22 20:11   ` Simon Glass
2018-04-23  4:58     ` Masahiro Yamada
2018-04-25  5:01       ` Simon Glass

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1524019125-26287-2-git-send-email-yamada.masahiro@socionext.com \
    --to=yamada.masahiro@socionext.com \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.