leds-gpio.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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/err.h>
  14. #include <linux/gpio.h>
  15. #include <linux/gpio/consumer.h>
  16. #include <linux/kernel.h>
  17. #include <linux/leds.h>
  18. #include <linux/module.h>
  19. #include <linux/of.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/property.h>
  22. #include <linux/slab.h>
  23. #include <linux/workqueue.h>
  24. struct gpio_led_data {
  25. struct led_classdev cdev;
  26. struct gpio_desc *gpiod;
  27. struct work_struct work;
  28. u8 new_level;
  29. u8 can_sleep;
  30. u8 blinking;
  31. int (*platform_gpio_blink_set)(struct gpio_desc *desc, int state,
  32. unsigned long *delay_on, unsigned long *delay_off);
  33. };
  34. static void gpio_led_work(struct work_struct *work)
  35. {
  36. struct gpio_led_data *led_dat =
  37. container_of(work, struct gpio_led_data, work);
  38. if (led_dat->blinking) {
  39. led_dat->platform_gpio_blink_set(led_dat->gpiod,
  40. led_dat->new_level, NULL, NULL);
  41. led_dat->blinking = 0;
  42. } else
  43. gpiod_set_value_cansleep(led_dat->gpiod, 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. /* Setting GPIOs with I2C/etc requires a task context, and we don't
  56. * seem to have a reliable way to know if we're already in one; so
  57. * let's just assume the worst.
  58. */
  59. if (led_dat->can_sleep) {
  60. led_dat->new_level = level;
  61. schedule_work(&led_dat->work);
  62. } else {
  63. if (led_dat->blinking) {
  64. led_dat->platform_gpio_blink_set(led_dat->gpiod, level,
  65. NULL, NULL);
  66. led_dat->blinking = 0;
  67. } else
  68. gpiod_set_value(led_dat->gpiod, level);
  69. }
  70. }
  71. static int gpio_blink_set(struct led_classdev *led_cdev,
  72. unsigned long *delay_on, unsigned long *delay_off)
  73. {
  74. struct gpio_led_data *led_dat =
  75. container_of(led_cdev, struct gpio_led_data, cdev);
  76. led_dat->blinking = 1;
  77. return led_dat->platform_gpio_blink_set(led_dat->gpiod, GPIO_LED_BLINK,
  78. delay_on, delay_off);
  79. }
  80. static int create_gpio_led(const struct gpio_led *template,
  81. struct gpio_led_data *led_dat, struct device *parent,
  82. int (*blink_set)(struct gpio_desc *, int, unsigned long *,
  83. unsigned long *))
  84. {
  85. int ret, state;
  86. led_dat->gpiod = template->gpiod;
  87. if (!led_dat->gpiod) {
  88. /*
  89. * This is the legacy code path for platform code that
  90. * still uses GPIO numbers. Ultimately we would like to get
  91. * rid of this block completely.
  92. */
  93. unsigned long flags = 0;
  94. /* skip leds that aren't available */
  95. if (!gpio_is_valid(template->gpio)) {
  96. dev_info(parent, "Skipping unavailable LED gpio %d (%s)\n",
  97. template->gpio, template->name);
  98. return 0;
  99. }
  100. if (template->active_low)
  101. flags |= GPIOF_ACTIVE_LOW;
  102. ret = devm_gpio_request_one(parent, template->gpio, flags,
  103. template->name);
  104. if (ret < 0)
  105. return ret;
  106. led_dat->gpiod = gpio_to_desc(template->gpio);
  107. if (!led_dat->gpiod)
  108. return -EINVAL;
  109. }
  110. led_dat->cdev.name = template->name;
  111. led_dat->cdev.default_trigger = template->default_trigger;
  112. led_dat->can_sleep = gpiod_cansleep(led_dat->gpiod);
  113. led_dat->blinking = 0;
  114. if (blink_set) {
  115. led_dat->platform_gpio_blink_set = blink_set;
  116. led_dat->cdev.blink_set = gpio_blink_set;
  117. }
  118. led_dat->cdev.brightness_set = gpio_led_set;
  119. if (template->default_state == LEDS_GPIO_DEFSTATE_KEEP)
  120. state = !!gpiod_get_value_cansleep(led_dat->gpiod);
  121. else
  122. state = (template->default_state == LEDS_GPIO_DEFSTATE_ON);
  123. led_dat->cdev.brightness = state ? LED_FULL : LED_OFF;
  124. if (!template->retain_state_suspended)
  125. led_dat->cdev.flags |= LED_CORE_SUSPENDRESUME;
  126. ret = gpiod_direction_output(led_dat->gpiod, state);
  127. if (ret < 0)
  128. return ret;
  129. INIT_WORK(&led_dat->work, gpio_led_work);
  130. return led_classdev_register(parent, &led_dat->cdev);
  131. }
  132. static void delete_gpio_led(struct gpio_led_data *led)
  133. {
  134. led_classdev_unregister(&led->cdev);
  135. cancel_work_sync(&led->work);
  136. }
  137. struct gpio_leds_priv {
  138. int num_leds;
  139. struct gpio_led_data leds[];
  140. };
  141. static inline int sizeof_gpio_leds_priv(int num_leds)
  142. {
  143. return sizeof(struct gpio_leds_priv) +
  144. (sizeof(struct gpio_led_data) * num_leds);
  145. }
  146. static struct gpio_leds_priv *gpio_leds_create(struct platform_device *pdev)
  147. {
  148. struct device *dev = &pdev->dev;
  149. struct fwnode_handle *child;
  150. struct gpio_leds_priv *priv;
  151. int count, ret;
  152. struct device_node *np;
  153. count = device_get_child_node_count(dev);
  154. if (!count)
  155. return ERR_PTR(-ENODEV);
  156. priv = devm_kzalloc(dev, sizeof_gpio_leds_priv(count), GFP_KERNEL);
  157. if (!priv)
  158. return ERR_PTR(-ENOMEM);
  159. device_for_each_child_node(dev, child) {
  160. struct gpio_led led = {};
  161. const char *state = NULL;
  162. led.gpiod = devm_get_gpiod_from_child(dev, NULL, child);
  163. if (IS_ERR(led.gpiod)) {
  164. fwnode_handle_put(child);
  165. ret = PTR_ERR(led.gpiod);
  166. goto err;
  167. }
  168. np = to_of_node(child);
  169. if (fwnode_property_present(child, "label")) {
  170. fwnode_property_read_string(child, "label", &led.name);
  171. } else {
  172. if (IS_ENABLED(CONFIG_OF) && !led.name && np)
  173. led.name = np->name;
  174. if (!led.name) {
  175. ret = -EINVAL;
  176. goto err;
  177. }
  178. }
  179. fwnode_property_read_string(child, "linux,default-trigger",
  180. &led.default_trigger);
  181. if (!fwnode_property_read_string(child, "default-state",
  182. &state)) {
  183. if (!strcmp(state, "keep"))
  184. led.default_state = LEDS_GPIO_DEFSTATE_KEEP;
  185. else if (!strcmp(state, "on"))
  186. led.default_state = LEDS_GPIO_DEFSTATE_ON;
  187. else
  188. led.default_state = LEDS_GPIO_DEFSTATE_OFF;
  189. }
  190. if (fwnode_property_present(child, "retain-state-suspended"))
  191. led.retain_state_suspended = 1;
  192. ret = create_gpio_led(&led, &priv->leds[priv->num_leds],
  193. dev, NULL);
  194. if (ret < 0) {
  195. fwnode_handle_put(child);
  196. goto err;
  197. }
  198. priv->num_leds++;
  199. }
  200. return priv;
  201. err:
  202. for (count = priv->num_leds - 1; count >= 0; count--)
  203. delete_gpio_led(&priv->leds[count]);
  204. return ERR_PTR(ret);
  205. }
  206. static const struct of_device_id of_gpio_leds_match[] = {
  207. { .compatible = "gpio-leds", },
  208. {},
  209. };
  210. MODULE_DEVICE_TABLE(of, of_gpio_leds_match);
  211. static int gpio_led_probe(struct platform_device *pdev)
  212. {
  213. struct gpio_led_platform_data *pdata = dev_get_platdata(&pdev->dev);
  214. struct gpio_leds_priv *priv;
  215. int i, ret = 0;
  216. if (pdata && pdata->num_leds) {
  217. priv = devm_kzalloc(&pdev->dev,
  218. sizeof_gpio_leds_priv(pdata->num_leds),
  219. GFP_KERNEL);
  220. if (!priv)
  221. return -ENOMEM;
  222. priv->num_leds = pdata->num_leds;
  223. for (i = 0; i < priv->num_leds; i++) {
  224. ret = create_gpio_led(&pdata->leds[i],
  225. &priv->leds[i],
  226. &pdev->dev, pdata->gpio_blink_set);
  227. if (ret < 0) {
  228. /* On failure: unwind the led creations */
  229. for (i = i - 1; i >= 0; i--)
  230. delete_gpio_led(&priv->leds[i]);
  231. return ret;
  232. }
  233. }
  234. } else {
  235. priv = gpio_leds_create(pdev);
  236. if (IS_ERR(priv))
  237. return PTR_ERR(priv);
  238. }
  239. platform_set_drvdata(pdev, priv);
  240. return 0;
  241. }
  242. static int gpio_led_remove(struct platform_device *pdev)
  243. {
  244. struct gpio_leds_priv *priv = platform_get_drvdata(pdev);
  245. int i;
  246. for (i = 0; i < priv->num_leds; i++)
  247. delete_gpio_led(&priv->leds[i]);
  248. return 0;
  249. }
  250. static void gpio_led_shutdown(struct platform_device *pdev)
  251. {
  252. struct gpio_leds_priv *priv = platform_get_drvdata(pdev);
  253. int i;
  254. for (i = 0; i < priv->num_leds; i++) {
  255. struct gpio_led_data *led = &priv->leds[i];
  256. gpio_led_set(&led->cdev, LED_OFF);
  257. }
  258. }
  259. static struct platform_driver gpio_led_driver = {
  260. .probe = gpio_led_probe,
  261. .remove = gpio_led_remove,
  262. .shutdown = gpio_led_shutdown,
  263. .driver = {
  264. .name = "leds-gpio",
  265. .of_match_table = of_gpio_leds_match,
  266. },
  267. };
  268. module_platform_driver(gpio_led_driver);
  269. MODULE_AUTHOR("Raphael Assenat <raph@8d.com>, Trent Piepho <tpiepho@freescale.com>");
  270. MODULE_DESCRIPTION("GPIO LED driver");
  271. MODULE_LICENSE("GPL");
  272. MODULE_ALIAS("platform:leds-gpio");