Skip to content

Commit 5ada28b

Browse files
jmberg-inteltorvalds
authored andcommitted
led-class: always implement blinking
Currently, blinking LEDs can be awkward because it is not guaranteed that all LEDs implement blinking. The trigger that wants it to blink then needs to implement its own timer solution. Rather than require that, add led_blink_set() API that triggers can use. This function will attempt to use hw blinking, but if that fails implements a timer for it. To stop blinking again, brightness_set() also needs to be wrapped into API that will stop the software blink. As a result of this, the timer trigger becomes a very trivial one, and hopefully we can finally see triggers using blinking as well because it's always easy to use. Signed-off-by: Johannes Berg <johannes.berg@intel.com> Acked-by: Richard Purdie <rpurdie@linux.intel.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 52ca0e8 commit 5ada28b

7 files changed

Lines changed: 171 additions & 133 deletions

File tree

‎Documentation/leds-class.txt‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,18 @@ Hardware accelerated blink of LEDs
6060

6161
Some LEDs can be programmed to blink without any CPU interaction. To
6262
support this feature, a LED driver can optionally implement the
63-
blink_set() function (see <linux/leds.h>). If implemented, triggers can
64-
attempt to use it before falling back to software timers. The blink_set()
65-
function should return 0 if the blink setting is supported, or -EINVAL
66-
otherwise, which means that LED blinking will be handled by software.
67-
68-
The blink_set() function should choose a user friendly blinking
69-
value if it is called with *delay_on==0 && *delay_off==0 parameters. In
70-
this case the driver should give back the chosen value through delay_on
71-
and delay_off parameters to the leds subsystem.
63+
blink_set() function (see <linux/leds.h>). To set an LED to blinking,
64+
however, it is better to use use the API function led_blink_set(),
65+
as it will check and implement software fallback if necessary.
66+
67+
To turn off blinking again, use the API function led_brightness_set()
68+
as that will not just set the LED brightness but also stop any software
69+
timers that may have been required for blinking.
70+
71+
The blink_set() function should choose a user friendly blinking value
72+
if it is called with *delay_on==0 && *delay_off==0 parameters. In this
73+
case the driver should give back the chosen value through delay_on and
74+
delay_off parameters to the leds subsystem.
7275

7376
Setting the brightness to zero with brightness_set() callback function
7477
should completely turn off the LED and cancel the previously programmed

‎drivers/leds/Kconfig‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ menuconfig NEW_LEDS
1010
if NEW_LEDS
1111

1212
config LEDS_CLASS
13-
tristate "LED Class Support"
13+
bool "LED Class Support"
1414
help
1515
This option enables the led sysfs class in /sys/class/leds. You'll
1616
need this to do anything useful with LEDs. If unsure, say N.

‎drivers/leds/led-class.c‎

Lines changed: 104 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,79 @@ static struct device_attribute led_class_attrs[] = {
8181
__ATTR_NULL,
8282
};
8383

