Skip to contentSkip to conversation

LED: Add LED Class

PATCH In review Richard Purdie 4 participants 12 messages Jan 31, 2006 – Feb 1, 2006

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

PATCH 2/11 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/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/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 +48 -0

48 lines not on this page · raw message

arch/arm/Kconfig +2 -0
Index: linux-2.6.15/arch/arm/Kconfig
===================================================================
@@ -774,6 +774,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/drivers/Makefile
===================================================================
@@ -68,6 +68,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/drivers/Kconfig
===================================================================
@@ -64,6 +64,8 @@
 
 source "drivers/mmc/Kconfig"
 
+source "drivers/leds/Kconfig"
+
 source "drivers/infiniband/Kconfig"
 
 source "drivers/sn/Kconfig"
drivers/leds/leds.h new file +26 -0
Index: linux-2.6.15/drivers/leds/leds.h
===================================================================
@@ -0,0 +1,26 @@
+/*
+ * 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.
+ *
+ */
+
+/* led_dev->lock must be held as write */
+static inline void led_set_brightness(struct led_device *led_dev, enum led_brightness value)
+{
+	if (value > LED_FULL)
+		value = LED_FULL;
+	led_dev->brightness = value;
+	if (!(led_dev->flags & LED_SUSPENDED))
+		led_dev->brightness_set(led_dev, value);
+}
+
+extern rwlock_t leds_list_lock;
+extern struct list_head leds_list;
+
drivers/leds/led-class.c new file +150 -0

150 lines not on this page · raw message

drivers/leds/led-core.c new file +24 -0
Index: linux-2.6.15/drivers/leds/led-core.c
===================================================================
@@ -0,0 +1,24 @@
+/*
+ * LED Class Core
+ *
+ * Copyright 2005-2006 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

include/linux/leds.h line 32

Quoted 13 lines · Richard Purdie

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

include/linux/leds.h new file +9 -0
+enum led_brightness {
+	LED_OFF = 0,
+	LED_HALF = 127,
+	LED_FULL = 255,
+};
+
+struct led_device {
+	/* A function to set the brightness of the led */
+	void (*brightness_set)(struct led_device *led_dev, enum led_brightness brightness);

I somehow dislike using an enum being abused for a dimmed LED value...

MfG, JBG

Signature · 4 lines
Jan-Benedict Glaw       [email protected]    . +49-172-7608481             _ O _
"Eine Freie Meinung in  einem Freien Kopf    | Gegen Zensur | Gegen Krieg  _ _ O
 für einen Freien Staat voll Freier Bürger"  | im Internet! |   im Irak!   O O O
ret = do_actions((curr | FREE_SPEECH) & ~(NEW_COPYRIGHT_LAW | DRM | TCPA));
#2 lore raw
In reply to Richard Purdie

drivers/leds/led-class.c line 88

