ledtrig-gpio.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * ledtrig-gio.c - LED Trigger Based on GPIO events
  4. *
  5. * Copyright 2009 Felipe Balbi <me@felipebalbi.com>
  6. * Copyright 2023 Linus Walleij <linus.walleij@linaro.org>
  7. */
  8. #include <linux/module.h>
  9. #include <linux/kernel.h>
  10. #include <linux/init.h>
  11. #include <linux/gpio/consumer.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/leds.h>
  14. #include <linux/slab.h>
  15. #include "../leds.h"
  16. struct gpio_trig_data {
  17. struct led_classdev *led;
  18. unsigned desired_brightness; /* desired brightness when led is on */
  19. struct gpio_desc *gpiod; /* gpio that triggers the led */
  20. };
  21. static irqreturn_t gpio_trig_irq(int irq, void *_led)
  22. {
  23. struct led_classdev *led = _led;
  24. struct gpio_trig_data *gpio_data = led_get_trigger_data(led);
  25. int tmp;
  26. tmp = gpiod_get_value_cansleep(gpio_data->gpiod);
  27. if (tmp) {
  28. if (gpio_data->desired_brightness)
  29. led_set_brightness_nosleep(gpio_data->led,
  30. gpio_data->desired_brightness);
  31. else
  32. led_set_brightness_nosleep(gpio_data->led, LED_FULL);
  33. } else {
  34. led_set_brightness_nosleep(gpio_data->led, LED_OFF);
  35. }
  36. return IRQ_HANDLED;
  37. }
  38. static ssize_t desired_brightness_show(struct device *dev,
  39. struct device_attribute *attr, char *buf)
  40. {
  41. struct gpio_trig_data *gpio_data = led_trigger_get_drvdata(dev);
  42. return sysfs_emit(buf, "%u\n", gpio_data->desired_brightness);
  43. }
  44. static ssize_t desired_brightness_store(struct device *dev,
  45. struct device_attribute *attr, const char *buf, size_t n)
  46. {
  47. struct gpio_trig_data *gpio_data = led_trigger_get_drvdata(dev);
  48. u8 desired_brightness;
  49. int ret;
  50. ret = kstrtou8(buf, 10, &desired_brightness);
  51. if (ret)
  52. return ret;
  53. gpio_data->desired_brightness = desired_brightness;
  54. return n;
  55. }
  56. static DEVICE_ATTR_RW(desired_brightness);
  57. static struct attribute *gpio_trig_attrs[] = {
  58. &dev_attr_desired_brightness.attr,
  59. NULL
  60. };
  61. ATTRIBUTE_GROUPS(gpio_trig);
  62. static int gpio_trig_activate(struct led_classdev *led)
  63. {
  64. struct gpio_trig_data *gpio_data;
  65. struct device *dev = led->dev;
  66. int ret;
  67. gpio_data = kzalloc(sizeof(*gpio_data), GFP_KERNEL);
  68. if (!gpio_data)
  69. return -ENOMEM;
  70. /*
  71. * The generic property "trigger-sources" is followed,
  72. * and we hope that this is a GPIO.
  73. */
  74. gpio_data->gpiod = gpiod_get_optional(dev, "trigger-sources", GPIOD_IN);
  75. if (IS_ERR(gpio_data->gpiod)) {
  76. ret = PTR_ERR(gpio_data->gpiod);
  77. kfree(gpio_data);
  78. return ret;
  79. }
  80. if (!gpio_data->gpiod) {
  81. dev_err(dev, "no valid GPIO for the trigger\n");
  82. kfree(gpio_data);
  83. return -EINVAL;
  84. }
  85. gpiod_set_consumer_name(gpio_data->gpiod, "led-trigger");
  86. gpio_data->led = led;
  87. led_set_trigger_data(led, gpio_data);
  88. ret = request_threaded_irq(gpiod_to_irq(gpio_data->gpiod), NULL, gpio_trig_irq,
  89. IRQF_ONESHOT | IRQF_SHARED | IRQF_TRIGGER_RISING
  90. | IRQF_TRIGGER_FALLING, "ledtrig-gpio", led);
  91. if (ret) {
  92. dev_err(dev, "request_irq failed with error %d\n", ret);
  93. gpiod_put(gpio_data->gpiod);
  94. kfree(gpio_data);
  95. return ret;
  96. }
  97. /* Finally update the LED to initial status */
  98. gpio_trig_irq(0, led);
  99. return 0;
  100. }
  101. static void gpio_trig_deactivate(struct led_classdev *led)
  102. {
  103. struct gpio_trig_data *gpio_data = led_get_trigger_data(led);
  104. free_irq(gpiod_to_irq(gpio_data->gpiod), led);
  105. gpiod_put(gpio_data->gpiod);
  106. kfree(gpio_data);
  107. }
  108. static struct led_trigger gpio_led_trigger = {
  109. .name = "gpio",
  110. .activate = gpio_trig_activate,
  111. .deactivate = gpio_trig_deactivate,
  112. .groups = gpio_trig_groups,
  113. };
  114. module_led_trigger(gpio_led_trigger);
  115. MODULE_AUTHOR("Felipe Balbi <me@felipebalbi.com>");
  116. MODULE_DESCRIPTION("GPIO LED trigger");
  117. MODULE_LICENSE("GPL v2");