ledtrig-gpio.c 6.1 KB

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