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