[PATCH net-next v7 03/10] net: phy: introduce phy_read_mmd_poll_timeout macro

From: Dejin Zheng
Date: Mon Mar 23 2020 - 11:06:41 EST


it is sometimes necessary to poll a phy register by phy_read_mmd()
function until its value satisfies some condition. introduce
phy_read_mmd_poll_timeout() macros that do this.

Suggested-by: Andrew Lunn <andrew@xxxxxxx>
Reviewed-by: Andrew Lunn <andrew@xxxxxxx>
Reviewed-by: Florian Fainelli <f.fainelli@xxxxxxxxx>
Signed-off-by: Dejin Zheng <zhengdejin5@xxxxxxxxx>
---
v6 -> v7:
- adapt to a newly added parameter sleep_before_read.
- add prefix with double underscores for the variable ret to avoid
any variable re-declaration or shadowing.
v5 -> v6:
- no changed
v4 -> v5:
- no changed
v3 -> v4:
- deal with precedence issues for parameter cond.
v2 -> v3:
- modify the parameter order of newly added functions.
phy_read_mmd_poll_timeout(val, cond, sleep_us, timeout_us, \
phydev, devaddr, regnum)
||
\/
phy_read_mmd_poll_timeout(phydev, devaddr regnum, val, cond, \
sleep_us, timeout_us)
v1 -> v2:
- passed a phydev, device address and a reg to replace args...
parameter in phy_read_mmd_poll_timeout() by Andrew Lunn 's
suggestion. Andrew Lunn <andrew@xxxxxxx>, Thanks very much for
your help!
- handle phy_read_mmd return an error(the return value < 0) in
phy_read_mmd_poll_timeout(). Thanks Andrew again.

include/linux/phy.h | 14 ++++++++++++++
1 file changed, 14 insertions(+)

diff --git a/include/linux/phy.h b/include/linux/phy.h
index 36d9dea04016..c172747b4ab2 100644
--- a/include/linux/phy.h
+++ b/include/linux/phy.h
@@ -24,6 +24,7 @@
#include <linux/mod_devicetable.h>
#include <linux/u64_stats_sync.h>
#include <linux/irqreturn.h>
+#include <linux/iopoll.h>

#include <linux/atomic.h>

@@ -784,6 +785,19 @@ static inline int __phy_modify_changed(struct phy_device *phydev, u32 regnum,
*/
int phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum);

+#define phy_read_mmd_poll_timeout(phydev, devaddr, regnum, val, cond, \
+ sleep_us, timeout_us, sleep_before_read) \
+({ \
+ int __ret = read_poll_timeout(phy_read_mmd, val, (cond) || val < 0, \
+ sleep_us, timeout_us, sleep_before_read, \
+ phydev, devaddr, regnum); \
+ if (val < 0) \
+ __ret = val; \
+ if (__ret) \
+ phydev_err(phydev, "%s failed: %d\n", __func__, __ret); \
+ __ret; \
+})
+
/**
* __phy_read_mmd - Convenience function for reading a register
* from an MMD on a given PHY.
--
2.25.0