ledtrig-backlight.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. };
  27. static int fb_notifier_callback(struct notifier_block *p,
  28. unsigned long event, void *data)
  29. {
  30. struct bl_trig_notifier *n = container_of(p,
  31. struct bl_trig_notifier, notifier);
  32. struct led_classdev *led = n->led;
  33. struct fb_event *fb_event = data;
  34. int *blank = fb_event->data;
  35. switch (event) {
  36. case FB_EVENT_BLANK :
  37. if (*blank && n->old_status == UNBLANK) {
  38. n->brightness = led->brightness;
  39. led_set_brightness(led, LED_OFF);
  40. n->old_status = BLANK;
  41. } else if (!*blank && n->old_status == BLANK) {
  42. led_set_brightness(led, n->brightness);
  43. n->old_status = UNBLANK;
  44. }
  45. break;
  46. }
  47. return 0;
  48. }
  49. static void bl_trig_activate(struct led_classdev *led)
  50. {
  51. int ret;
  52. struct bl_trig_notifier *n;
  53. n = kzalloc(sizeof(struct bl_trig_notifier), GFP_KERNEL);
  54. led->trigger_data = n;
  55. if (!n) {
  56. dev_err(led->dev, "unable to allocate backlight trigger\n");
  57. return;
  58. }
  59. n->led = led;
  60. n->brightness = led->brightness;
  61. n->old_status = UNBLANK;
  62. n->notifier.notifier_call = fb_notifier_callback;
  63. ret = fb_register_client(&n->notifier);
  64. if (ret)
  65. dev_err(led->dev, "unable to register backlight trigger\n");
  66. }
  67. static void bl_trig_deactivate(struct led_classdev *led)
  68. {
  69. struct bl_trig_notifier *n =
  70. (struct bl_trig_notifier *) led->trigger_data;
  71. if (n) {
  72. fb_unregister_client(&n->notifier);
  73. kfree(n);
  74. }
  75. }
  76. static struct led_trigger bl_led_trigger = {
  77. .name = "backlight",
  78. .activate = bl_trig_activate,
  79. .deactivate = bl_trig_deactivate
  80. };
  81. static int __init bl_trig_init(void)
  82. {
  83. return led_trigger_register(&bl_led_trigger);
  84. }
  85. static void __exit bl_trig_exit(void)
  86. {
  87. led_trigger_unregister(&bl_led_trigger);
  88. }
  89. module_init(bl_trig_init);
  90. module_exit(bl_trig_exit);
  91. MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
  92. MODULE_DESCRIPTION("Backlight emulation LED trigger");
  93. MODULE_LICENSE("GPL v2");