1 From c1e3f753f6b85d7636024159bb78f764e09492f1 Mon Sep 17 00:00:00 2001
2 From: Heiner Kallweit <hkallweit1@gmail.com>
3 Date: Sun, 10 Feb 2019 19:57:56 +0100
4 Subject: [PATCH 604/660] net: phy: add register modifying helpers returning 1
7 When modifying registers there are scenarios where we need to know
8 whether the register content actually changed. This patch adds
9 new helpers to not break users of the current ones, phy_modify() etc.
11 Signed-off-by: Heiner Kallweit <hkallweit1@gmail.com>
12 Reviewed-by: Andrew Lunn <andrew@lunn.ch>
13 Reviewed-by: Florian Fainelli <f.fainelli@gmail.com>
14 Signed-off-by: David S. Miller <davem@davemloft.net>
15 Signed-off-by: Russell King <rmk+kernel@armlinux.org.uk>
17 drivers/net/phy/phy-core.c | 127 ++++++++++++++++++++++++++++++++++---
18 include/linux/phy.h | 12 +++-
19 2 files changed, 128 insertions(+), 11 deletions(-)
21 --- a/drivers/net/phy/phy-core.c
22 +++ b/drivers/net/phy/phy-core.c
23 @@ -364,7 +364,7 @@ int phy_write_mmd(struct phy_device *phy
24 EXPORT_SYMBOL(phy_write_mmd);
27 - * __phy_modify() - Convenience function for modifying a PHY register
28 + * __phy_modify_changed() - Convenience function for modifying a PHY register
29 * @phydev: a pointer to a &struct phy_device
30 * @regnum: register number
31 * @mask: bit mask of bits to clear
32 @@ -372,16 +372,69 @@ EXPORT_SYMBOL(phy_write_mmd);
34 * Unlocked helper function which allows a PHY register to be modified as
35 * new register value = (old register value & ~mask) | set
37 + * Returns negative errno, 0 if there was no change, and 1 in case of change
39 -int __phy_modify(struct phy_device *phydev, u32 regnum, u16 mask, u16 set)
40 +int __phy_modify_changed(struct phy_device *phydev, u32 regnum, u16 mask,
46 ret = __phy_read(phydev, regnum);
50 - ret = __phy_write(phydev, regnum, (ret & ~mask) | set);
51 + new = (ret & ~mask) | set;
55 + ret = __phy_write(phydev, regnum, new);
57 + return ret < 0 ? ret : 1;
59 +EXPORT_SYMBOL_GPL(__phy_modify_changed);
62 + * phy_modify_changed - Function for modifying a PHY register
63 + * @phydev: the phy_device struct
64 + * @regnum: register number to modify
65 + * @mask: bit mask of bits to clear
66 + * @set: new value of bits set in mask to write to @regnum
68 + * NOTE: MUST NOT be called from interrupt context,
69 + * because the bus read/write functions may wait for an interrupt
70 + * to conclude the operation.
72 + * Returns negative errno, 0 if there was no change, and 1 in case of change
74 +int phy_modify_changed(struct phy_device *phydev, u32 regnum, u16 mask, u16 set)
78 + mutex_lock(&phydev->mdio.bus->mdio_lock);
79 + ret = __phy_modify_changed(phydev, regnum, mask, set);
80 + mutex_unlock(&phydev->mdio.bus->mdio_lock);
84 +EXPORT_SYMBOL_GPL(phy_modify_changed);
87 + * __phy_modify - Convenience function for modifying a PHY register
88 + * @phydev: the phy_device struct
89 + * @regnum: register number to modify
90 + * @mask: bit mask of bits to clear
91 + * @set: new value of bits set in mask to write to @regnum
93 + * NOTE: MUST NOT be called from interrupt context,
94 + * because the bus read/write functions may wait for an interrupt
95 + * to conclude the operation.
97 +int __phy_modify(struct phy_device *phydev, u32 regnum, u16 mask, u16 set)
101 + ret = __phy_modify_changed(phydev, regnum, mask, set);
103 return ret < 0 ? ret : 0;
105 @@ -411,7 +464,7 @@ int phy_modify(struct phy_device *phydev
106 EXPORT_SYMBOL_GPL(phy_modify);
109 - * __phy_modify_mmd - Convenience function for modifying a register on MMD
110 + * __phy_modify_mmd_changed - Function for modifying a register on MMD
111 * @phydev: the phy_device struct
112 * @devad: the MMD containing register to modify
113 * @regnum: register number to modify
114 @@ -420,17 +473,73 @@ EXPORT_SYMBOL_GPL(phy_modify);
116 * Unlocked helper function which allows a MMD register to be modified as
117 * new register value = (old register value & ~mask) | set
119 + * Returns negative errno, 0 if there was no change, and 1 in case of change
121 -int __phy_modify_mmd(struct phy_device *phydev, int devad, u32 regnum,
123 +int __phy_modify_mmd_changed(struct phy_device *phydev, int devad, u32 regnum,
129 ret = __phy_read_mmd(phydev, devad, regnum);
133 - ret = __phy_write_mmd(phydev, devad, regnum, (ret & ~mask) | set);
134 + new = (ret & ~mask) | set;
138 + ret = __phy_write_mmd(phydev, devad, regnum, new);
140 + return ret < 0 ? ret : 1;
142 +EXPORT_SYMBOL_GPL(__phy_modify_mmd_changed);
145 + * phy_modify_mmd_changed - Function for modifying a register on MMD
146 + * @phydev: the phy_device struct
147 + * @devad: the MMD containing register to modify
148 + * @regnum: register number to modify
149 + * @mask: bit mask of bits to clear
150 + * @set: new value of bits set in mask to write to @regnum
152 + * NOTE: MUST NOT be called from interrupt context,
153 + * because the bus read/write functions may wait for an interrupt
154 + * to conclude the operation.
156 + * Returns negative errno, 0 if there was no change, and 1 in case of change
158 +int phy_modify_mmd_changed(struct phy_device *phydev, int devad, u32 regnum,
163 + mutex_lock(&phydev->mdio.bus->mdio_lock);
164 + ret = __phy_modify_mmd_changed(phydev, devad, regnum, mask, set);
165 + mutex_unlock(&phydev->mdio.bus->mdio_lock);
169 +EXPORT_SYMBOL_GPL(phy_modify_mmd_changed);
172 + * __phy_modify_mmd - Convenience function for modifying a register on MMD
173 + * @phydev: the phy_device struct
174 + * @devad: the MMD containing register to modify
175 + * @regnum: register number to modify
176 + * @mask: bit mask of bits to clear
177 + * @set: new value of bits set in mask to write to @regnum
179 + * NOTE: MUST NOT be called from interrupt context,
180 + * because the bus read/write functions may wait for an interrupt
181 + * to conclude the operation.
183 +int __phy_modify_mmd(struct phy_device *phydev, int devad, u32 regnum,
188 + ret = __phy_modify_mmd_changed(phydev, devad, regnum, mask, set);
190 return ret < 0 ? ret : 0;
192 --- a/include/linux/phy.h
193 +++ b/include/linux/phy.h
194 @@ -797,13 +797,21 @@ int phy_write_mmd(struct phy_device *phy
196 int __phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val);
198 +int __phy_modify_changed(struct phy_device *phydev, u32 regnum, u16 mask,
200 +int phy_modify_changed(struct phy_device *phydev, u32 regnum, u16 mask,
202 int __phy_modify(struct phy_device *phydev, u32 regnum, u16 mask, u16 set);
203 int phy_modify(struct phy_device *phydev, u32 regnum, u16 mask, u16 set);
205 +int __phy_modify_mmd_changed(struct phy_device *phydev, int devad, u32 regnum,
206 + u16 mask, u16 set);
207 +int phy_modify_mmd_changed(struct phy_device *phydev, int devad, u32 regnum,
208 + u16 mask, u16 set);
209 int __phy_modify_mmd(struct phy_device *phydev, int devad, u32 regnum,
210 - u16 mask, u16 set);
211 + u16 mask, u16 set);
212 int phy_modify_mmd(struct phy_device *phydev, int devad, u32 regnum,
213 - u16 mask, u16 set);
214 + u16 mask, u16 set);
217 * __phy_set_bits - Convenience function for setting bits in a PHY register