Merge tag 'fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/arm...
[cascardo/linux.git] / drivers / base / regmap / regmap.c
index bf441db..6555803 100644 (file)
@@ -64,6 +64,18 @@ bool regmap_precious(struct regmap *map, unsigned int reg)
        return false;
 }
 
+static bool regmap_volatile_range(struct regmap *map, unsigned int reg,
+       unsigned int num)
+{
+       unsigned int i;
+
+       for (i = 0; i < num; i++)
+               if (!regmap_volatile(map, reg + i))
+                       return false;
+
+       return true;
+}
+
 static void regmap_format_4_12_write(struct regmap *map,
                                     unsigned int reg, unsigned int val)
 {
@@ -78,6 +90,16 @@ static void regmap_format_7_9_write(struct regmap *map,
        *out = cpu_to_be16((reg << 9) | val);
 }
 
+static void regmap_format_10_14_write(struct regmap *map,
+                                   unsigned int reg, unsigned int val)
+{
+       u8 *out = map->work_buf;
+
+       out[2] = val;
+       out[1] = (val >> 8) | (reg << 6);
+       out[0] = reg >> 2;
+}
+
 static void regmap_format_8(void *buf, unsigned int val)
 {
        u8 *b = buf;
@@ -127,7 +149,7 @@ struct regmap *regmap_init(struct device *dev,
        int ret = -EINVAL;
 
        if (!bus || !config)
-               return NULL;
+               goto err;
 
        map = kzalloc(sizeof(*map), GFP_KERNEL);
        if (map == NULL) {
@@ -147,12 +169,6 @@ struct regmap *regmap_init(struct device *dev,
        map->volatile_reg = config->volatile_reg;
        map->precious_reg = config->precious_reg;
        map->cache_type = config->cache_type;
-       map->reg_defaults = config->reg_defaults;
-       map->num_reg_defaults = config->num_reg_defaults;
-       map->num_reg_defaults_raw = config->num_reg_defaults_raw;
-       map->reg_defaults_raw = config->reg_defaults_raw;
-       map->cache_size_raw = (config->val_bits / 8) * config->num_reg_defaults_raw;
-       map->cache_word_size = config->val_bits / 8;
 
        if (config->read_flag_mask || config->write_flag_mask) {
                map->read_flag_mask = config->read_flag_mask;
@@ -182,6 +198,16 @@ struct regmap *regmap_init(struct device *dev,
                }
                break;
 
+       case 10:
+               switch (config->val_bits) {
+               case 14:
+                       map->format.format_write = regmap_format_10_14_write;
+                       break;
+               default:
+                       goto err_map;
+               }
+               break;
+
        case 8:
                map->format.format_reg = regmap_format_8;
                break;
@@ -215,14 +241,16 @@ struct regmap *regmap_init(struct device *dev,
                goto err_map;
        }
 
-       ret = regcache_init(map);
-       if (ret < 0)
-               goto err_map;
-
        regmap_debugfs_init(map);
 
+       ret = regcache_init(map, config);
+       if (ret < 0)
+               goto err_free_workbuf;
+
        return map;
 
+err_free_workbuf:
+       kfree(map->work_buf);
 err_map:
        kfree(map);
 err:
@@ -230,6 +258,42 @@ err:
 }
 EXPORT_SYMBOL_GPL(regmap_init);
 
+/**
+ * regmap_reinit_cache(): Reinitialise the current register cache
+ *
+ * @map: Register map to operate on.
+ * @config: New configuration.  Only the cache data will be used.
+ *
+ * Discard any existing register cache for the map and initialize a
+ * new cache.  This can be used to restore the cache to defaults or to
+ * update the cache configuration to reflect runtime discovery of the
+ * hardware.
+ */
+int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
+{
+       int ret;
+
+       mutex_lock(&map->lock);
+
+       regcache_exit(map);
+
+       map->max_register = config->max_register;
+       map->writeable_reg = config->writeable_reg;
+       map->readable_reg = config->readable_reg;
+       map->volatile_reg = config->volatile_reg;
+       map->precious_reg = config->precious_reg;
+       map->cache_type = config->cache_type;
+
+       map->cache_bypass = false;
+       map->cache_only = false;
+
+       ret = regcache_init(map, config);
+
+       mutex_unlock(&map->lock);
+
+       return ret;
+}
+
 /**
  * regmap_exit(): Free a previously allocated register map
  */
@@ -306,8 +370,10 @@ int _regmap_write(struct regmap *map, unsigned int reg,
                ret = regcache_write(map, reg, val);
                if (ret != 0)
                        return ret;
-               if (map->cache_only)
+               if (map->cache_only) {
+                       map->cache_dirty = true;
                        return 0;
+               }
        }
 
        trace_regmap_reg_write(map->dev, reg, val);
@@ -375,9 +441,11 @@ EXPORT_SYMBOL_GPL(regmap_write);
 int regmap_raw_write(struct regmap *map, unsigned int reg,
                     const void *val, size_t val_len)
 {
+       size_t val_count = val_len / map->format.val_bytes;
        int ret;
 
-       WARN_ON(map->cache_type != REGCACHE_NONE);
+       WARN_ON(!regmap_volatile_range(map, reg, val_count) &&
+               map->cache_type != REGCACHE_NONE);
 
        mutex_lock(&map->lock);
 
@@ -422,15 +490,15 @@ static int _regmap_read(struct regmap *map, unsigned int reg,
 {
        int ret;
 
-       if (!map->format.parse_val)
-               return -EINVAL;
-
        if (!map->cache_bypass) {
                ret = regcache_read(map, reg, val);
                if (ret == 0)
                        return 0;
        }
 
+       if (!map->format.parse_val)
+               return -EINVAL;
+
        if (map->cache_only)
                return -EBUSY;
 
@@ -481,15 +549,11 @@ EXPORT_SYMBOL_GPL(regmap_read);
 int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
                    size_t val_len)
 {
+       size_t val_count = val_len / map->format.val_bytes;
        int ret;
-       int i;
-       bool vol = true;
-
-       for (i = 0; i < val_len / map->format.val_bytes; i++)
-               if (!regmap_volatile(map, reg + i))
-                       vol = false;
 
-       WARN_ON(!vol && map->cache_type != REGCACHE_NONE);
+       WARN_ON(!regmap_volatile_range(map, reg, val_count) &&
+               map->cache_type != REGCACHE_NONE);
 
        mutex_lock(&map->lock);
 
@@ -517,16 +581,11 @@ int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
 {
        int ret, i;
        size_t val_bytes = map->format.val_bytes;
-       bool vol = true;
+       bool vol = regmap_volatile_range(map, reg, val_count);
 
        if (!map->format.parse_val)
                return -EINVAL;
 
-       /* Is this a block of volatile registers? */
-       for (i = 0; i < val_count; i++)
-               if (!regmap_volatile(map, reg + i))
-                       vol = false;
-
        if (vol || map->cache_type == REGCACHE_NONE) {
                ret = regmap_raw_read(map, reg, val, val_bytes * val_count);
                if (ret != 0)
@@ -546,40 +605,73 @@ int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
 }
 EXPORT_SYMBOL_GPL(regmap_bulk_read);
 
-/**
- * remap_update_bits: Perform a read/modify/write cycle on the register map
- *
- * @map: Register map to update
- * @reg: Register to update
- * @mask: Bitmask to change
- * @val: New value for bitmask
- *
- * Returns zero for success, a negative number on error.
- */
-int regmap_update_bits(struct regmap *map, unsigned int reg,
-                      unsigned int mask, unsigned int val)
+static int _regmap_update_bits(struct regmap *map, unsigned int reg,
+                              unsigned int mask, unsigned int val,
+                              bool *change)
 {
        int ret;
-       unsigned int tmp;
+       unsigned int tmp, orig;
 
        mutex_lock(&map->lock);
 
-       ret = _regmap_read(map, reg, &tmp);
+       ret = _regmap_read(map, reg, &orig);
        if (ret != 0)
                goto out;
 
-       tmp &= ~mask;
+       tmp = orig & ~mask;
        tmp |= val & mask;
 
-       ret = _regmap_write(map, reg, tmp);
+       if (tmp != orig) {
+               ret = _regmap_write(map, reg, tmp);
+               *change = true;
+       } else {
+               *change = false;
+       }
 
 out:
        mutex_unlock(&map->lock);
 
        return ret;
 }
+
+/**
+ * regmap_update_bits: Perform a read/modify/write cycle on the register map
+ *
+ * @map: Register map to update
+ * @reg: Register to update
+ * @mask: Bitmask to change
+ * @val: New value for bitmask
+ *
+ * Returns zero for success, a negative number on error.
+ */
+int regmap_update_bits(struct regmap *map, unsigned int reg,
+                      unsigned int mask, unsigned int val)
+{
+       bool change;
+       return _regmap_update_bits(map, reg, mask, val, &change);
+}
 EXPORT_SYMBOL_GPL(regmap_update_bits);
 
+/**
+ * regmap_update_bits_check: Perform a read/modify/write cycle on the
+ *                           register map and report if updated
+ *
+ * @map: Register map to update
+ * @reg: Register to update
+ * @mask: Bitmask to change
+ * @val: New value for bitmask
+ * @change: Boolean indicating if a write was done
+ *
+ * Returns zero for success, a negative number on error.
+ */
+int regmap_update_bits_check(struct regmap *map, unsigned int reg,
+                            unsigned int mask, unsigned int val,
+                            bool *change)
+{
+       return _regmap_update_bits(map, reg, mask, val, change);
+}
+EXPORT_SYMBOL_GPL(regmap_update_bits_check);
+
 static int __init regmap_initcall(void)
 {
        regmap_debugfs_initcall();