powerpc/fadump: Do not allow hot-remove memory from fadump reserved area.
[linux/fpc-iii.git] / drivers / leds / trigger / ledtrig-backlight.c
blobc2b57beef718a3ed7931f51a6371cddb15ed77d4
1 /*
2 * Backlight emulation LED trigger
4 * Copyright 2008 (C) Rodolfo Giometti <giometti@linux.it>
5 * Copyright 2008 (C) Eurotech S.p.A. <info@eurotech.it>
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>
17 #include <linux/fb.h>
18 #include <linux/leds.h>
19 #include "../leds.h"
21 #define BLANK 1
22 #define UNBLANK 0
24 struct bl_trig_notifier {
25 struct led_classdev *led;
26 int brightness;
27 int old_status;
28 struct notifier_block notifier;
29 unsigned invert;
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;
39 int *blank;
40 int new_status;
42 /* If we aren't interested in this event, skip it immediately ... */
43 if (event != FB_EVENT_BLANK)
44 return 0;
46 blank = fb_event->data;
47 new_status = *blank ? BLANK : UNBLANK;
49 if (new_status == n->old_status)
50 return 0;
52 if ((n->old_status == UNBLANK) ^ n->invert) {
53 n->brightness = led->brightness;
54 led_set_brightness_nosleep(led, LED_OFF);
55 } else {
56 led_set_brightness_nosleep(led, n->brightness);
59 n->old_status = new_status;
61 return 0;
64 static ssize_t bl_trig_invert_show(struct device *dev,
65 struct device_attribute *attr, char *buf)
67 struct bl_trig_notifier *n = led_trigger_get_drvdata(dev);
69 return sprintf(buf, "%u\n", n->invert);
72 static ssize_t bl_trig_invert_store(struct device *dev,
73 struct device_attribute *attr, const char *buf, size_t num)
75 struct led_classdev *led = led_trigger_get_led(dev);
76 struct bl_trig_notifier *n = led_trigger_get_drvdata(dev);
77 unsigned long invert;
78 int ret;
80 ret = kstrtoul(buf, 10, &invert);
81 if (ret < 0)
82 return ret;
84 if (invert > 1)
85 return -EINVAL;
87 n->invert = invert;
89 /* After inverting, we need to update the LED. */
90 if ((n->old_status == BLANK) ^ n->invert)
91 led_set_brightness_nosleep(led, LED_OFF);
92 else
93 led_set_brightness_nosleep(led, n->brightness);
95 return num;
97 static DEVICE_ATTR(inverted, 0644, bl_trig_invert_show, bl_trig_invert_store);
99 static struct attribute *bl_trig_attrs[] = {
100 &dev_attr_inverted.attr,
101 NULL,
103 ATTRIBUTE_GROUPS(bl_trig);
105 static int bl_trig_activate(struct led_classdev *led)
107 int ret;
109 struct bl_trig_notifier *n;
111 n = kzalloc(sizeof(struct bl_trig_notifier), GFP_KERNEL);
112 if (!n)
113 return -ENOMEM;
114 led_set_trigger_data(led, n);
116 n->led = led;
117 n->brightness = led->brightness;
118 n->old_status = UNBLANK;
119 n->notifier.notifier_call = fb_notifier_callback;
121 ret = fb_register_client(&n->notifier);
122 if (ret)
123 dev_err(led->dev, "unable to register backlight trigger\n");
125 return 0;
128 static void bl_trig_deactivate(struct led_classdev *led)
130 struct bl_trig_notifier *n = led_get_trigger_data(led);
132 fb_unregister_client(&n->notifier);
133 kfree(n);
136 static struct led_trigger bl_led_trigger = {
137 .name = "backlight",
138 .activate = bl_trig_activate,
139 .deactivate = bl_trig_deactivate,
140 .groups = bl_trig_groups,
142 module_led_trigger(bl_led_trigger);
144 MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
145 MODULE_DESCRIPTION("Backlight emulation LED trigger");
146 MODULE_LICENSE("GPL v2");