Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Backlight emulation LED trigger
0004  *
0005  * Copyright 2008 (C) Rodolfo Giometti <giometti@linux.it>
0006  * Copyright 2008 (C) Eurotech S.p.A. <info@eurotech.it>
0007  */
0008 
0009 #include <linux/module.h>
0010 #include <linux/kernel.h>
0011 #include <linux/slab.h>
0012 #include <linux/init.h>
0013 #include <linux/fb.h>
0014 #include <linux/leds.h>
0015 #include "../leds.h"
0016 
0017 #define BLANK       1
0018 #define UNBLANK     0
0019 
0020 struct bl_trig_notifier {
0021     struct led_classdev *led;
0022     int brightness;
0023     int old_status;
0024     struct notifier_block notifier;
0025     unsigned invert;
0026 };
0027 
0028 static int fb_notifier_callback(struct notifier_block *p,
0029                 unsigned long event, void *data)
0030 {
0031     struct bl_trig_notifier *n = container_of(p,
0032                     struct bl_trig_notifier, notifier);
0033     struct led_classdev *led = n->led;
0034     struct fb_event *fb_event = data;
0035     int *blank;
0036     int new_status;
0037 
0038     /* If we aren't interested in this event, skip it immediately ... */
0039     if (event != FB_EVENT_BLANK)
0040         return 0;
0041 
0042     blank = fb_event->data;
0043     new_status = *blank ? BLANK : UNBLANK;
0044 
0045     if (new_status == n->old_status)
0046         return 0;
0047 
0048     if ((n->old_status == UNBLANK) ^ n->invert) {
0049         n->brightness = led->brightness;
0050         led_set_brightness_nosleep(led, LED_OFF);
0051     } else {
0052         led_set_brightness_nosleep(led, n->brightness);
0053     }
0054 
0055     n->old_status = new_status;
0056 
0057     return 0;
0058 }
0059 
0060 static ssize_t bl_trig_invert_show(struct device *dev,
0061         struct device_attribute *attr, char *buf)
0062 {
0063     struct bl_trig_notifier *n = led_trigger_get_drvdata(dev);
0064 
0065     return sprintf(buf, "%u\n", n->invert);
0066 }
0067 
0068 static ssize_t bl_trig_invert_store(struct device *dev,
0069         struct device_attribute *attr, const char *buf, size_t num)
0070 {
0071     struct led_classdev *led = led_trigger_get_led(dev);
0072     struct bl_trig_notifier *n = led_trigger_get_drvdata(dev);
0073     unsigned long invert;
0074     int ret;
0075 
0076     ret = kstrtoul(buf, 10, &invert);
0077     if (ret < 0)
0078         return ret;
0079 
0080     if (invert > 1)
0081         return -EINVAL;
0082 
0083     n->invert = invert;
0084 
0085     /* After inverting, we need to update the LED. */
0086     if ((n->old_status == BLANK) ^ n->invert)
0087         led_set_brightness_nosleep(led, LED_OFF);
0088     else
0089         led_set_brightness_nosleep(led, n->brightness);
0090 
0091     return num;
0092 }
0093 static DEVICE_ATTR(inverted, 0644, bl_trig_invert_show, bl_trig_invert_store);
0094 
0095 static struct attribute *bl_trig_attrs[] = {
0096     &dev_attr_inverted.attr,
0097     NULL,
0098 };
0099 ATTRIBUTE_GROUPS(bl_trig);
0100 
0101 static int bl_trig_activate(struct led_classdev *led)
0102 {
0103     int ret;
0104 
0105     struct bl_trig_notifier *n;
0106 
0107     n = kzalloc(sizeof(struct bl_trig_notifier), GFP_KERNEL);
0108     if (!n)
0109         return -ENOMEM;
0110     led_set_trigger_data(led, n);
0111 
0112     n->led = led;
0113     n->brightness = led->brightness;
0114     n->old_status = UNBLANK;
0115     n->notifier.notifier_call = fb_notifier_callback;
0116 
0117     ret = fb_register_client(&n->notifier);
0118     if (ret)
0119         dev_err(led->dev, "unable to register backlight trigger\n");
0120 
0121     return 0;
0122 }
0123 
0124 static void bl_trig_deactivate(struct led_classdev *led)
0125 {
0126     struct bl_trig_notifier *n = led_get_trigger_data(led);
0127 
0128     fb_unregister_client(&n->notifier);
0129     kfree(n);
0130 }
0131 
0132 static struct led_trigger bl_led_trigger = {
0133     .name       = "backlight",
0134     .activate   = bl_trig_activate,
0135     .deactivate = bl_trig_deactivate,
0136     .groups     = bl_trig_groups,
0137 };
0138 module_led_trigger(bl_led_trigger);
0139 
0140 MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
0141 MODULE_DESCRIPTION("Backlight emulation LED trigger");
0142 MODULE_LICENSE("GPL v2");