iio: adis: Add adis_update_bits() APIs
authorNuno Sá <nuno.sa@analog.com>
Mon, 13 Apr 2020 08:24:42 +0000 (10:24 +0200)
committerJonathan Cameron <Jonathan.Cameron@huawei.com>
Sat, 25 Apr 2020 15:10:44 +0000 (16:10 +0100)
This patch adds a `regmap_update_bits()` like API to the ADIS library.
It provides locked and unlocked variant.

Signed-off-by: Nuno Sá <nuno.sa@analog.com>
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
drivers/iio/imu/adis.c
include/linux/iio/imu/adis.h

index 2e7d0d3..c539dfa 100644 (file)
@@ -223,6 +223,31 @@ int __adis_read_reg(struct adis *adis, unsigned int reg,
        return ret;
 }
 EXPORT_SYMBOL_GPL(__adis_read_reg);
+/**
+ * __adis_update_bits_base() - ADIS Update bits function - Unlocked version
+ * @adis: The adis device
+ * @reg: The address of the lower of the two registers
+ * @mask: Bitmask to change
+ * @val: Value to be written
+ * @size: Size of the register to update
+ *
+ * Updates the desired bits of @reg in accordance with @mask and @val.
+ */
+int __adis_update_bits_base(struct adis *adis, unsigned int reg, const u32 mask,
+                           const u32 val, u8 size)
+{
+       int ret;
+       u32 __val;
+
+       ret = __adis_read_reg(adis, reg, &__val, size);
+       if (ret)
+               return ret;
+
+       __val = (__val & ~mask) | (val & mask);
+
+       return __adis_write_reg(adis, reg, __val, size);
+}
+EXPORT_SYMBOL_GPL(__adis_update_bits_base);
 
 #ifdef CONFIG_DEBUG_FS
 
index 70e4d1d..247fc4c 100644 (file)
@@ -333,6 +333,65 @@ static inline int adis_read_reg_32(struct adis *adis, unsigned int reg,
        return ret;
 }
 
+int __adis_update_bits_base(struct adis *adis, unsigned int reg, const u32 mask,
+                           const u32 val, u8 size);
+/**
+ * adis_update_bits_base() - ADIS Update bits function - Locked version
+ * @adis: The adis device
+ * @reg: The address of the lower of the two registers
+ * @mask: Bitmask to change
+ * @val: Value to be written
+ * @size: Size of the register to update
+ *
+ * Updates the desired bits of @reg in accordance with @mask and @val.
+ */
+static inline int adis_update_bits_base(struct adis *adis, unsigned int reg,
+                                       const u32 mask, const u32 val, u8 size)
+{
+       int ret;
+
+       mutex_lock(&adis->state_lock);
+       ret = __adis_update_bits_base(adis, reg, mask, val, size);
+       mutex_unlock(&adis->state_lock);
+       return ret;
+}
+
+/**
+ * adis_update_bits() - Wrapper macro for adis_update_bits_base - Locked version
+ * @adis: The adis device
+ * @reg: The address of the lower of the two registers
+ * @mask: Bitmask to change
+ * @val: Value to be written
+ *
+ * This macro evaluates the sizeof of @val at compile time and calls
+ * adis_update_bits_base() accordingly. Be aware that using MACROS/DEFINES for
+ * @val can lead to undesired behavior if the register to update is 16bit.
+ */
+#define adis_update_bits(adis, reg, mask, val) ({                      \
+       BUILD_BUG_ON(sizeof(val) == 1 || sizeof(val) == 8);             \
+       __builtin_choose_expr(sizeof(val) == 4,                         \
+               adis_update_bits_base(adis, reg, mask, val, 4),         \
+               adis_update_bits_base(adis, reg, mask, val, 2));        \
+})
+
+/**
+ * adis_update_bits() - Wrapper macro for adis_update_bits_base
+ * @adis: The adis device
+ * @reg: The address of the lower of the two registers
+ * @mask: Bitmask to change
+ * @val: Value to be written
+ *
+ * This macro evaluates the sizeof of @val at compile time and calls
+ * adis_update_bits_base() accordingly. Be aware that using MACROS/DEFINES for
+ * @val can lead to undesired behavior if the register to update is 16bit.
+ */
+#define __adis_update_bits(adis, reg, mask, val) ({                    \
+       BUILD_BUG_ON(sizeof(val) == 1 || sizeof(val) == 8);             \
+       __builtin_choose_expr(sizeof(val) == 4,                         \
+               __adis_update_bits_base(adis, reg, mask, val, 4),       \
+               __adis_update_bits_base(adis, reg, mask, val, 2));      \
+})
+
 int adis_enable_irq(struct adis *adis, bool enable);
 int __adis_check_status(struct adis *adis);
 int __adis_initial_startup(struct adis *adis);