leds-atmel-pwm.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 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 = devm_kzalloc(&pdev->dev, pdata->num_leds * sizeof(*leds),
  42. GFP_KERNEL);
  43. if (!leds)
  44. return -ENOMEM;
  45. for (i = 0; i < pdata->num_leds; i++) {
  46. struct pwmled *led = leds + i;
  47. const struct gpio_led *dat = pdata->leds + i;
  48. u32 tmp;
  49. led->cdev.name = dat->name;
  50. led->cdev.brightness = LED_OFF;
  51. led->cdev.brightness_set = pwmled_brightness;
  52. led->cdev.default_trigger = dat->default_trigger;
  53. led->active_low = dat->active_low;
  54. status = pwm_channel_alloc(dat->gpio, &led->pwmc);
  55. if (status < 0)
  56. goto err;
  57. /*
  58. * Prescale clock by 2^x, so PWM counts in low MHz.
  59. * Start each cycle with the LED active, so increasing
  60. * the duty cycle gives us more time on (== brighter).
  61. */
  62. tmp = 5;
  63. if (!led->active_low)
  64. tmp |= PWM_CPR_CPOL;
  65. pwm_channel_writel(&led->pwmc, PWM_CMR, tmp);
  66. /*
  67. * Pick a period so PWM cycles at 100+ Hz; and a multiplier
  68. * for scaling duty cycle: brightness * mult.
  69. */
  70. tmp = (led->pwmc.mck / (1 << 5)) / 100;
  71. tmp /= 255;
  72. led->mult = tmp;
  73. pwm_channel_writel(&led->pwmc, PWM_CDTY,
  74. led->cdev.brightness * 255);
  75. pwm_channel_writel(&led->pwmc, PWM_CPRD,
  76. LED_FULL * tmp);
  77. pwm_channel_enable(&led->pwmc);
  78. /* Hand it over to the LED framework */
  79. status = led_classdev_register(&pdev->dev, &led->cdev);
  80. if (status < 0) {
  81. pwm_channel_free(&led->pwmc);
  82. goto err;
  83. }
  84. }
  85. platform_set_drvdata(pdev, leds);
  86. return 0;
  87. err:
  88. if (i > 0) {
  89. for (i = i - 1; i >= 0; i--) {
  90. led_classdev_unregister(&leds[i].cdev);
  91. pwm_channel_free(&leds[i].pwmc);
  92. }
  93. }
  94. return status;
  95. }
  96. static int 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. platform_set_drvdata(pdev, NULL);
  109. return 0;
  110. }
  111. static struct platform_driver pwmled_driver = {
  112. .driver = {
  113. .name = "leds-atmel-pwm",
  114. .owner = THIS_MODULE,
  115. },
  116. /* REVISIT add suspend() and resume() methods */
  117. .probe = pwmled_probe,
  118. .remove = pwmled_remove,
  119. };
  120. module_platform_driver(pwmled_driver);
  121. MODULE_DESCRIPTION("Driver for LEDs with PWM-controlled brightness");
  122. MODULE_LICENSE("GPL");
  123. MODULE_ALIAS("platform:leds-atmel-pwm");