blob: 487577d22cfc651fed0127a4707b680ef8a5e1d7 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Rodolfo Giometti132e9302008-10-13 09:25:24 +01002/*
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>
Rodolfo Giometti132e9302008-10-13 09:25:24 +01007 */
8
9#include <linux/module.h>
10#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090011#include <linux/slab.h>
Rodolfo Giometti132e9302008-10-13 09:25:24 +010012#include <linux/init.h>
13#include <linux/fb.h>
14#include <linux/leds.h>
Kim, Milof07fb522013-02-20 00:36:01 -080015#include "../leds.h"
Rodolfo Giometti132e9302008-10-13 09:25:24 +010016
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;
Janusz Krzysztofik9f9455a2011-01-12 16:59:20 -080025 unsigned invert;
Rodolfo Giometti132e9302008-10-13 09:25:24 +010026};
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;
Manfred Schlaeglc945cbc2013-08-13 04:17:05 -070035 int *blank;
36 int new_status;
Rodolfo Giometti132e9302008-10-13 09:25:24 +010037
Manfred Schlaeglc945cbc2013-08-13 04:17:05 -070038 /* If we aren't interested in this event, skip it immediately ... */
39 if (event != FB_EVENT_BLANK)
40 return 0;
Janusz Krzysztofik9f9455a2011-01-12 16:59:20 -080041
Manfred Schlaeglc945cbc2013-08-13 04:17:05 -070042 blank = fb_event->data;
43 new_status = *blank ? BLANK : UNBLANK;
Janusz Krzysztofik9f9455a2011-01-12 16:59:20 -080044
Manfred Schlaeglc945cbc2013-08-13 04:17:05 -070045 if (new_status == n->old_status)
46 return 0;
Janusz Krzysztofik9f9455a2011-01-12 16:59:20 -080047
Manfred Schlaeglc945cbc2013-08-13 04:17:05 -070048 if ((n->old_status == UNBLANK) ^ n->invert) {
49 n->brightness = led->brightness;
Jacek Anaszewski81fe8e52015-10-07 11:10:41 +020050 led_set_brightness_nosleep(led, LED_OFF);
Manfred Schlaeglc945cbc2013-08-13 04:17:05 -070051 } else {
Jacek Anaszewski81fe8e52015-10-07 11:10:41 +020052 led_set_brightness_nosleep(led, n->brightness);
Rodolfo Giometti132e9302008-10-13 09:25:24 +010053 }
54
Manfred Schlaeglc945cbc2013-08-13 04:17:05 -070055 n->old_status = new_status;
56
Rodolfo Giometti132e9302008-10-13 09:25:24 +010057 return 0;
58}
59
Janusz Krzysztofik9f9455a2011-01-12 16:59:20 -080060static ssize_t bl_trig_invert_show(struct device *dev,
61 struct device_attribute *attr, char *buf)
62{
Uwe Kleine-Könige4786ba2018-07-02 22:05:33 +020063 struct bl_trig_notifier *n = led_trigger_get_drvdata(dev);
Janusz Krzysztofik9f9455a2011-01-12 16:59:20 -080064
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{
Uwe Kleine-Könige4786ba2018-07-02 22:05:33 +020071 struct led_classdev *led = led_trigger_get_led(dev);
72 struct bl_trig_notifier *n = led_trigger_get_drvdata(dev);
Janusz Krzysztofik9f9455a2011-01-12 16:59:20 -080073 unsigned long invert;
74 int ret;
75
Jingoo Han75253b32012-10-23 05:26:56 -070076 ret = kstrtoul(buf, 10, &invert);
Janusz Krzysztofik9f9455a2011-01-12 16:59:20 -080077 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)
Jacek Anaszewski81fe8e52015-10-07 11:10:41 +020087 led_set_brightness_nosleep(led, LED_OFF);
Janusz Krzysztofik9f9455a2011-01-12 16:59:20 -080088 else
Jacek Anaszewski81fe8e52015-10-07 11:10:41 +020089 led_set_brightness_nosleep(led, n->brightness);
Janusz Krzysztofik9f9455a2011-01-12 16:59:20 -080090
91 return num;
92}
93static DEVICE_ATTR(inverted, 0644, bl_trig_invert_show, bl_trig_invert_store);
94
Uwe Kleine-Könige4786ba2018-07-02 22:05:33 +020095static struct attribute *bl_trig_attrs[] = {
96 &dev_attr_inverted.attr,
97 NULL,
98};
99ATTRIBUTE_GROUPS(bl_trig);
100
Uwe Kleine-König2282e1252018-07-02 22:05:21 +0200101static int bl_trig_activate(struct led_classdev *led)
Rodolfo Giometti132e9302008-10-13 09:25:24 +0100102{
103 int ret;
104
105 struct bl_trig_notifier *n;
106
107 n = kzalloc(sizeof(struct bl_trig_notifier), GFP_KERNEL);
Uwe Kleine-Könige4786ba2018-07-02 22:05:33 +0200108 if (!n)
109 return -ENOMEM;
110 led_set_trigger_data(led, n);
Janusz Krzysztofik9f9455a2011-01-12 16:59:20 -0800111
Rodolfo Giometti132e9302008-10-13 09:25:24 +0100112 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");
Uwe Kleine-König2282e1252018-07-02 22:05:21 +0200120
121 return 0;
Rodolfo Giometti132e9302008-10-13 09:25:24 +0100122}
123
124static void bl_trig_deactivate(struct led_classdev *led)
125{
Uwe Kleine-Könige4786ba2018-07-02 22:05:33 +0200126 struct bl_trig_notifier *n = led_get_trigger_data(led);
Rodolfo Giometti132e9302008-10-13 09:25:24 +0100127
Uwe Kleine-Könige4786ba2018-07-02 22:05:33 +0200128 fb_unregister_client(&n->notifier);
129 kfree(n);
Rodolfo Giometti132e9302008-10-13 09:25:24 +0100130}
131
132static struct led_trigger bl_led_trigger = {
133 .name = "backlight",
134 .activate = bl_trig_activate,
Uwe Kleine-Könige4786ba2018-07-02 22:05:33 +0200135 .deactivate = bl_trig_deactivate,
136 .groups = bl_trig_groups,
Rodolfo Giometti132e9302008-10-13 09:25:24 +0100137};
Uwe Kleine-Könige4786ba2018-07-02 22:05:33 +0200138module_led_trigger(bl_led_trigger);
Rodolfo Giometti132e9302008-10-13 09:25:24 +0100139
140MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
141MODULE_DESCRIPTION("Backlight emulation LED trigger");
142MODULE_LICENSE("GPL v2");