84+
static void led_timer_function(unsigned long data)
85+
{
86+
struct led_classdev *led_cdev = (void *)data;
87+
unsigned long brightness;
88+
unsigned long delay;
89+
90+
if (!led_cdev->blink_delay_on || !led_cdev->blink_delay_off) {
91+
led_set_brightness(led_cdev, LED_OFF);
92+
return;
93+
}
94+
95+
brightness = led_get_brightness(led_cdev);
96+
if (!brightness) {
97+
/* Time to switch the LED on. */
98+
brightness = led_cdev->blink_brightness;
99+
delay = led_cdev->blink_delay_on;
100+
} else {
101+
/* Store the current brightness value to be able
102+
* to restore it when the delay_off period is over.
103+
*/
104+
led_cdev->blink_brightness = brightness;
105+
brightness = LED_OFF;
106+
delay = led_cdev->blink_delay_off;
107+
}
108+
109+
led_set_brightness(led_cdev, brightness);
110+
111+
mod_timer(&led_cdev->blink_timer, jiffies + msecs_to_jiffies(delay));
112+
}
113+
114+
static void led_stop_software_blink(struct led_classdev *led_cdev)
115+
{
116+
/* deactivate previous settings */
117+
del_timer_sync(&led_cdev->blink_timer);
118+
led_cdev->blink_delay_on = 0;
119+
led_cdev->blink_delay_off = 0;
120+
}
121+
122+
static void led_set_software_blink(struct led_classdev *led_cdev,
123+
unsigned long delay_on,
124+
unsigned long delay_off)
125+
{
126+
int current_brightness;
127+
128+
current_brightness = led_get_brightness(led_cdev);
129+
if (current_brightness)
130+
led_cdev->blink_brightness = current_brightness;
131+
if (!led_cdev->blink_brightness)
132+
led_cdev->blink_brightness = led_cdev->max_brightness;
133+
134+
if (delay_on == led_cdev->blink_delay_on &&
135+
delay_off == led_cdev->blink_delay_off)
136+
return;
137+
138+
led_stop_software_blink(led_cdev);
139+
140+
led_cdev->blink_delay_on = delay_on;
141+
led_cdev->blink_delay_off = delay_off;
142+
143+
/* never on - don't blink */
144+
if (!delay_on)
145+
return;
146+
147+
/* never off - just set to brightness */
148+
if (!delay_off) {
149+
led_set_brightness(led_cdev, led_cdev->blink_brightness);
150+
return;
151+
}
152+
153+
mod_timer(&led_cdev->blink_timer, jiffies + 1);
154+
}
155+
156+
84157
/**
85158
* led_classdev_suspend - suspend an led_classdev.
86159
* @led_cdev: the led_classdev to suspend.
@@ -148,6 +221,10 @@ int led_classdev_register(struct device *parent, struct led_classdev *led_cdev)
148221

149222
led_update_brightness(led_cdev);
150223

224+
init_timer(&led_cdev->blink_timer);
225+
led_cdev->blink_timer.function = led_timer_function;
226+
led_cdev->blink_timer.data = (unsigned long)led_cdev;
227+
151228
#ifdef CONFIG_LEDS_TRIGGERS
152229
led_trigger_set_default(led_cdev);
153230
#endif
@@ -157,7 +234,6 @@ int led_classdev_register(struct device *parent, struct led_classdev *led_cdev)
157234

158235
return 0;
159236
}
160-
161237
EXPORT_SYMBOL_GPL(led_classdev_register);
162238

163239
/**
@@ -175,6 +251,9 @@ void led_classdev_unregister(struct led_classdev *led_cdev)
175251
up_write(&led_cdev->trigger_lock);
176252
#endif
177253

254+
/* Stop blinking */
255+
led_brightness_set(led_cdev, LED_OFF);
256+
178257
device_unregister(led_cdev->dev);
179258

180259
down_write(&leds_list_lock);
@@ -183,6 +262,30 @@ void led_classdev_unregister(struct led_classdev *led_cdev)
183262
}
184263
EXPORT_SYMBOL_GPL(led_classdev_unregister);
185264

