blob: 26843dd6b85937068379da4b67f2d4e30e5d3f6a [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
227static int __devinit of_gpio_leds_probe(struct of_device *ofdev,
228 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;
Benjamin Herrenschmidt21463252010-05-22 20:54:55 +1000253 led.blinking = 0;
Trent Piephoa7d878a2009-01-10 17:26:01 +0000254 led.default_trigger =
255 of_get_property(child, "linux,default-trigger", NULL);
Trent Piephoed88bae2009-05-12 15:33:12 -0700256 state = of_get_property(child, "default-state", NULL);
257 if (state) {
258 if (!strcmp(state, "keep"))
259 led.default_state = LEDS_GPIO_DEFSTATE_KEEP;
260 else if(!strcmp(state, "on"))
261 led.default_state = LEDS_GPIO_DEFSTATE_ON;
262 else
263 led.default_state = LEDS_GPIO_DEFSTATE_OFF;
264 }
Trent Piephoa7d878a2009-01-10 17:26:01 +0000265
266 ret = create_gpio_led(&led, &pdata->led_data[pdata->num_leds++],
267 &ofdev->dev, NULL);
268 if (ret < 0) {
269 of_node_put(child);
270 goto err;
271 }
272 }
273
274 dev_set_drvdata(&ofdev->dev, pdata);
275
276 return 0;
277
278err:
279 for (count = pdata->num_leds - 2; count >= 0; count--)
280 delete_gpio_led(&pdata->led_data[count]);
281
282 kfree(pdata);
283
284 return ret;
285}
286
287static int __devexit of_gpio_leds_remove(struct of_device *ofdev)
288{
289 struct gpio_led_of_platform_data *pdata = dev_get_drvdata(&ofdev->dev);
290 int i;
291
292 for (i = 0; i < pdata->num_leds; i++)
293 delete_gpio_led(&pdata->led_data[i]);
294
295 kfree(pdata);
296
297 dev_set_drvdata(&ofdev->dev, NULL);
298
299 return 0;
300}
301
302static const struct of_device_id of_gpio_leds_match[] = {
303 { .compatible = "gpio-leds", },
304 {},
305};
306
307static struct of_platform_driver of_gpio_leds_driver = {
308 .driver = {
309 .name = "of_gpio_leds",
310 .owner = THIS_MODULE,
Grant Likely40182942010-04-13 16:13:02 -0700311 .of_match_table = of_gpio_leds_match,
Trent Piephoa7d878a2009-01-10 17:26:01 +0000312 },
Trent Piephoa7d878a2009-01-10 17:26:01 +0000313 .probe = of_gpio_leds_probe,
314 .remove = __devexit_p(of_gpio_leds_remove),
315};
Trent Piephoa7d878a2009-01-10 17:26:01 +0000316#endif
317
Richard Purdieb2bdc3e2009-02-02 23:04:42 +0000318static int __init gpio_led_init(void)
319{
320 int ret;
321
322#ifdef CONFIG_LEDS_GPIO_PLATFORM
323 ret = platform_driver_register(&gpio_led_driver);
324 if (ret)
325 return ret;
326#endif
327#ifdef CONFIG_LEDS_GPIO_OF
328 ret = of_register_platform_driver(&of_gpio_leds_driver);
329#endif
330#ifdef CONFIG_LEDS_GPIO_PLATFORM
331 if (ret)
332 platform_driver_unregister(&gpio_led_driver);
333#endif
334
335 return ret;
336}
337
338static void __exit gpio_led_exit(void)
339{
340#ifdef CONFIG_LEDS_GPIO_PLATFORM
341 platform_driver_unregister(&gpio_led_driver);
342#endif
343#ifdef CONFIG_LEDS_GPIO_OF
344 of_unregister_platform_driver(&of_gpio_leds_driver);
345#endif
346}
347
348module_init(gpio_led_init);
349module_exit(gpio_led_exit);
350
Trent Piephoa7d878a2009-01-10 17:26:01 +0000351MODULE_AUTHOR("Raphael Assenat <raph@8d.com>, Trent Piepho <tpiepho@freescale.com>");
Raphael Assenat22e03f32007-02-27 19:49:53 +0000352MODULE_DESCRIPTION("GPIO LED driver");
353MODULE_LICENSE("GPL");