ledtrig-gpio.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. */
  11. #include <linux/module.h>
  12. #include <linux/kernel.h>
  13. #include <linux/init.h>
  14. #include <linux/gpio.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/workqueue.h>
  17. #include <linux/leds.h>
  18. #include "leds.h"
  19. struct gpio_trig_data {
  20. struct led_classdev *led;
  21. struct work_struct work;
  22. unsigned desired_brightness; /* desired brightness when led is on */
  23. unsigned inverted; /* true when gpio is inverted */
  24. unsigned gpio; /* gpio that triggers the leds */
  25. };
  26. static irqreturn_t gpio_trig_irq(int irq, void *_led)
  27. {
  28. struct led_classdev *led = _led;
  29. struct gpio_trig_data *gpio_data = led->trigger_data;
  30. /* just schedule_work since gpio_get_value can sleep */
  31. schedule_work(&gpio_data->work);
  32. return IRQ_HANDLED;
  33. };
  34. static void gpio_trig_work(struct work_struct *work)
  35. {
  36. struct gpio_trig_data *gpio_data = container_of(work,
  37. struct gpio_trig_data, work);
  38. int tmp;
  39. if (!gpio_data->gpio)
  40. return;
  41. tmp = gpio_get_value(gpio_data->gpio);
  42. if (gpio_data->inverted)
  43. tmp = !tmp;
  44. if (tmp) {
  45. if (gpio_data->desired_brightness)
  46. led_set_brightness(gpio_data->led,
  47. gpio_data->desired_brightness);
  48. else
  49. led_set_brightness(gpio_data->led, LED_FULL);
  50. } else {
  51. led_set_brightness(gpio_data->led, LED_OFF);
  52. }
  53. }
  54. static ssize_t gpio_trig_brightness_show(struct device *dev,
  55. struct device_attribute *attr, char *buf)
  56. {
  57. struct led_classdev *led = dev_get_drvdata(dev);
  58. struct gpio_trig_data *gpio_data = led->trigger_data;
  59. return sprintf(buf, "%u\n", gpio_data->desired_brightness);
  60. }
  61. static ssize_t gpio_trig_brightness_store(struct device *dev,
  62. struct device_attribute *attr, const char *buf, size_t n)
  63. {
  64. struct led_classdev *led = dev_get_drvdata(dev);
  65. struct gpio_trig_data *gpio_data = led->trigger_data;
  66. unsigned desired_brightness;
  67. int ret;
  68. ret = sscanf(buf, "%u", &desired_brightness);
  69. if (ret < 1 || desired_brightness > 255) {
  70. dev_err(dev, "invalid value\n");
  71. return -EINVAL;
  72. }
  73. gpio_data->desired_brightness = desired_brightness;
  74. return n;
  75. }
  76. static DEVICE_ATTR(desired_brightness, 0644, gpio_trig_brightness_show,
  77. gpio_trig_brightness_store);
  78. static ssize_t gpio_trig_inverted_show(struct device *dev,
  79. struct device_attribute *attr, char *buf)
  80. {
  81. struct led_classdev *led = dev_get_drvdata(dev);
  82. struct gpio_trig_data *gpio_data = led->trigger_data;
  83. return sprintf(buf, "%s\n", gpio_data->inverted ? "yes" : "no");
  84. }
  85. static ssize_t gpio_trig_inverted_store(struct device *dev,
  86. struct device_attribute *attr, const char *buf, size_t n)
  87. {
  88. struct led_classdev *led = dev_get_drvdata(dev);
  89. struct gpio_trig_data *gpio_data = led->trigger_data;
  90. unsigned inverted;
  91. int ret;
  92. ret = sscanf(buf, "%u", &inverted);
  93. if (ret < 1) {
  94. dev_err(dev, "invalid value\n");
  95. return -EINVAL;
  96. }
  97. gpio_data->inverted = !!inverted;
  98. return n;
  99. }
  100. static DEVICE_ATTR(inverted, 0644, gpio_trig_inverted_show,
  101. gpio_trig_inverted_store);
  102. static ssize_t gpio_trig_gpio_show(struct device *dev,
  103. struct device_attribute *attr, char *buf)
  104. {
  105. struct led_classdev *led = dev_get_drvdata(dev);
  106. struct gpio_trig_data *gpio_data = led->trigger_data;
  107. return sprintf(buf, "%u\n", gpio_data->gpio);
  108. }
  109. static ssize_t gpio_trig_gpio_store(struct device *dev,
  110. struct device_attribute *attr, const char *buf, size_t n)
  111. {
  112. struct led_classdev *led = dev_get_drvdata(dev);
  113. struct gpio_trig_data *gpio_data = led->trigger_data;
  114. unsigned gpio;
  115. int ret;
  116. ret = sscanf(buf, "%u", &gpio);
  117. if (ret < 1) {
  118. dev_err(dev, "couldn't read gpio number\n");
  119. flush_work(&gpio_data->work);
  120. return -EINVAL;
  121. }
  122. if (gpio_data->gpio == gpio)
  123. return n;
  124. if (!gpio) {
  125. if (gpio_data->gpio != 0)
  126. free_irq(gpio_to_irq(gpio_data->gpio), led);
  127. gpio_data->gpio = 0;
  128. return n;
  129. }
  130. ret = request_irq(gpio_to_irq(gpio), gpio_trig_irq,
  131. IRQF_SHARED | IRQF_TRIGGER_RISING
  132. | IRQF_TRIGGER_FALLING, "ledtrig-gpio", led);
  133. if (ret) {
  134. dev_err(dev, "request_irq failed with error %d\n", ret);
  135. } else {
  136. if (gpio_data->gpio != 0)
  137. free_irq(gpio_to_irq(gpio_data->gpio), led);
  138. gpio_data->gpio = gpio;
  139. }
  140. return ret ? ret : n;
  141. }
  142. static DEVICE_ATTR(gpio, 0644, gpio_trig_gpio_show, gpio_trig_gpio_store);
  143. static void gpio_trig_activate(struct led_classdev *led)
  144. {
  145. struct gpio_trig_data *gpio_data;
  146. int ret;
  147. gpio_data = kzalloc(sizeof(*gpio_data), GFP_KERNEL);
  148. if (!gpio_data)
  149. return;
  150. ret = device_create_file(led->dev, &dev_attr_gpio);
  151. if (ret)
  152. goto err_gpio;
  153. ret = device_create_file(led->dev, &dev_attr_inverted);
  154. if (ret)
  155. goto err_inverted;
  156. ret = device_create_file(led->dev, &dev_attr_desired_brightness);
  157. if (ret)
  158. goto err_brightness;
  159. gpio_data->led = led;
  160. led->trigger_data = gpio_data;
  161. INIT_WORK(&gpio_data->work, gpio_trig_work);
  162. return;
  163. err_brightness:
  164. device_remove_file(led->dev, &dev_attr_inverted);
  165. err_inverted:
  166. device_remove_file(led->dev, &dev_attr_gpio);
  167. err_gpio:
  168. kfree(gpio_data);
  169. }
  170. static void gpio_trig_deactivate(struct led_classdev *led)
  171. {
  172. struct gpio_trig_data *gpio_data = led->trigger_data;
  173. if (gpio_data) {
  174. device_remove_file(led->dev, &dev_attr_gpio);
  175. device_remove_file(led->dev, &dev_attr_inverted);
  176. device_remove_file(led->dev, &dev_attr_desired_brightness);
  177. flush_work(&gpio_data->work);
  178. if (gpio_data->gpio != 0)
  179. free_irq(gpio_to_irq(gpio_data->gpio), led);
  180. kfree(gpio_data);
  181. }
  182. }
  183. static struct led_trigger gpio_led_trigger = {
  184. .name = "gpio",
  185. .activate = gpio_trig_activate,
  186. .deactivate = gpio_trig_deactivate,
  187. };
  188. static int __init gpio_trig_init(void)
  189. {
  190. return led_trigger_register(&gpio_led_trigger);
  191. }
  192. module_init(gpio_trig_init);
  193. static void __exit gpio_trig_exit(void)
  194. {
  195. led_trigger_unregister(&gpio_led_trigger);
  196. }
  197. module_exit(gpio_trig_exit);
  198. MODULE_AUTHOR("Felipe Balbi <me@felipebalbi.com>");
  199. MODULE_DESCRIPTION("GPIO LED trigger");
  200. MODULE_LICENSE("GPL");