ledtrig-gpio.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * ledtrig-gio.c - LED Trigger Based on GPIO events
  3. *
  4. * Copyright 2009 Felipe Balbi <me@felipebalbi.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/kernel.h>
  12. #include <linux/init.h>
  13. #include <linux/gpio.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/leds.h>
  16. #include <linux/slab.h>
  17. #include "../leds.h"
  18. struct gpio_trig_data {
  19. struct led_classdev *led;
  20. unsigned desired_brightness; /* desired brightness when led is on */
  21. unsigned inverted; /* true when gpio is inverted */
  22. unsigned gpio; /* gpio that triggers the leds */
  23. };
  24. static irqreturn_t gpio_trig_irq(int irq, void *_led)
  25. {
  26. struct led_classdev *led = _led;
  27. struct gpio_trig_data *gpio_data = led_get_trigger_data(led);
  28. int tmp;
  29. tmp = gpio_get_value_cansleep(gpio_data->gpio);
  30. if (gpio_data->inverted)
  31. tmp = !tmp;
  32. if (tmp) {
  33. if (gpio_data->desired_brightness)
  34. led_set_brightness_nosleep(gpio_data->led,
  35. gpio_data->desired_brightness);
  36. else
  37. led_set_brightness_nosleep(gpio_data->led, LED_FULL);
  38. } else {
  39. led_set_brightness_nosleep(gpio_data->led, LED_OFF);
  40. }
  41. return IRQ_HANDLED;
  42. }
  43. static ssize_t gpio_trig_brightness_show(struct device *dev,
  44. struct device_attribute *attr, char *buf)
  45. {
  46. struct gpio_trig_data *gpio_data = led_trigger_get_drvdata(dev);
  47. return sprintf(buf, "%u\n", gpio_data->desired_brightness);
  48. }
  49. static ssize_t gpio_trig_brightness_store(struct device *dev,
  50. struct device_attribute *attr, const char *buf, size_t n)
  51. {
  52. struct gpio_trig_data *gpio_data = led_trigger_get_drvdata(dev);
  53. unsigned desired_brightness;
  54. int ret;
  55. ret = sscanf(buf, "%u", &desired_brightness);
  56. if (ret < 1 || desired_brightness > 255) {
  57. dev_err(dev, "invalid value\n");
  58. return -EINVAL;
  59. }
  60. gpio_data->desired_brightness = desired_brightness;
  61. return n;
  62. }
  63. static DEVICE_ATTR(desired_brightness, 0644, gpio_trig_brightness_show,
  64. gpio_trig_brightness_store);
  65. static ssize_t gpio_trig_inverted_show(struct device *dev,
  66. struct device_attribute *attr, char *buf)
  67. {
  68. struct gpio_trig_data *gpio_data = led_trigger_get_drvdata(dev);
  69. return sprintf(buf, "%u\n", gpio_data->inverted);
  70. }
  71. static ssize_t gpio_trig_inverted_store(struct device *dev,
  72. struct device_attribute *attr, const char *buf, size_t n)
  73. {
  74. struct led_classdev *led = led_trigger_get_led(dev);
  75. struct gpio_trig_data *gpio_data = led_trigger_get_drvdata(dev);
  76. unsigned long inverted;
  77. int ret;
  78. ret = kstrtoul(buf, 10, &inverted);
  79. if (ret < 0)
  80. return ret;
  81. if (inverted > 1)
  82. return -EINVAL;
  83. gpio_data->inverted = inverted;
  84. /* After inverting, we need to update the LED. */
  85. gpio_trig_irq(0, led);
  86. return n;
  87. }
  88. static DEVICE_ATTR(inverted, 0644, gpio_trig_inverted_show,
  89. gpio_trig_inverted_store);
  90. static ssize_t gpio_trig_gpio_show(struct device *dev,
  91. struct device_attribute *attr, char *buf)
  92. {
  93. struct gpio_trig_data *gpio_data = led_trigger_get_drvdata(dev);
  94. return sprintf(buf, "%u\n", gpio_data->gpio);
  95. }
  96. static ssize_t gpio_trig_gpio_store(struct device *dev,
  97. struct device_attribute *attr, const char *buf, size_t n)
  98. {
  99. struct led_classdev *led = led_trigger_get_led(dev);
  100. struct gpio_trig_data *gpio_data = led_trigger_get_drvdata(dev);
  101. unsigned gpio;
  102. int ret;
  103. ret = sscanf(buf, "%u", &gpio);
  104. if (ret < 1) {
  105. dev_err(dev, "couldn't read gpio number\n");
  106. return -EINVAL;
  107. }
  108. if (gpio_data->gpio == gpio)
  109. return n;
  110. if (!gpio) {
  111. if (gpio_data->gpio != 0)
  112. free_irq(gpio_to_irq(gpio_data->gpio), led);
  113. gpio_data->gpio = 0;
  114. return n;
  115. }
  116. ret = request_threaded_irq(gpio_to_irq(gpio), NULL, gpio_trig_irq,
  117. IRQF_ONESHOT | IRQF_SHARED | IRQF_TRIGGER_RISING
  118. | IRQF_TRIGGER_FALLING, "ledtrig-gpio", led);
  119. if (ret) {
  120. dev_err(dev, "request_irq failed with error %d\n", ret);
  121. } else {
  122. if (gpio_data->gpio != 0)
  123. free_irq(gpio_to_irq(gpio_data->gpio), led);
  124. gpio_data->gpio = gpio;
  125. /* After changing the GPIO, we need to update the LED. */
  126. gpio_trig_irq(0, led);
  127. }
  128. return ret ? ret : n;
  129. }
  130. static DEVICE_ATTR(gpio, 0644, gpio_trig_gpio_show, gpio_trig_gpio_store);
  131. static struct attribute *gpio_trig_attrs[] = {
  132. &dev_attr_desired_brightness.attr,
  133. &dev_attr_inverted.attr,
  134. &dev_attr_gpio.attr,
  135. NULL
  136. };
  137. ATTRIBUTE_GROUPS(gpio_trig);
  138. static int gpio_trig_activate(struct led_classdev *led)
  139. {
  140. struct gpio_trig_data *gpio_data;
  141. gpio_data = kzalloc(sizeof(*gpio_data), GFP_KERNEL);
  142. if (!gpio_data)
  143. return -ENOMEM;
  144. gpio_data->led = led;
  145. led_set_trigger_data(led, gpio_data);
  146. return 0;
  147. }
  148. static void gpio_trig_deactivate(struct led_classdev *led)
  149. {
  150. struct gpio_trig_data *gpio_data = led_get_trigger_data(led);
  151. if (gpio_data->gpio != 0)
  152. free_irq(gpio_to_irq(gpio_data->gpio), led);
  153. kfree(gpio_data);
  154. }
  155. static struct led_trigger gpio_led_trigger = {
  156. .name = "gpio",
  157. .activate = gpio_trig_activate,
  158. .deactivate = gpio_trig_deactivate,
  159. .groups = gpio_trig_groups,
  160. };
  161. module_led_trigger(gpio_led_trigger);
  162. MODULE_AUTHOR("Felipe Balbi <me@felipebalbi.com>");
  163. MODULE_DESCRIPTION("GPIO LED trigger");
  164. MODULE_LICENSE("GPL v2");