From: Neil Armstrong <narmstrong@baylibre.com>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH u-boot v3 1/3] regmap: add regmap_read_poll_timeout() helper
Date: Wed, 14 Nov 2018 11:25:18 +0100 [thread overview]
Message-ID: <20181114102520.25346-2-narmstrong@baylibre.com> (raw)
In-Reply-To: <20181114102520.25346-1-narmstrong@baylibre.com>
Add the regmap_read_poll_timeout() macro based on the Linux implementation
to simplify register polling with configurable timeout and sleep.
Signed-off-by: Neil Armstrong <narmstrong@baylibre.com>
---
include/regmap.h | 38 ++++++++++++++++++++++++++++++++++++++
1 file changed, 38 insertions(+)
diff --git a/include/regmap.h b/include/regmap.h
index 6a574eaa41..cf795c455f 100644
--- a/include/regmap.h
+++ b/include/regmap.h
@@ -42,6 +42,44 @@ int regmap_read(struct regmap *map, uint offset, uint *valp);
#define regmap_read32(map, ptr, member, valp) \
regmap_read(map, (uint32_t *)(ptr)->member - (uint32_t *)(ptr), valp)
+/**
+ * regmap_read_poll_timeout - Poll until a condition is met or a timeout occurs
+ *
+ * @map: Regmap to read from
+ * @addr: Offset to poll
+ * @val: Unsigned integer variable to read the value into
+ * @cond: Break condition (usually involving @val)
+ * @sleep_us: Maximum time to sleep between reads in us (0 tight-loops).
+ * @timeout_ms: Timeout in ms, 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. Must not be called
+ * from atomic context if sleep_us or timeout_us are used.
+ *
+ * This is modelled after the regmap_read_poll_timeout macros in linux but
+ * with millisecond timeout.
+ */
+#define regmap_read_poll_timeout(map, addr, val, cond, sleep_us, timeout_ms) \
+({ \
+ unsigned long __start = get_timer(0); \
+ int __ret; \
+ for (;;) { \
+ __ret = regmap_read((map), (addr), &(val)); \
+ if (__ret) \
+ break; \
+ if (cond) \
+ break; \
+ if ((timeout_ms) && get_timer(__start) > (timeout_ms)) { \
+ __ret = regmap_read((map), (addr), &(val)); \
+ break; \
+ } \
+ if ((sleep_us)) \
+ udelay((sleep_us)); \
+ } \
+ __ret ?: ((cond) ? 0 : -ETIMEDOUT); \
+})
+
/**
* regmap_update_bits() - Perform a read/modify/write using a mask
*
--
2.19.1
next prev parent reply other threads:[~2018-11-14 10:25 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-11-14 10:25 [U-Boot] [PATCH u-boot v3 0/3] Add Amlogic Meson SPI Flash Controller driver Neil Armstrong
2018-11-14 10:25 ` Neil Armstrong [this message]
2018-11-14 13:09 ` [U-Boot] [PATCH u-boot v3 1/3] regmap: add regmap_read_poll_timeout() helper Jagan Teki
2018-11-14 10:25 ` [U-Boot] [PATCH u-boot v3 2/3] test: regmap: add regmap_read_poll_timeout test Neil Armstrong
2018-11-14 13:09 ` Jagan Teki
2018-11-14 10:25 ` [U-Boot] [PATCH u-boot v3 3/3] spi: Add Amlogic Meson SPI Flash Controller driver Neil Armstrong
2018-11-14 13:11 ` Jagan Teki
2018-11-14 13:12 ` Neil Armstrong
2018-11-15 15:41 ` [U-Boot] [PATCH u-boot v3 0/3] " Jerome Brunet
2018-11-22 6:40 ` Jagan Teki
2018-11-22 8:21 ` Neil Armstrong
-- strict thread matches above, loose matches on Subject: below --
2018-11-22 10:01 Neil Armstrong
2018-11-22 10:01 ` [U-Boot] [PATCH u-boot v3 1/3] regmap: add regmap_read_poll_timeout() helper Neil Armstrong
2018-11-27 1:02 ` Simon Glass
2018-11-27 13:58 ` Neil Armstrong
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20181114102520.25346-2-narmstrong@baylibre.com \
--to=narmstrong@baylibre.com \
--cc=u-boot@lists.denx.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox