ledtrig-backlight.c 3.6 KB

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