leds-pwm.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. #include <linux/workqueue.h>
  25. struct led_pwm_data {
  26. struct led_classdev cdev;
  27. struct pwm_device *pwm;
  28. struct work_struct work;
  29. unsigned int active_low;
  30. unsigned int period;
  31. int duty;
  32. bool can_sleep;
  33. };
  34. struct led_pwm_priv {
  35. int num_leds;
  36. struct led_pwm_data leds[0];
  37. };
  38. static void __led_pwm_set(struct led_pwm_data *led_dat)
  39. {
  40. int new_duty = led_dat->duty;
  41. pwm_config(led_dat->pwm, new_duty, led_dat->period);
  42. if (new_duty == 0)
  43. pwm_disable(led_dat->pwm);
  44. else
  45. pwm_enable(led_dat->pwm);
  46. }
  47. static void led_pwm_work(struct work_struct *work)
  48. {
  49. struct led_pwm_data *led_dat =
  50. container_of(work, struct led_pwm_data, work);
  51. __led_pwm_set(led_dat);
  52. }
  53. static void led_pwm_set(struct led_classdev *led_cdev,
  54. enum led_brightness brightness)
  55. {
  56. struct led_pwm_data *led_dat =
  57. container_of(led_cdev, struct led_pwm_data, cdev);
  58. unsigned int max = led_dat->cdev.max_brightness;
  59. unsigned long long duty = led_dat->period;
  60. duty *= brightness;
  61. do_div(duty, max);
  62. if (led_dat->active_low)
  63. duty = led_dat->period - duty;
  64. led_dat->duty = duty;
  65. if (led_dat->can_sleep)
  66. schedule_work(&led_dat->work);
  67. else
  68. __led_pwm_set(led_dat);
  69. }
  70. static inline size_t sizeof_pwm_leds_priv(int num_leds)
  71. {
  72. return sizeof(struct led_pwm_priv) +
  73. (sizeof(struct led_pwm_data) * num_leds);
  74. }
  75. static void led_pwm_cleanup(struct led_pwm_priv *priv)
  76. {
  77. while (priv->num_leds--) {
  78. led_classdev_unregister(&priv->leds[priv->num_leds].cdev);
  79. if (priv->leds[priv->num_leds].can_sleep)
  80. cancel_work_sync(&priv->leds[priv->num_leds].work);
  81. }
  82. }
  83. static int led_pwm_add(struct device *dev, struct led_pwm_priv *priv,
  84. struct led_pwm *led, struct device_node *child)
  85. {
  86. struct led_pwm_data *led_data = &priv->leds[priv->num_leds];
  87. int ret;
  88. led_data->active_low = led->active_low;
  89. led_data->cdev.name = led->name;
  90. led_data->cdev.default_trigger = led->default_trigger;
  91. led_data->cdev.brightness_set = led_pwm_set;
  92. led_data->cdev.brightness = LED_OFF;
  93. led_data->cdev.max_brightness = led->max_brightness;
  94. led_data->cdev.flags = LED_CORE_SUSPENDRESUME;
  95. if (child)
  96. led_data->pwm = devm_of_pwm_get(dev, child, NULL);
  97. else
  98. led_data->pwm = devm_pwm_get(dev, led->name);
  99. if (IS_ERR(led_data->pwm)) {
  100. ret = PTR_ERR(led_data->pwm);
  101. dev_err(dev, "unable to request PWM for %s: %d\n",
  102. led->name, ret);
  103. return ret;
  104. }
  105. led_data->can_sleep = pwm_can_sleep(led_data->pwm);
  106. if (led_data->can_sleep)
  107. INIT_WORK(&led_data->work, led_pwm_work);
  108. led_data->period = pwm_get_period(led_data->pwm);
  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("PWM LED driver for PXA");
  198. MODULE_LICENSE("GPL");
  199. MODULE_ALIAS("platform:leds-pwm");