linuxppc-dev.lists.ozlabs.org archive mirror
 help / color / mirror / Atom feed
From: Anton Vorontsov <avorontsov@ru.mvista.com>
To: Richard Purdie <rpurdie@rpsys.net>
Cc: linuxppc-dev@ozlabs.org, Stephen Rothwell <sfr@canb.auug.org.au>,
	linux-kernel@vger.kernel.org
Subject: [PATCH v3] leds: implement OpenFirmare GPIO LED driver
Date: Tue, 15 Jul 2008 19:19:17 +0400	[thread overview]
Message-ID: <20080715151917.GA30607@polina.dev.rtsoft.ru> (raw)
In-Reply-To: <1216133032.5345.73.camel@dax.rpnet.com>

Despite leds-gpio and leds-openfirmware-gpio similar purposes, there
is not much code can be shared between the two drivers (both are mostly
driver bindings anyway).

Signed-off-by: Anton Vorontsov <avorontsov@ru.mvista.com>
---

- dropped Documentation/powerpc/ changes, since Kumar applied them via
  powerpc tree.
- renamed leds-of-gpio to leds-openfirmware-gpio

 drivers/leds/Kconfig                  |    8 ++
 drivers/leds/Makefile                 |    1 +
 drivers/leds/leds-openfirmware-gpio.c |  194 +++++++++++++++++++++++++++++++++
 3 files changed, 203 insertions(+), 0 deletions(-)
 create mode 100644 drivers/leds/leds-openfirmware-gpio.c

diff --git a/drivers/leds/Kconfig b/drivers/leds/Kconfig
index 1c35dfa..a645e8d 100644
--- a/drivers/leds/Kconfig
+++ b/drivers/leds/Kconfig
@@ -119,6 +119,14 @@ config LEDS_GPIO
 	  outputs. To be useful the particular board must have LEDs
 	  and they must be connected to the GPIO lines.
 
+config LEDS_OPENFIRMWARE_GPIO
+	tristate "OpenFirmware bindings for GPIO connected LEDs"
+	depends on LEDS_CLASS && OF_GPIO
+	help
+	  This option enables support for the LEDs connected to GPIO
+	  outputs. To be useful the particular board must have LEDs
+	  and they must be connected to the GPIO lines.
+
 config LEDS_CM_X270
 	tristate "LED Support for the CM-X270 LEDs"
 	depends on LEDS_CLASS && MACH_ARMCORE
diff --git a/drivers/leds/Makefile b/drivers/leds/Makefile
index 7156f99..0258ab7 100644
--- a/drivers/leds/Makefile
+++ b/drivers/leds/Makefile
@@ -18,6 +18,7 @@ obj-$(CONFIG_LEDS_COBALT_QUBE)		+= leds-cobalt-qube.o
 obj-$(CONFIG_LEDS_COBALT_RAQ)		+= leds-cobalt-raq.o
 obj-$(CONFIG_LEDS_PCA9532)		+= leds-pca9532.o
 obj-$(CONFIG_LEDS_GPIO)			+= leds-gpio.o
+obj-$(CONFIG_LEDS_OPENFIRMWARE_GPIO)	+= leds-openfirmware-gpio.o
 obj-$(CONFIG_LEDS_CM_X270)              += leds-cm-x270.o
 obj-$(CONFIG_LEDS_CLEVO_MAIL)		+= leds-clevo-mail.o
 obj-$(CONFIG_LEDS_HP6XX)		+= leds-hp6xx.o
