ledtrig-backlight.c 2.4 KB

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