ledtrig-backlight.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Backlight emulation LED trigger
  4. *
  5. * Copyright 2008 (C) Rodolfo Giometti <[email protected]>
  6. * Copyright 2008 (C) Eurotech S.p.A. <[email protected]>
  7. */
  8. #include <linux/module.h>
  9. #include <linux/kernel.h>
  10. #include <linux/slab.h>
  11. #include <linux/init.h>
  12. #include <linux/fb.h>
  13. #include <linux/leds.h>
  14. #include "../leds.h"
  15. #define BLANK 1
  16. #define UNBLANK 0
  17. struct bl_trig_notifier {
  18. struct led_classdev *led;
  19. int brightness;
  20. int old_status;
  21. struct notifier_block notifier;
  22. unsigned invert;
  23. };
  24. static int fb_notifier_callback(struct notifier_block *p,
  25. unsigned long event, void *data)
  26. {
  27. struct bl_trig_notifier *n = container_of(p,
  28. struct bl_trig_notifier, notifier);
  29. struct led_classdev *led = n->led;
  30. struct fb_event *fb_event = data;
  31. int *blank;
  32. int new_status;
  33. /* If we aren't interested in this event, skip it immediately ... */
  34. if (event != FB_EVENT_BLANK)
  35. return 0;
  36. blank = fb_event->data;
  37. new_status = *blank ? BLANK : UNBLANK;
  38. if (new_status == n->old_status)
  39. return 0;
  40. if ((n->old_status == UNBLANK) ^ n->invert) {
  41. n->brightness = led->brightness;
  42. led_set_brightness_nosleep(led, LED_OFF);
  43. } else {
  44. led_set_brightness_nosleep(led, n->brightness);
  45. }
  46. n->old_status = new_status;
  47. return 0;
  48. }
  49. static ssize_t bl_trig_invert_show(struct device *dev,
  50. struct device_attribute *attr, char *buf)
  51. {
  52. struct bl_trig_notifier *n = led_trigger_get_drvdata(dev);
  53. return sprintf(buf, "%u\n", n->invert);
  54. }
  55. static ssize_t bl_trig_invert_store(struct device *dev,
  56. struct device_attribute *attr, const char *buf, size_t num)
  57. {
  58. struct led_classdev *led = led_trigger_get_led(dev);
  59. struct bl_trig_notifier *n = led_trigger_get_drvdata(dev);
  60. unsigned long invert;
  61. int ret;
  62. ret = kstrtoul(buf, 10, &invert);
  63. if (ret < 0)
  64. return ret;
  65. if (invert > 1)
  66. return -EINVAL;
  67. n->invert = invert;
  68. /* After inverting, we need to update the LED. */
  69. if ((n->old_status == BLANK) ^ n->invert)
  70. led_set_brightness_nosleep(led, LED_OFF);
  71. else
  72. led_set_brightness_nosleep(led, n->brightness);
  73. return num;
  74. }
  75. static DEVICE_ATTR(inverted, 0644, bl_trig_invert_show, bl_trig_invert_store);
  76. static struct attribute *bl_trig_attrs[] = {
  77. &dev_attr_inverted.attr,
  78. NULL,
  79. };
  80. ATTRIBUTE_GROUPS(bl_trig);
  81. static int 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. if (!n)
  87. return -ENOMEM;
  88. led_set_trigger_data(led, n);
  89. n->led = led;
  90. n->brightness = led->brightness;
  91. n->old_status = UNBLANK;
  92. n->notifier.notifier_call = fb_notifier_callback;
  93. ret = fb_register_client(&n->notifier);
  94. if (ret)
  95. dev_err(led->dev, "unable to register backlight trigger\n");
  96. return 0;
  97. }
  98. static void bl_trig_deactivate(struct led_classdev *led)
  99. {
  100. struct bl_trig_notifier *n = led_get_trigger_data(led);
  101. fb_unregister_client(&n->notifier);
  102. kfree(n);
  103. }
  104. static struct led_trigger bl_led_trigger = {
  105. .name = "backlight",
  106. .activate = bl_trig_activate,
  107. .deactivate = bl_trig_deactivate,
  108. .groups = bl_trig_groups,
  109. };
  110. module_led_trigger(bl_led_trigger);
  111. MODULE_AUTHOR("Rodolfo Giometti <[email protected]>");
  112. MODULE_DESCRIPTION("Backlight emulation LED trigger");
  113. MODULE_LICENSE("GPL v2");