ledtrig-backlight.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. #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"
  19. #define BLANK 1
  20. #define UNBLANK 0
  21. struct bl_trig_notifier {
  22. struct led_classdev *led;
  23. int brightness;
  24. int old_status;
  25. struct notifier_block notifier;
  26. unsigned invert;
  27. };
  28. static 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;
  35. int *blank;
  36. int new_status;
  37. /* If we aren't interested in this event, skip it immediately ... */
  38. if (event != FB_EVENT_BLANK)
  39. return 0;
  40. blank = fb_event->data;
  41. new_status = *blank ? BLANK : UNBLANK;
  42. if (new_status == n->old_status)
  43. return 0;
  44. if ((n->old_status == UNBLANK) ^ n->invert) {
  45. n->brightness = led->brightness;
  46. led_set_brightness_nosleep(led, LED_OFF);
  47. } else {
  48. led_set_brightness_nosleep(led, n->brightness);
  49. }
  50. n->old_status = new_status;
  51. return 0;
  52. }
  53. static ssize_t bl_trig_invert_show(struct device *dev,
  54. struct device_attribute *attr, char *buf)
  55. {
  56. struct led_classdev *led = dev_get_drvdata(dev);
  57. struct bl_trig_notifier *n = led->trigger_data;
  58. return sprintf(buf, "%u\n", n->invert);
  59. }
  60. static ssize_t bl_trig_invert_store(struct device *dev,
  61. struct device_attribute *attr, const char *buf, size_t num)
  62. {
  63. struct led_classdev *led = dev_get_drvdata(dev);
  64. struct bl_trig_notifier *n = led->trigger_data;
  65. unsigned long invert;
  66. int ret;
  67. ret = kstrtoul(buf, 10, &invert);
  68. if (ret < 0)
  69. return ret;
  70. if (invert > 1)
  71. return -EINVAL;
  72. n->invert = invert;
  73. /* After inverting, we need to update the LED. */
  74. if ((n->old_status == BLANK) ^ n->invert)
  75. led_set_brightness_nosleep(led, LED_OFF);
  76. else
  77. led_set_brightness_nosleep(led, n->brightness);
  78. return num;
  79. }
  80. static DEVICE_ATTR(inverted, 0644, bl_trig_invert_show, bl_trig_invert_store);
  81. static void bl_trig_activate(struct led_classdev *led)
  82. {
  83. int ret;
  84. struct bl_trig_notifier *n;
  85. n = kzalloc(sizeof(struct bl_trig_notifier), GFP_KERNEL);
  86. led->trigger_data = n;
  87. if (!n) {
  88. dev_err(led->dev, "unable to allocate backlight trigger\n");
  89. return;
  90. }
  91. ret = device_create_file(led->dev, &dev_attr_inverted);
  92. if (ret)
  93. goto err_invert;
  94. n->led = led;
  95. n->brightness = led->brightness;
  96. n->old_status = UNBLANK;
  97. n->notifier.notifier_call = fb_notifier_callback;
  98. ret = fb_register_client(&n->notifier);
  99. if (ret)
  100. dev_err(led->dev, "unable to register backlight trigger\n");
  101. led->activated = true;
  102. return;
  103. err_invert:
  104. led->trigger_data = NULL;
  105. kfree(n);
  106. }
  107. static void bl_trig_deactivate(struct led_classdev *led)
  108. {
  109. struct bl_trig_notifier *n =
  110. (struct bl_trig_notifier *) led->trigger_data;
  111. if (led->activated) {
  112. device_remove_file(led->dev, &dev_attr_inverted);
  113. fb_unregister_client(&n->notifier);
  114. kfree(n);
  115. led->activated = false;
  116. }
  117. }
  118. static struct led_trigger bl_led_trigger = {
  119. .name = "backlight",
  120. .activate = bl_trig_activate,
  121. .deactivate = bl_trig_deactivate
  122. };
  123. static int __init bl_trig_init(void)
  124. {
  125. return led_trigger_register(&bl_led_trigger);
  126. }
  127. static void __exit bl_trig_exit(void)
  128. {
  129. led_trigger_unregister(&bl_led_trigger);
  130. }
  131. module_init(bl_trig_init);
  132. module_exit(bl_trig_exit);
  133. MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
  134. MODULE_DESCRIPTION("Backlight emulation LED trigger");
  135. MODULE_LICENSE("GPL v2");