leds-pwm.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * linux/drivers/leds-pwm.c
  3. *
  4. * simple PWM based LED control
  5. *
  6. * Copyright 2009 Luotao Fu @ Pengutronix (l.fu@pengutronix.de)
  7. *
  8. * based on leds-gpio.c by Raphael Assenat <raph@8d.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/module.h>
  15. #include <linux/kernel.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/of_platform.h>
  18. #include <linux/fb.h>
  19. #include <linux/leds.h>
  20. #include <linux/err.h>
  21. #include <linux/pwm.h>
  22. #include <linux/leds_pwm.h>
  23. #include <linux/slab.h>
  24. struct led_pwm_data {
  25. struct led_classdev cdev;
  26. struct pwm_device *pwm;
  27. unsigned int active_low;
  28. unsigned int period;
  29. int duty;
  30. bool can_sleep;
  31. };
  32. struct led_pwm_priv {
  33. int num_leds;
  34. struct led_pwm_data leds[0];
  35. };
  36. static void __led_pwm_set(struct led_pwm_data *led_dat)
  37. {
  38. int new_duty = led_dat->duty;
  39. pwm_config(led_dat->pwm, new_duty, led_dat->period);
  40. if (new_duty == 0)
  41. pwm_disable(led_dat->pwm);
  42. else
  43. pwm_enable(led_dat->pwm);
  44. }
  45. static void led_pwm_set(struct led_classdev *led_cdev,
  46. enum led_brightness brightness)
  47. {
  48. struct led_pwm_data *led_dat =
  49. container_of(led_cdev, struct led_pwm_data, cdev);
  50. unsigned int max = led_dat->cdev.max_brightness;
  51. unsigned long long duty = led_dat->period;
  52. duty *= brightness;
  53. do_div(duty, max);
  54. if (led_dat->active_low)
  55. duty = led_dat->period - duty;
  56. led_dat->duty = duty;
  57. __led_pwm_set(led_dat);
  58. }
  59. static int led_pwm_set_blocking(struct led_classdev *led_cdev,
  60. enum led_brightness brightness)
  61. {
  62. led_pwm_set(led_cdev, brightness);
  63. return 0;
  64. }
  65. static inline size_t sizeof_pwm_leds_priv(int num_leds)
  66. {
  67. return sizeof(struct led_pwm_priv) +
  68. (sizeof(struct led_pwm_data) * num_leds);
  69. }
  70. static void led_pwm_cleanup(struct led_pwm_priv *priv)
  71. {
  72. while (priv->num_leds--)
  73. led_classdev_unregister(&priv->leds[priv->num_leds].cdev);
  74. }
  75. static int led_pwm_add(struct device *dev, struct led_pwm_priv *priv,
  76. struct led_pwm *led, struct device_node *child)
  77. {
  78. struct led_pwm_data *led_data = &priv->leds[priv->num_leds];
  79. struct pwm_args pargs;
  80. int ret;
  81. led_data->active_low = led->active_low;
  82. led_data->cdev.name = led->name;
  83. led_data->cdev.default_trigger = led->default_trigger;
  84. led_data->cdev.brightness = LED_OFF;
  85. led_data->cdev.max_brightness = led->max_brightness;
  86. led_data->cdev.flags = LED_CORE_SUSPENDRESUME;
  87. if (child)
  88. led_data->pwm = devm_of_pwm_get(dev, child, NULL);
  89. else
  90. led_data->pwm = devm_pwm_get(dev, led->name);
  91. if (IS_ERR(led_data->pwm)) {
  92. ret = PTR_ERR(led_data->pwm);
  93. dev_err(dev, "unable to request PWM for %s: %d\n",
  94. led->name, ret);
  95. return ret;
  96. }
  97. led_data->can_sleep = pwm_can_sleep(led_data->pwm);
  98. if (!led_data->can_sleep)
  99. led_data->cdev.brightness_set = led_pwm_set;
  100. else
  101. led_data->cdev.brightness_set_blocking = led_pwm_set_blocking;
  102. /*
  103. * FIXME: pwm_apply_args() should be removed when switching to the
  104. * atomic PWM API.
  105. */
  106. pwm_apply_args(led_data->pwm);
  107. pwm_get_args(led_data->pwm, &pargs);
  108. led_data->period = pargs.period;
  109. if (!led_data->period && (led->pwm_period_ns > 0))
  110. led_data->period = led->pwm_period_ns;
  111. ret = led_classdev_register(dev, &led_data->cdev);
  112. if (ret == 0) {
  113. priv->num_leds++;
  114. led_pwm_set(&led_data->cdev, led_data->cdev.brightness);
  115. } else {
  116. dev_err(dev, "failed to register PWM led for %s: %d\n",
  117. led->name, ret);
  118. }
  119. return ret;
  120. }
  121. static int led_pwm_create_of(struct device *dev, struct led_pwm_priv *priv)
  122. {
  123. struct device_node *child;
  124. struct led_pwm led;
  125. int ret = 0;
  126. memset(&led, 0, sizeof(led));
  127. for_each_child_of_node(dev->of_node, child) {
  128. led.name = of_get_property(child, "label", NULL) ? :
  129. child->name;
  130. led.default_trigger = of_get_property(child,
  131. "linux,default-trigger", NULL);
  132. led.active_low = of_property_read_bool(child, "active-low");
  133. of_property_read_u32(child, "max-brightness",
  134. &led.max_brightness);
  135. ret = led_pwm_add(dev, priv, &led, child);
  136. if (ret) {
  137. of_node_put(child);
  138. break;
  139. }
  140. }
  141. return ret;
  142. }
  143. static int led_pwm_probe(struct platform_device *pdev)
  144. {
  145. struct led_pwm_platform_data *pdata = dev_get_platdata(&pdev->dev);
  146. struct led_pwm_priv *priv;
  147. int count, i;
  148. int ret = 0;
  149. if (pdata)
  150. count = pdata->num_leds;
  151. else
  152. count = of_get_child_count(pdev->dev.of_node);
  153. if (!count)
  154. return -EINVAL;
  155. priv = devm_kzalloc(&pdev->dev, sizeof_pwm_leds_priv(count),
  156. GFP_KERNEL);
  157. if (!priv)
  158. return -ENOMEM;
  159. if (pdata) {
  160. for (i = 0; i < count; i++) {
  161. ret = led_pwm_add(&pdev->dev, priv, &pdata->leds[i],
  162. NULL);
  163. if (ret)
  164. break;
  165. }
  166. } else {
  167. ret = led_pwm_create_of(&pdev->dev, priv);
  168. }
  169. if (ret) {
  170. led_pwm_cleanup(priv);
  171. return ret;
  172. }
  173. platform_set_drvdata(pdev, priv);
  174. return 0;
  175. }
  176. static int led_pwm_remove(struct platform_device *pdev)
  177. {
  178. struct led_pwm_priv *priv = platform_get_drvdata(pdev);
  179. led_pwm_cleanup(priv);
  180. return 0;
  181. }
  182. static const struct of_device_id of_pwm_leds_match[] = {
  183. { .compatible = "pwm-leds", },
  184. {},
  185. };
  186. MODULE_DEVICE_TABLE(of, of_pwm_leds_match);
  187. static struct platform_driver led_pwm_driver = {
  188. .probe = led_pwm_probe,
  189. .remove = led_pwm_remove,
  190. .driver = {
  191. .name = "leds_pwm",
  192. .of_match_table = of_pwm_leds_match,
  193. },
  194. };
  195. module_platform_driver(led_pwm_driver);
  196. MODULE_AUTHOR("Luotao Fu <l.fu@pengutronix.de>");
  197. MODULE_DESCRIPTION("generic PWM LED driver");
  198. MODULE_LICENSE("GPL v2");
  199. MODULE_ALIAS("platform:leds-pwm");