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
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
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
13 Signed-off-by: Marek BehĂșn <kabel@kernel.org>
14 Signed-off-by: David S. Miller <davem@davemloft.net>
16 drivers/net/phy/marvell.c | 369 +++++++++++++-------------------------
17 1 file changed, 125 insertions(+), 244 deletions(-)
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
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);
32 +static const struct marvell_hwmon_ops *
33 +to_marvell_hwmon_ops(const struct phy_device *phydev)
35 + return phydev->drv->driver_data;
38 static int m88e1121_get_temp(struct phy_device *phydev, long *temp)
41 @@ -2184,75 +2197,6 @@ error:
42 return phy_restore_page(phydev, oldpage, ret);
45 -static int m88e1121_hwmon_read(struct device *dev,
46 - enum hwmon_sensor_types type,
47 - u32 attr, int channel, long *temp)
49 - struct phy_device *phydev = dev_get_drvdata(dev);
53 - case hwmon_temp_input:
54 - err = m88e1121_get_temp(phydev, temp);
63 -static umode_t m88e1121_hwmon_is_visible(const void *data,
64 - enum hwmon_sensor_types type,
65 - u32 attr, int channel)
67 - if (type != hwmon_temp)
71 - case hwmon_temp_input:
78 -static u32 m88e1121_hwmon_chip_config[] = {
79 - HWMON_C_REGISTER_TZ,
83 -static const struct hwmon_channel_info m88e1121_hwmon_chip = {
85 - .config = m88e1121_hwmon_chip_config,
88 -static u32 m88e1121_hwmon_temp_config[] = {
93 -static const struct hwmon_channel_info m88e1121_hwmon_temp = {
95 - .config = m88e1121_hwmon_temp_config,
98 -static const struct hwmon_channel_info *m88e1121_hwmon_info[] = {
99 - &m88e1121_hwmon_chip,
100 - &m88e1121_hwmon_temp,
104 -static const struct hwmon_ops m88e1121_hwmon_hwmon_ops = {
105 - .is_visible = m88e1121_hwmon_is_visible,
106 - .read = m88e1121_hwmon_read,
109 -static const struct hwmon_chip_info m88e1121_hwmon_chip_info = {
110 - .ops = &m88e1121_hwmon_hwmon_ops,
111 - .info = m88e1121_hwmon_info,
114 static int m88e1510_get_temp(struct phy_device *phydev, long *temp)
117 @@ -2315,92 +2259,6 @@ static int m88e1510_get_temp_alarm(struc
121 -static int m88e1510_hwmon_read(struct device *dev,
122 - enum hwmon_sensor_types type,
123 - u32 attr, int channel, long *temp)
125 - struct phy_device *phydev = dev_get_drvdata(dev);
129 - case hwmon_temp_input:
130 - err = m88e1510_get_temp(phydev, temp);
132 - case hwmon_temp_crit:
133 - err = m88e1510_get_temp_critical(phydev, temp);
135 - case hwmon_temp_max_alarm:
136 - err = m88e1510_get_temp_alarm(phydev, temp);
139 - return -EOPNOTSUPP;
145 -static int m88e1510_hwmon_write(struct device *dev,
146 - enum hwmon_sensor_types type,
147 - u32 attr, int channel, long temp)
149 - struct phy_device *phydev = dev_get_drvdata(dev);
153 - case hwmon_temp_crit:
154 - err = m88e1510_set_temp_critical(phydev, temp);
157 - return -EOPNOTSUPP;
162 -static umode_t m88e1510_hwmon_is_visible(const void *data,
163 - enum hwmon_sensor_types type,
164 - u32 attr, int channel)
166 - if (type != hwmon_temp)
170 - case hwmon_temp_input:
171 - case hwmon_temp_max_alarm:
173 - case hwmon_temp_crit:
180 -static u32 m88e1510_hwmon_temp_config[] = {
181 - HWMON_T_INPUT | HWMON_T_CRIT | HWMON_T_MAX_ALARM,
185 -static const struct hwmon_channel_info m88e1510_hwmon_temp = {
186 - .type = hwmon_temp,
187 - .config = m88e1510_hwmon_temp_config,
190 -static const struct hwmon_channel_info *m88e1510_hwmon_info[] = {
191 - &m88e1121_hwmon_chip,
192 - &m88e1510_hwmon_temp,
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,
202 -static const struct hwmon_chip_info m88e1510_hwmon_chip_info = {
203 - .ops = &m88e1510_hwmon_hwmon_ops,
204 - .info = m88e1510_hwmon_info,
207 static int m88e6390_get_temp(struct phy_device *phydev, long *temp)
210 @@ -2459,63 +2317,112 @@ error:
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)
220 struct phy_device *phydev = dev_get_drvdata(dev);
222 + const struct marvell_hwmon_ops *ops = to_marvell_hwmon_ops(phydev);
223 + int err = -EOPNOTSUPP;
226 case hwmon_temp_input:
227 - err = m88e6390_get_temp(phydev, temp);
229 + err = ops->get_temp(phydev, temp);
231 + case hwmon_temp_crit:
232 + if (ops->get_temp_critical)
233 + err = ops->get_temp_critical(phydev, temp);
235 + case hwmon_temp_max_alarm:
236 + if (ops->get_temp_alarm)
237 + err = ops->get_temp_alarm(phydev, temp);
244 +static int marvell_hwmon_write(struct device *dev, enum hwmon_sensor_types type,
245 + u32 attr, int channel, long temp)
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;
252 + case hwmon_temp_crit:
253 + if (ops->set_temp_critical)
254 + err = ops->set_temp_critical(phydev, temp);
257 - return -EOPNOTSUPP;
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)
271 + const struct phy_device *phydev = data;
272 + const struct marvell_hwmon_ops *ops = to_marvell_hwmon_ops(phydev);
274 if (type != hwmon_temp)
278 case hwmon_temp_input:
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);
291 -static u32 m88e6390_hwmon_temp_config[] = {
293 +static u32 marvell_hwmon_chip_config[] = {
294 + HWMON_C_REGISTER_TZ,
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,
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
308 +static u32 marvell_hwmon_temp_config[] = {
309 + HWMON_T_INPUT | HWMON_T_CRIT | HWMON_T_MAX_ALARM,
313 +static const struct hwmon_channel_info marvell_hwmon_temp = {
315 - .config = m88e6390_hwmon_temp_config,
316 + .config = marvell_hwmon_temp_config,
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,
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,
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,
345 static int marvell_hwmon_name(struct phy_device *phydev)
346 @@ -2538,49 +2445,48 @@ static int marvell_hwmon_name(struct phy
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)
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;
362 err = marvell_hwmon_name(phydev);
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);
370 return PTR_ERR_OR_ZERO(priv->hwmon_dev);
373 -static int m88e1121_hwmon_probe(struct phy_device *phydev)
375 - return marvell_hwmon_probe(phydev, &m88e1121_hwmon_chip_info);
377 +static const struct marvell_hwmon_ops m88e1121_hwmon_ops = {
378 + .get_temp = m88e1121_get_temp,
381 -static int m88e1510_hwmon_probe(struct phy_device *phydev)
383 - return marvell_hwmon_probe(phydev, &m88e1510_hwmon_chip_info);
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,
392 +static const struct marvell_hwmon_ops m88e6390_hwmon_ops = {
393 + .get_temp = m88e6390_get_temp,
396 +#define DEF_MARVELL_HWMON_OPS(s) (&(s))
398 -static int m88e6390_hwmon_probe(struct phy_device *phydev)
400 - return marvell_hwmon_probe(phydev, &m88e6390_hwmon_chip_info);
403 -static int m88e1121_hwmon_probe(struct phy_device *phydev)
408 -static int m88e1510_hwmon_probe(struct phy_device *phydev)
412 +#define DEF_MARVELL_HWMON_OPS(s) NULL
414 -static int m88e6390_hwmon_probe(struct phy_device *phydev)
415 +static int marvell_hwmon_probe(struct phy_device *phydev)
419 @@ -2596,40 +2502,7 @@ static int marvell_probe(struct phy_devi
426 -static int m88e1121_probe(struct phy_device *phydev)
430 - err = marvell_probe(phydev);
434 - return m88e1121_hwmon_probe(phydev);
437 -static int m88e1510_probe(struct phy_device *phydev)
441 - err = marvell_probe(phydev);
445 - return m88e1510_hwmon_probe(phydev);
448 -static int m88e6390_probe(struct phy_device *phydev)
452 - err = marvell_probe(phydev);
456 - return m88e6390_hwmon_probe(phydev);
457 + return marvell_hwmon_probe(phydev);
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,