leds-atmel-pwm.c 3.7 KB

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