Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Backlight emulation LED trigger
4 *
5 * Copyright 2008 (C) Rodolfo Giometti <giometti@linux.it>
6 * Copyright 2008 (C) Eurotech S.p.A. <info@eurotech.it>
7 */
8
9#include <linux/module.h>
10#include <linux/kernel.h>
11#include <linux/slab.h>
12#include <linux/init.h>
13#include <linux/fb.h>
14#include <linux/leds.h>
15#include "../leds.h"
16
17#define BLANK 1
18#define UNBLANK 0
19
20struct bl_trig_notifier {
21 struct led_classdev *led;
22 int brightness;
23 int old_status;
24 struct notifier_block notifier;
25 unsigned invert;
26};
27
28static int fb_notifier_callback(struct notifier_block *p,
29 unsigned long event, void *data)
30{
31 struct bl_trig_notifier *n = container_of(p,
32 struct bl_trig_notifier, notifier);
33 struct led_classdev *led = n->led;
34 struct fb_event *fb_event = data;
35 int *blank;
36 int new_status;
37
38 /* If we aren't interested in this event, skip it immediately ... */
39 if (event != FB_EVENT_BLANK)
40 return 0;
41
42 blank = fb_event->data;
43 new_status = *blank ? BLANK : UNBLANK;
44
45 if (new_status == n->old_status)
46 return 0;
47
48 if ((n->old_status == UNBLANK) ^ n->invert) {
49 n->brightness = led->brightness;
50 led_set_brightness_nosleep(led, LED_OFF);
51 } else {
52 led_set_brightness_nosleep(led, n->brightness);
53 }
54
55 n->old_status = new_status;
56
57 return 0;
58}
59
60static ssize_t bl_trig_invert_show(struct device *dev,
61 struct device_attribute *attr, char *buf)
62{
63 struct bl_trig_notifier *n = led_trigger_get_drvdata(dev);
64
65 return sprintf(buf, "%u\n", n->invert);
66}
67
68static ssize_t bl_trig_invert_store(struct device *dev,
69 struct device_attribute *attr, const char *buf, size_t num)
70{
71 struct led_classdev *led = led_trigger_get_led(dev);
72 struct bl_trig_notifier *n = led_trigger_get_drvdata(dev);
73 unsigned long invert;
74 int ret;
75
76 ret = kstrtoul(buf, 10, &invert);
77 if (ret < 0)
78 return ret;
79
80 if (invert > 1)
81 return -EINVAL;
82
83 n->invert = invert;
84
85 /* After inverting, we need to update the LED. */
86 if ((n->old_status == BLANK) ^ n->invert)
87 led_set_brightness_nosleep(led, LED_OFF);
88 else
89 led_set_brightness_nosleep(led, n->brightness);
90
91 return num;
92}
93static DEVICE_ATTR(inverted, 0644, bl_trig_invert_show, bl_trig_invert_store);
94
95static struct attribute *bl_trig_attrs[] = {
96 &dev_attr_inverted.attr,
97 NULL,
98};
99ATTRIBUTE_GROUPS(bl_trig);
100
101static int bl_trig_activate(struct led_classdev *led)
102{
103 int ret;
104
105 struct bl_trig_notifier *n;
106
107 n = kzalloc(sizeof(struct bl_trig_notifier), GFP_KERNEL);
108 if (!n)
109 return -ENOMEM;
110 led_set_trigger_data(led, n);
111
112 n->led = led;
113 n->brightness = led->brightness;
114 n->old_status = UNBLANK;
115 n->notifier.notifier_call = fb_notifier_callback;
116
117 ret = fb_register_client(&n->notifier);
118 if (ret)
119 dev_err(led->dev, "unable to register backlight trigger\n");
120
121 return 0;
122}
123
124static void bl_trig_deactivate(struct led_classdev *led)
125{
126 struct bl_trig_notifier *n = led_get_trigger_data(led);
127
128 fb_unregister_client(&n->notifier);
129 kfree(n);
130}
131
132static struct led_trigger bl_led_trigger = {
133 .name = "backlight",
134 .activate = bl_trig_activate,
135 .deactivate = bl_trig_deactivate,
136 .groups = bl_trig_groups,
137};
138module_led_trigger(bl_led_trigger);
139
140MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
141MODULE_DESCRIPTION("Backlight emulation LED trigger");
142MODULE_LICENSE("GPL v2");
1/*
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>
15#include <linux/slab.h>
16#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;
29 unsigned invert;
30};
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;
40 int new_status;
41
42 /* If we aren't interested in this event, skip it immediately ... */
43 if (event != FB_EVENT_BLANK)
44 return 0;
45
46 blank = fb_event->data;
47 new_status = *blank ? BLANK : UNBLANK;
48
49 if (new_status == n->old_status)
50 return 0;
51
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);
57 }
58
59 n->old_status = new_status;
60
61 return 0;
62}
63
64static ssize_t bl_trig_invert_show(struct device *dev,
65 struct device_attribute *attr, char *buf)
66{
67 struct led_classdev *led = dev_get_drvdata(dev);
68 struct bl_trig_notifier *n = led->trigger_data;
69
70 return sprintf(buf, "%u\n", n->invert);
71}
72
73static ssize_t bl_trig_invert_store(struct device *dev,
74 struct device_attribute *attr, const char *buf, size_t num)
75{
76 struct led_classdev *led = dev_get_drvdata(dev);
77 struct bl_trig_notifier *n = led->trigger_data;
78 unsigned long invert;
79 int ret;
80
81 ret = kstrtoul(buf, 10, &invert);
82 if (ret < 0)
83 return ret;
84
85 if (invert > 1)
86 return -EINVAL;
87
88 n->invert = invert;
89
90 /* After inverting, we need to update the LED. */
91 if ((n->old_status == BLANK) ^ n->invert)
92 led_set_brightness_nosleep(led, LED_OFF);
93 else
94 led_set_brightness_nosleep(led, n->brightness);
95
96 return num;
97}
98static DEVICE_ATTR(inverted, 0644, bl_trig_invert_show, bl_trig_invert_store);
99
100static void bl_trig_activate(struct led_classdev *led)
101{
102 int ret;
103
104 struct bl_trig_notifier *n;
105
106 n = kzalloc(sizeof(struct bl_trig_notifier), GFP_KERNEL);
107 led->trigger_data = n;
108 if (!n) {
109 dev_err(led->dev, "unable to allocate backlight trigger\n");
110 return;
111 }
112
113 ret = device_create_file(led->dev, &dev_attr_inverted);
114 if (ret)
115 goto err_invert;
116
117 n->led = led;
118 n->brightness = led->brightness;
119 n->old_status = UNBLANK;
120 n->notifier.notifier_call = fb_notifier_callback;
121
122 ret = fb_register_client(&n->notifier);
123 if (ret)
124 dev_err(led->dev, "unable to register backlight trigger\n");
125 led->activated = true;
126
127 return;
128
129err_invert:
130 led->trigger_data = NULL;
131 kfree(n);
132}
133
134static void bl_trig_deactivate(struct led_classdev *led)
135{
136 struct bl_trig_notifier *n =
137 (struct bl_trig_notifier *) led->trigger_data;
138
139 if (led->activated) {
140 device_remove_file(led->dev, &dev_attr_inverted);
141 fb_unregister_client(&n->notifier);
142 kfree(n);
143 led->activated = false;
144 }
145}
146
147static struct led_trigger bl_led_trigger = {
148 .name = "backlight",
149 .activate = bl_trig_activate,
150 .deactivate = bl_trig_deactivate
151};
152
153static int __init bl_trig_init(void)
154{
155 return led_trigger_register(&bl_led_trigger);
156}
157
158static void __exit bl_trig_exit(void)
159{
160 led_trigger_unregister(&bl_led_trigger);
161}
162
163module_init(bl_trig_init);
164module_exit(bl_trig_exit);
165
166MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
167MODULE_DESCRIPTION("Backlight emulation LED trigger");
168MODULE_LICENSE("GPL v2");