ledtrig-backlight.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 bl_trig_notifier *n = led_trigger_get_drvdata(dev);
  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 = led_trigger_get_led(dev);
  63. struct bl_trig_notifier *n = led_trigger_get_drvdata(dev);
  64. unsigned long invert;
  65. int ret;
  66. ret = kstrtoul(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_nosleep(led, LED_OFF);
  75. else
  76. led_set_brightness_nosleep(led, n->brightness);
  77. return num;
  78. }
  79. static DEVICE_ATTR(inverted, 0644, bl_trig_invert_show, bl_trig_invert_store);
  80. static struct attribute *bl_trig_attrs[] = {
  81. &dev_attr_inverted.attr,
  82. NULL,
  83. };
  84. ATTRIBUTE_GROUPS(bl_trig);
  85. static int bl_trig_activate(struct led_classdev *led)
  86. {
  87. int ret;
  88. struct bl_trig_notifier *n;
  89. n = kzalloc(sizeof(struct bl_trig_notifier), GFP_KERNEL);
  90. if (!n)
  91. return -ENOMEM;
  92. led_set_trigger_data(led, n);
  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 0;
  101. }
  102. static void bl_trig_deactivate(struct led_classdev *led)
  103. {
  104. struct bl_trig_notifier *n = led_get_trigger_data(led);
  105. fb_unregister_client(&n->notifier);
  106. kfree(n);
  107. }
  108. static struct led_trigger bl_led_trigger = {
  109. .name = "backlight",
  110. .activate = bl_trig_activate,
  111. .deactivate = bl_trig_deactivate,
  112. .groups = bl_trig_groups,
  113. };
  114. module_led_trigger(bl_led_trigger);
  115. MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
  116. MODULE_DESCRIPTION("Backlight emulation LED trigger");
  117. MODULE_LICENSE("GPL v2");