blob: c2b57beef718a3ed7931f51a6371cddb15ed77d4 [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>
Kim, Milof07fb522013-02-20 00:36:01 -080019#include "../leds.h"
Rodolfo Giometti132e9302008-10-13 09:25:24 +010020
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;
Manfred Schlaeglc945cbc2013-08-13 04:17:05 -070039 int *blank;
40 int new_status;
Rodolfo Giometti132e9302008-10-13 09:25:24 +010041
Manfred Schlaeglc945cbc2013-08-13 04:17:05 -070042 /* If we aren't interested in this event, skip it immediately ... */
43 if (event != FB_EVENT_BLANK)
44 return 0;
Janusz Krzysztofik9f9455a2011-01-12 16:59:20 -080045
Manfred Schlaeglc945cbc2013-08-13 04:17:05 -070046 blank = fb_event->data;
47 new_status = *blank ? BLANK : UNBLANK;
Janusz Krzysztofik9f9455a2011-01-12 16:59:20 -080048
Manfred Schlaeglc945cbc2013-08-13 04:17:05 -070049 if (new_status == n->old_status)
50 return 0;
Janusz Krzysztofik9f9455a2011-01-12 16:59:20 -080051
Manfred Schlaeglc945cbc2013-08-13 04:17:05 -070052 if ((n->old_status == UNBLANK) ^ n->invert) {
53 n->brightness = led->brightness;
Jacek Anaszewski81fe8e52015-10-07 11:10:41 +020054 led_set_brightness_nosleep(led, LED_OFF);
Manfred Schlaeglc945cbc2013-08-13 04:17:05 -070055 } else {
Jacek Anaszewski81fe8e52015-10-07 11:10:41 +020056 led_set_brightness_nosleep(led, n->brightness);
Rodolfo Giometti132e9302008-10-13 09:25:24 +010057 }
58
Manfred Schlaeglc945cbc2013-08-13 04:17:05 -070059 n->old_status = new_status;
60
Rodolfo Giometti132e9302008-10-13 09:25:24 +010061 return 0;
62}
63
Janusz Krzysztofik9f9455a2011-01-12 16:59:20 -080064static ssize_t bl_trig_invert_show(struct device *dev,
65 struct device_attribute *attr, char *buf)
66{
Uwe Kleine-Könige4786ba2018-07-02 22:05:33 +020067 struct bl_trig_notifier *n = led_trigger_get_drvdata(dev);
Janusz Krzysztofik9f9455a2011-01-12 16:59:20 -080068
69 return sprintf(buf, "%u\n", n->invert);
70}
71
72static ssize_t bl_trig_invert_store(struct device *dev,
73 struct device_attribute *attr, const char *buf, size_t num)
74{
Uwe Kleine-Könige4786ba2018-07-02 22:05:33 +020075 struct led_classdev *led = led_trigger_get_led(dev);
76 struct bl_trig_notifier *n = led_trigger_get_drvdata(dev);
Janusz Krzysztofik9f9455a2011-01-12 16:59:20 -080077 unsigned long invert;
78 int ret;
79
Jingoo Han75253b32012-10-23 05:26:56 -070080 ret = kstrtoul(buf, 10, &invert);
Janusz Krzysztofik9f9455a2011-01-12 16:59:20 -080081 if (ret < 0)
82 return ret;
83
84 if (invert > 1)
85 return -EINVAL;
86
87 n->invert = invert;
88
89 /* After inverting, we need to update the LED. */
90 if ((n->old_status == BLANK) ^ n->invert)
Jacek Anaszewski81fe8e52015-10-07 11:10:41 +020091 led_set_brightness_nosleep(led, LED_OFF);
Janusz Krzysztofik9f9455a2011-01-12 16:59:20 -080092 else
Jacek Anaszewski81fe8e52015-10-07 11:10:41 +020093 led_set_brightness_nosleep(led, n->brightness);
Janusz Krzysztofik9f9455a2011-01-12 16:59:20 -080094
95 return num;
96}
97static DEVICE_ATTR(inverted, 0644, bl_trig_invert_show, bl_trig_invert_store);
98
Uwe Kleine-Könige4786ba2018-07-02 22:05:33 +020099static struct attribute *bl_trig_attrs[] = {
100 &dev_attr_inverted.attr,
101 NULL,
102};
103ATTRIBUTE_GROUPS(bl_trig);
104
Uwe Kleine-König2282e1252018-07-02 22:05:21 +0200105static int bl_trig_activate(struct led_classdev *led)
Rodolfo Giometti132e9302008-10-13 09:25:24 +0100106{
107 int ret;
108
109 struct bl_trig_notifier *n;
110
111 n = kzalloc(sizeof(struct bl_trig_notifier), GFP_KERNEL);
Uwe Kleine-Könige4786ba2018-07-02 22:05:33 +0200112 if (!n)
113 return -ENOMEM;
114 led_set_trigger_data(led, n);
Janusz Krzysztofik9f9455a2011-01-12 16:59:20 -0800115
Rodolfo Giometti132e9302008-10-13 09:25:24 +0100116 n->led = led;
117 n->brightness = led->brightness;
118 n->old_status = UNBLANK;
119 n->notifier.notifier_call = fb_notifier_callback;
120
121 ret = fb_register_client(&n->notifier);
122 if (ret)
123 dev_err(led->dev, "unable to register backlight trigger\n");
Uwe Kleine-König2282e1252018-07-02 22:05:21 +0200124
125 return 0;
Rodolfo Giometti132e9302008-10-13 09:25:24 +0100126}
127
128static void bl_trig_deactivate(struct led_classdev *led)
129{
Uwe Kleine-Könige4786ba2018-07-02 22:05:33 +0200130 struct bl_trig_notifier *n = led_get_trigger_data(led);
Rodolfo Giometti132e9302008-10-13 09:25:24 +0100131
Uwe Kleine-Könige4786ba2018-07-02 22:05:33 +0200132 fb_unregister_client(&n->notifier);
133 kfree(n);
Rodolfo Giometti132e9302008-10-13 09:25:24 +0100134}
135
136static struct led_trigger bl_led_trigger = {
137 .name = "backlight",
138 .activate = bl_trig_activate,
Uwe Kleine-Könige4786ba2018-07-02 22:05:33 +0200139 .deactivate = bl_trig_deactivate,
140 .groups = bl_trig_groups,
Rodolfo Giometti132e9302008-10-13 09:25:24 +0100141};
Uwe Kleine-Könige4786ba2018-07-02 22:05:33 +0200142module_led_trigger(bl_led_trigger);
Rodolfo Giometti132e9302008-10-13 09:25:24 +0100143
144MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
145MODULE_DESCRIPTION("Backlight emulation LED trigger");
146MODULE_LICENSE("GPL v2");