regmap: Add regmap_noinc_write API
authorBen Whitten <ben.whitten@gmail.com>
Fri, 19 Oct 2018 09:33:50 +0000 (10:33 +0100)
committerMark Brown <broonie@kernel.org>
Fri, 19 Oct 2018 11:51:19 +0000 (12:51 +0100)
The regmap API had a noinc_read function added for instances where devices
supported returning data from an internal FIFO in a single read.

This commit adds the noinc_write variant to allow writing to a non
incrementing register, this is used in devices such as the sx1301 for
loading firmware.

Signed-off-by: Ben Whitten <ben.whitten@lairdtech.com>
Signed-off-by: Mark Brown <broonie@kernel.org>
drivers/base/regmap/internal.h
drivers/base/regmap/regmap.c
include/linux/regmap.h

index a6bf34d6394ed2e48e37d24727af19842e1a9b1a..404f123cbe5536bf57403ac379c250bc52b4eef1 100644 (file)
@@ -94,11 +94,13 @@ struct regmap {
        bool (*readable_reg)(struct device *dev, unsigned int reg);
        bool (*volatile_reg)(struct device *dev, unsigned int reg);
        bool (*precious_reg)(struct device *dev, unsigned int reg);
+       bool (*writeable_noinc_reg)(struct device *dev, unsigned int reg);
        bool (*readable_noinc_reg)(struct device *dev, unsigned int reg);
        const struct regmap_access_table *wr_table;
        const struct regmap_access_table *rd_table;
        const struct regmap_access_table *volatile_table;
        const struct regmap_access_table *precious_table;
+       const struct regmap_access_table *wr_noinc_table;
        const struct regmap_access_table *rd_noinc_table;
 
        int (*reg_read)(void *context, unsigned int reg, unsigned int *val);
@@ -183,6 +185,7 @@ bool regmap_writeable(struct regmap *map, unsigned int reg);
 bool regmap_readable(struct regmap *map, unsigned int reg);
 bool regmap_volatile(struct regmap *map, unsigned int reg);
 bool regmap_precious(struct regmap *map, unsigned int reg);
+bool regmap_writeable_noinc(struct regmap *map, unsigned int reg);
 bool regmap_readable_noinc(struct regmap *map, unsigned int reg);
 
 int _regmap_write(struct regmap *map, unsigned int reg,
index 0360a90ad6b623530f0053dbc3b3d29748266aae..d4f1fc64260054759ebb0f55bf80269cedb9c2e9 100644 (file)
@@ -168,6 +168,17 @@ bool regmap_precious(struct regmap *map, unsigned int reg)
        return false;
 }
 
+bool regmap_writeable_noinc(struct regmap *map, unsigned int reg)
+{
+       if (map->writeable_noinc_reg)
+               return map->writeable_noinc_reg(map->dev, reg);
+
+       if (map->wr_noinc_table)
+               return regmap_check_range_table(map, reg, map->wr_noinc_table);
+
+       return true;
+}
+
 bool regmap_readable_noinc(struct regmap *map, unsigned int reg)
 {
        if (map->readable_noinc_reg)
@@ -777,11 +788,13 @@ struct regmap *__regmap_init(struct device *dev,
        map->rd_table = config->rd_table;
        map->volatile_table = config->volatile_table;
        map->precious_table = config->precious_table;
+       map->wr_noinc_table = config->wr_noinc_table;
        map->rd_noinc_table = config->rd_noinc_table;
        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->writeable_noinc_reg = config->writeable_noinc_reg;
        map->readable_noinc_reg = config->readable_noinc_reg;
        map->cache_type = config->cache_type;
 
@@ -1298,6 +1311,7 @@ int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
        map->readable_reg = config->readable_reg;
        map->volatile_reg = config->volatile_reg;
        map->precious_reg = config->precious_reg;
+       map->writeable_noinc_reg = config->writeable_noinc_reg;
        map->readable_noinc_reg = config->readable_noinc_reg;
        map->cache_type = config->cache_type;
 
@@ -1897,6 +1911,69 @@ int regmap_raw_write(struct regmap *map, unsigned int reg,
 }
 EXPORT_SYMBOL_GPL(regmap_raw_write);
 
+/**
+ * regmap_noinc_write(): Write data from a register without incrementing the
+ *                     register number
+ *
+ * @map: Register map to write to
+ * @reg: Register to write to
+ * @val: Pointer to data buffer
+ * @val_len: Length of output buffer in bytes.
+ *
+ * The regmap API usually assumes that bulk bus write operations will write a
+ * range of registers. Some devices have certain registers for which a write
+ * operation can write to an internal FIFO.
+ *
+ * The target register must be volatile but registers after it can be
+ * completely unrelated cacheable registers.
+ *
+ * This will attempt multiple writes as required to write val_len bytes.
+ *
+ * A value of zero will be returned on success, a negative errno will be
+ * returned in error cases.
+ */
+int regmap_noinc_write(struct regmap *map, unsigned int reg,
+                     const void *val, size_t val_len)
+{
+       size_t write_len;
+       int ret;
+
+       if (!map->bus)
+               return -EINVAL;
+       if (!map->bus->write)
+               return -ENOTSUPP;
+       if (val_len % map->format.val_bytes)
+               return -EINVAL;
+       if (!IS_ALIGNED(reg, map->reg_stride))
+               return -EINVAL;
+       if (val_len == 0)
+               return -EINVAL;
+
+       map->lock(map->lock_arg);
+
+       if (!regmap_volatile(map, reg) || !regmap_writeable_noinc(map, reg)) {
+               ret = -EINVAL;
+               goto out_unlock;
+       }
+
+       while (val_len) {
+               if (map->max_raw_write && map->max_raw_write < val_len)
+                       write_len = map->max_raw_write;
+               else
+                       write_len = val_len;
+               ret = _regmap_raw_write(map, reg, val, write_len);
+               if (ret)
+                       goto out_unlock;
+               val = ((u8 *)val) + write_len;
+               val_len -= write_len;
+       }
+
+out_unlock:
+       map->unlock(map->lock_arg);
+       return ret;
+}
+EXPORT_SYMBOL_GPL(regmap_noinc_write);
+
 /**
  * regmap_field_update_bits_base() - Perform a read/modify/write cycle a
  *                                   register field.
index 379505a53722fdeef727edc9c9d6c58cdbef4d4d..de04dc46c7a96fe302a7445013684ca2fd0ab969 100644 (file)
@@ -268,6 +268,13 @@ typedef void (*regmap_unlock)(void *);
  *                field is NULL but precious_table (see below) is not, the
  *                check is performed on such table (a register is precious if
  *                it belongs to one of the ranges specified by precious_table).
+ * @writeable_noinc_reg: Optional callback returning true if the register
+ *                     supports multiple write operations without incrementing
+ *                     the register number. If this field is NULL but
+ *                     wr_noinc_table (see below) is not, the check is
+ *                     performed on such table (a register is no increment
+ *                     writeable if it belongs to one of the ranges specified
+ *                     by wr_noinc_table).
  * @readable_noinc_reg: Optional callback returning true if the register
  *                     supports multiple read operations without incrementing
  *                     the register number. If this field is NULL but
@@ -302,6 +309,7 @@ typedef void (*regmap_unlock)(void *);
  * @rd_table:     As above, for read access.
  * @volatile_table: As above, for volatile registers.
  * @precious_table: As above, for precious registers.
+ * @wr_noinc_table: As above, for no increment writeable registers.
  * @rd_noinc_table: As above, for no increment readable registers.
  * @reg_defaults: Power on reset values for registers (for use with
  *                register cache support).
@@ -352,6 +360,7 @@ struct regmap_config {
        bool (*readable_reg)(struct device *dev, unsigned int reg);
        bool (*volatile_reg)(struct device *dev, unsigned int reg);
        bool (*precious_reg)(struct device *dev, unsigned int reg);
+       bool (*writeable_noinc_reg)(struct device *dev, unsigned int reg);
        bool (*readable_noinc_reg)(struct device *dev, unsigned int reg);
 
        bool disable_locking;
@@ -369,6 +378,7 @@ struct regmap_config {
        const struct regmap_access_table *rd_table;
        const struct regmap_access_table *volatile_table;
        const struct regmap_access_table *precious_table;
+       const struct regmap_access_table *wr_noinc_table;
        const struct regmap_access_table *rd_noinc_table;
        const struct reg_default *reg_defaults;
        unsigned int num_reg_defaults;
@@ -979,6 +989,8 @@ int regmap_write(struct regmap *map, unsigned int reg, unsigned int val);
 int regmap_write_async(struct regmap *map, unsigned int reg, unsigned int val);
 int regmap_raw_write(struct regmap *map, unsigned int reg,
                     const void *val, size_t val_len);
+int regmap_noinc_write(struct regmap *map, unsigned int reg,
+                    const void *val, size_t val_len);
 int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
                        size_t val_count);
 int regmap_multi_reg_write(struct regmap *map, const struct reg_sequence *regs,
@@ -1222,6 +1234,13 @@ static inline int regmap_raw_write_async(struct regmap *map, unsigned int reg,
        return -EINVAL;
 }
 
+static inline int regmap_noinc_write(struct regmap *map, unsigned int reg,
+                                   const void *val, size_t val_len)
+{
+       WARN_ONCE(1, "regmap API is disabled");
+       return -EINVAL;
+}
+
 static inline int regmap_bulk_write(struct regmap *map, unsigned int reg,
                                    const void *val, size_t val_count)
 {