leds-gpio.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. struct gpio_led_data {
  24. struct led_classdev cdev;
  25. struct gpio_desc *gpiod;
  26. u8 can_sleep;
  27. u8 blinking;
  28. gpio_blink_set_t platform_gpio_blink_set;
  29. };
  30. static inline struct gpio_led_data *
  31. cdev_to_gpio_led_data(struct led_classdev *led_cdev)
  32. {
  33. return container_of(led_cdev, struct gpio_led_data, cdev);
  34. }
  35. static void gpio_led_set(struct led_classdev *led_cdev,
  36. enum led_brightness value)
  37. {
  38. struct gpio_led_data *led_dat = cdev_to_gpio_led_data(led_cdev);
  39. int level;
  40. if (value == LED_OFF)
  41. level = 0;
  42. else
  43. level = 1;
  44. if (led_dat->blinking) {
  45. led_dat->platform_gpio_blink_set(led_dat->gpiod, level,
  46. NULL, NULL);
  47. led_dat->blinking = 0;
  48. } else {
  49. if (led_dat->can_sleep)
  50. gpiod_set_value_cansleep(led_dat->gpiod, level);
  51. else
  52. gpiod_set_value(led_dat->gpiod, level);
  53. }
  54. }
  55. static int gpio_led_set_blocking(struct led_classdev *led_cdev,
  56. enum led_brightness value)
  57. {
  58. gpio_led_set(led_cdev, value);
  59. return 0;
  60. }
  61. static int gpio_blink_set(struct led_classdev *led_cdev,
  62. unsigned long *delay_on, unsigned long *delay_off)
  63. {
  64. struct gpio_led_data *led_dat = cdev_to_gpio_led_data(led_cdev);
  65. led_dat->blinking = 1;
  66. return led_dat->platform_gpio_blink_set(led_dat->gpiod, GPIO_LED_BLINK,
  67. delay_on, delay_off);
  68. }
  69. static int create_gpio_led(const struct gpio_led *template,
  70. struct gpio_led_data *led_dat, struct device *parent,
  71. gpio_blink_set_t blink_set)
  72. {
  73. int ret, state;
  74. led_dat->gpiod = template->gpiod;
  75. if (!led_dat->gpiod) {
  76. /*
  77. * This is the legacy code path for platform code that
  78. * still uses GPIO numbers. Ultimately we would like to get
  79. * rid of this block completely.
  80. */
  81. unsigned long flags = GPIOF_OUT_INIT_LOW;
  82. /* skip leds that aren't available */
  83. if (!gpio_is_valid(template->gpio)) {
  84. dev_info(parent, "Skipping unavailable LED gpio %d (%s)\n",
  85. template->gpio, template->name);
  86. return 0;
  87. }
  88. if (template->active_low)
  89. flags |= GPIOF_ACTIVE_LOW;
  90. ret = devm_gpio_request_one(parent, template->gpio, flags,
  91. template->name);
  92. if (ret < 0)
  93. return ret;
  94. led_dat->gpiod = gpio_to_desc(template->gpio);
  95. if (!led_dat->gpiod)
  96. return -EINVAL;
  97. }
  98. led_dat->cdev.name = template->name;
  99. led_dat->cdev.default_trigger = template->default_trigger;
  100. led_dat->can_sleep = gpiod_cansleep(led_dat->gpiod);
  101. if (!led_dat->can_sleep)
  102. led_dat->cdev.brightness_set = gpio_led_set;
  103. else
  104. led_dat->cdev.brightness_set_blocking = gpio_led_set_blocking;
  105. led_dat->blinking = 0;
  106. if (blink_set) {
  107. led_dat->platform_gpio_blink_set = blink_set;
  108. led_dat->cdev.blink_set = gpio_blink_set;
  109. }
  110. if (template->default_state == LEDS_GPIO_DEFSTATE_KEEP) {
  111. state = gpiod_get_value_cansleep(led_dat->gpiod);
  112. if (state < 0)
  113. return state;
  114. } else {
  115. state = (template->default_state == LEDS_GPIO_DEFSTATE_ON);
  116. }
  117. led_dat->cdev.brightness = state ? LED_FULL : LED_OFF;
  118. if (!template->retain_state_suspended)
  119. led_dat->cdev.flags |= LED_CORE_SUSPENDRESUME;
  120. if (template->panic_indicator)
  121. led_dat->cdev.flags |= LED_PANIC_INDICATOR;
  122. ret = gpiod_direction_output(led_dat->gpiod, state);
  123. if (ret < 0)
  124. return ret;
  125. return devm_led_classdev_register(parent, &led_dat->cdev);
  126. }
  127. struct gpio_leds_priv {
  128. int num_leds;
  129. struct gpio_led_data leds[];
  130. };
  131. static inline int sizeof_gpio_leds_priv(int num_leds)
  132. {
  133. return sizeof(struct gpio_leds_priv) +
  134. (sizeof(struct gpio_led_data) * num_leds);
  135. }
  136. static struct gpio_leds_priv *gpio_leds_create(struct platform_device *pdev)
  137. {
  138. struct device *dev = &pdev->dev;
  139. struct fwnode_handle *child;
  140. struct gpio_leds_priv *priv;
  141. int count, ret;
  142. count = device_get_child_node_count(dev);
  143. if (!count)
  144. return ERR_PTR(-ENODEV);
  145. priv = devm_kzalloc(dev, sizeof_gpio_leds_priv(count), GFP_KERNEL);
  146. if (!priv)
  147. return ERR_PTR(-ENOMEM);
  148. device_for_each_child_node(dev, child) {
  149. struct gpio_led_data *led_dat = &priv->leds[priv->num_leds];
  150. struct gpio_led led = {};
  151. const char *state = NULL;
  152. struct device_node *np = to_of_node(child);
  153. led.gpiod = devm_get_gpiod_from_child(dev, NULL, child);
  154. if (IS_ERR(led.gpiod)) {
  155. fwnode_handle_put(child);
  156. return ERR_CAST(led.gpiod);
  157. }
  158. ret = fwnode_property_read_string(child, "label", &led.name);
  159. if (ret && IS_ENABLED(CONFIG_OF) && np)
  160. led.name = np->name;
  161. if (!led.name) {
  162. fwnode_handle_put(child);
  163. return ERR_PTR(-EINVAL);
  164. }
  165. fwnode_property_read_string(child, "linux,default-trigger",
  166. &led.default_trigger);
  167. if (!fwnode_property_read_string(child, "default-state",
  168. &state)) {
  169. if (!strcmp(state, "keep"))
  170. led.default_state = LEDS_GPIO_DEFSTATE_KEEP;
  171. else if (!strcmp(state, "on"))
  172. led.default_state = LEDS_GPIO_DEFSTATE_ON;
  173. else
  174. led.default_state = LEDS_GPIO_DEFSTATE_OFF;
  175. }
  176. if (fwnode_property_present(child, "retain-state-suspended"))
  177. led.retain_state_suspended = 1;
  178. if (fwnode_property_present(child, "panic-indicator"))
  179. led.panic_indicator = 1;
  180. ret = create_gpio_led(&led, led_dat, dev, NULL);
  181. if (ret < 0) {
  182. fwnode_handle_put(child);
  183. return ERR_PTR(ret);
  184. }
  185. led_dat->cdev.dev->of_node = np;
  186. priv->num_leds++;
  187. }
  188. return priv;
  189. }
  190. static const struct of_device_id of_gpio_leds_match[] = {
  191. { .compatible = "gpio-leds", },
  192. {},
  193. };
  194. MODULE_DEVICE_TABLE(of, of_gpio_leds_match);
  195. static int gpio_led_probe(struct platform_device *pdev)
  196. {
  197. struct gpio_led_platform_data *pdata = dev_get_platdata(&pdev->dev);
  198. struct gpio_leds_priv *priv;
  199. int i, ret = 0;
  200. if (pdata && pdata->num_leds) {
  201. priv = devm_kzalloc(&pdev->dev,
  202. sizeof_gpio_leds_priv(pdata->num_leds),
  203. GFP_KERNEL);
  204. if (!priv)
  205. return -ENOMEM;
  206. priv->num_leds = pdata->num_leds;
  207. for (i = 0; i < priv->num_leds; i++) {
  208. ret = create_gpio_led(&pdata->leds[i],
  209. &priv->leds[i],
  210. &pdev->dev, pdata->gpio_blink_set);
  211. if (ret < 0)
  212. return ret;
  213. }
  214. } else {
  215. priv = gpio_leds_create(pdev);
  216. if (IS_ERR(priv))
  217. return PTR_ERR(priv);
  218. }
  219. platform_set_drvdata(pdev, priv);
  220. return 0;
  221. }
  222. static void gpio_led_shutdown(struct platform_device *pdev)
  223. {
  224. struct gpio_leds_priv *priv = platform_get_drvdata(pdev);
  225. int i;
  226. for (i = 0; i < priv->num_leds; i++) {
  227. struct gpio_led_data *led = &priv->leds[i];
  228. gpio_led_set(&led->cdev, LED_OFF);
  229. }
  230. }
  231. static struct platform_driver gpio_led_driver = {
  232. .probe = gpio_led_probe,
  233. .shutdown = gpio_led_shutdown,
  234. .driver = {
  235. .name = "leds-gpio",
  236. .of_match_table = of_gpio_leds_match,
  237. },
  238. };
  239. module_platform_driver(gpio_led_driver);
  240. MODULE_AUTHOR("Raphael Assenat <raph@8d.com>, Trent Piepho <tpiepho@freescale.com>");
  241. MODULE_DESCRIPTION("GPIO LED driver");
  242. MODULE_LICENSE("GPL");
  243. MODULE_ALIAS("platform:leds-gpio");