1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
/*
* drivers/leds/leds-pwm.c
*
* Copyright (C) 2009 by Digi International Inc.
* All rights reserved.
*
* Code rebased on original leds-pwm.c from Bill Gatliff
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <linux/kernel.h>
#include <linux/platform_device.h>
#include <linux/leds.h>
#include <linux/io.h>
#include <linux/pwm.h>
#include <linux/pwm-led.h>
#include <linux/err.h>
#include <linux/delay.h>
#include <linux/slab.h>
struct led_pwm {
struct led_classdev led;
struct pwm_channel *pwm;
unsigned long period;
};
static void
led_pwm_brightness_set(struct led_classdev *c,
enum led_brightness b)
{
struct led_pwm *led;
unsigned long period;
period = 1000000000UL;
led = container_of(c, struct led_pwm, led);
led->period = period;
pwm_set_period_ns(led->pwm, period);
}
static enum led_brightness
led_pwm_brightness_get(struct led_classdev *c)
{
struct led_pwm *led;
led = container_of(c, struct led_pwm, led);
return led->period;
}
static int
led_pwm_blink_set(struct led_classdev *c,
unsigned long *on_ms,
unsigned long *off_ms)
{
struct led_pwm *led;
struct pwm_channel_config cfg;
led = container_of(c, struct led_pwm, led);
if (*on_ms == 0 && *off_ms == 0) {
*on_ms = 1000UL;
*off_ms = 1000UL;
}
cfg.config_mask = PWM_CONFIG_DUTY_NS
| PWM_CONFIG_PERIOD_NS;
cfg.duty_ns = *on_ms * 1000000000UL;
cfg.period_ns = (*on_ms + *off_ms) * 1000000000UL;
return pwm_config(led->pwm, &cfg);
}
static int __init
led_pwm_probe(struct platform_device *pdev)
{
struct pwm_led_platform_data *pdata = pdev->dev.platform_data;
struct led_pwm *led;
int ret;
if (!pdata || !pdata->led_info) {
return -EINVAL;
}
led = kzalloc(sizeof(*led), GFP_KERNEL);
if (!led)
return -ENOMEM;
led->pwm = pwm_request(pdata->bus_id, pdata->chan,
pdata->led_info->name);
if (!led->pwm) {
ret = -EINVAL;
goto err_pwm_request;
}
platform_set_drvdata(pdev, led);
led->led.name = pdata->led_info->name;
led->led.default_trigger = pdata->led_info->default_trigger;
led->led.brightness_set = led_pwm_brightness_set;
led->led.brightness_get = led_pwm_brightness_get;
led->led.blink_set = led_pwm_blink_set;
led->led.brightness = LED_OFF;
ret = pwm_config(led->pwm, pdata->config);
if (ret)
goto err_pwm_config;
pwm_start(led->pwm);
ret = led_classdev_register(&pdev->dev, &led->led);
if (ret < 0)
goto err_classdev_register;
return 0;
err_classdev_register:
pwm_stop(led->pwm);
err_pwm_config:
pwm_free(led->pwm);
err_pwm_request:
kfree(led);
return ret;
}
static int
led_pwm_remove(struct platform_device *pdev)
{
struct led_pwm *led = platform_get_drvdata(pdev);
led_classdev_unregister(&led->led);
if (led->pwm) {
pwm_stop(led->pwm);
pwm_free(led->pwm);
}
kfree(led);
platform_set_drvdata(pdev, NULL);
return 0;
}
static struct platform_driver led_pwm_driver = {
.driver = {
.name = "leds-pwm",
.owner = THIS_MODULE,
},
.probe = led_pwm_probe,
.remove = led_pwm_remove,
};
static int __init led_pwm_modinit(void)
{
return platform_driver_register(&led_pwm_driver);
}
late_initcall(led_pwm_modinit);
static void __exit led_pwm_modexit(void)
{
platform_driver_unregister(&led_pwm_driver);
}
module_exit(led_pwm_modexit);
MODULE_AUTHOR("Hector Oron <Hector.Oron <at> digi.com>");
MODULE_DESCRIPTION("Driver for LEDs with PWM-controlled brightness");
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:leds-pwm");
|