Merge branch 'perf-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[pandora-kernel.git] / drivers / base / regmap / regcache.c
index 5364dde..afcfef8 100644 (file)
@@ -12,6 +12,8 @@
 
 #include <linux/slab.h>
 #include <trace/events/regmap.h>
+#include <linux/bsearch.h>
+#include <linux/sort.h>
 
 #include "internal.h"
 
@@ -117,8 +119,8 @@ int regcache_init(struct regmap *map)
                if (!tmp_buf)
                        return -ENOMEM;
                map->reg_defaults = tmp_buf;
-       } else {
-               /* Some devices such as PMIC's don't have cache defaults,
+       } 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.
                 */
@@ -227,9 +229,13 @@ int regcache_sync(struct regmap *map)
        unsigned int val;
        unsigned int i;
        const char *name;
+       unsigned int bypass;
 
        BUG_ON(!map->cache_ops);
 
+       mutex_lock(&map->lock);
+       /* Remember the initial bypass state */
+       bypass = map->cache_bypass;
        dev_dbg(map->dev, "Syncing %s cache\n",
                map->cache_ops->name);
        name = map->cache_ops->name;
@@ -241,9 +247,9 @@ int regcache_sync(struct regmap *map)
                        ret = regcache_read(map, i, &val);
                        if (ret < 0)
                                goto out;
-                       regcache_cache_bypass(map, true);
-                       ret = regmap_write(map, i, val);
-                       regcache_cache_bypass(map, false);
+                       map->cache_bypass = 1;
+                       ret = _regmap_write(map, i, val);
+                       map->cache_bypass = 0;
                        if (ret < 0)
                                goto out;
                        dev_dbg(map->dev, "Synced register %#x, value %#x\n",
@@ -254,6 +260,9 @@ int regcache_sync(struct regmap *map)
        }
 out:
        trace_regcache_sync(map->dev, name, "stop");
+       /* Restore the bypass state */
+       map->cache_bypass = bypass;
+       mutex_unlock(&map->lock);
 
        return ret;
 }
@@ -273,10 +282,33 @@ EXPORT_SYMBOL_GPL(regcache_sync);
  */
 void regcache_cache_only(struct regmap *map, bool enable)
 {
+       mutex_lock(&map->lock);
+       WARN_ON(map->cache_bypass && enable);
        map->cache_only = enable;
+       mutex_unlock(&map->lock);
 }
 EXPORT_SYMBOL_GPL(regcache_cache_only);
 
+/**
+ * regcache_cache_bypass: Put a register map into cache bypass mode
+ *
+ * @map: map to configure
+ * @cache_bypass: flag if changes should not be written to the hardware
+ *
+ * When a register map is marked with the cache bypass option, writes
+ * to the register map API will only update the hardware and not the
+ * the cache directly.  This is useful when syncing the cache back to
+ * the hardware.
+ */
+void regcache_cache_bypass(struct regmap *map, bool enable)
+{
+       mutex_lock(&map->lock);
+       WARN_ON(map->cache_only && enable);
+       map->cache_bypass = enable;
+       mutex_unlock(&map->lock);
+}
+EXPORT_SYMBOL_GPL(regcache_cache_bypass);
+
 bool regcache_set_val(void *base, unsigned int idx,
                      unsigned int val, unsigned int word_size)
 {
@@ -324,14 +356,29 @@ unsigned int regcache_get_val(const void *base, unsigned int idx,
        return -1;
 }
 
+static int regcache_default_cmp(const void *a, const void *b)
+{
+       const struct reg_default *_a = a;
+       const struct reg_default *_b = b;
+
+       return _a->reg - _b->reg;
+}
+
 int regcache_lookup_reg(struct regmap *map, unsigned int reg)
 {
-       unsigned int i;
+       struct reg_default key;
+       struct reg_default *r;
 
-       for (i = 0; i < map->num_reg_defaults; i++)
-               if (map->reg_defaults[i].reg == reg)
-                       return i;
-       return -1;
+       key.reg = reg;
+       key.def = 0;
+
+       r = bsearch(&key, map->reg_defaults, map->num_reg_defaults,
+                   sizeof(struct reg_default), regcache_default_cmp);
+
+       if (r)
+               return r - map->reg_defaults;
+       else
+               return -ENOENT;
 }
 
 int regcache_insert_reg(struct regmap *map, unsigned int reg,
@@ -348,5 +395,7 @@ int regcache_insert_reg(struct regmap *map, unsigned int reg,
        map->num_reg_defaults++;
        map->reg_defaults[map->num_reg_defaults - 1].reg = reg;
        map->reg_defaults[map->num_reg_defaults - 1].def = val;
+       sort(map->reg_defaults, map->num_reg_defaults,
+            sizeof(struct reg_default), regcache_default_cmp, NULL);
        return 0;
 }