* [PATCH] leds: netxbig: Convert to use GPIO descriptors
@ 2020-04-15 14:51 Linus Walleij
2020-04-17 12:52 ` Pavel Machek
0 siblings, 1 reply; 6+ messages in thread
From: Linus Walleij @ 2020-04-15 14:51 UTC (permalink / raw)
To: Jacek Anaszewski, Pavel Machek, Dan Murphy
Cc: linux-leds, Linus Walleij, Simon Guinot
This converts the NetXbig LED driver to use GPIO descriptors
instead of using the legacy interfaces in <linux/of_gpio.h>
and <linux/gpio.h> to iteratively parse the device tree for
global GPIO numbers.
Cc: Simon Guinot <simon.guinot@sequanux.org>
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
---
drivers/leds/leds-netxbig.c | 148 ++++++++++++++++++++----------------
1 file changed, 81 insertions(+), 67 deletions(-)
diff --git a/drivers/leds/leds-netxbig.c b/drivers/leds/leds-netxbig.c
index 14ef4ccdda3a..ceceeb6a0e96 100644
--- a/drivers/leds/leds-netxbig.c
+++ b/drivers/leds/leds-netxbig.c
@@ -12,16 +12,17 @@
#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/platform_device.h>
-#include <linux/gpio.h>
-#include <linux/of_gpio.h>
+#include <linux/gpio/consumer.h>
#include <linux/leds.h>
+#include <linux/of.h>
+#include <linux/of_platform.h>
struct netxbig_gpio_ext {
- unsigned int *addr;
+ struct gpio_desc **addr;
int num_addr;
- unsigned int *data;
+ struct gpio_desc **data;
int num_data;
- unsigned int enable;
+ struct gpio_desc *enable;
};
enum netxbig_led_mode {
@@ -69,7 +70,7 @@ static void gpio_ext_set_addr(struct netxbig_gpio_ext *gpio_ext, int addr)
int pin;
for (pin = 0; pin < gpio_ext->num_addr; pin++)
- gpio_set_value(gpio_ext->addr[pin], (addr >> pin) & 1);
+ gpiod_set_value(gpio_ext->addr[pin], (addr >> pin) & 1);
}
static void gpio_ext_set_data(struct netxbig_gpio_ext *gpio_ext, int data)
@@ -77,14 +78,14 @@ static void gpio_ext_set_data(struct netxbig_gpio_ext *gpio_ext, int data)
int pin;
for (pin = 0; pin < gpio_ext->num_data; pin++)
- gpio_set_value(gpio_ext->data[pin], (data >> pin) & 1);
+ gpiod_set_value(gpio_ext->data[pin], (data >> pin) & 1);
}
static void gpio_ext_enable_select(struct netxbig_gpio_ext *gpio_ext)
{
/* Enable select is done on the raising edge. */
- gpio_set_value(gpio_ext->enable, 0);
- gpio_set_value(gpio_ext->enable, 1);
+ gpiod_set_value(gpio_ext->enable, 0);
+ gpiod_set_value(gpio_ext->enable, 1);
}
static void gpio_ext_set_value(struct netxbig_gpio_ext *gpio_ext,
@@ -99,41 +100,6 @@ static void gpio_ext_set_value(struct netxbig_gpio_ext *gpio_ext,
spin_unlock_irqrestore(&gpio_ext_lock, flags);
}
-static int gpio_ext_init(struct platform_device *pdev,
- struct netxbig_gpio_ext *gpio_ext)
-{
- int err;
- int i;
-
- if (unlikely(!gpio_ext))
- return -EINVAL;
-
- /* Configure address GPIOs. */
- for (i = 0; i < gpio_ext->num_addr; i++) {
- err = devm_gpio_request_one(&pdev->dev, gpio_ext->addr[i],
- GPIOF_OUT_INIT_LOW,
- "GPIO extension addr");
- if (err)
- return err;
- }
- /* Configure data GPIOs. */
- for (i = 0; i < gpio_ext->num_data; i++) {
- err = devm_gpio_request_one(&pdev->dev, gpio_ext->data[i],
- GPIOF_OUT_INIT_LOW,
- "GPIO extension data");
- if (err)
- return err;
- }
- /* Configure "enable select" GPIO. */
- err = devm_gpio_request_one(&pdev->dev, gpio_ext->enable,
- GPIOF_OUT_INIT_LOW,
- "GPIO extension enable");
- if (err)
- return err;
-
- return 0;
-}
-
/*
* Class LED driver.
*/
@@ -347,15 +313,47 @@ static int create_netxbig_led(struct platform_device *pdev,
return devm_led_classdev_register(&pdev->dev, &led_dat->cdev);
}
-static int gpio_ext_get_of_pdata(struct device *dev, struct device_node *np,
- struct netxbig_gpio_ext *gpio_ext)
+/**
+ * netxbig_gpio_ext_remove() - Clean up GPIO extension data
+ * @data: managed resource data to clean up
+ *
+ * Since we pick GPIO descriptors from another device than the device our
+ * driver is probing to, we need to register a specific callback to free
+ * these up using managed resources.
+ */
+static void netxbig_gpio_ext_remove(void *data)
+{
+ struct netxbig_gpio_ext *gpio_ext = data;
+ int i;
+
+ for (i = 0; i < gpio_ext->num_addr; i++)
+ gpiod_put(gpio_ext->addr[i]);
+ for (i = 0; i < gpio_ext->num_data; i++)
+ gpiod_put(gpio_ext->data[i]);
+ gpiod_put(gpio_ext->enable);
+}
+
+/**
+ * netxbig_gpio_ext_get() - Obtain GPIO extension device data
+ * @dev: main LED device
+ * @gpio_ext_dev: the GPIO extension device
+ * @gpio_ext: the data structure holding the GPIO extension data
+ *
+ * This function walks the subdevice that only contain GPIO line
+ * handles in the device tree and obtains the GPIO descriptors from that
+ * device.
+ */
+static int netxbig_gpio_ext_get(struct device *dev,
+ struct device *gpio_ext_dev,
+ struct netxbig_gpio_ext *gpio_ext)
{
- int *addr, *data;
+ struct gpio_desc **addr, **data;
int num_addr, num_data;
+ struct gpio_desc *gpiod;
int ret;
int i;
- ret = of_gpio_named_count(np, "addr-gpios");
+ ret = gpiod_count(gpio_ext_dev, "addr");
if (ret < 0) {
dev_err(dev,
"Failed to count GPIOs in DT property addr-gpios\n");
@@ -366,16 +364,25 @@ static int gpio_ext_get_of_pdata(struct device *dev, struct device_node *np,
if (!addr)
return -ENOMEM;
+ /*
+ * We cannot use devm_ managed resources with these GPIO descriptors
+ * since they are associated with the "GPIO extension device" which
+ * does not probe any driver. The device tree parser will however
+ * populate a platform device for it so we can anyway obtain the
+ * GPIO descriptors from the device.
+ */
for (i = 0; i < num_addr; i++) {
- ret = of_get_named_gpio(np, "addr-gpios", i);
- if (ret < 0)
- return ret;
- addr[i] = ret;
+ gpiod = gpiod_get_index(gpio_ext_dev, "addr", i,
+ GPIOD_OUT_LOW);
+ if (IS_ERR(gpiod))
+ return PTR_ERR(gpiod);
+ gpiod_set_consumer_name(gpiod, "GPIO extension addr");
+ addr[i] = gpiod;
}
gpio_ext->addr = addr;
gpio_ext->num_addr = num_addr;
- ret = of_gpio_named_count(np, "data-gpios");
+ ret = gpiod_count(gpio_ext_dev, "data");
if (ret < 0) {
dev_err(dev,
"Failed to count GPIOs in DT property data-gpios\n");
@@ -387,23 +394,26 @@ static int gpio_ext_get_of_pdata(struct device *dev, struct device_node *np,
return -ENOMEM;
for (i = 0; i < num_data; i++) {
- ret = of_get_named_gpio(np, "data-gpios", i);
- if (ret < 0)
- return ret;
- data[i] = ret;
+ gpiod = gpiod_get_index(gpio_ext_dev, "data", i,
+ GPIOD_OUT_LOW);
+ if (IS_ERR(gpiod))
+ return PTR_ERR(gpiod);
+ gpiod_set_consumer_name(gpiod, "GPIO extension data");
+ data[i] = gpiod;
}
gpio_ext->data = data;
gpio_ext->num_data = num_data;
- ret = of_get_named_gpio(np, "enable-gpio", 0);
- if (ret < 0) {
+ gpiod = gpiod_get(gpio_ext_dev, "enable", GPIOD_OUT_LOW);
+ if (IS_ERR(gpiod)) {
dev_err(dev,
"Failed to get GPIO from DT property enable-gpio\n");
- return ret;
+ return PTR_ERR(gpiod);
}
- gpio_ext->enable = ret;
+ gpiod_set_consumer_name(gpiod, "GPIO extension enable");
+ gpio_ext->enable = gpiod;
- return 0;
+ return devm_add_action_or_reset(dev, netxbig_gpio_ext_remove, gpio_ext);
}
static int netxbig_leds_get_of_pdata(struct device *dev,
@@ -411,6 +421,8 @@ static int netxbig_leds_get_of_pdata(struct device *dev,
{
struct device_node *np = dev->of_node;
struct device_node *gpio_ext_np;
+ struct platform_device *gpio_ext_pdev;
+ struct device *gpio_ext_dev;
struct device_node *child;
struct netxbig_gpio_ext *gpio_ext;
struct netxbig_led_timer *timers;
@@ -426,13 +438,19 @@ static int netxbig_leds_get_of_pdata(struct device *dev,
dev_err(dev, "Failed to get DT handle gpio-ext\n");
return -EINVAL;
}
+ gpio_ext_pdev = of_find_device_by_node(gpio_ext_np);
+ if (!gpio_ext_pdev) {
+ dev_err(dev, "Failed to find platform device for gpio-ext\n");
+ return -ENODEV;
+ }
+ gpio_ext_dev = &gpio_ext_pdev->dev;
gpio_ext = devm_kzalloc(dev, sizeof(*gpio_ext), GFP_KERNEL);
if (!gpio_ext) {
of_node_put(gpio_ext_np);
return -ENOMEM;
}
- ret = gpio_ext_get_of_pdata(dev, gpio_ext_np, gpio_ext);
+ ret = netxbig_gpio_ext_get(dev, gpio_ext_dev, gpio_ext);
of_node_put(gpio_ext_np);
if (ret)
return ret;
@@ -585,10 +603,6 @@ static int netxbig_led_probe(struct platform_device *pdev)
if (!leds_data)
return -ENOMEM;
- ret = gpio_ext_init(pdev, pdata->gpio_ext);
- if (ret < 0)
- return ret;
-
for (i = 0; i < pdata->num_leds; i++) {
ret = create_netxbig_led(pdev, pdata,
&leds_data[i], &pdata->leds[i]);
--
2.25.2
^ permalink raw reply related [flat|nested] 6+ messages in thread* Re: [PATCH] leds: netxbig: Convert to use GPIO descriptors
2020-04-15 14:51 [PATCH] leds: netxbig: Convert to use GPIO descriptors Linus Walleij
@ 2020-04-17 12:52 ` Pavel Machek
2020-04-17 18:07 ` Simon Guinot
0 siblings, 1 reply; 6+ messages in thread
From: Pavel Machek @ 2020-04-17 12:52 UTC (permalink / raw)
To: Linus Walleij; +Cc: Jacek Anaszewski, Dan Murphy, linux-leds, Simon Guinot
[-- Attachment #1: Type: text/plain, Size: 570 bytes --]
Hi!
> This converts the NetXbig LED driver to use GPIO descriptors
> instead of using the legacy interfaces in <linux/of_gpio.h>
> and <linux/gpio.h> to iteratively parse the device tree for
> global GPIO numbers.
>
> Cc: Simon Guinot <simon.guinot@sequanux.org>
> Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
Nothing obviously wrong here, but I'd not mind some testing.
Best regards,
Pavel
--
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 195 bytes --]
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] leds: netxbig: Convert to use GPIO descriptors
2020-04-17 12:52 ` Pavel Machek
@ 2020-04-17 18:07 ` Simon Guinot
2020-04-17 20:58 ` Linus Walleij
0 siblings, 1 reply; 6+ messages in thread
From: Simon Guinot @ 2020-04-17 18:07 UTC (permalink / raw)
To: Pavel Machek; +Cc: Linus Walleij, Jacek Anaszewski, Dan Murphy, linux-leds
[-- Attachment #1: Type: text/plain, Size: 571 bytes --]
On Fri, Apr 17, 2020 at 02:52:19PM +0200, Pavel Machek wrote:
> Hi!
>
> > This converts the NetXbig LED driver to use GPIO descriptors
> > instead of using the legacy interfaces in <linux/of_gpio.h>
> > and <linux/gpio.h> to iteratively parse the device tree for
> > global GPIO numbers.
> >
> > Cc: Simon Guinot <simon.guinot@sequanux.org>
> > Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
>
> Nothing obviously wrong here, but I'd not mind some testing.
Hi Pavel and Linux,
If you are not in a hurry, then I'll try it next week.
Simon
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] leds: netxbig: Convert to use GPIO descriptors
2020-04-17 18:07 ` Simon Guinot
@ 2020-04-17 20:58 ` Linus Walleij
2020-04-23 22:38 ` Simon Guinot
0 siblings, 1 reply; 6+ messages in thread
From: Linus Walleij @ 2020-04-17 20:58 UTC (permalink / raw)
To: Simon Guinot
Cc: Pavel Machek, Jacek Anaszewski, Dan Murphy, Linux LED Subsystem
On Fri, Apr 17, 2020 at 8:07 PM Simon Guinot <simon.guinot@sequanux.org> wrote:
> On Fri, Apr 17, 2020 at 02:52:19PM +0200, Pavel Machek wrote:
> > Hi!
> >
> > > This converts the NetXbig LED driver to use GPIO descriptors
> > > instead of using the legacy interfaces in <linux/of_gpio.h>
> > > and <linux/gpio.h> to iteratively parse the device tree for
> > > global GPIO numbers.
> > >
> > > Cc: Simon Guinot <simon.guinot@sequanux.org>
> > > Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
> >
> > Nothing obviously wrong here, but I'd not mind some testing.
>
> Hi Pavel and Linux,
>
> If you are not in a hurry, then I'll try it next week.
No hurry, take the time you need. If it explodes, please help me fix it!
Yours,
Linus Walleij
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] leds: netxbig: Convert to use GPIO descriptors
2020-04-17 20:58 ` Linus Walleij
@ 2020-04-23 22:38 ` Simon Guinot
2020-04-27 12:12 ` Pavel Machek
0 siblings, 1 reply; 6+ messages in thread
From: Simon Guinot @ 2020-04-23 22:38 UTC (permalink / raw)
To: Linus Walleij
Cc: Pavel Machek, Jacek Anaszewski, Dan Murphy, Linux LED Subsystem
[-- Attachment #1: Type: text/plain, Size: 1118 bytes --]
On Fri, Apr 17, 2020 at 10:58:24PM +0200, Linus Walleij wrote:
> On Fri, Apr 17, 2020 at 8:07 PM Simon Guinot <simon.guinot@sequanux.org> wrote:
> > On Fri, Apr 17, 2020 at 02:52:19PM +0200, Pavel Machek wrote:
> > > Hi!
> > >
> > > > This converts the NetXbig LED driver to use GPIO descriptors
> > > > instead of using the legacy interfaces in <linux/of_gpio.h>
> > > > and <linux/gpio.h> to iteratively parse the device tree for
> > > > global GPIO numbers.
> > > >
> > > > Cc: Simon Guinot <simon.guinot@sequanux.org>
> > > > Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
> > >
> > > Nothing obviously wrong here, but I'd not mind some testing.
> >
> > Hi Pavel and Linux,
> >
> > If you are not in a hurry, then I'll try it next week.
>
> No hurry, take the time you need. If it explodes, please help me fix it!
Well, nothing blew up. I successfully tested your patch on a LaCie
"2Big NAS" board and it appears that the LEDs are working nicely.
Thanks for converting this driver to use the GPIO descriptor API.
Tested-by: Simon Guinot <simon.guinot@sequanux.org>
Simon
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] leds: netxbig: Convert to use GPIO descriptors
2020-04-23 22:38 ` Simon Guinot
@ 2020-04-27 12:12 ` Pavel Machek
0 siblings, 0 replies; 6+ messages in thread
From: Pavel Machek @ 2020-04-27 12:12 UTC (permalink / raw)
To: Simon Guinot
Cc: Linus Walleij, Jacek Anaszewski, Dan Murphy, Linux LED Subsystem
[-- Attachment #1: Type: text/plain, Size: 1387 bytes --]
On Fri 2020-04-24 00:38:18, Simon Guinot wrote:
> On Fri, Apr 17, 2020 at 10:58:24PM +0200, Linus Walleij wrote:
> > On Fri, Apr 17, 2020 at 8:07 PM Simon Guinot <simon.guinot@sequanux.org> wrote:
> > > On Fri, Apr 17, 2020 at 02:52:19PM +0200, Pavel Machek wrote:
> > > > Hi!
> > > >
> > > > > This converts the NetXbig LED driver to use GPIO descriptors
> > > > > instead of using the legacy interfaces in <linux/of_gpio.h>
> > > > > and <linux/gpio.h> to iteratively parse the device tree for
> > > > > global GPIO numbers.
> > > > >
> > > > > Cc: Simon Guinot <simon.guinot@sequanux.org>
> > > > > Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
> > > >
> > > > Nothing obviously wrong here, but I'd not mind some testing.
> > >
> > > Hi Pavel and Linux,
> > >
> > > If you are not in a hurry, then I'll try it next week.
> >
> > No hurry, take the time you need. If it explodes, please help me fix it!
>
> Well, nothing blew up. I successfully tested your patch on a LaCie
> "2Big NAS" board and it appears that the LEDs are working nicely.
>
> Thanks for converting this driver to use the GPIO descriptor API.
>
> Tested-by: Simon Guinot <simon.guinot@sequanux.org>
Thanks, applied.
Pavel
--
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 195 bytes --]
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2020-04-27 12:12 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-04-15 14:51 [PATCH] leds: netxbig: Convert to use GPIO descriptors Linus Walleij
2020-04-17 12:52 ` Pavel Machek
2020-04-17 18:07 ` Simon Guinot
2020-04-17 20:58 ` Linus Walleij
2020-04-23 22:38 ` Simon Guinot
2020-04-27 12:12 ` Pavel Machek
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox