Skip to contentSkip to conversation

leds: gpio: Keep driver firmware interface agnostic

PATCH Applied Andy Shevchenko 2 participants 7 messages Oct 16, 2023 – Oct 19, 2023 LED

lore
Share & tools
lore
#0 lore raw

PATCH 1/6 leds: gpio: Keep driver firmware interface agnostic

The of.h is used as a proxy to mod_devicetable, replace former by latter.

The commit 2d6180147e92 ("leds: gpio: Configure per-LED pin control") added yet another unneeded OF APIs. Replace with direct use of fwnode.

Altogether this makes driver agnostic to the firmware interface in use.

---
 drivers/leds/leds-gpio.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/leds/leds-gpio.c +3 -3
index a6597f0f3eb4..debadb48ceda 100644
@@ -11,8 +11,8 @@
 #include <linux/gpio/consumer.h>
 #include <linux/kernel.h>
 #include <linux/leds.h>
+#include <linux/mod_devicetable.h>
 #include <linux/module.h>
-#include <linux/of.h>
 #include <linux/pinctrl/consumer.h>
 #include <linux/platform_device.h>
 #include <linux/property.h>
@@ -129,8 +129,8 @@ static int create_gpio_led(const struct gpio_led *template,
 		ret = PTR_ERR(pinctrl);
 		if (ret != -ENODEV) {
 			dev_warn(led_dat->cdev.dev,
-				 "Failed to select %pOF pinctrl: %d\n",
-				 to_of_node(fwnode), ret);
+				 "Failed to select %pfw pinctrl: %d\n",
+				 fwnode, ret);
 		} else {
 			/* pinctrl-%d not present, not an error */
 			ret = 0;
Signature · 1 line
2.40.0.1.gaa8946217a0b
#1 lore raw

PATCH 2/6 leds: gpio: Utilise PTR_ERR_OR_ZERO()

Avoid a boilerplate code by using PTR_ERR_OR_ZERO() in create_gpio_led().

---
 drivers/leds/leds-gpio.c | 17 +++++++----------
 1 file changed, 7 insertions(+), 10 deletions(-)
drivers/leds/leds-gpio.c +7 -10
index debadb48ceda..4071cb9eefec 100644
@@ -125,16 +125,13 @@ static int create_gpio_led(const struct gpio_led *template,
 		return ret;
 
 	pinctrl = devm_pinctrl_get_select_default(led_dat->cdev.dev);
-	if (IS_ERR(pinctrl)) {
-		ret = PTR_ERR(pinctrl);
-		if (ret != -ENODEV) {
-			dev_warn(led_dat->cdev.dev,
-				 "Failed to select %pfw pinctrl: %d\n",
-				 fwnode, ret);
-		} else {
-			/* pinctrl-%d not present, not an error */
-			ret = 0;
-		}
+	ret = PTR_ERR_OR_ZERO(pinctrl);
+	/* pinctrl-%d not present, not an error */
+	if (ret == -ENODEV)
+		ret = 0;
+	if (ret) {
+		dev_warn(led_dat->cdev.dev, "Failed to select %pfw pinctrl: %d\n",
+			 fwnode, ret);
 	}
 
 	return ret;
Signature · 1 line
2.40.0.1.gaa8946217a0b
#2 lore raw

PATCH 3/6 leds: gpio: Refactor code to use devm_gpiod_get_index_optional()

In reply to Andy Shevchenko

Instead of checking for the specific error codes, replace devm_gpiod_get_index() with devm_gpiod_get_index_optional(). In this case we just return all errors to the caller and simply check for NULL in case if legacy GPIO is being used. As the result the code is easier to read and maintain.

---
 drivers/leds/leds-gpio.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/leds/leds-gpio.c +4 -4
index 4071cb9eefec..7c9c6a93dfd7 100644
@@ -218,13 +218,13 @@ static struct gpio_desc *gpio_led_get_gpiod(struct device *dev, int idx,
 	 * device, this will hit the board file, if any and get
 	 * the GPIO from there.
 	 */
-	gpiod = devm_gpiod_get_index(dev, NULL, idx, GPIOD_OUT_LOW);
-	if (!IS_ERR(gpiod)) {
+	gpiod = devm_gpiod_get_index_optional(dev, NULL, idx, GPIOD_OUT_LOW);
+	if (IS_ERR(gpiod))
+		return gpiod;
+	if (gpiod) {
 		gpiod_set_consumer_name(gpiod, template->name);
 		return gpiod;
 	}
-	if (PTR_ERR(gpiod) != -ENOENT)
-		return gpiod;
 
 	/*
 	 * This is the legacy code path for platform code that
Signature · 1 line
2.40.0.1.gaa8946217a0b
#3 lore raw

PATCH 4/6 leds: gpio: Move temporary variable for struct device to gpio_led_probe()

In reply to Andy Shevchenko

Use temporary variable for struct device in gpio_led_probe() in order to make code neater.

---
 drivers/leds/leds-gpio.c | 19 ++++++++-----------
 1 file changed, 8 insertions(+), 11 deletions(-)
drivers/leds/leds-gpio.c +8 -11
index 7c9c6a93dfd7..fd3f90f95fa2 100644
@@ -142,9 +142,8 @@ struct gpio_leds_priv {
 	struct gpio_led_data leds[] __counted_by(num_leds);
 };
 
-static struct gpio_leds_priv *gpio_leds_create(struct platform_device *pdev)
+static struct gpio_leds_priv *gpio_leds_create(struct device *dev)
 {
-	struct device *dev = &pdev->dev;
 	struct fwnode_handle *child;
 	struct gpio_leds_priv *priv;
 	int count, ret;
@@ -253,13 +252,13 @@ static struct gpio_desc *gpio_led_get_gpiod(struct device *dev, int idx,
 
 static int gpio_led_probe(struct platform_device *pdev)
 {
-	struct gpio_led_platform_data *pdata = dev_get_platdata(&pdev->dev);
+	struct device *dev = &pdev->dev;
+	struct gpio_led_platform_data *pdata = dev_get_platdata(dev);
 	struct gpio_leds_priv *priv;
 	int i, ret = 0;
 
 	if (pdata && pdata->num_leds) {
-		priv = devm_kzalloc(&pdev->dev, struct_size(priv, leds, pdata->num_leds),
-				    GFP_KERNEL);
+		priv = devm_kzalloc(dev, struct_size(priv, leds, pdata->num_leds), GFP_KERNEL);
 		if (!priv)
 			return -ENOMEM;
 
@@ -272,22 +271,20 @@ static int gpio_led_probe(struct platform_device *pdev)
 				led_dat->gpiod = template->gpiod;
 			else
 				led_dat->gpiod =
-					gpio_led_get_gpiod(&pdev->dev,
-							   i, template);
+					gpio_led_get_gpiod(dev, i, template);
 			if (IS_ERR(led_dat->gpiod)) {
-				dev_info(&pdev->dev, "Skipping unavailable LED gpio %d (%s)\n",
+				dev_info(dev, "Skipping unavailable LED gpio %d (%s)\n",
 					 template->gpio, template->name);
 				continue;
 			}
 
-			ret = create_gpio_led(template, led_dat,
-					      &pdev->dev, NULL,
+			ret = create_gpio_led(template, led_dat, dev, NULL,
 					      pdata->gpio_blink_set);
 			if (ret < 0)
 				return ret;
 		}
 	} else {
-		priv = gpio_leds_create(pdev);
+		priv = gpio_leds_create(dev);
 		if (IS_ERR(priv))
 			return PTR_ERR(priv);
 	}
Signature · 1 line
2.40.0.1.gaa8946217a0b
#4 lore raw

PATCH 5/6 leds: gpio: Remove unneeded assignment

In reply to Andy Shevchenko

The initial ret is not used anywhere, drop the unneeded assignment.

---
 drivers/leds/leds-gpio.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/leds/leds-gpio.c +1 -1
index fd3f90f95fa2..d6e8298ffb3e 100644
@@ -255,7 +255,7 @@ static int gpio_led_probe(struct platform_device *pdev)
 	struct device *dev = &pdev->dev;
 	struct gpio_led_platform_data *pdata = dev_get_platdata(dev);
 	struct gpio_leds_priv *priv;
-	int i, ret = 0;
+	int i, ret;
 
 	if (pdata && pdata->num_leds) {
 		priv = devm_kzalloc(dev, struct_size(priv, leds, pdata->num_leds), GFP_KERNEL);
Signature · 1 line
2.40.0.1.gaa8946217a0b
#5 lore raw

PATCH 6/6 leds: gpio: Update headers

In reply to Andy Shevchenko

Include headers which we are direct users of, no need to have proxies.

---
 drivers/leds/leds-gpio.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/leds/leds-gpio.c +5 -1
index d6e8298ffb3e..710c319ad312 100644
@@ -6,17 +6,21 @@
  * Raphael Assenat <[email protected]>
  * Copyright (C) 2008 Freescale Semiconductor, Inc.
  */
+#include <linux/container_of.h>
+#include <linux/device.h>
 #include <linux/err.h>
 #include <linux/gpio.h>
 #include <linux/gpio/consumer.h>
-#include <linux/kernel.h>
 #include <linux/leds.h>
 #include <linux/mod_devicetable.h>
 #include <linux/module.h>
+#include <linux/overflow.h>
 #include <linux/pinctrl/consumer.h>
 #include <linux/platform_device.h>
 #include <linux/property.h>
 #include <linux/slab.h>
+#include <linux/types.h>
+
 #include "leds.h"
 
 struct gpio_led_data {
Signature · 1 line
2.40.0.1.gaa8946217a0b
maintainer Applied
#6 lore raw
In reply to Andy Shevchenko
Quoted 9 lines · Andy Shevchenko

The of.h is used as a proxy to mod_devicetable, replace former by latter.

The commit 2d6180147e92 ("leds: gpio: Configure per-LED pin control") added yet another unneeded OF APIs. Replace with direct use of fwnode.

Altogether this makes driver agnostic to the firmware interface in use.

[...]

Applied, thanks!

[1/6] leds: gpio: Keep driver firmware interface agnostic

      commit: 04262082e2c203e6834bf65c7a46e2eadf212c66
[2/6] leds: gpio: Utilise PTR_ERR_OR_ZERO()
      commit: 36d270892d4733439d3fd5b713ef07029aae1bf4
[3/6] leds: gpio: Refactor code to use devm_gpiod_get_index_optional()
      commit: 4c5f908c04fda867c8130087a628a1bccec3fb05
[4/6] leds: gpio: Move temporary variable for struct device to gpio_led_probe()
      commit: 087da384361247adeb894dcb38fbbec8d4d53790
[5/6] leds: gpio: Remove unneeded assignment
      commit: cdae3873bb328fbc690722b76b67f00213c92ade
[6/6] leds: gpio: Update headers
      commit: 1f313de42c4ff9b590f00d747bab25adc0cb011c

--
Lee Jones [李琼斯]