blob: 0daa2d21cbdec030aec4549b98fb467723c227d6 [file] [log] [blame]
Raphael Assenat22e03f32007-02-27 19:49:53 +00001/*
2 * LEDs driver for GPIOs
3 *
4 * Copyright (C) 2007 8D Technologies inc.
5 * Raphael Assenat <raph@8d.com>
Trent Piephoa7d878a2009-01-10 17:26:01 +00006 * Copyright (C) 2008 Freescale Semiconductor, Inc.
Raphael Assenat22e03f32007-02-27 19:49:53 +00007 *
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>
David Brownell00852272007-05-10 10:51:41 +010017#include <linux/workqueue.h>
18
Raphael Assenat22e03f32007-02-27 19:49:53 +000019#include <asm/gpio.h>
20
21struct gpio_led_data {
22 struct led_classdev cdev;
23 unsigned gpio;
David Brownell00852272007-05-10 10:51:41 +010024 struct work_struct work;
25 u8 new_level;
26 u8 can_sleep;
Raphael Assenat22e03f32007-02-27 19:49:53 +000027 u8 active_low;
Herbert Valerio Riedelca3259b2008-03-09 23:48:25 +000028 int (*platform_gpio_blink_set)(unsigned gpio,
29 unsigned long *delay_on, unsigned long *delay_off);
Raphael Assenat22e03f32007-02-27 19:49:53 +000030};
31
David Brownell00852272007-05-10 10:51:41 +010032static 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}
Raphael Assenat22e03f32007-02-27 19:49:53 +000039
40static 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
David Brownell306dd852008-03-27 00:59:02 +000055 /* 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 */
David Brownell00852272007-05-10 10:51:41 +010059 if (led_dat->can_sleep) {
David Brownell306dd852008-03-27 00:59:02 +000060 led_dat->new_level = level;
61 schedule_work(&led_dat->work);
David Brownell00852272007-05-10 10:51:41 +010062 } else
63 gpio_set_value(led_dat->gpio, level);
Raphael Assenat22e03f32007-02-27 19:49:53 +000064}
65
Herbert Valerio Riedelca3259b2008-03-09 23:48:25 +000066static 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
Trent Piephoa7d878a2009-01-10 17:26:01 +000075static 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
81 ret = gpio_request(template->gpio, template->name);
82 if (ret < 0)
83 return ret;
84
85 led_dat->cdev.name = template->name;
86 led_dat->cdev.default_trigger = template->default_trigger;
87 led_dat->gpio = template->gpio;
88 led_dat->can_sleep = gpio_cansleep(template->gpio);
89 led_dat->active_low = template->active_low;
90 if (blink_set) {
91 led_dat->platform_gpio_blink_set = blink_set;
92 led_dat->cdev.blink_set = gpio_blink_set;
93 }
94 led_dat->cdev.brightness_set = gpio_led_set;
95 led_dat->cdev.brightness = LED_OFF;
96 led_dat->cdev.flags |= LED_CORE_SUSPENDRESUME;
97
98 ret = gpio_direction_output(led_dat->gpio, led_dat->active_low);
99 if (ret < 0)
100 goto err;
101
102 INIT_WORK(&led_dat->work, gpio_led_work);
103
104 ret = led_classdev_register(parent, &led_dat->cdev);
105 if (ret < 0)
106 goto err;
107
108 return 0;
109err:
110 gpio_free(led_dat->gpio);
111 return ret;
112}
113
114static void delete_gpio_led(struct gpio_led_data *led)
115{
116 led_classdev_unregister(&led->cdev);
117 cancel_work_sync(&led->work);
118 gpio_free(led->gpio);
119}
120
121#ifdef CONFIG_LEDS_GPIO_PLATFORM
David Brownell199fb212007-10-31 10:37:37 +0100122static int gpio_led_probe(struct platform_device *pdev)
Raphael Assenat22e03f32007-02-27 19:49:53 +0000123{
124 struct gpio_led_platform_data *pdata = pdev->dev.platform_data;
Trent Piephoa7d878a2009-01-10 17:26:01 +0000125 struct gpio_led_data *leds_data;
Raphael Assenat22e03f32007-02-27 19:49:53 +0000126 int i, ret = 0;
127
128 if (!pdata)
129 return -EBUSY;
130
131 leds_data = kzalloc(sizeof(struct gpio_led_data) * pdata->num_leds,
132 GFP_KERNEL);
133 if (!leds_data)
134 return -ENOMEM;
135
136 for (i = 0; i < pdata->num_leds; i++) {
Trent Piephoa7d878a2009-01-10 17:26:01 +0000137 ret = create_gpio_led(&pdata->leds[i], &leds_data[i],
138 &pdev->dev, pdata->gpio_blink_set);
Uwe Kleine-Königd95cbe62008-03-09 20:42:27 +0000139 if (ret < 0)
140 goto err;
Raphael Assenat22e03f32007-02-27 19:49:53 +0000141 }
142
143 platform_set_drvdata(pdev, leds_data);
144
145 return 0;
146
147err:
Trent Piephoa7d878a2009-01-10 17:26:01 +0000148 for (i = i - 1; i >= 0; i--)
149 delete_gpio_led(&leds_data[i]);
David Brownell00852272007-05-10 10:51:41 +0100150
Raphael Assenat22e03f32007-02-27 19:49:53 +0000151 kfree(leds_data);
152
153 return ret;
154}
155
David Brownell199fb212007-10-31 10:37:37 +0100156static int __devexit gpio_led_remove(struct platform_device *pdev)
Raphael Assenat22e03f32007-02-27 19:49:53 +0000157{
158 int i;
159 struct gpio_led_platform_data *pdata = pdev->dev.platform_data;
160 struct gpio_led_data *leds_data;
161
162 leds_data = platform_get_drvdata(pdev);
163
Trent Piephoa7d878a2009-01-10 17:26:01 +0000164 for (i = 0; i < pdata->num_leds; i++)
165 delete_gpio_led(&leds_data[i]);
David Brownell199fb212007-10-31 10:37:37 +0100166
Raphael Assenat22e03f32007-02-27 19:49:53 +0000167 kfree(leds_data);
168
169 return 0;
170}
171
Raphael Assenat22e03f32007-02-27 19:49:53 +0000172static struct platform_driver gpio_led_driver = {
David Brownell199fb212007-10-31 10:37:37 +0100173 .probe = gpio_led_probe,
174 .remove = __devexit_p(gpio_led_remove),
Raphael Assenat22e03f32007-02-27 19:49:53 +0000175 .driver = {
176 .name = "leds-gpio",
177 .owner = THIS_MODULE,
178 },
179};
180
Trent Piephoa7d878a2009-01-10 17:26:01 +0000181MODULE_ALIAS("platform:leds-gpio");
182#endif /* CONFIG_LEDS_GPIO_PLATFORM */
183
184/* Code to create from OpenFirmware platform devices */
185#ifdef CONFIG_LEDS_GPIO_OF
186#include <linux/of_platform.h>
187#include <linux/of_gpio.h>
188
189struct gpio_led_of_platform_data {
190 int num_leds;
191 struct gpio_led_data led_data[];
192};
193
194static int __devinit of_gpio_leds_probe(struct of_device *ofdev,
195 const struct of_device_id *match)
196{
197 struct device_node *np = ofdev->node, *child;
198 struct gpio_led led;
199 struct gpio_led_of_platform_data *pdata;
200 int count = 0, ret;
201
202 /* count LEDs defined by this device, so we know how much to allocate */
203 for_each_child_of_node(np, child)
204 count++;
205 if (!count)
206 return 0; /* or ENODEV? */
207
208 pdata = kzalloc(sizeof(*pdata) + sizeof(struct gpio_led_data) * count,
209 GFP_KERNEL);
210 if (!pdata)
211 return -ENOMEM;
212
213 memset(&led, 0, sizeof(led));
214 for_each_child_of_node(np, child) {
215 enum of_gpio_flags flags;
216
217 led.gpio = of_get_gpio_flags(child, 0, &flags);
218 led.active_low = flags & OF_GPIO_ACTIVE_LOW;
219 led.name = of_get_property(child, "label", NULL) ? : child->name;
220 led.default_trigger =
221 of_get_property(child, "linux,default-trigger", NULL);
222
223 ret = create_gpio_led(&led, &pdata->led_data[pdata->num_leds++],
224 &ofdev->dev, NULL);
225 if (ret < 0) {
226 of_node_put(child);
227 goto err;
228 }
229 }
230
231 dev_set_drvdata(&ofdev->dev, pdata);
232
233 return 0;
234
235err:
236 for (count = pdata->num_leds - 2; count >= 0; count--)
237 delete_gpio_led(&pdata->led_data[count]);
238
239 kfree(pdata);
240
241 return ret;
242}
243
244static int __devexit of_gpio_leds_remove(struct of_device *ofdev)
245{
246 struct gpio_led_of_platform_data *pdata = dev_get_drvdata(&ofdev->dev);
247 int i;
248
249 for (i = 0; i < pdata->num_leds; i++)
250 delete_gpio_led(&pdata->led_data[i]);
251
252 kfree(pdata);
253
254 dev_set_drvdata(&ofdev->dev, NULL);
255
256 return 0;
257}
258
259static const struct of_device_id of_gpio_leds_match[] = {
260 { .compatible = "gpio-leds", },
261 {},
262};
263
264static struct of_platform_driver of_gpio_leds_driver = {
265 .driver = {
266 .name = "of_gpio_leds",
267 .owner = THIS_MODULE,
268 },
269 .match_table = of_gpio_leds_match,
270 .probe = of_gpio_leds_probe,
271 .remove = __devexit_p(of_gpio_leds_remove),
272};
Trent Piephoa7d878a2009-01-10 17:26:01 +0000273#endif
274
Richard Purdieb2bdc3e2009-02-02 23:04:42 +0000275static int __init gpio_led_init(void)
276{
277 int ret;
278
279#ifdef CONFIG_LEDS_GPIO_PLATFORM
280 ret = platform_driver_register(&gpio_led_driver);
281 if (ret)
282 return ret;
283#endif
284#ifdef CONFIG_LEDS_GPIO_OF
285 ret = of_register_platform_driver(&of_gpio_leds_driver);
286#endif
287#ifdef CONFIG_LEDS_GPIO_PLATFORM
288 if (ret)
289 platform_driver_unregister(&gpio_led_driver);
290#endif
291
292 return ret;
293}
294
295static void __exit gpio_led_exit(void)
296{
297#ifdef CONFIG_LEDS_GPIO_PLATFORM
298 platform_driver_unregister(&gpio_led_driver);
299#endif
300#ifdef CONFIG_LEDS_GPIO_OF
301 of_unregister_platform_driver(&of_gpio_leds_driver);
302#endif
303}
304
305module_init(gpio_led_init);
306module_exit(gpio_led_exit);
307
Trent Piephoa7d878a2009-01-10 17:26:01 +0000308MODULE_AUTHOR("Raphael Assenat <raph@8d.com>, Trent Piepho <tpiepho@freescale.com>");
Raphael Assenat22e03f32007-02-27 19:49:53 +0000309MODULE_DESCRIPTION("GPIO LED driver");
310MODULE_LICENSE("GPL");