blob: 027a2b15d7d873d09b510209bdf95dcf72a45ddd [file] [log] [blame]
Rodolfo Giometti132e9302008-10-13 09:25:24 +01001/*
2 * Backlight emulation LED trigger
3 *
4 * Copyright 2008 (C) Rodolfo Giometti <giometti@linux.it>
5 * Copyright 2008 (C) Eurotech S.p.A. <info@eurotech.it>
6 *
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.
10 *
11 */
12
13#include <linux/module.h>
14#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Rodolfo Giometti132e9302008-10-13 09:25:24 +010016#include <linux/init.h>
17#include <linux/fb.h>
18#include <linux/leds.h>
19#include "leds.h"
20
21#define BLANK 1
22#define UNBLANK 0
23
24struct bl_trig_notifier {
25 struct led_classdev *led;
26 int brightness;
27 int old_status;
28 struct notifier_block notifier;
Janusz Krzysztofik9f9455a2011-01-12 16:59:20 -080029 unsigned invert;
Rodolfo Giometti132e9302008-10-13 09:25:24 +010030};
31
32static int fb_notifier_callback(struct notifier_block *p,
33 unsigned long event, void *data)
34{
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 = fb_event->data;
Janusz Krzysztofik9f9455a2011-01-12 16:59:20 -080040 int new_status = *blank ? BLANK : UNBLANK;
Rodolfo Giometti132e9302008-10-13 09:25:24 +010041
42 switch (event) {
Sachin Kamat18e6f132012-11-25 12:52:28 +053043 case FB_EVENT_BLANK:
Janusz Krzysztofik9f9455a2011-01-12 16:59:20 -080044 if (new_status == n->old_status)
45 break;
46
47 if ((n->old_status == UNBLANK) ^ n->invert) {
Rodolfo Giometti132e9302008-10-13 09:25:24 +010048 n->brightness = led->brightness;
Shuah Khan0da3e652012-06-13 10:01:37 +080049 __led_set_brightness(led, LED_OFF);
Janusz Krzysztofik9f9455a2011-01-12 16:59:20 -080050 } else {
Shuah Khan0da3e652012-06-13 10:01:37 +080051 __led_set_brightness(led, n->brightness);
Rodolfo Giometti132e9302008-10-13 09:25:24 +010052 }
Janusz Krzysztofik9f9455a2011-01-12 16:59:20 -080053
54 n->old_status = new_status;
55
Rodolfo Giometti132e9302008-10-13 09:25:24 +010056 break;
57 }
58
59 return 0;
60}
61
Janusz Krzysztofik9f9455a2011-01-12 16:59:20 -080062static ssize_t bl_trig_invert_show(struct device *dev,
63 struct device_attribute *attr, char *buf)
64{
65 struct led_classdev *led = dev_get_drvdata(dev);
66 struct bl_trig_notifier *n = led->trigger_data;
67
68 return sprintf(buf, "%u\n", n->invert);
69}
70
71static ssize_t bl_trig_invert_store(struct device *dev,
72 struct device_attribute *attr, const char *buf, size_t num)
73{
74 struct led_classdev *led = dev_get_drvdata(dev);
75 struct bl_trig_notifier *n = led->trigger_data;
76 unsigned long invert;
77 int ret;
78
Jingoo Han75253b32012-10-23 05:26:56 -070079 ret = kstrtoul(buf, 10, &invert);
Janusz Krzysztofik9f9455a2011-01-12 16:59:20 -080080 if (ret < 0)
81 return ret;
82
83 if (invert > 1)
84 return -EINVAL;
85
86 n->invert = invert;
87
88 /* After inverting, we need to update the LED. */
89 if ((n->old_status == BLANK) ^ n->invert)
Shuah Khan0da3e652012-06-13 10:01:37 +080090 __led_set_brightness(led, LED_OFF);
Janusz Krzysztofik9f9455a2011-01-12 16:59:20 -080091 else
Shuah Khan0da3e652012-06-13 10:01:37 +080092 __led_set_brightness(led, n->brightness);
Janusz Krzysztofik9f9455a2011-01-12 16:59:20 -080093
94 return num;
95}
96static DEVICE_ATTR(inverted, 0644, bl_trig_invert_show, bl_trig_invert_store);
97
Rodolfo Giometti132e9302008-10-13 09:25:24 +010098static void bl_trig_activate(struct led_classdev *led)
99{
100 int ret;
101
102 struct bl_trig_notifier *n;
103
104 n = kzalloc(sizeof(struct bl_trig_notifier), GFP_KERNEL);
105 led->trigger_data = n;
106 if (!n) {
107 dev_err(led->dev, "unable to allocate backlight trigger\n");
108 return;
109 }
110
Janusz Krzysztofik9f9455a2011-01-12 16:59:20 -0800111 ret = device_create_file(led->dev, &dev_attr_inverted);
112 if (ret)
113 goto err_invert;
114
Rodolfo Giometti132e9302008-10-13 09:25:24 +0100115 n->led = led;
116 n->brightness = led->brightness;
117 n->old_status = UNBLANK;
118 n->notifier.notifier_call = fb_notifier_callback;
119
120 ret = fb_register_client(&n->notifier);
121 if (ret)
122 dev_err(led->dev, "unable to register backlight trigger\n");
Shuah Khan03c091e2012-05-29 15:07:28 -0700123 led->activated = true;
Janusz Krzysztofik9f9455a2011-01-12 16:59:20 -0800124
125 return;
126
127err_invert:
128 led->trigger_data = NULL;
129 kfree(n);
Rodolfo Giometti132e9302008-10-13 09:25:24 +0100130}
131
132static void bl_trig_deactivate(struct led_classdev *led)
133{
134 struct bl_trig_notifier *n =
135 (struct bl_trig_notifier *) led->trigger_data;
136
Shuah Khan03c091e2012-05-29 15:07:28 -0700137 if (led->activated) {
Janusz Krzysztofik9f9455a2011-01-12 16:59:20 -0800138 device_remove_file(led->dev, &dev_attr_inverted);
Rodolfo Giometti132e9302008-10-13 09:25:24 +0100139 fb_unregister_client(&n->notifier);
140 kfree(n);
Shuah Khan03c091e2012-05-29 15:07:28 -0700141 led->activated = false;
Rodolfo Giometti132e9302008-10-13 09:25:24 +0100142 }
143}
144
145static struct led_trigger bl_led_trigger = {
146 .name = "backlight",
147 .activate = bl_trig_activate,
148 .deactivate = bl_trig_deactivate
149};
150
151static int __init bl_trig_init(void)
152{
153 return led_trigger_register(&bl_led_trigger);
154}
155
156static void __exit bl_trig_exit(void)
157{
158 led_trigger_unregister(&bl_led_trigger);
159}
160
161module_init(bl_trig_init);
162module_exit(bl_trig_exit);
163
164MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
165MODULE_DESCRIPTION("Backlight emulation LED trigger");
166MODULE_LICENSE("GPL v2");