Quoted 6 lines · Richard Purdie
drivers/leds/led-class.c new file +6 -0
+/**
+ * led_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 led_device_register(struct device *dev, struct led_device *led_dev)

Shouldn't struct led_device contain a struct device within it, like the rest of the driver model?

thanks,

greg k-h

#3 lore raw
Quoted 10 lines · Greg KH

drivers/leds/led-class.c line 88

Quoted 6 lines · Richard Purdie
drivers/leds/led-class.c new file +6 -0
+/**
+ * led_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 led_device_register(struct device *dev, struct led_device *led_dev)

Shouldn't struct led_device contain a struct device within it, like the rest of the driver model?

The code supports more than one led per struct device.

Perhaps the name is misleading and should be led_class_register? The code has changed a lot through its various development stages (it did start out as a device IIRC).

led_device should also probably be led_class by that argument...

Richard

#4 lore raw
Quoted 12 lines · Richard Purdie
Quoted 10 lines · Greg KH

drivers/leds/led-class.c line 88

Quoted 6 lines · Richard Purdie
drivers/leds/led-class.c new file +6 -0
+/**
+ * led_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 led_device_register(struct device *dev, struct led_device *led_dev)

Shouldn't struct led_device contain a struct device within it, like the rest of the driver model?

Quoted 3 lines

The code supports more than one led per struct device.

Perhaps the name is misleading and should be led_class_register?

Yes, that would make more sense, and have the struct device * be called "parent".

Quoted 4 lines

The code has changed a lot through its various development stages (it did start out as a device IIRC).

led_device should also probably be led_class by that argument...

Yes.

thanks,

greg k-h

#5 lore raw

PATCH 4/11 LED: Add LED Timer Trigger

Add an example of a complex LED trigger in the form of a generic timer which triggers the LED its attached to at a user specified frequency and duty cycle.

drivers/leds/Kconfig +7 -0
Index: linux-2.6.15/drivers/leds/Kconfig
===================================================================
@@ -22,5 +22,12 @@
 	  These triggers allow kernel events to drive the LEDs and can
 	  be configured via sysfs. If unsure, say Y.
 
+config LEDS_TRIGGER_TIMER
+	tristate "LED Timer Trigger"
+	depends LEDS_TRIGGERS
+	help
+	  This allows LEDs to be controlled by a programmable timer
+	  via sysfs. If unsure, say Y.
+
 endmenu
 
drivers/leds/Makefile +3 -0
Index: linux-2.6.15/drivers/leds/Makefile
===================================================================
@@ -3,3 +3,6 @@
 obj-$(CONFIG_NEW_LEDS)			+= led-core.o
 obj-$(CONFIG_LEDS_CLASS)		+= led-class.o
 obj-$(CONFIG_LEDS_TRIGGERS)		+= led-triggers.o
+
+# LED Triggers
+obj-$(CONFIG_LEDS_TRIGGER_TIMER)	+= ledtrig-timer.o
drivers/leds/ledtrig-timer.c new file +204 -0

204 lines not on this page · raw message

#6 lore raw

Re: [PATCH 4/11] LED: Add LED Timer Trigger

drivers/leds/ledtrig-timer.c line 8

Quoted 13 lines · Richard Purdie

Index: linux-2.6.15/drivers/leds/ledtrig-timer.c
===================================================================

drivers/leds/ledtrig-timer.c new file +8 -0
@@ -0,0 +1,204 @@
+/* led_dev write lock needs to be held */
+static void led_timer_setdata(struct led_device *led_dev, unsigned long duty, unsigned long frequency)
+{
+	struct timer_trig_data *timer_data = led_dev->trigger_data;
+	signed long duty1;
+
+	if (frequency > 500)
+		frequency = 500;

Why? ...and especially: why, without complaining?

drivers/leds/ledtrig-timer.c line 70

Quoted 2 lines
drivers/leds/ledtrig-timer.c new file +2 -0
+	if (duty > 100)
+		duty = 100;

Dito.

drivers/leds/ledtrig-timer.c line 79

Quoted 8 lines
drivers/leds/ledtrig-timer.c new file +8 -0
+	duty1 = duty - 50;
+
+	timer_data->duty = duty;
+	timer_data->frequency = frequency;
+	if (frequency != 0) {
+		timer_data->delay_on = (50 - duty1) * 1000 / 50 / frequency;
+		timer_data->delay_off = (50 + duty1) * 1000 / 50 / frequency;
+	}

Nice math :-)

drivers/leds/ledtrig-timer.c line 83

Quoted 3 lines
drivers/leds/ledtrig-timer.c new file +3 -0
+	mod_timer(&timer_data->timer, jiffies);
+}
+

MfG, JBG

Signature · 4 lines
Jan-Benedict Glaw       [email protected]    . +49-172-7608481             _ O _
"Eine Freie Meinung in  einem Freien Kopf    | Gegen Zensur | Gegen Krieg  _ _ O
 für einen Freien Staat voll Freier Bürger"  | im Internet! |   im Irak!   O O O
ret = do_actions((curr | FREE_SPEECH) & ~(NEW_COPYRIGHT_LAW | DRM | TCPA));
#7 lore raw

Re: [PATCH 4/11] LED: Add LED Timer Trigger

Quoted 10 lines · Jan-Benedict Glaw

drivers/leds/ledtrig-timer.c line 67

