Skip to contentSkip to conversation

LED: Add LED Class

RFC PATCH In review Richard Purdie 4 participants 5 messages Dec 5, 2005 – Dec 6, 2005

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/8 LED: Add LED Class

Add the foundations of a new LEDs subsystem. This patch adds a class which presents LED devices within sysfs and allows their brightness to be controlled.

drivers/leds/Kconfig new file +18 -0
Index: linux-2.6.15-rc2/drivers/leds/Kconfig
===================================================================
@@ -0,0 +1,18 @@
+
+menu "LED devices"
+
+config NEW_LEDS
+	bool "LED Support"
+	help
+	  Say Y to enable Linux LED support.  This is not related to standard
+	  keyboard LEDs which are controlled via the input system.
+
+config LEDS_CLASS
+	tristate "LED Class Support"
+	depends NEW_LEDS
+	help
+	  This option enables the led sysfs class in /sys/class/leds.  You'll
+	  need this to do anything useful with LEDs.  If unsure, say N.
+
+endmenu
+
drivers/leds/Makefile new file +4 -0
Index: linux-2.6.15-rc2/drivers/leds/Makefile
===================================================================
@@ -0,0 +1,4 @@
+
+# LED Core
+obj-$(CONFIG_NEW_LEDS)			+= led_core.o
+obj-$(CONFIG_LEDS_CLASS)		+= led_class.o
include/linux/leds.h new file +43 -0

43 lines not on this page · raw message

arch/arm/Kconfig +2 -0
Index: linux-2.6.15-rc2/arch/arm/Kconfig
===================================================================
@@ -738,6 +738,8 @@
 
 source "drivers/mfd/Kconfig"
 
+source "drivers/leds/Kconfig"
+
 source "drivers/media/Kconfig"
 
 source "drivers/video/Kconfig"
drivers/Makefile +1 -0
Index: linux-2.6.15-rc2/drivers/Makefile
===================================================================
@@ -64,6 +64,7 @@
 obj-$(CONFIG_EISA)		+= eisa/
 obj-$(CONFIG_CPU_FREQ)		+= cpufreq/
 obj-$(CONFIG_MMC)		+= mmc/
+obj-$(CONFIG_NEW_LEDS)		+= leds/
 obj-$(CONFIG_INFINIBAND)	+= infiniband/
 obj-$(CONFIG_SGI_IOC4)		+= sn/
 obj-y				+= firmware/
drivers/Kconfig +2 -0
Index: linux-2.6.15-rc2/drivers/Kconfig
===================================================================
@@ -62,6 +62,8 @@
 
 source "drivers/mmc/Kconfig"
 
+source "drivers/leds/Kconfig"
+
 source "drivers/infiniband/Kconfig"
 
 source "drivers/sn/Kconfig"
drivers/leds/leds.h new file +15 -0
Index: linux-2.6.15-rc2/drivers/leds/leds.h
===================================================================
@@ -0,0 +1,15 @@
+/*
+ * LED Core
+ *
+ * Copyright 2005 Openedhand Ltd.
+ *
+ * Author: Richard Purdie <[email protected]>
+ *
+ * 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.
+ *
+ */
+
+extern rwlock_t leds_list_lock;
+extern struct list_head leds_list;
drivers/leds/led_class.c new file +171 -0

171 lines not on this page · raw message

