leds-atmel-pwm.c 3.7 KB

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