[PATCH 07/28] mfd: zl3073x: Add macro to wait for register value bits to be cleared
From: Ivan Vecera
Date: Mon Apr 07 2025 - 13:31:23 EST
Sometimes in communication with the device is necessary to set
certain bit(s) in certain register and then the driver has to
wait until these bits are cleared by the device.
Add the macro for this functionality, it will be used by later
commits.
Reviewed-by: Michal Schmidt <mschmidt@xxxxxxxxxx>
Signed-off-by: Ivan Vecera <ivecera@xxxxxxxxxx>
---
include/linux/mfd/zl3073x.h | 29 +++++++++++++++++++++++++++++
1 file changed, 29 insertions(+)
diff --git a/include/linux/mfd/zl3073x.h b/include/linux/mfd/zl3073x.h
index 3524426f0e3ba..15dfb0d8bf3cb 100644
--- a/include/linux/mfd/zl3073x.h
+++ b/include/linux/mfd/zl3073x.h
@@ -143,4 +143,33 @@ int zl3073x_write_##_name(struct zl3073x_dev *zldev, unsigned int idx, \
#define ZL3073X_REG48_IDX_DEF(_name, _addr, _num, _stride) \
__ZL3073X_REG_IDX_DEF(_name, _addr, 6, u64, _num, _stride)
+/**
+ * zl3073x_wait_clear_bits - wait for specific bits to be cleared
+ * _zldev: pointer to device structure
+ * _reg: register name
+ * _bits: bits that should be cleared
+ * _index: optional index for indexed register
+ *
+ * The macro waits up to @READ_TIMEOUT_US microseconds for @_bits in @_reg
+ * to be cleared.
+ *
+ * Returns:
+ * -ETIMEDOUT: if timeout occurred
+ * <0: for other errors occurred during communication
+ * 0: success
+ */
+#define READ_SLEEP_US 10
+#define READ_TIMEOUT_US 2000000
+#define zl3073x_wait_clear_bits(_zldev, _reg, _bits, _index...) \
+({ \
+ zl3073x_##_reg##_t __val; \
+ int __rc; \
+ if (read_poll_timeout(zl3073x_read_##_reg, __rc, \
+ __rc || !((_bits) & __val), \
+ READ_SLEEP_US, READ_TIMEOUT_US, false, \
+ _zldev, ##_index, &__val)) \
+ __rc = -ETIMEDOUT; \
+ __rc; \
+})
+
#endif /* __LINUX_MFD_ZL3073X_H */
--
2.48.1