regmap: provide helpers for simple bit operations
authorBartosz Golaszewski <bgolaszewski@baylibre.com>
Thu, 28 May 2020 15:45:02 +0000 (17:45 +0200)
committerMark Brown <broonie@kernel.org>
Fri, 29 May 2020 13:00:42 +0000 (14:00 +0100)
In many instances regmap_update_bits() is used for simple bit setting
and clearing. In these cases the last argument is redundant and we can
hide it with a static inline function.

This adds three new helpers for simple bit operations: set_bits,
clear_bits and test_bits (the last one defined as a regular function).

Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
Link: https://lore.kernel.org/r/20200528154503.26304-2-brgl@bgdev.pl
Signed-off-by: Mark Brown <broonie@kernel.org>
drivers/base/regmap/regmap.c
include/linux/regmap.h

index 59f911e..4ad5c5a 100644 (file)
@@ -2936,6 +2936,28 @@ int regmap_update_bits_base(struct regmap *map, unsigned int reg,
 }
 EXPORT_SYMBOL_GPL(regmap_update_bits_base);
 
+/**
+ * regmap_test_bits() - Check if all specified bits are set in a register.
+ *
+ * @map: Register map to operate on
+ * @reg: Register to read from
+ * @bits: Bits to test
+ *
+ * Returns -1 if the underlying regmap_read() fails, 0 if at least one of the
+ * tested bits is not set and 1 if all tested bits are set.
+ */
+int regmap_test_bits(struct regmap *map, unsigned int reg, unsigned int bits)
+{
+       unsigned int val, ret;
+
+       ret = regmap_read(map, reg, &val);
+       if (ret)
+               return ret;
+
+       return (val & bits) == bits;
+}
+EXPORT_SYMBOL_GPL(regmap_test_bits);
+
 void regmap_async_complete_cb(struct regmap_async *async, int ret)
 {
        struct regmap *map = async->map;
index 40b0716..ddf0baf 100644 (file)
@@ -1111,6 +1111,21 @@ bool regmap_reg_in_ranges(unsigned int reg,
                          const struct regmap_range *ranges,
                          unsigned int nranges);
 
+static inline int regmap_set_bits(struct regmap *map,
+                                 unsigned int reg, unsigned int bits)
+{
+       return regmap_update_bits_base(map, reg, bits, bits,
+                                      NULL, false, false);
+}
+
+static inline int regmap_clear_bits(struct regmap *map,
+                                   unsigned int reg, unsigned int bits)
+{
+       return regmap_update_bits_base(map, reg, bits, 0, NULL, false, false);
+}
+
+int regmap_test_bits(struct regmap *map, unsigned int reg, unsigned int bits);
+
 /**
  * struct reg_field - Description of an register field
  *
@@ -1410,6 +1425,27 @@ static inline int regmap_update_bits_base(struct regmap *map, unsigned int reg,
        return -EINVAL;
 }
 
+static inline int regmap_set_bits(struct regmap *map,
+                                 unsigned int reg, unsigned int bits)
+{
+       WARN_ONCE(1, "regmap API is disabled");
+       return -EINVAL;
+}
+
+static inline int regmap_clear_bits(struct regmap *map,
+                                   unsigned int reg, unsigned int bits)
+{
+       WARN_ONCE(1, "regmap API is disabled");
+       return -EINVAL;
+}
+
+static inline int regmap_test_bits(struct regmap *map,
+                                  unsigned int reg, unsigned int bits)
+{
+       WARN_ONCE(1, "regmap API is disabled");
+       return -EINVAL;
+}
+
 static inline int regmap_field_update_bits_base(struct regmap_field *field,
                                        unsigned int mask, unsigned int val,
                                        bool *change, bool async, bool force)