leds-gpio.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. * LEDs driver for GPIOs
  3. *
  4. * Copyright (C) 2007 8D Technologies inc.
  5. * Raphael Assenat <raph@8d.com>
  6. * Copyright (C) 2008 Freescale Semiconductor, Inc.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/init.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/leds.h>
  17. #include <linux/of_platform.h>
  18. #include <linux/of_gpio.h>
  19. #include <linux/slab.h>
  20. #include <linux/workqueue.h>
  21. #include <asm/gpio.h>
  22. struct gpio_led_data {
  23. struct led_classdev cdev;
  24. unsigned gpio;
  25. struct work_struct work;
  26. u8 new_level;
  27. u8 can_sleep;
  28. u8 active_low;
  29. u8 blinking;
  30. int (*platform_gpio_blink_set)(unsigned gpio, int state,
  31. unsigned long *delay_on, unsigned long *delay_off);
  32. };
  33. static void gpio_led_work(struct work_struct *work)
  34. {
  35. struct gpio_led_data *led_dat =
  36. container_of(work, struct gpio_led_data, work);
  37. if (led_dat->blinking) {
  38. led_dat->platform_gpio_blink_set(led_dat->gpio,
  39. led_dat->new_level,
  40. NULL, NULL);
  41. led_dat->blinking = 0;
  42. } else
  43. gpio_set_value_cansleep(led_dat->gpio, led_dat->new_level);
  44. }
  45. static void gpio_led_set(struct led_classdev *led_cdev,
  46. enum led_brightness value)
  47. {
  48. struct gpio_led_data *led_dat =
  49. container_of(led_cdev, struct gpio_led_data, cdev);
  50. int level;
  51. if (value == LED_OFF)
  52. level = 0;
  53. else
  54. level = 1;
  55. if (led_dat->active_low)
  56. level = !level;
  57. /* Setting GPIOs with I2C/etc requires a task context, and we don't
  58. * seem to have a reliable way to know if we're already in one; so
  59. * let's just assume the worst.
  60. */
  61. if (led_dat->can_sleep) {
  62. led_dat->new_level = level;
  63. schedule_work(&led_dat->work);
  64. } else {
  65. if (led_dat->blinking) {
  66. led_dat->platform_gpio_blink_set(led_dat->gpio, level,
  67. NULL, NULL);
  68. led_dat->blinking = 0;
  69. } else
  70. gpio_set_value(led_dat->gpio, level);
  71. }
  72. }
  73. static int gpio_blink_set(struct led_classdev *led_cdev,
  74. unsigned long *delay_on, unsigned long *delay_off)
  75. {
  76. struct gpio_led_data *led_dat =
  77. container_of(led_cdev, struct gpio_led_data, cdev);
  78. led_dat->blinking = 1;
  79. return led_dat->platform_gpio_blink_set(led_dat->gpio, GPIO_LED_BLINK,
  80. delay_on, delay_off);
  81. }
  82. static int __devinit create_gpio_led(const struct gpio_led *template,
  83. struct gpio_led_data *led_dat, struct device *parent,
  84. int (*blink_set)(unsigned, int, unsigned long *, unsigned long *))
  85. {
  86. int ret, state;
  87. led_dat->gpio = -1;
  88. /* skip leds that aren't available */
  89. if (!gpio_is_valid(template->gpio)) {
  90. printk(KERN_INFO "Skipping unavailable LED gpio %d (%s)\n",
  91. template->gpio, template->name);
  92. return 0;
  93. }
  94. ret = gpio_request(template->gpio, template->name);
  95. if (ret < 0)
  96. return ret;
  97. led_dat->cdev.name = template->name;
  98. led_dat->cdev.default_trigger = template->default_trigger;
  99. led_dat->gpio = template->gpio;
  100. led_dat->can_sleep = gpio_cansleep(template->gpio);
  101. led_dat->active_low = template->active_low;
  102. led_dat->blinking = 0;
  103. if (blink_set) {
  104. led_dat->platform_gpio_blink_set = blink_set;
  105. led_dat->cdev.blink_set = gpio_blink_set;
  106. }
  107. led_dat->cdev.brightness_set = gpio_led_set;
  108. if (template->default_state == LEDS_GPIO_DEFSTATE_KEEP)
  109. state = !!gpio_get_value(led_dat->gpio) ^ led_dat->active_low;
  110. else
  111. state = (template->default_state == LEDS_GPIO_DEFSTATE_ON);
  112. led_dat->cdev.brightness = state ? LED_FULL : LED_OFF;
  113. if (!template->retain_state_suspended)
  114. led_dat->cdev.flags |= LED_CORE_SUSPENDRESUME;
  115. ret = gpio_direction_output(led_dat->gpio, led_dat->active_low ^ state);
  116. if (ret < 0)
  117. goto err;
  118. INIT_WORK(&led_dat->work, gpio_led_work);
  119. ret = led_classdev_register(parent, &led_dat->cdev);
  120. if (ret < 0)
  121. goto err;
  122. return 0;
  123. err:
  124. gpio_free(led_dat->gpio);
  125. return ret;
  126. }
  127. static void delete_gpio_led(struct gpio_led_data *led)
  128. {
  129. if (!gpio_is_valid(led->gpio))
  130. return;
  131. led_classdev_unregister(&led->cdev);
  132. cancel_work_sync(&led->work);
  133. gpio_free(led->gpio);
  134. }
  135. struct gpio_leds_priv {
  136. int num_leds;
  137. struct gpio_led_data leds[];
  138. };
  139. static inline int sizeof_gpio_leds_priv(int num_leds)
  140. {
  141. return sizeof(struct gpio_leds_priv) +
  142. (sizeof(struct gpio_led_data) * num_leds);
  143. }
  144. /* Code to create from OpenFirmware platform devices */
  145. #ifdef CONFIG_LEDS_GPIO_OF
  146. static struct gpio_leds_priv * __devinit gpio_leds_create_of(struct platform_device *pdev)
  147. {
  148. struct device_node *np = pdev->dev.of_node, *child;
  149. struct gpio_leds_priv *priv;
  150. int count = 0, ret;
  151. /* count LEDs in this device, so we know how much to allocate */
  152. for_each_child_of_node(np, child)
  153. count++;
  154. if (!count)
  155. return NULL;
  156. priv = kzalloc(sizeof_gpio_leds_priv(count), GFP_KERNEL);
  157. if (!priv)
  158. return NULL;
  159. for_each_child_of_node(np, child) {
  160. struct gpio_led led = {};
  161. enum of_gpio_flags flags;
  162. const char *state;
  163. led.gpio = of_get_gpio_flags(child, 0, &flags);
  164. led.active_low = flags & OF_GPIO_ACTIVE_LOW;
  165. led.name = of_get_property(child, "label", NULL) ? : child->name;
  166. led.default_trigger =
  167. of_get_property(child, "linux,default-trigger", NULL);
  168. state = of_get_property(child, "default-state", NULL);
  169. if (state) {
  170. if (!strcmp(state, "keep"))
  171. led.default_state = LEDS_GPIO_DEFSTATE_KEEP;
  172. else if (!strcmp(state, "on"))
  173. led.default_state = LEDS_GPIO_DEFSTATE_ON;
  174. else
  175. led.default_state = LEDS_GPIO_DEFSTATE_OFF;
  176. }
  177. ret = create_gpio_led(&led, &priv->leds[priv->num_leds++],
  178. &pdev->dev, NULL);
  179. if (ret < 0) {
  180. of_node_put(child);
  181. goto err;
  182. }
  183. }
  184. return priv;
  185. err:
  186. for (count = priv->num_leds - 2; count >= 0; count--)
  187. delete_gpio_led(&priv->leds[count]);
  188. kfree(priv);
  189. return NULL;
  190. }
  191. static const struct of_device_id of_gpio_leds_match[] = {
  192. { .compatible = "gpio-leds", },
  193. {},
  194. };
  195. #else
  196. static struct gpio_leds_priv * __devinit gpio_leds_create_of(struct platform_device *pdev)
  197. {
  198. return NULL;
  199. }
  200. #define of_gpio_leds_match NULL
  201. #endif
  202. static int __devinit gpio_led_probe(struct platform_device *pdev)
  203. {
  204. struct gpio_led_platform_data *pdata = pdev->dev.platform_data;
  205. struct gpio_leds_priv *priv;
  206. int i, ret = 0;
  207. if (pdata && pdata->num_leds) {
  208. priv = kzalloc(sizeof_gpio_leds_priv(pdata->num_leds),
  209. GFP_KERNEL);
  210. if (!priv)
  211. return -ENOMEM;
  212. priv->num_leds = pdata->num_leds;
  213. for (i = 0; i < priv->num_leds; i++) {
  214. ret = create_gpio_led(&pdata->leds[i],
  215. &priv->leds[i],
  216. &pdev->dev, pdata->gpio_blink_set);
  217. if (ret < 0) {
  218. /* On failure: unwind the led creations */
  219. for (i = i - 1; i >= 0; i--)
  220. delete_gpio_led(&priv->leds[i]);
  221. kfree(priv);
  222. return ret;
  223. }
  224. }
  225. } else {
  226. priv = gpio_leds_create_of(pdev);
  227. if (!priv)
  228. return -ENODEV;
  229. }
  230. platform_set_drvdata(pdev, priv);
  231. return 0;
  232. }
  233. static int __devexit gpio_led_remove(struct platform_device *pdev)
  234. {
  235. struct gpio_leds_priv *priv = dev_get_drvdata(&pdev->dev);
  236. int i;
  237. for (i = 0; i < priv->num_leds; i++)
  238. delete_gpio_led(&priv->leds[i]);
  239. dev_set_drvdata(&pdev->dev, NULL);
  240. kfree(priv);
  241. return 0;
  242. }
  243. static struct platform_driver gpio_led_driver = {
  244. .probe = gpio_led_probe,
  245. .remove = __devexit_p(gpio_led_remove),
  246. .driver = {
  247. .name = "leds-gpio",
  248. .owner = THIS_MODULE,
  249. .of_match_table = of_gpio_leds_match,
  250. },
  251. };
  252. MODULE_ALIAS("platform:leds-gpio");
  253. static int __init gpio_led_init(void)
  254. {
  255. return platform_driver_register(&gpio_led_driver);
  256. }
  257. static void __exit gpio_led_exit(void)
  258. {
  259. platform_driver_unregister(&gpio_led_driver);
  260. }
  261. module_init(gpio_led_init);
  262. module_exit(gpio_led_exit);
  263. MODULE_AUTHOR("Raphael Assenat <raph@8d.com>, Trent Piepho <tpiepho@freescale.com>");
  264. MODULE_DESCRIPTION("GPIO LED driver");
  265. MODULE_LICENSE("GPL");