1 From 01e5b728e9e43ae444e0369695a5f72209906464 Mon Sep 17 00:00:00 2001
2 From: Andrew Lunn <andrew@lunn.ch>
3 Date: Mon, 17 Apr 2023 17:17:27 +0200
4 Subject: [PATCH 5/9] net: phy: Add a binding for PHY LEDs
6 Define common binding parsing for all PHY drivers with LEDs using
7 phylib. Parse the DT as part of the phy_probe and add LEDs to the
8 linux LED class infrastructure. For the moment, provide a dummy
9 brightness function, which will later be replaced with a call into the
10 PHY driver. This allows testing since the LED core might otherwise
11 reject an LED whose brightness cannot be set.
13 Add a dependency on LED_CLASS. It either needs to be built in, or not
14 enabled, since a modular build can result in linker errors.
16 Signed-off-by: Andrew Lunn <andrew@lunn.ch>
17 Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
18 Reviewed-by: Florian Fainelli <f.fainelli@gmail.com>
19 Signed-off-by: David S. Miller <davem@davemloft.net>
21 drivers/net/phy/Kconfig | 1 +
22 drivers/net/phy/phy_device.c | 76 ++++++++++++++++++++++++++++++++++++
23 include/linux/phy.h | 16 ++++++++
24 3 files changed, 93 insertions(+)
26 --- a/drivers/net/phy/Kconfig
27 +++ b/drivers/net/phy/Kconfig
28 @@ -18,6 +18,7 @@ menuconfig PHYLIB
32 + depends on LEDS_CLASS || LEDS_CLASS=n
34 Ethernet controllers are usually attached to PHY
35 devices. This option provides infrastructure for
36 --- a/drivers/net/phy/phy_device.c
37 +++ b/drivers/net/phy/phy_device.c
39 #include <linux/interrupt.h>
41 #include <linux/kernel.h>
42 +#include <linux/list.h>
43 #include <linux/mdio.h>
44 #include <linux/mii.h>
46 #include <linux/module.h>
47 +#include <linux/of.h>
48 #include <linux/netdevice.h>
49 #include <linux/phy.h>
50 #include <linux/phy_led_triggers.h>
51 @@ -644,6 +646,7 @@ struct phy_device *phy_device_create(str
52 device_initialize(&mdiodev->dev);
54 dev->state = PHY_DOWN;
55 + INIT_LIST_HEAD(&dev->leds);
57 mutex_init(&dev->lock);
58 INIT_DELAYED_WORK(&dev->state_queue, phy_state_machine);
59 @@ -2931,6 +2934,74 @@ static bool phy_drv_supports_irq(struct
60 return phydrv->config_intr && phydrv->handle_interrupt;
63 +/* Dummy implementation until calls into PHY driver are added */
64 +static int phy_led_set_brightness(struct led_classdev *led_cdev,
65 + enum led_brightness value)
70 +static int of_phy_led(struct phy_device *phydev,
71 + struct device_node *led)
73 + struct device *dev = &phydev->mdio.dev;
74 + struct led_init_data init_data = {};
75 + struct led_classdev *cdev;
76 + struct phy_led *phyled;
79 + phyled = devm_kzalloc(dev, sizeof(*phyled), GFP_KERNEL);
83 + cdev = &phyled->led_cdev;
85 + err = of_property_read_u8(led, "reg", &phyled->index);
89 + cdev->brightness_set_blocking = phy_led_set_brightness;
90 + cdev->max_brightness = 1;
91 + init_data.devicename = dev_name(&phydev->mdio.dev);
92 + init_data.fwnode = of_fwnode_handle(led);
93 + init_data.devname_mandatory = true;
95 + err = devm_led_classdev_register_ext(dev, cdev, &init_data);
99 + list_add(&phyled->list, &phydev->leds);
104 +static int of_phy_leds(struct phy_device *phydev)
106 + struct device_node *node = phydev->mdio.dev.of_node;
107 + struct device_node *leds, *led;
110 + if (!IS_ENABLED(CONFIG_OF_MDIO))
116 + leds = of_get_child_by_name(node, "leds");
120 + for_each_available_child_of_node(leds, led) {
121 + err = of_phy_led(phydev, led);
132 * fwnode_mdio_find_device - Given a fwnode, find the mdio_device
133 * @fwnode: pointer to the mdio_device's fwnode
134 @@ -3109,6 +3180,11 @@ static int phy_probe(struct device *dev)
135 /* Set the state to READY by default */
136 phydev->state = PHY_READY;
138 + /* Get the LEDs from the device tree, and instantiate standard
141 + err = of_phy_leds(phydev);
144 /* Re-assert the reset signal on error */
146 --- a/include/linux/phy.h
147 +++ b/include/linux/phy.h
149 #include <linux/compiler.h>
150 #include <linux/spinlock.h>
151 #include <linux/ethtool.h>
152 +#include <linux/leds.h>
153 #include <linux/linkmode.h>
154 #include <linux/netlink.h>
155 #include <linux/mdio.h>
156 @@ -593,6 +594,7 @@ struct macsec_ops;
157 * @phy_num_led_triggers: Number of triggers in @phy_led_triggers
158 * @led_link_trigger: LED trigger for link up/down
159 * @last_triggered: last LED trigger for link speed
160 + * @leds: list of PHY LED structures
161 * @master_slave_set: User requested master/slave configuration
162 * @master_slave_get: Current master/slave advertisement
163 * @master_slave_state: Current master/slave configuration
164 @@ -685,6 +687,7 @@ struct phy_device {
166 struct phy_led_trigger *led_link_trigger;
168 + struct list_head leds;
171 * Interrupt number for this PHY
172 @@ -759,6 +762,19 @@ struct phy_tdr_config {
173 #define PHY_PAIR_ALL -1
176 + * struct phy_led: An LED driven by the PHY
178 + * @list: List of LEDs
179 + * @led_cdev: Standard LED class structure
180 + * @index: Number of the LED
183 + struct list_head list;
184 + struct led_classdev led_cdev;
189 * struct phy_driver - Driver structure for a particular PHY type
191 * @mdiodrv: Data common to all MDIO devices