leds-pwm.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. if (ret != -EPROBE_DEFER)
  87. dev_err(dev, "unable to request PWM for %s: %d\n",
  88. led->name, ret);
  89. return ret;
  90. }
  91. led_data->cdev.brightness_set_blocking = led_pwm_set;
  92. /*
  93. * FIXME: pwm_apply_args() should be removed when switching to the
  94. * atomic PWM API.
  95. */
  96. pwm_apply_args(led_data->pwm);
  97. pwm_get_args(led_data->pwm, &pargs);
  98. led_data->period = pargs.period;
  99. if (!led_data->period && (led->pwm_period_ns > 0))
  100. led_data->period = led->pwm_period_ns;
  101. ret = led_classdev_register(dev, &led_data->cdev);
  102. if (ret == 0) {
  103. priv->num_leds++;
  104. led_pwm_set(&led_data->cdev, led_data->cdev.brightness);
  105. } else {
  106. dev_err(dev, "failed to register PWM led for %s: %d\n",
  107. led->name, ret);
  108. }
  109. return ret;
  110. }
  111. static int led_pwm_create_of(struct device *dev, struct led_pwm_priv *priv)
  112. {
  113. struct device_node *child;
  114. struct led_pwm led;
  115. int ret = 0;
  116. memset(&led, 0, sizeof(led));
  117. for_each_child_of_node(dev->of_node, child) {
  118. led.name = of_get_property(child, "label", NULL) ? :
  119. child->name;
  120. led.default_trigger = of_get_property(child,
  121. "linux,default-trigger", NULL);
  122. led.active_low = of_property_read_bool(child, "active-low");
  123. of_property_read_u32(child, "max-brightness",
  124. &led.max_brightness);
  125. ret = led_pwm_add(dev, priv, &led, child);
  126. if (ret) {
  127. of_node_put(child);
  128. break;
  129. }
  130. }
  131. return ret;
  132. }
  133. static int led_pwm_probe(struct platform_device *pdev)
  134. {
  135. struct led_pwm_platform_data *pdata = dev_get_platdata(&pdev->dev);
  136. struct led_pwm_priv *priv;
  137. int count, i;
  138. int ret = 0;
  139. if (pdata)
  140. count = pdata->num_leds;
  141. else
  142. count = of_get_child_count(pdev->dev.of_node);
  143. if (!count)
  144. return -EINVAL;
  145. priv = devm_kzalloc(&pdev->dev, sizeof_pwm_leds_priv(count),
  146. GFP_KERNEL);
  147. if (!priv)
  148. return -ENOMEM;
  149. if (pdata) {
  150. for (i = 0; i < count; i++) {
  151. ret = led_pwm_add(&pdev->dev, priv, &pdata->leds[i],
  152. NULL);
  153. if (ret)
  154. break;
  155. }
  156. } else {
  157. ret = led_pwm_create_of(&pdev->dev, priv);
  158. }
  159. if (ret) {
  160. led_pwm_cleanup(priv);
  161. return ret;
  162. }
  163. platform_set_drvdata(pdev, priv);
  164. return 0;
  165. }
  166. static int led_pwm_remove(struct platform_device *pdev)
  167. {
  168. struct led_pwm_priv *priv = platform_get_drvdata(pdev);
  169. led_pwm_cleanup(priv);
  170. return 0;
  171. }
  172. static const struct of_device_id of_pwm_leds_match[] = {
  173. { .compatible = "pwm-leds", },
  174. {},
  175. };
  176. MODULE_DEVICE_TABLE(of, of_pwm_leds_match);
  177. static struct platform_driver led_pwm_driver = {
  178. .probe = led_pwm_probe,
  179. .remove = led_pwm_remove,
  180. .driver = {
  181. .name = "leds_pwm",
  182. .of_match_table = of_pwm_leds_match,
  183. },
  184. };
  185. module_platform_driver(led_pwm_driver);
  186. MODULE_AUTHOR("Luotao Fu <l.fu@pengutronix.de>");
  187. MODULE_DESCRIPTION("generic PWM LED driver");
  188. MODULE_LICENSE("GPL v2");
  189. MODULE_ALIAS("platform:leds-pwm");