801-v6.4-05-net-phy-Add-a-binding-for-PHY-LEDs.patch 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. From 01e5b728e9e43ae444e0369695a5f72209906464 Mon Sep 17 00:00:00 2001
  2. From: Andrew Lunn <[email protected]>
  3. Date: Mon, 17 Apr 2023 17:17:27 +0200
  4. Subject: [PATCH 5/9] net: phy: Add a binding for PHY LEDs
  5. Define common binding parsing for all PHY drivers with LEDs using
  6. phylib. Parse the DT as part of the phy_probe and add LEDs to the
  7. linux LED class infrastructure. For the moment, provide a dummy
  8. brightness function, which will later be replaced with a call into the
  9. PHY driver. This allows testing since the LED core might otherwise
  10. reject an LED whose brightness cannot be set.
  11. Add a dependency on LED_CLASS. It either needs to be built in, or not
  12. enabled, since a modular build can result in linker errors.
  13. Signed-off-by: Andrew Lunn <[email protected]>
  14. Signed-off-by: Christian Marangi <[email protected]>
  15. Reviewed-by: Florian Fainelli <[email protected]>
  16. Signed-off-by: David S. Miller <[email protected]>
  17. ---
  18. drivers/net/phy/Kconfig | 1 +
  19. drivers/net/phy/phy_device.c | 76 ++++++++++++++++++++++++++++++++++++
  20. include/linux/phy.h | 16 ++++++++
  21. 3 files changed, 93 insertions(+)
  22. --- a/drivers/net/phy/Kconfig
  23. +++ b/drivers/net/phy/Kconfig
  24. @@ -18,6 +18,7 @@ menuconfig PHYLIB
  25. depends on NETDEVICES
  26. select MDIO_DEVICE
  27. select MDIO_DEVRES
  28. + depends on LEDS_CLASS || LEDS_CLASS=n
  29. help
  30. Ethernet controllers are usually attached to PHY
  31. devices. This option provides infrastructure for
  32. --- a/drivers/net/phy/phy_device.c
  33. +++ b/drivers/net/phy/phy_device.c
  34. @@ -19,10 +19,12 @@
  35. #include <linux/interrupt.h>
  36. #include <linux/io.h>
  37. #include <linux/kernel.h>
  38. +#include <linux/list.h>
  39. #include <linux/mdio.h>
  40. #include <linux/mii.h>
  41. #include <linux/mm.h>
  42. #include <linux/module.h>
  43. +#include <linux/of.h>
  44. #include <linux/netdevice.h>
  45. #include <linux/phy.h>
  46. #include <linux/phy_led_triggers.h>
  47. @@ -644,6 +646,7 @@ struct phy_device *phy_device_create(str
  48. device_initialize(&mdiodev->dev);
  49. dev->state = PHY_DOWN;
  50. + INIT_LIST_HEAD(&dev->leds);
  51. mutex_init(&dev->lock);
  52. INIT_DELAYED_WORK(&dev->state_queue, phy_state_machine);
  53. @@ -3037,6 +3040,74 @@ static bool phy_drv_supports_irq(struct
  54. return phydrv->config_intr && phydrv->handle_interrupt;
  55. }
  56. +/* Dummy implementation until calls into PHY driver are added */
  57. +static int phy_led_set_brightness(struct led_classdev *led_cdev,
  58. + enum led_brightness value)
  59. +{
  60. + return 0;
  61. +}
  62. +
  63. +static int of_phy_led(struct phy_device *phydev,
  64. + struct device_node *led)
  65. +{
  66. + struct device *dev = &phydev->mdio.dev;
  67. + struct led_init_data init_data = {};
  68. + struct led_classdev *cdev;
  69. + struct phy_led *phyled;
  70. + int err;
  71. +
  72. + phyled = devm_kzalloc(dev, sizeof(*phyled), GFP_KERNEL);
  73. + if (!phyled)
  74. + return -ENOMEM;
  75. +
  76. + cdev = &phyled->led_cdev;
  77. +
  78. + err = of_property_read_u8(led, "reg", &phyled->index);
  79. + if (err)
  80. + return err;
  81. +
  82. + cdev->brightness_set_blocking = phy_led_set_brightness;
  83. + cdev->max_brightness = 1;
  84. + init_data.devicename = dev_name(&phydev->mdio.dev);
  85. + init_data.fwnode = of_fwnode_handle(led);
  86. + init_data.devname_mandatory = true;
  87. +
  88. + err = devm_led_classdev_register_ext(dev, cdev, &init_data);
  89. + if (err)
  90. + return err;
  91. +
  92. + list_add(&phyled->list, &phydev->leds);
  93. +
  94. + return 0;
  95. +}
  96. +
  97. +static int of_phy_leds(struct phy_device *phydev)
  98. +{
  99. + struct device_node *node = phydev->mdio.dev.of_node;
  100. + struct device_node *leds, *led;
  101. + int err;
  102. +
  103. + if (!IS_ENABLED(CONFIG_OF_MDIO))
  104. + return 0;
  105. +
  106. + if (!node)
  107. + return 0;
  108. +
  109. + leds = of_get_child_by_name(node, "leds");
  110. + if (!leds)
  111. + return 0;
  112. +
  113. + for_each_available_child_of_node(leds, led) {
  114. + err = of_phy_led(phydev, led);
  115. + if (err) {
  116. + of_node_put(led);
  117. + return err;
  118. + }
  119. + }
  120. +
  121. + return 0;
  122. +}
  123. +
  124. /**
  125. * fwnode_mdio_find_device - Given a fwnode, find the mdio_device
  126. * @fwnode: pointer to the mdio_device's fwnode
  127. @@ -3215,6 +3286,11 @@ static int phy_probe(struct device *dev)
  128. /* Set the state to READY by default */
  129. phydev->state = PHY_READY;
  130. + /* Get the LEDs from the device tree, and instantiate standard
  131. + * LEDs for them.
  132. + */
  133. + err = of_phy_leds(phydev);
  134. +
  135. out:
  136. /* Re-assert the reset signal on error */
  137. if (err)
  138. --- a/include/linux/phy.h
  139. +++ b/include/linux/phy.h
  140. @@ -14,6 +14,7 @@
  141. #include <linux/compiler.h>
  142. #include <linux/spinlock.h>
  143. #include <linux/ethtool.h>
  144. +#include <linux/leds.h>
  145. #include <linux/linkmode.h>
  146. #include <linux/netlink.h>
  147. #include <linux/mdio.h>
  148. @@ -606,6 +607,7 @@ struct macsec_ops;
  149. * @phy_num_led_triggers: Number of triggers in @phy_led_triggers
  150. * @led_link_trigger: LED trigger for link up/down
  151. * @last_triggered: last LED trigger for link speed
  152. + * @leds: list of PHY LED structures
  153. * @master_slave_set: User requested master/slave configuration
  154. * @master_slave_get: Current master/slave advertisement
  155. * @master_slave_state: Current master/slave configuration
  156. @@ -698,6 +700,7 @@ struct phy_device {
  157. struct phy_led_trigger *led_link_trigger;
  158. #endif
  159. + struct list_head leds;
  160. /*
  161. * Interrupt number for this PHY
  162. @@ -772,6 +775,19 @@ struct phy_tdr_config {
  163. #define PHY_PAIR_ALL -1
  164. /**
  165. + * struct phy_led: An LED driven by the PHY
  166. + *
  167. + * @list: List of LEDs
  168. + * @led_cdev: Standard LED class structure
  169. + * @index: Number of the LED
  170. + */
  171. +struct phy_led {
  172. + struct list_head list;
  173. + struct led_classdev led_cdev;
  174. + u8 index;
  175. +};
  176. +
  177. +/**
  178. * struct phy_driver - Driver structure for a particular PHY type
  179. *
  180. * @mdiodrv: Data common to all MDIO devices