blob: 4d9fa38d9ff6a3cffb2063823c92b0d147ce13d8 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
David Brownell00852272007-05-10 10:51:41 +010018#include <linux/workqueue.h>
19
Raphael Assenat22e03f32007-02-27 19:49:53 +000020#include <asm/gpio.h>
21
22struct gpio_led_data {
23 struct led_classdev cdev;
24 unsigned gpio;
David Brownell00852272007-05-10 10:51:41 +010025 struct work_struct work;
26 u8 new_level;
27 u8 can_sleep;
Raphael Assenat22e03f32007-02-27 19:49:53 +000028 u8 active_low;
Benjamin Herrenschmidt21463252010-05-22 20:54:55 +100029 u8 blinking;
30 int (*platform_gpio_blink_set)(unsigned gpio, int state,
Herbert Valerio Riedelca3259b2008-03-09 23:48:25 +000031 unsigned long *delay_on, unsigned long *delay_off);
Raphael Assenat22e03f32007-02-27 19:49:53 +000032};
33
David Brownell00852272007-05-10 10:51:41 +010034static void gpio_led_work(struct work_struct *work)
35{
36 struct gpio_led_data *led_dat =
37 container_of(work, struct gpio_led_data, work);
38
Benjamin Herrenschmidt21463252010-05-22 20:54:55 +100039 if (led_dat->blinking) {
40 led_dat->platform_gpio_blink_set(led_dat->gpio,
41 led_dat->new_level,
42 NULL, NULL);
43 led_dat->blinking = 0;
44 } else
45 gpio_set_value_cansleep(led_dat->gpio, led_dat->new_level);
David Brownell00852272007-05-10 10:51:41 +010046}
Raphael Assenat22e03f32007-02-27 19:49:53 +000047
48static void gpio_led_set(struct led_classdev *led_cdev,
49 enum led_brightness value)
50{
51 struct gpio_led_data *led_dat =
52 container_of(led_cdev, struct gpio_led_data, cdev);
53 int level;
54
55 if (value == LED_OFF)
56 level = 0;
57 else
58 level = 1;
59
60 if (led_dat->active_low)
61 level = !level;
62
David Brownell306dd852008-03-27 00:59:02 +000063 /* Setting GPIOs with I2C/etc requires a task context, and we don't
64 * seem to have a reliable way to know if we're already in one; so
65 * let's just assume the worst.
66 */
David Brownell00852272007-05-10 10:51:41 +010067 if (led_dat->can_sleep) {
David Brownell306dd852008-03-27 00:59:02 +000068 led_dat->new_level = level;
69 schedule_work(&led_dat->work);
Benjamin Herrenschmidt21463252010-05-22 20:54:55 +100070 } else {
71 if (led_dat->blinking) {
72 led_dat->platform_gpio_blink_set(led_dat->gpio, level,
73 NULL, NULL);
74 led_dat->blinking = 0;
75 } else
76 gpio_set_value(led_dat->gpio, level);
77 }
Raphael Assenat22e03f32007-02-27 19:49:53 +000078}
79
Herbert Valerio Riedelca3259b2008-03-09 23:48:25 +000080static int gpio_blink_set(struct led_classdev *led_cdev,
81 unsigned long *delay_on, unsigned long *delay_off)
82{
83 struct gpio_led_data *led_dat =
84 container_of(led_cdev, struct gpio_led_data, cdev);
85
Benjamin Herrenschmidt21463252010-05-22 20:54:55 +100086 led_dat->blinking = 1;
87 return led_dat->platform_gpio_blink_set(led_dat->gpio, GPIO_LED_BLINK,
88 delay_on, delay_off);
Herbert Valerio Riedelca3259b2008-03-09 23:48:25 +000089}
90
Trent Piephoa7d878a2009-01-10 17:26:01 +000091static int __devinit create_gpio_led(const struct gpio_led *template,
92 struct gpio_led_data *led_dat, struct device *parent,
Benjamin Herrenschmidt21463252010-05-22 20:54:55 +100093 int (*blink_set)(unsigned, int, unsigned long *, unsigned long *))
Trent Piephoa7d878a2009-01-10 17:26:01 +000094{
Trent Piephoed88bae2009-05-12 15:33:12 -070095 int ret, state;
Trent Piephoa7d878a2009-01-10 17:26:01 +000096
Dmitry Eremin-Solenikov0b4634f2009-11-16 01:48:32 +030097 led_dat->gpio = -1;
98
David Brownelld379ee82009-03-05 16:46:44 -080099 /* skip leds that aren't available */
100 if (!gpio_is_valid(template->gpio)) {
Michal Simek2fea0922009-08-06 16:04:51 -0700101 printk(KERN_INFO "Skipping unavailable LED gpio %d (%s)\n",
David Brownelld379ee82009-03-05 16:46:44 -0800102 template->gpio, template->name);
David Brownellac15e952009-04-07 17:51:49 -0700103 return 0;
David Brownelld379ee82009-03-05 16:46:44 -0800104 }
105
Trent Piephoa7d878a2009-01-10 17:26:01 +0000106 ret = gpio_request(template->gpio, template->name);
107 if (ret < 0)
108 return ret;
109
110 led_dat->cdev.name = template->name;
111 led_dat->cdev.default_trigger = template->default_trigger;
112 led_dat->gpio = template->gpio;
113 led_dat->can_sleep = gpio_cansleep(template->gpio);
114 led_dat->active_low = template->active_low;
Benjamin Herrenschmidt21463252010-05-22 20:54:55 +1000115 led_dat->blinking = 0;
Trent Piephoa7d878a2009-01-10 17:26:01 +0000116 if (blink_set) {
117 led_dat->platform_gpio_blink_set = blink_set;
118 led_dat->cdev.blink_set = gpio_blink_set;
119 }
120 led_dat->cdev.brightness_set = gpio_led_set;
Trent Piephoed88bae2009-05-12 15:33:12 -0700121 if (template->default_state == LEDS_GPIO_DEFSTATE_KEEP)
122 state = !!gpio_get_value(led_dat->gpio) ^ led_dat->active_low;
123 else
124 state = (template->default_state == LEDS_GPIO_DEFSTATE_ON);
125 led_dat->cdev.brightness = state ? LED_FULL : LED_OFF;
Richard Purdiedefb5122009-02-17 15:04:07 +0000126 if (!template->retain_state_suspended)
127 led_dat->cdev.flags |= LED_CORE_SUSPENDRESUME;
Trent Piephoa7d878a2009-01-10 17:26:01 +0000128
Trent Piephoed88bae2009-05-12 15:33:12 -0700129 ret = gpio_direction_output(led_dat->gpio, led_dat->active_low ^ state);
Trent Piephoa7d878a2009-01-10 17:26:01 +0000130 if (ret < 0)
131 goto err;
Benjamin Herrenschmidt21463252010-05-22 20:54:55 +1000132
Trent Piephoa7d878a2009-01-10 17:26:01 +0000133 INIT_WORK(&led_dat->work, gpio_led_work);
134
135 ret = led_classdev_register(parent, &led_dat->cdev);
136 if (ret < 0)
137 goto err;
138
139 return 0;
140err:
141 gpio_free(led_dat->gpio);
142 return ret;
143}
144
145static void delete_gpio_led(struct gpio_led_data *led)
146{
David Brownelld379ee82009-03-05 16:46:44 -0800147 if (!gpio_is_valid(led->gpio))
148 return;
Trent Piephoa7d878a2009-01-10 17:26:01 +0000149 led_classdev_unregister(&led->cdev);
150 cancel_work_sync(&led->work);
151 gpio_free(led->gpio);
152}
153
154#ifdef CONFIG_LEDS_GPIO_PLATFORM
Zhenwen Xu7fd02172009-06-10 12:48:51 -0700155static int __devinit gpio_led_probe(struct platform_device *pdev)
Raphael Assenat22e03f32007-02-27 19:49:53 +0000156{
157 struct gpio_led_platform_data *pdata = pdev->dev.platform_data;
Trent Piephoa7d878a2009-01-10 17:26:01 +0000158 struct gpio_led_data *leds_data;
Raphael Assenat22e03f32007-02-27 19:49:53 +0000159 int i, ret = 0;
160
161 if (!pdata)
162 return -EBUSY;
163
164 leds_data = kzalloc(sizeof(struct gpio_led_data) * pdata->num_leds,
165 GFP_KERNEL);
166 if (!leds_data)
167 return -ENOMEM;
168
169 for (i = 0; i < pdata->num_leds; i++) {
Trent Piephoa7d878a2009-01-10 17:26:01 +0000170 ret = create_gpio_led(&pdata->leds[i], &leds_data[i],
171 &pdev->dev, pdata->gpio_blink_set);
Uwe Kleine-Königd95cbe62008-03-09 20:42:27 +0000172 if (ret < 0)
173 goto err;
Raphael Assenat22e03f32007-02-27 19:49:53 +0000174 }
175
176 platform_set_drvdata(pdev, leds_data);
177
178 return 0;
179
180err:
Trent Piephoa7d878a2009-01-10 17:26:01 +0000181 for (i = i - 1; i >= 0; i--)
182 delete_gpio_led(&leds_data[i]);
David Brownell00852272007-05-10 10:51:41 +0100183
Raphael Assenat22e03f32007-02-27 19:49:53 +0000184 kfree(leds_data);
185
186 return ret;
187}
188
David Brownell199fb212007-10-31 10:37:37 +0100189static int __devexit gpio_led_remove(struct platform_device *pdev)
Raphael Assenat22e03f32007-02-27 19:49:53 +0000190{
191 int i;
192 struct gpio_led_platform_data *pdata = pdev->dev.platform_data;
193 struct gpio_led_data *leds_data;
194
195 leds_data = platform_get_drvdata(pdev);
196
Trent Piephoa7d878a2009-01-10 17:26:01 +0000197 for (i = 0; i < pdata->num_leds; i++)
198 delete_gpio_led(&leds_data[i]);
David Brownell199fb212007-10-31 10:37:37 +0100199
Raphael Assenat22e03f32007-02-27 19:49:53 +0000200 kfree(leds_data);
201
202 return 0;
203}
204
Raphael Assenat22e03f32007-02-27 19:49:53 +0000205static struct platform_driver gpio_led_driver = {
David Brownell199fb212007-10-31 10:37:37 +0100206 .probe = gpio_led_probe,
207 .remove = __devexit_p(gpio_led_remove),
Raphael Assenat22e03f32007-02-27 19:49:53 +0000208 .driver = {
209 .name = "leds-gpio",
210 .owner = THIS_MODULE,
211 },
212};
213
Trent Piephoa7d878a2009-01-10 17:26:01 +0000214MODULE_ALIAS("platform:leds-gpio");
215#endif /* CONFIG_LEDS_GPIO_PLATFORM */
216
217/* Code to create from OpenFirmware platform devices */
218#ifdef CONFIG_LEDS_GPIO_OF
219#include <linux/of_platform.h>
220#include <linux/of_gpio.h>
221
222struct gpio_led_of_platform_data {
223 int num_leds;
224 struct gpio_led_data led_data[];
225};
226
Grant Likely2dc11582010-08-06 09:25:50 -0600227static int __devinit of_gpio_leds_probe(struct platform_device *ofdev,
Trent Piephoa7d878a2009-01-10 17:26:01 +0000228 const struct of_device_id *match)
229{
Grant Likely61c7a082010-04-13 16:12:29 -0700230 struct device_node *np = ofdev->dev.of_node, *child;
Trent Piephoa7d878a2009-01-10 17:26:01 +0000231 struct gpio_led_of_platform_data *pdata;
232 int count = 0, ret;
233
234 /* count LEDs defined by this device, so we know how much to allocate */
235 for_each_child_of_node(np, child)
236 count++;
237 if (!count)
238 return 0; /* or ENODEV? */
239
240 pdata = kzalloc(sizeof(*pdata) + sizeof(struct gpio_led_data) * count,
241 GFP_KERNEL);
242 if (!pdata)
243 return -ENOMEM;
244
Trent Piephoa7d878a2009-01-10 17:26:01 +0000245 for_each_child_of_node(np, child) {
Anton Vorontsov0493a4f2010-03-11 13:58:47 -0800246 struct gpio_led led = {};
Trent Piephoa7d878a2009-01-10 17:26:01 +0000247 enum of_gpio_flags flags;
Trent Piephoed88bae2009-05-12 15:33:12 -0700248 const char *state;
Trent Piephoa7d878a2009-01-10 17:26:01 +0000249
250 led.gpio = of_get_gpio_flags(child, 0, &flags);
251 led.active_low = flags & OF_GPIO_ACTIVE_LOW;
252 led.name = of_get_property(child, "label", NULL) ? : child->name;
253 led.default_trigger =
254 of_get_property(child, "linux,default-trigger", NULL);
Trent Piephoed88bae2009-05-12 15:33:12 -0700255 state = of_get_property(child, "default-state", NULL);
256 if (state) {
257 if (!strcmp(state, "keep"))
258 led.default_state = LEDS_GPIO_DEFSTATE_KEEP;
259 else if(!strcmp(state, "on"))
260 led.default_state = LEDS_GPIO_DEFSTATE_ON;
261 else
262 led.default_state = LEDS_GPIO_DEFSTATE_OFF;
263 }
Trent Piephoa7d878a2009-01-10 17:26:01 +0000264
265 ret = create_gpio_led(&led, &pdata->led_data[pdata->num_leds++],
266 &ofdev->dev, NULL);
267 if (ret < 0) {
268 of_node_put(child);
269 goto err;
270 }
271 }
272
273 dev_set_drvdata(&ofdev->dev, pdata);
274
275 return 0;
276
277err:
278 for (count = pdata->num_leds - 2; count >= 0; count--)
279 delete_gpio_led(&pdata->led_data[count]);
280
281 kfree(pdata);
282
283 return ret;
284}
285
Grant Likely2dc11582010-08-06 09:25:50 -0600286static int __devexit of_gpio_leds_remove(struct platform_device *ofdev)
Trent Piephoa7d878a2009-01-10 17:26:01 +0000287{
288 struct gpio_led_of_platform_data *pdata = dev_get_drvdata(&ofdev->dev);
289 int i;
290
291 for (i = 0; i < pdata->num_leds; i++)
292 delete_gpio_led(&pdata->led_data[i]);
293
294 kfree(pdata);
295
296 dev_set_drvdata(&ofdev->dev, NULL);
297
298 return 0;
299}
300
301static const struct of_device_id of_gpio_leds_match[] = {
302 { .compatible = "gpio-leds", },
303 {},
304};
305
306static struct of_platform_driver of_gpio_leds_driver = {
307 .driver = {
308 .name = "of_gpio_leds",
309 .owner = THIS_MODULE,
Grant Likely40182942010-04-13 16:13:02 -0700310 .of_match_table = of_gpio_leds_match,
Trent Piephoa7d878a2009-01-10 17:26:01 +0000311 },
Trent Piephoa7d878a2009-01-10 17:26:01 +0000312 .probe = of_gpio_leds_probe,
313 .remove = __devexit_p(of_gpio_leds_remove),
314};
Trent Piephoa7d878a2009-01-10 17:26:01 +0000315#endif
316
Richard Purdieb2bdc3e2009-02-02 23:04:42 +0000317static int __init gpio_led_init(void)
318{
Davidlohr Bueso25672b92010-11-11 14:05:24 -0800319 int ret = 0;
Richard Purdieb2bdc3e2009-02-02 23:04:42 +0000320
321#ifdef CONFIG_LEDS_GPIO_PLATFORM
322 ret = platform_driver_register(&gpio_led_driver);
323 if (ret)
324 return ret;
325#endif
326#ifdef CONFIG_LEDS_GPIO_OF
327 ret = of_register_platform_driver(&of_gpio_leds_driver);
328#endif
329#ifdef CONFIG_LEDS_GPIO_PLATFORM
330 if (ret)
331 platform_driver_unregister(&gpio_led_driver);
332#endif
333
334 return ret;
335}
336
337static void __exit gpio_led_exit(void)
338{
339#ifdef CONFIG_LEDS_GPIO_PLATFORM
340 platform_driver_unregister(&gpio_led_driver);
341#endif
342#ifdef CONFIG_LEDS_GPIO_OF
343 of_unregister_platform_driver(&of_gpio_leds_driver);
344#endif
345}
346
347module_init(gpio_led_init);
348module_exit(gpio_led_exit);
349
Trent Piephoa7d878a2009-01-10 17:26:01 +0000350MODULE_AUTHOR("Raphael Assenat <raph@8d.com>, Trent Piepho <tpiepho@freescale.com>");
Raphael Assenat22e03f32007-02-27 19:49:53 +0000351MODULE_DESCRIPTION("GPIO LED driver");
352MODULE_LICENSE("GPL");