265+
void led_blink_set(struct led_classdev *led_cdev,
266+
unsigned long *delay_on,
267+
unsigned long *delay_off)
268+
{
269+
if (led_cdev->blink_set &&
270+
led_cdev->blink_set(led_cdev, delay_on, delay_off))
271+
return;
272+
273+
/* blink with 1 Hz as default if nothing specified */
274+
if (!*delay_on && !*delay_off)
275+
*delay_on = *delay_off = 500;
276+
277+
led_set_software_blink(led_cdev, *delay_on, *delay_off);
278+
}
279+
EXPORT_SYMBOL(led_blink_set);
280+
281+
void led_brightness_set(struct led_classdev *led_cdev,
282+
enum led_brightness brightness)
283+
{
284+
led_stop_software_blink(led_cdev);
285+
led_cdev->brightness_set(led_cdev, brightness);
286+
}
287+
EXPORT_SYMBOL(led_brightness_set);
288+
186289
static int __init leds_init(void)
187290
{
188291
leds_class = class_create(THIS_MODULE, "leds");

‎drivers/leds/led-triggers.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ void led_trigger_set(struct led_classdev *led_cdev, struct led_trigger *trigger)
113113
if (led_cdev->trigger->deactivate)
114114
led_cdev->trigger->deactivate(led_cdev);
115115
led_cdev->trigger = NULL;
116-
led_set_brightness(led_cdev, LED_OFF);
116+
led_brightness_set(led_cdev, LED_OFF);
117117
}
118118
if (trigger) {
119119
write_lock_irqsave(&trigger->leddev_list_lock, flags);

‎drivers/leds/ledtrig-timer.c‎

Lines changed: 10 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -12,73 +12,25 @@
1212
*/
1313

1414
#include <linux/module.h>
15-
#include <linux/jiffies.h>
1615
#include <linux/kernel.h>
1716
#include <linux/init.h>
18-
#include <linux/list.h>
19-
#include <linux/spinlock.h>
2017
#include <linux/device.h>
21-
#include <linux/sysdev.h>
22-
#include <linux/timer.h>
2318
#include <linux/ctype.h>
2419
#include <linux/leds.h>
25-
#include <linux/slab.h>
2620
#include "leds.h"
2721

28-
struct timer_trig_data {
29-
int brightness_on; /* LED brightness during "on" period.
30-
* (LED_OFF < brightness_on <= LED_FULL)
31-
*/
32-
unsigned long delay_on; /* milliseconds on */
33-
unsigned long delay_off; /* milliseconds off */
34-
struct timer_list timer;
35-
};
36-
37-
static void led_timer_function(unsigned long data)
38-
{
39-
struct led_classdev *led_cdev = (struct led_classdev *) data;
40-
struct timer_trig_data *timer_data = led_cdev->trigger_data;
41-
unsigned long brightness;
42-
unsigned long delay;
43-
44-
if (!timer_data->delay_on || !timer_data->delay_off) {
45-
led_set_brightness(led_cdev, LED_OFF);
46-
return;
47-
}
48-
49-
brightness = led_get_brightness(led_cdev);
50-
if (!brightness) {
51-
/* Time to switch the LED on. */
52-
brightness = timer_data->brightness_on;
53-
delay = timer_data->delay_on;
54-
} else {
55-
/* Store the current brightness value to be able
56-
* to restore it when the delay_off period is over.
57-
*/
58-
timer_data->brightness_on = brightness;
59-
brightness = LED_OFF;
60-
delay = timer_data->delay_off;
61-
}
62-
63-
led_set_brightness(led_cdev, brightness);
64-
65-
mod_timer(&timer_data->timer, jiffies + msecs_to_jiffies(delay));
66-
}
67-
6822
static ssize_t led_delay_on_show(struct device *dev,
6923
struct device_attribute *attr, char *buf)
7024
{
7125
struct led_classdev *led_cdev = dev_get_drvdata(dev);
72-
struct timer_trig_data *timer_data = led_cdev->trigger_data;
7326

74-
return sprintf(buf, "%lu\n", timer_data->delay_on);
27+
return sprintf(buf, "%lu\n", led_cdev->blink_delay_on);
7528
}
7629

7730
static ssize_t led_delay_on_store(struct device *dev,
7831
struct device_attribute *attr, const char *buf, size_t size)
7932
{
8033
struct led_classdev *led_cdev = dev_get_drvdata(dev);
81-
struct timer_trig_data *timer_data = led_cdev->trigger_data;
8234
int ret = -EINVAL;
8335
char *after;
8436
unsigned long state = simple_strtoul(buf, &after, 10);
@@ -88,21 +40,7 @@ static ssize_t led_delay_on_store(struct device *dev,
8840
count++;
8941

9042
if (count == size) {
91-
if (timer_data->delay_on != state) {
92-
/* the new value differs from the previous */
93-
timer_data->delay_on = state;
94-
95-
/* deactivate previous settings */
96-
del_timer_sync(&timer_data->timer);
97-
98-
/* try to activate hardware acceleration, if any */
99-
if (!led_cdev->blink_set ||
100-
led_cdev->blink_set(led_cdev,
101-
&timer_data->delay_on, &timer_data->delay_off)) {
102-
/* no hardware acceleration, blink via timer */
103-
mod_timer(&timer_data->timer, jiffies + 1);
104-
}
105-
}
43+
led_blink_set(led_cdev, &state, &led_cdev->blink_delay_off);
10644
ret = count;
10745
}
10846

@@ -113,16 +51,14 @@ static ssize_t led_delay_off_show(struct device *dev,
11351
struct device_attribute *attr, char *buf)
11452
{
11553
struct led_classdev *led_cdev = dev_get_drvdata(dev);
116-
struct timer_trig_data *timer_data = led_cdev->trigger_data;
11754

118-
return sprintf(buf, "%lu\n", timer_data->delay_off);
55+
return sprintf(buf, "%lu\n", led_cdev->blink_delay_off);
11956
}
12057

12158
static ssize_t led_delay_off_store(struct device *dev,
12259
struct device_attribute *attr, const char *buf, size_t size)
12360
{
12461
struct led_classdev *led_cdev = dev_get_drvdata(dev);
125-
struct timer_trig_data *timer_data = led_cdev->trigger_data;
12662
int ret = -EINVAL;
12763
char *after;
12864
unsigned long state = simple_strtoul(buf, &after, 10);
@@ -132,21 +68,7 @@ static ssize_t led_delay_off_store(struct device *dev,
13268
count++;
13369

13470
if (count == size) {
135-
if (timer_data->delay_off != state) {
136-
/* the new value differs from the previous */
137-
timer_data->delay_off = state;
138-
139-
/* deactivate previous settings */
140-
del_timer_sync(&timer_data->timer);
141-
142-
/* try to activate hardware acceleration, if any */
143-
if (!led_cdev->blink_set ||
144-
led_cdev->blink_set(led_cdev,
145-
&timer_data->delay_on, &timer_data->delay_off)) {
146-
/* no hardware acceleration, blink via timer */
147-
mod_timer(&timer_data->timer, jiffies + 1);
148-
}
149-
}
71+
led_blink_set(led_cdev, &led_cdev->blink_delay_on, &state);
15072
ret = count;
15173
}
15274

@@ -158,60 +80,34 @@ static DEVICE_ATTR(delay_off, 0644, led_delay_off_show, led_delay_off_store);
15880

15981
static void timer_trig_activate(struct led_classdev *led_cdev)
16082
{
161-
struct timer_trig_data *timer_data;
16283
int rc;
16384

164-
timer_data = kzalloc(sizeof(struct timer_trig_data), GFP_KERNEL);
165-
if (!timer_data)
166-
return;
167-
168-
timer_data->brightness_on = led_get_brightness(led_cdev);
169-
if (timer_data->brightness_on == LED_OFF)
170-
timer_data->brightness_on = led_cdev->max_brightness;
171-
led_cdev->trigger_data = timer_data;
172-
173-
init_timer(&timer_data->timer);
174-
timer_data->timer.function = led_timer_function;
175-
timer_data->timer.data = (unsigned long) led_cdev;
85+
led_cdev->trigger_data = NULL;
17686

17787
rc = device_create_file(led_cdev->dev, &dev_attr_delay_on);
17888
if (rc)
179-
goto err_out;
89+
return;
18090
rc = device_create_file(led_cdev->dev, &dev_attr_delay_off);
18191
if (rc)
18292
goto err_out_delayon;
18393

184-
/* If there is hardware support for blinking, start one
185-
* user friendly blink rate chosen by the driver.
186-
*/
187-
if (led_cdev->blink_set)
188-
led_cdev->blink_set(led_cdev,
189-
&timer_data->delay_on, &timer_data->delay_off);
94+
led_cdev->trigger_data = (void *)1;
19095

19196
return;
19297

19398
err_out_delayon:
19499
device_remove_file(led_cdev->dev, &dev_attr_delay_on);
195-
err_out:
196-
led_cdev->trigger_data = NULL;
197-
kfree(timer_data);
198100
}
199101

200102
static void timer_trig_deactivate(struct led_classdev *led_cdev)
201103
{
202-
struct timer_trig_data *timer_data = led_cdev->trigger_data;
203-
unsigned long on = 0, off = 0;
204-
205-
if (timer_data) {
104+
if (led_cdev->trigger_data) {
206105
device_remove_file(led_cdev->dev, &dev_attr_delay_on);
207106
device_remove_file(led_cdev->dev, &dev_attr_delay_off);
208-
del_timer_sync(&timer_data->timer);
209-
kfree(timer_data);
210107
}
211108

212-
/* If there is hardware support for blinking, stop it */
213-
if (led_cdev->blink_set)
214-
led_cdev->blink_set(led_cdev, &on, &off);
109+
/* Stop blinking */
110+
led_brightness_set(led_cdev, LED_OFF);
215111
}
216112

217113
static struct led_trigger timer_led_trigger = {

0 commit comments

Comments
 (0)