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/slab.h>
18 #include <linux/workqueue.h>
22 struct gpio_led_data
{
23 struct led_classdev cdev
;
25 struct work_struct work
;
30 int (*platform_gpio_blink_set
)(unsigned gpio
, int state
,
31 unsigned long *delay_on
, unsigned long *delay_off
);
34 static void gpio_led_work(struct work_struct
*work
)
36 struct gpio_led_data
*led_dat
=
37 container_of(work
, struct gpio_led_data
, work
);
39 if (led_dat
->blinking
) {
40 led_dat
->platform_gpio_blink_set(led_dat
->gpio
,
43 led_dat
->blinking
= 0;
45 gpio_set_value_cansleep(led_dat
->gpio
, led_dat
->new_level
);
48 static void gpio_led_set(struct led_classdev
*led_cdev
,
49 enum led_brightness value
)
51 struct gpio_led_data
*led_dat
=
52 container_of(led_cdev
, struct gpio_led_data
, cdev
);
60 if (led_dat
->active_low
)
63 /* 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.
67 if (led_dat
->can_sleep
) {
68 led_dat
->new_level
= level
;
69 schedule_work(&led_dat
->work
);
71 if (led_dat
->blinking
) {
72 led_dat
->platform_gpio_blink_set(led_dat
->gpio
, level
,
74 led_dat
->blinking
= 0;
76 gpio_set_value(led_dat
->gpio
, level
);
80 static int gpio_blink_set(struct led_classdev
*led_cdev
,
81 unsigned long *delay_on
, unsigned long *delay_off
)
83 struct gpio_led_data
*led_dat
=
84 container_of(led_cdev
, struct gpio_led_data
, cdev
);
86 led_dat
->blinking
= 1;
87 return led_dat
->platform_gpio_blink_set(led_dat
->gpio
, GPIO_LED_BLINK
,
91 static int __devinit
create_gpio_led(const struct gpio_led
*template,
92 struct gpio_led_data
*led_dat
, struct device
*parent
,
93 int (*blink_set
)(unsigned, int, unsigned long *, unsigned long *))
99 /* skip leds that aren't available */
100 if (!gpio_is_valid(template->gpio
)) {
101 printk(KERN_INFO
"Skipping unavailable LED gpio %d (%s)\n",
102 template->gpio
, template->name
);
106 ret
= gpio_request(template->gpio
, template->name
);
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
;
115 led_dat
->blinking
= 0;
117 led_dat
->platform_gpio_blink_set
= blink_set
;
118 led_dat
->cdev
.blink_set
= gpio_blink_set
;
120 led_dat
->cdev
.brightness_set
= gpio_led_set
;
121 if (template->default_state
== LEDS_GPIO_DEFSTATE_KEEP
)
122 state
= !!gpio_get_value(led_dat
->gpio
) ^ led_dat
->active_low
;
124 state
= (template->default_state
== LEDS_GPIO_DEFSTATE_ON
);
125 led_dat
->cdev
.brightness
= state
? LED_FULL
: LED_OFF
;
126 if (!template->retain_state_suspended
)
127 led_dat
->cdev
.flags
|= LED_CORE_SUSPENDRESUME
;
129 ret
= gpio_direction_output(led_dat
->gpio
, led_dat
->active_low
^ state
);
133 INIT_WORK(&led_dat
->work
, gpio_led_work
);
135 ret
= led_classdev_register(parent
, &led_dat
->cdev
);
141 gpio_free(led_dat
->gpio
);
145 static void delete_gpio_led(struct gpio_led_data
*led
)
147 if (!gpio_is_valid(led
->gpio
))
149 led_classdev_unregister(&led
->cdev
);
150 cancel_work_sync(&led
->work
);
151 gpio_free(led
->gpio
);
154 #ifdef CONFIG_LEDS_GPIO_PLATFORM
155 static int __devinit
gpio_led_probe(struct platform_device
*pdev
)
157 struct gpio_led_platform_data
*pdata
= pdev
->dev
.platform_data
;
158 struct gpio_led_data
*leds_data
;
164 leds_data
= kzalloc(sizeof(struct gpio_led_data
) * pdata
->num_leds
,
169 for (i
= 0; i
< pdata
->num_leds
; i
++) {
170 ret
= create_gpio_led(&pdata
->leds
[i
], &leds_data
[i
],
171 &pdev
->dev
, pdata
->gpio_blink_set
);
176 platform_set_drvdata(pdev
, leds_data
);
181 for (i
= i
- 1; i
>= 0; i
--)
182 delete_gpio_led(&leds_data
[i
]);
189 static int __devexit
gpio_led_remove(struct platform_device
*pdev
)
192 struct gpio_led_platform_data
*pdata
= pdev
->dev
.platform_data
;
193 struct gpio_led_data
*leds_data
;
195 leds_data
= platform_get_drvdata(pdev
);
197 for (i
= 0; i
< pdata
->num_leds
; i
++)
198 delete_gpio_led(&leds_data
[i
]);
205 static struct platform_driver gpio_led_driver
= {
206 .probe
= gpio_led_probe
,
207 .remove
= __devexit_p(gpio_led_remove
),
210 .owner
= THIS_MODULE
,
214 MODULE_ALIAS("platform:leds-gpio");
215 #endif /* CONFIG_LEDS_GPIO_PLATFORM */
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>
222 struct gpio_led_of_platform_data
{
224 struct gpio_led_data led_data
[];
227 static int __devinit
of_gpio_leds_probe(struct platform_device
*ofdev
,
228 const struct of_device_id
*match
)
230 struct device_node
*np
= ofdev
->dev
.of_node
, *child
;
231 struct gpio_led_of_platform_data
*pdata
;
234 /* count LEDs defined by this device, so we know how much to allocate */
235 for_each_child_of_node(np
, child
)
238 return 0; /* or ENODEV? */
240 pdata
= kzalloc(sizeof(*pdata
) + sizeof(struct gpio_led_data
) * count
,
245 for_each_child_of_node(np
, child
) {
246 struct gpio_led led
= {};
247 enum of_gpio_flags flags
;
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
);
255 state
= of_get_property(child
, "default-state", NULL
);
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
;
262 led
.default_state
= LEDS_GPIO_DEFSTATE_OFF
;
265 ret
= create_gpio_led(&led
, &pdata
->led_data
[pdata
->num_leds
++],
273 dev_set_drvdata(&ofdev
->dev
, pdata
);
278 for (count
= pdata
->num_leds
- 2; count
>= 0; count
--)
279 delete_gpio_led(&pdata
->led_data
[count
]);
286 static int __devexit
of_gpio_leds_remove(struct platform_device
*ofdev
)
288 struct gpio_led_of_platform_data
*pdata
= dev_get_drvdata(&ofdev
->dev
);
291 for (i
= 0; i
< pdata
->num_leds
; i
++)
292 delete_gpio_led(&pdata
->led_data
[i
]);
296 dev_set_drvdata(&ofdev
->dev
, NULL
);
301 static const struct of_device_id of_gpio_leds_match
[] = {
302 { .compatible
= "gpio-leds", },
306 static struct of_platform_driver of_gpio_leds_driver
= {
308 .name
= "of_gpio_leds",
309 .owner
= THIS_MODULE
,
310 .of_match_table
= of_gpio_leds_match
,
312 .probe
= of_gpio_leds_probe
,
313 .remove
= __devexit_p(of_gpio_leds_remove
),
317 static int __init
gpio_led_init(void)
321 #ifdef CONFIG_LEDS_GPIO_PLATFORM
322 ret
= platform_driver_register(&gpio_led_driver
);
326 #ifdef CONFIG_LEDS_GPIO_OF
327 ret
= of_register_platform_driver(&of_gpio_leds_driver
);
329 #ifdef CONFIG_LEDS_GPIO_PLATFORM
331 platform_driver_unregister(&gpio_led_driver
);
337 static void __exit
gpio_led_exit(void)
339 #ifdef CONFIG_LEDS_GPIO_PLATFORM
340 platform_driver_unregister(&gpio_led_driver
);
342 #ifdef CONFIG_LEDS_GPIO_OF
343 of_unregister_platform_driver(&of_gpio_leds_driver
);
347 module_init(gpio_led_init
);
348 module_exit(gpio_led_exit
);
350 MODULE_AUTHOR("Raphael Assenat <raph@8d.com>, Trent Piepho <tpiepho@freescale.com>");
351 MODULE_DESCRIPTION("GPIO LED driver");
352 MODULE_LICENSE("GPL");