]> git.karo-electronics.de Git - karo-tx-linux.git/blobdiff - drivers/base/regmap/regmap.c
Merge branches 'regmap-core', 'regmap-stride', 'regmap-mmio' and 'regmap-irq' into...
[karo-tx-linux.git] / drivers / base / regmap / regmap.c
index 618173e4c2b147c3c6a4b534462065d65a9653a6..0bcda488f11cd45e6252e113ffd025b058309e99 100644 (file)
@@ -112,25 +112,36 @@ static void regmap_format_10_14_write(struct regmap *map,
        out[0] = reg >> 2;
 }
 
-static void regmap_format_8(void *buf, unsigned int val)
+static void regmap_format_8(void *buf, unsigned int val, unsigned int shift)
 {
        u8 *b = buf;
 
-       b[0] = val;
+       b[0] = val << shift;
 }
 
-static void regmap_format_16(void *buf, unsigned int val)
+static void regmap_format_16(void *buf, unsigned int val, unsigned int shift)
 {
        __be16 *b = buf;
 
-       b[0] = cpu_to_be16(val);
+       b[0] = cpu_to_be16(val << shift);
 }
 
-static void regmap_format_32(void *buf, unsigned int val)
+static void regmap_format_24(void *buf, unsigned int val, unsigned int shift)
+{
+       u8 *b = buf;
+
+       val <<= shift;
+
+       b[0] = val >> 16;
+       b[1] = val >> 8;
+       b[2] = val;
+}
+
+static void regmap_format_32(void *buf, unsigned int val, unsigned int shift)
 {
        __be32 *b = buf;
 
-       b[0] = cpu_to_be32(val);
+       b[0] = cpu_to_be32(val << shift);
 }
 
 static unsigned int regmap_parse_8(void *buf)
@@ -149,6 +160,16 @@ static unsigned int regmap_parse_16(void *buf)
        return b[0];
 }
 
