mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Sebastian Hesselbarth <sebastian.hesselbarth@gmail.com>
To: Sebastian Hesselbarth <sebastian.hesselbarth@gmail.com>
Cc: barebox@lists.infradead.org
Subject: [PATCH v2 4/6] led: gpio: Properly deal with deferred probing
Date: Wed, 15 Apr 2015 00:53:18 +0200	[thread overview]
Message-ID: <1429052000-20647-5-git-send-email-sebastian.hesselbarth@gmail.com> (raw)
In-Reply-To: <1429052000-20647-1-git-send-email-sebastian.hesselbarth@gmail.com>

GPIO LEDs can suffer from deferred probing due to failing gpio request.
Instead of registering each gpio led independently, pre-allocate an
array of struct gpio_led for all and tear it down properly if probing
of one leds fails. While at it, silence error messages on -EPROBE_DEFER.

Signed-off-by: Sebastian Hesselbarth <sebastian.hesselbarth@gmail.com>
---
Changelog:
v1->v2:
- Drop static registered bitmap as two "gpio-leds" nodes will both
  call led_gpio_of_probe(). The second call will find the static
  variable modified and skip some leds.
- Use a pre-allocated array of struct gpio_led instead that will
  be torn down on failure.

Cc: barebox@lists.infradead.org
---
 drivers/led/led-gpio.c | 30 ++++++++++++++++++++++++++----
 1 file changed, 26 insertions(+), 4 deletions(-)

diff --git a/drivers/led/led-gpio.c b/drivers/led/led-gpio.c
index ae3f13f45b6c..807251721fd3 100644
--- a/drivers/led/led-gpio.c
+++ b/drivers/led/led-gpio.c
@@ -20,6 +20,7 @@
 #include <common.h>
 #include <init.h>
 #include <led.h>
+#include <malloc.h>
 #include <gpio.h>
 #include <of_gpio.h>
 
@@ -201,19 +202,32 @@ void led_gpio_rgb_unregister(struct gpio_led *led)
 static int led_gpio_of_probe(struct device_d *dev)
 {
 	struct device_node *child;
+	struct gpio_led *leds;
+	int num_leds;
+	int ret = 0, n = 0;
+
+	num_leds = of_get_child_count(dev->device_node);
+	if (num_leds <= 0)
+		return num_leds;
+
+	leds = xzalloc(num_leds * sizeof(struct gpio_led));
 
 	for_each_child_of_node(dev->device_node, child) {
-		struct gpio_led *gled;
+		struct gpio_led *gled = &leds[n];
 		const char *default_state;
 		enum of_gpio_flags flags;
 		int gpio;
 		const char *label;
 
 		gpio = of_get_named_gpio_flags(child, "gpios", 0, &flags);
-		if (gpio < 0)
-			continue;
+		if (gpio < 0) {
+			if (gpio != -EPROBE_DEFER)
+				dev_err(dev, "failed to get gpio for %s: %d\n",
+					child->full_name, gpio);
+			ret = gpio;
+			goto err;
+		}
 
-		gled = xzalloc(sizeof(*gled));
 		if (of_property_read_string(child, "label", &label))
 			label = child->name;
 		gled->led.name = xstrdup(label);
@@ -233,9 +247,17 @@ static int led_gpio_of_probe(struct device_d *dev)
 			else if (!strcmp(default_state, "off"))
 				led_gpio_set(&gled->led, 0);
 		}
+
+		n++;
 	}
 
 	return 0;
+
+err:
+	for (n = n - 1; n >= 0; n--)
+		led_gpio_unregister(&leds[n]);
+	free(leds);
+	return ret;
 }
 
 static struct of_device_id led_gpio_of_ids[] = {
-- 
2.1.0


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

  parent reply	other threads:[~2015-04-14 22:53 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-04-14 22:53 [PATCH v2 0/6] Introduce " Sebastian Hesselbarth
2015-04-14 22:53 ` [PATCH v2 1/6] base: " Sebastian Hesselbarth
2015-04-14 22:53 ` [PATCH v2 2/6] gpio: Return -EPROBE_DEFER on gpio_get_num() Sebastian Hesselbarth
2015-04-14 22:53 ` [PATCH v2 3/6] OF: gpio: Silence error message on -EPROBE_DEFER Sebastian Hesselbarth
2015-04-14 22:53 ` Sebastian Hesselbarth [this message]
2015-04-14 22:53 ` [PATCH v2 5/6] led: gpio: Free GPIOs on unregister() Sebastian Hesselbarth
2015-04-14 22:53 ` [PATCH v2 6/6] gpio: orion: Convert to platform_driver Sebastian Hesselbarth
2015-04-17  5:24 ` [PATCH v2 0/6] Introduce deferred probing Sascha Hauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1429052000-20647-5-git-send-email-sebastian.hesselbarth@gmail.com \
    --to=sebastian.hesselbarth@gmail.com \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox