]>
Commit | Line | Data |
---|---|---|
22e03f3b RA |
1 | /* |
2 | * LEDs driver for GPIOs | |
3 | * | |
4 | * Copyright (C) 2007 8D Technologies inc. | |
5 | * Raphael Assenat <raph@8d.com> | |
a7d878af | 6 | * Copyright (C) 2008 Freescale Semiconductor, Inc. |
22e03f3b RA |
7 | * |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License version 2 as | |
10 | * published by the Free Software Foundation. | |
11 | * | |
12 | */ | |
13 | #include <linux/kernel.h> | |
14 | #include <linux/init.h> | |
15 | #include <linux/platform_device.h> | |
16 | #include <linux/leds.h> | |
00852279 DB |
17 | #include <linux/workqueue.h> |
18 | ||
22e03f3b RA |
19 | #include <asm/gpio.h> |
20 | ||
21 | struct gpio_led_data { | |
22 | struct led_classdev cdev; | |
23 | unsigned gpio; | |
00852279 DB |
24 | struct work_struct work; |
25 | u8 new_level; | |
26 | u8 can_sleep; | |
22e03f3b | 27 | u8 active_low; |
ca3259b3 HVR |
28 | int (*platform_gpio_blink_set)(unsigned gpio, |
29 | unsigned long *delay_on, unsigned long *delay_off); | |
22e03f3b RA |
30 | }; |
31 | ||
00852279 DB |
32 | static void gpio_led_work(struct work_struct *work) |
33 | { | |
34 | struct gpio_led_data *led_dat = | |
35 | container_of(work, struct gpio_led_data, work); | |
36 | ||
37 | gpio_set_value_cansleep(led_dat->gpio, led_dat->new_level); | |
38 | } | |
22e03f3b RA |
39 | |
40 | static void gpio_led_set(struct led_classdev *led_cdev, | |
41 | enum led_brightness value) | |
42 | { | |
43 | struct gpio_led_data *led_dat = | |
44 | container_of(led_cdev, struct gpio_led_data, cdev); | |
45 | int level; | |
46 | ||
47 | if (value == LED_OFF) | |
48 | level = 0; | |
49 | else | |
50 | level = 1; | |
51 | ||
52 | if (led_dat->active_low) | |
53 | level = !level; | |
54 | ||
306dd85c DB |
55 | /* Setting GPIOs with I2C/etc requires a task context, and we don't |
56 | * seem to have a reliable way to know if we're already in one; so | |
57 | * let's just assume the worst. | |
58 | */ | |
00852279 | 59 | if (led_dat->can_sleep) { |
306dd85c DB |
60 | led_dat->new_level = level; |
61 | schedule_work(&led_dat->work); | |
00852279 DB |
62 | } else |
63 | gpio_set_value(led_dat->gpio, level); | |
22e03f3b RA |
64 | } |
65 | ||
ca3259b3 HVR |
66 | static int gpio_blink_set(struct led_classdev *led_cdev, |
67 | unsigned long *delay_on, unsigned long *delay_off) | |
68 | { | |
69 | struct gpio_led_data *led_dat = | |
70 | container_of(led_cdev, struct gpio_led_data, cdev); | |
71 | ||
72 | return led_dat->platform_gpio_blink_set(led_dat->gpio, delay_on, delay_off); | |
73 | } | |
74 | ||
a7d878af TP |
75 | static int __devinit create_gpio_led(const struct gpio_led *template, |
76 | struct gpio_led_data *led_dat, struct device *parent, | |
77 | int (*blink_set)(unsigned, unsigned long *, unsigned long *)) | |
78 | { | |
79 | int ret; | |
80 | ||
d379ee8a DB |
81 | /* skip leds that aren't available */ |
82 | if (!gpio_is_valid(template->gpio)) { | |
83 | printk(KERN_INFO "Skipping unavilable LED gpio %d (%s)\n", | |
84 | template->gpio, template->name); | |
ac15e950 | 85 | return 0; |
d379ee8a DB |
86 | } |
87 | ||
a7d878af TP |
88 | ret = gpio_request(template->gpio, template->name); |
89 | if (ret < 0) | |
90 | return ret; | |
91 | ||
92 | led_dat->cdev.name = template->name; | |
93 | led_dat->cdev.default_trigger = template->default_trigger; | |
94 | led_dat->gpio = template->gpio; | |
95 | led_dat->can_sleep = gpio_cansleep(template->gpio); | |
96 | led_dat->active_low = template->active_low; | |
97 | if (blink_set) { | |
98 | led_dat->platform_gpio_blink_set = blink_set; | |
99 | led_dat->cdev.blink_set = gpio_blink_set; | |
100 | } | |
101 | led_dat->cdev.brightness_set = gpio_led_set; | |
102 | led_dat->cdev.brightness = LED_OFF; | |
defb512d RP |
103 | if (!template->retain_state_suspended) |
104 | led_dat->cdev.flags |= LED_CORE_SUSPENDRESUME; | |
a7d878af TP |
105 | |
106 | ret = gpio_direction_output(led_dat->gpio, led_dat->active_low); | |
107 | if (ret < 0) | |
108 | goto err; | |
109 | ||
110 | INIT_WORK(&led_dat->work, gpio_led_work); | |
111 | ||
112 | ret = led_classdev_register(parent, &led_dat->cdev); | |
113 | if (ret < 0) | |
114 | goto err; | |
115 | ||
116 | return 0; | |
117 | err: | |
118 | gpio_free(led_dat->gpio); | |
119 | return ret; | |
120 | } | |
121 | ||
122 | static void delete_gpio_led(struct gpio_led_data *led) | |
123 | { | |
d379ee8a DB |
124 | if (!gpio_is_valid(led->gpio)) |
125 | return; | |
a7d878af TP |
126 | led_classdev_unregister(&led->cdev); |
127 | cancel_work_sync(&led->work); | |
128 | gpio_free(led->gpio); | |
129 | } | |
130 | ||
131 | #ifdef CONFIG_LEDS_GPIO_PLATFORM | |
199fb21d | 132 | static int gpio_led_probe(struct platform_device *pdev) |
22e03f3b RA |
133 | { |
134 | struct gpio_led_platform_data *pdata = pdev->dev.platform_data; | |
a7d878af | 135 | struct gpio_led_data *leds_data; |
22e03f3b RA |
136 | int i, ret = 0; |
137 | ||
138 | if (!pdata) | |
139 | return -EBUSY; | |
140 | ||
141 | leds_data = kzalloc(sizeof(struct gpio_led_data) * pdata->num_leds, | |
142 | GFP_KERNEL); | |
143 | if (!leds_data) | |
144 | return -ENOMEM; | |
145 | ||
146 | for (i = 0; i < pdata->num_leds; i++) { | |
a7d878af TP |
147 | ret = create_gpio_led(&pdata->leds[i], &leds_data[i], |
148 | &pdev->dev, pdata->gpio_blink_set); | |
d95cbe61 UKK |
149 | if (ret < 0) |
150 | goto err; | |
22e03f3b RA |
151 | } |
152 | ||
153 | platform_set_drvdata(pdev, leds_data); | |
154 | ||
155 | return 0; | |
156 | ||
157 | err: | |
a7d878af TP |
158 | for (i = i - 1; i >= 0; i--) |
159 | delete_gpio_led(&leds_data[i]); | |
00852279 | 160 | |
22e03f3b RA |
161 | kfree(leds_data); |
162 | ||
163 | return ret; | |
164 | } | |
165 | ||
199fb21d | 166 | static int __devexit gpio_led_remove(struct platform_device *pdev) |
22e03f3b RA |
167 | { |
168 | int i; | |
169 | struct gpio_led_platform_data *pdata = pdev->dev.platform_data; | |
170 | struct gpio_led_data *leds_data; | |
171 | ||
172 | leds_data = platform_get_drvdata(pdev); | |
173 | ||
a7d878af TP |
174 | for (i = 0; i < pdata->num_leds; i++) |
175 | delete_gpio_led(&leds_data[i]); | |
199fb21d | 176 | |
22e03f3b RA |
177 | kfree(leds_data); |
178 | ||
179 | return 0; | |
180 | } | |
181 | ||
22e03f3b | 182 | static struct platform_driver gpio_led_driver = { |
199fb21d DB |
183 | .probe = gpio_led_probe, |
184 | .remove = __devexit_p(gpio_led_remove), | |
22e03f3b RA |
185 | .driver = { |
186 | .name = "leds-gpio", | |
187 | .owner = THIS_MODULE, | |
188 | }, | |
189 | }; | |
190 | ||
a7d878af TP |
191 | MODULE_ALIAS("platform:leds-gpio"); |
192 | #endif /* CONFIG_LEDS_GPIO_PLATFORM */ | |
193 | ||
194 | /* Code to create from OpenFirmware platform devices */ | |
195 | #ifdef CONFIG_LEDS_GPIO_OF | |
196 | #include <linux/of_platform.h> | |
197 | #include <linux/of_gpio.h> | |
198 | ||
199 | struct gpio_led_of_platform_data { | |
200 | int num_leds; | |
201 | struct gpio_led_data led_data[]; | |
202 | }; | |
203 | ||
204 | static int __devinit of_gpio_leds_probe(struct of_device *ofdev, | |
205 | const struct of_device_id *match) | |
206 | { | |
207 | struct device_node *np = ofdev->node, *child; | |
208 | struct gpio_led led; | |
209 | struct gpio_led_of_platform_data *pdata; | |
210 | int count = 0, ret; | |
211 | ||
212 | /* count LEDs defined by this device, so we know how much to allocate */ | |
213 | for_each_child_of_node(np, child) | |
214 | count++; | |
215 | if (!count) | |
216 | return 0; /* or ENODEV? */ | |
217 | ||
218 | pdata = kzalloc(sizeof(*pdata) + sizeof(struct gpio_led_data) * count, | |
219 | GFP_KERNEL); | |
220 | if (!pdata) | |
221 | return -ENOMEM; | |
222 | ||
223 | memset(&led, 0, sizeof(led)); | |
224 | for_each_child_of_node(np, child) { | |
225 | enum of_gpio_flags flags; | |
226 | ||
227 | led.gpio = of_get_gpio_flags(child, 0, &flags); | |
228 | led.active_low = flags & OF_GPIO_ACTIVE_LOW; | |
229 | led.name = of_get_property(child, "label", NULL) ? : child->name; | |
230 | led.default_trigger = | |
231 | of_get_property(child, "linux,default-trigger", NULL); | |
232 | ||
233 | ret = create_gpio_led(&led, &pdata->led_data[pdata->num_leds++], | |
234 | &ofdev->dev, NULL); | |
235 | if (ret < 0) { | |
236 | of_node_put(child); | |
237 | goto err; | |
238 | } | |
239 | } | |
240 | ||
241 | dev_set_drvdata(&ofdev->dev, pdata); | |
242 | ||
243 | return 0; | |
244 | ||
245 | err: | |
246 | for (count = pdata->num_leds - 2; count >= 0; count--) | |
247 | delete_gpio_led(&pdata->led_data[count]); | |
248 | ||
249 | kfree(pdata); | |
250 | ||
251 | return ret; | |
252 | } | |
253 | ||
254 | static int __devexit of_gpio_leds_remove(struct of_device *ofdev) | |
255 | { | |
256 | struct gpio_led_of_platform_data *pdata = dev_get_drvdata(&ofdev->dev); | |
257 | int i; | |
258 | ||
259 | for (i = 0; i < pdata->num_leds; i++) | |
260 | delete_gpio_led(&pdata->led_data[i]); | |
261 | ||
262 | kfree(pdata); | |
263 | ||
264 | dev_set_drvdata(&ofdev->dev, NULL); | |
265 | ||
266 | return 0; | |
267 | } | |
268 | ||
269 | static const struct of_device_id of_gpio_leds_match[] = { | |
270 | { .compatible = "gpio-leds", }, | |
271 | {}, | |
272 | }; | |
273 | ||
274 | static struct of_platform_driver of_gpio_leds_driver = { | |
275 | .driver = { | |
276 | .name = "of_gpio_leds", | |
277 | .owner = THIS_MODULE, | |
278 | }, | |
279 | .match_table = of_gpio_leds_match, | |
280 | .probe = of_gpio_leds_probe, | |
281 | .remove = __devexit_p(of_gpio_leds_remove), | |
282 | }; | |
b2bdc3e7 | 283 | #endif |
a7d878af | 284 | |
b2bdc3e7 | 285 | static int __init gpio_led_init(void) |
a7d878af | 286 | { |
b2bdc3e7 RP |
287 | int ret; |
288 | ||
289 | #ifdef CONFIG_LEDS_GPIO_PLATFORM | |
290 | ret = platform_driver_register(&gpio_led_driver); | |
291 | if (ret) | |
292 | return ret; | |
293 | #endif | |
294 | #ifdef CONFIG_LEDS_GPIO_OF | |
295 | ret = of_register_platform_driver(&of_gpio_leds_driver); | |
296 | #endif | |
297 | #ifdef CONFIG_LEDS_GPIO_PLATFORM | |
298 | if (ret) | |
299 | platform_driver_unregister(&gpio_led_driver); | |
300 | #endif | |
301 | ||
302 | return ret; | |
a7d878af | 303 | } |
a7d878af | 304 | |
b2bdc3e7 | 305 | static void __exit gpio_led_exit(void) |
a7d878af | 306 | { |
b2bdc3e7 RP |
307 | #ifdef CONFIG_LEDS_GPIO_PLATFORM |
308 | platform_driver_unregister(&gpio_led_driver); | |
309 | #endif | |
310 | #ifdef CONFIG_LEDS_GPIO_OF | |
a7d878af | 311 | of_unregister_platform_driver(&of_gpio_leds_driver); |
a7d878af | 312 | #endif |
b2bdc3e7 RP |
313 | } |
314 | ||
315 | module_init(gpio_led_init); | |
316 | module_exit(gpio_led_exit); | |
a7d878af TP |
317 | |
318 | MODULE_AUTHOR("Raphael Assenat <raph@8d.com>, Trent Piepho <tpiepho@freescale.com>"); | |
22e03f3b RA |
319 | MODULE_DESCRIPTION("GPIO LED driver"); |
320 | MODULE_LICENSE("GPL"); |