1f49b3af0cc82c237626aac602d0f4639d263d6a
[openwrt/staging/jow.git] / target / linux / generic / backport-6.1 / 835-v6.9-net-phy-add-support-for-PHY-LEDs-polarity-modes.patch
1 From 7ae215ee7bb855f13c80565470fc7f67db4ba82f Mon Sep 17 00:00:00 2001
2 From: Christian Marangi <ansuelsmth@gmail.com>
3 Date: Thu, 25 Jan 2024 21:36:59 +0100
4 Subject: [PATCH 3/5] net: phy: add support for PHY LEDs polarity modes
5
6 Add support for PHY LEDs polarity modes. Some PHY require LED to be set
7 to active low to be turned ON. Adds support for this by declaring
8 active-low property in DT.
9
10 PHY driver needs to declare .led_polarity_set() to configure LED
11 polarity modes. Function will pass the index with the LED index and a
12 bitmap with all the required modes to set.
13
14 Current supported modes are:
15 - active-low with the flag PHY_LED_ACTIVE_LOW. LED is set to active-low
16 to turn it ON.
17 - inactive-high-impedance with the flag PHY_LED_INACTIVE_HIGH_IMPEDANCE.
18 LED is set to high impedance to turn it OFF.
19
20 Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
21 Reviewed-by: Andrew Lunn <andrew@lunn.ch>
22 Link: https://lore.kernel.org/r/20240125203702.4552-4-ansuelsmth@gmail.com
23 Signed-off-by: Jakub Kicinski <kuba@kernel.org>
24 ---
25 drivers/net/phy/phy_device.c | 16 ++++++++++++++++
26 include/linux/phy.h | 22 ++++++++++++++++++++++
27 2 files changed, 38 insertions(+)
28
29 --- a/drivers/net/phy/phy_device.c
30 +++ b/drivers/net/phy/phy_device.c
31 @@ -3140,6 +3140,7 @@ static int of_phy_led(struct phy_device
32 struct device *dev = &phydev->mdio.dev;
33 struct led_init_data init_data = {};
34 struct led_classdev *cdev;
35 + unsigned long modes = 0;
36 struct phy_led *phyled;
37 u32 index;
38 int err;
39 @@ -3157,6 +3158,21 @@ static int of_phy_led(struct phy_device
40 if (index > U8_MAX)
41 return -EINVAL;
42
43 + if (of_property_read_bool(led, "active-low"))
44 + set_bit(PHY_LED_ACTIVE_LOW, &modes);
45 + if (of_property_read_bool(led, "inactive-high-impedance"))
46 + set_bit(PHY_LED_INACTIVE_HIGH_IMPEDANCE, &modes);
47 +
48 + if (modes) {
49 + /* Return error if asked to set polarity modes but not supported */
50 + if (!phydev->drv->led_polarity_set)
51 + return -EINVAL;
52 +
53 + err = phydev->drv->led_polarity_set(phydev, index, modes);
54 + if (err)
55 + return err;
56 + }
57 +
58 phyled->index = index;
59 if (phydev->drv->led_brightness_set)
60 cdev->brightness_set_blocking = phy_led_set_brightness;
61 --- a/include/linux/phy.h
62 +++ b/include/linux/phy.h
63 @@ -791,6 +791,15 @@ struct phy_led {
64
65 #define to_phy_led(d) container_of(d, struct phy_led, led_cdev)
66
67 +/* Modes for PHY LED configuration */
68 +enum phy_led_modes {
69 + PHY_LED_ACTIVE_LOW = 0,
70 + PHY_LED_INACTIVE_HIGH_IMPEDANCE = 1,
71 +
72 + /* keep it last */
73 + __PHY_LED_MODES_NUM,
74 +};
75 +
76 /**
77 * struct phy_driver - Driver structure for a particular PHY type
78 *
79 @@ -1059,6 +1068,19 @@ struct phy_driver {
80 int (*led_hw_control_get)(struct phy_device *dev, u8 index,
81 unsigned long *rules);
82
83 + /**
84 + * @led_polarity_set: Set the LED polarity modes
85 + * @dev: PHY device which has the LED
86 + * @index: Which LED of the PHY device
87 + * @modes: bitmap of LED polarity modes
88 + *
89 + * Configure LED with all the required polarity modes in @modes
90 + * to make it correctly turn ON or OFF.
91 + *
92 + * Returns 0, or an error code.
93 + */
94 + int (*led_polarity_set)(struct phy_device *dev, int index,
95 + unsigned long modes);
96 };
97 #define to_phy_driver(d) container_of(to_mdio_common_driver(d), \
98 struct phy_driver, mdiodrv)