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