ledtrig-gpio.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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) {
  123. free_irq(gpio_to_irq(gpio_data->gpio), led);
  124. return n;
  125. }
  126. if (gpio_data->gpio > 0 && gpio_data->gpio != gpio)
  127. free_irq(gpio_to_irq(gpio_data->gpio), led);
  128. gpio_data->gpio = gpio;
  129. ret = request_irq(gpio_to_irq(gpio), gpio_trig_irq,
  130. IRQF_SHARED | IRQF_TRIGGER_RISING
  131. | IRQF_TRIGGER_FALLING, "ledtrig-gpio", led);
  132. if (ret)
  133. dev_err(dev, "request_irq failed with error %d\n", ret);
  134. return ret ? ret : n;
  135. }
  136. static DEVICE_ATTR(gpio, 0644, gpio_trig_gpio_show, gpio_trig_gpio_store);
  137. static void gpio_trig_activate(struct led_classdev *led)
  138. {
  139. struct gpio_trig_data *gpio_data;
  140. int ret;
  141. gpio_data = kzalloc(sizeof(*gpio_data), GFP_KERNEL);
  142. if (!gpio_data)
  143. return;
  144. ret = device_create_file(led->dev, &dev_attr_gpio);
  145. if (ret)
  146. goto err_gpio;
  147. ret = device_create_file(led->dev, &dev_attr_inverted);
  148. if (ret)
  149. goto err_inverted;
  150. ret = device_create_file(led->dev, &dev_attr_desired_brightness);
  151. if (ret)
  152. goto err_brightness;
  153. gpio_data->led = led;
  154. led->trigger_data = gpio_data;
  155. INIT_WORK(&gpio_data->work, gpio_trig_work);
  156. return;
  157. err_brightness:
  158. device_remove_file(led->dev, &dev_attr_inverted);
  159. err_inverted:
  160. device_remove_file(led->dev, &dev_attr_gpio);
  161. err_gpio:
  162. kfree(gpio_data);
  163. }
  164. static void gpio_trig_deactivate(struct led_classdev *led)
  165. {
  166. struct gpio_trig_data *gpio_data = led->trigger_data;
  167. if (gpio_data) {
  168. device_remove_file(led->dev, &dev_attr_gpio);
  169. device_remove_file(led->dev, &dev_attr_inverted);
  170. device_remove_file(led->dev, &dev_attr_desired_brightness);
  171. flush_work(&gpio_data->work);
  172. free_irq(gpio_to_irq(gpio_data->gpio),led);
  173. kfree(gpio_data);
  174. }
  175. }
  176. static struct led_trigger gpio_led_trigger = {
  177. .name = "gpio",
  178. .activate = gpio_trig_activate,
  179. .deactivate = gpio_trig_deactivate,
  180. };
  181. static int __init gpio_trig_init(void)
  182. {
  183. return led_trigger_register(&gpio_led_trigger);
  184. }
  185. module_init(gpio_trig_init);
  186. static void __exit gpio_trig_exit(void)
  187. {
  188. led_trigger_unregister(&gpio_led_trigger);
  189. }
  190. module_exit(gpio_trig_exit);
  191. MODULE_AUTHOR("Felipe Balbi <me@felipebalbi.com>");
  192. MODULE_DESCRIPTION("GPIO LED trigger");
  193. MODULE_LICENSE("GPL");