ledtrig-backlight.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Backlight emulation LED trigger
  4. *
  5. * Copyright 2008 (C) Rodolfo Giometti <giometti@linux.it>
  6. * Copyright 2008 (C) Eurotech S.p.A. <info@eurotech.it>
  7. */
  8. #include <linux/module.h>
  9. #include <linux/kernel.h>
  10. #include <linux/slab.h>
  11. #include <linux/init.h>
  12. #include <linux/leds.h>
  13. #include "../leds.h"
  14. #define BLANK 1
  15. #define UNBLANK 0
  16. struct bl_trig_notifier {
  17. struct led_classdev *led;
  18. int brightness;
  19. int old_status;
  20. unsigned invert;
  21. struct list_head entry;
  22. };
  23. static DEFINE_MUTEX(ledtrig_backlight_list_mutex);
  24. static LIST_HEAD(ledtrig_backlight_list);
  25. static void ledtrig_backlight_notify_blank(struct bl_trig_notifier *n, int new_status)
  26. {
  27. struct led_classdev *led = n->led;
  28. if (new_status == n->old_status)
  29. return;
  30. if ((n->old_status == UNBLANK) ^ n->invert) {
  31. n->brightness = led->brightness;
  32. led_set_brightness_nosleep(led, LED_OFF);
  33. } else {
  34. led_set_brightness_nosleep(led, n->brightness);
  35. }
  36. n->old_status = new_status;
  37. }
  38. void ledtrig_backlight_blank(bool blank)
  39. {
  40. struct bl_trig_notifier *n;
  41. int new_status = blank ? BLANK : UNBLANK;
  42. guard(mutex)(&ledtrig_backlight_list_mutex);
  43. list_for_each_entry(n, &ledtrig_backlight_list, entry)
  44. ledtrig_backlight_notify_blank(n, new_status);
  45. }
  46. EXPORT_SYMBOL(ledtrig_backlight_blank);
  47. static ssize_t bl_trig_invert_show(struct device *dev,
  48. struct device_attribute *attr, char *buf)
  49. {
  50. struct bl_trig_notifier *n = led_trigger_get_drvdata(dev);
  51. return sprintf(buf, "%u\n", n->invert);
  52. }
  53. static ssize_t bl_trig_invert_store(struct device *dev,
  54. struct device_attribute *attr, const char *buf, size_t num)
  55. {
  56. struct led_classdev *led = led_trigger_get_led(dev);
  57. struct bl_trig_notifier *n = led_trigger_get_drvdata(dev);
  58. unsigned long invert;
  59. int ret;
  60. ret = kstrtoul(buf, 10, &invert);
  61. if (ret < 0)
  62. return ret;
  63. if (invert > 1)
  64. return -EINVAL;
  65. n->invert = invert;
  66. /* After inverting, we need to update the LED. */
  67. if ((n->old_status == BLANK) ^ n->invert)
  68. led_set_brightness_nosleep(led, LED_OFF);
  69. else
  70. led_set_brightness_nosleep(led, n->brightness);
  71. return num;
  72. }
  73. static DEVICE_ATTR(inverted, 0644, bl_trig_invert_show, bl_trig_invert_store);
  74. static struct attribute *bl_trig_attrs[] = {
  75. &dev_attr_inverted.attr,
  76. NULL,
  77. };
  78. ATTRIBUTE_GROUPS(bl_trig);
  79. static int bl_trig_activate(struct led_classdev *led)
  80. {
  81. struct bl_trig_notifier *n;
  82. n = kzalloc_obj(struct bl_trig_notifier);
  83. if (!n)
  84. return -ENOMEM;
  85. led_set_trigger_data(led, n);
  86. n->led = led;
  87. n->brightness = led->brightness;
  88. n->old_status = UNBLANK;
  89. guard(mutex)(&ledtrig_backlight_list_mutex);
  90. list_add(&n->entry, &ledtrig_backlight_list);
  91. return 0;
  92. }
  93. static void bl_trig_deactivate(struct led_classdev *led)
  94. {
  95. struct bl_trig_notifier *n = led_get_trigger_data(led);
  96. guard(mutex)(&ledtrig_backlight_list_mutex);
  97. list_del(&n->entry);
  98. kfree(n);
  99. }
  100. static struct led_trigger bl_led_trigger = {
  101. .name = "backlight",
  102. .activate = bl_trig_activate,
  103. .deactivate = bl_trig_deactivate,
  104. .groups = bl_trig_groups,
  105. };
  106. module_led_trigger(bl_led_trigger);
  107. MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
  108. MODULE_DESCRIPTION("Backlight emulation LED trigger");
  109. MODULE_LICENSE("GPL v2");