#include <linux/slab.h>
#include <trace/events/regmap.h>
+#include <linux/bsearch.h>
+#include <linux/sort.h>
#include "internal.h"
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.
*/
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;
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",
}
out:
trace_regcache_sync(map->dev, name, "stop");
+ /* Restore the bypass state */
+ map->cache_bypass = bypass;
+ mutex_unlock(&map->lock);
return ret;
}
*/
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)
{
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,
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;
}