2 * LEDs driver for GPIOs
4 * Copyright (C) 2007 8D Technologies inc.
5 * Raphael Assenat <raph@8d.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/kernel.h>
13 #include <linux/init.h>
14 #include <linux/platform_device.h>
15 #include <linux/leds.h>
16 #include <linux/workqueue.h>
20 struct gpio_led_data {
21 struct led_classdev cdev;
23 struct work_struct work;
29 static void gpio_led_work(struct work_struct *work)
31 struct gpio_led_data *led_dat =
32 container_of(work, struct gpio_led_data, work);
34 gpio_set_value_cansleep(led_dat->gpio, led_dat->new_level);
37 static void gpio_led_set(struct led_classdev *led_cdev,
38 enum led_brightness value)
40 struct gpio_led_data *led_dat =
41 container_of(led_cdev, struct gpio_led_data, cdev);
49 if (led_dat->active_low)
52 /* Setting GPIOs with I2C/etc requires a task context, and we don't
53 * seem to have a reliable way to know if we're already in one; so
54 * let's just assume the worst.
56 if (led_dat->can_sleep) {
57 led_dat->new_level = level;
58 schedule_work(&led_dat->work);
60 gpio_set_value(led_dat->gpio, level);
63 static int gpio_led_probe(struct platform_device *pdev)
65 struct gpio_led_platform_data *pdata = pdev->dev.platform_data;
66 struct gpio_led *cur_led;
67 struct gpio_led_data *leds_data, *led_dat;
73 leds_data = kzalloc(sizeof(struct gpio_led_data) * pdata->num_leds,
78 for (i = 0; i < pdata->num_leds; i++) {
79 cur_led = &pdata->leds[i];
80 led_dat = &leds_data[i];
82 ret = gpio_request(cur_led->gpio, cur_led->name);
86 led_dat->cdev.name = cur_led->name;
87 led_dat->cdev.default_trigger = cur_led->default_trigger;
88 led_dat->gpio = cur_led->gpio;
89 led_dat->can_sleep = gpio_cansleep(cur_led->gpio);
90 led_dat->active_low = cur_led->active_low;
91 led_dat->cdev.brightness_set = gpio_led_set;
92 led_dat->cdev.brightness = LED_OFF;
94 gpio_direction_output(led_dat->gpio, led_dat->active_low);
96 INIT_WORK(&led_dat->work, gpio_led_work);
98 ret = led_classdev_register(&pdev->dev, &led_dat->cdev);
100 gpio_free(led_dat->gpio);
105 platform_set_drvdata(pdev, leds_data);
111 for (i = i - 1; i >= 0; i--) {
112 led_classdev_unregister(&leds_data[i].cdev);
113 cancel_work_sync(&leds_data[i].work);
114 gpio_free(leds_data[i].gpio);
123 static int __devexit gpio_led_remove(struct platform_device *pdev)
126 struct gpio_led_platform_data *pdata = pdev->dev.platform_data;
127 struct gpio_led_data *leds_data;
129 leds_data = platform_get_drvdata(pdev);
131 for (i = 0; i < pdata->num_leds; i++) {
132 led_classdev_unregister(&leds_data[i].cdev);
133 cancel_work_sync(&leds_data[i].work);
134 gpio_free(leds_data[i].gpio);
143 static int gpio_led_suspend(struct platform_device *pdev, pm_message_t state)
145 struct gpio_led_platform_data *pdata = pdev->dev.platform_data;
146 struct gpio_led_data *leds_data;
149 leds_data = platform_get_drvdata(pdev);
151 for (i = 0; i < pdata->num_leds; i++)
152 led_classdev_suspend(&leds_data[i].cdev);
157 static int gpio_led_resume(struct platform_device *pdev)
159 struct gpio_led_platform_data *pdata = pdev->dev.platform_data;
160 struct gpio_led_data *leds_data;
163 leds_data = platform_get_drvdata(pdev);
165 for (i = 0; i < pdata->num_leds; i++)
166 led_classdev_resume(&leds_data[i].cdev);
171 #define gpio_led_suspend NULL
172 #define gpio_led_resume NULL
175 static struct platform_driver gpio_led_driver = {
176 .probe = gpio_led_probe,
177 .remove = __devexit_p(gpio_led_remove),
178 .suspend = gpio_led_suspend,
179 .resume = gpio_led_resume,
182 .owner = THIS_MODULE,
186 static int __init gpio_led_init(void)
188 return platform_driver_register(&gpio_led_driver);
191 static void __exit gpio_led_exit(void)
193 platform_driver_unregister(&gpio_led_driver);
196 module_init(gpio_led_init);
197 module_exit(gpio_led_exit);
199 MODULE_AUTHOR("Raphael Assenat <raph@8d.com>");
200 MODULE_DESCRIPTION("GPIO LED driver");
201 MODULE_LICENSE("GPL");