3bd36f6ffe2f7a08a2f709074ab83939343541cd
[openwrt/staging/wigyori.git] /
1 From f508a226b517a6a8afd78a317de46bc83e3e3d51 Mon Sep 17 00:00:00 2001
2 From: Christian Marangi <ansuelsmth@gmail.com>
3 Date: Tue, 6 Feb 2024 18:31:13 +0100
4 Subject: [PATCH 10/10] net: phy: qca807x: add support for configurable LED
5
6 QCA8072/5 have up to 2 LEDs attached for PHY.
7
8 LEDs can be configured to be ON/hw blink or be set to HW control.
9
10 Hw blink mode is set to blink at 4Hz or 250ms.
11
12 PHY can support both copper (TP) or fiber (FIBRE) kind and supports
13 different HW control modes based on the port type.
14
15 HW control modes supported for netdev trigger for copper ports are:
16 - LINK_10
17 - LINK_100
18 - LINK_1000
19 - TX
20 - RX
21 - FULL_DUPLEX
22 - HALF_DUPLEX
23
24 HW control modes supported for netdev trigger for fiber ports are:
25 - LINK_100
26 - LINK_1000
27 - TX
28 - RX
29 - FULL_DUPLEX
30 - HALF_DUPLEX
31
32 LED support conflicts with GPIO controller feature and must be disabled
33 if gpio-controller is used for the PHY.
34
35 Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
36 Signed-off-by: David S. Miller <davem@davemloft.net>
37 ---
38 drivers/net/phy/qcom/qca807x.c | 256 ++++++++++++++++++++++++++++++++-
39 1 file changed, 254 insertions(+), 2 deletions(-)
40
41 --- a/drivers/net/phy/qcom/qca807x.c
42 +++ b/drivers/net/phy/qcom/qca807x.c
43 @@ -57,8 +57,18 @@
44 #define QCA807X_MMD7_LED_CTRL(x) (0x8074 + ((x) * 2))
45 #define QCA807X_MMD7_LED_FORCE_CTRL(x) (0x8075 + ((x) * 2))
46
47 -#define QCA807X_GPIO_FORCE_EN BIT(15)
48 -#define QCA807X_GPIO_FORCE_MODE_MASK GENMASK(14, 13)
49 +/* LED hw control pattern for fiber port */
50 +#define QCA807X_LED_FIBER_PATTERN_MASK GENMASK(11, 1)
51 +#define QCA807X_LED_FIBER_TXACT_BLK_EN BIT(10)
52 +#define QCA807X_LED_FIBER_RXACT_BLK_EN BIT(9)
53 +#define QCA807X_LED_FIBER_FDX_ON_EN BIT(6)
54 +#define QCA807X_LED_FIBER_HDX_ON_EN BIT(5)
55 +#define QCA807X_LED_FIBER_1000BX_ON_EN BIT(2)
56 +#define QCA807X_LED_FIBER_100FX_ON_EN BIT(1)
57 +
58 +/* Some device repurpose the LED as GPIO out */
59 +#define QCA807X_GPIO_FORCE_EN QCA808X_LED_FORCE_EN
60 +#define QCA807X_GPIO_FORCE_MODE_MASK QCA808X_LED_FORCE_MODE_MASK
61
62 #define QCA807X_FUNCTION_CONTROL 0x10
63 #define QCA807X_FC_MDI_CROSSOVER_MODE_MASK GENMASK(6, 5)
64 @@ -121,6 +131,233 @@ static int qca807x_cable_test_start(stru
65 return 0;
66 }
67
68 +static int qca807x_led_parse_netdev(struct phy_device *phydev, unsigned long rules,
69 + u16 *offload_trigger)
70 +{
71 + /* Parsing specific to netdev trigger */
72 + switch (phydev->port) {
73 + case PORT_TP:
74 + if (test_bit(TRIGGER_NETDEV_TX, &rules))
75 + *offload_trigger |= QCA808X_LED_TX_BLINK;
76 + if (test_bit(TRIGGER_NETDEV_RX, &rules))
77 + *offload_trigger |= QCA808X_LED_RX_BLINK;
78 + if (test_bit(TRIGGER_NETDEV_LINK_10, &rules))
79 + *offload_trigger |= QCA808X_LED_SPEED10_ON;
80 + if (test_bit(TRIGGER_NETDEV_LINK_100, &rules))
81 + *offload_trigger |= QCA808X_LED_SPEED100_ON;
82 + if (test_bit(TRIGGER_NETDEV_LINK_1000, &rules))
83 + *offload_trigger |= QCA808X_LED_SPEED1000_ON;
84 + if (test_bit(TRIGGER_NETDEV_HALF_DUPLEX, &rules))
85 + *offload_trigger |= QCA808X_LED_HALF_DUPLEX_ON;
86 + if (test_bit(TRIGGER_NETDEV_FULL_DUPLEX, &rules))
87 + *offload_trigger |= QCA808X_LED_FULL_DUPLEX_ON;
88 + break;
89 + case PORT_FIBRE:
90 + if (test_bit(TRIGGER_NETDEV_TX, &rules))
91 + *offload_trigger |= QCA807X_LED_FIBER_TXACT_BLK_EN;
92 + if (test_bit(TRIGGER_NETDEV_RX, &rules))
93 + *offload_trigger |= QCA807X_LED_FIBER_RXACT_BLK_EN;
94 + if (test_bit(TRIGGER_NETDEV_LINK_100, &rules))
95 + *offload_trigger |= QCA807X_LED_FIBER_100FX_ON_EN;
96 + if (test_bit(TRIGGER_NETDEV_LINK_1000, &rules))
97 + *offload_trigger |= QCA807X_LED_FIBER_1000BX_ON_EN;
98 + if (test_bit(TRIGGER_NETDEV_HALF_DUPLEX, &rules))
99 + *offload_trigger |= QCA807X_LED_FIBER_HDX_ON_EN;
100 + if (test_bit(TRIGGER_NETDEV_FULL_DUPLEX, &rules))
101 + *offload_trigger |= QCA807X_LED_FIBER_FDX_ON_EN;
102 + break;
103 + default:
104 + return -EOPNOTSUPP;
105 + }
106 +
107 + if (rules && !*offload_trigger)
108 + return -EOPNOTSUPP;
109 +
110 + return 0;
111 +}
112 +
113 +static int qca807x_led_hw_control_enable(struct phy_device *phydev, u8 index)
114 +{
115 + u16 reg;
116 +
117 + if (index > 1)
118 + return -EINVAL;
119 +
120 + reg = QCA807X_MMD7_LED_FORCE_CTRL(index);
121 + return qca808x_led_reg_hw_control_enable(phydev, reg);
122 +}
123 +
124 +static int qca807x_led_hw_is_supported(struct phy_device *phydev, u8 index,
125 + unsigned long rules)
126 +{
127 + u16 offload_trigger = 0;
128 +
129 + if (index > 1)
130 + return -EINVAL;
131 +
132 + return qca807x_led_parse_netdev(phydev, rules, &offload_trigger);
133 +}
134 +
135 +static int qca807x_led_hw_control_set(struct phy_device *phydev, u8 index,
136 + unsigned long rules)
137 +{
138 + u16 reg, mask, offload_trigger = 0;
139 + int ret;
140 +
141 + if (index > 1)
142 + return -EINVAL;
143 +
144 + ret = qca807x_led_parse_netdev(phydev, rules, &offload_trigger);
145 + if (ret)
146 + return ret;
147 +
148 + ret = qca807x_led_hw_control_enable(phydev, index);
149 + if (ret)
150 + return ret;
151 +
152 + switch (phydev->port) {
153 + case PORT_TP:
154 + reg = QCA807X_MMD7_LED_CTRL(index);
155 + mask = QCA808X_LED_PATTERN_MASK;
156 + break;
157 + case PORT_FIBRE:
158 + /* HW control pattern bits are in LED FORCE reg */
159 + reg = QCA807X_MMD7_LED_FORCE_CTRL(index);
160 + mask = QCA807X_LED_FIBER_PATTERN_MASK;
161 + break;
162 + default:
163 + return -EINVAL;
164 + }
165 +
166 + return phy_modify_mmd(phydev, MDIO_MMD_AN, reg, mask,
167 + offload_trigger);
168 +}
169 +
170 +static bool qca807x_led_hw_control_status(struct phy_device *phydev, u8 index)
171 +{
172 + u16 reg;
173 +
174 + if (index > 1)
175 + return false;
176 +
177 + reg = QCA807X_MMD7_LED_FORCE_CTRL(index);
178 + return qca808x_led_reg_hw_control_status(phydev, reg);
179 +}
180 +
181 +static int qca807x_led_hw_control_get(struct phy_device *phydev, u8 index,
182 + unsigned long *rules)
183 +{
184 + u16 reg;
185 + int val;
186 +
187 + if (index > 1)
188 + return -EINVAL;
189 +
190 + /* Check if we have hw control enabled */
191 + if (qca807x_led_hw_control_status(phydev, index))
192 + return -EINVAL;
193 +
194 + /* Parsing specific to netdev trigger */
195 + switch (phydev->port) {
196 + case PORT_TP:
197 + reg = QCA807X_MMD7_LED_CTRL(index);
198 + val = phy_read_mmd(phydev, MDIO_MMD_AN, reg);
199 + if (val & QCA808X_LED_TX_BLINK)
200 + set_bit(TRIGGER_NETDEV_TX, rules);
201 + if (val & QCA808X_LED_RX_BLINK)
202 + set_bit(TRIGGER_NETDEV_RX, rules);
203 + if (val & QCA808X_LED_SPEED10_ON)
204 + set_bit(TRIGGER_NETDEV_LINK_10, rules);
205 + if (val & QCA808X_LED_SPEED100_ON)
206 + set_bit(TRIGGER_NETDEV_LINK_100, rules);
207 + if (val & QCA808X_LED_SPEED1000_ON)
208 + set_bit(TRIGGER_NETDEV_LINK_1000, rules);
209 + if (val & QCA808X_LED_HALF_DUPLEX_ON)
210 + set_bit(TRIGGER_NETDEV_HALF_DUPLEX, rules);
211 + if (val & QCA808X_LED_FULL_DUPLEX_ON)
212 + set_bit(TRIGGER_NETDEV_FULL_DUPLEX, rules);
213 + break;
214 + case PORT_FIBRE:
215 + /* HW control pattern bits are in LED FORCE reg */
216 + reg = QCA807X_MMD7_LED_FORCE_CTRL(index);
217 + val = phy_read_mmd(phydev, MDIO_MMD_AN, reg);
218 + if (val & QCA807X_LED_FIBER_TXACT_BLK_EN)
219 + set_bit(TRIGGER_NETDEV_TX, rules);
220 + if (val & QCA807X_LED_FIBER_RXACT_BLK_EN)
221 + set_bit(TRIGGER_NETDEV_RX, rules);
222 + if (val & QCA807X_LED_FIBER_100FX_ON_EN)
223 + set_bit(TRIGGER_NETDEV_LINK_100, rules);
224 + if (val & QCA807X_LED_FIBER_1000BX_ON_EN)
225 + set_bit(TRIGGER_NETDEV_LINK_1000, rules);
226 + if (val & QCA807X_LED_FIBER_HDX_ON_EN)
227 + set_bit(TRIGGER_NETDEV_HALF_DUPLEX, rules);
228 + if (val & QCA807X_LED_FIBER_FDX_ON_EN)
229 + set_bit(TRIGGER_NETDEV_FULL_DUPLEX, rules);
230 + break;
231 + default:
232 + return -EINVAL;
233 + }
234 +
235 + return 0;
236 +}
237 +
238 +static int qca807x_led_hw_control_reset(struct phy_device *phydev, u8 index)
239 +{
240 + u16 reg, mask;
241 +
242 + if (index > 1)
243 + return -EINVAL;
244 +
245 + switch (phydev->port) {
246 + case PORT_TP:
247 + reg = QCA807X_MMD7_LED_CTRL(index);
248 + mask = QCA808X_LED_PATTERN_MASK;
249 + break;
250 + case PORT_FIBRE:
251 + /* HW control pattern bits are in LED FORCE reg */
252 + reg = QCA807X_MMD7_LED_FORCE_CTRL(index);
253 + mask = QCA807X_LED_FIBER_PATTERN_MASK;
254 + break;
255 + default:
256 + return -EINVAL;
257 + }
258 +
259 + return phy_clear_bits_mmd(phydev, MDIO_MMD_AN, reg, mask);
260 +}
261 +
262 +static int qca807x_led_brightness_set(struct phy_device *phydev,
263 + u8 index, enum led_brightness value)
264 +{
265 + u16 reg;
266 + int ret;
267 +
268 + if (index > 1)
269 + return -EINVAL;
270 +
271 + /* If we are setting off the LED reset any hw control rule */
272 + if (!value) {
273 + ret = qca807x_led_hw_control_reset(phydev, index);
274 + if (ret)
275 + return ret;
276 + }
277 +
278 + reg = QCA807X_MMD7_LED_FORCE_CTRL(index);
279 + return qca808x_led_reg_brightness_set(phydev, reg, value);
280 +}
281 +
282 +static int qca807x_led_blink_set(struct phy_device *phydev, u8 index,
283 + unsigned long *delay_on,
284 + unsigned long *delay_off)
285 +{
286 + u16 reg;
287 +
288 + if (index > 1)
289 + return -EINVAL;
290 +
291 + reg = QCA807X_MMD7_LED_FORCE_CTRL(index);
292 + return qca808x_led_reg_blink_set(phydev, reg, delay_on, delay_off);
293 +}
294 +
295 #ifdef CONFIG_GPIOLIB
296 static int qca807x_gpio_get_direction(struct gpio_chip *gc, unsigned int offset)
297 {
298 @@ -496,6 +733,16 @@ static int qca807x_probe(struct phy_devi
299 "qcom,dac-disable-bias-current-tweak");
300
301 if (IS_ENABLED(CONFIG_GPIOLIB)) {
302 + /* Make sure we don't have mixed leds node and gpio-controller
303 + * to prevent registering leds and having gpio-controller usage
304 + * conflicting with them.
305 + */
306 + if (of_find_property(node, "leds", NULL) &&
307 + of_find_property(node, "gpio-controller", NULL)) {
308 + phydev_err(phydev, "Invalid property detected. LEDs and gpio-controller are mutually exclusive.");
309 + return -EINVAL;
310 + }
311 +
312 /* Do not register a GPIO controller unless flagged for it */
313 if (of_property_read_bool(node, "gpio-controller")) {
314 ret = qca807x_gpio(phydev);
315 @@ -580,6 +827,11 @@ static struct phy_driver qca807x_drivers
316 .suspend = genphy_suspend,
317 .cable_test_start = qca807x_cable_test_start,
318 .cable_test_get_status = qca808x_cable_test_get_status,
319 + .led_brightness_set = qca807x_led_brightness_set,
320 + .led_blink_set = qca807x_led_blink_set,
321 + .led_hw_is_supported = qca807x_led_hw_is_supported,
322 + .led_hw_control_set = qca807x_led_hw_control_set,
323 + .led_hw_control_get = qca807x_led_hw_control_get,
324 },
325 };
326 module_phy_driver(qca807x_drivers);