kernel: bump 5.10 to 5.10.110
[openwrt/openwrt.git] / target / linux / generic / backport-5.10 / 712-v5.13-net-phy-marvell-refactor-HWMON-OOP-style.patch
1 From 41d26bf4aba070dfd2ab48866cc27a48ee6228c7 Mon Sep 17 00:00:00 2001
2 From: =?UTF-8?q?Marek=20Beh=C3=BAn?= <kabel@kernel.org>
3 Date: Tue, 20 Apr 2021 09:53:59 +0200
4 Subject: [PATCH] net: phy: marvell: refactor HWMON OOP style
5 MIME-Version: 1.0
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
8
9 Use a structure of Marvell PHY specific HWMON methods to reduce code
10 duplication. Store a pointer to this structure into the PHY driver's
11 driver_data member.
12
13 Signed-off-by: Marek BehĂșn <kabel@kernel.org>
14 Signed-off-by: David S. Miller <davem@davemloft.net>
15 ---
16 drivers/net/phy/marvell.c | 369 +++++++++++++-------------------------
17 1 file changed, 125 insertions(+), 244 deletions(-)
18
19 --- a/drivers/net/phy/marvell.c
20 +++ b/drivers/net/phy/marvell.c
21 @@ -2141,6 +2141,19 @@ static int marvell_vct7_cable_test_get_s
22 }
23
24 #ifdef CONFIG_HWMON
25 +struct marvell_hwmon_ops {
26 + int (*get_temp)(struct phy_device *phydev, long *temp);
27 + int (*get_temp_critical)(struct phy_device *phydev, long *temp);
28 + int (*set_temp_critical)(struct phy_device *phydev, long temp);
29 + int (*get_temp_alarm)(struct phy_device *phydev, long *alarm);
30 +};
31 +
32 +static const struct marvell_hwmon_ops *
33 +to_marvell_hwmon_ops(const struct phy_device *phydev)
34 +{
35 + return phydev->drv->driver_data;
36 +}
37 +
38 static int m88e1121_get_temp(struct phy_device *phydev, long *temp)
39 {
40 int oldpage;
41 @@ -2184,75 +2197,6 @@ error:
42 return phy_restore_page(phydev, oldpage, ret);
43 }
44
45 -static int m88e1121_hwmon_read(struct device *dev,
46 - enum hwmon_sensor_types type,
47 - u32 attr, int channel, long *temp)
48 -{
49 - struct phy_device *phydev = dev_get_drvdata(dev);
50 - int err;
51 -
52 - switch (attr) {
53 - case hwmon_temp_input:
54 - err = m88e1121_get_temp(phydev, temp);
55 - break;
56 - default:
57 - return -EOPNOTSUPP;
58 - }
59 -
60 - return err;
61 -}
62 -
63 -static umode_t m88e1121_hwmon_is_visible(const void *data,
64 - enum hwmon_sensor_types type,
65 - u32 attr, int channel)
66 -{
67 - if (type != hwmon_temp)
68 - return 0;
69 -
70 - switch (attr) {
71 - case hwmon_temp_input:
72 - return 0444;
73 - default:
74 - return 0;
75 - }
76 -}
77 -
78 -static u32 m88e1121_hwmon_chip_config[] = {
79 - HWMON_C_REGISTER_TZ,
80 - 0
81 -};
82 -
83 -static const struct hwmon_channel_info m88e1121_hwmon_chip = {
84 - .type = hwmon_chip,
85 - .config = m88e1121_hwmon_chip_config,
86 -};
87 -
88 -static u32 m88e1121_hwmon_temp_config[] = {
89 - HWMON_T_INPUT,
90 - 0
91 -};
92 -
93 -static const struct hwmon_channel_info m88e1121_hwmon_temp = {
94 - .type = hwmon_temp,
95 - .config = m88e1121_hwmon_temp_config,
96 -};
97 -
98 -static const struct hwmon_channel_info *m88e1121_hwmon_info[] = {
99 - &m88e1121_hwmon_chip,
100 - &m88e1121_hwmon_temp,
101 - NULL
102 -};
103 -
104 -static const struct hwmon_ops m88e1121_hwmon_hwmon_ops = {
105 - .is_visible = m88e1121_hwmon_is_visible,
106 - .read = m88e1121_hwmon_read,
107 -};
108 -
109 -static const struct hwmon_chip_info m88e1121_hwmon_chip_info = {
110 - .ops = &m88e1121_hwmon_hwmon_ops,
111 - .info = m88e1121_hwmon_info,
112 -};
113 -
114 static int m88e1510_get_temp(struct phy_device *phydev, long *temp)
115 {
116 int ret;
117 @@ -2315,92 +2259,6 @@ static int m88e1510_get_temp_alarm(struc
118 return 0;
119 }
120
121 -static int m88e1510_hwmon_read(struct device *dev,
122 - enum hwmon_sensor_types type,
123 - u32 attr, int channel, long *temp)
124 -{
125 - struct phy_device *phydev = dev_get_drvdata(dev);
126 - int err;
127 -
128 - switch (attr) {
129 - case hwmon_temp_input:
130 - err = m88e1510_get_temp(phydev, temp);
131 - break;
132 - case hwmon_temp_crit:
133 - err = m88e1510_get_temp_critical(phydev, temp);
134 - break;
135 - case hwmon_temp_max_alarm:
136 - err = m88e1510_get_temp_alarm(phydev, temp);
137 - break;
138 - default:
139 - return -EOPNOTSUPP;
140 - }
141 -
142 - return err;
143 -}
144 -
145 -static int m88e1510_hwmon_write(struct device *dev,
146 - enum hwmon_sensor_types type,
147 - u32 attr, int channel, long temp)
148 -{
149 - struct phy_device *phydev = dev_get_drvdata(dev);
150 - int err;
151 -
152 - switch (attr) {
153 - case hwmon_temp_crit:
154 - err = m88e1510_set_temp_critical(phydev, temp);
155 - break;
156 - default:
157 - return -EOPNOTSUPP;
158 - }
159 - return err;
160 -}
161 -
162 -static umode_t m88e1510_hwmon_is_visible(const void *data,
163 - enum hwmon_sensor_types type,
164 - u32 attr, int channel)
165 -{
166 - if (type != hwmon_temp)
167 - return 0;
168 -
169 - switch (attr) {
170 - case hwmon_temp_input:
171 - case hwmon_temp_max_alarm:
172 - return 0444;
173 - case hwmon_temp_crit:
174 - return 0644;
175 - default:
176 - return 0;
177 - }
178 -}
179 -
180 -static u32 m88e1510_hwmon_temp_config[] = {
181 - HWMON_T_INPUT | HWMON_T_CRIT | HWMON_T_MAX_ALARM,
182 - 0
183 -};
184 -
185 -static const struct hwmon_channel_info m88e1510_hwmon_temp = {
186 - .type = hwmon_temp,
187 - .config = m88e1510_hwmon_temp_config,
188 -};
189 -
190 -static const struct hwmon_channel_info *m88e1510_hwmon_info[] = {
191 - &m88e1121_hwmon_chip,
192 - &m88e1510_hwmon_temp,
193 - NULL
194 -};
195 -
196 -static const struct hwmon_ops m88e1510_hwmon_hwmon_ops = {
197 - .is_visible = m88e1510_hwmon_is_visible,
198 - .read = m88e1510_hwmon_read,
199 - .write = m88e1510_hwmon_write,
200 -};
201 -
202 -static const struct hwmon_chip_info m88e1510_hwmon_chip_info = {
203 - .ops = &m88e1510_hwmon_hwmon_ops,
204 - .info = m88e1510_hwmon_info,
205 -};
206 -
207 static int m88e6390_get_temp(struct phy_device *phydev, long *temp)
208 {
209 int sum = 0;
210 @@ -2459,63 +2317,112 @@ error:
211 return ret;
212 }
213
214 -static int m88e6390_hwmon_read(struct device *dev,
215 - enum hwmon_sensor_types type,
216 - u32 attr, int channel, long *temp)
217 +static int marvell_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
218 + u32 attr, int channel, long *temp)
219 {
220 struct phy_device *phydev = dev_get_drvdata(dev);
221 - int err;
222 + const struct marvell_hwmon_ops *ops = to_marvell_hwmon_ops(phydev);
223 + int err = -EOPNOTSUPP;
224
225 switch (attr) {
226 case hwmon_temp_input:
227 - err = m88e6390_get_temp(phydev, temp);
228 + if (ops->get_temp)
229 + err = ops->get_temp(phydev, temp);
230 + break;
231 + case hwmon_temp_crit:
232 + if (ops->get_temp_critical)
233 + err = ops->get_temp_critical(phydev, temp);
234 + break;
235 + case hwmon_temp_max_alarm:
236 + if (ops->get_temp_alarm)
237 + err = ops->get_temp_alarm(phydev, temp);
238 + break;
239 + }
240 +
241 + return err;
242 +}
243 +
244 +static int marvell_hwmon_write(struct device *dev, enum hwmon_sensor_types type,
245 + u32 attr, int channel, long temp)
246 +{
247 + struct phy_device *phydev = dev_get_drvdata(dev);
248 + const struct marvell_hwmon_ops *ops = to_marvell_hwmon_ops(phydev);
249 + int err = -EOPNOTSUPP;
250 +
251 + switch (attr) {
252 + case hwmon_temp_crit:
253 + if (ops->set_temp_critical)
254 + err = ops->set_temp_critical(phydev, temp);
255 break;
256 default:
257 - return -EOPNOTSUPP;
258 + fallthrough;
259 }
260
261 return err;
262 }
263
264 -static umode_t m88e6390_hwmon_is_visible(const void *data,
265 - enum hwmon_sensor_types type,
266 - u32 attr, int channel)
267 +static umode_t marvell_hwmon_is_visible(const void *data,
268 + enum hwmon_sensor_types type,
269 + u32 attr, int channel)
270 {
271 + const struct phy_device *phydev = data;
272 + const struct marvell_hwmon_ops *ops = to_marvell_hwmon_ops(phydev);
273 +
274 if (type != hwmon_temp)
275 return 0;
276
277 switch (attr) {
278 case hwmon_temp_input:
279 - return 0444;
280 + return ops->get_temp ? 0444 : 0;
281 + case hwmon_temp_max_alarm:
282 + return ops->get_temp_alarm ? 0444 : 0;
283 + case hwmon_temp_crit:
284 + return (ops->get_temp_critical ? 0444 : 0) |
285 + (ops->set_temp_critical ? 0200 : 0);
286 default:
287 return 0;
288 }
289 }
290
291 -static u32 m88e6390_hwmon_temp_config[] = {
292 - HWMON_T_INPUT,
293 +static u32 marvell_hwmon_chip_config[] = {
294 + HWMON_C_REGISTER_TZ,
295 0
296 };
297
298 -static const struct hwmon_channel_info m88e6390_hwmon_temp = {
299 +static const struct hwmon_channel_info marvell_hwmon_chip = {
300 + .type = hwmon_chip,
301 + .config = marvell_hwmon_chip_config,
302 +};
303 +
304 +/* we can define HWMON_T_CRIT and HWMON_T_MAX_ALARM even though these are not
305 + * defined for all PHYs, because the hwmon code checks whether the attributes
306 + * exists via the .is_visible method
307 + */
308 +static u32 marvell_hwmon_temp_config[] = {
309 + HWMON_T_INPUT | HWMON_T_CRIT | HWMON_T_MAX_ALARM,
310 + 0
311 +};
312 +
313 +static const struct hwmon_channel_info marvell_hwmon_temp = {
314 .type = hwmon_temp,
315 - .config = m88e6390_hwmon_temp_config,
316 + .config = marvell_hwmon_temp_config,
317 };
318
319 -static const struct hwmon_channel_info *m88e6390_hwmon_info[] = {
320 - &m88e1121_hwmon_chip,
321 - &m88e6390_hwmon_temp,
322 +static const struct hwmon_channel_info *marvell_hwmon_info[] = {
323 + &marvell_hwmon_chip,
324 + &marvell_hwmon_temp,
325 NULL
326 };
327
328 -static const struct hwmon_ops m88e6390_hwmon_hwmon_ops = {
329 - .is_visible = m88e6390_hwmon_is_visible,
330 - .read = m88e6390_hwmon_read,
331 +static const struct hwmon_ops marvell_hwmon_hwmon_ops = {
332 + .is_visible = marvell_hwmon_is_visible,
333 + .read = marvell_hwmon_read,
334 + .write = marvell_hwmon_write,
335 };
336
337 -static const struct hwmon_chip_info m88e6390_hwmon_chip_info = {
338 - .ops = &m88e6390_hwmon_hwmon_ops,
339 - .info = m88e6390_hwmon_info,
340 +static const struct hwmon_chip_info marvell_hwmon_chip_info = {
341 + .ops = &marvell_hwmon_hwmon_ops,
342 + .info = marvell_hwmon_info,
343 };
344
345 static int marvell_hwmon_name(struct phy_device *phydev)
346 @@ -2538,49 +2445,48 @@ static int marvell_hwmon_name(struct phy
347 return 0;
348 }
349
350 -static int marvell_hwmon_probe(struct phy_device *phydev,
351 - const struct hwmon_chip_info *chip)
352 +static int marvell_hwmon_probe(struct phy_device *phydev)
353 {
354 + const struct marvell_hwmon_ops *ops = to_marvell_hwmon_ops(phydev);
355 struct marvell_priv *priv = phydev->priv;
356 struct device *dev = &phydev->mdio.dev;
357 int err;
358
359 + if (!ops)
360 + return 0;
361 +
362 err = marvell_hwmon_name(phydev);
363 if (err)
364 return err;
365
366 priv->hwmon_dev = devm_hwmon_device_register_with_info(
367 - dev, priv->hwmon_name, phydev, chip, NULL);
368 + dev, priv->hwmon_name, phydev, &marvell_hwmon_chip_info, NULL);
369
370 return PTR_ERR_OR_ZERO(priv->hwmon_dev);
371 }
372
373 -static int m88e1121_hwmon_probe(struct phy_device *phydev)
374 -{
375 - return marvell_hwmon_probe(phydev, &m88e1121_hwmon_chip_info);
376 -}
377 +static const struct marvell_hwmon_ops m88e1121_hwmon_ops = {
378 + .get_temp = m88e1121_get_temp,
379 +};
380
381 -static int m88e1510_hwmon_probe(struct phy_device *phydev)
382 -{
383 - return marvell_hwmon_probe(phydev, &m88e1510_hwmon_chip_info);
384 -}
385 +static const struct marvell_hwmon_ops m88e1510_hwmon_ops = {
386 + .get_temp = m88e1510_get_temp,
387 + .get_temp_critical = m88e1510_get_temp_critical,
388 + .set_temp_critical = m88e1510_set_temp_critical,
389 + .get_temp_alarm = m88e1510_get_temp_alarm,
390 +};
391 +
392 +static const struct marvell_hwmon_ops m88e6390_hwmon_ops = {
393 + .get_temp = m88e6390_get_temp,
394 +};
395 +
396 +#define DEF_MARVELL_HWMON_OPS(s) (&(s))
397
398 -static int m88e6390_hwmon_probe(struct phy_device *phydev)
399 -{
400 - return marvell_hwmon_probe(phydev, &m88e6390_hwmon_chip_info);
401 -}
402 #else
403 -static int m88e1121_hwmon_probe(struct phy_device *phydev)
404 -{
405 - return 0;
406 -}
407
408 -static int m88e1510_hwmon_probe(struct phy_device *phydev)
409 -{
410 - return 0;
411 -}
412 +#define DEF_MARVELL_HWMON_OPS(s) NULL
413
414 -static int m88e6390_hwmon_probe(struct phy_device *phydev)
415 +static int marvell_hwmon_probe(struct phy_device *phydev)
416 {
417 return 0;
418 }
419 @@ -2596,40 +2502,7 @@ static int marvell_probe(struct phy_devi
420
421 phydev->priv = priv;
422
423 - return 0;
424 -}
425 -
426 -static int m88e1121_probe(struct phy_device *phydev)
427 -{
428 - int err;
429 -
430 - err = marvell_probe(phydev);
431 - if (err)
432 - return err;
433 -
434 - return m88e1121_hwmon_probe(phydev);
435 -}
436 -
437 -static int m88e1510_probe(struct phy_device *phydev)
438 -{
439 - int err;
440 -
441 - err = marvell_probe(phydev);
442 - if (err)
443 - return err;
444 -
445 - return m88e1510_hwmon_probe(phydev);
446 -}
447 -
448 -static int m88e6390_probe(struct phy_device *phydev)
449 -{
450 - int err;
451 -
452 - err = marvell_probe(phydev);
453 - if (err)
454 - return err;
455 -
456 - return m88e6390_hwmon_probe(phydev);
457 + return marvell_hwmon_probe(phydev);
458 }
459
460 static struct phy_driver marvell_drivers[] = {
461 @@ -2714,8 +2587,9 @@ static struct phy_driver marvell_drivers
462 .phy_id = MARVELL_PHY_ID_88E1121R,
463 .phy_id_mask = MARVELL_PHY_ID_MASK,
464 .name = "Marvell 88E1121R",
465 + .driver_data = DEF_MARVELL_HWMON_OPS(m88e1121_hwmon_ops),
466 /* PHY_GBIT_FEATURES */
467 - .probe = m88e1121_probe,
468 + .probe = marvell_probe,
469 .config_init = marvell_config_init,
470 .config_aneg = m88e1121_config_aneg,
471 .read_status = marvell_read_status,
472 @@ -2834,9 +2708,10 @@ static struct phy_driver marvell_drivers
473 .phy_id = MARVELL_PHY_ID_88E1510,
474 .phy_id_mask = MARVELL_PHY_ID_MASK,
475 .name = "Marvell 88E1510",
476 + .driver_data = DEF_MARVELL_HWMON_OPS(m88e1510_hwmon_ops),
477 .features = PHY_GBIT_FIBRE_FEATURES,
478 .flags = PHY_POLL_CABLE_TEST,
479 - .probe = m88e1510_probe,
480 + .probe = marvell_probe,
481 .config_init = m88e1510_config_init,
482 .config_aneg = m88e1510_config_aneg,
483 .read_status = marvell_read_status,
484 @@ -2863,9 +2738,10 @@ static struct phy_driver marvell_drivers
485 .phy_id = MARVELL_PHY_ID_88E1540,
486 .phy_id_mask = MARVELL_PHY_ID_MASK,
487 .name = "Marvell 88E1540",
488 + .driver_data = DEF_MARVELL_HWMON_OPS(m88e1510_hwmon_ops),
489 /* PHY_GBIT_FEATURES */
490 .flags = PHY_POLL_CABLE_TEST,
491 - .probe = m88e1510_probe,
492 + .probe = marvell_probe,
493 .config_init = marvell_config_init,
494 .config_aneg = m88e1510_config_aneg,
495 .read_status = marvell_read_status,
496 @@ -2889,7 +2765,8 @@ static struct phy_driver marvell_drivers
497 .phy_id = MARVELL_PHY_ID_88E1545,
498 .phy_id_mask = MARVELL_PHY_ID_MASK,
499 .name = "Marvell 88E1545",
500 - .probe = m88e1510_probe,
501 + .driver_data = DEF_MARVELL_HWMON_OPS(m88e1510_hwmon_ops),
502 + .probe = marvell_probe,
503 /* PHY_GBIT_FEATURES */
504 .flags = PHY_POLL_CABLE_TEST,
505 .config_init = marvell_config_init,
506 @@ -2935,9 +2812,10 @@ static struct phy_driver marvell_drivers
507 .phy_id = MARVELL_PHY_ID_88E6341_FAMILY,
508 .phy_id_mask = MARVELL_PHY_ID_MASK,
509 .name = "Marvell 88E6341 Family",
510 + .driver_data = DEF_MARVELL_HWMON_OPS(m88e1510_hwmon_ops),
511 /* PHY_GBIT_FEATURES */
512 .flags = PHY_POLL_CABLE_TEST,
513 - .probe = m88e1510_probe,
514 + .probe = marvell_probe,
515 .config_init = marvell_config_init,
516 .config_aneg = m88e6390_config_aneg,
517 .read_status = marvell_read_status,
518 @@ -2961,9 +2839,10 @@ static struct phy_driver marvell_drivers
519 .phy_id = MARVELL_PHY_ID_88E6390_FAMILY,
520 .phy_id_mask = MARVELL_PHY_ID_MASK,
521 .name = "Marvell 88E6390 Family",
522 + .driver_data = DEF_MARVELL_HWMON_OPS(m88e6390_hwmon_ops),
523 /* PHY_GBIT_FEATURES */
524 .flags = PHY_POLL_CABLE_TEST,
525 - .probe = m88e6390_probe,
526 + .probe = marvell_probe,
527 .config_init = marvell_config_init,
528 .config_aneg = m88e6390_config_aneg,
529 .read_status = marvell_read_status,
530 @@ -2987,7 +2866,8 @@ static struct phy_driver marvell_drivers
531 .phy_id = MARVELL_PHY_ID_88E1340S,
532 .phy_id_mask = MARVELL_PHY_ID_MASK,
533 .name = "Marvell 88E1340S",
534 - .probe = m88e1510_probe,
535 + .driver_data = DEF_MARVELL_HWMON_OPS(m88e1510_hwmon_ops),
536 + .probe = marvell_probe,
537 /* PHY_GBIT_FEATURES */
538 .config_init = marvell_config_init,
539 .config_aneg = m88e1510_config_aneg,
540 @@ -3009,7 +2889,8 @@ static struct phy_driver marvell_drivers
541 .phy_id = MARVELL_PHY_ID_88E1548P,
542 .phy_id_mask = MARVELL_PHY_ID_MASK,
543 .name = "Marvell 88E1548P",
544 - .probe = m88e1510_probe,
545 + .driver_data = DEF_MARVELL_HWMON_OPS(m88e1510_hwmon_ops),
546 + .probe = marvell_probe,
547 .features = PHY_GBIT_FIBRE_FEATURES,
548 .config_init = marvell_config_init,
549 .config_aneg = m88e1510_config_aneg,