diff --git a/drivers/leds/leds-openfirmware-gpio.c b/drivers/leds/leds-openfirmware-gpio.c
new file mode 100644
index 0000000..ce2c3cd
--- /dev/null
+++ b/drivers/leds/leds-openfirmware-gpio.c
@@ -0,0 +1,194 @@
+/*
+ * OpenFirmware bindings for GPIO connected LEDs
+ *
+ * Copyright (C) 2007 8D Technologies inc.
+ * Raphael Assenat <raph@8d.com>
+ * Copyright (C) 2008 MontaVista Software, Inc.
+ * Anton Vorontsov <avorontsov@ru.mvista.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/init.h>
+#include <linux/device.h>
+#include <linux/gpio.h>
+#include <linux/of_gpio.h>
+#include <linux/of_platform.h>
+#include <linux/leds.h>
+#include <linux/workqueue.h>
+#include <asm/prom.h>
+
+struct of_gpio_led {
+	struct device_node *np;
+	struct led_classdev cdev;
+	unsigned int gpio;
+	struct work_struct work;
+	u8 new_level;
+	u8 can_sleep;
+};
+
+static void gpio_led_work(struct work_struct *work)
+{
+	struct of_gpio_led *led = container_of(work, struct of_gpio_led, work);
+
+	gpio_set_value_cansleep(led->gpio, led->new_level);
+}
+
+static void gpio_led_set(struct led_classdev *led_cdev,
+			 enum led_brightness value)
+{
+	struct of_gpio_led *led = container_of(led_cdev, struct of_gpio_led,
+					       cdev);
+	int level;
+
+	if (value == LED_OFF)
+		level = 0;
+	else
+		level = 1;
+
+	/* Setting GPIOs with I2C/etc requires a task context, and we don't
+	 * seem to have a reliable way to know if we're already in one; so
+	 * let's just assume the worst.
+	 */
+	if (led->can_sleep) {
+		led->new_level = level;
+		schedule_work(&led->work);
+	} else {
+		gpio_set_value(led->gpio, level);
+	}
+}
+
+static int __devinit of_gpio_leds_probe(struct of_device *ofdev,
+					const struct of_device_id *match)
+{
+	int ret;
+	struct of_gpio_led *led;
+	struct device_node *np = ofdev->node;
+
+	led = kzalloc(sizeof(*led), GFP_KERNEL);
+	if (!led)
+		return -ENOMEM;
+
+	led->np = of_node_get(np);
+
+	ret = of_get_gpio(np, 0);
+	if (!gpio_is_valid(ret)) {
+		dev_err(&ofdev->dev, "gpio is invalid\n");
+		goto err_get_gpio;
+	}
+	led->gpio = ret;
+	led->can_sleep = gpio_cansleep(led->gpio);
+
+	led->cdev.name = of_get_property(np, "label", NULL);
+	if (!led->cdev.name)
+		led->cdev.name = dev_name(&ofdev->dev);
+	led->cdev.brightness_set = gpio_led_set;
+
+	ret = gpio_request(led->gpio, dev_name(&ofdev->dev));
+	if (ret < 0) {
+		dev_err(&ofdev->dev, "could not request gpio, status is %d\n",
+			ret);
+		goto err_gpio;
+	}
+
+	ret = gpio_direction_output(led->gpio, 0);
+	if (ret) {
+		dev_err(&ofdev->dev, "gpio could not be an output, "
+			"status is %d\n", ret);
+		goto err_gpio;
+	}
+
+	INIT_WORK(&led->work, gpio_led_work);
+	dev_set_drvdata(&ofdev->dev, led);
+
+	ret = led_classdev_register(&ofdev->dev, &led->cdev);
+	if (ret < 0) {
+		dev_err(&ofdev->dev, "could register led cdev, status is %d\n",
+			ret);
+		gpio_free(led->gpio);
+		goto err_reg_cdev;
+	}
+
+	return 0;
+
+err_reg_cdev:
+	cancel_work_sync(&led->work);
+err_gpio:
+	gpio_free(led->gpio);
+err_get_gpio:
+	of_node_put(led->np);
+	kfree(led);
+	return ret;
+}
+
+static int __devexit of_gpio_leds_remove(struct of_device *ofdev)
+{
+	struct of_gpio_led *led = dev_get_drvdata(&ofdev->dev);
+
+	led_classdev_unregister(&led->cdev);
+	cancel_work_sync(&led->work);
+	gpio_free(led->gpio);
+	of_node_put(led->np);
+	kfree(led);
+
+	return 0;
+}
+
+#ifdef CONFIG_PM
+static int of_gpio_led_suspend(struct of_device *ofdev, pm_message_t state)
+{
+	struct of_gpio_led *led = dev_get_drvdata(&ofdev->dev);
+
+	led_classdev_suspend(&led->cdev);
+	return 0;
+}
+
+static int of_gpio_led_resume(struct of_device *ofdev)
+{
+	struct of_gpio_led *led = dev_get_drvdata(&ofdev->dev);
+
+	led_classdev_resume(&led->cdev);
+	return 0;
+}
+#else
+#define of_gpio_led_suspend NULL
+#define of_gpio_led_resume NULL
+#endif /* CONFIG_PM */
+
+static const struct of_device_id of_gpio_leds_match[] = {
+	{ .compatible = "gpio-led", },
+	{},
+};
+MODULE_DEVICE_TABLE(of, of_gpio_leds_match);
+
+static struct of_platform_driver of_gpio_leds_driver = {
+	.driver = {
+		.name = "of_gpio_leds",
+		.owner = THIS_MODULE,
+	},
+	.match_table = of_gpio_leds_match,
+	.probe = of_gpio_leds_probe,
+	.remove = __devexit_p(of_gpio_leds_remove),
+	.suspend = of_gpio_led_suspend,
+	.resume = of_gpio_led_resume,
+};
+
+static int __init of_gpio_leds_init(void)
+{
+	return of_register_platform_driver(&of_gpio_leds_driver);
+}
+module_init(of_gpio_leds_init);
+
+static void __exit of_gpio_leds_exit(void)
+{
+	of_unregister_platform_driver(&of_gpio_leds_driver);
+}
+module_exit(of_gpio_leds_exit);
+
+MODULE_DESCRIPTION("OpenFirmware bindings for GPIO connected LEDs");
+MODULE_AUTHOR("Anton Vorontsov <avorontsov@ru.mvista.com>");
+MODULE_LICENSE("GPL");
-- 
1.5.5.4

  reply	other threads:[~2008-07-15 15:19 UTC|newest]

