leds-atmel-pwm.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #include <linux/kernel.h>
  2. #include <linux/platform_device.h>
  3. #include <linux/leds.h>
  4. #include <linux/io.h>
  5. #include <linux/atmel_pwm.h>
  6. #include <linux/slab.h>
  7. #include <linux/module.h>
  8. struct pwmled {
  9. struct led_classdev cdev;
  10. struct pwm_channel pwmc;
  11. struct gpio_led *desc;
  12. u32 mult;
  13. u8 active_low;
  14. };
  15. /*
  16. * For simplicity, we use "brightness" as if it were a linear function
  17. * of PWM duty cycle. However, a logarithmic function of duty cycle is
  18. * probably a better match for perceived brightness: two is half as bright
  19. * as four, four is half as bright as eight, etc
  20. */
  21. static void pwmled_brightness(struct led_classdev *cdev, enum led_brightness b)
  22. {
  23. struct pwmled *led;
  24. /* update the duty cycle for the *next* period */
  25. led = container_of(cdev, struct pwmled, cdev);
  26. pwm_channel_writel(&led->pwmc, PWM_CUPD, led->mult * (unsigned) b);
  27. }
  28. /*
  29. * NOTE: we reuse the platform_data structure of GPIO leds,
  30. * but repurpose its "gpio" number as a PWM channel number.
  31. */
  32. static int __devinit pwmled_probe(struct platform_device *pdev)
  33. {
  34. const struct gpio_led_platform_data *pdata;
  35. struct pwmled *leds;
  36. int i;
  37. int status;
  38. pdata = pdev->dev.platform_data;
  39. if (!pdata || pdata->num_leds < 1)
  40. return -ENODEV;
  41. leds = kcalloc(pdata->num_leds, sizeof(*leds), GFP_KERNEL);
  42. if (!leds)
  43. return -ENOMEM;
  44. for (i = 0; i < pdata->num_leds; i++) {
  45. struct pwmled *led = leds + i;
  46. const struct gpio_led *dat = pdata->leds + i;
  47. u32 tmp;
  48. led->cdev.name = dat->name;
  49. led->cdev.brightness = LED_OFF;
  50. led->cdev.brightness_set = pwmled_brightness;
  51. led->cdev.default_trigger = dat->default_trigger;
  52. led->active_low = dat->active_low;
  53. status = pwm_channel_alloc(dat->gpio, &led->pwmc);
  54. if (status < 0)
  55. goto err;
  56. /*
  57. * Prescale clock by 2^x, so PWM counts in low MHz.
  58. * Start each cycle with the LED active, so increasing
  59. * the duty cycle gives us more time on (== brighter).
  60. */
  61. tmp = 5;
  62. if (!led->active_low)
  63. tmp |= PWM_CPR_CPOL;
  64. pwm_channel_writel(&led->pwmc, PWM_CMR, tmp);
  65. /*
  66. * Pick a period so PWM cycles at 100+ Hz; and a multiplier
  67. * for scaling duty cycle: brightness * mult.
  68. */
  69. tmp = (led->pwmc.mck / (1 << 5)) / 100;
  70. tmp /= 255;
  71. led->mult = tmp;
  72. pwm_channel_writel(&led->pwmc, PWM_CDTY,
  73. led->cdev.brightness * 255);
  74. pwm_channel_writel(&led->pwmc, PWM_CPRD,
  75. LED_FULL * tmp);
  76. pwm_channel_enable(&led->pwmc);
  77. /* Hand it over to the LED framework */
  78. status = led_classdev_register(&pdev->dev, &led->cdev);
  79. if (status < 0) {
  80. pwm_channel_free(&led->pwmc);
  81. goto err;
  82. }
  83. }
  84. platform_set_drvdata(pdev, leds);
  85. return 0;
  86. err:
  87. if (i > 0) {
  88. for (i = i - 1; i >= 0; i--) {
  89. led_classdev_unregister(&leds[i].cdev);
  90. pwm_channel_free(&leds[i].pwmc);
  91. }
  92. }
  93. kfree(leds);
  94. return status;
  95. }
  96. static int __exit pwmled_remove(struct platform_device *pdev)
  97. {
  98. const struct gpio_led_platform_data *pdata;
  99. struct pwmled *leds;
  100. unsigned i;
  101. pdata = pdev->dev.platform_data;
  102. leds = platform_get_drvdata(pdev);
  103. for (i = 0; i < pdata->num_leds; i++) {
  104. struct pwmled *led = leds + i;
  105. led_classdev_unregister(&led->cdev);
  106. pwm_channel_free(&led->pwmc);
  107. }
  108. kfree(leds);
  109. platform_set_drvdata(pdev, NULL);
  110. return 0;
  111. }
  112. static struct platform_driver pwmled_driver = {
  113. .driver = {
  114. .name = "leds-atmel-pwm",
  115. .owner = THIS_MODULE,
  116. },
  117. /* REVISIT add suspend() and resume() methods */
  118. .probe = pwmled_probe,
  119. .remove = __exit_p(pwmled_remove),
  120. };
  121. module_platform_driver(pwmled_driver);
  122. MODULE_DESCRIPTION("Driver for LEDs with PWM-controlled brightness");
  123. MODULE_LICENSE("GPL");
  124. MODULE_ALIAS("platform:leds-atmel-pwm");