2 * LEDs driver for GPIOs
4 * Copyright (C) 2007 8D Technologies inc.
5 * Raphael Assenat <raph@8d.com>
6 * Copyright (C) 2008 Freescale Semiconductor, Inc.
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.
13 #include <linux/kernel.h>
14 #include <linux/init.h>
15 #include <linux/platform_device.h>
16 #include <linux/leds.h>
17 #include <linux/workqueue.h>
21 struct gpio_led_data
{
22 struct led_classdev cdev
;
24 struct work_struct work
;
28 int (*platform_gpio_blink_set
)(unsigned gpio
,
29 unsigned long *delay_on
, unsigned long *delay_off
);
32 static void gpio_led_work(struct work_struct
*work
)
34 struct gpio_led_data
*led_dat
=
35 container_of(work
, struct gpio_led_data
, work
);
37 gpio_set_value_cansleep(led_dat
->gpio
, led_dat
->new_level
);
40 static void gpio_led_set(struct led_classdev
*led_cdev
,
41 enum led_brightness value
)
43 struct gpio_led_data
*led_dat
=
44 container_of(led_cdev
, struct gpio_led_data
, cdev
);
52 if (led_dat
->active_low
)
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.
59 if (led_dat
->can_sleep
) {
60 led_dat
->new_level
= level
;
61 schedule_work(&led_dat
->work
);
63 gpio_set_value(led_dat
->gpio
, level
);
66 static int gpio_blink_set(struct led_classdev
*led_cdev
,
67 unsigned long *delay_on
, unsigned long *delay_off
)
69 struct gpio_led_data
*led_dat
=
70 container_of(led_cdev
, struct gpio_led_data
, cdev
);
72 return led_dat
->platform_gpio_blink_set(led_dat
->gpio
, delay_on
, delay_off
);
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 *))
83 /* skip leds that aren't available */
84 if (!gpio_is_valid(template->gpio
)) {
85 printk(KERN_INFO
"Skipping unavailable LED gpio %d (%s)\n",
86 template->gpio
, template->name
);
90 ret
= gpio_request(template->gpio
, template->name
);
94 led_dat
->cdev
.name
= template->name
;
95 led_dat
->cdev
.default_trigger
= template->default_trigger
;
96 led_dat
->gpio
= template->gpio
;
97 led_dat
->can_sleep
= gpio_cansleep(template->gpio
);
98 led_dat
->active_low
= template->active_low
;
100 led_dat
->platform_gpio_blink_set
= blink_set
;
101 led_dat
->cdev
.blink_set
= gpio_blink_set
;
103 led_dat
->cdev
.brightness_set
= gpio_led_set
;
104 if (template->default_state
== LEDS_GPIO_DEFSTATE_KEEP
)
105 state
= !!gpio_get_value(led_dat
->gpio
) ^ led_dat
->active_low
;
107 state
= (template->default_state
== LEDS_GPIO_DEFSTATE_ON
);
108 led_dat
->cdev
.brightness
= state
? LED_FULL
: LED_OFF
;
109 if (!template->retain_state_suspended
)
110 led_dat
->cdev
.flags
|= LED_CORE_SUSPENDRESUME
;
112 ret
= gpio_direction_output(led_dat
->gpio
, led_dat
->active_low
^ state
);
116 INIT_WORK(&led_dat
->work
, gpio_led_work
);
118 ret
= led_classdev_register(parent
, &led_dat
->cdev
);
124 gpio_free(led_dat
->gpio
);
128 static void delete_gpio_led(struct gpio_led_data
*led
)
130 if (!gpio_is_valid(led
->gpio
))
132 led_classdev_unregister(&led
->cdev
);
133 cancel_work_sync(&led
->work
);
134 gpio_free(led
->gpio
);
137 #ifdef CONFIG_LEDS_GPIO_PLATFORM
138 static int __devinit
gpio_led_probe(struct platform_device
*pdev
)
140 struct gpio_led_platform_data
*pdata
= pdev
->dev
.platform_data
;
141 struct gpio_led_data
*leds_data
;
147 leds_data
= kzalloc(sizeof(struct gpio_led_data
) * pdata
->num_leds
,
152 for (i
= 0; i
< pdata
->num_leds
; i
++) {
153 ret
= create_gpio_led(&pdata
->leds
[i
], &leds_data
[i
],
154 &pdev
->dev
, pdata
->gpio_blink_set
);
159 platform_set_drvdata(pdev
, leds_data
);
164 for (i
= i
- 1; i
>= 0; i
--)
165 delete_gpio_led(&leds_data
[i
]);
172 static int __devexit
gpio_led_remove(struct platform_device
*pdev
)
175 struct gpio_led_platform_data
*pdata
= pdev
->dev
.platform_data
;
176 struct gpio_led_data
*leds_data
;
178 leds_data
= platform_get_drvdata(pdev
);
180 for (i
= 0; i
< pdata
->num_leds
; i
++)
181 delete_gpio_led(&leds_data
[i
]);
188 static struct platform_driver gpio_led_driver
= {
189 .probe
= gpio_led_probe
,
190 .remove
= __devexit_p(gpio_led_remove
),
193 .owner
= THIS_MODULE
,
197 MODULE_ALIAS("platform:leds-gpio");
198 #endif /* CONFIG_LEDS_GPIO_PLATFORM */
200 /* Code to create from OpenFirmware platform devices */
201 #ifdef CONFIG_LEDS_GPIO_OF
202 #include <linux/of_platform.h>
203 #include <linux/of_gpio.h>
205 struct gpio_led_of_platform_data
{
207 struct gpio_led_data led_data
[];
210 static int __devinit
of_gpio_leds_probe(struct of_device
*ofdev
,
211 const struct of_device_id
*match
)
213 struct device_node
*np
= ofdev
->node
, *child
;
214 struct gpio_led_of_platform_data
*pdata
;
217 /* count LEDs defined by this device, so we know how much to allocate */
218 for_each_child_of_node(np
, child
)
221 return 0; /* or ENODEV? */
223 pdata
= kzalloc(sizeof(*pdata
) + sizeof(struct gpio_led_data
) * count
,
228 for_each_child_of_node(np
, child
) {
229 struct gpio_led led
= {};
230 enum of_gpio_flags flags
;
233 led
.gpio
= of_get_gpio_flags(child
, 0, &flags
);
234 led
.active_low
= flags
& OF_GPIO_ACTIVE_LOW
;
235 led
.name
= of_get_property(child
, "label", NULL
) ? : child
->name
;
236 led
.default_trigger
=
237 of_get_property(child
, "linux,default-trigger", NULL
);
238 state
= of_get_property(child
, "default-state", NULL
);
240 if (!strcmp(state
, "keep"))
241 led
.default_state
= LEDS_GPIO_DEFSTATE_KEEP
;
242 else if(!strcmp(state
, "on"))
243 led
.default_state
= LEDS_GPIO_DEFSTATE_ON
;
245 led
.default_state
= LEDS_GPIO_DEFSTATE_OFF
;
248 ret
= create_gpio_led(&led
, &pdata
->led_data
[pdata
->num_leds
++],
256 dev_set_drvdata(&ofdev
->dev
, pdata
);
261 for (count
= pdata
->num_leds
- 2; count
>= 0; count
--)
262 delete_gpio_led(&pdata
->led_data
[count
]);
269 static int __devexit
of_gpio_leds_remove(struct of_device
*ofdev
)
271 struct gpio_led_of_platform_data
*pdata
= dev_get_drvdata(&ofdev
->dev
);
274 for (i
= 0; i
< pdata
->num_leds
; i
++)
275 delete_gpio_led(&pdata
->led_data
[i
]);
279 dev_set_drvdata(&ofdev
->dev
, NULL
);
284 static const struct of_device_id of_gpio_leds_match
[] = {
285 { .compatible
= "gpio-leds", },
289 static struct of_platform_driver of_gpio_leds_driver
= {
291 .name
= "of_gpio_leds",
292 .owner
= THIS_MODULE
,
294 .match_table
= of_gpio_leds_match
,
295 .probe
= of_gpio_leds_probe
,
296 .remove
= __devexit_p(of_gpio_leds_remove
),
300 static int __init
gpio_led_init(void)
304 #ifdef CONFIG_LEDS_GPIO_PLATFORM
305 ret
= platform_driver_register(&gpio_led_driver
);
309 #ifdef CONFIG_LEDS_GPIO_OF
310 ret
= of_register_platform_driver(&of_gpio_leds_driver
);
312 #ifdef CONFIG_LEDS_GPIO_PLATFORM
314 platform_driver_unregister(&gpio_led_driver
);
320 static void __exit
gpio_led_exit(void)
322 #ifdef CONFIG_LEDS_GPIO_PLATFORM
323 platform_driver_unregister(&gpio_led_driver
);
325 #ifdef CONFIG_LEDS_GPIO_OF
326 of_unregister_platform_driver(&of_gpio_leds_driver
);
330 module_init(gpio_led_init
);
331 module_exit(gpio_led_exit
);
333 MODULE_AUTHOR("Raphael Assenat <raph@8d.com>, Trent Piepho <tpiepho@freescale.com>");
334 MODULE_DESCRIPTION("GPIO LED driver");
335 MODULE_LICENSE("GPL");