Thread overview: 59+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-07-14 16:41 [PATCH] leds: implement OpenFirmare GPIO LED driver Anton Vorontsov
2008-07-15  3:10 ` Stephen Rothwell
2008-07-15 12:38   ` Anton Vorontsov
2008-07-15 12:40     ` [PATCH v2] " Anton Vorontsov
2008-07-15 12:54       ` Richard Purdie
2008-07-15 13:24         ` Anton Vorontsov
2008-07-15 13:31           ` Richard Purdie
2008-07-15 14:23             ` Anton Vorontsov
2008-07-15 14:43               ` Richard Purdie
2008-07-15 15:19                 ` Anton Vorontsov [this message]
2008-07-16 23:18                   ` [PATCH v3] " Trent Piepho
2008-07-17  4:15                     ` Grant Likely
2008-07-17  5:13                       ` Trent Piepho
2008-07-17 13:55                         ` Anton Vorontsov
2008-07-17 20:01                           ` Trent Piepho
2008-07-17 14:05                       ` Anton Vorontsov
2008-07-17 14:13                         ` Anton Vorontsov
2008-07-17 15:04                           ` Grant Likely
2008-07-17 15:20                             ` Anton Vorontsov
2008-07-17 18:05                               ` Grant Likely
2008-07-17 20:18                                 ` Trent Piepho
2008-07-17 20:49                                   ` Grant Likely
2008-07-17 23:42                                   ` Anton Vorontsov
2008-07-18  5:09                                     ` Grant Likely
2008-07-18  9:20                                     ` Trent Piepho
2008-07-18 10:05                                       ` Anton Vorontsov
2008-07-19 21:08                                         ` PIXIS gpio controller and gpio flags Trent Piepho
2008-07-21 17:53                                           ` Anton Vorontsov
2008-07-21 21:12                                             ` Trent Piepho
2008-07-23 14:56                                               ` Anton Vorontsov
2008-07-23 23:42                                                 ` Trent Piepho
2008-07-25 16:48                                                   ` [RFC PATCH] of_gpio: implement of_get_gpio_flags() Anton Vorontsov
2008-07-26  8:26                                                     ` Trent Piepho
2008-07-25 20:38                                         ` [PATCH v3] leds: implement OpenFirmare GPIO LED driver Trent Piepho
2008-07-25 21:01                                           ` [PATCH 1/2] leds: make the default trigger name const Trent Piepho
2008-07-27  2:08                                             ` Grant Likely
2008-07-27 13:11                                               ` Stephen Rothwell
2008-07-28  1:56                                                 ` Trent Piepho
2008-07-28  2:02                                                   ` [PATCH v2] " Trent Piepho
2008-07-28  9:53                                                   ` [PATCH 1/2] " Anton Vorontsov
2008-08-29  1:22                                                     ` Trent Piepho
2008-07-25 21:01                                           ` [PATCH 2/2] leds: Support OpenFirmware led bindings Trent Piepho
2008-07-27  2:21                                             ` Grant Likely
2008-07-28  8:31                                               ` Trent Piepho
2008-07-28 17:09                                                 ` Grant Likely
2008-07-28 18:02                                                   ` Anton Vorontsov
2008-07-28 18:06                                                     ` Grant Likely
2008-07-28 18:26                                                     ` Trent Piepho
2008-07-28 18:49                                                       ` Grant Likely
2008-07-28 18:51                                                       ` Anton Vorontsov
2008-07-28 19:11                                                         ` Trent Piepho
2008-07-17 21:29                   ` [PATCH v3] leds: implement OpenFirmare GPIO LED driver Nate Case
2008-07-16 23:22                 ` [PATCH v2] " Trent Piepho
2008-07-17  5:59 ` [PATCH] " Segher Boessenkool
2008-07-17 11:07   ` Anton Vorontsov
2008-07-17 14:58     ` Sean MacLennan
2008-07-17 15:07     ` Grant Likely
2008-07-18  3:35       ` David Gibson
2008-07-18  4:44         ` Grant Likely

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=20080715151917.GA30607@polina.dev.rtsoft.ru \
    --to=avorontsov@ru.mvista.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linuxppc-dev@ozlabs.org \
    --cc=rpurdie@rpsys.net \
    --cc=sfr@canb.auug.org.au \
    /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;
as well as URLs for NNTP newsgroup(s).