2 * Backlight emulation LED trigger
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/slab.h>
16 #include <linux/init.h>
18 #include <linux/leds.h>
24 struct bl_trig_notifier {
25 struct led_classdev *led;
28 struct notifier_block notifier;
32 static int fb_notifier_callback(struct notifier_block *p,
33 unsigned long event, void *data)
35 struct bl_trig_notifier *n = container_of(p,
36 struct bl_trig_notifier, notifier);
37 struct led_classdev *led = n->led;
38 struct fb_event *fb_event = data;
42 /* If we aren't interested in this event, skip it immediately ... */
43 if (event != FB_EVENT_BLANK)
46 blank = fb_event->data;
47 new_status = *blank ? BLANK : UNBLANK;
49 if (new_status == n->old_status)
52 if ((n->old_status == UNBLANK) ^ n->invert) {
53 n->brightness = led->brightness;
54 __led_set_brightness(led, LED_OFF);
56 __led_set_brightness(led, n->brightness);
59 n->old_status = new_status;
64 static ssize_t bl_trig_invert_show(struct device *dev,
65 struct device_attribute *attr, char *buf)
67 struct led_classdev *led = dev_get_drvdata(dev);
68 struct bl_trig_notifier *n = led->trigger_data;
70 return sprintf(buf, "%u\n", n->invert);
73 static ssize_t bl_trig_invert_store(struct device *dev,
74 struct device_attribute *attr, const char *buf, size_t num)
76 struct led_classdev *led = dev_get_drvdata(dev);
77 struct bl_trig_notifier *n = led->trigger_data;
81 ret = kstrtoul(buf, 10, &invert);
90 /* After inverting, we need to update the LED. */
91 if ((n->old_status == BLANK) ^ n->invert)
92 __led_set_brightness(led, LED_OFF);
94 __led_set_brightness(led, n->brightness);
98 static DEVICE_ATTR(inverted, 0644, bl_trig_invert_show, bl_trig_invert_store);
100 static void bl_trig_activate(struct led_classdev *led)
104 struct bl_trig_notifier *n;
106 n = kzalloc(sizeof(struct bl_trig_notifier), GFP_KERNEL);
107 led->trigger_data = n;
109 dev_err(led->dev, "unable to allocate backlight trigger\n");
113 ret = device_create_file(led->dev, &dev_attr_inverted);
118 n->brightness = led->brightness;
119 n->old_status = UNBLANK;
120 n->notifier.notifier_call = fb_notifier_callback;
122 ret = fb_register_client(&n->notifier);
124 dev_err(led->dev, "unable to register backlight trigger\n");
125 led->activated = true;
130 led->trigger_data = NULL;
134 static void bl_trig_deactivate(struct led_classdev *led)
136 struct bl_trig_notifier *n =
137 (struct bl_trig_notifier *) led->trigger_data;
139 if (led->activated) {
140 device_remove_file(led->dev, &dev_attr_inverted);
141 fb_unregister_client(&n->notifier);
143 led->activated = false;
147 static struct led_trigger bl_led_trigger = {
149 .activate = bl_trig_activate,
150 .deactivate = bl_trig_deactivate
153 static int __init bl_trig_init(void)
155 return led_trigger_register(&bl_led_trigger);
158 static void __exit bl_trig_exit(void)
160 led_trigger_unregister(&bl_led_trigger);
163 module_init(bl_trig_init);
164 module_exit(bl_trig_exit);
167 MODULE_DESCRIPTION("Backlight emulation LED trigger");
168 MODULE_LICENSE("GPL v2");