leds-pwm.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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/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. };
  30. static void led_pwm_set(struct led_classdev *led_cdev,
  31. enum led_brightness brightness)
  32. {
  33. struct led_pwm_data *led_dat =
  34. container_of(led_cdev, struct led_pwm_data, cdev);
  35. unsigned int max = led_dat->cdev.max_brightness;
  36. unsigned int period = led_dat->period;
  37. if (brightness == 0) {
  38. pwm_config(led_dat->pwm, 0, period);
  39. pwm_disable(led_dat->pwm);
  40. } else {
  41. pwm_config(led_dat->pwm, brightness * period / max, period);
  42. pwm_enable(led_dat->pwm);
  43. }
  44. }
  45. static int led_pwm_probe(struct platform_device *pdev)
  46. {
  47. struct led_pwm_platform_data *pdata = pdev->dev.platform_data;
  48. struct led_pwm *cur_led;
  49. struct led_pwm_data *leds_data, *led_dat;
  50. int i, ret = 0;
  51. if (!pdata)
  52. return -EBUSY;
  53. leds_data = kzalloc(sizeof(struct led_pwm_data) * pdata->num_leds,
  54. GFP_KERNEL);
  55. if (!leds_data)
  56. return -ENOMEM;
  57. for (i = 0; i < pdata->num_leds; i++) {
  58. cur_led = &pdata->leds[i];
  59. led_dat = &leds_data[i];
  60. led_dat->pwm = pwm_request(cur_led->pwm_id,
  61. cur_led->name);
  62. if (IS_ERR(led_dat->pwm)) {
  63. ret = PTR_ERR(led_dat->pwm);
  64. dev_err(&pdev->dev, "unable to request PWM %d\n",
  65. cur_led->pwm_id);
  66. goto err;
  67. }
  68. led_dat->cdev.name = cur_led->name;
  69. led_dat->cdev.default_trigger = cur_led->default_trigger;
  70. led_dat->active_low = cur_led->active_low;
  71. led_dat->period = cur_led->pwm_period_ns;
  72. led_dat->cdev.brightness_set = led_pwm_set;
  73. led_dat->cdev.brightness = LED_OFF;
  74. led_dat->cdev.max_brightness = cur_led->max_brightness;
  75. led_dat->cdev.flags |= LED_CORE_SUSPENDRESUME;
  76. ret = led_classdev_register(&pdev->dev, &led_dat->cdev);
  77. if (ret < 0) {
  78. pwm_free(led_dat->pwm);
  79. goto err;
  80. }
  81. }
  82. platform_set_drvdata(pdev, leds_data);
  83. return 0;
  84. err:
  85. if (i > 0) {
  86. for (i = i - 1; i >= 0; i--) {
  87. led_classdev_unregister(&leds_data[i].cdev);
  88. pwm_free(leds_data[i].pwm);
  89. }
  90. }
  91. kfree(leds_data);
  92. return ret;
  93. }
  94. static int __devexit led_pwm_remove(struct platform_device *pdev)
  95. {
  96. int i;
  97. struct led_pwm_platform_data *pdata = pdev->dev.platform_data;
  98. struct led_pwm_data *leds_data;
  99. leds_data = platform_get_drvdata(pdev);
  100. for (i = 0; i < pdata->num_leds; i++) {
  101. led_classdev_unregister(&leds_data[i].cdev);
  102. pwm_free(leds_data[i].pwm);
  103. }
  104. kfree(leds_data);
  105. return 0;
  106. }
  107. static struct platform_driver led_pwm_driver = {
  108. .probe = led_pwm_probe,
  109. .remove = __devexit_p(led_pwm_remove),
  110. .driver = {
  111. .name = "leds_pwm",
  112. .owner = THIS_MODULE,
  113. },
  114. };
  115. static int __init led_pwm_init(void)
  116. {
  117. return platform_driver_register(&led_pwm_driver);
  118. }
  119. static void __exit led_pwm_exit(void)
  120. {
  121. platform_driver_unregister(&led_pwm_driver);
  122. }
  123. module_init(led_pwm_init);
  124. module_exit(led_pwm_exit);
  125. MODULE_AUTHOR("Luotao Fu <l.fu@pengutronix.de>");
  126. MODULE_DESCRIPTION("PWM LED driver for PXA");
  127. MODULE_LICENSE("GPL");
  128. MODULE_ALIAS("platform:leds-pwm");