drivers/leds/led_core.c new file +24 -0
Index: linux-2.6.15-rc2/drivers/leds/led_core.c
===================================================================
@@ -0,0 +1,24 @@
+/*
+ * LED Class Core
+ *
+ * Copyright 2005 Openedhand Ltd.
+ *
+ * Author: Richard Purdie <[email protected]>
+ *
+ * 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/list.h>
+#include <linux/module.h>
+#include <linux/spinlock.h>
+#include <linux/leds.h>
+#include "leds.h"
+
+rwlock_t leds_list_lock = RW_LOCK_UNLOCKED;
+LIST_HEAD(leds_list);
+
+EXPORT_SYMBOL_GPL(leds_list);
#1 lore raw

This LED subsystem isn't usable with LEDs that are controlled by I2C GPIO devices. Getting rid of (struct led_device).lock would go some way to making it work. It's not clear to me why it's needed anyway.

Suspend and resume probably needs to be LED specific.

drivers/leds/Kconfig line 4

Quoted 9 lines · Richard Purdie

Index: linux-2.6.15-rc2/drivers/leds/Kconfig
===================================================================

drivers/leds/Kconfig new file +4 -0
@@ -0,0 +1,18 @@
+
+menu "LED devices"
+
+config NEW_LEDS

Is there a better name than NEW_LEDS? It won't be 'new' for very long...

include/linux/leds.h line 26

Quoted 8 lines

Index: linux-2.6.15-rc2/include/linux/leds.h
===================================================================

include/linux/leds.h new file +3 -0
[...]
+	/* A function to set the brightness of the led.
+	 * Values are between 0-100 */
+	void (*brightness_set)(struct led_device *led_dev, int value);

0-255 is probably a better range to use. Might be worth having an enum like.

enum led_brightness {
	LED_OFF = 0, LED_HALF_BRIGHT = 127, LED_FULL_BRIGHT = 255,
};

David Vrabel

#2 lore raw
Quoted 3 lines · David Vrabel

This LED subsystem isn't usable with LEDs that are controlled by I2C GPIO devices. Getting rid of (struct led_device).lock would go some way to making it work. It's not clear to me why it's needed anyway.

The lock is so no two code paths try and change led_device at once. This is particularly apparent when triggers are considered as you could otherwise end up with two calls trying to change an led to different triggers or other equally bizarre circumstances. We need to guarantee any trigger init and exit calls are made, other wise we'd have memory leaks (and worse). Locking the brightness calls is just an extension of that so the practise is fully evident.

For i2c devices to use this, the devices will have to use a workqueue for brightness changes, lock or no lock. The reason is that trigger events can come from undetermined kernel contexts and therefore the brightness changing function should not sleep.

Quoted 1 line

Suspend and resume probably needs to be LED specific.

The core functions are probably applicable in 95% of cases and any LED driver has the option of not using them if it so wishes.

Out of interest, what would an LED device wish to do instead of this? Corgi/Spitz already don't suspend one of the leds under certain circumstances as a device specific trigger (charging) is know to be suspend aware.

Quoted 6 lines

drivers/leds/Kconfig line 4

Quoted 4 lines
drivers/leds/Kconfig new file +4 -0
+
+menu "LED devices"
+
+config NEW_LEDS

Is there a better name than NEW_LEDS? It won't be 'new' for very long...

I'd prefer LEDS but this will clash with ARM. I'll wait for Russell's comments but I'm open to alternative suggestions.

Quoted 6 lines

0-255 is probably a better range to use. Might be worth having an enum like.

enum led_brightness {
	LED_OFF = 0, LED_HALF_BRIGHT = 127, LED_FULL_BRIGHT = 255,
};

Yes, that's probably not a bad idea.

Cheers,

Richard

#3 lore raw
In reply to David Vrabel

Hi!

Quoted 7 lines

This LED subsystem isn't usable with LEDs that are controlled by I2C GPIO devices. Getting rid of (struct led_device).lock would go some way to making it work. It's not clear to me why it's needed anyway.

Suspend and resume probably needs to be LED specific.

Richard Purdie wrote:

Quoted 11 lines

drivers/leds/Kconfig line 4

Quoted 9 lines

Index: linux-2.6.15-rc2/drivers/leds/Kconfig
===================================================================

drivers/leds/Kconfig new file +4 -0
@@ -0,0 +1,18 @@
+
+menu "LED devices"
+
+config NEW_LEDS

Is there a better name than NEW_LEDS? It won't be 'new' for very long...

Well, there's chance to rename ARM leds to OLD_LEDS. :-).

Quoted 11 lines

include/linux/leds.h line 26

Quoted 8 lines

Index: linux-2.6.15-rc2/include/linux/leds.h
===================================================================

