Rodolfo Giometti | 132e930 | 2008-10-13 09:25:24 +0100 | [diff] [blame] | 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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 15 | #include <linux/slab.h> |
Rodolfo Giometti | 132e930 | 2008-10-13 09:25:24 +0100 | [diff] [blame] | 16 | #include <linux/init.h> |
| 17 | #include <linux/fb.h> |
| 18 | #include <linux/leds.h> |
Kim, Milo | f07fb52 | 2013-02-20 00:36:01 -0800 | [diff] [blame] | 19 | #include "../leds.h" |
Rodolfo Giometti | 132e930 | 2008-10-13 09:25:24 +0100 | [diff] [blame] | 20 | |
| 21 | #define BLANK 1 |
| 22 | #define UNBLANK 0 |
| 23 | |
| 24 | struct bl_trig_notifier { |
| 25 | struct led_classdev *led; |
| 26 | int brightness; |
| 27 | int old_status; |
| 28 | struct notifier_block notifier; |
Janusz Krzysztofik | 9f9455a | 2011-01-12 16:59:20 -0800 | [diff] [blame] | 29 | unsigned invert; |
Rodolfo Giometti | 132e930 | 2008-10-13 09:25:24 +0100 | [diff] [blame] | 30 | }; |
| 31 | |
| 32 | static 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 Schlaegl | c945cbc | 2013-08-13 04:17:05 -0700 | [diff] [blame] | 39 | int *blank; |
| 40 | int new_status; |
Rodolfo Giometti | 132e930 | 2008-10-13 09:25:24 +0100 | [diff] [blame] | 41 | |
Manfred Schlaegl | c945cbc | 2013-08-13 04:17:05 -0700 | [diff] [blame] | 42 | /* If we aren't interested in this event, skip it immediately ... */ |
| 43 | if (event != FB_EVENT_BLANK) |
| 44 | return 0; |
Janusz Krzysztofik | 9f9455a | 2011-01-12 16:59:20 -0800 | [diff] [blame] | 45 | |
Manfred Schlaegl | c945cbc | 2013-08-13 04:17:05 -0700 | [diff] [blame] | 46 | blank = fb_event->data; |
| 47 | new_status = *blank ? BLANK : UNBLANK; |
Janusz Krzysztofik | 9f9455a | 2011-01-12 16:59:20 -0800 | [diff] [blame] | 48 | |
Manfred Schlaegl | c945cbc | 2013-08-13 04:17:05 -0700 | [diff] [blame] | 49 | if (new_status == n->old_status) |
| 50 | return 0; |
Janusz Krzysztofik | 9f9455a | 2011-01-12 16:59:20 -0800 | [diff] [blame] | 51 | |
Manfred Schlaegl | c945cbc | 2013-08-13 04:17:05 -0700 | [diff] [blame] | 52 | if ((n->old_status == UNBLANK) ^ n->invert) { |
| 53 | n->brightness = led->brightness; |
Jacek Anaszewski | 4d71a4a | 2014-11-14 02:50:18 -0800 | [diff] [blame] | 54 | led_set_brightness_async(led, LED_OFF); |
Manfred Schlaegl | c945cbc | 2013-08-13 04:17:05 -0700 | [diff] [blame] | 55 | } else { |
Jacek Anaszewski | 4d71a4a | 2014-11-14 02:50:18 -0800 | [diff] [blame] | 56 | led_set_brightness_async(led, n->brightness); |
Rodolfo Giometti | 132e930 | 2008-10-13 09:25:24 +0100 | [diff] [blame] | 57 | } |
| 58 | |
Manfred Schlaegl | c945cbc | 2013-08-13 04:17:05 -0700 | [diff] [blame] | 59 | n->old_status = new_status; |
| 60 | |
Rodolfo Giometti | 132e930 | 2008-10-13 09:25:24 +0100 | [diff] [blame] | 61 | return 0; |
| 62 | } |
| 63 | |
Janusz Krzysztofik | 9f9455a | 2011-01-12 16:59:20 -0800 | [diff] [blame] | 64 | static 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 | |
| 73 | static 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 | |
Jingoo Han | 75253b3 | 2012-10-23 05:26:56 -0700 | [diff] [blame] | 81 | ret = kstrtoul(buf, 10, &invert); |
Janusz Krzysztofik | 9f9455a | 2011-01-12 16:59:20 -0800 | [diff] [blame] | 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) |
Jacek Anaszewski | 4d71a4a | 2014-11-14 02:50:18 -0800 | [diff] [blame] | 92 | led_set_brightness_async(led, LED_OFF); |
Janusz Krzysztofik | 9f9455a | 2011-01-12 16:59:20 -0800 | [diff] [blame] | 93 | else |
Jacek Anaszewski | 4d71a4a | 2014-11-14 02:50:18 -0800 | [diff] [blame] | 94 | led_set_brightness_async(led, n->brightness); |
Janusz Krzysztofik | 9f9455a | 2011-01-12 16:59:20 -0800 | [diff] [blame] | 95 | |
| 96 | return num; |
| 97 | } |
| 98 | static DEVICE_ATTR(inverted, 0644, bl_trig_invert_show, bl_trig_invert_store); |
| 99 | |
Rodolfo Giometti | 132e930 | 2008-10-13 09:25:24 +0100 | [diff] [blame] | 100 | static 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 | |
Janusz Krzysztofik | 9f9455a | 2011-01-12 16:59:20 -0800 | [diff] [blame] | 113 | ret = device_create_file(led->dev, &dev_attr_inverted); |
| 114 | if (ret) |
| 115 | goto err_invert; |
| 116 | |
Rodolfo Giometti | 132e930 | 2008-10-13 09:25:24 +0100 | [diff] [blame] | 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"); |
Shuah Khan | 03c091e | 2012-05-29 15:07:28 -0700 | [diff] [blame] | 125 | led->activated = true; |
Janusz Krzysztofik | 9f9455a | 2011-01-12 16:59:20 -0800 | [diff] [blame] | 126 | |
| 127 | return; |
| 128 | |
| 129 | err_invert: |
| 130 | led->trigger_data = NULL; |
| 131 | kfree(n); |
Rodolfo Giometti | 132e930 | 2008-10-13 09:25:24 +0100 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | static void bl_trig_deactivate(struct led_classdev *led) |
| 135 | { |
| 136 | struct bl_trig_notifier *n = |
| 137 | (struct bl_trig_notifier *) led->trigger_data; |
| 138 | |
Shuah Khan | 03c091e | 2012-05-29 15:07:28 -0700 | [diff] [blame] | 139 | if (led->activated) { |
Janusz Krzysztofik | 9f9455a | 2011-01-12 16:59:20 -0800 | [diff] [blame] | 140 | device_remove_file(led->dev, &dev_attr_inverted); |
Rodolfo Giometti | 132e930 | 2008-10-13 09:25:24 +0100 | [diff] [blame] | 141 | fb_unregister_client(&n->notifier); |
| 142 | kfree(n); |
Shuah Khan | 03c091e | 2012-05-29 15:07:28 -0700 | [diff] [blame] | 143 | led->activated = false; |
Rodolfo Giometti | 132e930 | 2008-10-13 09:25:24 +0100 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | |
| 147 | static struct led_trigger bl_led_trigger = { |
| 148 | .name = "backlight", |
| 149 | .activate = bl_trig_activate, |
| 150 | .deactivate = bl_trig_deactivate |
| 151 | }; |
| 152 | |
| 153 | static int __init bl_trig_init(void) |
| 154 | { |
| 155 | return led_trigger_register(&bl_led_trigger); |
| 156 | } |
| 157 | |
| 158 | static void __exit bl_trig_exit(void) |
| 159 | { |
| 160 | led_trigger_unregister(&bl_led_trigger); |
| 161 | } |
| 162 | |
| 163 | module_init(bl_trig_init); |
| 164 | module_exit(bl_trig_exit); |
| 165 | |
| 166 | MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>"); |
| 167 | MODULE_DESCRIPTION("Backlight emulation LED trigger"); |
| 168 | MODULE_LICENSE("GPL v2"); |