diff mbox series

[net-next,v3,2/4] net: phy: add support for PHY LEDs active-low

Message ID 20231213111322.6152-2-ansuelsmth@gmail.com (mailing list archive)
State Changes Requested
Delegated to: Netdev Maintainers
Headers show
Series [net-next,v3,1/4] dt-bindings: net: phy: Document new LEDs active-low property | expand

Checks

Context Check Description
netdev/series_format warning Series does not have a cover letter
netdev/tree_selection success Clearly marked for net-next
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 1564 this patch: 1564
netdev/cc_maintainers success CCed 7 of 7 maintainers
netdev/build_clang success Errors and warnings before: 1156 this patch: 1156
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 1601 this patch: 1601
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 63 lines checked
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Christian Marangi Dec. 13, 2023, 11:13 a.m. UTC
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 v3:
- Out of RFC
Changes v2:
- Add this patch

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

Patch

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)