include/linux/leds.h new file +3 -0
[...]
+	/* A function to set the brightness of the led.
+	 * Values are between 0-100 */
+	void (*brightness_set)(struct led_device *led_dev, int value);

0-255 is probably a better range to use. Might be worth having an enum like.

Well, don't overdo it, most LEDs are 0/1 anyway.
Pavel

Signature · 1 line
Boycott Kodak -- for their patent abuse against Java.
#4 lore raw
In reply to Richard Purdie

drivers/leds/led_class.c line 32

Quoted 9 lines · Richard Purdie
drivers/leds/led_class.c new file +9 -0
+static void leds_class_release(struct class_device *dev)
+{
+	kfree(dev);
+}
+
+static struct class leds_class = {
+	.name		= "leds",
+	.release	= leds_class_release,
+};

Instead of this, just use class_create(). Then you don't need to specify a release function at all.

Quoted 13 lines
drivers/leds/led_class.c new file +13 -0
+/**
+ * leds_device_register - register a new object of led_device class.
+ * @dev: The device to register.
+ * @led_dev: the led_device structure for this device.
+ */
+int leds_device_register(struct device *dev, struct led_device *led_dev)
+{
+	int rc;
+
+	led_dev->class_dev = kzalloc (sizeof (struct class_device), GFP_KERNEL);
+	if (unlikely (!led_dev->class_dev))
+		return -ENOMEM;
+

drivers/leds/led_class.c line 114

Quoted 10 lines
drivers/leds/led_class.c new file +10 -0
+	rwlock_init(&led_dev->lock);
+
+	led_dev->class_dev->class = &leds_class;
+	led_dev->class_dev->dev = dev;
+	led_dev->class_dev->class_data = led_dev;
+
+	/* assign this led its name */
+	strncpy(led_dev->class_dev->class_id, led_dev->name, sizeof(led_dev->class_dev->class_id));
+
+	rc = class_device_register (led_dev->class_dev);

Use class_device_create() instead, it's simpler, and will work better than creating your own class device in the future.

Quoted 29 lines
drivers/leds/led_class.c new file +29 -0
+	if (unlikely (rc)) {
+		kfree (led_dev->class_dev);
+		return rc;
+	}
+
+	/* register the attributes */
+	class_device_create_file(led_dev->class_dev, &class_device_attr_brightness);
+
+	/* add to the list of leds */
+	write_lock(&leds_list_lock);
+	list_add_tail(&led_dev->node, &leds_list);
+	write_unlock(&leds_list_lock);
+
+	printk(KERN_INFO "Registered led device: %s\n", led_dev->class_dev->class_id);
+
+	return 0;
+}
+
+/**
+ * leds_device_unregister - unregisters a object of led_properties class.
+ * @led_dev: the led device to unreigister
+ *
+ * Unregisters a previously registered via leds_device_register object.
+ */
+void leds_device_unregister(struct led_device *led_dev)
+{
+	class_device_remove_file (led_dev->class_dev, &class_device_attr_brightness);
+
+	class_device_unregister(led_dev->class_dev);

drivers/leds/led_class.c line 153

Quoted 10 lines
drivers/leds/led_class.c new file +10 -0
+
+	write_lock(&leds_list_lock);
+	list_del(&led_dev->node);
+	write_unlock(&leds_list_lock);
+}
+
+EXPORT_SYMBOL(leds_device_suspend);
+EXPORT_SYMBOL(leds_device_resume);
+EXPORT_SYMBOL(leds_device_register);
+EXPORT_SYMBOL(leds_device_unregister);

EXPORT_SYMBOL_GPL() perhaps?

drivers/leds/led_class.c line 162

Quoted 9 lines
drivers/leds/led_class.c new file +9 -0
+
+static int __init leds_init(void)
+{
+	return class_register(&leds_class);
+}
+
+static void __exit leds_exit(void)
+{
+	class_unregister(&leds_class);

class_destroy() instead, if you use class_create().

Other than these minor things, looks good to me.

thanks,

greg k-h