2023-10-16 16:10:23

by Andy Shevchenko

[permalink] [raw]
Subject: [PATCH v1 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.

Signed-off-by: Andy Shevchenko <[email protected]>
---
drivers/leds/leds-gpio.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/leds/leds-gpio.c b/drivers/leds/leds-gpio.c
index a6597f0f3eb4..debadb48ceda 100644
--- a/drivers/leds/leds-gpio.c
+++ b/drivers/leds/leds-gpio.c
@@ -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;
--
2.40.0.1.gaa8946217a0b


2023-10-16 16:10:32

by Andy Shevchenko

[permalink] [raw]
Subject: [PATCH v1 3/6] leds: gpio: Refactor code to use devm_gpiod_get_index_optional()

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.

Signed-off-by: Andy Shevchenko <[email protected]>
---
drivers/leds/leds-gpio.c | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/drivers/leds/leds-gpio.c b/drivers/leds/leds-gpio.c
index 4071cb9eefec..7c9c6a93dfd7 100644
--- a/drivers/leds/leds-gpio.c
+++ b/drivers/leds/leds-gpio.c
@@ -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
--
2.40.0.1.gaa8946217a0b

2023-10-16 16:11:13

by Andy Shevchenko

[permalink] [raw]
Subject: [PATCH v1 4/6] leds: gpio: Move temporary variable for struct device to gpio_led_probe()

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

Signed-off-by: Andy Shevchenko <[email protected]>
---
drivers/leds/leds-gpio.c | 19 ++++++++-----------
1 file changed, 8 insertions(+), 11 deletions(-)

diff --git a/drivers/leds/leds-gpio.c b/drivers/leds/leds-gpio.c
index 7c9c6a93dfd7..fd3f90f95fa2 100644
--- a/drivers/leds/leds-gpio.c
+++ b/drivers/leds/leds-gpio.c
@@ -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);
}
--
2.40.0.1.gaa8946217a0b

2023-10-16 16:11:18

by Andy Shevchenko

[permalink] [raw]
Subject: [PATCH v1 2/6] leds: gpio: Utilise PTR_ERR_OR_ZERO()

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

Signed-off-by: Andy Shevchenko <[email protected]>
---
drivers/leds/leds-gpio.c | 17 +++++++----------
1 file changed, 7 insertions(+), 10 deletions(-)

diff --git a/drivers/leds/leds-gpio.c b/drivers/leds/leds-gpio.c
index debadb48ceda..4071cb9eefec 100644
--- a/drivers/leds/leds-gpio.c
+++ b/drivers/leds/leds-gpio.c
@@ -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;
--
2.40.0.1.gaa8946217a0b

2023-10-16 16:11:22

by Andy Shevchenko

[permalink] [raw]
Subject: [PATCH v1 5/6] leds: gpio: Remove unneeded assignment

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

Signed-off-by: Andy Shevchenko <[email protected]>
---
drivers/leds/leds-gpio.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/leds/leds-gpio.c b/drivers/leds/leds-gpio.c
index fd3f90f95fa2..d6e8298ffb3e 100644
--- a/drivers/leds/leds-gpio.c
+++ b/drivers/leds/leds-gpio.c
@@ -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);
--
2.40.0.1.gaa8946217a0b

2023-10-16 16:11:54

by Andy Shevchenko

[permalink] [raw]
Subject: [PATCH v1 6/6] leds: gpio: Update headers

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

Signed-off-by: Andy Shevchenko <[email protected]>
---
drivers/leds/leds-gpio.c | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/drivers/leds/leds-gpio.c b/drivers/leds/leds-gpio.c
index d6e8298ffb3e..710c319ad312 100644
--- a/drivers/leds/leds-gpio.c
+++ b/drivers/leds/leds-gpio.c
@@ -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 {
--
2.40.0.1.gaa8946217a0b

2023-10-19 12:26:36

by Lee Jones

[permalink] [raw]
Subject: Re: [PATCH v1 1/6] leds: gpio: Keep driver firmware interface agnostic

On Mon, 16 Oct 2023 19:10:00 +0300, Andy Shevchenko wrote:
> 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 [李琼斯]