[U-Boot] [PATCH v3 05/13] regmap: Add support for polling on a register

Faiz Abbas faiz_abbas at ti.com
Tue Feb 12 08:58:11 UTC 2019


Add an API to continuously read a register until a condition is
satisfied or a timeout occurs.

Signed-off-by: Faiz Abbas <faiz_abbas at ti.com>
Reviewed-by: Tom Rini <trini at konsulko.com>
---
 include/regmap.h | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)

diff --git a/include/regmap.h b/include/regmap.h
index 3cd97d3b94..dfc3a4f231 100644
--- a/include/regmap.h
+++ b/include/regmap.h
@@ -299,4 +299,38 @@ void *regmap_get_range(struct regmap *map, unsigned int range_num);
  */
 int regmap_uninit(struct regmap *map);
 
+/**
+ * regmap_read_poll_timeout - Poll until a condition is met or a timeout occurs
+ *
+ * @map: Regmap to read from
+ * @addr: Address to poll
+ * @val: Unsigned integer variable to read the value into
+ * @cond: Break condition (usually involving @val)
+ * @timeout_us: Timeout in us, 0 means never timeout
+ *
+ * Returns 0 on success and -ETIMEDOUT upon a timeout or the regmap_read
+ * error return value in case of a error read. In the two former cases,
+ * the last read value at @addr is stored in @val.
+ *
+ * Modelled after readx_poll_timeout macro in linux/iopoll.h
+ */
+
+#define regmap_read_poll_timeout(map, addr, val, cond, timeout_us)	\
+({ \
+	unsigned long timeout = timer_get_us() + timeout_us; \
+	int pollret; \
+	for (;;) { \
+		pollret = regmap_read((map), (addr), &(val)); \
+		if (pollret) \
+			break; \
+		if (cond) \
+			break; \
+		if (timeout_us && time_after(timer_get_us(), timeout)) { \
+			pollret = regmap_read((map), (addr), &(val)); \
+			break; \
+		} \
+	} \
+	pollret ?: ((cond) ? 0 : -ETIMEDOUT); \
+})
+
 #endif
-- 
2.19.2



More information about the U-Boot mailing list