blob: 88b1dd091cfb9f3887ec052f75a3f4e0620d69ad [file] [log] [blame]
Luotao Fu41c42ff2009-02-11 13:24:40 -08001/*
2 * linux/drivers/leds-pwm.c
3 *
4 * simple PWM based LED control
5 *
6 * Copyright 2009 Luotao Fu @ Pengutronix (l.fu@pengutronix.de)
7 *
8 * based on leds-gpio.c by Raphael Assenat <raph@8d.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 */
14
15#include <linux/module.h>
16#include <linux/kernel.h>
17#include <linux/init.h>
18#include <linux/platform_device.h>
19#include <linux/fb.h>
20#include <linux/leds.h>
21#include <linux/err.h>
22#include <linux/pwm.h>
23#include <linux/leds_pwm.h>
24
25struct led_pwm_data {
26 struct led_classdev cdev;
27 struct pwm_device *pwm;
28 unsigned int active_low;
29 unsigned int period;
Luotao Fu41c42ff2009-02-11 13:24:40 -080030};
31
32static void led_pwm_set(struct led_classdev *led_cdev,
33 enum led_brightness brightness)
34{
35 struct led_pwm_data *led_dat =
36 container_of(led_cdev, struct led_pwm_data, cdev);
Lars-Peter Clausene4590622009-11-27 06:17:38 +010037 unsigned int max = led_dat->cdev.max_brightness;
Luotao Fu41c42ff2009-02-11 13:24:40 -080038 unsigned int period = led_dat->period;
39
40 if (brightness == 0) {
41 pwm_config(led_dat->pwm, 0, period);
42 pwm_disable(led_dat->pwm);
43 } else {
44 pwm_config(led_dat->pwm, brightness * period / max, period);
45 pwm_enable(led_dat->pwm);
46 }
47}
48
49static int led_pwm_probe(struct platform_device *pdev)
50{
51 struct led_pwm_platform_data *pdata = pdev->dev.platform_data;
52 struct led_pwm *cur_led;
53 struct led_pwm_data *leds_data, *led_dat;
54 int i, ret = 0;
55
56 if (!pdata)
57 return -EBUSY;
58
59 leds_data = kzalloc(sizeof(struct led_pwm_data) * pdata->num_leds,
60 GFP_KERNEL);
61 if (!leds_data)
62 return -ENOMEM;
63
64 for (i = 0; i < pdata->num_leds; i++) {
65 cur_led = &pdata->leds[i];
66 led_dat = &leds_data[i];
67
68 led_dat->pwm = pwm_request(cur_led->pwm_id,
69 cur_led->name);
70 if (IS_ERR(led_dat->pwm)) {
71 dev_err(&pdev->dev, "unable to request PWM %d\n",
72 cur_led->pwm_id);
73 goto err;
74 }
75
76 led_dat->cdev.name = cur_led->name;
77 led_dat->cdev.default_trigger = cur_led->default_trigger;
78 led_dat->active_low = cur_led->active_low;
Luotao Fu41c42ff2009-02-11 13:24:40 -080079 led_dat->period = cur_led->pwm_period_ns;
80 led_dat->cdev.brightness_set = led_pwm_set;
81 led_dat->cdev.brightness = LED_OFF;
Lars-Peter Clausene4590622009-11-27 06:17:38 +010082 led_dat->cdev.max_brightness = cur_led->max_brightness;
Luotao Fu41c42ff2009-02-11 13:24:40 -080083 led_dat->cdev.flags |= LED_CORE_SUSPENDRESUME;
84
85 ret = led_classdev_register(&pdev->dev, &led_dat->cdev);
86 if (ret < 0) {
87 pwm_free(led_dat->pwm);
88 goto err;
89 }
90 }
91
92 platform_set_drvdata(pdev, leds_data);
93
94 return 0;
95
96err:
97 if (i > 0) {
98 for (i = i - 1; i >= 0; i--) {
99 led_classdev_unregister(&leds_data[i].cdev);
100 pwm_free(leds_data[i].pwm);
101 }
102 }
103
104 kfree(leds_data);
105
106 return ret;
107}
108
109static int __devexit led_pwm_remove(struct platform_device *pdev)
110{
111 int i;
112 struct led_pwm_platform_data *pdata = pdev->dev.platform_data;
113 struct led_pwm_data *leds_data;
114
115 leds_data = platform_get_drvdata(pdev);
116
117 for (i = 0; i < pdata->num_leds; i++) {
118 led_classdev_unregister(&leds_data[i].cdev);
119 pwm_free(leds_data[i].pwm);
120 }
121
122 kfree(leds_data);
123
124 return 0;
125}
126
127static struct platform_driver led_pwm_driver = {
128 .probe = led_pwm_probe,
129 .remove = __devexit_p(led_pwm_remove),
130 .driver = {
131 .name = "leds_pwm",
132 .owner = THIS_MODULE,
133 },
134};
135
136static int __init led_pwm_init(void)
137{
138 return platform_driver_register(&led_pwm_driver);
139}
140
141static void __exit led_pwm_exit(void)
142{
143 platform_driver_unregister(&led_pwm_driver);
144}
145
146module_init(led_pwm_init);
147module_exit(led_pwm_exit);
148
149MODULE_AUTHOR("Luotao Fu <l.fu@pengutronix.de>");
150MODULE_DESCRIPTION("PWM LED driver for PXA");
151MODULE_LICENSE("GPL");
152MODULE_ALIAS("platform:leds-pwm");