1300583b665a66fa138312fb350b54157fdd5799
[openwrt/staging/981213.git] /
1 From 460b0b648fab24f576c481424e0de5479ffb9786 Mon Sep 17 00:00:00 2001
2 From: Andrew Lunn <andrew@lunn.ch>
3 Date: Tue, 8 Aug 2023 23:04:35 +0200
4 Subject: [PATCH 3/4] net: phy: marvell: Add support for offloading LED
5 blinking
6
7 Add the code needed to indicate if a given blinking pattern can be
8 offloaded, to offload a pattern and to try to return the current
9 pattern.
10
11 Reviewed-by: Simon Horman <simon.horman@corigine.com>
12 Signed-off-by: Andrew Lunn <andrew@lunn.ch>
13 Tested-by: Daniel Golle <daniel@makrotopia.org>
14 Link: https://lore.kernel.org/r/20230808210436.838995-4-andrew@lunn.ch
15 Signed-off-by: Jakub Kicinski <kuba@kernel.org>
16 ---
17 drivers/net/phy/marvell.c | 281 ++++++++++++++++++++++++++++++++++++++
18 1 file changed, 281 insertions(+)
19
20 --- a/drivers/net/phy/marvell.c
21 +++ b/drivers/net/phy/marvell.c
22 @@ -2893,6 +2893,272 @@ static int m88e1318_led_blink_set(struct
23 MII_88E1318S_PHY_LED_FUNC, reg);
24 }
25
26 +struct marvell_led_rules {
27 + int mode;
28 + unsigned long rules;
29 +};
30 +
31 +static const struct marvell_led_rules marvell_led0[] = {
32 + {
33 + .mode = 0,
34 + .rules = BIT(TRIGGER_NETDEV_LINK),
35 + },
36 + {
37 + .mode = 1,
38 + .rules = (BIT(TRIGGER_NETDEV_LINK) |
39 + BIT(TRIGGER_NETDEV_RX) |
40 + BIT(TRIGGER_NETDEV_TX)),
41 + },
42 + {
43 + .mode = 3,
44 + .rules = (BIT(TRIGGER_NETDEV_RX) |
45 + BIT(TRIGGER_NETDEV_TX)),
46 + },
47 + {
48 + .mode = 4,
49 + .rules = (BIT(TRIGGER_NETDEV_RX) |
50 + BIT(TRIGGER_NETDEV_TX)),
51 + },
52 + {
53 + .mode = 5,
54 + .rules = BIT(TRIGGER_NETDEV_TX),
55 + },
56 + {
57 + .mode = 6,
58 + .rules = BIT(TRIGGER_NETDEV_LINK),
59 + },
60 + {
61 + .mode = 7,
62 + .rules = BIT(TRIGGER_NETDEV_LINK_1000),
63 + },
64 + {
65 + .mode = 8,
66 + .rules = 0,
67 + },
68 +};
69 +
70 +static const struct marvell_led_rules marvell_led1[] = {
71 + {
72 + .mode = 1,
73 + .rules = (BIT(TRIGGER_NETDEV_LINK) |
74 + BIT(TRIGGER_NETDEV_RX) |
75 + BIT(TRIGGER_NETDEV_TX)),
76 + },
77 + {
78 + .mode = 2,
79 + .rules = (BIT(TRIGGER_NETDEV_LINK) |
80 + BIT(TRIGGER_NETDEV_RX)),
81 + },
82 + {
83 + .mode = 3,
84 + .rules = (BIT(TRIGGER_NETDEV_RX) |
85 + BIT(TRIGGER_NETDEV_TX)),
86 + },
87 + {
88 + .mode = 4,
89 + .rules = (BIT(TRIGGER_NETDEV_RX) |
90 + BIT(TRIGGER_NETDEV_TX)),
91 + },
92 + {
93 + .mode = 6,
94 + .rules = (BIT(TRIGGER_NETDEV_LINK_100) |
95 + BIT(TRIGGER_NETDEV_LINK_1000)),
96 + },
97 + {
98 + .mode = 7,
99 + .rules = BIT(TRIGGER_NETDEV_LINK_100),
100 + },
101 + {
102 + .mode = 8,
103 + .rules = 0,
104 + },
105 +};
106 +
107 +static const struct marvell_led_rules marvell_led2[] = {
108 + {
109 + .mode = 0,
110 + .rules = BIT(TRIGGER_NETDEV_LINK),
111 + },
112 + {
113 + .mode = 1,
114 + .rules = (BIT(TRIGGER_NETDEV_LINK) |
115 + BIT(TRIGGER_NETDEV_RX) |
116 + BIT(TRIGGER_NETDEV_TX)),
117 + },
118 + {
119 + .mode = 3,
120 + .rules = (BIT(TRIGGER_NETDEV_RX) |
121 + BIT(TRIGGER_NETDEV_TX)),
122 + },
123 + {
124 + .mode = 4,
125 + .rules = (BIT(TRIGGER_NETDEV_RX) |
126 + BIT(TRIGGER_NETDEV_TX)),
127 + },
128 + {
129 + .mode = 5,
130 + .rules = BIT(TRIGGER_NETDEV_TX),
131 + },
132 + {
133 + .mode = 6,
134 + .rules = (BIT(TRIGGER_NETDEV_LINK_10) |
135 + BIT(TRIGGER_NETDEV_LINK_1000)),
136 + },
137 + {
138 + .mode = 7,
139 + .rules = BIT(TRIGGER_NETDEV_LINK_10),
140 + },
141 + {
142 + .mode = 8,
143 + .rules = 0,
144 + },
145 +};
146 +
147 +static int marvell_find_led_mode(unsigned long rules,
148 + const struct marvell_led_rules *marvell_rules,
149 + int count,
150 + int *mode)
151 +{
152 + int i;
153 +
154 + for (i = 0; i < count; i++) {
155 + if (marvell_rules[i].rules == rules) {
156 + *mode = marvell_rules[i].mode;
157 + return 0;
158 + }
159 + }
160 + return -EOPNOTSUPP;
161 +}
162 +
163 +static int marvell_get_led_mode(u8 index, unsigned long rules, int *mode)
164 +{
165 + int ret;
166 +
167 + switch (index) {
168 + case 0:
169 + ret = marvell_find_led_mode(rules, marvell_led0,
170 + ARRAY_SIZE(marvell_led0), mode);
171 + break;
172 + case 1:
173 + ret = marvell_find_led_mode(rules, marvell_led1,
174 + ARRAY_SIZE(marvell_led1), mode);
175 + break;
176 + case 2:
177 + ret = marvell_find_led_mode(rules, marvell_led2,
178 + ARRAY_SIZE(marvell_led2), mode);
179 + break;
180 + default:
181 + ret = -EINVAL;
182 + }
183 +
184 + return ret;
185 +}
186 +
187 +static int marvell_find_led_rules(unsigned long *rules,
188 + const struct marvell_led_rules *marvell_rules,
189 + int count,
190 + int mode)
191 +{
192 + int i;
193 +
194 + for (i = 0; i < count; i++) {
195 + if (marvell_rules[i].mode == mode) {
196 + *rules = marvell_rules[i].rules;
197 + return 0;
198 + }
199 + }
200 + return -EOPNOTSUPP;
201 +}
202 +
203 +static int marvell_get_led_rules(u8 index, unsigned long *rules, int mode)
204 +{
205 + int ret;
206 +
207 + switch (index) {
208 + case 0:
209 + ret = marvell_find_led_rules(rules, marvell_led0,
210 + ARRAY_SIZE(marvell_led0), mode);
211 + break;
212 + case 1:
213 + ret = marvell_find_led_rules(rules, marvell_led1,
214 + ARRAY_SIZE(marvell_led1), mode);
215 + break;
216 + case 2:
217 + ret = marvell_find_led_rules(rules, marvell_led2,
218 + ARRAY_SIZE(marvell_led2), mode);
219 + break;
220 + default:
221 + ret = -EOPNOTSUPP;
222 + }
223 +
224 + return ret;
225 +}
226 +
227 +static int m88e1318_led_hw_is_supported(struct phy_device *phydev, u8 index,
228 + unsigned long rules)
229 +{
230 + int mode, ret;
231 +
232 + switch (index) {
233 + case 0:
234 + case 1:
235 + case 2:
236 + ret = marvell_get_led_mode(index, rules, &mode);
237 + break;
238 + default:
239 + ret = -EINVAL;
240 + }
241 +
242 + return ret;
243 +}
244 +
245 +static int m88e1318_led_hw_control_set(struct phy_device *phydev, u8 index,
246 + unsigned long rules)
247 +{
248 + int mode, ret, reg;
249 +
250 + switch (index) {
251 + case 0:
252 + case 1:
253 + case 2:
254 + ret = marvell_get_led_mode(index, rules, &mode);
255 + break;
256 + default:
257 + ret = -EINVAL;
258 + }
259 +
260 + if (ret < 0)
261 + return ret;
262 +
263 + reg = phy_read_paged(phydev, MII_MARVELL_LED_PAGE,
264 + MII_88E1318S_PHY_LED_FUNC);
265 + if (reg < 0)
266 + return reg;
267 +
268 + reg &= ~(0xf << (4 * index));
269 + reg |= mode << (4 * index);
270 + return phy_write_paged(phydev, MII_MARVELL_LED_PAGE,
271 + MII_88E1318S_PHY_LED_FUNC, reg);
272 +}
273 +
274 +static int m88e1318_led_hw_control_get(struct phy_device *phydev, u8 index,
275 + unsigned long *rules)
276 +{
277 + int mode, reg;
278 +
279 + if (index > 2)
280 + return -EINVAL;
281 +
282 + reg = phy_read_paged(phydev, MII_MARVELL_LED_PAGE,
283 + MII_88E1318S_PHY_LED_FUNC);
284 + if (reg < 0)
285 + return reg;
286 +
287 + mode = (reg >> (4 * index)) & 0xf;
288 +
289 + return marvell_get_led_rules(index, rules, mode);
290 +}
291 +
292 static int marvell_probe(struct phy_device *phydev)
293 {
294 struct marvell_priv *priv;
295 @@ -3144,6 +3410,9 @@ static struct phy_driver marvell_drivers
296 .get_stats = marvell_get_stats,
297 .led_brightness_set = m88e1318_led_brightness_set,
298 .led_blink_set = m88e1318_led_blink_set,
299 + .led_hw_is_supported = m88e1318_led_hw_is_supported,
300 + .led_hw_control_set = m88e1318_led_hw_control_set,
301 + .led_hw_control_get = m88e1318_led_hw_control_get,
302 },
303 {
304 .phy_id = MARVELL_PHY_ID_88E1145,
305 @@ -3252,6 +3521,9 @@ static struct phy_driver marvell_drivers
306 .cable_test_get_status = marvell_vct7_cable_test_get_status,
307 .led_brightness_set = m88e1318_led_brightness_set,
308 .led_blink_set = m88e1318_led_blink_set,
309 + .led_hw_is_supported = m88e1318_led_hw_is_supported,
310 + .led_hw_control_set = m88e1318_led_hw_control_set,
311 + .led_hw_control_get = m88e1318_led_hw_control_get,
312 },
313 {
314 .phy_id = MARVELL_PHY_ID_88E1540,
315 @@ -3280,6 +3552,9 @@ static struct phy_driver marvell_drivers
316 .cable_test_get_status = marvell_vct7_cable_test_get_status,
317 .led_brightness_set = m88e1318_led_brightness_set,
318 .led_blink_set = m88e1318_led_blink_set,
319 + .led_hw_is_supported = m88e1318_led_hw_is_supported,
320 + .led_hw_control_set = m88e1318_led_hw_control_set,
321 + .led_hw_control_get = m88e1318_led_hw_control_get,
322 },
323 {
324 .phy_id = MARVELL_PHY_ID_88E1545,
325 @@ -3308,6 +3583,9 @@ static struct phy_driver marvell_drivers
326 .cable_test_get_status = marvell_vct7_cable_test_get_status,
327 .led_brightness_set = m88e1318_led_brightness_set,
328 .led_blink_set = m88e1318_led_blink_set,
329 + .led_hw_is_supported = m88e1318_led_hw_is_supported,
330 + .led_hw_control_set = m88e1318_led_hw_control_set,
331 + .led_hw_control_get = m88e1318_led_hw_control_get,
332 },
333 {
334 .phy_id = MARVELL_PHY_ID_88E3016,
335 @@ -3451,6 +3729,9 @@ static struct phy_driver marvell_drivers
336 .set_tunable = m88e1540_set_tunable,
337 .led_brightness_set = m88e1318_led_brightness_set,
338 .led_blink_set = m88e1318_led_blink_set,
339 + .led_hw_is_supported = m88e1318_led_hw_is_supported,
340 + .led_hw_control_set = m88e1318_led_hw_control_set,
341 + .led_hw_control_get = m88e1318_led_hw_control_get,
342 },
343 };
344