diff mbox series

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

Message ID 20190128064531.3331-6-faiz_abbas@ti.com
State Superseded
Delegated to: Tom Rini
Headers show
Series Add Support for eMMC in AM65x-evm | expand

Commit Message

Faiz Abbas Jan. 28, 2019, 6:45 a.m. UTC
Add an API to continuously read a register until a condition is
satisfied or a timeout occurs.

Signed-off-by: Faiz Abbas <faiz_abbas@ti.com>
---
 include/regmap.h | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)

Comments

Tom Rini Jan. 30, 2019, 2:19 a.m. UTC | #1
On Mon, Jan 28, 2019 at 12:15:23PM +0530, Faiz Abbas wrote:

> Add an API to continuously read a register until a condition is
> satisfied or a timeout occurs.
> 
> Signed-off-by: Faiz Abbas <faiz_abbas@ti.com>

Reviewed-by: Tom Rini <trini@konsulko.com>
diff mbox series

Patch

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