+static unsigned int regmap_parse_24(void *buf)
+{
+       u8 *b = buf;
+       unsigned int ret = b[2];
+       ret |= ((unsigned int)b[1]) << 8;
+       ret |= ((unsigned int)b[0]) << 16;
+
+       return ret;
+}
+
 static unsigned int regmap_parse_32(void *buf)
 {
        __be32 *b = buf;
@@ -230,6 +251,12 @@ struct regmap *regmap_init(struct device *dev,
        map->format.pad_bytes = config->pad_bits / 8;
        map->format.val_bytes = DIV_ROUND_UP(config->val_bits, 8);
        map->format.buf_size += map->format.pad_bytes;
+       map->reg_shift = config->pad_bits % 8;
+       if (config->reg_stride)
+               map->reg_stride = config->reg_stride;
+       else
+               map->reg_stride = 1;
+       map->use_single_rw = config->use_single_rw;
        map->dev = dev;
        map->bus = bus;
        map->bus_context = bus_context;
@@ -248,7 +275,7 @@ struct regmap *regmap_init(struct device *dev,
                map->read_flag_mask = bus->read_flag_mask;
        }
 
-       switch (config->reg_bits) {
+       switch (config->reg_bits + map->reg_shift) {
        case 2:
                switch (config->val_bits) {
                case 6:
@@ -314,12 +341,19 @@ struct regmap *regmap_init(struct device *dev,
                map->format.format_val = regmap_format_16;
                map->format.parse_val = regmap_parse_16;
                break;
+       case 24:
+               map->format.format_val = regmap_format_24;
+               map->format.parse_val = regmap_parse_24;
+               break;
        case 32:
                map->format.format_val = regmap_format_32;
                map->format.parse_val = regmap_parse_32;
                break;
        }
 
+       if (map->format.format_write)
+               map->use_single_rw = true;
+
        if (!map->format.format_write &&
            !(map->format.format_reg && map->format.format_val))
                goto err_map;
@@ -330,7 +364,7 @@ struct regmap *regmap_init(struct device *dev,
                goto err_map;
        }
 
-       regmap_debugfs_init(map);
+       regmap_debugfs_init(map, config->name);
 
        ret = regcache_init(map, config);
        if (ret < 0)
@@ -426,7 +460,7 @@ int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
        map->precious_reg = config->precious_reg;
        map->cache_type = config->cache_type;
 
-       regmap_debugfs_init(map);
+       regmap_debugfs_init(map, config->name);
 
        map->cache_bypass = false;
        map->cache_only = false;
@@ -502,7 +536,8 @@ static int _regmap_raw_write(struct regmap *map, unsigned int reg,
        /* Check for unwritable registers before we start */
        if (map->writeable_reg)
                for (i = 0; i < val_len / map->format.val_bytes; i++)
-                       if (!map->writeable_reg(map->dev, reg + i))
+                       if (!map->writeable_reg(map->dev,
+                                               reg + (i * map->reg_stride)))
                                return -EINVAL;
 
        if (!map->cache_bypass && map->format.parse_val) {
@@ -511,7 +546,8 @@ static int _regmap_raw_write(struct regmap *map, unsigned int reg,
                for (i = 0; i < val_len / val_bytes; i++) {
                        memcpy(map->work_buf, val + (i * val_bytes), val_bytes);
                        ival = map->format.parse_val(map->work_buf);
-                       ret = regcache_write(map, reg + i, ival);
+                       ret = regcache_write(map, reg + (i * map->reg_stride),
+                                            ival);
                        if (ret) {
                                dev_err(map->dev,
                                   "Error in caching of register: %u ret: %d\n",
@@ -525,7 +561,7 @@ static int _regmap_raw_write(struct regmap *map, unsigned int reg,
                }
        }
 
-       map->format.format_reg(map->work_buf, reg);
+       map->format.format_reg(map->work_buf, reg, map->reg_shift);
 
        u8[0] |= map->write_flag_mask;
 
@@ -600,7 +636,7 @@ int _regmap_write(struct regmap *map, unsigned int reg,
                return ret;
        } else {
                map->format.format_val(map->work_buf + map->format.reg_bytes
-                                      + map->format.pad_bytes, val);
+                                      + map->format.pad_bytes, val, 0);
                return _regmap_raw_write(map, reg,
                                         map->work_buf +
                                         map->format.reg_bytes +
@@ -623,6 +659,9 @@ int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
 {
        int ret;
 
+       if (reg % map->reg_stride)
+               return -EINVAL;
+
        map->lock(map);
 
        ret = _regmap_write(map, reg, val);
@@ -656,6 +695,8 @@ int regmap_raw_write(struct regmap *map, unsigned int reg,
 
        if (val_len % map->format.val_bytes)
                return -EINVAL;
+       if (reg % map->reg_stride)
+               return -EINVAL;
 
        map->lock(map);
 
@@ -690,6 +731,8 @@ int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
 
        if (!map->format.parse_val)
                return -EINVAL;
+       if (reg % map->reg_stride)
+               return -EINVAL;
 
        map->lock(map);
 
@@ -706,7 +749,22 @@ int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
                for (i = 0; i < val_count * val_bytes; i += val_bytes)
                        map->format.parse_val(wval + i);
        }
-       ret = _regmap_raw_write(map, reg, wval, val_bytes * val_count);
+       /*
+        * Some devices does not support bulk write, for
+        * them we have a series of single write operations.
+        */
+       if (map->use_single_rw) {
+               for (i = 0; i < val_count; i++) {
+                       ret = regmap_raw_write(map,
+                                               reg + (i * map->reg_stride),
+                                               val + (i * val_bytes),
+                                               val_bytes);
+                       if (ret != 0)
+                               return ret;
+               }
+       } else {
+               ret = _regmap_raw_write(map, reg, wval, val_bytes * val_count);
+       }
 
        if (val_bytes != 1)
                kfree(wval);
@@ -723,7 +781,7 @@ static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
        u8 *u8 = map->work_buf;
        int ret;
 
-       map->format.format_reg(map->work_buf, reg);
+       map->format.format_reg(map->work_buf, reg, map->reg_shift);
 
        /*
         * Some buses or devices flag reads by setting the high bits in the
@@ -769,6 +827,9 @@ static int _regmap_read(struct regmap *map, unsigned int reg,
                trace_regmap_reg_read(map->dev, reg, *val);
        }
 
+       if (ret == 0 && !map->cache_bypass)
+               regcache_write(map, reg, *val);
+
        return ret;
 }
 
@@ -786,6 +847,9 @@ int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
 {
        int ret;
 
+       if (reg % map->reg_stride)
+               return -EINVAL;
+
        map->lock(map);
 
        ret = _regmap_read(map, reg, val);
@@ -817,6 +881,8 @@ int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
 
        if (val_len % map->format.val_bytes)
                return -EINVAL;
+       if (reg % map->reg_stride)
+               return -EINVAL;
 
        map->lock(map);
 
@@ -830,11 +896,12 @@ int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
                 * cost as we expect to hit the cache.
                 */
                for (i = 0; i < val_count; i++) {
-                       ret = _regmap_read(map, reg + i, &v);
+                       ret = _regmap_read(map, reg + (i * map->reg_stride),
+                                          &v);
                        if (ret != 0)
                                goto out;
 
-                       map->format.format_val(val + (i * val_bytes), v);
+                       map->format.format_val(val + (i * val_bytes), v, 0);
                }
        }
 
@@ -865,19 +932,40 @@ int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
 
        if (!map->format.parse_val)
                return -EINVAL;
+       if (reg % map->reg_stride)
+               return -EINVAL;
 
        if (vol || map->cache_type == REGCACHE_NONE) {
-               ret = regmap_raw_read(map, reg, val, val_bytes * val_count);
-               if (ret != 0)
-                       return ret;
+               /*
+                * Some devices does not support bulk read, for
+                * them we have a series of single read operations.
+                */
+               if (map->use_single_rw) {
+                       for (i = 0; i < val_count; i++) {
+                               ret = regmap_raw_read(map,
+                                               reg + (i * map->reg_stride),
+                                               val + (i * val_bytes),
+                                               val_bytes);
+                               if (ret != 0)
+                                       return ret;
+                       }
+               } else {
+                       ret = regmap_raw_read(map, reg, val,
+                                             val_bytes * val_count);
+                       if (ret != 0)
+                               return ret;
+               }
 
                for (i = 0; i < val_count * val_bytes; i += val_bytes)
                        map->format.parse_val(val + i);
        } else {
                for (i = 0; i < val_count; i++) {
-                       ret = regmap_read(map, reg + i, val + (i * val_bytes));
+                       unsigned int ival;
+                       ret = regmap_read(map, reg + (i * map->reg_stride),
+                                         &ival);
                        if (ret != 0)
                                return ret;
+                       memcpy(val + (i * val_bytes), &ival, val_bytes);
                }
        }