Skip to contentSkip to conversation

regmap: Return a sensible error code if we fail to read the cache

PATCH In review Mark Brown 3 participants 8 messages Oct 9, 2011 – Oct 10, 2011

Subsystems (2)
lore
Share & tools
lore

Mark Brown

author
#0 lore raw

PATCH 1/2 regmap: Return a sensible error code if we fail to read the cache

If a register isn't cached then let callers know that so they can fall back or error handle appropriately.

---
 drivers/base/regmap/regcache-indexed.c |    7 +++----
 drivers/base/regmap/regcache-lzo.c     |    4 ++--
 drivers/base/regmap/regcache-rbtree.c  |    3 +--
 drivers/base/regmap/regcache.c         |    2 +-
 4 files changed, 7 insertions(+), 9 deletions(-)
drivers/base/regmap/regcache-indexed.c +3 -4
index 2e10bb1..507731a 100644
@@ -20,11 +20,10 @@ static int regcache_indexed_read(struct regmap *map, unsigned int reg,
 	int ret;
 
 	ret = regcache_lookup_reg(map, reg);
-	if (ret < 0)
-		*value = 0;
-	else
+	if (ret >= 0)
 		*value = map->reg_defaults[ret].def;
-	return 0;
+
+	return ret;
 }
 
 static int regcache_indexed_write(struct regmap *map, unsigned int reg,
drivers/base/regmap/regcache-lzo.c +2 -2
index ad6af92..066aeec 100644
@@ -232,7 +232,6 @@ static int regcache_lzo_read(struct regmap *map,
 	size_t blksize, tmp_dst_len;
 	void *tmp_dst;
 
-	*value = 0;
 	/* index of the compressed lzo block */
 	blkindex = regcache_lzo_get_blkindex(map, reg);
 	/* register index within the decompressed block */
@@ -261,7 +260,8 @@ static int regcache_lzo_read(struct regmap *map,
 	/* restore the pointer and length of the compressed block */
 	lzo_block->dst = tmp_dst;
 	lzo_block->dst_len = tmp_dst_len;
-	return 0;
+
+	return ret;
 }
 
 static int regcache_lzo_write(struct regmap *map,
drivers/base/regmap/regcache-rbtree.c +1 -2
index f80eebc..e314984 100644
@@ -193,8 +193,7 @@ static int regcache_rbtree_read(struct regmap *map,
 		*value = regcache_rbtree_get_register(rbnode, reg_tmp,
 						      map->cache_word_size);
 	} else {
-		/* uninitialized registers default to 0 */
-		*value = 0;
+		return -ENOENT;
 	}
 
 	return 0;
drivers/base/regmap/regcache.c +1 -1
index bf82783..67c7703 100644
@@ -379,7 +379,7 @@ int regcache_lookup_reg(struct regmap *map, unsigned int reg)
 	if (r)
 		return r - map->reg_defaults;
 	else
-		return -1;
+		return -ENOENT;
 }
 
 int regcache_insert_reg(struct regmap *map, unsigned int reg,
Signature · 1 line
1.7.6.3
author
#1 lore raw

PATCH 2/2 regmap: Allow caches for devices with no defaults

We only really need the defaults in order to cut down the number of registers we sync and to satisfy reads while the device is powered off but not all devices are going to need to do that (always on devices like PMICs being the prime example) so don't require those devices to supply a default. Instead only try to fall back to hardware defaults if the driver told us to.

Devices using LZO won't be able to instantiate with this, that will require some updates in the LZO code to handle this case.

---
 drivers/base/regmap/regcache.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
drivers/base/regmap/regcache.c +1 -1
index 67c7703..666f6f5 100644
@@ -120,7 +120,7 @@ int regcache_init(struct regmap *map)
 		if (!tmp_buf)
 			return -ENOMEM;
 		map->reg_defaults = tmp_buf;
-	} else {
+	} else if (map->num_reg_defaults_raw) {
 		/* Some devices such as PMICs don't have cache defaults,
 		 * we cope with this by reading back the HW registers and
 		 * crafting the cache defaults by hand.
Signature · 1 line
1.7.6.3
#2 lore raw

Re: [PATCH 2/2] regmap: Allow caches for devices with no defaults

Quoted 2 lines · Mark Brown

We only really need the defaults in order to cut down the number of registers we sync and to satisfy reads while the device is powered off

So let me get this straight - we cache the values so that if there's a read, we can return our best guess as to what the device would return if it was in fact powered on, instead of notifying the caller that the device is off?

I think I just sprained my brain trying to parse the logic there :)

author
#3 lore raw

Re: [PATCH 2/2] regmap: Allow caches for devices with no defaults

Quoted 10 lines · On Sun, Oct 09, 2011 at 01:43:42PM -0400, [email protected] wrote:
Quoted 2 lines · Mark Brown

We only really need the defaults in order to cut down the number of registers we sync and to satisfy reads while the device is powered off

So let me get this straight - we cache the values so that if there's a read, we can return our best guess as to what the device would return if it was in fact powered on, instead of notifying the caller that the device is off?

I think I just sprained my brain trying to parse the logic there :)

It's not a guess, it's very solid information which comes from a combination of knowing the default register values, knowing that the registers we cache won't be changed except by writes and caching writes. We're not really doing the caching for this particular feature, having a cache is really useful anyway and once you're doing register caching it's a small step to be able to skip the actual device interaction.

Having a register cache is useful where you don't need much abstraction between the register and the upper layers that talk to the driver - you can end up with large chunks of driver which are just data tables saying "this bitfield in this register has these semantics" which is obviously a substantial win.

However, not all devices have readback support (on older analogue processes digital was very expensive and there were a few other issues for some buses) and this can get very read heavy (both from read/modify/write cycles and from upper layers querying state) so when your device is on a slow bus it's really useful to be able to cache the register values rather than actually read from the device.

Once you've got a cache it's a simple matter to skip the step where we actually interact with the device for non-volatile registers. In situations where the device is idle and we've got the sort of thin mapping described above we don't really want to have to keep the device powered on just for the mapping and the register values are about all the abstraction we need so we don't want another layer. If there a lot of updates being made this can give a further performance boost as upper layers can update things in any order they see fit as often as they like and then we can coalesce the actual writes into a minimal subset when powering the device up for actual use.

For a concrete example think of an audio CODEC - there's lots of controls like volume settings which can be mapped very directly to what users interact with and which have no practical impact unless the device is actually doing something other than to store the setting which will be used then.

#4 lore raw

Re: [PATCH 2/2] regmap: Allow caches for devices with no defaults

In reply to Mark Brown
Quoted 30 lines · Mark Brown

We only really need the defaults in order to cut down the number of registers we sync and to satisfy reads while the device is powered off but not all devices are going to need to do that (always on devices like PMICs being the prime example) so don't require those devices to supply a default. Instead only try to fall back to hardware defaults if the driver told us to.

Devices using LZO won't be able to instantiate with this, that will require some updates in the LZO code to handle this case.

---
 drivers/base/regmap/regcache.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
drivers/base/regmap/regcache.c +1 -1
index 67c7703..666f6f5 100644
@@ -120,7 +120,7 @@ int regcache_init(struct regmap *map)
 		if (!tmp_buf)
 			return -ENOMEM;
 		map->reg_defaults = tmp_buf;
-	} else {
+	} else if (map->num_reg_defaults_raw) {
 		/* Some devices such as PMICs don't have cache defaults,
 		 * we cope with this by reading back the HW registers and
 		 * crafting the cache defaults by hand.
Signature · 1 line
1.7.6.3

We could probably then also remove the first check in regcache_hw_init() I guess.

The one:

if (!map->num_reg_defaults_raw)

	return -EINVAL;

Thanks,
Dimitris

author
#5 lore raw

Re: [PATCH 2/2] regmap: Allow caches for devices with no defaults

Quoted 10 lines · Dimitris Papastamos

drivers/base/regmap/regcache.c int regcache_init(struct regmap *map) · line 126

Quoted 4 lines · Mark Brown
drivers/base/regmap/regcache.c +1 -0
+	} else if (map->num_reg_defaults_raw) {
 		/* Some devices such as PMICs don't have cache defaults,
 		 * we cope with this by reading back the HW registers and
 		 * crafting the cache defaults by hand.

We could probably then also remove the first check in regcache_hw_init() I guess.

Quoted 4 lines

The one:

if (!map->num_reg_defaults_raw)

	return -EINVAL;

I felt it was clearer to have the defaults init fail if it doesn't work so that the callers know what happened. Otherwise the callers have to figure out if it succeeded because it did what it was supposed to.

#6 lore raw

Re: [PATCH 2/2] regmap: Allow caches for devices with no defaults

Quoted 11 lines · Mark Brown
Quoted 9 lines · Dimitris Papastamos

drivers/base/regmap/regcache.c int regcache_init(struct regmap *map) · line 126

Quoted 4 lines · Mark Brown
drivers/base/regmap/regcache.c +1 -0
+	} else if (map->num_reg_defaults_raw) {
 		/* Some devices such as PMICs don't have cache defaults,
 		 * we cope with this by reading back the HW registers and
 		 * crafting the cache defaults by hand.

We could probably then also remove the first check in regcache_hw_init() I guess.

Quoted 8 lines
Quoted 4 lines

The one:

if (!map->num_reg_defaults_raw)

	return -EINVAL;

I felt it was clearer to have the defaults init fail if it doesn't work so that the callers know what happened. Otherwise the callers have to figure out if it succeeded because it did what it was supposed to.

Aw right yes.

Acked-by on v1 1/2
#7 lore raw
In reply to Mark Brown
Quoted 10 lines · Mark Brown

If a register isn't cached then let callers know that so they can fall back or error handle appropriately.

---
 drivers/base/regmap/regcache-indexed.c |    7 +++----
 drivers/base/regmap/regcache-lzo.c     |    4 ++--
 drivers/base/regmap/regcache-rbtree.c  |    3 +--
 drivers/base/regmap/regcache.c         |    2 +-
 4 files changed, 7 insertions(+), 9 deletions(-)