[net-next RFC PATCH v2 2/4] net: phy: add support for PHY LEDs active-low

Christian Marangi
2023-12-11T19:23:54Z

Add support for PHY LEDs active-low. Some PHY require LED to be set to
active low to be turned on. Adds support for this by declaring
active-low property in DT.

Active-low can be defined in 2 different way:
- In leds node to apply the active low setting globally for every LED.
- In each led to apply the active low setting per LED (if supported).

PHY driver needs to declare .led_polarity_set() to configure LED
polarity. Index will be -1 if the option is set globally or will
indicate the LED to apply the polarity settings. Function finally pass a
bool to indicate if LED has to be set active low.

.led_polarity_set() is called for both leds node and for each led
subnode. PHY driver will ignore the additional call based on the passed
index value if global setting or per LED setting is supported.

Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
---
Changes v2:
- Add this patch

drivers/net/phy/phy_device.c | 18 ++++++++++++++++++
include/linux/phy.h | 15 +++++++++++++++
2 files changed, 33 insertions(+)

diff --git a/drivers/net/phy/phy_device.c b/drivers/net/phy/phy_device.c
index d8e9335d415c..a9f5d250abff 100644
--- a/drivers/net/phy/phy_device.c
+++ b/drivers/net/phy/phy_device.c
@@ -3093,6 +3093,7 @@ static int of_phy_led(struct phy_device *phydev,
struct led_init_data init_data = {};
struct led_classdev *cdev;
struct phy_led *phyled;
+ bool active_low;
u32 index;
int err;

@@ -3109,6 +3110,13 @@ static int of_phy_led(struct phy_device *phydev,
if (index > U8_MAX)
return -EINVAL;

+ active_low = of_property_read_bool(led, "active-low");
+ if (phydev->drv->led_polarity_set) {
+ err = phydev->drv->led_polarity_set(phydev, index, active_low);
+ if (err)
+ return err;
+ }
+
phyled->index = index;
if (phydev->drv->led_brightness_set)
cdev->brightness_set_blocking = phy_led_set_brightness;
@@ -3145,6 +3153,7 @@ static int of_phy_leds(struct phy_device *phydev)
{
struct device_node *node = phydev->mdio.dev.of_node;
struct device_node *leds, *led;
+ bool active_low;
int err;

if (!IS_ENABLED(CONFIG_OF_MDIO))
@@ -3157,6 +3166,15 @@ static int of_phy_leds(struct phy_device *phydev)
if (!leds)
return 0;

+ active_low = of_property_read_bool(leds, "active-low");
+ if (phydev->drv->led_polarity_set) {
+ err = phydev->drv->led_polarity_set(phydev, -1, active_low);
+ if (err) {
+ of_node_put(leds);
+ return err;
+ }
+ }
+
for_each_available_child_of_node(leds, led) {
err = of_phy_led(phydev, led);
if (err) {
diff --git a/include/linux/phy.h b/include/linux/phy.h
index 6e7ebcc50b85..cbdebf174361 100644
--- a/include/linux/phy.h
+++ b/include/linux/phy.h
@@ -1145,6 +1145,21 @@ struct phy_driver {
int (*led_hw_control_get)(struct phy_device *dev, u8 index,
unsigned long *rules);

+ /**
+ * @led_polarity_set: Set the LED polarity if active low
+ * @dev: PHY device which has the LED
+ * @index: Which LED of the PHY device or -1
+ * @active_low: LED needs to be set low to turn on
+ *
+ * Set the PHY to require the LED low to turn on.
+ * index can be the LED of the PHY device or -1 whether the
+ * LED polatiry is global and applied to every LED or polarity
+ * is set per LED.
+ *
+ * Returns 0, or an error code.
+ */
+ int (*led_polarity_set)(struct phy_device *dev, int index,
+ bool active_low);
};
#define to_phy_driver(d) container_of(to_mdio_common_driver(d), \
struct phy_driver, mdiodrv)
--
2.40.1