Path: blob/master/drivers/leds/ledtrig-backlight.c
15112 views
/*1* Backlight emulation LED trigger2*3* Copyright 2008 (C) Rodolfo Giometti <[email protected]>4* Copyright 2008 (C) Eurotech S.p.A. <[email protected]>5*6* This program is free software; you can redistribute it and/or modify7* it under the terms of the GNU General Public License version 2 as8* published by the Free Software Foundation.9*10*/1112#include <linux/module.h>13#include <linux/kernel.h>14#include <linux/slab.h>15#include <linux/init.h>16#include <linux/fb.h>17#include <linux/leds.h>18#include "leds.h"1920#define BLANK 121#define UNBLANK 02223struct bl_trig_notifier {24struct led_classdev *led;25int brightness;26int old_status;27struct notifier_block notifier;28unsigned invert;29};3031static int fb_notifier_callback(struct notifier_block *p,32unsigned long event, void *data)33{34struct bl_trig_notifier *n = container_of(p,35struct bl_trig_notifier, notifier);36struct led_classdev *led = n->led;37struct fb_event *fb_event = data;38int *blank = fb_event->data;39int new_status = *blank ? BLANK : UNBLANK;4041switch (event) {42case FB_EVENT_BLANK :43if (new_status == n->old_status)44break;4546if ((n->old_status == UNBLANK) ^ n->invert) {47n->brightness = led->brightness;48led_set_brightness(led, LED_OFF);49} else {50led_set_brightness(led, n->brightness);51}5253n->old_status = new_status;5455break;56}5758return 0;59}6061static ssize_t bl_trig_invert_show(struct device *dev,62struct device_attribute *attr, char *buf)63{64struct led_classdev *led = dev_get_drvdata(dev);65struct bl_trig_notifier *n = led->trigger_data;6667return sprintf(buf, "%u\n", n->invert);68}6970static ssize_t bl_trig_invert_store(struct device *dev,71struct device_attribute *attr, const char *buf, size_t num)72{73struct led_classdev *led = dev_get_drvdata(dev);74struct bl_trig_notifier *n = led->trigger_data;75unsigned long invert;76int ret;7778ret = strict_strtoul(buf, 10, &invert);79if (ret < 0)80return ret;8182if (invert > 1)83return -EINVAL;8485n->invert = invert;8687/* After inverting, we need to update the LED. */88if ((n->old_status == BLANK) ^ n->invert)89led_set_brightness(led, LED_OFF);90else91led_set_brightness(led, n->brightness);9293return num;94}95static DEVICE_ATTR(inverted, 0644, bl_trig_invert_show, bl_trig_invert_store);9697static void bl_trig_activate(struct led_classdev *led)98{99int ret;100101struct bl_trig_notifier *n;102103n = kzalloc(sizeof(struct bl_trig_notifier), GFP_KERNEL);104led->trigger_data = n;105if (!n) {106dev_err(led->dev, "unable to allocate backlight trigger\n");107return;108}109110ret = device_create_file(led->dev, &dev_attr_inverted);111if (ret)112goto err_invert;113114n->led = led;115n->brightness = led->brightness;116n->old_status = UNBLANK;117n->notifier.notifier_call = fb_notifier_callback;118119ret = fb_register_client(&n->notifier);120if (ret)121dev_err(led->dev, "unable to register backlight trigger\n");122123return;124125err_invert:126led->trigger_data = NULL;127kfree(n);128}129130static void bl_trig_deactivate(struct led_classdev *led)131{132struct bl_trig_notifier *n =133(struct bl_trig_notifier *) led->trigger_data;134135if (n) {136device_remove_file(led->dev, &dev_attr_inverted);137fb_unregister_client(&n->notifier);138kfree(n);139}140}141142static struct led_trigger bl_led_trigger = {143.name = "backlight",144.activate = bl_trig_activate,145.deactivate = bl_trig_deactivate146};147148static int __init bl_trig_init(void)149{150return led_trigger_register(&bl_led_trigger);151}152153static void __exit bl_trig_exit(void)154{155led_trigger_unregister(&bl_led_trigger);156}157158module_init(bl_trig_init);159module_exit(bl_trig_exit);160161MODULE_AUTHOR("Rodolfo Giometti <[email protected]>");162MODULE_DESCRIPTION("Backlight emulation LED trigger");163MODULE_LICENSE("GPL v2");164165166