leds-pwm.c 5.1 KB

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