generic: 6.1: backport PHY package MMD patch
[openwrt/staging/svanheule.git] / target / linux / generic / backport-6.1 / 801-v6.4-05-net-phy-Add-a-binding-for-PHY-LEDs.patch
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
5
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.
12
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.
15
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>
20 ---
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(+)
25
26 --- a/drivers/net/phy/Kconfig
27 +++ b/drivers/net/phy/Kconfig
28 @@ -18,6 +18,7 @@ menuconfig PHYLIB
29 depends on NETDEVICES
30 select MDIO_DEVICE
31 select MDIO_DEVRES
32 + depends on LEDS_CLASS || LEDS_CLASS=n
33 help
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
38 @@ -19,10 +19,12 @@
39 #include <linux/interrupt.h>
40 #include <linux/io.h>
41 #include <linux/kernel.h>
42 +#include <linux/list.h>
43 #include <linux/mdio.h>
44 #include <linux/mii.h>
45 #include <linux/mm.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);
53
54 dev->state = PHY_DOWN;
55 + INIT_LIST_HEAD(&dev->leds);
56
57 mutex_init(&dev->lock);
58 INIT_DELAYED_WORK(&dev->state_queue, phy_state_machine);
59 @@ -2934,6 +2937,74 @@ static bool phy_drv_supports_irq(struct
60 return phydrv->config_intr && phydrv->handle_interrupt;
61 }
62
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)
66 +{
67 + return 0;
68 +}
69 +
70 +static int of_phy_led(struct phy_device *phydev,
71 + struct device_node *led)
72 +{
73 + struct device *dev = &phydev->mdio.dev;
74 + struct led_init_data init_data = {};
75 + struct led_classdev *cdev;
76 + struct phy_led *phyled;
77 + int err;
78 +
79 + phyled = devm_kzalloc(dev, sizeof(*phyled), GFP_KERNEL);
80 + if (!phyled)
81 + return -ENOMEM;
82 +
83 + cdev = &phyled->led_cdev;
84 +
85 + err = of_property_read_u8(led, "reg", &phyled->index);
86 + if (err)
87 + return err;
88 +
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;
94 +
95 + err = devm_led_classdev_register_ext(dev, cdev, &init_data);
96 + if (err)
97 + return err;
98 +
99 + list_add(&phyled->list, &phydev->leds);
100 +
101 + return 0;
102 +}
103 +
104 +static int of_phy_leds(struct phy_device *phydev)
105 +{
106 + struct device_node *node = phydev->mdio.dev.of_node;
107 + struct device_node *leds, *led;
108 + int err;
109 +
110 + if (!IS_ENABLED(CONFIG_OF_MDIO))
111 + return 0;
112 +
113 + if (!node)
114 + return 0;
115 +
116 + leds = of_get_child_by_name(node, "leds");
117 + if (!leds)
118 + return 0;
119 +
120 + for_each_available_child_of_node(leds, led) {
121 + err = of_phy_led(phydev, led);
122 + if (err) {
123 + of_node_put(led);
124 + return err;
125 + }
126 + }
127 +
128 + return 0;
129 +}
130 +
131 /**
132 * fwnode_mdio_find_device - Given a fwnode, find the mdio_device
133 * @fwnode: pointer to the mdio_device's fwnode
134 @@ -3112,6 +3183,11 @@ static int phy_probe(struct device *dev)
135 /* Set the state to READY by default */
136 phydev->state = PHY_READY;
137
138 + /* Get the LEDs from the device tree, and instantiate standard
139 + * LEDs for them.
140 + */
141 + err = of_phy_leds(phydev);
142 +
143 out:
144 /* Re-assert the reset signal on error */
145 if (err)
146 --- a/include/linux/phy.h
147 +++ b/include/linux/phy.h
148 @@ -14,6 +14,7 @@
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 @@ -603,6 +604,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 @@ -695,6 +697,7 @@ struct phy_device {
165
166 struct phy_led_trigger *led_link_trigger;
167 #endif
168 + struct list_head leds;
169
170 /*
171 * Interrupt number for this PHY
172 @@ -769,6 +772,19 @@ struct phy_tdr_config {
173 #define PHY_PAIR_ALL -1
174
175 /**
176 + * struct phy_led: An LED driven by the PHY
177 + *
178 + * @list: List of LEDs
179 + * @led_cdev: Standard LED class structure
180 + * @index: Number of the LED
181 + */
182 +struct phy_led {
183 + struct list_head list;
184 + struct led_classdev led_cdev;
185 + u8 index;
186 +};
187 +
188 +/**
189 * struct phy_driver - Driver structure for a particular PHY type
190 *
191 * @mdiodrv: Data common to all MDIO devices