backlight: max8925_bl: Fix Device Tree node lookup
[linux/fpc-iii.git] / drivers / leds / led-triggers.c
blob9e801d6dda22bbe9079714809c92c9840bfe7225
1 /*
2 * LED Triggers Core
4 * Copyright 2005-2007 Openedhand Ltd.
6 * Author: Richard Purdie <rpurdie@openedhand.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/list.h>
17 #include <linux/spinlock.h>
18 #include <linux/device.h>
19 #include <linux/timer.h>
20 #include <linux/rwsem.h>
21 #include <linux/leds.h>
22 #include <linux/slab.h>
23 #include "leds.h"
26 * Nests outside led_cdev->trigger_lock
28 static DECLARE_RWSEM(triggers_list_lock);
29 static LIST_HEAD(trigger_list);
31 /* Used by LED Class */
33 ssize_t led_trigger_store(struct device *dev, struct device_attribute *attr,
34 const char *buf, size_t count)
36 struct led_classdev *led_cdev = dev_get_drvdata(dev);
37 char trigger_name[TRIG_NAME_MAX];
38 struct led_trigger *trig;
39 size_t len;
41 trigger_name[sizeof(trigger_name) - 1] = '\0';
42 strncpy(trigger_name, buf, sizeof(trigger_name) - 1);
43 len = strlen(trigger_name);
45 if (len && trigger_name[len - 1] == '\n')
46 trigger_name[len - 1] = '\0';
48 if (!strcmp(trigger_name, "none")) {
49 led_trigger_remove(led_cdev);
50 return count;
53 down_read(&triggers_list_lock);
54 list_for_each_entry(trig, &trigger_list, next_trig) {
55 if (!strcmp(trigger_name, trig->name)) {
56 down_write(&led_cdev->trigger_lock);
57 led_trigger_set(led_cdev, trig);
58 up_write(&led_cdev->trigger_lock);
60 up_read(&triggers_list_lock);
61 return count;
64 up_read(&triggers_list_lock);
66 return -EINVAL;
68 EXPORT_SYMBOL_GPL(led_trigger_store);
70 ssize_t led_trigger_show(struct device *dev, struct device_attribute *attr,
71 char *buf)
73 struct led_classdev *led_cdev = dev_get_drvdata(dev);
74 struct led_trigger *trig;
75 int len = 0;
77 down_read(&triggers_list_lock);
78 down_read(&led_cdev->trigger_lock);
80 if (!led_cdev->trigger)
81 len += scnprintf(buf+len, PAGE_SIZE - len, "[none] ");
82 else
83 len += scnprintf(buf+len, PAGE_SIZE - len, "none ");
85 list_for_each_entry(trig, &trigger_list, next_trig) {
86 if (led_cdev->trigger && !strcmp(led_cdev->trigger->name,
87 trig->name))
88 len += scnprintf(buf+len, PAGE_SIZE - len, "[%s] ",
89 trig->name);
90 else
91 len += scnprintf(buf+len, PAGE_SIZE - len, "%s ",
92 trig->name);
94 up_read(&led_cdev->trigger_lock);
95 up_read(&triggers_list_lock);
97 len += scnprintf(len+buf, PAGE_SIZE - len, "\n");
98 return len;
100 EXPORT_SYMBOL_GPL(led_trigger_show);
102 /* Caller must ensure led_cdev->trigger_lock held */
103 void led_trigger_set(struct led_classdev *led_cdev, struct led_trigger *trig)
105 unsigned long flags;
106 char *event = NULL;
107 char *envp[2];
108 const char *name;
110 name = trig ? trig->name : "none";
111 event = kasprintf(GFP_KERNEL, "TRIGGER=%s", name);
113 /* Remove any existing trigger */
114 if (led_cdev->trigger) {
115 write_lock_irqsave(&led_cdev->trigger->leddev_list_lock, flags);
116 list_del(&led_cdev->trig_list);
117 write_unlock_irqrestore(&led_cdev->trigger->leddev_list_lock,
118 flags);
119 cancel_work_sync(&led_cdev->set_brightness_work);
120 led_stop_software_blink(led_cdev);
121 if (led_cdev->trigger->deactivate)
122 led_cdev->trigger->deactivate(led_cdev);
123 led_cdev->trigger = NULL;
124 led_set_brightness(led_cdev, LED_OFF);
126 if (trig) {
127 write_lock_irqsave(&trig->leddev_list_lock, flags);
128 list_add_tail(&led_cdev->trig_list, &trig->led_cdevs);
129 write_unlock_irqrestore(&trig->leddev_list_lock, flags);
130 led_cdev->trigger = trig;
131 if (trig->activate)
132 trig->activate(led_cdev);
135 if (event) {
136 envp[0] = event;
137 envp[1] = NULL;
138 kobject_uevent_env(&led_cdev->dev->kobj, KOBJ_CHANGE, envp);
139 kfree(event);
142 EXPORT_SYMBOL_GPL(led_trigger_set);
144 void led_trigger_remove(struct led_classdev *led_cdev)
146 down_write(&led_cdev->trigger_lock);
147 led_trigger_set(led_cdev, NULL);
148 up_write(&led_cdev->trigger_lock);
150 EXPORT_SYMBOL_GPL(led_trigger_remove);
152 void led_trigger_set_default(struct led_classdev *led_cdev)
154 struct led_trigger *trig;
156 if (!led_cdev->default_trigger)
157 return;
159 down_read(&triggers_list_lock);
160 down_write(&led_cdev->trigger_lock);
161 list_for_each_entry(trig, &trigger_list, next_trig) {
162 if (!strcmp(led_cdev->default_trigger, trig->name))
163 led_trigger_set(led_cdev, trig);
165 up_write(&led_cdev->trigger_lock);
166 up_read(&triggers_list_lock);
168 EXPORT_SYMBOL_GPL(led_trigger_set_default);
170 void led_trigger_rename_static(const char *name, struct led_trigger *trig)
172 /* new name must be on a temporary string to prevent races */
173 BUG_ON(name == trig->name);
175 down_write(&triggers_list_lock);
176 /* this assumes that trig->name was originaly allocated to
177 * non constant storage */
178 strcpy((char *)trig->name, name);
179 up_write(&triggers_list_lock);
181 EXPORT_SYMBOL_GPL(led_trigger_rename_static);
183 /* LED Trigger Interface */
185 int led_trigger_register(struct led_trigger *trig)
187 struct led_classdev *led_cdev;
188 struct led_trigger *_trig;
190 rwlock_init(&trig->leddev_list_lock);
191 INIT_LIST_HEAD(&trig->led_cdevs);
193 down_write(&triggers_list_lock);
194 /* Make sure the trigger's name isn't already in use */
195 list_for_each_entry(_trig, &trigger_list, next_trig) {
196 if (!strcmp(_trig->name, trig->name)) {
197 up_write(&triggers_list_lock);
198 return -EEXIST;
201 /* Add to the list of led triggers */
202 list_add_tail(&trig->next_trig, &trigger_list);
203 up_write(&triggers_list_lock);
205 /* Register with any LEDs that have this as a default trigger */
206 down_read(&leds_list_lock);
207 list_for_each_entry(led_cdev, &leds_list, node) {
208 down_write(&led_cdev->trigger_lock);
209 if (!led_cdev->trigger && led_cdev->default_trigger &&
210 !strcmp(led_cdev->default_trigger, trig->name))
211 led_trigger_set(led_cdev, trig);
212 up_write(&led_cdev->trigger_lock);
214 up_read(&leds_list_lock);
216 return 0;
218 EXPORT_SYMBOL_GPL(led_trigger_register);
220 void led_trigger_unregister(struct led_trigger *trig)
222 struct led_classdev *led_cdev;
224 if (list_empty_careful(&trig->next_trig))
225 return;
227 /* Remove from the list of led triggers */
228 down_write(&triggers_list_lock);
229 list_del_init(&trig->next_trig);
230 up_write(&triggers_list_lock);
232 /* Remove anyone actively using this trigger */
233 down_read(&leds_list_lock);
234 list_for_each_entry(led_cdev, &leds_list, node) {
235 down_write(&led_cdev->trigger_lock);
236 if (led_cdev->trigger == trig)
237 led_trigger_set(led_cdev, NULL);
238 up_write(&led_cdev->trigger_lock);
240 up_read(&leds_list_lock);
242 EXPORT_SYMBOL_GPL(led_trigger_unregister);
244 /* Simple LED Tigger Interface */
246 void led_trigger_event(struct led_trigger *trig,
247 enum led_brightness brightness)
249 struct led_classdev *led_cdev;
251 if (!trig)
252 return;
254 read_lock(&trig->leddev_list_lock);
255 list_for_each_entry(led_cdev, &trig->led_cdevs, trig_list)
256 led_set_brightness(led_cdev, brightness);
257 read_unlock(&trig->leddev_list_lock);
259 EXPORT_SYMBOL_GPL(led_trigger_event);
261 static void led_trigger_blink_setup(struct led_trigger *trig,
262 unsigned long *delay_on,
263 unsigned long *delay_off,
264 int oneshot,
265 int invert)
267 struct led_classdev *led_cdev;
269 if (!trig)
270 return;
272 read_lock(&trig->leddev_list_lock);
273 list_for_each_entry(led_cdev, &trig->led_cdevs, trig_list) {
274 if (oneshot)
275 led_blink_set_oneshot(led_cdev, delay_on, delay_off,
276 invert);
277 else
278 led_blink_set(led_cdev, delay_on, delay_off);
280 read_unlock(&trig->leddev_list_lock);
283 void led_trigger_blink(struct led_trigger *trig,
284 unsigned long *delay_on,
285 unsigned long *delay_off)
287 led_trigger_blink_setup(trig, delay_on, delay_off, 0, 0);
289 EXPORT_SYMBOL_GPL(led_trigger_blink);
291 void led_trigger_blink_oneshot(struct led_trigger *trig,
292 unsigned long *delay_on,
293 unsigned long *delay_off,
294 int invert)
296 led_trigger_blink_setup(trig, delay_on, delay_off, 1, invert);
298 EXPORT_SYMBOL_GPL(led_trigger_blink_oneshot);
300 void led_trigger_register_simple(const char *name, struct led_trigger **tp)
302 struct led_trigger *trig;
303 int err;
305 trig = kzalloc(sizeof(struct led_trigger), GFP_KERNEL);
307 if (trig) {
308 trig->name = name;
309 err = led_trigger_register(trig);
310 if (err < 0) {
311 kfree(trig);
312 trig = NULL;
313 pr_warn("LED trigger %s failed to register (%d)\n",
314 name, err);
316 } else {
317 pr_warn("LED trigger %s failed to register (no memory)\n",
318 name);
320 *tp = trig;
322 EXPORT_SYMBOL_GPL(led_trigger_register_simple);
324 void led_trigger_unregister_simple(struct led_trigger *trig)
326 if (trig)
327 led_trigger_unregister(trig);
328 kfree(trig);
330 EXPORT_SYMBOL_GPL(led_trigger_unregister_simple);
332 MODULE_AUTHOR("Richard Purdie");
333 MODULE_LICENSE("GPL");
334 MODULE_DESCRIPTION("LED Triggers Core");