Quoted 7 lines · Richard Purdie
drivers/leds/ledtrig-timer.c new file +7 -0
+static void led_timer_setdata(struct led_device *led_dev, unsigned long duty, unsigned long frequency)
+{
+	struct timer_trig_data *timer_data = led_dev->trigger_data;
+	signed long duty1;
+
+	if (frequency > 500)
+		frequency = 500;

Why?

We're dealing with msec delays. Any frequency > 1000 will just cause problems. There was a reason for using half that but it escapes me and might be unneeded now. 500Hz/1000Hz is above the frequency the human eye can see so is unlikely to present a problem.

Quoted 1 line

...and especially: why, without complaining?

This is the important bit. It should return an -EINVAL back to userspace.

Quoted 4 lines

drivers/leds/ledtrig-timer.c line 70

Quoted 2 lines
drivers/leds/ledtrig-timer.c new file +2 -0
+	if (duty > 100)
+		duty = 100;

Dito.

Duty cycles > 100 make no sense and would break the subsequent calculation. Same problem/solution as above.

Thanks,

Richard

#8 lore raw

cover LED Class, Triggers and Drivers

This is an updated version of the LED class/subsystem I proposed a while ago. It takes John Lenz's work and extends and alters it to give what I think should be a fairly universal LED implementation.

Hopefully a decision on whether this is going to head into mainline or not can be made soon. I've not had any feedback from Russell on this issue (but have asked).

I'm unsure what the kconfig name for the class should be (something better than NEW_LEDS is needed but arm is using LEDS).

The series consists of several logical units:

* LED Core + Class implementation
* LED Trigger Core implementation
* LED timer trigger (example of a complex trigger)
* LED device drivers for corgi, spitz and tosa Zaurus models
* LED device driver for locomo LEDs
* LED device driver for ARM ixp4xx LEDs
* Zaurus charging LED trigger
* IDE disk activity LED trigger
* NAND MTD activity LED trigger

Changes
=======

The patches have been updated after the feedback from GregKH and others on LKML and other feedback elsewhere. These include:

* using class_create()/class_destroy()/class_device_create()
* updates to the timer trigger so it accepts a duty parameter
* addition of led drivers for tosa and ixp4xx
* using an enum for led brightness
* correct the ide trigger when ide_disk isn't compiled
* added some brief documentation

Why?
====

LEDs are really simple devices usually amounting to a GPIO that can be turned on and off so why do we need all this code? On handheld or embedded devices they're an important part of an often limited user interface. Both users and developers want to be able to control and configure what the LED does and the number of different things they'd potentially want the LED to show is large.

A subsystem is needed to try and provide all this different functionality in an architecture independent, simple but complete, generic and scalable manner.

The alternative is for everyone to implement just what they need hidden away in different corners of the kernel source tree and to provide an inconsistent interface to userspace.

Other Implementations
=====================

I'm aware of the existing arm led implementation. Currently the new subsystem and the arm code can coexist quite happily. Its up to the arm community to decide whether this new interface is acceptable to them. As far as I can see, the new interface can do everything the existing arm implementation can with the advantage that the new code is architecture independent and much more generic configurable and scalable.

I'm prepared to make the conversion to the LED subsystem (or assist with it) if appropriate.

Implementation Details
======================

I've stripped a lot of code out of John's original LED class. Colours were removed as LED colour is now part of the device name. Multiple colours are to be handled as multiple led devices. This means you get full control over each colour. I also removed the LED hardware timer code as the generic timer isn't going to add much overhead and is just as useful. I also decided to have the LED core track the current LED status (to ease suspend/resume handling) removing the need for brightness_get implementations in the LED drivers.

An underlying design philosophy is simplicity. The aim is to keep a small amount of code giving as much functionality as possible.

The major new idea is the led "trigger". A trigger is a source of led events. Triggers can either be simple or complex. A simple trigger isn't configurable and is designed to slot into existing subsystems with minimal additional code. Examples are the ide-disk, nand-disk and zaurus-charging triggers. With leds disabled, the code optimises away. Examples are nand-disk and ide-disk.

Complex triggers whilst available to all LEDs have LED specific parameters and work on a per LED basis. The timer trigger is an example.

You can change triggers in a similar manner to the way an IO scheduler is chosen (via /sys/class/leds/somedevice/trigger).

So far there are only a handful of examples but it should easy to add further LED triggers without too much interference into other subsystems.

Known Issues
============

The LED Trigger core cannot be a module as the simple trigger functions would cause nightmare dependency issues. I see this as a minor issue compared to the benefits the simple trigger functionality brings. The rest of the LED subsystem can be modular.

Some leds can be programmed to flash in hardware. As this isn't a generic LED device property, I think this should be exported as a device specific sysfs attribute rather than part of the class if this functionality is required (eg. to keep the led flashing whilst the device is suspended).

Future Development
==================

At the moment, a trigger can't be created specifically for a single LED. There are a number of cases where a trigger might only be mappable to a particular LED. The addition of triggers provided by the LED driver should cover this option and be possible to add without breaking the current interface.

A cpu activity trigger similar to that found in the arm led implementation should be trivial to add.

Richard

#9 lore raw

Re: [PATCH 0/11] LED Class, Triggers and Drivers

Quoted 3 lines · Richard Purdie

This is an updated version of the LED class/subsystem I proposed a while ago. It takes John Lenz's work and extends and alters it to give what I think should be a fairly universal LED implementation.

I'd abandon my own LED implementation I wrote for the status LEDs of some VAXen, though it cannot substitute it fully (eg. there are some 7-segment displays using bitmasks _or_ 0x00..0x0f for the bars) once some small tweaks are done (which I just sent with the other emails.)

MfG, JBG

Signature · 4 lines
Jan-Benedict Glaw       [email protected]    . +49-172-7608481             _ O _
"Eine Freie Meinung in  einem Freien Kopf    | Gegen Zensur | Gegen Krieg  _ _ O
 für einen Freien Staat voll Freier Bürger"  | im Internet! |   im Irak!   O O O
ret = do_actions((curr | FREE_SPEECH) & ~(NEW_COPYRIGHT_LAW | DRM | TCPA));
#10 lore raw

Re: [PATCH 0/11] LED Class, Triggers and Drivers

In reply to Richard Purdie
Quoted 3 lines · Richard Purdie

Future Development
==================

A MMC trigger in the pipeline? My new SDHCI MMC driver can be built as a LED device, but it should probably be mapped to MMC activity by default.

Rgds
Pierre

#11 lore raw

Re: [PATCH 0/11] LED Class, Triggers and Drivers

Quoted 3 lines · Pierre Ossman

A MMC trigger in the pipeline? My new SDHCI MMC driver can be built as a LED device, but it should probably be mapped to MMC activity by default.

Not specifically. I suspect about 5 lines of code in mmc_block.c would be enough though. Patches welcome :)

Richard